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 != -2)
30 #define VALID_ITER(iter, list_store) (iter!= NULL && iter->user_data != NULL && list_store->stamp == iter->stamp)
32 static void gtk_list_store_init (GtkListStore *list_store);
33 static void gtk_list_store_class_init (GtkListStoreClass *class);
34 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
35 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
36 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
37 static void gtk_list_store_sortable_init (GtkTreeSortableIface *iface);
38 static void gtk_list_store_finalize (GObject *object);
39 static guint gtk_list_store_get_flags (GtkTreeModel *tree_model);
40 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
41 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
43 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
46 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
48 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
52 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
54 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
57 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
59 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
61 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
65 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
70 static void gtk_list_store_set_n_columns (GtkListStore *list_store,
72 static void gtk_list_store_set_column_type (GtkListStore *list_store,
78 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
80 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
82 GtkSelectionData *selection_data);
83 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
85 GtkSelectionData *selection_data);
86 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
87 GtkTreePath *dest_path,
88 GtkSelectionData *selection_data);
92 static void gtk_list_store_sort (GtkListStore *list_store);
93 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
96 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
99 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
102 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
104 GtkTreeIterCompareFunc func,
106 GtkDestroyNotify destroy);
107 static void gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
108 GtkTreeIterCompareFunc func,
110 GtkDestroyNotify destroy);
111 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable);
114 static GObjectClass *parent_class = NULL;
118 validate_list_store (GtkListStore *list_store)
120 if (gtk_debug_flags & GTK_DEBUG_TREE)
122 g_assert (g_slist_length (list_store->root) == list_store->length);
124 g_assert (g_slist_last (list_store->root) == list_store->tail);
129 gtk_list_store_get_type (void)
131 static GType list_store_type = 0;
133 if (!list_store_type)
135 static const GTypeInfo list_store_info =
137 sizeof (GtkListStoreClass),
138 NULL, /* base_init */
139 NULL, /* base_finalize */
140 (GClassInitFunc) gtk_list_store_class_init,
141 NULL, /* class_finalize */
142 NULL, /* class_data */
143 sizeof (GtkListStore),
145 (GInstanceInitFunc) gtk_list_store_init,
148 static const GInterfaceInfo tree_model_info =
150 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
155 static const GInterfaceInfo drag_source_info =
157 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
162 static const GInterfaceInfo drag_dest_info =
164 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
169 static const GInterfaceInfo sortable_info =
171 (GInterfaceInitFunc) gtk_list_store_sortable_init,
176 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
177 g_type_add_interface_static (list_store_type,
180 g_type_add_interface_static (list_store_type,
181 GTK_TYPE_TREE_DRAG_SOURCE,
183 g_type_add_interface_static (list_store_type,
184 GTK_TYPE_TREE_DRAG_DEST,
186 g_type_add_interface_static (list_store_type,
187 GTK_TYPE_TREE_SORTABLE,
191 return list_store_type;
195 gtk_list_store_class_init (GtkListStoreClass *class)
197 GObjectClass *object_class;
199 parent_class = g_type_class_peek_parent (class);
200 object_class = (GObjectClass*) class;
202 object_class->finalize = gtk_list_store_finalize;
206 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
208 iface->get_flags = gtk_list_store_get_flags;
209 iface->get_n_columns = gtk_list_store_get_n_columns;
210 iface->get_column_type = gtk_list_store_get_column_type;
211 iface->get_iter = gtk_list_store_get_iter;
212 iface->get_path = gtk_list_store_get_path;
213 iface->get_value = gtk_list_store_get_value;
214 iface->iter_next = gtk_list_store_iter_next;
215 iface->iter_children = gtk_list_store_iter_children;
216 iface->iter_has_child = gtk_list_store_iter_has_child;
217 iface->iter_n_children = gtk_list_store_iter_n_children;
218 iface->iter_nth_child = gtk_list_store_iter_nth_child;
219 iface->iter_parent = gtk_list_store_iter_parent;
223 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
225 iface->drag_data_delete = gtk_list_store_drag_data_delete;
226 iface->drag_data_get = gtk_list_store_drag_data_get;
230 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
232 iface->drag_data_received = gtk_list_store_drag_data_received;
233 iface->row_drop_possible = gtk_list_store_row_drop_possible;
237 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
239 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
240 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
241 iface->set_sort_func = gtk_list_store_set_sort_func;
242 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
243 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
247 gtk_list_store_init (GtkListStore *list_store)
249 list_store->root = NULL;
250 list_store->tail = NULL;
251 list_store->sort_list = NULL;
252 list_store->stamp = g_random_int ();
253 list_store->length = 0;
254 list_store->sort_column_id = -2;
255 list_store->columns_dirty = FALSE;
259 * gtk_list_store_new:
260 * @n_columns: number of columns in the list store
261 * @Varargs: all #GType types for the columns, from first to last
263 * Creates a new list store as with @n_columns columns each of the types passed
264 * in. As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
265 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
266 * int, string and #GdkPixbuf respectively.
268 * Return value: a new #GtkListStore
271 gtk_list_store_new (gint n_columns,
274 GtkListStore *retval;
278 g_return_val_if_fail (n_columns > 0, NULL);
280 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
281 gtk_list_store_set_n_columns (retval, n_columns);
283 va_start (args, n_columns);
285 for (i = 0; i < n_columns; i++)
287 GType type = va_arg (args, GType);
288 if (! _gtk_tree_data_list_check_type (type))
290 g_warning ("%s: Invalid type %s passed to gtk_list_store_new\n", G_STRLOC, g_type_name (type));
291 g_object_unref (G_OBJECT (retval));
295 gtk_list_store_set_column_type (retval, i, type);
305 * gtk_list_store_newv:
306 * @n_columns: number of columns in the list store
307 * @types: an array of #GType types for the columns, from first to last
309 * Non-vararg creation function. Used primarily by language bindings.
311 * Return value: a new #GtkListStore
314 gtk_list_store_newv (gint n_columns,
317 GtkListStore *retval;
320 g_return_val_if_fail (n_columns > 0, NULL);
322 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
323 gtk_list_store_set_n_columns (retval, n_columns);
325 for (i = 0; i < n_columns; i++)
327 if (! _gtk_tree_data_list_check_type (types[i]))
329 g_warning ("%s: Invalid type %s passed to gtk_list_store_newv\n", G_STRLOC, g_type_name (types[i]));
330 g_object_unref (G_OBJECT (retval));
334 gtk_list_store_set_column_type (retval, i, types[i]);
341 * gtk_list_store_set_column_types:
342 * @list_store: A #GtkListStore
343 * @n_columns: Number of columns for the list store
344 * @types: An array length n of #GTypes
346 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
347 * and should only be used when constructing a new #GtkListStore. It will not
348 * function after a row has been added, or a method on the #GtkTreeModel
349 * interface is called.
352 gtk_list_store_set_column_types (GtkListStore *list_store,
358 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
359 g_return_if_fail (list_store->columns_dirty == 0);
361 gtk_list_store_set_n_columns (list_store, n_columns);
362 for (i = 0; i < n_columns; i++)
364 if (! _gtk_tree_data_list_check_type (types[i]))
366 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_types\n", G_STRLOC, g_type_name (types[i]));
369 gtk_list_store_set_column_type (list_store, i, types[i]);
374 gtk_list_store_set_n_columns (GtkListStore *list_store,
379 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
380 g_return_if_fail (n_columns > 0);
382 if (list_store->n_columns == n_columns)
385 new_columns = g_new0 (GType, n_columns);
386 if (list_store->column_headers)
388 /* copy the old header orders over */
389 if (n_columns >= list_store->n_columns)
390 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
392 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
394 g_free (list_store->column_headers);
397 if (list_store->sort_list)
398 _gtk_tree_data_list_header_free (list_store->sort_list);
400 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
402 list_store->column_headers = new_columns;
403 list_store->n_columns = n_columns;
407 gtk_list_store_set_column_type (GtkListStore *list_store,
411 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
412 g_return_if_fail (column >=0 && column < list_store->n_columns);
413 if (!_gtk_tree_data_list_check_type (type))
415 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_type\n", G_STRLOC, g_type_name (type));
419 list_store->column_headers[column] = type;
423 gtk_list_store_finalize (GObject *object)
425 GtkListStore *list_store = GTK_LIST_STORE (object);
427 g_list_foreach (list_store->root, (GFunc) _gtk_tree_data_list_free, list_store->column_headers);
428 _gtk_tree_data_list_header_free (list_store->sort_list);
429 g_free (list_store->column_headers);
431 if (list_store->default_sort_destroy)
433 (* list_store->default_sort_destroy) (list_store->default_sort_data);
434 list_store->default_sort_destroy = NULL;
435 list_store->default_sort_data = NULL;
438 (* parent_class->finalize) (object);
441 /* Fulfill the GtkTreeModel requirements */
443 gtk_list_store_get_flags (GtkTreeModel *tree_model)
445 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
447 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
451 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
453 GtkListStore *list_store = (GtkListStore *) tree_model;
455 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
457 list_store->columns_dirty = TRUE;
459 return list_store->n_columns;
463 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
466 GtkListStore *list_store = (GtkListStore *) tree_model;
468 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
469 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
470 index >= 0, G_TYPE_INVALID);
472 list_store->columns_dirty = TRUE;
474 return list_store->column_headers[index];
478 gtk_list_store_get_iter (GtkTreeModel *tree_model,
482 GtkListStore *list_store = (GtkListStore *) tree_model;
486 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
487 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
489 list_store->columns_dirty = TRUE;
491 i = gtk_tree_path_get_indices (path)[0];
493 if (i >= list_store->length)
496 list = g_slist_nth (G_SLIST (list_store->root), i);
498 /* If this fails, list_store->length has gotten mangled. */
501 iter->stamp = list_store->stamp;
502 iter->user_data = list;
508 gtk_list_store_get_path (GtkTreeModel *tree_model,
515 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
516 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
518 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
520 if (list == G_SLIST (iter->user_data))
527 retval = gtk_tree_path_new ();
528 gtk_tree_path_append_index (retval, i);
533 gtk_list_store_get_value (GtkTreeModel *tree_model,
538 GtkTreeDataList *list;
539 gint tmp_column = column;
541 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
542 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
543 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
545 list = G_SLIST (iter->user_data)->data;
547 while (tmp_column-- > 0 && list)
551 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
553 _gtk_tree_data_list_node_to_value (list,
554 GTK_LIST_STORE (tree_model)->column_headers[column],
559 gtk_list_store_iter_next (GtkTreeModel *tree_model,
562 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
563 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
565 iter->user_data = G_SLIST (iter->user_data)->next;
567 return (iter->user_data != NULL);
571 gtk_list_store_iter_children (GtkTreeModel *tree_model,
575 /* this is a list, nodes have no children */
579 /* but if parent == NULL we return the list itself as children of the
583 if (GTK_LIST_STORE (tree_model)->root)
585 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
586 iter->user_data = GTK_LIST_STORE (tree_model)->root;
594 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
601 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
604 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
606 return GTK_LIST_STORE (tree_model)->length;
608 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
613 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
620 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
625 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
629 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
630 iter->user_data = child;
638 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
646 gtk_list_store_real_set_value (GtkListStore *list_store,
652 GtkTreeDataList *list;
653 GtkTreeDataList *prev;
655 GValue real_value = {0, };
656 gboolean converted = FALSE;
657 gint orig_column = column;
658 gboolean retval = FALSE;
660 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
661 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
662 g_return_val_if_fail (column >= 0 && column < list_store->n_columns, FALSE);
663 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
665 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
667 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
668 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
670 g_warning ("%s: Unable to convert from %s to %s\n",
672 g_type_name (G_VALUE_TYPE (value)),
673 g_type_name (list_store->column_headers[column]));
676 if (!g_value_transform (value, &real_value))
678 g_warning ("%s: Unable to make conversion from %s to %s\n",
680 g_type_name (G_VALUE_TYPE (value)),
681 g_type_name (list_store->column_headers[column]));
682 g_value_unset (&real_value);
688 prev = list = G_SLIST (iter->user_data)->data;
694 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
696 _gtk_tree_data_list_value_to_node (list, &real_value);
698 _gtk_tree_data_list_value_to_node (list, value);
700 gtk_tree_path_free (path);
702 g_value_unset (&real_value);
711 if (G_SLIST (iter->user_data)->data == NULL)
713 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
718 list = prev->next = _gtk_tree_data_list_alloc ();
724 list->next = _gtk_tree_data_list_alloc ();
730 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
732 _gtk_tree_data_list_value_to_node (list, &real_value);
734 _gtk_tree_data_list_value_to_node (list, value);
736 gtk_tree_path_free (path);
738 g_value_unset (&real_value);
740 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
741 gtk_list_store_sort_iter_changed (list_store, iter, orig_column);
748 * gtk_list_store_set_value:
749 * @list_store: A #GtkListStore
750 * @iter: A valid #GtkTreeIter for the row being modified
751 * @column: column number to modify
752 * @value: new value for the cell
754 * Sets the data in the cell specified by @iter and @column.
755 * The type of @value must be convertible to the type of the
760 gtk_list_store_set_value (GtkListStore *list_store,
765 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
766 g_return_if_fail (VALID_ITER (iter, list_store));
767 g_return_if_fail (column >= 0 && column < list_store->n_columns);
768 g_return_if_fail (G_IS_VALUE (value));
770 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
774 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
775 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
776 gtk_tree_path_free (path);
781 * gtk_list_store_set_valist:
782 * @list_store: A #GtkListStore
783 * @iter: A valid #GtkTreeIter for the row being modified
784 * @var_args: va_list of column/value pairs
786 * See gtk_list_store_set(); this version takes a va_list for use by language
791 gtk_list_store_set_valist (GtkListStore *list_store,
796 gboolean emit_signal = FALSE;
797 gboolean maybe_need_sort = FALSE;
798 GtkTreeIterCompareFunc func;
800 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
801 g_return_if_fail (VALID_ITER (iter, list_store));
803 column = va_arg (var_args, gint);
805 if (GTK_LIST_STORE_IS_SORTED (list_store))
807 if (list_store->sort_column_id != -1)
809 GtkTreeDataSortHeader *header;
810 header = _gtk_tree_data_list_get_header (list_store->sort_list,
811 list_store->sort_column_id);
812 g_return_if_fail (header != NULL);
813 g_return_if_fail (header->func != NULL);
818 func = list_store->default_sort_func;
822 if (func != gtk_tree_data_list_compare_func)
823 maybe_need_sort = TRUE;
827 GValue value = { 0, };
830 if (column >= list_store->n_columns)
832 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
835 g_value_init (&value, list_store->column_headers[column]);
837 G_VALUE_COLLECT (&value, var_args, 0, &error);
840 g_warning ("%s: %s", G_STRLOC, error);
843 /* we purposely leak the value here, it might not be
844 * in a sane state if an error condition occoured
849 /* FIXME: instead of calling this n times, refactor with above */
850 emit_signal = gtk_list_store_real_set_value (list_store,
854 FALSE) || emit_signal;
856 if (func == gtk_tree_data_list_compare_func &&
857 column == list_store->sort_column_id)
858 maybe_need_sort = TRUE;
860 g_value_unset (&value);
862 column = va_arg (var_args, gint);
865 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
866 gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
872 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
873 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
874 gtk_tree_path_free (path);
879 * gtk_list_store_set:
880 * @list_store: a #GtkListStore
881 * @iter: row iterator
882 * @Varargs: pairs of column number and value, terminated with -1
884 * Sets the value of one or more cells in the row referenced by @iter.
885 * The variable argument list should contain integer column numbers,
886 * each column number followed by the value to be set.
887 * The list is terminated by a -1. For example, to set column 0 with type
888 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
889 * 0, "Foo", -1)</literal>.
892 gtk_list_store_set (GtkListStore *list_store,
898 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
899 g_return_if_fail (iter != NULL);
900 g_return_if_fail (iter->stamp == list_store->stamp);
902 va_start (var_args, iter);
903 gtk_list_store_set_valist (list_store, iter, var_args);
908 remove_link_saving_prev (GSList *list,
923 prev->next = link->next;
942 gtk_list_store_remove_silently (GtkListStore *list_store,
946 if (G_SLIST (iter->user_data)->data)
948 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
949 list_store->column_headers);
950 G_SLIST (iter->user_data)->data = NULL;
956 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
957 G_SLIST (iter->user_data),
960 list_store->length -= 1;
962 if (iter->user_data == list_store->tail)
963 list_store->tail = prev;
966 list_store->stamp ++;
970 * gtk_list_store_remove:
971 * @list_store: A #GtkListStore
972 * @iter: A valid #GtkTreeIter
974 * Removes the given row from the list store. After being removed,
975 * @iter is set to be the next valid row, or invalidated if it pointed
976 * to the last row in @list_store.
980 gtk_list_store_remove (GtkListStore *list_store,
986 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
987 g_return_if_fail (VALID_ITER (iter, list_store));
989 next = G_SLIST (iter->user_data)->next;
990 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
992 validate_list_store (list_store);
994 gtk_list_store_remove_silently (list_store, iter, path);
996 validate_list_store (list_store);
998 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
999 gtk_tree_path_free (path);
1003 iter->stamp = list_store->stamp;
1004 iter->user_data = next;
1013 insert_after (GtkListStore *list_store,
1017 g_return_if_fail (sibling != NULL);
1018 g_return_if_fail (new_list != NULL);
1020 /* insert new node after list */
1021 new_list->next = sibling->next;
1022 sibling->next = new_list;
1024 /* if list was the tail, the new node is the new tail */
1025 if (sibling == ((GSList *) list_store->tail))
1026 list_store->tail = new_list;
1028 list_store->length += 1;
1032 * gtk_list_store_insert:
1033 * @list_store: A #GtkListStore
1034 * @iter: An unset #GtkTreeIter to set to the new row
1035 * @position: position to insert the new row
1037 * Creates a new row at @position. @iter will be changed to point to this new
1038 * row. If @position is larger than the number of rows on the list, then the
1039 * new row will be appended to the list. The row will be empty before this
1040 * function is called. To fill in values, you need to call gtk_list_store_set()
1041 * or gtk_list_store_set_value().
1045 gtk_list_store_insert (GtkListStore *list_store,
1053 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1054 g_return_if_fail (iter != NULL);
1055 g_return_if_fail (position >= 0);
1057 list_store->columns_dirty = TRUE;
1059 if (position == 0 ||
1060 GTK_LIST_STORE_IS_SORTED (list_store))
1062 gtk_list_store_prepend (list_store, iter);
1066 new_list = g_slist_alloc ();
1068 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
1072 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
1076 insert_after (list_store, list, new_list);
1078 iter->stamp = list_store->stamp;
1079 iter->user_data = new_list;
1081 validate_list_store (list_store);
1083 path = gtk_tree_path_new ();
1084 gtk_tree_path_append_index (path, position);
1085 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1086 gtk_tree_path_free (path);
1090 * gtk_list_store_insert_before:
1091 * @list_store: A #GtkListStore
1092 * @iter: An unset #GtkTreeIter to set to the new row
1093 * @sibling: A valid #GtkTreeIter, or %NULL
1095 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
1096 * appended to the end of the list. @iter will be changed to point to this new
1097 * row. The row will be empty before this function is called. To fill in values,
1098 * you need to call gtk_list_store_set() or gtk_list_store_set_value().
1102 gtk_list_store_insert_before (GtkListStore *list_store,
1104 GtkTreeIter *sibling)
1107 GSList *list, *prev, *new_list;
1110 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1111 g_return_if_fail (iter != NULL);
1113 g_return_if_fail (VALID_ITER (sibling, list_store));
1115 list_store->columns_dirty = TRUE;
1117 if (GTK_LIST_STORE_IS_SORTED (list_store))
1119 gtk_list_store_prepend (list_store, iter);
1123 if (sibling == NULL)
1125 gtk_list_store_append (list_store, iter);
1129 new_list = g_slist_alloc ();
1132 list = list_store->root;
1133 while (list && list != sibling->user_data)
1140 if (list != sibling->user_data)
1142 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1146 /* if there are no nodes, we become the list tail, otherwise we
1147 * are inserting before any existing nodes so we can't change
1151 if (list_store->root == NULL)
1152 list_store->tail = new_list;
1156 new_list->next = prev->next;
1157 prev->next = new_list;
1161 new_list->next = list_store->root;
1162 list_store->root = new_list;
1165 iter->stamp = list_store->stamp;
1166 iter->user_data = new_list;
1168 list_store->length += 1;
1170 validate_list_store (list_store);
1172 path = gtk_tree_path_new ();
1173 gtk_tree_path_append_index (path, i);
1174 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1175 gtk_tree_path_free (path);
1179 * gtk_list_store_insert_after:
1180 * @list_store: A #GtkListStore
1181 * @iter: An unset #GtkTreeIter to set to the new row
1182 * @sibling: A valid #GtkTreeIter, or %NULL
1184 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1185 * prepended to the beginning of the list. @iter will be changed to point to
1186 * this new row. The row will be empty after this function is called. To fill
1187 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1191 gtk_list_store_insert_after (GtkListStore *list_store,
1193 GtkTreeIter *sibling)
1196 GSList *list, *new_list;
1199 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1200 g_return_if_fail (iter != NULL);
1202 g_return_if_fail (VALID_ITER (sibling, list_store));
1204 list_store->columns_dirty = TRUE;
1206 if (sibling == NULL ||
1207 GTK_LIST_STORE_IS_SORTED (list_store))
1209 gtk_list_store_prepend (list_store, iter);
1213 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1216 g_return_if_fail (list == sibling->user_data);
1218 new_list = g_slist_alloc ();
1220 insert_after (list_store, list, new_list);
1222 iter->stamp = list_store->stamp;
1223 iter->user_data = new_list;
1225 validate_list_store (list_store);
1227 path = gtk_tree_path_new ();
1228 gtk_tree_path_append_index (path, i);
1229 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1230 gtk_tree_path_free (path);
1234 * gtk_list_store_prepend:
1235 * @list_store: A #GtkListStore
1236 * @iter: An unset #GtkTreeIter to set to the prepend row
1238 * Prepends a new row to @list_store. @iter will be changed to point to this new
1239 * row. The row will be empty after this function is called. To fill in
1240 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1244 gtk_list_store_prepend (GtkListStore *list_store,
1249 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1250 g_return_if_fail (iter != NULL);
1252 iter->stamp = list_store->stamp;
1253 iter->user_data = g_slist_alloc ();
1255 list_store->columns_dirty = TRUE;
1257 if (list_store->root == NULL)
1258 list_store->tail = iter->user_data;
1260 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1261 list_store->root = iter->user_data;
1263 list_store->length += 1;
1265 validate_list_store (list_store);
1267 path = gtk_tree_path_new ();
1268 gtk_tree_path_append_index (path, 0);
1269 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1270 gtk_tree_path_free (path);
1274 * gtk_list_store_append:
1275 * @list_store: A #GtkListStore
1276 * @iter: An unset #GtkTreeIter to set to the appended row
1278 * Appends a new row to @list_store. @iter will be changed to point to this new
1279 * row. The row will be empty after this function is called. To fill in
1280 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1284 gtk_list_store_append (GtkListStore *list_store,
1289 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1290 g_return_if_fail (iter != NULL);
1292 list_store->columns_dirty = TRUE;
1294 if (GTK_LIST_STORE_IS_SORTED (list_store))
1296 gtk_list_store_prepend (list_store, iter);
1300 iter->stamp = list_store->stamp;
1301 iter->user_data = g_slist_alloc ();
1303 if (list_store->tail)
1304 ((GSList *)list_store->tail)->next = iter->user_data;
1306 list_store->root = iter->user_data;
1308 list_store->tail = iter->user_data;
1310 list_store->length += 1;
1312 validate_list_store (list_store);
1314 path = gtk_tree_path_new ();
1315 gtk_tree_path_append_index (path, list_store->length - 1);
1316 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1317 gtk_tree_path_free (path);
1321 * gtk_list_store_clear:
1322 * @list_store: a #GtkListStore.
1324 * Removes all rows from the list store.
1328 gtk_list_store_clear (GtkListStore *list_store)
1331 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1333 while (list_store->root)
1335 iter.stamp = list_store->stamp;
1336 iter.user_data = list_store->root;
1337 gtk_list_store_remove (list_store, &iter);
1343 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1347 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1349 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1353 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1360 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1362 GtkSelectionData *selection_data)
1364 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1366 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1367 * target, because the default handler does it for us, but
1368 * we do anyway for the convenience of someone maybe overriding the
1372 if (gtk_tree_set_row_drag_data (selection_data,
1373 GTK_TREE_MODEL (drag_source),
1380 /* FIXME handle text targets at least. */
1387 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1389 GtkSelectionData *selection_data)
1391 GtkTreeModel *tree_model;
1392 GtkListStore *list_store;
1393 GtkTreeModel *src_model = NULL;
1394 GtkTreePath *src_path = NULL;
1395 gboolean retval = FALSE;
1397 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1399 tree_model = GTK_TREE_MODEL (drag_dest);
1400 list_store = GTK_LIST_STORE (drag_dest);
1402 if (gtk_tree_get_row_drag_data (selection_data,
1405 src_model == tree_model)
1407 /* Copy the given row to a new position */
1408 GtkTreeIter src_iter;
1409 GtkTreeIter dest_iter;
1412 if (!gtk_tree_model_get_iter (src_model,
1419 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1420 prev = gtk_tree_path_copy (dest);
1422 if (!gtk_tree_path_prev (prev))
1424 /* dest was the first spot in the list; which means we are supposed
1427 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1434 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1438 GtkTreeIter tmp_iter = dest_iter;
1439 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1446 gtk_tree_path_free (prev);
1448 /* If we succeeded in creating dest_iter, copy data from src
1452 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1453 GtkTreeDataList *copy_head = NULL;
1454 GtkTreeDataList *copy_prev = NULL;
1455 GtkTreeDataList *copy_iter = NULL;
1462 copy_iter = _gtk_tree_data_list_node_copy (dl,
1463 list_store->column_headers[col]);
1465 if (copy_head == NULL)
1466 copy_head = copy_iter;
1469 copy_prev->next = copy_iter;
1471 copy_prev = copy_iter;
1477 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1478 G_SLIST (dest_iter.user_data)->data = copy_head;
1480 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1481 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter);
1482 gtk_tree_path_free (path);
1487 /* FIXME maybe add some data targets eventually, or handle text
1488 * targets in the simple case.
1495 gtk_tree_path_free (src_path);
1501 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1502 GtkTreePath *dest_path,
1503 GtkSelectionData *selection_data)
1506 GtkTreeModel *src_model = NULL;
1507 GtkTreePath *src_path = NULL;
1508 gboolean retval = FALSE;
1510 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1512 if (!gtk_tree_get_row_drag_data (selection_data,
1517 if (src_model != GTK_TREE_MODEL (drag_dest))
1520 if (gtk_tree_path_get_depth (dest_path) != 1)
1523 /* can drop before any existing node, or before one past any existing. */
1525 indices = gtk_tree_path_get_indices (dest_path);
1527 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1531 gtk_tree_path_free (src_path);
1538 typedef struct _SortTuple
1545 gtk_list_store_compare_func (gconstpointer a,
1549 GtkListStore *list_store = user_data;
1550 GSList *el_a; /* Los Angeles? */
1555 GtkTreeIterCompareFunc func;
1559 if (list_store->sort_column_id != -1)
1561 GtkTreeDataSortHeader *header;
1563 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1564 list_store->sort_column_id);
1565 g_return_val_if_fail (header != NULL, 0);
1566 g_return_val_if_fail (header->func != NULL, 0);
1568 func = header->func;
1569 data = header->data;
1573 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1574 func = list_store->default_sort_func;
1575 data = list_store->default_sort_data;
1578 el_a = ((SortTuple *) a)->el;
1579 el_b = ((SortTuple *) b)->el;
1581 iter_a.stamp = list_store->stamp;
1582 iter_a.user_data = el_a;
1583 iter_b.stamp = list_store->stamp;
1584 iter_b.user_data = el_b;
1586 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1588 if (list_store->order == GTK_SORT_DESCENDING)
1592 else if (retval < 0)
1599 gtk_list_store_sort (GtkListStore *list_store)
1607 if (list_store->length <= 1)
1610 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1612 list = G_SLIST (list_store->root);
1614 sort_array = g_array_sized_new (FALSE, FALSE,
1616 list_store->length);
1618 for (i = 0; i < list_store->length; i++)
1622 /* If this fails, we are in an inconsistent state. Bad */
1623 g_return_if_fail (list != NULL);
1627 g_array_append_val (sort_array, tuple);
1632 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1634 for (i = 0; i < list_store->length - 1; i++)
1635 g_array_index (sort_array, SortTuple, i).el->next =
1636 g_array_index (sort_array, SortTuple, i + 1).el;
1637 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1638 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1640 /* Let the world know about our new order */
1641 new_order = g_new (gint, list_store->length);
1642 for (i = 0; i < list_store->length; i++)
1643 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1645 path = gtk_tree_path_new ();
1646 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1647 path, NULL, new_order);
1648 gtk_tree_path_free (path);
1650 g_array_free (sort_array, TRUE);
1654 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1659 GSList *prev = NULL;
1660 GSList *next = NULL;
1661 GSList *list = G_SLIST (list_store->root);
1662 GtkTreePath *tmp_path;
1663 GtkTreeIter tmp_iter;
1670 GtkTreeIterCompareFunc func;
1673 if (list_store->length < 2)
1676 tmp_iter.stamp = list_store->stamp;
1678 if (list_store->sort_column_id != -1)
1680 GtkTreeDataSortHeader *header;
1681 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1682 list_store->sort_column_id);
1683 g_return_if_fail (header != NULL);
1684 g_return_if_fail (header->func != NULL);
1685 func = header->func;
1686 data = header->data;
1690 g_return_if_fail (list_store->default_sort_func != NULL);
1691 func = list_store->default_sort_func;
1692 data = list_store->default_sort_data;
1695 /* If it's the built in function, we don't sort. */
1696 if (func == gtk_tree_data_list_compare_func &&
1697 list_store->sort_column_id != column)
1701 /* First we find the iter, its prev, and its next */
1704 if (list == G_SLIST (iter->user_data))
1710 g_assert (list != NULL);
1714 /* Check the common case, where we don't need to sort it moved. */
1717 tmp_iter.user_data = prev;
1718 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1723 tmp_iter.user_data = next;
1724 cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1728 if (list_store->order == GTK_SORT_DESCENDING)
1741 if (prev == NULL && cmp_b <= 0)
1743 else if (next == NULL && cmp_a <= 0)
1745 else if (prev != NULL && next != NULL &&
1746 cmp_a <= 0 && cmp_b <= 0)
1749 /* We actually need to sort it */
1750 /* First, remove the old link. */
1753 list_store->root = next;
1757 list_store->tail = prev;
1760 /* FIXME: as an optimization, we can potentially start at next */
1762 list = G_SLIST (list_store->root);
1764 tmp_iter.user_data = list;
1765 if (list_store->order == GTK_SORT_DESCENDING)
1766 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1768 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1770 while ((list->next) && (cmp_a > 0))
1775 tmp_iter.user_data = list;
1776 if (list_store->order == GTK_SORT_DESCENDING)
1777 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1779 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1782 if ((!list->next) && (cmp_a > 0))
1784 list->next = G_SLIST (iter->user_data);
1785 list_store->tail = list->next;
1789 prev->next = G_SLIST (iter->user_data);
1790 G_SLIST (iter->user_data)->next = list;
1794 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1795 list_store->root = G_SLIST (iter->user_data);
1798 /* Emit the reordered signal. */
1799 new_order = g_new (int, list_store->length);
1800 if (old_location < new_location)
1801 for (i = 0; i < list_store->length; i++)
1803 if (i < old_location ||
1806 else if (i >= old_location &&
1808 new_order[i] = i + 1;
1809 else if (i == new_location)
1810 new_order[i] = old_location;
1813 for (i = 0; i < list_store->length; i++)
1815 if (i < new_location ||
1818 else if (i > new_location &&
1820 new_order[i] = i - 1;
1821 else if (i == new_location)
1822 new_order[i] = old_location;
1825 tmp_path = gtk_tree_path_new ();
1826 tmp_iter.user_data = NULL;
1828 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1829 tmp_path, &tmp_iter,
1832 gtk_tree_path_free (tmp_path);
1837 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1838 gint *sort_column_id,
1841 GtkListStore *list_store = (GtkListStore *) sortable;
1843 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1845 if (list_store->sort_column_id == -1)
1849 * sort_column_id = list_store->sort_column_id;
1851 * order = list_store->order;
1856 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1857 gint sort_column_id,
1860 GtkListStore *list_store = (GtkListStore *) sortable;
1862 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1864 if ((list_store->sort_column_id == sort_column_id) &&
1865 (list_store->order == order))
1868 if (sort_column_id != -1)
1870 GtkTreeDataSortHeader *header = NULL;
1872 header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
1874 /* We want to make sure that we have a function */
1875 g_return_if_fail (header != NULL);
1876 g_return_if_fail (header->func != NULL);
1880 g_return_if_fail (list_store->default_sort_func != NULL);
1884 list_store->sort_column_id = sort_column_id;
1885 list_store->order = order;
1887 gtk_list_store_sort (list_store);
1889 gtk_tree_sortable_sort_column_changed (sortable);
1893 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
1894 gint sort_column_id,
1895 GtkTreeIterCompareFunc func,
1897 GtkDestroyNotify destroy)
1899 GtkListStore *list_store = (GtkListStore *) sortable;
1900 GtkTreeDataSortHeader *header = NULL;
1903 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1904 g_return_if_fail (func != NULL);
1906 for (list = list_store->sort_list; list; list = list->next)
1908 header = (GtkTreeDataSortHeader*) list->data;
1909 if (header->sort_column_id == sort_column_id)
1915 header = g_new0 (GtkTreeDataSortHeader, 1);
1916 header->sort_column_id = sort_column_id;
1917 list_store->sort_list = g_list_append (list_store->sort_list, header);
1920 if (header->destroy)
1921 (* header->destroy) (header->data);
1923 header->func = func;
1924 header->data = data;
1925 header->destroy = destroy;
1930 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
1931 GtkTreeIterCompareFunc func,
1933 GtkDestroyNotify destroy)
1935 GtkListStore *list_store = (GtkListStore *) sortable;
1937 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1939 if (list_store->default_sort_destroy)
1940 (* list_store->default_sort_destroy) (list_store->default_sort_data);
1942 list_store->default_sort_func = func;
1943 list_store->default_sort_data = data;
1944 list_store->default_sort_destroy = destroy;
1948 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
1950 GtkListStore *list_store = (GtkListStore *) sortable;
1952 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1954 return (list_store->default_sort_func != NULL);