2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gobject/gvaluecollector.h>
25 #include "gtktreemodel.h"
26 #include "gtkliststore.h"
27 #include "gtktreedatalist.h"
28 #include "gtktreednd.h"
30 #include "gtkbuildable.h"
31 #include "gtkbuilderprivate.h"
35 * SECTION:gtkliststore
36 * @Short_description: A list-like data structure that can be used with the GtkTreeView
37 * @Title: GtkListStore
38 * @See_also:#GtkTreeModel, #GtkTreeStore
40 * The #GtkListStore object is a list model for use with a #GtkTreeView
41 * widget. It implements the #GtkTreeModel interface, and consequentialy,
42 * can use all of the methods available there. It also implements the
43 * #GtkTreeSortable interface so it can be sorted by the view.
44 * Finally, it also implements the tree <link linkend="gtktreednd">drag and
45 * drop</link> interfaces.
47 * The #GtkListStore can accept most GObject types as a column type, though
48 * it can't accept all custom types. Internally, it will keep a copy of
49 * data passed in (such as a string or a boxed pointer). Columns that
50 * accept #GObject<!-- -->s are handled a little differently. The
51 * #GtkListStore will keep a reference to the object instead of copying the
52 * value. As a result, if the object is modified, it is up to the
53 * application writer to call gtk_tree_model_row_changed() to emit the
54 * #GtkTreeModel::row_changed signal. This most commonly affects lists with
55 * #GdkPixbuf<!-- -->s stored.
58 * <title>Creating a simple list store.</title>
68 * GtkListStore *list_store;
73 * list_store = gtk_list_store_new (N_COLUMNS,
78 * for (i = 0; i < 10; i++)
82 * some_data = get_some_data (i);
84 * // Add a new row to the model
85 * gtk_list_store_append (list_store, &iter);
86 * gtk_list_store_set (list_store, &iter,
87 * COLUMN_STRING, some_data,
89 * COLUMN_BOOLEAN, FALSE,
92 * /<!---->* As the store will keep a copy of the string internally, we
98 * // Modify a particular row
99 * path = gtk_tree_path_new_from_string ("4");
100 * gtk_tree_model_get_iter (GTK_TREE_MODEL (list_store),
103 * gtk_tree_path_free (path);
104 * gtk_list_store_set (list_store, &iter,
105 * COLUMN_BOOLEAN, TRUE,
112 * <title>Performance Considerations</title>
113 * Internally, the #GtkListStore was implemented with a linked list with a
114 * tail pointer prior to GTK+ 2.6. As a result, it was fast at data
115 * insertion and deletion, and not fast at random data access. The
116 * #GtkListStore sets the #GTK_TREE_MODEL_ITERS_PERSIST flag, which means
117 * that #GtkTreeIter<!-- -->s can be cached while the row exists. Thus, if
118 * access to a particular row is needed often and your code is expected to
119 * run on older versions of GTK+, it is worth keeping the iter around.
122 * <title>Atomic Operations</title>
123 * It is important to note that only the methods
124 * gtk_list_store_insert_with_values() and gtk_list_store_insert_with_valuesv()
125 * are atomic, in the sense that the row is being appended to the store and the
126 * values filled in in a single operation with regard to #GtkTreeModel signaling.
127 * In contrast, using e.g. gtk_list_store_append() and then gtk_list_store_set()
128 * will first create a row, which triggers the #GtkTreeModel::row-inserted signal
129 * on #GtkListStore. The row, however, is still empty, and any signal handler
130 * connecting to #GtkTreeModel::row-inserted on this particular store should be prepared
131 * for the situation that the row might be empty. This is especially important
132 * if you are wrapping the #GtkListStore inside a #GtkTreeModelFilter and are
133 * using a #GtkTreeModelFilterVisibleFunc. Using any of the non-atomic operations
134 * to append rows to the #GtkListStore will cause the
135 * #GtkTreeModelFilterVisibleFunc to be visited with an empty row first; the
136 * function must be prepared for that.
138 * <refsect2 id="GtkListStore-BUILDER-UI">
139 * <title>GtkListStore as GtkBuildable</title>
141 * The GtkListStore implementation of the GtkBuildable interface allows
142 * to specify the model columns with a <columns> element that may
143 * contain multiple <column> elements, each specifying one model
144 * column. The "type" attribute specifies the data type for the column.
146 * Additionally, it is possible to specify content for the list store
147 * in the UI definition, with the <data> element. It can contain
148 * multiple <row> elements, each specifying to content for one
149 * row of the list model. Inside a <row>, the <col> elements
150 * specify the content for individual cells.
152 * Note that it is probably more common to define your models
153 * in the code, and one might consider it a layering violation
154 * to specify the content of a list store in a UI definition,
155 * <emphasis>data</emphasis>, not <emphasis>presentation</emphasis>,
156 * and common wisdom is to separate the two, as far as possible.
157 * <!-- FIXME a bit inconclusive -->
160 * <title>A UI Definition fragment for a list store</title>
161 * <programlisting><![CDATA[
162 * <object class="GtkListStore">
164 * <column type="gchararray"/>
165 * <column type="gchararray"/>
166 * <column type="gint"/>
170 * <col id="0">John</col>
171 * <col id="1">Doe</col>
172 * <col id="2">25</col>
175 * <col id="0">Johan</col>
176 * <col id="1">Dahlin</col>
177 * <col id="2">50</col>
181 * ]]></programlisting>
188 struct _GtkListStorePrivate
190 GtkTreeIterCompareFunc default_sort_func;
192 GDestroyNotify default_sort_destroy;
194 GType *column_headers;
203 guint columns_dirty : 1;
205 gpointer default_sort_data;
206 gpointer seq; /* head of the list */
209 #define GTK_LIST_STORE_IS_SORTED(list) (((GtkListStore*)(list))->priv->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
210 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
211 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
212 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
213 static void gtk_list_store_sortable_init (GtkTreeSortableIface *iface);
214 static void gtk_list_store_buildable_init (GtkBuildableIface *iface);
215 static void gtk_list_store_finalize (GObject *object);
216 static GtkTreeModelFlags gtk_list_store_get_flags (GtkTreeModel *tree_model);
217 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
218 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
220 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
223 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
225 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
229 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
231 static gboolean gtk_list_store_iter_previous (GtkTreeModel *tree_model,
233 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
235 GtkTreeIter *parent);
236 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
238 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
240 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
244 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
249 static void gtk_list_store_set_n_columns (GtkListStore *list_store,
251 static void gtk_list_store_set_column_type (GtkListStore *list_store,
255 static void gtk_list_store_increment_stamp (GtkListStore *list_store);
259 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
261 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
263 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
265 GtkSelectionData *selection_data);
266 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
268 GtkSelectionData *selection_data);
269 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
270 GtkTreePath *dest_path,
271 GtkSelectionData *selection_data);
275 static void gtk_list_store_sort (GtkListStore *list_store);
276 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
279 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
280 gint *sort_column_id,
282 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
285 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
287 GtkTreeIterCompareFunc func,
289 GDestroyNotify destroy);
290 static void gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
291 GtkTreeIterCompareFunc func,
293 GDestroyNotify destroy);
294 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable);
298 static gboolean gtk_list_store_buildable_custom_tag_start (GtkBuildable *buildable,
301 const gchar *tagname,
302 GMarkupParser *parser,
304 static void gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
307 const gchar *tagname,
310 G_DEFINE_TYPE_WITH_CODE (GtkListStore, gtk_list_store, G_TYPE_OBJECT,
311 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
312 gtk_list_store_tree_model_init)
313 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
314 gtk_list_store_drag_source_init)
315 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
316 gtk_list_store_drag_dest_init)
317 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
318 gtk_list_store_sortable_init)
319 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
320 gtk_list_store_buildable_init))
324 gtk_list_store_class_init (GtkListStoreClass *class)
326 GObjectClass *object_class;
328 object_class = (GObjectClass*) class;
330 object_class->finalize = gtk_list_store_finalize;
332 g_type_class_add_private (class, sizeof (GtkListStorePrivate));
336 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
338 iface->get_flags = gtk_list_store_get_flags;
339 iface->get_n_columns = gtk_list_store_get_n_columns;
340 iface->get_column_type = gtk_list_store_get_column_type;
341 iface->get_iter = gtk_list_store_get_iter;
342 iface->get_path = gtk_list_store_get_path;
343 iface->get_value = gtk_list_store_get_value;
344 iface->iter_next = gtk_list_store_iter_next;
345 iface->iter_previous = gtk_list_store_iter_previous;
346 iface->iter_children = gtk_list_store_iter_children;
347 iface->iter_has_child = gtk_list_store_iter_has_child;
348 iface->iter_n_children = gtk_list_store_iter_n_children;
349 iface->iter_nth_child = gtk_list_store_iter_nth_child;
350 iface->iter_parent = gtk_list_store_iter_parent;
354 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
356 iface->row_draggable = real_gtk_list_store_row_draggable;
357 iface->drag_data_delete = gtk_list_store_drag_data_delete;
358 iface->drag_data_get = gtk_list_store_drag_data_get;
362 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
364 iface->drag_data_received = gtk_list_store_drag_data_received;
365 iface->row_drop_possible = gtk_list_store_row_drop_possible;
369 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
371 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
372 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
373 iface->set_sort_func = gtk_list_store_set_sort_func;
374 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
375 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
379 gtk_list_store_buildable_init (GtkBuildableIface *iface)
381 iface->custom_tag_start = gtk_list_store_buildable_custom_tag_start;
382 iface->custom_tag_end = gtk_list_store_buildable_custom_tag_end;
386 gtk_list_store_init (GtkListStore *list_store)
388 GtkListStorePrivate *priv;
390 list_store->priv = G_TYPE_INSTANCE_GET_PRIVATE (list_store,
392 GtkListStorePrivate);
393 priv = list_store->priv;
395 priv->seq = g_sequence_new (NULL);
396 priv->sort_list = NULL;
397 priv->stamp = g_random_int ();
398 priv->sort_column_id = GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID;
399 priv->columns_dirty = FALSE;
404 iter_is_valid (GtkTreeIter *iter,
405 GtkListStore *list_store)
407 return iter != NULL &&
408 iter->user_data != NULL &&
409 list_store->priv->stamp == iter->stamp &&
410 !g_sequence_iter_is_end (iter->user_data) &&
411 g_sequence_iter_get_sequence (iter->user_data) == list_store->priv->seq;
415 * gtk_list_store_new:
416 * @n_columns: number of columns in the list store
417 * @...: all #GType types for the columns, from first to last
419 * Creates a new list store as with @n_columns columns each of the types passed
420 * in. Note that only types derived from standard GObject fundamental types
423 * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
424 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
425 * int, string and #GdkPixbuf respectively.
427 * Return value: a new #GtkListStore
430 gtk_list_store_new (gint n_columns,
433 GtkListStore *retval;
437 g_return_val_if_fail (n_columns > 0, NULL);
439 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
440 gtk_list_store_set_n_columns (retval, n_columns);
442 va_start (args, n_columns);
444 for (i = 0; i < n_columns; i++)
446 GType type = va_arg (args, GType);
447 if (! _gtk_tree_data_list_check_type (type))
449 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
450 g_object_unref (retval);
456 gtk_list_store_set_column_type (retval, i, type);
466 * gtk_list_store_newv:
467 * @n_columns: number of columns in the list store
468 * @types: (array length=n_columns): an array of #GType types for the columns, from first to last
470 * Non-vararg creation function. Used primarily by language bindings.
472 * Return value: (transfer none): a new #GtkListStore
473 * Rename to: gtk_list_store_new
476 gtk_list_store_newv (gint n_columns,
479 GtkListStore *retval;
482 g_return_val_if_fail (n_columns > 0, NULL);
484 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
485 gtk_list_store_set_n_columns (retval, n_columns);
487 for (i = 0; i < n_columns; i++)
489 if (! _gtk_tree_data_list_check_type (types[i]))
491 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
492 g_object_unref (retval);
496 gtk_list_store_set_column_type (retval, i, types[i]);
503 * gtk_list_store_set_column_types:
504 * @list_store: A #GtkListStore
505 * @n_columns: Number of columns for the list store
506 * @types: (array length=n_columns): An array length n of #GTypes
508 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
509 * and should only be used when constructing a new #GtkListStore. It will not
510 * function after a row has been added, or a method on the #GtkTreeModel
511 * interface is called.
514 gtk_list_store_set_column_types (GtkListStore *list_store,
518 GtkListStorePrivate *priv;
521 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
523 priv = list_store->priv;
525 g_return_if_fail (priv->columns_dirty == 0);
527 gtk_list_store_set_n_columns (list_store, n_columns);
528 for (i = 0; i < n_columns; i++)
530 if (! _gtk_tree_data_list_check_type (types[i]))
532 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
535 gtk_list_store_set_column_type (list_store, i, types[i]);
540 gtk_list_store_set_n_columns (GtkListStore *list_store,
543 GtkListStorePrivate *priv = list_store->priv;
546 if (priv->n_columns == n_columns)
549 priv->column_headers = g_renew (GType, priv->column_headers, n_columns);
550 for (i = priv->n_columns; i < n_columns; i++)
551 priv->column_headers[i] = G_TYPE_INVALID;
552 priv->n_columns = n_columns;
555 _gtk_tree_data_list_header_free (priv->sort_list);
556 priv->sort_list = _gtk_tree_data_list_header_new (n_columns, priv->column_headers);
560 gtk_list_store_set_column_type (GtkListStore *list_store,
564 GtkListStorePrivate *priv = list_store->priv;
566 if (!_gtk_tree_data_list_check_type (type))
568 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
572 priv->column_headers[column] = type;
576 gtk_list_store_finalize (GObject *object)
578 GtkListStore *list_store = GTK_LIST_STORE (object);
579 GtkListStorePrivate *priv = list_store->priv;
581 g_sequence_foreach (priv->seq,
582 (GFunc) _gtk_tree_data_list_free, priv->column_headers);
584 g_sequence_free (priv->seq);
586 _gtk_tree_data_list_header_free (priv->sort_list);
587 g_free (priv->column_headers);
589 if (priv->default_sort_destroy)
591 GDestroyNotify d = priv->default_sort_destroy;
593 priv->default_sort_destroy = NULL;
594 d (priv->default_sort_data);
595 priv->default_sort_data = NULL;
598 G_OBJECT_CLASS (gtk_list_store_parent_class)->finalize (object);
601 /* Fulfill the GtkTreeModel requirements */
602 static GtkTreeModelFlags
603 gtk_list_store_get_flags (GtkTreeModel *tree_model)
605 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
609 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
611 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
612 GtkListStorePrivate *priv = list_store->priv;
614 priv->columns_dirty = TRUE;
616 return priv->n_columns;
620 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
623 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
624 GtkListStorePrivate *priv = list_store->priv;
626 g_return_val_if_fail (index < priv->n_columns, G_TYPE_INVALID);
628 priv->columns_dirty = TRUE;
630 return priv->column_headers[index];
634 gtk_list_store_get_iter (GtkTreeModel *tree_model,
638 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
639 GtkListStorePrivate *priv = list_store->priv;
643 priv->columns_dirty = TRUE;
647 i = gtk_tree_path_get_indices (path)[0];
649 if (i >= g_sequence_get_length (seq))
655 iter->stamp = priv->stamp;
656 iter->user_data = g_sequence_get_iter_at_pos (seq, i);
662 gtk_list_store_get_path (GtkTreeModel *tree_model,
665 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
666 GtkListStorePrivate *priv = list_store->priv;
669 g_return_val_if_fail (iter->stamp == priv->stamp, NULL);
671 if (g_sequence_iter_is_end (iter->user_data))
674 path = gtk_tree_path_new ();
675 gtk_tree_path_append_index (path, g_sequence_iter_get_position (iter->user_data));
681 gtk_list_store_get_value (GtkTreeModel *tree_model,
686 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
687 GtkListStorePrivate *priv = list_store->priv;
688 GtkTreeDataList *list;
689 gint tmp_column = column;
691 g_return_if_fail (column < priv->n_columns);
692 g_return_if_fail (iter_is_valid (iter, list_store));
694 list = g_sequence_get (iter->user_data);
696 while (tmp_column-- > 0 && list)
700 g_value_init (value, priv->column_headers[column]);
702 _gtk_tree_data_list_node_to_value (list,
703 priv->column_headers[column],
708 gtk_list_store_iter_next (GtkTreeModel *tree_model,
711 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
712 GtkListStorePrivate *priv = list_store->priv;
715 g_return_val_if_fail (priv->stamp == iter->stamp, FALSE);
716 iter->user_data = g_sequence_iter_next (iter->user_data);
718 retval = g_sequence_iter_is_end (iter->user_data);
726 gtk_list_store_iter_previous (GtkTreeModel *tree_model,
729 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
730 GtkListStorePrivate *priv = list_store->priv;
732 g_return_val_if_fail (priv->stamp == iter->stamp, FALSE);
734 if (g_sequence_iter_is_begin (iter->user_data))
740 iter->user_data = g_sequence_iter_prev (iter->user_data);
746 gtk_list_store_iter_children (GtkTreeModel *tree_model,
750 GtkListStore *list_store = (GtkListStore *) tree_model;
751 GtkListStorePrivate *priv = list_store->priv;
753 /* this is a list, nodes have no children */
760 if (g_sequence_get_length (priv->seq) > 0)
762 iter->stamp = priv->stamp;
763 iter->user_data = g_sequence_get_begin_iter (priv->seq);
774 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
781 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
784 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
785 GtkListStorePrivate *priv = list_store->priv;
788 return g_sequence_get_length (priv->seq);
790 g_return_val_if_fail (priv->stamp == iter->stamp, -1);
796 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
801 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
802 GtkListStorePrivate *priv = list_store->priv;
803 GSequenceIter *child;
810 child = g_sequence_get_iter_at_pos (priv->seq, n);
812 if (g_sequence_iter_is_end (child))
815 iter->stamp = priv->stamp;
816 iter->user_data = child;
822 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
831 gtk_list_store_real_set_value (GtkListStore *list_store,
837 GtkListStorePrivate *priv = list_store->priv;
838 GtkTreeDataList *list;
839 GtkTreeDataList *prev;
840 gint old_column = column;
841 GValue real_value = G_VALUE_INIT;
842 gboolean converted = FALSE;
843 gboolean retval = FALSE;
845 if (! g_type_is_a (G_VALUE_TYPE (value), priv->column_headers[column]))
847 if (! (g_value_type_compatible (G_VALUE_TYPE (value), priv->column_headers[column]) &&
848 g_value_type_compatible (priv->column_headers[column], G_VALUE_TYPE (value))))
850 g_warning ("%s: Unable to convert from %s to %s\n",
852 g_type_name (G_VALUE_TYPE (value)),
853 g_type_name (priv->column_headers[column]));
856 if (!g_value_transform (value, &real_value))
858 g_warning ("%s: Unable to make conversion from %s to %s\n",
860 g_type_name (G_VALUE_TYPE (value)),
861 g_type_name (priv->column_headers[column]));
862 g_value_unset (&real_value);
868 prev = list = g_sequence_get (iter->user_data);
875 _gtk_tree_data_list_value_to_node (list, &real_value);
877 _gtk_tree_data_list_value_to_node (list, value);
880 g_value_unset (&real_value);
881 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
882 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
891 if (g_sequence_get (iter->user_data) == NULL)
893 list = _gtk_tree_data_list_alloc();
894 g_sequence_set (iter->user_data, list);
899 list = prev->next = _gtk_tree_data_list_alloc ();
905 list->next = _gtk_tree_data_list_alloc ();
912 _gtk_tree_data_list_value_to_node (list, &real_value);
914 _gtk_tree_data_list_value_to_node (list, value);
918 g_value_unset (&real_value);
920 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
921 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
928 * gtk_list_store_set_value:
929 * @list_store: A #GtkListStore
930 * @iter: A valid #GtkTreeIter for the row being modified
931 * @column: column number to modify
932 * @value: new value for the cell
934 * Sets the data in the cell specified by @iter and @column.
935 * The type of @value must be convertible to the type of the
940 gtk_list_store_set_value (GtkListStore *list_store,
945 GtkListStorePrivate *priv;
947 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
948 g_return_if_fail (iter_is_valid (iter, list_store));
949 g_return_if_fail (G_IS_VALUE (value));
950 priv = list_store->priv;
951 g_return_if_fail (column >= 0 && column < priv->n_columns);
953 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
957 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
958 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
959 gtk_tree_path_free (path);
963 static GtkTreeIterCompareFunc
964 gtk_list_store_get_compare_func (GtkListStore *list_store)
966 GtkListStorePrivate *priv = list_store->priv;
967 GtkTreeIterCompareFunc func = NULL;
969 if (GTK_LIST_STORE_IS_SORTED (list_store))
971 if (priv->sort_column_id != -1)
973 GtkTreeDataSortHeader *header;
974 header = _gtk_tree_data_list_get_header (priv->sort_list,
975 priv->sort_column_id);
976 g_return_val_if_fail (header != NULL, NULL);
977 g_return_val_if_fail (header->func != NULL, NULL);
982 func = priv->default_sort_func;
990 gtk_list_store_set_vector_internal (GtkListStore *list_store,
992 gboolean *emit_signal,
993 gboolean *maybe_need_sort,
998 GtkListStorePrivate *priv = list_store->priv;
1000 GtkTreeIterCompareFunc func = NULL;
1002 func = gtk_list_store_get_compare_func (list_store);
1003 if (func != _gtk_tree_data_list_compare_func)
1004 *maybe_need_sort = TRUE;
1006 for (i = 0; i < n_values; i++)
1008 *emit_signal = gtk_list_store_real_set_value (list_store,
1012 FALSE) || *emit_signal;
1014 if (func == _gtk_tree_data_list_compare_func &&
1015 columns[i] == priv->sort_column_id)
1016 *maybe_need_sort = TRUE;
1021 gtk_list_store_set_valist_internal (GtkListStore *list_store,
1023 gboolean *emit_signal,
1024 gboolean *maybe_need_sort,
1027 GtkListStorePrivate *priv = list_store->priv;
1029 GtkTreeIterCompareFunc func = NULL;
1031 column = va_arg (var_args, gint);
1033 func = gtk_list_store_get_compare_func (list_store);
1034 if (func != _gtk_tree_data_list_compare_func)
1035 *maybe_need_sort = TRUE;
1037 while (column != -1)
1039 GValue value = G_VALUE_INIT;
1040 gchar *error = NULL;
1042 if (column < 0 || column >= priv->n_columns)
1044 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
1048 G_VALUE_COLLECT_INIT (&value, priv->column_headers[column],
1049 var_args, 0, &error);
1052 g_warning ("%s: %s", G_STRLOC, error);
1055 /* we purposely leak the value here, it might not be
1056 * in a sane state if an error condition occoured
1061 /* FIXME: instead of calling this n times, refactor with above */
1062 *emit_signal = gtk_list_store_real_set_value (list_store,
1066 FALSE) || *emit_signal;
1068 if (func == _gtk_tree_data_list_compare_func &&
1069 column == priv->sort_column_id)
1070 *maybe_need_sort = TRUE;
1072 g_value_unset (&value);
1074 column = va_arg (var_args, gint);
1079 * gtk_list_store_set_valuesv:
1080 * @list_store: A #GtkListStore
1081 * @iter: A valid #GtkTreeIter for the row being modified
1082 * @columns: (array length=n_values): an array of column numbers
1083 * @values: (array length=n_values): an array of GValues
1084 * @n_values: the length of the @columns and @values arrays
1086 * A variant of gtk_list_store_set_valist() which
1087 * takes the columns and values as two arrays, instead of
1088 * varargs. This function is mainly intended for
1089 * language-bindings and in case the number of columns to
1090 * change is not known until run-time.
1093 * Rename to: gtk_list_store_set
1096 gtk_list_store_set_valuesv (GtkListStore *list_store,
1102 GtkListStorePrivate *priv;
1103 gboolean emit_signal = FALSE;
1104 gboolean maybe_need_sort = FALSE;
1106 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1107 g_return_if_fail (iter_is_valid (iter, list_store));
1109 priv = list_store->priv;
1111 gtk_list_store_set_vector_internal (list_store, iter,
1114 columns, values, n_values);
1116 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
1117 gtk_list_store_sort_iter_changed (list_store, iter, priv->sort_column_id);
1123 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1124 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
1125 gtk_tree_path_free (path);
1130 * gtk_list_store_set_valist:
1131 * @list_store: A #GtkListStore
1132 * @iter: A valid #GtkTreeIter for the row being modified
1133 * @var_args: va_list of column/value pairs
1135 * See gtk_list_store_set(); this version takes a va_list for use by language
1140 gtk_list_store_set_valist (GtkListStore *list_store,
1144 GtkListStorePrivate *priv;
1145 gboolean emit_signal = FALSE;
1146 gboolean maybe_need_sort = FALSE;
1148 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1149 g_return_if_fail (iter_is_valid (iter, list_store));
1151 priv = list_store->priv;
1153 gtk_list_store_set_valist_internal (list_store, iter,
1158 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
1159 gtk_list_store_sort_iter_changed (list_store, iter, priv->sort_column_id);
1165 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1166 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
1167 gtk_tree_path_free (path);
1172 * gtk_list_store_set:
1173 * @list_store: a #GtkListStore
1174 * @iter: row iterator
1175 * @...: pairs of column number and value, terminated with -1
1177 * Sets the value of one or more cells in the row referenced by @iter.
1178 * The variable argument list should contain integer column numbers,
1179 * each column number followed by the value to be set.
1180 * The list is terminated by a -1. For example, to set column 0 with type
1181 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
1182 * 0, "Foo", -1)</literal>.
1184 * The value will be referenced by the store if it is a %G_TYPE_OBJECT, and it
1185 * will be copied if it is a %G_TYPE_STRING or %G_TYPE_BOXED.
1188 gtk_list_store_set (GtkListStore *list_store,
1194 va_start (var_args, iter);
1195 gtk_list_store_set_valist (list_store, iter, var_args);
1200 * gtk_list_store_remove:
1201 * @list_store: A #GtkListStore
1202 * @iter: A valid #GtkTreeIter
1204 * Removes the given row from the list store. After being removed,
1205 * @iter is set to be the next valid row, or invalidated if it pointed
1206 * to the last row in @list_store.
1208 * Return value: %TRUE if @iter is valid, %FALSE if not.
1211 gtk_list_store_remove (GtkListStore *list_store,
1214 GtkListStorePrivate *priv;
1216 GSequenceIter *ptr, *next;
1218 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1219 g_return_val_if_fail (iter_is_valid (iter, list_store), FALSE);
1221 priv = list_store->priv;
1223 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1225 ptr = iter->user_data;
1226 next = g_sequence_iter_next (ptr);
1228 _gtk_tree_data_list_free (g_sequence_get (ptr), priv->column_headers);
1229 g_sequence_remove (iter->user_data);
1233 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
1234 gtk_tree_path_free (path);
1236 if (g_sequence_iter_is_end (next))
1243 iter->stamp = priv->stamp;
1244 iter->user_data = next;
1250 * gtk_list_store_insert:
1251 * @list_store: A #GtkListStore
1252 * @iter: (out): An unset #GtkTreeIter to set to the new row
1253 * @position: position to insert the new row
1255 * Creates a new row at @position. @iter will be changed to point to this new
1256 * row. If @position is larger than the number of rows on the list, then the
1257 * new row will be appended to the list. The row will be empty after this
1258 * function is called. To fill in values, you need to call
1259 * gtk_list_store_set() or gtk_list_store_set_value().
1263 gtk_list_store_insert (GtkListStore *list_store,
1267 GtkListStorePrivate *priv;
1273 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1274 g_return_if_fail (iter != NULL);
1275 g_return_if_fail (position >= 0);
1277 priv = list_store->priv;
1279 priv->columns_dirty = TRUE;
1283 length = g_sequence_get_length (seq);
1284 if (position > length)
1287 ptr = g_sequence_get_iter_at_pos (seq, position);
1288 ptr = g_sequence_insert_before (ptr, NULL);
1290 iter->stamp = priv->stamp;
1291 iter->user_data = ptr;
1293 g_assert (iter_is_valid (iter, list_store));
1297 path = gtk_tree_path_new ();
1298 gtk_tree_path_append_index (path, position);
1299 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1300 gtk_tree_path_free (path);
1304 * gtk_list_store_insert_before:
1305 * @list_store: A #GtkListStore
1306 * @iter: (out): An unset #GtkTreeIter to set to the new row
1307 * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1309 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will
1310 * be appended to the end of the list. @iter will be changed to point to this
1311 * new row. The row will be empty after this function is called. To fill in
1312 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1316 gtk_list_store_insert_before (GtkListStore *list_store,
1318 GtkTreeIter *sibling)
1320 GtkListStorePrivate *priv;
1321 GSequenceIter *after;
1323 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1324 g_return_if_fail (iter != NULL);
1326 priv = list_store->priv;
1329 g_return_if_fail (iter_is_valid (sibling, list_store));
1332 after = g_sequence_get_end_iter (priv->seq);
1334 after = sibling->user_data;
1336 gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1340 * gtk_list_store_insert_after:
1341 * @list_store: A #GtkListStore
1342 * @iter: (out): An unset #GtkTreeIter to set to the new row
1343 * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1345 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1346 * prepended to the beginning of the list. @iter will be changed to point to
1347 * this new row. The row will be empty after this function is called. To fill
1348 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1352 gtk_list_store_insert_after (GtkListStore *list_store,
1354 GtkTreeIter *sibling)
1356 GtkListStorePrivate *priv;
1357 GSequenceIter *after;
1359 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1360 g_return_if_fail (iter != NULL);
1362 priv = list_store->priv;
1365 g_return_if_fail (iter_is_valid (sibling, list_store));
1368 after = g_sequence_get_begin_iter (priv->seq);
1370 after = g_sequence_iter_next (sibling->user_data);
1372 gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1376 * gtk_list_store_prepend:
1377 * @list_store: A #GtkListStore
1378 * @iter: (out): An unset #GtkTreeIter to set to the prepend row
1380 * Prepends a new row to @list_store. @iter will be changed to point to this new
1381 * row. The row will be empty after this function is called. To fill in
1382 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1386 gtk_list_store_prepend (GtkListStore *list_store,
1389 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1390 g_return_if_fail (iter != NULL);
1392 gtk_list_store_insert (list_store, iter, 0);
1396 * gtk_list_store_append:
1397 * @list_store: A #GtkListStore
1398 * @iter: (out): An unset #GtkTreeIter to set to the appended row
1400 * Appends a new row to @list_store. @iter will be changed to point to this new
1401 * row. The row will be empty after this function is called. To fill in
1402 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1406 gtk_list_store_append (GtkListStore *list_store,
1409 GtkListStorePrivate *priv;
1411 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1412 g_return_if_fail (iter != NULL);
1414 priv = list_store->priv;
1416 gtk_list_store_insert (list_store, iter, g_sequence_get_length (priv->seq));
1420 gtk_list_store_increment_stamp (GtkListStore *list_store)
1422 GtkListStorePrivate *priv = list_store->priv;
1428 while (priv->stamp == 0);
1432 * gtk_list_store_clear:
1433 * @list_store: a #GtkListStore.
1435 * Removes all rows from the list store.
1439 gtk_list_store_clear (GtkListStore *list_store)
1441 GtkListStorePrivate *priv;
1444 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1446 priv = list_store->priv;
1448 while (g_sequence_get_length (priv->seq) > 0)
1450 iter.stamp = priv->stamp;
1451 iter.user_data = g_sequence_get_begin_iter (priv->seq);
1452 gtk_list_store_remove (list_store, &iter);
1455 gtk_list_store_increment_stamp (list_store);
1459 * gtk_list_store_iter_is_valid:
1460 * @list_store: A #GtkListStore.
1461 * @iter: A #GtkTreeIter.
1463 * <warning>This function is slow. Only use it for debugging and/or testing
1464 * purposes.</warning>
1466 * Checks if the given iter is a valid iter for this #GtkListStore.
1468 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1473 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1476 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1477 g_return_val_if_fail (iter != NULL, FALSE);
1479 return iter_is_valid (iter, list_store);
1482 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
1489 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1494 if (gtk_list_store_get_iter (GTK_TREE_MODEL (drag_source),
1498 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1505 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1507 GtkSelectionData *selection_data)
1509 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1510 * target, because the default handler does it for us, but
1511 * we do anyway for the convenience of someone maybe overriding the
1515 if (gtk_tree_set_row_drag_data (selection_data,
1516 GTK_TREE_MODEL (drag_source),
1523 /* FIXME handle text targets at least. */
1530 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1532 GtkSelectionData *selection_data)
1534 GtkTreeModel *tree_model = GTK_TREE_MODEL (drag_dest);
1535 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
1536 GtkListStorePrivate *priv = list_store->priv;
1537 GtkTreeModel *src_model = NULL;
1538 GtkTreePath *src_path = NULL;
1539 gboolean retval = FALSE;
1541 if (gtk_tree_get_row_drag_data (selection_data,
1544 src_model == tree_model)
1546 /* Copy the given row to a new position */
1547 GtkTreeIter src_iter;
1548 GtkTreeIter dest_iter;
1551 if (!gtk_list_store_get_iter (src_model,
1558 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1559 prev = gtk_tree_path_copy (dest);
1561 if (!gtk_tree_path_prev (prev))
1563 /* dest was the first spot in the list; which means we are supposed
1566 gtk_list_store_prepend (list_store, &dest_iter);
1572 if (gtk_list_store_get_iter (tree_model, &dest_iter, prev))
1574 GtkTreeIter tmp_iter = dest_iter;
1576 gtk_list_store_insert_after (list_store, &dest_iter, &tmp_iter);
1582 gtk_tree_path_free (prev);
1584 /* If we succeeded in creating dest_iter, copy data from src
1588 GtkTreeDataList *dl = g_sequence_get (src_iter.user_data);
1589 GtkTreeDataList *copy_head = NULL;
1590 GtkTreeDataList *copy_prev = NULL;
1591 GtkTreeDataList *copy_iter = NULL;
1598 copy_iter = _gtk_tree_data_list_node_copy (dl,
1599 priv->column_headers[col]);
1601 if (copy_head == NULL)
1602 copy_head = copy_iter;
1605 copy_prev->next = copy_iter;
1607 copy_prev = copy_iter;
1613 dest_iter.stamp = priv->stamp;
1614 g_sequence_set (dest_iter.user_data, copy_head);
1616 path = gtk_list_store_get_path (tree_model, &dest_iter);
1617 gtk_tree_model_row_changed (tree_model, path, &dest_iter);
1618 gtk_tree_path_free (path);
1623 /* FIXME maybe add some data targets eventually, or handle text
1624 * targets in the simple case.
1631 gtk_tree_path_free (src_path);
1637 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1638 GtkTreePath *dest_path,
1639 GtkSelectionData *selection_data)
1642 GtkTreeModel *src_model = NULL;
1643 GtkTreePath *src_path = NULL;
1644 gboolean retval = FALSE;
1646 /* don't accept drops if the list has been sorted */
1647 if (GTK_LIST_STORE_IS_SORTED (drag_dest))
1650 if (!gtk_tree_get_row_drag_data (selection_data,
1655 if (src_model != GTK_TREE_MODEL (drag_dest))
1658 if (gtk_tree_path_get_depth (dest_path) != 1)
1661 /* can drop before any existing node, or before one past any existing. */
1663 indices = gtk_tree_path_get_indices (dest_path);
1665 if (indices[0] <= g_sequence_get_length (GTK_LIST_STORE (drag_dest)->priv->seq))
1670 gtk_tree_path_free (src_path);
1675 /* Sorting and reordering */
1679 gtk_list_store_reorder_func (GSequenceIter *a,
1683 GHashTable *new_positions = user_data;
1684 gint apos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, a));
1685 gint bpos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, b));
1695 * gtk_list_store_reorder: (skip)
1696 * @store: A #GtkListStore.
1697 * @new_order: (array): an array of integers mapping the new position of each child
1698 * to its old position before the re-ordering,
1699 * i.e. @new_order<literal>[newpos] = oldpos</literal>.
1701 * Reorders @store to follow the order indicated by @new_order. Note that
1702 * this function only works with unsorted stores.
1707 gtk_list_store_reorder (GtkListStore *store,
1710 GtkListStorePrivate *priv;
1713 GHashTable *new_positions;
1717 g_return_if_fail (GTK_IS_LIST_STORE (store));
1718 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1719 g_return_if_fail (new_order != NULL);
1723 order = g_new (gint, g_sequence_get_length (priv->seq));
1724 for (i = 0; i < g_sequence_get_length (priv->seq); i++)
1725 order[new_order[i]] = i;
1727 new_positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1729 ptr = g_sequence_get_begin_iter (priv->seq);
1731 while (!g_sequence_iter_is_end (ptr))
1733 g_hash_table_insert (new_positions, ptr, GINT_TO_POINTER (order[i++]));
1735 ptr = g_sequence_iter_next (ptr);
1739 g_sequence_sort_iter (priv->seq, gtk_list_store_reorder_func, new_positions);
1741 g_hash_table_destroy (new_positions);
1744 path = gtk_tree_path_new ();
1745 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1746 path, NULL, new_order);
1747 gtk_tree_path_free (path);
1751 save_positions (GSequence *seq)
1753 GHashTable *positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1756 ptr = g_sequence_get_begin_iter (seq);
1757 while (!g_sequence_iter_is_end (ptr))
1759 g_hash_table_insert (positions, ptr,
1760 GINT_TO_POINTER (g_sequence_iter_get_position (ptr)));
1761 ptr = g_sequence_iter_next (ptr);
1768 generate_order (GSequence *seq,
1769 GHashTable *old_positions)
1772 int *order = g_new (int, g_sequence_get_length (seq));
1776 ptr = g_sequence_get_begin_iter (seq);
1777 while (!g_sequence_iter_is_end (ptr))
1779 int old_pos = GPOINTER_TO_INT (g_hash_table_lookup (old_positions, ptr));
1780 order[i++] = old_pos;
1781 ptr = g_sequence_iter_next (ptr);
1784 g_hash_table_destroy (old_positions);
1790 * gtk_list_store_swap:
1791 * @store: A #GtkListStore.
1792 * @a: A #GtkTreeIter.
1793 * @b: Another #GtkTreeIter.
1795 * Swaps @a and @b in @store. Note that this function only works with
1801 gtk_list_store_swap (GtkListStore *store,
1805 GtkListStorePrivate *priv;
1806 GHashTable *old_positions;
1810 g_return_if_fail (GTK_IS_LIST_STORE (store));
1811 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1812 g_return_if_fail (iter_is_valid (a, store));
1813 g_return_if_fail (iter_is_valid (b, store));
1817 if (a->user_data == b->user_data)
1820 old_positions = save_positions (priv->seq);
1822 g_sequence_swap (a->user_data, b->user_data);
1824 order = generate_order (priv->seq, old_positions);
1825 path = gtk_tree_path_new ();
1827 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1830 gtk_tree_path_free (path);
1835 gtk_list_store_move_to (GtkListStore *store,
1839 GtkListStorePrivate *priv = store->priv;
1840 GHashTable *old_positions;
1844 old_positions = save_positions (priv->seq);
1846 g_sequence_move (iter->user_data, g_sequence_get_iter_at_pos (priv->seq, new_pos));
1848 order = generate_order (priv->seq, old_positions);
1850 path = gtk_tree_path_new ();
1851 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1853 gtk_tree_path_free (path);
1858 * gtk_list_store_move_before:
1859 * @store: A #GtkListStore.
1860 * @iter: A #GtkTreeIter.
1861 * @position: (allow-none): A #GtkTreeIter, or %NULL.
1863 * Moves @iter in @store to the position before @position. Note that this
1864 * function only works with unsorted stores. If @position is %NULL, @iter
1865 * will be moved to the end of the list.
1870 gtk_list_store_move_before (GtkListStore *store,
1872 GtkTreeIter *position)
1876 g_return_if_fail (GTK_IS_LIST_STORE (store));
1877 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1878 g_return_if_fail (iter_is_valid (iter, store));
1880 g_return_if_fail (iter_is_valid (position, store));
1883 pos = g_sequence_iter_get_position (position->user_data);
1887 gtk_list_store_move_to (store, iter, pos);
1891 * gtk_list_store_move_after:
1892 * @store: A #GtkListStore.
1893 * @iter: A #GtkTreeIter.
1894 * @position: (allow-none): A #GtkTreeIter or %NULL.
1896 * Moves @iter in @store to the position after @position. Note that this
1897 * function only works with unsorted stores. If @position is %NULL, @iter
1898 * will be moved to the start of the list.
1903 gtk_list_store_move_after (GtkListStore *store,
1905 GtkTreeIter *position)
1909 g_return_if_fail (GTK_IS_LIST_STORE (store));
1910 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1911 g_return_if_fail (iter_is_valid (iter, store));
1913 g_return_if_fail (iter_is_valid (position, store));
1916 pos = g_sequence_iter_get_position (position->user_data) + 1;
1920 gtk_list_store_move_to (store, iter, pos);
1925 gtk_list_store_compare_func (GSequenceIter *a,
1929 GtkListStore *list_store = user_data;
1930 GtkListStorePrivate *priv = list_store->priv;
1934 GtkTreeIterCompareFunc func;
1937 if (priv->sort_column_id != -1)
1939 GtkTreeDataSortHeader *header;
1941 header = _gtk_tree_data_list_get_header (priv->sort_list,
1942 priv->sort_column_id);
1943 g_return_val_if_fail (header != NULL, 0);
1944 g_return_val_if_fail (header->func != NULL, 0);
1946 func = header->func;
1947 data = header->data;
1951 g_return_val_if_fail (priv->default_sort_func != NULL, 0);
1952 func = priv->default_sort_func;
1953 data = priv->default_sort_data;
1956 iter_a.stamp = priv->stamp;
1957 iter_a.user_data = (gpointer)a;
1958 iter_b.stamp = priv->stamp;
1959 iter_b.user_data = (gpointer)b;
1961 g_assert (iter_is_valid (&iter_a, list_store));
1962 g_assert (iter_is_valid (&iter_b, list_store));
1964 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1966 if (priv->order == GTK_SORT_DESCENDING)
1970 else if (retval < 0)
1978 gtk_list_store_sort (GtkListStore *list_store)
1980 GtkListStorePrivate *priv = list_store->priv;
1983 GHashTable *old_positions;
1985 if (!GTK_LIST_STORE_IS_SORTED (list_store) ||
1986 g_sequence_get_length (priv->seq) <= 1)
1989 old_positions = save_positions (priv->seq);
1991 g_sequence_sort_iter (priv->seq, gtk_list_store_compare_func, list_store);
1993 /* Let the world know about our new order */
1994 new_order = generate_order (priv->seq, old_positions);
1996 path = gtk_tree_path_new ();
1997 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1998 path, NULL, new_order);
1999 gtk_tree_path_free (path);
2004 iter_is_sorted (GtkListStore *list_store,
2009 if (!g_sequence_iter_is_begin (iter->user_data))
2011 cmp = g_sequence_iter_prev (iter->user_data);
2012 if (gtk_list_store_compare_func (cmp, iter->user_data, list_store) > 0)
2016 cmp = g_sequence_iter_next (iter->user_data);
2017 if (!g_sequence_iter_is_end (cmp))
2019 if (gtk_list_store_compare_func (iter->user_data, cmp, list_store) > 0)
2027 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
2032 GtkListStorePrivate *priv = list_store->priv;
2035 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2036 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
2037 gtk_tree_path_free (path);
2039 if (!iter_is_sorted (list_store, iter))
2041 GHashTable *old_positions;
2044 old_positions = save_positions (priv->seq);
2045 g_sequence_sort_changed_iter (iter->user_data,
2046 gtk_list_store_compare_func,
2048 order = generate_order (priv->seq, old_positions);
2049 path = gtk_tree_path_new ();
2050 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
2052 gtk_tree_path_free (path);
2058 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
2059 gint *sort_column_id,
2062 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2063 GtkListStorePrivate *priv = list_store->priv;
2066 * sort_column_id = priv->sort_column_id;
2068 * order = priv->order;
2070 if (priv->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
2071 priv->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
2078 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
2079 gint sort_column_id,
2082 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2083 GtkListStorePrivate *priv = list_store->priv;
2085 if ((priv->sort_column_id == sort_column_id) &&
2086 (priv->order == order))
2089 if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
2091 if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2093 GtkTreeDataSortHeader *header = NULL;
2095 header = _gtk_tree_data_list_get_header (priv->sort_list,
2098 /* We want to make sure that we have a function */
2099 g_return_if_fail (header != NULL);
2100 g_return_if_fail (header->func != NULL);
2104 g_return_if_fail (priv->default_sort_func != NULL);
2109 priv->sort_column_id = sort_column_id;
2110 priv->order = order;
2112 gtk_tree_sortable_sort_column_changed (sortable);
2114 gtk_list_store_sort (list_store);
2118 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
2119 gint sort_column_id,
2120 GtkTreeIterCompareFunc func,
2122 GDestroyNotify destroy)
2124 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2125 GtkListStorePrivate *priv = list_store->priv;
2127 priv->sort_list = _gtk_tree_data_list_set_header (priv->sort_list,
2129 func, data, destroy);
2131 if (priv->sort_column_id == sort_column_id)
2132 gtk_list_store_sort (list_store);
2136 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
2137 GtkTreeIterCompareFunc func,
2139 GDestroyNotify destroy)
2141 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2142 GtkListStorePrivate *priv = list_store->priv;
2144 if (priv->default_sort_destroy)
2146 GDestroyNotify d = priv->default_sort_destroy;
2148 priv->default_sort_destroy = NULL;
2149 d (priv->default_sort_data);
2152 priv->default_sort_func = func;
2153 priv->default_sort_data = data;
2154 priv->default_sort_destroy = destroy;
2156 if (priv->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
2157 gtk_list_store_sort (list_store);
2161 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
2163 GtkListStore *list_store = GTK_LIST_STORE (sortable);
2164 GtkListStorePrivate *priv = list_store->priv;
2166 return (priv->default_sort_func != NULL);
2171 * gtk_list_store_insert_with_values:
2172 * @list_store: A #GtkListStore
2173 * @iter: (out) (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL
2174 * @position: position to insert the new row
2175 * @...: pairs of column number and value, terminated with -1
2177 * Creates a new row at @position. @iter will be changed to point to this new
2178 * row. If @position is larger than the number of rows on the list, then the
2179 * new row will be appended to the list. The row will be filled with the
2180 * values given to this function.
2183 * <literal>gtk_list_store_insert_with_values (list_store, iter, position...)</literal>
2184 * has the same effect as calling
2186 * gtk_list_store_insert (list_store, iter, position);
2187 * gtk_list_store_set (list_store, iter, ...);
2189 * with the difference that the former will only emit a row_inserted signal,
2190 * while the latter will emit row_inserted, row_changed and, if the list store
2191 * is sorted, rows_reordered. Since emitting the rows_reordered signal
2192 * repeatedly can affect the performance of the program,
2193 * gtk_list_store_insert_with_values() should generally be preferred when
2194 * inserting rows in a sorted list store.
2199 gtk_list_store_insert_with_values (GtkListStore *list_store,
2204 GtkListStorePrivate *priv;
2208 GtkTreeIter tmp_iter;
2210 gboolean changed = FALSE;
2211 gboolean maybe_need_sort = FALSE;
2214 /* FIXME: refactor to reduce overlap with gtk_list_store_set() */
2215 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2217 priv = list_store->priv;
2222 priv->columns_dirty = TRUE;
2226 length = g_sequence_get_length (seq);
2227 if (position > length)
2230 ptr = g_sequence_get_iter_at_pos (seq, position);
2231 ptr = g_sequence_insert_before (ptr, NULL);
2233 iter->stamp = priv->stamp;
2234 iter->user_data = ptr;
2236 g_assert (iter_is_valid (iter, list_store));
2240 va_start (var_args, position);
2241 gtk_list_store_set_valist_internal (list_store, iter,
2242 &changed, &maybe_need_sort,
2246 /* Don't emit rows_reordered here */
2247 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2248 g_sequence_sort_changed_iter (iter->user_data,
2249 gtk_list_store_compare_func,
2252 /* Just emit row_inserted */
2253 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2254 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2255 gtk_tree_path_free (path);
2260 * gtk_list_store_insert_with_valuesv:
2261 * @list_store: A #GtkListStore
2262 * @iter: (out) (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL.
2263 * @position: position to insert the new row
2264 * @columns: (array length=n_values): an array of column numbers
2265 * @values: (array length=n_values): an array of GValues
2266 * @n_values: the length of the @columns and @values arrays
2268 * A variant of gtk_list_store_insert_with_values() which
2269 * takes the columns and values as two arrays, instead of
2270 * varargs. This function is mainly intended for
2271 * language-bindings.
2276 gtk_list_store_insert_with_valuesv (GtkListStore *list_store,
2283 GtkListStorePrivate *priv;
2287 GtkTreeIter tmp_iter;
2289 gboolean changed = FALSE;
2290 gboolean maybe_need_sort = FALSE;
2292 /* FIXME refactor to reduce overlap with
2293 * gtk_list_store_insert_with_values()
2295 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2297 priv = list_store->priv;
2302 priv->columns_dirty = TRUE;
2306 length = g_sequence_get_length (seq);
2307 if (position > length)
2310 ptr = g_sequence_get_iter_at_pos (seq, position);
2311 ptr = g_sequence_insert_before (ptr, NULL);
2313 iter->stamp = priv->stamp;
2314 iter->user_data = ptr;
2316 g_assert (iter_is_valid (iter, list_store));
2320 gtk_list_store_set_vector_internal (list_store, iter,
2321 &changed, &maybe_need_sort,
2322 columns, values, n_values);
2324 /* Don't emit rows_reordered here */
2325 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2326 g_sequence_sort_changed_iter (iter->user_data,
2327 gtk_list_store_compare_func,
2330 /* Just emit row_inserted */
2331 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2332 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2333 gtk_tree_path_free (path);
2336 /* GtkBuildable custom tag implementation
2339 * <column type="..."/>
2340 * <column type="..."/>
2344 gboolean translatable;
2350 GtkBuilder *builder;
2352 GSList *column_type_names;
2353 GType *column_types;
2362 const gchar *domain;
2366 list_store_start_element (GMarkupParseContext *context,
2367 const gchar *element_name,
2368 const gchar **names,
2369 const gchar **values,
2374 SubParserData *data = (SubParserData*)user_data;
2376 if (strcmp (element_name, "col") == 0)
2379 gchar *context = NULL;
2380 gboolean translatable = FALSE;
2383 if (data->row_column >= data->n_columns)
2385 g_set_error (error, data->error_quark, 0,
2386 "Too many columns, maximum is %d\n", data->n_columns - 1);
2390 for (i = 0; names[i]; i++)
2391 if (strcmp (names[i], "id") == 0)
2394 id = atoi (values[i]);
2397 g_set_error (error, data->error_quark, 0,
2398 "the id tag %s could not be converted to an integer",
2402 if (id < 0 || id >= data->n_columns)
2404 g_set_error (error, data->error_quark, 0,
2405 "id value %d out of range", id);
2409 else if (strcmp (names[i], "translatable") == 0)
2411 if (!_gtk_builder_boolean_from_string (values[i], &translatable,
2415 else if (strcmp (names[i], "comments") == 0)
2417 /* do nothing, comments are for translators */
2419 else if (strcmp (names[i], "context") == 0)
2421 context = g_strdup (values[i]);
2426 g_set_error (error, data->error_quark, 0,
2427 "<col> needs an id attribute");
2431 info = g_slice_new0 (ColInfo);
2432 info->translatable = translatable;
2433 info->context = context;
2436 data->colids[data->row_column] = id;
2437 data->columns[data->row_column] = info;
2439 data->is_data = TRUE;
2441 else if (strcmp (element_name, "row") == 0)
2443 else if (strcmp (element_name, "column") == 0)
2444 for (i = 0; names[i]; i++)
2445 if (strcmp (names[i], "type") == 0)
2446 data->column_type_names = g_slist_prepend (data->column_type_names,
2447 g_strdup (values[i]));
2448 else if (strcmp (element_name, "columns") == 0)
2450 else if (strcmp (element_name, "data") == 0)
2453 g_set_error (error, data->error_quark, 0,
2454 "Unknown start tag: %s", element_name);
2458 list_store_end_element (GMarkupParseContext *context,
2459 const gchar *element_name,
2463 SubParserData *data = (SubParserData*)user_data;
2465 g_assert (data->builder);
2467 if (strcmp (element_name, "row") == 0)
2472 gtk_list_store_insert_with_valuesv (GTK_LIST_STORE (data->object),
2478 for (i = 0; i < data->row_column; i++)
2480 ColInfo *info = data->columns[i];
2481 g_free (info->context);
2482 g_slice_free (ColInfo, info);
2483 data->columns[i] = NULL;
2484 g_value_unset (&data->values[i]);
2486 g_free (data->values);
2487 data->values = g_new0 (GValue, data->n_columns);
2489 data->row_column = 0;
2491 else if (strcmp (element_name, "columns") == 0)
2493 GType *column_types;
2498 data->column_type_names = g_slist_reverse (data->column_type_names);
2499 column_types = g_new0 (GType, g_slist_length (data->column_type_names));
2501 for (l = data->column_type_names, i = 0; l; l = l->next, i++)
2503 type = gtk_builder_get_type_from_name (data->builder, l->data);
2504 if (type == G_TYPE_INVALID)
2506 g_warning ("Unknown type %s specified in treemodel %s",
2507 (const gchar*)l->data,
2508 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
2511 column_types[i] = type;
2516 gtk_list_store_set_column_types (GTK_LIST_STORE (data->object), i,
2519 g_free (column_types);
2521 else if (strcmp (element_name, "col") == 0)
2522 data->is_data = FALSE;
2523 else if (strcmp (element_name, "data") == 0)
2525 else if (strcmp (element_name, "column") == 0)
2528 g_set_error (error, data->error_quark, 0,
2529 "Unknown end tag: %s", element_name);
2533 list_store_text (GMarkupParseContext *context,
2539 SubParserData *data = (SubParserData*)user_data;
2541 GError *tmp_error = NULL;
2548 i = data->row_column - 1;
2549 info = data->columns[i];
2551 string = g_strndup (text, text_len);
2552 if (info->translatable && text_len)
2556 /* FIXME: This will not use the domain set in the .ui file,
2557 * since the parser is not telling the builder about the domain.
2558 * However, it will work for gtk_builder_set_translation_domain() calls.
2560 translated = _gtk_builder_parser_translate (data->domain,
2564 string = translated;
2567 if (!gtk_builder_value_from_string_type (data->builder,
2568 data->column_types[info->id],
2576 "Could not convert '%s' to type %s: %s\n",
2577 text, g_type_name (data->column_types[info->id]),
2578 tmp_error->message);
2579 g_error_free (tmp_error);
2584 static const GMarkupParser list_store_parser =
2586 list_store_start_element,
2587 list_store_end_element,
2592 gtk_list_store_buildable_custom_tag_start (GtkBuildable *buildable,
2593 GtkBuilder *builder,
2595 const gchar *tagname,
2596 GMarkupParser *parser,
2599 SubParserData *parser_data;
2604 if (strcmp (tagname, "columns") == 0)
2607 parser_data = g_slice_new0 (SubParserData);
2608 parser_data->builder = builder;
2609 parser_data->object = G_OBJECT (buildable);
2610 parser_data->column_type_names = NULL;
2612 *parser = list_store_parser;
2613 *data = parser_data;
2616 else if (strcmp (tagname, "data") == 0)
2618 gint n_columns = gtk_list_store_get_n_columns (GTK_TREE_MODEL (buildable));
2620 g_error ("Cannot append data to an empty model");
2622 parser_data = g_slice_new0 (SubParserData);
2623 parser_data->builder = builder;
2624 parser_data->object = G_OBJECT (buildable);
2625 parser_data->values = g_new0 (GValue, n_columns);
2626 parser_data->colids = g_new0 (gint, n_columns);
2627 parser_data->columns = g_new0 (ColInfo*, n_columns);
2628 parser_data->column_types = GTK_LIST_STORE (buildable)->priv->column_headers;
2629 parser_data->n_columns = n_columns;
2630 parser_data->last_row = 0;
2631 parser_data->error_quark = g_quark_from_static_string ("GtkListStore");
2632 parser_data->domain = gtk_builder_get_translation_domain (builder);
2634 *parser = list_store_parser;
2635 *data = parser_data;
2639 g_warning ("Unknown custom list store tag: %s", tagname);
2645 gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
2646 GtkBuilder *builder,
2648 const gchar *tagname,
2651 SubParserData *sub = (SubParserData*)data;
2653 if (strcmp (tagname, "columns") == 0)
2655 g_slist_free (sub->column_type_names);
2656 g_slice_free (SubParserData, sub);
2658 else if (strcmp (tagname, "data") == 0)
2661 for (i = 0; i < sub->n_columns; i++)
2663 ColInfo *info = sub->columns[i];
2666 g_free (info->context);
2667 g_slice_free (ColInfo, info);
2670 g_free (sub->colids);
2671 g_free (sub->columns);
2672 g_free (sub->values);
2673 g_slice_free (SubParserData, sub);
2676 g_warning ("Unknown custom list store tag: %s", tagname);