Columns vs fields in GtkTreeView



I couldn't see this in the archives, so here goes:

I believe that the current tree/list API is unnecessarily hard to
understand because of the potential for confusion between columns in the
view and columns in the model.

So I propose that the "columns" in the model (and the various store
classes) get renamed to "field".  That way "columns" only refers to
columns in the view.  I've been using this in my own code, and it
clarifies things when you have more than one column in the model per
column in the view, and when you have "hidden columns" - it's suddenly a
lot clearer that these two concepts called "column" aren't the same
thing.

Obviously this would be a pain of an API change (maybe for GTK 3?).  So,
as a simpler change, we could merely update the docs and sample code to
use this terminology.

Here's the sample code from the docs adapted to use the proposed new
terminology:

enum
{
   TITLE_FIELD,
   AUTHOR_FIELD,
   CHECKED_FIELD,
   N_FIELDS
};

void
populate_tree_model (GtkTreeStore *store)
{
	GtkTreeIter iter1;  /* Parent iter */
	GtkTreeIter iter2;  /* Child iter  */

	gtk_tree_store_append (store, &iter1, NULL);  /* Acquire a top-level iterator */
	gtk_tree_store_set (store, &iter1,
        	            TITLE_FIELD, "The Art of Computer Programming",
        	            AUTHOR_FIELD, "Donald E. Knuth",
        	            CHECKED_FIELD, FALSE,
        	            -1);

	gtk_tree_store_append (store, &iter2, &iter1);  /* Acquire a child iterator */
	gtk_tree_store_set (store, &iter2,
        	            TITLE_FIELD, "Volume 1: Fundamental Algorithms",
	                    -1);

	gtk_tree_store_append (store, &iter2, &iter1);
	gtk_tree_store_set (store, &iter2,
        	            TITLE_FIELD, "Volume 2: Seminumerical Algorithms",
	                    -1);

	gtk_tree_store_append (store, &iter2, &iter1);
	gtk_tree_store_set (store, &iter2,
        	            TITLE_FIELD, "Volume 3: Sorting and Searching",
	                    -1);
}

void
setup_tree (void)
{
   GtkTreeStore *store;
   GtkWidget *tree;
   GtkTreeViewColumn *column;
   GtkCellRenderer *renderer;

   /* Create a model.  We are using the store model for now, though we
    * could use any other GtkTreeModel */
   store = gtk_tree_store_new (N_FIELDS,
                               G_TYPE_STRING,
                               G_TYPE_STRING,
                               G_TYPE_BOOLEAN);

   /* custom function to fill the model with data */
   populate_tree_model (store);

   /* Create a view */
   tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));

   /* The view now holds a reference.  We can get rid of our own
    * reference */
   g_object_unref (G_OBJECT (store));

   /* Create a cell render and arbitrarily make it red for demonstration
    * purposes */
   renderer = gtk_cell_renderer_text_new ();
   g_object_set (G_OBJECT (renderer),
                 "foreground", "red",
                 NULL);

   /* Create a column, associating the "text" attribute of the
    * cell_renderer to the first field of the model */
   column = gtk_tree_view_column_new_with_attributes ("Author", renderer,
                                                      "text", AUTHOR_FIELD,
                                                      NULL);

   /* Add the column to the view. */
   gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);

   /* Second column.. title of the book. */
   renderer = gtk_cell_renderer_text_new ();
   column = gtk_tree_view_column_new_with_attributes ("Title",
                                                      renderer,
                                                      "text", TITLE_FIELD,
                                                      NULL);
   gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);

   /* Last column.. whether a book is checked out. */
   renderer = gtk_cell_renderer_toggle_new ();
   column = gtk_tree_view_column_new_with_attributes ("Checked out",
                                                      renderer,
                                                      "active", CHECKED_FIELD,
                                                      NULL);
   gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);

   /* Now we can manipulate the view just like any other GTK widget */
   ...
}

Thoughts?  Do other people think this is better?  Should I make a patch for the docs and samples?  






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