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)
30 static void gtk_list_store_init (GtkListStore *list_store);
31 static void gtk_list_store_class_init (GtkListStoreClass *class);
32 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
33 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
34 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
35 static guint gtk_list_store_get_flags (GtkTreeModel *tree_model);
36 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
37 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
39 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
42 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
44 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
48 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
50 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
53 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
55 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
57 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
61 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
65 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
67 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
69 GtkSelectionData *selection_data);
70 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
72 GtkSelectionData *selection_data);
73 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
74 GtkTreeModel *src_model,
75 GtkTreePath *src_path,
76 GtkTreePath *dest_path);
78 validate_list_store (GtkListStore *list_store)
80 if (gtk_debug_flags & GTK_DEBUG_TREE)
82 g_assert (g_slist_length (list_store->root) == list_store->length);
84 g_assert (g_slist_last (list_store->root) == list_store->tail);
89 gtk_list_store_get_type (void)
91 static GType list_store_type = 0;
95 static const GTypeInfo list_store_info =
97 sizeof (GtkListStoreClass),
99 NULL, /* base_finalize */
100 (GClassInitFunc) gtk_list_store_class_init,
101 NULL, /* class_finalize */
102 NULL, /* class_data */
103 sizeof (GtkListStore),
105 (GInstanceInitFunc) gtk_list_store_init,
108 static const GInterfaceInfo tree_model_info =
110 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
115 static const GInterfaceInfo drag_source_info =
117 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
122 static const GInterfaceInfo drag_dest_info =
124 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
129 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
130 g_type_add_interface_static (list_store_type,
133 g_type_add_interface_static (list_store_type,
134 GTK_TYPE_TREE_DRAG_SOURCE,
136 g_type_add_interface_static (list_store_type,
137 GTK_TYPE_TREE_DRAG_DEST,
141 return list_store_type;
145 gtk_list_store_class_init (GtkListStoreClass *class)
147 GObjectClass *object_class;
149 object_class = (GObjectClass*) class;
153 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
155 iface->get_flags = gtk_list_store_get_flags;
156 iface->get_n_columns = gtk_list_store_get_n_columns;
157 iface->get_column_type = gtk_list_store_get_column_type;
158 iface->get_iter = gtk_list_store_get_iter;
159 iface->get_path = gtk_list_store_get_path;
160 iface->get_value = gtk_list_store_get_value;
161 iface->iter_next = gtk_list_store_iter_next;
162 iface->iter_children = gtk_list_store_iter_children;
163 iface->iter_has_child = gtk_list_store_iter_has_child;
164 iface->iter_n_children = gtk_list_store_iter_n_children;
165 iface->iter_nth_child = gtk_list_store_iter_nth_child;
166 iface->iter_parent = gtk_list_store_iter_parent;
170 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
172 iface->drag_data_delete = gtk_list_store_drag_data_delete;
173 iface->drag_data_get = gtk_list_store_drag_data_get;
177 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
179 iface->drag_data_received = gtk_list_store_drag_data_received;
180 iface->row_drop_possible = gtk_list_store_row_drop_possible;
184 gtk_list_store_init (GtkListStore *list_store)
186 list_store->root = NULL;
187 list_store->tail = NULL;
188 list_store->stamp = g_random_int ();
189 list_store->length = 0;
193 * gtk_list_store_new:
195 * Creates a new #GtkListStore. A #GtkListStore implements the
196 * #GtkTreeModel interface, and stores a linked list of
197 * rows; each row can have any number of columns. Columns are of uniform type,
198 * i.e. all cells in a column have the same type such as #G_TYPE_STRING or
199 * #GDK_TYPE_PIXBUF. Use #GtkListStore to store data to be displayed in a
202 * Return value: a new #GtkListStore
205 gtk_list_store_new (void)
207 return GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
211 * gtk_list_store_new_with_types:
212 * @n_columns: number of columns in the list store
213 * @Varargs: pairs of column number and #GType
215 * Creates a new list store as with gtk_list_store_new(),
216 * simultaneously setting up the columns and column types as with
217 * gtk_list_store_set_n_columns() and
218 * gtk_list_store_set_column_type().
221 * Return value: a new #GtkListStore
224 gtk_list_store_new_with_types (gint n_columns,
227 GtkListStore *retval;
231 g_return_val_if_fail (n_columns > 0, NULL);
233 retval = gtk_list_store_new ();
234 gtk_list_store_set_n_columns (retval, n_columns);
236 va_start (args, n_columns);
238 for (i = 0; i < n_columns; i++)
239 gtk_list_store_set_column_type (retval, i, va_arg (args, GType));
247 * gtk_list_store_set_n_columns:
248 * @store: a #GtkListStore
249 * @n_columns: number of columns
251 * Sets the number of columns in the #GtkListStore.
255 gtk_list_store_set_n_columns (GtkListStore *list_store,
260 g_return_if_fail (list_store != NULL);
261 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
262 g_return_if_fail (n_columns > 0);
264 if (list_store->n_columns == n_columns)
267 new_columns = g_new0 (GType, n_columns);
268 if (list_store->column_headers)
270 /* copy the old header orders over */
271 if (n_columns >= list_store->n_columns)
272 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
274 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
276 g_free (list_store->column_headers);
279 list_store->column_headers = new_columns;
280 list_store->n_columns = n_columns;
284 * gtk_list_store_set_column_type:
285 * @store: a #GtkListStore
286 * @column: column number
287 * @type: type of the data stored in @column
289 * Supported types include: %G_TYPE_UINT, %G_TYPE_INT, %G_TYPE_UCHAR,
290 * %G_TYPE_CHAR, %G_TYPE_BOOLEAN, %G_TYPE_POINTER, %G_TYPE_FLOAT, %G_TYPE_STRING,
291 * %G_TYPE_OBJECT, and %G_TYPE_BOXED, along with subclasses of those types such
292 * as %GDK_TYPE_PIXBUF.
296 gtk_list_store_set_column_type (GtkListStore *list_store,
300 g_return_if_fail (list_store != NULL);
301 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
302 g_return_if_fail (column >=0 && column < list_store->n_columns);
304 list_store->column_headers[column] = type;
307 /* Fulfill the GtkTreeModel requirements */
309 gtk_list_store_get_flags (GtkTreeModel *tree_model)
311 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
313 return GTK_TREE_MODEL_ITERS_PERSIST;
317 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
319 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
321 return GTK_LIST_STORE (tree_model)->n_columns;
325 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
328 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
329 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
330 index >= 0, G_TYPE_INVALID);
332 return GTK_LIST_STORE (tree_model)->column_headers[index];
336 gtk_list_store_get_iter (GtkTreeModel *tree_model,
343 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
344 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
346 i = gtk_tree_path_get_indices (path)[0];
348 if (i >= GTK_LIST_STORE (tree_model)->length)
351 list = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root),
354 /* If this fails, list_store->length has gotten mangled. */
357 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
358 iter->user_data = list;
363 gtk_list_store_get_path (GtkTreeModel *tree_model,
370 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
371 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
373 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
375 if (list == G_SLIST (iter->user_data))
382 retval = gtk_tree_path_new ();
383 gtk_tree_path_append_index (retval, i);
388 gtk_list_store_get_value (GtkTreeModel *tree_model,
393 GtkTreeDataList *list;
394 gint tmp_column = column;
396 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
397 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
398 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
400 list = G_SLIST (iter->user_data)->data;
402 while (tmp_column-- > 0 && list)
406 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
408 _gtk_tree_data_list_node_to_value (list,
409 GTK_LIST_STORE (tree_model)->column_headers[column],
414 gtk_list_store_iter_next (GtkTreeModel *tree_model,
417 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
418 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
420 if (G_SLIST (iter->user_data)->next)
422 iter->user_data = G_SLIST (iter->user_data)->next;
430 gtk_list_store_iter_children (GtkTreeModel *tree_model,
434 /* this is a list, nodes have no children */
438 /* but if parent == NULL we return the list itself as children of the
442 if (GTK_LIST_STORE (tree_model)->root)
444 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
445 iter->user_data = GTK_LIST_STORE (tree_model)->root;
453 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
460 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
464 return GTK_LIST_STORE (tree_model)->length;
470 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
477 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
482 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
486 iter->user_data = child;
487 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
495 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
502 /* Public accessors */
503 /* This is a somewhat inelegant function that does a lot of list
504 * manipulations on it's own.
508 * gtk_list_store_set_cell:
509 * @store: a #GtkListStore
510 * @iter: iterator for the row you're modifying
511 * @column: column number to modify
512 * @value: new value for the cell
514 * Sets the data in the cell specified by @iter and @column.
515 * The type of @value must be convertible to the type of the
520 gtk_list_store_set_cell (GtkListStore *list_store,
525 GtkTreeDataList *list;
526 GtkTreeDataList *prev;
528 g_return_if_fail (list_store != NULL);
529 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
530 g_return_if_fail (iter != NULL);
531 g_return_if_fail (column >= 0 && column < list_store->n_columns);
533 prev = list = G_SLIST (iter->user_data)->data;
539 _gtk_tree_data_list_value_to_node (list, value);
540 g_signal_emit_by_name (G_OBJECT (list_store),
551 if (G_SLIST (iter->user_data)->data == NULL)
553 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
558 list = prev->next = _gtk_tree_data_list_alloc ();
564 list->next = _gtk_tree_data_list_alloc ();
569 _gtk_tree_data_list_value_to_node (list, value);
570 g_signal_emit_by_name (G_OBJECT (list_store),
576 * gtk_list_store_set_valist:
577 * @list_store: a #GtkListStore
578 * @iter: row to set data for
579 * @var_args: va_list of column/value pairs
581 * See gtk_list_store_set(); this version takes a va_list for
582 * use by language bindings.
586 gtk_list_store_set_valist (GtkListStore *list_store,
592 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
594 column = va_arg (var_args, gint);
598 GValue value = { 0, };
601 if (column >= list_store->n_columns)
603 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
606 g_value_init (&value, list_store->column_headers[column]);
608 G_VALUE_COLLECT (&value, var_args, 0, &error);
611 g_warning ("%s: %s", G_STRLOC, error);
614 /* we purposely leak the value here, it might not be
615 * in a sane state if an error condition occoured
620 gtk_list_store_set_cell (list_store,
625 g_value_unset (&value);
627 column = va_arg (var_args, gint);
632 * gtk_list_store_set:
633 * @list_store: a #GtkListStore
634 * @iter: row iterator
635 * @Varargs: pairs of column number and value, terminated with -1
637 * Sets the value of one or more cells in the row referenced by @iter.
638 * The variable argument list should contain integer column numbers,
639 * each column number followed by the value to be set.
640 * The list is terminated by a -1. For example, to set column 0 with type
641 * %G_TYPE_STRING to "Foo", you would write gtk_list_store_set (store, iter,
645 gtk_list_store_set (GtkListStore *list_store,
651 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
653 va_start (var_args, iter);
654 gtk_list_store_set_valist (list_store, iter, var_args);
659 remove_link_saving_prev (GSList *list,
674 prev->next = link->next;
693 gtk_list_store_remove_silently (GtkListStore *list_store,
697 if (G_SLIST (iter->user_data)->data)
699 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
700 list_store->column_headers);
701 G_SLIST (iter->user_data)->data = NULL;
707 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
708 G_SLIST (iter->user_data),
711 list_store->length -= 1;
713 if (iter->user_data == list_store->tail)
714 list_store->tail = prev;
717 list_store->stamp ++;
721 * gtk_list_store_remove:
722 * @store: a #GtkListStore
723 * @iter: a row in @list_store
725 * Removes the given row from the list store, emitting the
726 * "deleted" signal on #GtkTreeModel.
730 gtk_list_store_remove (GtkListStore *list_store,
735 g_return_if_fail (list_store != NULL);
736 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
737 g_return_if_fail (iter->user_data != NULL);
739 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
741 validate_list_store (list_store);
743 gtk_list_store_remove_silently (list_store, iter, path);
745 validate_list_store (list_store);
747 g_signal_emit_by_name (G_OBJECT (list_store),
750 gtk_tree_path_free (path);
754 insert_after (GtkListStore *list_store,
758 g_return_if_fail (sibling != NULL);
759 g_return_if_fail (new_list != NULL);
761 /* insert new node after list */
762 new_list->next = sibling->next;
763 sibling->next = new_list;
765 /* if list was the tail, the new node is the new tail */
766 if (sibling == list_store->tail)
767 list_store->tail = new_list;
769 list_store->length += 1;
773 * gtk_list_store_insert:
774 * @store: a #GtkListStore
775 * @iter: iterator to initialize with the new row
776 * @position: position to insert the new row
778 * Creates a new row at @position, initializing @iter to point to the
779 * new row, and emitting the "inserted" signal from the #GtkTreeModel
784 gtk_list_store_insert (GtkListStore *list_store,
792 g_return_if_fail (list_store != NULL);
793 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
794 g_return_if_fail (iter != NULL);
795 g_return_if_fail (position >= 0);
799 gtk_list_store_prepend (list_store, iter);
803 new_list = g_slist_alloc ();
805 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
809 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
813 insert_after (list_store, list, new_list);
815 iter->stamp = list_store->stamp;
816 iter->user_data = new_list;
818 validate_list_store (list_store);
820 path = gtk_tree_path_new ();
821 gtk_tree_path_append_index (path, position);
822 g_signal_emit_by_name (G_OBJECT (list_store),
825 gtk_tree_path_free (path);
829 * gtk_list_store_insert_before:
830 * @store: a #GtkListStore
831 * @iter: iterator to initialize with the new row
832 * @sibling: an existing row
834 * Inserts a new row before @sibling, initializing @iter to point to
835 * the new row, and emitting the "inserted" signal from the
836 * #GtkTreeModel interface.
840 gtk_list_store_insert_before (GtkListStore *list_store,
842 GtkTreeIter *sibling)
845 GSList *list, *prev, *new_list;
848 g_return_if_fail (list_store != NULL);
849 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
850 g_return_if_fail (iter != NULL);
854 gtk_list_store_append (list_store, iter);
858 new_list = g_slist_alloc ();
861 list = list_store->root;
862 while (list && list != sibling->user_data)
869 if (list != sibling->user_data)
871 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
875 /* if there are no nodes, we become the list tail, otherwise we
876 * are inserting before any existing nodes so we can't change
880 if (list_store->root == NULL)
881 list_store->tail = new_list;
885 new_list->next = prev->next;
886 prev->next = new_list;
890 new_list->next = list_store->root;
891 list_store->root = new_list;
894 iter->stamp = list_store->stamp;
895 iter->user_data = new_list;
897 list_store->length += 1;
899 validate_list_store (list_store);
901 path = gtk_tree_path_new ();
902 gtk_tree_path_append_index (path, i);
903 g_signal_emit_by_name (G_OBJECT (list_store),
906 gtk_tree_path_free (path);
910 * gtk_list_store_insert_after:
911 * @store: a #GtkListStore
912 * @iter: iterator to initialize with the new row
913 * @sibling: an existing row
915 * Inserts a new row after @sibling, initializing @iter to point to
916 * the new row, and emitting the "inserted" signal from the
917 * #GtkTreeModel interface.
921 gtk_list_store_insert_after (GtkListStore *list_store,
923 GtkTreeIter *sibling)
926 GSList *list, *new_list;
929 g_return_if_fail (list_store != NULL);
930 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
931 g_return_if_fail (iter != NULL);
933 g_return_if_fail (sibling->stamp == list_store->stamp);
937 gtk_list_store_prepend (list_store, iter);
941 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
944 g_return_if_fail (list == sibling->user_data);
946 new_list = g_slist_alloc ();
948 insert_after (list_store, list, new_list);
950 iter->stamp = list_store->stamp;
951 iter->user_data = new_list;
953 validate_list_store (list_store);
955 path = gtk_tree_path_new ();
956 gtk_tree_path_append_index (path, i);
957 g_signal_emit_by_name (G_OBJECT (list_store),
960 gtk_tree_path_free (path);
964 * gtk_list_store_prepend:
965 * @store: a #GtkListStore
966 * @iter: iterator to initialize with new row
968 * Prepends a row to @store, initializing @iter to point to the
969 * new row, and emitting the "inserted" signal on the #GtkTreeModel
970 * interface for the @store.
974 gtk_list_store_prepend (GtkListStore *list_store,
979 g_return_if_fail (list_store != NULL);
980 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
981 g_return_if_fail (iter != NULL);
983 iter->stamp = list_store->stamp;
984 iter->user_data = g_slist_alloc ();
986 if (list_store->root == NULL)
987 list_store->tail = iter->user_data;
989 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
990 list_store->root = iter->user_data;
992 list_store->length += 1;
994 validate_list_store (list_store);
996 path = gtk_tree_path_new ();
997 gtk_tree_path_append_index (path, 0);
998 g_signal_emit_by_name (G_OBJECT (list_store),
1001 gtk_tree_path_free (path);
1005 * gtk_list_store_append:
1006 * @store: a #GtkListStore
1007 * @iter: iterator to initialize with the new row
1009 * Appends a row to @store, initializing @iter to point to the
1010 * new row, and emitting the "inserted" signal on the #GtkTreeModel
1011 * interface for the @store.
1015 gtk_list_store_append (GtkListStore *list_store,
1021 g_return_if_fail (list_store != NULL);
1022 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1023 g_return_if_fail (iter != NULL);
1025 iter->stamp = list_store->stamp;
1026 iter->user_data = g_slist_alloc ();
1028 if (list_store->tail)
1029 list_store->tail->next = iter->user_data;
1031 list_store->root = iter->user_data;
1033 list_store->tail = iter->user_data;
1035 list_store->length += 1;
1037 validate_list_store (list_store);
1039 path = gtk_tree_path_new ();
1040 gtk_tree_path_append_index (path, i);
1041 g_signal_emit_by_name (G_OBJECT (list_store),
1044 gtk_tree_path_free (path);
1048 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1052 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1054 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1058 gtk_list_store_remove (GTK_LIST_STORE (drag_source),
1069 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1071 GtkSelectionData *selection_data)
1073 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1075 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1076 * target, because the default handler does it for us, but
1077 * we do anyway for the convenience of someone maybe overriding the
1081 if (gtk_selection_data_set_tree_row (selection_data,
1082 GTK_TREE_MODEL (drag_source),
1089 /* FIXME handle text targets at least. */
1096 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1098 GtkSelectionData *selection_data)
1100 GtkTreeModel *tree_model;
1101 GtkListStore *list_store;
1102 GtkTreeModel *src_model = NULL;
1103 GtkTreePath *src_path = NULL;
1104 gboolean retval = FALSE;
1106 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1108 tree_model = GTK_TREE_MODEL (drag_dest);
1109 list_store = GTK_LIST_STORE (drag_dest);
1111 if (gtk_selection_data_get_tree_row (selection_data,
1114 src_model == tree_model)
1116 /* Copy the given row to a new position */
1117 GtkTreeIter src_iter;
1118 GtkTreeIter dest_iter;
1121 if (!gtk_tree_model_get_iter (src_model,
1128 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1129 prev = gtk_tree_path_copy (dest);
1131 if (!gtk_tree_path_prev (prev))
1133 /* dest was the first spot in the list; which means we are supposed
1136 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1143 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1147 GtkTreeIter tmp_iter = dest_iter;
1148 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1155 gtk_tree_path_free (prev);
1157 /* If we succeeded in creating dest_iter, copy data from src
1161 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1162 GtkTreeDataList *copy_head = NULL;
1163 GtkTreeDataList *copy_prev = NULL;
1164 GtkTreeDataList *copy_iter = NULL;
1170 copy_iter = _gtk_tree_data_list_node_copy (dl,
1171 list_store->column_headers[col]);
1173 if (copy_head == NULL)
1174 copy_head = copy_iter;
1177 copy_prev->next = copy_iter;
1179 copy_prev = copy_iter;
1185 G_SLIST (dest_iter.user_data)->data = copy_head;
1187 g_signal_emit_by_name (G_OBJECT (tree_model),
1194 /* FIXME maybe add some data targets eventually, or handle text
1195 * targets in the simple case.
1202 gtk_tree_path_free (src_path);
1208 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1209 GtkTreeModel *src_model,
1210 GtkTreePath *src_path,
1211 GtkTreePath *dest_path)
1215 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1217 if (src_model != GTK_TREE_MODEL (drag_dest))
1220 if (gtk_tree_path_get_depth (dest_path) != 1)
1223 /* can drop before any existing node, or before one past any existing. */
1225 indices = gtk_tree_path_get_indices (dest_path);
1227 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)