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_tree_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_compatible (G_VALUE_TYPE (value), priv->column_headers[column]) &&
846 g_value_type_compatible (priv->column_headers[column], G_VALUE_TYPE (value))))
848 g_warning ("%s: Unable to convert from %s to %s\n",
850 g_type_name (G_VALUE_TYPE (value)),
851 g_type_name (priv->column_headers[column]));
854 if (!g_value_transform (value, &real_value))
856 g_warning ("%s: Unable to make conversion from %s to %s\n",
858 g_type_name (G_VALUE_TYPE (value)),
859 g_type_name (priv->column_headers[column]));
860 g_value_unset (&real_value);
866 prev = list = g_sequence_get (iter->user_data);
873 _gtk_tree_data_list_value_to_node (list, &real_value);
875 _gtk_tree_data_list_value_to_node (list, value);
878 g_value_unset (&real_value);
879 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
880 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
889 if (g_sequence_get (iter->user_data) == NULL)
891 list = _gtk_tree_data_list_alloc();
892 g_sequence_set (iter->user_data, list);
897 list = prev->next = _gtk_tree_data_list_alloc ();
903 list->next = _gtk_tree_data_list_alloc ();
910 _gtk_tree_data_list_value_to_node (list, &real_value);
912 _gtk_tree_data_list_value_to_node (list, value);
916 g_value_unset (&real_value);
918 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
919 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
926 * gtk_list_store_set_value:
927 * @list_store: A #GtkListStore
928 * @iter: A valid #GtkTreeIter for the row being modified
929 * @column: column number to modify
930 * @value: new value for the cell
932 * Sets the data in the cell specified by @iter and @column.
933 * The type of @value must be convertible to the type of the
938 gtk_list_store_set_value (GtkListStore *list_store,
943 GtkListStorePrivate *priv;
945 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
946 g_return_if_fail (iter_is_valid (iter, list_store));
947 g_return_if_fail (G_IS_VALUE (value));
948 priv = list_store->priv;
949 g_return_if_fail (column >= 0 && column < priv->n_columns);
951 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
955 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
956 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
957 gtk_tree_path_free (path);
961 static GtkTreeIterCompareFunc
962 gtk_list_store_get_compare_func (GtkListStore *list_store)
964 GtkListStorePrivate *priv = list_store->priv;
965 GtkTreeIterCompareFunc func = NULL;
967 if (GTK_LIST_STORE_IS_SORTED (list_store))
969 if (priv->sort_column_id != -1)
971 GtkTreeDataSortHeader *header;
972 header = _gtk_tree_data_list_get_header (priv->sort_list,
973 priv->sort_column_id);
974 g_return_val_if_fail (header != NULL, NULL);
975 g_return_val_if_fail (header->func != NULL, NULL);
980 func = priv->default_sort_func;
988 gtk_list_store_set_vector_internal (GtkListStore *list_store,
990 gboolean *emit_signal,
991 gboolean *maybe_need_sort,
996 GtkListStorePrivate *priv = list_store->priv;
998 GtkTreeIterCompareFunc func = NULL;
1000 func = gtk_list_store_get_compare_func (list_store);
1001 if (func != _gtk_tree_data_list_compare_func)
1002 *maybe_need_sort = TRUE;
1004 for (i = 0; i < n_values; i++)
1006 *emit_signal = gtk_list_store_real_set_value (list_store,
1010 FALSE) || *emit_signal;
1012 if (func == _gtk_tree_data_list_compare_func &&
1013 columns[i] == priv->sort_column_id)
1014 *maybe_need_sort = TRUE;
1019 gtk_list_store_set_valist_internal (GtkListStore *list_store,
1021 gboolean *emit_signal,
1022 gboolean *maybe_need_sort,
1025 GtkListStorePrivate *priv = list_store->priv;
1027 GtkTreeIterCompareFunc func = NULL;
1029 column = va_arg (var_args, gint);
1031 func = gtk_list_store_get_compare_func (list_store);
1032 if (func != _gtk_tree_data_list_compare_func)
1033 *maybe_need_sort = TRUE;
1035 while (column != -1)
1037 GValue value = G_VALUE_INIT;
1038 gchar *error = NULL;
1040 if (column < 0 || column >= priv->n_columns)
1042 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
1046 G_VALUE_COLLECT_INIT (&value, priv->column_headers[column],
1047 var_args, 0, &error);
1050 g_warning ("%s: %s", G_STRLOC, error);
1053 /* we purposely leak the value here, it might not be
1054 * in a sane state if an error condition occoured
1059 /* FIXME: instead of calling this n times, refactor with above */
1060 *emit_signal = gtk_list_store_real_set_value (list_store,
1064 FALSE) || *emit_signal;
1066 if (func == _gtk_tree_data_list_compare_func &&
1067 column == priv->sort_column_id)
1068 *maybe_need_sort = TRUE;
1070 g_value_unset (&value);
1072 column = va_arg (var_args, gint);
1077 * gtk_list_store_set_valuesv:
1078 * @list_store: A #GtkListStore
1079 * @iter: A valid #GtkTreeIter for the row being modified
1080 * @columns: (array length=n_values): an array of column numbers
1081 * @values: (array length=n_values): an array of GValues
1082 * @n_values: the length of the @columns and @values arrays
1084 * A variant of gtk_list_store_set_valist() which
1085 * takes the columns and values as two arrays, instead of
1086 * varargs. This function is mainly intended for
1087 * language-bindings and in case the number of columns to
1088 * change is not known until run-time.
1091 * Rename to: gtk_list_store_set
1094 gtk_list_store_set_valuesv (GtkListStore *list_store,
1100 GtkListStorePrivate *priv;
1101 gboolean emit_signal = FALSE;
1102 gboolean maybe_need_sort = FALSE;
1104 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1105 g_return_if_fail (iter_is_valid (iter, list_store));
1107 priv = list_store->priv;
1109 gtk_list_store_set_vector_internal (list_store, iter,
1112 columns, values, n_values);
1114 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
1115 gtk_list_store_sort_iter_changed (list_store, iter, priv->sort_column_id);
1121 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1122 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
1123 gtk_tree_path_free (path);
1128 * gtk_list_store_set_valist:
1129 * @list_store: A #GtkListStore
1130 * @iter: A valid #GtkTreeIter for the row being modified
1131 * @var_args: va_list of column/value pairs
1133 * See gtk_list_store_set(); this version takes a va_list for use by language
1138 gtk_list_store_set_valist (GtkListStore *list_store,
1142 GtkListStorePrivate *priv;
1143 gboolean emit_signal = FALSE;
1144 gboolean maybe_need_sort = FALSE;
1146 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1147 g_return_if_fail (iter_is_valid (iter, list_store));
1149 priv = list_store->priv;
1151 gtk_list_store_set_valist_internal (list_store, iter,
1156 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
1157 gtk_list_store_sort_iter_changed (list_store, iter, priv->sort_column_id);
1163 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1164 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
1165 gtk_tree_path_free (path);
1170 * gtk_list_store_set:
1171 * @list_store: a #GtkListStore
1172 * @iter: row iterator
1173 * @...: pairs of column number and value, terminated with -1
1175 * Sets the value of one or more cells in the row referenced by @iter.
1176 * The variable argument list should contain integer column numbers,
1177 * each column number followed by the value to be set.
1178 * The list is terminated by a -1. For example, to set column 0 with type
1179 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
1180 * 0, "Foo", -1)</literal>.
1182 * The value will be referenced by the store if it is a %G_TYPE_OBJECT, and it
1183 * will be copied if it is a %G_TYPE_STRING or %G_TYPE_BOXED.
1186 gtk_list_store_set (GtkListStore *list_store,
1192 va_start (var_args, iter);
1193 gtk_list_store_set_valist (list_store, iter, var_args);
1198 * gtk_list_store_remove:
1199 * @list_store: A #GtkListStore
1200 * @iter: A valid #GtkTreeIter
1202 * Removes the given row from the list store. After being removed,
1203 * @iter is set to be the next valid row, or invalidated if it pointed
1204 * to the last row in @list_store.
1206 * Return value: %TRUE if @iter is valid, %FALSE if not.
1209 gtk_list_store_remove (GtkListStore *list_store,
1212 GtkListStorePrivate *priv;
1214 GSequenceIter *ptr, *next;
1216 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1217 g_return_val_if_fail (iter_is_valid (iter, list_store), FALSE);
1219 priv = list_store->priv;
1221 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1223 ptr = iter->user_data;
1224 next = g_sequence_iter_next (ptr);
1226 _gtk_tree_data_list_free (g_sequence_get (ptr), priv->column_headers);
1227 g_sequence_remove (iter->user_data);
1231 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
1232 gtk_tree_path_free (path);
1234 if (g_sequence_iter_is_end (next))
1241 iter->stamp = priv->stamp;
1242 iter->user_data = next;
1248 * gtk_list_store_insert:
1249 * @list_store: A #GtkListStore
1250 * @iter: (out): An unset #GtkTreeIter to set to the new row
1251 * @position: position to insert the new row
1253 * Creates a new row at @position. @iter will be changed to point to this new
1254 * row. If @position is larger than the number of rows on the list, then the
1255 * new row will be appended to the list. The row will be empty after this
1256 * function is called. To fill in values, you need to call
1257 * gtk_list_store_set() or gtk_list_store_set_value().
1261 gtk_list_store_insert (GtkListStore *list_store,
1265 GtkListStorePrivate *priv;
1271 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1272 g_return_if_fail (iter != NULL);
1273 g_return_if_fail (position >= 0);
1275 priv = list_store->priv;
1277 priv->columns_dirty = TRUE;
1281 length = g_sequence_get_length (seq);
1282 if (position > length)
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 GtkListStorePrivate *priv;
1409 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1410 g_return_if_fail (iter != NULL);
1412 priv = list_store->priv;
1414 gtk_list_store_insert (list_store, iter, g_sequence_get_length (priv->seq));
1418 gtk_list_store_increment_stamp (GtkListStore *list_store)
1420 GtkListStorePrivate *priv = list_store->priv;
1426 while (priv->stamp == 0);
1430 * gtk_list_store_clear:
1431 * @list_store: a #GtkListStore.
1433 * Removes all rows from the list store.
1437 gtk_list_store_clear (GtkListStore *list_store)
1439 GtkListStorePrivate *priv;
1442 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1444 priv = list_store->priv;
1446 while (g_sequence_get_length (priv->seq) > 0)
1448 iter.stamp = priv->stamp;
1449 iter.user_data = g_sequence_get_begin_iter (priv->seq);
1450 gtk_list_store_remove (list_store, &iter);
1453 gtk_list_store_increment_stamp (list_store);
1457 * gtk_list_store_iter_is_valid:
1458 * @list_store: A #GtkListStore.
1459 * @iter: A #GtkTreeIter.
1461 * <warning>This function is slow. Only use it for debugging and/or testing
1462 * purposes.</warning>
1464 * Checks if the given iter is a valid iter for this #GtkListStore.
1466 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1471 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1474 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1475 g_return_val_if_fail (iter != NULL, FALSE);
1477 return iter_is_valid (iter, list_store);
1480 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
1487 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1492 if (gtk_list_store_get_iter (GTK_TREE_MODEL (drag_source),
1496 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1503 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1505 GtkSelectionData *selection_data)
1507 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1508 * target, because the default handler does it for us, but
1509 * we do anyway for the convenience of someone maybe overriding the
1513 if (gtk_tree_set_row_drag_data (selection_data,
1514 GTK_TREE_MODEL (drag_source),
1521 /* FIXME handle text targets at least. */
1528 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1530 GtkSelectionData *selection_data)
1532 GtkTreeModel *tree_model = GTK_TREE_MODEL (drag_dest);
1533 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
1534 GtkListStorePrivate *priv = list_store->priv;
1535 GtkTreeModel *src_model = NULL;
1536 GtkTreePath *src_path = NULL;
1537 gboolean retval = FALSE;
1539 if (gtk_tree_get_row_drag_data (selection_data,
1542 src_model == tree_model)
1544 /* Copy the given row to a new position */
1545 GtkTreeIter src_iter;
1546 GtkTreeIter dest_iter;
1549 if (!gtk_list_store_get_iter (src_model,
1556 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1557 prev = gtk_tree_path_copy (dest);
1559 if (!gtk_tree_path_prev (prev))
1561 /* dest was the first spot in the list; which means we are supposed
1564 gtk_list_store_prepend (list_store, &dest_iter);
1570 if (gtk_list_store_get_iter (tree_model, &dest_iter, prev))
1572 GtkTreeIter tmp_iter = dest_iter;
1574 gtk_list_store_insert_after (list_store, &dest_iter, &tmp_iter);
1580 gtk_tree_path_free (prev);
1582 /* If we succeeded in creating dest_iter, copy data from src
1586 GtkTreeDataList *dl = g_sequence_get (src_iter.user_data);
1587 GtkTreeDataList *copy_head = NULL;
1588 GtkTreeDataList *copy_prev = NULL;
1589 GtkTreeDataList *copy_iter = NULL;
1596 copy_iter = _gtk_tree_data_list_node_copy (dl,
1597 priv->column_headers[col]);
1599 if (copy_head == NULL)
1600 copy_head = copy_iter;
1603 copy_prev->next = copy_iter;
1605 copy_prev = copy_iter;
1611 dest_iter.stamp = priv->stamp;
1612 g_sequence_set (dest_iter.user_data, copy_head);
1614 path = gtk_list_store_get_path (tree_model, &dest_iter);
1615 gtk_tree_model_row_changed (tree_model, path, &dest_iter);
1616 gtk_tree_path_free (path);
1621 /* FIXME maybe add some data targets eventually, or handle text
1622 * targets in the simple case.
1629 gtk_tree_path_free (src_path);
1635 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1636 GtkTreePath *dest_path,
1637 GtkSelectionData *selection_data)
1640 GtkTreeModel *src_model = NULL;
1641 GtkTreePath *src_path = NULL;
1642 gboolean retval = FALSE;
1644 /* don't accept drops if the list has been sorted */
1645 if (GTK_LIST_STORE_IS_SORTED (drag_dest))
1648 if (!gtk_tree_get_row_drag_data (selection_data,
1653 if (src_model != GTK_TREE_MODEL (drag_dest))
1656 if (gtk_tree_path_get_depth (dest_path) != 1)
1659 /* can drop before any existing node, or before one past any existing. */
1661 indices = gtk_tree_path_get_indices (dest_path);
1663 if (indices[0] <= g_sequence_get_length (GTK_LIST_STORE (drag_dest)->priv->seq))
1668 gtk_tree_path_free (src_path);
1673 /* Sorting and reordering */
1677 gtk_list_store_reorder_func (GSequenceIter *a,
1681 GHashTable *new_positions = user_data;
1682 gint apos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, a));
1683 gint bpos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, b));
1693 * gtk_list_store_reorder: (skip)
1694 * @store: A #GtkListStore.
1695 * @new_order: (array): an array of integers mapping the new position of each child
1696 * to its old position before the re-ordering,
1697 * i.e. @new_order<literal>[newpos] = oldpos</literal>.
1699 * Reorders @store to follow the order indicated by @new_order. Note that
1700 * this function only works with unsorted stores.
1705 gtk_list_store_reorder (GtkListStore *store,
1708 GtkListStorePrivate *priv;
1711 GHashTable *new_positions;
1715 g_return_if_fail (GTK_IS_LIST_STORE (store));
1716 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1717 g_return_if_fail (new_order != NULL);
1721 order = g_new (gint, g_sequence_get_length (priv->seq));
1722 for (i = 0; i < g_sequence_get_length (priv->seq); i++)
1723 order[new_order[i]] = i;
1725 new_positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1727 ptr = g_sequence_get_begin_iter (priv->seq);
1729 while (!g_sequence_iter_is_end (ptr))
1731 g_hash_table_insert (new_positions, ptr, GINT_TO_POINTER (order[i++]));
1733 ptr = g_sequence_iter_next (ptr);
1737 g_sequence_sort_iter (priv->seq, gtk_list_store_reorder_func, new_positions);
1739 g_hash_table_destroy (new_positions);
1742 path = gtk_tree_path_new ();
1743 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1744 path, NULL, new_order);
1745 gtk_tree_path_free (path);
1749 save_positions (GSequence *seq)
1751 GHashTable *positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1754 ptr = g_sequence_get_begin_iter (seq);
1755 while (!g_sequence_iter_is_end (ptr))
1757 g_hash_table_insert (positions, ptr,
1758 GINT_TO_POINTER (g_sequence_iter_get_position (ptr)));
1759 ptr = g_sequence_iter_next (ptr);
1766 generate_order (GSequence *seq,
1767 GHashTable *old_positions)
1770 int *order = g_new (int, g_sequence_get_length (seq));
1774 ptr = g_sequence_get_begin_iter (seq);
1775 while (!g_sequence_iter_is_end (ptr))
1777 int old_pos = GPOINTER_TO_INT (g_hash_table_lookup (old_positions, ptr));
1778 order[i++] = old_pos;
1779 ptr = g_sequence_iter_next (ptr);
1782 g_hash_table_destroy (old_positions);
1788 * gtk_list_store_swap:
1789 * @store: A #GtkListStore.
1790 * @a: A #GtkTreeIter.
1791 * @b: Another #GtkTreeIter.
1793 * Swaps @a and @b in @store. Note that this function only works with
1799 gtk_list_store_swap (GtkListStore *store,
1803 GtkListStorePrivate *priv;
1804 GHashTable *old_positions;
1808 g_return_if_fail (GTK_IS_LIST_STORE (store));
1809 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1810 g_return_if_fail (iter_is_valid (a, store));
1811 g_return_if_fail (iter_is_valid (b, store));
1815 if (a->user_data == b->user_data)
1818 old_positions = save_positions (priv->seq);
1820 g_sequence_swap (a->user_data, b->user_data);
1822 order = generate_order (priv->seq, old_positions);
1823 path = gtk_tree_path_new ();
1825 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1828 gtk_tree_path_free (path);
1833 gtk_list_store_move_to (GtkListStore *store,
1837 GtkListStorePrivate *priv = store->priv;
1838 GHashTable *old_positions;
1842 old_positions = save_positions (priv->seq);
1844 g_sequence_move (iter->user_data, g_sequence_get_iter_at_pos (priv->seq, new_pos));
1846 order = generate_order (priv->seq, old_positions);
1848 path = gtk_tree_path_new ();
1849 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1851 gtk_tree_path_free (path);
1856 * gtk_list_store_move_before:
1857 * @store: A #GtkListStore.
1858 * @iter: A #GtkTreeIter.
1859 * @position: (allow-none): A #GtkTreeIter, or %NULL.
1861 * Moves @iter in @store to the position before @position. Note that this
1862 * function only works with unsorted stores. If @position is %NULL, @iter
1863 * will be moved to the end of the list.
1868 gtk_list_store_move_before (GtkListStore *store,
1870 GtkTreeIter *position)
1874 g_return_if_fail (GTK_IS_LIST_STORE (store));
1875 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1876 g_return_if_fail (iter_is_valid (iter, store));
1878 g_return_if_fail (iter_is_valid (position, store));
1881 pos = g_sequence_iter_get_position (position->user_data);
1885 gtk_list_store_move_to (store, iter, pos);
1889 * gtk_list_store_move_after:
1890 * @store: A #GtkListStore.
1891 * @iter: A #GtkTreeIter.
1892 * @position: (allow-none): A #GtkTreeIter or %NULL.
1894 * Moves @iter in @store to the position after @position. Note that this
1895 * function only works with unsorted stores. If @position is %NULL, @iter
1896 * will be moved to the start of the list.
1901 gtk_list_store_move_after (GtkListStore *store,
1903 GtkTreeIter *position)
1907 g_return_if_fail (GTK_IS_LIST_STORE (store));
1908 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1909 g_return_if_fail (iter_is_valid (iter, store));
1911 g_return_if_fail (iter_is_valid (position, store));
1914 pos = g_sequence_iter_get_position (position->user_data) + 1;
1918 gtk_list_store_move_to (store, iter, pos);
1923 gtk_list_store_compare_func (GSequenceIter *a,
1927 GtkListStore *list_store = user_data;
1928 GtkListStorePrivate *priv = list_store->priv;
1932 GtkTreeIterCompareFunc func;
1935 if (priv->sort_column_id != -1)
1937 GtkTreeDataSortHeader *header;
1939 header = _gtk_tree_data_list_get_header (priv->sort_list,
1940 priv->sort_column_id);
1941 g_return_val_if_fail (header != NULL, 0);
1942 g_return_val_if_fail (header->func != NULL, 0);
1944 func = header->func;
1945 data = header->data;
1949 g_return_val_if_fail (priv->default_sort_func != NULL, 0);
1950 func = priv->default_sort_func;
1951 data = priv->default_sort_data;
1954 iter_a.stamp = priv->stamp;
1955 iter_a.user_data = (gpointer)a;
1956 iter_b.stamp = priv->stamp;
1957 iter_b.user_data = (gpointer)b;
1959 g_assert (iter_is_valid (&iter_a, list_store));
1960 g_assert (iter_is_valid (&iter_b, list_store));
1962 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1964 if (priv->order == GTK_SORT_DESCENDING)
1968 else if (retval < 0)
1976 gtk_list_store_sort (GtkListStore *list_store)
1978 GtkListStorePrivate *priv = list_store->priv;
1981 GHashTable *old_positions;
1983 if (!GTK_LIST_STORE_IS_SORTED (list_store) ||
1984 g_sequence_get_length (priv->seq) <= 1)
1987 old_positions = save_positions (priv->seq);
1989 g_sequence_sort_iter (priv->seq, gtk_list_store_compare_func, list_store);
1991 /* Let the world know about our new order */
1992 new_order = generate_order (priv->seq, old_positions);
1994 path = gtk_tree_path_new ();
1995 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1996 path, NULL, new_order);
1997 gtk_tree_path_free (path);
2002 iter_is_sorted (GtkListStore *list_store,
2007 if (!g_sequence_iter_is_begin (iter->user_data))
2009 cmp = g_sequence_iter_prev (iter->user_data);
2010 if (gtk_list_store_compare_func (cmp, iter->user_data, list_store) > 0)
2014 cmp = g_sequence_iter_next (iter->user_data);
2015 if (!g_sequence_iter_is_end (cmp))
2017 if (gtk_list_store_compare_func (iter->user_data, cmp, list_store) > 0)
2025 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
2030 GtkListStorePrivate *priv = list_store->priv;
2033 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2034 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
2035 gtk_tree_path_free (path);
2037 if (!iter_is_sorted (list_store, iter))
2039 GHashTable *old_positions;
2042 old_positions = save_positions (priv->seq);
2043 g_sequence_sort_changed_iter (iter->user_data,
2044 gtk_list_store_compare_func,
2046 order = generate_order (priv->seq, old_positions);
2047 path = gtk_tree_path_new ();
2048 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
2050 gtk_tree_path_free (path);
2056 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
2057 gint *sort_column_id,
2060 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2061 GtkListStorePrivate *priv = list_store->priv;
2064 * sort_column_id = priv->sort_column_id;
2066 * order = priv->order;
2068 if (priv->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
2069 priv->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
2076 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
2077 gint sort_column_id,
2080 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2081 GtkListStorePrivate *priv = list_store->priv;
2083 if ((priv->sort_column_id == sort_column_id) &&
2084 (priv->order == order))
2087 if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
2089 if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2091 GtkTreeDataSortHeader *header = NULL;
2093 header = _gtk_tree_data_list_get_header (priv->sort_list,
2096 /* We want to make sure that we have a function */
2097 g_return_if_fail (header != NULL);
2098 g_return_if_fail (header->func != NULL);
2102 g_return_if_fail (priv->default_sort_func != NULL);
2107 priv->sort_column_id = sort_column_id;
2108 priv->order = order;
2110 gtk_tree_sortable_sort_column_changed (sortable);
2112 gtk_list_store_sort (list_store);
2116 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
2117 gint sort_column_id,
2118 GtkTreeIterCompareFunc func,
2120 GDestroyNotify destroy)
2122 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2123 GtkListStorePrivate *priv = list_store->priv;
2125 priv->sort_list = _gtk_tree_data_list_set_header (priv->sort_list,
2127 func, data, destroy);
2129 if (priv->sort_column_id == sort_column_id)
2130 gtk_list_store_sort (list_store);
2134 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
2135 GtkTreeIterCompareFunc func,
2137 GDestroyNotify destroy)
2139 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2140 GtkListStorePrivate *priv = list_store->priv;
2142 if (priv->default_sort_destroy)
2144 GDestroyNotify d = priv->default_sort_destroy;
2146 priv->default_sort_destroy = NULL;
2147 d (priv->default_sort_data);
2150 priv->default_sort_func = func;
2151 priv->default_sort_data = data;
2152 priv->default_sort_destroy = destroy;
2154 if (priv->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2155 gtk_list_store_sort (list_store);
2159 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
2161 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2162 GtkListStorePrivate *priv = list_store->priv;
2164 return (priv->default_sort_func != NULL);
2169 * gtk_list_store_insert_with_values:
2170 * @list_store: A #GtkListStore
2171 * @iter: (out) (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL
2172 * @position: position to insert the new row, or -1 to append after existing
2174 * @...: pairs of column number and value, terminated with -1
2176 * Creates a new row at @position. @iter will be changed to point to this new
2177 * row. If @position is -1, or larger than the number of rows in the list, then
2178 * the new row will be appended to the list. The row will be filled with the
2179 * values given to this function.
2182 * <literal>gtk_list_store_insert_with_values (list_store, iter, position...)</literal>
2183 * has the same effect as calling
2185 * gtk_list_store_insert (list_store, iter, position);
2186 * gtk_list_store_set (list_store, iter, ...);
2188 * with the difference that the former will only emit a row_inserted signal,
2189 * while the latter will emit row_inserted, row_changed and, if the list store
2190 * is sorted, rows_reordered. Since emitting the rows_reordered signal
2191 * repeatedly can affect the performance of the program,
2192 * gtk_list_store_insert_with_values() should generally be preferred when
2193 * inserting rows in a sorted list store.
2198 gtk_list_store_insert_with_values (GtkListStore *list_store,
2203 GtkListStorePrivate *priv;
2207 GtkTreeIter tmp_iter;
2209 gboolean changed = FALSE;
2210 gboolean maybe_need_sort = FALSE;
2213 /* FIXME: refactor to reduce overlap with gtk_list_store_set() */
2214 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2216 priv = list_store->priv;
2221 priv->columns_dirty = TRUE;
2225 length = g_sequence_get_length (seq);
2226 if (position > length)
2229 ptr = g_sequence_get_iter_at_pos (seq, position);
2230 ptr = g_sequence_insert_before (ptr, NULL);
2232 iter->stamp = priv->stamp;
2233 iter->user_data = ptr;
2235 g_assert (iter_is_valid (iter, list_store));
2239 va_start (var_args, position);
2240 gtk_list_store_set_valist_internal (list_store, iter,
2241 &changed, &maybe_need_sort,
2245 /* Don't emit rows_reordered here */
2246 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2247 g_sequence_sort_changed_iter (iter->user_data,
2248 gtk_list_store_compare_func,
2251 /* Just emit row_inserted */
2252 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2253 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2254 gtk_tree_path_free (path);
2259 * gtk_list_store_insert_with_valuesv:
2260 * @list_store: A #GtkListStore
2261 * @iter: (out) (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL.
2262 * @position: position to insert the new row
2263 * @columns: (array length=n_values): an array of column numbers
2264 * @values: (array length=n_values): an array of GValues
2265 * @n_values: the length of the @columns and @values arrays
2267 * A variant of gtk_list_store_insert_with_values() which
2268 * takes the columns and values as two arrays, instead of
2269 * varargs. This function is mainly intended for
2270 * language-bindings.
2275 gtk_list_store_insert_with_valuesv (GtkListStore *list_store,
2282 GtkListStorePrivate *priv;
2286 GtkTreeIter tmp_iter;
2288 gboolean changed = FALSE;
2289 gboolean maybe_need_sort = FALSE;
2291 /* FIXME refactor to reduce overlap with
2292 * gtk_list_store_insert_with_values()
2294 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2296 priv = list_store->priv;
2301 priv->columns_dirty = TRUE;
2305 length = g_sequence_get_length (seq);
2306 if (position > length)
2309 ptr = g_sequence_get_iter_at_pos (seq, position);
2310 ptr = g_sequence_insert_before (ptr, NULL);
2312 iter->stamp = priv->stamp;
2313 iter->user_data = ptr;
2315 g_assert (iter_is_valid (iter, list_store));
2319 gtk_list_store_set_vector_internal (list_store, iter,
2320 &changed, &maybe_need_sort,
2321 columns, values, n_values);
2323 /* Don't emit rows_reordered here */
2324 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2325 g_sequence_sort_changed_iter (iter->user_data,
2326 gtk_list_store_compare_func,
2329 /* Just emit row_inserted */
2330 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2331 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2332 gtk_tree_path_free (path);
2335 /* GtkBuildable custom tag implementation
2338 * <column type="..."/>
2339 * <column type="..."/>
2343 gboolean translatable;
2349 GtkBuilder *builder;
2351 GSList *column_type_names;
2352 GType *column_types;
2361 const gchar *domain;
2365 list_store_start_element (GMarkupParseContext *context,
2366 const gchar *element_name,
2367 const gchar **names,
2368 const gchar **values,
2373 SubParserData *data = (SubParserData*)user_data;
2375 if (strcmp (element_name, "col") == 0)
2378 gchar *context = NULL;
2379 gboolean translatable = FALSE;
2382 if (data->row_column >= data->n_columns)
2384 g_set_error (error, data->error_quark, 0,
2385 "Too many columns, maximum is %d\n", data->n_columns - 1);
2389 for (i = 0; names[i]; i++)
2390 if (strcmp (names[i], "id") == 0)
2393 id = atoi (values[i]);
2396 g_set_error (error, data->error_quark, 0,
2397 "the id tag %s could not be converted to an integer",
2401 if (id < 0 || id >= data->n_columns)
2403 g_set_error (error, data->error_quark, 0,
2404 "id value %d out of range", id);
2408 else if (strcmp (names[i], "translatable") == 0)
2410 if (!_gtk_builder_boolean_from_string (values[i], &translatable,
2414 else if (strcmp (names[i], "comments") == 0)
2416 /* do nothing, comments are for translators */
2418 else if (strcmp (names[i], "context") == 0)
2420 context = g_strdup (values[i]);
2425 g_set_error (error, data->error_quark, 0,
2426 "<col> needs an id attribute");
2430 info = g_slice_new0 (ColInfo);
2431 info->translatable = translatable;
2432 info->context = context;
2435 data->colids[data->row_column] = id;
2436 data->columns[data->row_column] = info;
2438 data->is_data = TRUE;
2440 else if (strcmp (element_name, "row") == 0)
2442 else if (strcmp (element_name, "column") == 0)
2443 for (i = 0; names[i]; i++)
2444 if (strcmp (names[i], "type") == 0)
2445 data->column_type_names = g_slist_prepend (data->column_type_names,
2446 g_strdup (values[i]));
2447 else if (strcmp (element_name, "columns") == 0)
2449 else if (strcmp (element_name, "data") == 0)
2452 g_set_error (error, data->error_quark, 0,
2453 "Unknown start tag: %s", element_name);
2457 list_store_end_element (GMarkupParseContext *context,
2458 const gchar *element_name,
2462 SubParserData *data = (SubParserData*)user_data;
2464 g_assert (data->builder);
2466 if (strcmp (element_name, "row") == 0)
2471 gtk_list_store_insert_with_valuesv (GTK_LIST_STORE (data->object),
2477 for (i = 0; i < data->row_column; i++)
2479 ColInfo *info = data->columns[i];
2480 g_free (info->context);
2481 g_slice_free (ColInfo, info);
2482 data->columns[i] = NULL;
2483 g_value_unset (&data->values[i]);
2485 g_free (data->values);
2486 data->values = g_new0 (GValue, data->n_columns);
2488 data->row_column = 0;
2490 else if (strcmp (element_name, "columns") == 0)
2492 GType *column_types;
2497 data->column_type_names = g_slist_reverse (data->column_type_names);
2498 column_types = g_new0 (GType, g_slist_length (data->column_type_names));
2500 for (l = data->column_type_names, i = 0; l; l = l->next, i++)
2502 type = gtk_builder_get_type_from_name (data->builder, l->data);
2503 if (type == G_TYPE_INVALID)
2505 g_warning ("Unknown type %s specified in treemodel %s",
2506 (const gchar*)l->data,
2507 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
2510 column_types[i] = type;
2515 gtk_list_store_set_column_types (GTK_LIST_STORE (data->object), i,
2518 g_free (column_types);
2520 else if (strcmp (element_name, "col") == 0)
2521 data->is_data = FALSE;
2522 else if (strcmp (element_name, "data") == 0)
2524 else if (strcmp (element_name, "column") == 0)
2527 g_set_error (error, data->error_quark, 0,
2528 "Unknown end tag: %s", element_name);
2532 list_store_text (GMarkupParseContext *context,
2538 SubParserData *data = (SubParserData*)user_data;
2540 GError *tmp_error = NULL;
2547 i = data->row_column - 1;
2548 info = data->columns[i];
2550 string = g_strndup (text, text_len);
2551 if (info->translatable && text_len)
2555 /* FIXME: This will not use the domain set in the .ui file,
2556 * since the parser is not telling the builder about the domain.
2557 * However, it will work for gtk_builder_set_translation_domain() calls.
2559 translated = _gtk_builder_parser_translate (data->domain,
2563 string = translated;
2566 if (!gtk_builder_value_from_string_type (data->builder,
2567 data->column_types[info->id],
2575 "Could not convert '%s' to type %s: %s\n",
2576 text, g_type_name (data->column_types[info->id]),
2577 tmp_error->message);
2578 g_error_free (tmp_error);
2583 static const GMarkupParser list_store_parser =
2585 list_store_start_element,
2586 list_store_end_element,
2591 gtk_list_store_buildable_custom_tag_start (GtkBuildable *buildable,
2592 GtkBuilder *builder,
2594 const gchar *tagname,
2595 GMarkupParser *parser,
2598 SubParserData *parser_data;
2603 if (strcmp (tagname, "columns") == 0)
2606 parser_data = g_slice_new0 (SubParserData);
2607 parser_data->builder = builder;
2608 parser_data->object = G_OBJECT (buildable);
2609 parser_data->column_type_names = NULL;
2611 *parser = list_store_parser;
2612 *data = parser_data;
2615 else if (strcmp (tagname, "data") == 0)
2617 gint n_columns = gtk_list_store_get_n_columns (GTK_TREE_MODEL (buildable));
2619 g_error ("Cannot append data to an empty model");
2621 parser_data = g_slice_new0 (SubParserData);
2622 parser_data->builder = builder;
2623 parser_data->object = G_OBJECT (buildable);
2624 parser_data->values = g_new0 (GValue, n_columns);
2625 parser_data->colids = g_new0 (gint, n_columns);
2626 parser_data->columns = g_new0 (ColInfo*, n_columns);
2627 parser_data->column_types = GTK_LIST_STORE (buildable)->priv->column_headers;
2628 parser_data->n_columns = n_columns;
2629 parser_data->last_row = 0;
2630 parser_data->error_quark = g_quark_from_static_string ("GtkListStore");
2631 parser_data->domain = gtk_builder_get_translation_domain (builder);
2633 *parser = list_store_parser;
2634 *data = parser_data;
2638 g_warning ("Unknown custom list store tag: %s", tagname);
2644 gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
2645 GtkBuilder *builder,
2647 const gchar *tagname,
2650 SubParserData *sub = (SubParserData*)data;
2652 if (strcmp (tagname, "columns") == 0)
2654 g_slist_free (sub->column_type_names);
2655 g_slice_free (SubParserData, sub);
2657 else if (strcmp (tagname, "data") == 0)
2660 for (i = 0; i < sub->n_columns; i++)
2662 ColInfo *info = sub->columns[i];
2665 g_free (info->context);
2666 g_slice_free (ColInfo, info);
2669 g_free (sub->colids);
2670 g_free (sub->columns);
2671 g_free (sub->values);
2672 g_slice_free (SubParserData, sub);
2675 g_warning ("Unknown custom list store tag: %s", tagname);