]> Pileus Git - ~andy/gtk/blob - tests/testactions.c
3c9a507892f2ae5fef3a9fab15fa39e7a95a19d1
[~andy/gtk] / tests / testactions.c
1 #undef GTK_DISABLE_DEPRECATED
2 #include <config.h>
3 #include <gtk/gtk.h>
4
5 static GtkActionGroup *action_group = NULL;
6 static GtkToolbar *toolbar = NULL;
7
8 static void
9 activate_action (GtkAction *action)
10 {
11   const gchar *name = gtk_action_get_name (action);
12   const gchar *typename = G_OBJECT_TYPE_NAME (action);
13
14   g_message ("Action %s (type=%s) activated", name, typename);
15 }
16
17 static void
18 toggle_action (GtkAction *action)
19 {
20   const gchar *name = gtk_action_get_name (action);
21   const gchar *typename = G_OBJECT_TYPE_NAME (action);
22
23   g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
24              gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
25 }
26
27
28 static void
29 radio_action (GtkAction *action)
30 {
31   const gchar *name = gtk_action_get_name (action);
32   const gchar *typename = G_OBJECT_TYPE_NAME (action);
33
34   g_message ("Action %s (type=%s) activated (active=%d) (value %d)", name, typename,
35              gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
36              gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action)));
37 }
38
39 static void
40 toggle_cnp_actions (GtkAction *action)
41 {
42   gboolean sensitive;
43
44   sensitive = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
45   action = gtk_action_group_get_action (action_group, "cut");
46   g_object_set (action, "sensitive", sensitive, NULL);
47   action = gtk_action_group_get_action (action_group, "copy");
48   g_object_set (action, "sensitive", sensitive, NULL);
49   action = gtk_action_group_get_action (action_group, "paste");
50   g_object_set (action, "sensitive", sensitive, NULL);
51
52   action = gtk_action_group_get_action (action_group, "toggle-cnp");
53   if (sensitive)
54     g_object_set (action, "label", "Disable Cut and paste ops", NULL);
55   else
56     g_object_set (action, "label", "Enable Cut and paste ops", NULL);
57 }
58
59 static void
60 show_accel_dialog (GtkAction *action)
61 {
62   g_message ("Sorry, accel dialog not available");
63 }
64
65 static void
66 toolbar_style (GtkAction *action, 
67                gpointer   user_data)
68 {
69   GtkToolbarStyle style;
70
71   g_return_if_fail (toolbar != NULL);
72   style = GPOINTER_TO_INT (user_data);
73
74   gtk_toolbar_set_style (toolbar, style);
75 }
76
77 static void
78 toolbar_size_small (GtkAction *action)
79 {
80   g_return_if_fail (toolbar != NULL);
81
82   gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
83 }
84
85 static void
86 toolbar_size_large (GtkAction *action)
87 {
88   g_return_if_fail (toolbar != NULL);
89
90   gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
91 }
92
93 /* convenience functions for declaring actions */
94 static GtkActionEntry entries[] = {
95   { "Menu1Action", NULL, "Menu _1" },
96   { "Menu2Action", NULL, "Menu _2" },
97   { "Menu3Action", NULL, "_Dynamic Menu" },
98
99   { "cut", GTK_STOCK_CUT, "C_ut", "<control>X",
100     "Cut the selected text to the clipboard", G_CALLBACK (activate_action) },
101   { "copy", GTK_STOCK_COPY, "_Copy", "<control>C",
102     "Copy the selected text to the clipboard", G_CALLBACK (activate_action) },
103   { "paste", GTK_STOCK_PASTE, "_Paste", "<control>V",
104     "Paste the text from the clipboard", G_CALLBACK (activate_action) },
105   { "quit", GTK_STOCK_QUIT,  NULL, "<control>Q",
106     "Quit the application", G_CALLBACK (gtk_main_quit) },
107   { "customise-accels", NULL, "Customise _Accels", NULL,
108     "Customise keyboard shortcuts", G_CALLBACK (show_accel_dialog) },
109   { "toolbar-small-icons", NULL, "Small Icons", NULL, 
110     NULL, G_CALLBACK (toolbar_size_small) },
111   { "toolbar-large-icons", NULL, "Large Icons", NULL,
112     NULL, G_CALLBACK (toolbar_size_large) }
113 };
114 static guint n_entries = G_N_ELEMENTS (entries);
115
116 static GtkToggleActionEntry toggle_entries[] = {
117   { "bold", GTK_STOCK_BOLD, "_Bold", "<control>B",
118     "Change to bold face", 
119     G_CALLBACK (toggle_action), FALSE },
120   { "toggle-cnp", NULL, "Enable Cut/Copy/Paste", NULL,
121     "Change the sensitivity of the cut, copy and paste actions",
122     G_CALLBACK (toggle_cnp_actions), TRUE },
123 };
124 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
125
126 enum {
127   JUSTIFY_LEFT,
128   JUSTIFY_CENTER,
129   JUSTIFY_RIGHT,
130   JUSTIFY_FILL
131 };
132
133 static GtkRadioActionEntry justify_entries[] = {
134   { "justify-left", GTK_STOCK_JUSTIFY_LEFT, "_Left", "<control>L",
135     "Left justify the text", JUSTIFY_LEFT },
136   { "justify-center", GTK_STOCK_JUSTIFY_CENTER, "C_enter", "<control>E",
137     "Center justify the text", JUSTIFY_CENTER },
138   { "justify-right", GTK_STOCK_JUSTIFY_RIGHT, "_Right", "<control>R",
139     "Right justify the text", JUSTIFY_RIGHT },
140   { "justify-fill", GTK_STOCK_JUSTIFY_FILL, "_Fill", "<control>J",
141     "Fill justify the text", JUSTIFY_FILL }
142 };
143 static guint n_justify_entries = G_N_ELEMENTS (justify_entries);
144
145 static GtkRadioActionEntry toolbar_entries[] = {
146   { "toolbar-icons", NULL, "Icons", NULL, NULL, GTK_TOOLBAR_ICONS },
147   { "toolbar-text", NULL, "Text", NULL, NULL, GTK_TOOLBAR_TEXT },
148   { "toolbar-both", NULL, "Both", NULL, NULL, GTK_TOOLBAR_BOTH },
149   { "toolbar-both-horiz", NULL, "Both Horizontal", NULL, NULL, GTK_TOOLBAR_BOTH_HORIZ }
150 };
151 static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries);
152
153 /* XML description of the menus for the test app.  The parser understands
154  * a subset of the Bonobo UI XML format, and uses GMarkup for parsing */
155 static const gchar *ui_info =
156 "  <menubar>\n"
157 "    <menu name=\"Menu _1\" action=\"Menu1Action\">\n"
158 "      <menuitem name=\"cut\" action=\"cut\" />\n"
159 "      <menuitem name=\"copy\" action=\"copy\" />\n"
160 "      <menuitem name=\"paste\" action=\"paste\" />\n"
161 "      <separator name=\"sep1\" />\n"
162 "      <menuitem name=\"bold1\" action=\"bold\" />\n"
163 "      <menuitem name=\"bold2\" action=\"bold\" />\n"
164 "      <separator name=\"sep2\" />\n"
165 "      <menuitem name=\"toggle-cnp\" action=\"toggle-cnp\" />\n"
166 "      <separator name=\"sep3\" />\n"
167 "      <menuitem name=\"quit\" action=\"quit\" />\n"
168 "    </menu>\n"
169 "    <menu name=\"Menu _2\" action=\"Menu2Action\">\n"
170 "      <menuitem name=\"cut\" action=\"cut\" />\n"
171 "      <menuitem name=\"copy\" action=\"copy\" />\n"
172 "      <menuitem name=\"paste\" action=\"paste\" />\n"
173 "      <separator name=\"sep4\"/>\n"
174 "      <menuitem name=\"bold\" action=\"bold\" />\n"
175 "      <separator name=\"sep5\"/>\n"
176 "      <menuitem name=\"justify-left\" action=\"justify-left\" />\n"
177 "      <menuitem name=\"justify-center\" action=\"justify-center\" />\n"
178 "      <menuitem name=\"justify-right\" action=\"justify-right\" />\n"
179 "      <menuitem name=\"justify-fill\" action=\"justify-fill\" />\n"
180 "      <separator name=\"sep6\"/>\n"
181 "      <menuitem  name=\"customise-accels\" action=\"customise-accels\" />\n"
182 "      <separator name=\"sep7\"/>\n"
183 "      <menuitem action=\"toolbar-icons\" />\n"
184 "      <menuitem action=\"toolbar-text\" />\n"
185 "      <menuitem action=\"toolbar-both\" />\n"
186 "      <menuitem action=\"toolbar-both-horiz\" />\n"
187 "      <separator name=\"sep8\"/>\n"
188 "      <menuitem action=\"toolbar-small-icons\" />\n"
189 "      <menuitem action=\"toolbar-large-icons\" />\n"
190 "    </menu>\n"
191   "    <menu name=\"DynamicMenu\" action=\"Menu3Action\" />\n"
192 "  </menubar>\n"
193 "  <toolbar name=\"toolbar\">\n"
194 "    <toolitem name=\"cut\" action=\"cut\" />\n"
195 "    <toolitem name=\"copy\" action=\"copy\" />\n"
196 "    <toolitem name=\"paste\" action=\"paste\" />\n"
197 "    <separator name=\"sep9\" />\n"
198 "    <toolitem name=\"bold\" action=\"bold\" />\n"
199 "    <separator name=\"sep10\" />\n"
200 "    <toolitem name=\"justify-left\" action=\"justify-left\" />\n"
201 "    <toolitem name=\"justify-center\" action=\"justify-center\" />\n"
202 "    <toolitem name=\"justify-right\" action=\"justify-right\" />\n"
203 "    <toolitem name=\"justify-fill\" action=\"justify-fill\" />\n"
204 "    <separator name=\"sep11\"/>\n"
205 "    <toolitem name=\"quit\" action=\"quit\" />\n"
206 "  </toolbar>\n"
207 "  <popup name=\"popup\">\n"
208 "    <menuitem name=\"popcut\" action=\"cut\" />\n"
209 "    <menuitem name=\"popcopy\" action=\"copy\" />\n"
210 "    <menuitem name=\"poppaste\" action=\"paste\" />\n"
211 "  </popup>\n";
212
213 static void
214 add_widget (GtkUIManager *merge,
215             GtkWidget   *widget,
216             GtkContainer *container)
217 {
218
219   gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
220   gtk_widget_show (widget);
221
222   if (GTK_IS_TOOLBAR (widget)) 
223     {
224       toolbar = GTK_TOOLBAR (widget);
225       gtk_toolbar_set_show_arrow (toolbar, TRUE);
226     }
227 }
228
229 static guint ui_id = 0;
230 static GtkActionGroup *dag = NULL;
231
232 static void
233 ensure_update (GtkUIManager *manager)
234 {
235   GTimer *timer;
236   double seconds;
237   gulong microsecs;
238   
239   timer = g_timer_new ();
240   g_timer_start (timer);
241   
242   gtk_ui_manager_ensure_update (manager);
243   
244   g_timer_stop (timer);
245   seconds = g_timer_elapsed (timer, &microsecs);
246   g_timer_destroy (timer);
247   
248   g_print ("Time: %fs\n", seconds);
249 }
250
251 static void
252 add_cb (GtkWidget *button,
253         GtkUIManager *manager)
254 {
255   GtkWidget *spinbutton;
256   GtkAction *action;
257   int i, num;
258   char *name, *label;
259   
260   if (ui_id != 0 || dag != NULL)
261     return;
262   
263   spinbutton = g_object_get_data (G_OBJECT (button), "spinbutton");
264   num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spinbutton));
265   
266   dag = gtk_action_group_new ("DynamicActions");
267   gtk_ui_manager_insert_action_group (manager, dag, 0);
268   
269   ui_id = gtk_ui_manager_new_merge_id (manager);
270   
271   for (i = 0; i < num; i++)
272     {
273       name = g_strdup_printf ("DynAction%u", i);
274       label = g_strdup_printf ("Dynamic Item %d", i);
275       
276       action = g_object_new (GTK_TYPE_ACTION,
277                              "name", name,
278                              "label", label,
279                              NULL);
280       gtk_action_group_add_action (dag, action);
281       g_object_unref (action);
282       
283       gtk_ui_manager_add_ui (manager, ui_id, "/menubar/DynamicMenu",
284                              name, name,
285                              GTK_UI_MANAGER_MENUITEM, FALSE);
286     }
287   
288   ensure_update (manager);
289 }
290
291 static void
292 remove_cb (GtkWidget *button,
293            GtkUIManager *manager)
294 {
295   if (ui_id == 0 || dag == NULL)
296     return;
297   
298   gtk_ui_manager_remove_ui (manager, ui_id);
299   ensure_update (manager);
300   ui_id = 0;
301   
302   gtk_ui_manager_remove_action_group (manager, dag);
303   g_object_unref (dag);
304   dag = NULL;
305 }
306
307 static void
308 create_window (GtkActionGroup *action_group)
309 {
310   GtkUIManager *merge;
311   GtkWidget *window;
312   GtkWidget *box;
313   GtkWidget *hbox, *spinbutton, *button;
314   GError *error = NULL;
315
316   merge = gtk_ui_manager_new ();
317
318   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
319   gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
320   gtk_window_set_title (GTK_WINDOW (window), "Action Test");
321   g_signal_connect_swapped (window, "destroy", G_CALLBACK (g_object_unref), merge);
322   g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
323
324   box = gtk_vbox_new (FALSE, 0);
325   gtk_container_add (GTK_CONTAINER (window), box);
326   gtk_widget_show (box);
327
328   gtk_ui_manager_insert_action_group (merge, action_group, 0);
329   g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
330
331   gtk_window_add_accel_group (GTK_WINDOW (window), 
332                               gtk_ui_manager_get_accel_group (merge));
333
334   if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
335     {
336       g_message ("building menus failed: %s", error->message);
337       g_error_free (error);
338     }
339
340   hbox = gtk_hbox_new (FALSE, 0);
341   gtk_box_pack_end (GTK_BOX (box), hbox, FALSE, FALSE, 0);
342   gtk_widget_show (hbox);
343   
344   spinbutton = gtk_spin_button_new_with_range (100, 10000, 100);
345   gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
346   gtk_widget_show (spinbutton);
347   
348   button = gtk_button_new_with_label ("Add");
349   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
350   gtk_widget_show (button);
351   
352   g_object_set_data (G_OBJECT (button), "spinbutton", spinbutton);
353   g_signal_connect (button, "clicked", G_CALLBACK (add_cb), merge);
354   
355   button = gtk_button_new_with_label ("Remove");
356   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
357   gtk_widget_show (button);
358   
359   g_signal_connect (button, "clicked", G_CALLBACK (remove_cb), merge);
360   
361   gtk_widget_show (window);
362 }
363
364 int
365 main (int argc, char **argv)
366 {
367   gtk_init (&argc, &argv);
368
369   if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
370     gtk_accel_map_load ("accels");
371
372   action_group = gtk_action_group_new ("TestActions");
373   gtk_action_group_add_actions (action_group, 
374                                 entries, n_entries, 
375                                 NULL);
376   gtk_action_group_add_toggle_actions (action_group, 
377                                        toggle_entries, n_toggle_entries, 
378                                        NULL);
379   gtk_action_group_add_radio_actions (action_group, 
380                                       justify_entries, n_justify_entries, 
381                                       JUSTIFY_LEFT,
382                                       G_CALLBACK (radio_action), NULL);
383   gtk_action_group_add_radio_actions (action_group, 
384                                       toolbar_entries, n_toolbar_entries, 
385                                       GTK_TOOLBAR_BOTH,
386                                       G_CALLBACK (radio_action), NULL);
387
388   create_window (action_group);
389
390   gtk_main ();
391
392 #ifdef DEBUG_UI_MANAGER
393   {
394     GList *action;
395
396     for (action = gtk_action_group_list_actions (action_group);
397          action; 
398          action = action->next)
399       {
400         GtkAction *a = action->data;
401         g_print ("action %s ref count %d\n", 
402                  gtk_action_get_name (a), G_OBJECT (a)->ref_count);
403       }
404   }
405 #endif
406
407   g_object_unref (action_group);
408
409   gtk_accel_map_save ("accels");
410
411   return 0;
412 }