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 <gobject/gvaluecollector.h>
22 #include "gtktreemodel.h"
23 #include "gtkliststore.h"
24 #include "gtktreedatalist.h"
25 #include "gtktreednd.h"
27 #define G_SLIST(x) ((GSList *) x)
28 #define GTK_LIST_STORE_IS_SORTED(list) (GTK_LIST_STORE (list)->sort_column_id != -2)
29 #define VALID_ITER(iter, list_store) (iter!= NULL && iter->user_data != NULL && list_store->stamp == iter->stamp)
31 static void gtk_list_store_init (GtkListStore *list_store);
32 static void gtk_list_store_class_init (GtkListStoreClass *class);
33 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
34 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
35 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
36 static void gtk_list_store_sortable_init (GtkTreeSortableIface *iface);
37 static void gtk_list_store_finalize (GObject *object);
38 static GtkTreeModelFlags gtk_list_store_get_flags (GtkTreeModel *tree_model);
39 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
40 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
42 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
45 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
47 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
51 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
53 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
56 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
58 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
60 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
64 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
69 static void gtk_list_store_set_n_columns (GtkListStore *list_store,
71 static void gtk_list_store_set_column_type (GtkListStore *list_store,
77 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
79 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
81 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
83 GtkSelectionData *selection_data);
84 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
86 GtkSelectionData *selection_data);
87 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
88 GtkTreePath *dest_path,
89 GtkSelectionData *selection_data);
93 static void gtk_list_store_sort (GtkListStore *list_store);
94 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
97 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
100 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
103 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
105 GtkTreeIterCompareFunc func,
107 GtkDestroyNotify destroy);
108 static void gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
109 GtkTreeIterCompareFunc func,
111 GtkDestroyNotify destroy);
112 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable);
114 static void gtk_list_store_move (GtkListStore *store,
120 static GObjectClass *parent_class = NULL;
124 validate_list_store (GtkListStore *list_store)
126 if (gtk_debug_flags & GTK_DEBUG_TREE)
128 g_assert (g_slist_length (list_store->root) == list_store->length);
130 g_assert (g_slist_last (list_store->root) == list_store->tail);
135 gtk_list_store_get_type (void)
137 static GType list_store_type = 0;
139 if (!list_store_type)
141 static const GTypeInfo list_store_info =
143 sizeof (GtkListStoreClass),
144 NULL, /* base_init */
145 NULL, /* base_finalize */
146 (GClassInitFunc) gtk_list_store_class_init,
147 NULL, /* class_finalize */
148 NULL, /* class_data */
149 sizeof (GtkListStore),
151 (GInstanceInitFunc) gtk_list_store_init,
154 static const GInterfaceInfo tree_model_info =
156 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
161 static const GInterfaceInfo drag_source_info =
163 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
168 static const GInterfaceInfo drag_dest_info =
170 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
175 static const GInterfaceInfo sortable_info =
177 (GInterfaceInitFunc) gtk_list_store_sortable_init,
182 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore",
183 &list_store_info, 0);
185 g_type_add_interface_static (list_store_type,
188 g_type_add_interface_static (list_store_type,
189 GTK_TYPE_TREE_DRAG_SOURCE,
191 g_type_add_interface_static (list_store_type,
192 GTK_TYPE_TREE_DRAG_DEST,
194 g_type_add_interface_static (list_store_type,
195 GTK_TYPE_TREE_SORTABLE,
199 return list_store_type;
203 gtk_list_store_class_init (GtkListStoreClass *class)
205 GObjectClass *object_class;
207 parent_class = g_type_class_peek_parent (class);
208 object_class = (GObjectClass*) class;
210 object_class->finalize = gtk_list_store_finalize;
214 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
216 iface->get_flags = gtk_list_store_get_flags;
217 iface->get_n_columns = gtk_list_store_get_n_columns;
218 iface->get_column_type = gtk_list_store_get_column_type;
219 iface->get_iter = gtk_list_store_get_iter;
220 iface->get_path = gtk_list_store_get_path;
221 iface->get_value = gtk_list_store_get_value;
222 iface->iter_next = gtk_list_store_iter_next;
223 iface->iter_children = gtk_list_store_iter_children;
224 iface->iter_has_child = gtk_list_store_iter_has_child;
225 iface->iter_n_children = gtk_list_store_iter_n_children;
226 iface->iter_nth_child = gtk_list_store_iter_nth_child;
227 iface->iter_parent = gtk_list_store_iter_parent;
231 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
233 iface->row_draggable = real_gtk_list_store_row_draggable;
234 iface->drag_data_delete = gtk_list_store_drag_data_delete;
235 iface->drag_data_get = gtk_list_store_drag_data_get;
239 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
241 iface->drag_data_received = gtk_list_store_drag_data_received;
242 iface->row_drop_possible = gtk_list_store_row_drop_possible;
246 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
248 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
249 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
250 iface->set_sort_func = gtk_list_store_set_sort_func;
251 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
252 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
256 gtk_list_store_init (GtkListStore *list_store)
258 list_store->root = NULL;
259 list_store->tail = NULL;
260 list_store->sort_list = NULL;
261 list_store->stamp = g_random_int ();
262 list_store->length = 0;
263 list_store->sort_column_id = -2;
264 list_store->columns_dirty = FALSE;
268 * gtk_list_store_new:
269 * @n_columns: number of columns in the list store
270 * @Varargs: all #GType types for the columns, from first to last
272 * Creates a new list store as with @n_columns columns each of the types passed
273 * in. Note that only types derived from standard GObject fundamental types
276 * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
277 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
278 * int, string and #GdkPixbuf respectively.
280 * Return value: a new #GtkListStore
283 gtk_list_store_new (gint n_columns,
286 GtkListStore *retval;
290 g_return_val_if_fail (n_columns > 0, NULL);
292 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
293 gtk_list_store_set_n_columns (retval, n_columns);
295 va_start (args, n_columns);
297 for (i = 0; i < n_columns; i++)
299 GType type = va_arg (args, GType);
300 if (! _gtk_tree_data_list_check_type (type))
302 g_warning ("%s: Invalid type %s passed to gtk_list_store_new\n",
303 G_STRLOC, g_type_name (type));
304 g_object_unref (retval);
308 gtk_list_store_set_column_type (retval, i, type);
318 * gtk_list_store_newv:
319 * @n_columns: number of columns in the list store
320 * @types: an array of #GType types for the columns, from first to last
322 * Non-vararg creation function. Used primarily by language bindings.
324 * Return value: a new #GtkListStore
327 gtk_list_store_newv (gint n_columns,
330 GtkListStore *retval;
333 g_return_val_if_fail (n_columns > 0, NULL);
335 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
336 gtk_list_store_set_n_columns (retval, n_columns);
338 for (i = 0; i < n_columns; i++)
340 if (! _gtk_tree_data_list_check_type (types[i]))
342 g_warning ("%s: Invalid type %s passed to gtk_list_store_newv\n",
343 G_STRLOC, g_type_name (types[i]));
344 g_object_unref (retval);
348 gtk_list_store_set_column_type (retval, i, types[i]);
355 * gtk_list_store_set_column_types:
356 * @list_store: A #GtkListStore
357 * @n_columns: Number of columns for the list store
358 * @types: An array length n of #GTypes
360 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
361 * and should only be used when constructing a new #GtkListStore. It will not
362 * function after a row has been added, or a method on the #GtkTreeModel
363 * interface is called.
366 gtk_list_store_set_column_types (GtkListStore *list_store,
372 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
373 g_return_if_fail (list_store->columns_dirty == 0);
375 gtk_list_store_set_n_columns (list_store, n_columns);
376 for (i = 0; i < n_columns; i++)
378 if (! _gtk_tree_data_list_check_type (types[i]))
380 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_types\n", G_STRLOC, g_type_name (types[i]));
383 gtk_list_store_set_column_type (list_store, i, types[i]);
388 gtk_list_store_set_n_columns (GtkListStore *list_store,
393 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
394 g_return_if_fail (n_columns > 0);
396 if (list_store->n_columns == n_columns)
399 new_columns = g_new0 (GType, n_columns);
400 if (list_store->column_headers)
402 /* copy the old header orders over */
403 if (n_columns >= list_store->n_columns)
404 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
406 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
408 g_free (list_store->column_headers);
411 if (list_store->sort_list)
412 _gtk_tree_data_list_header_free (list_store->sort_list);
414 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
416 list_store->column_headers = new_columns;
417 list_store->n_columns = n_columns;
421 gtk_list_store_set_column_type (GtkListStore *list_store,
425 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
426 g_return_if_fail (column >=0 && column < list_store->n_columns);
428 if (!_gtk_tree_data_list_check_type (type))
430 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_type\n", G_STRLOC, g_type_name (type));
434 list_store->column_headers[column] = type;
438 gtk_list_store_finalize (GObject *object)
440 GtkListStore *list_store = GTK_LIST_STORE (object);
442 g_slist_foreach (list_store->root, (GFunc) _gtk_tree_data_list_free, list_store->column_headers);
443 g_slist_free (list_store->root);
445 _gtk_tree_data_list_header_free (list_store->sort_list);
446 g_free (list_store->column_headers);
448 if (list_store->default_sort_destroy)
450 GtkDestroyNotify d = list_store->default_sort_destroy;
452 list_store->default_sort_destroy = NULL;
453 d (list_store->default_sort_data);
454 list_store->default_sort_data = NULL;
458 (* parent_class->finalize) (object);
461 /* Fulfill the GtkTreeModel requirements */
462 static GtkTreeModelFlags
463 gtk_list_store_get_flags (GtkTreeModel *tree_model)
465 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
467 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
471 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
473 GtkListStore *list_store = (GtkListStore *) tree_model;
475 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
477 list_store->columns_dirty = TRUE;
479 return list_store->n_columns;
483 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
486 GtkListStore *list_store = (GtkListStore *) tree_model;
488 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
489 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
490 index >= 0, G_TYPE_INVALID);
492 list_store->columns_dirty = TRUE;
494 return list_store->column_headers[index];
498 gtk_list_store_get_iter (GtkTreeModel *tree_model,
502 GtkListStore *list_store = (GtkListStore *) tree_model;
506 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
507 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
509 list_store->columns_dirty = TRUE;
511 i = gtk_tree_path_get_indices (path)[0];
513 if (i >= list_store->length)
516 list = g_slist_nth (G_SLIST (list_store->root), i);
518 /* If this fails, list_store->length has gotten mangled. */
521 iter->stamp = list_store->stamp;
522 iter->user_data = list;
528 gtk_list_store_get_path (GtkTreeModel *tree_model,
535 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
536 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
537 if (G_SLIST (iter->user_data) == G_SLIST (GTK_LIST_STORE (tree_model)->tail))
539 retval = gtk_tree_path_new ();
540 gtk_tree_path_append_index (retval, GTK_LIST_STORE (tree_model)->length - 1);
544 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
546 if (list == G_SLIST (iter->user_data))
553 retval = gtk_tree_path_new ();
554 gtk_tree_path_append_index (retval, i);
559 gtk_list_store_get_value (GtkTreeModel *tree_model,
564 GtkTreeDataList *list;
565 gint tmp_column = column;
567 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
568 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
569 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
571 list = G_SLIST (iter->user_data)->data;
573 while (tmp_column-- > 0 && list)
577 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
579 _gtk_tree_data_list_node_to_value (list,
580 GTK_LIST_STORE (tree_model)->column_headers[column],
585 gtk_list_store_iter_next (GtkTreeModel *tree_model,
588 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
589 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
591 iter->user_data = G_SLIST (iter->user_data)->next;
593 return (iter->user_data != NULL);
597 gtk_list_store_iter_children (GtkTreeModel *tree_model,
601 /* this is a list, nodes have no children */
605 /* but if parent == NULL we return the list itself as children of the
609 if (GTK_LIST_STORE (tree_model)->root)
611 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
612 iter->user_data = GTK_LIST_STORE (tree_model)->root;
620 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
627 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
630 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
632 return GTK_LIST_STORE (tree_model)->length;
634 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
639 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
646 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
651 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
655 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
656 iter->user_data = child;
664 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
672 gtk_list_store_real_set_value (GtkListStore *list_store,
678 GtkTreeDataList *list;
679 GtkTreeDataList *prev;
680 gint old_column = column;
681 GValue real_value = {0, };
682 gboolean converted = FALSE;
683 gboolean retval = FALSE;
685 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
686 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
687 g_return_val_if_fail (column >= 0 && column < list_store->n_columns, FALSE);
688 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
690 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
692 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
693 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
695 g_warning ("%s: Unable to convert from %s to %s\n",
697 g_type_name (G_VALUE_TYPE (value)),
698 g_type_name (list_store->column_headers[column]));
701 if (!g_value_transform (value, &real_value))
703 g_warning ("%s: Unable to make conversion from %s to %s\n",
705 g_type_name (G_VALUE_TYPE (value)),
706 g_type_name (list_store->column_headers[column]));
707 g_value_unset (&real_value);
713 prev = list = G_SLIST (iter->user_data)->data;
720 _gtk_tree_data_list_value_to_node (list, &real_value);
722 _gtk_tree_data_list_value_to_node (list, value);
725 g_value_unset (&real_value);
726 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
727 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
736 if (G_SLIST (iter->user_data)->data == NULL)
738 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
743 list = prev->next = _gtk_tree_data_list_alloc ();
749 list->next = _gtk_tree_data_list_alloc ();
756 _gtk_tree_data_list_value_to_node (list, &real_value);
758 _gtk_tree_data_list_value_to_node (list, value);
762 g_value_unset (&real_value);
764 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
765 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
772 * gtk_list_store_set_value:
773 * @list_store: A #GtkListStore
774 * @iter: A valid #GtkTreeIter for the row being modified
775 * @column: column number to modify
776 * @value: new value for the cell
778 * Sets the data in the cell specified by @iter and @column.
779 * The type of @value must be convertible to the type of the
784 gtk_list_store_set_value (GtkListStore *list_store,
789 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
790 g_return_if_fail (VALID_ITER (iter, list_store));
791 g_return_if_fail (column >= 0 && column < list_store->n_columns);
792 g_return_if_fail (G_IS_VALUE (value));
794 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
798 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
799 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
800 gtk_tree_path_free (path);
805 * gtk_list_store_set_valist:
806 * @list_store: A #GtkListStore
807 * @iter: A valid #GtkTreeIter for the row being modified
808 * @var_args: va_list of column/value pairs
810 * See gtk_list_store_set(); this version takes a va_list for use by language
815 gtk_list_store_set_valist (GtkListStore *list_store,
820 gboolean emit_signal = FALSE;
821 gboolean maybe_need_sort = FALSE;
822 GtkTreeIterCompareFunc func = NULL;
824 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
825 g_return_if_fail (VALID_ITER (iter, list_store));
827 column = va_arg (var_args, gint);
829 if (GTK_LIST_STORE_IS_SORTED (list_store))
831 if (list_store->sort_column_id != -1)
833 GtkTreeDataSortHeader *header;
834 header = _gtk_tree_data_list_get_header (list_store->sort_list,
835 list_store->sort_column_id);
836 g_return_if_fail (header != NULL);
837 g_return_if_fail (header->func != NULL);
842 func = list_store->default_sort_func;
846 if (func != gtk_tree_data_list_compare_func)
847 maybe_need_sort = TRUE;
851 GValue value = { 0, };
854 if (column >= list_store->n_columns)
856 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
859 g_value_init (&value, list_store->column_headers[column]);
861 G_VALUE_COLLECT (&value, var_args, 0, &error);
864 g_warning ("%s: %s", G_STRLOC, error);
867 /* we purposely leak the value here, it might not be
868 * in a sane state if an error condition occoured
873 /* FIXME: instead of calling this n times, refactor with above */
874 emit_signal = gtk_list_store_real_set_value (list_store,
878 FALSE) || emit_signal;
880 if (func == gtk_tree_data_list_compare_func &&
881 column == list_store->sort_column_id)
882 maybe_need_sort = TRUE;
884 g_value_unset (&value);
886 column = va_arg (var_args, gint);
889 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
890 gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
896 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
897 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
898 gtk_tree_path_free (path);
903 * gtk_list_store_set:
904 * @list_store: a #GtkListStore
905 * @iter: row iterator
906 * @Varargs: pairs of column number and value, terminated with -1
908 * Sets the value of one or more cells in the row referenced by @iter.
909 * The variable argument list should contain integer column numbers,
910 * each column number followed by the value to be set.
911 * The list is terminated by a -1. For example, to set column 0 with type
912 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
913 * 0, "Foo", -1)</literal>.
916 gtk_list_store_set (GtkListStore *list_store,
922 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
923 g_return_if_fail (iter != NULL);
924 g_return_if_fail (iter->stamp == list_store->stamp);
926 va_start (var_args, iter);
927 gtk_list_store_set_valist (list_store, iter, var_args);
932 remove_link_saving_prev (GSList *list,
947 prev->next = link->next;
966 gtk_list_store_remove_silently (GtkListStore *list_store,
970 if (G_SLIST (iter->user_data)->data)
972 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
973 list_store->column_headers);
974 G_SLIST (iter->user_data)->data = NULL;
980 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
981 G_SLIST (iter->user_data),
984 list_store->length -= 1;
986 if (iter->user_data == list_store->tail)
987 list_store->tail = prev;
989 g_slist_free (G_SLIST (iter->user_data));
994 * gtk_list_store_remove:
995 * @list_store: A #GtkListStore
996 * @iter: A valid #GtkTreeIter
998 * Removes the given row from the list store. After being removed,
999 * @iter is set to be the next valid row, or invalidated if it pointed
1000 * to the last row in @list_store.
1002 * Return value: %TRUE if @iter is valid, %FALSE if not.
1005 gtk_list_store_remove (GtkListStore *list_store,
1011 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1012 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
1014 next = G_SLIST (iter->user_data)->next;
1015 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1017 validate_list_store (list_store);
1019 gtk_list_store_remove_silently (list_store, iter, path);
1021 validate_list_store (list_store);
1023 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
1024 gtk_tree_path_free (path);
1028 iter->stamp = list_store->stamp;
1029 iter->user_data = next;
1041 insert_after (GtkListStore *list_store,
1045 g_return_if_fail (sibling != NULL);
1046 g_return_if_fail (new_list != NULL);
1048 /* insert new node after list */
1049 new_list->next = sibling->next;
1050 sibling->next = new_list;
1052 /* if list was the tail, the new node is the new tail */
1053 if (sibling == ((GSList *) list_store->tail))
1054 list_store->tail = new_list;
1056 list_store->length += 1;
1060 * gtk_list_store_insert:
1061 * @list_store: A #GtkListStore
1062 * @iter: An unset #GtkTreeIter to set to the new row
1063 * @position: position to insert the new row
1065 * Creates a new row at @position. @iter will be changed to point to this new
1066 * row. If @position is larger than the number of rows on the list, then the
1067 * new row will be appended to the list. The row will be empty before this
1068 * function is called. To fill in values, you need to call gtk_list_store_set()
1069 * or gtk_list_store_set_value().
1073 gtk_list_store_insert (GtkListStore *list_store,
1081 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1082 g_return_if_fail (iter != NULL);
1083 g_return_if_fail (position >= 0);
1085 list_store->columns_dirty = TRUE;
1087 if (position == 0 ||
1088 GTK_LIST_STORE_IS_SORTED (list_store))
1090 gtk_list_store_prepend (list_store, iter);
1094 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
1098 /* position if off the end of the list, append it */
1099 gtk_list_store_append (list_store, iter);
1104 new_list = g_slist_alloc ();
1106 insert_after (list_store, list, new_list);
1108 iter->stamp = list_store->stamp;
1109 iter->user_data = new_list;
1111 validate_list_store (list_store);
1113 path = gtk_tree_path_new ();
1114 gtk_tree_path_append_index (path, position);
1115 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1116 gtk_tree_path_free (path);
1120 * gtk_list_store_insert_before:
1121 * @list_store: A #GtkListStore
1122 * @iter: An unset #GtkTreeIter to set to the new row
1123 * @sibling: A valid #GtkTreeIter, or %NULL
1125 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
1126 * appended to the end of the list. @iter will be changed to point to this new
1127 * row. The row will be empty before this function is called. To fill in values,
1128 * you need to call gtk_list_store_set() or gtk_list_store_set_value().
1132 gtk_list_store_insert_before (GtkListStore *list_store,
1134 GtkTreeIter *sibling)
1137 GSList *list, *prev, *new_list;
1140 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1141 g_return_if_fail (iter != NULL);
1143 g_return_if_fail (VALID_ITER (sibling, list_store));
1145 list_store->columns_dirty = TRUE;
1147 if (GTK_LIST_STORE_IS_SORTED (list_store))
1149 gtk_list_store_prepend (list_store, iter);
1153 if (sibling == NULL)
1155 gtk_list_store_append (list_store, iter);
1159 new_list = g_slist_alloc ();
1162 list = list_store->root;
1163 while (list && list != sibling->user_data)
1170 if (list != sibling->user_data)
1172 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1176 /* if there are no nodes, we become the list tail, otherwise we
1177 * are inserting before any existing nodes so we can't change
1181 if (list_store->root == NULL)
1182 list_store->tail = new_list;
1186 new_list->next = prev->next;
1187 prev->next = new_list;
1191 new_list->next = list_store->root;
1192 list_store->root = new_list;
1195 iter->stamp = list_store->stamp;
1196 iter->user_data = new_list;
1198 list_store->length += 1;
1200 validate_list_store (list_store);
1202 path = gtk_tree_path_new ();
1203 gtk_tree_path_append_index (path, i);
1204 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1205 gtk_tree_path_free (path);
1209 * gtk_list_store_insert_after:
1210 * @list_store: A #GtkListStore
1211 * @iter: An unset #GtkTreeIter to set to the new row
1212 * @sibling: A valid #GtkTreeIter, or %NULL
1214 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1215 * prepended to the beginning of the list. @iter will be changed to point to
1216 * this new row. The row will be empty after this function is called. To fill
1217 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1221 gtk_list_store_insert_after (GtkListStore *list_store,
1223 GtkTreeIter *sibling)
1226 GSList *list, *new_list;
1229 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1230 g_return_if_fail (iter != NULL);
1232 g_return_if_fail (VALID_ITER (sibling, list_store));
1234 list_store->columns_dirty = TRUE;
1236 if (sibling == NULL ||
1237 GTK_LIST_STORE_IS_SORTED (list_store))
1239 gtk_list_store_prepend (list_store, iter);
1243 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1246 g_return_if_fail (list == sibling->user_data);
1248 new_list = g_slist_alloc ();
1250 insert_after (list_store, list, new_list);
1252 iter->stamp = list_store->stamp;
1253 iter->user_data = new_list;
1255 validate_list_store (list_store);
1257 path = gtk_tree_path_new ();
1258 gtk_tree_path_append_index (path, i + 1);
1259 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1260 gtk_tree_path_free (path);
1264 * gtk_list_store_prepend:
1265 * @list_store: A #GtkListStore
1266 * @iter: An unset #GtkTreeIter to set to the prepend row
1268 * Prepends a new row to @list_store. @iter will be changed to point to this new
1269 * row. The row will be empty after this function is called. To fill in
1270 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1274 gtk_list_store_prepend (GtkListStore *list_store,
1279 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1280 g_return_if_fail (iter != NULL);
1282 iter->stamp = list_store->stamp;
1283 iter->user_data = g_slist_alloc ();
1285 list_store->columns_dirty = TRUE;
1287 if (list_store->root == NULL)
1288 list_store->tail = iter->user_data;
1290 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1291 list_store->root = iter->user_data;
1293 list_store->length += 1;
1295 validate_list_store (list_store);
1297 path = gtk_tree_path_new ();
1298 gtk_tree_path_append_index (path, 0);
1299 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1300 gtk_tree_path_free (path);
1304 * gtk_list_store_append:
1305 * @list_store: A #GtkListStore
1306 * @iter: An unset #GtkTreeIter to set to the appended row
1308 * Appends a new row to @list_store. @iter will be changed to point to this new
1309 * row. The row will be empty after this function is called. To fill in
1310 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1314 gtk_list_store_append (GtkListStore *list_store,
1319 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1320 g_return_if_fail (iter != NULL);
1322 list_store->columns_dirty = TRUE;
1324 if (GTK_LIST_STORE_IS_SORTED (list_store))
1326 gtk_list_store_prepend (list_store, iter);
1330 iter->stamp = list_store->stamp;
1331 iter->user_data = g_slist_alloc ();
1333 if (list_store->tail)
1334 ((GSList *)list_store->tail)->next = iter->user_data;
1336 list_store->root = iter->user_data;
1338 list_store->tail = iter->user_data;
1340 list_store->length += 1;
1342 validate_list_store (list_store);
1344 path = gtk_tree_path_new ();
1345 gtk_tree_path_append_index (path, list_store->length - 1);
1346 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1347 gtk_tree_path_free (path);
1351 * gtk_list_store_clear:
1352 * @list_store: a #GtkListStore.
1354 * Removes all rows from the list store.
1358 gtk_list_store_clear (GtkListStore *list_store)
1361 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1363 while (list_store->root)
1365 iter.stamp = list_store->stamp;
1366 iter.user_data = list_store->root;
1367 gtk_list_store_remove (list_store, &iter);
1372 * gtk_list_store_iter_is_valid:
1373 * @list_store: A #GtkListStore.
1374 * @iter: A #GtkTreeIter.
1376 * WARNING: This function is slow. Only use it for debugging and/or testing
1379 * Checks if the given iter is a valid iter for this #GtkListStore.
1381 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1386 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1391 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1392 g_return_val_if_fail (iter != NULL, FALSE);
1394 if (!VALID_ITER (iter, list_store))
1397 if (iter->user_data == list_store->root)
1399 if (iter->user_data == list_store->tail)
1402 for (list = ((GList *)list_store->root)->next; list; list = list->next)
1403 if (list == iter->user_data)
1409 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
1416 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1420 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1422 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1426 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1433 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1435 GtkSelectionData *selection_data)
1437 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1439 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1440 * target, because the default handler does it for us, but
1441 * we do anyway for the convenience of someone maybe overriding the
1445 if (gtk_tree_set_row_drag_data (selection_data,
1446 GTK_TREE_MODEL (drag_source),
1453 /* FIXME handle text targets at least. */
1460 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1462 GtkSelectionData *selection_data)
1464 GtkTreeModel *tree_model;
1465 GtkListStore *list_store;
1466 GtkTreeModel *src_model = NULL;
1467 GtkTreePath *src_path = NULL;
1468 gboolean retval = FALSE;
1470 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1472 tree_model = GTK_TREE_MODEL (drag_dest);
1473 list_store = GTK_LIST_STORE (drag_dest);
1475 if (gtk_tree_get_row_drag_data (selection_data,
1478 src_model == tree_model)
1480 /* Copy the given row to a new position */
1481 GtkTreeIter src_iter;
1482 GtkTreeIter dest_iter;
1485 if (!gtk_tree_model_get_iter (src_model,
1492 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1493 prev = gtk_tree_path_copy (dest);
1495 if (!gtk_tree_path_prev (prev))
1497 /* dest was the first spot in the list; which means we are supposed
1500 gtk_list_store_prepend (list_store, &dest_iter);
1506 if (gtk_tree_model_get_iter (tree_model, &dest_iter, prev))
1508 GtkTreeIter tmp_iter = dest_iter;
1510 gtk_list_store_insert_after (list_store, &dest_iter, &tmp_iter);
1516 gtk_tree_path_free (prev);
1518 /* If we succeeded in creating dest_iter, copy data from src
1522 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1523 GtkTreeDataList *copy_head = NULL;
1524 GtkTreeDataList *copy_prev = NULL;
1525 GtkTreeDataList *copy_iter = NULL;
1532 copy_iter = _gtk_tree_data_list_node_copy (dl,
1533 list_store->column_headers[col]);
1535 if (copy_head == NULL)
1536 copy_head = copy_iter;
1539 copy_prev->next = copy_iter;
1541 copy_prev = copy_iter;
1547 dest_iter.stamp = list_store->stamp;
1548 G_SLIST (dest_iter.user_data)->data = copy_head;
1550 path = gtk_list_store_get_path (tree_model, &dest_iter);
1551 gtk_tree_model_row_changed (tree_model, path, &dest_iter);
1552 gtk_tree_path_free (path);
1557 /* FIXME maybe add some data targets eventually, or handle text
1558 * targets in the simple case.
1565 gtk_tree_path_free (src_path);
1571 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1572 GtkTreePath *dest_path,
1573 GtkSelectionData *selection_data)
1576 GtkTreeModel *src_model = NULL;
1577 GtkTreePath *src_path = NULL;
1578 gboolean retval = FALSE;
1580 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1582 /* don't accept drops if the list has been sorted */
1583 if (GTK_LIST_STORE_IS_SORTED (drag_dest))
1586 if (!gtk_tree_get_row_drag_data (selection_data,
1591 if (src_model != GTK_TREE_MODEL (drag_dest))
1594 if (gtk_tree_path_get_depth (dest_path) != 1)
1597 /* can drop before any existing node, or before one past any existing. */
1599 indices = gtk_tree_path_get_indices (dest_path);
1601 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1606 gtk_tree_path_free (src_path);
1611 /* Sorting and reordering */
1612 typedef struct _SortTuple
1620 gtk_list_store_reorder_func (gconstpointer a,
1624 SortTuple *a_reorder;
1625 SortTuple *b_reorder;
1627 a_reorder = (SortTuple *)a;
1628 b_reorder = (SortTuple *)b;
1630 if (a_reorder->offset < b_reorder->offset)
1632 if (a_reorder->offset > b_reorder->offset)
1639 * gtk_list_store_reorder:
1640 * @store: A #GtkListStore.
1641 * @new_order: An integer array indicating the new order for the list.
1643 * Reorders @store to follow the order indicated by @new_order. Note that
1644 * this function only works with unsorted stores.
1649 gtk_list_store_reorder (GtkListStore *store,
1653 GSList *current_list;
1655 SortTuple *sort_array;
1657 g_return_if_fail (GTK_IS_LIST_STORE (store));
1658 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1659 g_return_if_fail (new_order != NULL);
1661 sort_array = g_new (SortTuple, store->length);
1663 current_list = store->root;
1665 for (i = 0; i < store->length; i++)
1667 sort_array[i].offset = new_order[i];
1668 sort_array[i].el = current_list;
1670 current_list = current_list->next;
1673 g_qsort_with_data (sort_array,
1676 gtk_list_store_reorder_func,
1679 for (i = 0; i < store->length - 1; i++)
1680 G_SLIST (sort_array[i].el)->next = G_SLIST (sort_array[i+1].el);
1682 store->root = G_SLIST (sort_array[0].el);
1683 store->tail = G_SLIST (sort_array[store->length-1].el);
1684 G_SLIST (store->tail)->next = NULL;
1687 path = gtk_tree_path_new ();
1688 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1689 path, NULL, new_order);
1690 gtk_tree_path_free (path);
1691 g_free (sort_array);
1695 * gtk_list_store_swap:
1696 * @store: A #GtkListStore.
1697 * @a: A #GtkTreeIter.
1698 * @b: Another #GtkTreeIter.
1700 * Swaps @a and @b in @store. Note that this function only works with
1706 gtk_list_store_swap (GtkListStore *store,
1710 GSList *i, *prev_a = NULL, *prev_b = NULL;
1711 gint j, a_count = 0, b_count = 0, *order;
1714 g_return_if_fail (GTK_IS_LIST_STORE (store));
1715 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1716 g_return_if_fail (VALID_ITER (a, store));
1717 g_return_if_fail (VALID_ITER (b, store));
1719 if (a->user_data == b->user_data)
1722 if (a->user_data == store->root)
1726 for (i = store->root; i; i = i->next, a_count++)
1727 if (i->next == a->user_data)
1736 if (b->user_data == store->root)
1740 for (i = store->root; i; i = i->next, b_count++)
1741 if (i->next == b->user_data)
1751 store->root = b->user_data;
1753 prev_a->next = b->user_data;
1756 store->root = a->user_data;
1758 prev_b->next = a->user_data;
1760 /* think a_next inspead of a_prev here ... */
1761 prev_a = G_SLIST (a->user_data)->next;
1762 prev_b = G_SLIST (b->user_data)->next;
1764 G_SLIST (a->user_data)->next = prev_b;
1765 G_SLIST (b->user_data)->next = prev_a;
1767 /* update tail if needed */
1768 if (! G_SLIST (a->user_data)->next)
1769 store->tail = G_SLIST (a->user_data);
1770 else if (! G_SLIST (b->user_data)->next)
1771 store->tail = G_SLIST (b->user_data);
1774 order = g_new (gint, store->length);
1775 for (j = 0; j < store->length; j++)
1778 else if (j == b_count)
1783 path = gtk_tree_path_new ();
1784 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1786 gtk_tree_path_free (path);
1791 gtk_list_store_move (GtkListStore *store,
1793 GtkTreeIter *position,
1797 GSList *i, *a, *prev = NULL, *tmp;
1798 gint new_pos = 0, old_pos = 0, j = 0, *order;
1799 GtkTreePath *path = NULL, *pos_path = NULL;
1801 g_return_if_fail (GTK_IS_LIST_STORE (store));
1802 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1803 g_return_if_fail (VALID_ITER (iter, store));
1805 g_return_if_fail (VALID_ITER (position, store));
1807 /* lots of sanity checks */
1810 path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), iter);
1811 pos_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), position);
1813 if (gtk_tree_path_get_depth (pos_path) != 1)
1814 goto free_paths_and_out;
1817 * moving the iter before path or "path + 1" doesn't make sense
1819 * moving the iter before path or "path - 1" doesn't make sense
1821 if (!gtk_tree_path_compare (path, pos_path))
1822 goto free_paths_and_out;
1825 gtk_tree_path_next (path);
1827 gtk_tree_path_prev (path);
1829 if (!gtk_tree_path_compare (path, pos_path))
1830 goto free_paths_and_out;
1832 gtk_tree_path_free (path);
1836 /* getting destination iters */
1837 if (before && position)
1839 if (gtk_tree_path_get_indices (pos_path)[0] > 0)
1841 gtk_tree_path_prev (pos_path);
1842 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &dst_a, pos_path))
1843 a = G_SLIST (dst_a.user_data);
1846 gtk_tree_path_next (pos_path);
1851 else if (before && !position)
1856 a = G_SLIST (position->user_data);
1861 /* don't try to reorder the iter to it's own position */
1864 if (a == iter->user_data)
1865 goto free_paths_and_out;
1869 if (iter->user_data == store->tail)
1870 goto free_paths_and_out;
1874 if (iter->user_data == store->root)
1875 goto free_paths_and_out;
1878 /* getting the old prev node */
1879 if (iter->user_data == store->root)
1883 for (i = store->root; i; i = i->next, old_pos++)
1884 if (i->next == iter->user_data)
1895 store->root = G_SLIST (iter->user_data)->next;
1898 prev->next = G_SLIST (iter->user_data)->next;
1903 /* and reinsert it */
1908 a->next = G_SLIST (iter->user_data);
1909 a->next->next = tmp;
1911 else if (!a && !before)
1913 tmp = G_SLIST (store->root);
1915 store->root = G_SLIST (iter->user_data);
1916 G_SLIST (store->root)->next = tmp;
1918 else if (!a && before)
1920 G_SLIST (store->tail)->next = G_SLIST (iter->user_data);
1921 G_SLIST (iter->user_data)->next = NULL;
1924 /* update tail if needed */
1925 if (!G_SLIST (iter->user_data)->next)
1926 store->tail = G_SLIST (iter->user_data);
1930 new_pos = gtk_tree_path_get_indices (pos_path)[0];
1932 new_pos = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL) - 1;
1936 if (new_pos > old_pos)
1938 if (before && position)
1943 if (!before && position)
1947 order = g_new (gint, store->length);
1948 if (new_pos > old_pos)
1950 for (j = 0; j < store->length; j++)
1953 else if (j >= old_pos && j < new_pos)
1955 else if (j == new_pos)
1962 for (j = 0; j < store->length; j++)
1965 else if (j > new_pos && j <= old_pos)
1971 path = gtk_tree_path_new ();
1972 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1974 gtk_tree_path_free (path);
1976 gtk_tree_path_free (pos_path);
1983 gtk_tree_path_free (path);
1985 gtk_tree_path_free (pos_path);
1989 * gtk_list_store_move_before:
1990 * @store: A #GtkListStore.
1991 * @iter: A #GtkTreeIter.
1992 * @position: A #GtkTreeIter, or %NULL.
1994 * Moves @iter in @store to the position before @position. Note that this
1995 * function only works with unsorted stores. If @position is %NULL, @iter
1996 * will be moved to the end of the list.
2001 gtk_list_store_move_before (GtkListStore *store,
2003 GtkTreeIter *position)
2005 gtk_list_store_move (store, iter, position, TRUE);
2009 * gtk_list_store_move_after:
2010 * @store: A #GtkListStore.
2011 * @iter: A #GtkTreeIter.
2012 * @position: A #GtkTreeIter or %NULL.
2014 * Moves @iter in @store to the position after @position. Note that this
2015 * function only works with unsorted stores. If @position is %NULL, @iter
2016 * will be moved to the start of the list.
2021 gtk_list_store_move_after (GtkListStore *store,
2023 GtkTreeIter *position)
2025 gtk_list_store_move (store, iter, position, FALSE);
2030 gtk_list_store_compare_func (gconstpointer a,
2034 GtkListStore *list_store = user_data;
2035 GSList *el_a; /* Los Angeles? */
2040 GtkTreeIterCompareFunc func;
2044 if (list_store->sort_column_id != -1)
2046 GtkTreeDataSortHeader *header;
2048 header = _gtk_tree_data_list_get_header (list_store->sort_list,
2049 list_store->sort_column_id);
2050 g_return_val_if_fail (header != NULL, 0);
2051 g_return_val_if_fail (header->func != NULL, 0);
2053 func = header->func;
2054 data = header->data;
2058 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
2059 func = list_store->default_sort_func;
2060 data = list_store->default_sort_data;
2063 el_a = ((SortTuple *) a)->el;
2064 el_b = ((SortTuple *) b)->el;
2066 iter_a.stamp = list_store->stamp;
2067 iter_a.user_data = el_a;
2068 iter_b.stamp = list_store->stamp;
2069 iter_b.user_data = el_b;
2071 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
2073 if (list_store->order == GTK_SORT_DESCENDING)
2077 else if (retval < 0)
2084 gtk_list_store_sort (GtkListStore *list_store)
2092 if (list_store->length <= 1)
2095 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
2097 list = G_SLIST (list_store->root);
2099 sort_array = g_array_sized_new (FALSE, FALSE,
2101 list_store->length);
2103 for (i = 0; i < list_store->length; i++)
2105 SortTuple tuple = {0,};
2107 /* If this fails, we are in an inconsistent state. Bad */
2108 g_return_if_fail (list != NULL);
2112 g_array_append_val (sort_array, tuple);
2117 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
2119 for (i = 0; i < list_store->length - 1; i++)
2120 g_array_index (sort_array, SortTuple, i).el->next =
2121 g_array_index (sort_array, SortTuple, i + 1).el;
2122 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
2123 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
2124 list_store->tail = g_array_index (sort_array, SortTuple, list_store->length - 1).el;
2126 /* Let the world know about our new order */
2127 new_order = g_new (gint, list_store->length);
2128 for (i = 0; i < list_store->length; i++)
2129 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
2131 path = gtk_tree_path_new ();
2132 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
2133 path, NULL, new_order);
2134 gtk_tree_path_free (path);
2136 g_array_free (sort_array, TRUE);
2140 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
2145 GSList *prev = NULL;
2146 GSList *next = NULL;
2147 GSList *list = G_SLIST (list_store->root);
2148 GtkTreePath *tmp_path;
2149 GtkTreeIter tmp_iter;
2156 GtkTreeIterCompareFunc func;
2159 if (list_store->length < 2)
2162 tmp_iter.stamp = list_store->stamp;
2164 if (list_store->sort_column_id != -1)
2166 GtkTreeDataSortHeader *header;
2167 header = _gtk_tree_data_list_get_header (list_store->sort_list,
2168 list_store->sort_column_id);
2169 g_return_if_fail (header != NULL);
2170 g_return_if_fail (header->func != NULL);
2171 func = header->func;
2172 data = header->data;
2176 g_return_if_fail (list_store->default_sort_func != NULL);
2177 func = list_store->default_sort_func;
2178 data = list_store->default_sort_data;
2181 /* If it's the built in function, we don't sort. */
2182 if (func == gtk_tree_data_list_compare_func &&
2183 list_store->sort_column_id != column)
2187 /* First we find the iter, its prev, and its next */
2190 if (list == G_SLIST (iter->user_data))
2196 g_assert (list != NULL);
2200 /* Check the common case, where we don't need to sort it moved. */
2203 tmp_iter.user_data = prev;
2204 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2209 tmp_iter.user_data = next;
2210 cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2213 if (list_store->order == GTK_SORT_DESCENDING)
2226 if (prev == NULL && cmp_b <= 0)
2228 else if (next == NULL && cmp_a <= 0)
2230 else if (prev != NULL && next != NULL &&
2231 cmp_a <= 0 && cmp_b <= 0)
2234 /* We actually need to sort it */
2235 /* First, remove the old link. */
2238 list_store->root = next;
2242 list_store->tail = prev;
2245 /* FIXME: as an optimization, we can potentially start at next */
2247 list = G_SLIST (list_store->root);
2249 tmp_iter.user_data = list;
2250 if (list_store->order == GTK_SORT_DESCENDING)
2251 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2253 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2255 while ((list->next) && (cmp_a > 0))
2260 tmp_iter.user_data = list;
2261 if (list_store->order == GTK_SORT_DESCENDING)
2262 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2264 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2267 if ((!list->next) && (cmp_a > 0))
2270 list->next = G_SLIST (iter->user_data);
2271 list_store->tail = list->next;
2275 prev->next = G_SLIST (iter->user_data);
2276 G_SLIST (iter->user_data)->next = list;
2280 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
2281 list_store->root = G_SLIST (iter->user_data);
2284 /* Emit the reordered signal. */
2285 new_order = g_new (int, list_store->length);
2286 if (old_location < new_location)
2287 for (i = 0; i < list_store->length; i++)
2289 if (i < old_location ||
2292 else if (i >= old_location &&
2294 new_order[i] = i + 1;
2295 else if (i == new_location)
2296 new_order[i] = old_location;
2299 for (i = 0; i < list_store->length; i++)
2301 if (i < new_location ||
2304 else if (i > new_location &&
2306 new_order[i] = i - 1;
2307 else if (i == new_location)
2308 new_order[i] = old_location;
2311 tmp_path = gtk_tree_path_new ();
2312 tmp_iter.user_data = NULL;
2314 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
2317 gtk_tree_path_free (tmp_path);
2322 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
2323 gint *sort_column_id,
2326 GtkListStore *list_store = (GtkListStore *) sortable;
2328 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
2330 if (list_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2334 * sort_column_id = list_store->sort_column_id;
2336 * order = list_store->order;
2341 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
2342 gint sort_column_id,
2345 GtkListStore *list_store = (GtkListStore *) sortable;
2347 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2349 if ((list_store->sort_column_id == sort_column_id) &&
2350 (list_store->order == order))
2353 if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2355 GtkTreeDataSortHeader *header = NULL;
2357 header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
2359 /* We want to make sure that we have a function */
2360 g_return_if_fail (header != NULL);
2361 g_return_if_fail (header->func != NULL);
2365 g_return_if_fail (list_store->default_sort_func != NULL);
2369 list_store->sort_column_id = sort_column_id;
2370 list_store->order = order;
2372 gtk_tree_sortable_sort_column_changed (sortable);
2374 gtk_list_store_sort (list_store);
2378 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
2379 gint sort_column_id,
2380 GtkTreeIterCompareFunc func,
2382 GtkDestroyNotify destroy)
2384 GtkListStore *list_store = (GtkListStore *) sortable;
2385 GtkTreeDataSortHeader *header = NULL;
2388 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2389 g_return_if_fail (func != NULL);
2391 for (list = list_store->sort_list; list; list = list->next)
2393 GtkTreeDataSortHeader *list_header;
2395 list_header = (GtkTreeDataSortHeader*) list->data;
2396 if (list_header->sort_column_id == sort_column_id)
2398 header = list_header;
2405 header = g_new0 (GtkTreeDataSortHeader, 1);
2406 header->sort_column_id = sort_column_id;
2407 list_store->sort_list = g_list_append (list_store->sort_list, header);
2410 if (header->destroy)
2412 GtkDestroyNotify d = header->destroy;
2414 header->destroy = NULL;
2418 header->func = func;
2419 header->data = data;
2420 header->destroy = destroy;
2422 if (list_store->sort_column_id == sort_column_id)
2423 gtk_list_store_sort (list_store);
2427 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
2428 GtkTreeIterCompareFunc func,
2430 GtkDestroyNotify destroy)
2432 GtkListStore *list_store = (GtkListStore *) sortable;
2434 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2436 if (list_store->default_sort_destroy)
2438 GtkDestroyNotify d = list_store->default_sort_destroy;
2440 list_store->default_sort_destroy = NULL;
2441 d (list_store->default_sort_data);
2444 list_store->default_sort_func = func;
2445 list_store->default_sort_data = data;
2446 list_store->default_sort_destroy = destroy;
2448 if (list_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2449 gtk_list_store_sort (list_store);
2453 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
2455 GtkListStore *list_store = (GtkListStore *) sortable;
2457 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
2459 return (list_store->default_sort_func != NULL);