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.
21 #include "gtktreemodel.h"
22 #include "gtkliststore.h"
23 #include "gtktreedatalist.h"
24 #include "gtksignal.h"
25 #include "gtktreednd.h"
26 #include <gobject/gvaluecollector.h>
28 #define G_SLIST(x) ((GSList *) x)
29 #define GTK_LIST_STORE_IS_SORTED(list) (GTK_LIST_STORE (list)->sort_column_id != -2)
30 #define VALID_ITER(iter, list_store) (iter!= NULL && iter->user_data != NULL && list_store->stamp == iter->stamp)
32 static void gtk_list_store_init (GtkListStore *list_store);
33 static void gtk_list_store_class_init (GtkListStoreClass *class);
34 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
35 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
36 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
37 static void gtk_list_store_sortable_init (GtkTreeSortableIface *iface);
38 static guint gtk_list_store_get_flags (GtkTreeModel *tree_model);
39 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
40 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
42 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
45 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
47 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
51 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
53 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
56 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
58 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
60 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
64 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
69 static void gtk_list_store_set_n_columns (GtkListStore *list_store,
71 static void gtk_list_store_set_column_type (GtkListStore *list_store,
77 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
79 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
81 GtkSelectionData *selection_data);
82 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
84 GtkSelectionData *selection_data);
85 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
86 GtkTreeModel *src_model,
87 GtkTreePath *src_path,
88 GtkTreePath *dest_path);
92 static void gtk_list_store_sort (GtkListStore *list_store);
93 static void gtk_list_store_sort_iter_changed (GtkListStore *list_store,
96 static gboolean gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
99 static void gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
102 static void gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
104 GtkTreeIterCompareFunc func,
106 GtkDestroyNotify destroy);
107 static void gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
108 GtkTreeIterCompareFunc func,
110 GtkDestroyNotify destroy);
111 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable);
116 validate_list_store (GtkListStore *list_store)
118 if (gtk_debug_flags & GTK_DEBUG_TREE)
120 g_assert (g_slist_length (list_store->root) == list_store->length);
122 g_assert (g_slist_last (list_store->root) == list_store->tail);
127 gtk_list_store_get_type (void)
129 static GType list_store_type = 0;
131 if (!list_store_type)
133 static const GTypeInfo list_store_info =
135 sizeof (GtkListStoreClass),
136 NULL, /* base_init */
137 NULL, /* base_finalize */
138 (GClassInitFunc) gtk_list_store_class_init,
139 NULL, /* class_finalize */
140 NULL, /* class_data */
141 sizeof (GtkListStore),
143 (GInstanceInitFunc) gtk_list_store_init,
146 static const GInterfaceInfo tree_model_info =
148 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
153 static const GInterfaceInfo drag_source_info =
155 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
160 static const GInterfaceInfo drag_dest_info =
162 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
167 static const GInterfaceInfo sortable_info =
169 (GInterfaceInitFunc) gtk_list_store_sortable_init,
174 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
175 g_type_add_interface_static (list_store_type,
178 g_type_add_interface_static (list_store_type,
179 GTK_TYPE_TREE_DRAG_SOURCE,
181 g_type_add_interface_static (list_store_type,
182 GTK_TYPE_TREE_DRAG_DEST,
184 g_type_add_interface_static (list_store_type,
185 GTK_TYPE_TREE_SORTABLE,
189 return list_store_type;
193 gtk_list_store_class_init (GtkListStoreClass *class)
195 GObjectClass *object_class;
197 object_class = (GObjectClass*) class;
201 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
203 iface->get_flags = gtk_list_store_get_flags;
204 iface->get_n_columns = gtk_list_store_get_n_columns;
205 iface->get_column_type = gtk_list_store_get_column_type;
206 iface->get_iter = gtk_list_store_get_iter;
207 iface->get_path = gtk_list_store_get_path;
208 iface->get_value = gtk_list_store_get_value;
209 iface->iter_next = gtk_list_store_iter_next;
210 iface->iter_children = gtk_list_store_iter_children;
211 iface->iter_has_child = gtk_list_store_iter_has_child;
212 iface->iter_n_children = gtk_list_store_iter_n_children;
213 iface->iter_nth_child = gtk_list_store_iter_nth_child;
214 iface->iter_parent = gtk_list_store_iter_parent;
218 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
220 iface->drag_data_delete = gtk_list_store_drag_data_delete;
221 iface->drag_data_get = gtk_list_store_drag_data_get;
225 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
227 iface->drag_data_received = gtk_list_store_drag_data_received;
228 iface->row_drop_possible = gtk_list_store_row_drop_possible;
232 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
234 iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
235 iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
236 iface->set_sort_func = gtk_list_store_set_sort_func;
237 iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
238 iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
242 gtk_list_store_init (GtkListStore *list_store)
244 list_store->root = NULL;
245 list_store->tail = NULL;
246 list_store->sort_list = NULL;
247 list_store->stamp = g_random_int ();
248 list_store->length = 0;
249 list_store->sort_column_id = -2;
253 * gtk_list_store_new:
254 * @n_columns: number of columns in the list store
255 * @Varargs: all #GType types for the columns, from first to last
257 * Creates a new list store as with @n_columns columns each of the types passed
258 * in. As an example, gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
259 * GDK_TYPE_PIXBUF); will create a new GtkListStore with three columns, of type
260 * int, string and GDkPixbuf respectively.
262 * Return value: a new #GtkListStore
265 gtk_list_store_new (gint n_columns,
268 GtkListStore *retval;
272 g_return_val_if_fail (n_columns > 0, NULL);
274 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
275 gtk_list_store_set_n_columns (retval, n_columns);
277 va_start (args, n_columns);
279 for (i = 0; i < n_columns; i++)
281 GType type = va_arg (args, GType);
282 if (! _gtk_tree_data_list_check_type (type))
284 g_warning ("%s: Invalid type %s passed to gtk_list_store_new\n", G_STRLOC, g_type_name (type));
285 g_object_unref (G_OBJECT (retval));
289 gtk_list_store_set_column_type (retval, i, type);
299 * gtk_list_store_newv:
300 * @n_columns: number of columns in the list store
301 * @types: an array of #GType types for the columns, from first to last
303 * Non vararg creation function. Used primarily by language bindings.
305 * Return value: a new #GtkListStore
308 gtk_list_store_newv (gint n_columns,
311 GtkListStore *retval;
314 g_return_val_if_fail (n_columns > 0, NULL);
316 retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
317 gtk_list_store_set_n_columns (retval, n_columns);
319 for (i = 0; i < n_columns; i++)
321 if (! _gtk_tree_data_list_check_type (types[i]))
323 g_warning ("%s: Invalid type %s passed to gtk_list_store_newv\n", G_STRLOC, g_type_name (types[i]));
324 g_object_unref (G_OBJECT (retval));
328 gtk_list_store_set_column_type (retval, i, types[i]);
335 gtk_list_store_set_n_columns (GtkListStore *list_store,
340 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
341 g_return_if_fail (n_columns > 0);
343 if (list_store->n_columns == n_columns)
346 new_columns = g_new0 (GType, n_columns);
347 if (list_store->column_headers)
349 /* copy the old header orders over */
350 if (n_columns >= list_store->n_columns)
351 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
353 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
355 g_free (list_store->column_headers);
358 if (list_store->sort_list)
359 _gtk_tree_data_list_header_free (list_store->sort_list);
361 list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
363 list_store->column_headers = new_columns;
364 list_store->n_columns = n_columns;
368 gtk_list_store_set_column_type (GtkListStore *list_store,
372 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
373 g_return_if_fail (column >=0 && column < list_store->n_columns);
374 if (!_gtk_tree_data_list_check_type (type))
376 g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_type\n", G_STRLOC, g_type_name (type));
380 list_store->column_headers[column] = type;
383 /* Fulfill the GtkTreeModel requirements */
385 gtk_list_store_get_flags (GtkTreeModel *tree_model)
387 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
389 return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
393 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
395 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
397 return GTK_LIST_STORE (tree_model)->n_columns;
401 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
404 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
405 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
406 index >= 0, G_TYPE_INVALID);
408 return GTK_LIST_STORE (tree_model)->column_headers[index];
412 gtk_list_store_get_iter (GtkTreeModel *tree_model,
419 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
420 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
422 i = gtk_tree_path_get_indices (path)[0];
424 if (i >= GTK_LIST_STORE (tree_model)->length)
427 list = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), i);
429 /* If this fails, list_store->length has gotten mangled. */
432 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
433 iter->user_data = list;
438 gtk_list_store_get_path (GtkTreeModel *tree_model,
445 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
446 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
448 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
450 if (list == G_SLIST (iter->user_data))
457 retval = gtk_tree_path_new ();
458 gtk_tree_path_append_index (retval, i);
463 gtk_list_store_get_value (GtkTreeModel *tree_model,
468 GtkTreeDataList *list;
469 gint tmp_column = column;
471 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
472 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
473 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
475 list = G_SLIST (iter->user_data)->data;
477 while (tmp_column-- > 0 && list)
481 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
483 _gtk_tree_data_list_node_to_value (list,
484 GTK_LIST_STORE (tree_model)->column_headers[column],
489 gtk_list_store_iter_next (GtkTreeModel *tree_model,
492 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
493 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
495 iter->user_data = G_SLIST (iter->user_data)->next;
497 return (iter->user_data != NULL);
501 gtk_list_store_iter_children (GtkTreeModel *tree_model,
505 /* this is a list, nodes have no children */
509 /* but if parent == NULL we return the list itself as children of the
513 if (GTK_LIST_STORE (tree_model)->root)
515 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
516 iter->user_data = GTK_LIST_STORE (tree_model)->root;
524 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
531 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
534 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
536 return GTK_LIST_STORE (tree_model)->length;
538 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
543 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
550 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
555 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
559 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
560 iter->user_data = child;
568 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
576 * gtk_list_store_set_value:
577 * @list_store: A #GtkListStore
578 * @iter: A valid #GtkTreeIter for the row being modified
579 * @column: column number to modify
580 * @value: new value for the cell
582 * Sets the data in the cell specified by @iter and @column.
583 * The type of @value must be convertible to the type of the
588 gtk_list_store_set_value (GtkListStore *list_store,
593 GtkTreeDataList *list;
594 GtkTreeDataList *prev;
596 GValue real_value = {0, };
597 gboolean converted = FALSE;
598 gint orig_column = column;
600 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
601 g_return_if_fail (VALID_ITER (iter, list_store));
602 g_return_if_fail (column >= 0 && column < list_store->n_columns);
603 g_return_if_fail (G_IS_VALUE (value));
605 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
607 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
608 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
610 g_warning ("%s: Unable to convert from %s to %s\n",
612 g_type_name (G_VALUE_TYPE (value)),
613 g_type_name (list_store->column_headers[column]));
616 if (!g_value_transform (value, &real_value))
618 g_warning ("%s: Unable to make conversion from %s to %s\n",
620 g_type_name (G_VALUE_TYPE (value)),
621 g_type_name (list_store->column_headers[column]));
622 g_value_unset (&real_value);
628 prev = list = G_SLIST (iter->user_data)->data;
634 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
636 _gtk_tree_data_list_value_to_node (list, &real_value);
638 _gtk_tree_data_list_value_to_node (list, value);
639 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
640 gtk_tree_path_free (path);
642 g_value_unset (&real_value);
651 if (G_SLIST (iter->user_data)->data == NULL)
653 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
658 list = prev->next = _gtk_tree_data_list_alloc ();
664 list->next = _gtk_tree_data_list_alloc ();
670 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
672 _gtk_tree_data_list_value_to_node (list, &real_value);
674 _gtk_tree_data_list_value_to_node (list, value);
675 gtk_tree_model_range_changed (GTK_TREE_MODEL (list_store), path, iter, path, iter);
676 gtk_tree_path_free (path);
678 g_value_unset (&real_value);
680 if (GTK_LIST_STORE_IS_SORTED (list_store))
681 gtk_list_store_sort_iter_changed (list_store, iter, orig_column);
685 * gtk_list_store_set_valist:
686 * @list_store: A #GtkListStore
687 * @iter: A valid #GtkTreeIter for the row being modified
688 * @var_args: va_list of column/value pairs
690 * See @gtk_list_store_set; this version takes a va_list for
691 * use by language bindings.
695 gtk_list_store_set_valist (GtkListStore *list_store,
701 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
702 g_return_if_fail (VALID_ITER (iter, list_store));
704 column = va_arg (var_args, gint);
708 GValue value = { 0, };
711 if (column >= list_store->n_columns)
713 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
716 g_value_init (&value, list_store->column_headers[column]);
718 G_VALUE_COLLECT (&value, var_args, 0, &error);
721 g_warning ("%s: %s", G_STRLOC, error);
724 /* we purposely leak the value here, it might not be
725 * in a sane state if an error condition occoured
730 /* FIXME: instead of calling this n times, refactor with above */
731 gtk_list_store_set_value (list_store,
736 g_value_unset (&value);
738 column = va_arg (var_args, gint);
743 * gtk_list_store_set:
744 * @list_store: a #GtkListStore
745 * @iter: row iterator
746 * @Varargs: pairs of column number and value, terminated with -1
748 * Sets the value of one or more cells in the row referenced by @iter.
749 * The variable argument list should contain integer column numbers,
750 * each column number followed by the value to be set.
751 * The list is terminated by a -1. For example, to set column 0 with type
752 * %G_TYPE_STRING to "Foo", you would write gtk_list_store_set (store, iter,
756 gtk_list_store_set (GtkListStore *list_store,
762 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
763 g_return_if_fail (iter != NULL);
764 g_return_if_fail (iter->stamp == list_store->stamp);
766 va_start (var_args, iter);
767 gtk_list_store_set_valist (list_store, iter, var_args);
772 remove_link_saving_prev (GSList *list,
787 prev->next = link->next;
806 gtk_list_store_remove_silently (GtkListStore *list_store,
810 if (G_SLIST (iter->user_data)->data)
812 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
813 list_store->column_headers);
814 G_SLIST (iter->user_data)->data = NULL;
820 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
821 G_SLIST (iter->user_data),
824 list_store->length -= 1;
826 if (iter->user_data == list_store->tail)
827 list_store->tail = prev;
830 list_store->stamp ++;
834 * gtk_list_store_remove:
835 * @list_store: A #GtkListStore
836 * @iter: A valid #GtkTreeIter
838 * Removes the given row from the list store. After being removed, @iter is set to be the next valid row, or invalidated if it pointed to the last row inn @list_store
842 gtk_list_store_remove (GtkListStore *list_store,
848 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
849 g_return_if_fail (VALID_ITER (iter, list_store));
851 next = G_SLIST (iter->user_data)->next;
852 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
854 validate_list_store (list_store);
856 gtk_list_store_remove_silently (list_store, iter, path);
858 validate_list_store (list_store);
860 list_store->stamp ++;
861 gtk_tree_model_deleted (GTK_TREE_MODEL (list_store), path);
862 gtk_tree_path_free (path);
866 iter->stamp = list_store->stamp;
867 iter->user_data = next;
876 insert_after (GtkListStore *list_store,
880 g_return_if_fail (sibling != NULL);
881 g_return_if_fail (new_list != NULL);
883 /* insert new node after list */
884 new_list->next = sibling->next;
885 sibling->next = new_list;
887 /* if list was the tail, the new node is the new tail */
888 if (sibling == ((GSList *) list_store->tail))
889 list_store->tail = new_list;
891 list_store->length += 1;
895 * gtk_list_store_insert:
896 * @list_store: A #GtkListStore
897 * @iter: An unset #GtkTreeIter to set to the new row
898 * @position: position to insert the new row
900 * Creates a new row at @position. @iter will be changed to point to this new
901 * row. If @position is larger than the number of rows on the list, then the
902 * new row will be appended to the list. The row will be empty before this
903 * function is called. To fill in values, you need to call @gtk_list_store_set
904 * or @gtk_list_store_set_value.
908 gtk_list_store_insert (GtkListStore *list_store,
916 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
917 g_return_if_fail (iter != NULL);
918 g_return_if_fail (position >= 0);
921 GTK_LIST_STORE_IS_SORTED (list_store))
923 gtk_list_store_prepend (list_store, iter);
927 new_list = g_slist_alloc ();
929 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
933 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
937 insert_after (list_store, list, new_list);
939 iter->stamp = list_store->stamp;
940 iter->user_data = new_list;
942 validate_list_store (list_store);
944 path = gtk_tree_path_new ();
945 gtk_tree_path_append_index (path, position);
946 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
947 gtk_tree_path_free (path);
951 * gtk_list_store_insert_before:
952 * @list_store: A #GtkListStore
953 * @iter: An unset #GtkTreeIter to set to the new row
954 * @sibling: A valid #GtkTreeIter, or %NULL
956 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
957 * appended to the beginning of the list. @iter will be changed to point to
958 * this new row. The row will be empty before this function is called. To fill
959 * in values, you need to call @gtk_list_store_set or @gtk_list_store_set_value.
963 gtk_list_store_insert_before (GtkListStore *list_store,
965 GtkTreeIter *sibling)
968 GSList *list, *prev, *new_list;
971 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
972 g_return_if_fail (iter != NULL);
974 g_return_if_fail (VALID_ITER (sibling, list_store));
977 if (GTK_LIST_STORE_IS_SORTED (list_store))
979 gtk_list_store_prepend (list_store, iter);
985 gtk_list_store_append (list_store, iter);
989 new_list = g_slist_alloc ();
992 list = list_store->root;
993 while (list && list != sibling->user_data)
1000 if (list != sibling->user_data)
1002 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1006 /* if there are no nodes, we become the list tail, otherwise we
1007 * are inserting before any existing nodes so we can't change
1011 if (list_store->root == NULL)
1012 list_store->tail = new_list;
1016 new_list->next = prev->next;
1017 prev->next = new_list;
1021 new_list->next = list_store->root;
1022 list_store->root = new_list;
1025 iter->stamp = list_store->stamp;
1026 iter->user_data = new_list;
1028 list_store->length += 1;
1030 validate_list_store (list_store);
1032 path = gtk_tree_path_new ();
1033 gtk_tree_path_append_index (path, i);
1034 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1035 gtk_tree_path_free (path);
1039 * gtk_list_store_insert_after:
1040 * @list_store: A #GtkListStore
1041 * @iter: An unset #GtkTreeIter to set to the new row
1042 * @sibling: A valid #GtkTreeIter, or %NULL
1044 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1045 * prepended to the beginning of the list. @iter will be changed to point to
1046 * this new row. The row will be empty after this function is called. To fill
1047 * in values, you need to call @gtk_list_store_set or @gtk_list_store_set_value.
1051 gtk_list_store_insert_after (GtkListStore *list_store,
1053 GtkTreeIter *sibling)
1056 GSList *list, *new_list;
1059 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1060 g_return_if_fail (iter != NULL);
1062 g_return_if_fail (VALID_ITER (sibling, list_store));
1064 if (sibling == NULL ||
1065 GTK_LIST_STORE_IS_SORTED (list_store))
1067 gtk_list_store_prepend (list_store, iter);
1071 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1074 g_return_if_fail (list == sibling->user_data);
1076 new_list = g_slist_alloc ();
1078 insert_after (list_store, list, new_list);
1080 iter->stamp = list_store->stamp;
1081 iter->user_data = new_list;
1083 validate_list_store (list_store);
1085 path = gtk_tree_path_new ();
1086 gtk_tree_path_append_index (path, i);
1087 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1088 gtk_tree_path_free (path);
1092 * gtk_list_store_prepend:
1093 * @list_store: A #GtkListStore
1094 * @iter: An unset #GtkTreeIter to set to the prepend row
1096 * Prepend a new row to @list_store. @iter will be changed to point to this new
1097 * row. The row will be empty after this function is called. To fill in
1098 * values, you need to call @gtk_list_store_set or @gtk_list_store_set_value.
1102 gtk_list_store_prepend (GtkListStore *list_store,
1107 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1108 g_return_if_fail (iter != NULL);
1110 iter->stamp = list_store->stamp;
1111 iter->user_data = g_slist_alloc ();
1113 if (list_store->root == NULL)
1114 list_store->tail = iter->user_data;
1116 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1117 list_store->root = iter->user_data;
1119 list_store->length += 1;
1121 validate_list_store (list_store);
1123 path = gtk_tree_path_new ();
1124 gtk_tree_path_append_index (path, 0);
1125 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1126 gtk_tree_path_free (path);
1130 * gtk_list_store_append:
1131 * @list_store: A #GtkListStore
1132 * @iter: An unset #GtkTreeIter to set to the appended row
1134 * Appends a new row to @list_store. @iter will be changed to point to this new
1135 * row. The row will be empty after this function is called. To fill in
1136 * values, you need to call @gtk_list_store_set or @gtk_list_store_set_value.
1140 gtk_list_store_append (GtkListStore *list_store,
1145 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1146 g_return_if_fail (iter != NULL);
1148 if (GTK_LIST_STORE_IS_SORTED (list_store))
1150 gtk_list_store_prepend (list_store, iter);
1154 iter->stamp = list_store->stamp;
1155 iter->user_data = g_slist_alloc ();
1157 if (list_store->tail)
1158 ((GSList *)list_store->tail)->next = iter->user_data;
1160 list_store->root = iter->user_data;
1162 list_store->tail = iter->user_data;
1164 list_store->length += 1;
1166 validate_list_store (list_store);
1168 path = gtk_tree_path_new ();
1169 gtk_tree_path_append_index (path, list_store->length - 1);
1170 gtk_tree_model_inserted (GTK_TREE_MODEL (list_store), path, iter);
1171 gtk_tree_path_free (path);
1175 gtk_list_store_clear (GtkListStore *list_store)
1178 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1180 while (list_store->root)
1182 iter.stamp = list_store->stamp;
1183 iter.user_data = list_store->root;
1184 gtk_list_store_remove (list_store, &iter);
1190 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1194 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1196 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1200 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1207 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1209 GtkSelectionData *selection_data)
1211 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1213 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1214 * target, because the default handler does it for us, but
1215 * we do anyway for the convenience of someone maybe overriding the
1219 if (gtk_selection_data_set_tree_row (selection_data,
1220 GTK_TREE_MODEL (drag_source),
1227 /* FIXME handle text targets at least. */
1234 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1236 GtkSelectionData *selection_data)
1238 GtkTreeModel *tree_model;
1239 GtkListStore *list_store;
1240 GtkTreeModel *src_model = NULL;
1241 GtkTreePath *src_path = NULL;
1242 gboolean retval = FALSE;
1244 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1246 tree_model = GTK_TREE_MODEL (drag_dest);
1247 list_store = GTK_LIST_STORE (drag_dest);
1249 if (gtk_selection_data_get_tree_row (selection_data,
1252 src_model == tree_model)
1254 /* Copy the given row to a new position */
1255 GtkTreeIter src_iter;
1256 GtkTreeIter dest_iter;
1259 if (!gtk_tree_model_get_iter (src_model,
1266 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1267 prev = gtk_tree_path_copy (dest);
1269 if (!gtk_tree_path_prev (prev))
1271 /* dest was the first spot in the list; which means we are supposed
1274 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1281 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1285 GtkTreeIter tmp_iter = dest_iter;
1286 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1293 gtk_tree_path_free (prev);
1295 /* If we succeeded in creating dest_iter, copy data from src
1299 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1300 GtkTreeDataList *copy_head = NULL;
1301 GtkTreeDataList *copy_prev = NULL;
1302 GtkTreeDataList *copy_iter = NULL;
1309 copy_iter = _gtk_tree_data_list_node_copy (dl,
1310 list_store->column_headers[col]);
1312 if (copy_head == NULL)
1313 copy_head = copy_iter;
1316 copy_prev->next = copy_iter;
1318 copy_prev = copy_iter;
1324 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1325 G_SLIST (dest_iter.user_data)->data = copy_head;
1327 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1328 gtk_tree_model_range_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter, path, &dest_iter);
1329 gtk_tree_path_free (path);
1334 /* FIXME maybe add some data targets eventually, or handle text
1335 * targets in the simple case.
1342 gtk_tree_path_free (src_path);
1348 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1349 GtkTreeModel *src_model,
1350 GtkTreePath *src_path,
1351 GtkTreePath *dest_path)
1355 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1357 if (src_model != GTK_TREE_MODEL (drag_dest))
1360 if (gtk_tree_path_get_depth (dest_path) != 1)
1363 /* can drop before any existing node, or before one past any existing. */
1365 indices = gtk_tree_path_get_indices (dest_path);
1367 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1375 typedef struct _SortTuple
1382 gtk_list_store_compare_func (gconstpointer a,
1386 GtkListStore *list_store = user_data;
1387 GSList *el_a; /* Los Angeles? */
1392 GtkTreeIterCompareFunc func;
1396 if (list_store->sort_column_id != -1)
1398 GtkTreeDataSortHeader *header;
1400 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1401 list_store->sort_column_id);
1402 g_return_val_if_fail (header != NULL, 0);
1403 g_return_val_if_fail (header->func != NULL, 0);
1405 func = header->func;
1406 data = header->data;
1410 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1411 func = list_store->default_sort_func;
1412 data = list_store->default_sort_data;
1415 el_a = ((SortTuple *) a)->el;
1416 el_b = ((SortTuple *) b)->el;
1418 iter_a.stamp = list_store->stamp;
1419 iter_a.user_data = el_a;
1420 iter_b.stamp = list_store->stamp;
1421 iter_b.user_data = el_b;
1423 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1425 if (list_store->order == GTK_SORT_DESCENDING)
1429 else if (retval < 0)
1436 gtk_list_store_sort (GtkListStore *list_store)
1445 if (list_store->length <= 1)
1448 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1450 list = G_SLIST (list_store->root);
1452 sort_array = g_array_sized_new (FALSE, FALSE,
1454 list_store->length);
1456 for (i = 0; i < list_store->length; i++)
1460 /* If this fails, we are in an inconsistent state. Bad */
1461 g_return_if_fail (list != NULL);
1465 g_array_append_val (sort_array, tuple);
1470 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1472 for (i = 0; i < list_store->length - 1; i++)
1473 g_array_index (sort_array, SortTuple, i).el->next =
1474 g_array_index (sort_array, SortTuple, i + 1).el;
1475 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1476 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1478 /* Let the world know about our new order */
1479 new_order = g_new (gint, list_store->length);
1480 for (i = 0; i < list_store->length; i++)
1481 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1482 path = gtk_tree_path_new ();
1483 iter.stamp = list_store->stamp;
1484 iter.user_data = NULL;
1485 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1486 path, &iter, new_order);
1487 gtk_tree_path_free (path);
1489 g_array_free (sort_array, TRUE);
1493 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1498 GSList *prev = NULL;
1499 GSList *next = NULL;
1500 GSList *list = G_SLIST (list_store->root);
1501 GtkTreePath *tmp_path;
1502 GtkTreeIter tmp_iter;
1509 GtkTreeIterCompareFunc func;
1512 if (list_store->length < 2)
1515 tmp_iter.stamp = list_store->stamp;
1517 if (list_store->sort_column_id != -1)
1519 GtkTreeDataSortHeader *header;
1520 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1521 list_store->sort_column_id);
1522 g_return_if_fail (header != NULL);
1523 g_return_if_fail (header->func != NULL);
1524 func = header->func;
1525 data = header->data;
1529 g_return_if_fail (list_store->default_sort_func != NULL);
1530 func = list_store->default_sort_func;
1531 data = list_store->default_sort_data;
1534 /* If it's the built in function, we don't sort. */
1535 if (func == gtk_tree_data_list_compare_func &&
1536 list_store->sort_column_id != column)
1540 /* First we find the iter, its prev, and its next */
1543 if (list == G_SLIST (iter->user_data))
1549 g_assert (list != NULL);
1553 /* Check the common case, where we don't need to sort it moved. */
1556 tmp_iter.user_data = prev;
1557 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1562 tmp_iter.user_data = next;
1563 cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1567 if (list_store->order == GTK_SORT_DESCENDING)
1580 if (prev == NULL && cmp_b <= 0)
1582 else if (next == NULL && cmp_a <= 0)
1584 else if (prev != NULL && next != NULL &&
1585 cmp_a <= 0 && cmp_b <= 0)
1588 /* We actually need to sort it */
1589 /* First, remove the old link. */
1592 list_store->root = next;
1596 list_store->tail = prev;
1599 /* FIXME: as an optimization, we can potentially start at next */
1601 list = G_SLIST (list_store->root);
1603 tmp_iter.user_data = list;
1604 if (list_store->order == GTK_SORT_DESCENDING)
1605 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1607 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1609 while ((list->next) && (cmp_a > 0))
1614 tmp_iter.user_data = list;
1615 if (list_store->order == GTK_SORT_DESCENDING)
1616 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1618 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1621 if ((!list->next) && (cmp_a > 0))
1623 list->next = G_SLIST (iter->user_data);
1624 list_store->tail = list->next;
1628 prev->next = G_SLIST (iter->user_data);
1629 G_SLIST (iter->user_data)->next = list;
1633 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1634 list_store->root = G_SLIST (iter->user_data);
1637 /* Emit the reordered signal. */
1638 new_order = g_new (int, list_store->length);
1639 if (old_location < new_location)
1640 for (i = 0; i < list_store->length; i++)
1642 if (i < old_location ||
1645 else if (i >= old_location &&
1647 new_order[i] = i + 1;
1648 else if (i == new_location)
1649 new_order[i] = old_location;
1652 for (i = 0; i < list_store->length; i++)
1654 if (i < new_location ||
1657 else if (i > new_location &&
1659 new_order[i] = i - 1;
1660 else if (i == new_location)
1661 new_order[i] = old_location;
1664 tmp_path = gtk_tree_path_new ();
1665 tmp_iter.user_data = NULL;
1667 gtk_tree_model_reordered (GTK_TREE_MODEL (list_store),
1668 tmp_path, &tmp_iter,
1671 gtk_tree_path_free (tmp_path);
1676 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1677 gint *sort_column_id,
1680 GtkListStore *list_store = (GtkListStore *) sortable;
1682 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1684 if (list_store->sort_column_id == -1)
1688 * sort_column_id = list_store->sort_column_id;
1690 * order = list_store->order;
1695 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1696 gint sort_column_id,
1699 GtkListStore *list_store = (GtkListStore *) sortable;
1701 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1703 if ((list_store->sort_column_id == sort_column_id) &&
1704 (list_store->order == order))
1707 if (sort_column_id != -1)
1709 GtkTreeDataSortHeader *header = NULL;
1711 header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
1713 /* We want to make sure that we have a function */
1714 g_return_if_fail (header != NULL);
1715 g_return_if_fail (header->func != NULL);
1719 g_return_if_fail (list_store->default_sort_func != NULL);
1723 list_store->sort_column_id = sort_column_id;
1724 list_store->order = order;
1726 gtk_list_store_sort (list_store);
1728 gtk_tree_sortable_sort_column_changed (sortable);
1732 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
1733 gint sort_column_id,
1734 GtkTreeIterCompareFunc func,
1736 GtkDestroyNotify destroy)
1738 GtkListStore *list_store = (GtkListStore *) sortable;
1739 GtkTreeDataSortHeader *header = NULL;
1742 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1743 g_return_if_fail (func != NULL);
1745 for (list = list_store->sort_list; list; list = list->next)
1747 header = (GtkTreeDataSortHeader*) list->data;
1748 if (header->sort_column_id == sort_column_id)
1754 header = g_new0 (GtkTreeDataSortHeader, 1);
1755 header->sort_column_id = sort_column_id;
1756 list_store->sort_list = g_list_append (list_store->sort_list, header);
1759 if (header->destroy)
1760 (* header->destroy) (header->data);
1762 header->func = func;
1763 header->data = data;
1764 header->destroy = destroy;
1769 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
1770 GtkTreeIterCompareFunc func,
1772 GtkDestroyNotify destroy)
1774 GtkListStore *list_store = (GtkListStore *) sortable;
1776 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1778 if (list_store->default_sort_destroy)
1779 (* list_store->default_sort_destroy) (list_store->default_sort_data);
1781 list_store->default_sort_func = func;
1782 list_store->default_sort_data = data;
1783 list_store->default_sort_destroy = destroy;
1787 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
1789 GtkListStore *list_store = (GtkListStore *) sortable;
1791 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1793 return (list_store->default_sort_func != NULL);