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