gtktreestore.c 87.8 KB
Newer Older
1
/* gtktreestore.c
Manish Singh's avatar
Manish Singh committed
2
 * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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.
 */

20
#include <config.h>
Manish Singh's avatar
Manish Singh committed
21 22
#include <string.h>
#include <gobject/gvaluecollector.h>
23 24 25
#include "gtktreemodel.h"
#include "gtktreestore.h"
#include "gtktreedatalist.h"
26
#include "gtktreednd.h"
Johan Dahlin's avatar
Johan Dahlin committed
27
#include "gtkbuildable.h"
Matthias Clasen's avatar
Matthias Clasen committed
28
#include "gtkintl.h"
29
#include "gtkalias.h"
30 31

#define G_NODE(node) ((GNode *)node)
32 33
#define GTK_TREE_STORE_IS_SORTED(tree) (((GtkTreeStore*)(tree))->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
#define VALID_ITER(iter, tree_store) ((iter)!= NULL && (iter)->user_data != NULL && ((GtkTreeStore*)(tree_store))->stamp == (iter)->stamp)
34

35
static void         gtk_tree_store_tree_model_init (GtkTreeModelIface *iface);
36 37
static void         gtk_tree_store_drag_source_init(GtkTreeDragSourceIface *iface);
static void         gtk_tree_store_drag_dest_init  (GtkTreeDragDestIface   *iface);
38
static void         gtk_tree_store_sortable_init   (GtkTreeSortableIface   *iface);
Johan Dahlin's avatar
Johan Dahlin committed
39
static void         gtk_tree_store_buildable_init  (GtkBuildableIface      *iface);
40
static void         gtk_tree_store_finalize        (GObject           *object);
41
static GtkTreeModelFlags gtk_tree_store_get_flags  (GtkTreeModel      *tree_model);
42
static gint         gtk_tree_store_get_n_columns   (GtkTreeModel      *tree_model);
43 44
static GType        gtk_tree_store_get_column_type (GtkTreeModel      *tree_model,
						    gint               index);
45 46 47
static gboolean     gtk_tree_store_get_iter        (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter,
						    GtkTreePath       *path);
48 49
static GtkTreePath *gtk_tree_store_get_path        (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter);
50
static void         gtk_tree_store_get_value       (GtkTreeModel      *tree_model,
51 52 53 54 55 56 57 58 59 60 61 62 63 64
						    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,
65
						    GtkTreeIter       *parent,
66 67 68
						    gint               n);
static gboolean     gtk_tree_store_iter_parent     (GtkTreeModel      *tree_model,
						    GtkTreeIter       *iter,
69
						    GtkTreeIter       *child);
70 71


72 73 74 75 76 77
static void gtk_tree_store_set_n_columns   (GtkTreeStore *tree_store,
					    gint          n_columns);
static void gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
					    gint          column,
					    GType         type);

78 79
static void gtk_tree_store_increment_stamp (GtkTreeStore  *tree_store);

80

81
/* DND interfaces */
Murray Cumming's avatar
Murray Cumming committed
82 83
static gboolean real_gtk_tree_store_row_draggable   (GtkTreeDragSource *drag_source,
						   GtkTreePath       *path);
84
static gboolean gtk_tree_store_drag_data_delete   (GtkTreeDragSource *drag_source,
85
						   GtkTreePath       *path);
86
static gboolean gtk_tree_store_drag_data_get      (GtkTreeDragSource *drag_source,
87 88
						   GtkTreePath       *path,
						   GtkSelectionData  *selection_data);
89
static gboolean gtk_tree_store_drag_data_received (GtkTreeDragDest   *drag_dest,
90 91
						   GtkTreePath       *dest,
						   GtkSelectionData  *selection_data);
92
static gboolean gtk_tree_store_row_drop_possible  (GtkTreeDragDest   *drag_dest,
93 94
						   GtkTreePath       *dest_path,
						   GtkSelectionData  *selection_data);
95 96 97

/* Sortable Interfaces */

98 99 100
static void     gtk_tree_store_sort                    (GtkTreeStore           *tree_store);
static void     gtk_tree_store_sort_iter_changed       (GtkTreeStore           *tree_store,
							GtkTreeIter            *iter,
101 102
							gint                    column,
							gboolean                emit_signal);
103 104
static gboolean gtk_tree_store_get_sort_column_id      (GtkTreeSortable        *sortable,
							gint                   *sort_column_id,
105
							GtkSortType            *order);
106 107
static void     gtk_tree_store_set_sort_column_id      (GtkTreeSortable        *sortable,
							gint                    sort_column_id,
108
							GtkSortType             order);
109
static void     gtk_tree_store_set_sort_func           (GtkTreeSortable        *sortable,
110 111 112
							gint                    sort_column_id,
							GtkTreeIterCompareFunc  func,
							gpointer                data,
Michael Natterer's avatar
Michael Natterer committed
113
							GDestroyNotify          destroy);
114 115 116
static void     gtk_tree_store_set_default_sort_func   (GtkTreeSortable        *sortable,
							GtkTreeIterCompareFunc  func,
							gpointer                data,
Michael Natterer's avatar
Michael Natterer committed
117
							GDestroyNotify          destroy);
118
static gboolean gtk_tree_store_has_default_sort_func   (GtkTreeSortable        *sortable);
119

Johan Dahlin's avatar
Johan Dahlin committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

/* buildable */

static gboolean gtk_tree_store_buildable_custom_tag_start (GtkBuildable  *buildable,
							   GtkBuilder    *builder,
							   GObject       *child,
							   const gchar   *tagname,
							   GMarkupParser *parser,
							   gpointer      *data);
static void     gtk_tree_store_buildable_custom_finished (GtkBuildable 	 *buildable,
							  GtkBuilder   	 *builder,
							  GObject      	 *child,
							  const gchar  	 *tagname,
							  gpointer     	  user_data);

135 136
static void     validate_gnode                         (GNode *node);

137 138 139 140 141
static void     gtk_tree_store_move                    (GtkTreeStore           *tree_store,
                                                        GtkTreeIter            *iter,
                                                        GtkTreeIter            *position,
                                                        gboolean                before);

142

143 144 145 146 147 148
static inline void
validate_tree (GtkTreeStore *tree_store)
{
  if (gtk_debug_flags & GTK_DEBUG_TREE)
    {
      g_assert (G_NODE (tree_store->root)->parent == NULL);
149

150 151 152 153
      validate_gnode (G_NODE (tree_store->root));
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
154 155 156 157 158 159 160 161
G_DEFINE_TYPE_WITH_CODE (GtkTreeStore, gtk_tree_store, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
						gtk_tree_store_tree_model_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
						gtk_tree_store_drag_source_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
						gtk_tree_store_drag_dest_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
Johan Dahlin's avatar
Johan Dahlin committed
162 163 164
						gtk_tree_store_sortable_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
						gtk_tree_store_buildable_init))
165 166

static void
167
gtk_tree_store_class_init (GtkTreeStoreClass *class)
168
{
169 170
  GObjectClass *object_class;

171
  object_class = (GObjectClass *) class;
172

173
  object_class->finalize = gtk_tree_store_finalize;
174 175 176 177 178
}

static void
gtk_tree_store_tree_model_init (GtkTreeModelIface *iface)
{
179
  iface->get_flags = gtk_tree_store_get_flags;
180
  iface->get_n_columns = gtk_tree_store_get_n_columns;
181
  iface->get_column_type = gtk_tree_store_get_column_type;
182
  iface->get_iter = gtk_tree_store_get_iter;
183
  iface->get_path = gtk_tree_store_get_path;
184
  iface->get_value = gtk_tree_store_get_value;
185 186 187 188 189 190
  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;
191 192
}

193 194 195
static void
gtk_tree_store_drag_source_init (GtkTreeDragSourceIface *iface)
{
Murray Cumming's avatar
Murray Cumming committed
196
  iface->row_draggable = real_gtk_tree_store_row_draggable;
197 198 199 200 201
  iface->drag_data_delete = gtk_tree_store_drag_data_delete;
  iface->drag_data_get = gtk_tree_store_drag_data_get;
}

static void
202
gtk_tree_store_drag_dest_init (GtkTreeDragDestIface *iface)
203 204 205 206 207
{
  iface->drag_data_received = gtk_tree_store_drag_data_received;
  iface->row_drop_possible = gtk_tree_store_row_drop_possible;
}

208 209 210
static void
gtk_tree_store_sortable_init (GtkTreeSortableIface *iface)
{
211 212
  iface->get_sort_column_id = gtk_tree_store_get_sort_column_id;
  iface->set_sort_column_id = gtk_tree_store_set_sort_column_id;
213
  iface->set_sort_func = gtk_tree_store_set_sort_func;
214 215
  iface->set_default_sort_func = gtk_tree_store_set_default_sort_func;
  iface->has_default_sort_func = gtk_tree_store_has_default_sort_func;
216 217
}

Johan Dahlin's avatar
Johan Dahlin committed
218 219 220 221 222 223 224
void
gtk_tree_store_buildable_init (GtkBuildableIface *iface)
{
  iface->custom_tag_start = gtk_tree_store_buildable_custom_tag_start;
  iface->custom_finished = gtk_tree_store_buildable_custom_finished;
}

225 226 227
static void
gtk_tree_store_init (GtkTreeStore *tree_store)
{
228
  tree_store->root = g_node_new (NULL);
229 230
  /* While the odds are against us getting 0...
   */
231 232 233 234
  do
    {
      tree_store->stamp = g_random_int ();
    }
235
  while (tree_store->stamp == 0);
236

237
  tree_store->sort_list = NULL;
238
  tree_store->sort_column_id = -2;
239
  tree_store->columns_dirty = FALSE;
240 241
}

242 243 244 245 246 247
/**
 * gtk_tree_store_new:
 * @n_columns: number of columns in the tree store
 * @Varargs: all #GType types for the columns, from first to last
 *
 * Creates a new tree store as with @n_columns columns each of the types passed
248 249 250 251
 * in.  Note that only types derived from standard GObject fundamental types 
 * are supported. 
 *
 * As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
252 253
 * GDK_TYPE_PIXBUF);</literal> will create a new #GtkTreeStore with three columns, of type
 * <type>int</type>, <type>string</type> and #GdkPixbuf respectively.
254 255 256
 *
 * Return value: a new #GtkTreeStore
 **/
257
GtkTreeStore *
258
gtk_tree_store_new (gint n_columns,
259
			       ...)
260
{
261
  GtkTreeStore *retval;
262 263 264 265 266
  va_list args;
  gint i;

  g_return_val_if_fail (n_columns > 0, NULL);

Manish Singh's avatar
Manish Singh committed
267
  retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
268
  gtk_tree_store_set_n_columns (retval, n_columns);
269 270

  va_start (args, n_columns);
271

272
  for (i = 0; i < n_columns; i++)
273 274 275 276
    {
      GType type = va_arg (args, GType);
      if (! _gtk_tree_data_list_check_type (type))
	{
277
	  g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
Manish Singh's avatar
Manish Singh committed
278
	  g_object_unref (retval);
279 280 281 282
	  return NULL;
	}
      gtk_tree_store_set_column_type (retval, i, type);
    }
283 284 285 286
  va_end (args);

  return retval;
}
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
/**
 * gtk_tree_store_newv:
 * @n_columns: number of columns in the tree store
 * @types: an array of #GType types for the columns, from first to last
 *
 * Non vararg creation function.  Used primarily by language bindings.
 *
 * Return value: a new #GtkTreeStore
 **/
GtkTreeStore *
gtk_tree_store_newv (gint   n_columns,
		     GType *types)
{
  GtkTreeStore *retval;
  gint i;

  g_return_val_if_fail (n_columns > 0, NULL);

Manish Singh's avatar
Manish Singh committed
305
  retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
306 307 308 309 310 311
  gtk_tree_store_set_n_columns (retval, n_columns);

   for (i = 0; i < n_columns; i++)
    {
      if (! _gtk_tree_data_list_check_type (types[i]))
	{
312
	  g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
Manish Singh's avatar
Manish Singh committed
313
	  g_object_unref (retval);
314 315 316 317 318 319 320
	  return NULL;
	}
      gtk_tree_store_set_column_type (retval, i, types[i]);
    }

  return retval;
}
321

322 323 324 325 326

/**
 * gtk_tree_store_set_column_types:
 * @tree_store: A #GtkTreeStore
 * @n_columns: Number of columns for the tree store
Matthias Clasen's avatar
Matthias Clasen committed
327
 * @types: An array of #GType types, one for each column
328
 * 
Matthias Clasen's avatar
Matthias Clasen committed
329 330 331 332
 * This function is meant primarily for #GObjects that inherit from 
 * #GtkTreeStore, and should only be used when constructing a new 
 * #GtkTreeStore.  It will not function after a row has been added, 
 * or a method on the #GtkTreeModel interface is called.
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
 **/
void
gtk_tree_store_set_column_types (GtkTreeStore *tree_store,
				 gint          n_columns,
				 GType        *types)
{
  gint i;

  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
  g_return_if_fail (tree_store->columns_dirty == 0);

  gtk_tree_store_set_n_columns (tree_store, n_columns);
   for (i = 0; i < n_columns; i++)
    {
      if (! _gtk_tree_data_list_check_type (types[i]))
	{
349
	  g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
350 351 352 353 354 355
	  continue;
	}
      gtk_tree_store_set_column_type (tree_store, i, types[i]);
    }
}

356
static void
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
gtk_tree_store_set_n_columns (GtkTreeStore *tree_store,
			      gint          n_columns)
{
  GType *new_columns;

  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);
    }

377 378 379 380 381
  if (tree_store->sort_list)
    _gtk_tree_data_list_header_free (tree_store->sort_list);

  tree_store->sort_list = _gtk_tree_data_list_header_new (n_columns, tree_store->column_headers);

382 383 384 385
  tree_store->column_headers = new_columns;
  tree_store->n_columns = n_columns;
}

386 387 388 389 390
/**
 * gtk_tree_store_set_column_type:
 * @tree_store: a #GtkTreeStore
 * @column: column number
 * @type: type of the data to be stored in @column
391
 *
392 393 394 395
 * 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.
396
 *
397
 **/
398
static void
399 400 401 402
gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
				gint          column,
				GType         type)
{
403 404
  if (!_gtk_tree_data_list_check_type (type))
    {
405
      g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
406 407
      return;
    }
408 409 410
  tree_store->column_headers[column] = type;
}

411
static gboolean
412 413
node_free (GNode *node, gpointer data)
{
414 415 416 417
  if (node->data)
    _gtk_tree_data_list_free (node->data, (GType*)data);
  node->data = NULL;

418
  return FALSE;
419 420 421 422 423 424 425
}

static void
gtk_tree_store_finalize (GObject *object)
{
  GtkTreeStore *tree_store = GTK_TREE_STORE (object);

426 427
  g_node_traverse (tree_store->root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
		   node_free, tree_store->column_headers);
428
  g_node_destroy (tree_store->root);
429 430 431 432 433
  _gtk_tree_data_list_header_free (tree_store->sort_list);
  g_free (tree_store->column_headers);

  if (tree_store->default_sort_destroy)
    {
Michael Natterer's avatar
Michael Natterer committed
434
      GDestroyNotify d = tree_store->default_sort_destroy;
435

436
      tree_store->default_sort_destroy = NULL;
437
      d (tree_store->default_sort_data);
438 439
      tree_store->default_sort_data = NULL;
    }
440

441
  /* must chain up */
Matthias Clasen's avatar
Matthias Clasen committed
442
  G_OBJECT_CLASS (gtk_tree_store_parent_class)->finalize (object);
443 444
}

445 446
/* fulfill the GtkTreeModel requirements */
/* NOTE: GtkTreeStore::root is a GNode, that acts as the parent node.  However,
447
 * it is not visible to the tree or to the user., and the path "0" refers to the
448 449
 * first child of GtkTreeStore::root.
 */
450 451


452
static GtkTreeModelFlags
453 454 455 456 457
gtk_tree_store_get_flags (GtkTreeModel *tree_model)
{
  return GTK_TREE_MODEL_ITERS_PERSIST;
}

458 459 460
static gint
gtk_tree_store_get_n_columns (GtkTreeModel *tree_model)
{
461 462 463 464 465
  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;

  tree_store->columns_dirty = TRUE;

  return tree_store->n_columns;
466 467
}

468 469 470 471
static GType
gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
				gint          index)
{
472 473
  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;

474
  g_return_val_if_fail (index < tree_store->n_columns, G_TYPE_INVALID);
475

476 477 478
  tree_store->columns_dirty = TRUE;

  return tree_store->column_headers[index];
479 480
}

481 482 483 484 485 486 487 488 489 490
static gboolean
gtk_tree_store_get_iter (GtkTreeModel *tree_model,
			 GtkTreeIter  *iter,
			 GtkTreePath  *path)
{
  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
  GtkTreeIter parent;
  gint *indices;
  gint depth, i;

491 492
  tree_store->columns_dirty = TRUE;

493 494 495 496 497 498 499 500
  indices = gtk_tree_path_get_indices (path);
  depth = gtk_tree_path_get_depth (path);

  g_return_val_if_fail (depth > 0, FALSE);

  parent.stamp = tree_store->stamp;
  parent.user_data = tree_store->root;

501
  if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[0]))
502 503 504 505 506
    return FALSE;

  for (i = 1; i < depth; i++)
    {
      parent = *iter;
507
      if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[i]))
508 509 510 511 512 513
	return FALSE;
    }

  return TRUE;
}

514 515
static GtkTreePath *
gtk_tree_store_get_path (GtkTreeModel *tree_model,
516
			 GtkTreeIter  *iter)
517
{
518
  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
519 520 521
  GtkTreePath *retval;
  GNode *tmp_node;
  gint i = 0;
522

523
  g_return_val_if_fail (iter->user_data != NULL, NULL);
524
  g_return_val_if_fail (iter->stamp == tree_store->stamp, NULL);
525

526
  validate_tree (tree_store);
527

528
  if (G_NODE (iter->user_data)->parent == NULL &&
529
      G_NODE (iter->user_data) == tree_store->root)
530
    return gtk_tree_path_new ();
531
  g_assert (G_NODE (iter->user_data)->parent != NULL);
532

533
  if (G_NODE (iter->user_data)->parent == G_NODE (tree_store->root))
534 535
    {
      retval = gtk_tree_path_new ();
536
      tmp_node = G_NODE (tree_store->root)->children;
537 538 539
    }
  else
    {
540
      GtkTreeIter tmp_iter = *iter;
541

542
      tmp_iter.user_data = G_NODE (iter->user_data)->parent;
543

544
      retval = gtk_tree_store_get_path (tree_model, &tmp_iter);
Havoc Pennington's avatar
Havoc Pennington committed
545
      tmp_node = G_NODE (iter->user_data)->parent->children;
546 547 548 549
    }

  if (retval == NULL)
    return NULL;
550

551 552 553 554 555 556 557 558
  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
559
      if (tmp_node == G_NODE (iter->user_data))
560 561 562
	break;
      i++;
    }
563

564 565 566
  if (tmp_node == NULL)
    {
      /* We couldn't find node, meaning it's prolly not ours */
567
      /* Perhaps I should do a g_return_if_fail here. */
568 569 570 571 572 573 574 575 576 577 578
      gtk_tree_path_free (retval);
      return NULL;
    }

  gtk_tree_path_append_index (retval, i);

  return retval;
}


static void
579 580 581 582
gtk_tree_store_get_value (GtkTreeModel *tree_model,
			  GtkTreeIter  *iter,
			  gint          column,
			  GValue       *value)
583
{
584
  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
585 586 587
  GtkTreeDataList *list;
  gint tmp_column = column;

588 589
  g_return_if_fail (column < tree_store->n_columns);
  g_return_if_fail (VALID_ITER (iter, tree_store));
590

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

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

596 597
  if (list)
    {
598
      _gtk_tree_data_list_node_to_value (list,
599
					 tree_store->column_headers[column],
600
					 value);
601 602 603 604
    }
  else
    {
      /* We want to return an initialized but empty (default) value */
605
      g_value_init (value, tree_store->column_headers[column]);
606
    }
607 608 609
}

static gboolean
610 611
gtk_tree_store_iter_next (GtkTreeModel  *tree_model,
			  GtkTreeIter   *iter)
612
{
613
  g_return_val_if_fail (iter->user_data != NULL, FALSE);
614
  g_return_val_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
615

616 617 618 619 620 621
  if (G_NODE (iter->user_data)->next)
    {
      iter->user_data = G_NODE (iter->user_data)->next;
      return TRUE;
    }
  else
622 623 624
    return FALSE;
}

625 626 627 628
static gboolean
gtk_tree_store_iter_children (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter,
			      GtkTreeIter  *parent)
629
{
630
  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
631 632
  GNode *children;

633 634
  if (parent)
    g_return_val_if_fail (VALID_ITER (parent, tree_store), FALSE);
635

636
  if (parent)
637
    children = G_NODE (parent->user_data)->children;
638
  else
639
    children = G_NODE (tree_store->root)->children;
640

641 642
  if (children)
    {
643
      iter->stamp = tree_store->stamp;
644 645 646 647 648
      iter->user_data = children;
      return TRUE;
    }
  else
    return FALSE;
649 650 651
}

static gboolean
652 653
gtk_tree_store_iter_has_child (GtkTreeModel *tree_model,
			       GtkTreeIter  *iter)
654
{
655
  g_return_val_if_fail (iter->user_data != NULL, FALSE);
656
  g_return_val_if_fail (VALID_ITER (iter, tree_model), FALSE);
657

Havoc Pennington's avatar
Havoc Pennington committed
658
  return G_NODE (iter->user_data)->children != NULL;
659 660 661
}

static gint
662 663
gtk_tree_store_iter_n_children (GtkTreeModel *tree_model,
				GtkTreeIter  *iter)
664
{
665
  GNode *node;
666 667
  gint i = 0;

Federico Mena Quintero's avatar
Federico Mena Quintero committed
668
  g_return_val_if_fail (iter == NULL || iter->user_data != NULL, 0);
669

670 671 672
  if (iter == NULL)
    node = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
  else
Havoc Pennington's avatar
Havoc Pennington committed
673
    node = G_NODE (iter->user_data)->children;
674

675
  while (node)
676 677
    {
      i++;
678
      node = node->next;
679 680 681 682 683
    }

  return i;
}

684 685 686 687
static gboolean
gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
			       GtkTreeIter  *iter,
			       GtkTreeIter  *parent,
688 689
			       gint          n)
{
690
  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
691
  GNode *parent_node;
692
  GNode *child;
693

694
  g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
695

696
  if (parent == NULL)
697
    parent_node = tree_store->root;
698
  else
Havoc Pennington's avatar
Havoc Pennington committed
699
    parent_node = parent->user_data;
700

701
  child = g_node_nth_child (parent_node, n);
702

703 704 705
  if (child)
    {
      iter->user_data = child;
706
      iter->stamp = tree_store->stamp;
707 708
      return TRUE;
    }
709
  else
710
    return FALSE;
711 712
}

713 714 715 716
static gboolean
gtk_tree_store_iter_parent (GtkTreeModel *tree_model,
			    GtkTreeIter  *iter,
			    GtkTreeIter  *child)
717
{
718
  GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
719
  GNode *parent;
720

721
  g_return_val_if_fail (iter != NULL, FALSE);
722
  g_return_val_if_fail (VALID_ITER (child, tree_store), FALSE);
723 724 725

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

726
  g_assert (parent != NULL);
727

728
  if (parent != tree_store->root)
729
    {
730
      iter->user_data = parent;
731
      iter->stamp = tree_store->stamp;
732
      return TRUE;
733
    }
734 735
  else
    return FALSE;
736 737
}

738 739

/* Does not emit a signal */
740
static gboolean
741 742 743
gtk_tree_store_real_set_value (GtkTreeStore *tree_store,
			       GtkTreeIter  *iter,
			       gint          column,
744 745
			       GValue       *value,
			       gboolean      sort)
746 747 748
{
  GtkTreeDataList *list;
  GtkTreeDataList *prev;
749
  gint old_column = column;
750 751
  GValue real_value = {0, };
  gboolean converted = FALSE;
752
  gboolean retval = FALSE;
753

754
  if (! g_type_is_a (G_VALUE_TYPE (value), tree_store->column_headers[column]))
755
    {
756 757
      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))))
758 759 760 761
	{
	  g_warning ("%s: Unable to convert from %s to %s\n",
		     G_STRLOC,
		     g_type_name (G_VALUE_TYPE (value)),
762
		     g_type_name (tree_store->column_headers[column]));
763
	  return retval;
764 765 766 767 768 769
	}
      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)),
770
		     g_type_name (tree_store->column_headers[column]));
771
	  g_value_unset (&real_value);
772
	  return retval;
773 774 775
	}
      converted = TRUE;
    }
776

Havoc Pennington's avatar
Havoc Pennington committed
777
  prev = list = G_NODE (iter->user_data)->data;
778 779 780 781 782

  while (list != NULL)
    {
      if (column == 0)
	{
783 784 785 786
	  if (converted)
	    _gtk_tree_data_list_value_to_node (list, &real_value);
	  else
	    _gtk_tree_data_list_value_to_node (list, value);
787
	  retval = TRUE;
788 789
	  if (converted)
	    g_value_unset (&real_value);
790
          if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
791
            gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
792
	  return retval;
793 794 795 796 797 798 799
	}

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

Havoc Pennington's avatar
Havoc Pennington committed
800
  if (G_NODE (iter->user_data)->data == NULL)
801
    {
Havoc Pennington's avatar
Havoc Pennington committed
802
      G_NODE (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
803 804 805 806
      list->next = NULL;
    }
  else
    {
807
      list = prev->next = _gtk_tree_data_list_alloc ();
808 809 810 811 812
      list->next = NULL;
    }

  while (column != 0)
    {
813
      list->next = _gtk_tree_data_list_alloc ();
814 815 816 817
      list = list->next;
      list->next = NULL;
      column --;
    }
818

819
  if (converted)
820
    _gtk_tree_data_list_value_to_node (list, &real_value);
821 822
  else
    _gtk_tree_data_list_value_to_node (list, value);
823 824
  
  retval = TRUE;
825 826
  if (converted)
    g_value_unset (&real_value);
827

828
  if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
829
    gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
830

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
  return retval;
}

/**
 * gtk_tree_store_set_value:
 * @tree_store: a #GtkTreeStore
 * @iter: A valid #GtkTreeIter for the row being modified
 * @column: column number to modify
 * @value: new value for the cell
 *
 * Sets the data in the cell specified by @iter and @column.
 * The type of @value must be convertible to the type of the
 * column.
 *
 **/
void
gtk_tree_store_set_value (GtkTreeStore *tree_store,
			  GtkTreeIter  *iter,
			  gint          column,
			  GValue       *value)
{
  g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
  g_return_if_fail (VALID_ITER (iter, tree_store));
  g_return_if_fail (column >= 0 && column < tree_store->n_columns);
  g_return_if_fail (G_IS_VALUE (value));

857
  if (gtk_tree_store_real_set_value (tree_store, iter, column, value, TRUE))
858 859 860
    {
      GtkTreePath *path;

861
      path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
862 863 864
      gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
      gtk_tree_path_free (path);
    }
865 866
}

867 868
static GtkTreeIterCompareFunc
gtk_tree_store_get_compare_func (GtkTreeStore *tree_store)
869
{
870
  GtkTreeIterCompareFunc func = NULL;
871

872
  if (GTK_TREE_STORE_IS_SORTED (tree_store))
873
    {
874 875 876 877 878
      if (tree_store->sort_column_id != -1)
	{
	  GtkTreeDataSortHeader *header;
	  header = _gtk_tree_data_list_get_header (tree_store->sort_list,
						   tree_store->sort_column_id);
879 880
	  g_return_val_if_fail (header != NULL, NULL);
	  g_return_val_if_fail (header->func != NULL, NULL);
881 882 883 884 885 886
	  func = header->func;
	}
      else
	{
	  func = tree_store->default_sort_func;
	}
887 888
    }

889 890 891
  return func;
}

892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
static void
gtk_tree_store_set_vector_internal (GtkTreeStore *tree_store,
				    GtkTreeIter  *iter,
				    gboolean     *emit_signal,
				    gboolean     *maybe_need_sort,
				    gint         *columns,
				    GValue       *values,
				    gint          n_values)
{
  gint i;
  GtkTreeIterCompareFunc func = NULL;

  func = gtk_tree_store_get_compare_func (tree_store);
  if (func != _gtk_tree_data_list_compare_func)
    *maybe_need_sort = TRUE;

  for (i = 0; i < n_values; i++)
    {
      *emit_signal = gtk_tree_store_real_set_value (tree_store, iter,
						    columns[i], &values[i],
						    FALSE) || *emit_signal;

      if (func == _gtk_tree_data_list_compare_func &&
	  columns[i] == tree_store->sort_column_id)
	*maybe_need_sort = TRUE;
    }
}

920 921 922 923 924 925 926 927 928 929 930 931 932
static void
gtk_tree_store_set_valist_internal (GtkTreeStore *tree_store,
                                    GtkTreeIter  *iter,
                                    gboolean     *emit_signal,
                                    gboolean     *maybe_need_sort,
                                    va_list       var_args)
{
  gint column;
  GtkTreeIterCompareFunc func = NULL;

  column = va_arg (var_args, gint);

  func = gtk_tree_store_get_compare_func (tree_store);
933
  if (func != _gtk_tree_data_list_compare_func)
934
    *maybe_need_sort = TRUE;
935

Havoc Pennington's avatar
Havoc Pennington committed
936
  while (column != -1)
937 938 939 940
    {
      GValue value = { 0, };
      gchar *error = NULL;

941
      if (column < 0 || column >= tree_store->n_columns)
942
	{
Havoc Pennington's avatar
Havoc Pennington committed
943
	  g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
944 945 946 947
	  break;
	}
      g_value_init (&value, tree_store->column_headers[column]);

948
      G_VALUE_COLLECT (&value, var_args, 0, &error);
949 950 951 952 953 954 955 956 957 958 959
      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;
	}

960 961 962 963 964
      *emit_signal = gtk_tree_store_real_set_value (tree_store,
						    iter,
						    column,
						    &value,
						    FALSE) || *emit_signal;
965

966
      if (func == _gtk_tree_data_list_compare_func &&
967
	  column == tree_store->sort_column_id)
968
	*maybe_need_sort = TRUE;
969 970 971 972 973

      g_value_unset (&value);

      column = va_arg (var_args, gint);
    }
974 975
}

976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 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
/**
 * gtk_tree_store_set_valuesv:
 * @tree_store: A #GtkTreeStore
 * @iter: A valid #GtkTreeIter for the row being modified
 * @columns: an array of column numbers
 * @values: an array of GValues
 * @n_values: the length of the @columns and @values arrays
 *
 * A variant of gtk_tree_store_set_valist() which takes
 * the columns and values as two arrays, instead of varargs.  This
 * function