2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include <gobject/gvaluecollector.h>
23 #include "gtktreemodel.h"
24 #include "gtktreestore.h"
25 #include "gtktreedatalist.h"
26 #include "gtktreednd.h"
27 #include "gtkbuildable.h"
32 * SECTION:gtktreestore
33 * @Short_description: A tree-like data structure that can be used with the GtkTreeView
34 * @Title: GtkTreeStore
35 * @See_also: #GtkTreeModel
37 * The #GtkTreeStore object is a list model for use with a #GtkTreeView
38 * widget. It implements the #GtkTreeModel interface, and consequentialy,
39 * can use all of the methods available there. It also implements the
40 * #GtkTreeSortable interface so it can be sorted by the view. Finally,
41 * it also implements the tree <link linkend="gtktreednd">drag and
42 * drop</link> interfaces.
44 * <refsect2 id="GtkTreeStore-BUILDER-UI">
45 * <title>GtkTreeStore as GtkBuildable</title>
46 * The GtkTreeStore implementation of the #GtkBuildable interface allows
47 * to specify the model columns with a <columns> element that may
48 * contain multiple <column> elements, each specifying one model
49 * column. The "type" attribute specifies the data type for the column.
51 * <title>A UI Definition fragment for a tree store</title>
52 * <programlisting><![CDATA[
53 * <object class="GtkTreeStore">
55 * <column type="gchararray"/>
56 * <column type="gchararray"/>
57 * <column type="gint"/>
60 * ]]></programlisting>
66 #define G_NODE(node) ((GNode *)node)
67 #define GTK_TREE_STORE_IS_SORTED(tree) (((GtkTreeStore*)(tree))->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
68 #define VALID_ITER(iter, tree_store) ((iter)!= NULL && (iter)->user_data != NULL && ((GtkTreeStore*)(tree_store))->stamp == (iter)->stamp)
70 static void gtk_tree_store_tree_model_init (GtkTreeModelIface *iface);
71 static void gtk_tree_store_drag_source_init(GtkTreeDragSourceIface *iface);
72 static void gtk_tree_store_drag_dest_init (GtkTreeDragDestIface *iface);
73 static void gtk_tree_store_sortable_init (GtkTreeSortableIface *iface);
74 static void gtk_tree_store_buildable_init (GtkBuildableIface *iface);
75 static void gtk_tree_store_finalize (GObject *object);
76 static GtkTreeModelFlags gtk_tree_store_get_flags (GtkTreeModel *tree_model);
77 static gint gtk_tree_store_get_n_columns (GtkTreeModel *tree_model);
78 static GType gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
80 static gboolean gtk_tree_store_get_iter (GtkTreeModel *tree_model,
83 static GtkTreePath *gtk_tree_store_get_path (GtkTreeModel *tree_model,
85 static void gtk_tree_store_get_value (GtkTreeModel *tree_model,
89 static gboolean gtk_tree_store_iter_next (GtkTreeModel *tree_model,
91 static gboolean gtk_tree_store_iter_children (GtkTreeModel *tree_model,
94 static gboolean gtk_tree_store_iter_has_child (GtkTreeModel *tree_model,
96 static gint gtk_tree_store_iter_n_children (GtkTreeModel *tree_model,
98 static gboolean gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
102 static gboolean gtk_tree_store_iter_parent (GtkTreeModel *tree_model,
107 static void gtk_tree_store_set_n_columns (GtkTreeStore *tree_store,
109 static void gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
113 static void gtk_tree_store_increment_stamp (GtkTreeStore *tree_store);
117 static gboolean real_gtk_tree_store_row_draggable (GtkTreeDragSource *drag_source,
119 static gboolean gtk_tree_store_drag_data_delete (GtkTreeDragSource *drag_source,
121 static gboolean gtk_tree_store_drag_data_get (GtkTreeDragSource *drag_source,
123 GtkSelectionData *selection_data);
124 static gboolean gtk_tree_store_drag_data_received (GtkTreeDragDest *drag_dest,
126 GtkSelectionData *selection_data);
127 static gboolean gtk_tree_store_row_drop_possible (GtkTreeDragDest *drag_dest,
128 GtkTreePath *dest_path,
129 GtkSelectionData *selection_data);
131 /* Sortable Interfaces */
133 static void gtk_tree_store_sort (GtkTreeStore *tree_store);
134 static void gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
137 gboolean emit_signal);
138 static gboolean gtk_tree_store_get_sort_column_id (GtkTreeSortable *sortable,
139 gint *sort_column_id,
141 static void gtk_tree_store_set_sort_column_id (GtkTreeSortable *sortable,
144 static void gtk_tree_store_set_sort_func (GtkTreeSortable *sortable,
146 GtkTreeIterCompareFunc func,
148 GDestroyNotify destroy);
149 static void gtk_tree_store_set_default_sort_func (GtkTreeSortable *sortable,
150 GtkTreeIterCompareFunc func,
152 GDestroyNotify destroy);
153 static gboolean gtk_tree_store_has_default_sort_func (GtkTreeSortable *sortable);
158 static gboolean gtk_tree_store_buildable_custom_tag_start (GtkBuildable *buildable,
161 const gchar *tagname,
162 GMarkupParser *parser,
164 static void gtk_tree_store_buildable_custom_finished (GtkBuildable *buildable,
167 const gchar *tagname,
170 static void validate_gnode (GNode *node);
172 static void gtk_tree_store_move (GtkTreeStore *tree_store,
174 GtkTreeIter *position,
179 validate_tree (GtkTreeStore *tree_store)
181 if (gtk_get_debug_flags () & GTK_DEBUG_TREE)
183 g_assert (G_NODE (tree_store->root)->parent == NULL);
185 validate_gnode (G_NODE (tree_store->root));
189 G_DEFINE_TYPE_WITH_CODE (GtkTreeStore, gtk_tree_store, G_TYPE_OBJECT,
190 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
191 gtk_tree_store_tree_model_init)
192 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
193 gtk_tree_store_drag_source_init)
194 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
195 gtk_tree_store_drag_dest_init)
196 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
197 gtk_tree_store_sortable_init)
198 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
199 gtk_tree_store_buildable_init))
202 gtk_tree_store_class_init (GtkTreeStoreClass *class)
204 GObjectClass *object_class;
206 object_class = (GObjectClass *) class;
208 object_class->finalize = gtk_tree_store_finalize;
212 gtk_tree_store_tree_model_init (GtkTreeModelIface *iface)
214 iface->get_flags = gtk_tree_store_get_flags;
215 iface->get_n_columns = gtk_tree_store_get_n_columns;
216 iface->get_column_type = gtk_tree_store_get_column_type;
217 iface->get_iter = gtk_tree_store_get_iter;
218 iface->get_path = gtk_tree_store_get_path;
219 iface->get_value = gtk_tree_store_get_value;
220 iface->iter_next = gtk_tree_store_iter_next;
221 iface->iter_children = gtk_tree_store_iter_children;
222 iface->iter_has_child = gtk_tree_store_iter_has_child;
223 iface->iter_n_children = gtk_tree_store_iter_n_children;
224 iface->iter_nth_child = gtk_tree_store_iter_nth_child;
225 iface->iter_parent = gtk_tree_store_iter_parent;
229 gtk_tree_store_drag_source_init (GtkTreeDragSourceIface *iface)
231 iface->row_draggable = real_gtk_tree_store_row_draggable;
232 iface->drag_data_delete = gtk_tree_store_drag_data_delete;
233 iface->drag_data_get = gtk_tree_store_drag_data_get;
237 gtk_tree_store_drag_dest_init (GtkTreeDragDestIface *iface)
239 iface->drag_data_received = gtk_tree_store_drag_data_received;
240 iface->row_drop_possible = gtk_tree_store_row_drop_possible;
244 gtk_tree_store_sortable_init (GtkTreeSortableIface *iface)
246 iface->get_sort_column_id = gtk_tree_store_get_sort_column_id;
247 iface->set_sort_column_id = gtk_tree_store_set_sort_column_id;
248 iface->set_sort_func = gtk_tree_store_set_sort_func;
249 iface->set_default_sort_func = gtk_tree_store_set_default_sort_func;
250 iface->has_default_sort_func = gtk_tree_store_has_default_sort_func;
254 gtk_tree_store_buildable_init (GtkBuildableIface *iface)
256 iface->custom_tag_start = gtk_tree_store_buildable_custom_tag_start;
257 iface->custom_finished = gtk_tree_store_buildable_custom_finished;
261 gtk_tree_store_init (GtkTreeStore *tree_store)
263 tree_store->root = g_node_new (NULL);
264 /* While the odds are against us getting 0...
268 tree_store->stamp = g_random_int ();
270 while (tree_store->stamp == 0);
272 tree_store->sort_list = NULL;
273 tree_store->sort_column_id = GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID;
274 tree_store->columns_dirty = FALSE;
278 * gtk_tree_store_new:
279 * @n_columns: number of columns in the tree store
280 * @Varargs: all #GType types for the columns, from first to last
282 * Creates a new tree store as with @n_columns columns each of the types passed
283 * in. Note that only types derived from standard GObject fundamental types
286 * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
287 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkTreeStore with three columns, of type
288 * <type>int</type>, <type>string</type> and #GdkPixbuf respectively.
290 * Return value: a new #GtkTreeStore
293 gtk_tree_store_new (gint n_columns,
296 GtkTreeStore *retval;
300 g_return_val_if_fail (n_columns > 0, NULL);
302 retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
303 gtk_tree_store_set_n_columns (retval, n_columns);
305 va_start (args, n_columns);
307 for (i = 0; i < n_columns; i++)
309 GType type = va_arg (args, GType);
310 if (! _gtk_tree_data_list_check_type (type))
312 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
313 g_object_unref (retval);
317 gtk_tree_store_set_column_type (retval, i, type);
324 * gtk_tree_store_newv:
325 * @n_columns: number of columns in the tree store
326 * @types: (array length=n_columns): an array of #GType types for the columns, from first to last
328 * Non vararg creation function. Used primarily by language bindings.
330 * Return value: (transfer full): a new #GtkTreeStore
333 gtk_tree_store_newv (gint n_columns,
336 GtkTreeStore *retval;
339 g_return_val_if_fail (n_columns > 0, NULL);
341 retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
342 gtk_tree_store_set_n_columns (retval, n_columns);
344 for (i = 0; i < n_columns; i++)
346 if (! _gtk_tree_data_list_check_type (types[i]))
348 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
349 g_object_unref (retval);
352 gtk_tree_store_set_column_type (retval, i, types[i]);
360 * gtk_tree_store_set_column_types:
361 * @tree_store: A #GtkTreeStore
362 * @n_columns: Number of columns for the tree store
363 * @types: (array length=n_columns): An array of #GType types, one for each column
365 * This function is meant primarily for #GObjects that inherit from
366 * #GtkTreeStore, and should only be used when constructing a new
367 * #GtkTreeStore. It will not function after a row has been added,
368 * or a method on the #GtkTreeModel interface is called.
371 gtk_tree_store_set_column_types (GtkTreeStore *tree_store,
377 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
378 g_return_if_fail (tree_store->columns_dirty == 0);
380 gtk_tree_store_set_n_columns (tree_store, n_columns);
381 for (i = 0; i < n_columns; i++)
383 if (! _gtk_tree_data_list_check_type (types[i]))
385 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
388 gtk_tree_store_set_column_type (tree_store, i, types[i]);
393 gtk_tree_store_set_n_columns (GtkTreeStore *tree_store,
398 if (tree_store->n_columns == n_columns)
401 tree_store->column_headers = g_renew (GType, tree_store->column_headers, n_columns);
402 for (i = tree_store->n_columns; i < n_columns; i++)
403 tree_store->column_headers[i] = G_TYPE_INVALID;
404 tree_store->n_columns = n_columns;
406 if (tree_store->sort_list)
407 _gtk_tree_data_list_header_free (tree_store->sort_list);
409 tree_store->sort_list = _gtk_tree_data_list_header_new (n_columns, tree_store->column_headers);
413 * gtk_tree_store_set_column_type:
414 * @tree_store: a #GtkTreeStore
415 * @column: column number
416 * @type: type of the data to be stored in @column
418 * Supported types include: %G_TYPE_UINT, %G_TYPE_INT, %G_TYPE_UCHAR,
419 * %G_TYPE_CHAR, %G_TYPE_BOOLEAN, %G_TYPE_POINTER, %G_TYPE_FLOAT,
420 * %G_TYPE_DOUBLE, %G_TYPE_STRING, %G_TYPE_OBJECT, and %G_TYPE_BOXED, along with
421 * subclasses of those types such as %GDK_TYPE_PIXBUF.
425 gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
429 if (!_gtk_tree_data_list_check_type (type))
431 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
434 tree_store->column_headers[column] = type;
438 node_free (GNode *node, gpointer data)
441 _gtk_tree_data_list_free (node->data, (GType*)data);
448 gtk_tree_store_finalize (GObject *object)
450 GtkTreeStore *tree_store = GTK_TREE_STORE (object);
452 g_node_traverse (tree_store->root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
453 node_free, tree_store->column_headers);
454 g_node_destroy (tree_store->root);
455 _gtk_tree_data_list_header_free (tree_store->sort_list);
456 g_free (tree_store->column_headers);
458 if (tree_store->default_sort_destroy)
460 GDestroyNotify d = tree_store->default_sort_destroy;
462 tree_store->default_sort_destroy = NULL;
463 d (tree_store->default_sort_data);
464 tree_store->default_sort_data = NULL;
468 G_OBJECT_CLASS (gtk_tree_store_parent_class)->finalize (object);
471 /* fulfill the GtkTreeModel requirements */
472 /* NOTE: GtkTreeStore::root is a GNode, that acts as the parent node. However,
473 * it is not visible to the tree or to the user., and the path "0" refers to the
474 * first child of GtkTreeStore::root.
478 static GtkTreeModelFlags
479 gtk_tree_store_get_flags (GtkTreeModel *tree_model)
481 return GTK_TREE_MODEL_ITERS_PERSIST;
485 gtk_tree_store_get_n_columns (GtkTreeModel *tree_model)
487 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
489 tree_store->columns_dirty = TRUE;
491 return tree_store->n_columns;
495 gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
498 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
500 g_return_val_if_fail (index < tree_store->n_columns, G_TYPE_INVALID);
502 tree_store->columns_dirty = TRUE;
504 return tree_store->column_headers[index];
508 gtk_tree_store_get_iter (GtkTreeModel *tree_model,
512 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
517 tree_store->columns_dirty = TRUE;
519 indices = gtk_tree_path_get_indices (path);
520 depth = gtk_tree_path_get_depth (path);
522 g_return_val_if_fail (depth > 0, FALSE);
524 parent.stamp = tree_store->stamp;
525 parent.user_data = tree_store->root;
527 if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[0]))
530 for (i = 1; i < depth; i++)
533 if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[i]))
541 gtk_tree_store_get_path (GtkTreeModel *tree_model,
544 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
549 g_return_val_if_fail (iter->user_data != NULL, NULL);
550 g_return_val_if_fail (iter->stamp == tree_store->stamp, NULL);
552 validate_tree (tree_store);
554 if (G_NODE (iter->user_data)->parent == NULL &&
555 G_NODE (iter->user_data) == tree_store->root)
556 return gtk_tree_path_new ();
557 g_assert (G_NODE (iter->user_data)->parent != NULL);
559 if (G_NODE (iter->user_data)->parent == G_NODE (tree_store->root))
561 retval = gtk_tree_path_new ();
562 tmp_node = G_NODE (tree_store->root)->children;
566 GtkTreeIter tmp_iter = *iter;
568 tmp_iter.user_data = G_NODE (iter->user_data)->parent;
570 retval = gtk_tree_store_get_path (tree_model, &tmp_iter);
571 tmp_node = G_NODE (iter->user_data)->parent->children;
577 if (tmp_node == NULL)
579 gtk_tree_path_free (retval);
583 for (; tmp_node; tmp_node = tmp_node->next)
585 if (tmp_node == G_NODE (iter->user_data))
590 if (tmp_node == NULL)
592 /* We couldn't find node, meaning it's prolly not ours */
593 /* Perhaps I should do a g_return_if_fail here. */
594 gtk_tree_path_free (retval);
598 gtk_tree_path_append_index (retval, i);
605 gtk_tree_store_get_value (GtkTreeModel *tree_model,
610 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
611 GtkTreeDataList *list;
612 gint tmp_column = column;
614 g_return_if_fail (column < tree_store->n_columns);
615 g_return_if_fail (VALID_ITER (iter, tree_store));
617 list = G_NODE (iter->user_data)->data;
619 while (tmp_column-- > 0 && list)
624 _gtk_tree_data_list_node_to_value (list,
625 tree_store->column_headers[column],
630 /* We want to return an initialized but empty (default) value */
631 g_value_init (value, tree_store->column_headers[column]);
636 gtk_tree_store_iter_next (GtkTreeModel *tree_model,
639 g_return_val_if_fail (iter->user_data != NULL, FALSE);
640 g_return_val_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
642 if (G_NODE (iter->user_data)->next)
644 iter->user_data = G_NODE (iter->user_data)->next;
655 gtk_tree_store_iter_children (GtkTreeModel *tree_model,
659 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
663 g_return_val_if_fail (VALID_ITER (parent, tree_store), FALSE);
666 children = G_NODE (parent->user_data)->children;
668 children = G_NODE (tree_store->root)->children;
672 iter->stamp = tree_store->stamp;
673 iter->user_data = children;
684 gtk_tree_store_iter_has_child (GtkTreeModel *tree_model,
687 g_return_val_if_fail (iter->user_data != NULL, FALSE);
688 g_return_val_if_fail (VALID_ITER (iter, tree_model), FALSE);
690 return G_NODE (iter->user_data)->children != NULL;
694 gtk_tree_store_iter_n_children (GtkTreeModel *tree_model,
700 g_return_val_if_fail (iter == NULL || iter->user_data != NULL, 0);
703 node = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
705 node = G_NODE (iter->user_data)->children;
717 gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
722 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
726 g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
729 parent_node = tree_store->root;
731 parent_node = parent->user_data;
733 child = g_node_nth_child (parent_node, n);
737 iter->user_data = child;
738 iter->stamp = tree_store->stamp;
749 gtk_tree_store_iter_parent (GtkTreeModel *tree_model,
753 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
756 g_return_val_if_fail (iter != NULL, FALSE);
757 g_return_val_if_fail (VALID_ITER (child, tree_store), FALSE);
759 parent = G_NODE (child->user_data)->parent;
761 g_assert (parent != NULL);
763 if (parent != tree_store->root)
765 iter->user_data = parent;
766 iter->stamp = tree_store->stamp;
777 /* Does not emit a signal */
779 gtk_tree_store_real_set_value (GtkTreeStore *tree_store,
785 GtkTreeDataList *list;
786 GtkTreeDataList *prev;
787 gint old_column = column;
788 GValue real_value = {0, };
789 gboolean converted = FALSE;
790 gboolean retval = FALSE;
792 if (! g_type_is_a (G_VALUE_TYPE (value), tree_store->column_headers[column]))
794 if (! (g_value_type_compatible (G_VALUE_TYPE (value), tree_store->column_headers[column]) &&
795 g_value_type_compatible (tree_store->column_headers[column], G_VALUE_TYPE (value))))
797 g_warning ("%s: Unable to convert from %s to %s\n",
799 g_type_name (G_VALUE_TYPE (value)),
800 g_type_name (tree_store->column_headers[column]));
803 if (!g_value_transform (value, &real_value))
805 g_warning ("%s: Unable to make conversion from %s to %s\n",
807 g_type_name (G_VALUE_TYPE (value)),
808 g_type_name (tree_store->column_headers[column]));
809 g_value_unset (&real_value);
815 prev = list = G_NODE (iter->user_data)->data;
822 _gtk_tree_data_list_value_to_node (list, &real_value);
824 _gtk_tree_data_list_value_to_node (list, value);
827 g_value_unset (&real_value);
828 if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
829 gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
838 if (G_NODE (iter->user_data)->data == NULL)
840 G_NODE (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
845 list = prev->next = _gtk_tree_data_list_alloc ();
851 list->next = _gtk_tree_data_list_alloc ();
858 _gtk_tree_data_list_value_to_node (list, &real_value);
860 _gtk_tree_data_list_value_to_node (list, value);
864 g_value_unset (&real_value);
866 if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
867 gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
873 * gtk_tree_store_set_value:
874 * @tree_store: a #GtkTreeStore
875 * @iter: A valid #GtkTreeIter for the row being modified
876 * @column: column number to modify
877 * @value: new value for the cell
879 * Sets the data in the cell specified by @iter and @column.
880 * The type of @value must be convertible to the type of the
885 gtk_tree_store_set_value (GtkTreeStore *tree_store,
890 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
891 g_return_if_fail (VALID_ITER (iter, tree_store));
892 g_return_if_fail (column >= 0 && column < tree_store->n_columns);
893 g_return_if_fail (G_IS_VALUE (value));
895 if (gtk_tree_store_real_set_value (tree_store, iter, column, value, TRUE))
899 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
900 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
901 gtk_tree_path_free (path);
905 static GtkTreeIterCompareFunc
906 gtk_tree_store_get_compare_func (GtkTreeStore *tree_store)
908 GtkTreeIterCompareFunc func = NULL;
910 if (GTK_TREE_STORE_IS_SORTED (tree_store))
912 if (tree_store->sort_column_id != -1)
914 GtkTreeDataSortHeader *header;
915 header = _gtk_tree_data_list_get_header (tree_store->sort_list,
916 tree_store->sort_column_id);
917 g_return_val_if_fail (header != NULL, NULL);
918 g_return_val_if_fail (header->func != NULL, NULL);
923 func = tree_store->default_sort_func;
931 gtk_tree_store_set_vector_internal (GtkTreeStore *tree_store,
933 gboolean *emit_signal,
934 gboolean *maybe_need_sort,
940 GtkTreeIterCompareFunc func = NULL;
942 func = gtk_tree_store_get_compare_func (tree_store);
943 if (func != _gtk_tree_data_list_compare_func)
944 *maybe_need_sort = TRUE;
946 for (i = 0; i < n_values; i++)
948 *emit_signal = gtk_tree_store_real_set_value (tree_store, iter,
949 columns[i], &values[i],
950 FALSE) || *emit_signal;
952 if (func == _gtk_tree_data_list_compare_func &&
953 columns[i] == tree_store->sort_column_id)
954 *maybe_need_sort = TRUE;
959 gtk_tree_store_set_valist_internal (GtkTreeStore *tree_store,
961 gboolean *emit_signal,
962 gboolean *maybe_need_sort,
966 GtkTreeIterCompareFunc func = NULL;
968 column = va_arg (var_args, gint);
970 func = gtk_tree_store_get_compare_func (tree_store);
971 if (func != _gtk_tree_data_list_compare_func)
972 *maybe_need_sort = TRUE;
976 GValue value = { 0, };
979 if (column < 0 || column >= tree_store->n_columns)
981 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
984 g_value_init (&value, tree_store->column_headers[column]);
986 G_VALUE_COLLECT (&value, var_args, 0, &error);
989 g_warning ("%s: %s", G_STRLOC, error);
992 /* we purposely leak the value here, it might not be
993 * in a sane state if an error condition occoured
998 *emit_signal = gtk_tree_store_real_set_value (tree_store,
1002 FALSE) || *emit_signal;
1004 if (func == _gtk_tree_data_list_compare_func &&
1005 column == tree_store->sort_column_id)
1006 *maybe_need_sort = TRUE;
1008 g_value_unset (&value);
1010 column = va_arg (var_args, gint);
1015 * gtk_tree_store_set_valuesv:
1016 * @tree_store: A #GtkTreeStore
1017 * @iter: A valid #GtkTreeIter for the row being modified
1018 * @columns: (array length=n_values): an array of column numbers
1019 * @values: (array length=n_values): an array of GValues
1020 * @n_values: the length of the @columns and @values arrays
1022 * A variant of gtk_tree_store_set_valist() which takes
1023 * the columns and values as two arrays, instead of varargs. This
1024 * function is mainly intended for language bindings or in case
1025 * the number of columns to change is not known until run-time.
1030 gtk_tree_store_set_valuesv (GtkTreeStore *tree_store,
1036 gboolean emit_signal = FALSE;
1037 gboolean maybe_need_sort = FALSE;
1039 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1040 g_return_if_fail (VALID_ITER (iter, tree_store));
1042 gtk_tree_store_set_vector_internal (tree_store, iter,
1045 columns, values, n_values);
1047 if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
1048 gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, TRUE);
1054 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1055 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
1056 gtk_tree_path_free (path);
1061 * gtk_tree_store_set_valist:
1062 * @tree_store: A #GtkTreeStore
1063 * @iter: A valid #GtkTreeIter for the row being modified
1064 * @var_args: <type>va_list</type> of column/value pairs
1066 * See gtk_tree_store_set(); this version takes a <type>va_list</type> for
1067 * use by language bindings.
1071 gtk_tree_store_set_valist (GtkTreeStore *tree_store,
1075 gboolean emit_signal = FALSE;
1076 gboolean maybe_need_sort = FALSE;
1078 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1079 g_return_if_fail (VALID_ITER (iter, tree_store));
1081 gtk_tree_store_set_valist_internal (tree_store, iter,
1086 if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
1087 gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, TRUE);
1093 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1094 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
1095 gtk_tree_path_free (path);
1100 * gtk_tree_store_set:
1101 * @tree_store: A #GtkTreeStore
1102 * @iter: A valid #GtkTreeIter for the row being modified
1103 * @Varargs: pairs of column number and value, terminated with -1
1105 * Sets the value of one or more cells in the row referenced by @iter.
1106 * The variable argument list should contain integer column numbers,
1107 * each column number followed by the value to be set.
1108 * The list is terminated by a -1. For example, to set column 0 with type
1109 * %G_TYPE_STRING to "Foo", you would write
1110 * <literal>gtk_tree_store_set (store, iter, 0, "Foo", -1)</literal>.
1112 * The value will be referenced by the store if it is a %G_TYPE_OBJECT, and it
1113 * will be copied if it is a %G_TYPE_STRING or %G_TYPE_BOXED.
1116 gtk_tree_store_set (GtkTreeStore *tree_store,
1122 va_start (var_args, iter);
1123 gtk_tree_store_set_valist (tree_store, iter, var_args);
1128 * gtk_tree_store_remove:
1129 * @tree_store: A #GtkTreeStore
1130 * @iter: A valid #GtkTreeIter
1132 * Removes @iter from @tree_store. After being removed, @iter is set to the
1133 * next valid row at that level, or invalidated if it previously pointed to the
1136 * Return value: %TRUE if @iter is still valid, %FALSE if not.
1139 gtk_tree_store_remove (GtkTreeStore *tree_store,
1143 GtkTreeIter new_iter = {0,};
1147 g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
1148 g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
1150 parent = G_NODE (iter->user_data)->parent;
1152 g_assert (parent != NULL);
1153 next_node = G_NODE (iter->user_data)->next;
1155 if (G_NODE (iter->user_data)->data)
1156 g_node_traverse (G_NODE (iter->user_data), G_POST_ORDER, G_TRAVERSE_ALL,
1157 -1, node_free, tree_store->column_headers);
1159 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1160 g_node_destroy (G_NODE (iter->user_data));
1162 gtk_tree_model_row_deleted (GTK_TREE_MODEL (tree_store), path);
1164 if (parent != G_NODE (tree_store->root))
1167 if (parent->children == NULL)
1169 gtk_tree_path_up (path);
1171 new_iter.stamp = tree_store->stamp;
1172 new_iter.user_data = parent;
1173 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &new_iter);
1176 gtk_tree_path_free (path);
1178 /* revalidate iter */
1179 if (next_node != NULL)
1181 iter->stamp = tree_store->stamp;
1182 iter->user_data = next_node;
1188 iter->user_data = NULL;
1195 * gtk_tree_store_insert:
1196 * @tree_store: A #GtkTreeStore
1197 * @iter: (out): An unset #GtkTreeIter to set to the new row
1198 * @parent: (allow-none): A valid #GtkTreeIter, or %NULL
1199 * @position: position to insert the new row
1201 * Creates a new row at @position. If parent is non-%NULL, then the row will be
1202 * made a child of @parent. Otherwise, the row will be created at the toplevel.
1203 * If @position is larger than the number of rows at that level, then the new
1204 * row will be inserted to the end of the list. @iter will be changed to point
1205 * to this new row. The row will be empty after this function is called. To
1206 * fill in values, you need to call gtk_tree_store_set() or
1207 * gtk_tree_store_set_value().
1211 gtk_tree_store_insert (GtkTreeStore *tree_store,
1213 GtkTreeIter *parent,
1220 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1221 g_return_if_fail (iter != NULL);
1223 g_return_if_fail (VALID_ITER (parent, tree_store));
1226 parent_node = parent->user_data;
1228 parent_node = tree_store->root;
1230 tree_store->columns_dirty = TRUE;
1232 new_node = g_node_new (NULL);
1234 iter->stamp = tree_store->stamp;
1235 iter->user_data = new_node;
1236 g_node_insert (parent_node, position, new_node);
1238 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1239 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1241 if (parent_node != tree_store->root)
1243 if (new_node->prev == NULL && new_node->next == NULL)
1245 gtk_tree_path_up (path);
1246 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1250 gtk_tree_path_free (path);
1252 validate_tree ((GtkTreeStore*)tree_store);
1256 * gtk_tree_store_insert_before:
1257 * @tree_store: A #GtkTreeStore
1258 * @iter: (out): An unset #GtkTreeIter to set to the new row
1259 * @parent: (allow-none): A valid #GtkTreeIter, or %NULL
1260 * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1262 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will
1263 * be appended to @parent 's children. If @parent and @sibling are %NULL, then
1264 * the row will be appended to the toplevel. If both @sibling and @parent are
1265 * set, then @parent must be the parent of @sibling. When @sibling is set,
1266 * @parent is optional.
1268 * @iter will be changed to point to this new row. The row will be empty after
1269 * this function is called. To fill in values, you need to call
1270 * gtk_tree_store_set() or gtk_tree_store_set_value().
1274 gtk_tree_store_insert_before (GtkTreeStore *tree_store,
1276 GtkTreeIter *parent,
1277 GtkTreeIter *sibling)
1280 GNode *parent_node = NULL;
1283 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1284 g_return_if_fail (iter != NULL);
1286 g_return_if_fail (VALID_ITER (parent, tree_store));
1287 if (sibling != NULL)
1288 g_return_if_fail (VALID_ITER (sibling, tree_store));
1290 if (parent == NULL && sibling == NULL)
1291 parent_node = tree_store->root;
1292 else if (parent == NULL)
1293 parent_node = G_NODE (sibling->user_data)->parent;
1294 else if (sibling == NULL)
1295 parent_node = G_NODE (parent->user_data);
1298 g_return_if_fail (G_NODE (sibling->user_data)->parent == G_NODE (parent->user_data));
1299 parent_node = G_NODE (parent->user_data);
1302 tree_store->columns_dirty = TRUE;
1304 new_node = g_node_new (NULL);
1306 g_node_insert_before (parent_node,
1307 sibling ? G_NODE (sibling->user_data) : NULL,
1310 iter->stamp = tree_store->stamp;
1311 iter->user_data = new_node;
1313 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1314 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1316 if (parent_node != tree_store->root)
1318 if (new_node->prev == NULL && new_node->next == NULL)
1320 GtkTreeIter parent_iter;
1322 parent_iter.stamp = tree_store->stamp;
1323 parent_iter.user_data = parent_node;
1325 gtk_tree_path_up (path);
1326 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &parent_iter);
1330 gtk_tree_path_free (path);
1332 validate_tree (tree_store);
1336 * gtk_tree_store_insert_after:
1337 * @tree_store: A #GtkTreeStore
1338 * @iter: (out): An unset #GtkTreeIter to set to the new row
1339 * @parent: (allow-none): A valid #GtkTreeIter, or %NULL
1340 * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1342 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1343 * prepended to @parent 's children. If @parent and @sibling are %NULL, then
1344 * the row will be prepended to the toplevel. If both @sibling and @parent are
1345 * set, then @parent must be the parent of @sibling. When @sibling is set,
1346 * @parent is optional.
1348 * @iter will be changed to point to this new row. The row will be empty after
1349 * this function is called. To fill in values, you need to call
1350 * gtk_tree_store_set() or gtk_tree_store_set_value().
1354 gtk_tree_store_insert_after (GtkTreeStore *tree_store,
1356 GtkTreeIter *parent,
1357 GtkTreeIter *sibling)
1363 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1364 g_return_if_fail (iter != NULL);
1366 g_return_if_fail (VALID_ITER (parent, tree_store));
1367 if (sibling != NULL)
1368 g_return_if_fail (VALID_ITER (sibling, tree_store));
1370 if (parent == NULL && sibling == NULL)
1371 parent_node = tree_store->root;
1372 else if (parent == NULL)
1373 parent_node = G_NODE (sibling->user_data)->parent;
1374 else if (sibling == NULL)
1375 parent_node = G_NODE (parent->user_data);
1378 g_return_if_fail (G_NODE (sibling->user_data)->parent ==
1379 G_NODE (parent->user_data));
1380 parent_node = G_NODE (parent->user_data);
1383 tree_store->columns_dirty = TRUE;
1385 new_node = g_node_new (NULL);
1387 g_node_insert_after (parent_node,
1388 sibling ? G_NODE (sibling->user_data) : NULL,
1391 iter->stamp = tree_store->stamp;
1392 iter->user_data = new_node;
1394 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1395 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1397 if (parent_node != tree_store->root)
1399 if (new_node->prev == NULL && new_node->next == NULL)
1401 GtkTreeIter parent_iter;
1403 parent_iter.stamp = tree_store->stamp;
1404 parent_iter.user_data = parent_node;
1406 gtk_tree_path_up (path);
1407 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &parent_iter);
1411 gtk_tree_path_free (path);
1413 validate_tree (tree_store);
1417 * gtk_tree_store_insert_with_values:
1418 * @tree_store: A #GtkTreeStore
1419 * @iter: (out) (allow-none): An unset #GtkTreeIter to set the new row, or %NULL.
1420 * @parent: (allow-none): A valid #GtkTreeIter, or %NULL
1421 * @position: position to insert the new row
1422 * @Varargs: pairs of column number and value, terminated with -1
1424 * Creates a new row at @position. @iter will be changed to point to this
1425 * new row. If @position is larger than the number of rows on the list, then
1426 * the new row will be appended to the list. The row will be filled with
1427 * the values given to this function.
1430 * <literal>gtk_tree_store_insert_with_values (tree_store, iter, position, ...)</literal>
1431 * has the same effect as calling
1433 * gtk_tree_store_insert (tree_store, iter, position);
1434 * gtk_tree_store_set (tree_store, iter, ...);
1436 * with the different that the former will only emit a row_inserted signal,
1437 * while the latter will emit row_inserted, row_changed and if the tree store
1438 * is sorted, rows_reordered. Since emitting the rows_reordered signal
1439 * repeatedly can affect the performance of the program,
1440 * gtk_tree_store_insert_with_values() should generally be preferred when
1441 * inserting rows in a sorted tree store.
1446 gtk_tree_store_insert_with_values (GtkTreeStore *tree_store,
1448 GtkTreeIter *parent,
1455 GtkTreeIter tmp_iter;
1457 gboolean changed = FALSE;
1458 gboolean maybe_need_sort = FALSE;
1460 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1466 g_return_if_fail (VALID_ITER (parent, tree_store));
1469 parent_node = parent->user_data;
1471 parent_node = tree_store->root;
1473 tree_store->columns_dirty = TRUE;
1475 new_node = g_node_new (NULL);
1477 iter->stamp = tree_store->stamp;
1478 iter->user_data = new_node;
1479 g_node_insert (parent_node, position, new_node);
1481 va_start (var_args, position);
1482 gtk_tree_store_set_valist_internal (tree_store, iter,
1483 &changed, &maybe_need_sort,
1487 if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
1488 gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, FALSE);
1490 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1491 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1493 if (parent_node != tree_store->root)
1495 if (new_node->prev == NULL && new_node->next == NULL)
1497 gtk_tree_path_up (path);
1498 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1502 gtk_tree_path_free (path);
1504 validate_tree ((GtkTreeStore *)tree_store);
1508 * gtk_tree_store_insert_with_valuesv:
1509 * @tree_store: A #GtkTreeStore
1510 * @iter: (out) (allow-none): An unset #GtkTreeIter to set the new row, or %NULL.
1511 * @parent: (allow-none): A valid #GtkTreeIter, or %NULL
1512 * @position: position to insert the new row
1513 * @columns: an array of column numbers
1514 * @values: an array of GValues
1515 * @n_values: the length of the @columns and @values arrays
1517 * A variant of gtk_tree_store_insert_with_values() which takes
1518 * the columns and values as two arrays, instead of varargs. This
1519 * function is mainly intended for language bindings.
1524 gtk_tree_store_insert_with_valuesv (GtkTreeStore *tree_store,
1526 GtkTreeIter *parent,
1535 GtkTreeIter tmp_iter;
1536 gboolean changed = FALSE;
1537 gboolean maybe_need_sort = FALSE;
1539 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1545 g_return_if_fail (VALID_ITER (parent, tree_store));
1548 parent_node = parent->user_data;
1550 parent_node = tree_store->root;
1552 tree_store->columns_dirty = TRUE;
1554 new_node = g_node_new (NULL);
1556 iter->stamp = tree_store->stamp;
1557 iter->user_data = new_node;
1558 g_node_insert (parent_node, position, new_node);
1560 gtk_tree_store_set_vector_internal (tree_store, iter,
1561 &changed, &maybe_need_sort,
1562 columns, values, n_values);
1564 if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
1565 gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, FALSE);
1567 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1568 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1570 if (parent_node != tree_store->root)
1572 if (new_node->prev == NULL && new_node->next == NULL)
1574 gtk_tree_path_up (path);
1575 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1579 gtk_tree_path_free (path);
1581 validate_tree ((GtkTreeStore *)tree_store);
1585 * gtk_tree_store_prepend:
1586 * @tree_store: A #GtkTreeStore
1587 * @iter: (out): An unset #GtkTreeIter to set to the prepended row
1588 * @parent: (allow-none): A valid #GtkTreeIter, or %NULL
1590 * Prepends a new row to @tree_store. If @parent is non-%NULL, then it will prepend
1591 * the new row before the first child of @parent, otherwise it will prepend a row
1592 * to the top level. @iter will be changed to point to this new row. The row
1593 * will be empty after this function is called. To fill in values, you need to
1594 * call gtk_tree_store_set() or gtk_tree_store_set_value().
1597 gtk_tree_store_prepend (GtkTreeStore *tree_store,
1599 GtkTreeIter *parent)
1603 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1604 g_return_if_fail (iter != NULL);
1606 g_return_if_fail (VALID_ITER (parent, tree_store));
1608 tree_store->columns_dirty = TRUE;
1611 parent_node = tree_store->root;
1613 parent_node = parent->user_data;
1615 if (parent_node->children == NULL)
1619 iter->stamp = tree_store->stamp;
1620 iter->user_data = g_node_new (NULL);
1622 g_node_prepend (parent_node, G_NODE (iter->user_data));
1624 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1625 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1627 if (parent_node != tree_store->root)
1629 gtk_tree_path_up (path);
1630 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1632 gtk_tree_path_free (path);
1636 gtk_tree_store_insert_after (tree_store, iter, parent, NULL);
1639 validate_tree (tree_store);
1643 * gtk_tree_store_append:
1644 * @tree_store: A #GtkTreeStore
1645 * @iter: (out): An unset #GtkTreeIter to set to the appended row
1646 * @parent: (allow-none): A valid #GtkTreeIter, or %NULL
1648 * Appends a new row to @tree_store. If @parent is non-%NULL, then it will append the
1649 * new row after the last child of @parent, otherwise it will append a row to
1650 * the top level. @iter will be changed to point to this new row. The row will
1651 * be empty after this function is called. To fill in values, you need to call
1652 * gtk_tree_store_set() or gtk_tree_store_set_value().
1655 gtk_tree_store_append (GtkTreeStore *tree_store,
1657 GtkTreeIter *parent)
1661 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1662 g_return_if_fail (iter != NULL);
1664 g_return_if_fail (VALID_ITER (parent, tree_store));
1667 parent_node = tree_store->root;
1669 parent_node = parent->user_data;
1671 tree_store->columns_dirty = TRUE;
1673 if (parent_node->children == NULL)
1677 iter->stamp = tree_store->stamp;
1678 iter->user_data = g_node_new (NULL);
1680 g_node_append (parent_node, G_NODE (iter->user_data));
1682 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1683 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1685 if (parent_node != tree_store->root)
1687 gtk_tree_path_up (path);
1688 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1690 gtk_tree_path_free (path);
1694 gtk_tree_store_insert_before (tree_store, iter, parent, NULL);
1697 validate_tree (tree_store);
1701 * gtk_tree_store_is_ancestor:
1702 * @tree_store: A #GtkTreeStore
1703 * @iter: A valid #GtkTreeIter
1704 * @descendant: A valid #GtkTreeIter
1706 * Returns %TRUE if @iter is an ancestor of @descendant. That is, @iter is the
1707 * parent (or grandparent or great-grandparent) of @descendant.
1709 * Return value: %TRUE, if @iter is an ancestor of @descendant
1712 gtk_tree_store_is_ancestor (GtkTreeStore *tree_store,
1714 GtkTreeIter *descendant)
1716 g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
1717 g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
1718 g_return_val_if_fail (VALID_ITER (descendant, tree_store), FALSE);
1720 return g_node_is_ancestor (G_NODE (iter->user_data),
1721 G_NODE (descendant->user_data));
1726 * gtk_tree_store_iter_depth:
1727 * @tree_store: A #GtkTreeStore
1728 * @iter: A valid #GtkTreeIter
1730 * Returns the depth of @iter. This will be 0 for anything on the root level, 1
1731 * for anything down a level, etc.
1733 * Return value: The depth of @iter
1736 gtk_tree_store_iter_depth (GtkTreeStore *tree_store,
1739 g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), 0);
1740 g_return_val_if_fail (VALID_ITER (iter, tree_store), 0);
1742 return g_node_depth (G_NODE (iter->user_data)) - 2;
1745 /* simple ripoff from g_node_traverse_post_order */
1747 gtk_tree_store_clear_traverse (GNode *node,
1748 GtkTreeStore *store)
1756 child = node->children;
1759 register GNode *current;
1762 child = current->next;
1763 if (gtk_tree_store_clear_traverse (current, store))
1769 iter.stamp = store->stamp;
1770 iter.user_data = node;
1772 gtk_tree_store_remove (store, &iter);
1775 else if (node->parent)
1777 iter.stamp = store->stamp;
1778 iter.user_data = node;
1780 gtk_tree_store_remove (store, &iter);
1787 gtk_tree_store_increment_stamp (GtkTreeStore *tree_store)
1791 tree_store->stamp++;
1793 while (tree_store->stamp == 0);
1797 * gtk_tree_store_clear:
1798 * @tree_store: a #GtkTreeStore
1800 * Removes all rows from @tree_store
1803 gtk_tree_store_clear (GtkTreeStore *tree_store)
1805 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1807 gtk_tree_store_clear_traverse (tree_store->root, tree_store);
1808 gtk_tree_store_increment_stamp (tree_store);
1812 gtk_tree_store_iter_is_valid_helper (GtkTreeIter *iter,
1821 if (node == iter->user_data)
1825 if (gtk_tree_store_iter_is_valid_helper (iter, node->children))
1836 * gtk_tree_store_iter_is_valid:
1837 * @tree_store: A #GtkTreeStore.
1838 * @iter: A #GtkTreeIter.
1840 * WARNING: This function is slow. Only use it for debugging and/or testing
1843 * Checks if the given iter is a valid iter for this #GtkTreeStore.
1845 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1850 gtk_tree_store_iter_is_valid (GtkTreeStore *tree_store,
1853 g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
1854 g_return_val_if_fail (iter != NULL, FALSE);
1856 if (!VALID_ITER (iter, tree_store))
1859 return gtk_tree_store_iter_is_valid_helper (iter, tree_store->root);
1865 static gboolean real_gtk_tree_store_row_draggable (GtkTreeDragSource *drag_source,
1872 gtk_tree_store_drag_data_delete (GtkTreeDragSource *drag_source,
1877 if (gtk_tree_store_get_iter (GTK_TREE_MODEL (drag_source),
1881 gtk_tree_store_remove (GTK_TREE_STORE (drag_source),
1892 gtk_tree_store_drag_data_get (GtkTreeDragSource *drag_source,
1894 GtkSelectionData *selection_data)
1896 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1897 * target, because the default handler does it for us, but
1898 * we do anyway for the convenience of someone maybe overriding the
1902 if (gtk_tree_set_row_drag_data (selection_data,
1903 GTK_TREE_MODEL (drag_source),
1910 /* FIXME handle text targets at least. */
1917 copy_node_data (GtkTreeStore *tree_store,
1918 GtkTreeIter *src_iter,
1919 GtkTreeIter *dest_iter)
1921 GtkTreeDataList *dl = G_NODE (src_iter->user_data)->data;
1922 GtkTreeDataList *copy_head = NULL;
1923 GtkTreeDataList *copy_prev = NULL;
1924 GtkTreeDataList *copy_iter = NULL;
1931 copy_iter = _gtk_tree_data_list_node_copy (dl, tree_store->column_headers[col]);
1933 if (copy_head == NULL)
1934 copy_head = copy_iter;
1937 copy_prev->next = copy_iter;
1939 copy_prev = copy_iter;
1945 G_NODE (dest_iter->user_data)->data = copy_head;
1947 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), dest_iter);
1948 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, dest_iter);
1949 gtk_tree_path_free (path);
1953 recursive_node_copy (GtkTreeStore *tree_store,
1954 GtkTreeIter *src_iter,
1955 GtkTreeIter *dest_iter)
1958 GtkTreeModel *model;
1960 model = GTK_TREE_MODEL (tree_store);
1962 copy_node_data (tree_store, src_iter, dest_iter);
1964 if (gtk_tree_store_iter_children (model,
1968 /* Need to create children and recurse. Note our
1969 * dependence on persistent iterators here.
1975 /* Gee, a really slow algorithm... ;-) FIXME */
1976 gtk_tree_store_append (tree_store,
1980 recursive_node_copy (tree_store, &child, ©);
1982 while (gtk_tree_store_iter_next (model, &child));
1987 gtk_tree_store_drag_data_received (GtkTreeDragDest *drag_dest,
1989 GtkSelectionData *selection_data)
1991 GtkTreeModel *tree_model;
1992 GtkTreeStore *tree_store;
1993 GtkTreeModel *src_model = NULL;
1994 GtkTreePath *src_path = NULL;
1995 gboolean retval = FALSE;
1997 tree_model = GTK_TREE_MODEL (drag_dest);
1998 tree_store = GTK_TREE_STORE (drag_dest);
2000 validate_tree (tree_store);
2002 if (gtk_tree_get_row_drag_data (selection_data,
2005 src_model == tree_model)
2007 /* Copy the given row to a new position */
2008 GtkTreeIter src_iter;
2009 GtkTreeIter dest_iter;
2012 if (!gtk_tree_store_get_iter (src_model,
2019 /* Get the path to insert _after_ (dest is the path to insert _before_) */
2020 prev = gtk_tree_path_copy (dest);
2022 if (!gtk_tree_path_prev (prev))
2024 GtkTreeIter dest_parent;
2025 GtkTreePath *parent;
2026 GtkTreeIter *dest_parent_p;
2028 /* dest was the first spot at the current depth; which means
2029 * we are supposed to prepend.
2032 /* Get the parent, NULL if parent is the root */
2033 dest_parent_p = NULL;
2034 parent = gtk_tree_path_copy (dest);
2035 if (gtk_tree_path_up (parent) &&
2036 gtk_tree_path_get_depth (parent) > 0)
2038 gtk_tree_store_get_iter (tree_model,
2041 dest_parent_p = &dest_parent;
2043 gtk_tree_path_free (parent);
2046 gtk_tree_store_prepend (tree_store,
2054 if (gtk_tree_store_get_iter (tree_model, &dest_iter, prev))
2056 GtkTreeIter tmp_iter = dest_iter;
2058 gtk_tree_store_insert_after (tree_store, &dest_iter, NULL,
2065 gtk_tree_path_free (prev);
2067 /* If we succeeded in creating dest_iter, walk src_iter tree branch,
2068 * duplicating it below dest_iter.
2073 recursive_node_copy (tree_store,
2080 /* FIXME maybe add some data targets eventually, or handle text
2081 * targets in the simple case.
2089 gtk_tree_path_free (src_path);
2095 gtk_tree_store_row_drop_possible (GtkTreeDragDest *drag_dest,
2096 GtkTreePath *dest_path,
2097 GtkSelectionData *selection_data)
2099 GtkTreeModel *src_model = NULL;
2100 GtkTreePath *src_path = NULL;
2101 GtkTreePath *tmp = NULL;
2102 gboolean retval = FALSE;
2104 /* don't accept drops if the tree has been sorted */
2105 if (GTK_TREE_STORE_IS_SORTED (drag_dest))
2108 if (!gtk_tree_get_row_drag_data (selection_data,
2113 /* can only drag to ourselves */
2114 if (src_model != GTK_TREE_MODEL (drag_dest))
2117 /* Can't drop into ourself. */
2118 if (gtk_tree_path_is_ancestor (src_path,
2122 /* Can't drop if dest_path's parent doesn't exist */
2126 if (gtk_tree_path_get_depth (dest_path) > 1)
2128 tmp = gtk_tree_path_copy (dest_path);
2129 gtk_tree_path_up (tmp);
2131 if (!gtk_tree_store_get_iter (GTK_TREE_MODEL (drag_dest),
2137 /* Can otherwise drop anywhere. */
2143 gtk_tree_path_free (src_path);
2145 gtk_tree_path_free (tmp);
2150 /* Sorting and reordering */
2151 typedef struct _SortTuple
2159 gtk_tree_store_reorder_func (gconstpointer a,
2163 SortTuple *a_reorder;
2164 SortTuple *b_reorder;
2166 a_reorder = (SortTuple *)a;
2167 b_reorder = (SortTuple *)b;
2169 if (a_reorder->offset < b_reorder->offset)
2171 if (a_reorder->offset > b_reorder->offset)
2178 * gtk_tree_store_reorder:
2179 * @tree_store: A #GtkTreeStore.
2180 * @parent: A #GtkTreeIter.
2181 * @new_order: an array of integers mapping the new position of each child
2182 * to its old position before the re-ordering,
2183 * i.e. @new_order<literal>[newpos] = oldpos</literal>.
2185 * Reorders the children of @parent in @tree_store to follow the order
2186 * indicated by @new_order. Note that this function only works with
2192 gtk_tree_store_reorder (GtkTreeStore *tree_store,
2193 GtkTreeIter *parent,
2197 GNode *level, *node;
2199 SortTuple *sort_array;
2201 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
2202 g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
2203 g_return_if_fail (parent == NULL || VALID_ITER (parent, tree_store));
2204 g_return_if_fail (new_order != NULL);
2207 level = G_NODE (tree_store->root)->children;
2209 level = G_NODE (parent->user_data)->children;
2219 /* set up sortarray */
2220 sort_array = g_new (SortTuple, length);
2223 for (i = 0; i < length; i++)
2225 sort_array[new_order[i]].offset = i;
2226 sort_array[i].node = node;
2231 g_qsort_with_data (sort_array,
2234 gtk_tree_store_reorder_func,
2238 for (i = 0; i < length - 1; i++)
2240 sort_array[i].node->next = sort_array[i+1].node;
2241 sort_array[i+1].node->prev = sort_array[i].node;
2244 sort_array[length-1].node->next = NULL;
2245 sort_array[0].node->prev = NULL;
2247 G_NODE (parent->user_data)->children = sort_array[0].node;
2249 G_NODE (tree_store->root)->children = sort_array[0].node;
2253 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), parent);
2255 path = gtk_tree_path_new ();
2256 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path,
2258 gtk_tree_path_free (path);
2259 g_free (sort_array);
2263 * gtk_tree_store_swap:
2264 * @tree_store: A #GtkTreeStore.
2265 * @a: A #GtkTreeIter.
2266 * @b: Another #GtkTreeIter.
2268 * Swaps @a and @b in the same level of @tree_store. Note that this function
2269 * only works with unsorted stores.
2274 gtk_tree_store_swap (GtkTreeStore *tree_store,
2278 GNode *tmp, *node_a, *node_b, *parent_node;
2279 GNode *a_prev, *a_next, *b_prev, *b_next;
2280 gint i, a_count, b_count, length, *order;
2281 GtkTreePath *path_a, *path_b;
2284 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
2285 g_return_if_fail (VALID_ITER (a, tree_store));
2286 g_return_if_fail (VALID_ITER (b, tree_store));
2288 node_a = G_NODE (a->user_data);
2289 node_b = G_NODE (b->user_data);
2291 /* basic sanity checking */
2292 if (node_a == node_b)
2295 path_a = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), a);
2296 path_b = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), b);
2298 g_return_if_fail (path_a && path_b);
2300 gtk_tree_path_up (path_a);
2301 gtk_tree_path_up (path_b);
2303 if (gtk_tree_path_get_depth (path_a) == 0
2304 || gtk_tree_path_get_depth (path_b) == 0)
2306 if (gtk_tree_path_get_depth (path_a) != gtk_tree_path_get_depth (path_b))
2308 gtk_tree_path_free (path_a);
2309 gtk_tree_path_free (path_b);
2311 g_warning ("Given children are not in the same level\n");
2314 parent_node = G_NODE (tree_store->root);
2318 if (gtk_tree_path_compare (path_a, path_b))
2320 gtk_tree_path_free (path_a);
2321 gtk_tree_path_free (path_b);
2323 g_warning ("Given children don't have a common parent\n");
2326 gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), &parent,
2328 parent_node = G_NODE (parent.user_data);
2330 gtk_tree_path_free (path_b);
2332 /* old links which we have to keep around */
2333 a_prev = node_a->prev;
2334 a_next = node_a->next;
2336 b_prev = node_b->prev;
2337 b_next = node_b->next;
2339 /* fix up links if the nodes are next to eachother */
2340 if (a_prev == node_b)
2342 if (a_next == node_b)
2345 if (b_prev == node_a)
2347 if (b_next == node_a)
2350 /* counting nodes */
2351 tmp = parent_node->children;
2352 i = a_count = b_count = 0;
2365 /* hacking the tree */
2367 parent_node->children = node_b;
2369 a_prev->next = node_b;
2372 a_next->prev = node_b;
2375 parent_node->children = node_a;
2377 b_prev->next = node_a;
2380 b_next->prev = node_a;
2382 node_a->prev = b_prev;
2383 node_a->next = b_next;
2385 node_b->prev = a_prev;
2386 node_b->next = a_next;
2389 order = g_new (gint, length);
2390 for (i = 0; i < length; i++)
2393 else if (i == b_count)
2398 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path_a,
2399 parent_node == tree_store->root
2400 ? NULL : &parent, order);
2401 gtk_tree_path_free (path_a);
2405 /* WARNING: this function is *incredibly* fragile. Please smashtest after
2406 * making changes here.
2410 gtk_tree_store_move (GtkTreeStore *tree_store,
2412 GtkTreeIter *position,
2415 GNode *parent, *node, *a, *b, *tmp, *tmp_a, *tmp_b;
2416 gint old_pos, new_pos, length, i, *order;
2417 GtkTreePath *path = NULL, *tmppath, *pos_path = NULL;
2418 GtkTreeIter parent_iter, dst_a, dst_b;
2420 gboolean handle_b = TRUE;
2422 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
2423 g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
2424 g_return_if_fail (VALID_ITER (iter, tree_store));
2426 g_return_if_fail (VALID_ITER (position, tree_store));
2433 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
2434 pos_path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store),
2438 * moving the iter before path or "path + 1" doesn't make sense
2440 * moving the iter before path or "path - 1" doesn't make sense
2442 if (!gtk_tree_path_compare (path, pos_path))
2443 goto free_paths_and_out;
2446 gtk_tree_path_next (path);
2448 gtk_tree_path_prev (path);
2450 if (!gtk_tree_path_compare (path, pos_path))
2451 goto free_paths_and_out;
2454 gtk_tree_path_prev (path);
2456 gtk_tree_path_next (path);
2458 if (gtk_tree_path_get_depth (path) != gtk_tree_path_get_depth (pos_path))
2460 g_warning ("Given children are not in the same level\n");
2462 goto free_paths_and_out;
2465 tmppath = gtk_tree_path_copy (pos_path);
2466 gtk_tree_path_up (path);
2467 gtk_tree_path_up (tmppath);
2469 if (gtk_tree_path_get_depth (path) > 0 &&
2470 gtk_tree_path_compare (path, tmppath))
2472 g_warning ("Given children are not in the same level\n");
2474 gtk_tree_path_free (tmppath);
2475 goto free_paths_and_out;
2478 gtk_tree_path_free (tmppath);
2483 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
2484 gtk_tree_path_up (path);
2487 depth = gtk_tree_path_get_depth (path);
2491 gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store),
2492 &parent_iter, path);
2494 parent = G_NODE (parent_iter.user_data);
2497 parent = G_NODE (tree_store->root);
2499 /* yes, I know that this can be done shorter, but I'm doing it this way
2500 * so the code is also maintainable
2503 if (before && position)
2505 b = G_NODE (position->user_data);
2507 if (gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path) - 1] > 0)
2509 gtk_tree_path_prev (pos_path);
2510 if (gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store),
2512 a = G_NODE (dst_a.user_data);
2515 gtk_tree_path_next (pos_path);
2518 /* if b is NULL, a is NULL too -- we are at the beginning of the list
2519 * yes and we leak memory here ...
2521 g_return_if_fail (b);
2523 else if (before && !position)
2525 /* move before without position is appending */
2532 a = G_NODE (position->user_data);
2538 gtk_tree_path_next (pos_path);
2539 if (gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), &dst_b, pos_path))
2540 b = G_NODE (dst_b.user_data);
2543 gtk_tree_path_prev (pos_path);
2547 /* move after without position is prepending */
2549 gtk_tree_store_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
2552 gtk_tree_store_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
2555 b = G_NODE (dst_b.user_data);
2558 /* if a is NULL, b is NULL too -- we are at the end of the list
2559 * yes and we leak memory here ...
2562 g_return_if_fail (a);
2565 /* counting nodes */
2566 tmp = parent->children;
2568 length = old_pos = 0;
2571 if (tmp == iter->user_data)
2578 /* remove node from list */
2579 node = G_NODE (iter->user_data);
2584 tmp_a->next = tmp_b;
2586 parent->children = tmp_b;
2589 tmp_b->prev = tmp_a;
2591 /* and reinsert the node */
2600 else if (!a && !before)
2602 tmp = parent->children;
2605 parent->children = node;
2613 else if (!a && before)
2617 node->parent = NULL;
2618 node->next = node->prev = NULL;
2620 /* before with sibling = NULL appends */
2621 g_node_insert_before (parent, NULL, node);
2625 node->parent = NULL;
2626 node->next = node->prev = NULL;
2628 /* after with sibling = NULL prepends */
2629 g_node_insert_after (parent, NULL, node);
2645 else if (!(!a && before)) /* !a && before is completely handled above */
2651 new_pos = gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path)-1];
2655 new_pos = gtk_tree_store_iter_n_children (GTK_TREE_MODEL (tree_store),
2658 new_pos = gtk_tree_store_iter_n_children (GTK_TREE_MODEL (tree_store),
2664 if (new_pos > old_pos)
2666 if (before && position)
2671 if (!before && position)
2675 order = g_new (gint, length);
2676 if (new_pos > old_pos)
2678 for (i = 0; i < length; i++)
2681 else if (i >= old_pos && i < new_pos)
2683 else if (i == new_pos)
2690 for (i = 0; i < length; i++)
2693 else if (i > new_pos && i <= old_pos)
2701 tmppath = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store),
2703 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
2704 tmppath, &parent_iter, order);
2708 tmppath = gtk_tree_path_new ();
2709 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
2710 tmppath, NULL, order);
2713 gtk_tree_path_free (tmppath);
2714 gtk_tree_path_free (path);
2716 gtk_tree_path_free (pos_path);
2722 gtk_tree_path_free (path);
2723 gtk_tree_path_free (pos_path);
2727 * gtk_tree_store_move_before:
2728 * @tree_store: A #GtkTreeStore.
2729 * @iter: A #GtkTreeIter.
2730 * @position: (allow-none): A #GtkTreeIter or %NULL.
2732 * Moves @iter in @tree_store to the position before @position. @iter and
2733 * @position should be in the same level. Note that this function only
2734 * works with unsorted stores. If @position is %NULL, @iter will be
2735 * moved to the end of the level.
2740 gtk_tree_store_move_before (GtkTreeStore *tree_store,
2742 GtkTreeIter *position)
2744 gtk_tree_store_move (tree_store, iter, position, TRUE);
2748 * gtk_tree_store_move_after:
2749 * @tree_store: A #GtkTreeStore.
2750 * @iter: A #GtkTreeIter.
2751 * @position: (allow-none): A #GtkTreeIter.
2753 * Moves @iter in @tree_store to the position after @position. @iter and
2754 * @position should be in the same level. Note that this function only
2755 * works with unsorted stores. If @position is %NULL, @iter will be moved
2756 * to the start of the level.
2761 gtk_tree_store_move_after (GtkTreeStore *tree_store,
2763 GtkTreeIter *position)
2765 gtk_tree_store_move (tree_store, iter, position, FALSE);
2770 gtk_tree_store_compare_func (gconstpointer a,
2774 GtkTreeStore *tree_store = user_data;
2777 GtkTreeIterCompareFunc func;
2784 if (tree_store->sort_column_id != -1)
2786 GtkTreeDataSortHeader *header;
2788 header = _gtk_tree_data_list_get_header (tree_store->sort_list,
2789 tree_store->sort_column_id);
2790 g_return_val_if_fail (header != NULL, 0);
2791 g_return_val_if_fail (header->func != NULL, 0);
2793 func = header->func;
2794 data = header->data;
2798 g_return_val_if_fail (tree_store->default_sort_func != NULL, 0);
2799 func = tree_store->default_sort_func;
2800 data = tree_store->default_sort_data;
2803 node_a = ((SortTuple *) a)->node;
2804 node_b = ((SortTuple *) b)->node;
2806 iter_a.stamp = tree_store->stamp;
2807 iter_a.user_data = node_a;
2808 iter_b.stamp = tree_store->stamp;
2809 iter_b.user_data = node_b;
2811 retval = (* func) (GTK_TREE_MODEL (user_data), &iter_a, &iter_b, data);
2813 if (tree_store->order == GTK_SORT_DESCENDING)
2817 else if (retval < 0)
2824 gtk_tree_store_sort_helper (GtkTreeStore *tree_store,
2837 node = parent->children;
2838 if (node == NULL || node->next == NULL)
2840 if (recurse && node && node->children)
2841 gtk_tree_store_sort_helper (tree_store, node, TRUE);
2847 for (tmp_node = node; tmp_node; tmp_node = tmp_node->next)
2850 sort_array = g_array_sized_new (FALSE, FALSE, sizeof (SortTuple), list_length);
2853 for (tmp_node = node; tmp_node; tmp_node = tmp_node->next)
2858 tuple.node = tmp_node;
2859 g_array_append_val (sort_array, tuple);
2863 /* Sort the array */
2864 g_array_sort_with_data (sort_array, gtk_tree_store_compare_func, tree_store);
2866 for (i = 0; i < list_length - 1; i++)
2868 g_array_index (sort_array, SortTuple, i).node->next =
2869 g_array_index (sort_array, SortTuple, i + 1).node;
2870 g_array_index (sort_array, SortTuple, i + 1).node->prev =
2871 g_array_index (sort_array, SortTuple, i).node;
2873 g_array_index (sort_array, SortTuple, list_length - 1).node->next = NULL;
2874 g_array_index (sort_array, SortTuple, 0).node->prev = NULL;
2875 parent->children = g_array_index (sort_array, SortTuple, 0).node;
2877 /* Let the world know about our new order */
2878 new_order = g_new (gint, list_length);
2879 for (i = 0; i < list_length; i++)
2880 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
2882 iter.stamp = tree_store->stamp;
2883 iter.user_data = parent;
2884 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), &iter);
2885 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
2886 path, &iter, new_order);
2887 gtk_tree_path_free (path);
2889 g_array_free (sort_array, TRUE);
2893 for (tmp_node = parent->children; tmp_node; tmp_node = tmp_node->next)
2895 if (tmp_node->children)
2896 gtk_tree_store_sort_helper (tree_store, tmp_node, TRUE);
2902 gtk_tree_store_sort (GtkTreeStore *tree_store)
2904 if (!GTK_TREE_STORE_IS_SORTED (tree_store))
2907 if (tree_store->sort_column_id != -1)
2909 GtkTreeDataSortHeader *header = NULL;
2911 header = _gtk_tree_data_list_get_header (tree_store->sort_list,
2912 tree_store->sort_column_id);
2914 /* We want to make sure that we have a function */
2915 g_return_if_fail (header != NULL);
2916 g_return_if_fail (header->func != NULL);
2920 g_return_if_fail (tree_store->default_sort_func != NULL);
2923 gtk_tree_store_sort_helper (tree_store, G_NODE (tree_store->root), TRUE);
2927 gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
2930 gboolean emit_signal)
2935 GtkTreePath *tmp_path;
2936 GtkTreeIter tmp_iter;
2944 GtkTreeIterCompareFunc func;
2947 g_return_if_fail (G_NODE (iter->user_data)->parent != NULL);
2949 tmp_iter.stamp = tree_store->stamp;
2950 if (tree_store->sort_column_id != -1)
2952 GtkTreeDataSortHeader *header;
2953 header = _gtk_tree_data_list_get_header (tree_store->sort_list,
2954 tree_store->sort_column_id);
2955 g_return_if_fail (header != NULL);
2956 g_return_if_fail (header->func != NULL);
2957 func = header->func;
2958 data = header->data;
2962 g_return_if_fail (tree_store->default_sort_func != NULL);
2963 func = tree_store->default_sort_func;
2964 data = tree_store->default_sort_data;
2967 /* If it's the built in function, we don't sort. */
2968 if (func == _gtk_tree_data_list_compare_func &&
2969 tree_store->sort_column_id != column)
2973 node = G_NODE (iter->user_data)->parent->children;
2974 /* First we find the iter, its prev, and its next */
2977 if (node == G_NODE (iter->user_data))
2982 g_assert (node != NULL);
2987 /* Check the common case, where we don't need to sort it moved. */
2990 tmp_iter.user_data = prev;
2991 cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
2996 tmp_iter.user_data = next;
2997 cmp_b = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
3000 if (tree_store->order == GTK_SORT_DESCENDING)
3013 if (prev == NULL && cmp_b <= 0)
3015 else if (next == NULL && cmp_a <= 0)
3017 else if (prev != NULL && next != NULL &&
3018 cmp_a <= 0 && cmp_b <= 0)
3021 /* We actually need to sort it */
3022 /* First, remove the old link. */
3027 node->parent->children = next;
3035 /* FIXME: as an optimization, we can potentially start at next */
3037 node = node->parent->children;
3039 tmp_iter.user_data = node;
3040 if (tree_store->order == GTK_SORT_DESCENDING)
3041 cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
3043 cmp_a = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
3045 while ((node->next) && (cmp_a > 0))
3050 tmp_iter.user_data = node;
3051 if (tree_store->order == GTK_SORT_DESCENDING)
3052 cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
3054 cmp_a = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
3057 if ((!node->next) && (cmp_a > 0))
3060 node->next = G_NODE (iter->user_data);
3061 node->next->prev = node;
3065 prev->next = G_NODE (iter->user_data);
3066 prev->next->prev = prev;
3067 G_NODE (iter->user_data)->next = node;
3068 G_NODE (iter->user_data)->next->prev = G_NODE (iter->user_data);
3072 G_NODE (iter->user_data)->next = G_NODE (iter->user_data)->parent->children;
3073 G_NODE (iter->user_data)->next->prev = G_NODE (iter->user_data);
3074 G_NODE (iter->user_data)->parent->children = G_NODE (iter->user_data);
3080 /* Emit the reordered signal. */
3081 length = g_node_n_children (node->parent);
3082 new_order = g_new (int, length);
3083 if (old_location < new_location)
3084 for (i = 0; i < length; i++)
3086 if (i < old_location ||
3089 else if (i >= old_location &&
3091 new_order[i] = i + 1;
3092 else if (i == new_location)
3093 new_order[i] = old_location;
3096 for (i = 0; i < length; i++)
3098 if (i < new_location ||
3101 else if (i > new_location &&
3103 new_order[i] = i - 1;
3104 else if (i == new_location)
3105 new_order[i] = old_location;
3108 tmp_iter.user_data = node->parent;
3109 tmp_path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), &tmp_iter);
3111 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
3112 tmp_path, &tmp_iter,
3115 gtk_tree_path_free (tmp_path);
3121 gtk_tree_store_get_sort_column_id (GtkTreeSortable *sortable,
3122 gint *sort_column_id,
3125 GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3128 * sort_column_id = tree_store->sort_column_id;
3130 * order = tree_store->order;
3132 if (tree_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
3133 tree_store->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
3140 gtk_tree_store_set_sort_column_id (GtkTreeSortable *sortable,
3141 gint sort_column_id,
3144 GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3147 if ((tree_store->sort_column_id == sort_column_id) &&
3148 (tree_store->order == order))
3151 if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
3153 if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
3155 GtkTreeDataSortHeader *header = NULL;
3157 header = _gtk_tree_data_list_get_header (tree_store->sort_list,
3160 /* We want to make sure that we have a function */
3161 g_return_if_fail (header != NULL);
3162 g_return_if_fail (header->func != NULL);
3166 g_return_if_fail (tree_store->default_sort_func != NULL);
3170 tree_store->sort_column_id = sort_column_id;
3171 tree_store->order = order;
3173 gtk_tree_sortable_sort_column_changed (sortable);
3175 gtk_tree_store_sort (tree_store);
3179 gtk_tree_store_set_sort_func (GtkTreeSortable *sortable,
3180 gint sort_column_id,
3181 GtkTreeIterCompareFunc func,
3183 GDestroyNotify destroy)
3185 GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3187 tree_store->sort_list = _gtk_tree_data_list_set_header (tree_store->sort_list,
3189 func, data, destroy);
3191 if (tree_store->sort_column_id == sort_column_id)
3192 gtk_tree_store_sort (tree_store);
3196 gtk_tree_store_set_default_sort_func (GtkTreeSortable *sortable,
3197 GtkTreeIterCompareFunc func,
3199 GDestroyNotify destroy)
3201 GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3203 if (tree_store->default_sort_destroy)
3205 GDestroyNotify d = tree_store->default_sort_destroy;
3207 tree_store->default_sort_destroy = NULL;
3208 d (tree_store->default_sort_data);
3211 tree_store->default_sort_func = func;
3212 tree_store->default_sort_data = data;
3213 tree_store->default_sort_destroy = destroy;
3215 if (tree_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
3216 gtk_tree_store_sort (tree_store);
3220 gtk_tree_store_has_default_sort_func (GtkTreeSortable *sortable)
3222 GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3224 return (tree_store->default_sort_func != NULL);
3228 validate_gnode (GNode* node)
3232 iter = node->children;
3233 while (iter != NULL)
3235 g_assert (iter->parent == node);
3237 g_assert (iter->prev->next == iter);
3238 validate_gnode (iter);
3243 /* GtkBuildable custom tag implementation
3246 * <column type="..."/>
3247 * <column type="..."/>
3251 GtkBuilder *builder;
3254 } GSListSubParserData;
3257 tree_model_start_element (GMarkupParseContext *context,
3258 const gchar *element_name,
3259 const gchar **names,
3260 const gchar **values,
3265 GSListSubParserData *data = (GSListSubParserData*)user_data;
3267 for (i = 0; names[i]; i++)
3269 if (strcmp (names[i], "type") == 0)
3270 data->items = g_slist_prepend (data->items, g_strdup (values[i]));
3275 tree_model_end_element (GMarkupParseContext *context,
3276 const gchar *element_name,
3280 GSListSubParserData *data = (GSListSubParserData*)user_data;
3282 g_assert(data->builder);
3284 if (strcmp (element_name, "columns") == 0)
3291 data = (GSListSubParserData*)user_data;
3292 data->items = g_slist_reverse (data->items);
3293 types = g_new0 (GType, g_slist_length (data->items));
3295 for (l = data->items, i = 0; l; l = l->next, i++)
3297 type = gtk_builder_get_type_from_name (data->builder, l->data);
3298 if (type == G_TYPE_INVALID)
3300 g_warning ("Unknown type %s specified in treemodel %s",
3301 (const gchar*)l->data,
3302 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
3310 gtk_tree_store_set_column_types (GTK_TREE_STORE (data->object), i, types);
3316 static const GMarkupParser tree_model_parser =
3318 tree_model_start_element,
3319 tree_model_end_element
3324 gtk_tree_store_buildable_custom_tag_start (GtkBuildable *buildable,
3325 GtkBuilder *builder,
3327 const gchar *tagname,
3328 GMarkupParser *parser,
3331 GSListSubParserData *parser_data;
3336 if (strcmp (tagname, "columns") == 0)
3338 parser_data = g_slice_new0 (GSListSubParserData);
3339 parser_data->builder = builder;
3340 parser_data->items = NULL;
3341 parser_data->object = G_OBJECT (buildable);
3343 *parser = tree_model_parser;
3344 *data = parser_data;
3352 gtk_tree_store_buildable_custom_finished (GtkBuildable *buildable,
3353 GtkBuilder *builder,
3355 const gchar *tagname,
3358 GSListSubParserData *data;
3360 if (strcmp (tagname, "columns"))
3363 data = (GSListSubParserData*)user_data;
3365 g_slist_free (data->items);
3366 g_slice_free (GSListSubParserData, data);