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)
31 static void gtk_list_store_init (GtkListStore *list_store);
32 static void gtk_list_store_class_init (GtkListStoreClass *class);
33 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
34 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
35 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
36 static void gtk_list_store_sortable_init (GtkTreeSortableIface *iface);
37 static guint gtk_list_store_get_flags (GtkTreeModel *tree_model);
38 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
39 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
41 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
44 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
46 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
50 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
52 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
55 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
57 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
59 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
63 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
69 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
71 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
73 GtkSelectionData *selection_data);
74 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
76 GtkSelectionData *selection_data);
77 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
78 GtkTreeModel *src_model,
79 GtkTreePath *src_path,
80 GtkTreePath *dest_path);
84 static void gtk_list_store_sort (GtkListStore *list_store);
85 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
88 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
90 GtkTreeSortOrder *order);
91 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
93 GtkTreeSortOrder order);
94 static void gtk_list_store_sort_column_id_set_func (GtkTreeSortable *sortable,
96 GtkTreeIterCompareFunc func,
98 GtkDestroyNotify destroy);
102 validate_list_store (GtkListStore *list_store)
104 if (gtk_debug_flags & GTK_DEBUG_TREE)
106 g_assert (g_slist_length (list_store->root) == list_store->length);
108 g_assert (g_slist_last (list_store->root) == list_store->tail);
113 gtk_list_store_get_type (void)
115 static GType list_store_type = 0;
117 if (!list_store_type)
119 static const GTypeInfo list_store_info =
121 sizeof (GtkListStoreClass),
122 NULL, /* base_init */
123 NULL, /* base_finalize */
124 (GClassInitFunc) gtk_list_store_class_init,
125 NULL, /* class_finalize */
126 NULL, /* class_data */
127 sizeof (GtkListStore),
129 (GInstanceInitFunc) gtk_list_store_init,
132 static const GInterfaceInfo tree_model_info =
134 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
139 static const GInterfaceInfo drag_source_info =
141 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
146 static const GInterfaceInfo drag_dest_info =
148 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
153 static const GInterfaceInfo sortable_info =
155 (GInterfaceInitFunc) gtk_list_store_sortable_init,
160 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
161 g_type_add_interface_static (list_store_type,
164 g_type_add_interface_static (list_store_type,
165 GTK_TYPE_TREE_DRAG_SOURCE,
167 g_type_add_interface_static (list_store_type,
168 GTK_TYPE_TREE_DRAG_DEST,
170 g_type_add_interface_static (list_store_type,
171 GTK_TYPE_TREE_SORTABLE,
175 return list_store_type;
179 gtk_list_store_class_init (GtkListStoreClass *class)
181 GObjectClass *object_class;
183 object_class = (GObjectClass*) class;
187 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
189 iface->get_flags = gtk_list_store_get_flags;
190 iface->get_n_columns = gtk_list_store_get_n_columns;
191 iface->get_column_type = gtk_list_store_get_column_type;
192 iface->get_iter = gtk_list_store_get_iter;
193 iface->get_path = gtk_list_store_get_path;
194 iface->get_value = gtk_list_store_get_value;
195 iface->iter_next = gtk_list_store_iter_next;
196 iface->iter_children = gtk_list_store_iter_children;
197 iface->iter_has_child = gtk_list_store_iter_has_child;
198 iface->iter_n_children = gtk_list_store_iter_n_children;
199 iface->iter_nth_child = gtk_list_store_iter_nth_child;
200 iface->iter_parent = gtk_list_store_iter_parent;
204 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
206 iface->drag_data_delete = gtk_list_store_drag_data_delete;
207 iface->drag_data_get = gtk_list_store_drag_data_get;
211 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
213 iface->drag_data_received = gtk_list_store_drag_data_received;
214 iface->row_drop_possible = gtk_list_store_row_drop_possible;
218 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
220 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
221 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
222 iface->sort_column_id_set_func = gtk_list_store_sort_column_id_set_func;
226 gtk_list_store_init (GtkListStore *list_store)
228 list_store->root = NULL;
229 list_store->tail = NULL;
230 list_store->sort_list = NULL;
231 list_store->stamp = g_random_int ();
232 list_store->length = 0;
233 list_store->sort_column_id = -1;
237 * gtk_list_store_new:
239 * Creates a new #GtkListStore. A #GtkListStore implements the
240 * #GtkTreeModel interface, and stores a linked list of
241 * rows; each row can have any number of columns. Columns are of uniform type,
242 * i.e. all cells in a column have the same type such as #G_TYPE_STRING or
243 * #GDK_TYPE_PIXBUF. Use #GtkListStore to store data to be displayed in a
246 * Return value: a new #GtkListStore
249 gtk_list_store_new (void)
251 return GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
255 * gtk_list_store_new_with_types:
256 * @n_columns: number of columns in the list store
257 * @Varargs: all #GType types for the columns, from first to last
259 * Creates a new list store as with gtk_list_store_new(), simultaneously setting
260 * up the columns and column types as with gtk_list_store_set_n_columns() and
261 * gtk_list_store_set_column_type(). As an example,
262 * gtk_tree_store_new_with_types (3, G_TYPE_INT, G_TYPE_STRING,
263 * GTK_TYPE_PIXBUF); will create a new GtkListStore with three columns, of type
264 * int, string and GtkPixbuf respectively.
266 * Return value: a new #GtkListStore
269 gtk_list_store_new_with_types (gint n_columns,
272 GtkListStore *retval;
276 g_return_val_if_fail (n_columns > 0, NULL);
278 retval = gtk_list_store_new ();
279 gtk_list_store_set_n_columns (retval, n_columns);
281 va_start (args, n_columns);
283 for (i = 0; i < n_columns; i++)
285 GType type = va_arg (args, GType);
286 if (! _gtk_tree_data_list_check_type (type))
288 g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (type));
289 g_object_unref (G_OBJECT (retval));
293 gtk_list_store_set_column_type (retval, i, type);
302 * gtk_list_store_set_n_columns:
303 * @store: a #GtkListStore
304 * @n_columns: number of columns
306 * Sets the number of columns in the #GtkListStore.
310 gtk_list_store_set_n_columns (GtkListStore *list_store,
315 g_return_if_fail (list_store != NULL);
316 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
317 g_return_if_fail (n_columns > 0);
319 if (list_store->n_columns == n_columns)
322 new_columns = g_new0 (GType, n_columns);
323 if (list_store->column_headers)
325 /* copy the old header orders over */
326 if (n_columns >= list_store->n_columns)
327 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
329 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
331 g_free (list_store->column_headers);
334 if (list_store->sort_list)
335 _gtk_tree_data_list_header_free (list_store->sort_list);
337 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
339 list_store->column_headers = new_columns;
340 list_store->n_columns = n_columns;
344 * gtk_list_store_set_column_type:
345 * @store: a #GtkListStore
346 * @column: column number
347 * @type: type of the data stored in @column
349 * Supported types include: %G_TYPE_UINT, %G_TYPE_INT, %G_TYPE_UCHAR,
350 * %G_TYPE_CHAR, %G_TYPE_BOOLEAN, %G_TYPE_POINTER, %G_TYPE_FLOAT,
351 * %G_TYPE_DOUBLE, %G_TYPE_STRING, %G_TYPE_OBJECT, and %G_TYPE_BOXED, along with
352 * subclasses of those types such as %GDK_TYPE_PIXBUF.
356 gtk_list_store_set_column_type (GtkListStore *list_store,
360 g_return_if_fail (list_store != NULL);
361 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
362 g_return_if_fail (column >=0 && column < list_store->n_columns);
363 if (!_gtk_tree_data_list_check_type (type))
365 g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (type));
369 list_store->column_headers[column] = type;
372 /* Fulfill the GtkTreeModel requirements */
374 gtk_list_store_get_flags (GtkTreeModel *tree_model)
376 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
378 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
382 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
384 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
386 return GTK_LIST_STORE (tree_model)->n_columns;
390 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
393 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
394 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
395 index >= 0, G_TYPE_INVALID);
397 return GTK_LIST_STORE (tree_model)->column_headers[index];
401 gtk_list_store_get_iter (GtkTreeModel *tree_model,
408 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
409 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
411 i = gtk_tree_path_get_indices (path)[0];
413 if (i >= GTK_LIST_STORE (tree_model)->length)
416 list = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), i);
418 /* If this fails, list_store->length has gotten mangled. */
421 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
422 iter->user_data = list;
427 gtk_list_store_get_path (GtkTreeModel *tree_model,
434 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
435 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
437 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
439 if (list == G_SLIST (iter->user_data))
446 retval = gtk_tree_path_new ();
447 gtk_tree_path_append_index (retval, i);
452 gtk_list_store_get_value (GtkTreeModel *tree_model,
457 GtkTreeDataList *list;
458 gint tmp_column = column;
460 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
461 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
462 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
464 list = G_SLIST (iter->user_data)->data;
466 while (tmp_column-- > 0 && list)
470 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
472 _gtk_tree_data_list_node_to_value (list,
473 GTK_LIST_STORE (tree_model)->column_headers[column],
478 gtk_list_store_iter_next (GtkTreeModel *tree_model,
481 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
482 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
484 iter->user_data = G_SLIST (iter->user_data)->next;
486 return (iter->user_data != NULL);
490 gtk_list_store_iter_children (GtkTreeModel *tree_model,
494 /* this is a list, nodes have no children */
498 /* but if parent == NULL we return the list itself as children of the
502 if (GTK_LIST_STORE (tree_model)->root)
504 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
505 iter->user_data = GTK_LIST_STORE (tree_model)->root;
513 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
520 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
523 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
525 if (iter->user_data == NULL)
526 return GTK_LIST_STORE (tree_model)->length;
532 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
539 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
544 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
548 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
549 iter->user_data = child;
557 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
564 /* Public accessors */
565 /* This is a somewhat inelegant function that does a lot of list
566 * manipulations on it's own.
570 * gtk_list_store_set_value:
571 * @store: a #GtkListStore
572 * @iter: iterator for the row you're modifying
573 * @column: column number to modify
574 * @value: new value for the cell
576 * Sets the data in the cell specified by @iter and @column.
577 * The type of @value must be convertible to the type of the
582 gtk_list_store_set_value (GtkListStore *list_store,
587 GtkTreeDataList *list;
588 GtkTreeDataList *prev;
590 GValue real_value = {0, };
591 gboolean converted = FALSE;
593 g_return_if_fail (list_store != NULL);
594 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
595 g_return_if_fail (iter != NULL);
596 g_return_if_fail (column >= 0 && column < list_store->n_columns);
597 g_return_if_fail (G_IS_VALUE (value));
599 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
601 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
602 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
604 g_warning ("%s: Unable to convert from %s to %s\n",
606 g_type_name (G_VALUE_TYPE (value)),
607 g_type_name (list_store->column_headers[column]));
610 if (!g_value_transform (value, &real_value))
612 g_warning ("%s: Unable to make conversion from %s to %s\n",
614 g_type_name (G_VALUE_TYPE (value)),
615 g_type_name (list_store->column_headers[column]));
616 g_value_unset (&real_value);
622 prev = list = G_SLIST (iter->user_data)->data;
628 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
630 _gtk_tree_data_list_value_to_node (list, &real_value);
632 _gtk_tree_data_list_value_to_node (list, value);
633 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
634 gtk_tree_path_free (path);
636 g_value_unset (&real_value);
645 if (G_SLIST (iter->user_data)->data == NULL)
647 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
652 list = prev->next = _gtk_tree_data_list_alloc ();
658 list->next = _gtk_tree_data_list_alloc ();
664 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
666 _gtk_tree_data_list_value_to_node (list, &real_value);
668 _gtk_tree_data_list_value_to_node (list, value);
669 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
670 gtk_tree_path_free (path);
672 g_value_unset (&real_value);
674 if (GTK_LIST_STORE_IS_SORTED (list_store))
675 gtk_list_store_sort_iter_changed (list_store, iter, column);
679 * gtk_list_store_set_valist:
680 * @list_store: a #GtkListStore
681 * @iter: row to set data for
682 * @var_args: va_list of column/value pairs
684 * See gtk_list_store_set(); this version takes a va_list for
685 * use by language bindings.
689 gtk_list_store_set_valist (GtkListStore *list_store,
695 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
697 column = va_arg (var_args, gint);
701 GValue value = { 0, };
704 if (column >= list_store->n_columns)
706 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
709 g_value_init (&value, list_store->column_headers[column]);
711 G_VALUE_COLLECT (&value, var_args, 0, &error);
714 g_warning ("%s: %s", G_STRLOC, error);
717 /* we purposely leak the value here, it might not be
718 * in a sane state if an error condition occoured
723 /* FIXME: instead of calling this n times, refactor with above */
724 gtk_list_store_set_value (list_store,
729 g_value_unset (&value);
731 column = va_arg (var_args, gint);
736 * gtk_list_store_set:
737 * @list_store: a #GtkListStore
738 * @iter: row iterator
739 * @Varargs: pairs of column number and value, terminated with -1
741 * Sets the value of one or more cells in the row referenced by @iter.
742 * The variable argument list should contain integer column numbers,
743 * each column number followed by the value to be set.
744 * The list is terminated by a -1. For example, to set column 0 with type
745 * %G_TYPE_STRING to "Foo", you would write gtk_list_store_set (store, iter,
749 gtk_list_store_set (GtkListStore *list_store,
755 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
757 va_start (var_args, iter);
758 gtk_list_store_set_valist (list_store, iter, var_args);
763 remove_link_saving_prev (GSList *list,
778 prev->next = link->next;
797 gtk_list_store_remove_silently (GtkListStore *list_store,
801 if (G_SLIST (iter->user_data)->data)
803 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
804 list_store->column_headers);
805 G_SLIST (iter->user_data)->data = NULL;
811 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
812 G_SLIST (iter->user_data),
815 list_store->length -= 1;
817 if (iter->user_data == list_store->tail)
818 list_store->tail = prev;
821 list_store->stamp ++;
825 * gtk_list_store_remove:
826 * @store: a #GtkListStore
827 * @iter: a row in @list_store
829 * Removes the given row from the list store, emitting the
830 * "deleted" signal on #GtkTreeModel.
834 gtk_list_store_remove (GtkListStore *list_store,
839 g_return_if_fail (list_store != NULL);
840 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
841 g_return_if_fail (iter->user_data != NULL);
843 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
845 validate_list_store (list_store);
847 gtk_list_store_remove_silently (list_store, iter, path);
849 validate_list_store (list_store);
851 list_store->stamp ++;
852 gtk_tree_model_deleted (GTK_TREE_MODEL (list_store), path);
853 gtk_tree_path_free (path);
857 insert_after (GtkListStore *list_store,
861 g_return_if_fail (sibling != NULL);
862 g_return_if_fail (new_list != NULL);
864 /* insert new node after list */
865 new_list->next = sibling->next;
866 sibling->next = new_list;
868 /* if list was the tail, the new node is the new tail */
869 if (sibling == ((GSList *) list_store->tail))
870 list_store->tail = new_list;
872 list_store->length += 1;
876 * gtk_list_store_insert:
877 * @store: a #GtkListStore
878 * @iter: iterator to initialize with the new row
879 * @position: position to insert the new row
881 * Creates a new row at @position, initializing @iter to point to the
882 * new row, and emitting the "inserted" signal from the #GtkTreeModel
887 gtk_list_store_insert (GtkListStore *list_store,
895 g_return_if_fail (list_store != NULL);
896 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
897 g_return_if_fail (iter != NULL);
898 g_return_if_fail (position >= 0);
901 GTK_LIST_STORE_IS_SORTED (list_store))
903 gtk_list_store_prepend (list_store, iter);
907 new_list = g_slist_alloc ();
909 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
913 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
917 insert_after (list_store, list, new_list);
919 iter->stamp = list_store->stamp;
920 iter->user_data = new_list;
922 validate_list_store (list_store);
924 path = gtk_tree_path_new ();
925 gtk_tree_path_append_index (path, position);
926 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
927 gtk_tree_path_free (path);
931 * gtk_list_store_insert_before:
932 * @store: a #GtkListStore
933 * @iter: iterator to initialize with the new row
934 * @sibling: an existing row
936 * Inserts a new row before @sibling, initializing @iter to point to
937 * the new row, and emitting the "inserted" signal from the
938 * #GtkTreeModel interface.
942 gtk_list_store_insert_before (GtkListStore *list_store,
944 GtkTreeIter *sibling)
947 GSList *list, *prev, *new_list;
950 g_return_if_fail (list_store != NULL);
951 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
952 g_return_if_fail (iter != NULL);
954 if (GTK_LIST_STORE_IS_SORTED (list_store))
956 gtk_list_store_prepend (list_store, iter);
962 gtk_list_store_append (list_store, iter);
966 new_list = g_slist_alloc ();
969 list = list_store->root;
970 while (list && list != sibling->user_data)
977 if (list != sibling->user_data)
979 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
983 /* if there are no nodes, we become the list tail, otherwise we
984 * are inserting before any existing nodes so we can't change
988 if (list_store->root == NULL)
989 list_store->tail = new_list;
993 new_list->next = prev->next;
994 prev->next = new_list;
998 new_list->next = list_store->root;
999 list_store->root = new_list;
1002 iter->stamp = list_store->stamp;
1003 iter->user_data = new_list;
1005 list_store->length += 1;
1007 validate_list_store (list_store);
1009 path = gtk_tree_path_new ();
1010 gtk_tree_path_append_index (path, i);
1011 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1012 gtk_tree_path_free (path);
1016 * gtk_list_store_insert_after:
1017 * @store: a #GtkListStore
1018 * @iter: iterator to initialize with the new row
1019 * @sibling: an existing row
1021 * Inserts a new row after @sibling, initializing @iter to point to
1022 * the new row, and emitting the "inserted" signal from the
1023 * #GtkTreeModel interface.
1027 gtk_list_store_insert_after (GtkListStore *list_store,
1029 GtkTreeIter *sibling)
1032 GSList *list, *new_list;
1035 g_return_if_fail (list_store != NULL);
1036 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1037 g_return_if_fail (iter != NULL);
1039 g_return_if_fail (sibling->stamp == list_store->stamp);
1041 if (sibling == NULL ||
1042 GTK_LIST_STORE_IS_SORTED (list_store))
1044 gtk_list_store_prepend (list_store, iter);
1048 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1051 g_return_if_fail (list == sibling->user_data);
1053 new_list = g_slist_alloc ();
1055 insert_after (list_store, list, new_list);
1057 iter->stamp = list_store->stamp;
1058 iter->user_data = new_list;
1060 validate_list_store (list_store);
1062 path = gtk_tree_path_new ();
1063 gtk_tree_path_append_index (path, i);
1064 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1065 gtk_tree_path_free (path);
1069 * gtk_list_store_prepend:
1070 * @store: a #GtkListStore
1071 * @iter: iterator to initialize with new row
1073 * Prepends a row to @store, initializing @iter to point to the
1074 * new row, and emitting the "inserted" signal on the #GtkTreeModel
1075 * interface for the @store.
1079 gtk_list_store_prepend (GtkListStore *list_store,
1084 g_return_if_fail (list_store != NULL);
1085 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1086 g_return_if_fail (iter != NULL);
1088 iter->stamp = list_store->stamp;
1089 iter->user_data = g_slist_alloc ();
1091 if (list_store->root == NULL)
1092 list_store->tail = iter->user_data;
1094 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1095 list_store->root = iter->user_data;
1097 list_store->length += 1;
1099 validate_list_store (list_store);
1101 path = gtk_tree_path_new ();
1102 gtk_tree_path_append_index (path, 0);
1103 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1104 gtk_tree_path_free (path);
1108 * gtk_list_store_append:
1109 * @store: a #GtkListStore
1110 * @iter: iterator to initialize with the new row
1112 * Appends a row to @store, initializing @iter to point to the
1113 * new row, and emitting the "inserted" signal on the #GtkTreeModel
1114 * interface for the @store.
1118 gtk_list_store_append (GtkListStore *list_store,
1123 g_return_if_fail (list_store != NULL);
1124 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1125 g_return_if_fail (iter != NULL);
1127 if (GTK_LIST_STORE_IS_SORTED (list_store))
1129 gtk_list_store_prepend (list_store, iter);
1133 iter->stamp = list_store->stamp;
1134 iter->user_data = g_slist_alloc ();
1136 if (list_store->tail)
1137 ((GSList *)list_store->tail)->next = iter->user_data;
1139 list_store->root = iter->user_data;
1141 list_store->tail = iter->user_data;
1143 list_store->length += 1;
1145 validate_list_store (list_store);
1147 path = gtk_tree_path_new ();
1148 gtk_tree_path_append_index (path, list_store->length - 1);
1149 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1150 gtk_tree_path_free (path);
1154 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1158 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1160 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1164 gtk_list_store_remove (GTK_LIST_STORE (drag_source),
1175 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1177 GtkSelectionData *selection_data)
1179 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1181 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1182 * target, because the default handler does it for us, but
1183 * we do anyway for the convenience of someone maybe overriding the
1187 if (gtk_selection_data_set_tree_row (selection_data,
1188 GTK_TREE_MODEL (drag_source),
1195 /* FIXME handle text targets at least. */
1202 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1204 GtkSelectionData *selection_data)
1206 GtkTreeModel *tree_model;
1207 GtkListStore *list_store;
1208 GtkTreeModel *src_model = NULL;
1209 GtkTreePath *src_path = NULL;
1210 gboolean retval = FALSE;
1212 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1214 tree_model = GTK_TREE_MODEL (drag_dest);
1215 list_store = GTK_LIST_STORE (drag_dest);
1217 if (gtk_selection_data_get_tree_row (selection_data,
1220 src_model == tree_model)
1222 /* Copy the given row to a new position */
1223 GtkTreeIter src_iter;
1224 GtkTreeIter dest_iter;
1227 if (!gtk_tree_model_get_iter (src_model,
1234 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1235 prev = gtk_tree_path_copy (dest);
1237 if (!gtk_tree_path_prev (prev))
1239 /* dest was the first spot in the list; which means we are supposed
1242 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1249 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1253 GtkTreeIter tmp_iter = dest_iter;
1254 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1261 gtk_tree_path_free (prev);
1263 /* If we succeeded in creating dest_iter, copy data from src
1267 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1268 GtkTreeDataList *copy_head = NULL;
1269 GtkTreeDataList *copy_prev = NULL;
1270 GtkTreeDataList *copy_iter = NULL;
1277 copy_iter = _gtk_tree_data_list_node_copy (dl,
1278 list_store->column_headers[col]);
1280 if (copy_head == NULL)
1281 copy_head = copy_iter;
1284 copy_prev->next = copy_iter;
1286 copy_prev = copy_iter;
1292 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1293 G_SLIST (dest_iter.user_data)->data = copy_head;
1295 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1296 gtk_tree_model_range_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter, path, &dest_iter);
1297 gtk_tree_path_free (path);
1302 /* FIXME maybe add some data targets eventually, or handle text
1303 * targets in the simple case.
1310 gtk_tree_path_free (src_path);
1316 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1317 GtkTreeModel *src_model,
1318 GtkTreePath *src_path,
1319 GtkTreePath *dest_path)
1323 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1325 if (src_model != GTK_TREE_MODEL (drag_dest))
1328 if (gtk_tree_path_get_depth (dest_path) != 1)
1331 /* can drop before any existing node, or before one past any existing. */
1333 indices = gtk_tree_path_get_indices (dest_path);
1335 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1343 typedef struct _SortTuple
1350 gtk_list_store_compare_func (gconstpointer a,
1354 GtkListStore *list_store = user_data;
1355 GtkTreeDataSortHeader *header = NULL;
1356 GSList *el_a; /* Los Angeles? */
1362 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1363 list_store->sort_column_id);
1365 g_return_val_if_fail (header != NULL, 0);
1366 g_return_val_if_fail (header->func != NULL, 0);
1368 el_a = ((SortTuple *) a)->el;
1369 el_b = ((SortTuple *) b)->el;
1371 iter_a.stamp = list_store->stamp;
1372 iter_a.user_data = el_a;
1373 iter_b.stamp = list_store->stamp;
1374 iter_b.user_data = el_b;
1376 retval = (* header->func) (GTK_TREE_MODEL (list_store),
1380 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1384 else if (retval < 0)
1391 gtk_list_store_sort (GtkListStore *list_store)
1393 GtkTreeDataSortHeader *header = NULL;
1401 if (list_store->length <= 1)
1404 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1406 header = _gtk_tree_data_list_get_header (list_store->sort_list, list_store->sort_column_id);
1408 /* We want to make sure that we have a function */
1409 g_return_if_fail (header != NULL);
1410 g_return_if_fail (header->func != NULL);
1412 list = G_SLIST (list_store->root);
1414 sort_array = g_array_sized_new (FALSE, FALSE,
1416 list_store->length);
1418 for (i = 0; i < list_store->length; i++)
1422 /* If this fails, we are in an inconsistent state. Bad */
1423 g_return_if_fail (list != NULL);
1427 g_array_append_val (sort_array, tuple);
1432 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1434 for (i = 0; i < list_store->length - 1; i++)
1435 g_array_index (sort_array, SortTuple, i).el->next =
1436 g_array_index (sort_array, SortTuple, i + 1).el;
1437 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1438 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1440 /* Let the world know about our new order */
1441 new_order = g_new (gint, list_store->length);
1442 for (i = 0; i < list_store->length; i++)
1443 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1444 path = gtk_tree_path_new ();
1445 iter.stamp = list_store->stamp;
1446 iter.user_data = NULL;
1447 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1448 path, &iter, new_order);
1449 gtk_tree_path_free (path);
1451 g_array_free (sort_array, TRUE);
1455 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1460 GtkTreeDataSortHeader *header;
1461 GSList *prev = NULL;
1462 GSList *next = NULL;
1463 GSList *list = G_SLIST (list_store->root);
1464 GtkTreePath *tmp_path;
1465 GtkTreeIter tmp_iter;
1473 if (list_store->length < 2)
1476 tmp_iter.stamp = list_store->stamp;
1477 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1478 list_store->sort_column_id);
1479 g_return_if_fail (header != NULL);
1480 g_return_if_fail (header->func != NULL);
1482 /* If it's the built in function, we don't sort. */
1483 if (header->func == gtk_tree_data_list_compare_func &&
1484 list_store->sort_column_id != column)
1488 /* First we find the iter, its prev, and its next */
1491 if (list == G_SLIST (iter->user_data))
1497 g_assert (list != NULL);
1501 /* Check the common case, where we don't need to sort it moved. */
1504 tmp_iter.user_data = prev;
1505 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1512 tmp_iter.user_data = next;
1513 cmp_b = (* header->func) (GTK_TREE_MODEL (list_store),
1519 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1532 if (prev == NULL && cmp_b <= 0)
1534 else if (next == NULL && cmp_a <= 0)
1536 else if (prev != NULL && next != NULL &&
1537 cmp_a <= 0 && cmp_b <= 0)
1540 /* We actually need to sort it */
1541 /* First, remove the old link. */
1544 list_store->root = next;
1548 list_store->tail = prev;
1551 /* FIXME: as an optimization, we can potentially start at next */
1553 list = G_SLIST (list_store->root);
1555 tmp_iter.user_data = list;
1556 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1557 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1558 &tmp_iter, iter, header->data);
1560 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1561 iter, &tmp_iter, header->data);
1563 while ((list->next) && (cmp_a > 0))
1568 tmp_iter.user_data = list;
1569 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1570 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1571 &tmp_iter, iter, header->data);
1573 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1574 iter, &tmp_iter, header->data);
1577 if ((!list->next) && (cmp_a > 0))
1579 list->next = G_SLIST (iter->user_data);
1580 list_store->tail = list->next;
1584 prev->next = G_SLIST (iter->user_data);
1585 G_SLIST (iter->user_data)->next = list;
1589 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1590 list_store->root = G_SLIST (iter->user_data);
1593 /* Emit the reordered signal. */
1594 new_order = g_new (int, list_store->length);
1595 if (old_location < new_location)
1596 for (i = 0; i < list_store->length; i++)
1598 if (i < old_location ||
1601 else if (i >= old_location &&
1603 new_order[i] = i + 1;
1604 else if (i == new_location)
1605 new_order[i] = old_location;
1608 for (i = 0; i < list_store->length; i++)
1610 if (i < new_location ||
1613 else if (i > new_location &&
1615 new_order[i] = i - 1;
1616 else if (i == new_location)
1617 new_order[i] = old_location;
1620 tmp_path = gtk_tree_path_new ();
1621 tmp_iter.user_data = NULL;
1623 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1624 tmp_path, &tmp_iter,
1627 gtk_tree_path_free (tmp_path);
1632 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1633 gint *sort_column_id,
1634 GtkTreeSortOrder *order)
1636 GtkListStore *list_store = (GtkListStore *) sortable;
1638 g_return_val_if_fail (sortable != NULL, FALSE);
1639 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1641 if (list_store->sort_column_id == -1)
1645 * sort_column_id = list_store->sort_column_id;
1647 * order = list_store->order;
1652 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1653 gint sort_column_id,
1654 GtkTreeSortOrder order)
1656 GtkListStore *list_store = (GtkListStore *) sortable;
1659 g_return_if_fail (sortable != NULL);
1660 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1662 for (list = list_store->sort_list; list; list = list->next)
1664 GtkTreeDataSortHeader *header = (GtkTreeDataSortHeader*) list->data;
1665 if (header->sort_column_id == sort_column_id)
1668 g_return_if_fail (list != NULL);
1670 if ((list_store->sort_column_id == sort_column_id) &&
1671 (list_store->order == order))
1674 list_store->sort_column_id = sort_column_id;
1675 list_store->order = order;
1677 if (list_store->sort_column_id >= 0)
1678 gtk_list_store_sort (list_store);
1680 gtk_tree_sortable_sort_column_changed (sortable);
1684 gtk_list_store_sort_column_id_set_func (GtkTreeSortable *sortable,
1685 gint sort_column_id,
1686 GtkTreeIterCompareFunc func,
1688 GtkDestroyNotify destroy)
1690 GtkListStore *list_store = (GtkListStore *) sortable;
1691 GtkTreeDataSortHeader *header = NULL;
1694 g_return_if_fail (sortable != NULL);
1695 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1696 g_return_if_fail (func != NULL);
1698 for (list = list_store->sort_list; list; list = list->next)
1700 header = (GtkTreeDataSortHeader*) list->data;
1701 if (header->sort_column_id == sort_column_id)
1707 header = g_new0 (GtkTreeDataSortHeader, 1);
1708 header->sort_column_id = sort_column_id;
1709 list_store->sort_list = g_list_append (list_store->sort_list, header);
1712 if (header->destroy)
1713 (* header->destroy) (header->data);
1715 header->func = func;
1716 header->data = data;
1717 header->destroy = destroy;