| GTK+ Reference Manual | |||
|---|---|---|---|
| <<< Previous Page | Home | Up | Next Page >>> |
To create a tree or list in GTK+, you need to use the GtkTreeModel interface, in conjunction with the GtkTreeView widget.
This widget is designed around a Model/View/Controller design and consists of four major parts:
| the tree view widget (GtkTreeView) |
| the view column (GtkTreeViewColumn) |
| the cell renderers (GtkCellRenderer etc.) |
| and the model interface (GtkTreeModel) |
Here is a simple example of using a GtkTreeView widget in context of the other widgets. It simply creates a simple model and view, and puts them together. Note that the model is never populated with data — that is left as an exercise for the reader. More information can be found on this in the GtkTreeModel section.
enum
{
COLUMN_ONE,
N_COLUMNS
};
{
GtkTreeStore *model;
GtkWidget *view;
GtkTreeViewColumn *column;
GtkCellRenderer *cell_renderer;
/* Create a model. We are using the store model for now, though we
* could use any other GtkTreeModel */
model = gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING);
/* custom function to fill the model with data */
populate_tree_model (model);
/* Create a view */
view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
/* The view now holds a reference. We can get rid of our own
* reference */
g_object_unref (G_OBJECT (model));
/* Create a cell render and arbitrarily make it red for demonstration
* purposes */
cell_renderer = gtk_cell_renderer_text_new ();
g_object_set (G_OBJECT (cell_renderer), "foreground", "red", NULL);
/* Create a column, associating the "text" attribute of the
* cell_renderer to the first column of the model */
column = gtk_tree_view_column_new_with_attributes ("title",
cell_renderer,
"text", COLUMN_ONE,
NULL);
/* Add the column to the view. */
gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
/* Now we can manipulate the view just like any other GTK widget */
...
}
|