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"
34 struct _GtkListStorePrivate
37 GtkTreeIterCompareFunc default_sort_func;
39 GDestroyNotify default_sort_destroy;
41 GType *column_headers;
48 guint columns_dirty : 1;
50 gpointer default_sort_data;
51 gpointer seq; /* head of the list */
54 #define GTK_LIST_STORE_IS_SORTED(list) (((GtkListStore*)(list))->priv->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
55 #define VALID_ITER(iter, list_store) ((iter)!= NULL && (iter)->user_data != NULL && list_store->priv->stamp == (iter)->stamp && !g_sequence_iter_is_end ((iter)->user_data) && g_sequence_iter_get_sequence ((iter)->user_data) == list_store->priv->seq)
57 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
58 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
59 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
60 static void gtk_list_store_sortable_init (GtkTreeSortableIface *iface);
61 static void gtk_list_store_buildable_init (GtkBuildableIface *iface);
62 static void gtk_list_store_finalize (GObject *object);
63 static GtkTreeModelFlags gtk_list_store_get_flags (GtkTreeModel *tree_model);
64 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
65 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
67 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
70 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
72 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
76 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
78 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
81 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
83 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
85 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
89 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
94 static void gtk_list_store_set_n_columns (GtkListStore *list_store,
96 static void gtk_list_store_set_column_type (GtkListStore *list_store,
100 static void gtk_list_store_increment_stamp (GtkListStore *list_store);
104 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
106 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
108 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
110 GtkSelectionData *selection_data);
111 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
113 GtkSelectionData *selection_data);
114 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
115 GtkTreePath *dest_path,
116 GtkSelectionData *selection_data);
120 static void gtk_list_store_sort (GtkListStore *list_store);
121 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
124 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
125 gint *sort_column_id,
127 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
130 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
132 GtkTreeIterCompareFunc func,
134 GDestroyNotify destroy);
135 static void gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
136 GtkTreeIterCompareFunc func,
138 GDestroyNotify destroy);
139 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable);
143 static gboolean gtk_list_store_buildable_custom_tag_start (GtkBuildable *buildable,
146 const gchar *tagname,
147 GMarkupParser *parser,
149 static void gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
152 const gchar *tagname,
155 G_DEFINE_TYPE_WITH_CODE (GtkListStore, gtk_list_store, G_TYPE_OBJECT,
156 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
157 gtk_list_store_tree_model_init)
158 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
159 gtk_list_store_drag_source_init)
160 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
161 gtk_list_store_drag_dest_init)
162 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
163 gtk_list_store_sortable_init)
164 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
165 gtk_list_store_buildable_init))
169 gtk_list_store_class_init (GtkListStoreClass *class)
171 GObjectClass *object_class;
173 object_class = (GObjectClass*) class;
175 object_class->finalize = gtk_list_store_finalize;
177 g_type_class_add_private (class, sizeof (GtkListStorePrivate));
181 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
183 iface->get_flags = gtk_list_store_get_flags;
184 iface->get_n_columns = gtk_list_store_get_n_columns;
185 iface->get_column_type = gtk_list_store_get_column_type;
186 iface->get_iter = gtk_list_store_get_iter;
187 iface->get_path = gtk_list_store_get_path;
188 iface->get_value = gtk_list_store_get_value;
189 iface->iter_next = gtk_list_store_iter_next;
190 iface->iter_children = gtk_list_store_iter_children;
191 iface->iter_has_child = gtk_list_store_iter_has_child;
192 iface->iter_n_children = gtk_list_store_iter_n_children;
193 iface->iter_nth_child = gtk_list_store_iter_nth_child;
194 iface->iter_parent = gtk_list_store_iter_parent;
198 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
200 iface->row_draggable = real_gtk_list_store_row_draggable;
201 iface->drag_data_delete = gtk_list_store_drag_data_delete;
202 iface->drag_data_get = gtk_list_store_drag_data_get;
206 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
208 iface->drag_data_received = gtk_list_store_drag_data_received;
209 iface->row_drop_possible = gtk_list_store_row_drop_possible;
213 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
215 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
216 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
217 iface->set_sort_func = gtk_list_store_set_sort_func;
218 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
219 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
223 gtk_list_store_buildable_init (GtkBuildableIface *iface)
225 iface->custom_tag_start = gtk_list_store_buildable_custom_tag_start;
226 iface->custom_tag_end = gtk_list_store_buildable_custom_tag_end;
230 gtk_list_store_init (GtkListStore *list_store)
232 GtkListStorePrivate *priv;
234 list_store->priv = G_TYPE_INSTANCE_GET_PRIVATE (list_store,
236 GtkListStorePrivate);
237 priv = list_store->priv;
239 priv->seq = g_sequence_new (NULL);
240 priv->sort_list = NULL;
241 priv->stamp = g_random_int ();
242 priv->sort_column_id = GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID;
243 priv->columns_dirty = FALSE;
248 * gtk_list_store_new:
249 * @n_columns: number of columns in the list store
250 * @Varargs: all #GType types for the columns, from first to last
252 * Creates a new list store as with @n_columns columns each of the types passed
253 * in. Note that only types derived from standard GObject fundamental types
256 * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
257 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
258 * int, string and #GdkPixbuf respectively.
260 * Return value: a new #GtkListStore
263 gtk_list_store_new (gint n_columns,
266 GtkListStore *retval;
270 g_return_val_if_fail (n_columns > 0, NULL);
272 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
273 gtk_list_store_set_n_columns (retval, n_columns);
275 va_start (args, n_columns);
277 for (i = 0; i < n_columns; i++)
279 GType type = va_arg (args, GType);
280 if (! _gtk_tree_data_list_check_type (type))
282 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
283 g_object_unref (retval);
287 gtk_list_store_set_column_type (retval, i, type);
297 * gtk_list_store_newv:
298 * @n_columns: number of columns in the list store
299 * @types: (array length=n_columns): an array of #GType types for the columns, from first to last
301 * Non-vararg creation function. Used primarily by language bindings.
303 * Return value: (transfer none): a new #GtkListStore
306 gtk_list_store_newv (gint n_columns,
309 GtkListStore *retval;
312 g_return_val_if_fail (n_columns > 0, NULL);
314 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
315 gtk_list_store_set_n_columns (retval, n_columns);
317 for (i = 0; i < n_columns; i++)
319 if (! _gtk_tree_data_list_check_type (types[i]))
321 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
322 g_object_unref (retval);
326 gtk_list_store_set_column_type (retval, i, types[i]);
333 * gtk_list_store_set_column_types:
334 * @list_store: A #GtkListStore
335 * @n_columns: Number of columns for the list store
336 * @types: (array length=n_columns): An array length n of #GTypes
338 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
339 * and should only be used when constructing a new #GtkListStore. It will not
340 * function after a row has been added, or a method on the #GtkTreeModel
341 * interface is called.
344 gtk_list_store_set_column_types (GtkListStore *list_store,
348 GtkListStorePrivate *priv;
351 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
353 priv = list_store->priv;
355 g_return_if_fail (priv->columns_dirty == 0);
357 gtk_list_store_set_n_columns (list_store, n_columns);
358 for (i = 0; i < n_columns; i++)
360 if (! _gtk_tree_data_list_check_type (types[i]))
362 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
365 gtk_list_store_set_column_type (list_store, i, types[i]);
370 gtk_list_store_set_n_columns (GtkListStore *list_store,
373 GtkListStorePrivate *priv = list_store->priv;
376 if (priv->n_columns == n_columns)
379 priv->column_headers = g_renew (GType, priv->column_headers, n_columns);
380 for (i = priv->n_columns; i < n_columns; i++)
381 priv->column_headers[i] = G_TYPE_INVALID;
382 priv->n_columns = n_columns;
385 _gtk_tree_data_list_header_free (priv->sort_list);
386 priv->sort_list = _gtk_tree_data_list_header_new (n_columns, priv->column_headers);
390 gtk_list_store_set_column_type (GtkListStore *list_store,
394 GtkListStorePrivate *priv = list_store->priv;
396 if (!_gtk_tree_data_list_check_type (type))
398 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
402 priv->column_headers[column] = type;
406 gtk_list_store_finalize (GObject *object)
408 GtkListStore *list_store = GTK_LIST_STORE (object);
409 GtkListStorePrivate *priv = list_store->priv;
411 g_sequence_foreach (priv->seq,
412 (GFunc) _gtk_tree_data_list_free, priv->column_headers);
414 g_sequence_free (priv->seq);
416 _gtk_tree_data_list_header_free (priv->sort_list);
417 g_free (priv->column_headers);
419 if (priv->default_sort_destroy)
421 GDestroyNotify d = priv->default_sort_destroy;
423 priv->default_sort_destroy = NULL;
424 d (priv->default_sort_data);
425 priv->default_sort_data = NULL;
428 G_OBJECT_CLASS (gtk_list_store_parent_class)->finalize (object);
431 /* Fulfill the GtkTreeModel requirements */
432 static GtkTreeModelFlags
433 gtk_list_store_get_flags (GtkTreeModel *tree_model)
435 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
439 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
441 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
442 GtkListStorePrivate *priv = list_store->priv;
444 priv->columns_dirty = TRUE;
446 return priv->n_columns;
450 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
453 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
454 GtkListStorePrivate *priv = list_store->priv;
456 g_return_val_if_fail (index < priv->n_columns, G_TYPE_INVALID);
458 priv->columns_dirty = TRUE;
460 return priv->column_headers[index];
464 gtk_list_store_get_iter (GtkTreeModel *tree_model,
468 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
469 GtkListStorePrivate *priv = list_store->priv;
473 priv->columns_dirty = TRUE;
477 i = gtk_tree_path_get_indices (path)[0];
479 if (i >= g_sequence_get_length (seq))
482 iter->stamp = priv->stamp;
483 iter->user_data = g_sequence_get_iter_at_pos (seq, i);
489 gtk_list_store_get_path (GtkTreeModel *tree_model,
492 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
493 GtkListStorePrivate *priv = list_store->priv;
496 g_return_val_if_fail (iter->stamp == priv->stamp, NULL);
498 if (g_sequence_iter_is_end (iter->user_data))
501 path = gtk_tree_path_new ();
502 gtk_tree_path_append_index (path, g_sequence_iter_get_position (iter->user_data));
508 gtk_list_store_get_value (GtkTreeModel *tree_model,
513 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
514 GtkListStorePrivate *priv = list_store->priv;
515 GtkTreeDataList *list;
516 gint tmp_column = column;
518 g_return_if_fail (column < priv->n_columns);
519 g_return_if_fail (VALID_ITER (iter, list_store));
521 list = g_sequence_get (iter->user_data);
523 while (tmp_column-- > 0 && list)
527 g_value_init (value, priv->column_headers[column]);
529 _gtk_tree_data_list_node_to_value (list,
530 priv->column_headers[column],
535 gtk_list_store_iter_next (GtkTreeModel *tree_model,
538 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
539 GtkListStorePrivate *priv = list_store->priv;
542 g_return_val_if_fail (priv->stamp == iter->stamp, FALSE);
543 iter->user_data = g_sequence_iter_next (iter->user_data);
545 retval = g_sequence_iter_is_end (iter->user_data);
553 gtk_list_store_iter_children (GtkTreeModel *tree_model,
557 GtkListStore *list_store = (GtkListStore *) tree_model;
558 GtkListStorePrivate *priv = list_store->priv;
560 /* this is a list, nodes have no children */
567 if (g_sequence_get_length (priv->seq) > 0)
569 iter->stamp = priv->stamp;
570 iter->user_data = g_sequence_get_begin_iter (priv->seq);
581 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
588 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
591 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
592 GtkListStorePrivate *priv = list_store->priv;
595 return g_sequence_get_length (priv->seq);
597 g_return_val_if_fail (priv->stamp == iter->stamp, -1);
603 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
608 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
609 GtkListStorePrivate *priv = list_store->priv;
610 GSequenceIter *child;
617 child = g_sequence_get_iter_at_pos (priv->seq, n);
619 if (g_sequence_iter_is_end (child))
622 iter->stamp = priv->stamp;
623 iter->user_data = child;
629 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
638 gtk_list_store_real_set_value (GtkListStore *list_store,
644 GtkListStorePrivate *priv = list_store->priv;
645 GtkTreeDataList *list;
646 GtkTreeDataList *prev;
647 gint old_column = column;
648 GValue real_value = {0, };
649 gboolean converted = FALSE;
650 gboolean retval = FALSE;
652 if (! g_type_is_a (G_VALUE_TYPE (value), priv->column_headers[column]))
654 if (! (g_value_type_compatible (G_VALUE_TYPE (value), priv->column_headers[column]) &&
655 g_value_type_compatible (priv->column_headers[column], G_VALUE_TYPE (value))))
657 g_warning ("%s: Unable to convert from %s to %s\n",
659 g_type_name (G_VALUE_TYPE (value)),
660 g_type_name (priv->column_headers[column]));
663 if (!g_value_transform (value, &real_value))
665 g_warning ("%s: Unable to make conversion from %s to %s\n",
667 g_type_name (G_VALUE_TYPE (value)),
668 g_type_name (priv->column_headers[column]));
669 g_value_unset (&real_value);
675 prev = list = g_sequence_get (iter->user_data);
682 _gtk_tree_data_list_value_to_node (list, &real_value);
684 _gtk_tree_data_list_value_to_node (list, value);
687 g_value_unset (&real_value);
688 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
689 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
698 if (g_sequence_get (iter->user_data) == NULL)
700 list = _gtk_tree_data_list_alloc();
701 g_sequence_set (iter->user_data, list);
706 list = prev->next = _gtk_tree_data_list_alloc ();
712 list->next = _gtk_tree_data_list_alloc ();
719 _gtk_tree_data_list_value_to_node (list, &real_value);
721 _gtk_tree_data_list_value_to_node (list, value);
725 g_value_unset (&real_value);
727 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
728 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
735 * gtk_list_store_set_value:
736 * @list_store: A #GtkListStore
737 * @iter: A valid #GtkTreeIter for the row being modified
738 * @column: column number to modify
739 * @value: new value for the cell
741 * Sets the data in the cell specified by @iter and @column.
742 * The type of @value must be convertible to the type of the
747 gtk_list_store_set_value (GtkListStore *list_store,
752 GtkListStorePrivate *priv = list_store->priv;
754 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
755 g_return_if_fail (VALID_ITER (iter, list_store));
756 g_return_if_fail (G_IS_VALUE (value));
757 priv = list_store->priv;
758 g_return_if_fail (column >= 0 && column < priv->n_columns);
760 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
764 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
765 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
766 gtk_tree_path_free (path);
770 static GtkTreeIterCompareFunc
771 gtk_list_store_get_compare_func (GtkListStore *list_store)
773 GtkListStorePrivate *priv = list_store->priv;
774 GtkTreeIterCompareFunc func = NULL;
776 if (GTK_LIST_STORE_IS_SORTED (list_store))
778 if (priv->sort_column_id != -1)
780 GtkTreeDataSortHeader *header;
781 header = _gtk_tree_data_list_get_header (priv->sort_list,
782 priv->sort_column_id);
783 g_return_val_if_fail (header != NULL, NULL);
784 g_return_val_if_fail (header->func != NULL, NULL);
789 func = priv->default_sort_func;
797 gtk_list_store_set_vector_internal (GtkListStore *list_store,
799 gboolean *emit_signal,
800 gboolean *maybe_need_sort,
805 GtkListStorePrivate *priv = list_store->priv;
807 GtkTreeIterCompareFunc func = NULL;
809 func = gtk_list_store_get_compare_func (list_store);
810 if (func != _gtk_tree_data_list_compare_func)
811 *maybe_need_sort = TRUE;
813 for (i = 0; i < n_values; i++)
815 *emit_signal = gtk_list_store_real_set_value (list_store,
819 FALSE) || *emit_signal;
821 if (func == _gtk_tree_data_list_compare_func &&
822 columns[i] == priv->sort_column_id)
823 *maybe_need_sort = TRUE;
828 gtk_list_store_set_valist_internal (GtkListStore *list_store,
830 gboolean *emit_signal,
831 gboolean *maybe_need_sort,
834 GtkListStorePrivate *priv = list_store->priv;
836 GtkTreeIterCompareFunc func = NULL;
838 column = va_arg (var_args, gint);
840 func = gtk_list_store_get_compare_func (list_store);
841 if (func != _gtk_tree_data_list_compare_func)
842 *maybe_need_sort = TRUE;
846 GValue value = { 0, };
849 if (column < 0 || column >= priv->n_columns)
851 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
854 g_value_init (&value, priv->column_headers[column]);
856 G_VALUE_COLLECT (&value, var_args, 0, &error);
859 g_warning ("%s: %s", G_STRLOC, error);
862 /* we purposely leak the value here, it might not be
863 * in a sane state if an error condition occoured
868 /* FIXME: instead of calling this n times, refactor with above */
869 *emit_signal = gtk_list_store_real_set_value (list_store,
873 FALSE) || *emit_signal;
875 if (func == _gtk_tree_data_list_compare_func &&
876 column == priv->sort_column_id)
877 *maybe_need_sort = TRUE;
879 g_value_unset (&value);
881 column = va_arg (var_args, gint);
886 * gtk_list_store_set_valuesv:
887 * @list_store: A #GtkListStore
888 * @iter: A valid #GtkTreeIter for the row being modified
889 * @columns: (array length=n_values): an array of column numbers
890 * @values: (array length=n_values): an array of GValues
891 * @n_values: the length of the @columns and @values arrays
893 * A variant of gtk_list_store_set_valist() which
894 * takes the columns and values as two arrays, instead of
895 * varargs. This function is mainly intended for
896 * language-bindings and in case the number of columns to
897 * change is not known until run-time.
902 gtk_list_store_set_valuesv (GtkListStore *list_store,
908 GtkListStorePrivate *priv;
909 gboolean emit_signal = FALSE;
910 gboolean maybe_need_sort = FALSE;
912 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
913 g_return_if_fail (VALID_ITER (iter, list_store));
915 priv = list_store->priv;
917 gtk_list_store_set_vector_internal (list_store, iter,
920 columns, values, n_values);
922 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
923 gtk_list_store_sort_iter_changed (list_store, iter, priv->sort_column_id);
929 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
930 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
931 gtk_tree_path_free (path);
936 * gtk_list_store_set_valist:
937 * @list_store: A #GtkListStore
938 * @iter: A valid #GtkTreeIter for the row being modified
939 * @var_args: va_list of column/value pairs
941 * See gtk_list_store_set(); this version takes a va_list for use by language
946 gtk_list_store_set_valist (GtkListStore *list_store,
950 GtkListStorePrivate *priv;
951 gboolean emit_signal = FALSE;
952 gboolean maybe_need_sort = FALSE;
954 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
955 g_return_if_fail (VALID_ITER (iter, list_store));
957 priv = list_store->priv;
959 gtk_list_store_set_valist_internal (list_store, iter,
964 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
965 gtk_list_store_sort_iter_changed (list_store, iter, priv->sort_column_id);
971 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
972 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
973 gtk_tree_path_free (path);
978 * gtk_list_store_set:
979 * @list_store: a #GtkListStore
980 * @iter: row iterator
981 * @Varargs: pairs of column number and value, terminated with -1
983 * Sets the value of one or more cells in the row referenced by @iter.
984 * The variable argument list should contain integer column numbers,
985 * each column number followed by the value to be set.
986 * The list is terminated by a -1. For example, to set column 0 with type
987 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
988 * 0, "Foo", -1)</literal>.
990 * The value will be referenced by the store if it is a %G_TYPE_OBJECT, and it
991 * will be copied if it is a %G_TYPE_STRING or %G_TYPE_BOXED.
994 gtk_list_store_set (GtkListStore *list_store,
1000 va_start (var_args, iter);
1001 gtk_list_store_set_valist (list_store, iter, var_args);
1006 * gtk_list_store_remove:
1007 * @list_store: A #GtkListStore
1008 * @iter: A valid #GtkTreeIter
1010 * Removes the given row from the list store. After being removed,
1011 * @iter is set to be the next valid row, or invalidated if it pointed
1012 * to the last row in @list_store.
1014 * Return value: %TRUE if @iter is valid, %FALSE if not.
1017 gtk_list_store_remove (GtkListStore *list_store,
1020 GtkListStorePrivate *priv;
1022 GSequenceIter *ptr, *next;
1024 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1025 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
1027 priv = list_store->priv;
1029 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1031 ptr = iter->user_data;
1032 next = g_sequence_iter_next (ptr);
1034 _gtk_tree_data_list_free (g_sequence_get (ptr), priv->column_headers);
1035 g_sequence_remove (iter->user_data);
1039 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
1040 gtk_tree_path_free (path);
1042 if (g_sequence_iter_is_end (next))
1049 iter->stamp = priv->stamp;
1050 iter->user_data = next;
1056 * gtk_list_store_insert:
1057 * @list_store: A #GtkListStore
1058 * @iter: (out): An unset #GtkTreeIter to set to the new row
1059 * @position: position to insert the new row
1061 * Creates a new row at @position. @iter will be changed to point to this new
1062 * row. If @position is larger than the number of rows on the list, then the
1063 * new row will be appended to the list. The row will be empty after this
1064 * function is called. To fill in values, you need to call
1065 * gtk_list_store_set() or gtk_list_store_set_value().
1069 gtk_list_store_insert (GtkListStore *list_store,
1073 GtkListStorePrivate *priv;
1079 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1080 g_return_if_fail (iter != NULL);
1081 g_return_if_fail (position >= 0);
1083 priv = list_store->priv;
1085 priv->columns_dirty = TRUE;
1089 length = g_sequence_get_length (seq);
1090 if (position > length)
1093 ptr = g_sequence_get_iter_at_pos (seq, position);
1094 ptr = g_sequence_insert_before (ptr, NULL);
1096 iter->stamp = priv->stamp;
1097 iter->user_data = ptr;
1099 g_assert (VALID_ITER (iter, list_store));
1103 path = gtk_tree_path_new ();
1104 gtk_tree_path_append_index (path, position);
1105 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1106 gtk_tree_path_free (path);
1110 * gtk_list_store_insert_before:
1111 * @list_store: A #GtkListStore
1112 * @iter: (out): An unset #GtkTreeIter to set to the new row
1113 * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1115 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will
1116 * be appended to the end of the list. @iter will be changed to point to this
1117 * new row. The row will be empty after this function is called. To fill in
1118 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1122 gtk_list_store_insert_before (GtkListStore *list_store,
1124 GtkTreeIter *sibling)
1126 GtkListStorePrivate *priv;
1127 GSequenceIter *after;
1129 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1130 g_return_if_fail (iter != NULL);
1132 priv = list_store->priv;
1135 g_return_if_fail (VALID_ITER (sibling, list_store));
1138 after = g_sequence_get_end_iter (priv->seq);
1140 after = sibling->user_data;
1142 gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1146 * gtk_list_store_insert_after:
1147 * @list_store: A #GtkListStore
1148 * @iter: (out): An unset #GtkTreeIter to set to the new row
1149 * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1151 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1152 * prepended to the beginning of the list. @iter will be changed to point to
1153 * this new row. The row will be empty after this function is called. To fill
1154 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1158 gtk_list_store_insert_after (GtkListStore *list_store,
1160 GtkTreeIter *sibling)
1162 GtkListStorePrivate *priv;
1163 GSequenceIter *after;
1165 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1166 g_return_if_fail (iter != NULL);
1168 priv = list_store->priv;
1171 g_return_if_fail (VALID_ITER (sibling, list_store));
1174 after = g_sequence_get_begin_iter (priv->seq);
1176 after = g_sequence_iter_next (sibling->user_data);
1178 gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1182 * gtk_list_store_prepend:
1183 * @list_store: A #GtkListStore
1184 * @iter: (out): An unset #GtkTreeIter to set to the prepend row
1186 * Prepends a new row to @list_store. @iter will be changed to point to this new
1187 * row. The row will be empty after this function is called. To fill in
1188 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1192 gtk_list_store_prepend (GtkListStore *list_store,
1195 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1196 g_return_if_fail (iter != NULL);
1198 gtk_list_store_insert (list_store, iter, 0);
1202 * gtk_list_store_append:
1203 * @list_store: A #GtkListStore
1204 * @iter: (out): An unset #GtkTreeIter to set to the appended row
1206 * Appends a new row to @list_store. @iter will be changed to point to this new
1207 * row. The row will be empty after this function is called. To fill in
1208 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1212 gtk_list_store_append (GtkListStore *list_store,
1215 GtkListStorePrivate *priv;
1217 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1218 g_return_if_fail (iter != NULL);
1220 priv = list_store->priv;
1222 gtk_list_store_insert (list_store, iter, g_sequence_get_length (priv->seq));
1226 gtk_list_store_increment_stamp (GtkListStore *list_store)
1228 GtkListStorePrivate *priv = list_store->priv;
1234 while (priv->stamp == 0);
1238 * gtk_list_store_clear:
1239 * @list_store: a #GtkListStore.
1241 * Removes all rows from the list store.
1245 gtk_list_store_clear (GtkListStore *list_store)
1247 GtkListStorePrivate *priv;
1250 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1252 priv = list_store->priv;
1254 while (g_sequence_get_length (priv->seq) > 0)
1256 iter.stamp = priv->stamp;
1257 iter.user_data = g_sequence_get_begin_iter (priv->seq);
1258 gtk_list_store_remove (list_store, &iter);
1261 gtk_list_store_increment_stamp (list_store);
1265 * gtk_list_store_iter_is_valid:
1266 * @list_store: A #GtkListStore.
1267 * @iter: A #GtkTreeIter.
1269 * <warning>This function is slow. Only use it for debugging and/or testing
1270 * purposes.</warning>
1272 * Checks if the given iter is a valid iter for this #GtkListStore.
1274 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1279 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1282 GtkListStorePrivate *priv;
1284 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1285 g_return_val_if_fail (iter != NULL, FALSE);
1287 priv = list_store->priv;
1289 if (!VALID_ITER (iter, list_store))
1292 if (g_sequence_iter_get_sequence (iter->user_data) != priv->seq)
1298 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
1305 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1310 if (gtk_list_store_get_iter (GTK_TREE_MODEL (drag_source),
1314 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1321 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1323 GtkSelectionData *selection_data)
1325 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1326 * target, because the default handler does it for us, but
1327 * we do anyway for the convenience of someone maybe overriding the
1331 if (gtk_tree_set_row_drag_data (selection_data,
1332 GTK_TREE_MODEL (drag_source),
1339 /* FIXME handle text targets at least. */
1346 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1348 GtkSelectionData *selection_data)
1350 GtkTreeModel *tree_model = GTK_TREE_MODEL (drag_dest);
1351 GtkListStore *list_store = GTK_LIST_STORE (tree_model);
1352 GtkListStorePrivate *priv = list_store->priv;
1353 GtkTreeModel *src_model = NULL;
1354 GtkTreePath *src_path = NULL;
1355 gboolean retval = FALSE;
1357 if (gtk_tree_get_row_drag_data (selection_data,
1360 src_model == tree_model)
1362 /* Copy the given row to a new position */
1363 GtkTreeIter src_iter;
1364 GtkTreeIter dest_iter;
1367 if (!gtk_list_store_get_iter (src_model,
1374 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1375 prev = gtk_tree_path_copy (dest);
1377 if (!gtk_tree_path_prev (prev))
1379 /* dest was the first spot in the list; which means we are supposed
1382 gtk_list_store_prepend (list_store, &dest_iter);
1388 if (gtk_list_store_get_iter (tree_model, &dest_iter, prev))
1390 GtkTreeIter tmp_iter = dest_iter;
1392 gtk_list_store_insert_after (list_store, &dest_iter, &tmp_iter);
1398 gtk_tree_path_free (prev);
1400 /* If we succeeded in creating dest_iter, copy data from src
1404 GtkTreeDataList *dl = g_sequence_get (src_iter.user_data);
1405 GtkTreeDataList *copy_head = NULL;
1406 GtkTreeDataList *copy_prev = NULL;
1407 GtkTreeDataList *copy_iter = NULL;
1414 copy_iter = _gtk_tree_data_list_node_copy (dl,
1415 priv->column_headers[col]);
1417 if (copy_head == NULL)
1418 copy_head = copy_iter;
1421 copy_prev->next = copy_iter;
1423 copy_prev = copy_iter;
1429 dest_iter.stamp = priv->stamp;
1430 g_sequence_set (dest_iter.user_data, copy_head);
1432 path = gtk_list_store_get_path (tree_model, &dest_iter);
1433 gtk_tree_model_row_changed (tree_model, path, &dest_iter);
1434 gtk_tree_path_free (path);
1439 /* FIXME maybe add some data targets eventually, or handle text
1440 * targets in the simple case.
1447 gtk_tree_path_free (src_path);
1453 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1454 GtkTreePath *dest_path,
1455 GtkSelectionData *selection_data)
1458 GtkTreeModel *src_model = NULL;
1459 GtkTreePath *src_path = NULL;
1460 gboolean retval = FALSE;
1462 /* don't accept drops if the list has been sorted */
1463 if (GTK_LIST_STORE_IS_SORTED (drag_dest))
1466 if (!gtk_tree_get_row_drag_data (selection_data,
1471 if (src_model != GTK_TREE_MODEL (drag_dest))
1474 if (gtk_tree_path_get_depth (dest_path) != 1)
1477 /* can drop before any existing node, or before one past any existing. */
1479 indices = gtk_tree_path_get_indices (dest_path);
1481 if (indices[0] <= g_sequence_get_length (GTK_LIST_STORE (drag_dest)->priv->seq))
1486 gtk_tree_path_free (src_path);
1491 /* Sorting and reordering */
1495 gtk_list_store_reorder_func (GSequenceIter *a,
1499 GHashTable *new_positions = user_data;
1500 gint apos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, a));
1501 gint bpos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, b));
1511 * gtk_list_store_reorder:
1512 * @store: A #GtkListStore.
1513 * @new_order: an array of integers mapping the new position of each child
1514 * to its old position before the re-ordering,
1515 * i.e. @new_order<literal>[newpos] = oldpos</literal>.
1517 * Reorders @store to follow the order indicated by @new_order. Note that
1518 * this function only works with unsorted stores.
1523 gtk_list_store_reorder (GtkListStore *store,
1526 GtkListStorePrivate *priv;
1529 GHashTable *new_positions;
1533 g_return_if_fail (GTK_IS_LIST_STORE (store));
1534 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1535 g_return_if_fail (new_order != NULL);
1539 order = g_new (gint, g_sequence_get_length (priv->seq));
1540 for (i = 0; i < g_sequence_get_length (priv->seq); i++)
1541 order[new_order[i]] = i;
1543 new_positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1545 ptr = g_sequence_get_begin_iter (priv->seq);
1547 while (!g_sequence_iter_is_end (ptr))
1549 g_hash_table_insert (new_positions, ptr, GINT_TO_POINTER (order[i++]));
1551 ptr = g_sequence_iter_next (ptr);
1555 g_sequence_sort_iter (priv->seq, gtk_list_store_reorder_func, new_positions);
1557 g_hash_table_destroy (new_positions);
1560 path = gtk_tree_path_new ();
1561 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1562 path, NULL, new_order);
1563 gtk_tree_path_free (path);
1567 save_positions (GSequence *seq)
1569 GHashTable *positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1572 ptr = g_sequence_get_begin_iter (seq);
1573 while (!g_sequence_iter_is_end (ptr))
1575 g_hash_table_insert (positions, ptr,
1576 GINT_TO_POINTER (g_sequence_iter_get_position (ptr)));
1577 ptr = g_sequence_iter_next (ptr);
1584 generate_order (GSequence *seq,
1585 GHashTable *old_positions)
1588 int *order = g_new (int, g_sequence_get_length (seq));
1592 ptr = g_sequence_get_begin_iter (seq);
1593 while (!g_sequence_iter_is_end (ptr))
1595 int old_pos = GPOINTER_TO_INT (g_hash_table_lookup (old_positions, ptr));
1596 order[i++] = old_pos;
1597 ptr = g_sequence_iter_next (ptr);
1600 g_hash_table_destroy (old_positions);
1606 * gtk_list_store_swap:
1607 * @store: A #GtkListStore.
1608 * @a: A #GtkTreeIter.
1609 * @b: Another #GtkTreeIter.
1611 * Swaps @a and @b in @store. Note that this function only works with
1617 gtk_list_store_swap (GtkListStore *store,
1621 GtkListStorePrivate *priv;
1622 GHashTable *old_positions;
1626 g_return_if_fail (GTK_IS_LIST_STORE (store));
1627 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1628 g_return_if_fail (VALID_ITER (a, store));
1629 g_return_if_fail (VALID_ITER (b, store));
1633 if (a->user_data == b->user_data)
1636 old_positions = save_positions (priv->seq);
1638 g_sequence_swap (a->user_data, b->user_data);
1640 order = generate_order (priv->seq, old_positions);
1641 path = gtk_tree_path_new ();
1643 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1646 gtk_tree_path_free (path);
1651 gtk_list_store_move_to (GtkListStore *store,
1655 GtkListStorePrivate *priv = store->priv;
1656 GHashTable *old_positions;
1660 old_positions = save_positions (priv->seq);
1662 g_sequence_move (iter->user_data, g_sequence_get_iter_at_pos (priv->seq, new_pos));
1664 order = generate_order (priv->seq, old_positions);
1666 path = gtk_tree_path_new ();
1667 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1669 gtk_tree_path_free (path);
1674 * gtk_list_store_move_before:
1675 * @store: A #GtkListStore.
1676 * @iter: A #GtkTreeIter.
1677 * @position: (allow-none): A #GtkTreeIter, or %NULL.
1679 * Moves @iter in @store to the position before @position. Note that this
1680 * function only works with unsorted stores. If @position is %NULL, @iter
1681 * will be moved to the end of the list.
1686 gtk_list_store_move_before (GtkListStore *store,
1688 GtkTreeIter *position)
1692 g_return_if_fail (GTK_IS_LIST_STORE (store));
1693 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1694 g_return_if_fail (VALID_ITER (iter, store));
1696 g_return_if_fail (VALID_ITER (position, store));
1699 pos = g_sequence_iter_get_position (position->user_data);
1703 gtk_list_store_move_to (store, iter, pos);
1707 * gtk_list_store_move_after:
1708 * @store: A #GtkListStore.
1709 * @iter: A #GtkTreeIter.
1710 * @position: (allow-none): A #GtkTreeIter or %NULL.
1712 * Moves @iter in @store to the position after @position. Note that this
1713 * function only works with unsorted stores. If @position is %NULL, @iter
1714 * will be moved to the start of the list.
1719 gtk_list_store_move_after (GtkListStore *store,
1721 GtkTreeIter *position)
1725 g_return_if_fail (GTK_IS_LIST_STORE (store));
1726 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1727 g_return_if_fail (VALID_ITER (iter, store));
1729 g_return_if_fail (VALID_ITER (position, store));
1732 pos = g_sequence_iter_get_position (position->user_data) + 1;
1736 gtk_list_store_move_to (store, iter, pos);
1741 gtk_list_store_compare_func (GSequenceIter *a,
1745 GtkListStore *list_store = user_data;
1746 GtkListStorePrivate *priv = list_store->priv;
1750 GtkTreeIterCompareFunc func;
1753 if (priv->sort_column_id != -1)
1755 GtkTreeDataSortHeader *header;
1757 header = _gtk_tree_data_list_get_header (priv->sort_list,
1758 priv->sort_column_id);
1759 g_return_val_if_fail (header != NULL, 0);
1760 g_return_val_if_fail (header->func != NULL, 0);
1762 func = header->func;
1763 data = header->data;
1767 g_return_val_if_fail (priv->default_sort_func != NULL, 0);
1768 func = priv->default_sort_func;
1769 data = priv->default_sort_data;
1772 iter_a.stamp = priv->stamp;
1773 iter_a.user_data = (gpointer)a;
1774 iter_b.stamp = priv->stamp;
1775 iter_b.user_data = (gpointer)b;
1777 g_assert (VALID_ITER (&iter_a, list_store));
1778 g_assert (VALID_ITER (&iter_b, list_store));
1780 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1782 if (priv->order == GTK_SORT_DESCENDING)
1786 else if (retval < 0)
1794 gtk_list_store_sort (GtkListStore *list_store)
1796 GtkListStorePrivate *priv = list_store->priv;
1799 GHashTable *old_positions;
1801 if (!GTK_LIST_STORE_IS_SORTED (list_store) ||
1802 g_sequence_get_length (priv->seq) <= 1)
1805 old_positions = save_positions (priv->seq);
1807 g_sequence_sort_iter (priv->seq, gtk_list_store_compare_func, list_store);
1809 /* Let the world know about our new order */
1810 new_order = generate_order (priv->seq, old_positions);
1812 path = gtk_tree_path_new ();
1813 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1814 path, NULL, new_order);
1815 gtk_tree_path_free (path);
1820 iter_is_sorted (GtkListStore *list_store,
1825 if (!g_sequence_iter_is_begin (iter->user_data))
1827 cmp = g_sequence_iter_prev (iter->user_data);
1828 if (gtk_list_store_compare_func (cmp, iter->user_data, list_store) > 0)
1832 cmp = g_sequence_iter_next (iter->user_data);
1833 if (!g_sequence_iter_is_end (cmp))
1835 if (gtk_list_store_compare_func (iter->user_data, cmp, list_store) > 0)
1843 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1848 GtkListStorePrivate *priv = list_store->priv;
1851 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1852 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
1853 gtk_tree_path_free (path);
1855 if (!iter_is_sorted (list_store, iter))
1857 GHashTable *old_positions;
1860 old_positions = save_positions (priv->seq);
1861 g_sequence_sort_changed_iter (iter->user_data,
1862 gtk_list_store_compare_func,
1864 order = generate_order (priv->seq, old_positions);
1865 path = gtk_tree_path_new ();
1866 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1868 gtk_tree_path_free (path);
1874 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1875 gint *sort_column_id,
1878 GtkListStore *list_store = GTK_LIST_STORE (sortable);
1879 GtkListStorePrivate *priv = list_store->priv;
1882 * sort_column_id = priv->sort_column_id;
1884 * order = priv->order;
1886 if (priv->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
1887 priv->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
1894 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1895 gint sort_column_id,
1898 GtkListStore *list_store = GTK_LIST_STORE (sortable);
1899 GtkListStorePrivate *priv = list_store->priv;
1901 if ((priv->sort_column_id == sort_column_id) &&
1902 (priv->order == order))
1905 if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
1907 if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1909 GtkTreeDataSortHeader *header = NULL;
1911 header = _gtk_tree_data_list_get_header (priv->sort_list,
1914 /* We want to make sure that we have a function */
1915 g_return_if_fail (header != NULL);
1916 g_return_if_fail (header->func != NULL);
1920 g_return_if_fail (priv->default_sort_func != NULL);
1925 priv->sort_column_id = sort_column_id;
1926 priv->order = order;
1928 gtk_tree_sortable_sort_column_changed (sortable);
1930 gtk_list_store_sort (list_store);
1934 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
1935 gint sort_column_id,
1936 GtkTreeIterCompareFunc func,
1938 GDestroyNotify destroy)
1940 GtkListStore *list_store = GTK_LIST_STORE (sortable);
1941 GtkListStorePrivate *priv = list_store->priv;
1943 priv->sort_list = _gtk_tree_data_list_set_header (priv->sort_list,
1945 func, data, destroy);
1947 if (priv->sort_column_id == sort_column_id)
1948 gtk_list_store_sort (list_store);
1952 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
1953 GtkTreeIterCompareFunc func,
1955 GDestroyNotify destroy)
1957 GtkListStore *list_store = GTK_LIST_STORE (sortable);
1958 GtkListStorePrivate *priv = list_store->priv;
1960 if (priv->default_sort_destroy)
1962 GDestroyNotify d = priv->default_sort_destroy;
1964 priv->default_sort_destroy = NULL;
1965 d (priv->default_sort_data);
1968 priv->default_sort_func = func;
1969 priv->default_sort_data = data;
1970 priv->default_sort_destroy = destroy;
1972 if (priv->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1973 gtk_list_store_sort (list_store);
1977 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
1979 GtkListStore *list_store = GTK_LIST_STORE (sortable);
1980 GtkListStorePrivate *priv = list_store->priv;
1982 return (priv->default_sort_func != NULL);
1987 * gtk_list_store_insert_with_values:
1988 * @list_store: A #GtkListStore
1989 * @iter: (out) (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL.
1990 * @position: position to insert the new row
1991 * @Varargs: pairs of column number and value, terminated with -1
1993 * Creates a new row at @position. @iter will be changed to point to this new
1994 * row. If @position is larger than the number of rows on the list, then the
1995 * new row will be appended to the list. The row will be filled with the
1996 * values given to this function.
1999 * <literal>gtk_list_store_insert_with_values(list_store, iter, position...)</literal>
2000 * has the same effect as calling
2002 * gtk_list_store_insert (list_store, iter, position);
2003 * gtk_list_store_set (list_store, iter, ...);
2005 * with the difference that the former will only emit a row_inserted signal,
2006 * while the latter will emit row_inserted, row_changed and, if the list store
2007 * is sorted, rows_reordered. Since emitting the rows_reordered signal
2008 * repeatedly can affect the performance of the program,
2009 * gtk_list_store_insert_with_values() should generally be preferred when
2010 * inserting rows in a sorted list store.
2015 gtk_list_store_insert_with_values (GtkListStore *list_store,
2020 GtkListStorePrivate *priv;
2024 GtkTreeIter tmp_iter;
2026 gboolean changed = FALSE;
2027 gboolean maybe_need_sort = FALSE;
2030 /* FIXME: refactor to reduce overlap with gtk_list_store_set() */
2031 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2033 priv = list_store->priv;
2038 priv->columns_dirty = TRUE;
2042 length = g_sequence_get_length (seq);
2043 if (position > length)
2046 ptr = g_sequence_get_iter_at_pos (seq, position);
2047 ptr = g_sequence_insert_before (ptr, NULL);
2049 iter->stamp = priv->stamp;
2050 iter->user_data = ptr;
2052 g_assert (VALID_ITER (iter, list_store));
2056 va_start (var_args, position);
2057 gtk_list_store_set_valist_internal (list_store, iter,
2058 &changed, &maybe_need_sort,
2062 /* Don't emit rows_reordered here */
2063 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2064 g_sequence_sort_changed_iter (iter->user_data,
2065 gtk_list_store_compare_func,
2068 /* Just emit row_inserted */
2069 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2070 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2071 gtk_tree_path_free (path);
2076 * gtk_list_store_insert_with_valuesv:
2077 * @list_store: A #GtkListStore
2078 * @iter: (out) (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL.
2079 * @position: position to insert the new row
2080 * @columns: an array of column numbers
2081 * @values: an array of GValues
2082 * @n_values: the length of the @columns and @values arrays
2084 * A variant of gtk_list_store_insert_with_values() which
2085 * takes the columns and values as two arrays, instead of
2086 * varargs. This function is mainly intended for
2087 * language-bindings.
2092 gtk_list_store_insert_with_valuesv (GtkListStore *list_store,
2099 GtkListStorePrivate *priv;
2103 GtkTreeIter tmp_iter;
2105 gboolean changed = FALSE;
2106 gboolean maybe_need_sort = FALSE;
2108 /* FIXME refactor to reduce overlap with
2109 * gtk_list_store_insert_with_values()
2111 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2113 priv = list_store->priv;
2118 priv->columns_dirty = TRUE;
2122 length = g_sequence_get_length (seq);
2123 if (position > length)
2126 ptr = g_sequence_get_iter_at_pos (seq, position);
2127 ptr = g_sequence_insert_before (ptr, NULL);
2129 iter->stamp = priv->stamp;
2130 iter->user_data = ptr;
2132 g_assert (VALID_ITER (iter, list_store));
2136 gtk_list_store_set_vector_internal (list_store, iter,
2137 &changed, &maybe_need_sort,
2138 columns, values, n_values);
2140 /* Don't emit rows_reordered here */
2141 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2142 g_sequence_sort_changed_iter (iter->user_data,
2143 gtk_list_store_compare_func,
2146 /* Just emit row_inserted */
2147 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2148 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2149 gtk_tree_path_free (path);
2152 /* GtkBuildable custom tag implementation
2155 * <column type="..."/>
2156 * <column type="..."/>
2160 gboolean translatable;
2166 GtkBuilder *builder;
2168 GSList *column_type_names;
2169 GType *column_types;
2178 const gchar *domain;
2182 list_store_start_element (GMarkupParseContext *context,
2183 const gchar *element_name,
2184 const gchar **names,
2185 const gchar **values,
2190 SubParserData *data = (SubParserData*)user_data;
2192 if (strcmp (element_name, "col") == 0)
2195 gchar *context = NULL;
2196 gboolean translatable = FALSE;
2199 if (data->row_column >= data->n_columns)
2201 g_set_error (error, data->error_quark, 0,
2202 "Too many columns, maximum is %d\n", data->n_columns - 1);
2206 for (i = 0; names[i]; i++)
2207 if (strcmp (names[i], "id") == 0)
2210 id = atoi (values[i]);
2213 g_set_error (error, data->error_quark, 0,
2214 "the id tag %s could not be converted to an integer",
2218 if (id < 0 || id >= data->n_columns)
2220 g_set_error (error, data->error_quark, 0,
2221 "id value %d out of range", id);
2225 else if (strcmp (names[i], "translatable") == 0)
2227 if (!_gtk_builder_boolean_from_string (values[i], &translatable,
2231 else if (strcmp (names[i], "comments") == 0)
2233 /* do nothing, comments are for translators */
2235 else if (strcmp (names[i], "context") == 0)
2237 context = g_strdup (values[i]);
2242 g_set_error (error, data->error_quark, 0,
2243 "<col> needs an id attribute");
2247 info = g_slice_new0 (ColInfo);
2248 info->translatable = translatable;
2249 info->context = context;
2252 data->colids[data->row_column] = id;
2253 data->columns[data->row_column] = info;
2255 data->is_data = TRUE;
2257 else if (strcmp (element_name, "row") == 0)
2259 else if (strcmp (element_name, "column") == 0)
2260 for (i = 0; names[i]; i++)
2261 if (strcmp (names[i], "type") == 0)
2262 data->column_type_names = g_slist_prepend (data->column_type_names,
2263 g_strdup (values[i]));
2264 else if (strcmp (element_name, "columns") == 0)
2266 else if (strcmp (element_name, "data") == 0)
2269 g_set_error (error, data->error_quark, 0,
2270 "Unknown start tag: %s", element_name);
2274 list_store_end_element (GMarkupParseContext *context,
2275 const gchar *element_name,
2279 SubParserData *data = (SubParserData*)user_data;
2281 g_assert (data->builder);
2283 if (strcmp (element_name, "row") == 0)
2288 gtk_list_store_insert_with_valuesv (GTK_LIST_STORE (data->object),
2294 for (i = 0; i < data->row_column; i++)
2296 ColInfo *info = data->columns[i];
2297 g_free (info->context);
2298 g_slice_free (ColInfo, info);
2299 data->columns[i] = NULL;
2300 g_value_unset (&data->values[i]);
2302 g_free (data->values);
2303 data->values = g_new0 (GValue, data->n_columns);
2305 data->row_column = 0;
2307 else if (strcmp (element_name, "columns") == 0)
2309 GType *column_types;
2314 data->column_type_names = g_slist_reverse (data->column_type_names);
2315 column_types = g_new0 (GType, g_slist_length (data->column_type_names));
2317 for (l = data->column_type_names, i = 0; l; l = l->next, i++)
2319 type = gtk_builder_get_type_from_name (data->builder, l->data);
2320 if (type == G_TYPE_INVALID)
2322 g_warning ("Unknown type %s specified in treemodel %s",
2323 (const gchar*)l->data,
2324 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
2327 column_types[i] = type;
2332 gtk_list_store_set_column_types (GTK_LIST_STORE (data->object), i,
2335 g_free (column_types);
2337 else if (strcmp (element_name, "col") == 0)
2338 data->is_data = FALSE;
2339 else if (strcmp (element_name, "data") == 0)
2341 else if (strcmp (element_name, "column") == 0)
2344 g_set_error (error, data->error_quark, 0,
2345 "Unknown end tag: %s", element_name);
2349 list_store_text (GMarkupParseContext *context,
2355 SubParserData *data = (SubParserData*)user_data;
2357 GError *tmp_error = NULL;
2364 i = data->row_column - 1;
2365 info = data->columns[i];
2367 string = g_strndup (text, text_len);
2368 if (info->translatable && text_len)
2372 /* FIXME: This will not use the domain set in the .ui file,
2373 * since the parser is not telling the builder about the domain.
2374 * However, it will work for gtk_builder_set_translation_domain() calls.
2376 translated = _gtk_builder_parser_translate (data->domain,
2380 string = translated;
2383 if (!gtk_builder_value_from_string_type (data->builder,
2384 data->column_types[info->id],
2392 "Could not convert '%s' to type %s: %s\n",
2393 text, g_type_name (data->column_types[info->id]),
2394 tmp_error->message);
2395 g_error_free (tmp_error);
2400 static const GMarkupParser list_store_parser =
2402 list_store_start_element,
2403 list_store_end_element,
2408 gtk_list_store_buildable_custom_tag_start (GtkBuildable *buildable,
2409 GtkBuilder *builder,
2411 const gchar *tagname,
2412 GMarkupParser *parser,
2415 SubParserData *parser_data;
2420 if (strcmp (tagname, "columns") == 0)
2423 parser_data = g_slice_new0 (SubParserData);
2424 parser_data->builder = builder;
2425 parser_data->object = G_OBJECT (buildable);
2426 parser_data->column_type_names = NULL;
2428 *parser = list_store_parser;
2429 *data = parser_data;
2432 else if (strcmp (tagname, "data") == 0)
2434 gint n_columns = gtk_list_store_get_n_columns (GTK_TREE_MODEL (buildable));
2436 g_error ("Cannot append data to an empty model");
2438 parser_data = g_slice_new0 (SubParserData);
2439 parser_data->builder = builder;
2440 parser_data->object = G_OBJECT (buildable);
2441 parser_data->values = g_new0 (GValue, n_columns);
2442 parser_data->colids = g_new0 (gint, n_columns);
2443 parser_data->columns = g_new0 (ColInfo*, n_columns);
2444 parser_data->column_types = GTK_LIST_STORE (buildable)->priv->column_headers;
2445 parser_data->n_columns = n_columns;
2446 parser_data->last_row = 0;
2447 parser_data->error_quark = g_quark_from_static_string ("GtkListStore");
2448 parser_data->domain = gtk_builder_get_translation_domain (builder);
2450 *parser = list_store_parser;
2451 *data = parser_data;
2455 g_warning ("Unknown custom list store tag: %s", tagname);
2461 gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
2462 GtkBuilder *builder,
2464 const gchar *tagname,
2467 SubParserData *sub = (SubParserData*)data;
2469 if (strcmp (tagname, "columns") == 0)
2471 g_slist_free (sub->column_type_names);
2472 g_slice_free (SubParserData, sub);
2474 else if (strcmp (tagname, "data") == 0)
2477 for (i = 0; i < sub->n_columns; i++)
2479 ColInfo *info = sub->columns[i];
2482 g_free (info->context);
2483 g_slice_free (ColInfo, info);
2486 g_free (sub->colids);
2487 g_free (sub->columns);
2488 g_free (sub->values);
2489 g_slice_free (SubParserData, sub);
2492 g_warning ("Unknown custom list store tag: %s", tagname);