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_real_set_value (GtkListStore *list_store,
581 GtkTreeDataList *list;
582 GtkTreeDataList *prev;
584 GValue real_value = {0, };
585 gboolean converted = FALSE;
586 gint orig_column = column;
587 gboolean retval = FALSE;
589 g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
590 g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
591 g_return_val_if_fail (column >= 0 && column < list_store->n_columns, FALSE);
592 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
594 if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
596 if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
597 g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
599 g_warning ("%s: Unable to convert from %s to %s\n",
601 g_type_name (G_VALUE_TYPE (value)),
602 g_type_name (list_store->column_headers[column]));
605 if (!g_value_transform (value, &real_value))
607 g_warning ("%s: Unable to make conversion from %s to %s\n",
609 g_type_name (G_VALUE_TYPE (value)),
610 g_type_name (list_store->column_headers[column]));
611 g_value_unset (&real_value);
617 prev = list = G_SLIST (iter->user_data)->data;
623 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
625 _gtk_tree_data_list_value_to_node (list, &real_value);
627 _gtk_tree_data_list_value_to_node (list, value);
629 gtk_tree_path_free (path);
631 g_value_unset (&real_value);
640 if (G_SLIST (iter->user_data)->data == NULL)
642 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
647 list = prev->next = _gtk_tree_data_list_alloc ();
653 list->next = _gtk_tree_data_list_alloc ();
659 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
661 _gtk_tree_data_list_value_to_node (list, &real_value);
663 _gtk_tree_data_list_value_to_node (list, value);
665 gtk_tree_path_free (path);
667 g_value_unset (&real_value);
669 if (GTK_LIST_STORE_IS_SORTED (list_store))
670 gtk_list_store_sort_iter_changed (list_store, iter, orig_column);
677 * gtk_list_store_set_value:
678 * @list_store: A #GtkListStore
679 * @iter: A valid #GtkTreeIter for the row being modified
680 * @column: column number to modify
681 * @value: new value for the cell
683 * Sets the data in the cell specified by @iter and @column.
684 * The type of @value must be convertible to the type of the
689 gtk_list_store_set_value (GtkListStore *list_store,
694 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
695 g_return_if_fail (VALID_ITER (iter, list_store));
696 g_return_if_fail (column >= 0 && column < list_store->n_columns);
697 g_return_if_fail (G_IS_VALUE (value));
699 if (gtk_list_store_real_set_value (list_store, iter, column, value))
703 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
704 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
705 gtk_tree_path_free (path);
710 * gtk_list_store_set_valist:
711 * @list_store: A #GtkListStore
712 * @iter: A valid #GtkTreeIter for the row being modified
713 * @var_args: va_list of column/value pairs
715 * See @gtk_list_store_set; this version takes a va_list for use by language
720 gtk_list_store_set_valist (GtkListStore *list_store,
725 gboolean emit_signal = FALSE;
727 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
728 g_return_if_fail (VALID_ITER (iter, list_store));
730 column = va_arg (var_args, gint);
734 GValue value = { 0, };
737 if (column >= list_store->n_columns)
739 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
742 g_value_init (&value, list_store->column_headers[column]);
744 G_VALUE_COLLECT (&value, var_args, 0, &error);
747 g_warning ("%s: %s", G_STRLOC, error);
750 /* we purposely leak the value here, it might not be
751 * in a sane state if an error condition occoured
756 /* FIXME: instead of calling this n times, refactor with above */
757 emit_signal = gtk_list_store_real_set_value (list_store,
760 &value) || emit_signal;
762 g_value_unset (&value);
764 column = va_arg (var_args, gint);
771 path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
772 gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
773 gtk_tree_path_free (path);
778 * gtk_list_store_set:
779 * @list_store: a #GtkListStore
780 * @iter: row iterator
781 * @Varargs: pairs of column number and value, terminated with -1
783 * Sets the value of one or more cells in the row referenced by @iter.
784 * The variable argument list should contain integer column numbers,
785 * each column number followed by the value to be set.
786 * The list is terminated by a -1. For example, to set column 0 with type
787 * %G_TYPE_STRING to "Foo", you would write gtk_list_store_set (store, iter,
791 gtk_list_store_set (GtkListStore *list_store,
797 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
798 g_return_if_fail (iter != NULL);
799 g_return_if_fail (iter->stamp == list_store->stamp);
801 va_start (var_args, iter);
802 gtk_list_store_set_valist (list_store, iter, var_args);
807 remove_link_saving_prev (GSList *list,
822 prev->next = link->next;
841 gtk_list_store_remove_silently (GtkListStore *list_store,
845 if (G_SLIST (iter->user_data)->data)
847 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
848 list_store->column_headers);
849 G_SLIST (iter->user_data)->data = NULL;
855 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
856 G_SLIST (iter->user_data),
859 list_store->length -= 1;
861 if (iter->user_data == list_store->tail)
862 list_store->tail = prev;
865 list_store->stamp ++;
869 * gtk_list_store_remove:
870 * @list_store: A #GtkListStore
871 * @iter: A valid #GtkTreeIter
873 * 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
877 gtk_list_store_remove (GtkListStore *list_store,
883 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
884 g_return_if_fail (VALID_ITER (iter, list_store));
886 next = G_SLIST (iter->user_data)->next;
887 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
889 validate_list_store (list_store);
891 gtk_list_store_remove_silently (list_store, iter, path);
893 validate_list_store (list_store);
895 list_store->stamp ++;
896 gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
897 gtk_tree_path_free (path);
901 iter->stamp = list_store->stamp;
902 iter->user_data = next;
911 insert_after (GtkListStore *list_store,
915 g_return_if_fail (sibling != NULL);
916 g_return_if_fail (new_list != NULL);
918 /* insert new node after list */
919 new_list->next = sibling->next;
920 sibling->next = new_list;
922 /* if list was the tail, the new node is the new tail */
923 if (sibling == ((GSList *) list_store->tail))
924 list_store->tail = new_list;
926 list_store->length += 1;
930 * gtk_list_store_insert:
931 * @list_store: A #GtkListStore
932 * @iter: An unset #GtkTreeIter to set to the new row
933 * @position: position to insert the new row
935 * Creates a new row at @position. @iter will be changed to point to this new
936 * row. If @position is larger than the number of rows on the list, then the
937 * new row will be appended to the list. The row will be empty before this
938 * function is called. To fill in values, you need to call @gtk_list_store_set
939 * or @gtk_list_store_set_value.
943 gtk_list_store_insert (GtkListStore *list_store,
951 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
952 g_return_if_fail (iter != NULL);
953 g_return_if_fail (position >= 0);
956 GTK_LIST_STORE_IS_SORTED (list_store))
958 gtk_list_store_prepend (list_store, iter);
962 new_list = g_slist_alloc ();
964 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
968 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
972 insert_after (list_store, list, new_list);
974 iter->stamp = list_store->stamp;
975 iter->user_data = new_list;
977 validate_list_store (list_store);
979 path = gtk_tree_path_new ();
980 gtk_tree_path_append_index (path, position);
981 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
982 gtk_tree_path_free (path);
986 * gtk_list_store_insert_before:
987 * @list_store: A #GtkListStore
988 * @iter: An unset #GtkTreeIter to set to the new row
989 * @sibling: A valid #GtkTreeIter, or %NULL
991 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
992 * appended to the beginning of the list. @iter will be changed to point to
993 * this new row. The row will be empty before this function is called. To fill
994 * in values, you need to call @gtk_list_store_set or @gtk_list_store_set_value.
998 gtk_list_store_insert_before (GtkListStore *list_store,
1000 GtkTreeIter *sibling)
1003 GSList *list, *prev, *new_list;
1006 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1007 g_return_if_fail (iter != NULL);
1009 g_return_if_fail (VALID_ITER (sibling, list_store));
1012 if (GTK_LIST_STORE_IS_SORTED (list_store))
1014 gtk_list_store_prepend (list_store, iter);
1018 if (sibling == NULL)
1020 gtk_list_store_append (list_store, iter);
1024 new_list = g_slist_alloc ();
1027 list = list_store->root;
1028 while (list && list != sibling->user_data)
1035 if (list != sibling->user_data)
1037 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1041 /* if there are no nodes, we become the list tail, otherwise we
1042 * are inserting before any existing nodes so we can't change
1046 if (list_store->root == NULL)
1047 list_store->tail = new_list;
1051 new_list->next = prev->next;
1052 prev->next = new_list;
1056 new_list->next = list_store->root;
1057 list_store->root = new_list;
1060 iter->stamp = list_store->stamp;
1061 iter->user_data = new_list;
1063 list_store->length += 1;
1065 validate_list_store (list_store);
1067 path = gtk_tree_path_new ();
1068 gtk_tree_path_append_index (path, i);
1069 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1070 gtk_tree_path_free (path);
1074 * gtk_list_store_insert_after:
1075 * @list_store: A #GtkListStore
1076 * @iter: An unset #GtkTreeIter to set to the new row
1077 * @sibling: 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)
1091 GSList *list, *new_list;
1094 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1095 g_return_if_fail (iter != NULL);
1097 g_return_if_fail (VALID_ITER (sibling, list_store));
1099 if (sibling == NULL ||
1100 GTK_LIST_STORE_IS_SORTED (list_store))
1102 gtk_list_store_prepend (list_store, iter);
1106 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1109 g_return_if_fail (list == sibling->user_data);
1111 new_list = g_slist_alloc ();
1113 insert_after (list_store, list, new_list);
1115 iter->stamp = list_store->stamp;
1116 iter->user_data = new_list;
1118 validate_list_store (list_store);
1120 path = gtk_tree_path_new ();
1121 gtk_tree_path_append_index (path, i);
1122 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1123 gtk_tree_path_free (path);
1127 * gtk_list_store_prepend:
1128 * @list_store: A #GtkListStore
1129 * @iter: An unset #GtkTreeIter to set to the prepend row
1131 * Prepend a new row to @list_store. @iter will be changed to point to this new
1132 * row. The row will be empty after this function is called. To fill in
1133 * values, you need to call @gtk_list_store_set or @gtk_list_store_set_value.
1137 gtk_list_store_prepend (GtkListStore *list_store,
1142 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1143 g_return_if_fail (iter != NULL);
1145 iter->stamp = list_store->stamp;
1146 iter->user_data = g_slist_alloc ();
1148 if (list_store->root == NULL)
1149 list_store->tail = iter->user_data;
1151 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1152 list_store->root = iter->user_data;
1154 list_store->length += 1;
1156 validate_list_store (list_store);
1158 path = gtk_tree_path_new ();
1159 gtk_tree_path_append_index (path, 0);
1160 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1161 gtk_tree_path_free (path);
1165 * gtk_list_store_append:
1166 * @list_store: A #GtkListStore
1167 * @iter: An unset #GtkTreeIter to set to the appended row
1169 * Appends a new row to @list_store. @iter will be changed to point to this new
1170 * row. The row will be empty after this function is called. To fill in
1171 * values, you need to call @gtk_list_store_set or @gtk_list_store_set_value.
1175 gtk_list_store_append (GtkListStore *list_store,
1180 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1181 g_return_if_fail (iter != NULL);
1183 if (GTK_LIST_STORE_IS_SORTED (list_store))
1185 gtk_list_store_prepend (list_store, iter);
1189 iter->stamp = list_store->stamp;
1190 iter->user_data = g_slist_alloc ();
1192 if (list_store->tail)
1193 ((GSList *)list_store->tail)->next = iter->user_data;
1195 list_store->root = iter->user_data;
1197 list_store->tail = iter->user_data;
1199 list_store->length += 1;
1201 validate_list_store (list_store);
1203 path = gtk_tree_path_new ();
1204 gtk_tree_path_append_index (path, list_store->length - 1);
1205 gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1206 gtk_tree_path_free (path);
1210 gtk_list_store_clear (GtkListStore *list_store)
1213 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1215 while (list_store->root)
1217 iter.stamp = list_store->stamp;
1218 iter.user_data = list_store->root;
1219 gtk_list_store_remove (list_store, &iter);
1225 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1229 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1231 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1235 gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1242 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1244 GtkSelectionData *selection_data)
1246 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1248 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1249 * target, because the default handler does it for us, but
1250 * we do anyway for the convenience of someone maybe overriding the
1254 if (gtk_selection_data_set_tree_row (selection_data,
1255 GTK_TREE_MODEL (drag_source),
1262 /* FIXME handle text targets at least. */
1269 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1271 GtkSelectionData *selection_data)
1273 GtkTreeModel *tree_model;
1274 GtkListStore *list_store;
1275 GtkTreeModel *src_model = NULL;
1276 GtkTreePath *src_path = NULL;
1277 gboolean retval = FALSE;
1279 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1281 tree_model = GTK_TREE_MODEL (drag_dest);
1282 list_store = GTK_LIST_STORE (drag_dest);
1284 if (gtk_selection_data_get_tree_row (selection_data,
1287 src_model == tree_model)
1289 /* Copy the given row to a new position */
1290 GtkTreeIter src_iter;
1291 GtkTreeIter dest_iter;
1294 if (!gtk_tree_model_get_iter (src_model,
1301 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1302 prev = gtk_tree_path_copy (dest);
1304 if (!gtk_tree_path_prev (prev))
1306 /* dest was the first spot in the list; which means we are supposed
1309 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1316 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1320 GtkTreeIter tmp_iter = dest_iter;
1321 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1328 gtk_tree_path_free (prev);
1330 /* If we succeeded in creating dest_iter, copy data from src
1334 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1335 GtkTreeDataList *copy_head = NULL;
1336 GtkTreeDataList *copy_prev = NULL;
1337 GtkTreeDataList *copy_iter = NULL;
1344 copy_iter = _gtk_tree_data_list_node_copy (dl,
1345 list_store->column_headers[col]);
1347 if (copy_head == NULL)
1348 copy_head = copy_iter;
1351 copy_prev->next = copy_iter;
1353 copy_prev = copy_iter;
1359 dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1360 G_SLIST (dest_iter.user_data)->data = copy_head;
1362 path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1363 gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter);
1364 gtk_tree_path_free (path);
1369 /* FIXME maybe add some data targets eventually, or handle text
1370 * targets in the simple case.
1377 gtk_tree_path_free (src_path);
1383 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1384 GtkTreeModel *src_model,
1385 GtkTreePath *src_path,
1386 GtkTreePath *dest_path)
1390 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1392 if (src_model != GTK_TREE_MODEL (drag_dest))
1395 if (gtk_tree_path_get_depth (dest_path) != 1)
1398 /* can drop before any existing node, or before one past any existing. */
1400 indices = gtk_tree_path_get_indices (dest_path);
1402 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1410 typedef struct _SortTuple
1417 gtk_list_store_compare_func (gconstpointer a,
1421 GtkListStore *list_store = user_data;
1422 GSList *el_a; /* Los Angeles? */
1427 GtkTreeIterCompareFunc func;
1431 if (list_store->sort_column_id != -1)
1433 GtkTreeDataSortHeader *header;
1435 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1436 list_store->sort_column_id);
1437 g_return_val_if_fail (header != NULL, 0);
1438 g_return_val_if_fail (header->func != NULL, 0);
1440 func = header->func;
1441 data = header->data;
1445 g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1446 func = list_store->default_sort_func;
1447 data = list_store->default_sort_data;
1450 el_a = ((SortTuple *) a)->el;
1451 el_b = ((SortTuple *) b)->el;
1453 iter_a.stamp = list_store->stamp;
1454 iter_a.user_data = el_a;
1455 iter_b.stamp = list_store->stamp;
1456 iter_b.user_data = el_b;
1458 retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1460 if (list_store->order == GTK_SORT_DESCENDING)
1464 else if (retval < 0)
1471 gtk_list_store_sort (GtkListStore *list_store)
1480 if (list_store->length <= 1)
1483 g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1485 list = G_SLIST (list_store->root);
1487 sort_array = g_array_sized_new (FALSE, FALSE,
1489 list_store->length);
1491 for (i = 0; i < list_store->length; i++)
1495 /* If this fails, we are in an inconsistent state. Bad */
1496 g_return_if_fail (list != NULL);
1500 g_array_append_val (sort_array, tuple);
1505 g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1507 for (i = 0; i < list_store->length - 1; i++)
1508 g_array_index (sort_array, SortTuple, i).el->next =
1509 g_array_index (sort_array, SortTuple, i + 1).el;
1510 g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1511 list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1513 /* Let the world know about our new order */
1514 new_order = g_new (gint, list_store->length);
1515 for (i = 0; i < list_store->length; i++)
1516 new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1517 path = gtk_tree_path_new ();
1518 iter.stamp = list_store->stamp;
1519 iter.user_data = NULL;
1520 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1521 path, &iter, new_order);
1522 gtk_tree_path_free (path);
1524 g_array_free (sort_array, TRUE);
1528 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1533 GSList *prev = NULL;
1534 GSList *next = NULL;
1535 GSList *list = G_SLIST (list_store->root);
1536 GtkTreePath *tmp_path;
1537 GtkTreeIter tmp_iter;
1544 GtkTreeIterCompareFunc func;
1547 if (list_store->length < 2)
1550 tmp_iter.stamp = list_store->stamp;
1552 if (list_store->sort_column_id != -1)
1554 GtkTreeDataSortHeader *header;
1555 header = _gtk_tree_data_list_get_header (list_store->sort_list,
1556 list_store->sort_column_id);
1557 g_return_if_fail (header != NULL);
1558 g_return_if_fail (header->func != NULL);
1559 func = header->func;
1560 data = header->data;
1564 g_return_if_fail (list_store->default_sort_func != NULL);
1565 func = list_store->default_sort_func;
1566 data = list_store->default_sort_data;
1569 /* If it's the built in function, we don't sort. */
1570 if (func == gtk_tree_data_list_compare_func &&
1571 list_store->sort_column_id != column)
1575 /* First we find the iter, its prev, and its next */
1578 if (list == G_SLIST (iter->user_data))
1584 g_assert (list != NULL);
1588 /* Check the common case, where we don't need to sort it moved. */
1591 tmp_iter.user_data = prev;
1592 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1597 tmp_iter.user_data = next;
1598 cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1602 if (list_store->order == GTK_SORT_DESCENDING)
1615 if (prev == NULL && cmp_b <= 0)
1617 else if (next == NULL && cmp_a <= 0)
1619 else if (prev != NULL && next != NULL &&
1620 cmp_a <= 0 && cmp_b <= 0)
1623 /* We actually need to sort it */
1624 /* First, remove the old link. */
1627 list_store->root = next;
1631 list_store->tail = prev;
1634 /* FIXME: as an optimization, we can potentially start at next */
1636 list = G_SLIST (list_store->root);
1638 tmp_iter.user_data = list;
1639 if (list_store->order == GTK_SORT_DESCENDING)
1640 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1642 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1644 while ((list->next) && (cmp_a > 0))
1649 tmp_iter.user_data = list;
1650 if (list_store->order == GTK_SORT_DESCENDING)
1651 cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1653 cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1656 if ((!list->next) && (cmp_a > 0))
1658 list->next = G_SLIST (iter->user_data);
1659 list_store->tail = list->next;
1663 prev->next = G_SLIST (iter->user_data);
1664 G_SLIST (iter->user_data)->next = list;
1668 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1669 list_store->root = G_SLIST (iter->user_data);
1672 /* Emit the reordered signal. */
1673 new_order = g_new (int, list_store->length);
1674 if (old_location < new_location)
1675 for (i = 0; i < list_store->length; i++)
1677 if (i < old_location ||
1680 else if (i >= old_location &&
1682 new_order[i] = i + 1;
1683 else if (i == new_location)
1684 new_order[i] = old_location;
1687 for (i = 0; i < list_store->length; i++)
1689 if (i < new_location ||
1692 else if (i > new_location &&
1694 new_order[i] = i - 1;
1695 else if (i == new_location)
1696 new_order[i] = old_location;
1699 tmp_path = gtk_tree_path_new ();
1700 tmp_iter.user_data = NULL;
1702 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1703 tmp_path, &tmp_iter,
1706 gtk_tree_path_free (tmp_path);
1711 gtk_list_store_get_sort_column_id (GtkTreeSortable *sortable,
1712 gint *sort_column_id,
1715 GtkListStore *list_store = (GtkListStore *) sortable;
1717 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1719 if (list_store->sort_column_id == -1)
1723 * sort_column_id = list_store->sort_column_id;
1725 * order = list_store->order;
1730 gtk_list_store_set_sort_column_id (GtkTreeSortable *sortable,
1731 gint sort_column_id,
1734 GtkListStore *list_store = (GtkListStore *) sortable;
1736 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1738 if ((list_store->sort_column_id == sort_column_id) &&
1739 (list_store->order == order))
1742 if (sort_column_id != -1)
1744 GtkTreeDataSortHeader *header = NULL;
1746 header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
1748 /* We want to make sure that we have a function */
1749 g_return_if_fail (header != NULL);
1750 g_return_if_fail (header->func != NULL);
1754 g_return_if_fail (list_store->default_sort_func != NULL);
1758 list_store->sort_column_id = sort_column_id;
1759 list_store->order = order;
1761 gtk_list_store_sort (list_store);
1763 gtk_tree_sortable_sort_column_changed (sortable);
1767 gtk_list_store_set_sort_func (GtkTreeSortable *sortable,
1768 gint sort_column_id,
1769 GtkTreeIterCompareFunc func,
1771 GtkDestroyNotify destroy)
1773 GtkListStore *list_store = (GtkListStore *) sortable;
1774 GtkTreeDataSortHeader *header = NULL;
1777 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1778 g_return_if_fail (func != NULL);
1780 for (list = list_store->sort_list; list; list = list->next)
1782 header = (GtkTreeDataSortHeader*) list->data;
1783 if (header->sort_column_id == sort_column_id)
1789 header = g_new0 (GtkTreeDataSortHeader, 1);
1790 header->sort_column_id = sort_column_id;
1791 list_store->sort_list = g_list_append (list_store->sort_list, header);
1794 if (header->destroy)
1795 (* header->destroy) (header->data);
1797 header->func = func;
1798 header->data = data;
1799 header->destroy = destroy;
1804 gtk_list_store_set_default_sort_func (GtkTreeSortable *sortable,
1805 GtkTreeIterCompareFunc func,
1807 GtkDestroyNotify destroy)
1809 GtkListStore *list_store = (GtkListStore *) sortable;
1811 g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1813 if (list_store->default_sort_destroy)
1814 (* list_store->default_sort_destroy) (list_store->default_sort_data);
1816 list_store->default_sort_func = func;
1817 list_store->default_sort_data = data;
1818 list_store->default_sort_destroy = destroy;
1822 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
1824 GtkListStore *list_store = (GtkListStore *) sortable;
1826 g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1828 return (list_store->default_sort_func != NULL);