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