]> Pileus Git - ~andy/gtk/blob - gtk/gtkliststore.c
GtkListStorePrivate: Improve struct packing
[~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 "config.h"
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <gobject/gvaluecollector.h>
25 #include "gtktreemodel.h"
26 #include "gtkliststore.h"
27 #include "gtktreedatalist.h"
28 #include "gtktreednd.h"
29 #include "gtkintl.h"
30 #include "gtkbuildable.h"
31 #include "gtkbuilderprivate.h"
32
33
34 struct _GtkListStorePrivate
35 {
36   GtkTreeIterCompareFunc default_sort_func;
37
38   GDestroyNotify default_sort_destroy;
39   GList *sort_list;
40   GType *column_headers;
41
42   gint stamp;
43   gint n_columns;
44   gint sort_column_id;
45   gint length;
46
47   GtkSortType order;
48
49   guint columns_dirty : 1;
50
51   gpointer default_sort_data;
52   gpointer seq;         /* head of the list */
53 };
54
55 #define GTK_LIST_STORE_IS_SORTED(list) (((GtkListStore*)(list))->priv->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
56 #define VALID_ITER(iter, list_store) ((iter)!= NULL && (iter)->user_data != NULL && list_store->priv->stamp == (iter)->stamp && !g_sequence_iter_is_end ((iter)->user_data) && g_sequence_iter_get_sequence ((iter)->user_data) == list_store->priv->seq)
57
58 static void         gtk_list_store_tree_model_init (GtkTreeModelIface *iface);
59 static void         gtk_list_store_drag_source_init(GtkTreeDragSourceIface *iface);
60 static void         gtk_list_store_drag_dest_init  (GtkTreeDragDestIface   *iface);
61 static void         gtk_list_store_sortable_init   (GtkTreeSortableIface   *iface);
62 static void         gtk_list_store_buildable_init  (GtkBuildableIface      *iface);
63 static void         gtk_list_store_finalize        (GObject           *object);
64 static GtkTreeModelFlags gtk_list_store_get_flags  (GtkTreeModel      *tree_model);
65 static gint         gtk_list_store_get_n_columns   (GtkTreeModel      *tree_model);
66 static GType        gtk_list_store_get_column_type (GtkTreeModel      *tree_model,
67                                                     gint               index);
68 static gboolean     gtk_list_store_get_iter        (GtkTreeModel      *tree_model,
69                                                     GtkTreeIter       *iter,
70                                                     GtkTreePath       *path);
71 static GtkTreePath *gtk_list_store_get_path        (GtkTreeModel      *tree_model,
72                                                     GtkTreeIter       *iter);
73 static void         gtk_list_store_get_value       (GtkTreeModel      *tree_model,
74                                                     GtkTreeIter       *iter,
75                                                     gint               column,
76                                                     GValue            *value);
77 static gboolean     gtk_list_store_iter_next       (GtkTreeModel      *tree_model,
78                                                     GtkTreeIter       *iter);
79 static gboolean     gtk_list_store_iter_previous   (GtkTreeModel      *tree_model,
80                                                     GtkTreeIter       *iter);
81 static gboolean     gtk_list_store_iter_children   (GtkTreeModel      *tree_model,
82                                                     GtkTreeIter       *iter,
83                                                     GtkTreeIter       *parent);
84 static gboolean     gtk_list_store_iter_has_child  (GtkTreeModel      *tree_model,
85                                                     GtkTreeIter       *iter);
86 static gint         gtk_list_store_iter_n_children (GtkTreeModel      *tree_model,
87                                                     GtkTreeIter       *iter);
88 static gboolean     gtk_list_store_iter_nth_child  (GtkTreeModel      *tree_model,
89                                                     GtkTreeIter       *iter,
90                                                     GtkTreeIter       *parent,
91                                                     gint               n);
92 static gboolean     gtk_list_store_iter_parent     (GtkTreeModel      *tree_model,
93                                                     GtkTreeIter       *iter,
94                                                     GtkTreeIter       *child);
95
96
97 static void gtk_list_store_set_n_columns   (GtkListStore *list_store,
98                                             gint          n_columns);
99 static void gtk_list_store_set_column_type (GtkListStore *list_store,
100                                             gint          column,
101                                             GType         type);
102
103 static void gtk_list_store_increment_stamp (GtkListStore *list_store);
104
105
106 /* Drag and Drop */
107 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
108                                                    GtkTreePath       *path);
109 static gboolean gtk_list_store_drag_data_delete   (GtkTreeDragSource *drag_source,
110                                                    GtkTreePath       *path);
111 static gboolean gtk_list_store_drag_data_get      (GtkTreeDragSource *drag_source,
112                                                    GtkTreePath       *path,
113                                                    GtkSelectionData  *selection_data);
114 static gboolean gtk_list_store_drag_data_received (GtkTreeDragDest   *drag_dest,
115                                                    GtkTreePath       *dest,
116                                                    GtkSelectionData  *selection_data);
117 static gboolean gtk_list_store_row_drop_possible  (GtkTreeDragDest   *drag_dest,
118                                                    GtkTreePath       *dest_path,
119                                                    GtkSelectionData  *selection_data);
120
121
122 /* sortable */
123 static void     gtk_list_store_sort                  (GtkListStore           *list_store);
124 static void     gtk_list_store_sort_iter_changed     (GtkListStore           *list_store,
125                                                       GtkTreeIter            *iter,
126                                                       gint                    column);
127 static gboolean gtk_list_store_get_sort_column_id    (GtkTreeSortable        *sortable,
128                                                       gint                   *sort_column_id,
129                                                       GtkSortType            *order);
130 static void     gtk_list_store_set_sort_column_id    (GtkTreeSortable        *sortable,
131                                                       gint                    sort_column_id,
132                                                       GtkSortType             order);
133 static void     gtk_list_store_set_sort_func         (GtkTreeSortable        *sortable,
134                                                       gint                    sort_column_id,
135                                                       GtkTreeIterCompareFunc  func,
136                                                       gpointer                data,
137                                                       GDestroyNotify          destroy);
138 static void     gtk_list_store_set_default_sort_func (GtkTreeSortable        *sortable,
139                                                       GtkTreeIterCompareFunc  func,
140                                                       gpointer                data,
141                                                       GDestroyNotify          destroy);
142 static gboolean gtk_list_store_has_default_sort_func (GtkTreeSortable        *sortable);
143
144
145 /* buildable */
146 static gboolean gtk_list_store_buildable_custom_tag_start (GtkBuildable  *buildable,
147                                                            GtkBuilder    *builder,
148                                                            GObject       *child,
149                                                            const gchar   *tagname,
150                                                            GMarkupParser *parser,
151                                                            gpointer      *data);
152 static void     gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
153                                                          GtkBuilder   *builder,
154                                                          GObject      *child,
155                                                          const gchar  *tagname,
156                                                          gpointer     *data);
157
158 G_DEFINE_TYPE_WITH_CODE (GtkListStore, gtk_list_store, G_TYPE_OBJECT,
159                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
160                                                 gtk_list_store_tree_model_init)
161                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
162                                                 gtk_list_store_drag_source_init)
163                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
164                                                 gtk_list_store_drag_dest_init)
165                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
166                                                 gtk_list_store_sortable_init)
167                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
168                                                 gtk_list_store_buildable_init))
169
170
171 static void
172 gtk_list_store_class_init (GtkListStoreClass *class)
173 {
174   GObjectClass *object_class;
175
176   object_class = (GObjectClass*) class;
177
178   object_class->finalize = gtk_list_store_finalize;
179
180   g_type_class_add_private (class, sizeof (GtkListStorePrivate));
181 }
182
183 static void
184 gtk_list_store_tree_model_init (GtkTreeModelIface *iface)
185 {
186   iface->get_flags = gtk_list_store_get_flags;
187   iface->get_n_columns = gtk_list_store_get_n_columns;
188   iface->get_column_type = gtk_list_store_get_column_type;
189   iface->get_iter = gtk_list_store_get_iter;
190   iface->get_path = gtk_list_store_get_path;
191   iface->get_value = gtk_list_store_get_value;
192   iface->iter_next = gtk_list_store_iter_next;
193   iface->iter_previous = gtk_list_store_iter_previous;
194   iface->iter_children = gtk_list_store_iter_children;
195   iface->iter_has_child = gtk_list_store_iter_has_child;
196   iface->iter_n_children = gtk_list_store_iter_n_children;
197   iface->iter_nth_child = gtk_list_store_iter_nth_child;
198   iface->iter_parent = gtk_list_store_iter_parent;
199 }
200
201 static void
202 gtk_list_store_drag_source_init (GtkTreeDragSourceIface *iface)
203 {
204   iface->row_draggable = real_gtk_list_store_row_draggable;
205   iface->drag_data_delete = gtk_list_store_drag_data_delete;
206   iface->drag_data_get = gtk_list_store_drag_data_get;
207 }
208
209 static void
210 gtk_list_store_drag_dest_init (GtkTreeDragDestIface *iface)
211 {
212   iface->drag_data_received = gtk_list_store_drag_data_received;
213   iface->row_drop_possible = gtk_list_store_row_drop_possible;
214 }
215
216 static void
217 gtk_list_store_sortable_init (GtkTreeSortableIface *iface)
218 {
219   iface->get_sort_column_id = gtk_list_store_get_sort_column_id;
220   iface->set_sort_column_id = gtk_list_store_set_sort_column_id;
221   iface->set_sort_func = gtk_list_store_set_sort_func;
222   iface->set_default_sort_func = gtk_list_store_set_default_sort_func;
223   iface->has_default_sort_func = gtk_list_store_has_default_sort_func;
224 }
225
226 void
227 gtk_list_store_buildable_init (GtkBuildableIface *iface)
228 {
229   iface->custom_tag_start = gtk_list_store_buildable_custom_tag_start;
230   iface->custom_tag_end = gtk_list_store_buildable_custom_tag_end;
231 }
232
233 static void
234 gtk_list_store_init (GtkListStore *list_store)
235 {
236   GtkListStorePrivate *priv;
237
238   list_store->priv = G_TYPE_INSTANCE_GET_PRIVATE (list_store,
239                                                   GTK_TYPE_LIST_STORE,
240                                                   GtkListStorePrivate);
241   priv = list_store->priv;
242
243   priv->seq = g_sequence_new (NULL);
244   priv->sort_list = NULL;
245   priv->stamp = g_random_int ();
246   priv->sort_column_id = GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID;
247   priv->columns_dirty = FALSE;
248   priv->length = 0;
249 }
250
251 /**
252  * gtk_list_store_new:
253  * @n_columns: number of columns in the list store
254  * @Varargs: all #GType types for the columns, from first to last
255  *
256  * Creates a new list store as with @n_columns columns each of the types passed
257  * in.  Note that only types derived from standard GObject fundamental types 
258  * are supported. 
259  *
260  * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
261  * GDK_TYPE_PIXBUF);</literal> will create a new #GtkListStore with three columns, of type
262  * int, string and #GdkPixbuf respectively.
263  *
264  * Return value: a new #GtkListStore
265  **/
266 GtkListStore *
267 gtk_list_store_new (gint n_columns,
268                     ...)
269 {
270   GtkListStore *retval;
271   va_list args;
272   gint i;
273
274   g_return_val_if_fail (n_columns > 0, NULL);
275
276   retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
277   gtk_list_store_set_n_columns (retval, n_columns);
278
279   va_start (args, n_columns);
280
281   for (i = 0; i < n_columns; i++)
282     {
283       GType type = va_arg (args, GType);
284       if (! _gtk_tree_data_list_check_type (type))
285         {
286           g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
287           g_object_unref (retval);
288           return NULL;
289         }
290
291       gtk_list_store_set_column_type (retval, i, type);
292     }
293
294   va_end (args);
295
296   return retval;
297 }
298
299
300 /**
301  * gtk_list_store_newv:
302  * @n_columns: number of columns in the list store
303  * @types: (array length=n_columns): an array of #GType types for the columns, from first to last
304  *
305  * Non-vararg creation function.  Used primarily by language bindings.
306  *
307  * Return value: (transfer none): a new #GtkListStore
308  * Rename to: gtk_list_store_new
309  **/
310 GtkListStore *
311 gtk_list_store_newv (gint   n_columns,
312                      GType *types)
313 {
314   GtkListStore *retval;
315   gint i;
316
317   g_return_val_if_fail (n_columns > 0, NULL);
318
319   retval = g_object_new (GTK_TYPE_LIST_STORE, NULL);
320   gtk_list_store_set_n_columns (retval, n_columns);
321
322   for (i = 0; i < n_columns; i++)
323     {
324       if (! _gtk_tree_data_list_check_type (types[i]))
325         {
326           g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
327           g_object_unref (retval);
328           return NULL;
329         }
330
331       gtk_list_store_set_column_type (retval, i, types[i]);
332     }
333
334   return retval;
335 }
336
337 /**
338  * gtk_list_store_set_column_types:
339  * @list_store: A #GtkListStore
340  * @n_columns: Number of columns for the list store
341  * @types: (array length=n_columns): An array length n of #GTypes
342  *
343  * This function is meant primarily for #GObjects that inherit from #GtkListStore,
344  * and should only be used when constructing a new #GtkListStore.  It will not
345  * function after a row has been added, or a method on the #GtkTreeModel
346  * interface is called.
347  **/
348 void
349 gtk_list_store_set_column_types (GtkListStore *list_store,
350                                  gint          n_columns,
351                                  GType        *types)
352 {
353   GtkListStorePrivate *priv;
354   gint i;
355
356   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
357
358   priv = list_store->priv;
359
360   g_return_if_fail (priv->columns_dirty == 0);
361
362   gtk_list_store_set_n_columns (list_store, n_columns);
363   for (i = 0; i < n_columns; i++)
364     {
365       if (! _gtk_tree_data_list_check_type (types[i]))
366         {
367           g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
368           continue;
369         }
370       gtk_list_store_set_column_type (list_store, i, types[i]);
371     }
372 }
373
374 static void
375 gtk_list_store_set_n_columns (GtkListStore *list_store,
376                               gint          n_columns)
377 {
378   GtkListStorePrivate *priv = list_store->priv;
379   int i;
380
381   if (priv->n_columns == n_columns)
382     return;
383
384   priv->column_headers = g_renew (GType, priv->column_headers, n_columns);
385   for (i = priv->n_columns; i < n_columns; i++)
386     priv->column_headers[i] = G_TYPE_INVALID;
387   priv->n_columns = n_columns;
388
389   if (priv->sort_list)
390     _gtk_tree_data_list_header_free (priv->sort_list);
391   priv->sort_list = _gtk_tree_data_list_header_new (n_columns, priv->column_headers);
392 }
393
394 static void
395 gtk_list_store_set_column_type (GtkListStore *list_store,
396                                 gint          column,
397                                 GType         type)
398 {
399   GtkListStorePrivate *priv = list_store->priv;
400
401   if (!_gtk_tree_data_list_check_type (type))
402     {
403       g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
404       return;
405     }
406
407   priv->column_headers[column] = type;
408 }
409
410 static void
411 gtk_list_store_finalize (GObject *object)
412 {
413   GtkListStore *list_store = GTK_LIST_STORE (object);
414   GtkListStorePrivate *priv = list_store->priv;
415
416   g_sequence_foreach (priv->seq,
417                       (GFunc) _gtk_tree_data_list_free, priv->column_headers);
418
419   g_sequence_free (priv->seq);
420
421   _gtk_tree_data_list_header_free (priv->sort_list);
422   g_free (priv->column_headers);
423
424   if (priv->default_sort_destroy)
425     {
426       GDestroyNotify d = priv->default_sort_destroy;
427
428       priv->default_sort_destroy = NULL;
429       d (priv->default_sort_data);
430       priv->default_sort_data = NULL;
431     }
432
433   G_OBJECT_CLASS (gtk_list_store_parent_class)->finalize (object);
434 }
435
436 /* Fulfill the GtkTreeModel requirements */
437 static GtkTreeModelFlags
438 gtk_list_store_get_flags (GtkTreeModel *tree_model)
439 {
440   return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
441 }
442
443 static gint
444 gtk_list_store_get_n_columns (GtkTreeModel *tree_model)
445 {
446   GtkListStore *list_store = GTK_LIST_STORE (tree_model);
447   GtkListStorePrivate *priv = list_store->priv;
448
449   priv->columns_dirty = TRUE;
450
451   return priv->n_columns;
452 }
453
454 static GType
455 gtk_list_store_get_column_type (GtkTreeModel *tree_model,
456                                 gint          index)
457 {
458   GtkListStore *list_store = GTK_LIST_STORE (tree_model);
459   GtkListStorePrivate *priv = list_store->priv;
460
461   g_return_val_if_fail (index < priv->n_columns, G_TYPE_INVALID);
462
463   priv->columns_dirty = TRUE;
464
465   return priv->column_headers[index];
466 }
467
468 static gboolean
469 gtk_list_store_get_iter (GtkTreeModel *tree_model,
470                          GtkTreeIter  *iter,
471                          GtkTreePath  *path)
472 {
473   GtkListStore *list_store = GTK_LIST_STORE (tree_model);
474   GtkListStorePrivate *priv = list_store->priv;
475   GSequence *seq;
476   gint i;
477
478   priv->columns_dirty = TRUE;
479
480   seq = priv->seq;
481
482   i = gtk_tree_path_get_indices (path)[0];
483
484   if (i >= g_sequence_get_length (seq))
485     return FALSE;
486
487   iter->stamp = priv->stamp;
488   iter->user_data = g_sequence_get_iter_at_pos (seq, i);
489
490   return TRUE;
491 }
492
493 static GtkTreePath *
494 gtk_list_store_get_path (GtkTreeModel *tree_model,
495                          GtkTreeIter  *iter)
496 {
497   GtkListStore *list_store = GTK_LIST_STORE (tree_model);
498   GtkListStorePrivate *priv = list_store->priv;
499   GtkTreePath *path;
500
501   g_return_val_if_fail (iter->stamp == priv->stamp, NULL);
502
503   if (g_sequence_iter_is_end (iter->user_data))
504     return NULL;
505         
506   path = gtk_tree_path_new ();
507   gtk_tree_path_append_index (path, g_sequence_iter_get_position (iter->user_data));
508   
509   return path;
510 }
511
512 static void
513 gtk_list_store_get_value (GtkTreeModel *tree_model,
514                           GtkTreeIter  *iter,
515                           gint          column,
516                           GValue       *value)
517 {
518   GtkListStore *list_store = GTK_LIST_STORE (tree_model);
519   GtkListStorePrivate *priv = list_store->priv;
520   GtkTreeDataList *list;
521   gint tmp_column = column;
522
523   g_return_if_fail (column < priv->n_columns);
524   g_return_if_fail (VALID_ITER (iter, list_store));
525                     
526   list = g_sequence_get (iter->user_data);
527
528   while (tmp_column-- > 0 && list)
529     list = list->next;
530
531   if (list == NULL)
532     g_value_init (value, priv->column_headers[column]);
533   else
534     _gtk_tree_data_list_node_to_value (list,
535                                        priv->column_headers[column],
536                                        value);
537 }
538
539 static gboolean
540 gtk_list_store_iter_next (GtkTreeModel  *tree_model,
541                           GtkTreeIter   *iter)
542 {
543   GtkListStore *list_store = GTK_LIST_STORE (tree_model);
544   GtkListStorePrivate *priv = list_store->priv;
545   gboolean retval;
546
547   g_return_val_if_fail (priv->stamp == iter->stamp, FALSE);
548   iter->user_data = g_sequence_iter_next (iter->user_data);
549
550   retval = g_sequence_iter_is_end (iter->user_data);
551   if (retval)
552     iter->stamp = 0;
553
554   return !retval;
555 }
556
557 static gboolean
558 gtk_list_store_iter_previous (GtkTreeModel *tree_model,
559                               GtkTreeIter  *iter)
560 {
561   GtkListStore *list_store = GTK_LIST_STORE (tree_model);
562   GtkListStorePrivate *priv = list_store->priv;
563
564   g_return_val_if_fail (priv->stamp == iter->stamp, FALSE);
565
566   if (g_sequence_iter_is_begin (iter->user_data))
567     {
568       iter->stamp = 0;
569       return FALSE;
570     }
571
572   iter->user_data = g_sequence_iter_prev (iter->user_data);
573
574   return TRUE;
575 }
576
577 static gboolean
578 gtk_list_store_iter_children (GtkTreeModel *tree_model,
579                               GtkTreeIter  *iter,
580                               GtkTreeIter  *parent)
581 {
582   GtkListStore *list_store = (GtkListStore *) tree_model;
583   GtkListStorePrivate *priv = list_store->priv;
584
585   /* this is a list, nodes have no children */
586   if (parent)
587     {
588       iter->stamp = 0;
589       return FALSE;
590     }
591
592   if (g_sequence_get_length (priv->seq) > 0)
593     {
594       iter->stamp = priv->stamp;
595       iter->user_data = g_sequence_get_begin_iter (priv->seq);
596       return TRUE;
597     }
598   else
599     {
600       iter->stamp = 0;
601       return FALSE;
602     }
603 }
604
605 static gboolean
606 gtk_list_store_iter_has_child (GtkTreeModel *tree_model,
607                                GtkTreeIter  *iter)
608 {
609   return FALSE;
610 }
611
612 static gint
613 gtk_list_store_iter_n_children (GtkTreeModel *tree_model,
614                                 GtkTreeIter  *iter)
615 {
616   GtkListStore *list_store = GTK_LIST_STORE (tree_model);
617   GtkListStorePrivate *priv = list_store->priv;
618
619   if (iter == NULL)
620     return g_sequence_get_length (priv->seq);
621
622   g_return_val_if_fail (priv->stamp == iter->stamp, -1);
623
624   return 0;
625 }
626
627 static gboolean
628 gtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
629                                GtkTreeIter  *iter,
630                                GtkTreeIter  *parent,
631                                gint          n)
632 {
633   GtkListStore *list_store = GTK_LIST_STORE (tree_model);
634   GtkListStorePrivate *priv = list_store->priv;
635   GSequenceIter *child;
636
637   iter->stamp = 0;
638
639   if (parent)
640     return FALSE;
641
642   child = g_sequence_get_iter_at_pos (priv->seq, n);
643
644   if (g_sequence_iter_is_end (child))
645     return FALSE;
646
647   iter->stamp = priv->stamp;
648   iter->user_data = child;
649
650   return TRUE;
651 }
652
653 static gboolean
654 gtk_list_store_iter_parent (GtkTreeModel *tree_model,
655                             GtkTreeIter  *iter,
656                             GtkTreeIter  *child)
657 {
658   iter->stamp = 0;
659   return FALSE;
660 }
661
662 static gboolean
663 gtk_list_store_real_set_value (GtkListStore *list_store,
664                                GtkTreeIter  *iter,
665                                gint          column,
666                                GValue       *value,
667                                gboolean      sort)
668 {
669   GtkListStorePrivate *priv = list_store->priv;
670   GtkTreeDataList *list;
671   GtkTreeDataList *prev;
672   gint old_column = column;
673   GValue real_value = {0, };
674   gboolean converted = FALSE;
675   gboolean retval = FALSE;
676
677   if (! g_type_is_a (G_VALUE_TYPE (value), priv->column_headers[column]))
678     {
679       if (! (g_value_type_compatible (G_VALUE_TYPE (value), priv->column_headers[column]) &&
680              g_value_type_compatible (priv->column_headers[column], G_VALUE_TYPE (value))))
681         {
682           g_warning ("%s: Unable to convert from %s to %s\n",
683                      G_STRLOC,
684                      g_type_name (G_VALUE_TYPE (value)),
685                      g_type_name (priv->column_headers[column]));
686           return retval;
687         }
688       if (!g_value_transform (value, &real_value))
689         {
690           g_warning ("%s: Unable to make conversion from %s to %s\n",
691                      G_STRLOC,
692                      g_type_name (G_VALUE_TYPE (value)),
693                      g_type_name (priv->column_headers[column]));
694           g_value_unset (&real_value);
695           return retval;
696         }
697       converted = TRUE;
698     }
699
700   prev = list = g_sequence_get (iter->user_data);
701
702   while (list != NULL)
703     {
704       if (column == 0)
705         {
706           if (converted)
707             _gtk_tree_data_list_value_to_node (list, &real_value);
708           else
709             _gtk_tree_data_list_value_to_node (list, value);
710           retval = TRUE;
711           if (converted)
712             g_value_unset (&real_value);
713          if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
714             gtk_list_store_sort_iter_changed (list_store, iter, old_column);
715           return retval;
716         }
717
718       column--;
719       prev = list;
720       list = list->next;
721     }
722
723   if (g_sequence_get (iter->user_data) == NULL)
724     {
725       list = _gtk_tree_data_list_alloc();
726       g_sequence_set (iter->user_data, list);
727       list->next = NULL;
728     }
729   else
730     {
731       list = prev->next = _gtk_tree_data_list_alloc ();
732       list->next = NULL;
733     }
734
735   while (column != 0)
736     {
737       list->next = _gtk_tree_data_list_alloc ();
738       list = list->next;
739       list->next = NULL;
740       column --;
741     }
742
743   if (converted)
744     _gtk_tree_data_list_value_to_node (list, &real_value);
745   else
746     _gtk_tree_data_list_value_to_node (list, value);
747
748   retval = TRUE;
749   if (converted)
750     g_value_unset (&real_value);
751
752   if (sort && GTK_LIST_STORE_IS_SORTED (list_store))
753     gtk_list_store_sort_iter_changed (list_store, iter, old_column);
754
755   return retval;
756 }
757
758
759 /**
760  * gtk_list_store_set_value:
761  * @list_store: A #GtkListStore
762  * @iter: A valid #GtkTreeIter for the row being modified
763  * @column: column number to modify
764  * @value: new value for the cell
765  *
766  * Sets the data in the cell specified by @iter and @column.
767  * The type of @value must be convertible to the type of the
768  * column.
769  *
770  **/
771 void
772 gtk_list_store_set_value (GtkListStore *list_store,
773                           GtkTreeIter  *iter,
774                           gint          column,
775                           GValue       *value)
776 {
777   GtkListStorePrivate *priv = list_store->priv;
778
779   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
780   g_return_if_fail (VALID_ITER (iter, list_store));
781   g_return_if_fail (G_IS_VALUE (value));
782   priv = list_store->priv;
783   g_return_if_fail (column >= 0 && column < priv->n_columns);
784
785   if (gtk_list_store_real_set_value (list_store, iter, column, value, TRUE))
786     {
787       GtkTreePath *path;
788
789       path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
790       gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
791       gtk_tree_path_free (path);
792     }
793 }
794
795 static GtkTreeIterCompareFunc
796 gtk_list_store_get_compare_func (GtkListStore *list_store)
797 {
798   GtkListStorePrivate *priv = list_store->priv;
799   GtkTreeIterCompareFunc func = NULL;
800
801   if (GTK_LIST_STORE_IS_SORTED (list_store))
802     {
803       if (priv->sort_column_id != -1)
804         {
805           GtkTreeDataSortHeader *header;
806           header = _gtk_tree_data_list_get_header (priv->sort_list,
807                                                    priv->sort_column_id);
808           g_return_val_if_fail (header != NULL, NULL);
809           g_return_val_if_fail (header->func != NULL, NULL);
810           func = header->func;
811         }
812       else
813         {
814           func = priv->default_sort_func;
815         }
816     }
817
818   return func;
819 }
820
821 static void
822 gtk_list_store_set_vector_internal (GtkListStore *list_store,
823                                     GtkTreeIter  *iter,
824                                     gboolean     *emit_signal,
825                                     gboolean     *maybe_need_sort,
826                                     gint         *columns,
827                                     GValue       *values,
828                                     gint          n_values)
829 {
830   GtkListStorePrivate *priv = list_store->priv;
831   gint i;
832   GtkTreeIterCompareFunc func = NULL;
833
834   func = gtk_list_store_get_compare_func (list_store);
835   if (func != _gtk_tree_data_list_compare_func)
836     *maybe_need_sort = TRUE;
837
838   for (i = 0; i < n_values; i++)
839     {
840       *emit_signal = gtk_list_store_real_set_value (list_store, 
841                                                iter, 
842                                                columns[i],
843                                                &values[i],
844                                                FALSE) || *emit_signal;
845
846       if (func == _gtk_tree_data_list_compare_func &&
847           columns[i] == priv->sort_column_id)
848         *maybe_need_sort = TRUE;
849     }
850 }
851
852 static void
853 gtk_list_store_set_valist_internal (GtkListStore *list_store,
854                                     GtkTreeIter  *iter,
855                                     gboolean     *emit_signal,
856                                     gboolean     *maybe_need_sort,
857                                     va_list       var_args)
858 {
859   GtkListStorePrivate *priv = list_store->priv;
860   gint column;
861   GtkTreeIterCompareFunc func = NULL;
862
863   column = va_arg (var_args, gint);
864
865   func = gtk_list_store_get_compare_func (list_store);
866   if (func != _gtk_tree_data_list_compare_func)
867     *maybe_need_sort = TRUE;
868
869   while (column != -1)
870     {
871       GValue value = { 0, };
872       gchar *error = NULL;
873
874       if (column < 0 || column >= priv->n_columns)
875         {
876           g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
877           break;
878         }
879
880       G_VALUE_COLLECT_INIT (&value, priv->column_headers[column],
881                             var_args, 0, &error);
882       if (error)
883         {
884           g_warning ("%s: %s", G_STRLOC, error);
885           g_free (error);
886
887           /* we purposely leak the value here, it might not be
888            * in a sane state if an error condition occoured
889            */
890           break;
891         }
892
893       /* FIXME: instead of calling this n times, refactor with above */
894       *emit_signal = gtk_list_store_real_set_value (list_store,
895                                                     iter,
896                                                     column,
897                                                     &value,
898                                                     FALSE) || *emit_signal;
899       
900       if (func == _gtk_tree_data_list_compare_func &&
901           column == priv->sort_column_id)
902         *maybe_need_sort = TRUE;
903
904       g_value_unset (&value);
905
906       column = va_arg (var_args, gint);
907     }
908 }
909
910 /**
911  * gtk_list_store_set_valuesv:
912  * @list_store: A #GtkListStore
913  * @iter: A valid #GtkTreeIter for the row being modified
914  * @columns: (array length=n_values): an array of column numbers
915  * @values: (array length=n_values): an array of GValues
916  * @n_values: the length of the @columns and @values arrays
917  *
918  * A variant of gtk_list_store_set_valist() which
919  * takes the columns and values as two arrays, instead of
920  * varargs. This function is mainly intended for 
921  * language-bindings and in case the number of columns to
922  * change is not known until run-time.
923  *
924  * Since: 2.12
925  * Rename to: gtk_list_store_set
926  */
927 void
928 gtk_list_store_set_valuesv (GtkListStore *list_store,
929                             GtkTreeIter  *iter,
930                             gint         *columns,
931                             GValue       *values,
932                             gint          n_values)
933 {
934   GtkListStorePrivate *priv;
935   gboolean emit_signal = FALSE;
936   gboolean maybe_need_sort = FALSE;
937
938   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
939   g_return_if_fail (VALID_ITER (iter, list_store));
940
941   priv = list_store->priv;
942
943   gtk_list_store_set_vector_internal (list_store, iter,
944                                       &emit_signal,
945                                       &maybe_need_sort,
946                                       columns, values, n_values);
947
948   if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
949     gtk_list_store_sort_iter_changed (list_store, iter, priv->sort_column_id);
950
951   if (emit_signal)
952     {
953       GtkTreePath *path;
954
955       path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
956       gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
957       gtk_tree_path_free (path);
958     }
959 }
960
961 /**
962  * gtk_list_store_set_valist:
963  * @list_store: A #GtkListStore
964  * @iter: A valid #GtkTreeIter for the row being modified
965  * @var_args: va_list of column/value pairs
966  *
967  * See gtk_list_store_set(); this version takes a va_list for use by language
968  * bindings.
969  *
970  **/
971 void
972 gtk_list_store_set_valist (GtkListStore *list_store,
973                            GtkTreeIter  *iter,
974                            va_list       var_args)
975 {
976   GtkListStorePrivate *priv;
977   gboolean emit_signal = FALSE;
978   gboolean maybe_need_sort = FALSE;
979
980   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
981   g_return_if_fail (VALID_ITER (iter, list_store));
982
983   priv = list_store->priv;
984
985   gtk_list_store_set_valist_internal (list_store, iter, 
986                                       &emit_signal, 
987                                       &maybe_need_sort,
988                                       var_args);
989
990   if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
991     gtk_list_store_sort_iter_changed (list_store, iter, priv->sort_column_id);
992
993   if (emit_signal)
994     {
995       GtkTreePath *path;
996
997       path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
998       gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
999       gtk_tree_path_free (path);
1000     }
1001 }
1002
1003 /**
1004  * gtk_list_store_set:
1005  * @list_store: a #GtkListStore
1006  * @iter: row iterator
1007  * @Varargs: pairs of column number and value, terminated with -1
1008  *
1009  * Sets the value of one or more cells in the row referenced by @iter.
1010  * The variable argument list should contain integer column numbers,
1011  * each column number followed by the value to be set.
1012  * The list is terminated by a -1. For example, to set column 0 with type
1013  * %G_TYPE_STRING to "Foo", you would write <literal>gtk_list_store_set (store, iter,
1014  * 0, "Foo", -1)</literal>.
1015  *
1016  * The value will be referenced by the store if it is a %G_TYPE_OBJECT, and it
1017  * will be copied if it is a %G_TYPE_STRING or %G_TYPE_BOXED.
1018  **/
1019 void
1020 gtk_list_store_set (GtkListStore *list_store,
1021                     GtkTreeIter  *iter,
1022                     ...)
1023 {
1024   va_list var_args;
1025
1026   va_start (var_args, iter);
1027   gtk_list_store_set_valist (list_store, iter, var_args);
1028   va_end (var_args);
1029 }
1030
1031 /**
1032  * gtk_list_store_remove:
1033  * @list_store: A #GtkListStore
1034  * @iter: A valid #GtkTreeIter
1035  *
1036  * Removes the given row from the list store.  After being removed, 
1037  * @iter is set to be the next valid row, or invalidated if it pointed 
1038  * to the last row in @list_store.
1039  *
1040  * Return value: %TRUE if @iter is valid, %FALSE if not.
1041  **/
1042 gboolean
1043 gtk_list_store_remove (GtkListStore *list_store,
1044                        GtkTreeIter  *iter)
1045 {
1046   GtkListStorePrivate *priv;
1047   GtkTreePath *path;
1048   GSequenceIter *ptr, *next;
1049
1050   g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1051   g_return_val_if_fail (VALID_ITER (iter, list_store), FALSE);
1052
1053   priv = list_store->priv;
1054
1055   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1056
1057   ptr = iter->user_data;
1058   next = g_sequence_iter_next (ptr);
1059   
1060   _gtk_tree_data_list_free (g_sequence_get (ptr), priv->column_headers);
1061   g_sequence_remove (iter->user_data);
1062
1063   priv->length--;
1064   
1065   gtk_tree_model_row_deleted (GTK_TREE_MODEL (list_store), path);
1066   gtk_tree_path_free (path);
1067
1068   if (g_sequence_iter_is_end (next))
1069     {
1070       iter->stamp = 0;
1071       return FALSE;
1072     }
1073   else
1074     {
1075       iter->stamp = priv->stamp;
1076       iter->user_data = next;
1077       return TRUE;
1078     }
1079 }
1080
1081 /**
1082  * gtk_list_store_insert:
1083  * @list_store: A #GtkListStore
1084  * @iter: (out): An unset #GtkTreeIter to set to the new row
1085  * @position: position to insert the new row
1086  *
1087  * Creates a new row at @position.  @iter will be changed to point to this new
1088  * row.  If @position is larger than the number of rows on the list, then the
1089  * new row will be appended to the list. The row will be empty after this
1090  * function is called.  To fill in values, you need to call 
1091  * gtk_list_store_set() or gtk_list_store_set_value().
1092  *
1093  **/
1094 void
1095 gtk_list_store_insert (GtkListStore *list_store,
1096                        GtkTreeIter  *iter,
1097                        gint          position)
1098 {
1099   GtkListStorePrivate *priv;
1100   GtkTreePath *path;
1101   GSequence *seq;
1102   GSequenceIter *ptr;
1103   gint length;
1104
1105   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1106   g_return_if_fail (iter != NULL);
1107   g_return_if_fail (position >= 0);
1108
1109   priv = list_store->priv;
1110
1111   priv->columns_dirty = TRUE;
1112
1113   seq = priv->seq;
1114
1115   length = g_sequence_get_length (seq);
1116   if (position > length)
1117     position = length;
1118
1119   ptr = g_sequence_get_iter_at_pos (seq, position);
1120   ptr = g_sequence_insert_before (ptr, NULL);
1121
1122   iter->stamp = priv->stamp;
1123   iter->user_data = ptr;
1124
1125   g_assert (VALID_ITER (iter, list_store));
1126
1127   priv->length++;
1128   
1129   path = gtk_tree_path_new ();
1130   gtk_tree_path_append_index (path, position);
1131   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
1132   gtk_tree_path_free (path);
1133 }
1134
1135 /**
1136  * gtk_list_store_insert_before:
1137  * @list_store: A #GtkListStore
1138  * @iter: (out): An unset #GtkTreeIter to set to the new row
1139  * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1140  *
1141  * Inserts a new row before @sibling. If @sibling is %NULL, then the row will 
1142  * be appended to the end of the list. @iter will be changed to point to this 
1143  * new row. The row will be empty after this function is called. To fill in 
1144  * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1145  *
1146  **/
1147 void
1148 gtk_list_store_insert_before (GtkListStore *list_store,
1149                               GtkTreeIter  *iter,
1150                               GtkTreeIter  *sibling)
1151 {
1152   GtkListStorePrivate *priv;
1153   GSequenceIter *after;
1154   
1155   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1156   g_return_if_fail (iter != NULL);
1157
1158   priv = list_store->priv;
1159
1160   if (sibling)
1161     g_return_if_fail (VALID_ITER (sibling, list_store));
1162
1163   if (!sibling)
1164     after = g_sequence_get_end_iter (priv->seq);
1165   else
1166     after = sibling->user_data;
1167
1168   gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1169 }
1170
1171 /**
1172  * gtk_list_store_insert_after:
1173  * @list_store: A #GtkListStore
1174  * @iter: (out): An unset #GtkTreeIter to set to the new row
1175  * @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
1176  *
1177  * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
1178  * prepended to the beginning of the list. @iter will be changed to point to
1179  * this new row. The row will be empty after this function is called. To fill
1180  * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1181  *
1182  **/
1183 void
1184 gtk_list_store_insert_after (GtkListStore *list_store,
1185                              GtkTreeIter  *iter,
1186                              GtkTreeIter  *sibling)
1187 {
1188   GtkListStorePrivate *priv;
1189   GSequenceIter *after;
1190
1191   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1192   g_return_if_fail (iter != NULL);
1193
1194   priv = list_store->priv;
1195
1196   if (sibling)
1197     g_return_if_fail (VALID_ITER (sibling, list_store));
1198
1199   if (!sibling)
1200     after = g_sequence_get_begin_iter (priv->seq);
1201   else
1202     after = g_sequence_iter_next (sibling->user_data);
1203
1204   gtk_list_store_insert (list_store, iter, g_sequence_iter_get_position (after));
1205 }
1206
1207 /**
1208  * gtk_list_store_prepend:
1209  * @list_store: A #GtkListStore
1210  * @iter: (out): An unset #GtkTreeIter to set to the prepend row
1211  *
1212  * Prepends a new row to @list_store. @iter will be changed to point to this new
1213  * row. The row will be empty after this function is called. To fill in
1214  * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1215  *
1216  **/
1217 void
1218 gtk_list_store_prepend (GtkListStore *list_store,
1219                         GtkTreeIter  *iter)
1220 {
1221   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1222   g_return_if_fail (iter != NULL);
1223
1224   gtk_list_store_insert (list_store, iter, 0);
1225 }
1226
1227 /**
1228  * gtk_list_store_append:
1229  * @list_store: A #GtkListStore
1230  * @iter: (out): An unset #GtkTreeIter to set to the appended row
1231  *
1232  * Appends a new row to @list_store.  @iter will be changed to point to this new
1233  * row.  The row will be empty after this function is called.  To fill in
1234  * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
1235  *
1236  **/
1237 void
1238 gtk_list_store_append (GtkListStore *list_store,
1239                        GtkTreeIter  *iter)
1240 {
1241   GtkListStorePrivate *priv;
1242
1243   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1244   g_return_if_fail (iter != NULL);
1245
1246   priv = list_store->priv;
1247
1248   gtk_list_store_insert (list_store, iter, g_sequence_get_length (priv->seq));
1249 }
1250
1251 static void
1252 gtk_list_store_increment_stamp (GtkListStore *list_store)
1253 {
1254   GtkListStorePrivate *priv = list_store->priv;
1255
1256   do
1257     {
1258       priv->stamp++;
1259     }
1260   while (priv->stamp == 0);
1261 }
1262
1263 /**
1264  * gtk_list_store_clear:
1265  * @list_store: a #GtkListStore.
1266  *
1267  * Removes all rows from the list store.  
1268  *
1269  **/
1270 void
1271 gtk_list_store_clear (GtkListStore *list_store)
1272 {
1273   GtkListStorePrivate *priv;
1274   GtkTreeIter iter;
1275
1276   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
1277
1278   priv = list_store->priv;
1279
1280   while (g_sequence_get_length (priv->seq) > 0)
1281     {
1282       iter.stamp = priv->stamp;
1283       iter.user_data = g_sequence_get_begin_iter (priv->seq);
1284       gtk_list_store_remove (list_store, &iter);
1285     }
1286
1287   gtk_list_store_increment_stamp (list_store);
1288 }
1289
1290 /**
1291  * gtk_list_store_iter_is_valid:
1292  * @list_store: A #GtkListStore.
1293  * @iter: A #GtkTreeIter.
1294  *
1295  * <warning>This function is slow. Only use it for debugging and/or testing
1296  * purposes.</warning>
1297  *
1298  * Checks if the given iter is a valid iter for this #GtkListStore.
1299  *
1300  * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
1301  *
1302  * Since: 2.2
1303  **/
1304 gboolean
1305 gtk_list_store_iter_is_valid (GtkListStore *list_store,
1306                               GtkTreeIter  *iter)
1307 {
1308   GtkListStorePrivate *priv;
1309
1310   g_return_val_if_fail (GTK_IS_LIST_STORE (list_store), FALSE);
1311   g_return_val_if_fail (iter != NULL, FALSE);
1312
1313   priv = list_store->priv;
1314
1315   if (!VALID_ITER (iter, list_store))
1316     return FALSE;
1317
1318   if (g_sequence_iter_get_sequence (iter->user_data) != priv->seq)
1319     return FALSE;
1320
1321   return TRUE;
1322 }
1323
1324 static gboolean real_gtk_list_store_row_draggable (GtkTreeDragSource *drag_source,
1325                                                    GtkTreePath       *path)
1326 {
1327   return TRUE;
1328 }
1329   
1330 static gboolean
1331 gtk_list_store_drag_data_delete (GtkTreeDragSource *drag_source,
1332                                  GtkTreePath       *path)
1333 {
1334   GtkTreeIter iter;
1335
1336   if (gtk_list_store_get_iter (GTK_TREE_MODEL (drag_source),
1337                                &iter,
1338                                path))
1339     {
1340       gtk_list_store_remove (GTK_LIST_STORE (drag_source), &iter);
1341       return TRUE;
1342     }
1343   return FALSE;
1344 }
1345
1346 static gboolean
1347 gtk_list_store_drag_data_get (GtkTreeDragSource *drag_source,
1348                               GtkTreePath       *path,
1349                               GtkSelectionData  *selection_data)
1350 {
1351   /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
1352    * target, because the default handler does it for us, but
1353    * we do anyway for the convenience of someone maybe overriding the
1354    * default handler.
1355    */
1356
1357   if (gtk_tree_set_row_drag_data (selection_data,
1358                                   GTK_TREE_MODEL (drag_source),
1359                                   path))
1360     {
1361       return TRUE;
1362     }
1363   else
1364     {
1365       /* FIXME handle text targets at least. */
1366     }
1367
1368   return FALSE;
1369 }
1370
1371 static gboolean
1372 gtk_list_store_drag_data_received (GtkTreeDragDest   *drag_dest,
1373                                    GtkTreePath       *dest,
1374                                    GtkSelectionData  *selection_data)
1375 {
1376   GtkTreeModel *tree_model = GTK_TREE_MODEL (drag_dest);
1377   GtkListStore *list_store = GTK_LIST_STORE (tree_model);
1378   GtkListStorePrivate *priv = list_store->priv;
1379   GtkTreeModel *src_model = NULL;
1380   GtkTreePath *src_path = NULL;
1381   gboolean retval = FALSE;
1382
1383   if (gtk_tree_get_row_drag_data (selection_data,
1384                                   &src_model,
1385                                   &src_path) &&
1386       src_model == tree_model)
1387     {
1388       /* Copy the given row to a new position */
1389       GtkTreeIter src_iter;
1390       GtkTreeIter dest_iter;
1391       GtkTreePath *prev;
1392
1393       if (!gtk_list_store_get_iter (src_model,
1394                                     &src_iter,
1395                                     src_path))
1396         {
1397           goto out;
1398         }
1399
1400       /* Get the path to insert _after_ (dest is the path to insert _before_) */
1401       prev = gtk_tree_path_copy (dest);
1402
1403       if (!gtk_tree_path_prev (prev))
1404         {
1405           /* dest was the first spot in the list; which means we are supposed
1406            * to prepend.
1407            */
1408           gtk_list_store_prepend (list_store, &dest_iter);
1409
1410           retval = TRUE;
1411         }
1412       else
1413         {
1414           if (gtk_list_store_get_iter (tree_model, &dest_iter, prev))
1415             {
1416               GtkTreeIter tmp_iter = dest_iter;
1417
1418               gtk_list_store_insert_after (list_store, &dest_iter, &tmp_iter);
1419
1420               retval = TRUE;
1421             }
1422         }
1423
1424       gtk_tree_path_free (prev);
1425
1426       /* If we succeeded in creating dest_iter, copy data from src
1427        */
1428       if (retval)
1429         {
1430           GtkTreeDataList *dl = g_sequence_get (src_iter.user_data);
1431           GtkTreeDataList *copy_head = NULL;
1432           GtkTreeDataList *copy_prev = NULL;
1433           GtkTreeDataList *copy_iter = NULL;
1434           GtkTreePath *path;
1435           gint col;
1436
1437           col = 0;
1438           while (dl)
1439             {
1440               copy_iter = _gtk_tree_data_list_node_copy (dl,
1441                                                          priv->column_headers[col]);
1442
1443               if (copy_head == NULL)
1444                 copy_head = copy_iter;
1445
1446               if (copy_prev)
1447                 copy_prev->next = copy_iter;
1448
1449               copy_prev = copy_iter;
1450
1451               dl = dl->next;
1452               ++col;
1453             }
1454
1455           dest_iter.stamp = priv->stamp;
1456           g_sequence_set (dest_iter.user_data, copy_head);
1457
1458           path = gtk_list_store_get_path (tree_model, &dest_iter);
1459           gtk_tree_model_row_changed (tree_model, path, &dest_iter);
1460           gtk_tree_path_free (path);
1461         }
1462     }
1463   else
1464     {
1465       /* FIXME maybe add some data targets eventually, or handle text
1466        * targets in the simple case.
1467        */
1468     }
1469
1470  out:
1471
1472   if (src_path)
1473     gtk_tree_path_free (src_path);
1474
1475   return retval;
1476 }
1477
1478 static gboolean
1479 gtk_list_store_row_drop_possible (GtkTreeDragDest  *drag_dest,
1480                                   GtkTreePath      *dest_path,
1481                                   GtkSelectionData *selection_data)
1482 {
1483   gint *indices;
1484   GtkTreeModel *src_model = NULL;
1485   GtkTreePath *src_path = NULL;
1486   gboolean retval = FALSE;
1487
1488   /* don't accept drops if the list has been sorted */
1489   if (GTK_LIST_STORE_IS_SORTED (drag_dest))
1490     return FALSE;
1491
1492   if (!gtk_tree_get_row_drag_data (selection_data,
1493                                    &src_model,
1494                                    &src_path))
1495     goto out;
1496
1497   if (src_model != GTK_TREE_MODEL (drag_dest))
1498     goto out;
1499
1500   if (gtk_tree_path_get_depth (dest_path) != 1)
1501     goto out;
1502
1503   /* can drop before any existing node, or before one past any existing. */
1504
1505   indices = gtk_tree_path_get_indices (dest_path);
1506
1507   if (indices[0] <= g_sequence_get_length (GTK_LIST_STORE (drag_dest)->priv->seq))
1508     retval = TRUE;
1509
1510  out:
1511   if (src_path)
1512     gtk_tree_path_free (src_path);
1513   
1514   return retval;
1515 }
1516
1517 /* Sorting and reordering */
1518
1519 /* Reordering */
1520 static gint
1521 gtk_list_store_reorder_func (GSequenceIter *a,
1522                              GSequenceIter *b,
1523                              gpointer       user_data)
1524 {
1525   GHashTable *new_positions = user_data;
1526   gint apos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, a));
1527   gint bpos = GPOINTER_TO_INT (g_hash_table_lookup (new_positions, b));
1528
1529   if (apos < bpos)
1530     return -1;
1531   if (apos > bpos)
1532     return 1;
1533   return 0;
1534 }
1535   
1536 /**
1537  * gtk_list_store_reorder: (skip)
1538  * @store: A #GtkListStore.
1539  * @new_order: (array): an array of integers mapping the new position of each child
1540  *      to its old position before the re-ordering,
1541  *      i.e. @new_order<literal>[newpos] = oldpos</literal>.
1542  *
1543  * Reorders @store to follow the order indicated by @new_order. Note that
1544  * this function only works with unsorted stores.
1545  *
1546  * Since: 2.2
1547  **/
1548 void
1549 gtk_list_store_reorder (GtkListStore *store,
1550                         gint         *new_order)
1551 {
1552   GtkListStorePrivate *priv;
1553   gint i;
1554   GtkTreePath *path;
1555   GHashTable *new_positions;
1556   GSequenceIter *ptr;
1557   gint *order;
1558   
1559   g_return_if_fail (GTK_IS_LIST_STORE (store));
1560   g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1561   g_return_if_fail (new_order != NULL);
1562
1563   priv = store->priv;
1564
1565   order = g_new (gint, g_sequence_get_length (priv->seq));
1566   for (i = 0; i < g_sequence_get_length (priv->seq); i++)
1567     order[new_order[i]] = i;
1568   
1569   new_positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1570
1571   ptr = g_sequence_get_begin_iter (priv->seq);
1572   i = 0;
1573   while (!g_sequence_iter_is_end (ptr))
1574     {
1575       g_hash_table_insert (new_positions, ptr, GINT_TO_POINTER (order[i++]));
1576
1577       ptr = g_sequence_iter_next (ptr);
1578     }
1579   g_free (order);
1580   
1581   g_sequence_sort_iter (priv->seq, gtk_list_store_reorder_func, new_positions);
1582
1583   g_hash_table_destroy (new_positions);
1584   
1585   /* emit signal */
1586   path = gtk_tree_path_new ();
1587   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1588                                  path, NULL, new_order);
1589   gtk_tree_path_free (path);
1590 }
1591
1592 static GHashTable *
1593 save_positions (GSequence *seq)
1594 {
1595   GHashTable *positions = g_hash_table_new (g_direct_hash, g_direct_equal);
1596   GSequenceIter *ptr;
1597
1598   ptr = g_sequence_get_begin_iter (seq);
1599   while (!g_sequence_iter_is_end (ptr))
1600     {
1601       g_hash_table_insert (positions, ptr,
1602                            GINT_TO_POINTER (g_sequence_iter_get_position (ptr)));
1603       ptr = g_sequence_iter_next (ptr);
1604     }
1605
1606   return positions;
1607 }
1608
1609 static int *
1610 generate_order (GSequence *seq,
1611                 GHashTable *old_positions)
1612 {
1613   GSequenceIter *ptr;
1614   int *order = g_new (int, g_sequence_get_length (seq));
1615   int i;
1616
1617   i = 0;
1618   ptr = g_sequence_get_begin_iter (seq);
1619   while (!g_sequence_iter_is_end (ptr))
1620     {
1621       int old_pos = GPOINTER_TO_INT (g_hash_table_lookup (old_positions, ptr));
1622       order[i++] = old_pos;
1623       ptr = g_sequence_iter_next (ptr);
1624     }
1625
1626   g_hash_table_destroy (old_positions);
1627
1628   return order;
1629 }
1630
1631 /**
1632  * gtk_list_store_swap:
1633  * @store: A #GtkListStore.
1634  * @a: A #GtkTreeIter.
1635  * @b: Another #GtkTreeIter.
1636  *
1637  * Swaps @a and @b in @store. Note that this function only works with
1638  * unsorted stores.
1639  *
1640  * Since: 2.2
1641  **/
1642 void
1643 gtk_list_store_swap (GtkListStore *store,
1644                      GtkTreeIter  *a,
1645                      GtkTreeIter  *b)
1646 {
1647   GtkListStorePrivate *priv;
1648   GHashTable *old_positions;
1649   gint *order;
1650   GtkTreePath *path;
1651
1652   g_return_if_fail (GTK_IS_LIST_STORE (store));
1653   g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1654   g_return_if_fail (VALID_ITER (a, store));
1655   g_return_if_fail (VALID_ITER (b, store));
1656
1657   priv = store->priv;
1658
1659   if (a->user_data == b->user_data)
1660     return;
1661
1662   old_positions = save_positions (priv->seq);
1663   
1664   g_sequence_swap (a->user_data, b->user_data);
1665
1666   order = generate_order (priv->seq, old_positions);
1667   path = gtk_tree_path_new ();
1668   
1669   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1670                                  path, NULL, order);
1671
1672   gtk_tree_path_free (path);
1673   g_free (order);
1674 }
1675
1676 static void
1677 gtk_list_store_move_to (GtkListStore *store,
1678                         GtkTreeIter  *iter,
1679                         gint          new_pos)
1680 {
1681   GtkListStorePrivate *priv = store->priv;
1682   GHashTable *old_positions;
1683   GtkTreePath *path;
1684   gint *order;
1685
1686   old_positions = save_positions (priv->seq);
1687
1688   g_sequence_move (iter->user_data, g_sequence_get_iter_at_pos (priv->seq, new_pos));
1689
1690   order = generate_order (priv->seq, old_positions);
1691
1692   path = gtk_tree_path_new ();
1693   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (store),
1694                                  path, NULL, order);
1695   gtk_tree_path_free (path);
1696   g_free (order);
1697 }
1698
1699 /**
1700  * gtk_list_store_move_before:
1701  * @store: A #GtkListStore.
1702  * @iter: A #GtkTreeIter.
1703  * @position: (allow-none): A #GtkTreeIter, or %NULL.
1704  *
1705  * Moves @iter in @store to the position before @position. Note that this
1706  * function only works with unsorted stores. If @position is %NULL, @iter
1707  * will be moved to the end of the list.
1708  *
1709  * Since: 2.2
1710  **/
1711 void
1712 gtk_list_store_move_before (GtkListStore *store,
1713                             GtkTreeIter  *iter,
1714                             GtkTreeIter  *position)
1715 {
1716   gint pos;
1717   
1718   g_return_if_fail (GTK_IS_LIST_STORE (store));
1719   g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1720   g_return_if_fail (VALID_ITER (iter, store));
1721   if (position)
1722     g_return_if_fail (VALID_ITER (position, store));
1723
1724   if (position)
1725     pos = g_sequence_iter_get_position (position->user_data);
1726   else
1727     pos = -1;
1728   
1729   gtk_list_store_move_to (store, iter, pos);
1730 }
1731
1732 /**
1733  * gtk_list_store_move_after:
1734  * @store: A #GtkListStore.
1735  * @iter: A #GtkTreeIter.
1736  * @position: (allow-none): A #GtkTreeIter or %NULL.
1737  *
1738  * Moves @iter in @store to the position after @position. Note that this
1739  * function only works with unsorted stores. If @position is %NULL, @iter
1740  * will be moved to the start of the list.
1741  *
1742  * Since: 2.2
1743  **/
1744 void
1745 gtk_list_store_move_after (GtkListStore *store,
1746                            GtkTreeIter  *iter,
1747                            GtkTreeIter  *position)
1748 {
1749   gint pos;
1750   
1751   g_return_if_fail (GTK_IS_LIST_STORE (store));
1752   g_return_if_fail (!GTK_LIST_STORE_IS_SORTED (store));
1753   g_return_if_fail (VALID_ITER (iter, store));
1754   if (position)
1755     g_return_if_fail (VALID_ITER (position, store));
1756
1757   if (position)
1758     pos = g_sequence_iter_get_position (position->user_data) + 1;
1759   else
1760     pos = 0;
1761   
1762   gtk_list_store_move_to (store, iter, pos);
1763 }
1764     
1765 /* Sorting */
1766 static gint
1767 gtk_list_store_compare_func (GSequenceIter *a,
1768                              GSequenceIter *b,
1769                              gpointer      user_data)
1770 {
1771   GtkListStore *list_store = user_data;
1772   GtkListStorePrivate *priv = list_store->priv;
1773   GtkTreeIter iter_a;
1774   GtkTreeIter iter_b;
1775   gint retval;
1776   GtkTreeIterCompareFunc func;
1777   gpointer data;
1778
1779   if (priv->sort_column_id != -1)
1780     {
1781       GtkTreeDataSortHeader *header;
1782
1783       header = _gtk_tree_data_list_get_header (priv->sort_list,
1784                                                priv->sort_column_id);
1785       g_return_val_if_fail (header != NULL, 0);
1786       g_return_val_if_fail (header->func != NULL, 0);
1787
1788       func = header->func;
1789       data = header->data;
1790     }
1791   else
1792     {
1793       g_return_val_if_fail (priv->default_sort_func != NULL, 0);
1794       func = priv->default_sort_func;
1795       data = priv->default_sort_data;
1796     }
1797
1798   iter_a.stamp = priv->stamp;
1799   iter_a.user_data = (gpointer)a;
1800   iter_b.stamp = priv->stamp;
1801   iter_b.user_data = (gpointer)b;
1802
1803   g_assert (VALID_ITER (&iter_a, list_store));
1804   g_assert (VALID_ITER (&iter_b, list_store));
1805
1806   retval = (* func) (GTK_TREE_MODEL (list_store), &iter_a, &iter_b, data);
1807
1808   if (priv->order == GTK_SORT_DESCENDING)
1809     {
1810       if (retval > 0)
1811         retval = -1;
1812       else if (retval < 0)
1813         retval = 1;
1814     }
1815
1816   return retval;
1817 }
1818
1819 static void
1820 gtk_list_store_sort (GtkListStore *list_store)
1821 {
1822   GtkListStorePrivate *priv = list_store->priv;
1823   gint *new_order;
1824   GtkTreePath *path;
1825   GHashTable *old_positions;
1826
1827   if (!GTK_LIST_STORE_IS_SORTED (list_store) ||
1828       g_sequence_get_length (priv->seq) <= 1)
1829     return;
1830
1831   old_positions = save_positions (priv->seq);
1832
1833   g_sequence_sort_iter (priv->seq, gtk_list_store_compare_func, list_store);
1834
1835   /* Let the world know about our new order */
1836   new_order = generate_order (priv->seq, old_positions);
1837
1838   path = gtk_tree_path_new ();
1839   gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1840                                  path, NULL, new_order);
1841   gtk_tree_path_free (path);
1842   g_free (new_order);
1843 }
1844
1845 static gboolean
1846 iter_is_sorted (GtkListStore *list_store,
1847                 GtkTreeIter  *iter)
1848 {
1849   GSequenceIter *cmp;
1850
1851   if (!g_sequence_iter_is_begin (iter->user_data))
1852     {
1853       cmp = g_sequence_iter_prev (iter->user_data);
1854       if (gtk_list_store_compare_func (cmp, iter->user_data, list_store) > 0)
1855         return FALSE;
1856     }
1857
1858   cmp = g_sequence_iter_next (iter->user_data);
1859   if (!g_sequence_iter_is_end (cmp))
1860     {
1861       if (gtk_list_store_compare_func (iter->user_data, cmp, list_store) > 0)
1862         return FALSE;
1863     }
1864   
1865   return TRUE;
1866 }
1867
1868 static void
1869 gtk_list_store_sort_iter_changed (GtkListStore *list_store,
1870                                   GtkTreeIter  *iter,
1871                                   gint          column)
1872
1873 {
1874   GtkListStorePrivate *priv = list_store->priv;
1875   GtkTreePath *path;
1876
1877   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
1878   gtk_tree_model_row_changed (GTK_TREE_MODEL (list_store), path, iter);
1879   gtk_tree_path_free (path);
1880
1881   if (!iter_is_sorted (list_store, iter))
1882     {
1883       GHashTable *old_positions;
1884       gint *order;
1885
1886       old_positions = save_positions (priv->seq);
1887       g_sequence_sort_changed_iter (iter->user_data,
1888                                     gtk_list_store_compare_func,
1889                                     list_store);
1890       order = generate_order (priv->seq, old_positions);
1891       path = gtk_tree_path_new ();
1892       gtk_tree_model_rows_reordered (GTK_TREE_MODEL (list_store),
1893                                      path, NULL, order);
1894       gtk_tree_path_free (path);
1895       g_free (order);
1896     }
1897 }
1898
1899 static gboolean
1900 gtk_list_store_get_sort_column_id (GtkTreeSortable  *sortable,
1901                                    gint             *sort_column_id,
1902                                    GtkSortType      *order)
1903 {
1904   GtkListStore *list_store = GTK_LIST_STORE (sortable);
1905   GtkListStorePrivate *priv = list_store->priv;
1906
1907   if (sort_column_id)
1908     * sort_column_id = priv->sort_column_id;
1909   if (order)
1910     * order = priv->order;
1911
1912   if (priv->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
1913       priv->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
1914     return FALSE;
1915
1916   return TRUE;
1917 }
1918
1919 static void
1920 gtk_list_store_set_sort_column_id (GtkTreeSortable  *sortable,
1921                                    gint              sort_column_id,
1922                                    GtkSortType       order)
1923 {
1924   GtkListStore *list_store = GTK_LIST_STORE (sortable);
1925   GtkListStorePrivate *priv = list_store->priv;
1926
1927   if ((priv->sort_column_id == sort_column_id) &&
1928       (priv->order == order))
1929     return;
1930
1931   if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
1932     {
1933       if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1934         {
1935           GtkTreeDataSortHeader *header = NULL;
1936
1937           header = _gtk_tree_data_list_get_header (priv->sort_list, 
1938                                                    sort_column_id);
1939
1940           /* We want to make sure that we have a function */
1941           g_return_if_fail (header != NULL);
1942           g_return_if_fail (header->func != NULL);
1943         }
1944       else
1945         {
1946           g_return_if_fail (priv->default_sort_func != NULL);
1947         }
1948     }
1949
1950
1951   priv->sort_column_id = sort_column_id;
1952   priv->order = order;
1953
1954   gtk_tree_sortable_sort_column_changed (sortable);
1955
1956   gtk_list_store_sort (list_store);
1957 }
1958
1959 static void
1960 gtk_list_store_set_sort_func (GtkTreeSortable        *sortable,
1961                               gint                    sort_column_id,
1962                               GtkTreeIterCompareFunc  func,
1963                               gpointer                data,
1964                               GDestroyNotify          destroy)
1965 {
1966   GtkListStore *list_store = GTK_LIST_STORE (sortable);
1967   GtkListStorePrivate *priv = list_store->priv;
1968
1969   priv->sort_list = _gtk_tree_data_list_set_header (priv->sort_list, 
1970                                                           sort_column_id, 
1971                                                           func, data, destroy);
1972
1973   if (priv->sort_column_id == sort_column_id)
1974     gtk_list_store_sort (list_store);
1975 }
1976
1977 static void
1978 gtk_list_store_set_default_sort_func (GtkTreeSortable        *sortable,
1979                                       GtkTreeIterCompareFunc  func,
1980                                       gpointer                data,
1981                                       GDestroyNotify          destroy)
1982 {
1983   GtkListStore *list_store = GTK_LIST_STORE (sortable);
1984   GtkListStorePrivate *priv = list_store->priv;
1985
1986   if (priv->default_sort_destroy)
1987     {
1988       GDestroyNotify d = priv->default_sort_destroy;
1989
1990       priv->default_sort_destroy = NULL;
1991       d (priv->default_sort_data);
1992     }
1993
1994   priv->default_sort_func = func;
1995   priv->default_sort_data = data;
1996   priv->default_sort_destroy = destroy;
1997
1998   if (priv->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1999     gtk_list_store_sort (list_store);
2000 }
2001
2002 static gboolean
2003 gtk_list_store_has_default_sort_func (GtkTreeSortable *sortable)
2004 {
2005   GtkListStore *list_store = GTK_LIST_STORE (sortable);
2006   GtkListStorePrivate *priv = list_store->priv;
2007
2008   return (priv->default_sort_func != NULL);
2009 }
2010
2011
2012 /**
2013  * gtk_list_store_insert_with_values:
2014  * @list_store: A #GtkListStore
2015  * @iter: (out) (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL.
2016  * @position: position to insert the new row
2017  * @Varargs: pairs of column number and value, terminated with -1
2018  *
2019  * Creates a new row at @position.  @iter will be changed to point to this new
2020  * row.  If @position is larger than the number of rows on the list, then the
2021  * new row will be appended to the list. The row will be filled with the 
2022  * values given to this function. 
2023  * 
2024  * Calling
2025  * <literal>gtk_list_store_insert_with_values(list_store, iter, position...)</literal> 
2026  * has the same effect as calling 
2027  * |[
2028  * gtk_list_store_insert (list_store, iter, position);
2029  * gtk_list_store_set (list_store, iter, ...);
2030  * ]|
2031  * with the difference that the former will only emit a row_inserted signal,
2032  * while the latter will emit row_inserted, row_changed and, if the list store
2033  * is sorted, rows_reordered. Since emitting the rows_reordered signal
2034  * repeatedly can affect the performance of the program, 
2035  * gtk_list_store_insert_with_values() should generally be preferred when
2036  * inserting rows in a sorted list store.
2037  *
2038  * Since: 2.6
2039  */
2040 void
2041 gtk_list_store_insert_with_values (GtkListStore *list_store,
2042                                    GtkTreeIter  *iter,
2043                                    gint          position,
2044                                    ...)
2045 {
2046   GtkListStorePrivate *priv;
2047   GtkTreePath *path;
2048   GSequence *seq;
2049   GSequenceIter *ptr;
2050   GtkTreeIter tmp_iter;
2051   gint length;
2052   gboolean changed = FALSE;
2053   gboolean maybe_need_sort = FALSE;
2054   va_list var_args;
2055
2056   /* FIXME: refactor to reduce overlap with gtk_list_store_set() */
2057   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2058
2059   priv = list_store->priv;
2060
2061   if (!iter)
2062     iter = &tmp_iter;
2063
2064   priv->columns_dirty = TRUE;
2065
2066   seq = priv->seq;
2067
2068   length = g_sequence_get_length (seq);
2069   if (position > length)
2070     position = length;
2071
2072   ptr = g_sequence_get_iter_at_pos (seq, position);
2073   ptr = g_sequence_insert_before (ptr, NULL);
2074
2075   iter->stamp = priv->stamp;
2076   iter->user_data = ptr;
2077
2078   g_assert (VALID_ITER (iter, list_store));
2079
2080   priv->length++;
2081
2082   va_start (var_args, position);
2083   gtk_list_store_set_valist_internal (list_store, iter, 
2084                                       &changed, &maybe_need_sort,
2085                                       var_args);
2086   va_end (var_args);
2087
2088   /* Don't emit rows_reordered here */
2089   if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2090     g_sequence_sort_changed_iter (iter->user_data,
2091                                   gtk_list_store_compare_func,
2092                                   list_store);
2093
2094   /* Just emit row_inserted */
2095   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2096   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2097   gtk_tree_path_free (path);
2098 }
2099
2100
2101 /**
2102  * gtk_list_store_insert_with_valuesv:
2103  * @list_store: A #GtkListStore
2104  * @iter: (out) (allow-none): An unset #GtkTreeIter to set to the new row, or %NULL.
2105  * @position: position to insert the new row
2106  * @columns: (array length=n_values): an array of column numbers
2107  * @values: (array length=n_values): an array of GValues 
2108  * @n_values: the length of the @columns and @values arrays
2109  * 
2110  * A variant of gtk_list_store_insert_with_values() which
2111  * takes the columns and values as two arrays, instead of
2112  * varargs. This function is mainly intended for 
2113  * language-bindings.
2114  *
2115  * Since: 2.6
2116  */
2117 void
2118 gtk_list_store_insert_with_valuesv (GtkListStore *list_store,
2119                                     GtkTreeIter  *iter,
2120                                     gint          position,
2121                                     gint         *columns, 
2122                                     GValue       *values,
2123                                     gint          n_values)
2124 {
2125   GtkListStorePrivate *priv;
2126   GtkTreePath *path;
2127   GSequence *seq;
2128   GSequenceIter *ptr;
2129   GtkTreeIter tmp_iter;
2130   gint length;
2131   gboolean changed = FALSE;
2132   gboolean maybe_need_sort = FALSE;
2133
2134   /* FIXME refactor to reduce overlap with 
2135    * gtk_list_store_insert_with_values() 
2136    */
2137   g_return_if_fail (GTK_IS_LIST_STORE (list_store));
2138
2139   priv = list_store->priv;
2140
2141   if (!iter)
2142     iter = &tmp_iter;
2143
2144   priv->columns_dirty = TRUE;
2145
2146   seq = priv->seq;
2147
2148   length = g_sequence_get_length (seq);
2149   if (position > length)
2150     position = length;
2151
2152   ptr = g_sequence_get_iter_at_pos (seq, position);
2153   ptr = g_sequence_insert_before (ptr, NULL);
2154
2155   iter->stamp = priv->stamp;
2156   iter->user_data = ptr;
2157
2158   g_assert (VALID_ITER (iter, list_store));
2159
2160   priv->length++;  
2161
2162   gtk_list_store_set_vector_internal (list_store, iter,
2163                                       &changed, &maybe_need_sort,
2164                                       columns, values, n_values);
2165
2166   /* Don't emit rows_reordered here */
2167   if (maybe_need_sort && GTK_LIST_STORE_IS_SORTED (list_store))
2168     g_sequence_sort_changed_iter (iter->user_data,
2169                                   gtk_list_store_compare_func,
2170                                   list_store);
2171
2172   /* Just emit row_inserted */
2173   path = gtk_list_store_get_path (GTK_TREE_MODEL (list_store), iter);
2174   gtk_tree_model_row_inserted (GTK_TREE_MODEL (list_store), path, iter);
2175   gtk_tree_path_free (path);
2176 }
2177
2178 /* GtkBuildable custom tag implementation
2179  *
2180  * <columns>
2181  *   <column type="..."/>
2182  *   <column type="..."/>
2183  * </columns>
2184  */
2185 typedef struct {
2186   gboolean translatable;
2187   gchar *context;
2188   int id;
2189 } ColInfo;
2190
2191 typedef struct {
2192   GtkBuilder *builder;
2193   GObject *object;
2194   GSList *column_type_names;
2195   GType *column_types;
2196   GValue *values;
2197   gint *colids;
2198   ColInfo **columns;
2199   gint last_row;
2200   gint n_columns;
2201   gint row_column;
2202   GQuark error_quark;
2203   gboolean is_data;
2204   const gchar *domain;
2205 } SubParserData;
2206
2207 static void
2208 list_store_start_element (GMarkupParseContext *context,
2209                           const gchar         *element_name,
2210                           const gchar        **names,
2211                           const gchar        **values,
2212                           gpointer             user_data,
2213                           GError             **error)
2214 {
2215   guint i;
2216   SubParserData *data = (SubParserData*)user_data;
2217
2218   if (strcmp (element_name, "col") == 0)
2219     {
2220       int i, id = -1;
2221       gchar *context = NULL;
2222       gboolean translatable = FALSE;
2223       ColInfo *info;
2224
2225       if (data->row_column >= data->n_columns)
2226         {
2227           g_set_error (error, data->error_quark, 0,
2228                        "Too many columns, maximum is %d\n", data->n_columns - 1);
2229           return;
2230         }
2231
2232       for (i = 0; names[i]; i++)
2233         if (strcmp (names[i], "id") == 0)
2234           {
2235             errno = 0;
2236             id = atoi (values[i]);
2237             if (errno)
2238               {
2239                 g_set_error (error, data->error_quark, 0,
2240                              "the id tag %s could not be converted to an integer",
2241                              values[i]);
2242                 return;
2243               }
2244             if (id < 0 || id >= data->n_columns)
2245               {
2246                 g_set_error (error, data->error_quark, 0,
2247                              "id value %d out of range", id);
2248                 return;
2249               }
2250           }
2251         else if (strcmp (names[i], "translatable") == 0)
2252           {
2253             if (!_gtk_builder_boolean_from_string (values[i], &translatable,
2254                                                    error))
2255               return;
2256           }
2257         else if (strcmp (names[i], "comments") == 0)
2258           {
2259             /* do nothing, comments are for translators */
2260           }
2261         else if (strcmp (names[i], "context") == 0) 
2262           {
2263             context = g_strdup (values[i]);
2264           }
2265
2266       if (id == -1)
2267         {
2268           g_set_error (error, data->error_quark, 0,
2269                        "<col> needs an id attribute");
2270           return;
2271         }
2272       
2273       info = g_slice_new0 (ColInfo);
2274       info->translatable = translatable;
2275       info->context = context;
2276       info->id = id;
2277
2278       data->colids[data->row_column] = id;
2279       data->columns[data->row_column] = info;
2280       data->row_column++;
2281       data->is_data = TRUE;
2282     }
2283   else if (strcmp (element_name, "row") == 0)
2284     ;
2285   else if (strcmp (element_name, "column") == 0)
2286     for (i = 0; names[i]; i++)
2287       if (strcmp (names[i], "type") == 0)
2288         data->column_type_names = g_slist_prepend (data->column_type_names,
2289                                                    g_strdup (values[i]));
2290   else if (strcmp (element_name, "columns") == 0)
2291     ;
2292   else if (strcmp (element_name, "data") == 0)
2293     ;
2294   else
2295     g_set_error (error, data->error_quark, 0,
2296                  "Unknown start tag: %s", element_name);
2297 }
2298
2299 static void
2300 list_store_end_element (GMarkupParseContext *context,
2301                         const gchar         *element_name,
2302                         gpointer             user_data,
2303                         GError             **error)
2304 {
2305   SubParserData *data = (SubParserData*)user_data;
2306
2307   g_assert (data->builder);
2308   
2309   if (strcmp (element_name, "row") == 0)
2310     {
2311       GtkTreeIter iter;
2312       int i;
2313
2314       gtk_list_store_insert_with_valuesv (GTK_LIST_STORE (data->object),
2315                                           &iter,
2316                                           data->last_row,
2317                                           data->colids,
2318                                           data->values,
2319                                           data->row_column);
2320       for (i = 0; i < data->row_column; i++)
2321         {
2322           ColInfo *info = data->columns[i];
2323           g_free (info->context);
2324           g_slice_free (ColInfo, info);
2325           data->columns[i] = NULL;
2326           g_value_unset (&data->values[i]);
2327         }
2328       g_free (data->values);
2329       data->values = g_new0 (GValue, data->n_columns);
2330       data->last_row++;
2331       data->row_column = 0;
2332     }
2333   else if (strcmp (element_name, "columns") == 0)
2334     {
2335       GType *column_types;
2336       GSList *l;
2337       int i;
2338       GType type;
2339
2340       data->column_type_names = g_slist_reverse (data->column_type_names);
2341       column_types = g_new0 (GType, g_slist_length (data->column_type_names));
2342
2343       for (l = data->column_type_names, i = 0; l; l = l->next, i++)
2344         {
2345           type = gtk_builder_get_type_from_name (data->builder, l->data);
2346           if (type == G_TYPE_INVALID)
2347             {
2348               g_warning ("Unknown type %s specified in treemodel %s",
2349                          (const gchar*)l->data,
2350                          gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
2351               continue;
2352             }
2353           column_types[i] = type;
2354
2355           g_free (l->data);
2356         }
2357
2358       gtk_list_store_set_column_types (GTK_LIST_STORE (data->object), i,
2359                                        column_types);
2360
2361       g_free (column_types);
2362     }
2363   else if (strcmp (element_name, "col") == 0)
2364     data->is_data = FALSE;
2365   else if (strcmp (element_name, "data") == 0)
2366     ;
2367   else if (strcmp (element_name, "column") == 0)
2368     ;
2369   else
2370     g_set_error (error, data->error_quark, 0,
2371                  "Unknown end tag: %s", element_name);
2372 }
2373
2374 static void
2375 list_store_text (GMarkupParseContext *context,
2376                  const gchar         *text,
2377                  gsize                text_len,
2378                  gpointer             user_data,
2379                  GError             **error)
2380 {
2381   SubParserData *data = (SubParserData*)user_data;
2382   gint i;
2383   GError *tmp_error = NULL;
2384   gchar *string;
2385   ColInfo *info;
2386   
2387   if (!data->is_data)
2388     return;
2389
2390   i = data->row_column - 1;
2391   info = data->columns[i];
2392
2393   string = g_strndup (text, text_len);
2394   if (info->translatable && text_len)
2395     {
2396       gchar *translated;
2397
2398       /* FIXME: This will not use the domain set in the .ui file,
2399        * since the parser is not telling the builder about the domain.
2400        * However, it will work for gtk_builder_set_translation_domain() calls.
2401        */
2402       translated = _gtk_builder_parser_translate (data->domain,
2403                                                   info->context,
2404                                                   string);
2405       g_free (string);
2406       string = translated;
2407     }
2408
2409   if (!gtk_builder_value_from_string_type (data->builder,
2410                                            data->column_types[info->id],
2411                                            string,
2412                                            &data->values[i],
2413                                            &tmp_error))
2414     {
2415       g_set_error (error,
2416                    tmp_error->domain,
2417                    tmp_error->code,
2418                    "Could not convert '%s' to type %s: %s\n",
2419                    text, g_type_name (data->column_types[info->id]),
2420                    tmp_error->message);
2421       g_error_free (tmp_error);
2422     }
2423   g_free (string);
2424 }
2425
2426 static const GMarkupParser list_store_parser =
2427   {
2428     list_store_start_element,
2429     list_store_end_element,
2430     list_store_text
2431   };
2432
2433 static gboolean
2434 gtk_list_store_buildable_custom_tag_start (GtkBuildable  *buildable,
2435                                            GtkBuilder    *builder,
2436                                            GObject       *child,
2437                                            const gchar   *tagname,
2438                                            GMarkupParser *parser,
2439                                            gpointer      *data)
2440 {
2441   SubParserData *parser_data;
2442
2443   if (child)
2444     return FALSE;
2445
2446   if (strcmp (tagname, "columns") == 0)
2447     {
2448
2449       parser_data = g_slice_new0 (SubParserData);
2450       parser_data->builder = builder;
2451       parser_data->object = G_OBJECT (buildable);
2452       parser_data->column_type_names = NULL;
2453
2454       *parser = list_store_parser;
2455       *data = parser_data;
2456       return TRUE;
2457     }
2458   else if (strcmp (tagname, "data") == 0)
2459     {
2460       gint n_columns = gtk_list_store_get_n_columns (GTK_TREE_MODEL (buildable));
2461       if (n_columns == 0)
2462         g_error ("Cannot append data to an empty model");
2463
2464       parser_data = g_slice_new0 (SubParserData);
2465       parser_data->builder = builder;
2466       parser_data->object = G_OBJECT (buildable);
2467       parser_data->values = g_new0 (GValue, n_columns);
2468       parser_data->colids = g_new0 (gint, n_columns);
2469       parser_data->columns = g_new0 (ColInfo*, n_columns);
2470       parser_data->column_types = GTK_LIST_STORE (buildable)->priv->column_headers;
2471       parser_data->n_columns = n_columns;
2472       parser_data->last_row = 0;
2473       parser_data->error_quark = g_quark_from_static_string ("GtkListStore");
2474       parser_data->domain = gtk_builder_get_translation_domain (builder);
2475       
2476       *parser = list_store_parser;
2477       *data = parser_data;
2478       return TRUE;
2479     }
2480   else
2481     g_warning ("Unknown custom list store tag: %s", tagname);
2482   
2483   return FALSE;
2484 }
2485
2486 static void
2487 gtk_list_store_buildable_custom_tag_end (GtkBuildable *buildable,
2488                                          GtkBuilder   *builder,
2489                                          GObject      *child,
2490                                          const gchar  *tagname,
2491                                          gpointer     *data)
2492 {
2493   SubParserData *sub = (SubParserData*)data;
2494   
2495   if (strcmp (tagname, "columns") == 0)
2496     {
2497       g_slist_free (sub->column_type_names);
2498       g_slice_free (SubParserData, sub);
2499     }
2500   else if (strcmp (tagname, "data") == 0)
2501     {
2502       int i;
2503       for (i = 0; i < sub->n_columns; i++)
2504         {
2505           ColInfo *info = sub->columns[i];
2506           if (info)
2507             {
2508               g_free (info->context);
2509               g_slice_free (ColInfo, info);
2510             }
2511         }
2512       g_free (sub->colids);
2513       g_free (sub->columns);
2514       g_free (sub->values);
2515       g_slice_free (SubParserData, sub);
2516     }
2517   else
2518     g_warning ("Unknown custom list store tag: %s", tagname);
2519 }