1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 The tree interface used by #GtkTreeView
7 <!-- ##### SECTION Long_Description ##### -->
9 The #GtkTreeModel interface defines a generic tree interface for use by
10 the #GtkTreeView widget. It is an abstract interface, and is designed
11 to be usable with any appropriate data structure. The programmer just
12 has to implement this interface on their own data type for it to be
13 viewable by a #GtkTreeView widget.
17 The model is represented as a hierarchical tree of strongly-typed,
18 columned data. In other words, the model can be seen as a tree where
19 every node has different values depending on which column is being
20 queried. The type of data found in a column is determined by using the
21 GType system (ie. #G_TYPE_INT, #GTK_TYPE_BUTTON, #G_TYPE_POINTER, etc.).
22 The types are homogeneous per column across all nodes. It is important
23 to note that this interface only provides a way of examining a model and
24 observing changes. The implementation of each individual model decides
25 how and if changes are made.
29 In order to make life simpler for programmers who do not need to write
30 their own specialized model, two generic models are provided — the
31 #GtkTreeStore and the #GtkListStore. To use these, the developer simply
32 pushes data into these models as necessary. These models provide the
33 data structure as well as all appropriate tree interfaces. As a result,
34 implementing drag and drop, sorting, and storing data is trivial. For
35 the vast majority of trees and lists, these two models are sufficient.
39 Models are accessed on a node/column level of granularity. One can
40 query for the value of a model at a certain node and a certain column
41 on that node. There are two structures used to reference a particular
42 node in a model. They are the #GtkTreePath and the #GtkTreeIter
45 Here, <abbrev>iter</abbrev> is short for <quote>iterator</quote>
48 Most of the interface consists of operations on a #GtkTreeIter.
52 A path is essentially a potential node. It is a location on a model
53 that may or may not actually correspond to a node on a specific model.
54 The #GtkTreePath struct can be converted into either an array of
55 unsigned integers or a string. The string form is a list of numbers
56 separated by a colon. Each number refers to the offset at that level.
57 Thus, the path <quote>0</quote> refers to the root node and the path
58 <quote>2:4</quote> refers to the fifth child of the third node.
62 By contrast, a #GtkTreeIter is a reference to a specific node on a
63 specific model. It is a generic struct with an integer and three
64 generic pointers. These are filled in by the model in a model-specific
65 way. One can convert a path to an iterator by calling
66 gtk_tree_model_get_iter(). These iterators are the primary way of
67 accessing a model and are similar to the iterators used by
68 #GtkTextBuffer. They are generally statically allocated on the heap and
69 only used for a short time. The model interface defines a set of
70 operations using them for navigating the model.
74 It is expected that models fill in the iterator with private data. For
75 example, the #GtkListStore model, which is internally a simple linked
76 list, stores a list node in one of the pointers. The #GtkTreeModelSort
77 stores an array and an offset in two of the pointers. Additionally,
78 there is an integer field. This field is generally filled with a unique
79 stamp per model. This stamp is for catching errors resulting from using
80 invalid iterators with a model.
84 The lifecycle of an iterator can be a little confusing at first.
85 Iterators are expected to always be valid for as long as the model is
86 unchanged (and doesn't emit a signal). The model is considered to own
87 all outstanding iterators and nothing needs to be done to free them from
88 the user's point of view. Additionally, some models guarantee that an
89 iterator is valid for as long as the node it refers to is valid (most
90 notably the #GtkTreeStore and #GtkListStore). Although generally
91 uninteresting, as one always has to allow for the case where iterators
92 do not persist beyond a signal, some very important performance
93 enhancements were made in the sort model. As a result, the
94 #GTK_TREE_MODEL_ITERS_PERSIST flag was added to indicate this behavior.
98 To help show some common operation of a model, some examples are
99 provided. The first example shows three ways of getting the iter at the
100 location <quote>3:2:5</quote>. While the first method shown is easier,
101 the second is much more common, as you often get paths from callbacks.
105 <title>Acquiring a <structname>GtkTreeIter</structname></title>
107 /* Three ways of getting the iter pointing to the location
112 GtkTreeIter parent_iter;
114 /* get the iterator from a string */
115 gtk_tree_model_get_iter_from_string (model, &iter, "3:2:5");
117 /* get the iterator from a path */
118 path = gtk_tree_path_new_from_string ("3:2:5");
119 gtk_tree_model_get_iter (model, &iter, path);
120 gtk_tree_path_free (path);
123 /* walk the tree to find the iterator */
124 gtk_tree_model_get_nth_child (model, &iter, NULL, 3);
126 gtk_tree_model_get_nth_child (model, &iter, &parent_iter, 2);
128 gtk_tree_model_get_nth_child (model, &iter, NULL, 5);
135 This second example shows a quick way of iterating through a list and
136 getting a string and an integer from each row. The
137 <function>populate_model</function> function used below is not shown, as
138 it is specific to the #GtkListStore. For information on how to write
139 such a function, see the #GtkListStore documentation.
141 <title>Reading data from a <structname>GtkTreeModel</structname></title>
151 GtkTreeModel *list_store;
156 /* make a new list_store */
157 list_store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_INT);
159 /* Fill the list store with data */
160 populate_model (list_store);
162 /* Get the first iter in the list */
163 valid = gtk_tree_model_get_iter_first (model, &iter);
167 /* Walk through the list, reading each row */
171 /* Make sure you terminate calls to gtk_tree_model_get(<!-- -->)
174 gtk_tree_model_get (list_store, &iter,
175 STRING_COLUMN, &str_data,
176 INT_COLUMN, &int_data,
179 /* Do something with the data */
180 g_print ("Row &percent;d: (&percent;s,&percent;d)\n", row_count, str_data, int_data);
184 valid = gtk_tree_model_iter_next (model, &iter))
191 <!-- ##### SECTION See_Also ##### -->
193 #GtkTreeView, #GtkTreeStore, #GtkListStore, <link linkend="gtk-GtkTreeView-drag-and-drop">GtkTreeDnd</link>, #GtkTreeSortable
196 <!-- ##### STRUCT GtkTreeModel ##### -->
202 <!-- ##### STRUCT GtkTreeIter ##### -->
204 The <structname>GtkTreeIter</structname> is the primary structure for
205 accessing a structure. Models are expected to put a unique integer in
206 the <structfield>stamp</structfield> member, and put model-specific
207 data in the three <structfield>user_data</structfield> members.
210 @stamp: A unique stamp to catch invalid iterators
211 @user_data: Model specific data
212 @user_data2: Model specific data
213 @user_data3: Model specific data
215 <!-- ##### STRUCT GtkTreePath ##### -->
221 <!-- ##### STRUCT GtkTreeRowReference ##### -->
227 <!-- ##### STRUCT GtkTreeModelIface ##### -->
235 @row_has_child_toggled:
253 <!-- ##### USER_FUNCTION GtkTreeModelForeachFunc ##### -->
265 <!-- ##### ENUM GtkTreeModelFlags ##### -->
267 These flags indicate various properties of a #GtkTreeModel. They are
268 returned by gtk_tree_model_get_flags(), and must be static for the
269 lifetime of the object. A more complete description of
270 #GTK_TREE_MODEL_ITERS_PERSIST can be found in the overview of this
274 @GTK_TREE_MODEL_ITERS_PERSIST: Iterators survive all signals emitted by the tree.
275 @GTK_TREE_MODEL_LIST_ONLY: The model is a list only, and never has children
277 <!-- ##### FUNCTION gtk_tree_path_new ##### -->
285 <!-- ##### FUNCTION gtk_tree_path_new_from_string ##### -->
294 <!-- ##### FUNCTION gtk_tree_path_to_string ##### -->
303 <!-- ##### FUNCTION gtk_tree_path_new_first ##### -->
311 <!-- ##### MACRO gtk_tree_path_new_root ##### -->
313 A alternate name for gtk_tree_path_new_root() provided for
314 compatibility reasons; this macro will be deprecated in future
318 @Returns: A new #GtkTreePath.
321 <!-- ##### FUNCTION gtk_tree_path_append_index ##### -->
330 <!-- ##### FUNCTION gtk_tree_path_prepend_index ##### -->
339 <!-- ##### FUNCTION gtk_tree_path_get_depth ##### -->
348 <!-- ##### FUNCTION gtk_tree_path_get_indices ##### -->
357 <!-- ##### FUNCTION gtk_tree_path_free ##### -->
365 <!-- ##### FUNCTION gtk_tree_path_copy ##### -->
374 <!-- ##### FUNCTION gtk_tree_path_compare ##### -->
384 <!-- ##### FUNCTION gtk_tree_path_next ##### -->
392 <!-- ##### FUNCTION gtk_tree_path_prev ##### -->
401 <!-- ##### FUNCTION gtk_tree_path_up ##### -->
410 <!-- ##### FUNCTION gtk_tree_path_down ##### -->
418 <!-- ##### FUNCTION gtk_tree_path_is_ancestor ##### -->
428 <!-- ##### FUNCTION gtk_tree_path_is_descendant ##### -->
438 <!-- ##### FUNCTION gtk_tree_row_reference_new ##### -->
448 <!-- ##### FUNCTION gtk_tree_row_reference_new_proxy ##### -->
459 <!-- ##### FUNCTION gtk_tree_row_reference_get_path ##### -->
468 <!-- ##### FUNCTION gtk_tree_row_reference_valid ##### -->
477 <!-- ##### FUNCTION gtk_tree_row_reference_free ##### -->
485 <!-- ##### FUNCTION gtk_tree_row_reference_inserted ##### -->
494 <!-- ##### FUNCTION gtk_tree_row_reference_deleted ##### -->
503 <!-- ##### FUNCTION gtk_tree_row_reference_reordered ##### -->
514 <!-- ##### FUNCTION gtk_tree_iter_copy ##### -->
523 <!-- ##### FUNCTION gtk_tree_iter_free ##### -->
531 <!-- ##### FUNCTION gtk_tree_model_get_flags ##### -->
540 <!-- ##### FUNCTION gtk_tree_model_get_n_columns ##### -->
549 <!-- ##### FUNCTION gtk_tree_model_get_column_type ##### -->
559 <!-- ##### FUNCTION gtk_tree_model_get_iter ##### -->
570 <!-- ##### FUNCTION gtk_tree_model_get_iter_from_string ##### -->
581 <!-- ##### FUNCTION gtk_tree_model_get_iter_first ##### -->
591 <!-- ##### MACRO gtk_tree_model_get_iter_root ##### -->
593 A alternate name for gtk_tree_model_get_iter_root() provided for
594 compatibility reasons; this macro will be deprecated in future
598 @tree_model: A #GtkTreeModel.
599 @iter: uninitialized #GtkTreeIter.
600 @Returns: %TRUE, if @iter was set.
603 <!-- ##### FUNCTION gtk_tree_model_get_path ##### -->
613 <!-- ##### FUNCTION gtk_tree_model_get_value ##### -->
624 <!-- ##### FUNCTION gtk_tree_model_iter_next ##### -->
634 <!-- ##### FUNCTION gtk_tree_model_iter_children ##### -->
645 <!-- ##### FUNCTION gtk_tree_model_iter_has_child ##### -->
655 <!-- ##### FUNCTION gtk_tree_model_iter_n_children ##### -->
665 <!-- ##### FUNCTION gtk_tree_model_iter_nth_child ##### -->
677 <!-- ##### FUNCTION gtk_tree_model_iter_parent ##### -->
688 <!-- ##### FUNCTION gtk_tree_model_ref_node ##### -->
697 <!-- ##### FUNCTION gtk_tree_model_unref_node ##### -->
706 <!-- ##### FUNCTION gtk_tree_model_get ##### -->
716 <!-- ##### FUNCTION gtk_tree_model_get_valist ##### -->
726 <!-- ##### FUNCTION gtk_tree_model_foreach ##### -->
736 <!-- ##### FUNCTION gtk_tree_model_row_changed ##### -->
744 <!-- # Unused Parameters # -->
749 <!-- ##### FUNCTION gtk_tree_model_row_inserted ##### -->
759 <!-- ##### FUNCTION gtk_tree_model_row_has_child_toggled ##### -->
769 <!-- ##### FUNCTION gtk_tree_model_row_deleted ##### -->
778 <!-- ##### FUNCTION gtk_tree_model_rows_reordered ##### -->
789 <!-- ##### SIGNAL GtkTreeModel::row-changed ##### -->
794 @treemodel: the object which received the signal.
798 <!-- ##### SIGNAL GtkTreeModel::row-deleted ##### -->
803 @treemodel: the object which received the signal.
806 <!-- ##### SIGNAL GtkTreeModel::row-has-child-toggled ##### -->
811 @treemodel: the object which received the signal.
815 <!-- ##### SIGNAL GtkTreeModel::row-inserted ##### -->
820 @treemodel: the object which received the signal.
824 <!-- ##### SIGNAL GtkTreeModel::rows-reordered ##### -->
829 @treemodel: the object which received the signal.