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"
30 #define G_NODE(node) ((GNode *)node)
31 #define GTK_TREE_STORE_IS_SORTED(tree) (((GtkTreeStore*)(tree))->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
32 #define VALID_ITER(iter, tree_store) ((iter)!= NULL && (iter)->user_data != NULL && ((GtkTreeStore*)(tree_store))->stamp == (iter)->stamp)
34 static void gtk_tree_store_tree_model_init (GtkTreeModelIface *iface);
35 static void gtk_tree_store_drag_source_init(GtkTreeDragSourceIface *iface);
36 static void gtk_tree_store_drag_dest_init (GtkTreeDragDestIface *iface);
37 static void gtk_tree_store_sortable_init (GtkTreeSortableIface *iface);
38 static void gtk_tree_store_finalize (GObject *object);
39 static GtkTreeModelFlags gtk_tree_store_get_flags (GtkTreeModel *tree_model);
40 static gint gtk_tree_store_get_n_columns (GtkTreeModel *tree_model);
41 static GType gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
43 static gboolean gtk_tree_store_get_iter (GtkTreeModel *tree_model,
46 static GtkTreePath *gtk_tree_store_get_path (GtkTreeModel *tree_model,
48 static void gtk_tree_store_get_value (GtkTreeModel *tree_model,
52 static gboolean gtk_tree_store_iter_next (GtkTreeModel *tree_model,
54 static gboolean gtk_tree_store_iter_children (GtkTreeModel *tree_model,
57 static gboolean gtk_tree_store_iter_has_child (GtkTreeModel *tree_model,
59 static gint gtk_tree_store_iter_n_children (GtkTreeModel *tree_model,
61 static gboolean gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
65 static gboolean gtk_tree_store_iter_parent (GtkTreeModel *tree_model,
70 static void gtk_tree_store_set_n_columns (GtkTreeStore *tree_store,
72 static void gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
76 static void gtk_tree_store_increment_stamp (GtkTreeStore *tree_store);
80 static gboolean real_gtk_tree_store_row_draggable (GtkTreeDragSource *drag_source,
82 static gboolean gtk_tree_store_drag_data_delete (GtkTreeDragSource *drag_source,
84 static gboolean gtk_tree_store_drag_data_get (GtkTreeDragSource *drag_source,
86 GtkSelectionData *selection_data);
87 static gboolean gtk_tree_store_drag_data_received (GtkTreeDragDest *drag_dest,
89 GtkSelectionData *selection_data);
90 static gboolean gtk_tree_store_row_drop_possible (GtkTreeDragDest *drag_dest,
91 GtkTreePath *dest_path,
92 GtkSelectionData *selection_data);
94 /* Sortable Interfaces */
96 static void gtk_tree_store_sort (GtkTreeStore *tree_store);
97 static void gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
100 gboolean emit_signal);
101 static gboolean gtk_tree_store_get_sort_column_id (GtkTreeSortable *sortable,
102 gint *sort_column_id,
104 static void gtk_tree_store_set_sort_column_id (GtkTreeSortable *sortable,
107 static void gtk_tree_store_set_sort_func (GtkTreeSortable *sortable,
109 GtkTreeIterCompareFunc func,
111 GtkDestroyNotify destroy);
112 static void gtk_tree_store_set_default_sort_func (GtkTreeSortable *sortable,
113 GtkTreeIterCompareFunc func,
115 GtkDestroyNotify destroy);
116 static gboolean gtk_tree_store_has_default_sort_func (GtkTreeSortable *sortable);
118 static void validate_gnode (GNode *node);
120 static void gtk_tree_store_move (GtkTreeStore *tree_store,
122 GtkTreeIter *position,
127 validate_tree (GtkTreeStore *tree_store)
129 if (gtk_debug_flags & GTK_DEBUG_TREE)
131 g_assert (G_NODE (tree_store->root)->parent == NULL);
133 validate_gnode (G_NODE (tree_store->root));
137 G_DEFINE_TYPE_WITH_CODE (GtkTreeStore, gtk_tree_store, G_TYPE_OBJECT,
138 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
139 gtk_tree_store_tree_model_init)
140 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
141 gtk_tree_store_drag_source_init)
142 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
143 gtk_tree_store_drag_dest_init)
144 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
145 gtk_tree_store_sortable_init))
148 gtk_tree_store_class_init (GtkTreeStoreClass *class)
150 GObjectClass *object_class;
152 object_class = (GObjectClass *) class;
154 object_class->finalize = gtk_tree_store_finalize;
158 gtk_tree_store_tree_model_init (GtkTreeModelIface *iface)
160 iface->get_flags = gtk_tree_store_get_flags;
161 iface->get_n_columns = gtk_tree_store_get_n_columns;
162 iface->get_column_type = gtk_tree_store_get_column_type;
163 iface->get_iter = gtk_tree_store_get_iter;
164 iface->get_path = gtk_tree_store_get_path;
165 iface->get_value = gtk_tree_store_get_value;
166 iface->iter_next = gtk_tree_store_iter_next;
167 iface->iter_children = gtk_tree_store_iter_children;
168 iface->iter_has_child = gtk_tree_store_iter_has_child;
169 iface->iter_n_children = gtk_tree_store_iter_n_children;
170 iface->iter_nth_child = gtk_tree_store_iter_nth_child;
171 iface->iter_parent = gtk_tree_store_iter_parent;
175 gtk_tree_store_drag_source_init (GtkTreeDragSourceIface *iface)
177 iface->row_draggable = real_gtk_tree_store_row_draggable;
178 iface->drag_data_delete = gtk_tree_store_drag_data_delete;
179 iface->drag_data_get = gtk_tree_store_drag_data_get;
183 gtk_tree_store_drag_dest_init (GtkTreeDragDestIface *iface)
185 iface->drag_data_received = gtk_tree_store_drag_data_received;
186 iface->row_drop_possible = gtk_tree_store_row_drop_possible;
190 gtk_tree_store_sortable_init (GtkTreeSortableIface *iface)
192 iface->get_sort_column_id = gtk_tree_store_get_sort_column_id;
193 iface->set_sort_column_id = gtk_tree_store_set_sort_column_id;
194 iface->set_sort_func = gtk_tree_store_set_sort_func;
195 iface->set_default_sort_func = gtk_tree_store_set_default_sort_func;
196 iface->has_default_sort_func = gtk_tree_store_has_default_sort_func;
200 gtk_tree_store_init (GtkTreeStore *tree_store)
202 tree_store->root = g_node_new (NULL);
203 /* While the odds are against us getting 0...
207 tree_store->stamp = g_random_int ();
209 while (tree_store->stamp == 0);
211 tree_store->sort_list = NULL;
212 tree_store->sort_column_id = -2;
213 tree_store->columns_dirty = FALSE;
217 * gtk_tree_store_new:
218 * @n_columns: number of columns in the tree store
219 * @Varargs: all #GType types for the columns, from first to last
221 * Creates a new tree store as with @n_columns columns each of the types passed
222 * in. Note that only types derived from standard GObject fundamental types
225 * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
226 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkTreeStore with three columns, of type
227 * <type>int</type>, <type>string</type> and #GdkPixbuf respectively.
229 * Return value: a new #GtkTreeStore
232 gtk_tree_store_new (gint n_columns,
235 GtkTreeStore *retval;
239 g_return_val_if_fail (n_columns > 0, NULL);
241 retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
242 gtk_tree_store_set_n_columns (retval, n_columns);
244 va_start (args, n_columns);
246 for (i = 0; i < n_columns; i++)
248 GType type = va_arg (args, GType);
249 if (! _gtk_tree_data_list_check_type (type))
251 g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n",
252 G_STRLOC, g_type_name (type));
253 g_object_unref (retval);
256 gtk_tree_store_set_column_type (retval, i, type);
263 * gtk_tree_store_newv:
264 * @n_columns: number of columns in the tree store
265 * @types: an array of #GType types for the columns, from first to last
267 * Non vararg creation function. Used primarily by language bindings.
269 * Return value: a new #GtkTreeStore
272 gtk_tree_store_newv (gint n_columns,
275 GtkTreeStore *retval;
278 g_return_val_if_fail (n_columns > 0, NULL);
280 retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
281 gtk_tree_store_set_n_columns (retval, n_columns);
283 for (i = 0; i < n_columns; i++)
285 if (! _gtk_tree_data_list_check_type (types[i]))
287 g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n",
288 G_STRLOC, g_type_name (types[i]));
289 g_object_unref (retval);
292 gtk_tree_store_set_column_type (retval, i, types[i]);
300 * gtk_tree_store_set_column_types:
301 * @tree_store: A #GtkTreeStore
302 * @n_columns: Number of columns for the tree store
303 * @types: An array of #GType types, one for each column
305 * This function is meant primarily for #GObjects that inherit from
306 * #GtkTreeStore, and should only be used when constructing a new
307 * #GtkTreeStore. It will not function after a row has been added,
308 * or a method on the #GtkTreeModel interface is called.
311 gtk_tree_store_set_column_types (GtkTreeStore *tree_store,
317 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
318 g_return_if_fail (tree_store->columns_dirty == 0);
320 gtk_tree_store_set_n_columns (tree_store, n_columns);
321 for (i = 0; i < n_columns; i++)
323 if (! _gtk_tree_data_list_check_type (types[i]))
325 g_warning ("%s: Invalid type %s passed to gtk_tree_store_set_column_types\n", G_STRLOC, g_type_name (types[i]));
328 gtk_tree_store_set_column_type (tree_store, i, types[i]);
333 gtk_tree_store_set_n_columns (GtkTreeStore *tree_store,
338 if (tree_store->n_columns == n_columns)
341 new_columns = g_new0 (GType, n_columns);
342 if (tree_store->column_headers)
344 /* copy the old header orders over */
345 if (n_columns >= tree_store->n_columns)
346 memcpy (new_columns, tree_store->column_headers, tree_store->n_columns * sizeof (gchar *));
348 memcpy (new_columns, tree_store->column_headers, n_columns * sizeof (GType));
350 g_free (tree_store->column_headers);
353 if (tree_store->sort_list)
354 _gtk_tree_data_list_header_free (tree_store->sort_list);
356 tree_store->sort_list = _gtk_tree_data_list_header_new (n_columns, tree_store->column_headers);
358 tree_store->column_headers = new_columns;
359 tree_store->n_columns = n_columns;
363 * gtk_tree_store_set_column_type:
364 * @tree_store: a #GtkTreeStore
365 * @column: column number
366 * @type: type of the data to be stored in @column
368 * Supported types include: %G_TYPE_UINT, %G_TYPE_INT, %G_TYPE_UCHAR,
369 * %G_TYPE_CHAR, %G_TYPE_BOOLEAN, %G_TYPE_POINTER, %G_TYPE_FLOAT,
370 * %G_TYPE_DOUBLE, %G_TYPE_STRING, %G_TYPE_OBJECT, and %G_TYPE_BOXED, along with
371 * subclasses of those types such as %GDK_TYPE_PIXBUF.
375 gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
379 if (!_gtk_tree_data_list_check_type (type))
381 g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n", G_STRLOC, g_type_name (type));
384 tree_store->column_headers[column] = type;
388 node_free (GNode *node, gpointer data)
391 _gtk_tree_data_list_free (node->data, (GType*)data);
398 gtk_tree_store_finalize (GObject *object)
400 GtkTreeStore *tree_store = GTK_TREE_STORE (object);
402 g_node_traverse (tree_store->root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
403 node_free, tree_store->column_headers);
404 g_node_destroy (tree_store->root);
405 _gtk_tree_data_list_header_free (tree_store->sort_list);
406 g_free (tree_store->column_headers);
408 if (tree_store->default_sort_destroy)
410 GtkDestroyNotify d = tree_store->default_sort_destroy;
412 tree_store->default_sort_destroy = NULL;
413 d (tree_store->default_sort_data);
414 tree_store->default_sort_data = NULL;
418 G_OBJECT_CLASS (gtk_tree_store_parent_class)->finalize (object);
421 /* fulfill the GtkTreeModel requirements */
422 /* NOTE: GtkTreeStore::root is a GNode, that acts as the parent node. However,
423 * it is not visible to the tree or to the user., and the path "0" refers to the
424 * first child of GtkTreeStore::root.
428 static GtkTreeModelFlags
429 gtk_tree_store_get_flags (GtkTreeModel *tree_model)
431 return GTK_TREE_MODEL_ITERS_PERSIST;
435 gtk_tree_store_get_n_columns (GtkTreeModel *tree_model)
437 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
439 tree_store->columns_dirty = TRUE;
441 return tree_store->n_columns;
445 gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
448 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
450 g_return_val_if_fail (index < tree_store->n_columns, G_TYPE_INVALID);
452 tree_store->columns_dirty = TRUE;
454 return tree_store->column_headers[index];
458 gtk_tree_store_get_iter (GtkTreeModel *tree_model,
462 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
467 tree_store->columns_dirty = TRUE;
469 indices = gtk_tree_path_get_indices (path);
470 depth = gtk_tree_path_get_depth (path);
472 g_return_val_if_fail (depth > 0, FALSE);
474 parent.stamp = tree_store->stamp;
475 parent.user_data = tree_store->root;
477 if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[0]))
480 for (i = 1; i < depth; i++)
483 if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[i]))
491 gtk_tree_store_get_path (GtkTreeModel *tree_model,
494 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
499 g_return_val_if_fail (iter->user_data != NULL, NULL);
500 g_return_val_if_fail (iter->stamp == tree_store->stamp, NULL);
502 validate_tree (tree_store);
504 if (G_NODE (iter->user_data)->parent == NULL &&
505 G_NODE (iter->user_data) == tree_store->root)
506 return gtk_tree_path_new ();
507 g_assert (G_NODE (iter->user_data)->parent != NULL);
509 if (G_NODE (iter->user_data)->parent == G_NODE (tree_store->root))
511 retval = gtk_tree_path_new ();
512 tmp_node = G_NODE (tree_store->root)->children;
516 GtkTreeIter tmp_iter = *iter;
518 tmp_iter.user_data = G_NODE (iter->user_data)->parent;
520 retval = gtk_tree_store_get_path (tree_model, &tmp_iter);
521 tmp_node = G_NODE (iter->user_data)->parent->children;
527 if (tmp_node == NULL)
529 gtk_tree_path_free (retval);
533 for (; tmp_node; tmp_node = tmp_node->next)
535 if (tmp_node == G_NODE (iter->user_data))
540 if (tmp_node == NULL)
542 /* We couldn't find node, meaning it's prolly not ours */
543 /* Perhaps I should do a g_return_if_fail here. */
544 gtk_tree_path_free (retval);
548 gtk_tree_path_append_index (retval, i);
555 gtk_tree_store_get_value (GtkTreeModel *tree_model,
560 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
561 GtkTreeDataList *list;
562 gint tmp_column = column;
564 g_return_if_fail (column < tree_store->n_columns);
565 g_return_if_fail (VALID_ITER (iter, tree_store));
567 list = G_NODE (iter->user_data)->data;
569 while (tmp_column-- > 0 && list)
574 _gtk_tree_data_list_node_to_value (list,
575 tree_store->column_headers[column],
580 /* We want to return an initialized but empty (default) value */
581 g_value_init (value, tree_store->column_headers[column]);
586 gtk_tree_store_iter_next (GtkTreeModel *tree_model,
589 g_return_val_if_fail (iter->user_data != NULL, FALSE);
590 g_return_val_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
592 if (G_NODE (iter->user_data)->next)
594 iter->user_data = G_NODE (iter->user_data)->next;
602 gtk_tree_store_iter_children (GtkTreeModel *tree_model,
606 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
610 g_return_val_if_fail (VALID_ITER (parent, tree_store), FALSE);
613 children = G_NODE (parent->user_data)->children;
615 children = G_NODE (tree_store->root)->children;
619 iter->stamp = tree_store->stamp;
620 iter->user_data = children;
628 gtk_tree_store_iter_has_child (GtkTreeModel *tree_model,
631 g_return_val_if_fail (iter->user_data != NULL, FALSE);
632 g_return_val_if_fail (VALID_ITER (iter, tree_model), FALSE);
634 return G_NODE (iter->user_data)->children != NULL;
638 gtk_tree_store_iter_n_children (GtkTreeModel *tree_model,
644 g_return_val_if_fail (iter == NULL || iter->user_data != NULL, 0);
647 node = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
649 node = G_NODE (iter->user_data)->children;
661 gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
666 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
670 g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
673 parent_node = tree_store->root;
675 parent_node = parent->user_data;
677 child = g_node_nth_child (parent_node, n);
681 iter->user_data = child;
682 iter->stamp = tree_store->stamp;
690 gtk_tree_store_iter_parent (GtkTreeModel *tree_model,
694 GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
697 g_return_val_if_fail (iter != NULL, FALSE);
698 g_return_val_if_fail (VALID_ITER (child, tree_store), FALSE);
700 parent = G_NODE (child->user_data)->parent;
702 g_assert (parent != NULL);
704 if (parent != tree_store->root)
706 iter->user_data = parent;
707 iter->stamp = tree_store->stamp;
715 /* Does not emit a signal */
717 gtk_tree_store_real_set_value (GtkTreeStore *tree_store,
723 GtkTreeDataList *list;
724 GtkTreeDataList *prev;
725 gint old_column = column;
726 GValue real_value = {0, };
727 gboolean converted = FALSE;
728 gboolean retval = FALSE;
730 if (! g_type_is_a (G_VALUE_TYPE (value), tree_store->column_headers[column]))
732 if (! (g_value_type_compatible (G_VALUE_TYPE (value), tree_store->column_headers[column]) &&
733 g_value_type_compatible (tree_store->column_headers[column], G_VALUE_TYPE (value))))
735 g_warning ("%s: Unable to convert from %s to %s\n",
737 g_type_name (G_VALUE_TYPE (value)),
738 g_type_name (tree_store->column_headers[column]));
741 if (!g_value_transform (value, &real_value))
743 g_warning ("%s: Unable to make conversion from %s to %s\n",
745 g_type_name (G_VALUE_TYPE (value)),
746 g_type_name (tree_store->column_headers[column]));
747 g_value_unset (&real_value);
753 prev = list = G_NODE (iter->user_data)->data;
760 _gtk_tree_data_list_value_to_node (list, &real_value);
762 _gtk_tree_data_list_value_to_node (list, value);
765 g_value_unset (&real_value);
766 if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
767 gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
776 if (G_NODE (iter->user_data)->data == NULL)
778 G_NODE (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
783 list = prev->next = _gtk_tree_data_list_alloc ();
789 list->next = _gtk_tree_data_list_alloc ();
796 _gtk_tree_data_list_value_to_node (list, &real_value);
798 _gtk_tree_data_list_value_to_node (list, value);
802 g_value_unset (&real_value);
804 if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
805 gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
811 * gtk_tree_store_set_value:
812 * @tree_store: a #GtkTreeStore
813 * @iter: A valid #GtkTreeIter for the row being modified
814 * @column: column number to modify
815 * @value: new value for the cell
817 * Sets the data in the cell specified by @iter and @column.
818 * The type of @value must be convertible to the type of the
823 gtk_tree_store_set_value (GtkTreeStore *tree_store,
828 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
829 g_return_if_fail (VALID_ITER (iter, tree_store));
830 g_return_if_fail (column >= 0 && column < tree_store->n_columns);
831 g_return_if_fail (G_IS_VALUE (value));
833 if (gtk_tree_store_real_set_value (tree_store, iter, column, value, TRUE))
837 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
838 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
839 gtk_tree_path_free (path);
844 gtk_tree_store_set_valist_internal (GtkTreeStore *tree_store,
846 gboolean *emit_signal,
847 gboolean *maybe_need_sort,
851 GtkTreeIterCompareFunc func = NULL;
853 column = va_arg (var_args, gint);
855 if (GTK_TREE_STORE_IS_SORTED (tree_store))
857 if (tree_store->sort_column_id != -1)
859 GtkTreeDataSortHeader *header;
860 header = _gtk_tree_data_list_get_header (tree_store->sort_list,
861 tree_store->sort_column_id);
862 g_return_if_fail (header != NULL);
863 g_return_if_fail (header->func != NULL);
868 func = tree_store->default_sort_func;
872 if (func != _gtk_tree_data_list_compare_func)
873 *maybe_need_sort = TRUE;
877 GValue value = { 0, };
880 if (column >= tree_store->n_columns)
882 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
885 g_value_init (&value, tree_store->column_headers[column]);
887 G_VALUE_COLLECT (&value, var_args, 0, &error);
890 g_warning ("%s: %s", G_STRLOC, error);
893 /* we purposely leak the value here, it might not be
894 * in a sane state if an error condition occoured
899 *emit_signal = gtk_tree_store_real_set_value (tree_store,
903 FALSE) || *emit_signal;
905 if (func == _gtk_tree_data_list_compare_func &&
906 column == tree_store->sort_column_id)
907 *maybe_need_sort = TRUE;
909 g_value_unset (&value);
911 column = va_arg (var_args, gint);
916 * gtk_tree_store_set_valist:
917 * @tree_store: A #GtkTreeStore
918 * @iter: A valid #GtkTreeIter for the row being modified
919 * @var_args: <type>va_list</type> of column/value pairs
921 * See gtk_tree_store_set(); this version takes a <type>va_list</type> for
922 * use by language bindings.
926 gtk_tree_store_set_valist (GtkTreeStore *tree_store,
930 gboolean emit_signal = FALSE;
931 gboolean maybe_need_sort = FALSE;
933 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
934 g_return_if_fail (VALID_ITER (iter, tree_store));
936 gtk_tree_store_set_valist_internal (tree_store, iter,
941 if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
942 gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, TRUE);
948 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
949 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
950 gtk_tree_path_free (path);
955 * gtk_tree_store_set:
956 * @tree_store: A #GtkTreeStore
957 * @iter: A valid #GtkTreeIter for the row being modified
958 * @Varargs: pairs of column number and value, terminated with -1
960 * Sets the value of one or more cells in the row referenced by @iter.
961 * The variable argument list should contain integer column numbers,
962 * each column number followed by the value to be set.
963 * The list is terminated by a -1. For example, to set column 0 with type
964 * %G_TYPE_STRING to "Foo", you would write
965 * <literal>gtk_tree_store_set (store, iter, 0, "Foo", -1)</literal>.
968 gtk_tree_store_set (GtkTreeStore *tree_store,
974 va_start (var_args, iter);
975 gtk_tree_store_set_valist (tree_store, iter, var_args);
980 * gtk_tree_store_remove:
981 * @tree_store: A #GtkTreeStore
982 * @iter: A valid #GtkTreeIter
984 * Removes @iter from @tree_store. After being removed, @iter is set to the
985 * next valid row at that level, or invalidated if it previously pointed to the
988 * Return value: %TRUE if @iter is still valid, %FALSE if not.
991 gtk_tree_store_remove (GtkTreeStore *tree_store,
995 GtkTreeIter new_iter = {0,};
999 g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
1000 g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
1002 parent = G_NODE (iter->user_data)->parent;
1004 g_assert (parent != NULL);
1005 next_node = G_NODE (iter->user_data)->next;
1007 if (G_NODE (iter->user_data)->data)
1008 g_node_traverse (G_NODE (iter->user_data), G_POST_ORDER, G_TRAVERSE_ALL,
1009 -1, node_free, tree_store->column_headers);
1011 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1012 g_node_destroy (G_NODE (iter->user_data));
1014 gtk_tree_model_row_deleted (GTK_TREE_MODEL (tree_store), path);
1016 if (parent != G_NODE (tree_store->root))
1019 if (parent->children == NULL)
1021 gtk_tree_path_up (path);
1023 new_iter.stamp = tree_store->stamp;
1024 new_iter.user_data = parent;
1025 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &new_iter);
1028 gtk_tree_path_free (path);
1030 /* revalidate iter */
1031 if (next_node != NULL)
1033 iter->stamp = tree_store->stamp;
1034 iter->user_data = next_node;
1040 iter->user_data = NULL;
1047 * gtk_tree_store_insert:
1048 * @tree_store: A #GtkTreeStore
1049 * @iter: An unset #GtkTreeIter to set to the new row
1050 * @parent: A valid #GtkTreeIter, or %NULL
1051 * @position: position to insert the new row
1053 * Creates a new row at @position. If parent is non-%NULL, then the row will be
1054 * made a child of @parent. Otherwise, the row will be created at the toplevel.
1055 * If @position is larger than the number of rows at that level, then the new
1056 * row will be inserted to the end of the list. @iter will be changed to point
1057 * to this new row. The row will be empty after this function is called. To
1058 * fill in values, you need to call gtk_tree_store_set() or
1059 * gtk_tree_store_set_value().
1063 gtk_tree_store_insert (GtkTreeStore *tree_store,
1065 GtkTreeIter *parent,
1072 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1073 g_return_if_fail (iter != NULL);
1075 g_return_if_fail (VALID_ITER (parent, tree_store));
1078 parent_node = parent->user_data;
1080 parent_node = tree_store->root;
1082 tree_store->columns_dirty = TRUE;
1084 new_node = g_node_new (NULL);
1086 iter->stamp = tree_store->stamp;
1087 iter->user_data = new_node;
1088 g_node_insert (parent_node, position, new_node);
1090 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1091 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1093 if (parent_node != tree_store->root)
1095 if (new_node->prev == NULL && new_node->next == NULL)
1097 gtk_tree_path_up (path);
1098 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1102 gtk_tree_path_free (path);
1104 validate_tree ((GtkTreeStore*)tree_store);
1108 * gtk_tree_store_insert_before:
1109 * @tree_store: A #GtkTreeStore
1110 * @iter: An unset #GtkTreeIter to set to the new row
1111 * @parent: A valid #GtkTreeIter, or %NULL
1112 * @sibling: A valid #GtkTreeIter, or %NULL
1114 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will
1115 * be appended to @parent 's children. If @parent and @sibling are %NULL, then
1116 * the row will be appended to the toplevel. If both @sibling and @parent are
1117 * set, then @parent must be the parent of @sibling. When @sibling is set,
1118 * @parent is optional.
1120 * @iter will be changed to point to this new row. The row will be empty after
1121 * this function is called. To fill in values, you need to call
1122 * gtk_tree_store_set() or gtk_tree_store_set_value().
1126 gtk_tree_store_insert_before (GtkTreeStore *tree_store,
1128 GtkTreeIter *parent,
1129 GtkTreeIter *sibling)
1132 GNode *parent_node = NULL;
1135 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1136 g_return_if_fail (iter != NULL);
1138 g_return_if_fail (VALID_ITER (parent, tree_store));
1139 if (sibling != NULL)
1140 g_return_if_fail (VALID_ITER (sibling, tree_store));
1142 if (parent == NULL && sibling == NULL)
1143 parent_node = tree_store->root;
1144 else if (parent == NULL)
1145 parent_node = G_NODE (sibling->user_data)->parent;
1146 else if (sibling == NULL)
1147 parent_node = G_NODE (parent->user_data);
1150 g_return_if_fail (G_NODE (sibling->user_data)->parent == G_NODE (parent->user_data));
1151 parent_node = G_NODE (parent->user_data);
1154 tree_store->columns_dirty = TRUE;
1156 new_node = g_node_new (NULL);
1158 g_node_insert_before (parent_node,
1159 sibling ? G_NODE (sibling->user_data) : NULL,
1162 iter->stamp = tree_store->stamp;
1163 iter->user_data = new_node;
1165 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1166 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1168 if (parent_node != tree_store->root)
1170 if (new_node->prev == NULL && new_node->next == NULL)
1172 GtkTreeIter parent_iter;
1174 parent_iter.stamp = tree_store->stamp;
1175 parent_iter.user_data = parent_node;
1177 gtk_tree_path_up (path);
1178 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &parent_iter);
1182 gtk_tree_path_free (path);
1184 validate_tree (tree_store);
1188 * gtk_tree_store_insert_after:
1189 * @tree_store: A #GtkTreeStore
1190 * @iter: An unset #GtkTreeIter to set to the new row
1191 * @parent: A valid #GtkTreeIter, or %NULL
1192 * @sibling: A valid #GtkTreeIter, or %NULL
1194 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1195 * prepended to @parent 's children. If @parent and @sibling are %NULL, then
1196 * the row will be prepended to the toplevel. If both @sibling and @parent are
1197 * set, then @parent must be the parent of @sibling. When @sibling is set,
1198 * @parent is optional.
1200 * @iter will be changed to point to this new row. The row will be empty after
1201 * this function is called. To fill in values, you need to call
1202 * gtk_tree_store_set() or gtk_tree_store_set_value().
1206 gtk_tree_store_insert_after (GtkTreeStore *tree_store,
1208 GtkTreeIter *parent,
1209 GtkTreeIter *sibling)
1215 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1216 g_return_if_fail (iter != NULL);
1218 g_return_if_fail (VALID_ITER (parent, tree_store));
1219 if (sibling != NULL)
1220 g_return_if_fail (VALID_ITER (sibling, tree_store));
1222 if (parent == NULL && sibling == NULL)
1223 parent_node = tree_store->root;
1224 else if (parent == NULL)
1225 parent_node = G_NODE (sibling->user_data)->parent;
1226 else if (sibling == NULL)
1227 parent_node = G_NODE (parent->user_data);
1230 g_return_if_fail (G_NODE (sibling->user_data)->parent ==
1231 G_NODE (parent->user_data));
1232 parent_node = G_NODE (parent->user_data);
1235 tree_store->columns_dirty = TRUE;
1237 new_node = g_node_new (NULL);
1239 g_node_insert_after (parent_node,
1240 sibling ? G_NODE (sibling->user_data) : NULL,
1243 iter->stamp = tree_store->stamp;
1244 iter->user_data = new_node;
1246 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1247 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1249 if (parent_node != tree_store->root)
1251 if (new_node->prev == NULL && new_node->next == NULL)
1253 GtkTreeIter parent_iter;
1255 parent_iter.stamp = tree_store->stamp;
1256 parent_iter.user_data = parent_node;
1258 gtk_tree_path_up (path);
1259 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &parent_iter);
1263 gtk_tree_path_free (path);
1265 validate_tree (tree_store);
1269 * gtk_tree_store_insert_with_values:
1270 * @tree_store: A #GtkTreeStore
1271 * @iter: An unset #GtkTreeIter to set the new row
1272 * @parent: A valid #GtkTreeIter, or %NULL
1273 * @position: position to insert the new row
1274 * @Varargs: pairs of column number and value, terminated with -1
1276 * Creates a new row at @position. @iter will be changed to point to this
1277 * new row. If @position is larger than the number of rows on the list, then
1278 * the new row will be appended to the list. The row will be filled with
1279 * the values given to this function.
1282 * <literal>gtk_tree_store_insert_with_values (tree_store, iter, position, ...)</literal>
1283 * has the same effect as calling
1284 * <informalexample><programlisting>
1285 * gtk_tree_store_insert (tree_store, iter, position);
1286 * gtk_tree_store_set (tree_store, iter, ...);
1287 * </programlisting></informalexample>
1288 * with the different that the former will only emit a row_inserted signal,
1289 * while the latter will emit row_inserted, row_changed and if the tree store
1290 * is sorted, rows_reordered. Since emitting the rows_reordered signal
1291 * repeatedly can affect the performance of the program,
1292 * gtk_tree_store_insert_with_values() should generally be preferred when
1293 * inserting rows in a sorted tree store.
1298 gtk_tree_store_insert_with_values (GtkTreeStore *tree_store,
1300 GtkTreeIter *parent,
1308 gboolean changed = FALSE;
1309 gboolean maybe_need_sort = FALSE;
1311 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1312 g_return_if_fail (iter != NULL);
1314 g_return_if_fail (VALID_ITER (parent, tree_store));
1317 parent_node = parent->user_data;
1319 parent_node = tree_store->root;
1321 tree_store->columns_dirty = TRUE;
1323 new_node = g_node_new (NULL);
1325 iter->stamp = tree_store->stamp;
1326 iter->user_data = new_node;
1327 g_node_insert (parent_node, position, new_node);
1329 va_start (var_args, position);
1330 gtk_tree_store_set_valist_internal (tree_store, iter,
1331 &changed, &maybe_need_sort,
1335 if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
1336 gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, FALSE);
1338 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1339 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1341 if (parent_node != tree_store->root)
1343 if (new_node->prev == NULL && new_node->next == NULL)
1345 gtk_tree_path_up (path);
1346 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1350 gtk_tree_path_free (path);
1352 validate_tree ((GtkTreeStore *)tree_store);
1356 * gtk_tree_store_insert_with_valuesv:
1357 * @tree_store: A #GtkTreeStore
1358 * @iter: An unset #GtkTreeIter to set the new row
1359 * @parent: A valid #GtkTreeIter, or %NULL
1360 * @position: position to insert the new row
1361 * @columns: an array of column numbers
1362 * @values: an array of GValues
1363 * @n_values: the length of the @columns and @values arrays
1365 * A variant of gtk_tree_store_insert_with_values() which takes
1366 * the columns and values as two arrays, instead of varargs. This
1367 * function is mainly intended for language bindings.
1372 gtk_tree_store_insert_with_valuesv (GtkTreeStore *tree_store,
1374 GtkTreeIter *parent,
1383 gboolean changed = FALSE;
1384 gboolean maybe_need_sort = FALSE;
1385 GtkTreeIterCompareFunc func = NULL;
1388 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1389 g_return_if_fail (iter != NULL);
1391 g_return_if_fail (VALID_ITER (parent, tree_store));
1394 parent_node = parent->user_data;
1396 parent_node = tree_store->root;
1398 tree_store->columns_dirty = TRUE;
1400 new_node = g_node_new (NULL);
1402 iter->stamp = tree_store->stamp;
1403 iter->user_data = new_node;
1404 g_node_insert (parent_node, position, new_node);
1406 if (GTK_TREE_STORE_IS_SORTED (tree_store))
1408 if (tree_store->sort_column_id != -1)
1410 GtkTreeDataSortHeader *header;
1411 header = _gtk_tree_data_list_get_header (tree_store->sort_list,
1412 tree_store->sort_column_id);
1413 g_return_if_fail (header != NULL);
1414 g_return_if_fail (header->func != NULL);
1415 func = header->func;
1419 func = tree_store->default_sort_func;
1423 if (func != _gtk_tree_data_list_compare_func)
1424 maybe_need_sort = TRUE;
1426 for (i = 0; i < n_values; i++)
1428 changed = gtk_tree_store_real_set_value (tree_store, iter,
1429 columns[i], &values[i],
1432 if (func == _gtk_tree_data_list_compare_func &&
1433 columns[i] == tree_store->sort_column_id)
1434 maybe_need_sort = TRUE;
1437 if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
1438 gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, FALSE);
1440 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1441 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1443 if (parent_node != tree_store->root)
1445 if (new_node->prev == NULL && new_node->next == NULL)
1447 gtk_tree_path_up (path);
1448 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1452 gtk_tree_path_free (path);
1454 validate_tree ((GtkTreeStore *)tree_store);
1458 * gtk_tree_store_prepend:
1459 * @tree_store: A #GtkTreeStore
1460 * @iter: An unset #GtkTreeIter to set to the prepended row
1461 * @parent: A valid #GtkTreeIter, or %NULL
1463 * Prepends a new row to @tree_store. If @parent is non-%NULL, then it will prepend
1464 * the new row before the first child of @parent, otherwise it will prepend a row
1465 * to the top level. @iter will be changed to point to this new row. The row
1466 * will be empty after this function is called. To fill in values, you need to
1467 * call gtk_tree_store_set() or gtk_tree_store_set_value().
1470 gtk_tree_store_prepend (GtkTreeStore *tree_store,
1472 GtkTreeIter *parent)
1476 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1477 g_return_if_fail (iter != NULL);
1479 g_return_if_fail (VALID_ITER (parent, tree_store));
1481 tree_store->columns_dirty = TRUE;
1484 parent_node = tree_store->root;
1486 parent_node = parent->user_data;
1488 if (parent_node->children == NULL)
1492 iter->stamp = tree_store->stamp;
1493 iter->user_data = g_node_new (NULL);
1495 g_node_prepend (parent_node, G_NODE (iter->user_data));
1497 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1498 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1500 if (parent_node != tree_store->root)
1502 gtk_tree_path_up (path);
1503 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1505 gtk_tree_path_free (path);
1509 gtk_tree_store_insert_after (tree_store, iter, parent, NULL);
1512 validate_tree (tree_store);
1516 * gtk_tree_store_append:
1517 * @tree_store: A #GtkTreeStore
1518 * @iter: An unset #GtkTreeIter to set to the appended row
1519 * @parent: A valid #GtkTreeIter, or %NULL
1521 * Appends a new row to @tree_store. If @parent is non-%NULL, then it will append the
1522 * new row after the last child of @parent, otherwise it will append a row to
1523 * the top level. @iter will be changed to point to this new row. The row will
1524 * be empty after this function is called. To fill in values, you need to call
1525 * gtk_tree_store_set() or gtk_tree_store_set_value().
1528 gtk_tree_store_append (GtkTreeStore *tree_store,
1530 GtkTreeIter *parent)
1534 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1535 g_return_if_fail (iter != NULL);
1537 g_return_if_fail (VALID_ITER (parent, tree_store));
1540 parent_node = tree_store->root;
1542 parent_node = parent->user_data;
1544 tree_store->columns_dirty = TRUE;
1546 if (parent_node->children == NULL)
1550 iter->stamp = tree_store->stamp;
1551 iter->user_data = g_node_new (NULL);
1553 g_node_append (parent_node, G_NODE (iter->user_data));
1555 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
1556 gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
1558 if (parent_node != tree_store->root)
1560 gtk_tree_path_up (path);
1561 gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
1563 gtk_tree_path_free (path);
1567 gtk_tree_store_insert_before (tree_store, iter, parent, NULL);
1570 validate_tree (tree_store);
1574 * gtk_tree_store_is_ancestor:
1575 * @tree_store: A #GtkTreeStore
1576 * @iter: A valid #GtkTreeIter
1577 * @descendant: A valid #GtkTreeIter
1579 * Returns %TRUE if @iter is an ancestor of @descendant. That is, @iter is the
1580 * parent (or grandparent or great-grandparent) of @descendant.
1582 * Return value: %TRUE, if @iter is an ancestor of @descendant
1585 gtk_tree_store_is_ancestor (GtkTreeStore *tree_store,
1587 GtkTreeIter *descendant)
1589 g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
1590 g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
1591 g_return_val_if_fail (VALID_ITER (descendant, tree_store), FALSE);
1593 return g_node_is_ancestor (G_NODE (iter->user_data),
1594 G_NODE (descendant->user_data));
1599 * gtk_tree_store_iter_depth:
1600 * @tree_store: A #GtkTreeStore
1601 * @iter: A valid #GtkTreeIter
1603 * Returns the depth of @iter. This will be 0 for anything on the root level, 1
1604 * for anything down a level, etc.
1606 * Return value: The depth of @iter
1609 gtk_tree_store_iter_depth (GtkTreeStore *tree_store,
1612 g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), 0);
1613 g_return_val_if_fail (VALID_ITER (iter, tree_store), 0);
1615 return g_node_depth (G_NODE (iter->user_data)) - 2;
1618 /* simple ripoff from g_node_traverse_post_order */
1620 gtk_tree_store_clear_traverse (GNode *node,
1621 GtkTreeStore *store)
1629 child = node->children;
1632 register GNode *current;
1635 child = current->next;
1636 if (gtk_tree_store_clear_traverse (current, store))
1642 iter.stamp = store->stamp;
1643 iter.user_data = node;
1645 gtk_tree_store_remove (store, &iter);
1648 else if (node->parent)
1650 iter.stamp = store->stamp;
1651 iter.user_data = node;
1653 gtk_tree_store_remove (store, &iter);
1660 gtk_tree_store_increment_stamp (GtkTreeStore *tree_store)
1664 tree_store->stamp++;
1666 while (tree_store->stamp == 0);
1670 * gtk_tree_store_clear:
1671 * @tree_store: a #GtkTreeStore
1673 * Removes all rows from @tree_store
1676 gtk_tree_store_clear (GtkTreeStore *tree_store)
1678 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
1680 gtk_tree_store_clear_traverse (tree_store->root, tree_store);
1681 gtk_tree_store_increment_stamp (tree_store);
1685 gtk_tree_store_iter_is_valid_helper (GtkTreeIter *iter,
1694 if (node == iter->user_data)
1698 if (gtk_tree_store_iter_is_valid_helper (iter, node->children))
1709 * gtk_tree_store_iter_is_valid:
1710 * @tree_store: A #GtkTreeStore.
1711 * @iter: A #GtkTreeIter.
1713 * WARNING: This function is slow. Only use it for debugging and/or testing
1716 * Checks if the given iter is a valid iter for this #GtkTreeStore.
1718 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1723 gtk_tree_store_iter_is_valid (GtkTreeStore *tree_store,
1726 g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
1727 g_return_val_if_fail (iter != NULL, FALSE);
1729 if (!VALID_ITER (iter, tree_store))
1732 return gtk_tree_store_iter_is_valid_helper (iter, tree_store->root);
1738 static gboolean real_gtk_tree_store_row_draggable (GtkTreeDragSource *drag_source,
1745 gtk_tree_store_drag_data_delete (GtkTreeDragSource *drag_source,
1750 if (gtk_tree_store_get_iter (GTK_TREE_MODEL (drag_source),
1754 gtk_tree_store_remove (GTK_TREE_STORE (drag_source),
1765 gtk_tree_store_drag_data_get (GtkTreeDragSource *drag_source,
1767 GtkSelectionData *selection_data)
1769 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1770 * target, because the default handler does it for us, but
1771 * we do anyway for the convenience of someone maybe overriding the
1775 if (gtk_tree_set_row_drag_data (selection_data,
1776 GTK_TREE_MODEL (drag_source),
1783 /* FIXME handle text targets at least. */
1790 copy_node_data (GtkTreeStore *tree_store,
1791 GtkTreeIter *src_iter,
1792 GtkTreeIter *dest_iter)
1794 GtkTreeDataList *dl = G_NODE (src_iter->user_data)->data;
1795 GtkTreeDataList *copy_head = NULL;
1796 GtkTreeDataList *copy_prev = NULL;
1797 GtkTreeDataList *copy_iter = NULL;
1804 copy_iter = _gtk_tree_data_list_node_copy (dl, tree_store->column_headers[col]);
1806 if (copy_head == NULL)
1807 copy_head = copy_iter;
1810 copy_prev->next = copy_iter;
1812 copy_prev = copy_iter;
1818 G_NODE (dest_iter->user_data)->data = copy_head;
1820 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), dest_iter);
1821 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, dest_iter);
1822 gtk_tree_path_free (path);
1826 recursive_node_copy (GtkTreeStore *tree_store,
1827 GtkTreeIter *src_iter,
1828 GtkTreeIter *dest_iter)
1831 GtkTreeModel *model;
1833 model = GTK_TREE_MODEL (tree_store);
1835 copy_node_data (tree_store, src_iter, dest_iter);
1837 if (gtk_tree_store_iter_children (model,
1841 /* Need to create children and recurse. Note our
1842 * dependence on persistent iterators here.
1848 /* Gee, a really slow algorithm... ;-) FIXME */
1849 gtk_tree_store_append (tree_store,
1853 recursive_node_copy (tree_store, &child, ©);
1855 while (gtk_tree_store_iter_next (model, &child));
1860 gtk_tree_store_drag_data_received (GtkTreeDragDest *drag_dest,
1862 GtkSelectionData *selection_data)
1864 GtkTreeModel *tree_model;
1865 GtkTreeStore *tree_store;
1866 GtkTreeModel *src_model = NULL;
1867 GtkTreePath *src_path = NULL;
1868 gboolean retval = FALSE;
1870 tree_model = GTK_TREE_MODEL (drag_dest);
1871 tree_store = GTK_TREE_STORE (drag_dest);
1873 validate_tree (tree_store);
1875 if (gtk_tree_get_row_drag_data (selection_data,
1878 src_model == tree_model)
1880 /* Copy the given row to a new position */
1881 GtkTreeIter src_iter;
1882 GtkTreeIter dest_iter;
1885 if (!gtk_tree_store_get_iter (src_model,
1892 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1893 prev = gtk_tree_path_copy (dest);
1895 if (!gtk_tree_path_prev (prev))
1897 GtkTreeIter dest_parent;
1898 GtkTreePath *parent;
1899 GtkTreeIter *dest_parent_p;
1901 /* dest was the first spot at the current depth; which means
1902 * we are supposed to prepend.
1905 /* Get the parent, NULL if parent is the root */
1906 dest_parent_p = NULL;
1907 parent = gtk_tree_path_copy (dest);
1908 if (gtk_tree_path_up (parent) &&
1909 gtk_tree_path_get_depth (parent) > 0)
1911 gtk_tree_store_get_iter (tree_model,
1914 dest_parent_p = &dest_parent;
1916 gtk_tree_path_free (parent);
1919 gtk_tree_store_prepend (tree_store,
1927 if (gtk_tree_store_get_iter (tree_model, &dest_iter, prev))
1929 GtkTreeIter tmp_iter = dest_iter;
1931 gtk_tree_store_insert_after (tree_store, &dest_iter, NULL,
1938 gtk_tree_path_free (prev);
1940 /* If we succeeded in creating dest_iter, walk src_iter tree branch,
1941 * duplicating it below dest_iter.
1946 recursive_node_copy (tree_store,
1953 /* FIXME maybe add some data targets eventually, or handle text
1954 * targets in the simple case.
1962 gtk_tree_path_free (src_path);
1968 gtk_tree_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1969 GtkTreePath *dest_path,
1970 GtkSelectionData *selection_data)
1972 GtkTreeModel *src_model = NULL;
1973 GtkTreePath *src_path = NULL;
1974 GtkTreePath *tmp = NULL;
1975 gboolean retval = FALSE;
1977 /* don't accept drops if the tree has been sorted */
1978 if (GTK_TREE_STORE_IS_SORTED (drag_dest))
1981 if (!gtk_tree_get_row_drag_data (selection_data,
1986 /* can only drag to ourselves */
1987 if (src_model != GTK_TREE_MODEL (drag_dest))
1990 /* Can't drop into ourself. */
1991 if (gtk_tree_path_is_ancestor (src_path,
1995 /* Can't drop if dest_path's parent doesn't exist */
1999 if (gtk_tree_path_get_depth (dest_path) > 1)
2001 tmp = gtk_tree_path_copy (dest_path);
2002 gtk_tree_path_up (tmp);
2004 if (!gtk_tree_store_get_iter (GTK_TREE_MODEL (drag_dest),
2010 /* Can otherwise drop anywhere. */
2016 gtk_tree_path_free (src_path);
2018 gtk_tree_path_free (tmp);
2023 /* Sorting and reordering */
2024 typedef struct _SortTuple
2032 gtk_tree_store_reorder_func (gconstpointer a,
2036 SortTuple *a_reorder;
2037 SortTuple *b_reorder;
2039 a_reorder = (SortTuple *)a;
2040 b_reorder = (SortTuple *)b;
2042 if (a_reorder->offset < b_reorder->offset)
2044 if (a_reorder->offset > b_reorder->offset)
2051 * gtk_tree_store_reorder:
2052 * @tree_store: A #GtkTreeStore.
2053 * @parent: A #GtkTreeIter.
2054 * @new_order: an array of integers mapping the new position of each child
2055 * to its old position before the re-ordering,
2056 * i.e. @new_order<literal>[newpos] = oldpos</literal>.
2058 * Reorders the children of @parent in @tree_store to follow the order
2059 * indicated by @new_order. Note that this function only works with
2065 gtk_tree_store_reorder (GtkTreeStore *tree_store,
2066 GtkTreeIter *parent,
2070 GNode *level, *node;
2072 SortTuple *sort_array;
2074 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
2075 g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
2076 g_return_if_fail (parent == NULL || VALID_ITER (parent, tree_store));
2077 g_return_if_fail (new_order != NULL);
2080 level = G_NODE (tree_store->root)->children;
2082 level = G_NODE (parent->user_data)->children;
2092 /* set up sortarray */
2093 sort_array = g_new (SortTuple, length);
2096 for (i = 0; i < length; i++)
2098 sort_array[new_order[i]].offset = i;
2099 sort_array[i].node = node;
2104 g_qsort_with_data (sort_array,
2107 gtk_tree_store_reorder_func,
2111 for (i = 0; i < length - 1; i++)
2113 sort_array[i].node->next = sort_array[i+1].node;
2114 sort_array[i+1].node->prev = sort_array[i].node;
2117 sort_array[length-1].node->next = NULL;
2118 sort_array[0].node->prev = NULL;
2120 G_NODE (parent->user_data)->children = sort_array[0].node;
2122 G_NODE (tree_store->root)->children = sort_array[0].node;
2126 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), parent);
2128 path = gtk_tree_path_new ();
2129 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path,
2131 gtk_tree_path_free (path);
2132 g_free (sort_array);
2136 * gtk_tree_store_swap:
2137 * @tree_store: A #GtkTreeStore.
2138 * @a: A #GtkTreeIter.
2139 * @b: Another #GtkTreeIter.
2141 * Swaps @a and @b in the same level of @tree_store. Note that this function
2142 * only works with unsorted stores.
2147 gtk_tree_store_swap (GtkTreeStore *tree_store,
2151 GNode *tmp, *node_a, *node_b, *parent_node;
2152 GNode *a_prev, *a_next, *b_prev, *b_next;
2153 gint i, a_count, b_count, length, *order;
2154 GtkTreePath *path_a, *path_b;
2157 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
2158 g_return_if_fail (VALID_ITER (a, tree_store));
2159 g_return_if_fail (VALID_ITER (b, tree_store));
2161 node_a = G_NODE (a->user_data);
2162 node_b = G_NODE (b->user_data);
2164 /* basic sanity checking */
2165 if (node_a == node_b)
2168 path_a = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), a);
2169 path_b = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), b);
2171 g_return_if_fail (path_a && path_b);
2173 gtk_tree_path_up (path_a);
2174 gtk_tree_path_up (path_b);
2176 if (gtk_tree_path_get_depth (path_a) == 0
2177 || gtk_tree_path_get_depth (path_b) == 0)
2179 if (gtk_tree_path_get_depth (path_a) != gtk_tree_path_get_depth (path_b))
2181 gtk_tree_path_free (path_a);
2182 gtk_tree_path_free (path_b);
2184 g_warning ("Given children are not in the same level\n");
2187 parent_node = G_NODE (tree_store->root);
2191 if (gtk_tree_path_compare (path_a, path_b))
2193 gtk_tree_path_free (path_a);
2194 gtk_tree_path_free (path_b);
2196 g_warning ("Given children don't have a common parent\n");
2199 gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), &parent,
2201 parent_node = G_NODE (parent.user_data);
2203 gtk_tree_path_free (path_b);
2205 /* old links which we have to keep around */
2206 a_prev = node_a->prev;
2207 a_next = node_a->next;
2209 b_prev = node_b->prev;
2210 b_next = node_b->next;
2212 /* fix up links if the nodes are next to eachother */
2213 if (a_prev == node_b)
2215 if (a_next == node_b)
2218 if (b_prev == node_a)
2220 if (b_next == node_a)
2223 /* counting nodes */
2224 tmp = parent_node->children;
2225 i = a_count = b_count = 0;
2238 /* hacking the tree */
2240 parent_node->children = node_b;
2242 a_prev->next = node_b;
2245 a_next->prev = node_b;
2248 parent_node->children = node_a;
2250 b_prev->next = node_a;
2253 b_next->prev = node_a;
2255 node_a->prev = b_prev;
2256 node_a->next = b_next;
2258 node_b->prev = a_prev;
2259 node_b->next = a_next;
2262 order = g_new (gint, length);
2263 for (i = 0; i < length; i++)
2266 else if (i == b_count)
2271 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path_a,
2272 parent_node == tree_store->root
2273 ? NULL : &parent, order);
2274 gtk_tree_path_free (path_a);
2278 /* WARNING: this function is *incredibly* fragile. Please smashtest after
2279 * making changes here.
2283 gtk_tree_store_move (GtkTreeStore *tree_store,
2285 GtkTreeIter *position,
2288 GNode *parent, *node, *a, *b, *tmp, *tmp_a, *tmp_b;
2289 gint old_pos, new_pos, length, i, *order;
2290 GtkTreePath *path = NULL, *tmppath, *pos_path = NULL;
2291 GtkTreeIter parent_iter, dst_a, dst_b;
2293 gboolean handle_b = TRUE;
2295 g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
2296 g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
2297 g_return_if_fail (VALID_ITER (iter, tree_store));
2299 g_return_if_fail (VALID_ITER (position, tree_store));
2306 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
2307 pos_path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store),
2311 * moving the iter before path or "path + 1" doesn't make sense
2313 * moving the iter before path or "path - 1" doesn't make sense
2315 if (!gtk_tree_path_compare (path, pos_path))
2316 goto free_paths_and_out;
2319 gtk_tree_path_next (path);
2321 gtk_tree_path_prev (path);
2323 if (!gtk_tree_path_compare (path, pos_path))
2324 goto free_paths_and_out;
2327 gtk_tree_path_prev (path);
2329 gtk_tree_path_next (path);
2331 if (gtk_tree_path_get_depth (path) != gtk_tree_path_get_depth (pos_path))
2333 g_warning ("Given children are not in the same level\n");
2335 goto free_paths_and_out;
2338 tmppath = gtk_tree_path_copy (pos_path);
2339 gtk_tree_path_up (path);
2340 gtk_tree_path_up (tmppath);
2342 if (gtk_tree_path_get_depth (path) > 0 &&
2343 gtk_tree_path_compare (path, tmppath))
2345 g_warning ("Given children are not in the same level\n");
2347 gtk_tree_path_free (tmppath);
2348 goto free_paths_and_out;
2351 gtk_tree_path_free (tmppath);
2356 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
2357 gtk_tree_path_up (path);
2360 depth = gtk_tree_path_get_depth (path);
2364 gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store),
2365 &parent_iter, path);
2367 parent = G_NODE (parent_iter.user_data);
2370 parent = G_NODE (tree_store->root);
2372 /* yes, I know that this can be done shorter, but I'm doing it this way
2373 * so the code is also maintainable
2376 if (before && position)
2378 b = G_NODE (position->user_data);
2380 if (gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path) - 1] > 0)
2382 gtk_tree_path_prev (pos_path);
2383 if (gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store),
2385 a = G_NODE (dst_a.user_data);
2388 gtk_tree_path_next (pos_path);
2391 /* if b is NULL, a is NULL too -- we are at the beginning of the list
2392 * yes and we leak memory here ...
2394 g_return_if_fail (b);
2396 else if (before && !position)
2398 /* move before without position is appending */
2405 a = G_NODE (position->user_data);
2411 gtk_tree_path_next (pos_path);
2412 if (gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), &dst_b, pos_path))
2413 b = G_NODE (dst_b.user_data);
2416 gtk_tree_path_prev (pos_path);
2420 /* move after without position is prepending */
2422 gtk_tree_store_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
2425 gtk_tree_store_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
2428 b = G_NODE (dst_b.user_data);
2431 /* if a is NULL, b is NULL too -- we are at the end of the list
2432 * yes and we leak memory here ...
2435 g_return_if_fail (a);
2438 /* counting nodes */
2439 tmp = parent->children;
2441 length = old_pos = 0;
2444 if (tmp == iter->user_data)
2451 /* remove node from list */
2452 node = G_NODE (iter->user_data);
2457 tmp_a->next = tmp_b;
2459 parent->children = tmp_b;
2462 tmp_b->prev = tmp_a;
2464 /* and reinsert the node */
2473 else if (!a && !before)
2475 tmp = parent->children;
2478 parent->children = node;
2486 else if (!a && before)
2490 node->parent = NULL;
2491 node->next = node->prev = NULL;
2493 /* before with sibling = NULL appends */
2494 g_node_insert_before (parent, NULL, node);
2498 node->parent = NULL;
2499 node->next = node->prev = NULL;
2501 /* after with sibling = NULL prepends */
2502 g_node_insert_after (parent, NULL, node);
2518 else if (!(!a && before)) /* !a && before is completely handled above */
2524 new_pos = gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path)-1];
2528 new_pos = gtk_tree_store_iter_n_children (GTK_TREE_MODEL (tree_store),
2531 new_pos = gtk_tree_store_iter_n_children (GTK_TREE_MODEL (tree_store),
2537 if (new_pos > old_pos)
2539 if (before && position)
2544 if (!before && position)
2548 order = g_new (gint, length);
2549 if (new_pos > old_pos)
2551 for (i = 0; i < length; i++)
2554 else if (i >= old_pos && i < new_pos)
2556 else if (i == new_pos)
2563 for (i = 0; i < length; i++)
2566 else if (i > new_pos && i <= old_pos)
2574 tmppath = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store),
2576 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
2577 tmppath, &parent_iter, order);
2581 tmppath = gtk_tree_path_new ();
2582 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
2583 tmppath, NULL, order);
2586 gtk_tree_path_free (tmppath);
2587 gtk_tree_path_free (path);
2589 gtk_tree_path_free (pos_path);
2595 gtk_tree_path_free (path);
2596 gtk_tree_path_free (pos_path);
2600 * gtk_tree_store_move_before:
2601 * @tree_store: A #GtkTreeStore.
2602 * @iter: A #GtkTreeIter.
2603 * @position: A #GtkTreeIter or %NULL.
2605 * Moves @iter in @tree_store to the position before @position. @iter and
2606 * @position should be in the same level. Note that this function only
2607 * works with unsorted stores. If @position is %NULL, @iter will be
2608 * moved to the end of the level.
2613 gtk_tree_store_move_before (GtkTreeStore *tree_store,
2615 GtkTreeIter *position)
2617 gtk_tree_store_move (tree_store, iter, position, TRUE);
2621 * gtk_tree_store_move_after:
2622 * @tree_store: A #GtkTreeStore.
2623 * @iter: A #GtkTreeIter.
2624 * @position: A #GtkTreeIter.
2626 * Moves @iter in @tree_store to the position after @position. @iter and
2627 * @position should be in the same level. Note that this function only
2628 * works with unsorted stores. If @position is %NULL, @iter will be moved
2629 * to the start of the level.
2634 gtk_tree_store_move_after (GtkTreeStore *tree_store,
2636 GtkTreeIter *position)
2638 gtk_tree_store_move (tree_store, iter, position, FALSE);
2643 gtk_tree_store_compare_func (gconstpointer a,
2647 GtkTreeStore *tree_store = user_data;
2650 GtkTreeIterCompareFunc func;
2657 if (tree_store->sort_column_id != -1)
2659 GtkTreeDataSortHeader *header;
2661 header = _gtk_tree_data_list_get_header (tree_store->sort_list,
2662 tree_store->sort_column_id);
2663 g_return_val_if_fail (header != NULL, 0);
2664 g_return_val_if_fail (header->func != NULL, 0);
2666 func = header->func;
2667 data = header->data;
2671 g_return_val_if_fail (tree_store->default_sort_func != NULL, 0);
2672 func = tree_store->default_sort_func;
2673 data = tree_store->default_sort_data;
2676 node_a = ((SortTuple *) a)->node;
2677 node_b = ((SortTuple *) b)->node;
2679 iter_a.stamp = tree_store->stamp;
2680 iter_a.user_data = node_a;
2681 iter_b.stamp = tree_store->stamp;
2682 iter_b.user_data = node_b;
2684 retval = (* func) (GTK_TREE_MODEL (user_data), &iter_a, &iter_b, data);
2686 if (tree_store->order == GTK_SORT_DESCENDING)
2690 else if (retval < 0)
2697 gtk_tree_store_sort_helper (GtkTreeStore *tree_store,
2710 node = parent->children;
2711 if (node == NULL || node->next == NULL)
2713 if (recurse && node && node->children)
2714 gtk_tree_store_sort_helper (tree_store, node, TRUE);
2720 for (tmp_node = node; tmp_node; tmp_node = tmp_node->next)
2723 sort_array = g_array_sized_new (FALSE, FALSE, sizeof (SortTuple), list_length);
2726 for (tmp_node = node; tmp_node; tmp_node = tmp_node->next)
2731 tuple.node = tmp_node;
2732 g_array_append_val (sort_array, tuple);
2736 /* Sort the array */
2737 g_array_sort_with_data (sort_array, gtk_tree_store_compare_func, tree_store);
2739 for (i = 0; i < list_length - 1; i++)
2741 g_array_index (sort_array, SortTuple, i).node->next =
2742 g_array_index (sort_array, SortTuple, i + 1).node;
2743 g_array_index (sort_array, SortTuple, i + 1).node->prev =
2744 g_array_index (sort_array, SortTuple, i).node;
2746 g_array_index (sort_array, SortTuple, list_length - 1).node->next = NULL;
2747 g_array_index (sort_array, SortTuple, 0).node->prev = NULL;
2748 parent->children = g_array_index (sort_array, SortTuple, 0).node;
2750 /* Let the world know about our new order */
2751 new_order = g_new (gint, list_length);
2752 for (i = 0; i < list_length; i++)
2753 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
2755 iter.stamp = tree_store->stamp;
2756 iter.user_data = parent;
2757 path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), &iter);
2758 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
2759 path, &iter, new_order);
2760 gtk_tree_path_free (path);
2762 g_array_free (sort_array, TRUE);
2766 for (tmp_node = parent->children; tmp_node; tmp_node = tmp_node->next)
2768 if (tmp_node->children)
2769 gtk_tree_store_sort_helper (tree_store, tmp_node, TRUE);
2775 gtk_tree_store_sort (GtkTreeStore *tree_store)
2777 if (!GTK_TREE_STORE_IS_SORTED (tree_store))
2780 if (tree_store->sort_column_id != -1)
2782 GtkTreeDataSortHeader *header = NULL;
2784 header = _gtk_tree_data_list_get_header (tree_store->sort_list,
2785 tree_store->sort_column_id);
2787 /* We want to make sure that we have a function */
2788 g_return_if_fail (header != NULL);
2789 g_return_if_fail (header->func != NULL);
2793 g_return_if_fail (tree_store->default_sort_func != NULL);
2796 gtk_tree_store_sort_helper (tree_store, G_NODE (tree_store->root), TRUE);
2800 gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
2803 gboolean emit_signal)
2808 GtkTreePath *tmp_path;
2809 GtkTreeIter tmp_iter;
2817 GtkTreeIterCompareFunc func;
2820 g_return_if_fail (G_NODE (iter->user_data)->parent != NULL);
2822 tmp_iter.stamp = tree_store->stamp;
2823 if (tree_store->sort_column_id != -1)
2825 GtkTreeDataSortHeader *header;
2826 header = _gtk_tree_data_list_get_header (tree_store->sort_list,
2827 tree_store->sort_column_id);
2828 g_return_if_fail (header != NULL);
2829 g_return_if_fail (header->func != NULL);
2830 func = header->func;
2831 data = header->data;
2835 g_return_if_fail (tree_store->default_sort_func != NULL);
2836 func = tree_store->default_sort_func;
2837 data = tree_store->default_sort_data;
2840 /* If it's the built in function, we don't sort. */
2841 if (func == _gtk_tree_data_list_compare_func &&
2842 tree_store->sort_column_id != column)
2846 node = G_NODE (iter->user_data)->parent->children;
2847 /* First we find the iter, its prev, and its next */
2850 if (node == G_NODE (iter->user_data))
2855 g_assert (node != NULL);
2860 /* Check the common case, where we don't need to sort it moved. */
2863 tmp_iter.user_data = prev;
2864 cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
2869 tmp_iter.user_data = next;
2870 cmp_b = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
2873 if (tree_store->order == GTK_SORT_DESCENDING)
2886 if (prev == NULL && cmp_b <= 0)
2888 else if (next == NULL && cmp_a <= 0)
2890 else if (prev != NULL && next != NULL &&
2891 cmp_a <= 0 && cmp_b <= 0)
2894 /* We actually need to sort it */
2895 /* First, remove the old link. */
2900 node->parent->children = next;
2908 /* FIXME: as an optimization, we can potentially start at next */
2910 node = node->parent->children;
2912 tmp_iter.user_data = node;
2913 if (tree_store->order == GTK_SORT_DESCENDING)
2914 cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
2916 cmp_a = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
2918 while ((node->next) && (cmp_a > 0))
2923 tmp_iter.user_data = node;
2924 if (tree_store->order == GTK_SORT_DESCENDING)
2925 cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
2927 cmp_a = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
2930 if ((!node->next) && (cmp_a > 0))
2933 node->next = G_NODE (iter->user_data);
2934 node->next->prev = node;
2938 prev->next = G_NODE (iter->user_data);
2939 prev->next->prev = prev;
2940 G_NODE (iter->user_data)->next = node;
2941 G_NODE (iter->user_data)->next->prev = G_NODE (iter->user_data);
2945 G_NODE (iter->user_data)->next = G_NODE (iter->user_data)->parent->children;
2946 G_NODE (iter->user_data)->next->prev = G_NODE (iter->user_data);
2947 G_NODE (iter->user_data)->parent->children = G_NODE (iter->user_data);
2953 /* Emit the reordered signal. */
2954 length = g_node_n_children (node->parent);
2955 new_order = g_new (int, length);
2956 if (old_location < new_location)
2957 for (i = 0; i < length; i++)
2959 if (i < old_location ||
2962 else if (i >= old_location &&
2964 new_order[i] = i + 1;
2965 else if (i == new_location)
2966 new_order[i] = old_location;
2969 for (i = 0; i < length; i++)
2971 if (i < new_location ||
2974 else if (i > new_location &&
2976 new_order[i] = i - 1;
2977 else if (i == new_location)
2978 new_order[i] = old_location;
2981 tmp_iter.user_data = node->parent;
2982 tmp_path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), &tmp_iter);
2984 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
2985 tmp_path, &tmp_iter,
2988 gtk_tree_path_free (tmp_path);
2994 gtk_tree_store_get_sort_column_id (GtkTreeSortable *sortable,
2995 gint *sort_column_id,
2998 GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3001 * sort_column_id = tree_store->sort_column_id;
3003 * order = tree_store->order;
3005 if (tree_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
3006 tree_store->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
3013 gtk_tree_store_set_sort_column_id (GtkTreeSortable *sortable,
3014 gint sort_column_id,
3017 GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3020 if ((tree_store->sort_column_id == sort_column_id) &&
3021 (tree_store->order == order))
3024 if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
3026 if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
3028 GtkTreeDataSortHeader *header = NULL;
3030 header = _gtk_tree_data_list_get_header (tree_store->sort_list,
3033 /* We want to make sure that we have a function */
3034 g_return_if_fail (header != NULL);
3035 g_return_if_fail (header->func != NULL);
3039 g_return_if_fail (tree_store->default_sort_func != NULL);
3043 tree_store->sort_column_id = sort_column_id;
3044 tree_store->order = order;
3046 gtk_tree_sortable_sort_column_changed (sortable);
3048 gtk_tree_store_sort (tree_store);
3052 gtk_tree_store_set_sort_func (GtkTreeSortable *sortable,
3053 gint sort_column_id,
3054 GtkTreeIterCompareFunc func,
3056 GtkDestroyNotify destroy)
3058 GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3060 tree_store->sort_list = _gtk_tree_data_list_set_header (tree_store->sort_list,
3062 func, data, destroy);
3064 if (tree_store->sort_column_id == sort_column_id)
3065 gtk_tree_store_sort (tree_store);
3069 gtk_tree_store_set_default_sort_func (GtkTreeSortable *sortable,
3070 GtkTreeIterCompareFunc func,
3072 GtkDestroyNotify destroy)
3074 GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3076 if (tree_store->default_sort_destroy)
3078 GtkDestroyNotify d = tree_store->default_sort_destroy;
3080 tree_store->default_sort_destroy = NULL;
3081 d (tree_store->default_sort_data);
3084 tree_store->default_sort_func = func;
3085 tree_store->default_sort_data = data;
3086 tree_store->default_sort_destroy = destroy;
3088 if (tree_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
3089 gtk_tree_store_sort (tree_store);
3093 gtk_tree_store_has_default_sort_func (GtkTreeSortable *sortable)
3095 GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
3097 return (tree_store->default_sort_func != NULL);
3101 validate_gnode (GNode* node)
3105 iter = node->children;
3106 while (iter != NULL)
3108 g_assert (iter->parent == node);
3110 g_assert (iter->prev->next == iter);
3111 validate_gnode (iter);
3116 #define __GTK_TREE_STORE_C__
3117 #include "gtkaliasdef.c"