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