]> Pileus Git - ~andy/gtk/blob - tests/stresstest-toolbar.c
Remove deprecated GtkToolbar code
[~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 position;
35   gchar *label = g_strdup_printf ("Button %d", n);
36
37   GtkToolItem *toolitem = gtk_tool_button_new (NULL, label);
38   gtk_tool_item_set_tooltip_text (toolitem, "Bar");
39
40   g_free (label);
41   gtk_widget_show_all (GTK_WIDGET (toolitem));
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 (toolbar, toolitem, position);
49 }
50
51 static void
52 remove_random (GtkToolbar *toolbar)
53 {
54   GtkWidget *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);
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   gdk_threads_add_idle (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 }