1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 A GtkTreeModel which makes an underlying tree model sortable
7 <!-- ##### SECTION Long_Description ##### -->
9 The #GtkTreeModelSort is a model which implements the #GtkTreeSortable
10 interface. It does not hold any data itself, but rather is created with
11 a child model and proxies its data. It has identical column types to
12 this child model, and the changes in the child are propagated. The
13 primary purpose of this model is to provide a way to sort a different
14 model without modifying it. Note that the sort function used by
15 #GtkTreeModelSort is not guaranteed to be stable.
18 The use of this is best demonstrated through an example. In the
19 following sample code we create two #GtkTreeView widgets each with a
20 view of the same data. As the model is wrapped here by a
21 #GtkTreeModelSort, the two #GtkTreeView<!-- -->s can each sort their
22 view of the data without affecting the other. By contrast, if we
23 simply put the same model in each widget, then sorting the first would
28 <title>Using a <structname>GtkTreeModelSort</structname></title>
31 GtkTreeView *tree_view1;
32 GtkTreeView *tree_view2;
33 GtkTreeModel *sort_model1;
34 GtkTreeModel *sort_model2;
35 GtkTreeModel *child_model;
37 /* get the child model */
38 child_model = get_my_model ();
40 /* Create the first tree */
41 sort_model1 = gtk_tree_model_sort_new_with_model (child_model);
42 tree_view1 = gtk_tree_view_new_with_model (sort_model1);
44 /* Create the second tree */
45 sort_model2 = gtk_tree_model_sort_new_with_model (child_model);
46 tree_view2 = gtk_tree_view_new_with_model (sort_model2);
48 /* Now we can sort the two models independently */
49 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort_model1),
50 COLUMN_1, GTK_SORT_ASCENDING);
51 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort_model2),
52 COLUMN_1, GTK_SORT_DESCENDING);
59 To demonstrate how to access the underlying child model from the sort
60 model, the next example will be a callback for the #GtkTreeSelection
61 "changed" signal. In this callback, we get a string from COLUMN_1 of
62 the model. We then modify the string, find the same selected row on the
63 child model, and change the row there.
68 <title>Accessing the child model of in a selection changed callback</title>
71 selection_changed (GtkTreeSelection *selection, gpointer data)
73 GtkTreeModel *sort_model = NULL;
74 GtkTreeModel *child_model;
75 GtkTreeIter sort_iter;
76 GtkTreeIter child_iter;
77 char *some_data = NULL;
80 /* Get the current selected row and the model. */
81 if (! gtk_tree_selection_get_selected (selection,
87 /* Look up the current value on the selected row and get a new value
90 gtk_tree_model_get (GTK_TREE_MODEL (sort_model), &sort_iter,
91 COLUMN_1, &some_data,
94 modified_data = change_the_data (some_data);
97 /* Get an iterator on the child model, instead of the sort model. */
98 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (sort_model),
102 /* Get the child model and change the value of the row. In this
103 * example, the child model is a GtkListStore. It could be any other
104 * type of model, though.
106 child_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model));
107 gtk_list_store_set (GTK_LIST_STORE (child_model), &child_iter,
108 COLUMN_1, &modified_data,
110 g_free (modified_data);
116 <!-- ##### SECTION See_Also ##### -->
118 #GtkTreeModel, #GtkListStore, #GtkTreeStore, #GtkTreeSortable, #GtkTreeModelFilter
121 <!-- ##### SECTION Stability_Level ##### -->
124 <!-- ##### SECTION Image ##### -->
127 <!-- ##### STRUCT GtkTreeModelSort ##### -->
129 This should not be accessed directly. Use the accessor functions below.
133 <!-- ##### ARG GtkTreeModelSort:model ##### -->
138 <!-- ##### FUNCTION gtk_tree_model_sort_new_with_model ##### -->
147 <!-- ##### FUNCTION gtk_tree_model_sort_get_model ##### -->
156 <!-- ##### FUNCTION gtk_tree_model_sort_convert_child_path_to_path ##### -->
166 <!-- ##### FUNCTION gtk_tree_model_sort_convert_child_iter_to_iter ##### -->
177 <!-- ##### FUNCTION gtk_tree_model_sort_convert_path_to_child_path ##### -->
187 <!-- ##### FUNCTION gtk_tree_model_sort_convert_iter_to_child_iter ##### -->
197 <!-- ##### FUNCTION gtk_tree_model_sort_reset_default_sort_func ##### -->
205 <!-- ##### FUNCTION gtk_tree_model_sort_clear_cache ##### -->
213 <!-- ##### FUNCTION gtk_tree_model_sort_iter_is_valid ##### -->