]> Pileus Git - ~andy/gtk/blob - tests/testcombochange.c
b67b0f50feb54a4670c1d620f222fadd4b22827e
[~andy/gtk] / tests / testcombochange.c
1 /* testcombochange.c
2  * Copyright (C) 2004  Red Hat, Inc.
3  * Author: Owen Taylor
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 #include <stdarg.h>
24
25 GtkWidget *text_view;
26 GtkListStore *model;
27 GArray *contents;
28
29 static char next_value = 'A';
30
31 static void
32 test_init (void)
33 {
34   if (g_file_test ("../gdk-pixbuf/libpixbufloader-pnm.la",
35                    G_FILE_TEST_EXISTS))
36     {
37       g_setenv ("GDK_PIXBUF_MODULE_FILE", "../gdk-pixbuf/gdk-pixbuf.loaders", TRUE);
38       g_setenv ("GTK_IM_MODULE_FILE", "../modules/input/gtk.immodules", TRUE);
39     }
40 }
41
42 static void
43 combochange_log (const char *fmt,
44      ...)
45 {
46   GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
47   GtkTextIter iter;
48   va_list vap;
49   char *msg;
50   GString *order_string;
51   GtkTextMark *tmp_mark;
52   int i;
53
54   va_start (vap, fmt);
55   
56   msg = g_strdup_vprintf (fmt, vap);
57
58   gtk_text_buffer_get_end_iter (buffer, &iter);
59   gtk_text_buffer_insert (buffer, &iter, msg, -1);
60
61   order_string = g_string_new ("\n  ");
62   for (i = 0; i < contents->len; i++)
63     {
64       if (i)
65         g_string_append_c (order_string, ' ');
66       g_string_append_c (order_string, g_array_index (contents, char, i));
67     }
68   g_string_append_c (order_string, '\n');
69   gtk_text_buffer_insert (buffer, &iter, order_string->str, -1);
70   g_string_free (order_string, TRUE);
71
72   tmp_mark = gtk_text_buffer_create_mark (buffer, NULL, &iter, FALSE);
73   gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (text_view), tmp_mark);
74   gtk_text_buffer_delete_mark (buffer, tmp_mark);
75
76   g_free (msg);
77 }
78
79 static GtkWidget *
80 align_button_new (const char *text)
81 {
82   GtkWidget *button = gtk_button_new ();
83   GtkWidget *label = gtk_label_new (text);
84
85   gtk_container_add (GTK_CONTAINER (button), label);
86   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
87
88   return button;
89 }
90
91 static GtkWidget *
92 create_combo (const char *name,
93               gboolean is_list)
94 {
95   GtkCellRenderer *cell_renderer;
96   GtkWidget *combo;
97   char *rc_string;
98   
99   rc_string = g_strdup_printf ("style \"%s-style\" {\n"
100                                "  GtkComboBox::appears-as-list = %d\n"
101                                "}\n"
102                                "\n"
103                                "widget \"*.%s\" style \"%s-style\"",
104                                name, is_list, name, name);
105   gtk_rc_parse_string (rc_string);
106   g_free (rc_string);
107
108   combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (model));
109   cell_renderer = gtk_cell_renderer_text_new ();
110   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell_renderer, TRUE);
111   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell_renderer,
112                                   "text", 0, NULL);
113
114   gtk_widget_set_name (combo, name);
115   
116   return combo;
117 }
118
119 static void
120 on_insert (void)
121 {
122   GtkTreeIter iter;
123   
124   int insert_pos;
125   char new_value[2];
126
127   new_value[0] = next_value++;
128   new_value[1] = '\0';
129
130   if (next_value > 'Z')
131     next_value = 'A';
132   
133   if (contents->len)
134     insert_pos = g_random_int_range (0, contents->len + 1);
135   else
136     insert_pos = 0;
137   
138   gtk_list_store_insert (model, &iter, insert_pos);
139   gtk_list_store_set (model, &iter, 0, new_value, -1);
140
141   g_array_insert_val (contents, insert_pos, new_value);
142
143   combochange_log ("Inserted '%c' at position %d", new_value[0], insert_pos);
144 }
145
146 static void
147 on_delete (void)
148 {
149   GtkTreeIter iter;
150   
151   int delete_pos;
152   char old_val;
153
154   if (!contents->len)
155     return;
156   
157   delete_pos = g_random_int_range (0, contents->len);
158   gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (model), &iter, NULL, delete_pos);
159   
160   gtk_list_store_remove (model, &iter);
161
162   old_val = g_array_index (contents, char, delete_pos);
163   g_array_remove_index (contents, delete_pos);
164   combochange_log ("Deleted '%c' from position %d", old_val, delete_pos);
165 }
166
167 static void
168 on_reorder (void)
169 {
170   GArray *new_contents;
171   gint *shuffle_array;
172   gint i;
173
174   shuffle_array = g_new (int, contents->len);
175   
176   for (i = 0; i < contents->len; i++)
177     shuffle_array[i] = i;
178
179   for (i = 0; i + 1 < contents->len; i++)
180     {
181       gint pos = g_random_int_range (i, contents->len);
182       gint tmp;
183
184       tmp = shuffle_array[i];
185       shuffle_array[i] = shuffle_array[pos];
186       shuffle_array[pos] = tmp;
187     }
188
189   gtk_list_store_reorder (model, shuffle_array);
190
191   new_contents = g_array_new (FALSE, FALSE, sizeof (char));
192   for (i = 0; i < contents->len; i++)
193     g_array_append_val (new_contents,
194                         g_array_index (contents, char, shuffle_array[i]));
195   g_array_free (contents, TRUE);
196   contents = new_contents;
197
198   combochange_log ("Reordered array");
199     
200   g_free (shuffle_array);
201 }
202
203 static int n_animations = 0;
204 static int timer = 0;
205
206 static gint
207 animation_timer (gpointer data)
208 {
209   switch (g_random_int_range (0, 3)) 
210     {
211     case 0: 
212       on_insert ();
213       break;
214     case 1:
215       on_delete ();
216       break;
217     case 2:
218       on_reorder ();
219       break;
220     }
221
222   n_animations--;
223   return n_animations > 0;
224 }
225
226 static void
227 on_animate (void)
228 {
229   n_animations += 20;
230  
231   timer = g_timeout_add (1000, (GSourceFunc) animation_timer, NULL);
232 }
233
234 int
235 main (int argc, char **argv)
236 {
237   GtkWidget *dialog;
238   GtkWidget *scrolled_window;
239   GtkWidget *hbox;
240   GtkWidget *button_vbox;
241   GtkWidget *combo_vbox;
242   GtkWidget *button;
243   GtkWidget *menu_combo;
244   GtkWidget *alignment;
245   GtkWidget *label;
246   GtkWidget *list_combo;
247   
248   test_init ();
249
250   gtk_init (&argc, &argv);
251
252   model = gtk_list_store_new (1, G_TYPE_STRING);
253   contents = g_array_new (FALSE, FALSE, sizeof (char));
254   
255   dialog = gtk_dialog_new_with_buttons ("GtkComboBox model changes",
256                                         NULL, GTK_DIALOG_NO_SEPARATOR,
257                                         GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
258                                         NULL);
259   
260   hbox = gtk_hbox_new (FALSE, 12);
261   gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
262   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, TRUE, TRUE, 0);
263
264   combo_vbox = gtk_vbox_new (FALSE, 8);
265   gtk_box_pack_start (GTK_BOX (hbox), combo_vbox, FALSE, FALSE, 0);
266
267   label = gtk_label_new (NULL);
268   gtk_label_set_markup (GTK_LABEL (label), "<b>Menu mode</b>");
269   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
270   gtk_box_pack_start (GTK_BOX (combo_vbox), label, FALSE, FALSE, 0);
271
272   alignment = g_object_new (GTK_TYPE_ALIGNMENT, "left-padding", 12, NULL);
273   gtk_box_pack_start (GTK_BOX (combo_vbox), alignment, FALSE, FALSE, 0);
274
275   menu_combo = create_combo ("menu-combo", FALSE);
276   gtk_container_add (GTK_CONTAINER (alignment), menu_combo);
277
278   label = gtk_label_new (NULL);
279   gtk_label_set_markup (GTK_LABEL (label), "<b>List mode</b>");
280   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
281   gtk_box_pack_start (GTK_BOX (combo_vbox), label, FALSE, FALSE, 0);
282
283   alignment = g_object_new (GTK_TYPE_ALIGNMENT, "left-padding", 12, NULL);
284   gtk_box_pack_start (GTK_BOX (combo_vbox), alignment, FALSE, FALSE, 0);
285
286   list_combo = create_combo ("list-combo", TRUE);
287   gtk_container_add (GTK_CONTAINER (alignment), list_combo);
288
289   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
290   gtk_box_pack_start (GTK_BOX (hbox), scrolled_window, TRUE, TRUE, 0);
291   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
292                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
293
294   text_view = gtk_text_view_new ();
295   gtk_text_view_set_editable (GTK_TEXT_VIEW (text_view), FALSE);
296   gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (text_view), FALSE);
297
298   gtk_container_add (GTK_CONTAINER (scrolled_window), text_view);
299
300   button_vbox = gtk_vbox_new (FALSE, 8);
301   gtk_box_pack_start (GTK_BOX (hbox), button_vbox, FALSE, FALSE, 0);
302   
303   gtk_window_set_default_size (GTK_WINDOW (dialog), 500, 300);
304
305   button = align_button_new ("Insert");
306   gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0);
307   g_signal_connect (button, "clicked", G_CALLBACK (on_insert), NULL);
308   
309   button = align_button_new ("Delete");
310   gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0);
311   g_signal_connect (button, "clicked", G_CALLBACK (on_delete), NULL);
312
313   button = align_button_new ("Reorder");
314   gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0);
315   g_signal_connect (button, "clicked", G_CALLBACK (on_reorder), NULL);
316
317   button = align_button_new ("Animate");
318   gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0);
319   g_signal_connect (button, "clicked", G_CALLBACK (on_animate), NULL);
320
321   gtk_widget_show_all (dialog);
322   gtk_dialog_run (GTK_DIALOG (dialog));
323
324   return 0;
325 }