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