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);
517 if (G_SLIST (iter->user_data) == G_SLIST (GTK_LIST_STORE (tree_model)->tail))
519 retval = gtk_tree_path_new ();
520 gtk_tree_path_append_index (retval, GTK_LIST_STORE (tree_model)->length - 1);
524 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
526 if (list == G_SLIST (iter->user_data))
533 retval = gtk_tree_path_new ();
534 gtk_tree_path_append_index (retval, i);
539 gtk_list_store_get_value (GtkTreeModel *tree_model,
544 GtkTreeDataList *list;
545 gint tmp_column = column;
547 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
548 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
549 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
551 list = G_SLIST (iter->user_data)->data;
553 while (tmp_column-- > 0 && list)
557 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
559 _gtk_tree_data_list_node_to_value (list,
560 GTK_LIST_STORE (tree_model)->column_headers[column],
565 gtk_list_store_iter_next (GtkTreeModel *tree_model,
568 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
569 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
571 iter->user_data = G_SLIST (iter->user_data)->next;
573 return (iter->user_data != NULL);
577 gtk_list_store_iter_children (GtkTreeModel *tree_model,
581 /* this is a list, nodes have no children */
585 /* but if parent == NULL we return the list itself as children of the
589 if (GTK_LIST_STORE (tree_model)->root)
591 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
592 iter->user_data = GTK_LIST_STORE (tree_model)->root;
600 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
607 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
610 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
612 return GTK_LIST_STORE (tree_model)->length;
614 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
619 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
626 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
631 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
635 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
636 iter->user_data = child;
644 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
652 gtk_list_store_real_set_value (GtkListStore *list_store,
658 GtkTreeDataList *list;
659 GtkTreeDataList *prev;
660 GValue real_value = {0, };
661 gboolean converted = FALSE;
662 gint orig_column = column;
663 gboolean retval = FALSE;
665 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
666 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
667 g_return_val_if_fail (column >= 0 && column < list_store->n_columns, FALSE);
668 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
670 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
672 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
673 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
675 g_warning ("%s: Unable to convert from %s to %s\n",
677 g_type_name (G_VALUE_TYPE (value)),
678 g_type_name (list_store->column_headers[column]));
681 if (!g_value_transform (value, &real_value))
683 g_warning ("%s: Unable to make conversion from %s to %s\n",
685 g_type_name (G_VALUE_TYPE (value)),
686 g_type_name (list_store->column_headers[column]));
687 g_value_unset (&real_value);
693 prev = list = G_SLIST (iter->user_data)->data;
700 _gtk_tree_data_list_value_to_node (list, &real_value);
702 _gtk_tree_data_list_value_to_node (list, value);
705 g_value_unset (&real_value);
714 if (G_SLIST (iter->user_data)->data == NULL)
716 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
721 list = prev->next = _gtk_tree_data_list_alloc ();
727 list->next = _gtk_tree_data_list_alloc ();
734 _gtk_tree_data_list_value_to_node (list, &real_value);
736 _gtk_tree_data_list_value_to_node (list, value);
739 g_value_unset (&real_value);
741 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
742 gtk_list_store_sort_iter_changed (list_store, iter, orig_column);
749 * gtk_list_store_set_value:
750 * @list_store: A #GtkListStore
751 * @iter: A valid #GtkTreeIter for the row being modified
752 * @column: column number to modify
753 * @value: new value for the cell
755 * Sets the data in the cell specified by @iter and @column.
756 * The type of @value must be convertible to the type of the
761 gtk_list_store_set_value (GtkListStore *list_store,
766 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
767 g_return_if_fail (VALID_ITER (iter, list_store));
768 g_return_if_fail (column >= 0 && column < list_store->n_columns);
769 g_return_if_fail (G_IS_VALUE (value));
771 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
775 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
776 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
777 gtk_tree_path_free (path);
782 * gtk_list_store_set_valist:
783 * @list_store: A #GtkListStore
784 * @iter: A valid #GtkTreeIter for the row being modified
785 * @var_args: va_list of column/value pairs
787 * See gtk_list_store_set(); this version takes a va_list for use by language
792 gtk_list_store_set_valist (GtkListStore *list_store,
797 gboolean emit_signal = FALSE;
798 gboolean maybe_need_sort = FALSE;
799 GtkTreeIterCompareFunc func = NULL;
801 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
802 g_return_if_fail (VALID_ITER (iter, list_store));
804 column = va_arg (var_args, gint);
806 if (GTK_LIST_STORE_IS_SORTED (list_store))
808 if (list_store->sort_column_id != -1)
810 GtkTreeDataSortHeader *header;
811 header = _gtk_tree_data_list_get_header (list_store->sort_list,
812 list_store->sort_column_id);
813 g_return_if_fail (header != NULL);
814 g_return_if_fail (header->func != NULL);
819 func = list_store->default_sort_func;
823 if (func != gtk_tree_data_list_compare_func)
824 maybe_need_sort = TRUE;
828 GValue value = { 0, };
831 if (column >= list_store->n_columns)
833 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
836 g_value_init (&value, list_store->column_headers[column]);
838 G_VALUE_COLLECT (&value, var_args, 0, &error);
841 g_warning ("%s: %s", G_STRLOC, error);
844 /* we purposely leak the value here, it might not be
845 * in a sane state if an error condition occoured
850 /* FIXME: instead of calling this n times, refactor with above */
851 emit_signal = gtk_list_store_real_set_value (list_store,
855 FALSE) || emit_signal;
857 if (func == gtk_tree_data_list_compare_func &&
858 column == list_store->sort_column_id)
859 maybe_need_sort = TRUE;
861 g_value_unset (&value);
863 column = va_arg (var_args, gint);
866 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
867 gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
873 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
874 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
875 gtk_tree_path_free (path);
880 * gtk_list_store_set:
881 * @list_store: a #GtkListStore
882 * @iter: row iterator
883 * @Varargs: pairs of column number and value, terminated with -1
885 * Sets the value of one or more cells in the row referenced by @iter.
886 * The variable argument list should contain integer column numbers,
887 * each column number followed by the value to be set.
888 * The list is terminated by a -1. For example, to set column 0 with type
889 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
890 * 0, "Foo", -1)</literal>.
893 gtk_list_store_set (GtkListStore *list_store,
899 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
900 g_return_if_fail (iter != NULL);
901 g_return_if_fail (iter->stamp == list_store->stamp);
903 va_start (var_args, iter);
904 gtk_list_store_set_valist (list_store, iter, var_args);
909 remove_link_saving_prev (GSList *list,
924 prev->next = link->next;
943 gtk_list_store_remove_silently (GtkListStore *list_store,
947 if (G_SLIST (iter->user_data)->data)
949 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
950 list_store->column_headers);
951 G_SLIST (iter->user_data)->data = NULL;
957 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
958 G_SLIST (iter->user_data),
961 list_store->length -= 1;
963 if (iter->user_data == list_store->tail)
964 list_store->tail = prev;
969 * gtk_list_store_remove:
970 * @list_store: A #GtkListStore
971 * @iter: A valid #GtkTreeIter
973 * Removes the given row from the list store. After being removed,
974 * @iter is set to be the next valid row, or invalidated if it pointed
975 * to the last row in @list_store.
979 gtk_list_store_remove (GtkListStore *list_store,
985 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
986 g_return_if_fail (VALID_ITER (iter, list_store));
988 next = G_SLIST (iter->user_data)->next;
989 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
991 validate_list_store (list_store);
993 gtk_list_store_remove_silently (list_store, iter, path);
995 validate_list_store (list_store);
997 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
998 gtk_tree_path_free (path);
1002 iter->stamp = list_store->stamp;
1003 iter->user_data = next;
1012 insert_after (GtkListStore *list_store,
1016 g_return_if_fail (sibling != NULL);
1017 g_return_if_fail (new_list != NULL);
1019 /* insert new node after list */
1020 new_list->next = sibling->next;
1021 sibling->next = new_list;
1023 /* if list was the tail, the new node is the new tail */
1024 if (sibling == ((GSList *) list_store->tail))
1025 list_store->tail = new_list;
1027 list_store->length += 1;
1031 * gtk_list_store_insert:
1032 * @list_store: A #GtkListStore
1033 * @iter: An unset #GtkTreeIter to set to the new row
1034 * @position: position to insert the new row
1036 * Creates a new row at @position. @iter will be changed to point to this new
1037 * row. If @position is larger than the number of rows on the list, then the
1038 * new row will be appended to the list. The row will be empty before this
1039 * function is called. To fill in values, you need to call gtk_list_store_set()
1040 * or gtk_list_store_set_value().
1044 gtk_list_store_insert (GtkListStore *list_store,
1052 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1053 g_return_if_fail (iter != NULL);
1054 g_return_if_fail (position >= 0);
1056 list_store->columns_dirty = TRUE;
1058 if (position == 0 ||
1059 GTK_LIST_STORE_IS_SORTED (list_store))
1061 gtk_list_store_prepend (list_store, iter);
1065 new_list = g_slist_alloc ();
1067 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
1071 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
1075 insert_after (list_store, list, new_list);
1077 iter->stamp = list_store->stamp;
1078 iter->user_data = new_list;
1080 validate_list_store (list_store);
1082 path = gtk_tree_path_new ();
1083 gtk_tree_path_append_index (path, position);
1084 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1085 gtk_tree_path_free (path);
1089 * gtk_list_store_insert_before:
1090 * @list_store: A #GtkListStore
1091 * @iter: An unset #GtkTreeIter to set to the new row
1092 * @sibling: A valid #GtkTreeIter, or %NULL
1094 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
1095 * appended to the end of the list. @iter will be changed to point to this new
1096 * row. The row will be empty before this function is called. To fill in values,
1097 * you need to call gtk_list_store_set() or gtk_list_store_set_value().
1101 gtk_list_store_insert_before (GtkListStore *list_store,
1103 GtkTreeIter *sibling)
1106 GSList *list, *prev, *new_list;
1109 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1110 g_return_if_fail (iter != NULL);
1112 g_return_if_fail (VALID_ITER (sibling, list_store));
1114 list_store->columns_dirty = TRUE;
1116 if (GTK_LIST_STORE_IS_SORTED (list_store))
1118 gtk_list_store_prepend (list_store, iter);
1122 if (sibling == NULL)
1124 gtk_list_store_append (list_store, iter);
1128 new_list = g_slist_alloc ();
1131 list = list_store->root;
1132 while (list && list != sibling->user_data)
1139 if (list != sibling->user_data)
1141 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1145 /* if there are no nodes, we become the list tail, otherwise we
1146 * are inserting before any existing nodes so we can't change
1150 if (list_store->root == NULL)
1151 list_store->tail = new_list;
1155 new_list->next = prev->next;
1156 prev->next = new_list;
1160 new_list->next = list_store->root;
1161 list_store->root = new_list;
1164 iter->stamp = list_store->stamp;
1165 iter->user_data = new_list;
1167 list_store->length += 1;
1169 validate_list_store (list_store);
1171 path = gtk_tree_path_new ();
1172 gtk_tree_path_append_index (path, i);
1173 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1174 gtk_tree_path_free (path);
1178 * gtk_list_store_insert_after:
1179 * @list_store: A #GtkListStore
1180 * @iter: An unset #GtkTreeIter to set to the new row
1181 * @sibling: A valid #GtkTreeIter, or %NULL
1183 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1184 * prepended to the beginning of the list. @iter will be changed to point to
1185 * this new row. The row will be empty after this function is called. To fill
1186 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1190 gtk_list_store_insert_after (GtkListStore *list_store,
1192 GtkTreeIter *sibling)
1195 GSList *list, *new_list;
1198 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1199 g_return_if_fail (iter != NULL);
1201 g_return_if_fail (VALID_ITER (sibling, list_store));
1203 list_store->columns_dirty = TRUE;
1205 if (sibling == NULL ||
1206 GTK_LIST_STORE_IS_SORTED (list_store))
1208 gtk_list_store_prepend (list_store, iter);
1212 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1215 g_return_if_fail (list == sibling->user_data);
1217 new_list = g_slist_alloc ();
1219 insert_after (list_store, list, new_list);
1221 iter->stamp = list_store->stamp;
1222 iter->user_data = new_list;
1224 validate_list_store (list_store);
1226 path = gtk_tree_path_new ();
1227 gtk_tree_path_append_index (path, i + 1);
1228 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1229 gtk_tree_path_free (path);
1233 * gtk_list_store_prepend:
1234 * @list_store: A #GtkListStore
1235 * @iter: An unset #GtkTreeIter to set to the prepend row
1237 * Prepends a new row to @list_store. @iter will be changed to point to this new
1238 * row. The row will be empty after this function is called. To fill in
1239 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1243 gtk_list_store_prepend (GtkListStore *list_store,
1248 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1249 g_return_if_fail (iter != NULL);
1251 iter->stamp = list_store->stamp;
1252 iter->user_data = g_slist_alloc ();
1254 list_store->columns_dirty = TRUE;
1256 if (list_store->root == NULL)
1257 list_store->tail = iter->user_data;
1259 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1260 list_store->root = iter->user_data;
1262 list_store->length += 1;
1264 validate_list_store (list_store);
1266 path = gtk_tree_path_new ();
1267 gtk_tree_path_append_index (path, 0);
1268 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1269 gtk_tree_path_free (path);
1273 * gtk_list_store_append:
1274 * @list_store: A #GtkListStore
1275 * @iter: An unset #GtkTreeIter to set to the appended row
1277 * Appends a new row to @list_store. @iter will be changed to point to this new
1278 * row. The row will be empty after this function is called. To fill in
1279 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1283 gtk_list_store_append (GtkListStore *list_store,
1288 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1289 g_return_if_fail (iter != NULL);
1291 list_store->columns_dirty = TRUE;
1293 if (GTK_LIST_STORE_IS_SORTED (list_store))
1295 gtk_list_store_prepend (list_store, iter);
1299 iter->stamp = list_store->stamp;
1300 iter->user_data = g_slist_alloc ();
1302 if (list_store->tail)
1303 ((GSList *)list_store->tail)->next = iter->user_data;
1305 list_store->root = iter->user_data;
1307 list_store->tail = iter->user_data;
1309 list_store->length += 1;
1311 validate_list_store (list_store);
1313 path = gtk_tree_path_new ();
1314 gtk_tree_path_append_index (path, list_store->length - 1);
1315 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1316 gtk_tree_path_free (path);
1320 * gtk_list_store_clear:
1321 * @list_store: a #GtkListStore.
1323 * Removes all rows from the list store.
1327 gtk_list_store_clear (GtkListStore *list_store)
1330 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1332 while (list_store->root)
1334 iter.stamp = list_store->stamp;
1335 iter.user_data = list_store->root;
1336 gtk_list_store_remove (list_store, &iter);
1342 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1346 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1348 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1352 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1359 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1361 GtkSelectionData *selection_data)
1363 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1365 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1366 * target, because the default handler does it for us, but
1367 * we do anyway for the convenience of someone maybe overriding the
1371 if (gtk_tree_set_row_drag_data (selection_data,
1372 GTK_TREE_MODEL (drag_source),
1379 /* FIXME handle text targets at least. */
1386 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1388 GtkSelectionData *selection_data)
1390 GtkTreeModel *tree_model;
1391 GtkListStore *list_store;
1392 GtkTreeModel *src_model = NULL;
1393 GtkTreePath *src_path = NULL;
1394 gboolean retval = FALSE;
1396 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1398 tree_model = GTK_TREE_MODEL (drag_dest);
1399 list_store = GTK_LIST_STORE (drag_dest);
1401 if (gtk_tree_get_row_drag_data (selection_data,
1404 src_model == tree_model)
1406 /* Copy the given row to a new position */
1407 GtkTreeIter src_iter;
1408 GtkTreeIter dest_iter;
1411 if (!gtk_tree_model_get_iter (src_model,
1418 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1419 prev = gtk_tree_path_copy (dest);
1421 if (!gtk_tree_path_prev (prev))
1423 /* dest was the first spot in the list; which means we are supposed
1426 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1433 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1437 GtkTreeIter tmp_iter = dest_iter;
1438 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1445 gtk_tree_path_free (prev);
1447 /* If we succeeded in creating dest_iter, copy data from src
1451 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1452 GtkTreeDataList *copy_head = NULL;
1453 GtkTreeDataList *copy_prev = NULL;
1454 GtkTreeDataList *copy_iter = NULL;
1461 copy_iter = _gtk_tree_data_list_node_copy (dl,
1462 list_store->column_headers[col]);
1464 if (copy_head == NULL)
1465 copy_head = copy_iter;
1468 copy_prev->next = copy_iter;
1470 copy_prev = copy_iter;
1476 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1477 G_SLIST (dest_iter.user_data)->data = copy_head;
1479 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1480 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter);
1481 gtk_tree_path_free (path);
1486 /* FIXME maybe add some data targets eventually, or handle text
1487 * targets in the simple case.
1494 gtk_tree_path_free (src_path);
1500 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1501 GtkTreePath *dest_path,
1502 GtkSelectionData *selection_data)
1505 GtkTreeModel *src_model = NULL;
1506 GtkTreePath *src_path = NULL;
1507 gboolean retval = FALSE;
1509 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1511 if (!gtk_tree_get_row_drag_data (selection_data,
1516 if (src_model != GTK_TREE_MODEL (drag_dest))
1519 if (gtk_tree_path_get_depth (dest_path) != 1)
1522 /* can drop before any existing node, or before one past any existing. */
1524 indices = gtk_tree_path_get_indices (dest_path);
1526 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1530 gtk_tree_path_free (src_path);
1537 typedef struct _SortTuple
1544 gtk_list_store_compare_func (gconstpointer a,
1548 GtkListStore *list_store = user_data;
1549 GSList *el_a; /* Los Angeles? */
1554 GtkTreeIterCompareFunc func;
1558 if (list_store->sort_column_id != -1)
1560 GtkTreeDataSortHeader *header;
1562 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1563 list_store->sort_column_id);
1564 g_return_val_if_fail (header != NULL, 0);
1565 g_return_val_if_fail (header->func != NULL, 0);
1567 func = header->func;
1568 data = header->data;
1572 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1573 func = list_store->default_sort_func;
1574 data = list_store->default_sort_data;
1577 el_a = ((SortTuple *) a)->el;
1578 el_b = ((SortTuple *) b)->el;
1580 iter_a.stamp = list_store->stamp;
1581 iter_a.user_data = el_a;
1582 iter_b.stamp = list_store->stamp;
1583 iter_b.user_data = el_b;
1585 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1587 if (list_store->order == GTK_SORT_DESCENDING)
1591 else if (retval < 0)
1598 gtk_list_store_sort (GtkListStore *list_store)
1606 if (list_store->length <= 1)
1609 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1611 list = G_SLIST (list_store->root);
1613 sort_array = g_array_sized_new (FALSE, FALSE,
1615 list_store->length);
1617 for (i = 0; i < list_store->length; i++)
1621 /* If this fails, we are in an inconsistent state. Bad */
1622 g_return_if_fail (list != NULL);
1626 g_array_append_val (sort_array, tuple);
1631 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1633 for (i = 0; i < list_store->length - 1; i++)
1634 g_array_index (sort_array, SortTuple, i).el->next =
1635 g_array_index (sort_array, SortTuple, i + 1).el;
1636 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1637 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1639 /* Let the world know about our new order */
1640 new_order = g_new (gint, list_store->length);
1641 for (i = 0; i < list_store->length; i++)
1642 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1644 path = gtk_tree_path_new ();
1645 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1646 path, NULL, new_order);
1647 gtk_tree_path_free (path);
1649 g_array_free (sort_array, TRUE);
1653 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1658 GSList *prev = NULL;
1659 GSList *next = NULL;
1660 GSList *list = G_SLIST (list_store->root);
1661 GtkTreePath *tmp_path;
1662 GtkTreeIter tmp_iter;
1669 GtkTreeIterCompareFunc func;
1672 if (list_store->length < 2)
1675 tmp_iter.stamp = list_store->stamp;
1677 if (list_store->sort_column_id != -1)
1679 GtkTreeDataSortHeader *header;
1680 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1681 list_store->sort_column_id);
1682 g_return_if_fail (header != NULL);
1683 g_return_if_fail (header->func != NULL);
1684 func = header->func;
1685 data = header->data;
1689 g_return_if_fail (list_store->default_sort_func != NULL);
1690 func = list_store->default_sort_func;
1691 data = list_store->default_sort_data;
1694 /* If it's the built in function, we don't sort. */
1695 if (func == gtk_tree_data_list_compare_func &&
1696 list_store->sort_column_id != column)
1700 /* First we find the iter, its prev, and its next */
1703 if (list == G_SLIST (iter->user_data))
1709 g_assert (list != NULL);
1713 /* Check the common case, where we don't need to sort it moved. */
1716 tmp_iter.user_data = prev;
1717 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1722 tmp_iter.user_data = next;
1723 cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1727 if (list_store->order == GTK_SORT_DESCENDING)
1740 if (prev == NULL && cmp_b <= 0)
1742 else if (next == NULL && cmp_a <= 0)
1744 else if (prev != NULL && next != NULL &&
1745 cmp_a <= 0 && cmp_b <= 0)
1748 /* We actually need to sort it */
1749 /* First, remove the old link. */
1752 list_store->root = next;
1756 list_store->tail = prev;
1759 /* FIXME: as an optimization, we can potentially start at next */
1761 list = G_SLIST (list_store->root);
1763 tmp_iter.user_data = list;
1764 if (list_store->order == GTK_SORT_DESCENDING)
1765 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1767 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1769 while ((list->next) && (cmp_a > 0))
1774 tmp_iter.user_data = list;
1775 if (list_store->order == GTK_SORT_DESCENDING)
1776 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1778 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1781 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),
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);