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 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
79 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
81 GtkSelectionData *selection_data);
82 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
84 GtkSelectionData *selection_data);
85 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
86 GtkTreePath *dest_path,
87 GtkSelectionData *selection_data);
91 static void gtk_list_store_sort (GtkListStore *list_store);
92 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
95 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
98 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
101 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
103 GtkTreeIterCompareFunc func,
105 GtkDestroyNotify destroy);
106 static void gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
107 GtkTreeIterCompareFunc func,
109 GtkDestroyNotify destroy);
110 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable);
112 static void gtk_list_store_move (GtkListStore *store,
118 static GObjectClass *parent_class = NULL;
122 validate_list_store (GtkListStore *list_store)
124 if (gtk_debug_flags & GTK_DEBUG_TREE)
126 g_assert (g_slist_length (list_store->root) == list_store->length);
128 g_assert (g_slist_last (list_store->root) == list_store->tail);
133 gtk_list_store_get_type (void)
135 static GType list_store_type = 0;
137 if (!list_store_type)
139 static const GTypeInfo list_store_info =
141 sizeof (GtkListStoreClass),
142 NULL, /* base_init */
143 NULL, /* base_finalize */
144 (GClassInitFunc) gtk_list_store_class_init,
145 NULL, /* class_finalize */
146 NULL, /* class_data */
147 sizeof (GtkListStore),
149 (GInstanceInitFunc) gtk_list_store_init,
152 static const GInterfaceInfo tree_model_info =
154 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
159 static const GInterfaceInfo drag_source_info =
161 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
166 static const GInterfaceInfo drag_dest_info =
168 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
173 static const GInterfaceInfo sortable_info =
175 (GInterfaceInitFunc) gtk_list_store_sortable_init,
180 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore",
181 &list_store_info, 0);
183 g_type_add_interface_static (list_store_type,
186 g_type_add_interface_static (list_store_type,
187 GTK_TYPE_TREE_DRAG_SOURCE,
189 g_type_add_interface_static (list_store_type,
190 GTK_TYPE_TREE_DRAG_DEST,
192 g_type_add_interface_static (list_store_type,
193 GTK_TYPE_TREE_SORTABLE,
197 return list_store_type;
201 gtk_list_store_class_init (GtkListStoreClass *class)
203 GObjectClass *object_class;
205 parent_class = g_type_class_peek_parent (class);
206 object_class = (GObjectClass*) class;
208 object_class->finalize = gtk_list_store_finalize;
212 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
214 iface->get_flags = gtk_list_store_get_flags;
215 iface->get_n_columns = gtk_list_store_get_n_columns;
216 iface->get_column_type = gtk_list_store_get_column_type;
217 iface->get_iter = gtk_list_store_get_iter;
218 iface->get_path = gtk_list_store_get_path;
219 iface->get_value = gtk_list_store_get_value;
220 iface->iter_next = gtk_list_store_iter_next;
221 iface->iter_children = gtk_list_store_iter_children;
222 iface->iter_has_child = gtk_list_store_iter_has_child;
223 iface->iter_n_children = gtk_list_store_iter_n_children;
224 iface->iter_nth_child = gtk_list_store_iter_nth_child;
225 iface->iter_parent = gtk_list_store_iter_parent;
229 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
231 iface->drag_data_delete = gtk_list_store_drag_data_delete;
232 iface->drag_data_get = gtk_list_store_drag_data_get;
236 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
238 iface->drag_data_received = gtk_list_store_drag_data_received;
239 iface->row_drop_possible = gtk_list_store_row_drop_possible;
243 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
245 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
246 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
247 iface->set_sort_func = gtk_list_store_set_sort_func;
248 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
249 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
253 gtk_list_store_init (GtkListStore *list_store)
255 list_store->root = NULL;
256 list_store->tail = NULL;
257 list_store->sort_list = NULL;
258 list_store->stamp = g_random_int ();
259 list_store->length = 0;
260 list_store->sort_column_id = -2;
261 list_store->columns_dirty = FALSE;
265 * gtk_list_store_new:
266 * @n_columns: number of columns in the list store
267 * @Varargs: all #GType types for the columns, from first to last
269 * Creates a new list store as with @n_columns columns each of the types passed
270 * in. As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
271 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
272 * int, string and #GdkPixbuf respectively.
274 * Return value: a new #GtkListStore
277 gtk_list_store_new (gint n_columns,
280 GtkListStore *retval;
284 g_return_val_if_fail (n_columns > 0, NULL);
286 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
287 gtk_list_store_set_n_columns (retval, n_columns);
289 va_start (args, n_columns);
291 for (i = 0; i < n_columns; i++)
293 GType type = va_arg (args, GType);
294 if (! _gtk_tree_data_list_check_type (type))
296 g_warning ("%s: Invalid type %s passed to gtk_list_store_new\n",
297 G_STRLOC, g_type_name (type));
298 g_object_unref (retval);
302 gtk_list_store_set_column_type (retval, i, type);
312 * gtk_list_store_newv:
313 * @n_columns: number of columns in the list store
314 * @types: an array of #GType types for the columns, from first to last
316 * Non-vararg creation function. Used primarily by language bindings.
318 * Return value: a new #GtkListStore
321 gtk_list_store_newv (gint n_columns,
324 GtkListStore *retval;
327 g_return_val_if_fail (n_columns > 0, NULL);
329 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
330 gtk_list_store_set_n_columns (retval, n_columns);
332 for (i = 0; i < n_columns; i++)
334 if (! _gtk_tree_data_list_check_type (types[i]))
336 g_warning ("%s: Invalid type %s passed to gtk_list_store_newv\n",
337 G_STRLOC, g_type_name (types[i]));
338 g_object_unref (retval);
342 gtk_list_store_set_column_type (retval, i, types[i]);
349 * gtk_list_store_set_column_types:
350 * @list_store: A #GtkListStore
351 * @n_columns: Number of columns for the list store
352 * @types: An array length n of #GTypes
354 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
355 * and should only be used when constructing a new #GtkListStore. It will not
356 * function after a row has been added, or a method on the #GtkTreeModel
357 * interface is called.
360 gtk_list_store_set_column_types (GtkListStore *list_store,
366 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
367 g_return_if_fail (list_store->columns_dirty == 0);
369 gtk_list_store_set_n_columns (list_store, n_columns);
370 for (i = 0; i < n_columns; i++)
372 if (! _gtk_tree_data_list_check_type (types[i]))
374 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_types\n", G_STRLOC, g_type_name (types[i]));
377 gtk_list_store_set_column_type (list_store, i, types[i]);
382 gtk_list_store_set_n_columns (GtkListStore *list_store,
387 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
388 g_return_if_fail (n_columns > 0);
390 if (list_store->n_columns == n_columns)
393 new_columns = g_new0 (GType, n_columns);
394 if (list_store->column_headers)
396 /* copy the old header orders over */
397 if (n_columns >= list_store->n_columns)
398 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
400 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
402 g_free (list_store->column_headers);
405 if (list_store->sort_list)
406 _gtk_tree_data_list_header_free (list_store->sort_list);
408 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
410 list_store->column_headers = new_columns;
411 list_store->n_columns = n_columns;
415 gtk_list_store_set_column_type (GtkListStore *list_store,
419 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
420 g_return_if_fail (column >=0 && column < list_store->n_columns);
422 if (!_gtk_tree_data_list_check_type (type))
424 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_type\n", G_STRLOC, g_type_name (type));
428 list_store->column_headers[column] = type;
432 gtk_list_store_finalize (GObject *object)
434 GtkListStore *list_store = GTK_LIST_STORE (object);
436 g_slist_foreach (list_store->root, (GFunc) _gtk_tree_data_list_free, list_store->column_headers);
437 g_slist_free (list_store->root);
439 _gtk_tree_data_list_header_free (list_store->sort_list);
440 g_free (list_store->column_headers);
442 if (list_store->default_sort_destroy)
444 GtkDestroyNotify d = list_store->default_sort_destroy;
446 list_store->default_sort_destroy = NULL;
447 d (list_store->default_sort_data);
448 list_store->default_sort_data = NULL;
452 (* parent_class->finalize) (object);
455 /* Fulfill the GtkTreeModel requirements */
456 static GtkTreeModelFlags
457 gtk_list_store_get_flags (GtkTreeModel *tree_model)
459 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
461 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
465 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
467 GtkListStore *list_store = (GtkListStore *) tree_model;
469 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
471 list_store->columns_dirty = TRUE;
473 return list_store->n_columns;
477 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
480 GtkListStore *list_store = (GtkListStore *) tree_model;
482 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
483 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
484 index >= 0, G_TYPE_INVALID);
486 list_store->columns_dirty = TRUE;
488 return list_store->column_headers[index];
492 gtk_list_store_get_iter (GtkTreeModel *tree_model,
496 GtkListStore *list_store = (GtkListStore *) tree_model;
500 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
501 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
503 list_store->columns_dirty = TRUE;
505 i = gtk_tree_path_get_indices (path)[0];
507 if (i >= list_store->length)
510 list = g_slist_nth (G_SLIST (list_store->root), i);
512 /* If this fails, list_store->length has gotten mangled. */
515 iter->stamp = list_store->stamp;
516 iter->user_data = list;
522 gtk_list_store_get_path (GtkTreeModel *tree_model,
529 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
530 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
531 if (G_SLIST (iter->user_data) == G_SLIST (GTK_LIST_STORE (tree_model)->tail))
533 retval = gtk_tree_path_new ();
534 gtk_tree_path_append_index (retval, GTK_LIST_STORE (tree_model)->length - 1);
538 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
540 if (list == G_SLIST (iter->user_data))
547 retval = gtk_tree_path_new ();
548 gtk_tree_path_append_index (retval, i);
553 gtk_list_store_get_value (GtkTreeModel *tree_model,
558 GtkTreeDataList *list;
559 gint tmp_column = column;
561 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
562 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
563 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
565 list = G_SLIST (iter->user_data)->data;
567 while (tmp_column-- > 0 && list)
571 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
573 _gtk_tree_data_list_node_to_value (list,
574 GTK_LIST_STORE (tree_model)->column_headers[column],
579 gtk_list_store_iter_next (GtkTreeModel *tree_model,
582 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
583 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
585 iter->user_data = G_SLIST (iter->user_data)->next;
587 return (iter->user_data != NULL);
591 gtk_list_store_iter_children (GtkTreeModel *tree_model,
595 /* this is a list, nodes have no children */
599 /* but if parent == NULL we return the list itself as children of the
603 if (GTK_LIST_STORE (tree_model)->root)
605 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
606 iter->user_data = GTK_LIST_STORE (tree_model)->root;
614 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
621 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
624 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
626 return GTK_LIST_STORE (tree_model)->length;
628 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
633 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
640 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
645 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
649 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
650 iter->user_data = child;
658 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
666 gtk_list_store_real_set_value (GtkListStore *list_store,
672 GtkTreeDataList *list;
673 GtkTreeDataList *prev;
674 gint old_column = column;
675 GValue real_value = {0, };
676 gboolean converted = FALSE;
677 gboolean retval = FALSE;
679 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
680 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
681 g_return_val_if_fail (column >= 0 && column < list_store->n_columns, FALSE);
682 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
684 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
686 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
687 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
689 g_warning ("%s: Unable to convert from %s to %s\n",
691 g_type_name (G_VALUE_TYPE (value)),
692 g_type_name (list_store->column_headers[column]));
695 if (!g_value_transform (value, &real_value))
697 g_warning ("%s: Unable to make conversion from %s to %s\n",
699 g_type_name (G_VALUE_TYPE (value)),
700 g_type_name (list_store->column_headers[column]));
701 g_value_unset (&real_value);
707 prev = list = G_SLIST (iter->user_data)->data;
714 _gtk_tree_data_list_value_to_node (list, &real_value);
716 _gtk_tree_data_list_value_to_node (list, value);
719 g_value_unset (&real_value);
720 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
721 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
730 if (G_SLIST (iter->user_data)->data == NULL)
732 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
737 list = prev->next = _gtk_tree_data_list_alloc ();
743 list->next = _gtk_tree_data_list_alloc ();
750 _gtk_tree_data_list_value_to_node (list, &real_value);
752 _gtk_tree_data_list_value_to_node (list, value);
756 g_value_unset (&real_value);
758 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
759 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
766 * gtk_list_store_set_value:
767 * @list_store: A #GtkListStore
768 * @iter: A valid #GtkTreeIter for the row being modified
769 * @column: column number to modify
770 * @value: new value for the cell
772 * Sets the data in the cell specified by @iter and @column.
773 * The type of @value must be convertible to the type of the
778 gtk_list_store_set_value (GtkListStore *list_store,
783 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
784 g_return_if_fail (VALID_ITER (iter, list_store));
785 g_return_if_fail (column >= 0 && column < list_store->n_columns);
786 g_return_if_fail (G_IS_VALUE (value));
788 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
792 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
793 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
794 gtk_tree_path_free (path);
799 * gtk_list_store_set_valist:
800 * @list_store: A #GtkListStore
801 * @iter: A valid #GtkTreeIter for the row being modified
802 * @var_args: va_list of column/value pairs
804 * See gtk_list_store_set(); this version takes a va_list for use by language
809 gtk_list_store_set_valist (GtkListStore *list_store,
814 gboolean emit_signal = FALSE;
815 gboolean maybe_need_sort = FALSE;
816 GtkTreeIterCompareFunc func = NULL;
818 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
819 g_return_if_fail (VALID_ITER (iter, list_store));
821 column = va_arg (var_args, gint);
823 if (GTK_LIST_STORE_IS_SORTED (list_store))
825 if (list_store->sort_column_id != -1)
827 GtkTreeDataSortHeader *header;
828 header = _gtk_tree_data_list_get_header (list_store->sort_list,
829 list_store->sort_column_id);
830 g_return_if_fail (header != NULL);
831 g_return_if_fail (header->func != NULL);
836 func = list_store->default_sort_func;
840 if (func != gtk_tree_data_list_compare_func)
841 maybe_need_sort = TRUE;
845 GValue value = { 0, };
848 if (column >= list_store->n_columns)
850 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
853 g_value_init (&value, list_store->column_headers[column]);
855 G_VALUE_COLLECT (&value, var_args, 0, &error);
858 g_warning ("%s: %s", G_STRLOC, error);
861 /* we purposely leak the value here, it might not be
862 * in a sane state if an error condition occoured
867 /* FIXME: instead of calling this n times, refactor with above */
868 emit_signal = gtk_list_store_real_set_value (list_store,
872 FALSE) || emit_signal;
874 if (func == gtk_tree_data_list_compare_func &&
875 column == list_store->sort_column_id)
876 maybe_need_sort = TRUE;
878 g_value_unset (&value);
880 column = va_arg (var_args, gint);
883 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
884 gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
890 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
891 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
892 gtk_tree_path_free (path);
897 * gtk_list_store_set:
898 * @list_store: a #GtkListStore
899 * @iter: row iterator
900 * @Varargs: pairs of column number and value, terminated with -1
902 * Sets the value of one or more cells in the row referenced by @iter.
903 * The variable argument list should contain integer column numbers,
904 * each column number followed by the value to be set.
905 * The list is terminated by a -1. For example, to set column 0 with type
906 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
907 * 0, "Foo", -1)</literal>.
910 gtk_list_store_set (GtkListStore *list_store,
916 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
917 g_return_if_fail (iter != NULL);
918 g_return_if_fail (iter->stamp == list_store->stamp);
920 va_start (var_args, iter);
921 gtk_list_store_set_valist (list_store, iter, var_args);
926 remove_link_saving_prev (GSList *list,
941 prev->next = link->next;
960 gtk_list_store_remove_silently (GtkListStore *list_store,
964 if (G_SLIST (iter->user_data)->data)
966 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
967 list_store->column_headers);
968 G_SLIST (iter->user_data)->data = NULL;
974 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
975 G_SLIST (iter->user_data),
978 list_store->length -= 1;
980 if (iter->user_data == list_store->tail)
981 list_store->tail = prev;
983 g_slist_free (G_SLIST (iter->user_data));
988 * gtk_list_store_remove:
989 * @list_store: A #GtkListStore
990 * @iter: A valid #GtkTreeIter
992 * Removes the given row from the list store. After being removed,
993 * @iter is set to be the next valid row, or invalidated if it pointed
994 * to the last row in @list_store.
996 * Return value: %TRUE if @iter is valid, %FALSE if not.
999 gtk_list_store_remove (GtkListStore *list_store,
1005 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1006 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
1008 next = G_SLIST (iter->user_data)->next;
1009 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1011 validate_list_store (list_store);
1013 gtk_list_store_remove_silently (list_store, iter, path);
1015 validate_list_store (list_store);
1017 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
1018 gtk_tree_path_free (path);
1022 iter->stamp = list_store->stamp;
1023 iter->user_data = next;
1035 insert_after (GtkListStore *list_store,
1039 g_return_if_fail (sibling != NULL);
1040 g_return_if_fail (new_list != NULL);
1042 /* insert new node after list */
1043 new_list->next = sibling->next;
1044 sibling->next = new_list;
1046 /* if list was the tail, the new node is the new tail */
1047 if (sibling == ((GSList *) list_store->tail))
1048 list_store->tail = new_list;
1050 list_store->length += 1;
1054 * gtk_list_store_insert:
1055 * @list_store: A #GtkListStore
1056 * @iter: An unset #GtkTreeIter to set to the new row
1057 * @position: position to insert the new row
1059 * Creates a new row at @position. @iter will be changed to point to this new
1060 * row. If @position is larger than the number of rows on the list, then the
1061 * new row will be appended to the list. The row will be empty before this
1062 * function is called. To fill in values, you need to call gtk_list_store_set()
1063 * or gtk_list_store_set_value().
1067 gtk_list_store_insert (GtkListStore *list_store,
1075 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1076 g_return_if_fail (iter != NULL);
1077 g_return_if_fail (position >= 0);
1079 list_store->columns_dirty = TRUE;
1081 if (position == 0 ||
1082 GTK_LIST_STORE_IS_SORTED (list_store))
1084 gtk_list_store_prepend (list_store, iter);
1088 new_list = g_slist_alloc ();
1090 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
1094 /* position if off the end of the list, append it */
1095 gtk_list_store_append (list_store, iter);
1100 insert_after (list_store, list, new_list);
1102 iter->stamp = list_store->stamp;
1103 iter->user_data = new_list;
1105 validate_list_store (list_store);
1107 path = gtk_tree_path_new ();
1108 gtk_tree_path_append_index (path, position);
1109 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1110 gtk_tree_path_free (path);
1114 * gtk_list_store_insert_before:
1115 * @list_store: A #GtkListStore
1116 * @iter: An unset #GtkTreeIter to set to the new row
1117 * @sibling: A valid #GtkTreeIter, or %NULL
1119 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
1120 * appended to the end of the list. @iter will be changed to point to this new
1121 * row. The row will be empty before this function is called. To fill in values,
1122 * you need to call gtk_list_store_set() or gtk_list_store_set_value().
1126 gtk_list_store_insert_before (GtkListStore *list_store,
1128 GtkTreeIter *sibling)
1131 GSList *list, *prev, *new_list;
1134 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1135 g_return_if_fail (iter != NULL);
1137 g_return_if_fail (VALID_ITER (sibling, list_store));
1139 list_store->columns_dirty = TRUE;
1141 if (GTK_LIST_STORE_IS_SORTED (list_store))
1143 gtk_list_store_prepend (list_store, iter);
1147 if (sibling == NULL)
1149 gtk_list_store_append (list_store, iter);
1153 new_list = g_slist_alloc ();
1156 list = list_store->root;
1157 while (list && list != sibling->user_data)
1164 if (list != sibling->user_data)
1166 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1170 /* if there are no nodes, we become the list tail, otherwise we
1171 * are inserting before any existing nodes so we can't change
1175 if (list_store->root == NULL)
1176 list_store->tail = new_list;
1180 new_list->next = prev->next;
1181 prev->next = new_list;
1185 new_list->next = list_store->root;
1186 list_store->root = new_list;
1189 iter->stamp = list_store->stamp;
1190 iter->user_data = new_list;
1192 list_store->length += 1;
1194 validate_list_store (list_store);
1196 path = gtk_tree_path_new ();
1197 gtk_tree_path_append_index (path, i);
1198 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1199 gtk_tree_path_free (path);
1203 * gtk_list_store_insert_after:
1204 * @list_store: A #GtkListStore
1205 * @iter: An unset #GtkTreeIter to set to the new row
1206 * @sibling: A valid #GtkTreeIter, or %NULL
1208 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1209 * prepended to the beginning of the list. @iter will be changed to point to
1210 * this new row. The row will be empty after this function is called. To fill
1211 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1215 gtk_list_store_insert_after (GtkListStore *list_store,
1217 GtkTreeIter *sibling)
1220 GSList *list, *new_list;
1223 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1224 g_return_if_fail (iter != NULL);
1226 g_return_if_fail (VALID_ITER (sibling, list_store));
1228 list_store->columns_dirty = TRUE;
1230 if (sibling == NULL ||
1231 GTK_LIST_STORE_IS_SORTED (list_store))
1233 gtk_list_store_prepend (list_store, iter);
1237 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1240 g_return_if_fail (list == sibling->user_data);
1242 new_list = g_slist_alloc ();
1244 insert_after (list_store, list, new_list);
1246 iter->stamp = list_store->stamp;
1247 iter->user_data = new_list;
1249 validate_list_store (list_store);
1251 path = gtk_tree_path_new ();
1252 gtk_tree_path_append_index (path, i + 1);
1253 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1254 gtk_tree_path_free (path);
1258 * gtk_list_store_prepend:
1259 * @list_store: A #GtkListStore
1260 * @iter: An unset #GtkTreeIter to set to the prepend row
1262 * Prepends a new row to @list_store. @iter will be changed to point to this new
1263 * row. The row will be empty after this function is called. To fill in
1264 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1268 gtk_list_store_prepend (GtkListStore *list_store,
1273 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1274 g_return_if_fail (iter != NULL);
1276 iter->stamp = list_store->stamp;
1277 iter->user_data = g_slist_alloc ();
1279 list_store->columns_dirty = TRUE;
1281 if (list_store->root == NULL)
1282 list_store->tail = iter->user_data;
1284 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1285 list_store->root = iter->user_data;
1287 list_store->length += 1;
1289 validate_list_store (list_store);
1291 path = gtk_tree_path_new ();
1292 gtk_tree_path_append_index (path, 0);
1293 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1294 gtk_tree_path_free (path);
1298 * gtk_list_store_append:
1299 * @list_store: A #GtkListStore
1300 * @iter: An unset #GtkTreeIter to set to the appended row
1302 * Appends a new row to @list_store. @iter will be changed to point to this new
1303 * row. The row will be empty after this function is called. To fill in
1304 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1308 gtk_list_store_append (GtkListStore *list_store,
1313 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1314 g_return_if_fail (iter != NULL);
1316 list_store->columns_dirty = TRUE;
1318 if (GTK_LIST_STORE_IS_SORTED (list_store))
1320 gtk_list_store_prepend (list_store, iter);
1324 iter->stamp = list_store->stamp;
1325 iter->user_data = g_slist_alloc ();
1327 if (list_store->tail)
1328 ((GSList *)list_store->tail)->next = iter->user_data;
1330 list_store->root = iter->user_data;
1332 list_store->tail = iter->user_data;
1334 list_store->length += 1;
1336 validate_list_store (list_store);
1338 path = gtk_tree_path_new ();
1339 gtk_tree_path_append_index (path, list_store->length - 1);
1340 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1341 gtk_tree_path_free (path);
1345 * gtk_list_store_clear:
1346 * @list_store: a #GtkListStore.
1348 * Removes all rows from the list store.
1352 gtk_list_store_clear (GtkListStore *list_store)
1355 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1357 while (list_store->root)
1359 iter.stamp = list_store->stamp;
1360 iter.user_data = list_store->root;
1361 gtk_list_store_remove (list_store, &iter);
1366 * gtk_list_store_iter_is_valid:
1367 * @list_store: A #GtkListStore.
1368 * @iter: A #GtkTreeIter.
1370 * WARNING: This function is slow. Only use it for debugging and/or testing
1373 * Checks if the given iter is a valid iter for this #GtkListStore.
1375 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1378 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1383 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1384 g_return_val_if_fail (iter != NULL, FALSE);
1386 if (!VALID_ITER (iter, list_store))
1389 if (iter->user_data == list_store->root)
1391 if (iter->user_data == list_store->tail)
1394 for (list = ((GList *)list_store->root)->next; list; list = list->next)
1395 if (list == iter->user_data)
1402 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1406 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1408 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1412 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1419 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1421 GtkSelectionData *selection_data)
1423 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1425 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1426 * target, because the default handler does it for us, but
1427 * we do anyway for the convenience of someone maybe overriding the
1431 if (gtk_tree_set_row_drag_data (selection_data,
1432 GTK_TREE_MODEL (drag_source),
1439 /* FIXME handle text targets at least. */
1446 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1448 GtkSelectionData *selection_data)
1450 GtkTreeModel *tree_model;
1451 GtkListStore *list_store;
1452 GtkTreeModel *src_model = NULL;
1453 GtkTreePath *src_path = NULL;
1454 gboolean retval = FALSE;
1456 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1458 tree_model = GTK_TREE_MODEL (drag_dest);
1459 list_store = GTK_LIST_STORE (drag_dest);
1461 if (gtk_tree_get_row_drag_data (selection_data,
1464 src_model == tree_model)
1466 /* Copy the given row to a new position */
1467 GtkTreeIter src_iter;
1468 GtkTreeIter dest_iter;
1471 if (!gtk_tree_model_get_iter (src_model,
1478 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1479 prev = gtk_tree_path_copy (dest);
1481 if (!gtk_tree_path_prev (prev))
1483 /* dest was the first spot in the list; which means we are supposed
1486 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1493 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1497 GtkTreeIter tmp_iter = dest_iter;
1499 if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_model), "gtk-tree-model-drop-append")))
1500 gtk_list_store_append (GTK_LIST_STORE (tree_model), &dest_iter);
1502 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1503 &dest_iter, &tmp_iter);
1509 g_object_set_data (G_OBJECT (tree_model), "gtk-tree-model-drop-append",
1512 gtk_tree_path_free (prev);
1514 /* If we succeeded in creating dest_iter, copy data from src
1518 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1519 GtkTreeDataList *copy_head = NULL;
1520 GtkTreeDataList *copy_prev = NULL;
1521 GtkTreeDataList *copy_iter = NULL;
1528 copy_iter = _gtk_tree_data_list_node_copy (dl,
1529 list_store->column_headers[col]);
1531 if (copy_head == NULL)
1532 copy_head = copy_iter;
1535 copy_prev->next = copy_iter;
1537 copy_prev = copy_iter;
1543 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1544 G_SLIST (dest_iter.user_data)->data = copy_head;
1546 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1547 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter);
1548 gtk_tree_path_free (path);
1553 /* FIXME maybe add some data targets eventually, or handle text
1554 * targets in the simple case.
1561 gtk_tree_path_free (src_path);
1567 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1568 GtkTreePath *dest_path,
1569 GtkSelectionData *selection_data)
1572 GtkTreeModel *src_model = NULL;
1573 GtkTreePath *src_path = NULL;
1574 gboolean retval = FALSE;
1576 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1578 /* don't accept drops if the list has been sorted */
1579 if (GTK_LIST_STORE_IS_SORTED (drag_dest))
1582 if (!gtk_tree_get_row_drag_data (selection_data,
1587 if (src_model != GTK_TREE_MODEL (drag_dest))
1590 if (gtk_tree_path_get_depth (dest_path) != 1)
1593 /* can drop before any existing node, or before one past any existing. */
1595 indices = gtk_tree_path_get_indices (dest_path);
1597 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1601 gtk_tree_path_free (src_path);
1606 /* Sorting and reordering */
1607 typedef struct _SortTuple
1615 gtk_list_store_reorder_func (gconstpointer a,
1619 SortTuple *a_reorder;
1620 SortTuple *b_reorder;
1622 a_reorder = (SortTuple *)a;
1623 b_reorder = (SortTuple *)b;
1625 if (a_reorder->offset < b_reorder->offset)
1627 if (a_reorder->offset > b_reorder->offset)
1634 * gtk_list_store_reorder:
1635 * @store: A #GtkTreeStore.
1636 * @new_order: An integer array indicating the new order for the list.
1638 * Reorders @store to follow the order indicated by @new_order. Note that
1639 * this function only works with unsorted stores.
1642 gtk_list_store_reorder (GtkListStore *store,
1646 GSList *current_list;
1648 SortTuple *sort_array;
1650 g_return_if_fail (GTK_IS_LIST_STORE (store));
1651 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1652 g_return_if_fail (new_order != NULL);
1654 sort_array = g_new (SortTuple, store->length);
1656 current_list = store->root;
1658 for (i = 0; i < store->length; i++)
1660 sort_array[i].offset = new_order[i];
1661 sort_array[i].el = current_list;
1663 current_list = current_list->next;
1666 g_qsort_with_data (sort_array,
1669 gtk_list_store_reorder_func,
1672 for (i = 0; i < store->length - 1; i++)
1673 G_SLIST (sort_array[i].el)->next = G_SLIST (sort_array[i+1].el);
1675 store->root = G_SLIST (sort_array[0].el);
1676 store->tail = G_SLIST (sort_array[store->length-1].el);
1677 G_SLIST (store->tail)->next = NULL;
1680 path = gtk_tree_path_new ();
1681 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1682 path, NULL, new_order);
1683 gtk_tree_path_free (path);
1684 g_free (sort_array);
1688 * gtk_list_store_swap:
1689 * @store: A #GtkListStore.
1690 * @a: A #GtkTreeIter.
1691 * @b: Another #GtkTreeIter.
1693 * Swaps @a and @b in @store. Note that this function only works with
1697 gtk_list_store_swap (GtkListStore *store,
1701 GSList *i, *prev_a = NULL, *prev_b = NULL;
1702 gint j, a_count = 0, b_count = 0, *order;
1705 g_return_if_fail (GTK_IS_LIST_STORE (store));
1706 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1707 g_return_if_fail (VALID_ITER (a, store));
1708 g_return_if_fail (VALID_ITER (b, store));
1710 if (a->user_data == b->user_data)
1713 if (a->user_data == store->root)
1717 for (i = store->root; i; i = i->next, a_count++)
1718 if (i->next == a->user_data)
1727 if (b->user_data == store->root)
1731 for (i = store->root; i; i = i->next, b_count++)
1732 if (i->next == b->user_data)
1742 store->root = b->user_data;
1744 prev_a->next = b->user_data;
1747 store->root = a->user_data;
1749 prev_b->next = a->user_data;
1751 /* think a_next inspead of a_prev here ... */
1752 prev_a = G_SLIST (a->user_data)->next;
1753 prev_b = G_SLIST (b->user_data)->next;
1755 G_SLIST (a->user_data)->next = prev_b;
1756 G_SLIST (b->user_data)->next = prev_a;
1759 order = g_new (gint, store->length);
1760 for (j = 0; j < store->length; j++)
1763 else if (j == b_count)
1768 path = gtk_tree_path_new ();
1769 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1771 gtk_tree_path_free (path);
1776 gtk_list_store_move (GtkListStore *store,
1778 GtkTreeIter *position,
1782 GSList *i, *a, *prev = NULL, *tmp;
1783 gint new_pos = 0, old_pos = 0, j = 0, *order;
1784 GtkTreePath *path, *pos_path = NULL;
1786 g_return_if_fail (GTK_IS_LIST_STORE (store));
1787 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1788 g_return_if_fail (VALID_ITER (iter, store));
1790 g_return_if_fail (VALID_ITER (position, store));
1792 /* lots of sanity checks */
1795 path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), iter);
1796 pos_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), position);
1798 if (gtk_tree_path_get_depth (pos_path) != 1)
1799 goto free_paths_and_out;
1802 * moving the iter before path or "path + 1" doesn't make sense
1804 * moving the iter before path or "path - 1" doesn't make sense
1806 if (!gtk_tree_path_compare (path, pos_path))
1807 goto free_paths_and_out;
1810 gtk_tree_path_next (path);
1812 gtk_tree_path_prev (path);
1814 if (!gtk_tree_path_compare (path, pos_path))
1815 goto free_paths_and_out;
1817 gtk_tree_path_free (path);
1820 /* getting destination iters */
1821 if (before && position)
1823 if (gtk_tree_path_get_indices (pos_path)[0] > 0)
1825 gtk_tree_path_prev (pos_path);
1826 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &dst_a, pos_path))
1827 a = G_SLIST (dst_a.user_data);
1830 gtk_tree_path_next (pos_path);
1835 else if (before && !position)
1840 a = G_SLIST (position->user_data);
1845 /* getting the old prev node */
1846 if (iter->user_data == store->root)
1850 for (i = store->root; i; i = i->next, old_pos++)
1851 if (i->next == iter->user_data)
1862 store->root = G_SLIST (iter->user_data)->next;
1864 prev->next = G_SLIST (iter->user_data)->next;
1866 /* and reinsert it */
1871 a->next = G_SLIST (iter->user_data);
1872 a->next->next = tmp;
1874 else if (!a && !before)
1876 tmp = G_SLIST (store->root);
1878 store->root = G_SLIST (iter->user_data);
1879 G_SLIST (store->root)->next = tmp;
1881 else if (!a && before)
1883 G_SLIST (store->tail)->next = G_SLIST (iter->user_data);
1884 G_SLIST (iter->user_data)->next = NULL;
1885 store->tail = iter->user_data;
1890 new_pos = gtk_tree_path_get_indices (pos_path)[0];
1892 new_pos = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL) - 1;
1896 if (new_pos > old_pos)
1907 order = g_new (gint, store->length);
1908 if (new_pos > old_pos)
1910 for (j = 0; j < store->length; j++)
1913 else if (j >= old_pos && j < new_pos)
1915 else if (j == new_pos)
1922 for (j = 0; j < store->length; j++)
1925 else if (j > new_pos && j <= old_pos)
1931 path = gtk_tree_path_new ();
1932 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1934 gtk_tree_path_free (path);
1936 gtk_tree_path_free (pos_path);
1942 gtk_tree_path_free (path);
1943 gtk_tree_path_free (pos_path);
1947 * gtk_list_store_move_before:
1948 * @store: A #GtkListStore.
1949 * @iter: A #GtkTreeIter.
1950 * @position: A #GtkTreeIter, or %NULL.
1952 * Moves @iter in @store to the position before @position. Note that this
1953 * function only works with unsorted stores. If @position is %NULL, @iter
1954 * will be moved to the end of the list.
1957 gtk_list_store_move_before (GtkListStore *store,
1959 GtkTreeIter *position)
1961 gtk_list_store_move (store, iter, position, TRUE);
1965 * gtk_list_store_move_after:
1966 * @store: A #GtkListStore.
1967 * @iter: A #GtkTreeIter.
1968 * @position: A #GtkTreeIter, or %NULL.
1970 * Moves @iter in @store to the position after @position. Note that this
1971 * function only works with unsorted stores. If @position is %NULL, @iter
1972 * will be moved to the start of the list.
1975 gtk_list_store_move_after (GtkListStore *store,
1977 GtkTreeIter *position)
1979 gtk_list_store_move (store, iter, position, FALSE);
1984 gtk_list_store_compare_func (gconstpointer a,
1988 GtkListStore *list_store = user_data;
1989 GSList *el_a; /* Los Angeles? */
1994 GtkTreeIterCompareFunc func;
1998 if (list_store->sort_column_id != -1)
2000 GtkTreeDataSortHeader *header;
2002 header = _gtk_tree_data_list_get_header (list_store->sort_list,
2003 list_store->sort_column_id);
2004 g_return_val_if_fail (header != NULL, 0);
2005 g_return_val_if_fail (header->func != NULL, 0);
2007 func = header->func;
2008 data = header->data;
2012 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
2013 func = list_store->default_sort_func;
2014 data = list_store->default_sort_data;
2017 el_a = ((SortTuple *) a)->el;
2018 el_b = ((SortTuple *) b)->el;
2020 iter_a.stamp = list_store->stamp;
2021 iter_a.user_data = el_a;
2022 iter_b.stamp = list_store->stamp;
2023 iter_b.user_data = el_b;
2025 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
2027 if (list_store->order == GTK_SORT_DESCENDING)
2031 else if (retval < 0)
2038 gtk_list_store_sort (GtkListStore *list_store)
2046 if (list_store->length <= 1)
2049 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
2051 list = G_SLIST (list_store->root);
2053 sort_array = g_array_sized_new (FALSE, FALSE,
2055 list_store->length);
2057 for (i = 0; i < list_store->length; i++)
2061 /* If this fails, we are in an inconsistent state. Bad */
2062 g_return_if_fail (list != NULL);
2066 g_array_append_val (sort_array, tuple);
2071 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
2073 for (i = 0; i < list_store->length - 1; i++)
2074 g_array_index (sort_array, SortTuple, i).el->next =
2075 g_array_index (sort_array, SortTuple, i + 1).el;
2076 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
2077 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
2078 list_store->tail = g_array_index (sort_array, SortTuple, list_store->length - 1).el;
2080 /* Let the world know about our new order */
2081 new_order = g_new (gint, list_store->length);
2082 for (i = 0; i < list_store->length; i++)
2083 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
2085 path = gtk_tree_path_new ();
2086 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
2087 path, NULL, new_order);
2088 gtk_tree_path_free (path);
2090 g_array_free (sort_array, TRUE);
2094 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
2099 GSList *prev = NULL;
2100 GSList *next = NULL;
2101 GSList *list = G_SLIST (list_store->root);
2102 GtkTreePath *tmp_path;
2103 GtkTreeIter tmp_iter;
2110 GtkTreeIterCompareFunc func;
2113 if (list_store->length < 2)
2116 tmp_iter.stamp = list_store->stamp;
2118 if (list_store->sort_column_id != -1)
2120 GtkTreeDataSortHeader *header;
2121 header = _gtk_tree_data_list_get_header (list_store->sort_list,
2122 list_store->sort_column_id);
2123 g_return_if_fail (header != NULL);
2124 g_return_if_fail (header->func != NULL);
2125 func = header->func;
2126 data = header->data;
2130 g_return_if_fail (list_store->default_sort_func != NULL);
2131 func = list_store->default_sort_func;
2132 data = list_store->default_sort_data;
2135 /* If it's the built in function, we don't sort. */
2136 if (func == gtk_tree_data_list_compare_func &&
2137 list_store->sort_column_id != column)
2141 /* First we find the iter, its prev, and its next */
2144 if (list == G_SLIST (iter->user_data))
2150 g_assert (list != NULL);
2154 /* Check the common case, where we don't need to sort it moved. */
2157 tmp_iter.user_data = prev;
2158 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2163 tmp_iter.user_data = next;
2164 cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2167 if (list_store->order == GTK_SORT_DESCENDING)
2180 if (prev == NULL && cmp_b <= 0)
2182 else if (next == NULL && cmp_a <= 0)
2184 else if (prev != NULL && next != NULL &&
2185 cmp_a <= 0 && cmp_b <= 0)
2188 /* We actually need to sort it */
2189 /* First, remove the old link. */
2192 list_store->root = next;
2196 list_store->tail = prev;
2199 /* FIXME: as an optimization, we can potentially start at next */
2201 list = G_SLIST (list_store->root);
2203 tmp_iter.user_data = list;
2204 if (list_store->order == GTK_SORT_DESCENDING)
2205 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2207 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2209 while ((list->next) && (cmp_a > 0))
2214 tmp_iter.user_data = list;
2215 if (list_store->order == GTK_SORT_DESCENDING)
2216 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2218 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2221 if ((!list->next) && (cmp_a > 0))
2224 list->next = G_SLIST (iter->user_data);
2225 list_store->tail = list->next;
2229 prev->next = G_SLIST (iter->user_data);
2230 G_SLIST (iter->user_data)->next = list;
2234 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
2235 list_store->root = G_SLIST (iter->user_data);
2238 /* Emit the reordered signal. */
2239 new_order = g_new (int, list_store->length);
2240 if (old_location < new_location)
2241 for (i = 0; i < list_store->length; i++)
2243 if (i < old_location ||
2246 else if (i >= old_location &&
2248 new_order[i] = i + 1;
2249 else if (i == new_location)
2250 new_order[i] = old_location;
2253 for (i = 0; i < list_store->length; i++)
2255 if (i < new_location ||
2258 else if (i > new_location &&
2260 new_order[i] = i - 1;
2261 else if (i == new_location)
2262 new_order[i] = old_location;
2265 tmp_path = gtk_tree_path_new ();
2266 tmp_iter.user_data = NULL;
2268 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
2271 gtk_tree_path_free (tmp_path);
2276 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
2277 gint *sort_column_id,
2280 GtkListStore *list_store = (GtkListStore *) sortable;
2282 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
2284 if (list_store->sort_column_id == -1)
2288 * sort_column_id = list_store->sort_column_id;
2290 * order = list_store->order;
2295 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
2296 gint sort_column_id,
2299 GtkListStore *list_store = (GtkListStore *) sortable;
2301 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2303 if ((list_store->sort_column_id == sort_column_id) &&
2304 (list_store->order == order))
2307 if (sort_column_id != -1)
2309 GtkTreeDataSortHeader *header = NULL;
2311 header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
2313 /* We want to make sure that we have a function */
2314 g_return_if_fail (header != NULL);
2315 g_return_if_fail (header->func != NULL);
2319 g_return_if_fail (list_store->default_sort_func != NULL);
2323 list_store->sort_column_id = sort_column_id;
2324 list_store->order = order;
2326 gtk_list_store_sort (list_store);
2328 gtk_tree_sortable_sort_column_changed (sortable);
2332 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
2333 gint sort_column_id,
2334 GtkTreeIterCompareFunc func,
2336 GtkDestroyNotify destroy)
2338 GtkListStore *list_store = (GtkListStore *) sortable;
2339 GtkTreeDataSortHeader *header = NULL;
2342 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2343 g_return_if_fail (func != NULL);
2345 for (list = list_store->sort_list; list; list = list->next)
2347 GtkTreeDataSortHeader *list_header;
2349 list_header = (GtkTreeDataSortHeader*) list->data;
2350 if (list_header->sort_column_id == sort_column_id)
2352 header = list_header;
2359 header = g_new0 (GtkTreeDataSortHeader, 1);
2360 header->sort_column_id = sort_column_id;
2361 list_store->sort_list = g_list_append (list_store->sort_list, header);
2364 if (header->destroy)
2366 GtkDestroyNotify d = header->destroy;
2368 header->destroy = NULL;
2372 header->func = func;
2373 header->data = data;
2374 header->destroy = destroy;
2379 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
2380 GtkTreeIterCompareFunc func,
2382 GtkDestroyNotify destroy)
2384 GtkListStore *list_store = (GtkListStore *) sortable;
2386 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2388 if (list_store->default_sort_destroy)
2390 GtkDestroyNotify d = list_store->default_sort_destroy;
2392 list_store->default_sort_destroy = NULL;
2393 d (list_store->default_sort_data);
2396 list_store->default_sort_func = func;
2397 list_store->default_sort_data = data;
2398 list_store->default_sort_destroy = destroy;
2402 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
2404 GtkListStore *list_store = (GtkListStore *) sortable;
2406 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
2408 return (list_store->default_sort_func != NULL);