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 = NULL;
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;
968 * gtk_list_store_remove:
969 * @list_store: A #GtkListStore
970 * @iter: A valid #GtkTreeIter
972 * Removes the given row from the list store. After being removed,
973 * @iter is set to be the next valid row, or invalidated if it pointed
974 * to the last row in @list_store.
978 gtk_list_store_remove (GtkListStore *list_store,
984 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
985 g_return_if_fail (VALID_ITER (iter, list_store));
987 next = G_SLIST (iter->user_data)->next;
988 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
990 validate_list_store (list_store);
992 gtk_list_store_remove_silently (list_store, iter, path);
994 validate_list_store (list_store);
996 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
997 gtk_tree_path_free (path);
1001 iter->stamp = list_store->stamp;
1002 iter->user_data = next;
1011 insert_after (GtkListStore *list_store,
1015 g_return_if_fail (sibling != NULL);
1016 g_return_if_fail (new_list != NULL);
1018 /* insert new node after list */
1019 new_list->next = sibling->next;
1020 sibling->next = new_list;
1022 /* if list was the tail, the new node is the new tail */
1023 if (sibling == ((GSList *) list_store->tail))
1024 list_store->tail = new_list;
1026 list_store->length += 1;
1030 * gtk_list_store_insert:
1031 * @list_store: A #GtkListStore
1032 * @iter: An unset #GtkTreeIter to set to the new row
1033 * @position: position to insert the new row
1035 * Creates a new row at @position. @iter will be changed to point to this new
1036 * row. If @position is larger than the number of rows on the list, then the
1037 * new row will be appended to the list. The row will be empty before this
1038 * function is called. To fill in values, you need to call gtk_list_store_set()
1039 * or gtk_list_store_set_value().
1043 gtk_list_store_insert (GtkListStore *list_store,
1051 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1052 g_return_if_fail (iter != NULL);
1053 g_return_if_fail (position >= 0);
1055 list_store->columns_dirty = TRUE;
1057 if (position == 0 ||
1058 GTK_LIST_STORE_IS_SORTED (list_store))
1060 gtk_list_store_prepend (list_store, iter);
1064 new_list = g_slist_alloc ();
1066 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
1070 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
1074 insert_after (list_store, list, new_list);
1076 iter->stamp = list_store->stamp;
1077 iter->user_data = new_list;
1079 validate_list_store (list_store);
1081 path = gtk_tree_path_new ();
1082 gtk_tree_path_append_index (path, position);
1083 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1084 gtk_tree_path_free (path);
1088 * gtk_list_store_insert_before:
1089 * @list_store: A #GtkListStore
1090 * @iter: An unset #GtkTreeIter to set to the new row
1091 * @sibling: A valid #GtkTreeIter, or %NULL
1093 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
1094 * appended to the end of the list. @iter will be changed to point to this new
1095 * row. The row will be empty before this function is called. To fill in values,
1096 * you need to call gtk_list_store_set() or gtk_list_store_set_value().
1100 gtk_list_store_insert_before (GtkListStore *list_store,
1102 GtkTreeIter *sibling)
1105 GSList *list, *prev, *new_list;
1108 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1109 g_return_if_fail (iter != NULL);
1111 g_return_if_fail (VALID_ITER (sibling, list_store));
1113 list_store->columns_dirty = TRUE;
1115 if (GTK_LIST_STORE_IS_SORTED (list_store))
1117 gtk_list_store_prepend (list_store, iter);
1121 if (sibling == NULL)
1123 gtk_list_store_append (list_store, iter);
1127 new_list = g_slist_alloc ();
1130 list = list_store->root;
1131 while (list && list != sibling->user_data)
1138 if (list != sibling->user_data)
1140 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1144 /* if there are no nodes, we become the list tail, otherwise we
1145 * are inserting before any existing nodes so we can't change
1149 if (list_store->root == NULL)
1150 list_store->tail = new_list;
1154 new_list->next = prev->next;
1155 prev->next = new_list;
1159 new_list->next = list_store->root;
1160 list_store->root = new_list;
1163 iter->stamp = list_store->stamp;
1164 iter->user_data = new_list;
1166 list_store->length += 1;
1168 validate_list_store (list_store);
1170 path = gtk_tree_path_new ();
1171 gtk_tree_path_append_index (path, i);
1172 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1173 gtk_tree_path_free (path);
1177 * gtk_list_store_insert_after:
1178 * @list_store: A #GtkListStore
1179 * @iter: An unset #GtkTreeIter to set to the new row
1180 * @sibling: A valid #GtkTreeIter, or %NULL
1182 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1183 * prepended to the beginning of the list. @iter will be changed to point to
1184 * this new row. The row will be empty after this function is called. To fill
1185 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1189 gtk_list_store_insert_after (GtkListStore *list_store,
1191 GtkTreeIter *sibling)
1194 GSList *list, *new_list;
1197 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1198 g_return_if_fail (iter != NULL);
1200 g_return_if_fail (VALID_ITER (sibling, list_store));
1202 list_store->columns_dirty = TRUE;
1204 if (sibling == NULL ||
1205 GTK_LIST_STORE_IS_SORTED (list_store))
1207 gtk_list_store_prepend (list_store, iter);
1211 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1214 g_return_if_fail (list == sibling->user_data);
1216 new_list = g_slist_alloc ();
1218 insert_after (list_store, list, new_list);
1220 iter->stamp = list_store->stamp;
1221 iter->user_data = new_list;
1223 validate_list_store (list_store);
1225 path = gtk_tree_path_new ();
1226 gtk_tree_path_append_index (path, i + 1);
1227 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1228 gtk_tree_path_free (path);
1232 * gtk_list_store_prepend:
1233 * @list_store: A #GtkListStore
1234 * @iter: An unset #GtkTreeIter to set to the prepend row
1236 * Prepends a new row to @list_store. @iter will be changed to point to this new
1237 * row. The row will be empty after this function is called. To fill in
1238 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1242 gtk_list_store_prepend (GtkListStore *list_store,
1247 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1248 g_return_if_fail (iter != NULL);
1250 iter->stamp = list_store->stamp;
1251 iter->user_data = g_slist_alloc ();
1253 list_store->columns_dirty = TRUE;
1255 if (list_store->root == NULL)
1256 list_store->tail = iter->user_data;
1258 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1259 list_store->root = iter->user_data;
1261 list_store->length += 1;
1263 validate_list_store (list_store);
1265 path = gtk_tree_path_new ();
1266 gtk_tree_path_append_index (path, 0);
1267 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1268 gtk_tree_path_free (path);
1272 * gtk_list_store_append:
1273 * @list_store: A #GtkListStore
1274 * @iter: An unset #GtkTreeIter to set to the appended row
1276 * Appends a new row to @list_store. @iter will be changed to point to this new
1277 * row. The row will be empty after this function is called. To fill in
1278 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1282 gtk_list_store_append (GtkListStore *list_store,
1287 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1288 g_return_if_fail (iter != NULL);
1290 list_store->columns_dirty = TRUE;
1292 if (GTK_LIST_STORE_IS_SORTED (list_store))
1294 gtk_list_store_prepend (list_store, iter);
1298 iter->stamp = list_store->stamp;
1299 iter->user_data = g_slist_alloc ();
1301 if (list_store->tail)
1302 ((GSList *)list_store->tail)->next = iter->user_data;
1304 list_store->root = iter->user_data;
1306 list_store->tail = iter->user_data;
1308 list_store->length += 1;
1310 validate_list_store (list_store);
1312 path = gtk_tree_path_new ();
1313 gtk_tree_path_append_index (path, list_store->length - 1);
1314 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1315 gtk_tree_path_free (path);
1319 * gtk_list_store_clear:
1320 * @list_store: a #GtkListStore.
1322 * Removes all rows from the list store.
1326 gtk_list_store_clear (GtkListStore *list_store)
1329 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1331 while (list_store->root)
1333 iter.stamp = list_store->stamp;
1334 iter.user_data = list_store->root;
1335 gtk_list_store_remove (list_store, &iter);
1341 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1345 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1347 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1351 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1358 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1360 GtkSelectionData *selection_data)
1362 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1364 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1365 * target, because the default handler does it for us, but
1366 * we do anyway for the convenience of someone maybe overriding the
1370 if (gtk_tree_set_row_drag_data (selection_data,
1371 GTK_TREE_MODEL (drag_source),
1378 /* FIXME handle text targets at least. */
1385 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1387 GtkSelectionData *selection_data)
1389 GtkTreeModel *tree_model;
1390 GtkListStore *list_store;
1391 GtkTreeModel *src_model = NULL;
1392 GtkTreePath *src_path = NULL;
1393 gboolean retval = FALSE;
1395 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1397 tree_model = GTK_TREE_MODEL (drag_dest);
1398 list_store = GTK_LIST_STORE (drag_dest);
1400 if (gtk_tree_get_row_drag_data (selection_data,
1403 src_model == tree_model)
1405 /* Copy the given row to a new position */
1406 GtkTreeIter src_iter;
1407 GtkTreeIter dest_iter;
1410 if (!gtk_tree_model_get_iter (src_model,
1417 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1418 prev = gtk_tree_path_copy (dest);
1420 if (!gtk_tree_path_prev (prev))
1422 /* dest was the first spot in the list; which means we are supposed
1425 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1432 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1436 GtkTreeIter tmp_iter = dest_iter;
1437 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1444 gtk_tree_path_free (prev);
1446 /* If we succeeded in creating dest_iter, copy data from src
1450 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1451 GtkTreeDataList *copy_head = NULL;
1452 GtkTreeDataList *copy_prev = NULL;
1453 GtkTreeDataList *copy_iter = NULL;
1460 copy_iter = _gtk_tree_data_list_node_copy (dl,
1461 list_store->column_headers[col]);
1463 if (copy_head == NULL)
1464 copy_head = copy_iter;
1467 copy_prev->next = copy_iter;
1469 copy_prev = copy_iter;
1475 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1476 G_SLIST (dest_iter.user_data)->data = copy_head;
1478 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1479 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter);
1480 gtk_tree_path_free (path);
1485 /* FIXME maybe add some data targets eventually, or handle text
1486 * targets in the simple case.
1493 gtk_tree_path_free (src_path);
1499 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1500 GtkTreePath *dest_path,
1501 GtkSelectionData *selection_data)
1504 GtkTreeModel *src_model = NULL;
1505 GtkTreePath *src_path = NULL;
1506 gboolean retval = FALSE;
1508 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1510 if (!gtk_tree_get_row_drag_data (selection_data,
1515 if (src_model != GTK_TREE_MODEL (drag_dest))
1518 if (gtk_tree_path_get_depth (dest_path) != 1)
1521 /* can drop before any existing node, or before one past any existing. */
1523 indices = gtk_tree_path_get_indices (dest_path);
1525 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1529 gtk_tree_path_free (src_path);
1536 typedef struct _SortTuple
1543 gtk_list_store_compare_func (gconstpointer a,
1547 GtkListStore *list_store = user_data;
1548 GSList *el_a; /* Los Angeles? */
1553 GtkTreeIterCompareFunc func;
1557 if (list_store->sort_column_id != -1)
1559 GtkTreeDataSortHeader *header;
1561 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1562 list_store->sort_column_id);
1563 g_return_val_if_fail (header != NULL, 0);
1564 g_return_val_if_fail (header->func != NULL, 0);
1566 func = header->func;
1567 data = header->data;
1571 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1572 func = list_store->default_sort_func;
1573 data = list_store->default_sort_data;
1576 el_a = ((SortTuple *) a)->el;
1577 el_b = ((SortTuple *) b)->el;
1579 iter_a.stamp = list_store->stamp;
1580 iter_a.user_data = el_a;
1581 iter_b.stamp = list_store->stamp;
1582 iter_b.user_data = el_b;
1584 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1586 if (list_store->order == GTK_SORT_DESCENDING)
1590 else if (retval < 0)
1597 gtk_list_store_sort (GtkListStore *list_store)
1605 if (list_store->length <= 1)
1608 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1610 list = G_SLIST (list_store->root);
1612 sort_array = g_array_sized_new (FALSE, FALSE,
1614 list_store->length);
1616 for (i = 0; i < list_store->length; i++)
1620 /* If this fails, we are in an inconsistent state. Bad */
1621 g_return_if_fail (list != NULL);
1625 g_array_append_val (sort_array, tuple);
1630 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1632 for (i = 0; i < list_store->length - 1; i++)
1633 g_array_index (sort_array, SortTuple, i).el->next =
1634 g_array_index (sort_array, SortTuple, i + 1).el;
1635 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1636 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1638 /* Let the world know about our new order */
1639 new_order = g_new (gint, list_store->length);
1640 for (i = 0; i < list_store->length; i++)
1641 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1643 path = gtk_tree_path_new ();
1644 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1645 path, NULL, new_order);
1646 gtk_tree_path_free (path);
1648 g_array_free (sort_array, TRUE);
1652 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1657 GSList *prev = NULL;
1658 GSList *next = NULL;
1659 GSList *list = G_SLIST (list_store->root);
1660 GtkTreePath *tmp_path;
1661 GtkTreeIter tmp_iter;
1668 GtkTreeIterCompareFunc func;
1671 if (list_store->length < 2)
1674 tmp_iter.stamp = list_store->stamp;
1676 if (list_store->sort_column_id != -1)
1678 GtkTreeDataSortHeader *header;
1679 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1680 list_store->sort_column_id);
1681 g_return_if_fail (header != NULL);
1682 g_return_if_fail (header->func != NULL);
1683 func = header->func;
1684 data = header->data;
1688 g_return_if_fail (list_store->default_sort_func != NULL);
1689 func = list_store->default_sort_func;
1690 data = list_store->default_sort_data;
1693 /* If it's the built in function, we don't sort. */
1694 if (func == gtk_tree_data_list_compare_func &&
1695 list_store->sort_column_id != column)
1699 /* First we find the iter, its prev, and its next */
1702 if (list == G_SLIST (iter->user_data))
1708 g_assert (list != NULL);
1712 /* Check the common case, where we don't need to sort it moved. */
1715 tmp_iter.user_data = prev;
1716 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1721 tmp_iter.user_data = next;
1722 cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1726 if (list_store->order == GTK_SORT_DESCENDING)
1739 if (prev == NULL && cmp_b <= 0)
1741 else if (next == NULL && cmp_a <= 0)
1743 else if (prev != NULL && next != NULL &&
1744 cmp_a <= 0 && cmp_b <= 0)
1747 /* We actually need to sort it */
1748 /* First, remove the old link. */
1751 list_store->root = next;
1755 list_store->tail = prev;
1758 /* FIXME: as an optimization, we can potentially start at next */
1760 list = G_SLIST (list_store->root);
1762 tmp_iter.user_data = list;
1763 if (list_store->order == GTK_SORT_DESCENDING)
1764 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1766 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1768 while ((list->next) && (cmp_a > 0))
1773 tmp_iter.user_data = list;
1774 if (list_store->order == GTK_SORT_DESCENDING)
1775 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1777 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1780 if ((!list->next) && (cmp_a > 0))
1783 list->next = G_SLIST (iter->user_data);
1784 list_store->tail = list->next;
1788 prev->next = G_SLIST (iter->user_data);
1789 G_SLIST (iter->user_data)->next = list;
1793 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1794 list_store->root = G_SLIST (iter->user_data);
1797 /* Emit the reordered signal. */
1798 new_order = g_new (int, list_store->length);
1799 if (old_location < new_location)
1800 for (i = 0; i < list_store->length; i++)
1802 if (i < old_location ||
1805 else if (i >= old_location &&
1807 new_order[i] = i + 1;
1808 else if (i == new_location)
1809 new_order[i] = old_location;
1812 for (i = 0; i < list_store->length; i++)
1814 if (i < new_location ||
1817 else if (i > new_location &&
1819 new_order[i] = i - 1;
1820 else if (i == new_location)
1821 new_order[i] = old_location;
1824 tmp_path = gtk_tree_path_new ();
1825 tmp_iter.user_data = NULL;
1827 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1831 gtk_tree_path_free (tmp_path);
1836 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1837 gint *sort_column_id,
1840 GtkListStore *list_store = (GtkListStore *) sortable;
1842 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1844 if (list_store->sort_column_id == -1)
1848 * sort_column_id = list_store->sort_column_id;
1850 * order = list_store->order;
1855 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1856 gint sort_column_id,
1859 GtkListStore *list_store = (GtkListStore *) sortable;
1861 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1863 if ((list_store->sort_column_id == sort_column_id) &&
1864 (list_store->order == order))
1867 if (sort_column_id != -1)
1869 GtkTreeDataSortHeader *header = NULL;
1871 header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
1873 /* We want to make sure that we have a function */
1874 g_return_if_fail (header != NULL);
1875 g_return_if_fail (header->func != NULL);
1879 g_return_if_fail (list_store->default_sort_func != NULL);
1883 list_store->sort_column_id = sort_column_id;
1884 list_store->order = order;
1886 gtk_list_store_sort (list_store);
1888 gtk_tree_sortable_sort_column_changed (sortable);
1892 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
1893 gint sort_column_id,
1894 GtkTreeIterCompareFunc func,
1896 GtkDestroyNotify destroy)
1898 GtkListStore *list_store = (GtkListStore *) sortable;
1899 GtkTreeDataSortHeader *header = NULL;
1902 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1903 g_return_if_fail (func != NULL);
1905 for (list = list_store->sort_list; list; list = list->next)
1907 header = (GtkTreeDataSortHeader*) list->data;
1908 if (header->sort_column_id == sort_column_id)
1914 header = g_new0 (GtkTreeDataSortHeader, 1);
1915 header->sort_column_id = sort_column_id;
1916 list_store->sort_list = g_list_append (list_store->sort_list, header);
1919 if (header->destroy)
1920 (* header->destroy) (header->data);
1922 header->func = func;
1923 header->data = data;
1924 header->destroy = destroy;
1929 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
1930 GtkTreeIterCompareFunc func,
1932 GtkDestroyNotify destroy)
1934 GtkListStore *list_store = (GtkListStore *) sortable;
1936 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1938 if (list_store->default_sort_destroy)
1939 (* list_store->default_sort_destroy) (list_store->default_sort_data);
1941 list_store->default_sort_func = func;
1942 list_store->default_sort_data = data;
1943 list_store->default_sort_destroy = destroy;
1947 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
1949 GtkListStore *list_store = (GtkListStore *) sortable;
1951 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1953 return (list_store->default_sort_func != NULL);