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 #define GTK_LIST_STORE_IS_SORTED(list) (((GtkListStore*)(list))->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
35 #define VALID_ITER(iter, list_store) ((iter)!= NULL && (iter)->user_data != NULL && list_store->stamp == (iter)->stamp && !g_sequence_iter_is_end ((iter)->user_data) && g_sequence_iter_get_sequence ((iter)->user_data) == list_store->seq)
37 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
38 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
39 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
40 static void gtk_list_store_sortable_init (GtkTreeSortableIface *iface);
41 static void gtk_list_store_buildable_init (GtkBuildableIface *iface);
42 static void gtk_list_store_finalize (GObject *object);
43 static GtkTreeModelFlags gtk_list_store_get_flags (GtkTreeModel *tree_model);
44 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
45 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
47 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
50 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
52 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
56 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
58 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
61 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
63 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
65 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
69 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
74 static void gtk_list_store_set_n_columns (GtkListStore *list_store,
76 static void gtk_list_store_set_column_type (GtkListStore *list_store,
80 static void gtk_list_store_increment_stamp (GtkListStore *list_store);
84 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
86 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
88 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
90 GtkSelectionData *selection_data);
91 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
93 GtkSelectionData *selection_data);
94 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
95 GtkTreePath *dest_path,
96 GtkSelectionData *selection_data);
100 static void gtk_list_store_sort (GtkListStore *list_store);
101 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
104 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
105 gint *sort_column_id,
107 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
110 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
112 GtkTreeIterCompareFunc func,
114 GDestroyNotify destroy);
115 static void gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
116 GtkTreeIterCompareFunc func,
118 GDestroyNotify destroy);
119 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable);
123 static gboolean gtk_list_store_buildable_custom_tag_start (GtkBuildable *buildable,
126 const gchar *tagname,
127 GMarkupParser *parser,
129 static void gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
132 const gchar *tagname,
135 G_DEFINE_TYPE_WITH_CODE (GtkListStore, gtk_list_store, G_TYPE_OBJECT,
136 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
137 gtk_list_store_tree_model_init)
138 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
139 gtk_list_store_drag_source_init)
140 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
141 gtk_list_store_drag_dest_init)
142 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
143 gtk_list_store_sortable_init)
144 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
145 gtk_list_store_buildable_init))
149 gtk_list_store_class_init (GtkListStoreClass *class)
151 GObjectClass *object_class;
153 object_class = (GObjectClass*) class;
155 object_class->finalize = gtk_list_store_finalize;
159 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
161 iface->get_flags = gtk_list_store_get_flags;
162 iface->get_n_columns = gtk_list_store_get_n_columns;
163 iface->get_column_type = gtk_list_store_get_column_type;
164 iface->get_iter = gtk_list_store_get_iter;
165 iface->get_path = gtk_list_store_get_path;
166 iface->get_value = gtk_list_store_get_value;
167 iface->iter_next = gtk_list_store_iter_next;
168 iface->iter_children = gtk_list_store_iter_children;
169 iface->iter_has_child = gtk_list_store_iter_has_child;
170 iface->iter_n_children = gtk_list_store_iter_n_children;
171 iface->iter_nth_child = gtk_list_store_iter_nth_child;
172 iface->iter_parent = gtk_list_store_iter_parent;
176 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
178 iface->row_draggable = real_gtk_list_store_row_draggable;
179 iface->drag_data_delete = gtk_list_store_drag_data_delete;
180 iface->drag_data_get = gtk_list_store_drag_data_get;
184 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
186 iface->drag_data_received = gtk_list_store_drag_data_received;
187 iface->row_drop_possible = gtk_list_store_row_drop_possible;
191 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
193 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
194 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
195 iface->set_sort_func = gtk_list_store_set_sort_func;
196 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
197 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
201 gtk_list_store_buildable_init (GtkBuildableIface *iface)
203 iface->custom_tag_start = gtk_list_store_buildable_custom_tag_start;
204 iface->custom_tag_end = gtk_list_store_buildable_custom_tag_end;
208 gtk_list_store_init (GtkListStore *list_store)
210 list_store->seq = g_sequence_new (NULL);
211 list_store->sort_list = NULL;
212 list_store->stamp = g_random_int ();
213 list_store->sort_column_id = GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID;
214 list_store->columns_dirty = FALSE;
215 list_store->length = 0;
219 * gtk_list_store_new:
220 * @n_columns: number of columns in the list store
221 * @Varargs: all #GType types for the columns, from first to last
223 * Creates a new list store as with @n_columns columns each of the types passed
224 * in. Note that only types derived from standard GObject fundamental types
227 * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
228 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
229 * int, string and #GdkPixbuf respectively.
231 * Return value: a new #GtkListStore
234 gtk_list_store_new (gint n_columns,
237 GtkListStore *retval;
241 g_return_val_if_fail (n_columns > 0, NULL);
243 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
244 gtk_list_store_set_n_columns (retval, n_columns);
246 va_start (args, n_columns);
248 for (i = 0; i < n_columns; i++)
250 GType type = va_arg (args, GType);
251 if (! _gtk_tree_data_list_check_type (type))
253 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
254 g_object_unref (retval);
258 gtk_list_store_set_column_type (retval, i, type);
268 * gtk_list_store_newv:
269 * @n_columns: number of columns in the list store
270 * @types: (array length=n_columns): an array of #GType types for the columns, from first to last
272 * Non-vararg creation function. Used primarily by language bindings.
274 * Return value: (transfer none): a new #GtkListStore
277 gtk_list_store_newv (gint n_columns,
280 GtkListStore *retval;
283 g_return_val_if_fail (n_columns > 0, NULL);
285 retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
286 gtk_list_store_set_n_columns (retval, n_columns);
288 for (i = 0; i < n_columns; i++)
290 if (! _gtk_tree_data_list_check_type (types[i]))
292 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
293 g_object_unref (retval);
297 gtk_list_store_set_column_type (retval, i, types[i]);
304 * gtk_list_store_set_column_types:
305 * @list_store: A #GtkListStore
306 * @n_columns: Number of columns for the list store
307 * @types: (array length=n_columns): An array length n of #GTypes
309 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
310 * and should only be used when constructing a new #GtkListStore. It will not
311 * function after a row has been added, or a method on the #GtkTreeModel
312 * interface is called.
315 gtk_list_store_set_column_types (GtkListStore *list_store,
321 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
322 g_return_if_fail (list_store->columns_dirty == 0);
324 gtk_list_store_set_n_columns (list_store, n_columns);
325 for (i = 0; i < n_columns; i++)
327 if (! _gtk_tree_data_list_check_type (types[i]))
329 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
332 gtk_list_store_set_column_type (list_store, i, types[i]);
337 gtk_list_store_set_n_columns (GtkListStore *list_store,
342 if (list_store->n_columns == n_columns)
345 list_store->column_headers = g_renew (GType, list_store->column_headers, n_columns);
346 for (i = list_store->n_columns; i < n_columns; i++)
347 list_store->column_headers[i] = G_TYPE_INVALID;
348 list_store->n_columns = n_columns;
350 if (list_store->sort_list)
351 _gtk_tree_data_list_header_free (list_store->sort_list);
352 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
356 gtk_list_store_set_column_type (GtkListStore *list_store,
360 if (!_gtk_tree_data_list_check_type (type))
362 g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
366 list_store->column_headers[column] = type;
370 gtk_list_store_finalize (GObject *object)
372 GtkListStore *list_store = GTK_LIST_STORE (object);
374 g_sequence_foreach (list_store->seq,
375 (GFunc) _gtk_tree_data_list_free, list_store->column_headers);
377 g_sequence_free (list_store->seq);
379 _gtk_tree_data_list_header_free (list_store->sort_list);
380 g_free (list_store->column_headers);
382 if (list_store->default_sort_destroy)
384 GDestroyNotify d = list_store->default_sort_destroy;
386 list_store->default_sort_destroy = NULL;
387 d (list_store->default_sort_data);
388 list_store->default_sort_data = NULL;
391 G_OBJECT_CLASS (gtk_list_store_parent_class)->finalize (object);
394 /* Fulfill the GtkTreeModel requirements */
395 static GtkTreeModelFlags
396 gtk_list_store_get_flags (GtkTreeModel *tree_model)
398 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
402 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
404 GtkListStore *list_store = (GtkListStore *) tree_model;
406 list_store->columns_dirty = TRUE;
408 return list_store->n_columns;
412 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
415 GtkListStore *list_store = (GtkListStore *) tree_model;
417 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns,
420 list_store->columns_dirty = TRUE;
422 return list_store->column_headers[index];
426 gtk_list_store_get_iter (GtkTreeModel *tree_model,
430 GtkListStore *list_store = (GtkListStore *) tree_model;
434 list_store->columns_dirty = TRUE;
436 seq = list_store->seq;
438 i = gtk_tree_path_get_indices (path)[0];
440 if (i >= g_sequence_get_length (seq))
443 iter->stamp = list_store->stamp;
444 iter->user_data = g_sequence_get_iter_at_pos (seq, i);
450 gtk_list_store_get_path (GtkTreeModel *tree_model,
455 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
457 if (g_sequence_iter_is_end (iter->user_data))
460 path = gtk_tree_path_new ();
461 gtk_tree_path_append_index (path, g_sequence_iter_get_position (iter->user_data));
467 gtk_list_store_get_value (GtkTreeModel *tree_model,
472 GtkListStore *list_store = (GtkListStore *) tree_model;
473 GtkTreeDataList *list;
474 gint tmp_column = column;
476 g_return_if_fail (column < list_store->n_columns);
477 g_return_if_fail (VALID_ITER (iter, list_store));
479 list = g_sequence_get (iter->user_data);
481 while (tmp_column-- > 0 && list)
485 g_value_init (value, list_store->column_headers[column]);
487 _gtk_tree_data_list_node_to_value (list,
488 list_store->column_headers[column],
493 gtk_list_store_iter_next (GtkTreeModel *tree_model,
498 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
499 iter->user_data = g_sequence_iter_next (iter->user_data);
501 retval = g_sequence_iter_is_end (iter->user_data);
509 gtk_list_store_iter_children (GtkTreeModel *tree_model,
513 GtkListStore *list_store = (GtkListStore *) tree_model;
515 /* this is a list, nodes have no children */
522 if (g_sequence_get_length (list_store->seq) > 0)
524 iter->stamp = list_store->stamp;
525 iter->user_data = g_sequence_get_begin_iter (list_store->seq);
536 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
543 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
546 GtkListStore *list_store = (GtkListStore *) tree_model;
549 return g_sequence_get_length (list_store->seq);
551 g_return_val_if_fail (list_store->stamp == iter->stamp, -1);
557 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
562 GtkListStore *list_store = (GtkListStore *) tree_model;
563 GSequenceIter *child;
570 child = g_sequence_get_iter_at_pos (list_store->seq, n);
572 if (g_sequence_iter_is_end (child))
575 iter->stamp = list_store->stamp;
576 iter->user_data = child;
582 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
591 gtk_list_store_real_set_value (GtkListStore *list_store,
597 GtkTreeDataList *list;
598 GtkTreeDataList *prev;
599 gint old_column = column;
600 GValue real_value = {0, };
601 gboolean converted = FALSE;
602 gboolean retval = FALSE;
604 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
606 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
607 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
609 g_warning ("%s: Unable to convert from %s to %s\n",
611 g_type_name (G_VALUE_TYPE (value)),
612 g_type_name (list_store->column_headers[column]));
615 if (!g_value_transform (value, &real_value))
617 g_warning ("%s: Unable to make conversion from %s to %s\n",
619 g_type_name (G_VALUE_TYPE (value)),
620 g_type_name (list_store->column_headers[column]));
621 g_value_unset (&real_value);
627 prev = list = g_sequence_get (iter->user_data);
634 _gtk_tree_data_list_value_to_node (list, &real_value);
636 _gtk_tree_data_list_value_to_node (list, value);
639 g_value_unset (&real_value);
640 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
641 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
650 if (g_sequence_get (iter->user_data) == NULL)
652 list = _gtk_tree_data_list_alloc();
653 g_sequence_set (iter->user_data, list);
658 list = prev->next = _gtk_tree_data_list_alloc ();
664 list->next = _gtk_tree_data_list_alloc ();
671 _gtk_tree_data_list_value_to_node (list, &real_value);
673 _gtk_tree_data_list_value_to_node (list, value);
677 g_value_unset (&real_value);
679 if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
680 gtk_list_store_sort_iter_changed (list_store, iter, old_column);
687 * gtk_list_store_set_value:
688 * @list_store: A #GtkListStore
689 * @iter: A valid #GtkTreeIter for the row being modified
690 * @column: column number to modify
691 * @value: new value for the cell
693 * Sets the data in the cell specified by @iter and @column.
694 * The type of @value must be convertible to the type of the
699 gtk_list_store_set_value (GtkListStore *list_store,
704 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
705 g_return_if_fail (VALID_ITER (iter, list_store));
706 g_return_if_fail (column >= 0 && column < list_store->n_columns);
707 g_return_if_fail (G_IS_VALUE (value));
709 if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
713 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
714 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
715 gtk_tree_path_free (path);
719 static GtkTreeIterCompareFunc
720 gtk_list_store_get_compare_func (GtkListStore *list_store)
722 GtkTreeIterCompareFunc func = NULL;
724 if (GTK_LIST_STORE_IS_SORTED (list_store))
726 if (list_store->sort_column_id != -1)
728 GtkTreeDataSortHeader *header;
729 header = _gtk_tree_data_list_get_header (list_store->sort_list,
730 list_store->sort_column_id);
731 g_return_val_if_fail (header != NULL, NULL);
732 g_return_val_if_fail (header->func != NULL, NULL);
737 func = list_store->default_sort_func;
745 gtk_list_store_set_vector_internal (GtkListStore *list_store,
747 gboolean *emit_signal,
748 gboolean *maybe_need_sort,
754 GtkTreeIterCompareFunc func = NULL;
756 func = gtk_list_store_get_compare_func (list_store);
757 if (func != _gtk_tree_data_list_compare_func)
758 *maybe_need_sort = TRUE;
760 for (i = 0; i < n_values; i++)
762 *emit_signal = gtk_list_store_real_set_value (list_store,
766 FALSE) || *emit_signal;
768 if (func == _gtk_tree_data_list_compare_func &&
769 columns[i] == list_store->sort_column_id)
770 *maybe_need_sort = TRUE;
775 gtk_list_store_set_valist_internal (GtkListStore *list_store,
777 gboolean *emit_signal,
778 gboolean *maybe_need_sort,
782 GtkTreeIterCompareFunc func = NULL;
784 column = va_arg (var_args, gint);
786 func = gtk_list_store_get_compare_func (list_store);
787 if (func != _gtk_tree_data_list_compare_func)
788 *maybe_need_sort = TRUE;
792 GValue value = { 0, };
795 if (column < 0 || column >= list_store->n_columns)
797 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
800 g_value_init (&value, list_store->column_headers[column]);
802 G_VALUE_COLLECT (&value, var_args, 0, &error);
805 g_warning ("%s: %s", G_STRLOC, error);
808 /* we purposely leak the value here, it might not be
809 * in a sane state if an error condition occoured
814 /* FIXME: instead of calling this n times, refactor with above */
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 column == list_store->sort_column_id)
823 *maybe_need_sort = TRUE;
825 g_value_unset (&value);
827 column = va_arg (var_args, gint);
832 * gtk_list_store_set_valuesv:
833 * @list_store: A #GtkListStore
834 * @iter: A valid #GtkTreeIter for the row being modified
835 * @columns: (array length=n_values): an array of column numbers
836 * @values: (array length=n_values): an array of GValues
837 * @n_values: the length of the @columns and @values arrays
839 * A variant of gtk_list_store_set_valist() which
840 * takes the columns and values as two arrays, instead of
841 * varargs. This function is mainly intended for
842 * language-bindings and in case the number of columns to
843 * change is not known until run-time.
848 gtk_list_store_set_valuesv (GtkListStore *list_store,
854 gboolean emit_signal = FALSE;
855 gboolean maybe_need_sort = FALSE;
857 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
858 g_return_if_fail (VALID_ITER (iter, list_store));
860 gtk_list_store_set_vector_internal (list_store, iter,
863 columns, values, n_values);
865 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
866 gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
872 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
873 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
874 gtk_tree_path_free (path);
879 * gtk_list_store_set_valist:
880 * @list_store: A #GtkListStore
881 * @iter: A valid #GtkTreeIter for the row being modified
882 * @var_args: va_list of column/value pairs
884 * See gtk_list_store_set(); this version takes a va_list for use by language
889 gtk_list_store_set_valist (GtkListStore *list_store,
893 gboolean emit_signal = FALSE;
894 gboolean maybe_need_sort = FALSE;
896 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
897 g_return_if_fail (VALID_ITER (iter, list_store));
899 gtk_list_store_set_valist_internal (list_store, iter,
904 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
905 gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
911 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
912 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
913 gtk_tree_path_free (path);
918 * gtk_list_store_set:
919 * @list_store: a #GtkListStore
920 * @iter: row iterator
921 * @Varargs: pairs of column number and value, terminated with -1
923 * Sets the value of one or more cells in the row referenced by @iter.
924 * The variable argument list should contain integer column numbers,
925 * each column number followed by the value to be set.
926 * The list is terminated by a -1. For example, to set column 0 with type
927 * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
928 * 0, "Foo", -1)</literal>.
929 * The value will be copied or referenced by the store if appropriate.
932 gtk_list_store_set (GtkListStore *list_store,
938 va_start (var_args, iter);
939 gtk_list_store_set_valist (list_store, iter, var_args);
944 * gtk_list_store_remove:
945 * @list_store: A #GtkListStore
946 * @iter: A valid #GtkTreeIter
948 * Removes the given row from the list store. After being removed,
949 * @iter is set to be the next valid row, or invalidated if it pointed
950 * to the last row in @list_store.
952 * Return value: %TRUE if @iter is valid, %FALSE if not.
955 gtk_list_store_remove (GtkListStore *list_store,
959 GSequenceIter *ptr, *next;
961 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
962 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
964 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
966 ptr = iter->user_data;
967 next = g_sequence_iter_next (ptr);
969 _gtk_tree_data_list_free (g_sequence_get (ptr), list_store->column_headers);
970 g_sequence_remove (iter->user_data);
972 list_store->length--;
974 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
975 gtk_tree_path_free (path);
977 if (g_sequence_iter_is_end (next))
984 iter->stamp = list_store->stamp;
985 iter->user_data = next;
991 * gtk_list_store_insert:
992 * @list_store: A #GtkListStore
993 * @iter: An unset #GtkTreeIter to set to the new row
994 * @position: position to insert the new row
996 * Creates a new row at @position. @iter will be changed to point to this new
997 * row. If @position is larger than the number of rows on the list, then the
998 * new row will be appended to the list. The row will be empty after this
999 * function is called. To fill in values, you need to call
1000 * gtk_list_store_set() or gtk_list_store_set_value().
1004 gtk_list_store_insert (GtkListStore *list_store,
1013 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1014 g_return_if_fail (iter != NULL);
1015 g_return_if_fail (position >= 0);
1017 list_store->columns_dirty = TRUE;
1019 seq = list_store->seq;
1021 length = g_sequence_get_length (seq);
1022 if (position > length)
1025 ptr = g_sequence_get_iter_at_pos (seq, position);
1026 ptr = g_sequence_insert_before (ptr, NULL);
1028 iter->stamp = list_store->stamp;
1029 iter->user_data = ptr;
1031 g_assert (VALID_ITER (iter, list_store));
1033 list_store->length++;
1035 path = gtk_tree_path_new ();
1036 gtk_tree_path_append_index (path, position);
1037 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1038 gtk_tree_path_free (path);
1042 * gtk_list_store_insert_before:
1043 * @list_store: A #GtkListStore
1044 * @iter: An unset #GtkTreeIter to set to the new row
1045 * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1047 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will
1048 * be appended to the end of the list. @iter will be changed to point to this
1049 * new row. The row will be empty after this function is called. To fill in
1050 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1054 gtk_list_store_insert_before (GtkListStore *list_store,
1056 GtkTreeIter *sibling)
1058 GSequenceIter *after;
1060 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1061 g_return_if_fail (iter != NULL);
1063 g_return_if_fail (VALID_ITER (sibling, list_store));
1066 after = g_sequence_get_end_iter (list_store->seq);
1068 after = sibling->user_data;
1070 gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1074 * gtk_list_store_insert_after:
1075 * @list_store: A #GtkListStore
1076 * @iter: An unset #GtkTreeIter to set to the new row
1077 * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1079 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1080 * prepended to the beginning of the list. @iter will be changed to point to
1081 * this new row. The row will be empty after this function is called. To fill
1082 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1086 gtk_list_store_insert_after (GtkListStore *list_store,
1088 GtkTreeIter *sibling)
1090 GSequenceIter *after;
1092 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1093 g_return_if_fail (iter != NULL);
1095 g_return_if_fail (VALID_ITER (sibling, list_store));
1098 after = g_sequence_get_begin_iter (list_store->seq);
1100 after = g_sequence_iter_next (sibling->user_data);
1102 gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1106 * gtk_list_store_prepend:
1107 * @list_store: A #GtkListStore
1108 * @iter: An unset #GtkTreeIter to set to the prepend row
1110 * Prepends a new row to @list_store. @iter will be changed to point to this new
1111 * row. The row will be empty after this function is called. To fill in
1112 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1116 gtk_list_store_prepend (GtkListStore *list_store,
1119 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1120 g_return_if_fail (iter != NULL);
1122 gtk_list_store_insert (list_store, iter, 0);
1126 * gtk_list_store_append:
1127 * @list_store: A #GtkListStore
1128 * @iter: An unset #GtkTreeIter to set to the appended row
1130 * Appends a new row to @list_store. @iter will be changed to point to this new
1131 * row. The row will be empty after this function is called. To fill in
1132 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1136 gtk_list_store_append (GtkListStore *list_store,
1139 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1140 g_return_if_fail (iter != NULL);
1142 gtk_list_store_insert (list_store, iter, g_sequence_get_length (list_store->seq));
1146 gtk_list_store_increment_stamp (GtkListStore *list_store)
1150 list_store->stamp++;
1152 while (list_store->stamp == 0);
1156 * gtk_list_store_clear:
1157 * @list_store: a #GtkListStore.
1159 * Removes all rows from the list store.
1163 gtk_list_store_clear (GtkListStore *list_store)
1166 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1168 while (g_sequence_get_length (list_store->seq) > 0)
1170 iter.stamp = list_store->stamp;
1171 iter.user_data = g_sequence_get_begin_iter (list_store->seq);
1172 gtk_list_store_remove (list_store, &iter);
1175 gtk_list_store_increment_stamp (list_store);
1179 * gtk_list_store_iter_is_valid:
1180 * @list_store: A #GtkListStore.
1181 * @iter: A #GtkTreeIter.
1183 * <warning>This function is slow. Only use it for debugging and/or testing
1184 * purposes.</warning>
1186 * Checks if the given iter is a valid iter for this #GtkListStore.
1188 * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1193 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1196 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1197 g_return_val_if_fail (iter != NULL, FALSE);
1199 if (!VALID_ITER (iter, list_store))
1202 if (g_sequence_iter_get_sequence (iter->user_data) != list_store->seq)
1208 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
1215 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1220 if (gtk_list_store_get_iter (GTK_TREE_MODEL (drag_source),
1224 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1231 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1233 GtkSelectionData *selection_data)
1235 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1236 * target, because the default handler does it for us, but
1237 * we do anyway for the convenience of someone maybe overriding the
1241 if (gtk_tree_set_row_drag_data (selection_data,
1242 GTK_TREE_MODEL (drag_source),
1249 /* FIXME handle text targets at least. */
1256 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1258 GtkSelectionData *selection_data)
1260 GtkTreeModel *tree_model;
1261 GtkListStore *list_store;
1262 GtkTreeModel *src_model = NULL;
1263 GtkTreePath *src_path = NULL;
1264 gboolean retval = FALSE;
1266 tree_model = GTK_TREE_MODEL (drag_dest);
1267 list_store = GTK_LIST_STORE (drag_dest);
1269 if (gtk_tree_get_row_drag_data (selection_data,
1272 src_model == tree_model)
1274 /* Copy the given row to a new position */
1275 GtkTreeIter src_iter;
1276 GtkTreeIter dest_iter;
1279 if (!gtk_list_store_get_iter (src_model,
1286 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1287 prev = gtk_tree_path_copy (dest);
1289 if (!gtk_tree_path_prev (prev))
1291 /* dest was the first spot in the list; which means we are supposed
1294 gtk_list_store_prepend (list_store, &dest_iter);
1300 if (gtk_list_store_get_iter (tree_model, &dest_iter, prev))
1302 GtkTreeIter tmp_iter = dest_iter;
1304 gtk_list_store_insert_after (list_store, &dest_iter, &tmp_iter);
1310 gtk_tree_path_free (prev);
1312 /* If we succeeded in creating dest_iter, copy data from src
1316 GtkTreeDataList *dl = g_sequence_get (src_iter.user_data);
1317 GtkTreeDataList *copy_head = NULL;
1318 GtkTreeDataList *copy_prev = NULL;
1319 GtkTreeDataList *copy_iter = NULL;
1326 copy_iter = _gtk_tree_data_list_node_copy (dl,
1327 list_store->column_headers[col]);
1329 if (copy_head == NULL)
1330 copy_head = copy_iter;
1333 copy_prev->next = copy_iter;
1335 copy_prev = copy_iter;
1341 dest_iter.stamp = list_store->stamp;
1342 g_sequence_set (dest_iter.user_data, copy_head);
1344 path = gtk_list_store_get_path (tree_model, &dest_iter);
1345 gtk_tree_model_row_changed (tree_model, path, &dest_iter);
1346 gtk_tree_path_free (path);
1351 /* FIXME maybe add some data targets eventually, or handle text
1352 * targets in the simple case.
1359 gtk_tree_path_free (src_path);
1365 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1366 GtkTreePath *dest_path,
1367 GtkSelectionData *selection_data)
1370 GtkTreeModel *src_model = NULL;
1371 GtkTreePath *src_path = NULL;
1372 gboolean retval = FALSE;
1374 /* don't accept drops if the list has been sorted */
1375 if (GTK_LIST_STORE_IS_SORTED (drag_dest))
1378 if (!gtk_tree_get_row_drag_data (selection_data,
1383 if (src_model != GTK_TREE_MODEL (drag_dest))
1386 if (gtk_tree_path_get_depth (dest_path) != 1)
1389 /* can drop before any existing node, or before one past any existing. */
1391 indices = gtk_tree_path_get_indices (dest_path);
1393 if (indices[0] <= g_sequence_get_length (GTK_LIST_STORE (drag_dest)->seq))
1398 gtk_tree_path_free (src_path);
1403 /* Sorting and reordering */
1407 gtk_list_store_reorder_func (GSequenceIter *a,
1411 GHashTable *new_positions = user_data;
1412 gint apos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, a));
1413 gint bpos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, b));
1423 * gtk_list_store_reorder:
1424 * @store: A #GtkListStore.
1425 * @new_order: an array of integers mapping the new position of each child
1426 * to its old position before the re-ordering,
1427 * i.e. @new_order<literal>[newpos] = oldpos</literal>.
1429 * Reorders @store to follow the order indicated by @new_order. Note that
1430 * this function only works with unsorted stores.
1435 gtk_list_store_reorder (GtkListStore *store,
1440 GHashTable *new_positions;
1444 g_return_if_fail (GTK_IS_LIST_STORE (store));
1445 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1446 g_return_if_fail (new_order != NULL);
1448 order = g_new (gint, g_sequence_get_length (store->seq));
1449 for (i = 0; i < g_sequence_get_length (store->seq); i++)
1450 order[new_order[i]] = i;
1452 new_positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1454 ptr = g_sequence_get_begin_iter (store->seq);
1456 while (!g_sequence_iter_is_end (ptr))
1458 g_hash_table_insert (new_positions, ptr, GINT_TO_POINTER (order[i++]));
1460 ptr = g_sequence_iter_next (ptr);
1464 g_sequence_sort_iter (store->seq, gtk_list_store_reorder_func, new_positions);
1466 g_hash_table_destroy (new_positions);
1469 path = gtk_tree_path_new ();
1470 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1471 path, NULL, new_order);
1472 gtk_tree_path_free (path);
1476 save_positions (GSequence *seq)
1478 GHashTable *positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1481 ptr = g_sequence_get_begin_iter (seq);
1482 while (!g_sequence_iter_is_end (ptr))
1484 g_hash_table_insert (positions, ptr,
1485 GINT_TO_POINTER (g_sequence_iter_get_position (ptr)));
1486 ptr = g_sequence_iter_next (ptr);
1493 generate_order (GSequence *seq,
1494 GHashTable *old_positions)
1497 int *order = g_new (int, g_sequence_get_length (seq));
1501 ptr = g_sequence_get_begin_iter (seq);
1502 while (!g_sequence_iter_is_end (ptr))
1504 int old_pos = GPOINTER_TO_INT (g_hash_table_lookup (old_positions, ptr));
1505 order[i++] = old_pos;
1506 ptr = g_sequence_iter_next (ptr);
1509 g_hash_table_destroy (old_positions);
1515 * gtk_list_store_swap:
1516 * @store: A #GtkListStore.
1517 * @a: A #GtkTreeIter.
1518 * @b: Another #GtkTreeIter.
1520 * Swaps @a and @b in @store. Note that this function only works with
1526 gtk_list_store_swap (GtkListStore *store,
1530 GHashTable *old_positions;
1534 g_return_if_fail (GTK_IS_LIST_STORE (store));
1535 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1536 g_return_if_fail (VALID_ITER (a, store));
1537 g_return_if_fail (VALID_ITER (b, store));
1539 if (a->user_data == b->user_data)
1542 old_positions = save_positions (store->seq);
1544 g_sequence_swap (a->user_data, b->user_data);
1546 order = generate_order (store->seq, old_positions);
1547 path = gtk_tree_path_new ();
1549 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1552 gtk_tree_path_free (path);
1557 gtk_list_store_move_to (GtkListStore *store,
1561 GHashTable *old_positions;
1565 old_positions = save_positions (store->seq);
1567 g_sequence_move (iter->user_data, g_sequence_get_iter_at_pos (store->seq, new_pos));
1569 order = generate_order (store->seq, old_positions);
1571 path = gtk_tree_path_new ();
1572 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1574 gtk_tree_path_free (path);
1579 * gtk_list_store_move_before:
1580 * @store: A #GtkListStore.
1581 * @iter: A #GtkTreeIter.
1582 * @position: (allow-none): A #GtkTreeIter, or %NULL.
1584 * Moves @iter in @store to the position before @position. Note that this
1585 * function only works with unsorted stores. If @position is %NULL, @iter
1586 * will be moved to the end of the list.
1591 gtk_list_store_move_before (GtkListStore *store,
1593 GtkTreeIter *position)
1597 g_return_if_fail (GTK_IS_LIST_STORE (store));
1598 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1599 g_return_if_fail (VALID_ITER (iter, store));
1601 g_return_if_fail (VALID_ITER (position, store));
1604 pos = g_sequence_iter_get_position (position->user_data);
1608 gtk_list_store_move_to (store, iter, pos);
1612 * gtk_list_store_move_after:
1613 * @store: A #GtkListStore.
1614 * @iter: A #GtkTreeIter.
1615 * @position: (allow-none): A #GtkTreeIter or %NULL.
1617 * Moves @iter in @store to the position after @position. Note that this
1618 * function only works with unsorted stores. If @position is %NULL, @iter
1619 * will be moved to the start of the list.
1624 gtk_list_store_move_after (GtkListStore *store,
1626 GtkTreeIter *position)
1630 g_return_if_fail (GTK_IS_LIST_STORE (store));
1631 g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1632 g_return_if_fail (VALID_ITER (iter, store));
1634 g_return_if_fail (VALID_ITER (position, store));
1637 pos = g_sequence_iter_get_position (position->user_data) + 1;
1641 gtk_list_store_move_to (store, iter, pos);
1646 gtk_list_store_compare_func (GSequenceIter *a,
1650 GtkListStore *list_store = user_data;
1654 GtkTreeIterCompareFunc func;
1657 if (list_store->sort_column_id != -1)
1659 GtkTreeDataSortHeader *header;
1661 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1662 list_store->sort_column_id);
1663 g_return_val_if_fail (header != NULL, 0);
1664 g_return_val_if_fail (header->func != NULL, 0);
1666 func = header->func;
1667 data = header->data;
1671 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1672 func = list_store->default_sort_func;
1673 data = list_store->default_sort_data;
1676 iter_a.stamp = list_store->stamp;
1677 iter_a.user_data = (gpointer)a;
1678 iter_b.stamp = list_store->stamp;
1679 iter_b.user_data = (gpointer)b;
1681 g_assert (VALID_ITER (&iter_a, list_store));
1682 g_assert (VALID_ITER (&iter_b, list_store));
1684 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1686 if (list_store->order == GTK_SORT_DESCENDING)
1690 else if (retval < 0)
1698 gtk_list_store_sort (GtkListStore *list_store)
1702 GHashTable *old_positions;
1704 if (!GTK_LIST_STORE_IS_SORTED (list_store) ||
1705 g_sequence_get_length (list_store->seq) <= 1)
1708 old_positions = save_positions (list_store->seq);
1710 g_sequence_sort_iter (list_store->seq, gtk_list_store_compare_func, list_store);
1712 /* Let the world know about our new order */
1713 new_order = generate_order (list_store->seq, old_positions);
1715 path = gtk_tree_path_new ();
1716 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1717 path, NULL, new_order);
1718 gtk_tree_path_free (path);
1723 iter_is_sorted (GtkListStore *list_store,
1728 if (!g_sequence_iter_is_begin (iter->user_data))
1730 cmp = g_sequence_iter_prev (iter->user_data);
1731 if (gtk_list_store_compare_func (cmp, iter->user_data, list_store) > 0)
1735 cmp = g_sequence_iter_next (iter->user_data);
1736 if (!g_sequence_iter_is_end (cmp))
1738 if (gtk_list_store_compare_func (iter->user_data, cmp, list_store) > 0)
1746 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1753 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1754 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
1755 gtk_tree_path_free (path);
1757 if (!iter_is_sorted (list_store, iter))
1759 GHashTable *old_positions;
1762 old_positions = save_positions (list_store->seq);
1763 g_sequence_sort_changed_iter (iter->user_data,
1764 gtk_list_store_compare_func,
1766 order = generate_order (list_store->seq, old_positions);
1767 path = gtk_tree_path_new ();
1768 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1770 gtk_tree_path_free (path);
1776 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1777 gint *sort_column_id,
1780 GtkListStore *list_store = (GtkListStore *) sortable;
1783 * sort_column_id = list_store->sort_column_id;
1785 * order = list_store->order;
1787 if (list_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
1788 list_store->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
1795 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1796 gint sort_column_id,
1799 GtkListStore *list_store = (GtkListStore *) sortable;
1801 if ((list_store->sort_column_id == sort_column_id) &&
1802 (list_store->order == order))
1805 if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
1807 if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1809 GtkTreeDataSortHeader *header = NULL;
1811 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1814 /* We want to make sure that we have a function */
1815 g_return_if_fail (header != NULL);
1816 g_return_if_fail (header->func != NULL);
1820 g_return_if_fail (list_store->default_sort_func != NULL);
1825 list_store->sort_column_id = sort_column_id;
1826 list_store->order = order;
1828 gtk_tree_sortable_sort_column_changed (sortable);
1830 gtk_list_store_sort (list_store);
1834 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
1835 gint sort_column_id,
1836 GtkTreeIterCompareFunc func,
1838 GDestroyNotify destroy)
1840 GtkListStore *list_store = (GtkListStore *) sortable;
1842 list_store->sort_list = _gtk_tree_data_list_set_header (list_store->sort_list,
1844 func, data, destroy);
1846 if (list_store->sort_column_id == sort_column_id)
1847 gtk_list_store_sort (list_store);
1851 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
1852 GtkTreeIterCompareFunc func,
1854 GDestroyNotify destroy)
1856 GtkListStore *list_store = (GtkListStore *) sortable;
1858 if (list_store->default_sort_destroy)
1860 GDestroyNotify d = list_store->default_sort_destroy;
1862 list_store->default_sort_destroy = NULL;
1863 d (list_store->default_sort_data);
1866 list_store->default_sort_func = func;
1867 list_store->default_sort_data = data;
1868 list_store->default_sort_destroy = destroy;
1870 if (list_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1871 gtk_list_store_sort (list_store);
1875 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
1877 GtkListStore *list_store = (GtkListStore *) sortable;
1879 return (list_store->default_sort_func != NULL);
1884 * gtk_list_store_insert_with_values:
1885 * @list_store: A #GtkListStore
1886 * @iter: (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL.
1887 * @position: position to insert the new row
1888 * @Varargs: pairs of column number and value, terminated with -1
1890 * Creates a new row at @position. @iter will be changed to point to this new
1891 * row. If @position is larger than the number of rows on the list, then the
1892 * new row will be appended to the list. The row will be filled with the
1893 * values given to this function.
1896 * <literal>gtk_list_store_insert_with_values(list_store, iter, position...)</literal>
1897 * has the same effect as calling
1899 * gtk_list_store_insert (list_store, iter, position);
1900 * gtk_list_store_set (list_store, iter, ...);
1902 * with the difference that the former will only emit a row_inserted signal,
1903 * while the latter will emit row_inserted, row_changed and, if the list store
1904 * is sorted, rows_reordered. Since emitting the rows_reordered signal
1905 * repeatedly can affect the performance of the program,
1906 * gtk_list_store_insert_with_values() should generally be preferred when
1907 * inserting rows in a sorted list store.
1912 gtk_list_store_insert_with_values (GtkListStore *list_store,
1920 GtkTreeIter tmp_iter;
1922 gboolean changed = FALSE;
1923 gboolean maybe_need_sort = FALSE;
1926 /* FIXME: refactor to reduce overlap with gtk_list_store_set() */
1927 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1932 list_store->columns_dirty = TRUE;
1934 seq = list_store->seq;
1936 length = g_sequence_get_length (seq);
1937 if (position > length)
1940 ptr = g_sequence_get_iter_at_pos (seq, position);
1941 ptr = g_sequence_insert_before (ptr, NULL);
1943 iter->stamp = list_store->stamp;
1944 iter->user_data = ptr;
1946 g_assert (VALID_ITER (iter, list_store));
1948 list_store->length++;
1950 va_start (var_args, position);
1951 gtk_list_store_set_valist_internal (list_store, iter,
1952 &changed, &maybe_need_sort,
1956 /* Don't emit rows_reordered here */
1957 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
1958 g_sequence_sort_changed_iter (iter->user_data,
1959 gtk_list_store_compare_func,
1962 /* Just emit row_inserted */
1963 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1964 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1965 gtk_tree_path_free (path);
1970 * gtk_list_store_insert_with_valuesv:
1971 * @list_store: A #GtkListStore
1972 * @iter: (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL.
1973 * @position: position to insert the new row
1974 * @columns: an array of column numbers
1975 * @values: an array of GValues
1976 * @n_values: the length of the @columns and @values arrays
1978 * A variant of gtk_list_store_insert_with_values() which
1979 * takes the columns and values as two arrays, instead of
1980 * varargs. This function is mainly intended for
1981 * language-bindings.
1986 gtk_list_store_insert_with_valuesv (GtkListStore *list_store,
1996 GtkTreeIter tmp_iter;
1998 gboolean changed = FALSE;
1999 gboolean maybe_need_sort = FALSE;
2001 /* FIXME refactor to reduce overlap with
2002 * gtk_list_store_insert_with_values()
2004 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2009 list_store->columns_dirty = TRUE;
2011 seq = list_store->seq;
2013 length = g_sequence_get_length (seq);
2014 if (position > length)
2017 ptr = g_sequence_get_iter_at_pos (seq, position);
2018 ptr = g_sequence_insert_before (ptr, NULL);
2020 iter->stamp = list_store->stamp;
2021 iter->user_data = ptr;
2023 g_assert (VALID_ITER (iter, list_store));
2025 list_store->length++;
2027 gtk_list_store_set_vector_internal (list_store, iter,
2028 &changed, &maybe_need_sort,
2029 columns, values, n_values);
2031 /* Don't emit rows_reordered here */
2032 if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2033 g_sequence_sort_changed_iter (iter->user_data,
2034 gtk_list_store_compare_func,
2037 /* Just emit row_inserted */
2038 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2039 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2040 gtk_tree_path_free (path);
2043 /* GtkBuildable custom tag implementation
2046 * <column type="..."/>
2047 * <column type="..."/>
2051 gboolean translatable;
2057 GtkBuilder *builder;
2059 GSList *column_type_names;
2060 GType *column_types;
2069 const gchar *domain;
2073 list_store_start_element (GMarkupParseContext *context,
2074 const gchar *element_name,
2075 const gchar **names,
2076 const gchar **values,
2081 SubParserData *data = (SubParserData*)user_data;
2083 if (strcmp (element_name, "col") == 0)
2086 gchar *context = NULL;
2087 gboolean translatable = FALSE;
2090 if (data->row_column >= data->n_columns)
2092 g_set_error (error, data->error_quark, 0,
2093 "Too many columns, maximum is %d\n", data->n_columns - 1);
2097 for (i = 0; names[i]; i++)
2098 if (strcmp (names[i], "id") == 0)
2101 id = atoi (values[i]);
2104 g_set_error (error, data->error_quark, 0,
2105 "the id tag %s could not be converted to an integer",
2109 if (id < 0 || id >= data->n_columns)
2111 g_set_error (error, data->error_quark, 0,
2112 "id value %d out of range", id);
2116 else if (strcmp (names[i], "translatable") == 0)
2118 if (!_gtk_builder_boolean_from_string (values[i], &translatable,
2122 else if (strcmp (names[i], "comments") == 0)
2124 /* do nothing, comments are for translators */
2126 else if (strcmp (names[i], "context") == 0)
2128 context = g_strdup (values[i]);
2133 g_set_error (error, data->error_quark, 0,
2134 "<col> needs an id attribute");
2138 info = g_slice_new0 (ColInfo);
2139 info->translatable = translatable;
2140 info->context = context;
2143 data->colids[data->row_column] = id;
2144 data->columns[data->row_column] = info;
2146 data->is_data = TRUE;
2148 else if (strcmp (element_name, "row") == 0)
2150 else if (strcmp (element_name, "column") == 0)
2151 for (i = 0; names[i]; i++)
2152 if (strcmp (names[i], "type") == 0)
2153 data->column_type_names = g_slist_prepend (data->column_type_names,
2154 g_strdup (values[i]));
2155 else if (strcmp (element_name, "columns") == 0)
2157 else if (strcmp (element_name, "data") == 0)
2160 g_set_error (error, data->error_quark, 0,
2161 "Unknown start tag: %s", element_name);
2165 list_store_end_element (GMarkupParseContext *context,
2166 const gchar *element_name,
2170 SubParserData *data = (SubParserData*)user_data;
2172 g_assert (data->builder);
2174 if (strcmp (element_name, "row") == 0)
2179 gtk_list_store_insert_with_valuesv (GTK_LIST_STORE (data->object),
2185 for (i = 0; i < data->row_column; i++)
2187 ColInfo *info = data->columns[i];
2188 g_free (info->context);
2189 g_slice_free (ColInfo, info);
2190 data->columns[i] = NULL;
2191 g_value_unset (&data->values[i]);
2193 g_free (data->values);
2194 data->values = g_new0 (GValue, data->n_columns);
2196 data->row_column = 0;
2198 else if (strcmp (element_name, "columns") == 0)
2200 GType *column_types;
2205 data->column_type_names = g_slist_reverse (data->column_type_names);
2206 column_types = g_new0 (GType, g_slist_length (data->column_type_names));
2208 for (l = data->column_type_names, i = 0; l; l = l->next, i++)
2210 type = gtk_builder_get_type_from_name (data->builder, l->data);
2211 if (type == G_TYPE_INVALID)
2213 g_warning ("Unknown type %s specified in treemodel %s",
2214 (const gchar*)l->data,
2215 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
2218 column_types[i] = type;
2223 gtk_list_store_set_column_types (GTK_LIST_STORE (data->object), i,
2226 g_free (column_types);
2228 else if (strcmp (element_name, "col") == 0)
2229 data->is_data = FALSE;
2230 else if (strcmp (element_name, "data") == 0)
2232 else if (strcmp (element_name, "column") == 0)
2235 g_set_error (error, data->error_quark, 0,
2236 "Unknown end tag: %s", element_name);
2240 list_store_text (GMarkupParseContext *context,
2246 SubParserData *data = (SubParserData*)user_data;
2248 GError *tmp_error = NULL;
2255 i = data->row_column - 1;
2256 info = data->columns[i];
2258 string = g_strndup (text, text_len);
2259 if (info->translatable && text_len)
2263 /* FIXME: This will not use the domain set in the .ui file,
2264 * since the parser is not telling the builder about the domain.
2265 * However, it will work for gtk_builder_set_translation_domain() calls.
2267 translated = _gtk_builder_parser_translate (data->domain,
2271 string = translated;
2274 if (!gtk_builder_value_from_string_type (data->builder,
2275 data->column_types[info->id],
2283 "Could not convert '%s' to type %s: %s\n",
2284 text, g_type_name (data->column_types[info->id]),
2285 tmp_error->message);
2286 g_error_free (tmp_error);
2291 static const GMarkupParser list_store_parser =
2293 list_store_start_element,
2294 list_store_end_element,
2299 gtk_list_store_buildable_custom_tag_start (GtkBuildable *buildable,
2300 GtkBuilder *builder,
2302 const gchar *tagname,
2303 GMarkupParser *parser,
2306 SubParserData *parser_data;
2311 if (strcmp (tagname, "columns") == 0)
2314 parser_data = g_slice_new0 (SubParserData);
2315 parser_data->builder = builder;
2316 parser_data->object = G_OBJECT (buildable);
2317 parser_data->column_type_names = NULL;
2319 *parser = list_store_parser;
2320 *data = parser_data;
2323 else if (strcmp (tagname, "data") == 0)
2325 gint n_columns = gtk_list_store_get_n_columns (GTK_TREE_MODEL (buildable));
2327 g_error ("Cannot append data to an empty model");
2329 parser_data = g_slice_new0 (SubParserData);
2330 parser_data->builder = builder;
2331 parser_data->object = G_OBJECT (buildable);
2332 parser_data->values = g_new0 (GValue, n_columns);
2333 parser_data->colids = g_new0 (gint, n_columns);
2334 parser_data->columns = g_new0 (ColInfo*, n_columns);
2335 parser_data->column_types = GTK_LIST_STORE (buildable)->column_headers;
2336 parser_data->n_columns = n_columns;
2337 parser_data->last_row = 0;
2338 parser_data->error_quark = g_quark_from_static_string ("GtkListStore");
2339 parser_data->domain = gtk_builder_get_translation_domain (builder);
2341 *parser = list_store_parser;
2342 *data = parser_data;
2346 g_warning ("Unknown custom list store tag: %s", tagname);
2352 gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
2353 GtkBuilder *builder,
2355 const gchar *tagname,
2358 SubParserData *sub = (SubParserData*)data;
2360 if (strcmp (tagname, "columns") == 0)
2362 g_slist_free (sub->column_type_names);
2363 g_slice_free (SubParserData, sub);
2365 else if (strcmp (tagname, "data") == 0)
2368 for (i = 0; i < sub->n_columns; i++)
2370 ColInfo *info = sub->columns[i];
2373 g_free (info->context);
2374 g_slice_free (ColInfo, info);
2377 g_free (sub->colids);
2378 g_free (sub->columns);
2379 g_free (sub->values);
2380 g_slice_free (SubParserData, sub);
2383 g_warning ("Unknown custom list store tag: %s", tagname);
2386 #define __GTK_LIST_STORE_C__
2387 #include "gtkaliasdef.c"