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)
38 static guint list_store_signals[LAST_SIGNAL] = { 0 };
40 static void gtk_list_store_init (GtkListStore *list_store);
41 static void gtk_list_store_class_init (GtkListStoreClass *class);
42 static void gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
43 static void gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
44 static void gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface);
45 static guint gtk_list_store_get_flags (GtkTreeModel *tree_model);
46 static gint gtk_list_store_get_n_columns (GtkTreeModel *tree_model);
47 static GType gtk_list_store_get_column_type (GtkTreeModel *tree_model,
49 static gboolean gtk_list_store_get_iter (GtkTreeModel *tree_model,
52 static GtkTreePath *gtk_list_store_get_path (GtkTreeModel *tree_model,
54 static void gtk_list_store_get_value (GtkTreeModel *tree_model,
58 static gboolean gtk_list_store_iter_next (GtkTreeModel *tree_model,
60 static gboolean gtk_list_store_iter_children (GtkTreeModel *tree_model,
63 static gboolean gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
65 static gint gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
67 static gboolean gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
71 static gboolean gtk_list_store_iter_parent (GtkTreeModel *tree_model,
75 static gboolean gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
77 static gboolean gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
79 GtkSelectionData *selection_data);
80 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
82 GtkSelectionData *selection_data);
83 static gboolean gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
84 GtkTreeModel *src_model,
85 GtkTreePath *src_path,
86 GtkTreePath *dest_path);
88 validate_list_store (GtkListStore *list_store)
90 if (gtk_debug_flags & GTK_DEBUG_TREE)
92 g_assert (g_slist_length (list_store->root) == list_store->length);
94 g_assert (g_slist_last (list_store->root) == list_store->tail);
99 gtk_list_store_get_type (void)
101 static GtkType list_store_type = 0;
103 if (!list_store_type)
105 static const GTypeInfo list_store_info =
107 sizeof (GtkListStoreClass),
108 NULL, /* base_init */
109 NULL, /* base_finalize */
110 (GClassInitFunc) gtk_list_store_class_init,
111 NULL, /* class_finalize */
112 NULL, /* class_data */
113 sizeof (GtkListStore),
115 (GInstanceInitFunc) gtk_list_store_init,
118 static const GInterfaceInfo tree_model_info =
120 (GInterfaceInitFunc) gtk_list_store_tree_model_init,
125 static const GInterfaceInfo drag_source_info =
127 (GInterfaceInitFunc) gtk_list_store_drag_source_init,
132 static const GInterfaceInfo drag_dest_info =
134 (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
139 list_store_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
140 g_type_add_interface_static (list_store_type,
143 g_type_add_interface_static (list_store_type,
144 GTK_TYPE_TREE_DRAG_SOURCE,
146 g_type_add_interface_static (list_store_type,
147 GTK_TYPE_TREE_DRAG_DEST,
151 return list_store_type;
155 gtk_list_store_class_init (GtkListStoreClass *class)
157 GtkObjectClass *object_class;
159 object_class = (GtkObjectClass*) class;
161 list_store_signals[CHANGED] =
162 gtk_signal_new ("changed",
164 GTK_CLASS_TYPE (object_class),
165 GTK_SIGNAL_OFFSET (GtkListStoreClass, changed),
166 gtk_marshal_VOID__BOXED_BOXED,
170 list_store_signals[INSERTED] =
171 gtk_signal_new ("inserted",
173 GTK_CLASS_TYPE (object_class),
174 GTK_SIGNAL_OFFSET (GtkListStoreClass, inserted),
175 gtk_marshal_VOID__BOXED_BOXED,
179 list_store_signals[CHILD_TOGGLED] =
180 gtk_signal_new ("child_toggled",
182 GTK_CLASS_TYPE (object_class),
183 GTK_SIGNAL_OFFSET (GtkListStoreClass, child_toggled),
184 gtk_marshal_VOID__BOXED_BOXED,
188 list_store_signals[DELETED] =
189 gtk_signal_new ("deleted",
191 GTK_CLASS_TYPE (object_class),
192 GTK_SIGNAL_OFFSET (GtkListStoreClass, deleted),
193 gtk_marshal_VOID__BOXED,
199 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
201 iface->get_flags = gtk_list_store_get_flags;
202 iface->get_n_columns = gtk_list_store_get_n_columns;
203 iface->get_column_type = gtk_list_store_get_column_type;
204 iface->get_iter = gtk_list_store_get_iter;
205 iface->get_path = gtk_list_store_get_path;
206 iface->get_value = gtk_list_store_get_value;
207 iface->iter_next = gtk_list_store_iter_next;
208 iface->iter_children = gtk_list_store_iter_children;
209 iface->iter_has_child = gtk_list_store_iter_has_child;
210 iface->iter_n_children = gtk_list_store_iter_n_children;
211 iface->iter_nth_child = gtk_list_store_iter_nth_child;
212 iface->iter_parent = gtk_list_store_iter_parent;
216 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
218 iface->drag_data_delete = gtk_list_store_drag_data_delete;
219 iface->drag_data_get = gtk_list_store_drag_data_get;
223 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
225 iface->drag_data_received = gtk_list_store_drag_data_received;
226 iface->row_drop_possible = gtk_list_store_row_drop_possible;
230 gtk_list_store_init (GtkListStore *list_store)
232 list_store->root = NULL;
233 list_store->tail = NULL;
234 list_store->stamp = g_random_int ();
235 list_store->length = 0;
239 gtk_list_store_new (void)
241 return GTK_LIST_STORE (gtk_type_new (gtk_list_store_get_type ()));
245 gtk_list_store_new_with_types (gint n_columns,
248 GtkListStore *retval;
252 g_return_val_if_fail (n_columns > 0, NULL);
254 retval = gtk_list_store_new ();
255 gtk_list_store_set_n_columns (retval, n_columns);
257 va_start (args, n_columns);
259 for (i = 0; i < n_columns; i++)
260 gtk_list_store_set_column_type (retval, i, va_arg (args, GType));
268 gtk_list_store_set_n_columns (GtkListStore *list_store,
273 g_return_if_fail (list_store != NULL);
274 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
275 g_return_if_fail (n_columns > 0);
277 if (list_store->n_columns == n_columns)
280 new_columns = g_new0 (GType, n_columns);
281 if (list_store->column_headers)
283 /* copy the old header orders over */
284 if (n_columns >= list_store->n_columns)
285 memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
287 memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
289 g_free (list_store->column_headers);
292 list_store->column_headers = new_columns;
293 list_store->n_columns = n_columns;
297 gtk_list_store_set_column_type (GtkListStore *list_store,
301 g_return_if_fail (list_store != NULL);
302 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
303 g_return_if_fail (column >=0 && column < list_store->n_columns);
305 list_store->column_headers[column] = type;
308 /* Fulfill the GtkTreeModel requirements */
310 gtk_list_store_get_flags (GtkTreeModel *tree_model)
312 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
314 return GTK_TREE_MODEL_ITERS_PERSIST;
318 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
320 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
322 return GTK_LIST_STORE (tree_model)->n_columns;
326 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
329 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
330 g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
331 index >= 0, G_TYPE_INVALID);
333 return GTK_LIST_STORE (tree_model)->column_headers[index];
337 gtk_list_store_get_iter (GtkTreeModel *tree_model,
344 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
345 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
347 i = gtk_tree_path_get_indices (path)[0];
349 if (i >= GTK_LIST_STORE (tree_model)->length)
352 list = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root),
355 /* If this fails, list_store->length has gotten mangled. */
358 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
359 iter->user_data = list;
364 gtk_list_store_get_path (GtkTreeModel *tree_model,
371 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
372 g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
374 for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
376 if (list == G_SLIST (iter->user_data))
383 retval = gtk_tree_path_new ();
384 gtk_tree_path_append_index (retval, i);
389 gtk_list_store_get_value (GtkTreeModel *tree_model,
394 GtkTreeDataList *list;
395 gint tmp_column = column;
397 g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
398 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
399 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
401 list = G_SLIST (iter->user_data)->data;
403 while (tmp_column-- > 0 && list)
407 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
409 _gtk_tree_data_list_node_to_value (list,
410 GTK_LIST_STORE (tree_model)->column_headers[column],
415 gtk_list_store_iter_next (GtkTreeModel *tree_model,
418 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
419 g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
421 if (G_SLIST (iter->user_data)->next)
423 iter->user_data = G_SLIST (iter->user_data)->next;
431 gtk_list_store_iter_children (GtkTreeModel *tree_model,
435 /* this is a list, nodes have no children */
439 /* but if parent == NULL we return the list itself as children of the
443 if (GTK_LIST_STORE (tree_model)->root)
445 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
446 iter->user_data = GTK_LIST_STORE (tree_model)->root;
454 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
461 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
465 return GTK_LIST_STORE (tree_model)->length;
471 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
478 g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
483 child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
487 iter->user_data = child;
488 iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
496 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
503 /* Public accessors */
504 /* This is a somewhat inelegant function that does a lot of list
505 * manipulations on it's own.
508 gtk_list_store_set_cell (GtkListStore *list_store,
513 GtkTreeDataList *list;
514 GtkTreeDataList *prev;
516 g_return_if_fail (list_store != NULL);
517 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
518 g_return_if_fail (iter != NULL);
519 g_return_if_fail (column >= 0 && column < list_store->n_columns);
521 prev = list = G_SLIST (iter->user_data)->data;
527 _gtk_tree_data_list_value_to_node (list, value);
528 gtk_signal_emit_by_name (GTK_OBJECT (list_store),
539 if (G_SLIST (iter->user_data)->data == NULL)
541 G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
546 list = prev->next = _gtk_tree_data_list_alloc ();
552 list->next = _gtk_tree_data_list_alloc ();
557 _gtk_tree_data_list_value_to_node (list, value);
558 gtk_signal_emit_by_name (GTK_OBJECT (list_store),
564 gtk_list_store_set_valist (GtkListStore *list_store,
570 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
572 column = va_arg (var_args, gint);
576 GValue value = { 0, };
579 if (column >= list_store->n_columns)
581 g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
584 g_value_init (&value, list_store->column_headers[column]);
586 G_VALUE_COLLECT (&value, var_args, &error);
589 g_warning ("%s: %s", G_STRLOC, error);
592 /* we purposely leak the value here, it might not be
593 * in a sane state if an error condition occoured
598 gtk_list_store_set_cell (list_store,
603 g_value_unset (&value);
605 column = va_arg (var_args, gint);
610 * gtk_list_store_set:
611 * @list_store: a #GtkListStore
612 * @iter: row iterator
613 * @Varargs: pairs of column number and value, terminated with -1
615 * Sets the value of one or more cells in the row referenced by @iter.
616 * The variable argument list should contain integer column numbers,
617 * each column number followed by the value to be set. For example,
618 * The list is terminated by a -1. For example, to set column 0 with type
619 * %G_TYPE_STRING to "Foo", you would write gtk_list_store_set (store, iter,
623 gtk_list_store_set (GtkListStore *list_store,
629 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
631 va_start (var_args, iter);
632 gtk_list_store_set_valist (list_store, iter, var_args);
637 gtk_list_store_get_valist (GtkListStore *list_store,
643 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
645 column = va_arg (var_args, gint);
649 GValue value = { 0, };
652 if (column >= list_store->n_columns)
654 g_warning ("%s: Invalid column number %d accessed (remember to end your list of columns with a -1)", G_STRLOC, column);
658 gtk_list_store_get_value (GTK_TREE_MODEL (list_store), iter, column, &value);
660 G_VALUE_LCOPY (&value, var_args, &error);
663 g_warning ("%s: %s", G_STRLOC, error);
666 /* we purposely leak the value here, it might not be
667 * in a sane state if an error condition occoured
672 g_value_unset (&value);
674 column = va_arg (var_args, gint);
679 gtk_list_store_get (GtkListStore *list_store,
685 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
687 va_start (var_args, iter);
688 gtk_list_store_get_valist (list_store, iter, var_args);
693 remove_link_saving_prev (GSList *list,
708 prev->next = link->next;
727 gtk_list_store_remove_silently (GtkListStore *list_store,
731 if (G_SLIST (iter->user_data)->data)
733 _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
734 list_store->column_headers);
735 G_SLIST (iter->user_data)->data = NULL;
741 list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
742 G_SLIST (iter->user_data),
745 list_store->length -= 1;
747 if (iter->user_data == list_store->tail)
748 list_store->tail = prev;
751 list_store->stamp ++;
755 gtk_list_store_remove (GtkListStore *list_store,
760 g_return_if_fail (list_store != NULL);
761 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
762 g_return_if_fail (iter->user_data != NULL);
764 path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
766 validate_list_store (list_store);
768 gtk_list_store_remove_silently (list_store, iter, path);
770 validate_list_store (list_store);
772 gtk_signal_emit_by_name (GTK_OBJECT (list_store),
775 gtk_tree_path_free (path);
779 insert_after (GtkListStore *list_store,
783 g_return_if_fail (sibling != NULL);
784 g_return_if_fail (new_list != NULL);
786 /* insert new node after list */
787 new_list->next = sibling->next;
788 sibling->next = new_list;
790 /* if list was the tail, the new node is the new tail */
791 if (sibling == list_store->tail)
792 list_store->tail = new_list;
794 list_store->length += 1;
798 gtk_list_store_insert (GtkListStore *list_store,
806 g_return_if_fail (list_store != NULL);
807 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
808 g_return_if_fail (iter != NULL);
809 g_return_if_fail (position >= 0);
813 gtk_list_store_prepend (list_store, iter);
817 new_list = g_slist_alloc ();
819 list = g_slist_nth (G_SLIST (list_store->root), position - 1);
823 g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
827 insert_after (list_store, list, new_list);
829 iter->stamp = list_store->stamp;
830 iter->user_data = new_list;
832 validate_list_store (list_store);
834 path = gtk_tree_path_new ();
835 gtk_tree_path_append_index (path, position);
836 gtk_signal_emit_by_name (GTK_OBJECT (list_store),
839 gtk_tree_path_free (path);
843 gtk_list_store_insert_before (GtkListStore *list_store,
845 GtkTreeIter *sibling)
848 GSList *list, *prev, *new_list;
851 g_return_if_fail (list_store != NULL);
852 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
853 g_return_if_fail (iter != NULL);
857 gtk_list_store_append (list_store, iter);
861 new_list = g_slist_alloc ();
864 list = list_store->root;
865 while (list && list != sibling->user_data)
872 if (list != sibling->user_data)
874 g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
878 /* if there are no nodes, we become the list tail, otherwise we
879 * are inserting before any existing nodes so we can't change
883 if (list_store->root == NULL)
884 list_store->tail = new_list;
888 new_list->next = prev->next;
889 prev->next = new_list;
893 new_list->next = list_store->root;
894 list_store->root = new_list;
897 iter->stamp = list_store->stamp;
898 iter->user_data = new_list;
900 list_store->length += 1;
902 validate_list_store (list_store);
904 path = gtk_tree_path_new ();
905 gtk_tree_path_append_index (path, i);
906 gtk_signal_emit_by_name (GTK_OBJECT (list_store),
909 gtk_tree_path_free (path);
913 gtk_list_store_insert_after (GtkListStore *list_store,
915 GtkTreeIter *sibling)
918 GSList *list, *new_list;
921 g_return_if_fail (list_store != NULL);
922 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
923 g_return_if_fail (iter != NULL);
925 g_return_if_fail (sibling->stamp == list_store->stamp);
929 gtk_list_store_prepend (list_store, iter);
933 for (list = list_store->root; list && list != sibling->user_data; list = list->next)
936 g_return_if_fail (list == sibling->user_data);
938 new_list = g_slist_alloc ();
940 insert_after (list_store, list, new_list);
942 iter->stamp = list_store->stamp;
943 iter->user_data = new_list;
945 validate_list_store (list_store);
947 path = gtk_tree_path_new ();
948 gtk_tree_path_append_index (path, i);
949 gtk_signal_emit_by_name (GTK_OBJECT (list_store),
952 gtk_tree_path_free (path);
956 gtk_list_store_prepend (GtkListStore *list_store,
961 g_return_if_fail (list_store != NULL);
962 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
963 g_return_if_fail (iter != NULL);
965 iter->stamp = list_store->stamp;
966 iter->user_data = g_slist_alloc ();
968 if (list_store->root == NULL)
969 list_store->tail = iter->user_data;
971 G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
972 list_store->root = iter->user_data;
974 list_store->length += 1;
976 validate_list_store (list_store);
978 path = gtk_tree_path_new ();
979 gtk_tree_path_append_index (path, 0);
980 gtk_signal_emit_by_name (GTK_OBJECT (list_store),
983 gtk_tree_path_free (path);
987 gtk_list_store_append (GtkListStore *list_store,
993 g_return_if_fail (list_store != NULL);
994 g_return_if_fail (GTK_IS_LIST_STORE (list_store));
995 g_return_if_fail (iter != NULL);
997 iter->stamp = list_store->stamp;
998 iter->user_data = g_slist_alloc ();
1000 if (list_store->tail)
1001 list_store->tail->next = iter->user_data;
1003 list_store->root = iter->user_data;
1005 list_store->tail = iter->user_data;
1007 list_store->length += 1;
1009 validate_list_store (list_store);
1011 path = gtk_tree_path_new ();
1012 gtk_tree_path_append_index (path, i);
1013 gtk_signal_emit_by_name (GTK_OBJECT (list_store),
1016 gtk_tree_path_free (path);
1020 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1024 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1026 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1030 gtk_list_store_remove (GTK_LIST_STORE (drag_source),
1041 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1043 GtkSelectionData *selection_data)
1045 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1047 /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1048 * target, because the default handler does it for us, but
1049 * we do anyway for the convenience of someone maybe overriding the
1053 if (gtk_selection_data_set_tree_row (selection_data,
1054 GTK_TREE_MODEL (drag_source),
1061 /* FIXME handle text targets at least. */
1068 gtk_list_store_drag_data_received (GtkTreeDragDest *drag_dest,
1070 GtkSelectionData *selection_data)
1072 GtkTreeModel *tree_model;
1073 GtkListStore *list_store;
1074 GtkTreeModel *src_model = NULL;
1075 GtkTreePath *src_path = NULL;
1076 gboolean retval = FALSE;
1078 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1080 tree_model = GTK_TREE_MODEL (drag_dest);
1081 list_store = GTK_LIST_STORE (drag_dest);
1083 if (gtk_selection_data_get_tree_row (selection_data,
1086 src_model == tree_model)
1088 /* Copy the given row to a new position */
1089 GtkTreeIter src_iter;
1090 GtkTreeIter dest_iter;
1093 if (!gtk_tree_model_get_iter (src_model,
1097 g_print ("can't get source path as iter\n");
1101 /* Get the path to insert _after_ (dest is the path to insert _before_) */
1102 prev = gtk_tree_path_copy (dest);
1104 if (!gtk_tree_path_prev (prev))
1106 /* dest was the first spot in the list; which means we are supposed
1109 gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1114 g_print ("prepending to list\n");
1118 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1122 GtkTreeIter tmp_iter = dest_iter;
1123 gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1128 g_print ("inserting into list\n");
1131 g_print ("can't get iter to insert after\n");
1134 gtk_tree_path_free (prev);
1136 /* If we succeeded in creating dest_iter, copy data from src
1140 GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1141 GtkTreeDataList *copy_head = NULL;
1142 GtkTreeDataList *copy_prev = NULL;
1143 GtkTreeDataList *copy_iter = NULL;
1149 copy_iter = _gtk_tree_data_list_node_copy (dl,
1150 list_store->column_headers[col]);
1152 g_print ("copied col %d type %s\n", col,
1153 g_type_name (list_store->column_headers[col]));
1155 if (copy_head == NULL)
1156 copy_head = copy_iter;
1159 copy_prev->next = copy_iter;
1161 copy_prev = copy_iter;
1167 G_SLIST (dest_iter.user_data)->data = copy_head;
1169 gtk_signal_emit_by_name (GTK_OBJECT (tree_model),
1176 /* FIXME maybe add some data targets eventually, or handle text
1177 * targets in the simple case.
1179 g_print ("not accepting target\n");
1185 gtk_tree_path_free (src_path);
1191 gtk_list_store_row_drop_possible (GtkTreeDragDest *drag_dest,
1192 GtkTreeModel *src_model,
1193 GtkTreePath *src_path,
1194 GtkTreePath *dest_path)
1198 g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1200 if (src_model != GTK_TREE_MODEL (drag_dest))
1203 if (gtk_tree_path_get_depth (dest_path) != 1)
1206 /* can drop before any existing node, or before one past any existing. */
1208 indices = gtk_tree_path_get_indices (dest_path);
1210 if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)