]> Pileus Git - ~andy/gtk/blob - tests/testactions.c
Include "config.h" instead of <config.h> Command used: find -name
[~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 {
100   GtkToolbarStyle style;
101
102   g_return_if_fail (toolbar != NULL);
103
104   radio_action (action);
105
106   style = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action));
107
108   gtk_toolbar_set_style (toolbar, style);
109 }
110
111 static void
112 toolbar_size_small (GtkAction *action)
113 {
114   g_return_if_fail (toolbar != NULL);
115
116   gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
117 }
118
119 static void
120 toolbar_size_large (GtkAction *action)
121 {
122   g_return_if_fail (toolbar != NULL);
123
124   gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
125 }
126
127 /* convenience functions for declaring actions */
128 static GtkActionEntry entries[] = {
129   { "Menu1Action", NULL, "Menu _1" },
130   { "Menu2Action", NULL, "Menu _2" },
131   { "Menu3Action", NULL, "_Dynamic Menu" },
132
133   { "cut", GTK_STOCK_CUT, "C_ut", "<control>X",
134     "Cut the selected text to the clipboard", G_CALLBACK (activate_action) },
135   { "copy", GTK_STOCK_COPY, "_Copy", "<control>C",
136     "Copy the selected text to the clipboard", G_CALLBACK (activate_action) },
137   { "paste", GTK_STOCK_PASTE, "_Paste", "<control>V",
138     "Paste the text from the clipboard", G_CALLBACK (activate_action) },
139   { "quit", GTK_STOCK_QUIT,  NULL, "<control>Q",
140     "Quit the application", G_CALLBACK (gtk_main_quit) },
141   { "customise-accels", NULL, "Customise _Accels", NULL,
142     "Customise keyboard shortcuts", G_CALLBACK (show_accel_dialog) },
143   { "toolbar-small-icons", NULL, "Small Icons", NULL, 
144     NULL, G_CALLBACK (toolbar_size_small) },
145   { "toolbar-large-icons", NULL, "Large Icons", NULL,
146     NULL, G_CALLBACK (toolbar_size_large) }
147 };
148 static guint n_entries = G_N_ELEMENTS (entries);
149
150 static GtkToggleActionEntry toggle_entries[] = {
151   { "bold", GTK_STOCK_BOLD, "_Bold", "<control>B",
152     "Change to bold face", 
153     G_CALLBACK (toggle_action), FALSE },
154   { "toggle-cnp", NULL, "Enable Cut/Copy/Paste", NULL,
155     "Change the sensitivity of the cut, copy and paste actions",
156     G_CALLBACK (toggle_cnp_actions), TRUE },
157 };
158 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
159
160 enum {
161   JUSTIFY_LEFT,
162   JUSTIFY_CENTER,
163   JUSTIFY_RIGHT,
164   JUSTIFY_FILL
165 };
166
167 static GtkRadioActionEntry justify_entries[] = {
168   { "justify-left", GTK_STOCK_JUSTIFY_LEFT, "_Left", "<control>L",
169     "Left justify the text", JUSTIFY_LEFT },
170   { "justify-center", GTK_STOCK_JUSTIFY_CENTER, "C_enter", "<control>E",
171     "Center justify the text", JUSTIFY_CENTER },
172   { "justify-right", GTK_STOCK_JUSTIFY_RIGHT, "_Right", "<control>R",
173     "Right justify the text", JUSTIFY_RIGHT },
174   { "justify-fill", GTK_STOCK_JUSTIFY_FILL, "_Fill", "<control>J",
175     "Fill justify the text", JUSTIFY_FILL }
176 };
177 static guint n_justify_entries = G_N_ELEMENTS (justify_entries);
178
179 static GtkRadioActionEntry toolbar_entries[] = {
180   { "toolbar-icons", NULL, "Icons", NULL, NULL, GTK_TOOLBAR_ICONS },
181   { "toolbar-text", NULL, "Text", NULL, NULL, GTK_TOOLBAR_TEXT },
182   { "toolbar-both", NULL, "Both", NULL, NULL, GTK_TOOLBAR_BOTH },
183   { "toolbar-both-horiz", NULL, "Both Horizontal", NULL, NULL, GTK_TOOLBAR_BOTH_HORIZ }
184 };
185 static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries);
186
187 /* XML description of the menus for the test app.  The parser understands
188  * a subset of the Bonobo UI XML format, and uses GMarkup for parsing */
189 static const gchar *ui_info =
190 "  <menubar>\n"
191 "    <menu name=\"Menu _1\" action=\"Menu1Action\">\n"
192 "      <menuitem name=\"cut\" action=\"cut\" />\n"
193 "      <menuitem name=\"copy\" action=\"copy\" />\n"
194 "      <menuitem name=\"paste\" action=\"paste\" />\n"
195 "      <separator name=\"sep1\" />\n"
196 "      <menuitem name=\"bold1\" action=\"bold\" />\n"
197 "      <menuitem name=\"bold2\" action=\"bold\" />\n"
198 "      <separator name=\"sep2\" />\n"
199 "      <menuitem name=\"recent\" action=\"recent\" />\n"
200 "      <separator name=\"sep3\" />\n"
201 "      <menuitem name=\"toggle-cnp\" action=\"toggle-cnp\" />\n"
202 "      <separator name=\"sep4\" />\n"
203 "      <menuitem name=\"quit\" action=\"quit\" />\n"
204 "    </menu>\n"
205 "    <menu name=\"Menu _2\" action=\"Menu2Action\">\n"
206 "      <menuitem name=\"cut\" action=\"cut\" />\n"
207 "      <menuitem name=\"copy\" action=\"copy\" />\n"
208 "      <menuitem name=\"paste\" action=\"paste\" />\n"
209 "      <separator name=\"sep5\"/>\n"
210 "      <menuitem name=\"bold\" action=\"bold\" />\n"
211 "      <separator name=\"sep6\"/>\n"
212 "      <menuitem name=\"justify-left\" action=\"justify-left\" />\n"
213 "      <menuitem name=\"justify-center\" action=\"justify-center\" />\n"
214 "      <menuitem name=\"justify-right\" action=\"justify-right\" />\n"
215 "      <menuitem name=\"justify-fill\" action=\"justify-fill\" />\n"
216 "      <separator name=\"sep7\"/>\n"
217 "      <menuitem  name=\"customise-accels\" action=\"customise-accels\" />\n"
218 "      <separator name=\"sep8\"/>\n"
219 "      <menuitem action=\"toolbar-icons\" />\n"
220 "      <menuitem action=\"toolbar-text\" />\n"
221 "      <menuitem action=\"toolbar-both\" />\n"
222 "      <menuitem action=\"toolbar-both-horiz\" />\n"
223 "      <separator name=\"sep9\"/>\n"
224 "      <menuitem action=\"toolbar-small-icons\" />\n"
225 "      <menuitem action=\"toolbar-large-icons\" />\n"
226 "    </menu>\n"
227 "    <menu name=\"DynamicMenu\" action=\"Menu3Action\" />\n"
228 "  </menubar>\n"
229 "  <toolbar name=\"toolbar\">\n"
230 "    <toolitem name=\"cut\" action=\"cut\" />\n"
231 "    <toolitem name=\"copy\" action=\"copy\" />\n"
232 "    <toolitem name=\"paste\" action=\"paste\" />\n"
233 "    <toolitem name=\"recent\" action=\"recent\" />\n"
234 "    <separator name=\"sep10\" />\n"
235 "    <toolitem name=\"bold\" action=\"bold\" />\n"
236 "    <separator name=\"sep11\" />\n"
237 "    <toolitem name=\"justify-left\" action=\"justify-left\" />\n"
238 "    <toolitem name=\"justify-center\" action=\"justify-center\" />\n"
239 "    <toolitem name=\"justify-right\" action=\"justify-right\" />\n"
240 "    <toolitem name=\"justify-fill\" action=\"justify-fill\" />\n"
241 "    <separator name=\"sep12\"/>\n"
242 "    <toolitem name=\"quit\" action=\"quit\" />\n"
243 "  </toolbar>\n"
244 "  <popup name=\"popup\">\n"
245 "    <menuitem name=\"popcut\" action=\"cut\" />\n"
246 "    <menuitem name=\"popcopy\" action=\"copy\" />\n"
247 "    <menuitem name=\"poppaste\" action=\"paste\" />\n"
248 "  </popup>\n";
249
250 static void
251 add_widget (GtkUIManager *merge,
252             GtkWidget   *widget,
253             GtkContainer *container)
254 {
255
256   gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
257   gtk_widget_show (widget);
258
259   if (GTK_IS_TOOLBAR (widget)) 
260     {
261       toolbar = GTK_TOOLBAR (widget);
262       gtk_toolbar_set_show_arrow (toolbar, TRUE);
263     }
264 }
265
266 static guint ui_id = 0;
267 static GtkActionGroup *dag = NULL;
268
269 static void
270 ensure_update (GtkUIManager *manager)
271 {
272   GTimer *timer;
273   double seconds;
274   gulong microsecs;
275   
276   timer = g_timer_new ();
277   g_timer_start (timer);
278   
279   gtk_ui_manager_ensure_update (manager);
280   
281   g_timer_stop (timer);
282   seconds = g_timer_elapsed (timer, &microsecs);
283   g_timer_destroy (timer);
284   
285   g_print ("Time: %fs\n", seconds);
286 }
287
288 static void
289 add_cb (GtkWidget *button,
290         GtkUIManager *manager)
291 {
292   GtkWidget *spinbutton;
293   GtkAction *action;
294   int i, num;
295   char *name, *label;
296   
297   if (ui_id != 0 || dag != NULL)
298     return;
299   
300   spinbutton = g_object_get_data (G_OBJECT (button), "spinbutton");
301   num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spinbutton));
302   
303   dag = gtk_action_group_new ("DynamicActions");
304   gtk_ui_manager_insert_action_group (manager, dag, 0);
305   
306   ui_id = gtk_ui_manager_new_merge_id (manager);
307   
308   for (i = 0; i < num; i++)
309     {
310       name = g_strdup_printf ("DynAction%u", i);
311       label = g_strdup_printf ("Dynamic Item %d", i);
312       
313       action = g_object_new (GTK_TYPE_ACTION,
314                              "name", name,
315                              "label", label,
316                              NULL);
317       gtk_action_group_add_action (dag, action);
318       g_object_unref (action);
319       
320       gtk_ui_manager_add_ui (manager, ui_id, "/menubar/DynamicMenu",
321                              name, name,
322                              GTK_UI_MANAGER_MENUITEM, FALSE);
323     }
324   
325   ensure_update (manager);
326 }
327
328 static void
329 remove_cb (GtkWidget *button,
330            GtkUIManager *manager)
331 {
332   if (ui_id == 0 || dag == NULL)
333     return;
334   
335   gtk_ui_manager_remove_ui (manager, ui_id);
336   ensure_update (manager);
337   ui_id = 0;
338   
339   gtk_ui_manager_remove_action_group (manager, dag);
340   g_object_unref (dag);
341   dag = NULL;
342 }
343
344 static void
345 create_window (GtkActionGroup *action_group)
346 {
347   GtkUIManager *merge;
348   GtkWidget *window;
349   GtkWidget *box;
350   GtkWidget *hbox, *spinbutton, *button;
351   GError *error = NULL;
352
353   merge = gtk_ui_manager_new ();
354
355   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
356   gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
357   gtk_window_set_title (GTK_WINDOW (window), "Action Test");
358   g_signal_connect_swapped (window, "destroy", G_CALLBACK (g_object_unref), merge);
359   g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
360
361   box = gtk_vbox_new (FALSE, 0);
362   gtk_container_add (GTK_CONTAINER (window), box);
363   gtk_widget_show (box);
364
365   gtk_ui_manager_insert_action_group (merge, action_group, 0);
366   g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
367
368   gtk_window_add_accel_group (GTK_WINDOW (window), 
369                               gtk_ui_manager_get_accel_group (merge));
370
371   if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
372     {
373       g_message ("building menus failed: %s", error->message);
374       g_error_free (error);
375     }
376
377   hbox = gtk_hbox_new (FALSE, 0);
378   gtk_box_pack_end (GTK_BOX (box), hbox, FALSE, FALSE, 0);
379   gtk_widget_show (hbox);
380   
381   spinbutton = gtk_spin_button_new_with_range (100, 10000, 100);
382   gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
383   gtk_widget_show (spinbutton);
384   
385   button = gtk_button_new_with_label ("Add");
386   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
387   gtk_widget_show (button);
388   
389   g_object_set_data (G_OBJECT (button), "spinbutton", spinbutton);
390   g_signal_connect (button, "clicked", G_CALLBACK (add_cb), merge);
391   
392   button = gtk_button_new_with_label ("Remove");
393   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
394   gtk_widget_show (button);
395   
396   g_signal_connect (button, "clicked", G_CALLBACK (remove_cb), merge);
397   
398   gtk_widget_show (window);
399 }
400
401 int
402 main (int argc, char **argv)
403 {
404   GtkAction *action;
405
406   gtk_init (&argc, &argv);
407
408   if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
409     gtk_accel_map_load ("accels");
410
411   action = gtk_recent_action_new ("recent",
412                                   "Open Recent", "Open recent files",
413                                   NULL);
414   g_signal_connect (action, "item-activated",
415                     G_CALLBACK (recent_action),
416                     NULL);
417   g_signal_connect (action, "activate",
418                     G_CALLBACK (recent_action),
419                     NULL);
420
421   action_group = gtk_action_group_new ("TestActions");
422   gtk_action_group_add_actions (action_group, 
423                                 entries, n_entries, 
424                                 NULL);
425   gtk_action_group_add_toggle_actions (action_group, 
426                                        toggle_entries, n_toggle_entries, 
427                                        NULL);
428   gtk_action_group_add_radio_actions (action_group, 
429                                       justify_entries, n_justify_entries, 
430                                       JUSTIFY_LEFT,
431                                       G_CALLBACK (radio_action), NULL);
432   gtk_action_group_add_radio_actions (action_group, 
433                                       toolbar_entries, n_toolbar_entries, 
434                                       GTK_TOOLBAR_BOTH,
435                                       G_CALLBACK (toolbar_style), NULL);
436   gtk_action_group_add_action_with_accel (action_group, action, NULL);
437
438   create_window (action_group);
439
440   gtk_main ();
441
442 #ifdef DEBUG_UI_MANAGER
443   {
444     GList *action;
445
446     for (action = gtk_action_group_list_actions (action_group);
447          action; 
448          action = action->next)
449       {
450         GtkAction *a = action->data;
451         g_print ("action %s ref count %d\n", 
452                  gtk_action_get_name (a), G_OBJECT (a)->ref_count);
453       }
454   }
455 #endif
456   
457   g_object_unref (action);
458   g_object_unref (action_group);
459
460   gtk_accel_map_save ("accels");
461
462   return 0;
463 }