testtreeview.c 36.5 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 21
#include "prop-editor.h"
#include <gtk/gtk.h>
22
#include <stdlib.h>
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 53

/* 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
54 55 56

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

Manish Singh's avatar
Manish Singh committed
57 58 59 60 61
#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
62 63 64 65 66 67

typedef struct _GtkTreeModelTypes       GtkTreeModelTypes;
typedef struct _GtkTreeModelTypesClass  GtkTreeModelTypesClass;

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

  gint stamp;
};

struct _GtkTreeModelTypesClass
{
Manish Singh's avatar
Manish Singh committed
75
  GObjectClass parent_class;
Havoc Pennington's avatar
Havoc Pennington committed
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 124

  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
125
GType              gtk_tree_model_types_get_type      (void) G_GNUC_CONST;
Havoc Pennington's avatar
Havoc Pennington committed
126 127
GtkTreeModelTypes *gtk_tree_model_types_new           (void);

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

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

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#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;
158 159
#define BOOL_COLUMN 7
      column_types[BOOL_COLUMN] = G_TYPE_BOOLEAN;
160 161 162 163 164 165
      column_types[8] = G_TYPE_INT;
    }

  return column_types;
}

166 167 168 169 170
static void
col_clicked_cb (GtkTreeViewColumn *col, gpointer data)
{
  GtkWindow *win;

171
  win = GTK_WINDOW (create_prop_editor (G_OBJECT (col), GTK_TYPE_TREE_VIEW_COLUMN));
172 173 174 175 176 177 178

  gtk_window_set_title (win, gtk_tree_view_column_get_title (col));
}

static void
setup_column (GtkTreeViewColumn *col)
{
179
  gtk_tree_view_column_set_clickable (col, TRUE);
Manish Singh's avatar
Manish Singh committed
180
  g_signal_connect (col,
181
		    "clicked",
Manish Singh's avatar
Manish Singh committed
182
		    G_CALLBACK (col_clicked_cb),
183
		    NULL);
184 185
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
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))
    {
227
      gtk_tree_model_get (GTK_TREE_MODEL (model),
228 229 230 231 232 233 234 235 236 237 238 239 240
                          &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))
    {
241
      gtk_tree_model_get (GTK_TREE_MODEL (model),
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
                          &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)));
}

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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
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)));
}
318 319 320

static ColumnsType current_column_type = COLUMNS_LOTS;

321 322 323 324 325
static void
set_columns_type (GtkTreeView *tree_view, ColumnsType type)
{
  GtkTreeViewColumn *col;
  GtkCellRenderer *rend;
Havoc Pennington's avatar
Havoc Pennington committed
326 327
  GdkPixbuf *pixbuf;
  GtkWidget *image;
328
  GtkAdjustment *adjustment;
329

330 331
  current_column_type = type;
  
332 333 334 335 336 337 338 339
  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);
    }

340 341
  gtk_tree_view_set_rules_hint (tree_view, FALSE);
  
342 343 344 345 346
  switch (type)
    {
    case COLUMNS_NONE:
      break;

347 348 349 350
    case COLUMNS_LOTS:
      /* with lots of columns we need to turn on rules */
      gtk_tree_view_set_rules_hint (tree_view, TRUE);
      
351
      rend = gtk_cell_renderer_text_new ();
352

353 354 355 356
      col = gtk_tree_view_column_new_with_attributes ("Column 1",
                                                      rend,
                                                      "text", 1,
                                                      NULL);
357
      setup_column (col);
358 359 360
      
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
      
361 362
      col = gtk_tree_view_column_new();
      gtk_tree_view_column_set_title (col, "Column 2");
363
      
364 365 366 367 368 369
      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);
370 371

      setup_column (col);
372
      
373 374
      
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
375
      gtk_tree_view_set_expander_column (tree_view, col);
376
      
377 378
      rend = gtk_cell_renderer_toggle_new ();

Manish Singh's avatar
Manish Singh committed
379 380
      g_signal_connect (rend, "toggled",
			G_CALLBACK (toggled_callback), tree_view);
381 382 383 384 385 386 387 388 389
      
      col = gtk_tree_view_column_new_with_attributes ("Column 3",
                                                      rend,
                                                      "active", BOOL_COLUMN,
                                                      NULL);

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

391
      pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **)book_closed_xpm);
Havoc Pennington's avatar
Havoc Pennington committed
392 393 394

      image = gtk_image_new_from_pixbuf (pixbuf);

Manish Singh's avatar
Manish Singh committed
395
      g_object_unref (pixbuf);
396
      
Havoc Pennington's avatar
Havoc Pennington committed
397 398 399
      gtk_widget_show (image);
      
      gtk_tree_view_column_set_widget (col, image);
400 401 402 403 404 405
      
      rend = gtk_cell_renderer_toggle_new ();

      /* you could also set this per-row by tying it to a column
       * in the model of course.
       */
406
      g_object_set (rend, "radio", TRUE, NULL);
407
      
Manish Singh's avatar
Manish Singh committed
408
      g_signal_connect (rend, "toggled",
409
			G_CALLBACK (toggled_callback), tree_view);
410 411 412 413 414 415 416 417 418
      
      col = gtk_tree_view_column_new_with_attributes ("Column 4",
                                                      rend,
                                                      "active", BOOL_COLUMN,
                                                      NULL);

      setup_column (col);
      
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
419

420 421 422 423 424 425 426 427 428
      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);

429 430
      col = gtk_tree_view_column_new_with_attributes ("Column 5",
                                                      rend,
431
                                                      "text", 4,
432 433 434 435 436
                                                      NULL);

      setup_column (col);
      
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
437
#if 0
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
      
      rend = gtk_cell_renderer_text_new ();
      
      col = gtk_tree_view_column_new_with_attributes ("Column 6",
                                                      rend,
                                                      "text", 4,
                                                      NULL);

      setup_column (col);
      
      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);

      setup_column (col);
      
      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);

      setup_column (col);
      
      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);

      setup_column (col);
      
      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);

      setup_column (col);
      
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), col);
      
#endif
      
496 497 498 499 500 501 502 503 504
      /* 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);
505 506

      setup_column (col);
507 508 509 510 511 512 513
      
      gtk_tree_view_insert_column (GTK_TREE_VIEW (tree_view), col, 0);
    default:
      break;
    }
}

514 515 516 517 518 519
static ColumnsType
get_columns_type (void)
{
  return current_column_type;
}

520 521
static GdkPixbuf *our_pixbuf;
  
Havoc Pennington's avatar
Havoc Pennington committed
522 523
typedef enum
{
Havoc Pennington's avatar
Havoc Pennington committed
524 525 526 527 528
  /*   MODEL_TYPES, */
  MODEL_TREE,
  MODEL_LIST,
  MODEL_SORTED_TREE,
  MODEL_SORTED_LIST,
529 530
  MODEL_EMPTY_LIST,
  MODEL_EMPTY_TREE,
Havoc Pennington's avatar
Havoc Pennington committed
531 532
  MODEL_NULL,
  MODEL_LAST
Havoc Pennington's avatar
Havoc Pennington committed
533 534
} ModelType;

Havoc Pennington's avatar
Havoc Pennington committed
535
/* FIXME add a custom model to test */
Havoc Pennington's avatar
Havoc Pennington committed
536
static GtkTreeModel *models[MODEL_LAST];
Havoc Pennington's avatar
Havoc Pennington committed
537 538 539 540 541
static const char *model_names[MODEL_LAST] = {
  "GtkTreeStore",
  "GtkListStore",
  "GtkTreeModelSort wrapping GtkTreeStore",
  "GtkTreeModelSort wrapping GtkListStore",
542 543
  "Empty GtkListStore",
  "Empty GtkTreeStore",
Havoc Pennington's avatar
Havoc Pennington committed
544 545 546 547 548 549 550 551 552
  "NULL (no model)"
};

static GtkTreeModel*
create_list_model (void)
{
  GtkListStore *store;
  GtkTreeIter iter;
  gint i;
553 554 555
  GType *t;

  t = get_model_types ();
Havoc Pennington's avatar
Havoc Pennington committed
556
  
557 558 559 560
  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
561 562 563 564 565 566 567 568 569 570

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

      msg = g_strdup_printf ("%d", i);
      
571 572 573 574 575 576
      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
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

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

599
  str = g_strdup_printf ("%ld", (glong)type);
600 601 602 603 604 605
  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
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
  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;
626
  GType *t;
Havoc Pennington's avatar
Havoc Pennington committed
627
  
628
  /* Make the tree more interesting */
629 630 631 632
  /* - 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 ()));
633
  g_type_class_unref (g_type_class_ref (gtk_scrollbar_get_type ()));
634
  g_type_class_unref (g_type_class_ref (pango_layout_get_type ()));
635 636 637

  t = get_model_types ();
  
638 639 640 641
  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
642 643

  i = 0;
644
  while (i < G_TYPE_FUNDAMENTAL_MAX)
Havoc Pennington's avatar
Havoc Pennington committed
645 646 647 648 649 650 651 652 653 654
    {
      typesystem_recurse (i, NULL, store);
      
      ++i;
    }

  return GTK_TREE_MODEL (store);
}

static void
655
model_selected (GtkComboBox *combo_box, gpointer data)
Havoc Pennington's avatar
Havoc Pennington committed
656 657 658 659
{
  GtkTreeView *tree_view = GTK_TREE_VIEW (data);
  gint hist;

660
  hist = gtk_combo_box_get_active (combo_box);
Havoc Pennington's avatar
Havoc Pennington committed
661 662 663 664 665 666

  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
667

668
static void
669
columns_selected (GtkComboBox *combo_box, gpointer data)
670 671 672 673
{
  GtkTreeView *tree_view = GTK_TREE_VIEW (data);
  gint hist;

674
  hist = gtk_combo_box_get_active (combo_box);
675 676 677 678 679 680 681

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

682 683 684 685 686 687 688 689
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
690 691 692

enum
{
693
  TARGET_GTK_TREE_MODEL_ROW
Havoc Pennington's avatar
Havoc Pennington committed
694 695 696
};

static GtkTargetEntry row_targets[] = {
697 698
  { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_APP,
    TARGET_GTK_TREE_MODEL_ROW }
Havoc Pennington's avatar
Havoc Pennington committed
699 700
};

Havoc Pennington's avatar
Havoc Pennington committed
701 702 703 704 705 706 707
int
main (int    argc,
      char **argv)
{
  GtkWidget *window;
  GtkWidget *sw;
  GtkWidget *tv;
708
  GtkWidget *box;
709
  GtkWidget *combo_box;
Havoc Pennington's avatar
Havoc Pennington committed
710
  GtkTreeModel *model;
Havoc Pennington's avatar
Havoc Pennington committed
711 712 713 714
  gint i;
  
  gtk_init (&argc, &argv);

715 716 717
  if (g_getenv ("RTL"))
    gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);

718 719
  our_pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) book_closed_xpm);  
  
Havoc Pennington's avatar
Havoc Pennington committed
720
#if 0
Havoc Pennington's avatar
Havoc Pennington committed
721
  models[MODEL_TYPES] = GTK_TREE_MODEL (gtk_tree_model_types_new ());
Havoc Pennington's avatar
Havoc Pennington committed
722 723 724 725 726
#endif
  models[MODEL_LIST] = create_list_model ();
  models[MODEL_TREE] = create_tree_model ();

  model = create_list_model ();
727
  models[MODEL_SORTED_LIST] = gtk_tree_model_sort_new_with_model (model);
Manish Singh's avatar
Manish Singh committed
728
  g_object_unref (model);
Havoc Pennington's avatar
Havoc Pennington committed
729 730

  model = create_tree_model ();
731
  models[MODEL_SORTED_TREE] = gtk_tree_model_sort_new_with_model (model);
Manish Singh's avatar
Manish Singh committed
732
  g_object_unref (model);
733

734 735
  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
736 737
  
  models[MODEL_NULL] = NULL;
738 739

  run_automated_tests ();
Havoc Pennington's avatar
Havoc Pennington committed
740
  
741
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
742
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
743
  gtk_window_set_default_size (GTK_WINDOW (window), 430, 400);
744

745
  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
746

747
  gtk_container_add (GTK_CONTAINER (window), box);
748 749

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

752 753 754 755 756 757 758 759 760 761
  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);
762 763
  
  /* Model menu */
764
  combo_box = gtk_combo_box_text_new ();
765
  gtk_widget_set_halign (combo_box, GTK_ALIGN_CENTER);
766
  for (i = 0; i < MODEL_LAST; i++)
767 768
      gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), model_names[i]);

769
  gtk_container_add (GTK_CONTAINER (box), combo_box);
770
  g_signal_connect (combo_box,
Manish Singh's avatar
Manish Singh committed
771 772 773
                    "changed",
                    G_CALLBACK (model_selected),
		    tv);
774 775
  
  /* Columns menu */
776
  combo_box = gtk_combo_box_text_new ();
777
  gtk_widget_set_halign (combo_box, GTK_ALIGN_CENTER);
778
  for (i = 0; i < COLUMNS_LAST; i++)
779 780
      gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), column_type_names[i]);

781
  gtk_container_add (GTK_CONTAINER (box), combo_box);
782 783

  set_columns_type (GTK_TREE_VIEW (tv), COLUMNS_LOTS);
784 785 786
  gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), COLUMNS_LOTS);

  g_signal_connect (combo_box,
Manish Singh's avatar
Manish Singh committed
787 788 789
                    "changed",
                    G_CALLBACK (columns_selected),
                    tv);
Havoc Pennington's avatar
Havoc Pennington committed
790 791
  
  sw = gtk_scrolled_window_new (NULL, NULL);
792 793
  gtk_widget_set_hexpand (sw, TRUE);
  gtk_widget_set_vexpand (sw, TRUE);
Havoc Pennington's avatar
Havoc Pennington committed
794 795 796
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);
Havoc Pennington's avatar
Havoc Pennington committed
797
  
798
  gtk_container_add (GTK_CONTAINER (box), sw);
Havoc Pennington's avatar
Havoc Pennington committed
799 800
  
  gtk_container_add (GTK_CONTAINER (sw), tv);
Havoc Pennington's avatar
Havoc Pennington committed
801
  
802 803 804
  create_prop_editor (G_OBJECT (tv), GTK_TYPE_TREE_VIEW);
  create_prop_editor (G_OBJECT (gtk_tree_view_get_selection (GTK_TREE_VIEW (tv))), GTK_TYPE_TREE_SELECTION);

Havoc Pennington's avatar
Havoc Pennington committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
  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
845
GType
Havoc Pennington's avatar
Havoc Pennington committed
846 847
gtk_tree_model_types_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
848
  static GType model_types_type = 0;
Havoc Pennington's avatar
Havoc Pennington committed
849 850 851

  if (!model_types_type)
    {
852
      const GTypeInfo model_types_info =
Havoc Pennington's avatar
Havoc Pennington committed
853 854 855 856
      {
        sizeof (GtkTreeModelTypesClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
857
        NULL,           /* class_init */
Havoc Pennington's avatar
Havoc Pennington committed
858 859 860 861 862 863 864
	NULL,		/* class_finalize */
	NULL,		/* class_data */
        sizeof (GtkTreeModelTypes),
	0,
        (GInstanceInitFunc) gtk_tree_model_types_init
      };

865
      const GInterfaceInfo tree_model_info =
Havoc Pennington's avatar
Havoc Pennington committed
866 867 868 869 870 871
      {
	(GInterfaceInitFunc) gtk_tree_model_types_tree_model_init,
	NULL,
	NULL
      };

Manish Singh's avatar
Manish Singh committed
872 873 874
      model_types_type = g_type_register_static (G_TYPE_OBJECT,
						 "GtkTreeModelTypes",
						 &model_types_info, 0);
Havoc Pennington's avatar
Havoc Pennington committed
875 876 877 878 879 880 881 882 883 884 885 886 887
      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;

888
  retval = g_object_new (GTK_TYPE_MODEL_TYPES, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
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 961 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 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

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

1011
        str = g_strdup_printf ("%ld", (long int) type);
Havoc Pennington's avatar
Havoc Pennington committed
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
        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);
1038
  
Havoc Pennington's avatar
Havoc Pennington committed
1039 1040
  if (parent == G_TYPE_INVALID)
    {
1041 1042 1043 1044 1045 1046 1047 1048
      /* 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
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 1084 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 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
          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)
    {
1134
      return G_TYPE_FUNDAMENTAL_MAX;
Havoc Pennington's avatar
Havoc Pennington committed
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
    }
  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 */
1161
      if (n < G_TYPE_FUNDAMENTAL_MAX)
Havoc Pennington's avatar
Havoc Pennington committed
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
        {
          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)
    {
1209
      if (type > G_TYPE_FUNDAMENTAL_MAX)
1210 1211 1212
        g_warning ("no parent for %ld %s\n",
                   (long int) type,
                   g_type_name (type));
Havoc Pennington's avatar
Havoc Pennington committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
      return FALSE;
    }
  else
    {
      iter->user_data = GINT_TO_POINTER (parent);
      
      return TRUE;
    }
}

1223 1224 1225 1226
/*
 * Automated testing
 */

1227 1228
#if 0

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 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
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);
    }
}

1318 1319
#endif

1320 1321 1322
static void
run_automated_tests (void)
{
1323 1324
  g_print ("Running automated tests...\n");
  
1325 1326
  /* FIXME TreePath basic verification */

1327 1328 1329 1330 1331 1332 1333 1334 1335
  /* FIXME generic consistency checks on the models */

  {
    /* Make sure list store mutations don't crash anything */
    GtkListStore *store;
    GtkTreeModel *model;
    gint i;
    GtkTreeIter iter;
    
1336
    store = gtk_list_store_new (1, G_TYPE_INT);
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346

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

1347
    while (gtk_tree_model_get_iter_first (model, &iter))
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
      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 */
1379
    while (gtk_tree_model_get_iter_first (model, &iter))
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
      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 */
1394
    while (gtk_tree_model_get_iter_first (model, &iter))
1395 1396
      gtk_list_store_remove (store, &iter);
    
Manish Singh's avatar
Manish Singh committed
1397
    g_object_unref (store);
1398 1399 1400 1401 1402
  }

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

1405
    store = gtk_tree_store_new (1, G_TYPE_INT);
1406 1407 1408
    gtk_tree_store_append (GTK_TREE_STORE (store), &root, NULL);
    /* Remove test until it is rewritten to work */
    /*    treestore_torture_recurse (store, &root, 0);*/
1409
    
Manish Singh's avatar
Manish Singh committed
1410
    g_object_unref (store);
1411 1412 1413
  }

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