Re: How to align columns in a GtkTreeView widget?



On Tue, 2003-04-22 at 02:13, Boszormenyi Zoltan wrote:
> Do'h!
> 
> Boszormenyi Zoltan wrote:
> 
> > OK, here it is. ;-)

Cool, thank's.
> 
> ______________________________________________________________________
> 
> Q: How to set e.g. 2 decimal digit for a column in a GtkTreeView?
>    (It displays six digits as it is the default for printf("%f", (double)x);)

Maybe:

 Q: How do you change the way that numbers are formatted by GtkTreeView

> A1: If you are using GTK2 version 2.2.1 or older, you should use
>     gtk_tree_view_insert_column_with_data_func() or
>     gtk_tree_view_column_set_cell_data_func().
> 
>     If you don't know how to set up a GtkTreeView, then start reading
>     the GTK2 API documentation at http://developer.gnome.org
> 
>     Short (one column) example follows with two methods.
> 
>     enum {
>       DOUBLE_COLUMN,
>       N_COLUMNS
>     };
> 
>     GtkListStore *mycolumns;
>     GtkTreeView *treeview;
> 
>     void MyCellDouble2Text (GtkTreeViewColumn *tree_column,
> 	GtkCellRenderer *cell, GtkTreeModel *tree_model,
> 	GtkTreeIter *iter, gpointer data)
>     {
>       GtkCellRendererText *cell_text = (GtkCellRendererText *)cell;
>       gdouble d;
>       gchar *s;
> 
>       /* Free the previous (default) text of the column's renderer. */
>       g_free(cell_text->text);
>       /* Get the double value from the model. */
>       gtk_tree_model_get(tree_model, iter, (gint)data, &d, -1);
>       /* Now we can format the value ourselves. */
>       cell_text->text = g_strdup_printf("%.2f", d)

Eeek! You can't do that :-)

        text = g_strdup_printf ("%.2f", d);
        g_object_set (cell, "text", text, NULL);
        g_free (text);

>     /* 1st method. */
>     void set_up_new_columns1(GtkTreeView *myview)
>     {
>       GtkCellRendererText *renderer;
>       GtkTreeViewColumn *column;
> 
>       /* Create the data model and associate it with the given TreeView */
>       mycolumns = gtk_list_store_new (N_COLUMNS, G_TYPE_DOUBLE);
>       gtk_tree_view_set_model(myview, GTK_TREE_MODEL(mycolumns));
> 
>       /* Create a GtkCellRendererText */
>       renderer = gtk_cell_renderer_text_new();
> 
>       /* Create a new column that has a title ("Example column"),
>          uses the above created renderer that will render the double
>          value into text from the associated model's rows. */
>       column = gtk_tree_view_column_new_with_attributes(
> 		"Example column", renderer,
> 		"text", DOUBLE_COLUMN,
> 	  	NULL);

You don't want new_with_attributes() if you are going to use data_func;
they are mutually exclusive.

  column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_title  (column, "File name");
  renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_column_pack_start (column, renderer, TRUE);

>       /* Append the new column after the GtkTreeView's previous columns. */
>       gtk_tree_view_append_column (GTK_TREE_VIEW(myview), column);
>       /* Since we created the column by hand, we can set it up for our
>          needs, e.g. set its minimum and maximum width, etc.
>          ...
>       */
>       /* Set up a custom function that will be called when the column content
>          is rendered. We use the func_data pointer as an index into our
>          model. This is convenient when using multi column lists. */
>       gtk_tree_view_column_set_cell_data_func(column, renderer,
> 	MyCellDouble2Text, (gpointer)DOUBLE_COLUMN, NULL);

(gpointer)DOUBLE_COLUMN will produce a warning on 64-bit platforms
with most compilers. You should always use the GINT_TO_POINTER()
GPOINTER_TO_INT() when storing an integer in a pointer.

>     /* 2nd method */
>     void set_up_new_columns2(GtkTreeView *myview)

I think the FAQ entry would be clearer with only one method.
I prefer the above since it is more extensible. You don't
have to learn something new when you want to add multiple
renderers to a single column, set the sort column id,
etc.

>     Some notes:
> 
>     You can create the GtkTreeView by hand with gtk_tree_view_new()
>     or get a pointer to an existing one with lookup_widget()
>     if you use Glade.
> 
>     As you can read in the API documentation, there is a "func_data"
>     that can be used by the programmer, and there is a destroy notification
>     function, too. We gave NULL for the destroy notification function
>     because the func_data is a static number for the column in our example.
>     For every column that has similar (or the same) formatting, you can use
>     the same GtkCellRendererText and the same callback function for
>     formatting. To get the correctly displayed numbers, every column
>     can use the proper column index in the model and the above given
>     MyCellDouble2Text() function will work without changes. For different
>     data types and different formatting, you can still use the same
>     callback function for customized columns and inside your function
>     you can use the (gint)data value to differentiate between the columns.

You _could_, but it doesn't make a lot of sense to me to do:

 switch (column_id)
  {
    case DOUBE_COLUMN:
      handle_a_double;
      break;
    case INT_COLUMN;
      handle_an_int;
     break;
  }

rather than just using separate functions.

Regards,
                                                  Owen





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]