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,
68 static void gtk_list_store_set_n_columns (GtkListStore *list_store,
70 static void gtk_list_store_set_column_type (GtkListStore *list_store,
76 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
78 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
80 GtkSelectionData *selection_data);
81 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
83 GtkSelectionData *selection_data);
84 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
85 GtkTreeModel *src_model,
86 GtkTreePath *src_path,
87 GtkTreePath *dest_path);
91 static void gtk_list_store_sort (GtkListStore *list_store);
92 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
95 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
97 GtkTreeSortOrder *order);
98 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
100 GtkTreeSortOrder order);
101 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
103 GtkTreeIterCompareFunc func,
105 GtkDestroyNotify destroy);
109 validate_list_store (GtkListStore *list_store)
111 if (gtk_debug_flags & GTK_DEBUG_TREE)
113 g_assert (g_slist_length (list_store->root) == list_store->length);
115 g_assert (g_slist_last (list_store->root) == list_store->tail);
120 gtk_list_store_get_type (void)
122 static GType list_store_type = 0;
124 if (!list_store_type)
126 static const GTypeInfo list_store_info =
128 sizeof (GtkListStoreClass),
129 NULL, /* base_init */
130 NULL, /* base_finalize */
131 (GClassInitFunc) gtk_list_store_class_init,
132 NULL, /* class_finalize */
133 NULL, /* class_data */
134 sizeof (GtkListStore),
136 (GInstanceInitFunc) gtk_list_store_init,
139 static const GInterfaceInfo tree_model_info =
141 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
146 static const GInterfaceInfo drag_source_info =
148 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
153 static const GInterfaceInfo drag_dest_info =
155 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
160 static const GInterfaceInfo sortable_info =
162 (GInterfaceInitFunc) gtk_list_store_sortable_init,
167 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
168 g_type_add_interface_static (list_store_type,
171 g_type_add_interface_static (list_store_type,
172 GTK_TYPE_TREE_DRAG_SOURCE,
174 g_type_add_interface_static (list_store_type,
175 GTK_TYPE_TREE_DRAG_DEST,
177 g_type_add_interface_static (list_store_type,
178 GTK_TYPE_TREE_SORTABLE,
182 return list_store_type;
186 gtk_list_store_class_init (GtkListStoreClass *class)
188 GObjectClass *object_class;
190 object_class = (GObjectClass*) class;
194 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
196 iface->get_flags = gtk_list_store_get_flags;
197 iface->get_n_columns = gtk_list_store_get_n_columns;
198 iface->get_column_type = gtk_list_store_get_column_type;
199 iface->get_iter = gtk_list_store_get_iter;
200 iface->get_path = gtk_list_store_get_path;
201 iface->get_value = gtk_list_store_get_value;
202 iface->iter_next = gtk_list_store_iter_next;
203 iface->iter_children = gtk_list_store_iter_children;
204 iface->iter_has_child = gtk_list_store_iter_has_child;
205 iface->iter_n_children = gtk_list_store_iter_n_children;
206 iface->iter_nth_child = gtk_list_store_iter_nth_child;
207 iface->iter_parent = gtk_list_store_iter_parent;
211 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
213 iface->drag_data_delete = gtk_list_store_drag_data_delete;
214 iface->drag_data_get = gtk_list_store_drag_data_get;
218 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
220 iface->drag_data_received = gtk_list_store_drag_data_received;
221 iface->row_drop_possible = gtk_list_store_row_drop_possible;
225 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
227 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
228 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
229 iface->set_sort_func = gtk_list_store_set_sort_func;
233 gtk_list_store_init (GtkListStore *list_store)
235 list_store->root = NULL;
236 list_store->tail = NULL;
237 list_store->sort_list = NULL;
238 list_store->stamp = g_random_int ();
239 list_store->length = 0;
240 list_store->sort_column_id = -1;
244 * gtk_list_store_new:
245 * @n_columns: number of columns in the list store
246 * @Varargs: all #GType types for the columns, from first to last
248 * Creates a new list store as with @n_columns columns each of the types passed
249 * in. As an example, gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
250 * GDK_TYPE_PIXBUF); will create a new GtkListStore with three columns, of type
251 * int, string and GDkPixbuf respectively.
253 * Return value: a new #GtkListStore
256 gtk_list_store_new (gint n_columns,
259 GtkListStore *retval;
263 g_return_val_if_fail (n_columns > 0, NULL);
265 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
266 gtk_list_store_set_n_columns (retval, n_columns);
268 va_start (args, n_columns);
270 for (i = 0; i < n_columns; i++)
272 GType type = va_arg (args, GType);
273 if (! _gtk_tree_data_list_check_type (type))
275 g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (type));
276 g_object_unref (G_OBJECT (retval));
280 gtk_list_store_set_column_type (retval, i, type);
290 * gtk_list_store_newv:
291 * @n_columns: number of columns in the list store
292 * @types: an array of #GType types for the columns, from first to last
294 * Non vararg creation function. Used primarily by language bindings.
296 * Return value: a new #GtkListStore
299 gtk_list_store_newv (gint n_columns,
302 GtkListStore *retval;
305 g_return_val_if_fail (n_columns > 0, NULL);
307 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
308 gtk_list_store_set_n_columns (retval, n_columns);
310 for (i = 0; i < n_columns; i++)
312 if (! _gtk_tree_data_list_check_type (types[i]))
314 g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (types[i]));
315 g_object_unref (G_OBJECT (retval));
319 gtk_list_store_set_column_type (retval, i, types[i]);
326 gtk_list_store_set_n_columns (GtkListStore *list_store,
331 g_return_if_fail (list_store != NULL);
332 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
333 g_return_if_fail (n_columns > 0);
335 if (list_store->n_columns == n_columns)
338 new_columns = g_new0 (GType, n_columns);
339 if (list_store->column_headers)
341 /* copy the old header orders over */
342 if (n_columns >= list_store->n_columns)
343 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
345 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
347 g_free (list_store->column_headers);
350 if (list_store->sort_list)
351 _gtk_tree_data_list_header_free (list_store->sort_list);
353 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
355 list_store->column_headers = new_columns;
356 list_store->n_columns = n_columns;
360 gtk_list_store_set_column_type (GtkListStore *list_store,
364 g_return_if_fail (list_store != NULL);
365 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
366 g_return_if_fail (column >=0 && column < list_store->n_columns);
367 if (!_gtk_tree_data_list_check_type (type))
369 g_warning ("%s: Invalid type %s passed to gtk_list_store_new_with_types\n", G_STRLOC, g_type_name (type));
373 list_store->column_headers[column] = type;
376 /* Fulfill the GtkTreeModel requirements */
378 gtk_list_store_get_flags (GtkTreeModel *tree_model)
380 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
382 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
386 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
388 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
390 return GTK_LIST_STORE (tree_model)->n_columns;
394 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
397 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
398 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
399 index >= 0, G_TYPE_INVALID);
401 return GTK_LIST_STORE (tree_model)->column_headers[index];
405 gtk_list_store_get_iter (GtkTreeModel *tree_model,
412 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
413 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
415 i = gtk_tree_path_get_indices (path)[0];
417 if (i >= GTK_LIST_STORE (tree_model)->length)
420 list = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), i);
422 /* If this fails, list_store->length has gotten mangled. */
425 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
426 iter->user_data = list;
431 gtk_list_store_get_path (GtkTreeModel *tree_model,
438 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
439 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
441 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
443 if (list == G_SLIST (iter->user_data))
450 retval = gtk_tree_path_new ();
451 gtk_tree_path_append_index (retval, i);
456 gtk_list_store_get_value (GtkTreeModel *tree_model,
461 GtkTreeDataList *list;
462 gint tmp_column = column;
464 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
465 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
466 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
468 list = G_SLIST (iter->user_data)->data;
470 while (tmp_column-- > 0 && list)
474 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
476 _gtk_tree_data_list_node_to_value (list,
477 GTK_LIST_STORE (tree_model)->column_headers[column],
482 gtk_list_store_iter_next (GtkTreeModel *tree_model,
485 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
486 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
488 iter->user_data = G_SLIST (iter->user_data)->next;
490 return (iter->user_data != NULL);
494 gtk_list_store_iter_children (GtkTreeModel *tree_model,
498 /* this is a list, nodes have no children */
502 /* but if parent == NULL we return the list itself as children of the
506 if (GTK_LIST_STORE (tree_model)->root)
508 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
509 iter->user_data = GTK_LIST_STORE (tree_model)->root;
517 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
524 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
527 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
529 if (iter->user_data == NULL)
530 return GTK_LIST_STORE (tree_model)->length;
536 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
543 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
548 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
552 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
553 iter->user_data = child;
561 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
568 /* Public accessors */
569 /* This is a somewhat inelegant function that does a lot of list
570 * manipulations on it's own.
574 * gtk_list_store_set_value:
575 * @store: a #GtkListStore
576 * @iter: iterator for the row you're modifying
577 * @column: column number to modify
578 * @value: new value for the cell
580 * Sets the data in the cell specified by @iter and @column.
581 * The type of @value must be convertible to the type of the
586 gtk_list_store_set_value (GtkListStore *list_store,
591 GtkTreeDataList *list;
592 GtkTreeDataList *prev;
594 GValue real_value = {0, };
595 gboolean converted = FALSE;
596 gint orig_column = column;
598 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
599 g_return_if_fail (iter != NULL);
600 g_return_if_fail (column >= 0 && column < list_store->n_columns);
601 g_return_if_fail (G_IS_VALUE (value));
603 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
605 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
606 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
608 g_warning ("%s: Unable to convert from %s to %s\n",
610 g_type_name (G_VALUE_TYPE (value)),
611 g_type_name (list_store->column_headers[column]));
614 if (!g_value_transform (value, &real_value))
616 g_warning ("%s: Unable to make conversion from %s to %s\n",
618 g_type_name (G_VALUE_TYPE (value)),
619 g_type_name (list_store->column_headers[column]));
620 g_value_unset (&real_value);
626 prev = list = G_SLIST (iter->user_data)->data;
632 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
634 _gtk_tree_data_list_value_to_node (list, &real_value);
636 _gtk_tree_data_list_value_to_node (list, value);
637 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
638 gtk_tree_path_free (path);
640 g_value_unset (&real_value);
649 if (G_SLIST (iter->user_data)->data == NULL)
651 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
656 list = prev->next = _gtk_tree_data_list_alloc ();
662 list->next = _gtk_tree_data_list_alloc ();
668 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
670 _gtk_tree_data_list_value_to_node (list, &real_value);
672 _gtk_tree_data_list_value_to_node (list, value);
673 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
674 gtk_tree_path_free (path);
676 g_value_unset (&real_value);
678 if (GTK_LIST_STORE_IS_SORTED (list_store))
679 gtk_list_store_sort_iter_changed (list_store, iter, orig_column);
683 * gtk_list_store_set_valist:
684 * @list_store: a #GtkListStore
685 * @iter: row to set data for
686 * @var_args: va_list of column/value pairs
688 * See gtk_list_store_set(); this version takes a va_list for
689 * use by language bindings.
693 gtk_list_store_set_valist (GtkListStore *list_store,
699 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
701 column = va_arg (var_args, gint);
705 GValue value = { 0, };
708 if (column >= list_store->n_columns)
710 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
713 g_value_init (&value, list_store->column_headers[column]);
715 G_VALUE_COLLECT (&value, var_args, 0, &error);
718 g_warning ("%s: %s", G_STRLOC, error);
721 /* we purposely leak the value here, it might not be
722 * in a sane state if an error condition occoured
727 /* FIXME: instead of calling this n times, refactor with above */
728 gtk_list_store_set_value (list_store,
733 g_value_unset (&value);
735 column = va_arg (var_args, gint);
740 * gtk_list_store_set:
741 * @list_store: a #GtkListStore
742 * @iter: row iterator
743 * @Varargs: pairs of column number and value, terminated with -1
745 * Sets the value of one or more cells in the row referenced by @iter.
746 * The variable argument list should contain integer column numbers,
747 * each column number followed by the value to be set.
748 * The list is terminated by a -1. For example, to set column 0 with type
749 * %G_TYPE_STRING to "Foo", you would write gtk_list_store_set (store, iter,
753 gtk_list_store_set (GtkListStore *list_store,
759 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
761 va_start (var_args, iter);
762 gtk_list_store_set_valist (list_store, iter, var_args);
767 remove_link_saving_prev (GSList *list,
782 prev->next = link->next;
801 gtk_list_store_remove_silently (GtkListStore *list_store,
805 if (G_SLIST (iter->user_data)->data)
807 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
808 list_store->column_headers);
809 G_SLIST (iter->user_data)->data = NULL;
815 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
816 G_SLIST (iter->user_data),
819 list_store->length -= 1;
821 if (iter->user_data == list_store->tail)
822 list_store->tail = prev;
825 list_store->stamp ++;
829 * gtk_list_store_remove:
830 * @store: a #GtkListStore
831 * @iter: a row in @list_store
833 * Removes the given row from the list store, emitting the
834 * "deleted" signal on #GtkTreeModel.
838 gtk_list_store_remove (GtkListStore *list_store,
843 g_return_if_fail (list_store != NULL);
844 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
845 g_return_if_fail (iter->user_data != NULL);
847 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
849 validate_list_store (list_store);
851 gtk_list_store_remove_silently (list_store, iter, path);
853 validate_list_store (list_store);
855 list_store->stamp ++;
856 gtk_tree_model_deleted (GTK_TREE_MODEL (list_store), path);
857 gtk_tree_path_free (path);
861 insert_after (GtkListStore *list_store,
865 g_return_if_fail (sibling != NULL);
866 g_return_if_fail (new_list != NULL);
868 /* insert new node after list */
869 new_list->next = sibling->next;
870 sibling->next = new_list;
872 /* if list was the tail, the new node is the new tail */
873 if (sibling == ((GSList *) list_store->tail))
874 list_store->tail = new_list;
876 list_store->length += 1;
880 * gtk_list_store_insert:
881 * @store: a #GtkListStore
882 * @iter: iterator to initialize with the new row
883 * @position: position to insert the new row
885 * Creates a new row at @position, initializing @iter to point to the
886 * new row, and emitting the "inserted" signal from the #GtkTreeModel
891 gtk_list_store_insert (GtkListStore *list_store,
899 g_return_if_fail (list_store != NULL);
900 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
901 g_return_if_fail (iter != NULL);
902 g_return_if_fail (position >= 0);
905 GTK_LIST_STORE_IS_SORTED (list_store))
907 gtk_list_store_prepend (list_store, iter);
911 new_list = g_slist_alloc ();
913 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
917 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
921 insert_after (list_store, list, new_list);
923 iter->stamp = list_store->stamp;
924 iter->user_data = new_list;
926 validate_list_store (list_store);
928 path = gtk_tree_path_new ();
929 gtk_tree_path_append_index (path, position);
930 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
931 gtk_tree_path_free (path);
935 * gtk_list_store_insert_before:
936 * @store: a #GtkListStore
937 * @iter: iterator to initialize with the new row
938 * @sibling: an existing row
940 * Inserts a new row before @sibling, initializing @iter to point to
941 * the new row, and emitting the "inserted" signal from the
942 * #GtkTreeModel interface.
946 gtk_list_store_insert_before (GtkListStore *list_store,
948 GtkTreeIter *sibling)
951 GSList *list, *prev, *new_list;
954 g_return_if_fail (list_store != NULL);
955 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
956 g_return_if_fail (iter != NULL);
958 if (GTK_LIST_STORE_IS_SORTED (list_store))
960 gtk_list_store_prepend (list_store, iter);
966 gtk_list_store_append (list_store, iter);
970 new_list = g_slist_alloc ();
973 list = list_store->root;
974 while (list && list != sibling->user_data)
981 if (list != sibling->user_data)
983 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
987 /* if there are no nodes, we become the list tail, otherwise we
988 * are inserting before any existing nodes so we can't change
992 if (list_store->root == NULL)
993 list_store->tail = new_list;
997 new_list->next = prev->next;
998 prev->next = new_list;
1002 new_list->next = list_store->root;
1003 list_store->root = new_list;
1006 iter->stamp = list_store->stamp;
1007 iter->user_data = new_list;
1009 list_store->length += 1;
1011 validate_list_store (list_store);
1013 path = gtk_tree_path_new ();
1014 gtk_tree_path_append_index (path, i);
1015 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1016 gtk_tree_path_free (path);
1020 * gtk_list_store_insert_after:
1021 * @store: a #GtkListStore
1022 * @iter: iterator to initialize with the new row
1023 * @sibling: an existing row
1025 * Inserts a new row after @sibling, initializing @iter to point to
1026 * the new row, and emitting the "inserted" signal from the
1027 * #GtkTreeModel interface.
1031 gtk_list_store_insert_after (GtkListStore *list_store,
1033 GtkTreeIter *sibling)
1036 GSList *list, *new_list;
1039 g_return_if_fail (list_store != NULL);
1040 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1041 g_return_if_fail (iter != NULL);
1043 g_return_if_fail (sibling->stamp == list_store->stamp);
1045 if (sibling == NULL ||
1046 GTK_LIST_STORE_IS_SORTED (list_store))
1048 gtk_list_store_prepend (list_store, iter);
1052 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1055 g_return_if_fail (list == sibling->user_data);
1057 new_list = g_slist_alloc ();
1059 insert_after (list_store, list, new_list);
1061 iter->stamp = list_store->stamp;
1062 iter->user_data = new_list;
1064 validate_list_store (list_store);
1066 path = gtk_tree_path_new ();
1067 gtk_tree_path_append_index (path, i);
1068 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1069 gtk_tree_path_free (path);
1073 * gtk_list_store_prepend:
1074 * @store: a #GtkListStore
1075 * @iter: iterator to initialize with new row
1077 * Prepends a row to @store, initializing @iter to point to the
1078 * new row, and emitting the "inserted" signal on the #GtkTreeModel
1079 * interface for the @store.
1083 gtk_list_store_prepend (GtkListStore *list_store,
1088 g_return_if_fail (list_store != NULL);
1089 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1090 g_return_if_fail (iter != NULL);
1092 iter->stamp = list_store->stamp;
1093 iter->user_data = g_slist_alloc ();
1095 if (list_store->root == NULL)
1096 list_store->tail = iter->user_data;
1098 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1099 list_store->root = iter->user_data;
1101 list_store->length += 1;
1103 validate_list_store (list_store);
1105 path = gtk_tree_path_new ();
1106 gtk_tree_path_append_index (path, 0);
1107 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1108 gtk_tree_path_free (path);
1112 * gtk_list_store_append:
1113 * @store: a #GtkListStore
1114 * @iter: iterator to initialize with the new row
1116 * Appends a row to @store, initializing @iter to point to the
1117 * new row, and emitting the "inserted" signal on the #GtkTreeModel
1118 * interface for the @store.
1122 gtk_list_store_append (GtkListStore *list_store,
1127 g_return_if_fail (list_store != NULL);
1128 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1129 g_return_if_fail (iter != NULL);
1131 if (GTK_LIST_STORE_IS_SORTED (list_store))
1133 gtk_list_store_prepend (list_store, iter);
1137 iter->stamp = list_store->stamp;
1138 iter->user_data = g_slist_alloc ();
1140 if (list_store->tail)
1141 ((GSList *)list_store->tail)->next = iter->user_data;
1143 list_store->root = iter->user_data;
1145 list_store->tail = iter->user_data;
1147 list_store->length += 1;
1149 validate_list_store (list_store);
1151 path = gtk_tree_path_new ();
1152 gtk_tree_path_append_index (path, list_store->length - 1);
1153 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1154 gtk_tree_path_free (path);
1158 gtk_list_store_clear (GtkListStore *list_store)
1161 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1163 while (list_store->root)
1165 iter.stamp = list_store->stamp;
1166 iter.user_data = list_store->root;
1167 gtk_list_store_remove (list_store, &iter);
1173 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1177 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1179 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1183 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1190 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1192 GtkSelectionData *selection_data)
1194 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1196 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1197 * target, because the default handler does it for us, but
1198 * we do anyway for the convenience of someone maybe overriding the
1202 if (gtk_selection_data_set_tree_row (selection_data,
1203 GTK_TREE_MODEL (drag_source),
1210 /* FIXME handle text targets at least. */
1217 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1219 GtkSelectionData *selection_data)
1221 GtkTreeModel *tree_model;
1222 GtkListStore *list_store;
1223 GtkTreeModel *src_model = NULL;
1224 GtkTreePath *src_path = NULL;
1225 gboolean retval = FALSE;
1227 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1229 tree_model = GTK_TREE_MODEL (drag_dest);
1230 list_store = GTK_LIST_STORE (drag_dest);
1232 if (gtk_selection_data_get_tree_row (selection_data,
1235 src_model == tree_model)
1237 /* Copy the given row to a new position */
1238 GtkTreeIter src_iter;
1239 GtkTreeIter dest_iter;
1242 if (!gtk_tree_model_get_iter (src_model,
1249 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1250 prev = gtk_tree_path_copy (dest);
1252 if (!gtk_tree_path_prev (prev))
1254 /* dest was the first spot in the list; which means we are supposed
1257 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1264 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1268 GtkTreeIter tmp_iter = dest_iter;
1269 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1276 gtk_tree_path_free (prev);
1278 /* If we succeeded in creating dest_iter, copy data from src
1282 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1283 GtkTreeDataList *copy_head = NULL;
1284 GtkTreeDataList *copy_prev = NULL;
1285 GtkTreeDataList *copy_iter = NULL;
1292 copy_iter = _gtk_tree_data_list_node_copy (dl,
1293 list_store->column_headers[col]);
1295 if (copy_head == NULL)
1296 copy_head = copy_iter;
1299 copy_prev->next = copy_iter;
1301 copy_prev = copy_iter;
1307 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1308 G_SLIST (dest_iter.user_data)->data = copy_head;
1310 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1311 gtk_tree_model_range_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter, path, &dest_iter);
1312 gtk_tree_path_free (path);
1317 /* FIXME maybe add some data targets eventually, or handle text
1318 * targets in the simple case.
1325 gtk_tree_path_free (src_path);
1331 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1332 GtkTreeModel *src_model,
1333 GtkTreePath *src_path,
1334 GtkTreePath *dest_path)
1338 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1340 if (src_model != GTK_TREE_MODEL (drag_dest))
1343 if (gtk_tree_path_get_depth (dest_path) != 1)
1346 /* can drop before any existing node, or before one past any existing. */
1348 indices = gtk_tree_path_get_indices (dest_path);
1350 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1358 typedef struct _SortTuple
1365 gtk_list_store_compare_func (gconstpointer a,
1369 GtkListStore *list_store = user_data;
1370 GtkTreeDataSortHeader *header = NULL;
1371 GSList *el_a; /* Los Angeles? */
1377 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1378 list_store->sort_column_id);
1380 g_return_val_if_fail (header != NULL, 0);
1381 g_return_val_if_fail (header->func != NULL, 0);
1383 el_a = ((SortTuple *) a)->el;
1384 el_b = ((SortTuple *) b)->el;
1386 iter_a.stamp = list_store->stamp;
1387 iter_a.user_data = el_a;
1388 iter_b.stamp = list_store->stamp;
1389 iter_b.user_data = el_b;
1391 retval = (* header->func) (GTK_TREE_MODEL (list_store),
1395 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1399 else if (retval < 0)
1406 gtk_list_store_sort (GtkListStore *list_store)
1408 GtkTreeDataSortHeader *header = NULL;
1416 if (list_store->length <= 1)
1419 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1421 header = _gtk_tree_data_list_get_header (list_store->sort_list, list_store->sort_column_id);
1423 /* We want to make sure that we have a function */
1424 g_return_if_fail (header != NULL);
1425 g_return_if_fail (header->func != NULL);
1427 list = G_SLIST (list_store->root);
1429 sort_array = g_array_sized_new (FALSE, FALSE,
1431 list_store->length);
1433 for (i = 0; i < list_store->length; i++)
1437 /* If this fails, we are in an inconsistent state. Bad */
1438 g_return_if_fail (list != NULL);
1442 g_array_append_val (sort_array, tuple);
1447 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1449 for (i = 0; i < list_store->length - 1; i++)
1450 g_array_index (sort_array, SortTuple, i).el->next =
1451 g_array_index (sort_array, SortTuple, i + 1).el;
1452 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1453 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1455 /* Let the world know about our new order */
1456 new_order = g_new (gint, list_store->length);
1457 for (i = 0; i < list_store->length; i++)
1458 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1459 path = gtk_tree_path_new ();
1460 iter.stamp = list_store->stamp;
1461 iter.user_data = NULL;
1462 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1463 path, &iter, new_order);
1464 gtk_tree_path_free (path);
1466 g_array_free (sort_array, TRUE);
1470 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1475 GtkTreeDataSortHeader *header;
1476 GSList *prev = NULL;
1477 GSList *next = NULL;
1478 GSList *list = G_SLIST (list_store->root);
1479 GtkTreePath *tmp_path;
1480 GtkTreeIter tmp_iter;
1488 if (list_store->length < 2)
1491 tmp_iter.stamp = list_store->stamp;
1492 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1493 list_store->sort_column_id);
1494 g_return_if_fail (header != NULL);
1495 g_return_if_fail (header->func != NULL);
1497 /* If it's the built in function, we don't sort. */
1498 if (header->func == gtk_tree_data_list_compare_func &&
1499 list_store->sort_column_id != column)
1503 /* First we find the iter, its prev, and its next */
1506 if (list == G_SLIST (iter->user_data))
1512 g_assert (list != NULL);
1516 /* Check the common case, where we don't need to sort it moved. */
1519 tmp_iter.user_data = prev;
1520 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1527 tmp_iter.user_data = next;
1528 cmp_b = (* header->func) (GTK_TREE_MODEL (list_store),
1534 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1547 if (prev == NULL && cmp_b <= 0)
1549 else if (next == NULL && cmp_a <= 0)
1551 else if (prev != NULL && next != NULL &&
1552 cmp_a <= 0 && cmp_b <= 0)
1555 /* We actually need to sort it */
1556 /* First, remove the old link. */
1559 list_store->root = next;
1563 list_store->tail = prev;
1566 /* FIXME: as an optimization, we can potentially start at next */
1568 list = G_SLIST (list_store->root);
1570 tmp_iter.user_data = list;
1571 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1572 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1573 &tmp_iter, iter, header->data);
1575 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1576 iter, &tmp_iter, header->data);
1578 while ((list->next) && (cmp_a > 0))
1583 tmp_iter.user_data = list;
1584 if (list_store->order == GTK_TREE_SORT_DESCENDING)
1585 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1586 &tmp_iter, iter, header->data);
1588 cmp_a = (* header->func) (GTK_TREE_MODEL (list_store),
1589 iter, &tmp_iter, header->data);
1592 if ((!list->next) && (cmp_a > 0))
1594 list->next = G_SLIST (iter->user_data);
1595 list_store->tail = list->next;
1599 prev->next = G_SLIST (iter->user_data);
1600 G_SLIST (iter->user_data)->next = list;
1604 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1605 list_store->root = G_SLIST (iter->user_data);
1608 /* Emit the reordered signal. */
1609 new_order = g_new (int, list_store->length);
1610 if (old_location < new_location)
1611 for (i = 0; i < list_store->length; i++)
1613 if (i < old_location ||
1616 else if (i >= old_location &&
1618 new_order[i] = i + 1;
1619 else if (i == new_location)
1620 new_order[i] = old_location;
1623 for (i = 0; i < list_store->length; i++)
1625 if (i < new_location ||
1628 else if (i > new_location &&
1630 new_order[i] = i - 1;
1631 else if (i == new_location)
1632 new_order[i] = old_location;
1635 tmp_path = gtk_tree_path_new ();
1636 tmp_iter.user_data = NULL;
1638 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1639 tmp_path, &tmp_iter,
1642 gtk_tree_path_free (tmp_path);
1647 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1648 gint *sort_column_id,
1649 GtkTreeSortOrder *order)
1651 GtkListStore *list_store = (GtkListStore *) sortable;
1653 g_return_val_if_fail (sortable != NULL, FALSE);
1654 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1656 if (list_store->sort_column_id == -1)
1660 * sort_column_id = list_store->sort_column_id;
1662 * order = list_store->order;
1667 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1668 gint sort_column_id,
1669 GtkTreeSortOrder order)
1671 GtkListStore *list_store = (GtkListStore *) sortable;
1674 g_return_if_fail (sortable != NULL);
1675 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1677 for (list = list_store->sort_list; list; list = list->next)
1679 GtkTreeDataSortHeader *header = (GtkTreeDataSortHeader*) list->data;
1680 if (header->sort_column_id == sort_column_id)
1683 g_return_if_fail (list != NULL);
1685 if ((list_store->sort_column_id == sort_column_id) &&
1686 (list_store->order == order))
1689 list_store->sort_column_id = sort_column_id;
1690 list_store->order = order;
1692 if (list_store->sort_column_id >= 0)
1693 gtk_list_store_sort (list_store);
1695 gtk_tree_sortable_sort_column_changed (sortable);
1699 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
1700 gint sort_column_id,
1701 GtkTreeIterCompareFunc func,
1703 GtkDestroyNotify destroy)
1705 GtkListStore *list_store = (GtkListStore *) sortable;
1706 GtkTreeDataSortHeader *header = NULL;
1709 g_return_if_fail (sortable != NULL);
1710 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1711 g_return_if_fail (func != NULL);
1713 for (list = list_store->sort_list; list; list = list->next)
1715 header = (GtkTreeDataSortHeader*) list->data;
1716 if (header->sort_column_id == sort_column_id)
1722 header = g_new0 (GtkTreeDataSortHeader, 1);
1723 header->sort_column_id = sort_column_id;
1724 list_store->sort_list = g_list_append (list_store->sort_list, header);
1727 if (header->destroy)
1728 (* header->destroy) (header->data);
1730 header->func = func;
1731 header->data = data;
1732 header->destroy = destroy;