1
<refentry id="TreeWidget" revision="20 Mar 2002">
3
<refentrytitle>Tree and List Widget Overview</refentrytitle>
4
<manvolnum>3</manvolnum>
5
<refmiscinfo>GTK Library</refmiscinfo>
9
<refname>Tree and List Widget Overview</refname>
10
<refpurpose>Overview of #GtkTreeModel, #GtkTreeView, and other
11
associated widgets</refpurpose>
15
<title>Overview</title>
17
To create a tree or list in GTK+, use the #GtkTreeModel interface in
18
conjunction with the #GtkTreeView widget. This widget is
19
designed around a <firstterm>Model/View/Controller</firstterm>
20
design and consists of four major parts:
22
<member>The tree view widget (<structname>GtkTreeView</structname>)</member>
23
<member>The view column (<structname>GtkTreeViewColumn</structname>)</member>
24
<member>The cell renderers (<structname>GtkCellRenderer</structname> etc.)</member>
25
<member>The model interface (<structname>GtkTreeModel</structname>)</member>
27
The <emphasis>View</emphasis> is composed of the first three
28
objects, while the last is the <emphasis>Model</emphasis>. One
29
of the prime benefits of the MVC design is that multiple views
30
can be created of a single model. For example, a model mapping
31
the file system could be created for a file manager. Many views
32
could be created to display various parts of the file system,
33
but only one copy need be kept in memory.
36
The purpose of the cell renderers is to provide extensibility to the
37
widget and to allow multiple ways of rendering the same type of data.
38
For example, consider how to render a boolean variable. Should it
39
render it as a string of "True" or "False", "On" or "Off", or should
40
it be rendered as a checkbox?
44
<title>Creating a model</title>
46
GTK+ provides two simple models that can be used: the #GtkListStore
47
and the #GtkTreeStore. GtkListStore is used to model list widgets,
48
while the GtkTreeStore models trees. It is possible to develop a new
49
type of model, but the existing models should be satisfactory for all
50
but the most specialized of situations. Creating the model is quite
53
<informalexample><programlisting><![CDATA[
54
GtkListStore *store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
55
]]></programlisting></informalexample>
57
This creates a list store with two columns: a string column and a boolean
58
column. Typically the 2 is never passed directly like that; usually an
59
enum is created wherein the different columns are enumerated, followed by
60
a token that represents the total number of columns. The next example will
61
illustrate this, only using a tree store instead of a list store. Creating
62
a tree store operates almost exactly the same.
64
<informalexample><programlisting><![CDATA[
73
GtkTreeStore *store = gtk_tree_store_new (N_COLUMNS, /* Total number of columns */
74
G_TYPE_STRING, /* Book title */
75
G_TYPE_STRING, /* Author */
76
G_TYPE_BOOLEAN); /* Is checked out? */
77
]]></programlisting></informalexample>
79
Adding data to the model is done using gtk_tree_store_set() or
80
gtk_list_store_set(), depending upon which sort of model was
81
created. To do this, a #GtkTreeIter must be acquired. The iterator
82
points to the location where data will be added.
85
Once an iterator has been acquired, gtk_tree_store_set() is used to
86
apply data to the part of the model that the iterator points to.
87
Consider the following example:
89
<informalexample><programlisting><![CDATA[
92
gtk_tree_store_append (store, &iter, NULL); /* Acquire an iterator */
94
gtk_tree_store_set (store, &iter,
95
TITLE_COLUMN, "The Principle of Reason",
96
AUTHOR_COLUMN, "Martin Heidegger",
97
CHECKED_COLUMN, FALSE,
99
]]></programlisting></informalexample>
102
Notice that the last argument is -1. This is always done because
103
this is a variable-argument function and it needs to know when to stop
104
processing arguments. It can be used to set the data in any or all
105
columns in a given row.
108
The third argument to gtk_tree_store_append() is the parent iterator. It
109
is used to add a row to a GtkTreeStore as a child of an existing row. This
110
means that the new row will only be visible when its parent is visible and
111
in its expanded state. Consider the following example:
113
<informalexample><programlisting><![CDATA[
114
GtkTreeIter iter1; /* Parent iter */
115
GtkTreeIter iter2; /* Child iter */
117
gtk_tree_store_append (store, &iter1, NULL); /* Acquire a top-level iterator */
118
gtk_tree_store_set (store, &iter1,
119
TITLE_COLUMN, "The Art of Computer Programming",
120
AUTHOR_COLUMN, "Donald E. Knuth",
121
CHECKED_COLUMN, FALSE,
124
gtk_tree_store_append (store, &iter2, &iter1); /* Acquire a child iterator */
125
gtk_tree_store_set (store, &iter2,
126
TITLE_COLUMN, "Volume 1: Fundamental Algorithms",
129
gtk_tree_store_append (store, &iter2, &iter1);
130
gtk_tree_store_set (store, &iter2,
131
TITLE_COLUMN, "Volume 2: Seminumerical Algorithms",
134
gtk_tree_store_append (store, &iter2, &iter1);
135
gtk_tree_store_set (store, &iter2,
136
TITLE_COLUMN, "Volume 3: Sorting and Searching",
138
]]></programlisting></informalexample>
142
<title>Creating the view component</title>
144
While there are several different models to choose from, there is
145
only one view widget to deal with. It works with either the list
146
or the tree store. Setting up a #GtkTreeView is not a difficult
147
matter. It needs a #GtkTreeModel to know where to retrieve its data
150
<informalexample><programlisting><![CDATA[
153
tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
154
]]></programlisting></informalexample>
157
<title>Columns and cell renderers</title>
159
Once the #GtkTreeView widget has a model, it will need to know how
160
to display the model. It does this with columns and cell renderers.
163
Cell renderers are used to draw the data in the tree model in a
164
way. There are a number of cell renderers that come with GTK+ 2.x,
165
including the #GtkCellRendererText, #GtkCellRendererPixbuf and
166
the #GtkCellRendererToggle.
167
It is relatively easy to write a custom renderer.
170
A #GtkTreeViewColumn is the object that GtkTreeView uses to organize
171
the vertical columns in the tree view. It needs to know the name of
172
the column to label for the user, what type of cell renderer to use,
173
and which piece of data to retrieve from the model for a given row.
175
<informalexample><programlisting>
176
GtkCellRenderer *renderer;
177
GtkTreeViewColumn *column;
179
renderer = gtk_cell_renderer_text_new (<!-- -->);
180
column = gtk_tree_view_column_new_with_attributes ("Author",
182
"text", AUTHOR_COLUMN,
184
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
185
</programlisting></informalexample>
187
At this point, all the steps in creating a displayable tree have been
188
covered. The model is created, data is stored in it, a tree view is
189
created and columns are added to it.
194
<title>Selection handling</title>
196
Most applications will need to not only deal with displaying data, but
197
also receiving input events from users. To do this, simply get a
198
reference to a selection object and connect to the "changed" signal.
200
<informalexample><programlisting><![CDATA[
201
/* Prototype for selection handler callback */
202
static void tree_selection_changed_cb (GtkTreeSelection *selection, gpointer data);
204
/* Setup the selection handler */
205
GtkTreeSelection *select;
207
select = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));
208
gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
209
g_signal_connect (G_OBJECT (select), "changed",
210
G_CALLBACK (tree_selection_changed_cb),
212
]]></programlisting></informalexample>
214
Then to retrieve data for the row selected:
216
<informalexample><programlisting><![CDATA[
218
tree_selection_changed_cb (GtkTreeSelection *selection, gpointer data)
224
if (gtk_tree_selection_get_selected (selection, &model, &iter))
226
gtk_tree_model_get (model, &iter, AUTHOR_COLUMN, &author, -1);
228
g_print ("You selected a book by %s\n", author);
233
]]></programlisting></informalexample>
238
<title>Simple Example</title>
240
Here is a simple example of using a #GtkTreeView widget in context
241
of the other widgets. It simply creates a simple model and view,
242
and puts them together. Note that the model is never populated
243
with data — that is left as an exercise for the reader.
244
More information can be found on this in the #GtkTreeModel section.
245
<informalexample><programlisting>
259
GtkTreeViewColumn *column;
260
GtkCellRenderer *renderer;
262
/* Create a model. We are using the store model for now, though we
263
* could use any other GtkTreeModel */
264
store = gtk_tree_store_new (N_COLUMNS,
269
/* custom function to fill the model with data */
270
populate_tree_model (store);
273
tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
275
/* The view now holds a reference. We can get rid of our own
277
g_object_unref (G_OBJECT (store));
279
/* Create a cell render and arbitrarily make it red for demonstration
281
renderer = gtk_cell_renderer_text_new (<!-- -->);
282
g_object_set (G_OBJECT (renderer),
286
/* Create a column, associating the "text" attribute of the
287
* cell_renderer to the first column of the model */
288
column = gtk_tree_view_column_new_with_attributes ("Author", renderer,
289
"text", AUTHOR_COLUMN,
292
/* Add the column to the view. */
293
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
295
/* Second column.. title of the book. */
296
renderer = gtk_cell_renderer_text_new (<!-- -->);
297
column = gtk_tree_view_column_new_with_attributes ("Title",
299
"text", TITLE_COLUMN,
301
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
303
/* Last column.. whether a book is checked out. */
304
renderer = gtk_cell_renderer_toggle_new (<!-- -->);
305
column = gtk_tree_view_column_new_with_attributes ("Checked out",
307
"active", CHECKED_COLUMN,
309
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
311
/* Now we can manipulate the view just like any other GTK widget */
314
</programlisting></informalexample>