]> Pileus Git - ~andy/gtk/blob - gtk/gtkliststore.c
added compile time switch to put the tree views into a hpaned for owen to
[~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       GtkDestroyNotify d = list_store->default_sort_destroy;
434
435       list_store->default_sort_destroy = NULL;
436       d (list_store->default_sort_data);
437       list_store->default_sort_data = NULL;
438     }
439
440   (* parent_class->finalize) (object);
441 }
442
443 /* Fulfill the GtkTreeModel requirements */
444 static guint
445 gtk_list_store_get_flags (GtkTreeModel *tree_model)
446 {
447   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
448
449   return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
450 }
451
452 static gint
453 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
454 {
455   GtkListStore *list_store = (GtkListStore *) tree_model;
456
457   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), 0);
458
459   list_store->columns_dirty = TRUE;
460
461   return list_store->n_columns;
462 }
463
464 static GType
465 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
466                                 gint          index)
467 {
468   GtkListStore *list_store = (GtkListStore *) tree_model;
469
470   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), G_TYPE_INVALID);
471   g_return_val_if_fail (index < GTK_LIST_STORE (tree_model)->n_columns &&
472                         index >= 0, G_TYPE_INVALID);
473
474   list_store->columns_dirty = TRUE;
475
476   return list_store->column_headers[index];
477 }
478
479 static gboolean
480 gtk_list_store_get_iter (GtkTreeModel *tree_model,
481                          GtkTreeIter  *iter,
482                          GtkTreePath  *path)
483 {
484   GtkListStore *list_store = (GtkListStore *) tree_model;
485   GSList *list;
486   gint i;
487
488   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
489   g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
490
491   list_store->columns_dirty = TRUE;
492
493   i = gtk_tree_path_get_indices (path)[0];
494
495   if (i >= list_store->length)
496     return FALSE;
497
498   list = g_slist_nth (G_SLIST (list_store->root), i);
499
500   /* If this fails, list_store->length has gotten mangled. */
501   g_assert (list);
502
503   iter->stamp = list_store->stamp;
504   iter->user_data = list;
505
506   return TRUE;
507 }
508
509 static GtkTreePath *
510 gtk_list_store_get_path (GtkTreeModel *tree_model,
511                          GtkTreeIter  *iter)
512 {
513   GtkTreePath *retval;
514   GSList *list;
515   gint i = 0;
516
517   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), NULL);
518   g_return_val_if_fail (iter->stamp == GTK_LIST_STORE (tree_model)->stamp, NULL);
519   if (G_SLIST (iter->user_data) == G_SLIST (GTK_LIST_STORE (tree_model)->tail))
520     {
521       retval = gtk_tree_path_new ();
522       gtk_tree_path_append_index (retval, GTK_LIST_STORE (tree_model)->length - 1);
523       return retval;
524     }
525
526   for (list = G_SLIST (GTK_LIST_STORE (tree_model)->root); list; list = list->next)
527     {
528       if (list == G_SLIST (iter->user_data))
529         break;
530       i++;
531     }
532   if (list == NULL)
533     return NULL;
534
535   retval = gtk_tree_path_new ();
536   gtk_tree_path_append_index (retval, i);
537   return retval;
538 }
539
540 static void
541 gtk_list_store_get_value (GtkTreeModel *tree_model,
542                           GtkTreeIter  *iter,
543                           gint          column,
544                           GValue       *value)
545 {
546   GtkTreeDataList *list;
547   gint tmp_column = column;
548
549   g_return_if_fail (GTK_IS_LIST_STORE (tree_model));
550   g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
551   g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
552
553   list = G_SLIST (iter->user_data)->data;
554
555   while (tmp_column-- > 0 && list)
556     list = list->next;
557
558   if (list == NULL)
559     g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
560   else
561     _gtk_tree_data_list_node_to_value (list,
562                                        GTK_LIST_STORE (tree_model)->column_headers[column],
563                                        value);
564 }
565
566 static gboolean
567 gtk_list_store_iter_next (GtkTreeModel  *tree_model,
568                           GtkTreeIter   *iter)
569 {
570   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
571   g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, FALSE);
572
573   iter->user_data = G_SLIST (iter->user_data)->next;
574
575   return (iter->user_data != NULL);
576 }
577
578 static gboolean
579 gtk_list_store_iter_children (GtkTreeModel *tree_model,
580                               GtkTreeIter  *iter,
581                               GtkTreeIter  *parent)
582 {
583   /* this is a list, nodes have no children */
584   if (parent)
585     return FALSE;
586
587   /* but if parent == NULL we return the list itself as children of the
588    * "root"
589    */
590
591   if (GTK_LIST_STORE (tree_model)->root)
592     {
593       iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
594       iter->user_data = GTK_LIST_STORE (tree_model)->root;
595       return TRUE;
596     }
597   else
598     return FALSE;
599 }
600
601 static gboolean
602 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
603                                GtkTreeIter  *iter)
604 {
605   return FALSE;
606 }
607
608 static gint
609 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
610                                 GtkTreeIter  *iter)
611 {
612   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), -1);
613   if (iter == NULL)
614     return GTK_LIST_STORE (tree_model)->length;
615
616   g_return_val_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp, -1);
617   return 0;
618 }
619
620 static gboolean
621 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
622                                GtkTreeIter  *iter,
623                                GtkTreeIter  *parent,
624                                gint          n)
625 {
626   GSList *child;
627
628   g_return_val_if_fail (GTK_IS_LIST_STORE (tree_model), FALSE);
629
630   if (parent)
631     return FALSE;
632
633   child = g_slist_nth (G_SLIST (GTK_LIST_STORE (tree_model)->root), n);
634
635   if (child)
636     {
637       iter->stamp = GTK_LIST_STORE (tree_model)->stamp;
638       iter->user_data = child;
639       return TRUE;
640     }
641   else
642     return FALSE;
643 }
644
645 static gboolean
646 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
647                             GtkTreeIter  *iter,
648                             GtkTreeIter  *child)
649 {
650   return FALSE;
651 }
652
653 static gboolean
654 gtk_list_store_real_set_value (GtkListStore *list_store,
655                                GtkTreeIter  *iter,
656                                gint          column,
657                                GValue       *value,
658                                gboolean      sort)
659 {
660   GtkTreeDataList *list;
661   GtkTreeDataList *prev;
662   GValue real_value = {0, };
663   gboolean converted = FALSE;
664   gint orig_column = column;
665   gboolean retval = FALSE;
666
667   g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
668   g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
669   g_return_val_if_fail (column >= 0 && column < list_store->n_columns, FALSE);
670   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
671
672   if (! g_type_is_a (G_VALUE_TYPE (value), list_store->column_headers[column]))
673     {
674       if (! (g_value_type_compatible (G_VALUE_TYPE (value), list_store->column_headers[column]) &&
675              g_value_type_compatible (list_store->column_headers[column], G_VALUE_TYPE (value))))
676         {
677           g_warning ("%s: Unable to convert from %s to %s\n",
678                      G_STRLOC,
679                      g_type_name (G_VALUE_TYPE (value)),
680                      g_type_name (list_store->column_headers[column]));
681           return retval;
682         }
683       if (!g_value_transform (value, &real_value))
684         {
685           g_warning ("%s: Unable to make conversion from %s to %s\n",
686                      G_STRLOC,
687                      g_type_name (G_VALUE_TYPE (value)),
688                      g_type_name (list_store->column_headers[column]));
689           g_value_unset (&real_value);
690           return retval;
691         }
692       converted = TRUE;
693     }
694
695   prev = list = G_SLIST (iter->user_data)->data;
696
697   while (list != NULL)
698     {
699       if (column == 0)
700         {
701           if (converted)
702             _gtk_tree_data_list_value_to_node (list, &real_value);
703           else
704             _gtk_tree_data_list_value_to_node (list, value);
705           retval = TRUE;
706           if (converted)
707             g_value_unset (&real_value);
708           return retval;
709         }
710
711       column--;
712       prev = list;
713       list = list->next;
714     }
715
716   if (G_SLIST (iter->user_data)->data == NULL)
717     {
718       G_SLIST (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
719       list->next = NULL;
720     }
721   else
722     {
723       list = prev->next = _gtk_tree_data_list_alloc ();
724       list->next = NULL;
725     }
726
727   while (column != 0)
728     {
729       list->next = _gtk_tree_data_list_alloc ();
730       list = list->next;
731       list->next = NULL;
732       column --;
733     }
734
735   if (converted)
736     _gtk_tree_data_list_value_to_node (list, &real_value);
737   else
738     _gtk_tree_data_list_value_to_node (list, value);
739   retval = TRUE;
740   if (converted)
741     g_value_unset (&real_value);
742
743   if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
744     gtk_list_store_sort_iter_changed (list_store, iter, orig_column);
745
746   return retval;
747 }
748
749
750 /**
751  * gtk_list_store_set_value:
752  * @list_store: A #GtkListStore
753  * @iter: A valid #GtkTreeIter for the row being modified
754  * @column: column number to modify
755  * @value: new value for the cell
756  *
757  * Sets the data in the cell specified by @iter and @column.
758  * The type of @value must be convertible to the type of the
759  * column.
760  *
761  **/
762 void
763 gtk_list_store_set_value (GtkListStore *list_store,
764                           GtkTreeIter  *iter,
765                           gint          column,
766                           GValue       *value)
767 {
768   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
769   g_return_if_fail (VALID_ITER (iter, list_store));
770   g_return_if_fail (column >= 0 && column < list_store->n_columns);
771   g_return_if_fail (G_IS_VALUE (value));
772
773   if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
774     {
775       GtkTreePath *path;
776
777       path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
778       gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
779       gtk_tree_path_free (path);
780     }
781 }
782
783 /**
784  * gtk_list_store_set_valist:
785  * @list_store: A #GtkListStore
786  * @iter: A valid #GtkTreeIter for the row being modified
787  * @var_args: va_list of column/value pairs
788  *
789  * See gtk_list_store_set(); this version takes a va_list for use by language
790  * bindings.
791  *
792  **/
793 void
794 gtk_list_store_set_valist (GtkListStore *list_store,
795                            GtkTreeIter  *iter,
796                            va_list       var_args)
797 {
798   gint column;
799   gboolean emit_signal = FALSE;
800   gboolean maybe_need_sort = FALSE;
801   GtkTreeIterCompareFunc func = NULL;
802
803   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
804   g_return_if_fail (VALID_ITER (iter, list_store));
805
806   column = va_arg (var_args, gint);
807
808   if (GTK_LIST_STORE_IS_SORTED (list_store))
809     {
810       if (list_store->sort_column_id != -1)
811         {
812           GtkTreeDataSortHeader *header;
813           header = _gtk_tree_data_list_get_header (list_store->sort_list,
814                                                    list_store->sort_column_id);
815           g_return_if_fail (header != NULL);
816           g_return_if_fail (header->func != NULL);
817           func = header->func;
818         }
819       else
820         {
821           func = list_store->default_sort_func;
822         }
823     }
824
825   if (func != gtk_tree_data_list_compare_func)
826     maybe_need_sort = TRUE;
827
828   while (column != -1)
829     {
830       GValue value = { 0, };
831       gchar *error = NULL;
832
833       if (column >= list_store->n_columns)
834         {
835           g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
836           break;
837         }
838       g_value_init (&value, list_store->column_headers[column]);
839
840       G_VALUE_COLLECT (&value, var_args, 0, &error);
841       if (error)
842         {
843           g_warning ("%s: %s", G_STRLOC, error);
844           g_free (error);
845
846           /* we purposely leak the value here, it might not be
847            * in a sane state if an error condition occoured
848            */
849           break;
850         }
851
852       /* FIXME: instead of calling this n times, refactor with above */
853       emit_signal = gtk_list_store_real_set_value (list_store,
854                                                    iter,
855                                                    column,
856                                                    &value,
857                                                    FALSE) || emit_signal;
858
859       if (func == gtk_tree_data_list_compare_func &&
860           column == list_store->sort_column_id)
861         maybe_need_sort = TRUE;
862
863       g_value_unset (&value);
864
865       column = va_arg (var_args, gint);
866     }
867
868   if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
869     gtk_list_store_sort_iter_changed (list_store, iter, list_store->sort_column_id);
870
871   if (emit_signal)
872     {
873       GtkTreePath *path;
874
875       path = gtk_tree_model_get_path (GTK_TREE_MODEL (list_store), iter);
876       gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
877       gtk_tree_path_free (path);
878     }
879 }
880
881 /**
882  * gtk_list_store_set:
883  * @list_store: a #GtkListStore
884  * @iter: row iterator
885  * @Varargs: pairs of column number and value, terminated with -1
886  *
887  * Sets the value of one or more cells in the row referenced by @iter.
888  * The variable argument list should contain integer column numbers,
889  * each column number followed by the value to be set.
890  * The list is terminated by a -1. For example, to set column 0 with type
891  * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
892  * 0, "Foo", -1)</literal>.
893  **/
894 void
895 gtk_list_store_set (GtkListStore *list_store,
896                     GtkTreeIter  *iter,
897                     ...)
898 {
899   va_list var_args;
900
901   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
902   g_return_if_fail (iter != NULL);
903   g_return_if_fail (iter->stamp == list_store->stamp);
904
905   va_start (var_args, iter);
906   gtk_list_store_set_valist (list_store, iter, var_args);
907   va_end (var_args);
908 }
909
910 static GSList*
911 remove_link_saving_prev (GSList  *list,
912                          GSList  *link,
913                          GSList **prevp)
914 {
915   GSList *tmp;
916   GSList *prev;
917
918   prev = NULL;
919   tmp = list;
920
921   while (tmp)
922     {
923       if (tmp == link)
924         {
925           if (prev)
926             prev->next = link->next;
927
928           if (list == link)
929             list = list->next;
930
931           link->next = NULL;
932           break;
933         }
934
935       prev = tmp;
936       tmp = tmp->next;
937     }
938
939   *prevp = prev;
940
941   return list;
942 }
943
944 static void
945 gtk_list_store_remove_silently (GtkListStore *list_store,
946                                 GtkTreeIter  *iter,
947                                 GtkTreePath  *path)
948 {
949   if (G_SLIST (iter->user_data)->data)
950     {
951       _gtk_tree_data_list_free ((GtkTreeDataList *) G_SLIST (iter->user_data)->data,
952                                 list_store->column_headers);
953       G_SLIST (iter->user_data)->data = NULL;
954     }
955
956   {
957     GSList *prev = NULL;
958
959     list_store->root = remove_link_saving_prev (G_SLIST (list_store->root),
960                                                 G_SLIST (iter->user_data),
961                                                 &prev);
962
963     list_store->length -= 1;
964
965     if (iter->user_data == list_store->tail)
966       list_store->tail = prev;
967   }
968 }
969
970 /**
971  * gtk_list_store_remove:
972  * @list_store: A #GtkListStore
973  * @iter: A valid #GtkTreeIter
974  *
975  * Removes the given row from the list store.  After being removed, 
976  * @iter is set to be the next valid row, or invalidated if it pointed 
977  * to the last row in @list_store.
978  *
979  **/
980 void
981 gtk_list_store_remove (GtkListStore *list_store,
982                        GtkTreeIter  *iter)
983 {
984   GtkTreePath *path;
985   GSList *next;
986
987   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
988   g_return_if_fail (VALID_ITER (iter, list_store));
989
990   next = G_SLIST (iter->user_data)->next;
991   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
992
993   validate_list_store (list_store);
994
995   gtk_list_store_remove_silently (list_store, iter, path);
996
997   validate_list_store (list_store);
998
999   gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
1000   gtk_tree_path_free (path);
1001
1002   if (next)
1003     {
1004       iter->stamp = list_store->stamp;
1005       iter->user_data = next;
1006     }
1007   else
1008     {
1009       iter->stamp = 0;
1010     }
1011 }
1012
1013 static void
1014 insert_after (GtkListStore *list_store,
1015               GSList       *sibling,
1016               GSList       *new_list)
1017 {
1018   g_return_if_fail (sibling != NULL);
1019   g_return_if_fail (new_list != NULL);
1020
1021   /* insert new node after list */
1022   new_list->next = sibling->next;
1023   sibling->next = new_list;
1024
1025   /* if list was the tail, the new node is the new tail */
1026   if (sibling == ((GSList *) list_store->tail))
1027     list_store->tail = new_list;
1028
1029   list_store->length += 1;
1030 }
1031
1032 /**
1033  * gtk_list_store_insert:
1034  * @list_store: A #GtkListStore
1035  * @iter: An unset #GtkTreeIter to set to the new row
1036  * @position: position to insert the new row
1037  *
1038  * Creates a new row at @position.  @iter will be changed to point to this new
1039  * row.  If @position is larger than the number of rows on the list, then the
1040  * new row will be appended to the list.  The row will be empty before this
1041  * function is called.  To fill in values, you need to call gtk_list_store_set()
1042  * or gtk_list_store_set_value().
1043  *
1044  **/
1045 void
1046 gtk_list_store_insert (GtkListStore *list_store,
1047                        GtkTreeIter  *iter,
1048                        gint          position)
1049 {
1050   GSList *list;
1051   GtkTreePath *path;
1052   GSList *new_list;
1053
1054   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1055   g_return_if_fail (iter != NULL);
1056   g_return_if_fail (position >= 0);
1057
1058   list_store->columns_dirty = TRUE;
1059
1060   if (position == 0 ||
1061       GTK_LIST_STORE_IS_SORTED (list_store))
1062     {
1063       gtk_list_store_prepend (list_store, iter);
1064       return;
1065     }
1066
1067   new_list = g_slist_alloc ();
1068
1069   list = g_slist_nth (G_SLIST (list_store->root), position - 1);
1070
1071   if (list == NULL)
1072     {
1073       g_warning ("%s: position %d is off the end of the list\n", G_STRLOC, position);
1074       return;
1075     }
1076
1077   insert_after (list_store, list, new_list);
1078
1079   iter->stamp = list_store->stamp;
1080   iter->user_data = new_list;
1081
1082   validate_list_store (list_store);
1083
1084   path = gtk_tree_path_new ();
1085   gtk_tree_path_append_index (path, position);
1086   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1087   gtk_tree_path_free (path);
1088 }
1089
1090 /**
1091  * gtk_list_store_insert_before:
1092  * @list_store: A #GtkListStore
1093  * @iter: An unset #GtkTreeIter to set to the new row
1094  * @sibling: A valid #GtkTreeIter, or %NULL
1095  *
1096  * Inserts a new row before @sibling. If @sibling is %NULL, then the row will be
1097  * appended to the end of the list. @iter will be changed to point to this new 
1098  * row. The row will be empty before this function is called. To fill in values,
1099  * you need to call gtk_list_store_set() or gtk_list_store_set_value().
1100  *
1101  **/
1102 void
1103 gtk_list_store_insert_before (GtkListStore *list_store,
1104                               GtkTreeIter  *iter,
1105                               GtkTreeIter  *sibling)
1106 {
1107   GtkTreePath *path;
1108   GSList *list, *prev, *new_list;
1109   gint i = 0;
1110
1111   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1112   g_return_if_fail (iter != NULL);
1113   if (sibling)
1114     g_return_if_fail (VALID_ITER (sibling, list_store));
1115
1116   list_store->columns_dirty = TRUE;
1117
1118   if (GTK_LIST_STORE_IS_SORTED (list_store))
1119     {
1120       gtk_list_store_prepend (list_store, iter);
1121       return;
1122     }
1123
1124   if (sibling == NULL)
1125     {
1126       gtk_list_store_append (list_store, iter);
1127       return;
1128     }
1129
1130   new_list = g_slist_alloc ();
1131
1132   prev = NULL;
1133   list = list_store->root;
1134   while (list && list != sibling->user_data)
1135     {
1136       prev = list;
1137       list = list->next;
1138       i++;
1139     }
1140
1141   if (list != sibling->user_data)
1142     {
1143       g_warning ("%s: sibling iterator invalid? not found in the list", G_STRLOC);
1144       return;
1145     }
1146
1147   /* if there are no nodes, we become the list tail, otherwise we
1148    * are inserting before any existing nodes so we can't change
1149    * the tail
1150    */
1151
1152   if (list_store->root == NULL)
1153     list_store->tail = new_list;
1154
1155   if (prev)
1156     {
1157       new_list->next = prev->next;
1158       prev->next = new_list;
1159     }
1160   else
1161     {
1162       new_list->next = list_store->root;
1163       list_store->root = new_list;
1164     }
1165
1166   iter->stamp = list_store->stamp;
1167   iter->user_data = new_list;
1168
1169   list_store->length += 1;
1170
1171   validate_list_store (list_store);
1172
1173   path = gtk_tree_path_new ();
1174   gtk_tree_path_append_index (path, i);
1175   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1176   gtk_tree_path_free (path);
1177 }
1178
1179 /**
1180  * gtk_list_store_insert_after:
1181  * @list_store: A #GtkListStore
1182  * @iter: An unset #GtkTreeIter to set to the new row
1183  * @sibling: A valid #GtkTreeIter, or %NULL
1184  *
1185  * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1186  * prepended to the beginning of the list. @iter will be changed to point to
1187  * this new row. The row will be empty after this function is called. To fill
1188  * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1189  *
1190  **/
1191 void
1192 gtk_list_store_insert_after (GtkListStore *list_store,
1193                              GtkTreeIter  *iter,
1194                              GtkTreeIter  *sibling)
1195 {
1196   GtkTreePath *path;
1197   GSList *list, *new_list;
1198   gint i = 0;
1199
1200   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1201   g_return_if_fail (iter != NULL);
1202   if (sibling)
1203     g_return_if_fail (VALID_ITER (sibling, list_store));
1204
1205   list_store->columns_dirty = TRUE;
1206
1207   if (sibling == NULL ||
1208       GTK_LIST_STORE_IS_SORTED (list_store))
1209     {
1210       gtk_list_store_prepend (list_store, iter);
1211       return;
1212     }
1213
1214   for (list = list_store->root; list && list != sibling->user_data; list = list->next)
1215     i++;
1216
1217   g_return_if_fail (list == sibling->user_data);
1218
1219   new_list = g_slist_alloc ();
1220
1221   insert_after (list_store, list, new_list);
1222
1223   iter->stamp = list_store->stamp;
1224   iter->user_data = new_list;
1225
1226   validate_list_store (list_store);
1227
1228   path = gtk_tree_path_new ();
1229   gtk_tree_path_append_index (path, i + 1);
1230   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1231   gtk_tree_path_free (path);
1232 }
1233
1234 /**
1235  * gtk_list_store_prepend:
1236  * @list_store: A #GtkListStore
1237  * @iter: An unset #GtkTreeIter to set to the prepend row
1238  *
1239  * Prepends a new row to @list_store. @iter will be changed to point to this new
1240  * row. The row will be empty after this function is called. To fill in
1241  * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1242  *
1243  **/
1244 void
1245 gtk_list_store_prepend (GtkListStore *list_store,
1246                         GtkTreeIter  *iter)
1247 {
1248   GtkTreePath *path;
1249
1250   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1251   g_return_if_fail (iter != NULL);
1252
1253   iter->stamp = list_store->stamp;
1254   iter->user_data = g_slist_alloc ();
1255
1256   list_store->columns_dirty = TRUE;
1257
1258   if (list_store->root == NULL)
1259     list_store->tail = iter->user_data;
1260
1261   G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1262   list_store->root = iter->user_data;
1263
1264   list_store->length += 1;
1265
1266   validate_list_store (list_store);
1267
1268   path = gtk_tree_path_new ();
1269   gtk_tree_path_append_index (path, 0);
1270   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1271   gtk_tree_path_free (path);
1272 }
1273
1274 /**
1275  * gtk_list_store_append:
1276  * @list_store: A #GtkListStore
1277  * @iter: An unset #GtkTreeIter to set to the appended row
1278  *
1279  * Appends a new row to @list_store.  @iter will be changed to point to this new
1280  * row.  The row will be empty after this function is called.  To fill in
1281  * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1282  *
1283  **/
1284 void
1285 gtk_list_store_append (GtkListStore *list_store,
1286                        GtkTreeIter  *iter)
1287 {
1288   GtkTreePath *path;
1289
1290   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1291   g_return_if_fail (iter != NULL);
1292
1293   list_store->columns_dirty = TRUE;
1294
1295   if (GTK_LIST_STORE_IS_SORTED (list_store))
1296     {
1297       gtk_list_store_prepend (list_store, iter);
1298       return;
1299     }
1300
1301   iter->stamp = list_store->stamp;
1302   iter->user_data = g_slist_alloc ();
1303
1304   if (list_store->tail)
1305     ((GSList *)list_store->tail)->next = iter->user_data;
1306   else
1307     list_store->root = iter->user_data;
1308
1309   list_store->tail = iter->user_data;
1310
1311   list_store->length += 1;
1312
1313   validate_list_store (list_store);
1314
1315   path = gtk_tree_path_new ();
1316   gtk_tree_path_append_index (path, list_store->length - 1);
1317   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1318   gtk_tree_path_free (path);
1319 }
1320
1321 /**
1322  * gtk_list_store_clear:
1323  * @list_store: a #GtkListStore.
1324  *
1325  * Removes all rows from the list store.  
1326  *
1327  **/
1328 void
1329 gtk_list_store_clear (GtkListStore *list_store)
1330 {
1331   GtkTreeIter iter;
1332   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1333
1334   while (list_store->root)
1335     {
1336       iter.stamp = list_store->stamp;
1337       iter.user_data = list_store->root;
1338       gtk_list_store_remove (list_store, &iter);
1339     }
1340 }
1341
1342
1343 static gboolean
1344 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1345                                  GtkTreePath       *path)
1346 {
1347   GtkTreeIter iter;
1348   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1349
1350   if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
1351                                &iter,
1352                                path))
1353     {
1354       gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1355       return TRUE;
1356     }
1357   return FALSE;
1358 }
1359
1360 static gboolean
1361 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1362                               GtkTreePath       *path,
1363                               GtkSelectionData  *selection_data)
1364 {
1365   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_source), FALSE);
1366
1367   /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1368    * target, because the default handler does it for us, but
1369    * we do anyway for the convenience of someone maybe overriding the
1370    * default handler.
1371    */
1372
1373   if (gtk_tree_set_row_drag_data (selection_data,
1374                                   GTK_TREE_MODEL (drag_source),
1375                                   path))
1376     {
1377       return TRUE;
1378     }
1379   else
1380     {
1381       /* FIXME handle text targets at least. */
1382     }
1383
1384   return FALSE;
1385 }
1386
1387 static gboolean
1388 gtk_list_store_drag_data_received (GtkTreeDragDest   *drag_dest,
1389                                    GtkTreePath       *dest,
1390                                    GtkSelectionData  *selection_data)
1391 {
1392   GtkTreeModel *tree_model;
1393   GtkListStore *list_store;
1394   GtkTreeModel *src_model = NULL;
1395   GtkTreePath *src_path = NULL;
1396   gboolean retval = FALSE;
1397
1398   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1399
1400   tree_model = GTK_TREE_MODEL (drag_dest);
1401   list_store = GTK_LIST_STORE (drag_dest);
1402
1403   if (gtk_tree_get_row_drag_data (selection_data,
1404                                   &src_model,
1405                                   &src_path) &&
1406       src_model == tree_model)
1407     {
1408       /* Copy the given row to a new position */
1409       GtkTreeIter src_iter;
1410       GtkTreeIter dest_iter;
1411       GtkTreePath *prev;
1412
1413       if (!gtk_tree_model_get_iter (src_model,
1414                                     &src_iter,
1415                                     src_path))
1416         {
1417           goto out;
1418         }
1419
1420       /* Get the path to insert _after_ (dest is the path to insert _before_) */
1421       prev = gtk_tree_path_copy (dest);
1422
1423       if (!gtk_tree_path_prev (prev))
1424         {
1425           /* dest was the first spot in the list; which means we are supposed
1426            * to prepend.
1427            */
1428           gtk_list_store_prepend (GTK_LIST_STORE (tree_model),
1429                                   &dest_iter);
1430
1431           retval = TRUE;
1432         }
1433       else
1434         {
1435           if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
1436                                        &dest_iter,
1437                                        prev))
1438             {
1439               GtkTreeIter tmp_iter = dest_iter;
1440               gtk_list_store_insert_after (GTK_LIST_STORE (tree_model),
1441                                            &dest_iter,
1442                                            &tmp_iter);
1443               retval = TRUE;
1444             }
1445         }
1446
1447       gtk_tree_path_free (prev);
1448
1449       /* If we succeeded in creating dest_iter, copy data from src
1450        */
1451       if (retval)
1452         {
1453           GtkTreeDataList *dl = G_SLIST (src_iter.user_data)->data;
1454           GtkTreeDataList *copy_head = NULL;
1455           GtkTreeDataList *copy_prev = NULL;
1456           GtkTreeDataList *copy_iter = NULL;
1457           GtkTreePath *path;
1458           gint col;
1459
1460           col = 0;
1461           while (dl)
1462             {
1463               copy_iter = _gtk_tree_data_list_node_copy (dl,
1464                                                          list_store->column_headers[col]);
1465
1466               if (copy_head == NULL)
1467                 copy_head = copy_iter;
1468
1469               if (copy_prev)
1470                 copy_prev->next = copy_iter;
1471
1472               copy_prev = copy_iter;
1473
1474               dl = dl->next;
1475               ++col;
1476             }
1477
1478           dest_iter.stamp = GTK_LIST_STORE (tree_model)->stamp;
1479           G_SLIST (dest_iter.user_data)->data = copy_head;
1480
1481           path = gtk_list_store_get_path (GTK_TREE_MODEL (tree_model), &dest_iter);
1482           gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_model), path, &dest_iter);
1483           gtk_tree_path_free (path);
1484         }
1485     }
1486   else
1487     {
1488       /* FIXME maybe add some data targets eventually, or handle text
1489        * targets in the simple case.
1490        */
1491     }
1492
1493  out:
1494
1495   if (src_path)
1496     gtk_tree_path_free (src_path);
1497
1498   return retval;
1499 }
1500
1501 static gboolean
1502 gtk_list_store_row_drop_possible (GtkTreeDragDest  *drag_dest,
1503                                   GtkTreePath      *dest_path,
1504                                   GtkSelectionData *selection_data)
1505 {
1506   gint *indices;
1507   GtkTreeModel *src_model = NULL;
1508   GtkTreePath *src_path = NULL;
1509   gboolean retval = FALSE;
1510
1511   g_return_val_if_fail (GTK_IS_LIST_STORE (drag_dest), FALSE);
1512
1513   if (!gtk_tree_get_row_drag_data (selection_data,
1514                                    &src_model,
1515                                    &src_path))
1516     goto out;
1517     
1518   if (src_model != GTK_TREE_MODEL (drag_dest))
1519     goto out;
1520
1521   if (gtk_tree_path_get_depth (dest_path) != 1)
1522     goto out;
1523
1524   /* can drop before any existing node, or before one past any existing. */
1525
1526   indices = gtk_tree_path_get_indices (dest_path);
1527
1528   if (indices[0] <= GTK_LIST_STORE (drag_dest)->length)
1529     retval = TRUE;
1530
1531  out:
1532   gtk_tree_path_free (src_path);
1533   
1534   return retval;
1535 }
1536
1537
1538 /* Sorting */
1539 typedef struct _SortTuple
1540 {
1541   gint offset;
1542   GSList *el;
1543 } SortTuple;
1544
1545 static gint
1546 gtk_list_store_compare_func (gconstpointer a,
1547                              gconstpointer b,
1548                              gpointer      user_data)
1549 {
1550   GtkListStore *list_store = user_data;
1551   GSList *el_a; /* Los Angeles? */
1552   GSList *el_b;
1553   GtkTreeIter iter_a;
1554   GtkTreeIter iter_b;
1555   gint retval;
1556   GtkTreeIterCompareFunc func;
1557   gpointer data;
1558
1559
1560   if (list_store->sort_column_id != -1)
1561     {
1562       GtkTreeDataSortHeader *header;
1563
1564       header = _gtk_tree_data_list_get_header (list_store->sort_list,
1565                                                list_store->sort_column_id);
1566       g_return_val_if_fail (header != NULL, 0);
1567       g_return_val_if_fail (header->func != NULL, 0);
1568
1569       func = header->func;
1570       data = header->data;
1571     }
1572   else
1573     {
1574       g_return_val_if_fail (list_store->default_sort_func != NULL, 0);
1575       func = list_store->default_sort_func;
1576       data = list_store->default_sort_data;
1577     }
1578
1579   el_a = ((SortTuple *) a)->el;
1580   el_b = ((SortTuple *) b)->el;
1581
1582   iter_a.stamp = list_store->stamp;
1583   iter_a.user_data = el_a;
1584   iter_b.stamp = list_store->stamp;
1585   iter_b.user_data = el_b;
1586
1587   retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1588
1589   if (list_store->order == GTK_SORT_DESCENDING)
1590     {
1591       if (retval > 0)
1592         retval = -1;
1593       else if (retval < 0)
1594         retval = 1;
1595     }
1596   return retval;
1597 }
1598
1599 static void
1600 gtk_list_store_sort (GtkListStore *list_store)
1601 {
1602   GArray *sort_array;
1603   gint i;
1604   gint *new_order;
1605   GSList *list;
1606   GtkTreePath *path;
1607
1608   if (list_store->length <= 1)
1609     return;
1610
1611   g_assert (GTK_LIST_STORE_IS_SORTED (list_store));
1612
1613   list = G_SLIST (list_store->root);
1614
1615   sort_array = g_array_sized_new (FALSE, FALSE,
1616                                   sizeof (SortTuple),
1617                                   list_store->length);
1618
1619   for (i = 0; i < list_store->length; i++)
1620     {
1621       SortTuple tuple;
1622
1623       /* If this fails, we are in an inconsistent state.  Bad */
1624       g_return_if_fail (list != NULL);
1625
1626       tuple.offset = i;
1627       tuple.el = list;
1628       g_array_append_val (sort_array, tuple);
1629
1630       list = list->next;
1631     }
1632
1633   g_array_sort_with_data (sort_array, gtk_list_store_compare_func, list_store);
1634
1635   for (i = 0; i < list_store->length - 1; i++)
1636       g_array_index (sort_array, SortTuple, i).el->next =
1637         g_array_index (sort_array, SortTuple, i + 1).el;
1638   g_array_index (sort_array, SortTuple, list_store->length - 1).el->next = NULL;
1639   list_store->root = g_array_index (sort_array, SortTuple, 0).el;
1640
1641   /* Let the world know about our new order */
1642   new_order = g_new (gint, list_store->length);
1643   for (i = 0; i < list_store->length; i++)
1644     new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
1645
1646   path = gtk_tree_path_new ();
1647   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1648                                  path, NULL, new_order);
1649   gtk_tree_path_free (path);
1650   g_free (new_order);
1651   g_array_free (sort_array, TRUE);
1652 }
1653
1654 static void
1655 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1656                                   GtkTreeIter  *iter,
1657                                   gint          column)
1658
1659 {
1660   GSList *prev = NULL;
1661   GSList *next = NULL;
1662   GSList *list = G_SLIST (list_store->root);
1663   GtkTreePath *tmp_path;
1664   GtkTreeIter tmp_iter;
1665   gint cmp_a = 0;
1666   gint cmp_b = 0;
1667   gint i;
1668   gint old_location;
1669   gint new_location;
1670   gint *new_order;
1671   GtkTreeIterCompareFunc func;
1672   gpointer data;
1673
1674   if (list_store->length < 2)
1675     return;
1676
1677   tmp_iter.stamp = list_store->stamp;
1678
1679   if (list_store->sort_column_id != -1)
1680     {
1681       GtkTreeDataSortHeader *header;
1682       header = _gtk_tree_data_list_get_header (list_store->sort_list,
1683                                                list_store->sort_column_id);
1684       g_return_if_fail (header != NULL);
1685       g_return_if_fail (header->func != NULL);
1686       func = header->func;
1687       data = header->data;
1688     }
1689   else
1690     {
1691       g_return_if_fail (list_store->default_sort_func != NULL);
1692       func = list_store->default_sort_func;
1693       data = list_store->default_sort_data;
1694     }
1695
1696   /* If it's the built in function, we don't sort. */
1697   if (func == gtk_tree_data_list_compare_func &&
1698       list_store->sort_column_id != column)
1699     return;
1700
1701   old_location = 0;
1702   /* First we find the iter, its prev, and its next */
1703   while (list)
1704     {
1705       if (list == G_SLIST (iter->user_data))
1706         break;
1707       prev = list;
1708       list = list->next;
1709       old_location++;
1710     }
1711   g_assert (list != NULL);
1712
1713   next = list->next;
1714
1715   /* Check the common case, where we don't need to sort it moved. */
1716   if (prev != NULL)
1717     {
1718       tmp_iter.user_data = prev;
1719       cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1720     }
1721
1722   if (next != NULL)
1723     {
1724       tmp_iter.user_data = next;
1725       cmp_b = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1726     }
1727
1728
1729   if (list_store->order == GTK_SORT_DESCENDING)
1730     {
1731       if (cmp_a < 0)
1732         cmp_a = 1;
1733       else if (cmp_a > 0)
1734         cmp_a = -1;
1735
1736       if (cmp_b < 0)
1737         cmp_b = 1;
1738       else if (cmp_b > 0)
1739         cmp_b = -1;
1740     }
1741
1742   if (prev == NULL && cmp_b <= 0)
1743     return;
1744   else if (next == NULL && cmp_a <= 0)
1745     return;
1746   else if (prev != NULL && next != NULL &&
1747            cmp_a <= 0 && cmp_b <= 0)
1748     return;
1749
1750   /* We actually need to sort it */
1751   /* First, remove the old link. */
1752
1753   if (prev == NULL)
1754     list_store->root = next;
1755   else
1756     prev->next = next;
1757   if (next == NULL)
1758     list_store->tail = prev;
1759   list->next = NULL;
1760   
1761   /* FIXME: as an optimization, we can potentially start at next */
1762   prev = NULL;
1763   list = G_SLIST (list_store->root);
1764   new_location = 0;
1765   tmp_iter.user_data = list;
1766   if (list_store->order == GTK_SORT_DESCENDING)
1767     cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1768   else
1769     cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1770
1771   while ((list->next) && (cmp_a > 0))
1772     {
1773       prev = list;
1774       list = list->next;
1775       new_location++;
1776       tmp_iter.user_data = list;
1777       if (list_store->order == GTK_SORT_DESCENDING)
1778         cmp_a = (* func) (GTK_TREE_MODEL (list_store), &tmp_iter, iter, data);
1779       else
1780         cmp_a = (* func) (GTK_TREE_MODEL (list_store), iter, &tmp_iter, data);
1781     }
1782
1783   if ((!list->next) && (cmp_a > 0))
1784     {
1785       new_location++;
1786       list->next = G_SLIST (iter->user_data);
1787       list_store->tail = list->next;
1788     }
1789   else if (prev)
1790     {
1791       prev->next = G_SLIST (iter->user_data);
1792       G_SLIST (iter->user_data)->next = list;
1793     }
1794   else
1795     {
1796       G_SLIST (iter->user_data)->next = G_SLIST (list_store->root);
1797       list_store->root = G_SLIST (iter->user_data);
1798     }
1799
1800   /* Emit the reordered signal. */
1801   new_order = g_new (int, list_store->length);
1802   if (old_location < new_location)
1803     for (i = 0; i < list_store->length; i++)
1804       {
1805         if (i < old_location ||
1806             i > new_location)
1807           new_order[i] = i;
1808         else if (i >= old_location &&
1809                  i < new_location)
1810           new_order[i] = i + 1;
1811         else if (i == new_location)
1812           new_order[i] = old_location;
1813       }
1814   else
1815     for (i = 0; i < list_store->length; i++)
1816       {
1817         if (i < new_location ||
1818             i > old_location)
1819           new_order[i] = i;
1820         else if (i > new_location &&
1821                  i <= old_location)
1822           new_order[i] = i - 1;
1823         else if (i == new_location)
1824           new_order[i] = old_location;
1825       }
1826
1827   tmp_path = gtk_tree_path_new ();
1828   tmp_iter.user_data = NULL;
1829
1830   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1831                                  tmp_path, NULL,
1832                                  new_order);
1833
1834   gtk_tree_path_free (tmp_path);
1835   g_free (new_order);
1836 }
1837
1838 static gboolean
1839 gtk_list_store_get_sort_column_id (GtkTreeSortable  *sortable,
1840                                    gint             *sort_column_id,
1841                                    GtkSortType      *order)
1842 {
1843   GtkListStore *list_store = (GtkListStore *) sortable;
1844
1845   g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1846
1847   if (list_store->sort_column_id == -1)
1848     return FALSE;
1849
1850   if (sort_column_id)
1851     * sort_column_id = list_store->sort_column_id;
1852   if (order)
1853     * order = list_store->order;
1854   return TRUE;
1855 }
1856
1857 static void
1858 gtk_list_store_set_sort_column_id (GtkTreeSortable  *sortable,
1859                                    gint              sort_column_id,
1860                                    GtkSortType       order)
1861 {
1862   GtkListStore *list_store = (GtkListStore *) sortable;
1863
1864   g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1865
1866   if ((list_store->sort_column_id == sort_column_id) &&
1867       (list_store->order == order))
1868     return;
1869
1870   if (sort_column_id != -1)
1871     {
1872       GtkTreeDataSortHeader *header = NULL;
1873
1874       header = _gtk_tree_data_list_get_header (list_store->sort_list, sort_column_id);
1875
1876       /* We want to make sure that we have a function */
1877       g_return_if_fail (header != NULL);
1878       g_return_if_fail (header->func != NULL);
1879     }
1880   else
1881     {
1882       g_return_if_fail (list_store->default_sort_func != NULL);
1883     }
1884
1885
1886   list_store->sort_column_id = sort_column_id;
1887   list_store->order = order;
1888
1889   gtk_list_store_sort (list_store);
1890
1891   gtk_tree_sortable_sort_column_changed (sortable);
1892 }
1893
1894 static void
1895 gtk_list_store_set_sort_func (GtkTreeSortable        *sortable,
1896                               gint                    sort_column_id,
1897                               GtkTreeIterCompareFunc  func,
1898                               gpointer                data,
1899                               GtkDestroyNotify        destroy)
1900 {
1901   GtkListStore *list_store = (GtkListStore *) sortable;
1902   GtkTreeDataSortHeader *header = NULL;
1903   GList *list;
1904
1905   g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1906   g_return_if_fail (func != NULL);
1907
1908   for (list = list_store->sort_list; list; list = list->next)
1909     {
1910       header = (GtkTreeDataSortHeader*) list->data;
1911       if (header->sort_column_id == sort_column_id)
1912         break;
1913     }
1914
1915   if (header == NULL)
1916     {
1917       header = g_new0 (GtkTreeDataSortHeader, 1);
1918       header->sort_column_id = sort_column_id;
1919       list_store->sort_list = g_list_append (list_store->sort_list, header);
1920     }
1921
1922   if (header->destroy)
1923     {
1924       GtkDestroyNotify d = header->destroy;
1925
1926       header->destroy = NULL;
1927       d (header->data);
1928     }
1929
1930   header->func = func;
1931   header->data = data;
1932   header->destroy = destroy;
1933 }
1934
1935
1936 static void
1937 gtk_list_store_set_default_sort_func (GtkTreeSortable        *sortable,
1938                                       GtkTreeIterCompareFunc  func,
1939                                       gpointer                data,
1940                                       GtkDestroyNotify        destroy)
1941 {
1942   GtkListStore *list_store = (GtkListStore *) sortable;
1943
1944   g_return_if_fail (GTK_IS_LIST_STORE (sortable));
1945
1946   if (list_store->default_sort_destroy)
1947     {
1948       GtkDestroyNotify d = list_store->default_sort_destroy;
1949
1950       list_store->default_sort_destroy = NULL;
1951       d (list_store->default_sort_data);
1952     }
1953
1954   list_store->default_sort_func = func;
1955   list_store->default_sort_data = data;
1956   list_store->default_sort_destroy = destroy;
1957 }
1958
1959 static gboolean
1960 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
1961 {
1962   GtkListStore *list_store = (GtkListStore *) sortable;
1963
1964   g_return_val_if_fail (GTK_IS_LIST_STORE (sortable), FALSE);
1965
1966   return (list_store->default_sort_func != NULL);
1967 }