]> Pileus Git - ~andy/gtk/blob - tests/stresstest-toolbar.c
539e6de83b4022d85a20cc667704dc5138d8dc1d
[~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   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   if (g_list_length (toolbar->children) == 0)
45     position = 0;
46   else
47     position = g_random_int_range (0, g_list_length (toolbar->children));
48
49   gtk_toolbar_insert (toolbar, toolitem, position);
50 }
51
52 static void
53 remove_random (GtkToolbar *toolbar)
54 {
55   GtkToolbarChild *child;
56   gint position;
57
58   if (!toolbar->children)
59     return;
60
61   position = g_random_int_range (0, g_list_length (toolbar->children));
62
63   child = g_list_nth_data (toolbar->children, position);
64   
65   gtk_container_remove (GTK_CONTAINER (toolbar), child->widget);
66 }
67
68 static gboolean
69 stress_test_old_api (gpointer data)
70 {
71   typedef enum {
72     ADD_RANDOM,
73     REMOVE_RANDOM,
74     LAST_ACTION
75   } Action;
76       
77   Info *info = data;
78   Action action;
79   
80   if (info->counter++ == 200)
81     {
82       gtk_main_quit ();
83       return FALSE;
84     }
85
86   if (!info->toolbar)
87     {
88       info->toolbar = GTK_TOOLBAR (gtk_toolbar_new ());
89       gtk_container_add (GTK_CONTAINER (info->window),
90                          GTK_WIDGET (info->toolbar));
91       gtk_widget_show (GTK_WIDGET (info->toolbar));
92     }
93
94   if (!info->toolbar->children)
95     {
96       add_random (info->toolbar, info->counter);
97       return TRUE;
98     }
99   else if (g_list_length (info->toolbar->children) > 50)
100     {
101       int i;
102       for (i = 0; i < 25; i++)
103         remove_random (info->toolbar);
104       return TRUE;
105     }
106   
107   action = g_random_int_range (0, LAST_ACTION);
108
109   switch (action)
110     {
111     case ADD_RANDOM:
112       add_random (info->toolbar, info->counter);
113       break;
114
115     case REMOVE_RANDOM:
116       remove_random (info->toolbar);
117       break;
118       
119     default:
120       g_assert_not_reached();
121       break;
122     }
123   
124   return TRUE;
125 }
126
127
128 gint
129 main (gint argc, gchar **argv)
130 {
131   Info info;
132   
133   gtk_init (&argc, &argv);
134
135   info.toolbar = NULL;
136   info.counter = 0;
137   info.window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
138
139   gtk_widget_show (GTK_WIDGET (info.window));
140   
141   gdk_threads_add_idle (stress_test_old_api, &info);
142
143   gtk_widget_show_all (GTK_WIDGET (info.window));
144   
145   gtk_main ();
146
147   gtk_widget_destroy (GTK_WIDGET (info.window));
148
149   info.toolbar = NULL;
150   info.window = NULL;
151   
152   return 0;
153 }