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: pairs of column number and #GType
259 * Creates a new list store as with gtk_list_store_new(),
260 * simultaneously setting up the columns and column types as with
261 * gtk_list_store_set_n_columns() and
262 * gtk_list_store_set_column_type().
265 * Return value: a new #GtkListStore
268 gtk_list_store_new_with_types (gint n_columns,
271 GtkListStore *retval;
275 g_return_val_if_fail (n_columns > 0, NULL);
277 retval = gtk_list_store_new ();
278 gtk_list_store_set_n_columns (retval, n_columns);
280 va_start (args, n_columns);
282 for (i = 0; i < n_columns; i++)
284 GType type = va_arg (args, GType);
285 if (! _gtk_tree_data_list_check_type (type))
287 g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (type));
288 g_object_unref (G_OBJECT (retval));
292 gtk_list_store_set_column_type (retval, i, type);
301 * gtk_list_store_set_n_columns:
302 * @store: a #GtkListStore
303 * @n_columns: number of columns
305 * Sets the number of columns in the #GtkListStore.
309 gtk_list_store_set_n_columns (GtkListStore *list_store,
314 g_return_if_fail (list_store != NULL);
315 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
316 g_return_if_fail (n_columns > 0);
318 if (list_store->n_columns == n_columns)
321 new_columns = g_new0 (GType, n_columns);
322 if (list_store->column_headers)
324 /* copy the old header orders over */
325 if (n_columns >= list_store->n_columns)
326 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
328 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
330 g_free (list_store->column_headers);
333 if (list_store->sort_list)
334 _gtk_tree_data_list_header_free (list_store->sort_list);
336 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
338 list_store->column_headers = new_columns;
339 list_store->n_columns = n_columns;
343 * gtk_list_store_set_column_type:
344 * @store: a #GtkListStore
345 * @column: column number
346 * @type: type of the data stored in @column
348 * Supported types include: %G_TYPE_UINT, %G_TYPE_INT, %G_TYPE_UCHAR,
349 * %G_TYPE_CHAR, %G_TYPE_BOOLEAN, %G_TYPE_POINTER, %G_TYPE_FLOAT,
350 * %G_TYPE_DOUBLE, %G_TYPE_STRING, %G_TYPE_OBJECT, and %G_TYPE_BOXED, along with
351 * subclasses of those types such as %GDK_TYPE_PIXBUF.
355 gtk_list_store_set_column_type (GtkListStore *list_store,
359 g_return_if_fail (list_store != NULL);
360 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
361 g_return_if_fail (column >=0 && column < list_store->n_columns);
362 if (!_gtk_tree_data_list_check_type (type))
364 g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (type));
368 list_store->column_headers[column] = type;
371 /* Fulfill the GtkTreeModel requirements */
373 gtk_list_store_get_flags (GtkTreeModel *tree_model)
375 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
377 return GTK_TREE_MODEL_ITERS_PERSIST;
381 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
383 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
385 return GTK_LIST_STORE (tree_model)->n_columns;
389 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
392 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
393 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
394 index >= 0, G_TYPE_INVALID);
396 return GTK_LIST_STORE (tree_model)->column_headers[index];
400 gtk_list_store_get_iter (GtkTreeModel *tree_model,
407 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
408 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
410 i = gtk_tree_path_get_indices (path)[0];
412 if (i >= GTK_LIST_STORE (tree_model)->length)
415 list = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), i);
417 /* If this fails, list_store->length has gotten mangled. */
420 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
421 iter->user_data = list;
426 gtk_list_store_get_path (GtkTreeModel *tree_model,
433 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
434 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
436 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
438 if (list == G_SLIST (iter->user_data))
445 retval = gtk_tree_path_new ();
446 gtk_tree_path_append_index (retval, i);
451 gtk_list_store_get_value (GtkTreeModel *tree_model,
456 GtkTreeDataList *list;
457 gint tmp_column = column;
459 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
460 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
461 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
463 list = G_SLIST (iter->user_data)->data;
465 while (tmp_column-- > 0 && list)
469 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
471 _gtk_tree_data_list_node_to_value (list,
472 GTK_LIST_STORE (tree_model)->column_headers[column],
477 gtk_list_store_iter_next (GtkTreeModel *tree_model,
480 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
481 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
483 iter->user_data = G_SLIST (iter->user_data)->next;
485 return (iter->user_data != NULL);
489 gtk_list_store_iter_children (GtkTreeModel *tree_model,
493 /* this is a list, nodes have no children */
497 /* but if parent == NULL we return the list itself as children of the
501 if (GTK_LIST_STORE (tree_model)->root)
503 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
504 iter->user_data = GTK_LIST_STORE (tree_model)->root;
512 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
519 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
522 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
524 if (iter->user_data == NULL)
525 return GTK_LIST_STORE (tree_model)->length;
531 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
538 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
543 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
547 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
548 iter->user_data = child;
556 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
563 /* Public accessors */
564 /* This is a somewhat inelegant function that does a lot of list
565 * manipulations on it's own.
569 * gtk_list_store_set_value:
570 * @store: a #GtkListStore
571 * @iter: iterator for the row you're modifying
572 * @column: column number to modify
573 * @value: new value for the cell
575 * Sets the data in the cell specified by @iter and @column.
576 * The type of @value must be convertible to the type of the
581 gtk_list_store_set_value (GtkListStore *list_store,
586 GtkTreeDataList *list;
587 GtkTreeDataList *prev;
589 GValue real_value = {0, };
590 gboolean converted = FALSE;
592 g_return_if_fail (list_store != NULL);
593 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
594 g_return_if_fail (iter != NULL);
595 g_return_if_fail (column >= 0 && column < list_store->n_columns);
596 g_return_if_fail (G_IS_VALUE (value));
598 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
600 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
601 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
603 g_warning ("%s: Unable to convert from %s to %s\n",
605 g_type_name (G_VALUE_TYPE (value)),
606 g_type_name (list_store->column_headers[column]));
609 if (!g_value_transform (value, &real_value))
611 g_warning ("%s: Unable to make conversion from %s to %s\n",
613 g_type_name (G_VALUE_TYPE (value)),
614 g_type_name (list_store->column_headers[column]));
615 g_value_unset (&real_value);
621 prev = list = G_SLIST (iter->user_data)->data;
627 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
629 _gtk_tree_data_list_value_to_node (list, &real_value);
631 _gtk_tree_data_list_value_to_node (list, value);
632 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
633 gtk_tree_path_free (path);
635 g_value_unset (&real_value);
644 if (G_SLIST (iter->user_data)->data == NULL)
646 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
651 list = prev->next = _gtk_tree_data_list_alloc ();
657 list->next = _gtk_tree_data_list_alloc ();
663 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
665 _gtk_tree_data_list_value_to_node (list, &real_value);
667 _gtk_tree_data_list_value_to_node (list, value);
668 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
669 gtk_tree_path_free (path);
671 g_value_unset (&real_value);
673 if (GTK_LIST_STORE_IS_SORTED (list_store))
674 gtk_list_store_sort_iter_changed (list_store, iter, column);
678 * gtk_list_store_set_valist:
679 * @list_store: a #GtkListStore
680 * @iter: row to set data for
681 * @var_args: va_list of column/value pairs
683 * See gtk_list_store_set(); this version takes a va_list for
684 * use by language bindings.
688 gtk_list_store_set_valist (GtkListStore *list_store,
694 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
696 column = va_arg (var_args, gint);
700 GValue value = { 0, };
703 if (column >= list_store->n_columns)
705 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
708 g_value_init (&value, list_store->column_headers[column]);
710 G_VALUE_COLLECT (&value, var_args, 0, &error);
713 g_warning ("%s: %s", G_STRLOC, error);
716 /* we purposely leak the value here, it might not be
717 * in a sane state if an error condition occoured
722 /* FIXME: instead of calling this n times, refactor with above */
723 gtk_list_store_set_value (list_store,
728 g_value_unset (&value);
730 column = va_arg (var_args, gint);
735 * gtk_list_store_set:
736 * @list_store: a #GtkListStore
737 * @iter: row iterator
738 * @Varargs: pairs of column number and value, terminated with -1
740 * Sets the value of one or more cells in the row referenced by @iter.
741 * The variable argument list should contain integer column numbers,
742 * each column number followed by the value to be set.
743 * The list is terminated by a -1. For example, to set column 0 with type
744 * %G_TYPE_STRING to "Foo", you would write gtk_list_store_set (store, iter,
748 gtk_list_store_set (GtkListStore *list_store,
754 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
756 va_start (var_args, iter);
757 gtk_list_store_set_valist (list_store, iter, var_args);
762 remove_link_saving_prev (GSList *list,
777 prev->next = link->next;
796 gtk_list_store_remove_silently (GtkListStore *list_store,
800 if (G_SLIST (iter->user_data)->data)
802 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
803 list_store->column_headers);
804 G_SLIST (iter->user_data)->data = NULL;
810 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
811 G_SLIST (iter->user_data),
814 list_store->length -= 1;
816 if (iter->user_data == list_store->tail)
817 list_store->tail = prev;
820 list_store->stamp ++;
824 * gtk_list_store_remove:
825 * @store: a #GtkListStore
826 * @iter: a row in @list_store
828 * Removes the given row from the list store, emitting the
829 * "deleted" signal on #GtkTreeModel.
833 gtk_list_store_remove (GtkListStore *list_store,
838 g_return_if_fail (list_store != NULL);
839 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
840 g_return_if_fail (iter->user_data != NULL);
842 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
844 validate_list_store (list_store);
846 gtk_list_store_remove_silently (list_store, iter, path);
848 validate_list_store (list_store);
850 list_store->stamp ++;
851 gtk_tree_model_deleted (GTK_TREE_MODEL (list_store), path);
852 gtk_tree_path_free (path);
856 insert_after (GtkListStore *list_store,
860 g_return_if_fail (sibling != NULL);
861 g_return_if_fail (new_list != NULL);
863 /* insert new node after list */
864 new_list->next = sibling->next;
865 sibling->next = new_list;
867 /* if list was the tail, the new node is the new tail */
868 if (sibling == ((GSList *) list_store->tail))
869 list_store->tail = new_list;
871 list_store->length += 1;
875 * gtk_list_store_insert:
876 * @store: a #GtkListStore
877 * @iter: iterator to initialize with the new row
878 * @position: position to insert the new row
880 * Creates a new row at @position, initializing @iter to point to the
881 * new row, and emitting the "inserted" signal from the #GtkTreeModel
886 gtk_list_store_insert (GtkListStore *list_store,
894 g_return_if_fail (list_store != NULL);
895 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
896 g_return_if_fail (iter != NULL);
897 g_return_if_fail (position >= 0);
900 GTK_LIST_STORE_IS_SORTED (list_store))
902 gtk_list_store_prepend (list_store, iter);
906 new_list = g_slist_alloc ();
908 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
912 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
916 insert_after (list_store, list, new_list);
918 iter->stamp = list_store->stamp;
919 iter->user_data = new_list;
921 validate_list_store (list_store);
923 path = gtk_tree_path_new ();
924 gtk_tree_path_append_index (path, position);
925 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
926 gtk_tree_path_free (path);
930 * gtk_list_store_insert_before:
931 * @store: a #GtkListStore
932 * @iter: iterator to initialize with the new row
933 * @sibling: an existing row
935 * Inserts a new row before @sibling, initializing @iter to point to
936 * the new row, and emitting the "inserted" signal from the
937 * #GtkTreeModel interface.
941 gtk_list_store_insert_before (GtkListStore *list_store,
943 GtkTreeIter *sibling)
946 GSList *list, *prev, *new_list;
949 g_return_if_fail (list_store != NULL);
950 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
951 g_return_if_fail (iter != NULL);
953 if (GTK_LIST_STORE_IS_SORTED (list_store))
955 gtk_list_store_prepend (list_store, iter);
961 gtk_list_store_append (list_store, iter);
965 new_list = g_slist_alloc ();
968 list = list_store->root;
969 while (list && list != sibling->user_data)
976 if (list != sibling->user_data)
978 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
982 /* if there are no nodes, we become the list tail, otherwise we
983 * are inserting before any existing nodes so we can't change
987 if (list_store->root == NULL)
988 list_store->tail = new_list;
992 new_list->next = prev->next;
993 prev->next = new_list;
997 new_list->next = list_store->root;
998 list_store->root = new_list;
1001 iter->stamp = list_store->stamp;
1002 iter->user_data = new_list;
1004 list_store->length += 1;
1006 validate_list_store (list_store);
1008 path = gtk_tree_path_new ();
1009 gtk_tree_path_append_index (path, i);
1010 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1011 gtk_tree_path_free (path);
1015 * gtk_list_store_insert_after:
1016 * @store: a #GtkListStore
1017 * @iter: iterator to initialize with the new row
1018 * @sibling: an existing row
1020 * Inserts a new row after @sibling, initializing @iter to point to
1021 * the new row, and emitting the "inserted" signal from the
1022 * #GtkTreeModel interface.
1026 gtk_list_store_insert_after (GtkListStore *list_store,
1028 GtkTreeIter *sibling)
1031 GSList *list, *new_list;
1034 g_return_if_fail (list_store != NULL);
1035 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1036 g_return_if_fail (iter != NULL);
1038 g_return_if_fail (sibling->stamp == list_store->stamp);
1040 if (sibling == NULL ||
1041 GTK_LIST_STORE_IS_SORTED (list_store))
1043 gtk_list_store_prepend (list_store, iter);
1047 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1050 g_return_if_fail (list == sibling->user_data);
1052 new_list = g_slist_alloc ();
1054 insert_after (list_store, list, new_list);
1056 iter->stamp = list_store->stamp;
1057 iter->user_data = new_list;
1059 validate_list_store (list_store);
1061 path = gtk_tree_path_new ();
1062 gtk_tree_path_append_index (path, i);
1063 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1064 gtk_tree_path_free (path);
1068 * gtk_list_store_prepend:
1069 * @store: a #GtkListStore
1070 * @iter: iterator to initialize with new row
1072 * Prepends a row to @store, initializing @iter to point to the
1073 * new row, and emitting the "inserted" signal on the #GtkTreeModel
1074 * interface for the @store.
1078 gtk_list_store_prepend (GtkListStore *list_store,
1083 g_return_if_fail (list_store != NULL);
1084 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1085 g_return_if_fail (iter != NULL);
1087 iter->stamp = list_store->stamp;
1088 iter->user_data = g_slist_alloc ();
1090 if (list_store->root == NULL)
1091 list_store->tail = iter->user_data;
1093 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1094 list_store->root = iter->user_data;
1096 list_store->length += 1;
1098 validate_list_store (list_store);
1100 path = gtk_tree_path_new ();
1101 gtk_tree_path_append_index (path, 0);
1102 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1103 gtk_tree_path_free (path);
1107 * gtk_list_store_append:
1108 * @store: a #GtkListStore
1109 * @iter: iterator to initialize with the new row
1111 * Appends a row to @store, initializing @iter to point to the
1112 * new row, and emitting the "inserted" signal on the #GtkTreeModel
1113 * interface for the @store.
1117 gtk_list_store_append (GtkListStore *list_store,
1122 g_return_if_fail (list_store != NULL);
1123 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1124 g_return_if_fail (iter != NULL);
1126 if (GTK_LIST_STORE_IS_SORTED (list_store))
1128 gtk_list_store_prepend (list_store, iter);
1132 iter->stamp = list_store->stamp;
1133 iter->user_data = g_slist_alloc ();
1135 if (list_store->tail)
1136 ((GSList *)list_store->tail)->next = iter->user_data;
1138 list_store->root = iter->user_data;
1140 list_store->tail = iter->user_data;
1142 list_store->length += 1;
1144 validate_list_store (list_store);
1146 path = gtk_tree_path_new ();
1147 gtk_tree_path_append_index (path, list_store->length - 1);
1148 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1149 gtk_tree_path_free (path);
1153 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1157 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1159 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1163 gtk_list_store_remove (GTK_LIST_STORE (drag_source),
1174 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1176 GtkSelectionData *selection_data)
1178 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1180 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1181 * target, because the default handler does it for us, but
1182 * we do anyway for the convenience of someone maybe overriding the
1186 if (gtk_selection_data_set_tree_row (selection_data,
1187 GTK_TREE_MODEL (drag_source),
1194 /* FIXME handle text targets at least. */
1201 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1203 GtkSelectionData *selection_data)
1205 GtkTreeModel *tree_model;
1206 GtkListStore *list_store;
1207 GtkTreeModel *src_model = NULL;
1208 GtkTreePath *src_path = NULL;
1209 gboolean retval = FALSE;
1211 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1213 tree_model = GTK_TREE_MODEL (drag_dest);
1214 list_store = GTK_LIST_STORE (drag_dest);
1216 if (gtk_selection_data_get_tree_row (selection_data,
1219 src_model == tree_model)
1221 /* Copy the given row to a new position */
1222 GtkTreeIter src_iter;
1223 GtkTreeIter dest_iter;
1226 if (!gtk_tree_model_get_iter (src_model,
1233 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1234 prev = gtk_tree_path_copy (dest);
1236 if (!gtk_tree_path_prev (prev))
1238 /* dest was the first spot in the list; which means we are supposed
1241 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1248 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1252 GtkTreeIter tmp_iter = dest_iter;
1253 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1260 gtk_tree_path_free (prev);
1262 /* If we succeeded in creating dest_iter, copy data from src
1266 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1267 GtkTreeDataList *copy_head = NULL;
1268 GtkTreeDataList *copy_prev = NULL;
1269 GtkTreeDataList *copy_iter = NULL;
1276 copy_iter = _gtk_tree_data_list_node_copy (dl,
1277 list_store->column_headers[col]);
1279 if (copy_head == NULL)
1280 copy_head = copy_iter;
1283 copy_prev->next = copy_iter;
1285 copy_prev = copy_iter;
1291 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1292 G_SLIST (dest_iter.user_data)->data = copy_head;
1294 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1295 gtk_tree_model_range_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter, path, &dest_iter);
1296 gtk_tree_path_free (path);
1301 /* FIXME maybe add some data targets eventually, or handle text
1302 * targets in the simple case.
1309 gtk_tree_path_free (src_path);
1315 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1316 GtkTreeModel *src_model,
1317 GtkTreePath *src_path,
1318 GtkTreePath *dest_path)
1322 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1324 if (src_model != GTK_TREE_MODEL (drag_dest))
1327 if (gtk_tree_path_get_depth (dest_path) != 1)
1330 /* can drop before any existing node, or before one past any existing. */
1332 indices = gtk_tree_path_get_indices (dest_path);
1334 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1342 typedef struct _SortTuple
1349 _gtk_list_store_compare_func (gconstpointer a,
1353 GtkListStore *list_store = user_data;
1354 GtkTreeDataSortHeader *header = NULL;
1355 GSList *el_a; /* Los Angeles? */
1361 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1362 list_store->sort_column_id);
1364 g_return_val_if_fail (header != NULL, 0);
1365 g_return_val_if_fail (header->func != NULL, 0);
1367 el_a = ((SortTuple *) a)->el;
1368 el_b = ((SortTuple *) b)->el;
1370 iter_a.stamp = list_store->stamp;
1371 iter_a.user_data = el_a;
1372 iter_b.stamp = list_store->stamp;
1373 iter_b.user_data = el_b;
1375 retval = (* header->func) (GTK_TREE_MODEL (list_store),
1379 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1383 else if (retval < 0)
1390 gtk_list_store_sort (GtkListStore *list_store)
1392 GtkTreeDataSortHeader *header = NULL;
1401 if (list_store->length <= 1)
1404 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1406 for (header_list = list_store->sort_list; header_list; header_list = header_list->next)
1408 header = (GtkTreeDataSortHeader*) header_list->data;
1409 if (header->sort_column_id == list_store->sort_column_id)
1413 /* We want to make sure that we have a function */
1414 g_return_if_fail (header != NULL);
1415 g_return_if_fail (header->func != NULL);
1417 list = G_SLIST (list_store->root);
1419 sort_array = g_array_sized_new (FALSE, FALSE,
1421 list_store->length);
1423 for (i = 0; i < list_store->length; i++)
1427 /* If this fails, we are in an inconsistent state. Bad */
1428 g_return_if_fail (list != NULL);
1432 g_array_append_val (sort_array, tuple);
1437 g_array_sort_with_data (sort_array, _gtk_list_store_compare_func, list_store);
1439 for (i = 0; i < list_store->length - 1; i++)
1440 g_array_index (sort_array, SortTuple, i).el->next =
1441 g_array_index (sort_array, SortTuple, i + 1).el;
1442 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1443 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1445 /* Let the world know about our new order */
1446 new_order = g_new (gint, list_store->length);
1447 for (i = 0; i < list_store->length; i++)
1448 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1449 path = gtk_tree_path_new ();
1450 iter.stamp = list_store->stamp;
1451 iter.user_data = NULL;
1452 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1453 path, &iter, new_order);
1454 gtk_tree_path_free (path);
1456 g_array_free (sort_array, TRUE);
1460 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1465 GtkTreeDataSortHeader *header;
1466 GSList *prev = NULL;
1467 GSList *next = NULL;
1468 GSList *list = G_SLIST (list_store->root);
1469 GtkTreePath *tmp_path;
1470 GtkTreeIter tmp_iter;
1478 if (list_store->length < 2)
1481 tmp_iter.stamp = list_store->stamp;
1482 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1483 list_store->sort_column_id);
1484 g_return_if_fail (header != NULL);
1485 g_return_if_fail (header->func != NULL);
1487 /* If it's the built in function, we don't sort. */
1488 if (header->func == gtk_tree_data_list_compare_func &&
1489 list_store->sort_column_id != column)
1493 /* First we find the iter, its prev, and its next */
1496 if (list == G_SLIST (iter->user_data))
1502 g_assert (list != NULL);
1506 /* Check the common case, where we don't need to sort it moved. */
1509 tmp_iter.user_data = prev;
1510 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1517 tmp_iter.user_data = next;
1518 cmp_b = (* header->func) (GTK_TREE_MODEL (list_store),
1524 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1537 if (prev == NULL && cmp_b <= 0)
1539 else if (next == NULL && cmp_a <= 0)
1541 else if (prev != NULL && next != NULL &&
1542 cmp_a <= 0 && cmp_b <= 0)
1545 /* We actually need to sort it */
1546 /* First, remove the old link. */
1549 list_store->root = next;
1553 list_store->tail = prev;
1556 /* FIXME: as an optimization, we can potentially start at next */
1558 list = G_SLIST (list_store->root);
1560 tmp_iter.user_data = list;
1561 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1562 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1563 &tmp_iter, iter, header->data);
1565 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1566 iter, &tmp_iter, header->data);
1568 while ((list->next) && (cmp_a > 0))
1573 tmp_iter.user_data = list;
1574 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1575 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1576 &tmp_iter, iter, header->data);
1578 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1579 iter, &tmp_iter, header->data);
1582 if ((!list->next) && (cmp_a > 0))
1584 list->next = G_SLIST (iter->user_data);
1585 list_store->tail = list->next;
1589 prev->next = G_SLIST (iter->user_data);
1590 G_SLIST (iter->user_data)->next = list;
1594 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1595 list_store->root = G_SLIST (iter->user_data);
1598 /* Emit the reordered signal. */
1599 new_order = g_new (int, list_store->length);
1600 if (old_location < new_location)
1601 for (i = 0; i < list_store->length; i++)
1603 if (i < old_location ||
1606 else if (i >= old_location &&
1608 new_order[i] = i + 1;
1609 else if (i == new_location)
1610 new_order[i] = old_location;
1613 for (i = 0; i < list_store->length; i++)
1615 if (i < new_location ||
1618 else if (i > new_location &&
1620 new_order[i] = i - 1;
1621 else if (i == new_location)
1622 new_order[i] = old_location;
1625 tmp_path = gtk_tree_path_new ();
1626 tmp_iter.user_data = NULL;
1628 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1629 tmp_path, &tmp_iter,
1632 gtk_tree_path_free (tmp_path);
1637 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1638 gint *sort_column_id,
1639 GtkTreeSortOrder *order)
1641 GtkListStore *list_store = (GtkListStore *) sortable;
1643 g_return_val_if_fail (sortable != NULL, FALSE);
1644 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1646 if (list_store->sort_column_id == -1)
1650 * sort_column_id = list_store->sort_column_id;
1652 * order = list_store->order;
1657 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1658 gint sort_column_id,
1659 GtkTreeSortOrder order)
1661 GtkListStore *list_store = (GtkListStore *) sortable;
1664 g_return_if_fail (sortable != NULL);
1665 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1667 for (list = list_store->sort_list; list; list = list->next)
1669 GtkTreeDataSortHeader *header = (GtkTreeDataSortHeader*) list->data;
1670 if (header->sort_column_id == sort_column_id)
1673 g_return_if_fail (list != NULL);
1675 if ((list_store->sort_column_id == sort_column_id) &&
1676 (list_store->order == order))
1679 list_store->sort_column_id = sort_column_id;
1680 list_store->order = order;
1682 if (list_store->sort_column_id >= 0)
1683 gtk_list_store_sort (list_store);
1685 gtk_tree_sortable_sort_column_changed (sortable);
1689 gtk_list_store_sort_column_id_set_func (GtkTreeSortable *sortable,
1690 gint sort_column_id,
1691 GtkTreeIterCompareFunc func,
1693 GtkDestroyNotify destroy)
1695 GtkListStore *list_store = (GtkListStore *) sortable;
1696 GtkTreeDataSortHeader *header = NULL;
1699 g_return_if_fail (sortable != NULL);
1700 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1701 g_return_if_fail (func != NULL);
1703 for (list = list_store->sort_list; list; list = list->next)
1705 header = (GtkTreeDataSortHeader*) list->data;
1706 if (header->sort_column_id == sort_column_id)
1712 header = g_new0 (GtkTreeDataSortHeader, 1);
1713 header->sort_column_id = sort_column_id;
1714 list_store->sort_list = g_list_append (list_store->sort_list, header);
1717 if (header->destroy)
1718 (* header->destroy) (header->data);
1720 header->func = func;
1721 header->data = data;
1722 header->destroy = destroy;