gtktreestore.c 34.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* gtktreestore.c
 * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gtktreemodel.h"
#include "gtktreestore.h"
#include "gtktreedatalist.h"
23
#include "gtktreednd.h"
24
#include <string.h>
25
#include <gobject/gvaluecollector.h>
26
27
28

#define G_NODE(node) ((GNode *)node)

29
30
31
static void         gtk_tree_store_init            (GtkTreeStore      *tree_store);
static void         gtk_tree_store_class_init      (GtkTreeStoreClass *tree_store_class);
static void         gtk_tree_store_tree_model_init (GtkTreeModelIface *iface);
32
33
static void         gtk_tree_store_drag_source_init(GtkTreeDragSourceIface *iface);
static void         gtk_tree_store_drag_dest_init  (GtkTreeDragDestIface   *iface);
34
static guint        gtk_tree_store_get_flags       (GtkTreeModel      *tree_model);
35
static gint         gtk_tree_store_get_n_columns   (GtkTreeModel      *tree_model);
36
37
static GType        gtk_tree_store_get_column_type (GtkTreeModel      *tree_model,
						    gint               index);
38
39
static GtkTreePath *gtk_tree_store_get_path        (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter);
40
static void         gtk_tree_store_get_value       (GtkTreeModel      *tree_model,
41
42
43
44
45
46
47
48
49
50
51
52
53
54
						    GtkTreeIter       *iter,
						    gint               column,
						    GValue            *value);
static gboolean     gtk_tree_store_iter_next       (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter);
static gboolean     gtk_tree_store_iter_children   (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter,
						    GtkTreeIter       *parent);
static gboolean     gtk_tree_store_iter_has_child  (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter);
static gint         gtk_tree_store_iter_n_children (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter);
static gboolean     gtk_tree_store_iter_nth_child  (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter,
55
						    GtkTreeIter       *parent,
56
57
58
						    gint               n);
static gboolean     gtk_tree_store_iter_parent     (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter,
59
						    GtkTreeIter       *child);
60
61


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
static gboolean gtk_tree_store_drag_data_delete   (GtkTreeDragSource *drag_source,
                                                   GtkTreePath       *path);
static gboolean gtk_tree_store_drag_data_get      (GtkTreeDragSource *drag_source,
                                                   GtkTreePath       *path,
                                                   GtkSelectionData  *selection_data);
static gboolean gtk_tree_store_drag_data_received (GtkTreeDragDest   *drag_dest,
                                                   GtkTreePath       *dest,
                                                   GtkSelectionData  *selection_data);
static gboolean gtk_tree_store_row_drop_possible  (GtkTreeDragDest   *drag_dest,
                                                   GtkTreeModel      *src_model,
                                                   GtkTreePath       *src_path,
                                                   GtkTreePath       *dest_path);

static void     validate_gnode                    (GNode *node);

static inline void
validate_tree (GtkTreeStore *tree_store)
{
  if (gtk_debug_flags & GTK_DEBUG_TREE)
    {
      g_assert (G_NODE (tree_store->root)->parent == NULL);
      
      validate_gnode (G_NODE (tree_store->root));
    }
}

88
89
90
GtkType
gtk_tree_store_get_type (void)
{
91
  static GType tree_store_type = 0;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

  if (!tree_store_type)
    {
      static const GTypeInfo tree_store_info =
      {
        sizeof (GtkTreeStoreClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
        (GClassInitFunc) gtk_tree_store_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
        sizeof (GtkTreeStore),
	0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_tree_store_init
      };

108
109
110
111
112
113
114
      static const GInterfaceInfo tree_model_info =
      {
	(GInterfaceInitFunc) gtk_tree_store_tree_model_init,
	NULL,
	NULL
      };

115
116
117
118
119
120
121
122
123
124
125
126
127
128
      static const GInterfaceInfo drag_source_info =
      {
	(GInterfaceInitFunc) gtk_tree_store_drag_source_init,
	NULL,
	NULL
      };

      static const GInterfaceInfo drag_dest_info =
      {
	(GInterfaceInitFunc) gtk_tree_store_drag_dest_init,
	NULL,
	NULL
      };
      
129
      tree_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkTreeStore", &tree_store_info, 0);
130

131
132
133
      g_type_add_interface_static (tree_store_type,
				   GTK_TYPE_TREE_MODEL,
				   &tree_model_info);
134
135
136
137
138
139
140
141
142

      g_type_add_interface_static (tree_store_type,
				   GTK_TYPE_TREE_DRAG_SOURCE,
				   &drag_source_info);
      g_type_add_interface_static (tree_store_type,
				   GTK_TYPE_TREE_DRAG_DEST,
				   &drag_dest_info);

      
143
144
145
146
147
148
    }

  return tree_store_type;
}

static void
149
gtk_tree_store_class_init (GtkTreeStoreClass *tree_store_class)
150
{
151
152
153
154
  GObjectClass *object_class;

  object_class = (GObjectClass *) tree_store_class;

155
156
157
158
159
}

static void
gtk_tree_store_tree_model_init (GtkTreeModelIface *iface)
{
160
  iface->get_flags = gtk_tree_store_get_flags;
161
  iface->get_n_columns = gtk_tree_store_get_n_columns;
162
  iface->get_column_type = gtk_tree_store_get_column_type;
163
  iface->get_path = gtk_tree_store_get_path;
164
  iface->get_value = gtk_tree_store_get_value;
165
166
167
168
169
170
  iface->iter_next = gtk_tree_store_iter_next;
  iface->iter_children = gtk_tree_store_iter_children;
  iface->iter_has_child = gtk_tree_store_iter_has_child;
  iface->iter_n_children = gtk_tree_store_iter_n_children;
  iface->iter_nth_child = gtk_tree_store_iter_nth_child;
  iface->iter_parent = gtk_tree_store_iter_parent;
171
172
}

173
174
175
176
177
178
179
180
181
182
183
184
185
186
static void
gtk_tree_store_drag_source_init (GtkTreeDragSourceIface *iface)
{
  iface->drag_data_delete = gtk_tree_store_drag_data_delete;
  iface->drag_data_get = gtk_tree_store_drag_data_get;
}

static void
gtk_tree_store_drag_dest_init   (GtkTreeDragDestIface   *iface)
{
  iface->drag_data_received = gtk_tree_store_drag_data_received;
  iface->row_drop_possible = gtk_tree_store_row_drop_possible;
}

187
188
189
static void
gtk_tree_store_init (GtkTreeStore *tree_store)
{
190
  tree_store->root = g_node_new (NULL);
191
  tree_store->stamp = g_random_int ();
192
193
}

194
GtkTreeStore *
195
196
gtk_tree_store_new (void)
{
197
  return GTK_TREE_STORE (g_object_new (gtk_tree_store_get_type (), NULL));
198
199
}

200
GtkTreeStore *
201
202
gtk_tree_store_new_with_types (gint n_columns,
			       ...)
203
{
204
  GtkTreeStore *retval;
205
206
207
208
209
210
  va_list args;
  gint i;

  g_return_val_if_fail (n_columns > 0, NULL);

  retval = gtk_tree_store_new ();
211
  gtk_tree_store_set_n_columns (retval, n_columns);
212
213

  va_start (args, n_columns);
214

215
  for (i = 0; i < n_columns; i++)
216
217
218
219
220
221
222
223
224
225
    {
      GType type = va_arg (args, GType);
      if (! _gtk_tree_data_list_check_type (type))
	{
	  g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n", G_STRLOC, g_type_name (type));
	  g_object_unref (G_OBJECT (retval));
	  return NULL;
	}
      gtk_tree_store_set_column_type (retval, i, type);
    }
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  va_end (args);

  return retval;
}

void
gtk_tree_store_set_n_columns (GtkTreeStore *tree_store,
			      gint          n_columns)
{
  GType *new_columns;

  g_return_if_fail (tree_store != NULL);
  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));

  if (tree_store->n_columns == n_columns)
    return;

  new_columns = g_new0 (GType, n_columns);
  if (tree_store->column_headers)
    {
      /* copy the old header orders over */
      if (n_columns >= tree_store->n_columns)
	memcpy (new_columns, tree_store->column_headers, tree_store->n_columns * sizeof (gchar *));
      else
	memcpy (new_columns, tree_store->column_headers, n_columns * sizeof (GType));

      g_free (tree_store->column_headers);
    }

  tree_store->column_headers = new_columns;
  tree_store->n_columns = n_columns;
}

259
260
261
262
263
264
265
266
267
268
269
270
/**
 * gtk_tree_store_set_column_type:
 * @tree_store: a #GtkTreeStore
 * @column: column number
 * @type: type of the data to be stored in @column
 * 
 * Supported types include: %G_TYPE_UINT, %G_TYPE_INT, %G_TYPE_UCHAR,
 * %G_TYPE_CHAR, %G_TYPE_BOOLEAN, %G_TYPE_POINTER, %G_TYPE_FLOAT,
 * %G_TYPE_DOUBLE, %G_TYPE_STRING, %G_TYPE_OBJECT, and %G_TYPE_BOXED, along with
 * subclasses of those types such as %GDK_TYPE_PIXBUF.
 * 
 **/
271
272
273
274
275
276
277
278
void
gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
				gint          column,
				GType         type)
{
  g_return_if_fail (tree_store != NULL);
  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
  g_return_if_fail (column >=0 && column < tree_store->n_columns);
279
280
281
282
283
  if (!_gtk_tree_data_list_check_type (type))
    {
      g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n", G_STRLOC, g_type_name (type));
      return;
    }
284
285
286
287
288
  tree_store->column_headers[column] = type;
}

/* fulfill the GtkTreeModel requirements */
/* NOTE: GtkTreeStore::root is a GNode, that acts as the parent node.  However,
289
 * it is not visible to the tree or to the user., and the path "0" refers to the
290
291
 * first child of GtkTreeStore::root.
 */
292
293
294
295
296
297
298
299
300
301


static guint
gtk_tree_store_get_flags (GtkTreeModel *tree_model)
{
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), 0);

  return GTK_TREE_MODEL_ITERS_PERSIST;
}

302
303
304
305
306
307
308
309
static gint
gtk_tree_store_get_n_columns (GtkTreeModel *tree_model)
{
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), 0);

  return GTK_TREE_STORE (tree_model)->n_columns;
}

310
311
312
313
314
315
316
317
318
319
320
static GType
gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
				gint          index)
{
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), G_TYPE_INVALID);
  g_return_val_if_fail (index < GTK_TREE_STORE (tree_model)->n_columns &&
			index >= 0, G_TYPE_INVALID);

  return GTK_TREE_STORE (tree_model)->column_headers[index];
}

321
322
static GtkTreePath *
gtk_tree_store_get_path (GtkTreeModel *tree_model,
323
			 GtkTreeIter  *iter)
324
325
326
327
{
  GtkTreePath *retval;
  GNode *tmp_node;
  gint i = 0;
328
  
329
  g_return_val_if_fail (tree_model != NULL, NULL);
330
331
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), NULL);
  g_return_val_if_fail (iter != NULL, NULL);
332
  g_return_val_if_fail (iter->user_data != NULL, NULL);
333

334
  validate_tree ((GtkTreeStore*)tree_model);
335

336
337
  g_assert (G_NODE (iter->user_data)->parent != NULL);
  
Havoc Pennington's avatar
Havoc Pennington committed
338
  if (G_NODE (iter->user_data)->parent == G_NODE (GTK_TREE_STORE (tree_model)->root))
339
340
341
342
343
344
    {
      retval = gtk_tree_path_new ();
      tmp_node = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
    }
  else
    {
345
      GtkTreeIter tmp_iter = *iter;
346
347
      
      tmp_iter.user_data = G_NODE (iter->user_data)->parent;
348

349
      retval = gtk_tree_store_get_path (tree_model,
350
					&tmp_iter);
Havoc Pennington's avatar
Havoc Pennington committed
351
      tmp_node = G_NODE (iter->user_data)->parent->children;
352
353
354
355
    }

  if (retval == NULL)
    return NULL;
356

357
358
359
360
361
362
363
364
  if (tmp_node == NULL)
    {
      gtk_tree_path_free (retval);
      return NULL;
    }

  for (; tmp_node; tmp_node = tmp_node->next)
    {
Havoc Pennington's avatar
Havoc Pennington committed
365
      if (tmp_node == G_NODE (iter->user_data))
366
367
368
	break;
      i++;
    }
369

370
371
372
  if (tmp_node == NULL)
    {
      /* We couldn't find node, meaning it's prolly not ours */
373
      /* Perhaps I should do a g_return_if_fail here. */
374
375
376
377
378
379
380
381
382
383
384
      gtk_tree_path_free (retval);
      return NULL;
    }

  gtk_tree_path_append_index (retval, i);

  return retval;
}


static void
385
386
387
388
gtk_tree_store_get_value (GtkTreeModel *tree_model,
			  GtkTreeIter  *iter,
			  gint          column,
			  GValue       *value)
389
390
391
392
393
394
{
  GtkTreeDataList *list;
  gint tmp_column = column;

  g_return_if_fail (tree_model != NULL);
  g_return_if_fail (GTK_IS_TREE_STORE (tree_model));
395
  g_return_if_fail (iter != NULL);
396
397
  g_return_if_fail (column < GTK_TREE_STORE (tree_model)->n_columns);

Havoc Pennington's avatar
Havoc Pennington committed
398
  list = G_NODE (iter->user_data)->data;
399
400
401
402

  while (tmp_column-- > 0 && list)
    list = list->next;

403
404
  if (list)
    {
405
406
407
      _gtk_tree_data_list_node_to_value (list,
					 GTK_TREE_STORE (tree_model)->column_headers[column],
					 value);
408
409
410
411
412
413
    }
  else
    {
      /* We want to return an initialized but empty (default) value */
      g_value_init (value, GTK_TREE_STORE (tree_model)->column_headers[column]);
    }
414
415
416
}

static gboolean
417
418
gtk_tree_store_iter_next (GtkTreeModel  *tree_model,
			  GtkTreeIter   *iter)
419
{
420
421
422
423
424
425
426
427
  g_return_val_if_fail (iter->user_data != NULL, FALSE);
  
  if (G_NODE (iter->user_data)->next)
    {
      iter->user_data = G_NODE (iter->user_data)->next;
      return TRUE;
    }
  else
428
429
430
    return FALSE;
}

431
432
433
434
static gboolean
gtk_tree_store_iter_children (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter,
			      GtkTreeIter  *parent)
435
{
436
437
438
439
  GNode *children;

  g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
  
440
  if (parent)
441
    children = G_NODE (parent->user_data)->children;
442
  else
443
    children = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
444

445
446
447
448
449
450
451
452
  if (children)
    {
      iter->stamp = GTK_TREE_STORE (tree_model)->stamp;
      iter->user_data = children;
      return TRUE;
    }
  else
    return FALSE;
453
454
455
}

static gboolean
456
457
gtk_tree_store_iter_has_child (GtkTreeModel *tree_model,
			       GtkTreeIter  *iter)
458
{
459
460
461
  g_return_val_if_fail (tree_model != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);
462
  g_return_val_if_fail (iter->user_data != NULL, FALSE);
463

Havoc Pennington's avatar
Havoc Pennington committed
464
  return G_NODE (iter->user_data)->children != NULL;
465
466
467
}

static gint
468
469
gtk_tree_store_iter_n_children (GtkTreeModel *tree_model,
				GtkTreeIter  *iter)
470
{
471
  GNode *node;
472
473
  gint i = 0;

474
475
  g_return_val_if_fail (tree_model != NULL, 0);
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), 0);
476
477
478
  g_return_val_if_fail (iter != NULL, FALSE);
  g_return_val_if_fail (iter->user_data != NULL, FALSE);
  
479
480
481
  if (iter == NULL)
    node = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
  else
Havoc Pennington's avatar
Havoc Pennington committed
482
    node = G_NODE (iter->user_data)->children;
483

484
  while (node)
485
486
    {
      i++;
487
      node = node->next;
488
489
490
491
492
    }

  return i;
}

493
494
495
496
static gboolean
gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
			       GtkTreeIter  *iter,
			       GtkTreeIter  *parent,
497
498
			       gint          n)
{
499
  GNode *parent_node;
500
  GNode *child;
501

502
503
  g_return_val_if_fail (tree_model != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), FALSE);
504
  g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
505

506
507
508
  if (parent == NULL)
    parent_node = GTK_TREE_STORE (tree_model)->root;
  else
Havoc Pennington's avatar
Havoc Pennington committed
509
    parent_node = parent->user_data;
510

511
  child = g_node_nth_child (parent_node, n);
512

513
514
515
516
517
518
  if (child)
    {
      iter->user_data = child;
      iter->stamp = GTK_TREE_STORE (tree_model)->stamp;
      return TRUE;
    }
519
  else
520
    return FALSE;
521
522
}

523
524
525
526
static gboolean
gtk_tree_store_iter_parent (GtkTreeModel *tree_model,
			    GtkTreeIter  *iter,
			    GtkTreeIter  *child)
527
{
528
  GNode *parent;
529
  
530
531
532
533
534
535
  g_return_val_if_fail (iter != NULL, FALSE);
  g_return_val_if_fail (iter->user_data != NULL, FALSE);

  parent = G_NODE (child->user_data)->parent;

  g_assert (parent != NULL);  
536

537
  if (parent != GTK_TREE_STORE (tree_model)->root)
538
    {
539
540
541
      iter->user_data = parent;
      iter->stamp = GTK_TREE_STORE (tree_model)->stamp;
      return TRUE;
542
    }
543
544
  else
    return FALSE;
545
546
547
548
549
550
551
}

/*
 * This is a somewhat inelegant function that does a lot of list
 * manipulations on it's own.
 */
void
552
553
554
555
gtk_tree_store_set_cell (GtkTreeStore *tree_store,
			 GtkTreeIter  *iter,
			 gint          column,
			 GValue       *value)
556
557
558
{
  GtkTreeDataList *list;
  GtkTreeDataList *prev;
559
  GtkTreePath *path = NULL;
560
561
  GValue real_value = {0, };
  gboolean converted = FALSE;
562
563
564
565

  g_return_if_fail (tree_store != NULL);
  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
  g_return_if_fail (column >= 0 && column < tree_store->n_columns);
566
567
  g_return_if_fail (value != NULL);

568
  if (! g_type_is_a (G_VALUE_TYPE (value), tree_store->column_headers[column]))
569
    {
570
571
      if (! (g_value_type_compatible (G_VALUE_TYPE (value), tree_store->column_headers[column]) &&
	     g_value_type_compatible (tree_store->column_headers[column], G_VALUE_TYPE (value))))
572
573
574
575
	{
	  g_warning ("%s: Unable to convert from %s to %s\n",
		     G_STRLOC,
		     g_type_name (G_VALUE_TYPE (value)),
576
		     g_type_name (tree_store->column_headers[column]));
577
578
579
580
581
582
583
	  return;
	}
      if (!g_value_transform (value, &real_value))
	{
	  g_warning ("%s: Unable to make conversion from %s to %s\n",
		     G_STRLOC,
		     g_type_name (G_VALUE_TYPE (value)),
584
		     g_type_name (tree_store->column_headers[column]));
585
586
587
588
589
	  g_value_unset (&real_value);
	  return;
	}
      converted = TRUE;
    }
590

Havoc Pennington's avatar
Havoc Pennington committed
591
  prev = list = G_NODE (iter->user_data)->data;
592

593
594
  path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);

595
596
597
598
  while (list != NULL)
    {
      if (column == 0)
	{
599
600
601
602
	  if (converted)
	    _gtk_tree_data_list_value_to_node (list, &real_value);
	  else
	    _gtk_tree_data_list_value_to_node (list, value);
603
604
	  gtk_tree_model_changed (GTK_TREE_MODEL (tree_store), path, iter);
	  gtk_tree_path_free (path);
605
606
	  if (converted)
	    g_value_unset (&real_value);
607
608
609
610
611
612
613
614
	  return;
	}

      column--;
      prev = list;
      list = list->next;
    }

Havoc Pennington's avatar
Havoc Pennington committed
615
  if (G_NODE (iter->user_data)->data == NULL)
616
    {
Havoc Pennington's avatar
Havoc Pennington committed
617
      G_NODE (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
618
619
620
621
      list->next = NULL;
    }
  else
    {
622
      list = prev->next = _gtk_tree_data_list_alloc ();
623
624
625
626
627
      list->next = NULL;
    }

  while (column != 0)
    {
628
      list->next = _gtk_tree_data_list_alloc ();
629
630
631
632
      list = list->next;
      list->next = NULL;
      column --;
    }
633
  if (converted)
634
    _gtk_tree_data_list_value_to_node (list, &real_value);
635
636
  else
    _gtk_tree_data_list_value_to_node (list, value);
637
638
  gtk_tree_model_changed (GTK_TREE_MODEL (tree_store), path, iter);
  gtk_tree_path_free (path);
639
640
  if (converted)
    g_value_unset (&real_value);
641
642
}

643
644
645
646
647
648
649
650
651
652
/**
 * gtk_tree_store_set_valist:
 * @tree_store: a #GtkTreeStore
 * @iter: row to set data for
 * @var_args: va_list of column/value pairs
 *
 * See gtk_tree_store_set(); this version takes a va_list for
 * use by language bindings.
 * 
 **/
653
void
Havoc Pennington's avatar
Havoc Pennington committed
654
655
656
gtk_tree_store_set_valist (GtkTreeStore *tree_store,
                           GtkTreeIter  *iter,
                           va_list	var_args)
657
658
659
660
661
662
663
{
  gint column;

  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));

  column = va_arg (var_args, gint);

Havoc Pennington's avatar
Havoc Pennington committed
664
  while (column != -1)
665
666
667
668
669
670
    {
      GValue value = { 0, };
      gchar *error = NULL;

      if (column >= tree_store->n_columns)
	{
Havoc Pennington's avatar
Havoc Pennington committed
671
	  g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
672
673
674
675
	  break;
	}
      g_value_init (&value, tree_store->column_headers[column]);

676
      G_VALUE_COLLECT (&value, var_args, 0, &error);
677
678
679
680
681
682
683
684
685
686
687
      if (error)
	{
	  g_warning ("%s: %s", G_STRLOC, error);
	  g_free (error);

 	  /* we purposely leak the value here, it might not be
	   * in a sane state if an error condition occoured
	   */
	  break;
	}

688
689
690
691
      gtk_tree_store_set_cell (tree_store,
			       iter,
			       column,
			       &value);
692
693
694
695
696
697
698

      g_value_unset (&value);

      column = va_arg (var_args, gint);
    }
}

Havoc Pennington's avatar
Havoc Pennington committed
699
700
701
702
703
704
705
706
707
708
709
710
711
/**
 * gtk_tree_store_set:
 * @tree_store: a #GtkTreeStore
 * @iter: row iterator
 * @Varargs: pairs of column number and value, terminated with -1
 * 
 * Sets the value of one or more cells in the row referenced by @iter.
 * The variable argument list should contain integer column numbers,
 * each column number followed by the value to be set. For example,
 * The list is terminated by a -1. For example, to set column 0 with type
 * %G_TYPE_STRING to "Foo", you would write gtk_tree_store_set (store, iter,
 * 0, "Foo", -1).
 **/
712
void
713
714
715
gtk_tree_store_set (GtkTreeStore *tree_store,
		    GtkTreeIter  *iter,
		    ...)
716
717
718
719
720
721
{
  va_list var_args;

  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));

  va_start (var_args, iter);
Havoc Pennington's avatar
Havoc Pennington committed
722
  gtk_tree_store_set_valist (tree_store, iter, var_args);
723
724
725
726
  va_end (var_args);
}

void
727
728
gtk_tree_store_remove (GtkTreeStore *model,
		       GtkTreeIter  *iter)
729
730
{
  GtkTreePath *path;
731
  GtkTreeIter new_iter = {0,};
732
733
734
735
736
  GNode *parent;

  g_return_if_fail (model != NULL);
  g_return_if_fail (GTK_IS_TREE_STORE (model));

Havoc Pennington's avatar
Havoc Pennington committed
737
  parent = G_NODE (iter->user_data)->parent;
738

739
740
  g_assert (parent != NULL);
  
Havoc Pennington's avatar
Havoc Pennington committed
741
742
  if (G_NODE (iter->user_data)->data)
    _gtk_tree_data_list_free ((GtkTreeDataList *) G_NODE (iter->user_data)->data,
743
744
			      model->column_headers);

745
  path = gtk_tree_store_get_path (GTK_TREE_MODEL (model), iter);
Havoc Pennington's avatar
Havoc Pennington committed
746
  g_node_destroy (G_NODE (iter->user_data));
747
748

  model->stamp++;
749
750
  gtk_tree_model_deleted (GTK_TREE_MODEL (model), path);

751
752
753
  if (parent != G_NODE (model->root) && parent->children == NULL)
    {
      gtk_tree_path_up (path);
754
755
756

      new_iter.stamp = model->stamp;
      new_iter.user_data = parent;
757
      gtk_tree_model_has_child_toggled (GTK_TREE_MODEL (model), path, &new_iter);
758
759
760
761
    }
  gtk_tree_path_free (path);
}

762
void
763
764
765
766
gtk_tree_store_insert (GtkTreeStore *model,
		       GtkTreeIter  *iter,
		       GtkTreeIter  *parent,
		       gint          position)
767
768
{
  GtkTreePath *path;
769
  GNode *parent_node;
770
  
771
772
  g_return_if_fail (model != NULL);
  g_return_if_fail (GTK_IS_TREE_STORE (model));
773

774
775
776
777
  if (parent)
    parent_node = parent->user_data;
  else
    parent_node = model->root;
778

779
  iter->stamp = model->stamp;
Havoc Pennington's avatar
Havoc Pennington committed
780
  iter->user_data = g_node_new (NULL);
781
  g_node_insert (parent_node, position, G_NODE (iter->user_data));
782
783
  
  path = gtk_tree_store_get_path (GTK_TREE_MODEL (model), iter);
784
785
  gtk_tree_model_inserted (GTK_TREE_MODEL (model), path, iter);

786
  gtk_tree_path_free (path);
787
788

  validate_tree ((GtkTreeStore*)model);
789
790
}

791
void
792
793
794
795
gtk_tree_store_insert_before (GtkTreeStore *model,
			      GtkTreeIter  *iter,
			      GtkTreeIter  *parent,
			      GtkTreeIter  *sibling)
796
797
{
  GtkTreePath *path;
798
  GNode *parent_node = NULL;
799
800
  GNode *new_node;
  
801
802
803
  g_return_if_fail (model != NULL);
  g_return_if_fail (GTK_IS_TREE_STORE (model));
  g_return_if_fail (iter != NULL);
804

805
  new_node = g_node_new (NULL);  
806

807
808
809
  if (parent == NULL && sibling == NULL)
    parent_node = model->root;
  else if (parent == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
810
    parent_node = G_NODE (sibling->user_data)->parent;
811
  else if (sibling == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
812
    parent_node = G_NODE (parent->user_data);
813
814
815
816
817
818
  else
    {
      g_return_if_fail (G_NODE (sibling->user_data)->parent ==
                        G_NODE (parent->user_data));
      parent_node = G_NODE (parent->user_data);
    }
819

820
  g_node_insert_before (parent_node,
Havoc Pennington's avatar
Havoc Pennington committed
821
			sibling ? G_NODE (sibling->user_data) : NULL,
822
                        new_node);
823

824
825
826
  iter->stamp = model->stamp;
  iter->user_data = new_node;
  
827
  path = gtk_tree_store_get_path (GTK_TREE_MODEL (model), iter);
828
829
  gtk_tree_model_inserted (GTK_TREE_MODEL (model), path, iter);

830
  gtk_tree_path_free (path);
831
832

  validate_tree ((GtkTreeStore*)model);
833
834
}

835
void
836
837
838
839
gtk_tree_store_insert_after (GtkTreeStore *model,
			     GtkTreeIter  *iter,
			     GtkTreeIter  *parent,
			     GtkTreeIter  *sibling)
840
841
{
  GtkTreePath *path;
842
  GNode *parent_node;
843
844
  GNode *new_node;
  
845
846
847
  g_return_if_fail (model != NULL);
  g_return_if_fail (GTK_IS_TREE_STORE (model));
  g_return_if_fail (iter != NULL);
848

849
  new_node = g_node_new (NULL);
850

851
852
853
  if (parent == NULL && sibling == NULL)
    parent_node = model->root;
  else if (parent == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
854
    parent_node = G_NODE (sibling->user_data)->parent;
855
  else if (sibling == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
856
    parent_node = G_NODE (parent->user_data);
857
858
859
860
861
862
  else
    {
      g_return_if_fail (G_NODE (sibling->user_data)->parent ==
                        G_NODE (parent->user_data));
      parent_node = G_NODE (parent->user_data);
    }
863

864
  
865
  g_node_insert_after (parent_node,
Havoc Pennington's avatar
Havoc Pennington committed
866
		       sibling ? G_NODE (sibling->user_data) : NULL,
867
868
869
870
871
                       new_node);
  
  iter->stamp = model->stamp;
  iter->user_data = new_node;
  
872
  path = gtk_tree_store_get_path (GTK_TREE_MODEL (model), iter);
873
874
  gtk_tree_model_inserted (GTK_TREE_MODEL (model), path, iter);

875
  gtk_tree_path_free (path);
876
877

  validate_tree ((GtkTreeStore*)model);
878
879
}

880
void
881
882
883
gtk_tree_store_prepend (GtkTreeStore *model,
			GtkTreeIter  *iter,
			GtkTreeIter  *parent)
884
{
885
  GNode *parent_node;
886
  
887
888
889
  g_return_if_fail (model != NULL);
  g_return_if_fail (GTK_IS_TREE_STORE (model));
  g_return_if_fail (iter != NULL);
890
891

  if (parent == NULL)
892
893
    parent_node = model->root;
  else
Havoc Pennington's avatar
Havoc Pennington committed
894
    parent_node = parent->user_data;
895
  
896
  if (parent_node->children == NULL)
897
898
    {
      GtkTreePath *path;
899
900
901
902
903
904
      
      iter->stamp = model->stamp;
      iter->user_data = g_node_new (NULL);
      
      g_node_prepend (parent_node, iter->user_data);
      
905
      if (parent_node != model->root)
906
907
	{
	  path = gtk_tree_store_get_path (GTK_TREE_MODEL (model), parent);
908
	  gtk_tree_model_has_child_toggled (GTK_TREE_MODEL (model), path, parent);
909
	  gtk_tree_path_append_index (path, 0);
910
911
912
	}
      else
	{
913
	  path = gtk_tree_store_get_path (GTK_TREE_MODEL (model), iter);
914
	}
915
      gtk_tree_model_inserted (GTK_TREE_MODEL (model), path, iter);
916
917
918
919
      gtk_tree_path_free (path);
    }
  else
    {
920
      gtk_tree_store_insert_after (model, iter, parent, NULL);
921
    }
922
923

  validate_tree ((GtkTreeStore*)model);
924
925
}

926
void
927
928
929
gtk_tree_store_append (GtkTreeStore *model,
		       GtkTreeIter  *iter,
		       GtkTreeIter  *parent)
930
{
931
932
933
934
935
  GNode *parent_node;

  g_return_if_fail (model != NULL);
  g_return_if_fail (GTK_IS_TREE_STORE (model));
  g_return_if_fail (iter != NULL);
936
  
937
  if (parent == NULL)
938
939
    parent_node = model->root;
  else
Havoc Pennington's avatar
Havoc Pennington committed
940
    parent_node = parent->user_data;
941

942
  if (parent_node->children == NULL)
943
944
    {
      GtkTreePath *path;
945

946
947
948
      iter->stamp = model->stamp;
      iter->user_data = g_node_new (NULL);
      
Havoc Pennington's avatar
Havoc Pennington committed
949
      g_node_append (parent_node, G_NODE (iter->user_data));
950
951

      if (parent_node != model->root)
952
953
	{
	  path = gtk_tree_store_get_path (GTK_TREE_MODEL (model), parent);
954
	  gtk_tree_model_has_child_toggled (GTK_TREE_MODEL (model), path, parent);
955
	  gtk_tree_path_append_index (path, 0);
956
957
958
	}
      else
	{
959
	  path = gtk_tree_store_get_path (GTK_TREE_MODEL (model), iter);
960
	}
961
      
962
      gtk_tree_model_inserted (GTK_TREE_MODEL (model), path, iter);
963
964
965
966
      gtk_tree_path_free (path);
    }
  else
    {
967
      gtk_tree_store_insert_before (model, iter, parent, NULL);
968
    }
969
970

  validate_tree ((GtkTreeStore*)model);
971
972
973
}

gboolean
974
975
976
gtk_tree_store_is_ancestor (GtkTreeStore *model,
			    GtkTreeIter  *iter,
			    GtkTreeIter  *descendant)
977
978
979
{
  g_return_val_if_fail (model != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TREE_STORE (model), FALSE);
980
  g_return_val_if_fail (iter != NULL, FALSE);
981
982
  g_return_val_if_fail (descendant != NULL, FALSE);

Havoc Pennington's avatar
Havoc Pennington committed
983
984
  return g_node_is_ancestor (G_NODE (iter->user_data),
			     G_NODE (descendant->user_data));
985
986
987
988
}


gint
989
990
gtk_tree_store_iter_depth (GtkTreeStore *model,
			   GtkTreeIter  *iter)
991
992
993
{
  g_return_val_if_fail (model != NULL, 0);
  g_return_val_if_fail (GTK_IS_TREE_STORE (model), 0);
994
  g_return_val_if_fail (iter != NULL, 0);
995

Havoc Pennington's avatar
Havoc Pennington committed
996
  return g_node_depth (G_NODE (iter->user_data)) - 1;
997
}
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060

/* DND */



static gboolean
gtk_tree_store_drag_data_delete (GtkTreeDragSource *drag_source,
                                 GtkTreePath       *path)
{
  GtkTreeIter iter;

  g_return_val_if_fail (GTK_IS_TREE_STORE (drag_source), FALSE);
  
  if (gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_source),
                               &iter,
                               path))
    {
      gtk_tree_store_remove (GTK_TREE_STORE (drag_source),
                             &iter);
      return TRUE;
    }
  else
    {
      return FALSE;
    }
}

static gboolean
gtk_tree_store_drag_data_get (GtkTreeDragSource *drag_source,
                              GtkTreePath       *path,
                              GtkSelectionData  *selection_data)
{
  g_return_val_if_fail (GTK_IS_TREE_STORE (drag_source), FALSE);

  /* Note that we don't need to handle the GTK_TREE_MODEL_ROW
   * target, because the default handler does it for us, but
   * we do anyway for the convenience of someone maybe overriding the
   * default handler.
   */

  if (gtk_selection_data_set_tree_row (selection_data,
                                       GTK_TREE_MODEL (drag_source),
                                       path))
    {
      return TRUE;
    }
  else
    {
      /* FIXME handle text targets at least. */
    }

  return FALSE;
}

static void
copy_node_data (GtkTreeStore *tree_store,
                GtkTreeIter  *src_iter,
                GtkTreeIter  *dest_iter)
{
  GtkTreeDataList *dl = G_NODE (src_iter->user_data)->data;
  GtkTreeDataList *copy_head = NULL;
  GtkTreeDataList *copy_prev = NULL;
  GtkTreeDataList *copy_iter = NULL;
1061
  GtkTreePath *path;
1062
1063
1064
1065
1066
  gint col;
  
  col = 0;
  while (dl)
    {
1067
      copy_iter = _gtk_tree_data_list_node_copy (dl, tree_store->column_headers[col]);
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079

      if (copy_head == NULL)
        copy_head = copy_iter;

      if (copy_prev)
        copy_prev->next = copy_iter;

      copy_prev = copy_iter;

      dl = dl->next;
      ++col;
    }
1080

1081
1082
  G_NODE (dest_iter->user_data)->data = copy_head;

1083
  path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), dest_iter);
1084
1085
  gtk_tree_model_changed (GTK_TREE_MODEL (tree_store), path, dest_iter);
  gtk_tree_path_free (path);
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
}

static void
recursive_node_copy (GtkTreeStore *tree_store,
                     GtkTreeIter  *src_iter,
                     GtkTreeIter  *dest_iter)
{
  GtkTreeIter child;
  GtkTreeModel *model;

  model = GTK_TREE_MODEL (tree_store);
  
  copy_node_data (tree_store, src_iter, dest_iter);

  if (gtk_tree_model_iter_children (model,
                                    &child,
                                    src_iter))
    {
      /* Need to create children and recurse. Note our
       * dependence on persistent iterators here.
       */
      do
        {
          GtkTreeIter copy;

          /* Gee, a really slow algorithm... ;-) FIXME */
          gtk_tree_store_append (tree_store,
                                 &copy,
                                 dest_iter);
          
          recursive_node_copy (tree_store, &child, &copy);
        }
      while (gtk_tree_model_iter_next (model, &child));
    }
}

static gboolean
gtk_tree_store_drag_data_received (GtkTreeDragDest   *drag_dest,
                                   GtkTreePath       *dest,
                                   GtkSelectionData  *selection_data)
{
  GtkTreeModel *tree_model;
  GtkTreeStore *tree_store;
  GtkTreeModel *src_model = NULL;
  GtkTreePath *src_path = NULL;
  gboolean retval = FALSE;
  
  g_return_val_if_fail (GTK_IS_TREE_STORE (drag_dest), FALSE);

  tree_model = GTK_TREE_MODEL (drag_dest);
  tree_store = GTK_TREE_STORE (drag_dest);

  validate_tree (tree_store);
  
  if (gtk_selection_data_get_tree_row (selection_data,
                                       &src_model,
                                       &src_path) &&
      src_model == tree_model)
    {
      /* Copy the given row to a new position */
      GtkTreeIter src_iter;
      GtkTreeIter dest_iter;
      GtkTreePath *prev;
      
      if (!gtk_tree_model_get_iter (src_model,
                                    &src_iter,
                                    src_path))
        {
          goto out;
        }

      /* Get the path to insert _after_ (dest is the path to insert _before_) */
      prev = gtk_tree_path_copy (dest);
      
      if (!gtk_tree_path_prev (prev))
        {
          GtkTreeIter dest_parent;
          GtkTreePath *parent;
          GtkTreeIter *dest_parent_p;
          
          /* dest was the first spot at the current depth; which means
           * we are supposed to prepend.
           */
          
          /* Get the parent, NULL if parent is the root */
          dest_parent_p = NULL;
          parent = gtk_tree_path_copy (dest);
          if (gtk_tree_path_up (parent))
            {
              gtk_tree_model_get_iter (tree_model,
                                       &dest_parent,
                                       parent);
              dest_parent_p = &dest_parent;
            }
          gtk_tree_path_free (parent);
          parent = NULL;
          
          gtk_tree_store_prepend (GTK_TREE_STORE (tree_model),
                                  &dest_iter,
                                  dest_parent_p);
          
          retval = TRUE;
        }
      else
        {
          if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model),
                                       &dest_iter,
                                       prev))
            {
              GtkTreeIter tmp_iter = dest_iter;
              gtk_tree_store_insert_after (GTK_TREE_STORE (tree_model),
                                           &dest_iter,
                                           NULL,
                                           &tmp_iter);
              retval = TRUE;

            }
        }

      gtk_tree_path_free (prev);
      
      /* If we succeeded in creating dest_iter, walk src_iter tree branch,
       * duplicating it below dest_iter.
       */
      
      if (retval)
        {
          recursive_node_copy (tree_store,
                               &src_iter,
                               &dest_iter);
        }
    }
  else
    {
      /* FIXME maybe add some data targets eventually, or handle text
       * targets in the simple case.
       */
Havoc Pennington's avatar
Havoc Pennington committed
1223

1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
    }

 out:
  
  if (src_path)
    gtk_tree_path_free (src_path);
  
  return retval;  
}

static gboolean
gtk_tree_store_row_drop_possible (GtkTreeDragDest *drag_dest,
                                  GtkTreeModel    *src_model,
                                  GtkTreePath     *src_path,
                                  GtkTreePath     *dest_path)
{
  /* can only drag to ourselves */
  if (src_model != GTK_TREE_MODEL (drag_dest))
    return FALSE;
  
  /* Can't drop into ourself. */
  if (gtk_tree_path_is_ancestor (src_path,
                                 dest_path))
    return FALSE;

  /* Can't drop if dest_path's parent doesn't exist */
  {
    GtkTreeIter iter;
    GtkTreePath *tmp = gtk_tree_path_copy (dest_path);

    /* if we can't go up, we know the parent exists, the root
     * always exists.
     */
    if (gtk_tree_path_up (tmp))
      {
        if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (drag_dest),
                                      &iter, tmp))
          {
            if (tmp)
              gtk_tree_path_free (tmp);
            return FALSE;
          }
      }
    
    if (tmp)
      gtk_tree_path_free (tmp);
  }
  
  /* Can otherwise drop anywhere. */
  return TRUE;
}
     
static void
validate_gnode (GNode* node)
{
  GNode *iter;
  
  iter = node->children;
  while (iter != NULL)
    {
      g_assert (iter->parent == node);
      if (iter->prev)
        g_assert (iter->prev->next == iter);
      validate_gnode (iter);
      iter = iter->next;
    }
}