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, see <http://www.gnu.org/licenses/>.
22 #include <gobject/gvaluecollector.h>
23 #include "gtktreemodel.h"
24 #include "gtkliststore.h"
25 #include "gtktreedatalist.h"
26 #include "gtktreednd.h"
28 #include "gtkbuildable.h"
29 #include "gtkbuilderprivate.h"
33 * SECTION:gtkliststore
34 * @Short_description: A list-like data structure that can be used with the GtkTreeView
35 * @Title: GtkListStore
36 * @See_also:#GtkTreeModel, #GtkTreeStore
38 * The #GtkListStore object is a list model for use with a #GtkTreeView
39 * widget. It implements the #GtkTreeModel interface, and consequentialy,
40 * can use all of the methods available there. It also implements the
41 * #GtkTreeSortable interface so it can be sorted by the view.
42 * Finally, it also implements the tree <link linkend="gtktreednd">drag and
43 * drop</link> interfaces.
45 * The #GtkListStore can accept most GObject types as a column type, though
46 * it can't accept all custom types. Internally, it will keep a copy of
47 * data passed in (such as a string or a boxed pointer). Columns that
48 * accept #GObject<!-- -->s are handled a little differently. The
49 * #GtkListStore will keep a reference to the object instead of copying the
50 * value. As a result, if the object is modified, it is up to the
51 * application writer to call gtk_tree_model_row_changed() to emit the
52 * #GtkTreeModel::row_changed signal. This most commonly affects lists with
53 * #GdkPixbuf<!-- -->s stored.
56 * <title>Creating a simple list store.</title>
66 * GtkListStore *list_store;
71 * list_store = gtk_list_store_new (N_COLUMNS,
76 * for (i = 0; i < 10; i++)
80 * some_data = get_some_data (i);
82 * // Add a new row to the model
83 * gtk_list_store_append (list_store, &iter);
84 * gtk_list_store_set (list_store, &iter,
85 * COLUMN_STRING, some_data,
87 * COLUMN_BOOLEAN, FALSE,
90 * /<!---->* As the store will keep a copy of the string internally, we
96 * // Modify a particular row
97 * path = gtk_tree_path_new_from_string ("4");
98 * gtk_tree_model_get_iter (GTK_TREE_MODEL (list_store),
101 * gtk_tree_path_free (path);
102 * gtk_list_store_set (list_store, &iter,
103 * COLUMN_BOOLEAN, TRUE,
110 * <title>Performance Considerations</title>
111 * Internally, the #GtkListStore was implemented with a linked list with a
112 * tail pointer prior to GTK+ 2.6. As a result, it was fast at data
113 * insertion and deletion, and not fast at random data access. The
114 * #GtkListStore sets the #GTK_TREE_MODEL_ITERS_PERSIST flag, which means
115 * that #GtkTreeIter<!-- -->s can be cached while the row exists. Thus, if
116 * access to a particular row is needed often and your code is expected to
117 * run on older versions of GTK+, it is worth keeping the iter around.
120 * <title>Atomic Operations</title>
121 * It is important to note that only the methods
122 * gtk_list_store_insert_with_values() and gtk_list_store_insert_with_valuesv()
123 * are atomic, in the sense that the row is being appended to the store and the
124 * values filled in in a single operation with regard to #GtkTreeModel signaling.
125 * In contrast, using e.g. gtk_list_store_append() and then gtk_list_store_set()
126 * will first create a row, which triggers the #GtkTreeModel::row-inserted signal
127 * on #GtkListStore. The row, however, is still empty, and any signal handler
128 * connecting to #GtkTreeModel::row-inserted on this particular store should be prepared
129 * for the situation that the row might be empty. This is especially important
130 * if you are wrapping the #GtkListStore inside a #GtkTreeModelFilter and are
131 * using a #GtkTreeModelFilterVisibleFunc. Using any of the non-atomic operations
132 * to append rows to the #GtkListStore will cause the
133 * #GtkTreeModelFilterVisibleFunc to be visited with an empty row first; the
134 * function must be prepared for that.
136 * <refsect2 id="GtkListStore-BUILDER-UI">
137 * <title>GtkListStore as GtkBuildable</title>
139 * The GtkListStore implementation of the GtkBuildable interface allows
140 * to specify the model columns with a <columns> element that may
141 * contain multiple <column> elements, each specifying one model
142 * column. The "type" attribute specifies the data type for the column.
144 * Additionally, it is possible to specify content for the list store
145 * in the UI definition, with the <data> element. It can contain
146 * multiple <row> elements, each specifying to content for one
147 * row of the list model. Inside a <row>, the <col> elements
148 * specify the content for individual cells.
150 * Note that it is probably more common to define your models
151 * in the code, and one might consider it a layering violation
152 * to specify the content of a list store in a UI definition,
153 * <emphasis>data</emphasis>, not <emphasis>presentation</emphasis>,
154 * and common wisdom is to separate the two, as far as possible.
155 * <!-- FIXME a bit inconclusive -->
158 * <title>A UI Definition fragment for a list store</title>
159 * <programlisting><![CDATA[
160 * <object class="GtkListStore">
162 * <column type="gchararray"/>
163 * <column type="gchararray"/>
164 * <column type="gint"/>
168 * <col id="0">John</col>
169 * <col id="1">Doe</col>
170 * <col id="2">25</col>
173 * <col id="0">Johan</col>
174 * <col id="1">Dahlin</col>
175 * <col id="2">50</col>
179 * ]]></programlisting>
186 struct _GtkListStorePrivate
188 GtkTreeIterCompareFunc default_sort_func;
190 GDestroyNotify default_sort_destroy;
192 GType *column_headers;
201 guint columns_dirty : 1;
203 gpointer default_sort_data;
204 gpointer seq; /* head of the list */
207 #define GTK_LIST_STORE_IS_SORTED(list) (((GtkListStore*)(list))->priv->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
208 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
209 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
210 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
211 static void gtk_list_store_sortable_init (GtkTreeSortableIface *iface);
212 static void gtk_list_store_buildable_init (GtkBuildableIface *iface);
213 static void gtk_list_store_finalize (GObject *object);
214 static GtkTreeModelFlags gtk_list_store_get_flags (GtkTreeModel *tree_model);
215 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
216 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
218 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
221 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
223 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
227 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
229 static gboolean gtk_list_store_iter_previous (GtkTreeModel *tree_model,
231 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
233 GtkTreeIter *parent);
234 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
236 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
238 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
242 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
247 static void gtk_list_store_set_n_columns (GtkListStore *list_store,
249 static void gtk_list_store_set_column_type (GtkListStore *list_store,
253 static void gtk_list_store_increment_stamp (GtkListStore *list_store);
257 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
259 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
261 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
263 GtkSelectionData *selection_data);
264 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
266 GtkSelectionData *selection_data);
267 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
268 GtkTreePath *dest_path,
269 GtkSelectionData *selection_data);
273 static void gtk_list_store_sort (GtkListStore *list_store);
274 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
277 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
278 gint *sort_column_id,
280 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
283 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
285 GtkTreeIterCompareFunc func,
287 GDestroyNotify destroy);
288 static void gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
289 GtkTreeIterCompareFunc func,
291 GDestroyNotify destroy);
292 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable);
296 static gboolean gtk_list_store_buildable_custom_tag_start (GtkBuildable *buildable,
299 const gchar *tagname,
300 GMarkupParser *parser,
302 static void gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
305 const gchar *tagname,
308 G_DEFINE_TYPE_WITH_CODE (GtkListStore, gtk_list_store, G_TYPE_OBJECT,
309 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
310 gtk_list_store_tree_model_init)
311 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
312 gtk_list_store_drag_source_init)
313 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
314 gtk_list_store_drag_dest_init)
315 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
316 gtk_list_store_sortable_init)
317 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
318 gtk_list_store_buildable_init))
322 gtk_list_store_class_init (GtkListStoreClass *class)
324 GObjectClass *object_class;
326 object_class = (GObjectClass*) class;
328 object_class->finalize = gtk_list_store_finalize;
330 g_type_class_add_private (class, sizeof (GtkListStorePrivate));
334 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
336 iface->get_flags = gtk_list_store_get_flags;
337 iface->get_n_columns = gtk_list_store_get_n_columns;
338 iface->get_column_type = gtk_list_store_get_column_type;
339 iface->get_iter = gtk_list_store_get_iter;
340 iface->get_path = gtk_list_store_get_path;
341 iface->get_value = gtk_list_store_get_value;
342 iface->iter_next = gtk_list_store_iter_next;
343 iface->iter_previous = gtk_list_store_iter_previous;
344 iface->iter_children = gtk_list_store_iter_children;
345 iface->iter_has_child = gtk_list_store_iter_has_child;
346 iface->iter_n_children = gtk_list_store_iter_n_children;
347 iface->iter_nth_child = gtk_list_store_iter_nth_child;
348 iface->iter_parent = gtk_list_store_iter_parent;
352 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
354 iface->row_draggable = real_gtk_list_store_row_draggable;
355 iface->drag_data_delete = gtk_list_store_drag_data_delete;
356 iface->drag_data_get = gtk_list_store_drag_data_get;
360 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
362 iface->drag_data_received = gtk_list_store_drag_data_received;
363 iface->row_drop_possible = gtk_list_store_row_drop_possible;
367 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
369 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
370 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
371 iface->set_sort_func = gtk_list_store_set_sort_func;
372 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
373 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
377 gtk_list_store_buildable_init (GtkBuildableIface *iface)
379 iface->custom_tag_start = gtk_list_store_buildable_custom_tag_start;
380 iface->custom_tag_end = gtk_list_store_buildable_custom_tag_end;
384 gtk_list_store_init (GtkListStore *list_store)
386 GtkListStorePrivate *priv;
388 list_store->priv = G_TYPE_INSTANCE_GET_PRIVATE (list_store,
390 GtkListStorePrivate);
391 priv = list_store->priv;
393 priv->seq = g_sequence_new (NULL);
394 priv->sort_list = NULL;
395 priv->stamp = g_random_int ();
396 priv->sort_column_id = GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID;
397 priv->columns_dirty = FALSE;
402 iter_is_valid (GtkTreeIter *iter,
403 GtkListStore *list_store)
405 return iter != NULL &&
406 iter->user_data != NULL &&
407 list_store->priv->stamp == iter->stamp &&
408 !g_sequence_iter_is_end (iter->user_data) &&
409 g_sequence_iter_get_sequence (iter->user_data) == list_store->priv->seq;
413 * gtk_list_store_new:
414 * @n_columns: number of columns in the list store
415 * @...: all #GType types for the columns, from first to last
417 * Creates a new list store as with @n_columns columns each of the types passed
418 * in. Note that only types derived from standard GObject fundamental types
421 * As an example, <literal>gtk_list_store_new (3, G_TYPE_INT, G_TYPE_STRING,
422 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
423 * int, string and #GdkPixbuf respectively.
425 * Return value: a new #GtkListStore
428 gtk_list_store_new (gint n_columns,
431 GtkListStore *retval;
435 g_return_val_if_fail (n_columns > 0, NULL);
437 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
438 gtk_list_store_set_n_columns (retval, n_columns);
440 va_start (args, n_columns);
442 for (i = 0; i < n_columns; i++)
444 GType type = va_arg (args, GType);
445 if (! _gtk_tree_data_list_check_type (type))
447 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
448 g_object_unref (retval);
454 gtk_list_store_set_column_type (retval, i, type);
464 * gtk_list_store_newv:
465 * @n_columns: number of columns in the list store
466 * @types: (array length=n_columns): an array of #GType types for the columns, from first to last
468 * Non-vararg creation function. Used primarily by language bindings.
470 * Return value: (transfer full): a new #GtkListStore
471 * Rename to: gtk_list_store_new
474 gtk_list_store_newv (gint n_columns,
477 GtkListStore *retval;
480 g_return_val_if_fail (n_columns > 0, NULL);
482 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
483 gtk_list_store_set_n_columns (retval, n_columns);
485 for (i = 0; i < n_columns; i++)
487 if (! _gtk_tree_data_list_check_type (types[i]))
489 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
490 g_object_unref (retval);
494 gtk_list_store_set_column_type (retval, i, types[i]);
501 * gtk_list_store_set_column_types:
502 * @list_store: A #GtkListStore
503 * @n_columns: Number of columns for the list store
504 * @types: (array length=n_columns): An array length n of #GTypes
506 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
507 * and should only be used when constructing a new #GtkListStore. It will not
508 * function after a row has been added, or a method on the #GtkTreeModel
509 * interface is called.
512 gtk_list_store_set_column_types (GtkListStore *list_store,
516 GtkListStorePrivate *priv;
519 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
521 priv = list_store->priv;
523 g_return_if_fail (priv->columns_dirty == 0);
525 gtk_list_store_set_n_columns (list_store, n_columns);
526 for (i = 0; i < n_columns; i++)
528 if (! _gtk_tree_data_list_check_type (types[i]))
530 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
533 gtk_list_store_set_column_type (list_store, i, types[i]);
538 gtk_list_store_set_n_columns (GtkListStore *list_store,
541 GtkListStorePrivate *priv = list_store->priv;
544 if (priv->n_columns == n_columns)
547 priv->column_headers = g_renew (GType, priv->column_headers, n_columns);
548 for (i = priv->n_columns; i < n_columns; i++)
549 priv->column_headers[i] = G_TYPE_INVALID;
550 priv->n_columns = n_columns;
553 _gtk_tree_data_list_header_free (priv->sort_list);
554 priv->sort_list = _gtk_tree_data_list_header_new (n_columns, priv->column_headers);
558 gtk_list_store_set_column_type (GtkListStore *list_store,
562 GtkListStorePrivate *priv = list_store->priv;
564 if (!_gtk_tree_data_list_check_type (type))
566 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
570 priv->column_headers[column] = type;
574 gtk_list_store_finalize (GObject *object)
576 GtkListStore *list_store = GTK_LIST_STORE (object);
577 GtkListStorePrivate *priv = list_store->priv;
579 g_sequence_foreach (priv->seq,
580 (GFunc) _gtk_tree_data_list_free, priv->column_headers);
582 g_sequence_free (priv->seq);
584 _gtk_tree_data_list_header_free (priv->sort_list);
585 g_free (priv->column_headers);
587 if (priv->default_sort_destroy)
589 GDestroyNotify d = priv->default_sort_destroy;
591 priv->default_sort_destroy = NULL;
592 d (priv->default_sort_data);
593 priv->default_sort_data = NULL;
596 G_OBJECT_CLASS (gtk_list_store_parent_class)->finalize (object);
599 /* Fulfill the GtkTreeModel requirements */
600 static GtkTreeModelFlags
601 gtk_list_store_get_flags (GtkTreeModel *tree_model)
603 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
607 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
609 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
610 GtkListStorePrivate *priv = list_store->priv;
612 priv->columns_dirty = TRUE;
614 return priv->n_columns;
618 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
621 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
622 GtkListStorePrivate *priv = list_store->priv;
624 g_return_val_if_fail (index < priv->n_columns, G_TYPE_INVALID);
626 priv->columns_dirty = TRUE;
628 return priv->column_headers[index];
632 gtk_list_store_get_iter (GtkTreeModel *tree_model,
636 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
637 GtkListStorePrivate *priv = list_store->priv;
641 priv->columns_dirty = TRUE;
645 i = gtk_tree_path_get_indices (path)[0];
647 if (i >= g_sequence_get_length (seq))
653 iter->stamp = priv->stamp;
654 iter->user_data = g_sequence_get_iter_at_pos (seq, i);
660 gtk_list_store_get_path (GtkTreeModel *tree_model,
663 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
664 GtkListStorePrivate *priv = list_store->priv;
667 g_return_val_if_fail (iter->stamp == priv->stamp, NULL);
669 if (g_sequence_iter_is_end (iter->user_data))
672 path = gtk_tree_path_new ();
673 gtk_tree_path_append_index (path, g_sequence_iter_get_position (iter->user_data));
679 gtk_list_store_get_value (GtkTreeModel *tree_model,
684 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
685 GtkListStorePrivate *priv = list_store->priv;
686 GtkTreeDataList *list;
687 gint tmp_column = column;
689 g_return_if_fail (column < priv->n_columns);
690 g_return_if_fail (iter_is_valid (iter, list_store));
692 list = g_sequence_get (iter->user_data);
694 while (tmp_column-- > 0 && list)
698 g_value_init (value, priv->column_headers[column]);
700 _gtk_tree_data_list_node_to_value (list,
701 priv->column_headers[column],
706 gtk_list_store_iter_next (GtkTreeModel *tree_model,
709 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
710 GtkListStorePrivate *priv = list_store->priv;
713 g_return_val_if_fail (priv->stamp == iter->stamp, FALSE);
714 iter->user_data = g_sequence_iter_next (iter->user_data);
716 retval = g_sequence_iter_is_end (iter->user_data);
724 gtk_list_store_iter_previous (GtkTreeModel *tree_model,
727 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
728 GtkListStorePrivate *priv = list_store->priv;
730 g_return_val_if_fail (priv->stamp == iter->stamp, FALSE);
732 if (g_sequence_iter_is_begin (iter->user_data))
738 iter->user_data = g_sequence_iter_prev (iter->user_data);
744 gtk_list_store_iter_children (GtkTreeModel *tree_model,
748 GtkListStore *list_store = (GtkListStore *) tree_model;
749 GtkListStorePrivate *priv = list_store->priv;
751 /* this is a list, nodes have no children */
758 if (g_sequence_get_length (priv->seq) > 0)
760 iter->stamp = priv->stamp;
761 iter->user_data = g_sequence_get_begin_iter (priv->seq);
772 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
779 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
782 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
783 GtkListStorePrivate *priv = list_store->priv;
786 return g_sequence_get_length (priv->seq);
788 g_return_val_if_fail (priv->stamp == iter->stamp, -1);
794 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
799 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
800 GtkListStorePrivate *priv = list_store->priv;
801 GSequenceIter *child;
808 child = g_sequence_get_iter_at_pos (priv->seq, n);
810 if (g_sequence_iter_is_end (child))
813 iter->stamp = priv->stamp;
814 iter->user_data = child;
820 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
829 gtk_list_store_real_set_value (GtkListStore *list_store,
835 GtkListStorePrivate *priv = list_store->priv;
836 GtkTreeDataList *list;
837 GtkTreeDataList *prev;
838 gint old_column = column;
839 GValue real_value = G_VALUE_INIT;
840 gboolean converted = FALSE;
841 gboolean retval = FALSE;
843 if (! g_type_is_a (G_VALUE_TYPE (value), priv->column_headers[column]))
845 if (! (g_value_type_transformable (G_VALUE_TYPE (value), priv->column_headers[column])))
847 g_warning ("%s: Unable to convert from %s to %s\n",
849 g_type_name (G_VALUE_TYPE (value)),
850 g_type_name (priv->column_headers[column]));
854 g_value_init (&real_value, priv->column_headers[column]);
855 if (!g_value_transform (value, &real_value))
857 g_warning ("%s: Unable to make conversion from %s to %s\n",
859 g_type_name (G_VALUE_TYPE (value)),
860 g_type_name (priv->column_headers[column]));
861 g_value_unset (&real_value);
867 prev = list = g_sequence_get (iter->user_data);
874 _gtk_tree_data_list_value_to_node (list, &real_value);
876 _gtk_tree_data_list_value_to_node (list, value);
879 g_value_unset (&real_value);
880 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
881 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
890 if (g_sequence_get (iter->user_data) == NULL)
892 list = _gtk_tree_data_list_alloc();
893 g_sequence_set (iter->user_data, list);
898 list = prev->next = _gtk_tree_data_list_alloc ();
904 list->next = _gtk_tree_data_list_alloc ();
911 _gtk_tree_data_list_value_to_node (list, &real_value);
913 _gtk_tree_data_list_value_to_node (list, value);
917 g_value_unset (&real_value);
919 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
920 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
927 * gtk_list_store_set_value:
928 * @list_store: A #GtkListStore
929 * @iter: A valid #GtkTreeIter for the row being modified
930 * @column: column number to modify
931 * @value: new value for the cell
933 * Sets the data in the cell specified by @iter and @column.
934 * The type of @value must be convertible to the type of the
939 gtk_list_store_set_value (GtkListStore *list_store,
944 GtkListStorePrivate *priv;
946 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
947 g_return_if_fail (iter_is_valid (iter, list_store));
948 g_return_if_fail (G_IS_VALUE (value));
949 priv = list_store->priv;
950 g_return_if_fail (column >= 0 && column < priv->n_columns);
952 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
956 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
957 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
958 gtk_tree_path_free (path);
962 static GtkTreeIterCompareFunc
963 gtk_list_store_get_compare_func (GtkListStore *list_store)
965 GtkListStorePrivate *priv = list_store->priv;
966 GtkTreeIterCompareFunc func = NULL;
968 if (GTK_LIST_STORE_IS_SORTED (list_store))
970 if (priv->sort_column_id != -1)
972 GtkTreeDataSortHeader *header;
973 header = _gtk_tree_data_list_get_header (priv->sort_list,
974 priv->sort_column_id);
975 g_return_val_if_fail (header != NULL, NULL);
976 g_return_val_if_fail (header->func != NULL, NULL);
981 func = priv->default_sort_func;
989 gtk_list_store_set_vector_internal (GtkListStore *list_store,
991 gboolean *emit_signal,
992 gboolean *maybe_need_sort,
997 GtkListStorePrivate *priv = list_store->priv;
999 GtkTreeIterCompareFunc func = NULL;
1001 func = gtk_list_store_get_compare_func (list_store);
1002 if (func != _gtk_tree_data_list_compare_func)
1003 *maybe_need_sort = TRUE;
1005 for (i = 0; i < n_values; i++)
1007 *emit_signal = gtk_list_store_real_set_value (list_store,
1011 FALSE) || *emit_signal;
1013 if (func == _gtk_tree_data_list_compare_func &&
1014 columns[i] == priv->sort_column_id)
1015 *maybe_need_sort = TRUE;
1020 gtk_list_store_set_valist_internal (GtkListStore *list_store,
1022 gboolean *emit_signal,
1023 gboolean *maybe_need_sort,
1026 GtkListStorePrivate *priv = list_store->priv;
1028 GtkTreeIterCompareFunc func = NULL;
1030 column = va_arg (var_args, gint);
1032 func = gtk_list_store_get_compare_func (list_store);
1033 if (func != _gtk_tree_data_list_compare_func)
1034 *maybe_need_sort = TRUE;
1036 while (column != -1)
1038 GValue value = G_VALUE_INIT;
1039 gchar *error = NULL;
1041 if (column < 0 || column >= priv->n_columns)
1043 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
1047 G_VALUE_COLLECT_INIT (&value, priv->column_headers[column],
1048 var_args, 0, &error);
1051 g_warning ("%s: %s", G_STRLOC, error);
1054 /* we purposely leak the value here, it might not be
1055 * in a sane state if an error condition occoured
1060 /* FIXME: instead of calling this n times, refactor with above */
1061 *emit_signal = gtk_list_store_real_set_value (list_store,
1065 FALSE) || *emit_signal;
1067 if (func == _gtk_tree_data_list_compare_func &&
1068 column == priv->sort_column_id)
1069 *maybe_need_sort = TRUE;
1071 g_value_unset (&value);
1073 column = va_arg (var_args, gint);
1078 * gtk_list_store_set_valuesv:
1079 * @list_store: A #GtkListStore
1080 * @iter: A valid #GtkTreeIter for the row being modified
1081 * @columns: (array length=n_values): an array of column numbers
1082 * @values: (array length=n_values): an array of GValues
1083 * @n_values: the length of the @columns and @values arrays
1085 * A variant of gtk_list_store_set_valist() which
1086 * takes the columns and values as two arrays, instead of
1087 * varargs. This function is mainly intended for
1088 * language-bindings and in case the number of columns to
1089 * change is not known until run-time.
1092 * Rename to: gtk_list_store_set
1095 gtk_list_store_set_valuesv (GtkListStore *list_store,
1101 GtkListStorePrivate *priv;
1102 gboolean emit_signal = FALSE;
1103 gboolean maybe_need_sort = FALSE;
1105 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1106 g_return_if_fail (iter_is_valid (iter, list_store));
1108 priv = list_store->priv;
1110 gtk_list_store_set_vector_internal (list_store, iter,
1113 columns, values, n_values);
1115 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
1116 gtk_list_store_sort_iter_changed (list_store, iter, priv->sort_column_id);
1122 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1123 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
1124 gtk_tree_path_free (path);
1129 * gtk_list_store_set_valist:
1130 * @list_store: A #GtkListStore
1131 * @iter: A valid #GtkTreeIter for the row being modified
1132 * @var_args: va_list of column/value pairs
1134 * See gtk_list_store_set(); this version takes a va_list for use by language
1139 gtk_list_store_set_valist (GtkListStore *list_store,
1143 GtkListStorePrivate *priv;
1144 gboolean emit_signal = FALSE;
1145 gboolean maybe_need_sort = FALSE;
1147 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1148 g_return_if_fail (iter_is_valid (iter, list_store));
1150 priv = list_store->priv;
1152 gtk_list_store_set_valist_internal (list_store, iter,
1157 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
1158 gtk_list_store_sort_iter_changed (list_store, iter, priv->sort_column_id);
1164 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1165 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
1166 gtk_tree_path_free (path);
1171 * gtk_list_store_set:
1172 * @list_store: a #GtkListStore
1173 * @iter: row iterator
1174 * @...: pairs of column number and value, terminated with -1
1176 * Sets the value of one or more cells in the row referenced by @iter.
1177 * The variable argument list should contain integer column numbers,
1178 * each column number followed by the value to be set.
1179 * The list is terminated by a -1. For example, to set column 0 with type
1180 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
1181 * 0, "Foo", -1)</literal>.
1183 * The value will be referenced by the store if it is a %G_TYPE_OBJECT, and it
1184 * will be copied if it is a %G_TYPE_STRING or %G_TYPE_BOXED.
1187 gtk_list_store_set (GtkListStore *list_store,
1193 va_start (var_args, iter);
1194 gtk_list_store_set_valist (list_store, iter, var_args);
1199 * gtk_list_store_remove:
1200 * @list_store: A #GtkListStore
1201 * @iter: A valid #GtkTreeIter
1203 * Removes the given row from the list store. After being removed,
1204 * @iter is set to be the next valid row, or invalidated if it pointed
1205 * to the last row in @list_store.
1207 * Return value: %TRUE if @iter is valid, %FALSE if not.
1210 gtk_list_store_remove (GtkListStore *list_store,
1213 GtkListStorePrivate *priv;
1215 GSequenceIter *ptr, *next;
1217 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1218 g_return_val_if_fail (iter_is_valid (iter, list_store), FALSE);
1220 priv = list_store->priv;
1222 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1224 ptr = iter->user_data;
1225 next = g_sequence_iter_next (ptr);
1227 _gtk_tree_data_list_free (g_sequence_get (ptr), priv->column_headers);
1228 g_sequence_remove (iter->user_data);
1232 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
1233 gtk_tree_path_free (path);
1235 if (g_sequence_iter_is_end (next))
1242 iter->stamp = priv->stamp;
1243 iter->user_data = next;
1249 * gtk_list_store_insert:
1250 * @list_store: A #GtkListStore
1251 * @iter: (out): An unset #GtkTreeIter to set to the new row
1252 * @position: position to insert the new row, or -1 for last
1254 * Creates a new row at @position. @iter will be changed to point to this new
1255 * row. If @position is -1 or is larger than the number of rows on the list,
1256 * then the new row will be appended to the list. The row will be empty after
1257 * this function is called. To fill in values, you need to call
1258 * gtk_list_store_set() or gtk_list_store_set_value().
1262 gtk_list_store_insert (GtkListStore *list_store,
1266 GtkListStorePrivate *priv;
1272 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1273 g_return_if_fail (iter != NULL);
1275 priv = list_store->priv;
1277 priv->columns_dirty = TRUE;
1281 length = g_sequence_get_length (seq);
1282 if (position > length || position < 0)
1285 ptr = g_sequence_get_iter_at_pos (seq, position);
1286 ptr = g_sequence_insert_before (ptr, NULL);
1288 iter->stamp = priv->stamp;
1289 iter->user_data = ptr;
1291 g_assert (iter_is_valid (iter, list_store));
1295 path = gtk_tree_path_new ();
1296 gtk_tree_path_append_index (path, position);
1297 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1298 gtk_tree_path_free (path);
1302 * gtk_list_store_insert_before:
1303 * @list_store: A #GtkListStore
1304 * @iter: (out): An unset #GtkTreeIter to set to the new row
1305 * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1307 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will
1308 * be appended to the end of the list. @iter will be changed to point to this
1309 * new row. The row will be empty after this function is called. To fill in
1310 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1314 gtk_list_store_insert_before (GtkListStore *list_store,
1316 GtkTreeIter *sibling)
1318 GtkListStorePrivate *priv;
1319 GSequenceIter *after;
1321 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1322 g_return_if_fail (iter != NULL);
1324 priv = list_store->priv;
1327 g_return_if_fail (iter_is_valid (sibling, list_store));
1330 after = g_sequence_get_end_iter (priv->seq);
1332 after = sibling->user_data;
1334 gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1338 * gtk_list_store_insert_after:
1339 * @list_store: A #GtkListStore
1340 * @iter: (out): An unset #GtkTreeIter to set to the new row
1341 * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1343 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1344 * prepended to the beginning of the list. @iter will be changed to point to
1345 * this new row. The row will be empty after this function is called. To fill
1346 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1350 gtk_list_store_insert_after (GtkListStore *list_store,
1352 GtkTreeIter *sibling)
1354 GtkListStorePrivate *priv;
1355 GSequenceIter *after;
1357 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1358 g_return_if_fail (iter != NULL);
1360 priv = list_store->priv;
1363 g_return_if_fail (iter_is_valid (sibling, list_store));
1366 after = g_sequence_get_begin_iter (priv->seq);
1368 after = g_sequence_iter_next (sibling->user_data);
1370 gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1374 * gtk_list_store_prepend:
1375 * @list_store: A #GtkListStore
1376 * @iter: (out): An unset #GtkTreeIter to set to the prepend row
1378 * Prepends a new row to @list_store. @iter will be changed to point to this new
1379 * row. The row will be empty after this function is called. To fill in
1380 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1384 gtk_list_store_prepend (GtkListStore *list_store,
1387 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1388 g_return_if_fail (iter != NULL);
1390 gtk_list_store_insert (list_store, iter, 0);
1394 * gtk_list_store_append:
1395 * @list_store: A #GtkListStore
1396 * @iter: (out): An unset #GtkTreeIter to set to the appended row
1398 * Appends a new row to @list_store. @iter will be changed to point to this new
1399 * row. The row will be empty after this function is called. To fill in
1400 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1404 gtk_list_store_append (GtkListStore *list_store,
1407 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1408 g_return_if_fail (iter != NULL);
1410 gtk_list_store_insert (list_store, iter, -1);
1414 gtk_list_store_increment_stamp (GtkListStore *list_store)
1416 GtkListStorePrivate *priv = list_store->priv;
1422 while (priv->stamp == 0);
1426 * gtk_list_store_clear:
1427 * @list_store: a #GtkListStore.
1429 * Removes all rows from the list store.
1433 gtk_list_store_clear (GtkListStore *list_store)
1435 GtkListStorePrivate *priv;
1438 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1440 priv = list_store->priv;
1442 while (g_sequence_get_length (priv->seq) > 0)
1444 iter.stamp = priv->stamp;
1445 iter.user_data = g_sequence_get_begin_iter (priv->seq);
1446 gtk_list_store_remove (list_store, &iter);
1449 gtk_list_store_increment_stamp (list_store);
1453 * gtk_list_store_iter_is_valid:
1454 * @list_store: A #GtkListStore.
1455 * @iter: A #GtkTreeIter.
1457 * <warning>This function is slow. Only use it for debugging and/or testing
1458 * purposes.</warning>
1460 * Checks if the given iter is a valid iter for this #GtkListStore.
1462 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1467 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1470 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1471 g_return_val_if_fail (iter != NULL, FALSE);
1473 return iter_is_valid (iter, list_store);
1476 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
1483 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1488 if (gtk_list_store_get_iter (GTK_TREE_MODEL (drag_source),
1492 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1499 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1501 GtkSelectionData *selection_data)
1503 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1504 * target, because the default handler does it for us, but
1505 * we do anyway for the convenience of someone maybe overriding the
1509 if (gtk_tree_set_row_drag_data (selection_data,
1510 GTK_TREE_MODEL (drag_source),
1517 /* FIXME handle text targets at least. */
1524 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1526 GtkSelectionData *selection_data)
1528 GtkTreeModel *tree_model = GTK_TREE_MODEL (drag_dest);
1529 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
1530 GtkListStorePrivate *priv = list_store->priv;
1531 GtkTreeModel *src_model = NULL;
1532 GtkTreePath *src_path = NULL;
1533 gboolean retval = FALSE;
1535 if (gtk_tree_get_row_drag_data (selection_data,
1538 src_model == tree_model)
1540 /* Copy the given row to a new position */
1541 GtkTreeIter src_iter;
1542 GtkTreeIter dest_iter;
1545 if (!gtk_list_store_get_iter (src_model,
1552 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1553 prev = gtk_tree_path_copy (dest);
1555 if (!gtk_tree_path_prev (prev))
1557 /* dest was the first spot in the list; which means we are supposed
1560 gtk_list_store_prepend (list_store, &dest_iter);
1566 if (gtk_list_store_get_iter (tree_model, &dest_iter, prev))
1568 GtkTreeIter tmp_iter = dest_iter;
1570 gtk_list_store_insert_after (list_store, &dest_iter, &tmp_iter);
1576 gtk_tree_path_free (prev);
1578 /* If we succeeded in creating dest_iter, copy data from src
1582 GtkTreeDataList *dl = g_sequence_get (src_iter.user_data);
1583 GtkTreeDataList *copy_head = NULL;
1584 GtkTreeDataList *copy_prev = NULL;
1585 GtkTreeDataList *copy_iter = NULL;
1592 copy_iter = _gtk_tree_data_list_node_copy (dl,
1593 priv->column_headers[col]);
1595 if (copy_head == NULL)
1596 copy_head = copy_iter;
1599 copy_prev->next = copy_iter;
1601 copy_prev = copy_iter;
1607 dest_iter.stamp = priv->stamp;
1608 g_sequence_set (dest_iter.user_data, copy_head);
1610 path = gtk_list_store_get_path (tree_model, &dest_iter);
1611 gtk_tree_model_row_changed (tree_model, path, &dest_iter);
1612 gtk_tree_path_free (path);
1617 /* FIXME maybe add some data targets eventually, or handle text
1618 * targets in the simple case.
1625 gtk_tree_path_free (src_path);
1631 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1632 GtkTreePath *dest_path,
1633 GtkSelectionData *selection_data)
1636 GtkTreeModel *src_model = NULL;
1637 GtkTreePath *src_path = NULL;
1638 gboolean retval = FALSE;
1640 /* don't accept drops if the list has been sorted */
1641 if (GTK_LIST_STORE_IS_SORTED (drag_dest))
1644 if (!gtk_tree_get_row_drag_data (selection_data,
1649 if (src_model != GTK_TREE_MODEL (drag_dest))
1652 if (gtk_tree_path_get_depth (dest_path) != 1)
1655 /* can drop before any existing node, or before one past any existing. */
1657 indices = gtk_tree_path_get_indices (dest_path);
1659 if (indices[0] <= g_sequence_get_length (GTK_LIST_STORE (drag_dest)->priv->seq))
1664 gtk_tree_path_free (src_path);
1669 /* Sorting and reordering */
1673 gtk_list_store_reorder_func (GSequenceIter *a,
1677 GHashTable *new_positions = user_data;
1678 gint apos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, a));
1679 gint bpos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, b));
1689 * gtk_list_store_reorder:
1690 * @store: A #GtkListStore.
1691 * @new_order: (array zero-terminated=1): an array of integers mapping the new
1692 * position of each child to its old position before the re-ordering,
1693 * i.e. @new_order<literal>[newpos] = oldpos</literal>. It must have
1694 * exactly as many items as the list store's length.
1696 * Reorders @store to follow the order indicated by @new_order. Note that
1697 * this function only works with unsorted stores.
1702 gtk_list_store_reorder (GtkListStore *store,
1705 GtkListStorePrivate *priv;
1708 GHashTable *new_positions;
1712 g_return_if_fail (GTK_IS_LIST_STORE (store));
1713 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1714 g_return_if_fail (new_order != NULL);
1718 order = g_new (gint, g_sequence_get_length (priv->seq));
1719 for (i = 0; i < g_sequence_get_length (priv->seq); i++)
1720 order[new_order[i]] = i;
1722 new_positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1724 ptr = g_sequence_get_begin_iter (priv->seq);
1726 while (!g_sequence_iter_is_end (ptr))
1728 g_hash_table_insert (new_positions, ptr, GINT_TO_POINTER (order[i++]));
1730 ptr = g_sequence_iter_next (ptr);
1734 g_sequence_sort_iter (priv->seq, gtk_list_store_reorder_func, new_positions);
1736 g_hash_table_destroy (new_positions);
1739 path = gtk_tree_path_new ();
1740 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1741 path, NULL, new_order);
1742 gtk_tree_path_free (path);
1746 save_positions (GSequence *seq)
1748 GHashTable *positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1751 ptr = g_sequence_get_begin_iter (seq);
1752 while (!g_sequence_iter_is_end (ptr))
1754 g_hash_table_insert (positions, ptr,
1755 GINT_TO_POINTER (g_sequence_iter_get_position (ptr)));
1756 ptr = g_sequence_iter_next (ptr);
1763 generate_order (GSequence *seq,
1764 GHashTable *old_positions)
1767 int *order = g_new (int, g_sequence_get_length (seq));
1771 ptr = g_sequence_get_begin_iter (seq);
1772 while (!g_sequence_iter_is_end (ptr))
1774 int old_pos = GPOINTER_TO_INT (g_hash_table_lookup (old_positions, ptr));
1775 order[i++] = old_pos;
1776 ptr = g_sequence_iter_next (ptr);
1779 g_hash_table_destroy (old_positions);
1785 * gtk_list_store_swap:
1786 * @store: A #GtkListStore.
1787 * @a: A #GtkTreeIter.
1788 * @b: Another #GtkTreeIter.
1790 * Swaps @a and @b in @store. Note that this function only works with
1796 gtk_list_store_swap (GtkListStore *store,
1800 GtkListStorePrivate *priv;
1801 GHashTable *old_positions;
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 (iter_is_valid (a, store));
1808 g_return_if_fail (iter_is_valid (b, store));
1812 if (a->user_data == b->user_data)
1815 old_positions = save_positions (priv->seq);
1817 g_sequence_swap (a->user_data, b->user_data);
1819 order = generate_order (priv->seq, old_positions);
1820 path = gtk_tree_path_new ();
1822 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1825 gtk_tree_path_free (path);
1830 gtk_list_store_move_to (GtkListStore *store,
1834 GtkListStorePrivate *priv = store->priv;
1835 GHashTable *old_positions;
1839 old_positions = save_positions (priv->seq);
1841 g_sequence_move (iter->user_data, g_sequence_get_iter_at_pos (priv->seq, new_pos));
1843 order = generate_order (priv->seq, old_positions);
1845 path = gtk_tree_path_new ();
1846 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1848 gtk_tree_path_free (path);
1853 * gtk_list_store_move_before:
1854 * @store: A #GtkListStore.
1855 * @iter: A #GtkTreeIter.
1856 * @position: (allow-none): A #GtkTreeIter, or %NULL.
1858 * Moves @iter in @store to the position before @position. Note that this
1859 * function only works with unsorted stores. If @position is %NULL, @iter
1860 * will be moved to the end of the list.
1865 gtk_list_store_move_before (GtkListStore *store,
1867 GtkTreeIter *position)
1871 g_return_if_fail (GTK_IS_LIST_STORE (store));
1872 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1873 g_return_if_fail (iter_is_valid (iter, store));
1875 g_return_if_fail (iter_is_valid (position, store));
1878 pos = g_sequence_iter_get_position (position->user_data);
1882 gtk_list_store_move_to (store, iter, pos);
1886 * gtk_list_store_move_after:
1887 * @store: A #GtkListStore.
1888 * @iter: A #GtkTreeIter.
1889 * @position: (allow-none): A #GtkTreeIter or %NULL.
1891 * Moves @iter in @store to the position after @position. Note that this
1892 * function only works with unsorted stores. If @position is %NULL, @iter
1893 * will be moved to the start of the list.
1898 gtk_list_store_move_after (GtkListStore *store,
1900 GtkTreeIter *position)
1904 g_return_if_fail (GTK_IS_LIST_STORE (store));
1905 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1906 g_return_if_fail (iter_is_valid (iter, store));
1908 g_return_if_fail (iter_is_valid (position, store));
1911 pos = g_sequence_iter_get_position (position->user_data) + 1;
1915 gtk_list_store_move_to (store, iter, pos);
1920 gtk_list_store_compare_func (GSequenceIter *a,
1924 GtkListStore *list_store = user_data;
1925 GtkListStorePrivate *priv = list_store->priv;
1929 GtkTreeIterCompareFunc func;
1932 if (priv->sort_column_id != -1)
1934 GtkTreeDataSortHeader *header;
1936 header = _gtk_tree_data_list_get_header (priv->sort_list,
1937 priv->sort_column_id);
1938 g_return_val_if_fail (header != NULL, 0);
1939 g_return_val_if_fail (header->func != NULL, 0);
1941 func = header->func;
1942 data = header->data;
1946 g_return_val_if_fail (priv->default_sort_func != NULL, 0);
1947 func = priv->default_sort_func;
1948 data = priv->default_sort_data;
1951 iter_a.stamp = priv->stamp;
1952 iter_a.user_data = (gpointer)a;
1953 iter_b.stamp = priv->stamp;
1954 iter_b.user_data = (gpointer)b;
1956 g_assert (iter_is_valid (&iter_a, list_store));
1957 g_assert (iter_is_valid (&iter_b, list_store));
1959 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1961 if (priv->order == GTK_SORT_DESCENDING)
1965 else if (retval < 0)
1973 gtk_list_store_sort (GtkListStore *list_store)
1975 GtkListStorePrivate *priv = list_store->priv;
1978 GHashTable *old_positions;
1980 if (!GTK_LIST_STORE_IS_SORTED (list_store) ||
1981 g_sequence_get_length (priv->seq) <= 1)
1984 old_positions = save_positions (priv->seq);
1986 g_sequence_sort_iter (priv->seq, gtk_list_store_compare_func, list_store);
1988 /* Let the world know about our new order */
1989 new_order = generate_order (priv->seq, old_positions);
1991 path = gtk_tree_path_new ();
1992 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1993 path, NULL, new_order);
1994 gtk_tree_path_free (path);
1999 iter_is_sorted (GtkListStore *list_store,
2004 if (!g_sequence_iter_is_begin (iter->user_data))
2006 cmp = g_sequence_iter_prev (iter->user_data);
2007 if (gtk_list_store_compare_func (cmp, iter->user_data, list_store) > 0)
2011 cmp = g_sequence_iter_next (iter->user_data);
2012 if (!g_sequence_iter_is_end (cmp))
2014 if (gtk_list_store_compare_func (iter->user_data, cmp, list_store) > 0)
2022 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
2027 GtkListStorePrivate *priv = list_store->priv;
2030 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2031 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
2032 gtk_tree_path_free (path);
2034 if (!iter_is_sorted (list_store, iter))
2036 GHashTable *old_positions;
2039 old_positions = save_positions (priv->seq);
2040 g_sequence_sort_changed_iter (iter->user_data,
2041 gtk_list_store_compare_func,
2043 order = generate_order (priv->seq, old_positions);
2044 path = gtk_tree_path_new ();
2045 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
2047 gtk_tree_path_free (path);
2053 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
2054 gint *sort_column_id,
2057 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2058 GtkListStorePrivate *priv = list_store->priv;
2061 * sort_column_id = priv->sort_column_id;
2063 * order = priv->order;
2065 if (priv->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
2066 priv->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
2073 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
2074 gint sort_column_id,
2077 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2078 GtkListStorePrivate *priv = list_store->priv;
2080 if ((priv->sort_column_id == sort_column_id) &&
2081 (priv->order == order))
2084 if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
2086 if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2088 GtkTreeDataSortHeader *header = NULL;
2090 header = _gtk_tree_data_list_get_header (priv->sort_list,
2093 /* We want to make sure that we have a function */
2094 g_return_if_fail (header != NULL);
2095 g_return_if_fail (header->func != NULL);
2099 g_return_if_fail (priv->default_sort_func != NULL);
2104 priv->sort_column_id = sort_column_id;
2105 priv->order = order;
2107 gtk_tree_sortable_sort_column_changed (sortable);
2109 gtk_list_store_sort (list_store);
2113 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
2114 gint sort_column_id,
2115 GtkTreeIterCompareFunc func,
2117 GDestroyNotify destroy)
2119 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2120 GtkListStorePrivate *priv = list_store->priv;
2122 priv->sort_list = _gtk_tree_data_list_set_header (priv->sort_list,
2124 func, data, destroy);
2126 if (priv->sort_column_id == sort_column_id)
2127 gtk_list_store_sort (list_store);
2131 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
2132 GtkTreeIterCompareFunc func,
2134 GDestroyNotify destroy)
2136 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2137 GtkListStorePrivate *priv = list_store->priv;
2139 if (priv->default_sort_destroy)
2141 GDestroyNotify d = priv->default_sort_destroy;
2143 priv->default_sort_destroy = NULL;
2144 d (priv->default_sort_data);
2147 priv->default_sort_func = func;
2148 priv->default_sort_data = data;
2149 priv->default_sort_destroy = destroy;
2151 if (priv->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2152 gtk_list_store_sort (list_store);
2156 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
2158 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2159 GtkListStorePrivate *priv = list_store->priv;
2161 return (priv->default_sort_func != NULL);
2166 * gtk_list_store_insert_with_values:
2167 * @list_store: A #GtkListStore
2168 * @iter: (out) (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL
2169 * @position: position to insert the new row, or -1 to append after existing
2171 * @...: pairs of column number and value, terminated with -1
2173 * Creates a new row at @position. @iter will be changed to point to this new
2174 * row. If @position is -1, or larger than the number of rows in the list, then
2175 * the new row will be appended to the list. The row will be filled with the
2176 * values given to this function.
2179 * <literal>gtk_list_store_insert_with_values (list_store, iter, position...)</literal>
2180 * has the same effect as calling
2182 * gtk_list_store_insert (list_store, iter, position);
2183 * gtk_list_store_set (list_store, iter, ...);
2185 * with the difference that the former will only emit a row_inserted signal,
2186 * while the latter will emit row_inserted, row_changed and, if the list store
2187 * is sorted, rows_reordered. Since emitting the rows_reordered signal
2188 * repeatedly can affect the performance of the program,
2189 * gtk_list_store_insert_with_values() should generally be preferred when
2190 * inserting rows in a sorted list store.
2195 gtk_list_store_insert_with_values (GtkListStore *list_store,
2200 GtkListStorePrivate *priv;
2204 GtkTreeIter tmp_iter;
2206 gboolean changed = FALSE;
2207 gboolean maybe_need_sort = FALSE;
2210 /* FIXME: refactor to reduce overlap with gtk_list_store_set() */
2211 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2213 priv = list_store->priv;
2218 priv->columns_dirty = TRUE;
2222 length = g_sequence_get_length (seq);
2223 if (position > length || position < 0)
2226 ptr = g_sequence_get_iter_at_pos (seq, position);
2227 ptr = g_sequence_insert_before (ptr, NULL);
2229 iter->stamp = priv->stamp;
2230 iter->user_data = ptr;
2232 g_assert (iter_is_valid (iter, list_store));
2236 va_start (var_args, position);
2237 gtk_list_store_set_valist_internal (list_store, iter,
2238 &changed, &maybe_need_sort,
2242 /* Don't emit rows_reordered here */
2243 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2244 g_sequence_sort_changed_iter (iter->user_data,
2245 gtk_list_store_compare_func,
2248 /* Just emit row_inserted */
2249 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2250 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2251 gtk_tree_path_free (path);
2256 * gtk_list_store_insert_with_valuesv:
2257 * @list_store: A #GtkListStore
2258 * @iter: (out) (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL.
2259 * @position: position to insert the new row, or -1 for last
2260 * @columns: (array length=n_values): an array of column numbers
2261 * @values: (array length=n_values): an array of GValues
2262 * @n_values: the length of the @columns and @values arrays
2264 * A variant of gtk_list_store_insert_with_values() which
2265 * takes the columns and values as two arrays, instead of
2266 * varargs. This function is mainly intended for
2267 * language-bindings.
2272 gtk_list_store_insert_with_valuesv (GtkListStore *list_store,
2279 GtkListStorePrivate *priv;
2283 GtkTreeIter tmp_iter;
2285 gboolean changed = FALSE;
2286 gboolean maybe_need_sort = FALSE;
2288 /* FIXME refactor to reduce overlap with
2289 * gtk_list_store_insert_with_values()
2291 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2293 priv = list_store->priv;
2298 priv->columns_dirty = TRUE;
2302 length = g_sequence_get_length (seq);
2303 if (position > length || position < 0)
2306 ptr = g_sequence_get_iter_at_pos (seq, position);
2307 ptr = g_sequence_insert_before (ptr, NULL);
2309 iter->stamp = priv->stamp;
2310 iter->user_data = ptr;
2312 g_assert (iter_is_valid (iter, list_store));
2316 gtk_list_store_set_vector_internal (list_store, iter,
2317 &changed, &maybe_need_sort,
2318 columns, values, n_values);
2320 /* Don't emit rows_reordered here */
2321 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2322 g_sequence_sort_changed_iter (iter->user_data,
2323 gtk_list_store_compare_func,
2326 /* Just emit row_inserted */
2327 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2328 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2329 gtk_tree_path_free (path);
2332 /* GtkBuildable custom tag implementation
2335 * <column type="..."/>
2336 * <column type="..."/>
2340 gboolean translatable;
2346 GtkBuilder *builder;
2348 GSList *column_type_names;
2349 GType *column_types;
2358 const gchar *domain;
2362 list_store_start_element (GMarkupParseContext *context,
2363 const gchar *element_name,
2364 const gchar **names,
2365 const gchar **values,
2370 SubParserData *data = (SubParserData*)user_data;
2372 if (strcmp (element_name, "col") == 0)
2375 gchar *context = NULL;
2376 gboolean translatable = FALSE;
2379 if (data->row_column >= data->n_columns)
2381 g_set_error (error, data->error_quark, 0,
2382 "Too many columns, maximum is %d\n", data->n_columns - 1);
2386 for (i = 0; names[i]; i++)
2387 if (strcmp (names[i], "id") == 0)
2390 id = atoi (values[i]);
2393 g_set_error (error, data->error_quark, 0,
2394 "the id tag %s could not be converted to an integer",
2398 if (id < 0 || id >= data->n_columns)
2400 g_set_error (error, data->error_quark, 0,
2401 "id value %d out of range", id);
2405 else if (strcmp (names[i], "translatable") == 0)
2407 if (!_gtk_builder_boolean_from_string (values[i], &translatable,
2411 else if (strcmp (names[i], "comments") == 0)
2413 /* do nothing, comments are for translators */
2415 else if (strcmp (names[i], "context") == 0)
2417 context = g_strdup (values[i]);
2422 g_set_error (error, data->error_quark, 0,
2423 "<col> needs an id attribute");
2427 info = g_slice_new0 (ColInfo);
2428 info->translatable = translatable;
2429 info->context = context;
2432 data->colids[data->row_column] = id;
2433 data->columns[data->row_column] = info;
2435 data->is_data = TRUE;
2437 else if (strcmp (element_name, "row") == 0)
2439 else if (strcmp (element_name, "column") == 0)
2441 for (i = 0; names[i]; i++)
2442 if (strcmp (names[i], "type") == 0)
2443 data->column_type_names = g_slist_prepend (data->column_type_names,
2444 g_strdup (values[i]));
2446 else if (strcmp (element_name, "columns") == 0)
2448 else if (strcmp (element_name, "data") == 0)
2451 g_set_error (error, data->error_quark, 0,
2452 "Unknown start tag: %s", element_name);
2456 list_store_end_element (GMarkupParseContext *context,
2457 const gchar *element_name,
2461 SubParserData *data = (SubParserData*)user_data;
2463 g_assert (data->builder);
2465 if (strcmp (element_name, "row") == 0)
2470 gtk_list_store_insert_with_valuesv (GTK_LIST_STORE (data->object),
2476 for (i = 0; i < data->row_column; i++)
2478 ColInfo *info = data->columns[i];
2479 g_free (info->context);
2480 g_slice_free (ColInfo, info);
2481 data->columns[i] = NULL;
2482 g_value_unset (&data->values[i]);
2484 g_free (data->values);
2485 data->values = g_new0 (GValue, data->n_columns);
2487 data->row_column = 0;
2489 else if (strcmp (element_name, "columns") == 0)
2491 GType *column_types;
2496 data->column_type_names = g_slist_reverse (data->column_type_names);
2497 column_types = g_new0 (GType, g_slist_length (data->column_type_names));
2499 for (l = data->column_type_names, i = 0; l; l = l->next, i++)
2501 type = gtk_builder_get_type_from_name (data->builder, l->data);
2502 if (type == G_TYPE_INVALID)
2504 g_warning ("Unknown type %s specified in treemodel %s",
2505 (const gchar*)l->data,
2506 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
2509 column_types[i] = type;
2514 gtk_list_store_set_column_types (GTK_LIST_STORE (data->object), i,
2517 g_free (column_types);
2519 else if (strcmp (element_name, "col") == 0)
2520 data->is_data = FALSE;
2521 else if (strcmp (element_name, "data") == 0)
2523 else if (strcmp (element_name, "column") == 0)
2526 g_set_error (error, data->error_quark, 0,
2527 "Unknown end tag: %s", element_name);
2531 list_store_text (GMarkupParseContext *context,
2537 SubParserData *data = (SubParserData*)user_data;
2539 GError *tmp_error = NULL;
2546 i = data->row_column - 1;
2547 info = data->columns[i];
2549 string = g_strndup (text, text_len);
2550 if (info->translatable && text_len)
2554 /* FIXME: This will not use the domain set in the .ui file,
2555 * since the parser is not telling the builder about the domain.
2556 * However, it will work for gtk_builder_set_translation_domain() calls.
2558 translated = _gtk_builder_parser_translate (data->domain,
2562 string = translated;
2565 if (!gtk_builder_value_from_string_type (data->builder,
2566 data->column_types[info->id],
2574 "Could not convert '%s' to type %s: %s\n",
2575 text, g_type_name (data->column_types[info->id]),
2576 tmp_error->message);
2577 g_error_free (tmp_error);
2582 static const GMarkupParser list_store_parser =
2584 list_store_start_element,
2585 list_store_end_element,
2590 gtk_list_store_buildable_custom_tag_start (GtkBuildable *buildable,
2591 GtkBuilder *builder,
2593 const gchar *tagname,
2594 GMarkupParser *parser,
2597 SubParserData *parser_data;
2602 if (strcmp (tagname, "columns") == 0)
2605 parser_data = g_slice_new0 (SubParserData);
2606 parser_data->builder = builder;
2607 parser_data->object = G_OBJECT (buildable);
2608 parser_data->column_type_names = NULL;
2610 *parser = list_store_parser;
2611 *data = parser_data;
2614 else if (strcmp (tagname, "data") == 0)
2616 gint n_columns = gtk_list_store_get_n_columns (GTK_TREE_MODEL (buildable));
2618 g_error ("Cannot append data to an empty model");
2620 parser_data = g_slice_new0 (SubParserData);
2621 parser_data->builder = builder;
2622 parser_data->object = G_OBJECT (buildable);
2623 parser_data->values = g_new0 (GValue, n_columns);
2624 parser_data->colids = g_new0 (gint, n_columns);
2625 parser_data->columns = g_new0 (ColInfo*, n_columns);
2626 parser_data->column_types = GTK_LIST_STORE (buildable)->priv->column_headers;
2627 parser_data->n_columns = n_columns;
2628 parser_data->last_row = 0;
2629 parser_data->error_quark = g_quark_from_static_string ("GtkListStore");
2630 parser_data->domain = gtk_builder_get_translation_domain (builder);
2632 *parser = list_store_parser;
2633 *data = parser_data;
2637 g_warning ("Unknown custom list store tag: %s", tagname);
2643 gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
2644 GtkBuilder *builder,
2646 const gchar *tagname,
2649 SubParserData *sub = (SubParserData*)data;
2651 if (strcmp (tagname, "columns") == 0)
2653 g_slist_free (sub->column_type_names);
2654 g_slice_free (SubParserData, sub);
2656 else if (strcmp (tagname, "data") == 0)
2659 for (i = 0; i < sub->n_columns; i++)
2661 ColInfo *info = sub->columns[i];
2664 g_free (info->context);
2665 g_slice_free (ColInfo, info);
2668 g_free (sub->colids);
2669 g_free (sub->columns);
2670 g_free (sub->values);
2671 g_slice_free (SubParserData, sub);
2674 g_warning ("Unknown custom list store tag: %s", tagname);