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"
28 #include "gtksequence.h"
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 && !_gtk_sequence_ptr_is_end ((iter)->user_data) && _gtk_sequence_ptr_get_sequence ((iter)->user_data) == list_store->seq)
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);
117 static GObjectClass *parent_class = NULL;
120 gtk_list_store_get_type (void)
122 static GType list_store_type = 0;
124 if (!list_store_type)
126 static const GTypeInfo list_store_info =
128 sizeof (GtkListStoreClass),
129 NULL, /* base_init */
130 NULL, /* base_finalize */
131 (GClassInitFunc) gtk_list_store_class_init,
132 NULL, /* class_finalize */
133 NULL, /* class_data */
134 sizeof (GtkListStore),
136 (GInstanceInitFunc) gtk_list_store_init,
139 static const GInterfaceInfo tree_model_info =
141 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
146 static const GInterfaceInfo drag_source_info =
148 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
153 static const GInterfaceInfo drag_dest_info =
155 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
160 static const GInterfaceInfo sortable_info =
162 (GInterfaceInitFunc) gtk_list_store_sortable_init,
167 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore",
168 &list_store_info, 0);
170 g_type_add_interface_static (list_store_type,
173 g_type_add_interface_static (list_store_type,
174 GTK_TYPE_TREE_DRAG_SOURCE,
176 g_type_add_interface_static (list_store_type,
177 GTK_TYPE_TREE_DRAG_DEST,
179 g_type_add_interface_static (list_store_type,
180 GTK_TYPE_TREE_SORTABLE,
184 return list_store_type;
188 gtk_list_store_class_init (GtkListStoreClass *class)
190 GObjectClass *object_class;
192 parent_class = g_type_class_peek_parent (class);
193 object_class = (GObjectClass*) class;
195 object_class->finalize = gtk_list_store_finalize;
199 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
201 iface->get_flags = gtk_list_store_get_flags;
202 iface->get_n_columns = gtk_list_store_get_n_columns;
203 iface->get_column_type = gtk_list_store_get_column_type;
204 iface->get_iter = gtk_list_store_get_iter;
205 iface->get_path = gtk_list_store_get_path;
206 iface->get_value = gtk_list_store_get_value;
207 iface->iter_next = gtk_list_store_iter_next;
208 iface->iter_children = gtk_list_store_iter_children;
209 iface->iter_has_child = gtk_list_store_iter_has_child;
210 iface->iter_n_children = gtk_list_store_iter_n_children;
211 iface->iter_nth_child = gtk_list_store_iter_nth_child;
212 iface->iter_parent = gtk_list_store_iter_parent;
216 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
218 iface->row_draggable = real_gtk_list_store_row_draggable;
219 iface->drag_data_delete = gtk_list_store_drag_data_delete;
220 iface->drag_data_get = gtk_list_store_drag_data_get;
224 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
226 iface->drag_data_received = gtk_list_store_drag_data_received;
227 iface->row_drop_possible = gtk_list_store_row_drop_possible;
231 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
233 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
234 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
235 iface->set_sort_func = gtk_list_store_set_sort_func;
236 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
237 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
241 gtk_list_store_init (GtkListStore *list_store)
243 list_store->seq = _gtk_sequence_new (NULL);
244 list_store->sort_list = NULL;
245 list_store->stamp = g_random_int ();
246 list_store->sort_column_id = -2;
247 list_store->columns_dirty = FALSE;
248 list_store->length = 0;
252 * gtk_list_store_new:
253 * @n_columns: number of columns in the list store
254 * @Varargs: all #GType types for the columns, from first to last
256 * Creates a new list store as with @n_columns columns each of the types passed
257 * in. Note that only types derived from standard GObject fundamental types
260 * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
261 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
262 * int, string and #GdkPixbuf respectively.
264 * Return value: a new #GtkListStore
267 gtk_list_store_new (gint n_columns,
270 GtkListStore *retval;
274 g_return_val_if_fail (n_columns > 0, NULL);
276 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
277 gtk_list_store_set_n_columns (retval, n_columns);
279 va_start (args, n_columns);
281 for (i = 0; i < n_columns; i++)
283 GType type = va_arg (args, GType);
284 if (! _gtk_tree_data_list_check_type (type))
286 g_warning ("%s: Invalid type %s passed to gtk_list_store_new\n",
287 G_STRLOC, g_type_name (type));
288 g_object_unref (retval);
292 gtk_list_store_set_column_type (retval, i, type);
302 * gtk_list_store_newv:
303 * @n_columns: number of columns in the list store
304 * @types: an array of #GType types for the columns, from first to last
306 * Non-vararg creation function. Used primarily by language bindings.
308 * Return value: a new #GtkListStore
311 gtk_list_store_newv (gint n_columns,
314 GtkListStore *retval;
317 g_return_val_if_fail (n_columns > 0, NULL);
319 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
320 gtk_list_store_set_n_columns (retval, n_columns);
322 for (i = 0; i < n_columns; i++)
324 if (! _gtk_tree_data_list_check_type (types[i]))
326 g_warning ("%s: Invalid type %s passed to gtk_list_store_newv\n",
327 G_STRLOC, g_type_name (types[i]));
328 g_object_unref (retval);
332 gtk_list_store_set_column_type (retval, i, types[i]);
339 * gtk_list_store_set_column_types:
340 * @list_store: A #GtkListStore
341 * @n_columns: Number of columns for the list store
342 * @types: An array length n of #GTypes
344 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
345 * and should only be used when constructing a new #GtkListStore. It will not
346 * function after a row has been added, or a method on the #GtkTreeModel
347 * interface is called.
350 gtk_list_store_set_column_types (GtkListStore *list_store,
356 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
357 g_return_if_fail (list_store->columns_dirty == 0);
359 gtk_list_store_set_n_columns (list_store, n_columns);
360 for (i = 0; i < n_columns; i++)
362 if (! _gtk_tree_data_list_check_type (types[i]))
364 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_types\n", G_STRLOC, g_type_name (types[i]));
367 gtk_list_store_set_column_type (list_store, i, types[i]);
372 gtk_list_store_set_n_columns (GtkListStore *list_store,
377 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
378 g_return_if_fail (n_columns > 0);
380 if (list_store->n_columns == n_columns)
383 new_columns = g_new0 (GType, n_columns);
384 if (list_store->column_headers)
386 /* copy the old header orders over */
387 if (n_columns >= list_store->n_columns)
388 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
390 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
392 g_free (list_store->column_headers);
395 if (list_store->sort_list)
396 _gtk_tree_data_list_header_free (list_store->sort_list);
398 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
400 list_store->column_headers = new_columns;
401 list_store->n_columns = n_columns;
405 gtk_list_store_set_column_type (GtkListStore *list_store,
409 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
410 g_return_if_fail (column >=0 && column < list_store->n_columns);
412 if (!_gtk_tree_data_list_check_type (type))
414 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_type\n", G_STRLOC, g_type_name (type));
418 list_store->column_headers[column] = type;
422 gtk_list_store_finalize (GObject *object)
424 GtkListStore *list_store = GTK_LIST_STORE (object);
426 _gtk_sequence_foreach (list_store->seq,
427 (GFunc) _gtk_tree_data_list_free, list_store->column_headers);
429 _gtk_sequence_free (list_store->seq);
431 _gtk_tree_data_list_header_free (list_store->sort_list);
432 g_free (list_store->column_headers);
434 if (list_store->default_sort_destroy)
436 GtkDestroyNotify d = list_store->default_sort_destroy;
438 list_store->default_sort_destroy = NULL;
439 d (list_store->default_sort_data);
440 list_store->default_sort_data = NULL;
444 (* parent_class->finalize) (object);
447 /* Fulfill the GtkTreeModel requirements */
448 static GtkTreeModelFlags
449 gtk_list_store_get_flags (GtkTreeModel *tree_model)
451 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
453 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
457 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
459 GtkListStore *list_store = (GtkListStore *) tree_model;
461 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
463 list_store->columns_dirty = TRUE;
465 return list_store->n_columns;
469 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
472 GtkListStore *list_store = (GtkListStore *) tree_model;
474 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
475 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
476 index >= 0, G_TYPE_INVALID);
478 list_store->columns_dirty = TRUE;
480 return list_store->column_headers[index];
484 gtk_list_store_get_iter (GtkTreeModel *tree_model,
488 GtkListStore *list_store = (GtkListStore *) tree_model;
492 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
493 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
495 list_store->columns_dirty = TRUE;
497 seq = list_store->seq;
499 i = gtk_tree_path_get_indices (path)[0];
501 if (i >= _gtk_sequence_get_length (seq))
504 iter->stamp = list_store->stamp;
505 iter->user_data = _gtk_sequence_get_ptr_at_pos (seq, i);
511 gtk_list_store_get_path (GtkTreeModel *tree_model,
516 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
517 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
519 if (_gtk_sequence_ptr_is_end (iter->user_data))
522 path = gtk_tree_path_new ();
523 gtk_tree_path_append_index (path, _gtk_sequence_ptr_get_position (iter->user_data));
529 gtk_list_store_get_value (GtkTreeModel *tree_model,
534 GtkTreeDataList *list;
535 gint tmp_column = column;
537 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
538 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
539 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
540 g_return_if_fail (VALID_ITER (iter, GTK_LIST_STORE(tree_model)));
542 list = _gtk_sequence_ptr_get_data (iter->user_data);
544 while (tmp_column-- > 0 && list)
548 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
550 _gtk_tree_data_list_node_to_value (list,
551 GTK_LIST_STORE (tree_model)->column_headers[column],
556 gtk_list_store_iter_next (GtkTreeModel *tree_model,
559 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
560 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
561 iter->user_data = _gtk_sequence_ptr_next (iter->user_data);
563 return !_gtk_sequence_ptr_is_end (iter->user_data);
567 gtk_list_store_iter_children (GtkTreeModel *tree_model,
571 GtkListStore *list_store;
573 /* this is a list, nodes have no children */
577 list_store = GTK_LIST_STORE (tree_model);
579 if (_gtk_sequence_get_length (list_store->seq) == 0)
581 iter->stamp = list_store->stamp;
582 iter->user_data = _gtk_sequence_get_begin_ptr (list_store->seq);
590 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
597 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
602 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
604 store = GTK_LIST_STORE (tree_model);
607 return _gtk_sequence_get_length (store->seq);
609 g_return_val_if_fail (store->stamp == iter->stamp, -1);
614 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
619 GtkSequencePtr child;
622 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
624 store = GTK_LIST_STORE (tree_model);
629 child = _gtk_sequence_get_ptr_at_pos (store->seq, n);
631 if (_gtk_sequence_ptr_is_end (child))
634 iter->stamp = store->stamp;
635 iter->user_data = child;
640 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
648 gtk_list_store_real_set_value (GtkListStore *list_store,
654 GtkTreeDataList *list;
655 GtkTreeDataList *prev;
656 gint old_column = column;
657 GValue real_value = {0, };
658 gboolean converted = FALSE;
659 gboolean retval = FALSE;
661 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
662 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
663 g_return_val_if_fail (column >= 0 && column < list_store->n_columns, FALSE);
664 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
666 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
668 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
669 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
671 g_warning ("%s: Unable to convert from %s to %s\n",
673 g_type_name (G_VALUE_TYPE (value)),
674 g_type_name (list_store->column_headers[column]));
677 if (!g_value_transform (value, &real_value))
679 g_warning ("%s: Unable to make conversion from %s to %s\n",
681 g_type_name (G_VALUE_TYPE (value)),
682 g_type_name (list_store->column_headers[column]));
683 g_value_unset (&real_value);
689 prev = list = _gtk_sequence_ptr_get_data (iter->user_data);
696 _gtk_tree_data_list_value_to_node (list, &real_value);
698 _gtk_tree_data_list_value_to_node (list, value);
701 g_value_unset (&real_value);
702 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
703 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
712 if (_gtk_sequence_ptr_get_data (iter->user_data) == NULL)
714 list = _gtk_tree_data_list_alloc();
715 _gtk_sequence_set (iter->user_data, list);
720 list = prev->next = _gtk_tree_data_list_alloc ();
726 list->next = _gtk_tree_data_list_alloc ();
733 _gtk_tree_data_list_value_to_node (list, &real_value);
735 _gtk_tree_data_list_value_to_node (list, value);
739 g_value_unset (&real_value);
741 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
742 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
749 * gtk_list_store_set_value:
750 * @list_store: A #GtkListStore
751 * @iter: A valid #GtkTreeIter for the row being modified
752 * @column: column number to modify
753 * @value: new value for the cell
755 * Sets the data in the cell specified by @iter and @column.
756 * The type of @value must be convertible to the type of the
761 gtk_list_store_set_value (GtkListStore *list_store,
766 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
767 g_return_if_fail (VALID_ITER (iter, list_store));
768 g_return_if_fail (column >= 0 && column < list_store->n_columns);
769 g_return_if_fail (G_IS_VALUE (value));
771 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
775 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
776 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
777 gtk_tree_path_free (path);
782 * gtk_list_store_set_valist:
783 * @list_store: A #GtkListStore
784 * @iter: A valid #GtkTreeIter for the row being modified
785 * @var_args: va_list of column/value pairs
787 * See gtk_list_store_set(); this version takes a va_list for use by language
792 gtk_list_store_set_valist (GtkListStore *list_store,
797 gboolean emit_signal = FALSE;
798 gboolean maybe_need_sort = FALSE;
799 GtkTreeIterCompareFunc func = NULL;
801 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
802 g_return_if_fail (VALID_ITER (iter, list_store));
804 column = va_arg (var_args, gint);
806 if (GTK_LIST_STORE_IS_SORTED (list_store))
808 if (list_store->sort_column_id != -1)
810 GtkTreeDataSortHeader *header;
811 header = _gtk_tree_data_list_get_header (list_store->sort_list,
812 list_store->sort_column_id);
813 g_return_if_fail (header != NULL);
814 g_return_if_fail (header->func != NULL);
819 func = list_store->default_sort_func;
823 if (func != _gtk_tree_data_list_compare_func)
824 maybe_need_sort = TRUE;
828 GValue value = { 0, };
831 if (column >= list_store->n_columns)
833 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
836 g_value_init (&value, list_store->column_headers[column]);
838 G_VALUE_COLLECT (&value, var_args, 0, &error);
841 g_warning ("%s: %s", G_STRLOC, error);
844 /* we purposely leak the value here, it might not be
845 * in a sane state if an error condition occoured
850 /* FIXME: instead of calling this n times, refactor with above */
851 emit_signal = gtk_list_store_real_set_value (list_store,
855 FALSE) || emit_signal;
857 if (func == _gtk_tree_data_list_compare_func &&
858 column == list_store->sort_column_id)
859 maybe_need_sort = TRUE;
861 g_value_unset (&value);
863 column = va_arg (var_args, gint);
866 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
867 gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
873 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
874 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
875 gtk_tree_path_free (path);
880 * gtk_list_store_set:
881 * @list_store: a #GtkListStore
882 * @iter: row iterator
883 * @Varargs: pairs of column number and value, terminated with -1
885 * Sets the value of one or more cells in the row referenced by @iter.
886 * The variable argument list should contain integer column numbers,
887 * each column number followed by the value to be set.
888 * The list is terminated by a -1. For example, to set column 0 with type
889 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
890 * 0, "Foo", -1)</literal>.
893 gtk_list_store_set (GtkListStore *list_store,
899 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
900 g_return_if_fail (iter != NULL);
901 g_return_if_fail (iter->stamp == list_store->stamp);
903 va_start (var_args, iter);
904 gtk_list_store_set_valist (list_store, iter, var_args);
909 * gtk_list_store_remove:
910 * @list_store: A #GtkListStore
911 * @iter: A valid #GtkTreeIter
913 * Removes the given row from the list store. After being removed,
914 * @iter is set to be the next valid row, or invalidated if it pointed
915 * to the last row in @list_store.
917 * Return value: %TRUE if @iter is valid, %FALSE if not.
920 gtk_list_store_remove (GtkListStore *list_store,
924 GtkSequencePtr ptr, next;
926 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
927 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
929 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
931 ptr = iter->user_data;
932 next = _gtk_sequence_ptr_next (ptr);
934 _gtk_tree_data_list_free (_gtk_sequence_ptr_get_data (ptr), list_store->column_headers);
935 _gtk_sequence_remove (iter->user_data);
937 list_store->length--;
939 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
940 gtk_tree_path_free (path);
942 if (_gtk_sequence_ptr_is_end (next))
949 iter->stamp = list_store->stamp;
950 iter->user_data = next;
956 * gtk_list_store_insert:
957 * @list_store: A #GtkListStore
958 * @iter: An unset #GtkTreeIter to set to the new row
959 * @position: position to insert the new row
961 * Creates a new row at @position. @iter will be changed to point to this new
962 * row. If @position is larger than the number of rows on the list, then the
963 * new row will be appended to the list. The row will be empty before this
964 * function is called. To fill in values, you need to call gtk_list_store_set()
965 * or gtk_list_store_set_value().
969 gtk_list_store_insert (GtkListStore *list_store,
978 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
979 g_return_if_fail (iter != NULL);
980 g_return_if_fail (position >= 0);
982 list_store->columns_dirty = TRUE;
984 seq = list_store->seq;
986 length = _gtk_sequence_get_length (seq);
987 if (position > length)
990 ptr = _gtk_sequence_get_ptr_at_pos (seq, position);
991 ptr = _gtk_sequence_insert (ptr, NULL);
993 iter->stamp = list_store->stamp;
994 iter->user_data = ptr;
996 g_assert (VALID_ITER (iter, list_store));
998 list_store->length++;
1000 path = gtk_tree_path_new ();
1001 gtk_tree_path_append_index (path, position);
1002 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1003 gtk_tree_path_free (path);
1007 * gtk_list_store_insert_before:
1008 * @list_store: A #GtkListStore
1009 * @iter: An unset #GtkTreeIter to set to the new row
1010 * @sibling: A valid #GtkTreeIter, or %NULL
1012 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
1013 * appended to the end of the list. @iter will be changed to point to this new
1014 * row. The row will be empty before this function is called. To fill in values,
1015 * you need to call gtk_list_store_set() or gtk_list_store_set_value().
1019 gtk_list_store_insert_before (GtkListStore *list_store,
1021 GtkTreeIter *sibling)
1023 GtkSequencePtr after;
1025 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1026 g_return_if_fail (iter != NULL);
1028 g_return_if_fail (VALID_ITER (sibling, list_store));
1031 after = _gtk_sequence_get_end_ptr (list_store->seq);
1033 after = sibling->user_data;
1035 gtk_list_store_insert (list_store, iter, _gtk_sequence_ptr_get_position (after));
1039 * gtk_list_store_insert_after:
1040 * @list_store: A #GtkListStore
1041 * @iter: An unset #GtkTreeIter to set to the new row
1042 * @sibling: A valid #GtkTreeIter, or %NULL
1044 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1045 * prepended to the beginning of the list. @iter will be changed to point to
1046 * this new row. The row will be empty after this function is called. To fill
1047 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1051 gtk_list_store_insert_after (GtkListStore *list_store,
1053 GtkTreeIter *sibling)
1055 GtkSequencePtr after;
1057 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1058 g_return_if_fail (iter != NULL);
1060 g_return_if_fail (VALID_ITER (sibling, list_store));
1063 after = _gtk_sequence_get_begin_ptr (list_store->seq);
1065 after = _gtk_sequence_ptr_next (sibling->user_data);
1067 gtk_list_store_insert (list_store, iter, _gtk_sequence_ptr_get_position (after));
1071 * gtk_list_store_prepend:
1072 * @list_store: A #GtkListStore
1073 * @iter: An unset #GtkTreeIter to set to the prepend row
1075 * Prepends a new row to @list_store. @iter will be changed to point to this new
1076 * row. The row will be empty after this function is called. To fill in
1077 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1081 gtk_list_store_prepend (GtkListStore *list_store,
1084 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1085 g_return_if_fail (iter != NULL);
1087 gtk_list_store_insert (list_store, iter, 0);
1091 * gtk_list_store_append:
1092 * @list_store: A #GtkListStore
1093 * @iter: An unset #GtkTreeIter to set to the appended row
1095 * Appends a new row to @list_store. @iter will be changed to point to this new
1096 * row. The row will be empty after this function is called. To fill in
1097 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1101 gtk_list_store_append (GtkListStore *list_store,
1104 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1105 g_return_if_fail (iter != NULL);
1107 gtk_list_store_insert (list_store, iter, _gtk_sequence_get_length (list_store->seq));
1111 * gtk_list_store_clear:
1112 * @list_store: a #GtkListStore.
1114 * Removes all rows from the list store.
1118 gtk_list_store_clear (GtkListStore *list_store)
1121 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1123 while (_gtk_sequence_get_length (list_store->seq) > 0)
1125 iter.stamp = list_store->stamp;
1126 iter.user_data = _gtk_sequence_get_begin_ptr (list_store->seq);
1127 gtk_list_store_remove (list_store, &iter);
1132 * gtk_list_store_iter_is_valid:
1133 * @list_store: A #GtkListStore.
1134 * @iter: A #GtkTreeIter.
1136 * WARNING: This function is slow. Only use it for debugging and/or testing
1139 * Checks if the given iter is a valid iter for this #GtkListStore.
1141 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1146 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1149 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1150 g_return_val_if_fail (iter != NULL, FALSE);
1152 if (!VALID_ITER (iter, list_store))
1155 if (_gtk_sequence_ptr_get_sequence (iter->user_data) != list_store->seq)
1161 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
1168 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1172 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1174 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1178 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1185 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1187 GtkSelectionData *selection_data)
1189 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1191 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1192 * target, because the default handler does it for us, but
1193 * we do anyway for the convenience of someone maybe overriding the
1197 if (gtk_tree_set_row_drag_data (selection_data,
1198 GTK_TREE_MODEL (drag_source),
1205 /* FIXME handle text targets at least. */
1212 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1214 GtkSelectionData *selection_data)
1216 GtkTreeModel *tree_model;
1217 GtkListStore *list_store;
1218 GtkTreeModel *src_model = NULL;
1219 GtkTreePath *src_path = NULL;
1220 gboolean retval = FALSE;
1222 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1224 tree_model = GTK_TREE_MODEL (drag_dest);
1225 list_store = GTK_LIST_STORE (drag_dest);
1227 if (gtk_tree_get_row_drag_data (selection_data,
1230 src_model == tree_model)
1232 /* Copy the given row to a new position */
1233 GtkTreeIter src_iter;
1234 GtkTreeIter dest_iter;
1237 if (!gtk_tree_model_get_iter (src_model,
1244 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1245 prev = gtk_tree_path_copy (dest);
1247 if (!gtk_tree_path_prev (prev))
1249 /* dest was the first spot in the list; which means we are supposed
1252 gtk_list_store_prepend (list_store, &dest_iter);
1258 if (gtk_tree_model_get_iter (tree_model, &dest_iter, prev))
1260 GtkTreeIter tmp_iter = dest_iter;
1262 gtk_list_store_insert_after (list_store, &dest_iter, &tmp_iter);
1268 gtk_tree_path_free (prev);
1270 /* If we succeeded in creating dest_iter, copy data from src
1274 GtkTreeDataList *dl = _gtk_sequence_ptr_get_data (src_iter.user_data);
1275 GtkTreeDataList *copy_head = NULL;
1276 GtkTreeDataList *copy_prev = NULL;
1277 GtkTreeDataList *copy_iter = NULL;
1284 copy_iter = _gtk_tree_data_list_node_copy (dl,
1285 list_store->column_headers[col]);
1287 if (copy_head == NULL)
1288 copy_head = copy_iter;
1291 copy_prev->next = copy_iter;
1293 copy_prev = copy_iter;
1299 dest_iter.stamp = list_store->stamp;
1300 _gtk_sequence_set (dest_iter.user_data, copy_head);
1302 path = gtk_list_store_get_path (tree_model, &dest_iter);
1303 gtk_tree_model_row_changed (tree_model, path, &dest_iter);
1304 gtk_tree_path_free (path);
1309 /* FIXME maybe add some data targets eventually, or handle text
1310 * targets in the simple case.
1317 gtk_tree_path_free (src_path);
1323 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1324 GtkTreePath *dest_path,
1325 GtkSelectionData *selection_data)
1328 GtkTreeModel *src_model = NULL;
1329 GtkTreePath *src_path = NULL;
1330 gboolean retval = FALSE;
1332 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1334 /* don't accept drops if the list has been sorted */
1335 if (GTK_LIST_STORE_IS_SORTED (drag_dest))
1338 if (!gtk_tree_get_row_drag_data (selection_data,
1343 if (src_model != GTK_TREE_MODEL (drag_dest))
1346 if (gtk_tree_path_get_depth (dest_path) != 1)
1349 /* can drop before any existing node, or before one past any existing. */
1351 indices = gtk_tree_path_get_indices (dest_path);
1353 if (indices[0] <= _gtk_sequence_get_length (GTK_LIST_STORE (drag_dest)->seq))
1358 gtk_tree_path_free (src_path);
1363 /* Sorting and reordering */
1367 gtk_list_store_reorder_func (gconstpointer a,
1371 GHashTable *new_positions = user_data;
1372 gint apos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, a));
1373 gint bpos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, b));
1383 * gtk_list_store_reorder:
1384 * @store: A #GtkListStore.
1385 * @new_order: an array of integers mapping the new position of each child
1386 * to its old position before the re-ordering,
1387 * i.e. @new_order<literal>[newpos] = oldpos</literal>.
1389 * Reorders @store to follow the order indicated by @new_order. Note that
1390 * this function only works with unsorted stores.
1395 gtk_list_store_reorder (GtkListStore *store,
1400 GHashTable *new_positions;
1403 g_return_if_fail (GTK_IS_LIST_STORE (store));
1404 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1405 g_return_if_fail (new_order != NULL);
1407 new_positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1409 ptr = _gtk_sequence_get_begin_ptr (store->seq);
1413 g_hash_table_insert (new_positions, ptr, GINT_TO_POINTER (new_order[i++]));
1415 ptr = _gtk_sequence_ptr_next (ptr);
1418 _gtk_sequence_sort (store->seq, gtk_list_store_reorder_func, new_positions);
1420 g_hash_table_destroy (new_positions);
1423 path = gtk_tree_path_new ();
1424 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1425 path, NULL, new_order);
1426 gtk_tree_path_free (path);
1430 * gtk_list_store_swap:
1431 * @store: A #GtkListStore.
1432 * @a: A #GtkTreeIter.
1433 * @b: Another #GtkTreeIter.
1435 * Swaps @a and @b in @store. Note that this function only works with
1441 gtk_list_store_swap (GtkListStore *store,
1447 g_return_if_fail (GTK_IS_LIST_STORE (store));
1448 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1449 g_return_if_fail (VALID_ITER (a, store));
1450 g_return_if_fail (VALID_ITER (b, store));
1452 if (a->user_data == b->user_data)
1455 _gtk_sequence_swap (a->user_data, b->user_data);
1458 path = gtk_tree_path_new ();
1460 gtk_tree_model_row_changed (GTK_TREE_MODEL (store), path, a);
1461 gtk_tree_model_row_changed (GTK_TREE_MODEL (store), path, b);
1463 gtk_tree_path_free (path);
1467 save_positions (GtkSequence *seq)
1469 GHashTable *positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1472 ptr = _gtk_sequence_get_begin_ptr (seq);
1473 while (!_gtk_sequence_ptr_is_end (ptr))
1475 g_hash_table_insert (positions, ptr,
1476 GINT_TO_POINTER (_gtk_sequence_ptr_get_position (ptr)));
1477 ptr = _gtk_sequence_ptr_next (ptr);
1484 generate_order (GtkSequence *seq,
1485 GHashTable *old_positions)
1488 int *order = g_new (int, _gtk_sequence_get_length (seq));
1492 ptr = _gtk_sequence_get_begin_ptr (seq);
1493 while (!_gtk_sequence_ptr_is_end (ptr))
1495 int old_pos = GPOINTER_TO_INT (g_hash_table_lookup (old_positions, ptr));
1496 order[old_pos] = i++;
1497 ptr = _gtk_sequence_ptr_next (ptr);
1500 g_hash_table_destroy (old_positions);
1506 gtk_list_store_move_to (GtkListStore *store,
1510 GHashTable *old_positions;
1514 old_positions = save_positions (store->seq);
1516 _gtk_sequence_move (iter->user_data, _gtk_sequence_get_ptr_at_pos (store->seq, new_pos));
1518 order = generate_order (store->seq, old_positions);
1520 path = gtk_tree_path_new ();
1521 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1523 gtk_tree_path_free (path);
1528 * gtk_list_store_move_before:
1529 * @store: A #GtkListStore.
1530 * @iter: A #GtkTreeIter.
1531 * @position: A #GtkTreeIter, or %NULL.
1533 * Moves @iter in @store to the position before @position. Note that this
1534 * function only works with unsorted stores. If @position is %NULL, @iter
1535 * will be moved to the end of the list.
1540 gtk_list_store_move_before (GtkListStore *store,
1542 GtkTreeIter *position)
1546 g_return_if_fail (GTK_IS_LIST_STORE (store));
1547 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1548 g_return_if_fail (VALID_ITER (iter, store));
1550 g_return_if_fail (VALID_ITER (position, store));
1553 pos = _gtk_sequence_ptr_get_position (iter->user_data);
1557 gtk_list_store_move_to (store, iter, pos);
1561 * gtk_list_store_move_after:
1562 * @store: A #GtkListStore.
1563 * @iter: A #GtkTreeIter.
1564 * @position: A #GtkTreeIter or %NULL.
1566 * Moves @iter in @store to the position after @position. Note that this
1567 * function only works with unsorted stores. If @position is %NULL, @iter
1568 * will be moved to the start of the list.
1573 gtk_list_store_move_after (GtkListStore *store,
1575 GtkTreeIter *position)
1579 g_return_if_fail (GTK_IS_LIST_STORE (store));
1580 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1581 g_return_if_fail (VALID_ITER (iter, store));
1583 g_return_if_fail (VALID_ITER (position, store));
1586 pos = _gtk_sequence_ptr_get_position (iter->user_data);
1590 gtk_list_store_move_to (store, iter, pos);
1595 gtk_list_store_compare_func (gconstpointer a,
1599 GtkListStore *list_store = user_data;
1603 GtkTreeIterCompareFunc func;
1606 if (list_store->sort_column_id != -1)
1608 GtkTreeDataSortHeader *header;
1610 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1611 list_store->sort_column_id);
1612 g_return_val_if_fail (header != NULL, 0);
1613 g_return_val_if_fail (header->func != NULL, 0);
1615 func = header->func;
1616 data = header->data;
1620 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1621 func = list_store->default_sort_func;
1622 data = list_store->default_sort_data;
1625 iter_a.stamp = list_store->stamp;
1626 iter_a.user_data = (gpointer)a;
1627 iter_b.stamp = list_store->stamp;
1628 iter_b.user_data = (gpointer)b;
1630 g_assert (VALID_ITER (&iter_a, list_store));
1631 g_assert (VALID_ITER (&iter_b, list_store));
1633 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1635 if (list_store->order == GTK_SORT_DESCENDING)
1639 else if (retval < 0)
1647 gtk_list_store_sort (GtkListStore *list_store)
1651 GHashTable *old_positions;
1653 if (_gtk_sequence_get_length (list_store->seq) <= 1)
1656 old_positions = save_positions (list_store->seq);
1658 _gtk_sequence_sort (list_store->seq, gtk_list_store_compare_func, list_store);
1660 /* Let the world know about our new order */
1661 new_order = generate_order (list_store->seq, old_positions);
1663 path = gtk_tree_path_new ();
1664 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1665 path, NULL, new_order);
1666 gtk_tree_path_free (path);
1671 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1676 GtkTreePath *tmp_path;
1678 _gtk_sequence_sort_changed (iter->user_data,
1679 gtk_list_store_compare_func,
1682 tmp_path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
1683 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), tmp_path, iter);
1684 gtk_tree_path_free (tmp_path);
1688 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1689 gint *sort_column_id,
1692 GtkListStore *list_store = (GtkListStore *) sortable;
1694 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1696 if (list_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1700 * sort_column_id = list_store->sort_column_id;
1702 * order = list_store->order;
1707 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1708 gint sort_column_id,
1711 GtkListStore *list_store = (GtkListStore *) sortable;
1713 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1715 if ((list_store->sort_column_id == sort_column_id) &&
1716 (list_store->order == order))
1719 if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1721 GtkTreeDataSortHeader *header = NULL;
1723 header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
1725 /* We want to make sure that we have a function */
1726 g_return_if_fail (header != NULL);
1727 g_return_if_fail (header->func != NULL);
1731 g_return_if_fail (list_store->default_sort_func != NULL);
1735 list_store->sort_column_id = sort_column_id;
1736 list_store->order = order;
1738 gtk_tree_sortable_sort_column_changed (sortable);
1740 gtk_list_store_sort (list_store);
1744 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
1745 gint sort_column_id,
1746 GtkTreeIterCompareFunc func,
1748 GtkDestroyNotify destroy)
1750 GtkListStore *list_store = (GtkListStore *) sortable;
1751 GtkTreeDataSortHeader *header = NULL;
1754 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1755 g_return_if_fail (func != NULL);
1757 for (list = list_store->sort_list; list; list = list->next)
1759 GtkTreeDataSortHeader *list_header;
1761 list_header = (GtkTreeDataSortHeader*) list->data;
1762 if (list_header->sort_column_id == sort_column_id)
1764 header = list_header;
1771 header = g_new0 (GtkTreeDataSortHeader, 1);
1772 header->sort_column_id = sort_column_id;
1773 list_store->sort_list = g_list_append (list_store->sort_list, header);
1776 if (header->destroy)
1778 GtkDestroyNotify d = header->destroy;
1780 header->destroy = NULL;
1784 header->func = func;
1785 header->data = data;
1786 header->destroy = destroy;
1788 if (list_store->sort_column_id == sort_column_id)
1789 gtk_list_store_sort (list_store);
1793 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
1794 GtkTreeIterCompareFunc func,
1796 GtkDestroyNotify destroy)
1798 GtkListStore *list_store = (GtkListStore *) sortable;
1800 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1802 if (list_store->default_sort_destroy)
1804 GtkDestroyNotify d = list_store->default_sort_destroy;
1806 list_store->default_sort_destroy = NULL;
1807 d (list_store->default_sort_data);
1810 list_store->default_sort_func = func;
1811 list_store->default_sort_data = data;
1812 list_store->default_sort_destroy = destroy;
1814 if (list_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1815 gtk_list_store_sort (list_store);
1819 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
1821 GtkListStore *list_store = (GtkListStore *) sortable;
1823 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1825 return (list_store->default_sort_func != NULL);