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.
21 #include "gtktreemodel.h"
22 #include "gtkliststore.h"
23 #include "gtktreedatalist.h"
24 #include "gtksignal.h"
25 #include "gtktreednd.h"
26 #include <gobject/gvaluecollector.h>
28 #define G_SLIST(x) ((GSList *) x)
29 #define GTK_LIST_STORE_IS_SORTED(list) (GTK_LIST_STORE (list)->sort_column_id != -2)
30 #define VALID_ITER(iter, list_store) (iter!= NULL && iter->user_data != NULL && list_store->stamp == iter->stamp)
32 static void gtk_list_store_init (GtkListStore *list_store);
33 static void gtk_list_store_class_init (GtkListStoreClass *class);
34 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
35 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
36 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
37 static void gtk_list_store_sortable_init (GtkTreeSortableIface *iface);
38 static void gtk_list_store_finalize (GObject *object);
39 static guint gtk_list_store_get_flags (GtkTreeModel *tree_model);
40 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
41 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
43 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
46 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
48 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
52 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
54 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
57 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
59 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
61 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
65 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
70 static void gtk_list_store_set_n_columns (GtkListStore *list_store,
72 static void gtk_list_store_set_column_type (GtkListStore *list_store,
78 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
80 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
82 GtkSelectionData *selection_data);
83 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
85 GtkSelectionData *selection_data);
86 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
87 GtkTreePath *dest_path,
88 GtkSelectionData *selection_data);
92 static void gtk_list_store_sort (GtkListStore *list_store);
93 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
96 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
99 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
102 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
104 GtkTreeIterCompareFunc func,
106 GtkDestroyNotify destroy);
107 static void gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
108 GtkTreeIterCompareFunc func,
110 GtkDestroyNotify destroy);
111 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable);
114 static GObjectClass *parent_class = NULL;
118 validate_list_store (GtkListStore *list_store)
120 if (gtk_debug_flags & GTK_DEBUG_TREE)
122 g_assert (g_slist_length (list_store->root) == list_store->length);
124 g_assert (g_slist_last (list_store->root) == list_store->tail);
129 gtk_list_store_get_type (void)
131 static GType list_store_type = 0;
133 if (!list_store_type)
135 static const GTypeInfo list_store_info =
137 sizeof (GtkListStoreClass),
138 NULL, /* base_init */
139 NULL, /* base_finalize */
140 (GClassInitFunc) gtk_list_store_class_init,
141 NULL, /* class_finalize */
142 NULL, /* class_data */
143 sizeof (GtkListStore),
145 (GInstanceInitFunc) gtk_list_store_init,
148 static const GInterfaceInfo tree_model_info =
150 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
155 static const GInterfaceInfo drag_source_info =
157 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
162 static const GInterfaceInfo drag_dest_info =
164 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
169 static const GInterfaceInfo sortable_info =
171 (GInterfaceInitFunc) gtk_list_store_sortable_init,
176 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
177 g_type_add_interface_static (list_store_type,
180 g_type_add_interface_static (list_store_type,
181 GTK_TYPE_TREE_DRAG_SOURCE,
183 g_type_add_interface_static (list_store_type,
184 GTK_TYPE_TREE_DRAG_DEST,
186 g_type_add_interface_static (list_store_type,
187 GTK_TYPE_TREE_SORTABLE,
191 return list_store_type;
195 gtk_list_store_class_init (GtkListStoreClass *class)
197 GObjectClass *object_class;
199 parent_class = g_type_class_peek_parent (class);
200 object_class = (GObjectClass*) class;
202 object_class->finalize = gtk_list_store_finalize;
206 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
208 iface->get_flags = gtk_list_store_get_flags;
209 iface->get_n_columns = gtk_list_store_get_n_columns;
210 iface->get_column_type = gtk_list_store_get_column_type;
211 iface->get_iter = gtk_list_store_get_iter;
212 iface->get_path = gtk_list_store_get_path;
213 iface->get_value = gtk_list_store_get_value;
214 iface->iter_next = gtk_list_store_iter_next;
215 iface->iter_children = gtk_list_store_iter_children;
216 iface->iter_has_child = gtk_list_store_iter_has_child;
217 iface->iter_n_children = gtk_list_store_iter_n_children;
218 iface->iter_nth_child = gtk_list_store_iter_nth_child;
219 iface->iter_parent = gtk_list_store_iter_parent;
223 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
225 iface->drag_data_delete = gtk_list_store_drag_data_delete;
226 iface->drag_data_get = gtk_list_store_drag_data_get;
230 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
232 iface->drag_data_received = gtk_list_store_drag_data_received;
233 iface->row_drop_possible = gtk_list_store_row_drop_possible;
237 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
239 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
240 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
241 iface->set_sort_func = gtk_list_store_set_sort_func;
242 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
243 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
247 gtk_list_store_init (GtkListStore *list_store)
249 list_store->root = NULL;
250 list_store->tail = NULL;
251 list_store->sort_list = NULL;
252 list_store->stamp = g_random_int ();
253 list_store->length = 0;
254 list_store->sort_column_id = -2;
255 list_store->columns_dirty = FALSE;
259 * gtk_list_store_new:
260 * @n_columns: number of columns in the list store
261 * @Varargs: all #GType types for the columns, from first to last
263 * Creates a new list store as with @n_columns columns each of the types passed
264 * in. As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
265 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
266 * int, string and #GdkPixbuf respectively.
268 * Return value: a new #GtkListStore
271 gtk_list_store_new (gint n_columns,
274 GtkListStore *retval;
278 g_return_val_if_fail (n_columns > 0, NULL);
280 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
281 gtk_list_store_set_n_columns (retval, n_columns);
283 va_start (args, n_columns);
285 for (i = 0; i < n_columns; i++)
287 GType type = va_arg (args, GType);
288 if (! _gtk_tree_data_list_check_type (type))
290 g_warning ("%s: Invalid type %s passed to gtk_list_store_new\n", G_STRLOC, g_type_name (type));
291 g_object_unref (G_OBJECT (retval));
295 gtk_list_store_set_column_type (retval, i, type);
305 * gtk_list_store_newv:
306 * @n_columns: number of columns in the list store
307 * @types: an array of #GType types for the columns, from first to last
309 * Non-vararg creation function. Used primarily by language bindings.
311 * Return value: a new #GtkListStore
314 gtk_list_store_newv (gint n_columns,
317 GtkListStore *retval;
320 g_return_val_if_fail (n_columns > 0, NULL);
322 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
323 gtk_list_store_set_n_columns (retval, n_columns);
325 for (i = 0; i < n_columns; i++)
327 if (! _gtk_tree_data_list_check_type (types[i]))
329 g_warning ("%s: Invalid type %s passed to gtk_list_store_newv\n", G_STRLOC, g_type_name (types[i]));
330 g_object_unref (G_OBJECT (retval));
334 gtk_list_store_set_column_type (retval, i, types[i]);
341 * gtk_list_store_set_column_types:
342 * @list_store: A #GtkListStore
343 * @n_columns: Number of columns for the list store
344 * @types: An array length n of #GTypes
346 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
347 * and should only be used when constructing a new #GtkListStore. It will not
348 * function after a row has been added, or a method on the #GtkTreeModel
349 * interface is called.
352 gtk_list_store_set_column_types (GtkListStore *list_store,
358 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
359 g_return_if_fail (list_store->columns_dirty == 0);
361 gtk_list_store_set_n_columns (list_store, n_columns);
362 for (i = 0; i < n_columns; i++)
364 if (! _gtk_tree_data_list_check_type (types[i]))
366 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_types\n", G_STRLOC, g_type_name (types[i]));
369 gtk_list_store_set_column_type (list_store, i, types[i]);
374 gtk_list_store_set_n_columns (GtkListStore *list_store,
379 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
380 g_return_if_fail (n_columns > 0);
382 if (list_store->n_columns == n_columns)
385 new_columns = g_new0 (GType, n_columns);
386 if (list_store->column_headers)
388 /* copy the old header orders over */
389 if (n_columns >= list_store->n_columns)
390 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
392 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
394 g_free (list_store->column_headers);
397 if (list_store->sort_list)
398 _gtk_tree_data_list_header_free (list_store->sort_list);
400 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
402 list_store->column_headers = new_columns;
403 list_store->n_columns = n_columns;
407 gtk_list_store_set_column_type (GtkListStore *list_store,
411 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
412 g_return_if_fail (column >=0 && column < list_store->n_columns);
413 if (!_gtk_tree_data_list_check_type (type))
415 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_type\n", G_STRLOC, g_type_name (type));
419 list_store->column_headers[column] = type;
423 gtk_list_store_finalize (GObject *object)
425 GtkListStore *list_store = GTK_LIST_STORE (object);
427 g_list_foreach (list_store->root, (GFunc) _gtk_tree_data_list_free, list_store->column_headers);
428 _gtk_tree_data_list_header_free (list_store->sort_list);
429 g_free (list_store->column_headers);
431 if (list_store->default_sort_destroy)
433 GtkDestroyNotify d = list_store->default_sort_destroy;
435 list_store->default_sort_destroy = NULL;
436 d (list_store->default_sort_data);
437 list_store->default_sort_data = NULL;
440 (* parent_class->finalize) (object);
443 /* Fulfill the GtkTreeModel requirements */
445 gtk_list_store_get_flags (GtkTreeModel *tree_model)
447 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
449 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
453 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
455 GtkListStore *list_store = (GtkListStore *) tree_model;
457 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
459 list_store->columns_dirty = TRUE;
461 return list_store->n_columns;
465 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
468 GtkListStore *list_store = (GtkListStore *) tree_model;
470 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
471 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
472 index >= 0, G_TYPE_INVALID);
474 list_store->columns_dirty = TRUE;
476 return list_store->column_headers[index];
480 gtk_list_store_get_iter (GtkTreeModel *tree_model,
484 GtkListStore *list_store = (GtkListStore *) tree_model;
488 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
489 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
491 list_store->columns_dirty = TRUE;
493 i = gtk_tree_path_get_indices (path)[0];
495 if (i >= list_store->length)
498 list = g_slist_nth (G_SLIST (list_store->root), i);
500 /* If this fails, list_store->length has gotten mangled. */
503 iter->stamp = list_store->stamp;
504 iter->user_data = list;
510 gtk_list_store_get_path (GtkTreeModel *tree_model,
517 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
518 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
519 if (G_SLIST (iter->user_data) == G_SLIST (GTK_LIST_STORE (tree_model)->tail))
521 retval = gtk_tree_path_new ();
522 gtk_tree_path_append_index (retval, GTK_LIST_STORE (tree_model)->length - 1);
526 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
528 if (list == G_SLIST (iter->user_data))
535 retval = gtk_tree_path_new ();
536 gtk_tree_path_append_index (retval, i);
541 gtk_list_store_get_value (GtkTreeModel *tree_model,
546 GtkTreeDataList *list;
547 gint tmp_column = column;
549 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
550 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
551 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
553 list = G_SLIST (iter->user_data)->data;
555 while (tmp_column-- > 0 && list)
559 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
561 _gtk_tree_data_list_node_to_value (list,
562 GTK_LIST_STORE (tree_model)->column_headers[column],
567 gtk_list_store_iter_next (GtkTreeModel *tree_model,
570 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
571 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
573 iter->user_data = G_SLIST (iter->user_data)->next;
575 return (iter->user_data != NULL);
579 gtk_list_store_iter_children (GtkTreeModel *tree_model,
583 /* this is a list, nodes have no children */
587 /* but if parent == NULL we return the list itself as children of the
591 if (GTK_LIST_STORE (tree_model)->root)
593 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
594 iter->user_data = GTK_LIST_STORE (tree_model)->root;
602 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
609 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
612 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
614 return GTK_LIST_STORE (tree_model)->length;
616 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
621 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
628 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
633 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
637 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
638 iter->user_data = child;
646 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
654 gtk_list_store_real_set_value (GtkListStore *list_store,
660 GtkTreeDataList *list;
661 GtkTreeDataList *prev;
662 gint old_column = column;
663 GValue real_value = {0, };
664 gboolean converted = FALSE;
665 gboolean retval = FALSE;
667 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
668 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
669 g_return_val_if_fail (column >= 0 && column < list_store->n_columns, FALSE);
670 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
672 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
674 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
675 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
677 g_warning ("%s: Unable to convert from %s to %s\n",
679 g_type_name (G_VALUE_TYPE (value)),
680 g_type_name (list_store->column_headers[column]));
683 if (!g_value_transform (value, &real_value))
685 g_warning ("%s: Unable to make conversion from %s to %s\n",
687 g_type_name (G_VALUE_TYPE (value)),
688 g_type_name (list_store->column_headers[column]));
689 g_value_unset (&real_value);
695 prev = list = G_SLIST (iter->user_data)->data;
702 _gtk_tree_data_list_value_to_node (list, &real_value);
704 _gtk_tree_data_list_value_to_node (list, value);
707 g_value_unset (&real_value);
716 if (G_SLIST (iter->user_data)->data == NULL)
718 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
723 list = prev->next = _gtk_tree_data_list_alloc ();
729 list->next = _gtk_tree_data_list_alloc ();
736 _gtk_tree_data_list_value_to_node (list, &real_value);
738 _gtk_tree_data_list_value_to_node (list, value);
742 g_value_unset (&real_value);
744 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
745 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
752 * gtk_list_store_set_value:
753 * @list_store: A #GtkListStore
754 * @iter: A valid #GtkTreeIter for the row being modified
755 * @column: column number to modify
756 * @value: new value for the cell
758 * Sets the data in the cell specified by @iter and @column.
759 * The type of @value must be convertible to the type of the
764 gtk_list_store_set_value (GtkListStore *list_store,
769 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
770 g_return_if_fail (VALID_ITER (iter, list_store));
771 g_return_if_fail (column >= 0 && column < list_store->n_columns);
772 g_return_if_fail (G_IS_VALUE (value));
774 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
778 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
779 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
780 gtk_tree_path_free (path);
785 * gtk_list_store_set_valist:
786 * @list_store: A #GtkListStore
787 * @iter: A valid #GtkTreeIter for the row being modified
788 * @var_args: va_list of column/value pairs
790 * See gtk_list_store_set(); this version takes a va_list for use by language
795 gtk_list_store_set_valist (GtkListStore *list_store,
800 gboolean emit_signal = FALSE;
801 gboolean maybe_need_sort = FALSE;
802 GtkTreeIterCompareFunc func = NULL;
804 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
805 g_return_if_fail (VALID_ITER (iter, list_store));
807 column = va_arg (var_args, gint);
809 if (GTK_LIST_STORE_IS_SORTED (list_store))
811 if (list_store->sort_column_id != -1)
813 GtkTreeDataSortHeader *header;
814 header = _gtk_tree_data_list_get_header (list_store->sort_list,
815 list_store->sort_column_id);
816 g_return_if_fail (header != NULL);
817 g_return_if_fail (header->func != NULL);
822 func = list_store->default_sort_func;
826 if (func != gtk_tree_data_list_compare_func)
827 maybe_need_sort = TRUE;
831 GValue value = { 0, };
834 if (column >= list_store->n_columns)
836 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
839 g_value_init (&value, list_store->column_headers[column]);
841 G_VALUE_COLLECT (&value, var_args, 0, &error);
844 g_warning ("%s: %s", G_STRLOC, error);
847 /* we purposely leak the value here, it might not be
848 * in a sane state if an error condition occoured
853 /* FIXME: instead of calling this n times, refactor with above */
854 emit_signal = gtk_list_store_real_set_value (list_store,
858 FALSE) || emit_signal;
860 if (func == gtk_tree_data_list_compare_func &&
861 column == list_store->sort_column_id)
862 maybe_need_sort = TRUE;
864 g_value_unset (&value);
866 column = va_arg (var_args, gint);
869 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
870 gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
876 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
877 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
878 gtk_tree_path_free (path);
883 * gtk_list_store_set:
884 * @list_store: a #GtkListStore
885 * @iter: row iterator
886 * @Varargs: pairs of column number and value, terminated with -1
888 * Sets the value of one or more cells in the row referenced by @iter.
889 * The variable argument list should contain integer column numbers,
890 * each column number followed by the value to be set.
891 * The list is terminated by a -1. For example, to set column 0 with type
892 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
893 * 0, "Foo", -1)</literal>.
896 gtk_list_store_set (GtkListStore *list_store,
902 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
903 g_return_if_fail (iter != NULL);
904 g_return_if_fail (iter->stamp == list_store->stamp);
906 va_start (var_args, iter);
907 gtk_list_store_set_valist (list_store, iter, var_args);
912 remove_link_saving_prev (GSList *list,
927 prev->next = link->next;
946 gtk_list_store_remove_silently (GtkListStore *list_store,
950 if (G_SLIST (iter->user_data)->data)
952 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
953 list_store->column_headers);
954 G_SLIST (iter->user_data)->data = NULL;
960 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
961 G_SLIST (iter->user_data),
964 list_store->length -= 1;
966 if (iter->user_data == list_store->tail)
967 list_store->tail = prev;
972 * gtk_list_store_remove:
973 * @list_store: A #GtkListStore
974 * @iter: A valid #GtkTreeIter
976 * Removes the given row from the list store. After being removed,
977 * @iter is set to be the next valid row, or invalidated if it pointed
978 * to the last row in @list_store.
980 * Return value: %TRUE if @iter is valid, %FALSE if not.
983 gtk_list_store_remove (GtkListStore *list_store,
989 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
990 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
992 next = G_SLIST (iter->user_data)->next;
993 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
995 validate_list_store (list_store);
997 gtk_list_store_remove_silently (list_store, iter, path);
999 validate_list_store (list_store);
1001 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
1002 gtk_tree_path_free (path);
1006 iter->stamp = list_store->stamp;
1007 iter->user_data = next;
1019 insert_after (GtkListStore *list_store,
1023 g_return_if_fail (sibling != NULL);
1024 g_return_if_fail (new_list != NULL);
1026 /* insert new node after list */
1027 new_list->next = sibling->next;
1028 sibling->next = new_list;
1030 /* if list was the tail, the new node is the new tail */
1031 if (sibling == ((GSList *) list_store->tail))
1032 list_store->tail = new_list;
1034 list_store->length += 1;
1038 * gtk_list_store_insert:
1039 * @list_store: A #GtkListStore
1040 * @iter: An unset #GtkTreeIter to set to the new row
1041 * @position: position to insert the new row
1043 * Creates a new row at @position. @iter will be changed to point to this new
1044 * row. If @position is larger than the number of rows on the list, then the
1045 * new row will be appended to the list. The row will be empty before this
1046 * function is called. To fill in values, you need to call gtk_list_store_set()
1047 * or gtk_list_store_set_value().
1051 gtk_list_store_insert (GtkListStore *list_store,
1059 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1060 g_return_if_fail (iter != NULL);
1061 g_return_if_fail (position >= 0);
1063 list_store->columns_dirty = TRUE;
1065 if (position == 0 ||
1066 GTK_LIST_STORE_IS_SORTED (list_store))
1068 gtk_list_store_prepend (list_store, iter);
1072 new_list = g_slist_alloc ();
1074 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
1078 /* position if off the end of the list, append it */
1079 gtk_list_store_append (list_store, iter);
1084 insert_after (list_store, list, new_list);
1086 iter->stamp = list_store->stamp;
1087 iter->user_data = new_list;
1089 validate_list_store (list_store);
1091 path = gtk_tree_path_new ();
1092 gtk_tree_path_append_index (path, position);
1093 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1094 gtk_tree_path_free (path);
1098 * gtk_list_store_insert_before:
1099 * @list_store: A #GtkListStore
1100 * @iter: An unset #GtkTreeIter to set to the new row
1101 * @sibling: A valid #GtkTreeIter, or %NULL
1103 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
1104 * appended to the end of the list. @iter will be changed to point to this new
1105 * row. The row will be empty before this function is called. To fill in values,
1106 * you need to call gtk_list_store_set() or gtk_list_store_set_value().
1110 gtk_list_store_insert_before (GtkListStore *list_store,
1112 GtkTreeIter *sibling)
1115 GSList *list, *prev, *new_list;
1118 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1119 g_return_if_fail (iter != NULL);
1121 g_return_if_fail (VALID_ITER (sibling, list_store));
1123 list_store->columns_dirty = TRUE;
1125 if (GTK_LIST_STORE_IS_SORTED (list_store))
1127 gtk_list_store_prepend (list_store, iter);
1131 if (sibling == NULL)
1133 gtk_list_store_append (list_store, iter);
1137 new_list = g_slist_alloc ();
1140 list = list_store->root;
1141 while (list && list != sibling->user_data)
1148 if (list != sibling->user_data)
1150 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1154 /* if there are no nodes, we become the list tail, otherwise we
1155 * are inserting before any existing nodes so we can't change
1159 if (list_store->root == NULL)
1160 list_store->tail = new_list;
1164 new_list->next = prev->next;
1165 prev->next = new_list;
1169 new_list->next = list_store->root;
1170 list_store->root = new_list;
1173 iter->stamp = list_store->stamp;
1174 iter->user_data = new_list;
1176 list_store->length += 1;
1178 validate_list_store (list_store);
1180 path = gtk_tree_path_new ();
1181 gtk_tree_path_append_index (path, i);
1182 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1183 gtk_tree_path_free (path);
1187 * gtk_list_store_insert_after:
1188 * @list_store: A #GtkListStore
1189 * @iter: An unset #GtkTreeIter to set to the new row
1190 * @sibling: A valid #GtkTreeIter, or %NULL
1192 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1193 * prepended to the beginning of the list. @iter will be changed to point to
1194 * this new row. The row will be empty after this function is called. To fill
1195 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1199 gtk_list_store_insert_after (GtkListStore *list_store,
1201 GtkTreeIter *sibling)
1204 GSList *list, *new_list;
1207 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1208 g_return_if_fail (iter != NULL);
1210 g_return_if_fail (VALID_ITER (sibling, list_store));
1212 list_store->columns_dirty = TRUE;
1214 if (sibling == NULL ||
1215 GTK_LIST_STORE_IS_SORTED (list_store))
1217 gtk_list_store_prepend (list_store, iter);
1221 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1224 g_return_if_fail (list == sibling->user_data);
1226 new_list = g_slist_alloc ();
1228 insert_after (list_store, list, new_list);
1230 iter->stamp = list_store->stamp;
1231 iter->user_data = new_list;
1233 validate_list_store (list_store);
1235 path = gtk_tree_path_new ();
1236 gtk_tree_path_append_index (path, i + 1);
1237 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1238 gtk_tree_path_free (path);
1242 * gtk_list_store_prepend:
1243 * @list_store: A #GtkListStore
1244 * @iter: An unset #GtkTreeIter to set to the prepend row
1246 * Prepends a new row to @list_store. @iter will be changed to point to this new
1247 * row. The row will be empty after this function is called. To fill in
1248 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1252 gtk_list_store_prepend (GtkListStore *list_store,
1257 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1258 g_return_if_fail (iter != NULL);
1260 iter->stamp = list_store->stamp;
1261 iter->user_data = g_slist_alloc ();
1263 list_store->columns_dirty = TRUE;
1265 if (list_store->root == NULL)
1266 list_store->tail = iter->user_data;
1268 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1269 list_store->root = iter->user_data;
1271 list_store->length += 1;
1273 validate_list_store (list_store);
1275 path = gtk_tree_path_new ();
1276 gtk_tree_path_append_index (path, 0);
1277 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1278 gtk_tree_path_free (path);
1282 * gtk_list_store_append:
1283 * @list_store: A #GtkListStore
1284 * @iter: An unset #GtkTreeIter to set to the appended row
1286 * Appends a new row to @list_store. @iter will be changed to point to this new
1287 * row. The row will be empty after this function is called. To fill in
1288 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1292 gtk_list_store_append (GtkListStore *list_store,
1297 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1298 g_return_if_fail (iter != NULL);
1300 list_store->columns_dirty = TRUE;
1302 if (GTK_LIST_STORE_IS_SORTED (list_store))
1304 gtk_list_store_prepend (list_store, iter);
1308 iter->stamp = list_store->stamp;
1309 iter->user_data = g_slist_alloc ();
1311 if (list_store->tail)
1312 ((GSList *)list_store->tail)->next = iter->user_data;
1314 list_store->root = iter->user_data;
1316 list_store->tail = iter->user_data;
1318 list_store->length += 1;
1320 validate_list_store (list_store);
1322 path = gtk_tree_path_new ();
1323 gtk_tree_path_append_index (path, list_store->length - 1);
1324 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1325 gtk_tree_path_free (path);
1329 * gtk_list_store_clear:
1330 * @list_store: a #GtkListStore.
1332 * Removes all rows from the list store.
1336 gtk_list_store_clear (GtkListStore *list_store)
1339 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1341 while (list_store->root)
1343 iter.stamp = list_store->stamp;
1344 iter.user_data = list_store->root;
1345 gtk_list_store_remove (list_store, &iter);
1350 * gtk_list_store_iter_is_valid:
1351 * @list_store: A #GtkListStore.
1352 * @iter: A #GtkTreeIter.
1354 * WARNING: This function is slow. Only use it for debugging and/or testing
1357 * Checks if the given iter is a valid iter for this #GtkListStore.
1359 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1362 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1367 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1368 g_return_val_if_fail (iter != NULL, FALSE);
1370 if (!VALID_ITER (iter, list_store))
1373 if (iter->user_data == list_store->root)
1375 if (iter->user_data == list_store->tail)
1378 for (list = ((GList *)list_store->root)->next; list; list = list->next)
1379 if (list == iter->user_data)
1386 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1390 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1392 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1396 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1403 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1405 GtkSelectionData *selection_data)
1407 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1409 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1410 * target, because the default handler does it for us, but
1411 * we do anyway for the convenience of someone maybe overriding the
1415 if (gtk_tree_set_row_drag_data (selection_data,
1416 GTK_TREE_MODEL (drag_source),
1423 /* FIXME handle text targets at least. */
1430 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1432 GtkSelectionData *selection_data)
1434 GtkTreeModel *tree_model;
1435 GtkListStore *list_store;
1436 GtkTreeModel *src_model = NULL;
1437 GtkTreePath *src_path = NULL;
1438 gboolean retval = FALSE;
1440 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1442 tree_model = GTK_TREE_MODEL (drag_dest);
1443 list_store = GTK_LIST_STORE (drag_dest);
1445 if (gtk_tree_get_row_drag_data (selection_data,
1448 src_model == tree_model)
1450 /* Copy the given row to a new position */
1451 GtkTreeIter src_iter;
1452 GtkTreeIter dest_iter;
1455 if (!gtk_tree_model_get_iter (src_model,
1462 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1463 prev = gtk_tree_path_copy (dest);
1465 if (!gtk_tree_path_prev (prev))
1467 /* dest was the first spot in the list; which means we are supposed
1470 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1477 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1481 GtkTreeIter tmp_iter = dest_iter;
1483 if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_model), "gtk-tree-model-drop-append")))
1484 gtk_list_store_append (GTK_LIST_STORE (tree_model), &dest_iter);
1486 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1487 &dest_iter, &tmp_iter);
1493 g_object_set_data (G_OBJECT (tree_model), "gtk-tree-model-drop-append",
1496 gtk_tree_path_free (prev);
1498 /* If we succeeded in creating dest_iter, copy data from src
1502 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1503 GtkTreeDataList *copy_head = NULL;
1504 GtkTreeDataList *copy_prev = NULL;
1505 GtkTreeDataList *copy_iter = NULL;
1512 copy_iter = _gtk_tree_data_list_node_copy (dl,
1513 list_store->column_headers[col]);
1515 if (copy_head == NULL)
1516 copy_head = copy_iter;
1519 copy_prev->next = copy_iter;
1521 copy_prev = copy_iter;
1527 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1528 G_SLIST (dest_iter.user_data)->data = copy_head;
1530 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1531 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter);
1532 gtk_tree_path_free (path);
1537 /* FIXME maybe add some data targets eventually, or handle text
1538 * targets in the simple case.
1545 gtk_tree_path_free (src_path);
1551 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1552 GtkTreePath *dest_path,
1553 GtkSelectionData *selection_data)
1556 GtkTreeModel *src_model = NULL;
1557 GtkTreePath *src_path = NULL;
1558 gboolean retval = FALSE;
1560 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1562 if (!gtk_tree_get_row_drag_data (selection_data,
1567 if (src_model != GTK_TREE_MODEL (drag_dest))
1570 if (gtk_tree_path_get_depth (dest_path) != 1)
1573 /* can drop before any existing node, or before one past any existing. */
1575 indices = gtk_tree_path_get_indices (dest_path);
1577 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1581 gtk_tree_path_free (src_path);
1586 /* Sorting and reordering */
1587 typedef struct _SortTuple
1595 gtk_list_store_reorder_func (gconstpointer a,
1599 SortTuple *a_reorder;
1600 SortTuple *b_reorder;
1602 a_reorder = (SortTuple *)a;
1603 b_reorder = (SortTuple *)b;
1605 if (a_reorder->offset < b_reorder->offset)
1607 if (a_reorder->offset > b_reorder->offset)
1614 * gtk_list_store_reorder:
1615 * @store: A #GtkTreeStore.
1616 * @new_order: An integer array indicating the new order for the list.
1618 * Reorders @store to follow the order indicated by @new_order. Note that
1619 * this function only works with unsorted stores.
1622 gtk_list_store_reorder (GtkListStore *store,
1626 GSList *current_list;
1628 SortTuple *sort_array;
1630 g_return_if_fail (GTK_IS_LIST_STORE (store));
1631 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1632 g_return_if_fail (new_order != NULL);
1634 sort_array = g_new (SortTuple, store->length);
1636 current_list = store->root;
1638 for (i = 0; i < store->length; i++)
1640 sort_array[i].offset = new_order[i];
1641 sort_array[i].el = current_list;
1643 current_list = current_list->next;
1646 g_qsort_with_data (sort_array,
1649 gtk_list_store_reorder_func,
1652 for (i = 0; i < store->length - 1; i++)
1653 G_SLIST (sort_array[i].el)->next = G_SLIST (sort_array[i+1].el);
1655 store->root = G_SLIST (sort_array[0].el);
1656 store->tail = G_SLIST (sort_array[store->length-1].el);
1657 G_SLIST (store->tail)->next = NULL;
1660 path = gtk_tree_path_new ();
1661 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1662 path, NULL, new_order);
1663 gtk_tree_path_free (path);
1664 g_free (sort_array);
1668 * gtk_list_store_swap:
1669 * @store: A #GtkListStore.
1670 * @a: A #GtkTreeIter.
1671 * @b: Another #GtkTreeIter.
1673 * Swaps @a and @b in @store. Note that this function only works with
1677 gtk_list_store_swap (GtkListStore *store,
1681 GSList *i, *prev_a = NULL, *prev_b = NULL;
1682 gint j, a_count = 0, b_count = 0, *order;
1685 g_return_if_fail (GTK_IS_LIST_STORE (store));
1686 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1687 g_return_if_fail (VALID_ITER (a, store));
1688 g_return_if_fail (VALID_ITER (b, store));
1690 if (a->user_data == b->user_data)
1693 if (a->user_data == store->root)
1697 for (i = store->root; i; i = i->next, a_count++)
1698 if (i->next == a->user_data)
1707 if (a->user_data == store->root)
1711 for (i = store->root; i; i = i->next, b_count++)
1712 if (i->next == b->user_data)
1722 store->root = b->user_data;
1724 prev_a->next = b->user_data;
1727 store->root = a->user_data;
1729 prev_b->next = a->user_data;
1731 /* think a_next inspead of a_prev here ... */
1732 prev_a = G_SLIST (a->user_data)->next;
1733 prev_b = G_SLIST (b->user_data)->next;
1735 G_SLIST (a->user_data)->next = prev_b;
1736 G_SLIST (b->user_data)->next = prev_a;
1739 order = g_new (gint, store->length);
1740 for (j = 0; j < store->length; j++)
1743 else if (j == b_count)
1748 path = gtk_tree_path_new ();
1749 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1751 gtk_tree_path_free (path);
1756 * gtk_list_store_move:
1757 * @store: A #GtkTreeStore.
1758 * @iter: A #GtkTreeIter.
1759 * @position: A #GtkTreePath.
1761 * Moves @iter in @store to the position before @position. Note that this
1762 * function only works with unsorted stores.
1765 gtk_list_store_move (GtkListStore *store,
1767 GtkTreePath *position)
1769 GSList *i, *prev = NULL, *new_prev = NULL;
1770 gint old_pos = 0, j = 0, *order;
1773 g_return_if_fail (GTK_IS_LIST_STORE (store));
1774 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1775 g_return_if_fail (VALID_ITER (iter, store));
1776 g_return_if_fail (position != NULL);
1778 if (gtk_tree_path_get_depth (position) != 1)
1781 if (iter->user_data == store->root)
1785 for (i = store->root; i; i = i->next, old_pos++)
1786 if (i->next == iter->user_data)
1795 if (old_pos == gtk_tree_path_get_indices (position)[0])
1798 if (gtk_tree_path_get_indices (position)[0] == 0)
1802 for (i = store->root; i; i = i->next, j++)
1803 if (j == gtk_tree_path_get_indices (position)[0] - 1)
1811 store->root = G_SLIST (iter->user_data)->next;
1813 prev->next = G_SLIST (iter->user_data)->next;
1817 G_SLIST (iter->user_data)->next = store->root;
1818 store->root = G_SLIST (iter->user_data);
1822 G_SLIST (iter->user_data)->next = new_prev->next;
1823 new_prev->next = G_SLIST (iter->user_data);
1827 order = g_new (gint, store->length);
1828 for (j = 0; j < store->length; j++)
1831 else if (j >= old_pos && j < gtk_tree_path_get_indices (position)[0])
1833 else if (j == gtk_tree_path_get_indices (position)[0])
1838 path = gtk_tree_path_new ();
1839 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1841 gtk_tree_path_free (path);
1847 gtk_list_store_compare_func (gconstpointer a,
1851 GtkListStore *list_store = user_data;
1852 GSList *el_a; /* Los Angeles? */
1857 GtkTreeIterCompareFunc func;
1861 if (list_store->sort_column_id != -1)
1863 GtkTreeDataSortHeader *header;
1865 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1866 list_store->sort_column_id);
1867 g_return_val_if_fail (header != NULL, 0);
1868 g_return_val_if_fail (header->func != NULL, 0);
1870 func = header->func;
1871 data = header->data;
1875 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1876 func = list_store->default_sort_func;
1877 data = list_store->default_sort_data;
1880 el_a = ((SortTuple *) a)->el;
1881 el_b = ((SortTuple *) b)->el;
1883 iter_a.stamp = list_store->stamp;
1884 iter_a.user_data = el_a;
1885 iter_b.stamp = list_store->stamp;
1886 iter_b.user_data = el_b;
1888 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1890 if (list_store->order == GTK_SORT_DESCENDING)
1894 else if (retval < 0)
1901 gtk_list_store_sort (GtkListStore *list_store)
1909 if (list_store->length <= 1)
1912 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1914 list = G_SLIST (list_store->root);
1916 sort_array = g_array_sized_new (FALSE, FALSE,
1918 list_store->length);
1920 for (i = 0; i < list_store->length; i++)
1924 /* If this fails, we are in an inconsistent state. Bad */
1925 g_return_if_fail (list != NULL);
1929 g_array_append_val (sort_array, tuple);
1934 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1936 for (i = 0; i < list_store->length - 1; i++)
1937 g_array_index (sort_array, SortTuple, i).el->next =
1938 g_array_index (sort_array, SortTuple, i + 1).el;
1939 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1940 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1941 list_store->tail = g_array_index (sort_array, SortTuple, list_store->length - 1).el;
1943 /* Let the world know about our new order */
1944 new_order = g_new (gint, list_store->length);
1945 for (i = 0; i < list_store->length; i++)
1946 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1948 path = gtk_tree_path_new ();
1949 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1950 path, NULL, new_order);
1951 gtk_tree_path_free (path);
1953 g_array_free (sort_array, TRUE);
1957 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1962 GSList *prev = NULL;
1963 GSList *next = NULL;
1964 GSList *list = G_SLIST (list_store->root);
1965 GtkTreePath *tmp_path;
1966 GtkTreeIter tmp_iter;
1973 GtkTreeIterCompareFunc func;
1976 if (list_store->length < 2)
1979 tmp_iter.stamp = list_store->stamp;
1981 if (list_store->sort_column_id != -1)
1983 GtkTreeDataSortHeader *header;
1984 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1985 list_store->sort_column_id);
1986 g_return_if_fail (header != NULL);
1987 g_return_if_fail (header->func != NULL);
1988 func = header->func;
1989 data = header->data;
1993 g_return_if_fail (list_store->default_sort_func != NULL);
1994 func = list_store->default_sort_func;
1995 data = list_store->default_sort_data;
1998 /* If it's the built in function, we don't sort. */
1999 if (func == gtk_tree_data_list_compare_func &&
2000 list_store->sort_column_id != column)
2004 /* First we find the iter, its prev, and its next */
2007 if (list == G_SLIST (iter->user_data))
2013 g_assert (list != NULL);
2017 /* Check the common case, where we don't need to sort it moved. */
2020 tmp_iter.user_data = prev;
2021 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2026 tmp_iter.user_data = next;
2027 cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2030 if (list_store->order == GTK_SORT_DESCENDING)
2043 if (prev == NULL && cmp_b <= 0)
2045 else if (next == NULL && cmp_a <= 0)
2047 else if (prev != NULL && next != NULL &&
2048 cmp_a <= 0 && cmp_b <= 0)
2051 /* We actually need to sort it */
2052 /* First, remove the old link. */
2055 list_store->root = next;
2059 list_store->tail = prev;
2062 /* FIXME: as an optimization, we can potentially start at next */
2064 list = G_SLIST (list_store->root);
2066 tmp_iter.user_data = list;
2067 if (list_store->order == GTK_SORT_DESCENDING)
2068 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2070 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2072 while ((list->next) && (cmp_a > 0))
2077 tmp_iter.user_data = list;
2078 if (list_store->order == GTK_SORT_DESCENDING)
2079 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2081 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2084 if ((!list->next) && (cmp_a > 0))
2087 list->next = G_SLIST (iter->user_data);
2088 list_store->tail = list->next;
2092 prev->next = G_SLIST (iter->user_data);
2093 G_SLIST (iter->user_data)->next = list;
2097 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
2098 list_store->root = G_SLIST (iter->user_data);
2101 /* Emit the reordered signal. */
2102 new_order = g_new (int, list_store->length);
2103 if (old_location < new_location)
2104 for (i = 0; i < list_store->length; i++)
2106 if (i < old_location ||
2109 else if (i >= old_location &&
2111 new_order[i] = i + 1;
2112 else if (i == new_location)
2113 new_order[i] = old_location;
2116 for (i = 0; i < list_store->length; i++)
2118 if (i < new_location ||
2121 else if (i > new_location &&
2123 new_order[i] = i - 1;
2124 else if (i == new_location)
2125 new_order[i] = old_location;
2128 tmp_path = gtk_tree_path_new ();
2129 tmp_iter.user_data = NULL;
2131 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
2135 gtk_tree_path_free (tmp_path);
2140 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
2141 gint *sort_column_id,
2144 GtkListStore *list_store = (GtkListStore *) sortable;
2146 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
2148 if (list_store->sort_column_id == -1)
2152 * sort_column_id = list_store->sort_column_id;
2154 * order = list_store->order;
2159 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
2160 gint sort_column_id,
2163 GtkListStore *list_store = (GtkListStore *) sortable;
2165 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2167 if ((list_store->sort_column_id == sort_column_id) &&
2168 (list_store->order == order))
2171 if (sort_column_id != -1)
2173 GtkTreeDataSortHeader *header = NULL;
2175 header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
2177 /* We want to make sure that we have a function */
2178 g_return_if_fail (header != NULL);
2179 g_return_if_fail (header->func != NULL);
2183 g_return_if_fail (list_store->default_sort_func != NULL);
2187 list_store->sort_column_id = sort_column_id;
2188 list_store->order = order;
2190 gtk_list_store_sort (list_store);
2192 gtk_tree_sortable_sort_column_changed (sortable);
2196 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
2197 gint sort_column_id,
2198 GtkTreeIterCompareFunc func,
2200 GtkDestroyNotify destroy)
2202 GtkListStore *list_store = (GtkListStore *) sortable;
2203 GtkTreeDataSortHeader *header = NULL;
2206 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2207 g_return_if_fail (func != NULL);
2209 for (list = list_store->sort_list; list; list = list->next)
2211 GtkTreeDataSortHeader *list_header;
2213 list_header = (GtkTreeDataSortHeader*) list->data;
2214 if (list_header->sort_column_id == sort_column_id)
2216 header = list_header;
2223 header = g_new0 (GtkTreeDataSortHeader, 1);
2224 header->sort_column_id = sort_column_id;
2225 list_store->sort_list = g_list_append (list_store->sort_list, header);
2228 if (header->destroy)
2230 GtkDestroyNotify d = header->destroy;
2232 header->destroy = NULL;
2236 header->func = func;
2237 header->data = data;
2238 header->destroy = destroy;
2243 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
2244 GtkTreeIterCompareFunc func,
2246 GtkDestroyNotify destroy)
2248 GtkListStore *list_store = (GtkListStore *) sortable;
2250 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2252 if (list_store->default_sort_destroy)
2254 GtkDestroyNotify d = list_store->default_sort_destroy;
2256 list_store->default_sort_destroy = NULL;
2257 d (list_store->default_sort_data);
2260 list_store->default_sort_func = func;
2261 list_store->default_sort_data = data;
2262 list_store->default_sort_destroy = destroy;
2266 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
2268 GtkListStore *list_store = (GtkListStore *) sortable;
2270 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
2272 return (list_store->default_sort_func != NULL);