Re: How to align columns in a GtkTreeView widget?



Do'h!

Boszormenyi Zoltan wrote:

OK, here it is. ;-)


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);)

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);
    }

    /* 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);
      /* 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);
    }

    /* 2nd method */
    void set_up_new_columns2(GtkTreeView *myview)
    {
      GtkCellRendererText *renderer;
      GtkTreeViewColumn *column;
      gint i;

      /* 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();

      /* This creates the 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, associates it
         with the custom formatting function and appends it at the end
         of the GtkTreeView in one go.
         Variable i contains the number of columns in the GtkTreeView. */
      i = gtk_tree_view_insert_column_with_data_func(
         myview, -1, "Example column", renderer,
         MyCellDouble2Text, (gpointer)DOUBLE_COLUMN, NULL);
      /* Since we didn't created the column by hand, we can get
         a pointer to it so we can set it up for our needs,
         e.g. set its minimum and maximum width, etc.
         ...
      */
      column = gtk_tree_view_get_column(myview, i-1);
      /* You should at least set which column of the model it should use
         to get the data from. */
      gtk_tree_view_column_set_attributes(column, renderer,
         "text", DOUBLE_COLUMN,
         NULL);
    }

    Both methods work equally well, choose one as your taste dictates.

    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.

A2 (fiction):
    If you use GTK2 version 2.4.0 or newer, you can use "number", "number-set"
    and "number-format" properties of the GtkCellRendererText.


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