2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtktreemodel.h"
22 #include "gtkliststore.h"
23 #include "gtktreedatalist.h"
24 #include "gtksignal.h"
25 #include "gtktreednd.h"
26 #include <gobject/gvaluecollector.h>
28 #define G_SLIST(x) ((GSList *) x)
29 #define GTK_LIST_STORE_IS_SORTED(list) (GTK_LIST_STORE (list)->sort_column_id != -2)
30 #define VALID_ITER(iter, list_store) (iter!= NULL && iter->user_data != NULL && list_store->stamp == iter->stamp)
32 static void gtk_list_store_init (GtkListStore *list_store);
33 static void gtk_list_store_class_init (GtkListStoreClass *class);
34 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
35 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
36 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
37 static void gtk_list_store_sortable_init (GtkTreeSortableIface *iface);
38 static void gtk_list_store_finalize (GObject *object);
39 static guint gtk_list_store_get_flags (GtkTreeModel *tree_model);
40 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
41 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
43 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
46 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
48 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
52 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
54 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
57 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
59 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
61 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
65 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
70 static void gtk_list_store_set_n_columns (GtkListStore *list_store,
72 static void gtk_list_store_set_column_type (GtkListStore *list_store,
78 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
80 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
82 GtkSelectionData *selection_data);
83 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
85 GtkSelectionData *selection_data);
86 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
87 GtkTreePath *dest_path,
88 GtkSelectionData *selection_data);
92 static void gtk_list_store_sort (GtkListStore *list_store);
93 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
96 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
99 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
102 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
104 GtkTreeIterCompareFunc func,
106 GtkDestroyNotify destroy);
107 static void gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
108 GtkTreeIterCompareFunc func,
110 GtkDestroyNotify destroy);
111 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable);
114 static GObjectClass *parent_class = NULL;
118 validate_list_store (GtkListStore *list_store)
120 if (gtk_debug_flags & GTK_DEBUG_TREE)
122 g_assert (g_slist_length (list_store->root) == list_store->length);
124 g_assert (g_slist_last (list_store->root) == list_store->tail);
129 gtk_list_store_get_type (void)
131 static GType list_store_type = 0;
133 if (!list_store_type)
135 static const GTypeInfo list_store_info =
137 sizeof (GtkListStoreClass),
138 NULL, /* base_init */
139 NULL, /* base_finalize */
140 (GClassInitFunc) gtk_list_store_class_init,
141 NULL, /* class_finalize */
142 NULL, /* class_data */
143 sizeof (GtkListStore),
145 (GInstanceInitFunc) gtk_list_store_init,
148 static const GInterfaceInfo tree_model_info =
150 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
155 static const GInterfaceInfo drag_source_info =
157 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
162 static const GInterfaceInfo drag_dest_info =
164 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
169 static const GInterfaceInfo sortable_info =
171 (GInterfaceInitFunc) gtk_list_store_sortable_init,
176 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
177 g_type_add_interface_static (list_store_type,
180 g_type_add_interface_static (list_store_type,
181 GTK_TYPE_TREE_DRAG_SOURCE,
183 g_type_add_interface_static (list_store_type,
184 GTK_TYPE_TREE_DRAG_DEST,
186 g_type_add_interface_static (list_store_type,
187 GTK_TYPE_TREE_SORTABLE,
191 return list_store_type;
195 gtk_list_store_class_init (GtkListStoreClass *class)
197 GObjectClass *object_class;
199 parent_class = g_type_class_peek_parent (class);
200 object_class = (GObjectClass*) class;
202 object_class->finalize = gtk_list_store_finalize;
206 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
208 iface->get_flags = gtk_list_store_get_flags;
209 iface->get_n_columns = gtk_list_store_get_n_columns;
210 iface->get_column_type = gtk_list_store_get_column_type;
211 iface->get_iter = gtk_list_store_get_iter;
212 iface->get_path = gtk_list_store_get_path;
213 iface->get_value = gtk_list_store_get_value;
214 iface->iter_next = gtk_list_store_iter_next;
215 iface->iter_children = gtk_list_store_iter_children;
216 iface->iter_has_child = gtk_list_store_iter_has_child;
217 iface->iter_n_children = gtk_list_store_iter_n_children;
218 iface->iter_nth_child = gtk_list_store_iter_nth_child;
219 iface->iter_parent = gtk_list_store_iter_parent;
223 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
225 iface->drag_data_delete = gtk_list_store_drag_data_delete;
226 iface->drag_data_get = gtk_list_store_drag_data_get;
230 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
232 iface->drag_data_received = gtk_list_store_drag_data_received;
233 iface->row_drop_possible = gtk_list_store_row_drop_possible;
237 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
239 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
240 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
241 iface->set_sort_func = gtk_list_store_set_sort_func;
242 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
243 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
247 gtk_list_store_init (GtkListStore *list_store)
249 list_store->root = NULL;
250 list_store->tail = NULL;
251 list_store->sort_list = NULL;
252 list_store->stamp = g_random_int ();
253 list_store->length = 0;
254 list_store->sort_column_id = -2;
255 list_store->columns_dirty = FALSE;
259 * gtk_list_store_new:
260 * @n_columns: number of columns in the list store
261 * @Varargs: all #GType types for the columns, from first to last
263 * Creates a new list store as with @n_columns columns each of the types passed
264 * in. As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
265 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
266 * int, string and #GdkPixbuf respectively.
268 * Return value: a new #GtkListStore
271 gtk_list_store_new (gint n_columns,
274 GtkListStore *retval;
278 g_return_val_if_fail (n_columns > 0, NULL);
280 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
281 gtk_list_store_set_n_columns (retval, n_columns);
283 va_start (args, n_columns);
285 for (i = 0; i < n_columns; i++)
287 GType type = va_arg (args, GType);
288 if (! _gtk_tree_data_list_check_type (type))
290 g_warning ("%s: Invalid type %s passed to gtk_list_store_new\n", G_STRLOC, g_type_name (type));
291 g_object_unref (G_OBJECT (retval));
295 gtk_list_store_set_column_type (retval, i, type);
305 * gtk_list_store_newv:
306 * @n_columns: number of columns in the list store
307 * @types: an array of #GType types for the columns, from first to last
309 * Non-vararg creation function. Used primarily by language bindings.
311 * Return value: a new #GtkListStore
314 gtk_list_store_newv (gint n_columns,
317 GtkListStore *retval;
320 g_return_val_if_fail (n_columns > 0, NULL);
322 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
323 gtk_list_store_set_n_columns (retval, n_columns);
325 for (i = 0; i < n_columns; i++)
327 if (! _gtk_tree_data_list_check_type (types[i]))
329 g_warning ("%s: Invalid type %s passed to gtk_list_store_newv\n", G_STRLOC, g_type_name (types[i]));
330 g_object_unref (G_OBJECT (retval));
334 gtk_list_store_set_column_type (retval, i, types[i]);
341 * gtk_list_store_set_column_types:
342 * @list_store: A #GtkListStore
343 * @n_columns: Number of columns for the list store
344 * @types: An array length n of #GTypes
346 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
347 * and should only be used when constructing a new #GtkListStore. It will not
348 * function after a row has been added, or a method on the #GtkTreeModel
349 * interface is called.
352 gtk_list_store_set_column_types (GtkListStore *list_store,
358 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
359 g_return_if_fail (list_store->columns_dirty == 0);
361 gtk_list_store_set_n_columns (list_store, n_columns);
362 for (i = 0; i < n_columns; i++)
364 if (! _gtk_tree_data_list_check_type (types[i]))
366 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_types\n", G_STRLOC, g_type_name (types[i]));
369 gtk_list_store_set_column_type (list_store, i, types[i]);
374 gtk_list_store_set_n_columns (GtkListStore *list_store,
379 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
380 g_return_if_fail (n_columns > 0);
382 if (list_store->n_columns == n_columns)
385 new_columns = g_new0 (GType, n_columns);
386 if (list_store->column_headers)
388 /* copy the old header orders over */
389 if (n_columns >= list_store->n_columns)
390 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
392 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
394 g_free (list_store->column_headers);
397 if (list_store->sort_list)
398 _gtk_tree_data_list_header_free (list_store->sort_list);
400 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
402 list_store->column_headers = new_columns;
403 list_store->n_columns = n_columns;
407 gtk_list_store_set_column_type (GtkListStore *list_store,
411 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
412 g_return_if_fail (column >=0 && column < list_store->n_columns);
413 if (!_gtk_tree_data_list_check_type (type))
415 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_type\n", G_STRLOC, g_type_name (type));
419 list_store->column_headers[column] = type;
423 gtk_list_store_finalize (GObject *object)
425 GtkListStore *list_store = GTK_LIST_STORE (object);
427 g_slist_foreach (list_store->root, (GFunc) _gtk_tree_data_list_free, list_store->column_headers);
428 g_slist_free (list_store->root);
430 _gtk_tree_data_list_header_free (list_store->sort_list);
431 g_free (list_store->column_headers);
433 if (list_store->default_sort_destroy)
435 GtkDestroyNotify d = list_store->default_sort_destroy;
437 list_store->default_sort_destroy = NULL;
438 d (list_store->default_sort_data);
439 list_store->default_sort_data = NULL;
443 (* parent_class->finalize) (object);
446 /* Fulfill the GtkTreeModel requirements */
448 gtk_list_store_get_flags (GtkTreeModel *tree_model)
450 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
452 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
456 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
458 GtkListStore *list_store = (GtkListStore *) tree_model;
460 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
462 list_store->columns_dirty = TRUE;
464 return list_store->n_columns;
468 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
471 GtkListStore *list_store = (GtkListStore *) tree_model;
473 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
474 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
475 index >= 0, G_TYPE_INVALID);
477 list_store->columns_dirty = TRUE;
479 return list_store->column_headers[index];
483 gtk_list_store_get_iter (GtkTreeModel *tree_model,
487 GtkListStore *list_store = (GtkListStore *) tree_model;
491 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
492 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
494 list_store->columns_dirty = TRUE;
496 i = gtk_tree_path_get_indices (path)[0];
498 if (i >= list_store->length)
501 list = g_slist_nth (G_SLIST (list_store->root), i);
503 /* If this fails, list_store->length has gotten mangled. */
506 iter->stamp = list_store->stamp;
507 iter->user_data = list;
513 gtk_list_store_get_path (GtkTreeModel *tree_model,
520 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
521 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
522 if (G_SLIST (iter->user_data) == G_SLIST (GTK_LIST_STORE (tree_model)->tail))
524 retval = gtk_tree_path_new ();
525 gtk_tree_path_append_index (retval, GTK_LIST_STORE (tree_model)->length - 1);
529 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
531 if (list == G_SLIST (iter->user_data))
538 retval = gtk_tree_path_new ();
539 gtk_tree_path_append_index (retval, i);
544 gtk_list_store_get_value (GtkTreeModel *tree_model,
549 GtkTreeDataList *list;
550 gint tmp_column = column;
552 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
553 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
554 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
556 list = G_SLIST (iter->user_data)->data;
558 while (tmp_column-- > 0 && list)
562 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
564 _gtk_tree_data_list_node_to_value (list,
565 GTK_LIST_STORE (tree_model)->column_headers[column],
570 gtk_list_store_iter_next (GtkTreeModel *tree_model,
573 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
574 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
576 iter->user_data = G_SLIST (iter->user_data)->next;
578 return (iter->user_data != NULL);
582 gtk_list_store_iter_children (GtkTreeModel *tree_model,
586 /* this is a list, nodes have no children */
590 /* but if parent == NULL we return the list itself as children of the
594 if (GTK_LIST_STORE (tree_model)->root)
596 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
597 iter->user_data = GTK_LIST_STORE (tree_model)->root;
605 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
612 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
615 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
617 return GTK_LIST_STORE (tree_model)->length;
619 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
624 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
631 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
636 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
640 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
641 iter->user_data = child;
649 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
657 gtk_list_store_real_set_value (GtkListStore *list_store,
663 GtkTreeDataList *list;
664 GtkTreeDataList *prev;
665 gint old_column = column;
666 GValue real_value = {0, };
667 gboolean converted = FALSE;
668 gboolean retval = FALSE;
670 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
671 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
672 g_return_val_if_fail (column >= 0 && column < list_store->n_columns, FALSE);
673 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
675 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
677 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
678 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
680 g_warning ("%s: Unable to convert from %s to %s\n",
682 g_type_name (G_VALUE_TYPE (value)),
683 g_type_name (list_store->column_headers[column]));
686 if (!g_value_transform (value, &real_value))
688 g_warning ("%s: Unable to make conversion from %s to %s\n",
690 g_type_name (G_VALUE_TYPE (value)),
691 g_type_name (list_store->column_headers[column]));
692 g_value_unset (&real_value);
698 prev = list = G_SLIST (iter->user_data)->data;
705 _gtk_tree_data_list_value_to_node (list, &real_value);
707 _gtk_tree_data_list_value_to_node (list, value);
710 g_value_unset (&real_value);
719 if (G_SLIST (iter->user_data)->data == NULL)
721 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
726 list = prev->next = _gtk_tree_data_list_alloc ();
732 list->next = _gtk_tree_data_list_alloc ();
739 _gtk_tree_data_list_value_to_node (list, &real_value);
741 _gtk_tree_data_list_value_to_node (list, value);
745 g_value_unset (&real_value);
747 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
748 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
755 * gtk_list_store_set_value:
756 * @list_store: A #GtkListStore
757 * @iter: A valid #GtkTreeIter for the row being modified
758 * @column: column number to modify
759 * @value: new value for the cell
761 * Sets the data in the cell specified by @iter and @column.
762 * The type of @value must be convertible to the type of the
767 gtk_list_store_set_value (GtkListStore *list_store,
772 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
773 g_return_if_fail (VALID_ITER (iter, list_store));
774 g_return_if_fail (column >= 0 && column < list_store->n_columns);
775 g_return_if_fail (G_IS_VALUE (value));
777 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
781 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
782 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
783 gtk_tree_path_free (path);
788 * gtk_list_store_set_valist:
789 * @list_store: A #GtkListStore
790 * @iter: A valid #GtkTreeIter for the row being modified
791 * @var_args: va_list of column/value pairs
793 * See gtk_list_store_set(); this version takes a va_list for use by language
798 gtk_list_store_set_valist (GtkListStore *list_store,
803 gboolean emit_signal = FALSE;
804 gboolean maybe_need_sort = FALSE;
805 GtkTreeIterCompareFunc func = NULL;
807 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
808 g_return_if_fail (VALID_ITER (iter, list_store));
810 column = va_arg (var_args, gint);
812 if (GTK_LIST_STORE_IS_SORTED (list_store))
814 if (list_store->sort_column_id != -1)
816 GtkTreeDataSortHeader *header;
817 header = _gtk_tree_data_list_get_header (list_store->sort_list,
818 list_store->sort_column_id);
819 g_return_if_fail (header != NULL);
820 g_return_if_fail (header->func != NULL);
825 func = list_store->default_sort_func;
829 if (func != gtk_tree_data_list_compare_func)
830 maybe_need_sort = TRUE;
834 GValue value = { 0, };
837 if (column >= list_store->n_columns)
839 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
842 g_value_init (&value, list_store->column_headers[column]);
844 G_VALUE_COLLECT (&value, var_args, 0, &error);
847 g_warning ("%s: %s", G_STRLOC, error);
850 /* we purposely leak the value here, it might not be
851 * in a sane state if an error condition occoured
856 /* FIXME: instead of calling this n times, refactor with above */
857 emit_signal = gtk_list_store_real_set_value (list_store,
861 FALSE) || emit_signal;
863 if (func == gtk_tree_data_list_compare_func &&
864 column == list_store->sort_column_id)
865 maybe_need_sort = TRUE;
867 g_value_unset (&value);
869 column = va_arg (var_args, gint);
872 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
873 gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
879 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
880 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
881 gtk_tree_path_free (path);
886 * gtk_list_store_set:
887 * @list_store: a #GtkListStore
888 * @iter: row iterator
889 * @Varargs: pairs of column number and value, terminated with -1
891 * Sets the value of one or more cells in the row referenced by @iter.
892 * The variable argument list should contain integer column numbers,
893 * each column number followed by the value to be set.
894 * The list is terminated by a -1. For example, to set column 0 with type
895 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
896 * 0, "Foo", -1)</literal>.
899 gtk_list_store_set (GtkListStore *list_store,
905 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
906 g_return_if_fail (iter != NULL);
907 g_return_if_fail (iter->stamp == list_store->stamp);
909 va_start (var_args, iter);
910 gtk_list_store_set_valist (list_store, iter, var_args);
915 remove_link_saving_prev (GSList *list,
930 prev->next = link->next;
949 gtk_list_store_remove_silently (GtkListStore *list_store,
953 if (G_SLIST (iter->user_data)->data)
955 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
956 list_store->column_headers);
957 G_SLIST (iter->user_data)->data = NULL;
963 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
964 G_SLIST (iter->user_data),
967 list_store->length -= 1;
969 if (iter->user_data == list_store->tail)
970 list_store->tail = prev;
975 * gtk_list_store_remove:
976 * @list_store: A #GtkListStore
977 * @iter: A valid #GtkTreeIter
979 * Removes the given row from the list store. After being removed,
980 * @iter is set to be the next valid row, or invalidated if it pointed
981 * to the last row in @list_store.
983 * Return value: %TRUE if @iter is valid, %FALSE if not.
986 gtk_list_store_remove (GtkListStore *list_store,
992 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
993 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
995 next = G_SLIST (iter->user_data)->next;
996 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
998 validate_list_store (list_store);
1000 gtk_list_store_remove_silently (list_store, iter, path);
1002 validate_list_store (list_store);
1004 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
1005 gtk_tree_path_free (path);
1009 iter->stamp = list_store->stamp;
1010 iter->user_data = next;
1022 insert_after (GtkListStore *list_store,
1026 g_return_if_fail (sibling != NULL);
1027 g_return_if_fail (new_list != NULL);
1029 /* insert new node after list */
1030 new_list->next = sibling->next;
1031 sibling->next = new_list;
1033 /* if list was the tail, the new node is the new tail */
1034 if (sibling == ((GSList *) list_store->tail))
1035 list_store->tail = new_list;
1037 list_store->length += 1;
1041 * gtk_list_store_insert:
1042 * @list_store: A #GtkListStore
1043 * @iter: An unset #GtkTreeIter to set to the new row
1044 * @position: position to insert the new row
1046 * Creates a new row at @position. @iter will be changed to point to this new
1047 * row. If @position is larger than the number of rows on the list, then the
1048 * new row will be appended to the list. The row will be empty before this
1049 * function is called. To fill in values, you need to call gtk_list_store_set()
1050 * or gtk_list_store_set_value().
1054 gtk_list_store_insert (GtkListStore *list_store,
1062 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1063 g_return_if_fail (iter != NULL);
1064 g_return_if_fail (position >= 0);
1066 list_store->columns_dirty = TRUE;
1068 if (position == 0 ||
1069 GTK_LIST_STORE_IS_SORTED (list_store))
1071 gtk_list_store_prepend (list_store, iter);
1075 new_list = g_slist_alloc ();
1077 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
1081 /* position if off the end of the list, append it */
1082 gtk_list_store_append (list_store, iter);
1087 insert_after (list_store, list, new_list);
1089 iter->stamp = list_store->stamp;
1090 iter->user_data = new_list;
1092 validate_list_store (list_store);
1094 path = gtk_tree_path_new ();
1095 gtk_tree_path_append_index (path, position);
1096 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1097 gtk_tree_path_free (path);
1101 * gtk_list_store_insert_before:
1102 * @list_store: A #GtkListStore
1103 * @iter: An unset #GtkTreeIter to set to the new row
1104 * @sibling: A valid #GtkTreeIter, or %NULL
1106 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
1107 * appended to the end of the list. @iter will be changed to point to this new
1108 * row. The row will be empty before this function is called. To fill in values,
1109 * you need to call gtk_list_store_set() or gtk_list_store_set_value().
1113 gtk_list_store_insert_before (GtkListStore *list_store,
1115 GtkTreeIter *sibling)
1118 GSList *list, *prev, *new_list;
1121 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1122 g_return_if_fail (iter != NULL);
1124 g_return_if_fail (VALID_ITER (sibling, list_store));
1126 list_store->columns_dirty = TRUE;
1128 if (GTK_LIST_STORE_IS_SORTED (list_store))
1130 gtk_list_store_prepend (list_store, iter);
1134 if (sibling == NULL)
1136 gtk_list_store_append (list_store, iter);
1140 new_list = g_slist_alloc ();
1143 list = list_store->root;
1144 while (list && list != sibling->user_data)
1151 if (list != sibling->user_data)
1153 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1157 /* if there are no nodes, we become the list tail, otherwise we
1158 * are inserting before any existing nodes so we can't change
1162 if (list_store->root == NULL)
1163 list_store->tail = new_list;
1167 new_list->next = prev->next;
1168 prev->next = new_list;
1172 new_list->next = list_store->root;
1173 list_store->root = new_list;
1176 iter->stamp = list_store->stamp;
1177 iter->user_data = new_list;
1179 list_store->length += 1;
1181 validate_list_store (list_store);
1183 path = gtk_tree_path_new ();
1184 gtk_tree_path_append_index (path, i);
1185 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1186 gtk_tree_path_free (path);
1190 * gtk_list_store_insert_after:
1191 * @list_store: A #GtkListStore
1192 * @iter: An unset #GtkTreeIter to set to the new row
1193 * @sibling: A valid #GtkTreeIter, or %NULL
1195 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1196 * prepended to the beginning of the list. @iter will be changed to point to
1197 * this new row. The row will be empty after this function is called. To fill
1198 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1202 gtk_list_store_insert_after (GtkListStore *list_store,
1204 GtkTreeIter *sibling)
1207 GSList *list, *new_list;
1210 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1211 g_return_if_fail (iter != NULL);
1213 g_return_if_fail (VALID_ITER (sibling, list_store));
1215 list_store->columns_dirty = TRUE;
1217 if (sibling == NULL ||
1218 GTK_LIST_STORE_IS_SORTED (list_store))
1220 gtk_list_store_prepend (list_store, iter);
1224 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1227 g_return_if_fail (list == sibling->user_data);
1229 new_list = g_slist_alloc ();
1231 insert_after (list_store, list, new_list);
1233 iter->stamp = list_store->stamp;
1234 iter->user_data = new_list;
1236 validate_list_store (list_store);
1238 path = gtk_tree_path_new ();
1239 gtk_tree_path_append_index (path, i + 1);
1240 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1241 gtk_tree_path_free (path);
1245 * gtk_list_store_prepend:
1246 * @list_store: A #GtkListStore
1247 * @iter: An unset #GtkTreeIter to set to the prepend row
1249 * Prepends a new row to @list_store. @iter will be changed to point to this new
1250 * row. The row will be empty after this function is called. To fill in
1251 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1255 gtk_list_store_prepend (GtkListStore *list_store,
1260 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1261 g_return_if_fail (iter != NULL);
1263 iter->stamp = list_store->stamp;
1264 iter->user_data = g_slist_alloc ();
1266 list_store->columns_dirty = TRUE;
1268 if (list_store->root == NULL)
1269 list_store->tail = iter->user_data;
1271 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1272 list_store->root = iter->user_data;
1274 list_store->length += 1;
1276 validate_list_store (list_store);
1278 path = gtk_tree_path_new ();
1279 gtk_tree_path_append_index (path, 0);
1280 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1281 gtk_tree_path_free (path);
1285 * gtk_list_store_append:
1286 * @list_store: A #GtkListStore
1287 * @iter: An unset #GtkTreeIter to set to the appended row
1289 * Appends a new row to @list_store. @iter will be changed to point to this new
1290 * row. The row will be empty after this function is called. To fill in
1291 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1295 gtk_list_store_append (GtkListStore *list_store,
1300 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1301 g_return_if_fail (iter != NULL);
1303 list_store->columns_dirty = TRUE;
1305 if (GTK_LIST_STORE_IS_SORTED (list_store))
1307 gtk_list_store_prepend (list_store, iter);
1311 iter->stamp = list_store->stamp;
1312 iter->user_data = g_slist_alloc ();
1314 if (list_store->tail)
1315 ((GSList *)list_store->tail)->next = iter->user_data;
1317 list_store->root = iter->user_data;
1319 list_store->tail = iter->user_data;
1321 list_store->length += 1;
1323 validate_list_store (list_store);
1325 path = gtk_tree_path_new ();
1326 gtk_tree_path_append_index (path, list_store->length - 1);
1327 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1328 gtk_tree_path_free (path);
1332 * gtk_list_store_clear:
1333 * @list_store: a #GtkListStore.
1335 * Removes all rows from the list store.
1339 gtk_list_store_clear (GtkListStore *list_store)
1342 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1344 while (list_store->root)
1346 iter.stamp = list_store->stamp;
1347 iter.user_data = list_store->root;
1348 gtk_list_store_remove (list_store, &iter);
1353 * gtk_list_store_iter_is_valid:
1354 * @list_store: A #GtkListStore.
1355 * @iter: A #GtkTreeIter.
1357 * WARNING: This function is slow. Only use it for debugging and/or testing
1360 * Checks if the given iter is a valid iter for this #GtkListStore.
1362 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1365 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1370 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1371 g_return_val_if_fail (iter != NULL, FALSE);
1373 if (!VALID_ITER (iter, list_store))
1376 if (iter->user_data == list_store->root)
1378 if (iter->user_data == list_store->tail)
1381 for (list = ((GList *)list_store->root)->next; list; list = list->next)
1382 if (list == iter->user_data)
1389 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1393 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1395 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1399 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1406 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1408 GtkSelectionData *selection_data)
1410 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1412 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1413 * target, because the default handler does it for us, but
1414 * we do anyway for the convenience of someone maybe overriding the
1418 if (gtk_tree_set_row_drag_data (selection_data,
1419 GTK_TREE_MODEL (drag_source),
1426 /* FIXME handle text targets at least. */
1433 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1435 GtkSelectionData *selection_data)
1437 GtkTreeModel *tree_model;
1438 GtkListStore *list_store;
1439 GtkTreeModel *src_model = NULL;
1440 GtkTreePath *src_path = NULL;
1441 gboolean retval = FALSE;
1443 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1445 tree_model = GTK_TREE_MODEL (drag_dest);
1446 list_store = GTK_LIST_STORE (drag_dest);
1448 if (gtk_tree_get_row_drag_data (selection_data,
1451 src_model == tree_model)
1453 /* Copy the given row to a new position */
1454 GtkTreeIter src_iter;
1455 GtkTreeIter dest_iter;
1458 if (!gtk_tree_model_get_iter (src_model,
1465 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1466 prev = gtk_tree_path_copy (dest);
1468 if (!gtk_tree_path_prev (prev))
1470 /* dest was the first spot in the list; which means we are supposed
1473 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1480 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1484 GtkTreeIter tmp_iter = dest_iter;
1486 if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_model), "gtk-tree-model-drop-append")))
1487 gtk_list_store_append (GTK_LIST_STORE (tree_model), &dest_iter);
1489 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1490 &dest_iter, &tmp_iter);
1496 g_object_set_data (G_OBJECT (tree_model), "gtk-tree-model-drop-append",
1499 gtk_tree_path_free (prev);
1501 /* If we succeeded in creating dest_iter, copy data from src
1505 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1506 GtkTreeDataList *copy_head = NULL;
1507 GtkTreeDataList *copy_prev = NULL;
1508 GtkTreeDataList *copy_iter = NULL;
1515 copy_iter = _gtk_tree_data_list_node_copy (dl,
1516 list_store->column_headers[col]);
1518 if (copy_head == NULL)
1519 copy_head = copy_iter;
1522 copy_prev->next = copy_iter;
1524 copy_prev = copy_iter;
1530 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1531 G_SLIST (dest_iter.user_data)->data = copy_head;
1533 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1534 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter);
1535 gtk_tree_path_free (path);
1540 /* FIXME maybe add some data targets eventually, or handle text
1541 * targets in the simple case.
1548 gtk_tree_path_free (src_path);
1554 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1555 GtkTreePath *dest_path,
1556 GtkSelectionData *selection_data)
1559 GtkTreeModel *src_model = NULL;
1560 GtkTreePath *src_path = NULL;
1561 gboolean retval = FALSE;
1563 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1565 if (!gtk_tree_get_row_drag_data (selection_data,
1570 if (src_model != GTK_TREE_MODEL (drag_dest))
1573 if (gtk_tree_path_get_depth (dest_path) != 1)
1576 /* can drop before any existing node, or before one past any existing. */
1578 indices = gtk_tree_path_get_indices (dest_path);
1580 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1584 gtk_tree_path_free (src_path);
1589 /* Sorting and reordering */
1590 typedef struct _SortTuple
1598 gtk_list_store_reorder_func (gconstpointer a,
1602 SortTuple *a_reorder;
1603 SortTuple *b_reorder;
1605 a_reorder = (SortTuple *)a;
1606 b_reorder = (SortTuple *)b;
1608 if (a_reorder->offset < b_reorder->offset)
1610 if (a_reorder->offset > b_reorder->offset)
1617 * gtk_list_store_reorder:
1618 * @store: A #GtkTreeStore.
1619 * @new_order: An integer array indicating the new order for the list.
1621 * Reorders @store to follow the order indicated by @new_order. Note that
1622 * this function only works with unsorted stores.
1625 gtk_list_store_reorder (GtkListStore *store,
1629 GSList *current_list;
1631 SortTuple *sort_array;
1633 g_return_if_fail (GTK_IS_LIST_STORE (store));
1634 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1635 g_return_if_fail (new_order != NULL);
1637 sort_array = g_new (SortTuple, store->length);
1639 current_list = store->root;
1641 for (i = 0; i < store->length; i++)
1643 sort_array[i].offset = new_order[i];
1644 sort_array[i].el = current_list;
1646 current_list = current_list->next;
1649 g_qsort_with_data (sort_array,
1652 gtk_list_store_reorder_func,
1655 for (i = 0; i < store->length - 1; i++)
1656 G_SLIST (sort_array[i].el)->next = G_SLIST (sort_array[i+1].el);
1658 store->root = G_SLIST (sort_array[0].el);
1659 store->tail = G_SLIST (sort_array[store->length-1].el);
1660 G_SLIST (store->tail)->next = NULL;
1663 path = gtk_tree_path_new ();
1664 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1665 path, NULL, new_order);
1666 gtk_tree_path_free (path);
1667 g_free (sort_array);
1671 * gtk_list_store_swap:
1672 * @store: A #GtkListStore.
1673 * @a: A #GtkTreeIter.
1674 * @b: Another #GtkTreeIter.
1676 * Swaps @a and @b in @store. Note that this function only works with
1680 gtk_list_store_swap (GtkListStore *store,
1684 GSList *i, *prev_a = NULL, *prev_b = NULL;
1685 gint j, a_count = 0, b_count = 0, *order;
1688 g_return_if_fail (GTK_IS_LIST_STORE (store));
1689 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1690 g_return_if_fail (VALID_ITER (a, store));
1691 g_return_if_fail (VALID_ITER (b, store));
1693 if (a->user_data == b->user_data)
1696 if (a->user_data == store->root)
1700 for (i = store->root; i; i = i->next, a_count++)
1701 if (i->next == a->user_data)
1710 if (a->user_data == store->root)
1714 for (i = store->root; i; i = i->next, b_count++)
1715 if (i->next == b->user_data)
1725 store->root = b->user_data;
1727 prev_a->next = b->user_data;
1730 store->root = a->user_data;
1732 prev_b->next = a->user_data;
1734 /* think a_next inspead of a_prev here ... */
1735 prev_a = G_SLIST (a->user_data)->next;
1736 prev_b = G_SLIST (b->user_data)->next;
1738 G_SLIST (a->user_data)->next = prev_b;
1739 G_SLIST (b->user_data)->next = prev_a;
1742 order = g_new (gint, store->length);
1743 for (j = 0; j < store->length; j++)
1746 else if (j == b_count)
1751 path = gtk_tree_path_new ();
1752 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1754 gtk_tree_path_free (path);
1759 * gtk_list_store_move:
1760 * @store: A #GtkTreeStore.
1761 * @iter: A #GtkTreeIter.
1762 * @position: A #GtkTreePath.
1764 * Moves @iter in @store to the position before @position. Note that this
1765 * function only works with unsorted stores.
1768 gtk_list_store_move (GtkListStore *store,
1770 GtkTreePath *position)
1772 GSList *i, *prev = NULL, *new_prev = NULL;
1773 gint old_pos = 0, j = 0, *order;
1776 g_return_if_fail (GTK_IS_LIST_STORE (store));
1777 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1778 g_return_if_fail (VALID_ITER (iter, store));
1779 g_return_if_fail (position != NULL);
1781 if (gtk_tree_path_get_depth (position) != 1)
1784 if (iter->user_data == store->root)
1788 for (i = store->root; i; i = i->next, old_pos++)
1789 if (i->next == iter->user_data)
1798 if (old_pos == gtk_tree_path_get_indices (position)[0])
1801 if (gtk_tree_path_get_indices (position)[0] == 0)
1805 for (i = store->root; i; i = i->next, j++)
1806 if (j == gtk_tree_path_get_indices (position)[0] - 1)
1814 store->root = G_SLIST (iter->user_data)->next;
1816 prev->next = G_SLIST (iter->user_data)->next;
1820 G_SLIST (iter->user_data)->next = store->root;
1821 store->root = G_SLIST (iter->user_data);
1825 G_SLIST (iter->user_data)->next = new_prev->next;
1826 new_prev->next = G_SLIST (iter->user_data);
1830 order = g_new (gint, store->length);
1831 for (j = 0; j < store->length; j++)
1834 else if (j >= old_pos && j < gtk_tree_path_get_indices (position)[0])
1836 else if (j == gtk_tree_path_get_indices (position)[0])
1841 path = gtk_tree_path_new ();
1842 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1844 gtk_tree_path_free (path);
1850 gtk_list_store_compare_func (gconstpointer a,
1854 GtkListStore *list_store = user_data;
1855 GSList *el_a; /* Los Angeles? */
1860 GtkTreeIterCompareFunc func;
1864 if (list_store->sort_column_id != -1)
1866 GtkTreeDataSortHeader *header;
1868 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1869 list_store->sort_column_id);
1870 g_return_val_if_fail (header != NULL, 0);
1871 g_return_val_if_fail (header->func != NULL, 0);
1873 func = header->func;
1874 data = header->data;
1878 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1879 func = list_store->default_sort_func;
1880 data = list_store->default_sort_data;
1883 el_a = ((SortTuple *) a)->el;
1884 el_b = ((SortTuple *) b)->el;
1886 iter_a.stamp = list_store->stamp;
1887 iter_a.user_data = el_a;
1888 iter_b.stamp = list_store->stamp;
1889 iter_b.user_data = el_b;
1891 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1893 if (list_store->order == GTK_SORT_DESCENDING)
1897 else if (retval < 0)
1904 gtk_list_store_sort (GtkListStore *list_store)
1912 if (list_store->length <= 1)
1915 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1917 list = G_SLIST (list_store->root);
1919 sort_array = g_array_sized_new (FALSE, FALSE,
1921 list_store->length);
1923 for (i = 0; i < list_store->length; i++)
1927 /* If this fails, we are in an inconsistent state. Bad */
1928 g_return_if_fail (list != NULL);
1932 g_array_append_val (sort_array, tuple);
1937 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1939 for (i = 0; i < list_store->length - 1; i++)
1940 g_array_index (sort_array, SortTuple, i).el->next =
1941 g_array_index (sort_array, SortTuple, i + 1).el;
1942 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1943 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1944 list_store->tail = g_array_index (sort_array, SortTuple, list_store->length - 1).el;
1946 /* Let the world know about our new order */
1947 new_order = g_new (gint, list_store->length);
1948 for (i = 0; i < list_store->length; i++)
1949 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1951 path = gtk_tree_path_new ();
1952 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1953 path, NULL, new_order);
1954 gtk_tree_path_free (path);
1956 g_array_free (sort_array, TRUE);
1960 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1965 GSList *prev = NULL;
1966 GSList *next = NULL;
1967 GSList *list = G_SLIST (list_store->root);
1968 GtkTreePath *tmp_path;
1969 GtkTreeIter tmp_iter;
1976 GtkTreeIterCompareFunc func;
1979 if (list_store->length < 2)
1982 tmp_iter.stamp = list_store->stamp;
1984 if (list_store->sort_column_id != -1)
1986 GtkTreeDataSortHeader *header;
1987 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1988 list_store->sort_column_id);
1989 g_return_if_fail (header != NULL);
1990 g_return_if_fail (header->func != NULL);
1991 func = header->func;
1992 data = header->data;
1996 g_return_if_fail (list_store->default_sort_func != NULL);
1997 func = list_store->default_sort_func;
1998 data = list_store->default_sort_data;
2001 /* If it's the built in function, we don't sort. */
2002 if (func == gtk_tree_data_list_compare_func &&
2003 list_store->sort_column_id != column)
2007 /* First we find the iter, its prev, and its next */
2010 if (list == G_SLIST (iter->user_data))
2016 g_assert (list != NULL);
2020 /* Check the common case, where we don't need to sort it moved. */
2023 tmp_iter.user_data = prev;
2024 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2029 tmp_iter.user_data = next;
2030 cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2033 if (list_store->order == GTK_SORT_DESCENDING)
2046 if (prev == NULL && cmp_b <= 0)
2048 else if (next == NULL && cmp_a <= 0)
2050 else if (prev != NULL && next != NULL &&
2051 cmp_a <= 0 && cmp_b <= 0)
2054 /* We actually need to sort it */
2055 /* First, remove the old link. */
2058 list_store->root = next;
2062 list_store->tail = prev;
2065 /* FIXME: as an optimization, we can potentially start at next */
2067 list = G_SLIST (list_store->root);
2069 tmp_iter.user_data = list;
2070 if (list_store->order == GTK_SORT_DESCENDING)
2071 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2073 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2075 while ((list->next) && (cmp_a > 0))
2080 tmp_iter.user_data = list;
2081 if (list_store->order == GTK_SORT_DESCENDING)
2082 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2084 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2087 if ((!list->next) && (cmp_a > 0))
2090 list->next = G_SLIST (iter->user_data);
2091 list_store->tail = list->next;
2095 prev->next = G_SLIST (iter->user_data);
2096 G_SLIST (iter->user_data)->next = list;
2100 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
2101 list_store->root = G_SLIST (iter->user_data);
2104 /* Emit the reordered signal. */
2105 new_order = g_new (int, list_store->length);
2106 if (old_location < new_location)
2107 for (i = 0; i < list_store->length; i++)
2109 if (i < old_location ||
2112 else if (i >= old_location &&
2114 new_order[i] = i + 1;
2115 else if (i == new_location)
2116 new_order[i] = old_location;
2119 for (i = 0; i < list_store->length; i++)
2121 if (i < new_location ||
2124 else if (i > new_location &&
2126 new_order[i] = i - 1;
2127 else if (i == new_location)
2128 new_order[i] = old_location;
2131 tmp_path = gtk_tree_path_new ();
2132 tmp_iter.user_data = NULL;
2134 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
2138 gtk_tree_path_free (tmp_path);
2143 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
2144 gint *sort_column_id,
2147 GtkListStore *list_store = (GtkListStore *) sortable;
2149 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
2151 if (list_store->sort_column_id == -1)
2155 * sort_column_id = list_store->sort_column_id;
2157 * order = list_store->order;
2162 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
2163 gint sort_column_id,
2166 GtkListStore *list_store = (GtkListStore *) sortable;
2168 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2170 if ((list_store->sort_column_id == sort_column_id) &&
2171 (list_store->order == order))
2174 if (sort_column_id != -1)
2176 GtkTreeDataSortHeader *header = NULL;
2178 header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
2180 /* We want to make sure that we have a function */
2181 g_return_if_fail (header != NULL);
2182 g_return_if_fail (header->func != NULL);
2186 g_return_if_fail (list_store->default_sort_func != NULL);
2190 list_store->sort_column_id = sort_column_id;
2191 list_store->order = order;
2193 gtk_list_store_sort (list_store);
2195 gtk_tree_sortable_sort_column_changed (sortable);
2199 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
2200 gint sort_column_id,
2201 GtkTreeIterCompareFunc func,
2203 GtkDestroyNotify destroy)
2205 GtkListStore *list_store = (GtkListStore *) sortable;
2206 GtkTreeDataSortHeader *header = NULL;
2209 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2210 g_return_if_fail (func != NULL);
2212 for (list = list_store->sort_list; list; list = list->next)
2214 GtkTreeDataSortHeader *list_header;
2216 list_header = (GtkTreeDataSortHeader*) list->data;
2217 if (list_header->sort_column_id == sort_column_id)
2219 header = list_header;
2226 header = g_new0 (GtkTreeDataSortHeader, 1);
2227 header->sort_column_id = sort_column_id;
2228 list_store->sort_list = g_list_append (list_store->sort_list, header);
2231 if (header->destroy)
2233 GtkDestroyNotify d = header->destroy;
2235 header->destroy = NULL;
2239 header->func = func;
2240 header->data = data;
2241 header->destroy = destroy;
2246 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
2247 GtkTreeIterCompareFunc func,
2249 GtkDestroyNotify destroy)
2251 GtkListStore *list_store = (GtkListStore *) sortable;
2253 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2255 if (list_store->default_sort_destroy)
2257 GtkDestroyNotify d = list_store->default_sort_destroy;
2259 list_store->default_sort_destroy = NULL;
2260 d (list_store->default_sort_data);
2263 list_store->default_sort_func = func;
2264 list_store->default_sort_data = data;
2265 list_store->default_sort_destroy = destroy;
2269 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
2271 GtkListStore *list_store = (GtkListStore *) sortable;
2273 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
2275 return (list_store->default_sort_func != NULL);