GTK+ 2.0 Tree View Tutorial | ||
---|---|---|
<<< Previous | Next >>> |
This section deals with issues and questions that did not seem to
fit in anywhere else. If you can think of something else that should
be dealt with here, do not hesitate to send a mail to
<tim at centricular dot net>
.
Signal callbacks often only get passed a pointer to a GtkTreeViewColumn when the application programmer really just wants to know which column number was affected. There are two ways to find out the position of a column within the tree view. One way is to write a small helper function that looks up the column number from a given tree view column object, like this for example: [1].
/* Returns column number or -1 if not found or on error */ gint get_col_number_from_tree_view_column (GtkTreeViewColumn *col) { GList *cols; gint num; g_return_val_if_fail ( col != NULL, -1 ); g_return_val_if_fail ( col->tree_view != NULL, -1 ); cols = gtk_tree_view_get_columns(GTK_TREE_VIEW(col->tree_view)); num = g_list_index(cols, (gpointer) col); g_list_free(cols); return num; } |
Alternatively, it is possible to use g_object_set_data and g_object_get_data on the tree view column in order to identify which column it is. This also has the advantage that you can still keep track of your columns even if the columns get re-ordered within the tree view (a feature which is usually disabled though). Use like this:
... enum { COL_FIRSTNAME, COL_SURNAME, }; ... void some_callback (GtkWidget *treeview, ..., GtkTreeViewColumn *col, ...) { guint colnum = GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(col), "columnnum")); ... } void create_view(void) { ... col = gtk_tree_view_column_new(); g_object_set_data(G_OBJECT(col), "columnnum", GUINT_TO_POINTER(COL_FIRSTNAME)); ... col = gtk_tree_view_column_new(); g_object_set_data(G_OBJECT(col), "columnnum", GUINT_TO_POINTER(COL_SURNAME)); ... } |
"columnnum" is a random string in the above example - you can use whatever string you want instead, or store multiple bits of data (with different string identifiers of course). Of course you can also combine both approaches, as they do slightly different things (the first tracks the 'physical' position of a column within the tree view, the second tracks the 'meaning' of a column to you, independent of its position within the view).
[1] | This function has been inspired by this mailing list message (thanks to Ken Rastatter for the link and the topic suggestion). |
<<< Previous | Home | Next >>> |
Editable Spin Button Cells | Column Expander Visibility |