]> Pileus Git - ~andy/gtk/blob - gtk/gtkliststore.c
set value of func to NULL during initialization (we don't use func in the
[~andy/gtk] / gtk / gtkliststore.c
1 /* gtkliststore.c
2  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <string.h>
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>
27
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)
31
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 void         gtk_list_store_finalize        (GObject           *object);
39 static guint        gtk_list_store_get_flags       (GtkTreeModel      *tree_model);
40 static gint         gtk_list_store_get_n_columns   (GtkTreeModel      *tree_model);
41 static GType        gtk_list_store_get_column_type (GtkTreeModel      *tree_model,
42                                                     gint               index);
43 static gboolean     gtk_list_store_get_iter        (GtkTreeModel      *tree_model,
44                                                     GtkTreeIter       *iter,
45                                                     GtkTreePath       *path);
46 static GtkTreePath *gtk_list_store_get_path        (GtkTreeModel      *tree_model,
47                                                     GtkTreeIter       *iter);
48 static void         gtk_list_store_get_value       (GtkTreeModel      *tree_model,
49                                                     GtkTreeIter       *iter,
50                                                     gint               column,
51                                                     GValue            *value);
52 static gboolean     gtk_list_store_iter_next       (GtkTreeModel      *tree_model,
53                                                     GtkTreeIter       *iter);
54 static gboolean     gtk_list_store_iter_children   (GtkTreeModel      *tree_model,
55                                                     GtkTreeIter       *iter,
56                                                     GtkTreeIter       *parent);
57 static gboolean     gtk_list_store_iter_has_child  (GtkTreeModel      *tree_model,
58                                                     GtkTreeIter       *iter);
59 static gint         gtk_list_store_iter_n_children (GtkTreeModel      *tree_model,
60                                                     GtkTreeIter       *iter);
61 static gboolean     gtk_list_store_iter_nth_child  (GtkTreeModel      *tree_model,
62                                                     GtkTreeIter       *iter,
63                                                     GtkTreeIter       *parent,
64                                                     gint               n);
65 static gboolean     gtk_list_store_iter_parent     (GtkTreeModel      *tree_model,
66                                                     GtkTreeIter       *iter,
67                                                     GtkTreeIter       *child);
68
69
70 static void gtk_list_store_set_n_columns   (GtkListStore *list_store,
71                                             gint          n_columns);
72 static void gtk_list_store_set_column_type (GtkListStore *list_store,
73                                             gint          column,
74                                             GType         type);
75
76
77 /* Drag and Drop */
78 static gboolean gtk_list_store_drag_data_delete   (GtkTreeDragSource *drag_source,
79                                                    GtkTreePath       *path);
80 static gboolean gtk_list_store_drag_data_get      (GtkTreeDragSource *drag_source,
81                                                    GtkTreePath       *path,
82                                                    GtkSelectionData  *selection_data);
83 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest   *drag_dest,
84                                                    GtkTreePath       *dest,
85                                                    GtkSelectionData  *selection_data);
86 static gboolean gtk_list_store_row_drop_possible  (GtkTreeDragDest   *drag_dest,
87                                                    GtkTreePath       *dest_path,
88                                                    GtkSelectionData  *selection_data);
89
90
91 /* sortable */
92 static void     gtk_list_store_sort                  (GtkListStore           *list_store);
93 static void     gtk_list_store_sort_iter_changed     (GtkListStore           *list_store,
94                                                       GtkTreeIter            *iter,
95                                                       gint                    column);
96 static gboolean gtk_list_store_get_sort_column_id    (GtkTreeSortable        *sortable,
97                                                       gint                   *sort_column_id,
98                                                       GtkSortType            *order);
99 static void     gtk_list_store_set_sort_column_id    (GtkTreeSortable        *sortable,
100                                                       gint                    sort_column_id,
101                                                       GtkSortType             order);
102 static void     gtk_list_store_set_sort_func         (GtkTreeSortable        *sortable,
103                                                       gint                    sort_column_id,
104                                                       GtkTreeIterCompareFunc  func,
105                                                       gpointer                data,
106                                                       GtkDestroyNotify        destroy);
107 static void     gtk_list_store_set_default_sort_func (GtkTreeSortable        *sortable,
108                                                       GtkTreeIterCompareFunc  func,
109                                                       gpointer                data,
110                                                       GtkDestroyNotify        destroy);
111 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable        *sortable);
112
113
114 static GObjectClass *parent_class = NULL;
115
116
117 static void
118 validate_list_store (GtkListStore *list_store)
119 {
120   if (gtk_debug_flags & GTK_DEBUG_TREE)
121     {
122       g_assert (g_slist_length (list_store->root) == list_store->length);
123
124       g_assert (g_slist_last (list_store->root) == list_store->tail);
125     }
126 }
127
128 GtkType
129 gtk_list_store_get_type (void)
130 {
131   static GType list_store_type = 0;
132
133   if (!list_store_type)
134     {
135       static const GTypeInfo list_store_info =
136       {
137         sizeof (GtkListStoreClass),
138         NULL,           /* base_init */
139         NULL,           /* base_finalize */
140         (GClassInitFunc) gtk_list_store_class_init,
141         NULL,           /* class_finalize */
142         NULL,           /* class_data */
143         sizeof (GtkListStore),
144         0,
145         (GInstanceInitFunc) gtk_list_store_init,
146       };
147
148       static const GInterfaceInfo tree_model_info =
149       {
150         (GInterfaceInitFunc) gtk_list_store_tree_model_init,
151         NULL,
152         NULL
153       };
154
155       static const GInterfaceInfo drag_source_info =
156       {
157         (GInterfaceInitFunc) gtk_list_store_drag_source_init,
158         NULL,
159         NULL
160       };
161
162       static const GInterfaceInfo drag_dest_info =
163       {
164         (GInterfaceInitFunc) gtk_list_store_drag_dest_init,
165         NULL,
166         NULL
167       };
168
169       static const GInterfaceInfo sortable_info =
170       {
171         (GInterfaceInitFunc) gtk_list_store_sortable_init,
172         NULL,
173         NULL
174       };
175
176       list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkListStore", &list_store_info, 0);
177       g_type_add_interface_static (list_store_type,
178                                    GTK_TYPE_TREE_MODEL,
179                                    &tree_model_info);
180       g_type_add_interface_static (list_store_type,
181                                    GTK_TYPE_TREE_DRAG_SOURCE,
182                                    &drag_source_info);
183       g_type_add_interface_static (list_store_type,
184                                    GTK_TYPE_TREE_DRAG_DEST,
185                                    &drag_dest_info);
186       g_type_add_interface_static (list_store_type,
187                                    GTK_TYPE_TREE_SORTABLE,
188                                    &sortable_info);
189     }
190
191   return list_store_type;
192 }
193
194 static void
195 gtk_list_store_class_init (GtkListStoreClass *class)
196 {
197   GObjectClass *object_class;
198
199   parent_class = g_type_class_peek_parent (class);
200   object_class = (GObjectClass*) class;
201
202   object_class->finalize = gtk_list_store_finalize;
203 }
204
205 static void
206 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
207 {
208   iface->get_flags = gtk_list_store_get_flags;
209   iface->get_n_columns = gtk_list_store_get_n_columns;
210   iface->get_column_type = gtk_list_store_get_column_type;
211   iface->get_iter = gtk_list_store_get_iter;
212   iface->get_path = gtk_list_store_get_path;
213   iface->get_value = gtk_list_store_get_value;
214   iface->iter_next = gtk_list_store_iter_next;
215   iface->iter_children = gtk_list_store_iter_children;
216   iface->iter_has_child = gtk_list_store_iter_has_child;
217   iface->iter_n_children = gtk_list_store_iter_n_children;
218   iface->iter_nth_child = gtk_list_store_iter_nth_child;
219   iface->iter_parent = gtk_list_store_iter_parent;
220 }
221
222 static void
223 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
224 {
225   iface->drag_data_delete = gtk_list_store_drag_data_delete;
226   iface->drag_data_get = gtk_list_store_drag_data_get;
227 }
228
229 static void
230 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
231 {
232   iface->drag_data_received = gtk_list_store_drag_data_received;
233   iface->row_drop_possible = gtk_list_store_row_drop_possible;
234 }
235
236 static void
237 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
238 {
239   iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
240   iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
241   iface->set_sort_func = gtk_list_store_set_sort_func;
242   iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
243   iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
244 }
245
246 static void
247 gtk_list_store_init (GtkListStore *list_store)
248 {
249   list_store->root = NULL;
250   list_store->tail = NULL;
251   list_store->sort_list = NULL;
252   list_store->stamp = g_random_int ();
253   list_store->length = 0;
254   list_store->sort_column_id = -2;
255   list_store->columns_dirty = FALSE;
256 }
257
258 /**
259  * gtk_list_store_new:
260  * @n_columns: number of columns in the list store
261  * @Varargs: all #GType types for the columns, from first to last
262  *
263  * Creates a new list store as with @n_columns columns each of the types passed
264  * in.  As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
265  * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
266  * int, string and #GdkPixbuf respectively.
267  *
268  * Return value: a new #GtkListStore
269  **/
270 GtkListStore *
271 gtk_list_store_new (gint n_columns,
272                                ...)
273 {
274   GtkListStore *retval;
275   va_list args;
276   gint i;
277
278   g_return_val_if_fail (n_columns > 0, NULL);
279
280   retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
281   gtk_list_store_set_n_columns (retval, n_columns);
282
283   va_start (args, n_columns);
284
285   for (i = 0; i < n_columns; i++)
286     {
287       GType type = va_arg (args, GType);
288       if (! _gtk_tree_data_list_check_type (type))
289         {
290           g_warning ("%s: Invalid type %s passed to gtk_list_store_new\n", G_STRLOC, g_type_name (type));
291           g_object_unref (G_OBJECT (retval));
292           return NULL;
293         }
294
295       gtk_list_store_set_column_type (retval, i, type);
296     }
297
298   va_end (args);
299
300   return retval;
301 }
302
303
304 /**
305  * gtk_list_store_newv:
306  * @n_columns: number of columns in the list store
307  * @types: an array of #GType types for the columns, from first to last
308  *
309  * Non-vararg creation function.  Used primarily by language bindings.
310  *
311  * Return value: a new #GtkListStore
312  **/
313 GtkListStore *
314 gtk_list_store_newv (gint   n_columns,
315                      GType *types)
316 {
317   GtkListStore *retval;
318   gint i;
319
320   g_return_val_if_fail (n_columns > 0, NULL);
321
322   retval = GTK_LIST_STORE (g_object_new (gtk_list_store_get_type (), NULL));
323   gtk_list_store_set_n_columns (retval, n_columns);
324
325   for (i = 0; i < n_columns; i++)
326     {
327       if (! _gtk_tree_data_list_check_type (types[i]))
328         {
329           g_warning ("%s: Invalid type %s passed to gtk_list_store_newv\n", G_STRLOC, g_type_name (types[i]));
330           g_object_unref (G_OBJECT (retval));
331           return NULL;
332         }
333
334       gtk_list_store_set_column_type (retval, i, types[i]);
335     }
336
337   return retval;
338 }
339
340 /**
341  * gtk_list_store_set_column_types:
342  * @list_store: A #GtkListStore
343  * @n_columns: Number of columns for the list store
344  * @types: An array length n of #GTypes
345  * 
346  * This function is meant primarily for #GObjects that inherit from #GtkListStore,
347  * and should only be used when constructing a new #GtkListStore.  It will not
348  * function after a row has been added, or a method on the #GtkTreeModel
349  * interface is called.
350  **/
351 void
352 gtk_list_store_set_column_types (GtkListStore *list_store,
353                                  gint          n_columns,
354                                  GType        *types)
355 {
356   gint i;
357
358   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
359   g_return_if_fail (list_store->columns_dirty == 0);
360
361   gtk_list_store_set_n_columns (list_store, n_columns);
362    for (i = 0; i < n_columns; i++)
363     {
364       if (! _gtk_tree_data_list_check_type (types[i]))
365         {
366           g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_types\n", G_STRLOC, g_type_name (types[i]));
367           continue;
368         }
369       gtk_list_store_set_column_type (list_store, i, types[i]);
370     }
371 }
372
373 static void
374 gtk_list_store_set_n_columns (GtkListStore *list_store,
375                               gint          n_columns)
376 {
377   GType *new_columns;
378
379   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
380   g_return_if_fail (n_columns > 0);
381
382   if (list_store->n_columns == n_columns)
383     return;
384
385   new_columns = g_new0 (GType, n_columns);
386   if (list_store->column_headers)
387     {
388       /* copy the old header orders over */
389       if (n_columns >= list_store->n_columns)
390         memcpy (new_columns, list_store->column_headers, list_store->n_columns * sizeof (gchar *));
391       else
392         memcpy (new_columns, list_store->column_headers, n_columns * sizeof (GType));
393
394       g_free (list_store->column_headers);
395     }
396
397   if (list_store->sort_list)
398     _gtk_tree_data_list_header_free (list_store->sort_list);
399
400   list_store->sort_list = _gtk_tree_data_list_header_new (n_columns, list_store->column_headers);
401
402   list_store->column_headers = new_columns;
403   list_store->n_columns = n_columns;
404 }
405
406 static void
407 gtk_list_store_set_column_type (GtkListStore *list_store,
408                                 gint          column,
409                                 GType         type)
410 {
411   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
412   g_return_if_fail (column >=0 && column < list_store->n_columns);
413   if (!_gtk_tree_data_list_check_type (type))
414     {
415       g_warning ("%s: Invalid type %s passed to gtk_list_store_set_column_type\n", G_STRLOC, g_type_name (type));
416       return;
417     }
418
419   list_store->column_headers[column] = type;
420 }
421
422 static void
423 gtk_list_store_finalize (GObject *object)
424 {
425   GtkListStore *list_store = GTK_LIST_STORE (object);
426
427   g_list_foreach (list_store->root, (GFunc) _gtk_tree_data_list_free, list_store->column_headers);
428   _gtk_tree_data_list_header_free (list_store->sort_list);
429   g_free (list_store->column_headers);
430   
431   if (list_store->default_sort_destroy)
432     {
433       (* list_store->default_sort_destroy) (list_store->default_sort_data);
434       list_store->default_sort_destroy = NULL;
435       list_store->default_sort_data = NULL;
436     }
437
438   (* parent_class->finalize) (object);
439 }
440
441 /* Fulfill the GtkTreeModel requirements */
442 static guint
443 gtk_list_store_get_flags (GtkTreeModel *tree_model)
444 {
445   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
446
447   return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
448 }
449
450 static gint
451 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
452 {
453   GtkListStore *list_store = (GtkListStore *) tree_model;
454
455   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
456
457   list_store->columns_dirty = TRUE;
458
459   return list_store->n_columns;
460 }
461
462 static GType
463 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
464                                 gint          index)
465 {
466   GtkListStore *list_store = (GtkListStore *) tree_model;
467
468   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
469   g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
470                         index >= 0, G_TYPE_INVALID);
471
472   list_store->columns_dirty = TRUE;
473
474   return list_store->column_headers[index];
475 }
476
477 static gboolean
478 gtk_list_store_get_iter (GtkTreeModel *tree_model,
479                          GtkTreeIter  *iter,
480                          GtkTreePath  *path)
481 {
482   GtkListStore *list_store = (GtkListStore *) tree_model;
483   GSList *list;
484   gint i;
485
486   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
487   g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
488
489   list_store->columns_dirty = TRUE;
490
491   i = gtk_tree_path_get_indices (path)[0];
492
493   if (i >= list_store->length)
494     return FALSE;
495
496   list = g_slist_nth (G_SLIST (list_store->root), i);
497
498   /* If this fails, list_store->length has gotten mangled. */
499   g_assert (list);
500
501   iter->stamp = list_store->stamp;
502   iter->user_data = list;
503
504   return TRUE;
505 }
506
507 static GtkTreePath *
508 gtk_list_store_get_path (GtkTreeModel *tree_model,
509                          GtkTreeIter  *iter)
510 {
511   GtkTreePath *retval;
512   GSList *list;
513   gint i = 0;
514
515   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
516   g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
517
518   for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
519     {
520       if (list == G_SLIST (iter->user_data))
521         break;
522       i++;
523     }
524   if (list == NULL)
525     return NULL;
526
527   retval = gtk_tree_path_new ();
528   gtk_tree_path_append_index (retval, i);
529   return retval;
530 }
531
532 static void
533 gtk_list_store_get_value (GtkTreeModel *tree_model,
534                           GtkTreeIter  *iter,
535                           gint          column,
536                           GValue       *value)
537 {
538   GtkTreeDataList *list;
539   gint tmp_column = column;
540
541   g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
542   g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
543   g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
544
545   list = G_SLIST (iter->user_data)->data;
546
547   while (tmp_column-- > 0 && list)
548     list = list->next;
549
550   if (list == NULL)
551     g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
552   else
553     _gtk_tree_data_list_node_to_value (list,
554                                        GTK_LIST_STORE (tree_model)->column_headers[column],
555                                        value);
556 }
557
558 static gboolean
559 gtk_list_store_iter_next (GtkTreeModel  *tree_model,
560                           GtkTreeIter   *iter)
561 {
562   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
563   g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
564
565   iter->user_data = G_SLIST (iter->user_data)->next;
566
567   return (iter->user_data != NULL);
568 }
569
570 static gboolean
571 gtk_list_store_iter_children (GtkTreeModel *tree_model,
572                               GtkTreeIter  *iter,
573                               GtkTreeIter  *parent)
574 {
575   /* this is a list, nodes have no children */
576   if (parent)
577     return FALSE;
578
579   /* but if parent == NULL we return the list itself as children of the
580    * "root"
581    */
582
583   if (GTK_LIST_STORE (tree_model)->root)
584     {
585       iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
586       iter->user_data = GTK_LIST_STORE (tree_model)->root;
587       return TRUE;
588     }
589   else
590     return FALSE;
591 }
592
593 static gboolean
594 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
595                                GtkTreeIter  *iter)
596 {
597   return FALSE;
598 }
599
600 static gint
601 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
602                                 GtkTreeIter  *iter)
603 {
604   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
605   if (iter == NULL)
606     return GTK_LIST_STORE (tree_model)->length;
607
608   g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
609   return 0;
610 }
611
612 static gboolean
613 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
614                                GtkTreeIter  *iter,
615                                GtkTreeIter  *parent,
616                                gint          n)
617 {
618   GSList *child;
619
620   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
621
622   if (parent)
623     return FALSE;
624
625   child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
626
627   if (child)
628     {
629       iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
630       iter->user_data = child;
631       return TRUE;
632     }
633   else
634     return FALSE;
635 }
636
637 static gboolean
638 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
639                             GtkTreeIter  *iter,
640                             GtkTreeIter  *child)
641 {
642   return FALSE;
643 }
644
645 static gboolean
646 gtk_list_store_real_set_value (GtkListStore *list_store,
647                                GtkTreeIter  *iter,
648                                gint          column,
649                                GValue       *value,
650                                gboolean      sort)
651 {
652   GtkTreeDataList *list;
653   GtkTreeDataList *prev;
654   GtkTreePath *path;
655   GValue real_value = {0, };
656   gboolean converted = FALSE;
657   gint orig_column = column;
658   gboolean retval = FALSE;
659
660   g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
661   g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
662   g_return_val_if_fail (column >= 0 && column < list_store->n_columns, FALSE);
663   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
664
665   if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
666     {
667       if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
668              g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
669         {
670           g_warning ("%s: Unable to convert from %s to %s\n",
671                      G_STRLOC,
672                      g_type_name (G_VALUE_TYPE (value)),
673                      g_type_name (list_store->column_headers[column]));
674           return retval;
675         }
676       if (!g_value_transform (value, &real_value))
677         {
678           g_warning ("%s: Unable to make conversion from %s to %s\n",
679                      G_STRLOC,
680                      g_type_name (G_VALUE_TYPE (value)),
681                      g_type_name (list_store->column_headers[column]));
682           g_value_unset (&real_value);
683           return retval;
684         }
685       converted = TRUE;
686     }
687
688   prev = list = G_SLIST (iter->user_data)->data;
689
690   while (list != NULL)
691     {
692       if (column == 0)
693         {
694           path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
695           if (converted)
696             _gtk_tree_data_list_value_to_node (list, &real_value);
697           else
698             _gtk_tree_data_list_value_to_node (list, value);
699           retval = TRUE;
700           gtk_tree_path_free (path);
701           if (converted)
702             g_value_unset (&real_value);
703           return retval;
704         }
705
706       column--;
707       prev = list;
708       list = list->next;
709     }
710
711   if (G_SLIST (iter->user_data)->data == NULL)
712     {
713       G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
714       list->next = NULL;
715     }
716   else
717     {
718       list = prev->next = _gtk_tree_data_list_alloc ();
719       list->next = NULL;
720     }
721
722   while (column != 0)
723     {
724       list->next = _gtk_tree_data_list_alloc ();
725       list = list->next;
726       list->next = NULL;
727       column --;
728     }
729
730   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
731   if (converted)
732     _gtk_tree_data_list_value_to_node (list, &real_value);
733   else
734     _gtk_tree_data_list_value_to_node (list, value);
735   retval = TRUE;
736   gtk_tree_path_free (path);
737   if (converted)
738     g_value_unset (&real_value);
739
740   if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
741     gtk_list_store_sort_iter_changed (list_store, iter, orig_column);
742
743   return retval;
744 }
745
746
747 /**
748  * gtk_list_store_set_value:
749  * @list_store: A #GtkListStore
750  * @iter: A valid #GtkTreeIter for the row being modified
751  * @column: column number to modify
752  * @value: new value for the cell
753  *
754  * Sets the data in the cell specified by @iter and @column.
755  * The type of @value must be convertible to the type of the
756  * column.
757  *
758  **/
759 void
760 gtk_list_store_set_value (GtkListStore *list_store,
761                           GtkTreeIter  *iter,
762                           gint          column,
763                           GValue       *value)
764 {
765   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
766   g_return_if_fail (VALID_ITER (iter, list_store));
767   g_return_if_fail (column >= 0 && column < list_store->n_columns);
768   g_return_if_fail (G_IS_VALUE (value));
769
770   if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
771     {
772       GtkTreePath *path;
773
774       path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
775       gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
776       gtk_tree_path_free (path);
777     }
778 }
779
780 /**
781  * gtk_list_store_set_valist:
782  * @list_store: A #GtkListStore
783  * @iter: A valid #GtkTreeIter for the row being modified
784  * @var_args: va_list of column/value pairs
785  *
786  * See gtk_list_store_set(); this version takes a va_list for use by language
787  * bindings.
788  *
789  **/
790 void
791 gtk_list_store_set_valist (GtkListStore *list_store,
792                            GtkTreeIter  *iter,
793                            va_list       var_args)
794 {
795   gint column;
796   gboolean emit_signal = FALSE;
797   gboolean maybe_need_sort = FALSE;
798   GtkTreeIterCompareFunc func = NULL;
799
800   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
801   g_return_if_fail (VALID_ITER (iter, list_store));
802
803   column = va_arg (var_args, gint);
804
805   if (GTK_LIST_STORE_IS_SORTED (list_store))
806     {
807       if (list_store->sort_column_id != -1)
808         {
809           GtkTreeDataSortHeader *header;
810           header = _gtk_tree_data_list_get_header (list_store->sort_list,
811                                                    list_store->sort_column_id);
812           g_return_if_fail (header != NULL);
813           g_return_if_fail (header->func != NULL);
814           func = header->func;
815         }
816       else
817         {
818           func = list_store->default_sort_func;
819         }
820     }
821
822   if (func != gtk_tree_data_list_compare_func)
823     maybe_need_sort = TRUE;
824
825   while (column != -1)
826     {
827       GValue value = { 0, };
828       gchar *error = NULL;
829
830       if (column >= list_store->n_columns)
831         {
832           g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
833           break;
834         }
835       g_value_init (&value, list_store->column_headers[column]);
836
837       G_VALUE_COLLECT (&value, var_args, 0, &error);
838       if (error)
839         {
840           g_warning ("%s: %s", G_STRLOC, error);
841           g_free (error);
842
843           /* we purposely leak the value here, it might not be
844            * in a sane state if an error condition occoured
845            */
846           break;
847         }
848
849       /* FIXME: instead of calling this n times, refactor with above */
850       emit_signal = gtk_list_store_real_set_value (list_store,
851                                                    iter,
852                                                    column,
853                                                    &value,
854                                                    FALSE) || emit_signal;
855
856       if (func == gtk_tree_data_list_compare_func &&
857           column == list_store->sort_column_id)
858         maybe_need_sort = TRUE;
859
860       g_value_unset (&value);
861
862       column = va_arg (var_args, gint);
863     }
864
865   if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
866     gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
867
868   if (emit_signal)
869     {
870       GtkTreePath *path;
871
872       path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
873       gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
874       gtk_tree_path_free (path);
875     }
876 }
877
878 /**
879  * gtk_list_store_set:
880  * @list_store: a #GtkListStore
881  * @iter: row iterator
882  * @Varargs: pairs of column number and value, terminated with -1
883  *
884  * Sets the value of one or more cells in the row referenced by @iter.
885  * The variable argument list should contain integer column numbers,
886  * each column number followed by the value to be set.
887  * The list is terminated by a -1. For example, to set column 0 with type
888  * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
889  * 0, "Foo", -1)</literal>.
890  **/
891 void
892 gtk_list_store_set (GtkListStore *list_store,
893                     GtkTreeIter  *iter,
894                     ...)
895 {
896   va_list var_args;
897
898   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
899   g_return_if_fail (iter != NULL);
900   g_return_if_fail (iter->stamp == list_store->stamp);
901
902   va_start (var_args, iter);
903   gtk_list_store_set_valist (list_store, iter, var_args);
904   va_end (var_args);
905 }
906
907 static GSList*
908 remove_link_saving_prev (GSList  *list,
909                          GSList  *link,
910                          GSList **prevp)
911 {
912   GSList *tmp;
913   GSList *prev;
914
915   prev = NULL;
916   tmp = list;
917
918   while (tmp)
919     {
920       if (tmp == link)
921         {
922           if (prev)
923             prev->next = link->next;
924
925           if (list == link)
926             list = list->next;
927
928           link->next = NULL;
929           break;
930         }
931
932       prev = tmp;
933       tmp = tmp->next;
934     }
935
936   *prevp = prev;
937
938   return list;
939 }
940
941 static void
942 gtk_list_store_remove_silently (GtkListStore *list_store,
943                                 GtkTreeIter  *iter,
944                                 GtkTreePath  *path)
945 {
946   if (G_SLIST (iter->user_data)->data)
947     {
948       _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
949                                 list_store->column_headers);
950       G_SLIST (iter->user_data)->data = NULL;
951     }
952
953   {
954     GSList *prev = NULL;
955
956     list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
957                                                 G_SLIST (iter->user_data),
958                                                 &prev);
959
960     list_store->length -= 1;
961
962     if (iter->user_data == list_store->tail)
963       list_store->tail = prev;
964   }
965 }
966
967 /**
968  * gtk_list_store_remove:
969  * @list_store: A #GtkListStore
970  * @iter: A valid #GtkTreeIter
971  *
972  * Removes the given row from the list store.  After being removed, 
973  * @iter is set to be the next valid row, or invalidated if it pointed 
974  * to the last row in @list_store.
975  *
976  **/
977 void
978 gtk_list_store_remove (GtkListStore *list_store,
979                        GtkTreeIter  *iter)
980 {
981   GtkTreePath *path;
982   GSList *next;
983
984   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
985   g_return_if_fail (VALID_ITER (iter, list_store));
986
987   next = G_SLIST (iter->user_data)->next;
988   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
989
990   validate_list_store (list_store);
991
992   gtk_list_store_remove_silently (list_store, iter, path);
993
994   validate_list_store (list_store);
995
996   gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
997   gtk_tree_path_free (path);
998
999   if (next)
1000     {
1001       iter->stamp = list_store->stamp;
1002       iter->user_data = next;
1003     }
1004   else
1005     {
1006       iter->stamp = 0;
1007     }
1008 }
1009
1010 static void
1011 insert_after (GtkListStore *list_store,
1012               GSList       *sibling,
1013               GSList       *new_list)
1014 {
1015   g_return_if_fail (sibling != NULL);
1016   g_return_if_fail (new_list != NULL);
1017
1018   /* insert new node after list */
1019   new_list->next = sibling->next;
1020   sibling->next = new_list;
1021
1022   /* if list was the tail, the new node is the new tail */
1023   if (sibling == ((GSList *) list_store->tail))
1024     list_store->tail = new_list;
1025
1026   list_store->length += 1;
1027 }
1028
1029 /**
1030  * gtk_list_store_insert:
1031  * @list_store: A #GtkListStore
1032  * @iter: An unset #GtkTreeIter to set to the new row
1033  * @position: position to insert the new row
1034  *
1035  * Creates a new row at @position.  @iter will be changed to point to this new
1036  * row.  If @position is larger than the number of rows on the list, then the
1037  * new row will be appended to the list.  The row will be empty before this
1038  * function is called.  To fill in values, you need to call gtk_list_store_set()
1039  * or gtk_list_store_set_value().
1040  *
1041  **/
1042 void
1043 gtk_list_store_insert (GtkListStore *list_store,
1044                        GtkTreeIter  *iter,
1045                        gint          position)
1046 {
1047   GSList *list;
1048   GtkTreePath *path;
1049   GSList *new_list;
1050
1051   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1052   g_return_if_fail (iter != NULL);
1053   g_return_if_fail (position >= 0);
1054
1055   list_store->columns_dirty = TRUE;
1056
1057   if (position == 0 ||
1058       GTK_LIST_STORE_IS_SORTED (list_store))
1059     {
1060       gtk_list_store_prepend (list_store, iter);
1061       return;
1062     }
1063
1064   new_list = g_slist_alloc ();
1065
1066   list = g_slist_nth (G_SLIST (list_store->root), position - 1);
1067
1068   if (list == NULL)
1069     {
1070       g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
1071       return;
1072     }
1073
1074   insert_after (list_store, list, new_list);
1075
1076   iter->stamp = list_store->stamp;
1077   iter->user_data = new_list;
1078
1079   validate_list_store (list_store);
1080
1081   path = gtk_tree_path_new ();
1082   gtk_tree_path_append_index (path, position);
1083   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1084   gtk_tree_path_free (path);
1085 }
1086
1087 /**
1088  * gtk_list_store_insert_before:
1089  * @list_store: A #GtkListStore
1090  * @iter: An unset #GtkTreeIter to set to the new row
1091  * @sibling: A valid #GtkTreeIter, or %NULL
1092  *
1093  * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
1094  * appended to the end of the list. @iter will be changed to point to this new 
1095  * row. The row will be empty before this function is called. To fill in values,
1096  * you need to call gtk_list_store_set() or gtk_list_store_set_value().
1097  *
1098  **/
1099 void
1100 gtk_list_store_insert_before (GtkListStore *list_store,
1101                               GtkTreeIter  *iter,
1102                               GtkTreeIter  *sibling)
1103 {
1104   GtkTreePath *path;
1105   GSList *list, *prev, *new_list;
1106   gint i = 0;
1107
1108   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1109   g_return_if_fail (iter != NULL);
1110   if (sibling)
1111     g_return_if_fail (VALID_ITER (sibling, list_store));
1112
1113   list_store->columns_dirty = TRUE;
1114
1115   if (GTK_LIST_STORE_IS_SORTED (list_store))
1116     {
1117       gtk_list_store_prepend (list_store, iter);
1118       return;
1119     }
1120
1121   if (sibling == NULL)
1122     {
1123       gtk_list_store_append (list_store, iter);
1124       return;
1125     }
1126
1127   new_list = g_slist_alloc ();
1128
1129   prev = NULL;
1130   list = list_store->root;
1131   while (list && list != sibling->user_data)
1132     {
1133       prev = list;
1134       list = list->next;
1135       i++;
1136     }
1137
1138   if (list != sibling->user_data)
1139     {
1140       g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1141       return;
1142     }
1143
1144   /* if there are no nodes, we become the list tail, otherwise we
1145    * are inserting before any existing nodes so we can't change
1146    * the tail
1147    */
1148
1149   if (list_store->root == NULL)
1150     list_store->tail = new_list;
1151
1152   if (prev)
1153     {
1154       new_list->next = prev->next;
1155       prev->next = new_list;
1156     }
1157   else
1158     {
1159       new_list->next = list_store->root;
1160       list_store->root = new_list;
1161     }
1162
1163   iter->stamp = list_store->stamp;
1164   iter->user_data = new_list;
1165
1166   list_store->length += 1;
1167
1168   validate_list_store (list_store);
1169
1170   path = gtk_tree_path_new ();
1171   gtk_tree_path_append_index (path, i);
1172   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1173   gtk_tree_path_free (path);
1174 }
1175
1176 /**
1177  * gtk_list_store_insert_after:
1178  * @list_store: A #GtkListStore
1179  * @iter: An unset #GtkTreeIter to set to the new row
1180  * @sibling: A valid #GtkTreeIter, or %NULL
1181  *
1182  * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1183  * prepended to the beginning of the list. @iter will be changed to point to
1184  * this new row. The row will be empty after this function is called. To fill
1185  * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1186  *
1187  **/
1188 void
1189 gtk_list_store_insert_after (GtkListStore *list_store,
1190                              GtkTreeIter  *iter,
1191                              GtkTreeIter  *sibling)
1192 {
1193   GtkTreePath *path;
1194   GSList *list, *new_list;
1195   gint i = 0;
1196
1197   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1198   g_return_if_fail (iter != NULL);
1199   if (sibling)
1200     g_return_if_fail (VALID_ITER (sibling, list_store));
1201
1202   list_store->columns_dirty = TRUE;
1203
1204   if (sibling == NULL ||
1205       GTK_LIST_STORE_IS_SORTED (list_store))
1206     {
1207       gtk_list_store_prepend (list_store, iter);
1208       return;
1209     }
1210
1211   for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1212     i++;
1213
1214   g_return_if_fail (list == sibling->user_data);
1215
1216   new_list = g_slist_alloc ();
1217
1218   insert_after (list_store, list, new_list);
1219
1220   iter->stamp = list_store->stamp;
1221   iter->user_data = new_list;
1222
1223   validate_list_store (list_store);
1224
1225   path = gtk_tree_path_new ();
1226   gtk_tree_path_append_index (path, i + 1);
1227   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1228   gtk_tree_path_free (path);
1229 }
1230
1231 /**
1232  * gtk_list_store_prepend:
1233  * @list_store: A #GtkListStore
1234  * @iter: An unset #GtkTreeIter to set to the prepend row
1235  *
1236  * Prepends a new row to @list_store. @iter will be changed to point to this new
1237  * row. The row will be empty after this function is called. To fill in
1238  * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1239  *
1240  **/
1241 void
1242 gtk_list_store_prepend (GtkListStore *list_store,
1243                         GtkTreeIter  *iter)
1244 {
1245   GtkTreePath *path;
1246
1247   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1248   g_return_if_fail (iter != NULL);
1249
1250   iter->stamp = list_store->stamp;
1251   iter->user_data = g_slist_alloc ();
1252
1253   list_store->columns_dirty = TRUE;
1254
1255   if (list_store->root == NULL)
1256     list_store->tail = iter->user_data;
1257
1258   G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1259   list_store->root = iter->user_data;
1260
1261   list_store->length += 1;
1262
1263   validate_list_store (list_store);
1264
1265   path = gtk_tree_path_new ();
1266   gtk_tree_path_append_index (path, 0);
1267   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1268   gtk_tree_path_free (path);
1269 }
1270
1271 /**
1272  * gtk_list_store_append:
1273  * @list_store: A #GtkListStore
1274  * @iter: An unset #GtkTreeIter to set to the appended row
1275  *
1276  * Appends a new row to @list_store.  @iter will be changed to point to this new
1277  * row.  The row will be empty after this function is called.  To fill in
1278  * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1279  *
1280  **/
1281 void
1282 gtk_list_store_append (GtkListStore *list_store,
1283                        GtkTreeIter  *iter)
1284 {
1285   GtkTreePath *path;
1286
1287   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1288   g_return_if_fail (iter != NULL);
1289
1290   list_store->columns_dirty = TRUE;
1291
1292   if (GTK_LIST_STORE_IS_SORTED (list_store))
1293     {
1294       gtk_list_store_prepend (list_store, iter);
1295       return;
1296     }
1297
1298   iter->stamp = list_store->stamp;
1299   iter->user_data = g_slist_alloc ();
1300
1301   if (list_store->tail)
1302     ((GSList *)list_store->tail)->next = iter->user_data;
1303   else
1304     list_store->root = iter->user_data;
1305
1306   list_store->tail = iter->user_data;
1307
1308   list_store->length += 1;
1309
1310   validate_list_store (list_store);
1311
1312   path = gtk_tree_path_new ();
1313   gtk_tree_path_append_index (path, list_store->length - 1);
1314   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1315   gtk_tree_path_free (path);
1316 }
1317
1318 /**
1319  * gtk_list_store_clear:
1320  * @list_store: a #GtkListStore.
1321  *
1322  * Removes all rows from the list store.  
1323  *
1324  **/
1325 void
1326 gtk_list_store_clear (GtkListStore *list_store)
1327 {
1328   GtkTreeIter iter;
1329   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1330
1331   while (list_store->root)
1332     {
1333       iter.stamp = list_store->stamp;
1334       iter.user_data = list_store->root;
1335       gtk_list_store_remove (list_store, &iter);
1336     }
1337 }
1338
1339
1340 static gboolean
1341 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1342                                  GtkTreePath       *path)
1343 {
1344   GtkTreeIter iter;
1345   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1346
1347   if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1348                                &iter,
1349                                path))
1350     {
1351       gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1352       return TRUE;
1353     }
1354   return FALSE;
1355 }
1356
1357 static gboolean
1358 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1359                               GtkTreePath       *path,
1360                               GtkSelectionData  *selection_data)
1361 {
1362   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1363
1364   /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1365    * target, because the default handler does it for us, but
1366    * we do anyway for the convenience of someone maybe overriding the
1367    * default handler.
1368    */
1369
1370   if (gtk_tree_set_row_drag_data (selection_data,
1371                                   GTK_TREE_MODEL (drag_source),
1372                                   path))
1373     {
1374       return TRUE;
1375     }
1376   else
1377     {
1378       /* FIXME handle text targets at least. */
1379     }
1380
1381   return FALSE;
1382 }
1383
1384 static gboolean
1385 gtk_list_store_drag_data_received (GtkTreeDragDest   *drag_dest,
1386                                    GtkTreePath       *dest,
1387                                    GtkSelectionData  *selection_data)
1388 {
1389   GtkTreeModel *tree_model;
1390   GtkListStore *list_store;
1391   GtkTreeModel *src_model = NULL;
1392   GtkTreePath *src_path = NULL;
1393   gboolean retval = FALSE;
1394
1395   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1396
1397   tree_model = GTK_TREE_MODEL (drag_dest);
1398   list_store = GTK_LIST_STORE (drag_dest);
1399
1400   if (gtk_tree_get_row_drag_data (selection_data,
1401                                   &src_model,
1402                                   &src_path) &&
1403       src_model == tree_model)
1404     {
1405       /* Copy the given row to a new position */
1406       GtkTreeIter src_iter;
1407       GtkTreeIter dest_iter;
1408       GtkTreePath *prev;
1409
1410       if (!gtk_tree_model_get_iter (src_model,
1411                                     &src_iter,
1412                                     src_path))
1413         {
1414           goto out;
1415         }
1416
1417       /* Get the path to insert _after_ (dest is the path to insert _before_) */
1418       prev = gtk_tree_path_copy (dest);
1419
1420       if (!gtk_tree_path_prev (prev))
1421         {
1422           /* dest was the first spot in the list; which means we are supposed
1423            * to prepend.
1424            */
1425           gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1426                                   &dest_iter);
1427
1428           retval = TRUE;
1429         }
1430       else
1431         {
1432           if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1433                                        &dest_iter,
1434                                        prev))
1435             {
1436               GtkTreeIter tmp_iter = dest_iter;
1437               gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1438                                            &dest_iter,
1439                                            &tmp_iter);
1440               retval = TRUE;
1441             }
1442         }
1443
1444       gtk_tree_path_free (prev);
1445
1446       /* If we succeeded in creating dest_iter, copy data from src
1447        */
1448       if (retval)
1449         {
1450           GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1451           GtkTreeDataList *copy_head = NULL;
1452           GtkTreeDataList *copy_prev = NULL;
1453           GtkTreeDataList *copy_iter = NULL;
1454           GtkTreePath *path;
1455           gint col;
1456
1457           col = 0;
1458           while (dl)
1459             {
1460               copy_iter = _gtk_tree_data_list_node_copy (dl,
1461                                                          list_store->column_headers[col]);
1462
1463               if (copy_head == NULL)
1464                 copy_head = copy_iter;
1465
1466               if (copy_prev)
1467                 copy_prev->next = copy_iter;
1468
1469               copy_prev = copy_iter;
1470
1471               dl = dl->next;
1472               ++col;
1473             }
1474
1475           dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1476           G_SLIST (dest_iter.user_data)->data = copy_head;
1477
1478           path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1479           gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter);
1480           gtk_tree_path_free (path);
1481         }
1482     }
1483   else
1484     {
1485       /* FIXME maybe add some data targets eventually, or handle text
1486        * targets in the simple case.
1487        */
1488     }
1489
1490  out:
1491
1492   if (src_path)
1493     gtk_tree_path_free (src_path);
1494
1495   return retval;
1496 }
1497
1498 static gboolean
1499 gtk_list_store_row_drop_possible (GtkTreeDragDest  *drag_dest,
1500                                   GtkTreePath      *dest_path,
1501                                   GtkSelectionData *selection_data)
1502 {
1503   gint *indices;
1504   GtkTreeModel *src_model = NULL;
1505   GtkTreePath *src_path = NULL;
1506   gboolean retval = FALSE;
1507
1508   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1509
1510   if (!gtk_tree_get_row_drag_data (selection_data,
1511                                    &src_model,
1512                                    &src_path))
1513     goto out;
1514     
1515   if (src_model != GTK_TREE_MODEL (drag_dest))
1516     goto out;
1517
1518   if (gtk_tree_path_get_depth (dest_path) != 1)
1519     goto out;
1520
1521   /* can drop before any existing node, or before one past any existing. */
1522
1523   indices = gtk_tree_path_get_indices (dest_path);
1524
1525   if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1526     retval = TRUE;
1527
1528  out:
1529   gtk_tree_path_free (src_path);
1530   
1531   return retval;
1532 }
1533
1534
1535 /* Sorting */
1536 typedef struct _SortTuple
1537 {
1538   gint offset;
1539   GSList *el;
1540 } SortTuple;
1541
1542 static gint
1543 gtk_list_store_compare_func (gconstpointer a,
1544                              gconstpointer b,
1545                              gpointer      user_data)
1546 {
1547   GtkListStore *list_store = user_data;
1548   GSList *el_a; /* Los Angeles? */
1549   GSList *el_b;
1550   GtkTreeIter iter_a;
1551   GtkTreeIter iter_b;
1552   gint retval;
1553   GtkTreeIterCompareFunc func;
1554   gpointer data;
1555
1556
1557   if (list_store->sort_column_id != -1)
1558     {
1559       GtkTreeDataSortHeader *header;
1560
1561       header = _gtk_tree_data_list_get_header (list_store->sort_list,
1562                                                list_store->sort_column_id);
1563       g_return_val_if_fail (header != NULL, 0);
1564       g_return_val_if_fail (header->func != NULL, 0);
1565
1566       func = header->func;
1567       data = header->data;
1568     }
1569   else
1570     {
1571       g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1572       func = list_store->default_sort_func;
1573       data = list_store->default_sort_data;
1574     }
1575
1576   el_a = ((SortTuple *) a)->el;
1577   el_b = ((SortTuple *) b)->el;
1578
1579   iter_a.stamp = list_store->stamp;
1580   iter_a.user_data = el_a;
1581   iter_b.stamp = list_store->stamp;
1582   iter_b.user_data = el_b;
1583
1584   retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1585
1586   if (list_store->order == GTK_SORT_DESCENDING)
1587     {
1588       if (retval > 0)
1589         retval = -1;
1590       else if (retval < 0)
1591         retval = 1;
1592     }
1593   return retval;
1594 }
1595
1596 static void
1597 gtk_list_store_sort (GtkListStore *list_store)
1598 {
1599   GArray *sort_array;
1600   gint i;
1601   gint *new_order;
1602   GSList *list;
1603   GtkTreePath *path;
1604
1605   if (list_store->length <= 1)
1606     return;
1607
1608   g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1609
1610   list = G_SLIST (list_store->root);
1611
1612   sort_array = g_array_sized_new (FALSE, FALSE,
1613                                   sizeof (SortTuple),
1614                                   list_store->length);
1615
1616   for (i = 0; i < list_store->length; i++)
1617     {
1618       SortTuple tuple;
1619
1620       /* If this fails, we are in an inconsistent state.  Bad */
1621       g_return_if_fail (list != NULL);
1622
1623       tuple.offset = i;
1624       tuple.el = list;
1625       g_array_append_val (sort_array, tuple);
1626
1627       list = list->next;
1628     }
1629
1630   g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1631
1632   for (i = 0; i < list_store->length - 1; i++)
1633       g_array_index (sort_array, SortTuple, i).el->next =
1634         g_array_index (sort_array, SortTuple, i + 1).el;
1635   g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1636   list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1637
1638   /* Let the world know about our new order */
1639   new_order = g_new (gint, list_store->length);
1640   for (i = 0; i < list_store->length; i++)
1641     new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1642
1643   path = gtk_tree_path_new ();
1644   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1645                                  path, NULL, new_order);
1646   gtk_tree_path_free (path);
1647   g_free (new_order);
1648   g_array_free (sort_array, TRUE);
1649 }
1650
1651 static void
1652 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1653                                   GtkTreeIter  *iter,
1654                                   gint          column)
1655
1656 {
1657   GSList *prev = NULL;
1658   GSList *next = NULL;
1659   GSList *list = G_SLIST (list_store->root);
1660   GtkTreePath *tmp_path;
1661   GtkTreeIter tmp_iter;
1662   gint cmp_a = 0;
1663   gint cmp_b = 0;
1664   gint i;
1665   gint old_location;
1666   gint new_location;
1667   gint *new_order;
1668   GtkTreeIterCompareFunc func;
1669   gpointer data;
1670
1671   if (list_store->length < 2)
1672     return;
1673
1674   tmp_iter.stamp = list_store->stamp;
1675
1676   if (list_store->sort_column_id != -1)
1677     {
1678       GtkTreeDataSortHeader *header;
1679       header = _gtk_tree_data_list_get_header (list_store->sort_list,
1680                                                list_store->sort_column_id);
1681       g_return_if_fail (header != NULL);
1682       g_return_if_fail (header->func != NULL);
1683       func = header->func;
1684       data = header->data;
1685     }
1686   else
1687     {
1688       g_return_if_fail (list_store->default_sort_func != NULL);
1689       func = list_store->default_sort_func;
1690       data = list_store->default_sort_data;
1691     }
1692
1693   /* If it's the built in function, we don't sort. */
1694   if (func == gtk_tree_data_list_compare_func &&
1695       list_store->sort_column_id != column)
1696     return;
1697
1698   old_location = 0;
1699   /* First we find the iter, its prev, and its next */
1700   while (list)
1701     {
1702       if (list == G_SLIST (iter->user_data))
1703         break;
1704       prev = list;
1705       list = list->next;
1706       old_location++;
1707     }
1708   g_assert (list != NULL);
1709
1710   next = list->next;
1711
1712   /* Check the common case, where we don't need to sort it moved. */
1713   if (prev != NULL)
1714     {
1715       tmp_iter.user_data = prev;
1716       cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1717     }
1718
1719   if (next != NULL)
1720     {
1721       tmp_iter.user_data = next;
1722       cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1723     }
1724
1725
1726   if (list_store->order == GTK_SORT_DESCENDING)
1727     {
1728       if (cmp_a < 0)
1729         cmp_a = 1;
1730       else if (cmp_a > 0)
1731         cmp_a = -1;
1732
1733       if (cmp_b < 0)
1734         cmp_b = 1;
1735       else if (cmp_b > 0)
1736         cmp_b = -1;
1737     }
1738
1739   if (prev == NULL && cmp_b <= 0)
1740     return;
1741   else if (next == NULL && cmp_a <= 0)
1742     return;
1743   else if (prev != NULL && next != NULL &&
1744            cmp_a <= 0 && cmp_b <= 0)
1745     return;
1746
1747   /* We actually need to sort it */
1748   /* First, remove the old link. */
1749
1750   if (prev == NULL)
1751     list_store->root = next;
1752   else
1753     prev->next = next;
1754   if (next == NULL)
1755     list_store->tail = prev;
1756   list->next = NULL;
1757   
1758   /* FIXME: as an optimization, we can potentially start at next */
1759   prev = NULL;
1760   list = G_SLIST (list_store->root);
1761   new_location = 0;
1762   tmp_iter.user_data = list;
1763   if (list_store->order == GTK_SORT_DESCENDING)
1764     cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1765   else
1766     cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1767
1768   while ((list->next) && (cmp_a > 0))
1769     {
1770       prev = list;
1771       list = list->next;
1772       new_location++;
1773       tmp_iter.user_data = list;
1774       if (list_store->order == GTK_SORT_DESCENDING)
1775         cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1776       else
1777         cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1778     }
1779
1780   if ((!list->next) && (cmp_a > 0))
1781     {
1782       new_location++;
1783       list->next = G_SLIST (iter->user_data);
1784       list_store->tail = list->next;
1785     }
1786   else if (prev)
1787     {
1788       prev->next = G_SLIST (iter->user_data);
1789       G_SLIST (iter->user_data)->next = list;
1790     }
1791   else
1792     {
1793       G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1794       list_store->root = G_SLIST (iter->user_data);
1795     }
1796
1797   /* Emit the reordered signal. */
1798   new_order = g_new (int, list_store->length);
1799   if (old_location < new_location)
1800     for (i = 0; i < list_store->length; i++)
1801       {
1802         if (i < old_location ||
1803             i > new_location)
1804           new_order[i] = i;
1805         else if (i >= old_location &&
1806                  i < new_location)
1807           new_order[i] = i + 1;
1808         else if (i == new_location)
1809           new_order[i] = old_location;
1810       }
1811   else
1812     for (i = 0; i < list_store->length; i++)
1813       {
1814         if (i < new_location ||
1815             i > old_location)
1816           new_order[i] = i;
1817         else if (i > new_location &&
1818                  i <= old_location)
1819           new_order[i] = i - 1;
1820         else if (i == new_location)
1821           new_order[i] = old_location;
1822       }
1823
1824   tmp_path = gtk_tree_path_new ();
1825   tmp_iter.user_data = NULL;
1826
1827   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1828                                  tmp_path, NULL,
1829                                  new_order);
1830
1831   gtk_tree_path_free (tmp_path);
1832   g_free (new_order);
1833 }
1834
1835 static gboolean
1836 gtk_list_store_get_sort_column_id (GtkTreeSortable  *sortable,
1837                                    gint             *sort_column_id,
1838                                    GtkSortType      *order)
1839 {
1840   GtkListStore *list_store = (GtkListStore *) sortable;
1841
1842   g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1843
1844   if (list_store->sort_column_id == -1)
1845     return FALSE;
1846
1847   if (sort_column_id)
1848     * sort_column_id = list_store->sort_column_id;
1849   if (order)
1850     * order = list_store->order;
1851   return TRUE;
1852 }
1853
1854 static void
1855 gtk_list_store_set_sort_column_id (GtkTreeSortable  *sortable,
1856                                    gint              sort_column_id,
1857                                    GtkSortType       order)
1858 {
1859   GtkListStore *list_store = (GtkListStore *) sortable;
1860
1861   g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1862
1863   if ((list_store->sort_column_id == sort_column_id) &&
1864       (list_store->order == order))
1865     return;
1866
1867   if (sort_column_id != -1)
1868     {
1869       GtkTreeDataSortHeader *header = NULL;
1870
1871       header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
1872
1873       /* We want to make sure that we have a function */
1874       g_return_if_fail (header != NULL);
1875       g_return_if_fail (header->func != NULL);
1876     }
1877   else
1878     {
1879       g_return_if_fail (list_store->default_sort_func != NULL);
1880     }
1881
1882
1883   list_store->sort_column_id = sort_column_id;
1884   list_store->order = order;
1885
1886   gtk_list_store_sort (list_store);
1887
1888   gtk_tree_sortable_sort_column_changed (sortable);
1889 }
1890
1891 static void
1892 gtk_list_store_set_sort_func (GtkTreeSortable        *sortable,
1893                               gint                    sort_column_id,
1894                               GtkTreeIterCompareFunc  func,
1895                               gpointer                data,
1896                               GtkDestroyNotify        destroy)
1897 {
1898   GtkListStore *list_store = (GtkListStore *) sortable;
1899   GtkTreeDataSortHeader *header = NULL;
1900   GList *list;
1901
1902   g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1903   g_return_if_fail (func != NULL);
1904
1905   for (list = list_store->sort_list; list; list = list->next)
1906     {
1907       header = (GtkTreeDataSortHeader*) list->data;
1908       if (header->sort_column_id == sort_column_id)
1909         break;
1910     }
1911
1912   if (header == NULL)
1913     {
1914       header = g_new0 (GtkTreeDataSortHeader, 1);
1915       header->sort_column_id = sort_column_id;
1916       list_store->sort_list = g_list_append (list_store->sort_list, header);
1917     }
1918
1919   if (header->destroy)
1920     (* header->destroy) (header->data);
1921
1922   header->func = func;
1923   header->data = data;
1924   header->destroy = destroy;
1925 }
1926
1927
1928 static void
1929 gtk_list_store_set_default_sort_func (GtkTreeSortable        *sortable,
1930                                       GtkTreeIterCompareFunc  func,
1931                                       gpointer                data,
1932                                       GtkDestroyNotify        destroy)
1933 {
1934   GtkListStore *list_store = (GtkListStore *) sortable;
1935
1936   g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1937
1938   if (list_store->default_sort_destroy)
1939     (* list_store->default_sort_destroy) (list_store->default_sort_data);
1940
1941   list_store->default_sort_func = func;
1942   list_store->default_sort_data = data;
1943   list_store->default_sort_destroy = destroy;
1944 }
1945
1946 static gboolean
1947 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
1948 {
1949   GtkListStore *list_store = (GtkListStore *) sortable;
1950
1951   g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1952
1953   return (list_store->default_sort_func != NULL);
1954 }