]> Pileus Git - ~andy/gtk/blob - tests/testtreeflow.c
gtk: remove "gboolean homogeneous" from gtk_box_new()
[~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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #include <gtk/gtk.h>
23
24 GtkTreeModel *model = NULL;
25 static GRand *grand = NULL;
26 GtkTreeSelection *selection = NULL;
27 enum
28 {
29   TEXT_COLUMN,
30   NUM_COLUMNS
31 };
32
33 static char *words[] =
34 {
35   "Boom",
36   "Borp",
37   "Multiline\ntext",
38   "Bingo",
39   "Veni\nVedi\nVici",
40   NULL
41 };
42
43
44 #define NUM_WORDS 5
45 #define NUM_ROWS 100
46
47
48 static void
49 initialize_model (void)
50 {
51   gint i;
52   GtkTreeIter iter;
53
54   model = (GtkTreeModel *) gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING);
55   grand = g_rand_new ();
56   for (i = 0; i < NUM_ROWS; i++)
57     {
58       gtk_list_store_append (GTK_LIST_STORE (model), &iter);
59       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
60                           TEXT_COLUMN, words[g_rand_int_range (grand, 0, NUM_WORDS)],
61                           -1);
62     }
63 }
64
65 static void
66 futz_row (void)
67 {
68   gint i;
69   GtkTreePath *path;
70   GtkTreeIter iter;
71   GtkTreeIter iter2;
72
73   i = g_rand_int_range (grand, 0,
74                         gtk_tree_model_iter_n_children (model, NULL));
75   path = gtk_tree_path_new ();
76   gtk_tree_path_append_index (path, i);
77   gtk_tree_model_get_iter (model, &iter, path);
78   gtk_tree_path_free (path);
79
80   if (gtk_tree_selection_iter_is_selected (selection, &iter))
81     return;
82   switch (g_rand_int_range (grand, 0, 3))
83     {
84     case 0:
85       /* insert */
86             gtk_list_store_insert_after (GTK_LIST_STORE (model),
87                                            &iter2, &iter);
88             gtk_list_store_set (GTK_LIST_STORE (model), &iter2,
89                                   TEXT_COLUMN, words[g_rand_int_range (grand, 0, NUM_WORDS)],
90                                   -1);
91       break;
92     case 1:
93       /* delete */
94       if (gtk_tree_model_iter_n_children (model, NULL) == 0)
95         return;
96       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
97       break;
98     case 2:
99       /* modify */
100       return;
101       if (gtk_tree_model_iter_n_children (model, NULL) == 0)
102         return;
103       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
104                           TEXT_COLUMN, words[g_rand_int_range (grand, 0, NUM_WORDS)],
105                           -1);
106       break;
107     }
108 }
109
110 static gboolean
111 futz (void)
112 {
113   gint 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 int
122 main (int argc, char *argv[])
123 {
124   GtkWidget *window;
125   GtkWidget *vbox;
126   GtkWidget *scrolled_window;
127   GtkWidget *tree_view;
128   GtkWidget *hbox;
129   GtkWidget *button;
130   GtkTreePath *path;
131
132   gtk_init (&argc, &argv);
133
134   path = gtk_tree_path_new_from_string ("80");
135   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
136   gtk_window_set_title (GTK_WINDOW (window), "Reflow test");
137   g_signal_connect (window, "destroy", gtk_main_quit, NULL);
138   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
139   gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
140   gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Incremental Reflow Test"), FALSE, FALSE, 0);
141   gtk_container_add (GTK_CONTAINER (window), vbox);
142   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
143   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
144                                   GTK_POLICY_AUTOMATIC,
145                                   GTK_POLICY_AUTOMATIC);
146   gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
147   
148   initialize_model ();
149   tree_view = gtk_tree_view_new_with_model (model);
150   gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tree_view), path, NULL, TRUE, 0.5, 0.0);
151   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
152   gtk_tree_selection_select_path (selection, path);
153   gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE);
154   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
155   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
156                                                -1,
157                                                NULL,
158                                                gtk_cell_renderer_text_new (),
159                                                "text", TEXT_COLUMN,
160                                                NULL);
161   gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
162   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
163   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
164   button = gtk_button_new_with_mnemonic ("<b>_Futz!!</b>");
165   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
166   gtk_label_set_use_markup (GTK_LABEL (gtk_bin_get_child (GTK_BIN (button))), TRUE);
167   g_signal_connect (button, "clicked", G_CALLBACK (futz), NULL);
168   g_signal_connect (button, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
169   gtk_window_set_default_size (GTK_WINDOW (window), 300, 400);
170   gtk_widget_show_all (window);
171   gdk_threads_add_timeout (1000, (GSourceFunc) futz, NULL);
172   gtk_main ();
173   return 0;
174 }