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,
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);
116 validate_list_store (GtkListStore *list_store)
118 if (gtk_debug_flags & GTK_DEBUG_TREE)
120 g_assert (g_slist_length (list_store->root) == list_store->length);
122 g_assert (g_slist_last (list_store->root) == list_store->tail);
127 gtk_list_store_get_type (void)
129 static GType list_store_type = 0;
131 if (!list_store_type)
133 static const GTypeInfo list_store_info =
135 sizeof (GtkListStoreClass),
136 NULL, /* base_init */
137 NULL, /* base_finalize */
138 (GClassInitFunc) gtk_list_store_class_init,
139 NULL, /* class_finalize */
140 NULL, /* class_data */
141 sizeof (GtkListStore),
143 (GInstanceInitFunc) gtk_list_store_init,
146 static const GInterfaceInfo tree_model_info =
148 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
153 static const GInterfaceInfo drag_source_info =
155 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
160 static const GInterfaceInfo drag_dest_info =
162 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
167 static const GInterfaceInfo sortable_info =
169 (GInterfaceInitFunc) gtk_list_store_sortable_init,
174 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
175 g_type_add_interface_static (list_store_type,
178 g_type_add_interface_static (list_store_type,
179 GTK_TYPE_TREE_DRAG_SOURCE,
181 g_type_add_interface_static (list_store_type,
182 GTK_TYPE_TREE_DRAG_DEST,
184 g_type_add_interface_static (list_store_type,
185 GTK_TYPE_TREE_SORTABLE,
189 return list_store_type;
193 gtk_list_store_class_init (GtkListStoreClass *class)
195 GObjectClass *object_class;
197 object_class = (GObjectClass*) class;
201 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
203 iface->get_flags = gtk_list_store_get_flags;
204 iface->get_n_columns = gtk_list_store_get_n_columns;
205 iface->get_column_type = gtk_list_store_get_column_type;
206 iface->get_iter = gtk_list_store_get_iter;
207 iface->get_path = gtk_list_store_get_path;
208 iface->get_value = gtk_list_store_get_value;
209 iface->iter_next = gtk_list_store_iter_next;
210 iface->iter_children = gtk_list_store_iter_children;
211 iface->iter_has_child = gtk_list_store_iter_has_child;
212 iface->iter_n_children = gtk_list_store_iter_n_children;
213 iface->iter_nth_child = gtk_list_store_iter_nth_child;
214 iface->iter_parent = gtk_list_store_iter_parent;
218 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
220 iface->drag_data_delete = gtk_list_store_drag_data_delete;
221 iface->drag_data_get = gtk_list_store_drag_data_get;
225 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
227 iface->drag_data_received = gtk_list_store_drag_data_received;
228 iface->row_drop_possible = gtk_list_store_row_drop_possible;
232 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
234 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
235 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
236 iface->set_sort_func = gtk_list_store_set_sort_func;
237 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
238 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
242 gtk_list_store_init (GtkListStore *list_store)
244 list_store->root = NULL;
245 list_store->tail = NULL;
246 list_store->sort_list = NULL;
247 list_store->stamp = g_random_int ();
248 list_store->length = 0;
249 list_store->sort_column_id = -1;
253 * gtk_list_store_new:
254 * @n_columns: number of columns in the list store
255 * @Varargs: all #GType types for the columns, from first to last
257 * Creates a new list store as with @n_columns columns each of the types passed
258 * in. As an example, gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
259 * GDK_TYPE_PIXBUF); will create a new GtkListStore with three columns, of type
260 * int, string and GDkPixbuf respectively.
262 * Return value: a new #GtkListStore
265 gtk_list_store_new (gint n_columns,
268 GtkListStore *retval;
272 g_return_val_if_fail (n_columns > 0, NULL);
274 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
275 gtk_list_store_set_n_columns (retval, n_columns);
277 va_start (args, n_columns);
279 for (i = 0; i < n_columns; i++)
281 GType type = va_arg (args, GType);
282 if (! _gtk_tree_data_list_check_type (type))
284 g_warning ("%s: Invalid type %s passed to gtk_list_store_new\n", G_STRLOC, g_type_name (type));
285 g_object_unref (G_OBJECT (retval));
289 gtk_list_store_set_column_type (retval, i, type);
299 * gtk_list_store_newv:
300 * @n_columns: number of columns in the list store
301 * @types: an array of #GType types for the columns, from first to last
303 * Non vararg creation function. Used primarily by language bindings.
305 * Return value: a new #GtkListStore
308 gtk_list_store_newv (gint n_columns,
311 GtkListStore *retval;
314 g_return_val_if_fail (n_columns > 0, NULL);
316 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
317 gtk_list_store_set_n_columns (retval, n_columns);
319 for (i = 0; i < n_columns; i++)
321 if (! _gtk_tree_data_list_check_type (types[i]))
323 g_warning ("%s: Invalid type %s passed to gtk_list_store_newv\n", G_STRLOC, g_type_name (types[i]));
324 g_object_unref (G_OBJECT (retval));
328 gtk_list_store_set_column_type (retval, i, types[i]);
335 gtk_list_store_set_n_columns (GtkListStore *list_store,
340 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
341 g_return_if_fail (n_columns > 0);
343 if (list_store->n_columns == n_columns)
346 new_columns = g_new0 (GType, n_columns);
347 if (list_store->column_headers)
349 /* copy the old header orders over */
350 if (n_columns >= list_store->n_columns)
351 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
353 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
355 g_free (list_store->column_headers);
358 if (list_store->sort_list)
359 _gtk_tree_data_list_header_free (list_store->sort_list);
361 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
363 list_store->column_headers = new_columns;
364 list_store->n_columns = n_columns;
368 gtk_list_store_set_column_type (GtkListStore *list_store,
372 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
373 g_return_if_fail (column >=0 && column < list_store->n_columns);
374 if (!_gtk_tree_data_list_check_type (type))
376 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_type\n", G_STRLOC, g_type_name (type));
380 list_store->column_headers[column] = type;
383 /* Fulfill the GtkTreeModel requirements */
385 gtk_list_store_get_flags (GtkTreeModel *tree_model)
387 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
389 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
393 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
395 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
397 return GTK_LIST_STORE (tree_model)->n_columns;
401 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
404 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
405 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
406 index >= 0, G_TYPE_INVALID);
408 return GTK_LIST_STORE (tree_model)->column_headers[index];
412 gtk_list_store_get_iter (GtkTreeModel *tree_model,
419 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
420 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
422 i = gtk_tree_path_get_indices (path)[0];
424 if (i >= GTK_LIST_STORE (tree_model)->length)
427 list = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), i);
429 /* If this fails, list_store->length has gotten mangled. */
432 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
433 iter->user_data = list;
438 gtk_list_store_get_path (GtkTreeModel *tree_model,
445 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
446 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
448 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
450 if (list == G_SLIST (iter->user_data))
457 retval = gtk_tree_path_new ();
458 gtk_tree_path_append_index (retval, i);
463 gtk_list_store_get_value (GtkTreeModel *tree_model,
468 GtkTreeDataList *list;
469 gint tmp_column = column;
471 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
472 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
473 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
475 list = G_SLIST (iter->user_data)->data;
477 while (tmp_column-- > 0 && list)
481 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
483 _gtk_tree_data_list_node_to_value (list,
484 GTK_LIST_STORE (tree_model)->column_headers[column],
489 gtk_list_store_iter_next (GtkTreeModel *tree_model,
492 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
493 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
495 iter->user_data = G_SLIST (iter->user_data)->next;
497 return (iter->user_data != NULL);
501 gtk_list_store_iter_children (GtkTreeModel *tree_model,
505 /* this is a list, nodes have no children */
509 /* but if parent == NULL we return the list itself as children of the
513 if (GTK_LIST_STORE (tree_model)->root)
515 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
516 iter->user_data = GTK_LIST_STORE (tree_model)->root;
524 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
531 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
534 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
536 return GTK_LIST_STORE (tree_model)->length;
538 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
543 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
550 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
555 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
559 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
560 iter->user_data = child;
568 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
575 /* Public accessors */
576 /* This is a somewhat inelegant function that does a lot of list
577 * manipulations on it's own.
581 * gtk_list_store_set_value:
582 * @store: a #GtkListStore
583 * @iter: iterator for the row you're modifying
584 * @column: column number to modify
585 * @value: new value for the cell
587 * Sets the data in the cell specified by @iter and @column.
588 * The type of @value must be convertible to the type of the
593 gtk_list_store_set_value (GtkListStore *list_store,
598 GtkTreeDataList *list;
599 GtkTreeDataList *prev;
601 GValue real_value = {0, };
602 gboolean converted = FALSE;
603 gint orig_column = column;
605 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
606 g_return_if_fail (VALID_ITER (iter, list_store));
607 g_return_if_fail (column >= 0 && column < list_store->n_columns);
608 g_return_if_fail (G_IS_VALUE (value));
610 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
612 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
613 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
615 g_warning ("%s: Unable to convert from %s to %s\n",
617 g_type_name (G_VALUE_TYPE (value)),
618 g_type_name (list_store->column_headers[column]));
621 if (!g_value_transform (value, &real_value))
623 g_warning ("%s: Unable to make conversion from %s to %s\n",
625 g_type_name (G_VALUE_TYPE (value)),
626 g_type_name (list_store->column_headers[column]));
627 g_value_unset (&real_value);
633 prev = list = G_SLIST (iter->user_data)->data;
639 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
641 _gtk_tree_data_list_value_to_node (list, &real_value);
643 _gtk_tree_data_list_value_to_node (list, value);
644 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
645 gtk_tree_path_free (path);
647 g_value_unset (&real_value);
656 if (G_SLIST (iter->user_data)->data == NULL)
658 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
663 list = prev->next = _gtk_tree_data_list_alloc ();
669 list->next = _gtk_tree_data_list_alloc ();
675 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
677 _gtk_tree_data_list_value_to_node (list, &real_value);
679 _gtk_tree_data_list_value_to_node (list, value);
680 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
681 gtk_tree_path_free (path);
683 g_value_unset (&real_value);
685 if (GTK_LIST_STORE_IS_SORTED (list_store))
686 gtk_list_store_sort_iter_changed (list_store, iter, orig_column);
690 * gtk_list_store_set_valist:
691 * @list_store: a #GtkListStore
692 * @iter: row to set data for
693 * @var_args: va_list of column/value pairs
695 * See gtk_list_store_set(); this version takes a va_list for
696 * use by language bindings.
700 gtk_list_store_set_valist (GtkListStore *list_store,
706 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
707 g_return_if_fail (VALID_ITER (iter, list_store));
709 column = va_arg (var_args, gint);
713 GValue value = { 0, };
716 if (column >= list_store->n_columns)
718 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
721 g_value_init (&value, list_store->column_headers[column]);
723 G_VALUE_COLLECT (&value, var_args, 0, &error);
726 g_warning ("%s: %s", G_STRLOC, error);
729 /* we purposely leak the value here, it might not be
730 * in a sane state if an error condition occoured
735 /* FIXME: instead of calling this n times, refactor with above */
736 gtk_list_store_set_value (list_store,
741 g_value_unset (&value);
743 column = va_arg (var_args, gint);
748 * gtk_list_store_set:
749 * @list_store: a #GtkListStore
750 * @iter: row iterator
751 * @Varargs: pairs of column number and value, terminated with -1
753 * Sets the value of one or more cells in the row referenced by @iter.
754 * The variable argument list should contain integer column numbers,
755 * each column number followed by the value to be set.
756 * The list is terminated by a -1. For example, to set column 0 with type
757 * %G_TYPE_STRING to "Foo", you would write gtk_list_store_set (store, iter,
761 gtk_list_store_set (GtkListStore *list_store,
767 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
768 g_return_if_fail (iter != NULL);
769 g_return_if_fail (iter->stamp == list_store->stamp);
771 va_start (var_args, iter);
772 gtk_list_store_set_valist (list_store, iter, var_args);
777 remove_link_saving_prev (GSList *list,
792 prev->next = link->next;
811 gtk_list_store_remove_silently (GtkListStore *list_store,
815 if (G_SLIST (iter->user_data)->data)
817 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
818 list_store->column_headers);
819 G_SLIST (iter->user_data)->data = NULL;
825 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
826 G_SLIST (iter->user_data),
829 list_store->length -= 1;
831 if (iter->user_data == list_store->tail)
832 list_store->tail = prev;
835 list_store->stamp ++;
839 * gtk_list_store_remove:
840 * @store: a #GtkListStore
841 * @iter: a row in @list_store
843 * Removes the given row from the list store, emitting the
844 * "deleted" signal on #GtkTreeModel.
848 gtk_list_store_remove (GtkListStore *list_store,
853 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
854 g_return_if_fail (VALID_ITER (iter, list_store));
856 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
858 validate_list_store (list_store);
860 gtk_list_store_remove_silently (list_store, iter, path);
862 validate_list_store (list_store);
864 list_store->stamp ++;
865 gtk_tree_model_deleted (GTK_TREE_MODEL (list_store), path);
866 gtk_tree_path_free (path);
870 insert_after (GtkListStore *list_store,
874 g_return_if_fail (sibling != NULL);
875 g_return_if_fail (new_list != NULL);
877 /* insert new node after list */
878 new_list->next = sibling->next;
879 sibling->next = new_list;
881 /* if list was the tail, the new node is the new tail */
882 if (sibling == ((GSList *) list_store->tail))
883 list_store->tail = new_list;
885 list_store->length += 1;
889 * gtk_list_store_insert:
890 * @store: a #GtkListStore
891 * @iter: iterator to initialize with the new row
892 * @position: position to insert the new row
894 * Creates a new row at @position, initializing @iter to point to the
895 * new row, and emitting the "inserted" signal from the #GtkTreeModel
900 gtk_list_store_insert (GtkListStore *list_store,
908 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
909 g_return_if_fail (iter != NULL);
910 g_return_if_fail (position >= 0);
913 GTK_LIST_STORE_IS_SORTED (list_store))
915 gtk_list_store_prepend (list_store, iter);
919 new_list = g_slist_alloc ();
921 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
925 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
929 insert_after (list_store, list, new_list);
931 iter->stamp = list_store->stamp;
932 iter->user_data = new_list;
934 validate_list_store (list_store);
936 path = gtk_tree_path_new ();
937 gtk_tree_path_append_index (path, position);
938 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
939 gtk_tree_path_free (path);
943 * gtk_list_store_insert_before:
944 * @store: a #GtkListStore
945 * @iter: iterator to initialize with the new row
946 * @sibling: an existing row
948 * Inserts a new row before @sibling, initializing @iter to point to
949 * the new row, and emitting the "inserted" signal from the
950 * #GtkTreeModel interface.
954 gtk_list_store_insert_before (GtkListStore *list_store,
956 GtkTreeIter *sibling)
959 GSList *list, *prev, *new_list;
962 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
963 g_return_if_fail (iter != NULL);
965 g_return_if_fail (VALID_ITER (sibling, list_store));
968 if (GTK_LIST_STORE_IS_SORTED (list_store))
970 gtk_list_store_prepend (list_store, iter);
976 gtk_list_store_append (list_store, iter);
980 new_list = g_slist_alloc ();
983 list = list_store->root;
984 while (list && list != sibling->user_data)
991 if (list != sibling->user_data)
993 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
997 /* if there are no nodes, we become the list tail, otherwise we
998 * are inserting before any existing nodes so we can't change
1002 if (list_store->root == NULL)
1003 list_store->tail = new_list;
1007 new_list->next = prev->next;
1008 prev->next = new_list;
1012 new_list->next = list_store->root;
1013 list_store->root = new_list;
1016 iter->stamp = list_store->stamp;
1017 iter->user_data = new_list;
1019 list_store->length += 1;
1021 validate_list_store (list_store);
1023 path = gtk_tree_path_new ();
1024 gtk_tree_path_append_index (path, i);
1025 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1026 gtk_tree_path_free (path);
1030 * gtk_list_store_insert_after:
1031 * @store: a #GtkListStore
1032 * @iter: iterator to initialize with the new row
1033 * @sibling: an existing row
1035 * Inserts a new row after @sibling, initializing @iter to point to
1036 * the new row, and emitting the "inserted" signal from the
1037 * #GtkTreeModel interface.
1041 gtk_list_store_insert_after (GtkListStore *list_store,
1043 GtkTreeIter *sibling)
1046 GSList *list, *new_list;
1049 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1050 g_return_if_fail (iter != NULL);
1052 g_return_if_fail (VALID_ITER (sibling, list_store));
1054 if (sibling == NULL ||
1055 GTK_LIST_STORE_IS_SORTED (list_store))
1057 gtk_list_store_prepend (list_store, iter);
1061 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1064 g_return_if_fail (list == sibling->user_data);
1066 new_list = g_slist_alloc ();
1068 insert_after (list_store, list, new_list);
1070 iter->stamp = list_store->stamp;
1071 iter->user_data = new_list;
1073 validate_list_store (list_store);
1075 path = gtk_tree_path_new ();
1076 gtk_tree_path_append_index (path, i);
1077 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1078 gtk_tree_path_free (path);
1082 * gtk_list_store_prepend:
1083 * @store: a #GtkListStore
1084 * @iter: iterator to initialize with new row
1086 * Prepends a row to @store, initializing @iter to point to the
1087 * new row, and emitting the "inserted" signal on the #GtkTreeModel
1088 * interface for the @store.
1092 gtk_list_store_prepend (GtkListStore *list_store,
1097 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1098 g_return_if_fail (iter != NULL);
1100 iter->stamp = list_store->stamp;
1101 iter->user_data = g_slist_alloc ();
1103 if (list_store->root == NULL)
1104 list_store->tail = iter->user_data;
1106 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1107 list_store->root = iter->user_data;
1109 list_store->length += 1;
1111 validate_list_store (list_store);
1113 path = gtk_tree_path_new ();
1114 gtk_tree_path_append_index (path, 0);
1115 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1116 gtk_tree_path_free (path);
1120 * gtk_list_store_append:
1121 * @store: a #GtkListStore
1122 * @iter: iterator to initialize with the new row
1124 * Appends a row to @store, initializing @iter to point to the
1125 * new row, and emitting the "inserted" signal on the #GtkTreeModel
1126 * interface for the @store.
1130 gtk_list_store_append (GtkListStore *list_store,
1135 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1136 g_return_if_fail (iter != NULL);
1138 if (GTK_LIST_STORE_IS_SORTED (list_store))
1140 gtk_list_store_prepend (list_store, iter);
1144 iter->stamp = list_store->stamp;
1145 iter->user_data = g_slist_alloc ();
1147 if (list_store->tail)
1148 ((GSList *)list_store->tail)->next = iter->user_data;
1150 list_store->root = iter->user_data;
1152 list_store->tail = iter->user_data;
1154 list_store->length += 1;
1156 validate_list_store (list_store);
1158 path = gtk_tree_path_new ();
1159 gtk_tree_path_append_index (path, list_store->length - 1);
1160 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1161 gtk_tree_path_free (path);
1165 gtk_list_store_clear (GtkListStore *list_store)
1168 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1170 while (list_store->root)
1172 iter.stamp = list_store->stamp;
1173 iter.user_data = list_store->root;
1174 gtk_list_store_remove (list_store, &iter);
1180 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1184 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1186 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1190 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1197 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1199 GtkSelectionData *selection_data)
1201 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1203 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1204 * target, because the default handler does it for us, but
1205 * we do anyway for the convenience of someone maybe overriding the
1209 if (gtk_selection_data_set_tree_row (selection_data,
1210 GTK_TREE_MODEL (drag_source),
1217 /* FIXME handle text targets at least. */
1224 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1226 GtkSelectionData *selection_data)
1228 GtkTreeModel *tree_model;
1229 GtkListStore *list_store;
1230 GtkTreeModel *src_model = NULL;
1231 GtkTreePath *src_path = NULL;
1232 gboolean retval = FALSE;
1234 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1236 tree_model = GTK_TREE_MODEL (drag_dest);
1237 list_store = GTK_LIST_STORE (drag_dest);
1239 if (gtk_selection_data_get_tree_row (selection_data,
1242 src_model == tree_model)
1244 /* Copy the given row to a new position */
1245 GtkTreeIter src_iter;
1246 GtkTreeIter dest_iter;
1249 if (!gtk_tree_model_get_iter (src_model,
1256 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1257 prev = gtk_tree_path_copy (dest);
1259 if (!gtk_tree_path_prev (prev))
1261 /* dest was the first spot in the list; which means we are supposed
1264 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1271 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1275 GtkTreeIter tmp_iter = dest_iter;
1276 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1283 gtk_tree_path_free (prev);
1285 /* If we succeeded in creating dest_iter, copy data from src
1289 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1290 GtkTreeDataList *copy_head = NULL;
1291 GtkTreeDataList *copy_prev = NULL;
1292 GtkTreeDataList *copy_iter = NULL;
1299 copy_iter = _gtk_tree_data_list_node_copy (dl,
1300 list_store->column_headers[col]);
1302 if (copy_head == NULL)
1303 copy_head = copy_iter;
1306 copy_prev->next = copy_iter;
1308 copy_prev = copy_iter;
1314 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1315 G_SLIST (dest_iter.user_data)->data = copy_head;
1317 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1318 gtk_tree_model_range_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter, path, &dest_iter);
1319 gtk_tree_path_free (path);
1324 /* FIXME maybe add some data targets eventually, or handle text
1325 * targets in the simple case.
1332 gtk_tree_path_free (src_path);
1338 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1339 GtkTreeModel *src_model,
1340 GtkTreePath *src_path,
1341 GtkTreePath *dest_path)
1345 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1347 if (src_model != GTK_TREE_MODEL (drag_dest))
1350 if (gtk_tree_path_get_depth (dest_path) != 1)
1353 /* can drop before any existing node, or before one past any existing. */
1355 indices = gtk_tree_path_get_indices (dest_path);
1357 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1365 typedef struct _SortTuple
1372 gtk_list_store_compare_func (gconstpointer a,
1376 GtkListStore *list_store = user_data;
1377 GSList *el_a; /* Los Angeles? */
1382 GtkTreeIterCompareFunc func;
1386 if (list_store->sort_column_id != -1)
1388 GtkTreeDataSortHeader *header;
1390 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1391 list_store->sort_column_id);
1392 g_return_val_if_fail (header != NULL, 0);
1393 g_return_val_if_fail (header->func != NULL, 0);
1395 func = header->func;
1396 data = header->data;
1400 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1401 func = list_store->default_sort_func;
1402 data = list_store->default_sort_data;
1405 el_a = ((SortTuple *) a)->el;
1406 el_b = ((SortTuple *) b)->el;
1408 iter_a.stamp = list_store->stamp;
1409 iter_a.user_data = el_a;
1410 iter_b.stamp = list_store->stamp;
1411 iter_b.user_data = el_b;
1413 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1415 if (list_store->order == GTK_SORT_DESCENDING)
1419 else if (retval < 0)
1426 gtk_list_store_sort (GtkListStore *list_store)
1435 if (list_store->length <= 1)
1438 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1440 list = G_SLIST (list_store->root);
1442 sort_array = g_array_sized_new (FALSE, FALSE,
1444 list_store->length);
1446 for (i = 0; i < list_store->length; i++)
1450 /* If this fails, we are in an inconsistent state. Bad */
1451 g_return_if_fail (list != NULL);
1455 g_array_append_val (sort_array, tuple);
1460 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1462 for (i = 0; i < list_store->length - 1; i++)
1463 g_array_index (sort_array, SortTuple, i).el->next =
1464 g_array_index (sort_array, SortTuple, i + 1).el;
1465 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1466 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1468 /* Let the world know about our new order */
1469 new_order = g_new (gint, list_store->length);
1470 for (i = 0; i < list_store->length; i++)
1471 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1472 path = gtk_tree_path_new ();
1473 iter.stamp = list_store->stamp;
1474 iter.user_data = NULL;
1475 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1476 path, &iter, new_order);
1477 gtk_tree_path_free (path);
1479 g_array_free (sort_array, TRUE);
1483 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1488 GSList *prev = NULL;
1489 GSList *next = NULL;
1490 GSList *list = G_SLIST (list_store->root);
1491 GtkTreePath *tmp_path;
1492 GtkTreeIter tmp_iter;
1499 GtkTreeIterCompareFunc func;
1502 if (list_store->length < 2)
1505 tmp_iter.stamp = list_store->stamp;
1507 if (list_store->sort_column_id != -1)
1509 GtkTreeDataSortHeader *header;
1510 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1511 list_store->sort_column_id);
1512 g_return_if_fail (header != NULL);
1513 g_return_if_fail (header->func != NULL);
1514 func = header->func;
1515 data = header->data;
1519 g_return_if_fail (list_store->default_sort_func != NULL);
1520 func = list_store->default_sort_func;
1521 data = list_store->default_sort_data;
1524 /* If it's the built in function, we don't sort. */
1525 if (func == gtk_tree_data_list_compare_func &&
1526 list_store->sort_column_id != column)
1530 /* First we find the iter, its prev, and its next */
1533 if (list == G_SLIST (iter->user_data))
1539 g_assert (list != NULL);
1543 /* Check the common case, where we don't need to sort it moved. */
1546 tmp_iter.user_data = prev;
1547 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1552 tmp_iter.user_data = next;
1553 cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1557 if (list_store->order == GTK_SORT_DESCENDING)
1570 if (prev == NULL && cmp_b <= 0)
1572 else if (next == NULL && cmp_a <= 0)
1574 else if (prev != NULL && next != NULL &&
1575 cmp_a <= 0 && cmp_b <= 0)
1578 /* We actually need to sort it */
1579 /* First, remove the old link. */
1582 list_store->root = next;
1586 list_store->tail = prev;
1589 /* FIXME: as an optimization, we can potentially start at next */
1591 list = G_SLIST (list_store->root);
1593 tmp_iter.user_data = list;
1594 if (list_store->order == GTK_SORT_DESCENDING)
1595 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1597 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1599 while ((list->next) && (cmp_a > 0))
1604 tmp_iter.user_data = list;
1605 if (list_store->order == GTK_SORT_DESCENDING)
1606 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1608 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1611 if ((!list->next) && (cmp_a > 0))
1613 list->next = G_SLIST (iter->user_data);
1614 list_store->tail = list->next;
1618 prev->next = G_SLIST (iter->user_data);
1619 G_SLIST (iter->user_data)->next = list;
1623 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1624 list_store->root = G_SLIST (iter->user_data);
1627 /* Emit the reordered signal. */
1628 new_order = g_new (int, list_store->length);
1629 if (old_location < new_location)
1630 for (i = 0; i < list_store->length; i++)
1632 if (i < old_location ||
1635 else if (i >= old_location &&
1637 new_order[i] = i + 1;
1638 else if (i == new_location)
1639 new_order[i] = old_location;
1642 for (i = 0; i < list_store->length; i++)
1644 if (i < new_location ||
1647 else if (i > new_location &&
1649 new_order[i] = i - 1;
1650 else if (i == new_location)
1651 new_order[i] = old_location;
1654 tmp_path = gtk_tree_path_new ();
1655 tmp_iter.user_data = NULL;
1657 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1658 tmp_path, &tmp_iter,
1661 gtk_tree_path_free (tmp_path);
1666 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1667 gint *sort_column_id,
1670 GtkListStore *list_store = (GtkListStore *) sortable;
1672 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1674 if (list_store->sort_column_id == -1)
1678 * sort_column_id = list_store->sort_column_id;
1680 * order = list_store->order;
1685 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1686 gint sort_column_id,
1689 GtkListStore *list_store = (GtkListStore *) sortable;
1691 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1693 if ((list_store->sort_column_id == sort_column_id) &&
1694 (list_store->order == order))
1697 if (sort_column_id != -1)
1699 GtkTreeDataSortHeader *header = NULL;
1701 header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
1703 /* We want to make sure that we have a function */
1704 g_return_if_fail (header != NULL);
1705 g_return_if_fail (header->func != NULL);
1709 g_return_if_fail (list_store->default_sort_func != NULL);
1713 list_store->sort_column_id = sort_column_id;
1714 list_store->order = order;
1716 gtk_list_store_sort (list_store);
1718 gtk_tree_sortable_sort_column_changed (sortable);
1722 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
1723 gint sort_column_id,
1724 GtkTreeIterCompareFunc func,
1726 GtkDestroyNotify destroy)
1728 GtkListStore *list_store = (GtkListStore *) sortable;
1729 GtkTreeDataSortHeader *header = NULL;
1732 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1733 g_return_if_fail (func != NULL);
1735 for (list = list_store->sort_list; list; list = list->next)
1737 header = (GtkTreeDataSortHeader*) list->data;
1738 if (header->sort_column_id == sort_column_id)
1744 header = g_new0 (GtkTreeDataSortHeader, 1);
1745 header->sort_column_id = sort_column_id;
1746 list_store->sort_list = g_list_append (list_store->sort_list, header);
1749 if (header->destroy)
1750 (* header->destroy) (header->data);
1752 header->func = func;
1753 header->data = data;
1754 header->destroy = destroy;
1759 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
1760 GtkTreeIterCompareFunc func,
1762 GtkDestroyNotify destroy)
1764 GtkListStore *list_store = (GtkListStore *) sortable;
1766 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1768 if (list_store->default_sort_destroy)
1769 (* list_store->default_sort_destroy) (list_store->default_sort_data);
1771 list_store->default_sort_func = func;
1772 list_store->default_sort_data = data;
1773 list_store->default_sort_destroy = destroy;
1777 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
1779 GtkListStore *list_store = (GtkListStore *) sortable;
1781 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1783 return (list_store->default_sort_func != NULL);