testtreeflow.c 5.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* testtreeflow.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
 */

19
#include "config.h"
20 21 22
#include <gtk/gtk.h>

GtkTreeModel *model = NULL;
23
static GRand *grand = NULL;
24
GtkTreeSelection *selection = NULL;
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
enum
{
  TEXT_COLUMN,
  NUM_COLUMNS
};

static char *words[] =
{
  "Boom",
  "Borp",
  "Multiline\ntext",
  "Bingo",
  "Veni\nVedi\nVici",
  NULL
};


#define NUM_WORDS 5
#define NUM_ROWS 100


static void
initialize_model (void)
{
  gint i;
  GtkTreeIter iter;

  model = (GtkTreeModel *) gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING);
53
  grand = g_rand_new ();
54 55 56 57
  for (i = 0; i < NUM_ROWS; i++)
    {
      gtk_list_store_append (GTK_LIST_STORE (model), &iter);
      gtk_list_store_set (GTK_LIST_STORE (model), &iter,
58
			  TEXT_COLUMN, words[g_rand_int_range (grand, 0, NUM_WORDS)],
59 60 61 62 63 64 65 66 67 68 69 70
			  -1);
    }
}

static void
futz_row (void)
{
  gint i;
  GtkTreePath *path;
  GtkTreeIter iter;
  GtkTreeIter iter2;

71
  i = g_rand_int_range (grand, 0,
72 73 74 75 76 77
			gtk_tree_model_iter_n_children (model, NULL));
  path = gtk_tree_path_new ();
  gtk_tree_path_append_index (path, i);
  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_path_free (path);

78 79
  if (gtk_tree_selection_iter_is_selected (selection, &iter))
    return;
80
  switch (g_rand_int_range (grand, 0, 3))
81 82 83
    {
    case 0:
      /* insert */
84 85 86
            gtk_list_store_insert_after (GTK_LIST_STORE (model),
            				   &iter2, &iter);
            gtk_list_store_set (GTK_LIST_STORE (model), &iter2,
87
            			  TEXT_COLUMN, words[g_rand_int_range (grand, 0, NUM_WORDS)],
88
            			  -1);
89 90 91 92 93 94 95 96 97
      break;
    case 1:
      /* delete */
      if (gtk_tree_model_iter_n_children (model, NULL) == 0)
	return;
      gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
      break;
    case 2:
      /* modify */
98
      return;
99 100 101
      if (gtk_tree_model_iter_n_children (model, NULL) == 0)
	return;
      gtk_list_store_set (GTK_LIST_STORE (model), &iter,
102
      			  TEXT_COLUMN, words[g_rand_int_range (grand, 0, NUM_WORDS)],
103
      			  -1);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      break;
    }
}

static gboolean
futz (void)
{
  gint i;

  for (i = 0; i < 15; i++)
    futz_row ();
  g_print ("Number of rows: %d\n", gtk_tree_model_iter_n_children (model, NULL));
  return TRUE;
}

int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *scrolled_window;
  GtkWidget *tree_view;
  GtkWidget *hbox;
  GtkWidget *button;
128
  GtkTreePath *path;
129 130 131

  gtk_init (&argc, &argv);

132
  path = gtk_tree_path_new_from_string ("80");
133 134
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Reflow test");
Manish Singh's avatar
Manish Singh committed
135
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);
136
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
137 138 139 140 141 142 143 144 145 146 147
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
  gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Incremental Reflow Test"), FALSE, FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
				  GTK_POLICY_AUTOMATIC,
				  GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
  
  initialize_model ();
  tree_view = gtk_tree_view_new_with_model (model);
148 149
  gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tree_view), path, NULL, TRUE, 0.5, 0.0);
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
Jonathan Blandford's avatar
Jonathan Blandford committed
150
  gtk_tree_selection_select_path (selection, path);
151 152 153 154 155 156 157 158
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
					       -1,
					       NULL,
					       gtk_cell_renderer_text_new (),
					       "text", TEXT_COLUMN,
					       NULL);
  gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
159
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
160 161 162
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  button = gtk_button_new_with_mnemonic ("<b>_Futz!!</b>");
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
Javier Jardón's avatar
Javier Jardón committed
163
  gtk_label_set_use_markup (GTK_LABEL (gtk_bin_get_child (GTK_BIN (button))), TRUE);
Manish Singh's avatar
Manish Singh committed
164 165
  g_signal_connect (button, "clicked", G_CALLBACK (futz), NULL);
  g_signal_connect (button, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
166 167
  gtk_window_set_default_size (GTK_WINDOW (window), 300, 400);
  gtk_widget_show_all (window);
168
  gdk_threads_add_timeout (1000, (GSourceFunc) futz, NULL);
169 170 171
  gtk_main ();
  return 0;
}