testtreeview.c 35.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* testtreeview.c
 * Copyright (C) 2001 Red Hat, Inc
 * Author: Jonathan Blandford
 *
 * 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
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18
 */

Havoc Pennington's avatar
Havoc Pennington committed
19
#include <string.h>
20
#include <gtk/gtk.h>
21
#include <stdlib.h>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

/* Don't copy this bad example; inline RGB data is always a better
 * idea than inline XPMs.
 */
static char  *book_closed_xpm[] = {
"16 16 6 1",
"       c None s None",
".      c black",
"X      c red",
"o      c yellow",
"O      c #808080",
"#      c white",
"                ",
"       ..       ",
"     ..XX.      ",
"   ..XXXXX.     ",
" ..XXXXXXXX.    ",
".ooXXXXXXXXX.   ",
"..ooXXXXXXXXX.  ",
".X.ooXXXXXXXXX. ",
".XX.ooXXXXXX..  ",
" .XX.ooXXX..#O  ",
"  .XX.oo..##OO. ",
"   .XX..##OO..  ",
"    .X.#OO..    ",
"     ..O..      ",
"      ..        ",
"                "
};

static void run_automated_tests (void);
Havoc Pennington's avatar
Havoc Pennington committed
53 54 55

/* This custom model is to test custom model use. */

Manish Singh's avatar
Manish Singh committed
56 57 58 59 60
#define GTK_TYPE_MODEL_TYPES				(gtk_tree_model_types_get_type ())
#define GTK_TREE_MODEL_TYPES(obj)			(G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_MODEL_TYPES, GtkTreeModelTypes))
#define GTK_TREE_MODEL_TYPES_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_MODEL_TYPES, GtkTreeModelTypesClass))
#define GTK_IS_TREE_MODEL_TYPES(obj)			(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_MODEL_TYPES))
#define GTK_IS_TREE_MODEL_TYPES_GET_CLASS(klass)	(G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_MODEL_TYPES))
Havoc Pennington's avatar
Havoc Pennington committed
61 62 63 64 65 66

typedef struct _GtkTreeModelTypes       GtkTreeModelTypes;
typedef struct _GtkTreeModelTypesClass  GtkTreeModelTypesClass;

struct _GtkTreeModelTypes
{
Manish Singh's avatar
Manish Singh committed
67
  GObject parent;
Havoc Pennington's avatar
Havoc Pennington committed
68 69 70 71 72 73

  gint stamp;
};

struct _GtkTreeModelTypesClass
{
Manish Singh's avatar
Manish Singh committed
74
  GObjectClass parent_class;
Havoc Pennington's avatar
Havoc Pennington committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

  guint        (* get_flags)       (GtkTreeModel *tree_model);   
  gint         (* get_n_columns)   (GtkTreeModel *tree_model);
  GType        (* get_column_type) (GtkTreeModel *tree_model,
				    gint          index);
  gboolean     (* get_iter)        (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter,
				    GtkTreePath  *path);
  GtkTreePath *(* get_path)        (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter);
  void         (* get_value)       (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter,
				    gint          column,
				    GValue       *value);
  gboolean     (* iter_next)       (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter);
  gboolean     (* iter_children)   (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter,
				    GtkTreeIter  *parent);
  gboolean     (* iter_has_child)  (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter);
  gint         (* iter_n_children) (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter);
  gboolean     (* iter_nth_child)  (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter,
				    GtkTreeIter  *parent,
				    gint          n);
  gboolean     (* iter_parent)     (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter,
				    GtkTreeIter  *child);
  void         (* ref_iter)        (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter);
  void         (* unref_iter)      (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter);

  /* These will be moved into the GtkTreeModelIface eventually */
  void         (* changed)         (GtkTreeModel *tree_model,
				    GtkTreePath  *path,
				    GtkTreeIter  *iter);
  void         (* inserted)        (GtkTreeModel *tree_model,
				    GtkTreePath  *path,
				    GtkTreeIter  *iter);
  void         (* child_toggled)   (GtkTreeModel *tree_model,
				    GtkTreePath  *path,
				    GtkTreeIter  *iter);
  void         (* deleted)         (GtkTreeModel *tree_model,
				    GtkTreePath  *path);
};

Manish Singh's avatar
Manish Singh committed
124
GType              gtk_tree_model_types_get_type      (void) G_GNUC_CONST;
Havoc Pennington's avatar
Havoc Pennington committed
125 126
GtkTreeModelTypes *gtk_tree_model_types_new           (void);

127 128 129 130 131 132 133 134
typedef enum
{
  COLUMNS_NONE,
  COLUMNS_ONE,
  COLUMNS_LOTS,
  COLUMNS_LAST
} ColumnsType;

135 136 137 138 139 140
static gchar *column_type_names[] = {
  "No columns",
  "One column",
  "Many columns"
};

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#define N_COLUMNS 9

static GType*
get_model_types (void)
{
  static GType column_types[N_COLUMNS] = { 0 };
  
  if (column_types[0] == 0)
    {
      column_types[0] = G_TYPE_STRING;
      column_types[1] = G_TYPE_STRING;
      column_types[2] = GDK_TYPE_PIXBUF;
      column_types[3] = G_TYPE_FLOAT;
      column_types[4] = G_TYPE_UINT;
      column_types[5] = G_TYPE_UCHAR;
      column_types[6] = G_TYPE_CHAR;
157 158
#define BOOL_COLUMN 7
      column_types[BOOL_COLUMN] = G_TYPE_BOOLEAN;
159 160 161 162 163 164
      column_types[8] = G_TYPE_INT;
    }

  return column_types;
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
static void
toggled_callback (GtkCellRendererToggle *celltoggle,
                  gchar                 *path_string,
                  GtkTreeView           *tree_view)
{
  GtkTreeModel *model = NULL;
  GtkTreeModelSort *sort_model = NULL;
  GtkTreePath *path;
  GtkTreeIter iter;
  gboolean active = FALSE;
  
  g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));

  model = gtk_tree_view_get_model (tree_view);
  
  if (GTK_IS_TREE_MODEL_SORT (model))
    {
      sort_model = GTK_TREE_MODEL_SORT (model);
      model = gtk_tree_model_sort_get_model (sort_model);
    }

  if (model == NULL)
    return;

  if (sort_model)
    {
      g_warning ("FIXME implement conversion from TreeModelSort iter to child model iter");
      return;
    }
      
  path = gtk_tree_path_new_from_string (path_string);
  if (!gtk_tree_model_get_iter (model,
                                &iter, path))
    {
      g_warning ("%s: bad path?", G_STRLOC);
      return;
    }
  gtk_tree_path_free (path);
  
  if (GTK_IS_LIST_STORE (model))
    {
206
      gtk_tree_model_get (GTK_TREE_MODEL (model),
207 208 209 210 211 212 213 214 215 216 217 218 219
                          &iter,
                          BOOL_COLUMN,
                          &active,
                          -1);
      
      gtk_list_store_set (GTK_LIST_STORE (model),
                          &iter,
                          BOOL_COLUMN,
                          !active,
                          -1);
    }
  else if (GTK_IS_TREE_STORE (model))
    {
220
      gtk_tree_model_get (GTK_TREE_MODEL (model),
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
                          &iter,
                          BOOL_COLUMN,
                          &active,
                          -1);
            
      gtk_tree_store_set (GTK_TREE_STORE (model),
                          &iter,
                          BOOL_COLUMN,
                          !active,
                          -1);
    }
  else
    g_warning ("don't know how to actually toggle value for model type %s",
               g_type_name (G_TYPE_FROM_INSTANCE (model)));
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
static void
edited_callback (GtkCellRendererText *renderer,
		 const gchar   *path_string,
		 const gchar   *new_text,
		 GtkTreeView  *tree_view)
{
  GtkTreeModel *model = NULL;
  GtkTreeModelSort *sort_model = NULL;
  GtkTreePath *path;
  GtkTreeIter iter;
  guint value = atoi (new_text);
  
  g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));

  model = gtk_tree_view_get_model (tree_view);
  
  if (GTK_IS_TREE_MODEL_SORT (model))
    {
      sort_model = GTK_TREE_MODEL_SORT (model);
      model = gtk_tree_model_sort_get_model (sort_model);
    }

  if (model == NULL)
    return;

  if (sort_model)
    {
      g_warning ("FIXME implement conversion from TreeModelSort iter to child model iter");
      return;
    }
      
  path = gtk_tree_path_new_from_string (path_string);
  if (!gtk_tree_model_get_iter (model,
                                &iter, path))
    {
      g_warning ("%s: bad path?", G_STRLOC);
      return;
    }
  gtk_tree_path_free (path);

  if (GTK_IS_LIST_STORE (model))
    {
      gtk_list_store_set (GTK_LIST_STORE (model),
                          &iter,
                          4,
                          value,
                          -1);
    }
  else if (GTK_IS_TREE_STORE (model))
    {
      gtk_tree_store_set (GTK_TREE_STORE (model),
                          &iter,
                          4,
                          value,
                          -1);
    }
  else
    g_warning ("don't know how to actually toggle value for model type %s",
               g_type_name (G_TYPE_FROM_INSTANCE (model)));
}
297 298 299

static ColumnsType current_column_type = COLUMNS_LOTS;

300 301 302 303 304
static void
set_columns_type (GtkTreeView *tree_view, ColumnsType type)
{
  GtkTreeViewColumn *col;
  GtkCellRenderer *rend;
Havoc Pennington's avatar
Havoc Pennington committed
305 306
  GdkPixbuf *pixbuf;
  GtkWidget *image;
307
  GtkAdjustment *adjustment;
308

309 310
  current_column_type = type;
  
311 312 313 314 315 316 317 318 319 320 321 322 323
  col = gtk_tree_view_get_column (tree_view, 0);
  while (col)
    {
      gtk_tree_view_remove_column (tree_view, col);

      col = gtk_tree_view_get_column (tree_view, 0);
    }

  switch (type)
    {
    case COLUMNS_NONE:
      break;

324
    case COLUMNS_LOTS:
325
      rend = gtk_cell_renderer_text_new ();
326

327 328 329 330 331 332 333
      col = gtk_tree_view_column_new_with_attributes ("Column 1",
                                                      rend,
                                                      "text", 1,
                                                      NULL);
      
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
      
334 335
      col = gtk_tree_view_column_new();
      gtk_tree_view_column_set_title (col, "Column 2");
336
      
337 338 339 340 341 342
      rend = gtk_cell_renderer_pixbuf_new ();
      gtk_tree_view_column_pack_start (col, rend, FALSE);
      gtk_tree_view_column_add_attribute (col, rend, "pixbuf", 2);
      rend = gtk_cell_renderer_text_new ();
      gtk_tree_view_column_pack_start (col, rend, TRUE);
      gtk_tree_view_column_add_attribute (col, rend, "text", 0);
343

344 345
      
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
346
      gtk_tree_view_set_expander_column (tree_view, col);
347
      
348 349
      rend = gtk_cell_renderer_toggle_new ();

Manish Singh's avatar
Manish Singh committed
350 351
      g_signal_connect (rend, "toggled",
			G_CALLBACK (toggled_callback), tree_view);
352 353 354 355 356 357 358
      
      col = gtk_tree_view_column_new_with_attributes ("Column 3",
                                                      rend,
                                                      "active", BOOL_COLUMN,
                                                      NULL);

      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
Havoc Pennington's avatar
Havoc Pennington committed
359

360
      pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **)book_closed_xpm);
Havoc Pennington's avatar
Havoc Pennington committed
361 362 363

      image = gtk_image_new_from_pixbuf (pixbuf);

Manish Singh's avatar
Manish Singh committed
364
      g_object_unref (pixbuf);
365
      
Havoc Pennington's avatar
Havoc Pennington committed
366 367 368
      gtk_widget_show (image);
      
      gtk_tree_view_column_set_widget (col, image);
369 370 371 372 373 374
      
      rend = gtk_cell_renderer_toggle_new ();

      /* you could also set this per-row by tying it to a column
       * in the model of course.
       */
375
      g_object_set (rend, "radio", TRUE, NULL);
376
      
Manish Singh's avatar
Manish Singh committed
377
      g_signal_connect (rend, "toggled",
378
			G_CALLBACK (toggled_callback), tree_view);
379 380 381 382 383 384 385
      
      col = gtk_tree_view_column_new_with_attributes ("Column 4",
                                                      rend,
                                                      "active", BOOL_COLUMN,
                                                      NULL);

      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
386

387 388 389 390 391 392 393 394 395
      rend = gtk_cell_renderer_spin_new ();

      adjustment = gtk_adjustment_new (0, 0, 10000, 100, 100, 100);
      g_object_set (rend, "editable", TRUE, NULL);
      g_object_set (rend, "adjustment", adjustment, NULL);

      g_signal_connect (rend, "edited",
			G_CALLBACK (edited_callback), tree_view);

396 397
      col = gtk_tree_view_column_new_with_attributes ("Column 5",
                                                      rend,
398
                                                      "text", 4,
399 400 401
                                                      NULL);

      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
402
#if 0
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
      
      rend = gtk_cell_renderer_text_new ();
      
      col = gtk_tree_view_column_new_with_attributes ("Column 6",
                                                      rend,
                                                      "text", 4,
                                                      NULL);

      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
      
      rend = gtk_cell_renderer_text_new ();
      
      col = gtk_tree_view_column_new_with_attributes ("Column 7",
                                                      rend,
                                                      "text", 5,
                                                      NULL);

      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
      
      rend = gtk_cell_renderer_text_new ();
      
      col = gtk_tree_view_column_new_with_attributes ("Column 8",
                                                      rend,
                                                      "text", 6,
                                                      NULL);

      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
      
      rend = gtk_cell_renderer_text_new ();
      
      col = gtk_tree_view_column_new_with_attributes ("Column 9",
                                                      rend,
                                                      "text", 7,
                                                      NULL);

      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
      
      rend = gtk_cell_renderer_text_new ();
      
      col = gtk_tree_view_column_new_with_attributes ("Column 10",
                                                      rend,
                                                      "text", 8,
                                                      NULL);

      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
      
#endif
      
451 452 453 454 455 456 457 458 459
      /* FALL THRU */
      
    case COLUMNS_ONE:
      rend = gtk_cell_renderer_text_new ();
      
      col = gtk_tree_view_column_new_with_attributes ("Column 0",
                                                      rend,
                                                      "text", 0,
                                                      NULL);
460

461 462 463 464 465 466
      gtk_tree_view_insert_column (GTK_TREE_VIEW (tree_view), col, 0);
    default:
      break;
    }
}

467 468 469 470 471 472
static ColumnsType
get_columns_type (void)
{
  return current_column_type;
}

473 474
static GdkPixbuf *our_pixbuf;
  
Havoc Pennington's avatar
Havoc Pennington committed
475 476
typedef enum
{
Havoc Pennington's avatar
Havoc Pennington committed
477 478 479 480 481
  /*   MODEL_TYPES, */
  MODEL_TREE,
  MODEL_LIST,
  MODEL_SORTED_TREE,
  MODEL_SORTED_LIST,
482 483
  MODEL_EMPTY_LIST,
  MODEL_EMPTY_TREE,
Havoc Pennington's avatar
Havoc Pennington committed
484 485
  MODEL_NULL,
  MODEL_LAST
Havoc Pennington's avatar
Havoc Pennington committed
486 487
} ModelType;

Havoc Pennington's avatar
Havoc Pennington committed
488
/* FIXME add a custom model to test */
Havoc Pennington's avatar
Havoc Pennington committed
489
static GtkTreeModel *models[MODEL_LAST];
Havoc Pennington's avatar
Havoc Pennington committed
490 491 492 493 494
static const char *model_names[MODEL_LAST] = {
  "GtkTreeStore",
  "GtkListStore",
  "GtkTreeModelSort wrapping GtkTreeStore",
  "GtkTreeModelSort wrapping GtkListStore",
495 496
  "Empty GtkListStore",
  "Empty GtkTreeStore",
Havoc Pennington's avatar
Havoc Pennington committed
497 498 499 500 501 502 503 504 505
  "NULL (no model)"
};

static GtkTreeModel*
create_list_model (void)
{
  GtkListStore *store;
  GtkTreeIter iter;
  gint i;
506 507 508
  GType *t;

  t = get_model_types ();
Havoc Pennington's avatar
Havoc Pennington committed
509
  
510 511 512 513
  store = gtk_list_store_new (N_COLUMNS,
			      t[0], t[1], t[2],
			      t[3], t[4], t[5],
			      t[6], t[7], t[8]);
Havoc Pennington's avatar
Havoc Pennington committed
514 515 516 517 518 519 520 521 522 523

  i = 0;
  while (i < 200)
    {
      char *msg;
      
      gtk_list_store_append (store, &iter);

      msg = g_strdup_printf ("%d", i);
      
524 525 526 527 528 529
      gtk_list_store_set (store, &iter, 0, msg, 1, "Foo! Foo! Foo!",
                          2, our_pixbuf,
                          3, 7.0, 4, (guint) 9000,
                          5, 'f', 6, 'g',
                          7, TRUE, 8, 23245454,
                          -1);
Havoc Pennington's avatar
Havoc Pennington committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

      g_free (msg);
      
      ++i;
    }

  return GTK_TREE_MODEL (store);
}

static void
typesystem_recurse (GType        type,
                    GtkTreeIter *parent_iter,
                    GtkTreeStore *store)
{
  GType* children;
  guint n_children = 0;
  gint i;
  GtkTreeIter iter;
  gchar *str;
  
  gtk_tree_store_append (store, &iter, parent_iter);

552
  str = g_strdup_printf ("%ld", (glong)type);
553 554 555 556 557 558
  gtk_tree_store_set (store, &iter, 0, str, 1, g_type_name (type),
                      2, our_pixbuf,
                      3, 7.0, 4, (guint) 9000,
                      5, 'f', 6, 'g',
                      7, TRUE, 8, 23245454,
                      -1);
Havoc Pennington's avatar
Havoc Pennington committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
  g_free (str);
  
  children = g_type_children (type, &n_children);

  i = 0;
  while (i < n_children)
    {
      typesystem_recurse (children[i], &iter, store);

      ++i;
    }
  
  g_free (children);
}

static GtkTreeModel*
create_tree_model (void)
{
  GtkTreeStore *store;
  gint i;
579
  GType *t;
Havoc Pennington's avatar
Havoc Pennington committed
580
  
581
  /* Make the tree more interesting */
582 583 584 585
  /* - we need this magic here so we are sure the type ends up being
   * registered and gcc doesn't optimize away the code */
  g_type_class_unref (g_type_class_ref (gtk_scrolled_window_get_type ()));
  g_type_class_unref (g_type_class_ref (gtk_label_get_type ()));
586
  g_type_class_unref (g_type_class_ref (gtk_scrollbar_get_type ()));
587
  g_type_class_unref (g_type_class_ref (pango_layout_get_type ()));
588 589 590

  t = get_model_types ();
  
591 592 593 594
  store = gtk_tree_store_new (N_COLUMNS,
			      t[0], t[1], t[2],
			      t[3], t[4], t[5],
			      t[6], t[7], t[8]);
Havoc Pennington's avatar
Havoc Pennington committed
595 596

  i = 0;
597
  while (i < G_TYPE_FUNDAMENTAL_MAX)
Havoc Pennington's avatar
Havoc Pennington committed
598 599 600 601 602 603 604 605 606 607
    {
      typesystem_recurse (i, NULL, store);
      
      ++i;
    }

  return GTK_TREE_MODEL (store);
}

static void
608
model_selected (GtkComboBox *combo_box, gpointer data)
Havoc Pennington's avatar
Havoc Pennington committed
609 610 611 612
{
  GtkTreeView *tree_view = GTK_TREE_VIEW (data);
  gint hist;

613
  hist = gtk_combo_box_get_active (combo_box);
Havoc Pennington's avatar
Havoc Pennington committed
614 615 616 617 618 619

  if (models[hist] != gtk_tree_view_get_model (tree_view))
    {
      gtk_tree_view_set_model (tree_view, models[hist]);
    }
}
Havoc Pennington's avatar
Havoc Pennington committed
620

621
static void
622
columns_selected (GtkComboBox *combo_box, gpointer data)
623 624 625 626
{
  GtkTreeView *tree_view = GTK_TREE_VIEW (data);
  gint hist;

627
  hist = gtk_combo_box_get_active (combo_box);
628 629 630 631 632 633 634

  if (hist != get_columns_type ())
    {
      set_columns_type (tree_view, hist);
    }
}

635 636 637 638 639 640 641 642
void
on_row_activated (GtkTreeView       *tree_view,
                  GtkTreePath       *path,
                  GtkTreeViewColumn *column,
                  gpointer           user_data)
{
  g_print ("Row activated\n");
}
Havoc Pennington's avatar
Havoc Pennington committed
643 644 645

enum
{
646
  TARGET_GTK_TREE_MODEL_ROW
Havoc Pennington's avatar
Havoc Pennington committed
647 648 649
};

static GtkTargetEntry row_targets[] = {
650 651
  { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP,
    TARGET_GTK_TREE_MODEL_ROW }
Havoc Pennington's avatar
Havoc Pennington committed
652 653
};

Havoc Pennington's avatar
Havoc Pennington committed
654 655 656 657 658 659 660
int
main (int    argc,
      char **argv)
{
  GtkWidget *window;
  GtkWidget *sw;
  GtkWidget *tv;
661
  GtkWidget *box;
662
  GtkWidget *combo_box;
Havoc Pennington's avatar
Havoc Pennington committed
663
  GtkTreeModel *model;
Havoc Pennington's avatar
Havoc Pennington committed
664 665 666 667
  gint i;
  
  gtk_init (&argc, &argv);

668 669 670
  if (g_getenv ("RTL"))
    gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);

671 672
  our_pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) book_closed_xpm);  
  
Havoc Pennington's avatar
Havoc Pennington committed
673
#if 0
Havoc Pennington's avatar
Havoc Pennington committed
674
  models[MODEL_TYPES] = GTK_TREE_MODEL (gtk_tree_model_types_new ());
Havoc Pennington's avatar
Havoc Pennington committed
675 676 677 678 679
#endif
  models[MODEL_LIST] = create_list_model ();
  models[MODEL_TREE] = create_tree_model ();

  model = create_list_model ();
680
  models[MODEL_SORTED_LIST] = gtk_tree_model_sort_new_with_model (model);
Manish Singh's avatar
Manish Singh committed
681
  g_object_unref (model);
Havoc Pennington's avatar
Havoc Pennington committed
682 683

  model = create_tree_model ();
684
  models[MODEL_SORTED_TREE] = gtk_tree_model_sort_new_with_model (model);
Manish Singh's avatar
Manish Singh committed
685
  g_object_unref (model);
686

687 688
  models[MODEL_EMPTY_LIST] = GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_INT));
  models[MODEL_EMPTY_TREE] = GTK_TREE_MODEL (gtk_tree_store_new (1, G_TYPE_INT));
Havoc Pennington's avatar
Havoc Pennington committed
689 690
  
  models[MODEL_NULL] = NULL;
691 692

  run_automated_tests ();
Havoc Pennington's avatar
Havoc Pennington committed
693
  
694
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
695
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
696
  gtk_window_set_default_size (GTK_WINDOW (window), 430, 400);
697

698
  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
699

700
  gtk_container_add (GTK_CONTAINER (window), box);
701 702

  tv = gtk_tree_view_new_with_model (models[0]);
703 704
  g_signal_connect (tv, "row-activated", G_CALLBACK (on_row_activated), NULL);

705 706 707 708 709 710 711 712 713 714
  gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (tv),
					  GDK_BUTTON1_MASK,
					  row_targets,
					  G_N_ELEMENTS (row_targets),
					  GDK_ACTION_MOVE | GDK_ACTION_COPY);

  gtk_tree_view_enable_model_drag_dest (GTK_TREE_VIEW (tv),
					row_targets,
					G_N_ELEMENTS (row_targets),
					GDK_ACTION_MOVE | GDK_ACTION_COPY);
715 716
  
  /* Model menu */
717
  combo_box = gtk_combo_box_text_new ();
718
  gtk_widget_set_halign (combo_box, GTK_ALIGN_CENTER);
719
  for (i = 0; i < MODEL_LAST; i++)
720 721
      gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), model_names[i]);

722
  gtk_container_add (GTK_CONTAINER (box), combo_box);
723
  g_signal_connect (combo_box,
Manish Singh's avatar
Manish Singh committed
724 725 726
                    "changed",
                    G_CALLBACK (model_selected),
		    tv);
727 728
  
  /* Columns menu */
729
  combo_box = gtk_combo_box_text_new ();
730
  gtk_widget_set_halign (combo_box, GTK_ALIGN_CENTER);
731
  for (i = 0; i < COLUMNS_LAST; i++)
732 733
      gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), column_type_names[i]);

734
  gtk_container_add (GTK_CONTAINER (box), combo_box);
735 736

  set_columns_type (GTK_TREE_VIEW (tv), COLUMNS_LOTS);
737 738 739
  gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), COLUMNS_LOTS);

  g_signal_connect (combo_box,
Manish Singh's avatar
Manish Singh committed
740 741 742
                    "changed",
                    G_CALLBACK (columns_selected),
                    tv);
Havoc Pennington's avatar
Havoc Pennington committed
743 744
  
  sw = gtk_scrolled_window_new (NULL, NULL);
745 746
  gtk_widget_set_hexpand (sw, TRUE);
  gtk_widget_set_vexpand (sw, TRUE);
Havoc Pennington's avatar
Havoc Pennington committed
747 748 749
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);
Havoc Pennington's avatar
Havoc Pennington committed
750
  
751
  gtk_container_add (GTK_CONTAINER (box), sw);
Havoc Pennington's avatar
Havoc Pennington committed
752 753
  
  gtk_container_add (GTK_CONTAINER (sw), tv);
Havoc Pennington's avatar
Havoc Pennington committed
754
  
Havoc Pennington's avatar
Havoc Pennington committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
  gtk_widget_show_all (window);
  
  gtk_main ();

  return 0;
}

/*
 * GtkTreeModelTypes
 */

static void         gtk_tree_model_types_init                 (GtkTreeModelTypes      *model_types);
static void         gtk_tree_model_types_tree_model_init      (GtkTreeModelIface   *iface);
static gint         gtk_real_model_types_get_n_columns   (GtkTreeModel        *tree_model);
static GType        gtk_real_model_types_get_column_type (GtkTreeModel        *tree_model,
							   gint                 index);
static GtkTreePath *gtk_real_model_types_get_path        (GtkTreeModel        *tree_model,
							   GtkTreeIter         *iter);
static void         gtk_real_model_types_get_value       (GtkTreeModel        *tree_model,
							   GtkTreeIter         *iter,
							   gint                 column,
							   GValue              *value);
static gboolean     gtk_real_model_types_iter_next       (GtkTreeModel        *tree_model,
							   GtkTreeIter         *iter);
static gboolean     gtk_real_model_types_iter_children   (GtkTreeModel        *tree_model,
							   GtkTreeIter         *iter,
							   GtkTreeIter         *parent);
static gboolean     gtk_real_model_types_iter_has_child  (GtkTreeModel        *tree_model,
							   GtkTreeIter         *iter);
static gint         gtk_real_model_types_iter_n_children (GtkTreeModel        *tree_model,
							   GtkTreeIter         *iter);
static gboolean     gtk_real_model_types_iter_nth_child  (GtkTreeModel        *tree_model,
							   GtkTreeIter         *iter,
							   GtkTreeIter         *parent,
							   gint                 n);
static gboolean     gtk_real_model_types_iter_parent     (GtkTreeModel        *tree_model,
							   GtkTreeIter         *iter,
							   GtkTreeIter         *child);


Manish Singh's avatar
Manish Singh committed
795
GType
Havoc Pennington's avatar
Havoc Pennington committed
796 797
gtk_tree_model_types_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
798
  static GType model_types_type = 0;
Havoc Pennington's avatar
Havoc Pennington committed
799 800 801

  if (!model_types_type)
    {
802
      const GTypeInfo model_types_info =
Havoc Pennington's avatar
Havoc Pennington committed
803 804 805 806
      {
        sizeof (GtkTreeModelTypesClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
807
        NULL,           /* class_init */
Havoc Pennington's avatar
Havoc Pennington committed
808 809 810 811 812 813 814
	NULL,		/* class_finalize */
	NULL,		/* class_data */
        sizeof (GtkTreeModelTypes),
	0,
        (GInstanceInitFunc) gtk_tree_model_types_init
      };

815
      const GInterfaceInfo tree_model_info =
Havoc Pennington's avatar
Havoc Pennington committed
816 817 818 819 820 821
      {
	(GInterfaceInitFunc) gtk_tree_model_types_tree_model_init,
	NULL,
	NULL
      };

Manish Singh's avatar
Manish Singh committed
822 823 824
      model_types_type = g_type_register_static (G_TYPE_OBJECT,
						 "GtkTreeModelTypes",
						 &model_types_info, 0);
Havoc Pennington's avatar
Havoc Pennington committed
825 826 827 828 829 830 831 832 833 834 835 836 837
      g_type_add_interface_static (model_types_type,
				   GTK_TYPE_TREE_MODEL,
				   &tree_model_info);
    }

  return model_types_type;
}

GtkTreeModelTypes *
gtk_tree_model_types_new (void)
{
  GtkTreeModelTypes *retval;

838
  retval = g_object_new (GTK_TYPE_MODEL_TYPES, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 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 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960

  return retval;
}

static void
gtk_tree_model_types_tree_model_init (GtkTreeModelIface *iface)
{
  iface->get_n_columns = gtk_real_model_types_get_n_columns;
  iface->get_column_type = gtk_real_model_types_get_column_type;
  iface->get_path = gtk_real_model_types_get_path;
  iface->get_value = gtk_real_model_types_get_value;
  iface->iter_next = gtk_real_model_types_iter_next;
  iface->iter_children = gtk_real_model_types_iter_children;
  iface->iter_has_child = gtk_real_model_types_iter_has_child;
  iface->iter_n_children = gtk_real_model_types_iter_n_children;
  iface->iter_nth_child = gtk_real_model_types_iter_nth_child;
  iface->iter_parent = gtk_real_model_types_iter_parent;
}

static void
gtk_tree_model_types_init (GtkTreeModelTypes *model_types)
{
  model_types->stamp = g_random_int ();
}

static GType column_types[] = {
  G_TYPE_STRING, /* GType */
  G_TYPE_STRING  /* type name */
};
  
static gint
gtk_real_model_types_get_n_columns (GtkTreeModel *tree_model)
{
  return G_N_ELEMENTS (column_types);
}

static GType
gtk_real_model_types_get_column_type (GtkTreeModel *tree_model,
                                      gint          index)
{
  g_return_val_if_fail (index < G_N_ELEMENTS (column_types), G_TYPE_INVALID);
  
  return column_types[index];
}

#if 0
/* Use default implementation of this */
static gboolean
gtk_real_model_types_get_iter (GtkTreeModel *tree_model,
                               GtkTreeIter  *iter,
                               GtkTreePath  *path)
{
  
}
#endif

/* The toplevel nodes of the tree are the reserved types, G_TYPE_NONE through
 * G_TYPE_RESERVED_FUNDAMENTAL.
 */

static GtkTreePath *
gtk_real_model_types_get_path (GtkTreeModel *tree_model,
                               GtkTreeIter  *iter)
{
  GtkTreePath *retval;
  GType type;
  GType parent;
  
  g_return_val_if_fail (GTK_IS_TREE_MODEL_TYPES (tree_model), NULL);
  g_return_val_if_fail (iter != NULL, NULL);

  type = GPOINTER_TO_INT (iter->user_data);
  
  retval = gtk_tree_path_new ();
  
  parent = g_type_parent (type);
  while (parent != G_TYPE_INVALID)
    {
      GType* children = g_type_children (parent, NULL);
      gint i = 0;

      if (!children || children[0] == G_TYPE_INVALID)
        {
          g_warning ("bad iterator?");
          return NULL;
        }
      
      while (children[i] != type)
        ++i;

      gtk_tree_path_prepend_index (retval, i);

      g_free (children);
      
      type = parent;
      parent = g_type_parent (parent);
    }

  /* The fundamental type itself is the index on the toplevel */
  gtk_tree_path_prepend_index (retval, type);

  return retval;
}

static void
gtk_real_model_types_get_value (GtkTreeModel *tree_model,
                                GtkTreeIter  *iter,
                                gint          column,
                                GValue       *value)
{
  GType type;

  type = GPOINTER_TO_INT (iter->user_data);

  switch (column)
    {
    case 0:
      {
        gchar *str;
        
        g_value_init (value, G_TYPE_STRING);

961
        str = g_strdup_printf ("%ld", (long int) type);
Havoc Pennington's avatar
Havoc Pennington committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
        g_value_set_string (value, str);
        g_free (str);
      }
      break;

    case 1:
      g_value_init (value, G_TYPE_STRING);
      g_value_set_string (value, g_type_name (type));
      break;

    default:
      g_warning ("Bad column %d requested", column);
    }
}

static gboolean
gtk_real_model_types_iter_next (GtkTreeModel  *tree_model,
                                GtkTreeIter   *iter)
{
  
  GType parent;
  GType type;

  type = GPOINTER_TO_INT (iter->user_data);

  parent = g_type_parent (type);
988
  
Havoc Pennington's avatar
Havoc Pennington committed
989 990
  if (parent == G_TYPE_INVALID)
    {
991 992 993 994 995 996 997 998
      /* find next _valid_ fundamental type */
      do
	type++;
      while (!g_type_name (type) && type <= G_TYPE_FUNDAMENTAL_MAX);
      if (type <= G_TYPE_FUNDAMENTAL_MAX)
	{
	  /* found one */
          iter->user_data = GINT_TO_POINTER (type);
Havoc Pennington's avatar
Havoc Pennington committed
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 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
          return TRUE;
        }
      else
        return FALSE;
    }
  else
    {
      GType* children = g_type_children (parent, NULL);
      gint i = 0;

      g_assert (children != NULL);
      
      while (children[i] != type)
        ++i;
  
      ++i;

      if (children[i] != G_TYPE_INVALID)
        {
          g_free (children);
          iter->user_data = GINT_TO_POINTER (children[i]);
          return TRUE;
        }
      else
        {
          g_free (children);
          return FALSE;
        }
    }
}

static gboolean
gtk_real_model_types_iter_children (GtkTreeModel *tree_model,
                                    GtkTreeIter  *iter,
                                    GtkTreeIter  *parent)
{
  GType type;
  GType* children;
  
  type = GPOINTER_TO_INT (parent->user_data);

  children = g_type_children (type, NULL);

  if (!children || children[0] == G_TYPE_INVALID)
    {
      g_free (children);
      return FALSE;
    }
  else
    {
      iter->user_data = GINT_TO_POINTER (children[0]);
      g_free (children);
      return TRUE;
    }
}

static gboolean
gtk_real_model_types_iter_has_child (GtkTreeModel *tree_model,
                                     GtkTreeIter  *iter)
{
  GType type;
  GType* children;
  
  type = GPOINTER_TO_INT (iter->user_data);
  
  children = g_type_children (type, NULL);

  if (!children || children[0] == G_TYPE_INVALID)
    {
      g_free (children);
      return FALSE;
    }
  else
    {
      g_free (children);
      return TRUE;
    }
}

static gint
gtk_real_model_types_iter_n_children (GtkTreeModel *tree_model,
                                      GtkTreeIter  *iter)
{
  if (iter == NULL)
    {
1084
      return G_TYPE_FUNDAMENTAL_MAX;
Havoc Pennington's avatar
Havoc Pennington committed
1085 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
    }
  else
    {
      GType type;
      GType* children;
      guint n_children = 0;

      type = GPOINTER_TO_INT (iter->user_data);
      
      children = g_type_children (type, &n_children);
      
      g_free (children);
      
      return n_children;
    }
}

static gboolean
gtk_real_model_types_iter_nth_child (GtkTreeModel *tree_model,
                                     GtkTreeIter  *iter,
                                     GtkTreeIter  *parent,
                                     gint          n)
{  
  if (parent == NULL)
    {
      /* fundamental type */
1111
      if (n < G_TYPE_FUNDAMENTAL_MAX)
Havoc Pennington's avatar
Havoc Pennington committed
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
        {
          iter->user_data = GINT_TO_POINTER (n);
          return TRUE;
        }
      else
        return FALSE;
    }
  else
    {
      GType type = GPOINTER_TO_INT (parent->user_data);      
      guint n_children = 0;
      GType* children = g_type_children (type, &n_children);

      if (n_children == 0)
        {
          g_free (children);
          return FALSE;
        }
      else if (n >= n_children)
        {
          g_free (children);
          return FALSE;
        }
      else
        {
          iter->user_data = GINT_TO_POINTER (children[n]);
          g_free (children);

          return TRUE;
        }
    }
}

static gboolean
gtk_real_model_types_iter_parent (GtkTreeModel *tree_model,
                                  GtkTreeIter  *iter,
                                  GtkTreeIter  *child)
{
  GType type;
  GType parent;
  
  type = GPOINTER_TO_INT (child->user_data);
  
  parent = g_type_parent (type);
  
  if (parent == G_TYPE_INVALID)
    {
1159
      if (type > G_TYPE_FUNDAMENTAL_MAX)
1160 1161 1162
        g_warning ("no parent for %ld %s\n",
                   (long int) type,
                   g_type_name (type));
Havoc Pennington's avatar
Havoc Pennington committed
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
      return FALSE;
    }
  else
    {
      iter->user_data = GINT_TO_POINTER (parent);
      
      return TRUE;
    }
}

1173 1174 1175 1176
/*
 * Automated testing
 */

1177 1178
#if 0

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 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
static void
treestore_torture_recurse (GtkTreeStore *store,
                           GtkTreeIter  *root,
                           gint          depth)
{
  GtkTreeModel *model;
  gint i;
  GtkTreeIter iter;  
  
  model = GTK_TREE_MODEL (store);    

  if (depth > 2)
    return;

  ++depth;

  gtk_tree_store_append (store, &iter, root);
  
  gtk_tree_model_iter_children (model, &iter, root);
  
  i = 0;
  while (i < 100)
    {
      gtk_tree_store_append (store, &iter, root);
      ++i;
    }

  while (gtk_tree_model_iter_children (model, &iter, root))
    gtk_tree_store_remove (store, &iter);

  gtk_tree_store_append (store, &iter, root);

  /* inserts before last node in tree */
  i = 0;
  while (i < 100)
    {
      gtk_tree_store_insert_before (store, &iter, root, &iter);
      ++i;
    }

  /* inserts after the node before the last node */
  i = 0;
  while (i < 100)
    {
      gtk_tree_store_insert_after (store, &iter, root, &iter);
      ++i;
    }

  /* inserts after the last node */
  gtk_tree_store_append (store, &iter, root);
    
  i = 0;
  while (i < 100)
    {
      gtk_tree_store_insert_after (store, &iter, root, &iter);
      ++i;
    }

  /* remove everything again */
  while (gtk_tree_model_iter_children (model, &iter, root))
    gtk_tree_store_remove (store, &iter);


    /* Prepends */
  gtk_tree_store_prepend (store, &iter, root);
    
  i = 0;
  while (i < 100)
    {
      gtk_tree_store_prepend (store, &iter, root);
      ++i;
    }

  /* remove everything again */
  while (gtk_tree_model_iter_children (model, &iter, root))
    gtk_tree_store_remove (store, &iter);

  gtk_tree_store_append (store, &iter, root);
  gtk_tree_store_append (store, &iter, root);
  gtk_tree_store_append (store, &iter, root);
  gtk_tree_store_append (store, &iter, root);

  while (gtk_tree_model_iter_children (model, &iter, root))
    {
      treestore_torture_recurse (store, &iter, depth);
      gtk_tree_store_remove (store, &iter);
    }
}

1268 1269
#endif

1270 1271 1272
static void
run_automated_tests (void)
{
1273 1274
  g_print ("Running automated tests...\n");
  
1275 1276
  /* FIXME TreePath basic verification */

1277 1278 1279 1280 1281 1282 1283 1284 1285
  /* FIXME generic consistency checks on the models */

  {
    /* Make sure list store mutations don't crash anything */
    GtkListStore *store;
    GtkTreeModel *model;
    gint i;
    GtkTreeIter iter;
    
1286
    store = gtk_list_store_new (1, G_TYPE_INT);
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296

    model = GTK_TREE_MODEL (store);
    
    i = 0;
    while (i < 100)
      {
        gtk_list_store_append (store, &iter);
        ++i;
      }

1297
    while (gtk_tree_model_get_iter_first (model, &iter))
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
      gtk_list_store_remove (store, &iter);

    gtk_list_store_append (store, &iter);

    /* inserts before last node in list */
    i = 0;
    while (i < 100)
      {
        gtk_list_store_insert_before (store, &iter, &iter);
        ++i;
      }

    /* inserts after the node before the last node */
    i = 0;
    while (i < 100)
      {
        gtk_list_store_insert_after (store, &iter, &iter);
        ++i;
      }

    /* inserts after the last node */
    gtk_list_store_append (store, &iter);
    
    i = 0;
    while (i < 100)
      {
        gtk_list_store_insert_after (store, &iter, &iter);
        ++i;
      }

    /* remove everything again */
1329
    while (gtk_tree_model_get_iter_first (model, &iter))
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
      gtk_list_store_remove (store, &iter);


    /* Prepends */
    gtk_list_store_prepend (store, &iter);
    
    i = 0;
    while (i < 100)
      {
        gtk_list_store_prepend (store, &iter);
        ++i;
      }

    /* remove everything again */
1344
    while (gtk_tree_model_get_iter_first (model, &iter))
1345 1346
      gtk_list_store_remove (store, &iter);
    
Manish Singh's avatar
Manish Singh committed
1347
    g_object_unref (store);
1348 1349 1350 1351 1352
  }

  {
    /* Make sure tree store mutations don't crash anything */
    GtkTreeStore *store;
1353 1354
    GtkTreeIter root;

1355
    store = gtk_tree_store_new (1, G_TYPE_INT);
1356 1357 1358
    gtk_tree_store_append (GTK_TREE_STORE (store), &root, NULL);
    /* Remove test until it is rewritten to work */
    /*    treestore_torture_recurse (store, &root, 0);*/
1359
    
Manish Singh's avatar
Manish Singh committed
1360
    g_object_unref (store);
1361 1362 1363
  }

  g_print ("Passed.\n");
1364
}