]> Pileus Git - ~andy/gtk/blob - tests/stresstest-toolbar.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / stresstest-toolbar.c
1 /* stresstest-toolbar.c
2  *
3  * Copyright (C) 2003 Soeren Sandmann <sandmann@daimi.au.dk>
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 #include "config.h"
19 #include <gtk/gtk.h>
20
21 typedef struct _Info Info;
22 struct _Info
23 {
24   GtkWindow  *window;
25   GtkToolbar *toolbar;
26   gint        counter;
27 };
28
29 static void
30 add_random (GtkToolbar *toolbar, gint n)
31 {
32   gint n_items;
33   gint position;
34   gchar *label = g_strdup_printf ("Button %d", n);
35
36   GtkToolItem *toolitem = gtk_tool_button_new (NULL, label);
37   gtk_tool_item_set_tooltip_text (toolitem, "Bar");
38
39   g_free (label);
40   gtk_widget_show_all (GTK_WIDGET (toolitem));
41
42   n_items = gtk_toolbar_get_n_items (toolbar);
43   if (n_items == 0)
44     position = 0;
45   else
46     position = g_random_int_range (0, n_items);
47
48   gtk_toolbar_insert (toolbar, toolitem, position);
49 }
50
51 static void
52 remove_random (GtkToolbar *toolbar)
53 {
54   GtkToolItem *tool_item;
55   gint n_items;
56   gint position;
57
58   n_items = gtk_toolbar_get_n_items (toolbar);
59
60   if (n_items == 0)
61     return;
62
63   position = g_random_int_range (0, n_items);
64
65   tool_item = gtk_toolbar_get_nth_item (toolbar, position);
66
67   gtk_container_remove (GTK_CONTAINER (toolbar),
68                         GTK_WIDGET (tool_item));
69 }
70
71 static gboolean
72 stress_test_old_api (gpointer data)
73 {
74   typedef enum {
75     ADD_RANDOM,
76     REMOVE_RANDOM,
77     LAST_ACTION
78   } Action;
79       
80   Info *info = data;
81   Action action;
82   gint n_items;
83
84   if (info->counter++ == 200)
85     {
86       gtk_main_quit ();
87       return FALSE;
88     }
89
90   if (!info->toolbar)
91     {
92       info->toolbar = GTK_TOOLBAR (gtk_toolbar_new ());
93       gtk_container_add (GTK_CONTAINER (info->window),
94                          GTK_WIDGET (info->toolbar));
95       gtk_widget_show (GTK_WIDGET (info->toolbar));
96     }
97
98   n_items = gtk_toolbar_get_n_items (info->toolbar);
99   if (n_items == 0)
100     {
101       add_random (info->toolbar, info->counter);
102       return TRUE;
103     }
104   else if (n_items > 50)
105     {
106       int i;
107       for (i = 0; i < 25; i++)
108         remove_random (info->toolbar);
109       return TRUE;
110     }
111   
112   action = g_random_int_range (0, LAST_ACTION);
113
114   switch (action)
115     {
116     case ADD_RANDOM:
117       add_random (info->toolbar, info->counter);
118       break;
119
120     case REMOVE_RANDOM:
121       remove_random (info->toolbar);
122       break;
123       
124     default:
125       g_assert_not_reached();
126       break;
127     }
128   
129   return TRUE;
130 }
131
132
133 gint
134 main (gint argc, gchar **argv)
135 {
136   Info info;
137   
138   gtk_init (&argc, &argv);
139
140   info.toolbar = NULL;
141   info.counter = 0;
142   info.window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
143
144   gtk_widget_show (GTK_WIDGET (info.window));
145   
146   gdk_threads_add_idle (stress_test_old_api, &info);
147
148   gtk_widget_show_all (GTK_WIDGET (info.window));
149   
150   gtk_main ();
151
152   gtk_widget_destroy (GTK_WIDGET (info.window));
153
154   info.toolbar = NULL;
155   info.window = NULL;
156   
157   return 0;
158 }