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 != -1)
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 guint 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 GtkTreeModel *src_model,
87 GtkTreePath *src_path,
88 GtkTreePath *dest_path);
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,
98 GtkTreeSortOrder *order);
99 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
101 GtkTreeSortOrder order);
102 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
104 GtkTreeIterCompareFunc func,
106 GtkDestroyNotify destroy);
110 validate_list_store (GtkListStore *list_store)
112 if (gtk_debug_flags & GTK_DEBUG_TREE)
114 g_assert (g_slist_length (list_store->root) == list_store->length);
116 g_assert (g_slist_last (list_store->root) == list_store->tail);
121 gtk_list_store_get_type (void)
123 static GType list_store_type = 0;
125 if (!list_store_type)
127 static const GTypeInfo list_store_info =
129 sizeof (GtkListStoreClass),
130 NULL, /* base_init */
131 NULL, /* base_finalize */
132 (GClassInitFunc) gtk_list_store_class_init,
133 NULL, /* class_finalize */
134 NULL, /* class_data */
135 sizeof (GtkListStore),
137 (GInstanceInitFunc) gtk_list_store_init,
140 static const GInterfaceInfo tree_model_info =
142 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
147 static const GInterfaceInfo drag_source_info =
149 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
154 static const GInterfaceInfo drag_dest_info =
156 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
161 static const GInterfaceInfo sortable_info =
163 (GInterfaceInitFunc) gtk_list_store_sortable_init,
168 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
169 g_type_add_interface_static (list_store_type,
172 g_type_add_interface_static (list_store_type,
173 GTK_TYPE_TREE_DRAG_SOURCE,
175 g_type_add_interface_static (list_store_type,
176 GTK_TYPE_TREE_DRAG_DEST,
178 g_type_add_interface_static (list_store_type,
179 GTK_TYPE_TREE_SORTABLE,
183 return list_store_type;
187 gtk_list_store_class_init (GtkListStoreClass *class)
189 GObjectClass *object_class;
191 object_class = (GObjectClass*) class;
195 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
197 iface->get_flags = gtk_list_store_get_flags;
198 iface->get_n_columns = gtk_list_store_get_n_columns;
199 iface->get_column_type = gtk_list_store_get_column_type;
200 iface->get_iter = gtk_list_store_get_iter;
201 iface->get_path = gtk_list_store_get_path;
202 iface->get_value = gtk_list_store_get_value;
203 iface->iter_next = gtk_list_store_iter_next;
204 iface->iter_children = gtk_list_store_iter_children;
205 iface->iter_has_child = gtk_list_store_iter_has_child;
206 iface->iter_n_children = gtk_list_store_iter_n_children;
207 iface->iter_nth_child = gtk_list_store_iter_nth_child;
208 iface->iter_parent = gtk_list_store_iter_parent;
212 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
214 iface->drag_data_delete = gtk_list_store_drag_data_delete;
215 iface->drag_data_get = gtk_list_store_drag_data_get;
219 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
221 iface->drag_data_received = gtk_list_store_drag_data_received;
222 iface->row_drop_possible = gtk_list_store_row_drop_possible;
226 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
228 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
229 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
230 iface->set_sort_func = gtk_list_store_set_sort_func;
234 gtk_list_store_init (GtkListStore *list_store)
236 list_store->root = NULL;
237 list_store->tail = NULL;
238 list_store->sort_list = NULL;
239 list_store->stamp = g_random_int ();
240 list_store->length = 0;
241 list_store->sort_column_id = -1;
245 * gtk_list_store_new:
246 * @n_columns: number of columns in the list store
247 * @Varargs: all #GType types for the columns, from first to last
249 * Creates a new list store as with @n_columns columns each of the types passed
250 * in. As an example, gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
251 * GDK_TYPE_PIXBUF); will create a new GtkListStore with three columns, of type
252 * int, string and GDkPixbuf respectively.
254 * Return value: a new #GtkListStore
257 gtk_list_store_new (gint n_columns,
260 GtkListStore *retval;
264 g_return_val_if_fail (n_columns > 0, NULL);
266 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
267 gtk_list_store_set_n_columns (retval, n_columns);
269 va_start (args, n_columns);
271 for (i = 0; i < n_columns; i++)
273 GType type = va_arg (args, GType);
274 if (! _gtk_tree_data_list_check_type (type))
276 g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (type));
277 g_object_unref (G_OBJECT (retval));
281 gtk_list_store_set_column_type (retval, i, type);
291 * gtk_list_store_newv:
292 * @n_columns: number of columns in the list store
293 * @types: an array of #GType types for the columns, from first to last
295 * Non vararg creation function. Used primarily by language bindings.
297 * Return value: a new #GtkListStore
300 gtk_list_store_newv (gint n_columns,
303 GtkListStore *retval;
306 g_return_val_if_fail (n_columns > 0, NULL);
308 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
309 gtk_list_store_set_n_columns (retval, n_columns);
311 for (i = 0; i < n_columns; i++)
313 if (! _gtk_tree_data_list_check_type (types[i]))
315 g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (types[i]));
316 g_object_unref (G_OBJECT (retval));
320 gtk_list_store_set_column_type (retval, i, types[i]);
327 gtk_list_store_set_n_columns (GtkListStore *list_store,
332 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
333 g_return_if_fail (n_columns > 0);
335 if (list_store->n_columns == n_columns)
338 new_columns = g_new0 (GType, n_columns);
339 if (list_store->column_headers)
341 /* copy the old header orders over */
342 if (n_columns >= list_store->n_columns)
343 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
345 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
347 g_free (list_store->column_headers);
350 if (list_store->sort_list)
351 _gtk_tree_data_list_header_free (list_store->sort_list);
353 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
355 list_store->column_headers = new_columns;
356 list_store->n_columns = n_columns;
360 gtk_list_store_set_column_type (GtkListStore *list_store,
364 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
365 g_return_if_fail (column >=0 && column < list_store->n_columns);
366 if (!_gtk_tree_data_list_check_type (type))
368 g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (type));
372 list_store->column_headers[column] = type;
375 /* Fulfill the GtkTreeModel requirements */
377 gtk_list_store_get_flags (GtkTreeModel *tree_model)
379 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
381 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
385 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
387 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
389 return GTK_LIST_STORE (tree_model)->n_columns;
393 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
396 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
397 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
398 index >= 0, G_TYPE_INVALID);
400 return GTK_LIST_STORE (tree_model)->column_headers[index];
404 gtk_list_store_get_iter (GtkTreeModel *tree_model,
411 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
412 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
414 i = gtk_tree_path_get_indices (path)[0];
416 if (i >= GTK_LIST_STORE (tree_model)->length)
419 list = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), i);
421 /* If this fails, list_store->length has gotten mangled. */
424 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
425 iter->user_data = list;
430 gtk_list_store_get_path (GtkTreeModel *tree_model,
437 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
438 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
440 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
442 if (list == G_SLIST (iter->user_data))
449 retval = gtk_tree_path_new ();
450 gtk_tree_path_append_index (retval, i);
455 gtk_list_store_get_value (GtkTreeModel *tree_model,
460 GtkTreeDataList *list;
461 gint tmp_column = column;
463 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
464 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
465 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
467 list = G_SLIST (iter->user_data)->data;
469 while (tmp_column-- > 0 && list)
473 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
475 _gtk_tree_data_list_node_to_value (list,
476 GTK_LIST_STORE (tree_model)->column_headers[column],
481 gtk_list_store_iter_next (GtkTreeModel *tree_model,
484 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
485 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
487 iter->user_data = G_SLIST (iter->user_data)->next;
489 return (iter->user_data != NULL);
493 gtk_list_store_iter_children (GtkTreeModel *tree_model,
497 /* this is a list, nodes have no children */
501 /* but if parent == NULL we return the list itself as children of the
505 if (GTK_LIST_STORE (tree_model)->root)
507 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
508 iter->user_data = GTK_LIST_STORE (tree_model)->root;
516 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
523 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
526 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
528 return GTK_LIST_STORE (tree_model)->length;
530 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
535 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
542 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
547 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
551 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
552 iter->user_data = child;
560 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
567 /* Public accessors */
568 /* This is a somewhat inelegant function that does a lot of list
569 * manipulations on it's own.
573 * gtk_list_store_set_value:
574 * @store: a #GtkListStore
575 * @iter: iterator for the row you're modifying
576 * @column: column number to modify
577 * @value: new value for the cell
579 * Sets the data in the cell specified by @iter and @column.
580 * The type of @value must be convertible to the type of the
585 gtk_list_store_set_value (GtkListStore *list_store,
590 GtkTreeDataList *list;
591 GtkTreeDataList *prev;
593 GValue real_value = {0, };
594 gboolean converted = FALSE;
595 gint orig_column = column;
597 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
598 g_return_if_fail (VALID_ITER (iter, list_store));
599 g_return_if_fail (column >= 0 && column < list_store->n_columns);
600 g_return_if_fail (G_IS_VALUE (value));
602 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
604 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
605 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
607 g_warning ("%s: Unable to convert from %s to %s\n",
609 g_type_name (G_VALUE_TYPE (value)),
610 g_type_name (list_store->column_headers[column]));
613 if (!g_value_transform (value, &real_value))
615 g_warning ("%s: Unable to make conversion from %s to %s\n",
617 g_type_name (G_VALUE_TYPE (value)),
618 g_type_name (list_store->column_headers[column]));
619 g_value_unset (&real_value);
625 prev = list = G_SLIST (iter->user_data)->data;
631 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
633 _gtk_tree_data_list_value_to_node (list, &real_value);
635 _gtk_tree_data_list_value_to_node (list, value);
636 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
637 gtk_tree_path_free (path);
639 g_value_unset (&real_value);
648 if (G_SLIST (iter->user_data)->data == NULL)
650 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
655 list = prev->next = _gtk_tree_data_list_alloc ();
661 list->next = _gtk_tree_data_list_alloc ();
667 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
669 _gtk_tree_data_list_value_to_node (list, &real_value);
671 _gtk_tree_data_list_value_to_node (list, value);
672 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
673 gtk_tree_path_free (path);
675 g_value_unset (&real_value);
677 if (GTK_LIST_STORE_IS_SORTED (list_store))
678 gtk_list_store_sort_iter_changed (list_store, iter, orig_column);
682 * gtk_list_store_set_valist:
683 * @list_store: a #GtkListStore
684 * @iter: row to set data for
685 * @var_args: va_list of column/value pairs
687 * See gtk_list_store_set(); this version takes a va_list for
688 * use by language bindings.
692 gtk_list_store_set_valist (GtkListStore *list_store,
698 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
699 g_return_if_fail (VALID_ITER (iter, list_store));
701 column = va_arg (var_args, gint);
705 GValue value = { 0, };
708 if (column >= list_store->n_columns)
710 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
713 g_value_init (&value, list_store->column_headers[column]);
715 G_VALUE_COLLECT (&value, var_args, 0, &error);
718 g_warning ("%s: %s", G_STRLOC, error);
721 /* we purposely leak the value here, it might not be
722 * in a sane state if an error condition occoured
727 /* FIXME: instead of calling this n times, refactor with above */
728 gtk_list_store_set_value (list_store,
733 g_value_unset (&value);
735 column = va_arg (var_args, gint);
740 * gtk_list_store_set:
741 * @list_store: a #GtkListStore
742 * @iter: row iterator
743 * @Varargs: pairs of column number and value, terminated with -1
745 * Sets the value of one or more cells in the row referenced by @iter.
746 * The variable argument list should contain integer column numbers,
747 * each column number followed by the value to be set.
748 * The list is terminated by a -1. For example, to set column 0 with type
749 * %G_TYPE_STRING to "Foo", you would write gtk_list_store_set (store, iter,
753 gtk_list_store_set (GtkListStore *list_store,
759 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
760 g_return_if_fail (iter != NULL);
761 g_return_if_fail (iter->stamp == list_store->stamp);
763 va_start (var_args, iter);
764 gtk_list_store_set_valist (list_store, iter, var_args);
769 remove_link_saving_prev (GSList *list,
784 prev->next = link->next;
803 gtk_list_store_remove_silently (GtkListStore *list_store,
807 if (G_SLIST (iter->user_data)->data)
809 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
810 list_store->column_headers);
811 G_SLIST (iter->user_data)->data = NULL;
817 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
818 G_SLIST (iter->user_data),
821 list_store->length -= 1;
823 if (iter->user_data == list_store->tail)
824 list_store->tail = prev;
827 list_store->stamp ++;
831 * gtk_list_store_remove:
832 * @store: a #GtkListStore
833 * @iter: a row in @list_store
835 * Removes the given row from the list store, emitting the
836 * "deleted" signal on #GtkTreeModel.
840 gtk_list_store_remove (GtkListStore *list_store,
845 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
846 g_return_if_fail (VALID_ITER (iter, list_store));
848 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
850 validate_list_store (list_store);
852 gtk_list_store_remove_silently (list_store, iter, path);
854 validate_list_store (list_store);
856 list_store->stamp ++;
857 gtk_tree_model_deleted (GTK_TREE_MODEL (list_store), path);
858 gtk_tree_path_free (path);
862 insert_after (GtkListStore *list_store,
866 g_return_if_fail (sibling != NULL);
867 g_return_if_fail (new_list != NULL);
869 /* insert new node after list */
870 new_list->next = sibling->next;
871 sibling->next = new_list;
873 /* if list was the tail, the new node is the new tail */
874 if (sibling == ((GSList *) list_store->tail))
875 list_store->tail = new_list;
877 list_store->length += 1;
881 * gtk_list_store_insert:
882 * @store: a #GtkListStore
883 * @iter: iterator to initialize with the new row
884 * @position: position to insert the new row
886 * Creates a new row at @position, initializing @iter to point to the
887 * new row, and emitting the "inserted" signal from the #GtkTreeModel
892 gtk_list_store_insert (GtkListStore *list_store,
900 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
901 g_return_if_fail (iter != NULL);
902 g_return_if_fail (position >= 0);
905 GTK_LIST_STORE_IS_SORTED (list_store))
907 gtk_list_store_prepend (list_store, iter);
911 new_list = g_slist_alloc ();
913 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
917 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
921 insert_after (list_store, list, new_list);
923 iter->stamp = list_store->stamp;
924 iter->user_data = new_list;
926 validate_list_store (list_store);
928 path = gtk_tree_path_new ();
929 gtk_tree_path_append_index (path, position);
930 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
931 gtk_tree_path_free (path);
935 * gtk_list_store_insert_before:
936 * @store: a #GtkListStore
937 * @iter: iterator to initialize with the new row
938 * @sibling: an existing row
940 * Inserts a new row before @sibling, initializing @iter to point to
941 * the new row, and emitting the "inserted" signal from the
942 * #GtkTreeModel interface.
946 gtk_list_store_insert_before (GtkListStore *list_store,
948 GtkTreeIter *sibling)
951 GSList *list, *prev, *new_list;
954 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
955 g_return_if_fail (iter != NULL);
957 g_return_if_fail (VALID_ITER (sibling, list_store));
960 if (GTK_LIST_STORE_IS_SORTED (list_store))
962 gtk_list_store_prepend (list_store, iter);
968 gtk_list_store_append (list_store, iter);
972 new_list = g_slist_alloc ();
975 list = list_store->root;
976 while (list && list != sibling->user_data)
983 if (list != sibling->user_data)
985 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
989 /* if there are no nodes, we become the list tail, otherwise we
990 * are inserting before any existing nodes so we can't change
994 if (list_store->root == NULL)
995 list_store->tail = new_list;
999 new_list->next = prev->next;
1000 prev->next = new_list;
1004 new_list->next = list_store->root;
1005 list_store->root = new_list;
1008 iter->stamp = list_store->stamp;
1009 iter->user_data = new_list;
1011 list_store->length += 1;
1013 validate_list_store (list_store);
1015 path = gtk_tree_path_new ();
1016 gtk_tree_path_append_index (path, i);
1017 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1018 gtk_tree_path_free (path);
1022 * gtk_list_store_insert_after:
1023 * @store: a #GtkListStore
1024 * @iter: iterator to initialize with the new row
1025 * @sibling: an existing row
1027 * Inserts a new row after @sibling, initializing @iter to point to
1028 * the new row, and emitting the "inserted" signal from the
1029 * #GtkTreeModel interface.
1033 gtk_list_store_insert_after (GtkListStore *list_store,
1035 GtkTreeIter *sibling)
1038 GSList *list, *new_list;
1041 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1042 g_return_if_fail (iter != NULL);
1044 g_return_if_fail (VALID_ITER (sibling, list_store));
1046 if (sibling == NULL ||
1047 GTK_LIST_STORE_IS_SORTED (list_store))
1049 gtk_list_store_prepend (list_store, iter);
1053 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1056 g_return_if_fail (list == sibling->user_data);
1058 new_list = g_slist_alloc ();
1060 insert_after (list_store, list, new_list);
1062 iter->stamp = list_store->stamp;
1063 iter->user_data = new_list;
1065 validate_list_store (list_store);
1067 path = gtk_tree_path_new ();
1068 gtk_tree_path_append_index (path, i);
1069 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1070 gtk_tree_path_free (path);
1074 * gtk_list_store_prepend:
1075 * @store: a #GtkListStore
1076 * @iter: iterator to initialize with new row
1078 * Prepends a row to @store, initializing @iter to point to the
1079 * new row, and emitting the "inserted" signal on the #GtkTreeModel
1080 * interface for the @store.
1084 gtk_list_store_prepend (GtkListStore *list_store,
1089 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1090 g_return_if_fail (iter != NULL);
1092 iter->stamp = list_store->stamp;
1093 iter->user_data = g_slist_alloc ();
1095 if (list_store->root == NULL)
1096 list_store->tail = iter->user_data;
1098 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1099 list_store->root = iter->user_data;
1101 list_store->length += 1;
1103 validate_list_store (list_store);
1105 path = gtk_tree_path_new ();
1106 gtk_tree_path_append_index (path, 0);
1107 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1108 gtk_tree_path_free (path);
1112 * gtk_list_store_append:
1113 * @store: a #GtkListStore
1114 * @iter: iterator to initialize with the new row
1116 * Appends a row to @store, initializing @iter to point to the
1117 * new row, and emitting the "inserted" signal on the #GtkTreeModel
1118 * interface for the @store.
1122 gtk_list_store_append (GtkListStore *list_store,
1127 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1128 g_return_if_fail (iter != NULL);
1130 if (GTK_LIST_STORE_IS_SORTED (list_store))
1132 gtk_list_store_prepend (list_store, iter);
1136 iter->stamp = list_store->stamp;
1137 iter->user_data = g_slist_alloc ();
1139 if (list_store->tail)
1140 ((GSList *)list_store->tail)->next = iter->user_data;
1142 list_store->root = iter->user_data;
1144 list_store->tail = iter->user_data;
1146 list_store->length += 1;
1148 validate_list_store (list_store);
1150 path = gtk_tree_path_new ();
1151 gtk_tree_path_append_index (path, list_store->length - 1);
1152 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1153 gtk_tree_path_free (path);
1157 gtk_list_store_clear (GtkListStore *list_store)
1160 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1162 while (list_store->root)
1164 iter.stamp = list_store->stamp;
1165 iter.user_data = list_store->root;
1166 gtk_list_store_remove (list_store, &iter);
1172 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1176 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1178 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1182 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1189 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1191 GtkSelectionData *selection_data)
1193 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1195 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1196 * target, because the default handler does it for us, but
1197 * we do anyway for the convenience of someone maybe overriding the
1201 if (gtk_selection_data_set_tree_row (selection_data,
1202 GTK_TREE_MODEL (drag_source),
1209 /* FIXME handle text targets at least. */
1216 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1218 GtkSelectionData *selection_data)
1220 GtkTreeModel *tree_model;
1221 GtkListStore *list_store;
1222 GtkTreeModel *src_model = NULL;
1223 GtkTreePath *src_path = NULL;
1224 gboolean retval = FALSE;
1226 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1228 tree_model = GTK_TREE_MODEL (drag_dest);
1229 list_store = GTK_LIST_STORE (drag_dest);
1231 if (gtk_selection_data_get_tree_row (selection_data,
1234 src_model == tree_model)
1236 /* Copy the given row to a new position */
1237 GtkTreeIter src_iter;
1238 GtkTreeIter dest_iter;
1241 if (!gtk_tree_model_get_iter (src_model,
1248 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1249 prev = gtk_tree_path_copy (dest);
1251 if (!gtk_tree_path_prev (prev))
1253 /* dest was the first spot in the list; which means we are supposed
1256 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1263 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1267 GtkTreeIter tmp_iter = dest_iter;
1268 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1275 gtk_tree_path_free (prev);
1277 /* If we succeeded in creating dest_iter, copy data from src
1281 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1282 GtkTreeDataList *copy_head = NULL;
1283 GtkTreeDataList *copy_prev = NULL;
1284 GtkTreeDataList *copy_iter = NULL;
1291 copy_iter = _gtk_tree_data_list_node_copy (dl,
1292 list_store->column_headers[col]);
1294 if (copy_head == NULL)
1295 copy_head = copy_iter;
1298 copy_prev->next = copy_iter;
1300 copy_prev = copy_iter;
1306 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1307 G_SLIST (dest_iter.user_data)->data = copy_head;
1309 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1310 gtk_tree_model_range_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter, path, &dest_iter);
1311 gtk_tree_path_free (path);
1316 /* FIXME maybe add some data targets eventually, or handle text
1317 * targets in the simple case.
1324 gtk_tree_path_free (src_path);
1330 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1331 GtkTreeModel *src_model,
1332 GtkTreePath *src_path,
1333 GtkTreePath *dest_path)
1337 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1339 if (src_model != GTK_TREE_MODEL (drag_dest))
1342 if (gtk_tree_path_get_depth (dest_path) != 1)
1345 /* can drop before any existing node, or before one past any existing. */
1347 indices = gtk_tree_path_get_indices (dest_path);
1349 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1357 typedef struct _SortTuple
1364 gtk_list_store_compare_func (gconstpointer a,
1368 GtkListStore *list_store = user_data;
1369 GtkTreeDataSortHeader *header = NULL;
1370 GSList *el_a; /* Los Angeles? */
1376 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1377 list_store->sort_column_id);
1379 g_return_val_if_fail (header != NULL, 0);
1380 g_return_val_if_fail (header->func != NULL, 0);
1382 el_a = ((SortTuple *) a)->el;
1383 el_b = ((SortTuple *) b)->el;
1385 iter_a.stamp = list_store->stamp;
1386 iter_a.user_data = el_a;
1387 iter_b.stamp = list_store->stamp;
1388 iter_b.user_data = el_b;
1390 retval = (* header->func) (GTK_TREE_MODEL (list_store),
1394 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1398 else if (retval < 0)
1405 gtk_list_store_sort (GtkListStore *list_store)
1407 GtkTreeDataSortHeader *header = NULL;
1415 if (list_store->length <= 1)
1418 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1420 header = _gtk_tree_data_list_get_header (list_store->sort_list, list_store->sort_column_id);
1422 /* We want to make sure that we have a function */
1423 g_return_if_fail (header != NULL);
1424 g_return_if_fail (header->func != NULL);
1426 list = G_SLIST (list_store->root);
1428 sort_array = g_array_sized_new (FALSE, FALSE,
1430 list_store->length);
1432 for (i = 0; i < list_store->length; i++)
1436 /* If this fails, we are in an inconsistent state. Bad */
1437 g_return_if_fail (list != NULL);
1441 g_array_append_val (sort_array, tuple);
1446 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1448 for (i = 0; i < list_store->length - 1; i++)
1449 g_array_index (sort_array, SortTuple, i).el->next =
1450 g_array_index (sort_array, SortTuple, i + 1).el;
1451 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1452 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1454 /* Let the world know about our new order */
1455 new_order = g_new (gint, list_store->length);
1456 for (i = 0; i < list_store->length; i++)
1457 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1458 path = gtk_tree_path_new ();
1459 iter.stamp = list_store->stamp;
1460 iter.user_data = NULL;
1461 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1462 path, &iter, new_order);
1463 gtk_tree_path_free (path);
1465 g_array_free (sort_array, TRUE);
1469 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1474 GtkTreeDataSortHeader *header;
1475 GSList *prev = NULL;
1476 GSList *next = NULL;
1477 GSList *list = G_SLIST (list_store->root);
1478 GtkTreePath *tmp_path;
1479 GtkTreeIter tmp_iter;
1487 if (list_store->length < 2)
1490 tmp_iter.stamp = list_store->stamp;
1491 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1492 list_store->sort_column_id);
1493 g_return_if_fail (header != NULL);
1494 g_return_if_fail (header->func != NULL);
1496 /* If it's the built in function, we don't sort. */
1497 if (header->func == gtk_tree_data_list_compare_func &&
1498 list_store->sort_column_id != column)
1502 /* First we find the iter, its prev, and its next */
1505 if (list == G_SLIST (iter->user_data))
1511 g_assert (list != NULL);
1515 /* Check the common case, where we don't need to sort it moved. */
1518 tmp_iter.user_data = prev;
1519 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1526 tmp_iter.user_data = next;
1527 cmp_b = (* header->func) (GTK_TREE_MODEL (list_store),
1533 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1546 if (prev == NULL && cmp_b <= 0)
1548 else if (next == NULL && cmp_a <= 0)
1550 else if (prev != NULL && next != NULL &&
1551 cmp_a <= 0 && cmp_b <= 0)
1554 /* We actually need to sort it */
1555 /* First, remove the old link. */
1558 list_store->root = next;
1562 list_store->tail = prev;
1565 /* FIXME: as an optimization, we can potentially start at next */
1567 list = G_SLIST (list_store->root);
1569 tmp_iter.user_data = list;
1570 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1571 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1572 &tmp_iter, iter, header->data);
1574 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1575 iter, &tmp_iter, header->data);
1577 while ((list->next) && (cmp_a > 0))
1582 tmp_iter.user_data = list;
1583 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1584 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1585 &tmp_iter, iter, header->data);
1587 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1588 iter, &tmp_iter, header->data);
1591 if ((!list->next) && (cmp_a > 0))
1593 list->next = G_SLIST (iter->user_data);
1594 list_store->tail = list->next;
1598 prev->next = G_SLIST (iter->user_data);
1599 G_SLIST (iter->user_data)->next = list;
1603 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1604 list_store->root = G_SLIST (iter->user_data);
1607 /* Emit the reordered signal. */
1608 new_order = g_new (int, list_store->length);
1609 if (old_location < new_location)
1610 for (i = 0; i < list_store->length; i++)
1612 if (i < old_location ||
1615 else if (i >= old_location &&
1617 new_order[i] = i + 1;
1618 else if (i == new_location)
1619 new_order[i] = old_location;
1622 for (i = 0; i < list_store->length; i++)
1624 if (i < new_location ||
1627 else if (i > new_location &&
1629 new_order[i] = i - 1;
1630 else if (i == new_location)
1631 new_order[i] = old_location;
1634 tmp_path = gtk_tree_path_new ();
1635 tmp_iter.user_data = NULL;
1637 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1638 tmp_path, &tmp_iter,
1641 gtk_tree_path_free (tmp_path);
1646 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1647 gint *sort_column_id,
1648 GtkTreeSortOrder *order)
1650 GtkListStore *list_store = (GtkListStore *) sortable;
1652 g_return_val_if_fail (sortable != NULL, FALSE);
1653 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1655 if (list_store->sort_column_id == -1)
1659 * sort_column_id = list_store->sort_column_id;
1661 * order = list_store->order;
1666 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1667 gint sort_column_id,
1668 GtkTreeSortOrder order)
1670 GtkListStore *list_store = (GtkListStore *) sortable;
1673 g_return_if_fail (sortable != NULL);
1674 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1676 for (list = list_store->sort_list; list; list = list->next)
1678 GtkTreeDataSortHeader *header = (GtkTreeDataSortHeader*) list->data;
1679 if (header->sort_column_id == sort_column_id)
1682 g_return_if_fail (list != NULL);
1684 if ((list_store->sort_column_id == sort_column_id) &&
1685 (list_store->order == order))
1688 list_store->sort_column_id = sort_column_id;
1689 list_store->order = order;
1691 if (list_store->sort_column_id >= 0)
1692 gtk_list_store_sort (list_store);
1694 gtk_tree_sortable_sort_column_changed (sortable);
1698 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
1699 gint sort_column_id,
1700 GtkTreeIterCompareFunc func,
1702 GtkDestroyNotify destroy)
1704 GtkListStore *list_store = (GtkListStore *) sortable;
1705 GtkTreeDataSortHeader *header = NULL;
1708 g_return_if_fail (sortable != NULL);
1709 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1710 g_return_if_fail (func != NULL);
1712 for (list = list_store->sort_list; list; list = list->next)
1714 header = (GtkTreeDataSortHeader*) list->data;
1715 if (header->sort_column_id == sort_column_id)
1721 header = g_new0 (GtkTreeDataSortHeader, 1);
1722 header->sort_column_id = sort_column_id;
1723 list_store->sort_list = g_list_append (list_store->sort_list, header);
1726 if (header->destroy)
1727 (* header->destroy) (header->data);
1729 header->func = func;
1730 header->data = data;
1731 header->destroy = destroy;