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.
22 #include <gobject/gvaluecollector.h>
24 #include "gtktreemodel.h"
25 #include "gtkliststore.h"
26 #include "gtktreedatalist.h"
27 #include "gtktreednd.h"
29 #define G_SLIST(x) ((GSList *) x)
30 #define GTK_LIST_STORE_IS_SORTED(list) (GTK_LIST_STORE (list)->sort_column_id != -2)
31 #define VALID_ITER(iter, list_store) (iter!= NULL && iter->user_data != NULL && list_store->stamp == iter->stamp)
33 static void gtk_list_store_init (GtkListStore *list_store);
34 static void gtk_list_store_class_init (GtkListStoreClass *class);
35 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
36 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
37 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
38 static void gtk_list_store_sortable_init (GtkTreeSortableIface *iface);
39 static void gtk_list_store_finalize (GObject *object);
40 static GtkTreeModelFlags gtk_list_store_get_flags (GtkTreeModel *tree_model);
41 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
42 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
44 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
47 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
49 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
53 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
55 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
58 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
60 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
62 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
66 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
71 static void gtk_list_store_set_n_columns (GtkListStore *list_store,
73 static void gtk_list_store_set_column_type (GtkListStore *list_store,
79 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
81 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
83 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
85 GtkSelectionData *selection_data);
86 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
88 GtkSelectionData *selection_data);
89 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
90 GtkTreePath *dest_path,
91 GtkSelectionData *selection_data);
95 static void gtk_list_store_sort (GtkListStore *list_store);
96 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
99 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
100 gint *sort_column_id,
102 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
105 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
107 GtkTreeIterCompareFunc func,
109 GtkDestroyNotify destroy);
110 static void gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
111 GtkTreeIterCompareFunc func,
113 GtkDestroyNotify destroy);
114 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable);
116 static void gtk_list_store_move (GtkListStore *store,
122 static GObjectClass *parent_class = NULL;
126 validate_list_store (GtkListStore *list_store)
128 if (gtk_debug_flags & GTK_DEBUG_TREE)
130 g_assert (g_slist_length (list_store->root) == list_store->length);
132 g_assert (g_slist_last (list_store->root) == list_store->tail);
137 gtk_list_store_get_type (void)
139 static GType list_store_type = 0;
141 if (!list_store_type)
143 static const GTypeInfo list_store_info =
145 sizeof (GtkListStoreClass),
146 NULL, /* base_init */
147 NULL, /* base_finalize */
148 (GClassInitFunc) gtk_list_store_class_init,
149 NULL, /* class_finalize */
150 NULL, /* class_data */
151 sizeof (GtkListStore),
153 (GInstanceInitFunc) gtk_list_store_init,
156 static const GInterfaceInfo tree_model_info =
158 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
163 static const GInterfaceInfo drag_source_info =
165 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
170 static const GInterfaceInfo drag_dest_info =
172 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
177 static const GInterfaceInfo sortable_info =
179 (GInterfaceInitFunc) gtk_list_store_sortable_init,
184 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore",
185 &list_store_info, 0);
187 g_type_add_interface_static (list_store_type,
190 g_type_add_interface_static (list_store_type,
191 GTK_TYPE_TREE_DRAG_SOURCE,
193 g_type_add_interface_static (list_store_type,
194 GTK_TYPE_TREE_DRAG_DEST,
196 g_type_add_interface_static (list_store_type,
197 GTK_TYPE_TREE_SORTABLE,
201 return list_store_type;
205 gtk_list_store_class_init (GtkListStoreClass *class)
207 GObjectClass *object_class;
209 parent_class = g_type_class_peek_parent (class);
210 object_class = (GObjectClass*) class;
212 object_class->finalize = gtk_list_store_finalize;
216 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
218 iface->get_flags = gtk_list_store_get_flags;
219 iface->get_n_columns = gtk_list_store_get_n_columns;
220 iface->get_column_type = gtk_list_store_get_column_type;
221 iface->get_iter = gtk_list_store_get_iter;
222 iface->get_path = gtk_list_store_get_path;
223 iface->get_value = gtk_list_store_get_value;
224 iface->iter_next = gtk_list_store_iter_next;
225 iface->iter_children = gtk_list_store_iter_children;
226 iface->iter_has_child = gtk_list_store_iter_has_child;
227 iface->iter_n_children = gtk_list_store_iter_n_children;
228 iface->iter_nth_child = gtk_list_store_iter_nth_child;
229 iface->iter_parent = gtk_list_store_iter_parent;
233 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
235 iface->row_draggable = real_gtk_list_store_row_draggable;
236 iface->drag_data_delete = gtk_list_store_drag_data_delete;
237 iface->drag_data_get = gtk_list_store_drag_data_get;
241 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
243 iface->drag_data_received = gtk_list_store_drag_data_received;
244 iface->row_drop_possible = gtk_list_store_row_drop_possible;
248 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
250 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
251 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
252 iface->set_sort_func = gtk_list_store_set_sort_func;
253 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
254 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
258 gtk_list_store_init (GtkListStore *list_store)
260 list_store->root = NULL;
261 list_store->tail = NULL;
262 list_store->sort_list = NULL;
263 list_store->stamp = g_random_int ();
264 list_store->length = 0;
265 list_store->sort_column_id = -2;
266 list_store->columns_dirty = FALSE;
270 * gtk_list_store_new:
271 * @n_columns: number of columns in the list store
272 * @Varargs: all #GType types for the columns, from first to last
274 * Creates a new list store as with @n_columns columns each of the types passed
275 * in. Note that only types derived from standard GObject fundamental types
278 * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
279 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
280 * int, string and #GdkPixbuf respectively.
282 * Return value: a new #GtkListStore
285 gtk_list_store_new (gint n_columns,
288 GtkListStore *retval;
292 g_return_val_if_fail (n_columns > 0, NULL);
294 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
295 gtk_list_store_set_n_columns (retval, n_columns);
297 va_start (args, n_columns);
299 for (i = 0; i < n_columns; i++)
301 GType type = va_arg (args, GType);
302 if (! _gtk_tree_data_list_check_type (type))
304 g_warning ("%s: Invalid type %s passed to gtk_list_store_new\n",
305 G_STRLOC, g_type_name (type));
306 g_object_unref (retval);
310 gtk_list_store_set_column_type (retval, i, type);
320 * gtk_list_store_newv:
321 * @n_columns: number of columns in the list store
322 * @types: an array of #GType types for the columns, from first to last
324 * Non-vararg creation function. Used primarily by language bindings.
326 * Return value: a new #GtkListStore
329 gtk_list_store_newv (gint n_columns,
332 GtkListStore *retval;
335 g_return_val_if_fail (n_columns > 0, NULL);
337 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
338 gtk_list_store_set_n_columns (retval, n_columns);
340 for (i = 0; i < n_columns; i++)
342 if (! _gtk_tree_data_list_check_type (types[i]))
344 g_warning ("%s: Invalid type %s passed to gtk_list_store_newv\n",
345 G_STRLOC, g_type_name (types[i]));
346 g_object_unref (retval);
350 gtk_list_store_set_column_type (retval, i, types[i]);
357 * gtk_list_store_set_column_types:
358 * @list_store: A #GtkListStore
359 * @n_columns: Number of columns for the list store
360 * @types: An array length n of #GTypes
362 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
363 * and should only be used when constructing a new #GtkListStore. It will not
364 * function after a row has been added, or a method on the #GtkTreeModel
365 * interface is called.
368 gtk_list_store_set_column_types (GtkListStore *list_store,
374 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
375 g_return_if_fail (list_store->columns_dirty == 0);
377 gtk_list_store_set_n_columns (list_store, n_columns);
378 for (i = 0; i < n_columns; i++)
380 if (! _gtk_tree_data_list_check_type (types[i]))
382 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_types\n", G_STRLOC, g_type_name (types[i]));
385 gtk_list_store_set_column_type (list_store, i, types[i]);
390 gtk_list_store_set_n_columns (GtkListStore *list_store,
395 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
396 g_return_if_fail (n_columns > 0);
398 if (list_store->n_columns == n_columns)
401 new_columns = g_new0 (GType, n_columns);
402 if (list_store->column_headers)
404 /* copy the old header orders over */
405 if (n_columns >= list_store->n_columns)
406 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
408 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
410 g_free (list_store->column_headers);
413 if (list_store->sort_list)
414 _gtk_tree_data_list_header_free (list_store->sort_list);
416 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
418 list_store->column_headers = new_columns;
419 list_store->n_columns = n_columns;
423 gtk_list_store_set_column_type (GtkListStore *list_store,
427 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
428 g_return_if_fail (column >=0 && column < list_store->n_columns);
430 if (!_gtk_tree_data_list_check_type (type))
432 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_type\n", G_STRLOC, g_type_name (type));
436 list_store->column_headers[column] = type;
440 gtk_list_store_finalize (GObject *object)
442 GtkListStore *list_store = GTK_LIST_STORE (object);
444 g_slist_foreach (list_store->root, (GFunc) _gtk_tree_data_list_free, list_store->column_headers);
445 g_slist_free (list_store->root);
447 _gtk_tree_data_list_header_free (list_store->sort_list);
448 g_free (list_store->column_headers);
450 if (list_store->default_sort_destroy)
452 GtkDestroyNotify d = list_store->default_sort_destroy;
454 list_store->default_sort_destroy = NULL;
455 d (list_store->default_sort_data);
456 list_store->default_sort_data = NULL;
460 (* parent_class->finalize) (object);
463 /* Fulfill the GtkTreeModel requirements */
464 static GtkTreeModelFlags
465 gtk_list_store_get_flags (GtkTreeModel *tree_model)
467 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
469 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
473 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
475 GtkListStore *list_store = (GtkListStore *) tree_model;
477 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
479 list_store->columns_dirty = TRUE;
481 return list_store->n_columns;
485 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
488 GtkListStore *list_store = (GtkListStore *) tree_model;
490 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
491 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
492 index >= 0, G_TYPE_INVALID);
494 list_store->columns_dirty = TRUE;
496 return list_store->column_headers[index];
500 gtk_list_store_get_iter (GtkTreeModel *tree_model,
504 GtkListStore *list_store = (GtkListStore *) tree_model;
508 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
509 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
511 list_store->columns_dirty = TRUE;
513 i = gtk_tree_path_get_indices (path)[0];
515 if (i >= list_store->length)
518 list = g_slist_nth (G_SLIST (list_store->root), i);
520 /* If this fails, list_store->length has gotten mangled. */
523 iter->stamp = list_store->stamp;
524 iter->user_data = list;
530 gtk_list_store_get_path (GtkTreeModel *tree_model,
537 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
538 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
539 if (G_SLIST (iter->user_data) == G_SLIST (GTK_LIST_STORE (tree_model)->tail))
541 retval = gtk_tree_path_new ();
542 gtk_tree_path_append_index (retval, GTK_LIST_STORE (tree_model)->length - 1);
546 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
548 if (list == G_SLIST (iter->user_data))
555 retval = gtk_tree_path_new ();
556 gtk_tree_path_append_index (retval, i);
561 gtk_list_store_get_value (GtkTreeModel *tree_model,
566 GtkTreeDataList *list;
567 gint tmp_column = column;
569 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
570 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
571 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
573 list = G_SLIST (iter->user_data)->data;
575 while (tmp_column-- > 0 && list)
579 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
581 _gtk_tree_data_list_node_to_value (list,
582 GTK_LIST_STORE (tree_model)->column_headers[column],
587 gtk_list_store_iter_next (GtkTreeModel *tree_model,
590 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
591 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
593 iter->user_data = G_SLIST (iter->user_data)->next;
595 return (iter->user_data != NULL);
599 gtk_list_store_iter_children (GtkTreeModel *tree_model,
603 /* this is a list, nodes have no children */
607 /* but if parent == NULL we return the list itself as children of the
611 if (GTK_LIST_STORE (tree_model)->root)
613 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
614 iter->user_data = GTK_LIST_STORE (tree_model)->root;
622 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
629 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
632 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
634 return GTK_LIST_STORE (tree_model)->length;
636 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
641 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
648 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
653 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
657 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
658 iter->user_data = child;
666 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
674 gtk_list_store_real_set_value (GtkListStore *list_store,
680 GtkTreeDataList *list;
681 GtkTreeDataList *prev;
682 gint old_column = column;
683 GValue real_value = {0, };
684 gboolean converted = FALSE;
685 gboolean retval = FALSE;
687 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
688 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
689 g_return_val_if_fail (column >= 0 && column < list_store->n_columns, FALSE);
690 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
692 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
694 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
695 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
697 g_warning ("%s: Unable to convert from %s to %s\n",
699 g_type_name (G_VALUE_TYPE (value)),
700 g_type_name (list_store->column_headers[column]));
703 if (!g_value_transform (value, &real_value))
705 g_warning ("%s: Unable to make conversion from %s to %s\n",
707 g_type_name (G_VALUE_TYPE (value)),
708 g_type_name (list_store->column_headers[column]));
709 g_value_unset (&real_value);
715 prev = list = G_SLIST (iter->user_data)->data;
722 _gtk_tree_data_list_value_to_node (list, &real_value);
724 _gtk_tree_data_list_value_to_node (list, value);
727 g_value_unset (&real_value);
728 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
729 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
738 if (G_SLIST (iter->user_data)->data == NULL)
740 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
745 list = prev->next = _gtk_tree_data_list_alloc ();
751 list->next = _gtk_tree_data_list_alloc ();
758 _gtk_tree_data_list_value_to_node (list, &real_value);
760 _gtk_tree_data_list_value_to_node (list, value);
764 g_value_unset (&real_value);
766 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
767 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
774 * gtk_list_store_set_value:
775 * @list_store: A #GtkListStore
776 * @iter: A valid #GtkTreeIter for the row being modified
777 * @column: column number to modify
778 * @value: new value for the cell
780 * Sets the data in the cell specified by @iter and @column.
781 * The type of @value must be convertible to the type of the
786 gtk_list_store_set_value (GtkListStore *list_store,
791 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
792 g_return_if_fail (VALID_ITER (iter, list_store));
793 g_return_if_fail (column >= 0 && column < list_store->n_columns);
794 g_return_if_fail (G_IS_VALUE (value));
796 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
800 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
801 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
802 gtk_tree_path_free (path);
807 * gtk_list_store_set_valist:
808 * @list_store: A #GtkListStore
809 * @iter: A valid #GtkTreeIter for the row being modified
810 * @var_args: va_list of column/value pairs
812 * See gtk_list_store_set(); this version takes a va_list for use by language
817 gtk_list_store_set_valist (GtkListStore *list_store,
822 gboolean emit_signal = FALSE;
823 gboolean maybe_need_sort = FALSE;
824 GtkTreeIterCompareFunc func = NULL;
826 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
827 g_return_if_fail (VALID_ITER (iter, list_store));
829 column = va_arg (var_args, gint);
831 if (GTK_LIST_STORE_IS_SORTED (list_store))
833 if (list_store->sort_column_id != -1)
835 GtkTreeDataSortHeader *header;
836 header = _gtk_tree_data_list_get_header (list_store->sort_list,
837 list_store->sort_column_id);
838 g_return_if_fail (header != NULL);
839 g_return_if_fail (header->func != NULL);
844 func = list_store->default_sort_func;
848 if (func != _gtk_tree_data_list_compare_func)
849 maybe_need_sort = TRUE;
853 GValue value = { 0, };
856 if (column >= list_store->n_columns)
858 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
861 g_value_init (&value, list_store->column_headers[column]);
863 G_VALUE_COLLECT (&value, var_args, 0, &error);
866 g_warning ("%s: %s", G_STRLOC, error);
869 /* we purposely leak the value here, it might not be
870 * in a sane state if an error condition occoured
875 /* FIXME: instead of calling this n times, refactor with above */
876 emit_signal = gtk_list_store_real_set_value (list_store,
880 FALSE) || emit_signal;
882 if (func == _gtk_tree_data_list_compare_func &&
883 column == list_store->sort_column_id)
884 maybe_need_sort = TRUE;
886 g_value_unset (&value);
888 column = va_arg (var_args, gint);
891 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
892 gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
898 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
899 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
900 gtk_tree_path_free (path);
905 * gtk_list_store_set:
906 * @list_store: a #GtkListStore
907 * @iter: row iterator
908 * @Varargs: pairs of column number and value, terminated with -1
910 * Sets the value of one or more cells in the row referenced by @iter.
911 * The variable argument list should contain integer column numbers,
912 * each column number followed by the value to be set.
913 * The list is terminated by a -1. For example, to set column 0 with type
914 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
915 * 0, "Foo", -1)</literal>.
918 gtk_list_store_set (GtkListStore *list_store,
924 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
925 g_return_if_fail (iter != NULL);
926 g_return_if_fail (iter->stamp == list_store->stamp);
928 va_start (var_args, iter);
929 gtk_list_store_set_valist (list_store, iter, var_args);
934 remove_link_saving_prev (GSList *list,
949 prev->next = link->next;
968 gtk_list_store_remove_silently (GtkListStore *list_store,
972 if (G_SLIST (iter->user_data)->data)
974 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
975 list_store->column_headers);
976 G_SLIST (iter->user_data)->data = NULL;
982 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
983 G_SLIST (iter->user_data),
986 list_store->length -= 1;
988 if (iter->user_data == list_store->tail)
989 list_store->tail = prev;
991 g_slist_free (G_SLIST (iter->user_data));
996 * gtk_list_store_remove:
997 * @list_store: A #GtkListStore
998 * @iter: A valid #GtkTreeIter
1000 * Removes the given row from the list store. After being removed,
1001 * @iter is set to be the next valid row, or invalidated if it pointed
1002 * to the last row in @list_store.
1004 * Return value: %TRUE if @iter is valid, %FALSE if not.
1007 gtk_list_store_remove (GtkListStore *list_store,
1013 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1014 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
1016 next = G_SLIST (iter->user_data)->next;
1017 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1019 validate_list_store (list_store);
1021 gtk_list_store_remove_silently (list_store, iter, path);
1023 validate_list_store (list_store);
1025 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
1026 gtk_tree_path_free (path);
1030 iter->stamp = list_store->stamp;
1031 iter->user_data = next;
1043 insert_after (GtkListStore *list_store,
1047 g_return_if_fail (sibling != NULL);
1048 g_return_if_fail (new_list != NULL);
1050 /* insert new node after list */
1051 new_list->next = sibling->next;
1052 sibling->next = new_list;
1054 /* if list was the tail, the new node is the new tail */
1055 if (sibling == ((GSList *) list_store->tail))
1056 list_store->tail = new_list;
1058 list_store->length += 1;
1062 * gtk_list_store_insert:
1063 * @list_store: A #GtkListStore
1064 * @iter: An unset #GtkTreeIter to set to the new row
1065 * @position: position to insert the new row
1067 * Creates a new row at @position. @iter will be changed to point to this new
1068 * row. If @position is larger than the number of rows on the list, then the
1069 * new row will be appended to the list. The row will be empty before this
1070 * function is called. To fill in values, you need to call gtk_list_store_set()
1071 * or gtk_list_store_set_value().
1075 gtk_list_store_insert (GtkListStore *list_store,
1083 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1084 g_return_if_fail (iter != NULL);
1085 g_return_if_fail (position >= 0);
1087 list_store->columns_dirty = TRUE;
1089 if (position == 0 ||
1090 GTK_LIST_STORE_IS_SORTED (list_store))
1092 gtk_list_store_prepend (list_store, iter);
1096 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
1100 /* position if off the end of the list, append it */
1101 gtk_list_store_append (list_store, iter);
1106 new_list = g_slist_alloc ();
1108 insert_after (list_store, list, new_list);
1110 iter->stamp = list_store->stamp;
1111 iter->user_data = new_list;
1113 validate_list_store (list_store);
1115 path = gtk_tree_path_new ();
1116 gtk_tree_path_append_index (path, position);
1117 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1118 gtk_tree_path_free (path);
1122 * gtk_list_store_insert_before:
1123 * @list_store: A #GtkListStore
1124 * @iter: An unset #GtkTreeIter to set to the new row
1125 * @sibling: A valid #GtkTreeIter, or %NULL
1127 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
1128 * appended to the end of the list. @iter will be changed to point to this new
1129 * row. The row will be empty before this function is called. To fill in values,
1130 * you need to call gtk_list_store_set() or gtk_list_store_set_value().
1134 gtk_list_store_insert_before (GtkListStore *list_store,
1136 GtkTreeIter *sibling)
1139 GSList *list, *prev, *new_list;
1142 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1143 g_return_if_fail (iter != NULL);
1145 g_return_if_fail (VALID_ITER (sibling, list_store));
1147 list_store->columns_dirty = TRUE;
1149 if (GTK_LIST_STORE_IS_SORTED (list_store))
1151 gtk_list_store_prepend (list_store, iter);
1155 if (sibling == NULL)
1157 gtk_list_store_append (list_store, iter);
1161 new_list = g_slist_alloc ();
1164 list = list_store->root;
1165 while (list && list != sibling->user_data)
1172 if (list != sibling->user_data)
1174 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1178 /* if there are no nodes, we become the list tail, otherwise we
1179 * are inserting before any existing nodes so we can't change
1183 if (list_store->root == NULL)
1184 list_store->tail = new_list;
1188 new_list->next = prev->next;
1189 prev->next = new_list;
1193 new_list->next = list_store->root;
1194 list_store->root = new_list;
1197 iter->stamp = list_store->stamp;
1198 iter->user_data = new_list;
1200 list_store->length += 1;
1202 validate_list_store (list_store);
1204 path = gtk_tree_path_new ();
1205 gtk_tree_path_append_index (path, i);
1206 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1207 gtk_tree_path_free (path);
1211 * gtk_list_store_insert_after:
1212 * @list_store: A #GtkListStore
1213 * @iter: An unset #GtkTreeIter to set to the new row
1214 * @sibling: A valid #GtkTreeIter, or %NULL
1216 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1217 * prepended to the beginning of the list. @iter will be changed to point to
1218 * this new row. The row will be empty after this function is called. To fill
1219 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1223 gtk_list_store_insert_after (GtkListStore *list_store,
1225 GtkTreeIter *sibling)
1228 GSList *list, *new_list;
1231 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1232 g_return_if_fail (iter != NULL);
1234 g_return_if_fail (VALID_ITER (sibling, list_store));
1236 list_store->columns_dirty = TRUE;
1238 if (sibling == NULL ||
1239 GTK_LIST_STORE_IS_SORTED (list_store))
1241 gtk_list_store_prepend (list_store, iter);
1245 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1248 g_return_if_fail (list == sibling->user_data);
1250 new_list = g_slist_alloc ();
1252 insert_after (list_store, list, new_list);
1254 iter->stamp = list_store->stamp;
1255 iter->user_data = new_list;
1257 validate_list_store (list_store);
1259 path = gtk_tree_path_new ();
1260 gtk_tree_path_append_index (path, i + 1);
1261 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1262 gtk_tree_path_free (path);
1266 * gtk_list_store_prepend:
1267 * @list_store: A #GtkListStore
1268 * @iter: An unset #GtkTreeIter to set to the prepend row
1270 * Prepends a new row to @list_store. @iter will be changed to point to this new
1271 * row. The row will be empty after this function is called. To fill in
1272 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1276 gtk_list_store_prepend (GtkListStore *list_store,
1281 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1282 g_return_if_fail (iter != NULL);
1284 iter->stamp = list_store->stamp;
1285 iter->user_data = g_slist_alloc ();
1287 list_store->columns_dirty = TRUE;
1289 if (list_store->root == NULL)
1290 list_store->tail = iter->user_data;
1292 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1293 list_store->root = iter->user_data;
1295 list_store->length += 1;
1297 validate_list_store (list_store);
1299 path = gtk_tree_path_new ();
1300 gtk_tree_path_append_index (path, 0);
1301 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1302 gtk_tree_path_free (path);
1306 * gtk_list_store_append:
1307 * @list_store: A #GtkListStore
1308 * @iter: An unset #GtkTreeIter to set to the appended row
1310 * Appends a new row to @list_store. @iter will be changed to point to this new
1311 * row. The row will be empty after this function is called. To fill in
1312 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1316 gtk_list_store_append (GtkListStore *list_store,
1321 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1322 g_return_if_fail (iter != NULL);
1324 list_store->columns_dirty = TRUE;
1326 if (GTK_LIST_STORE_IS_SORTED (list_store))
1328 gtk_list_store_prepend (list_store, iter);
1332 iter->stamp = list_store->stamp;
1333 iter->user_data = g_slist_alloc ();
1335 if (list_store->tail)
1336 ((GSList *)list_store->tail)->next = iter->user_data;
1338 list_store->root = iter->user_data;
1340 list_store->tail = iter->user_data;
1342 list_store->length += 1;
1344 validate_list_store (list_store);
1346 path = gtk_tree_path_new ();
1347 gtk_tree_path_append_index (path, list_store->length - 1);
1348 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1349 gtk_tree_path_free (path);
1353 * gtk_list_store_clear:
1354 * @list_store: a #GtkListStore.
1356 * Removes all rows from the list store.
1360 gtk_list_store_clear (GtkListStore *list_store)
1363 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1365 while (list_store->root)
1367 iter.stamp = list_store->stamp;
1368 iter.user_data = list_store->root;
1369 gtk_list_store_remove (list_store, &iter);
1374 * gtk_list_store_iter_is_valid:
1375 * @list_store: A #GtkListStore.
1376 * @iter: A #GtkTreeIter.
1378 * WARNING: This function is slow. Only use it for debugging and/or testing
1381 * Checks if the given iter is a valid iter for this #GtkListStore.
1383 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1388 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1393 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1394 g_return_val_if_fail (iter != NULL, FALSE);
1396 if (!VALID_ITER (iter, list_store))
1399 if (iter->user_data == list_store->root)
1401 if (iter->user_data == list_store->tail)
1404 for (list = ((GList *)list_store->root)->next; list; list = list->next)
1405 if (list == iter->user_data)
1411 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
1418 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1422 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1424 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1428 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1435 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1437 GtkSelectionData *selection_data)
1439 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1441 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1442 * target, because the default handler does it for us, but
1443 * we do anyway for the convenience of someone maybe overriding the
1447 if (gtk_tree_set_row_drag_data (selection_data,
1448 GTK_TREE_MODEL (drag_source),
1455 /* FIXME handle text targets at least. */
1462 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1464 GtkSelectionData *selection_data)
1466 GtkTreeModel *tree_model;
1467 GtkListStore *list_store;
1468 GtkTreeModel *src_model = NULL;
1469 GtkTreePath *src_path = NULL;
1470 gboolean retval = FALSE;
1472 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1474 tree_model = GTK_TREE_MODEL (drag_dest);
1475 list_store = GTK_LIST_STORE (drag_dest);
1477 if (gtk_tree_get_row_drag_data (selection_data,
1480 src_model == tree_model)
1482 /* Copy the given row to a new position */
1483 GtkTreeIter src_iter;
1484 GtkTreeIter dest_iter;
1487 if (!gtk_tree_model_get_iter (src_model,
1494 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1495 prev = gtk_tree_path_copy (dest);
1497 if (!gtk_tree_path_prev (prev))
1499 /* dest was the first spot in the list; which means we are supposed
1502 gtk_list_store_prepend (list_store, &dest_iter);
1508 if (gtk_tree_model_get_iter (tree_model, &dest_iter, prev))
1510 GtkTreeIter tmp_iter = dest_iter;
1512 gtk_list_store_insert_after (list_store, &dest_iter, &tmp_iter);
1518 gtk_tree_path_free (prev);
1520 /* If we succeeded in creating dest_iter, copy data from src
1524 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1525 GtkTreeDataList *copy_head = NULL;
1526 GtkTreeDataList *copy_prev = NULL;
1527 GtkTreeDataList *copy_iter = NULL;
1534 copy_iter = _gtk_tree_data_list_node_copy (dl,
1535 list_store->column_headers[col]);
1537 if (copy_head == NULL)
1538 copy_head = copy_iter;
1541 copy_prev->next = copy_iter;
1543 copy_prev = copy_iter;
1549 dest_iter.stamp = list_store->stamp;
1550 G_SLIST (dest_iter.user_data)->data = copy_head;
1552 path = gtk_list_store_get_path (tree_model, &dest_iter);
1553 gtk_tree_model_row_changed (tree_model, path, &dest_iter);
1554 gtk_tree_path_free (path);
1559 /* FIXME maybe add some data targets eventually, or handle text
1560 * targets in the simple case.
1567 gtk_tree_path_free (src_path);
1573 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1574 GtkTreePath *dest_path,
1575 GtkSelectionData *selection_data)
1578 GtkTreeModel *src_model = NULL;
1579 GtkTreePath *src_path = NULL;
1580 gboolean retval = FALSE;
1582 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1584 /* don't accept drops if the list has been sorted */
1585 if (GTK_LIST_STORE_IS_SORTED (drag_dest))
1588 if (!gtk_tree_get_row_drag_data (selection_data,
1593 if (src_model != GTK_TREE_MODEL (drag_dest))
1596 if (gtk_tree_path_get_depth (dest_path) != 1)
1599 /* can drop before any existing node, or before one past any existing. */
1601 indices = gtk_tree_path_get_indices (dest_path);
1603 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1608 gtk_tree_path_free (src_path);
1613 /* Sorting and reordering */
1614 typedef struct _SortTuple
1622 gtk_list_store_reorder_func (gconstpointer a,
1626 SortTuple *a_reorder;
1627 SortTuple *b_reorder;
1629 a_reorder = (SortTuple *)a;
1630 b_reorder = (SortTuple *)b;
1632 if (a_reorder->offset < b_reorder->offset)
1634 if (a_reorder->offset > b_reorder->offset)
1641 * gtk_list_store_reorder:
1642 * @store: A #GtkListStore.
1643 * @new_order: an array of integers mapping the new position of each child
1644 * to its old position before the re-ordering,
1645 * i.e. @new_order<literal>[newpos] = oldpos</literal>.
1647 * Reorders @store to follow the order indicated by @new_order. Note that
1648 * this function only works with unsorted stores.
1653 gtk_list_store_reorder (GtkListStore *store,
1657 GSList *current_list;
1659 SortTuple *sort_array;
1661 g_return_if_fail (GTK_IS_LIST_STORE (store));
1662 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1663 g_return_if_fail (new_order != NULL);
1665 sort_array = g_new (SortTuple, store->length);
1667 current_list = store->root;
1669 for (i = 0; i < store->length; i++)
1671 sort_array[new_order[i]].offset = i;
1672 sort_array[i].el = current_list;
1674 current_list = current_list->next;
1677 g_qsort_with_data (sort_array,
1680 gtk_list_store_reorder_func,
1683 for (i = 0; i < store->length - 1; i++)
1684 G_SLIST (sort_array[i].el)->next = G_SLIST (sort_array[i+1].el);
1686 store->root = G_SLIST (sort_array[0].el);
1687 store->tail = G_SLIST (sort_array[store->length-1].el);
1688 G_SLIST (store->tail)->next = NULL;
1691 path = gtk_tree_path_new ();
1692 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1693 path, NULL, new_order);
1694 gtk_tree_path_free (path);
1695 g_free (sort_array);
1699 * gtk_list_store_swap:
1700 * @store: A #GtkListStore.
1701 * @a: A #GtkTreeIter.
1702 * @b: Another #GtkTreeIter.
1704 * Swaps @a and @b in @store. Note that this function only works with
1710 gtk_list_store_swap (GtkListStore *store,
1714 GSList *i, *prev_a = NULL, *prev_b = NULL;
1715 gint j, a_count = 0, b_count = 0, *order;
1718 g_return_if_fail (GTK_IS_LIST_STORE (store));
1719 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1720 g_return_if_fail (VALID_ITER (a, store));
1721 g_return_if_fail (VALID_ITER (b, store));
1723 if (a->user_data == b->user_data)
1726 if (a->user_data == store->root)
1730 for (i = store->root; i; i = i->next, a_count++)
1731 if (i->next == a->user_data)
1740 if (b->user_data == store->root)
1744 for (i = store->root; i; i = i->next, b_count++)
1745 if (i->next == b->user_data)
1755 store->root = b->user_data;
1757 prev_a->next = b->user_data;
1760 store->root = a->user_data;
1762 prev_b->next = a->user_data;
1764 /* think a_next inspead of a_prev here ... */
1765 prev_a = G_SLIST (a->user_data)->next;
1766 prev_b = G_SLIST (b->user_data)->next;
1768 G_SLIST (a->user_data)->next = prev_b;
1769 G_SLIST (b->user_data)->next = prev_a;
1771 /* update tail if needed */
1772 if (! G_SLIST (a->user_data)->next)
1773 store->tail = G_SLIST (a->user_data);
1774 else if (! G_SLIST (b->user_data)->next)
1775 store->tail = G_SLIST (b->user_data);
1778 order = g_new (gint, store->length);
1779 for (j = 0; j < store->length; j++)
1782 else if (j == b_count)
1787 path = gtk_tree_path_new ();
1788 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1790 gtk_tree_path_free (path);
1795 gtk_list_store_move (GtkListStore *store,
1797 GtkTreeIter *position,
1801 GSList *i, *a, *prev = NULL, *tmp;
1802 gint new_pos = 0, old_pos = 0, j = 0, *order;
1803 GtkTreePath *path = NULL, *pos_path = NULL;
1805 g_return_if_fail (GTK_IS_LIST_STORE (store));
1806 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1807 g_return_if_fail (VALID_ITER (iter, store));
1809 g_return_if_fail (VALID_ITER (position, store));
1811 /* lots of sanity checks */
1814 path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), iter);
1815 pos_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), position);
1817 if (gtk_tree_path_get_depth (pos_path) != 1)
1818 goto free_paths_and_out;
1821 * moving the iter before path or "path + 1" doesn't make sense
1823 * moving the iter before path or "path - 1" doesn't make sense
1825 if (!gtk_tree_path_compare (path, pos_path))
1826 goto free_paths_and_out;
1829 gtk_tree_path_next (path);
1831 gtk_tree_path_prev (path);
1833 if (!gtk_tree_path_compare (path, pos_path))
1834 goto free_paths_and_out;
1836 gtk_tree_path_free (path);
1840 /* getting destination iters */
1841 if (before && position)
1843 if (gtk_tree_path_get_indices (pos_path)[0] > 0)
1845 gtk_tree_path_prev (pos_path);
1846 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &dst_a, pos_path))
1847 a = G_SLIST (dst_a.user_data);
1850 gtk_tree_path_next (pos_path);
1855 else if (before && !position)
1860 a = G_SLIST (position->user_data);
1865 /* don't try to reorder the iter to it's own position */
1868 if (a == iter->user_data)
1869 goto free_paths_and_out;
1873 if (iter->user_data == store->tail)
1874 goto free_paths_and_out;
1878 if (iter->user_data == store->root)
1879 goto free_paths_and_out;
1882 /* getting the old prev node */
1883 if (iter->user_data == store->root)
1887 for (i = store->root; i; i = i->next, old_pos++)
1888 if (i->next == iter->user_data)
1899 store->root = G_SLIST (iter->user_data)->next;
1902 prev->next = G_SLIST (iter->user_data)->next;
1907 /* and reinsert it */
1912 a->next = G_SLIST (iter->user_data);
1913 a->next->next = tmp;
1915 else if (!a && !before)
1917 tmp = G_SLIST (store->root);
1919 store->root = G_SLIST (iter->user_data);
1920 G_SLIST (store->root)->next = tmp;
1922 else if (!a && before)
1924 G_SLIST (store->tail)->next = G_SLIST (iter->user_data);
1925 G_SLIST (iter->user_data)->next = NULL;
1928 /* update tail if needed */
1929 if (!G_SLIST (iter->user_data)->next)
1930 store->tail = G_SLIST (iter->user_data);
1934 new_pos = gtk_tree_path_get_indices (pos_path)[0];
1936 new_pos = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL) - 1;
1940 if (new_pos > old_pos)
1942 if (before && position)
1947 if (!before && position)
1951 order = g_new (gint, store->length);
1952 if (new_pos > old_pos)
1954 for (j = 0; j < store->length; j++)
1957 else if (j >= old_pos && j < new_pos)
1959 else if (j == new_pos)
1966 for (j = 0; j < store->length; j++)
1969 else if (j > new_pos && j <= old_pos)
1975 path = gtk_tree_path_new ();
1976 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1978 gtk_tree_path_free (path);
1980 gtk_tree_path_free (pos_path);
1987 gtk_tree_path_free (path);
1989 gtk_tree_path_free (pos_path);
1993 * gtk_list_store_move_before:
1994 * @store: A #GtkListStore.
1995 * @iter: A #GtkTreeIter.
1996 * @position: A #GtkTreeIter, or %NULL.
1998 * Moves @iter in @store to the position before @position. Note that this
1999 * function only works with unsorted stores. If @position is %NULL, @iter
2000 * will be moved to the end of the list.
2005 gtk_list_store_move_before (GtkListStore *store,
2007 GtkTreeIter *position)
2009 gtk_list_store_move (store, iter, position, TRUE);
2013 * gtk_list_store_move_after:
2014 * @store: A #GtkListStore.
2015 * @iter: A #GtkTreeIter.
2016 * @position: A #GtkTreeIter or %NULL.
2018 * Moves @iter in @store to the position after @position. Note that this
2019 * function only works with unsorted stores. If @position is %NULL, @iter
2020 * will be moved to the start of the list.
2025 gtk_list_store_move_after (GtkListStore *store,
2027 GtkTreeIter *position)
2029 gtk_list_store_move (store, iter, position, FALSE);
2034 gtk_list_store_compare_func (gconstpointer a,
2038 GtkListStore *list_store = user_data;
2039 GSList *el_a; /* Los Angeles? */
2044 GtkTreeIterCompareFunc func;
2048 if (list_store->sort_column_id != -1)
2050 GtkTreeDataSortHeader *header;
2052 header = _gtk_tree_data_list_get_header (list_store->sort_list,
2053 list_store->sort_column_id);
2054 g_return_val_if_fail (header != NULL, 0);
2055 g_return_val_if_fail (header->func != NULL, 0);
2057 func = header->func;
2058 data = header->data;
2062 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
2063 func = list_store->default_sort_func;
2064 data = list_store->default_sort_data;
2067 el_a = ((SortTuple *) a)->el;
2068 el_b = ((SortTuple *) b)->el;
2070 iter_a.stamp = list_store->stamp;
2071 iter_a.user_data = el_a;
2072 iter_b.stamp = list_store->stamp;
2073 iter_b.user_data = el_b;
2075 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
2077 if (list_store->order == GTK_SORT_DESCENDING)
2081 else if (retval < 0)
2088 gtk_list_store_sort (GtkListStore *list_store)
2096 if (list_store->length <= 1)
2099 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
2101 list = G_SLIST (list_store->root);
2103 sort_array = g_array_sized_new (FALSE, FALSE,
2105 list_store->length);
2107 for (i = 0; i < list_store->length; i++)
2109 SortTuple tuple = {0,};
2111 /* If this fails, we are in an inconsistent state. Bad */
2112 g_return_if_fail (list != NULL);
2116 g_array_append_val (sort_array, tuple);
2121 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
2123 for (i = 0; i < list_store->length - 1; i++)
2124 g_array_index (sort_array, SortTuple, i).el->next =
2125 g_array_index (sort_array, SortTuple, i + 1).el;
2126 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
2127 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
2128 list_store->tail = g_array_index (sort_array, SortTuple, list_store->length - 1).el;
2130 /* Let the world know about our new order */
2131 new_order = g_new (gint, list_store->length);
2132 for (i = 0; i < list_store->length; i++)
2133 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
2135 path = gtk_tree_path_new ();
2136 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
2137 path, NULL, new_order);
2138 gtk_tree_path_free (path);
2140 g_array_free (sort_array, TRUE);
2144 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
2149 GSList *prev = NULL;
2150 GSList *next = NULL;
2151 GSList *list = G_SLIST (list_store->root);
2152 GtkTreePath *tmp_path;
2153 GtkTreeIter tmp_iter;
2160 GtkTreeIterCompareFunc func;
2163 if (list_store->length < 2)
2166 tmp_iter.stamp = list_store->stamp;
2168 if (list_store->sort_column_id != -1)
2170 GtkTreeDataSortHeader *header;
2171 header = _gtk_tree_data_list_get_header (list_store->sort_list,
2172 list_store->sort_column_id);
2173 g_return_if_fail (header != NULL);
2174 g_return_if_fail (header->func != NULL);
2175 func = header->func;
2176 data = header->data;
2180 g_return_if_fail (list_store->default_sort_func != NULL);
2181 func = list_store->default_sort_func;
2182 data = list_store->default_sort_data;
2185 /* If it's the built in function, we don't sort. */
2186 if (func == _gtk_tree_data_list_compare_func &&
2187 list_store->sort_column_id != column)
2191 /* First we find the iter, its prev, and its next */
2194 if (list == G_SLIST (iter->user_data))
2200 g_assert (list != NULL);
2204 /* Check the common case, where we don't need to sort it moved. */
2207 tmp_iter.user_data = prev;
2208 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2213 tmp_iter.user_data = next;
2214 cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2217 if (list_store->order == GTK_SORT_DESCENDING)
2230 if (prev == NULL && cmp_b <= 0)
2232 else if (next == NULL && cmp_a <= 0)
2234 else if (prev != NULL && next != NULL &&
2235 cmp_a <= 0 && cmp_b <= 0)
2238 /* We actually need to sort it */
2239 /* First, remove the old link. */
2242 list_store->root = next;
2246 list_store->tail = prev;
2249 /* FIXME: as an optimization, we can potentially start at next */
2251 list = G_SLIST (list_store->root);
2253 tmp_iter.user_data = list;
2254 if (list_store->order == GTK_SORT_DESCENDING)
2255 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2257 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2259 while ((list->next) && (cmp_a > 0))
2264 tmp_iter.user_data = list;
2265 if (list_store->order == GTK_SORT_DESCENDING)
2266 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
2268 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
2271 if ((!list->next) && (cmp_a > 0))
2274 list->next = G_SLIST (iter->user_data);
2275 list_store->tail = list->next;
2279 prev->next = G_SLIST (iter->user_data);
2280 G_SLIST (iter->user_data)->next = list;
2284 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
2285 list_store->root = G_SLIST (iter->user_data);
2288 /* Emit the reordered signal. */
2289 new_order = g_new (int, list_store->length);
2290 if (old_location < new_location)
2291 for (i = 0; i < list_store->length; i++)
2293 if (i < old_location ||
2296 else if (i >= old_location &&
2298 new_order[i] = i + 1;
2299 else if (i == new_location)
2300 new_order[i] = old_location;
2303 for (i = 0; i < list_store->length; i++)
2305 if (i < new_location ||
2308 else if (i > new_location &&
2310 new_order[i] = i - 1;
2311 else if (i == new_location)
2312 new_order[i] = old_location;
2315 tmp_path = gtk_tree_path_new ();
2316 tmp_iter.user_data = NULL;
2318 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
2321 gtk_tree_path_free (tmp_path);
2326 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
2327 gint *sort_column_id,
2330 GtkListStore *list_store = (GtkListStore *) sortable;
2332 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
2334 if (list_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2338 * sort_column_id = list_store->sort_column_id;
2340 * order = list_store->order;
2345 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
2346 gint sort_column_id,
2349 GtkListStore *list_store = (GtkListStore *) sortable;
2351 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2353 if ((list_store->sort_column_id == sort_column_id) &&
2354 (list_store->order == order))
2357 if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2359 GtkTreeDataSortHeader *header = NULL;
2361 header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
2363 /* We want to make sure that we have a function */
2364 g_return_if_fail (header != NULL);
2365 g_return_if_fail (header->func != NULL);
2369 g_return_if_fail (list_store->default_sort_func != NULL);
2373 list_store->sort_column_id = sort_column_id;
2374 list_store->order = order;
2376 gtk_tree_sortable_sort_column_changed (sortable);
2378 gtk_list_store_sort (list_store);
2382 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
2383 gint sort_column_id,
2384 GtkTreeIterCompareFunc func,
2386 GtkDestroyNotify destroy)
2388 GtkListStore *list_store = (GtkListStore *) sortable;
2389 GtkTreeDataSortHeader *header = NULL;
2392 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2393 g_return_if_fail (func != NULL);
2395 for (list = list_store->sort_list; list; list = list->next)
2397 GtkTreeDataSortHeader *list_header;
2399 list_header = (GtkTreeDataSortHeader*) list->data;
2400 if (list_header->sort_column_id == sort_column_id)
2402 header = list_header;
2409 header = g_new0 (GtkTreeDataSortHeader, 1);
2410 header->sort_column_id = sort_column_id;
2411 list_store->sort_list = g_list_append (list_store->sort_list, header);
2414 if (header->destroy)
2416 GtkDestroyNotify d = header->destroy;
2418 header->destroy = NULL;
2422 header->func = func;
2423 header->data = data;
2424 header->destroy = destroy;
2426 if (list_store->sort_column_id == sort_column_id)
2427 gtk_list_store_sort (list_store);
2431 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
2432 GtkTreeIterCompareFunc func,
2434 GtkDestroyNotify destroy)
2436 GtkListStore *list_store = (GtkListStore *) sortable;
2438 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
2440 if (list_store->default_sort_destroy)
2442 GtkDestroyNotify d = list_store->default_sort_destroy;
2444 list_store->default_sort_destroy = NULL;
2445 d (list_store->default_sort_data);
2448 list_store->default_sort_func = func;
2449 list_store->default_sort_data = data;
2450 list_store->default_sort_destroy = destroy;
2452 if (list_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2453 gtk_list_store_sort (list_store);
2457 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
2459 GtkListStore *list_store = (GtkListStore *) sortable;
2461 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
2463 return (list_store->default_sort_func != NULL);