]> Pileus Git - ~andy/gtk/blob - tests/testtreeflow.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testtreeflow.c
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 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
47 initialize_model (void)
48 {
49   gint 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
64 futz_row (void)
65 {
66   gint 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     }
106 }
107
108 static gboolean
109 futz (void)
110 {
111   gint i;
112
113   for (i = 0; i < 15; i++)
114     futz_row ();
115   g_print ("Number of rows: %d\n", gtk_tree_model_iter_n_children (model, NULL));
116   return TRUE;
117 }
118
119 int
120 main (int argc, char *argv[])
121 {
122   GtkWidget *window;
123   GtkWidget *vbox;
124   GtkWidget *scrolled_window;
125   GtkWidget *tree_view;
126   GtkWidget *hbox;
127   GtkWidget *button;
128   GtkTreePath *path;
129
130   gtk_init (&argc, &argv);
131
132   path = gtk_tree_path_new_from_string ("80");
133   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
134   gtk_window_set_title (GTK_WINDOW (window), "Reflow test");
135   g_signal_connect (window, "destroy", gtk_main_quit, NULL);
136   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
137   gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
138   gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Incremental Reflow Test"), FALSE, FALSE, 0);
139   gtk_container_add (GTK_CONTAINER (window), vbox);
140   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
141   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
142                                   GTK_POLICY_AUTOMATIC,
143                                   GTK_POLICY_AUTOMATIC);
144   gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
145   
146   initialize_model ();
147   tree_view = gtk_tree_view_new_with_model (model);
148   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tree_view), path, NULL, TRUE, 0.5, 0.0);
149   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
150   gtk_tree_selection_select_path (selection, path);
151   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE);
152   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
153   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
154                                                -1,
155                                                NULL,
156                                                gtk_cell_renderer_text_new (),
157                                                "text", TEXT_COLUMN,
158                                                NULL);
159   gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
160   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
161   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
162   button = gtk_button_new_with_mnemonic ("<b>_Futz!!</b>");
163   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
164   gtk_label_set_use_markup (GTK_LABEL (gtk_bin_get_child (GTK_BIN (button))), TRUE);
165   g_signal_connect (button, "clicked", G_CALLBACK (futz), NULL);
166   g_signal_connect (button, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
167   gtk_window_set_default_size (GTK_WINDOW (window), 300, 400);
168   gtk_widget_show_all (window);
169   gdk_threads_add_timeout (1000, (GSourceFunc) futz, NULL);
170   gtk_main ();
171   return 0;
172 }