1 /* testtreeflow.c
2 * Copyright (C) 2001 Red Hat, Inc
3 * Author: Jonathan Blandford
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "config.h"
20 #include <gtk/gtk.h>
21
22 GtkTreeModel *model = NULL;
23 static GRand *grand = NULL;
24 GtkTreeSelection *selection = NULL;
25 enum
26 {
27 TEXT_COLUMN,
28 NUM_COLUMNS
29 };
30
31 static const char *words[] =
32 {
33 "Boom",
34 "Borp",
35 "Multiline\ntext",
36 "Bingo",
37 "Veni\nVedi\nVici",
38 NULL
39 };
40
41
42 #define NUM_WORDS 5
43 #define NUM_ROWS 100
44
45
46 static void
initialize_model(void)47 initialize_model (void)
48 {
49 int i;
50 GtkTreeIter iter;
51
52 model = (GtkTreeModel *) gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING);
53 grand = g_rand_new ();
54 for (i = 0; i < NUM_ROWS; i++)
55 {
56 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
57 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
58 TEXT_COLUMN, words[g_rand_int_range (grand, 0, NUM_WORDS)],
59 -1);
60 }
61 }
62
63 static void
futz_row(void)64 futz_row (void)
65 {
66 int i;
67 GtkTreePath *path;
68 GtkTreeIter iter;
69 GtkTreeIter iter2;
70
71 i = g_rand_int_range (grand, 0,
72 gtk_tree_model_iter_n_children (model, NULL));
73 path = gtk_tree_path_new ();
74 gtk_tree_path_append_index (path, i);
75 gtk_tree_model_get_iter (model, &iter, path);
76 gtk_tree_path_free (path);
77
78 if (gtk_tree_selection_iter_is_selected (selection, &iter))
79 return;
80 switch (g_rand_int_range (grand, 0, 3))
81 {
82 case 0:
83 /* insert */
84 gtk_list_store_insert_after (GTK_LIST_STORE (model),
85 &iter2, &iter);
86 gtk_list_store_set (GTK_LIST_STORE (model), &iter2,
87 TEXT_COLUMN, words[g_rand_int_range (grand, 0, NUM_WORDS)],
88 -1);
89 break;
90 case 1:
91 /* delete */
92 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
93 return;
94 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
95 break;
96 case 2:
97 /* modify */
98 return;
99 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
100 return;
101 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
102 TEXT_COLUMN, words[g_rand_int_range (grand, 0, NUM_WORDS)],
103 -1);
104 break;
105 default:
106 g_assert_not_reached ();
107 }
108 }
109
110 static gboolean
futz(void)111 futz (void)
112 {
113 int i;
114
115 for (i = 0; i < 15; i++)
116 futz_row ();
117 g_print ("Number of rows: %d\n", gtk_tree_model_iter_n_children (model, NULL));
118 return TRUE;
119 }
120
121 static void
quit_cb(GtkWidget * widget,gpointer data)122 quit_cb (GtkWidget *widget,
123 gpointer data)
124 {
125 gboolean *done = data;
126
127 *done = TRUE;
128
129 g_main_context_wakeup (NULL);
130 }
131
132 int
main(int argc,char * argv[])133 main (int argc, char *argv[])
134 {
135 GtkWidget *window;
136 GtkWidget *vbox;
137 GtkWidget *scrolled_window;
138 GtkWidget *tree_view;
139 GtkWidget *hbox;
140 GtkWidget *button;
141 GtkTreePath *path;
142 gboolean done = FALSE;
143
144 gtk_init ();
145
146 path = gtk_tree_path_new_from_string ("80");
147 window = gtk_window_new ();
148 gtk_window_set_title (GTK_WINDOW (window), "Reflow test");
149 g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
150 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
151 gtk_box_append (GTK_BOX (vbox), gtk_label_new ("Incremental Reflow Test"));
152 gtk_window_set_child (GTK_WINDOW (window), vbox);
153 scrolled_window = gtk_scrolled_window_new ();
154 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
155 GTK_POLICY_AUTOMATIC,
156 GTK_POLICY_AUTOMATIC);
157 gtk_widget_set_vexpand (scrolled_window, TRUE);
158 gtk_box_append (GTK_BOX (vbox), scrolled_window);
159
160 initialize_model ();
161 tree_view = gtk_tree_view_new_with_model (model);
162 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tree_view), path, NULL, TRUE, 0.5, 0.0);
163 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
164 gtk_tree_selection_select_path (selection, path);
165 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
166 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
167 -1,
168 NULL,
169 gtk_cell_renderer_text_new (),
170 "text", TEXT_COLUMN,
171 NULL);
172 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled_window), tree_view);
173 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
174 gtk_box_append (GTK_BOX (vbox), hbox);
175 button = gtk_button_new_with_mnemonic ("<b>_Futz!!</b>");
176 gtk_box_append (GTK_BOX (hbox), button);
177 gtk_label_set_use_markup (GTK_LABEL (gtk_button_get_child (GTK_BUTTON (button))), TRUE);
178 g_signal_connect (button, "clicked", G_CALLBACK (futz), NULL);
179 g_signal_connect (button, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
180 gtk_window_set_default_size (GTK_WINDOW (window), 300, 400);
181 gtk_widget_show (window);
182 g_timeout_add (1000, (GSourceFunc) futz, NULL);
183 while (!done)
184 g_main_context_iteration (NULL, TRUE);
185 return 0;
186 }
187