2 * Copyright (C) 2003 Matthias Clasen
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.
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.
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.
20 #undef GTK_DISABLE_DEPRECATED
24 static GtkActionGroup *action_group = NULL;
25 static GtkToolbar *toolbar = NULL;
28 activate_action (GtkAction *action)
30 const gchar *name = gtk_action_get_name (action);
31 const gchar *typename = G_OBJECT_TYPE_NAME (action);
33 g_message ("Action %s (type=%s) activated", name, typename);
37 toggle_action (GtkAction *action)
39 const gchar *name = gtk_action_get_name (action);
40 const gchar *typename = G_OBJECT_TYPE_NAME (action);
42 g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
43 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
48 radio_action (GtkAction *action)
50 const gchar *name = gtk_action_get_name (action);
51 const gchar *typename = G_OBJECT_TYPE_NAME (action);
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)));
59 recent_action (GtkAction *action)
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));
65 g_message ("Action %s (type=%s) activated (uri=%s)",
67 uri ? uri : "no item selected");
72 toggle_cnp_actions (GtkAction *action)
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);
84 action = gtk_action_group_get_action (action_group, "toggle-cnp");
86 g_object_set (action, "label", "Disable Cut and paste ops", NULL);
88 g_object_set (action, "label", "Enable Cut and paste ops", NULL);
92 show_accel_dialog (GtkAction *action)
94 g_message ("Sorry, accel dialog not available");
98 toolbar_style (GtkAction *action,
101 GtkToolbarStyle style;
103 g_return_if_fail (toolbar != NULL);
104 style = GPOINTER_TO_INT (user_data);
106 gtk_toolbar_set_style (toolbar, style);
110 toolbar_size_small (GtkAction *action)
112 g_return_if_fail (toolbar != NULL);
114 gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
118 toolbar_size_large (GtkAction *action)
120 g_return_if_fail (toolbar != NULL);
122 gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
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" },
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) }
146 static guint n_entries = G_N_ELEMENTS (entries);
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 },
156 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
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 }
175 static guint n_justify_entries = G_N_ELEMENTS (justify_entries);
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 }
183 static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries);
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 =
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"
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"
225 " <menu name=\"DynamicMenu\" action=\"Menu3Action\" />\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"
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"
249 add_widget (GtkUIManager *merge,
251 GtkContainer *container)
254 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
255 gtk_widget_show (widget);
257 if (GTK_IS_TOOLBAR (widget))
259 toolbar = GTK_TOOLBAR (widget);
260 gtk_toolbar_set_show_arrow (toolbar, TRUE);
264 static guint ui_id = 0;
265 static GtkActionGroup *dag = NULL;
268 ensure_update (GtkUIManager *manager)
274 timer = g_timer_new ();
275 g_timer_start (timer);
277 gtk_ui_manager_ensure_update (manager);
279 g_timer_stop (timer);
280 seconds = g_timer_elapsed (timer, µsecs);
281 g_timer_destroy (timer);
283 g_print ("Time: %fs\n", seconds);
287 add_cb (GtkWidget *button,
288 GtkUIManager *manager)
290 GtkWidget *spinbutton;
295 if (ui_id != 0 || dag != NULL)
298 spinbutton = g_object_get_data (G_OBJECT (button), "spinbutton");
299 num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spinbutton));
301 dag = gtk_action_group_new ("DynamicActions");
302 gtk_ui_manager_insert_action_group (manager, dag, 0);
304 ui_id = gtk_ui_manager_new_merge_id (manager);
306 for (i = 0; i < num; i++)
308 name = g_strdup_printf ("DynAction%u", i);
309 label = g_strdup_printf ("Dynamic Item %d", i);
311 action = g_object_new (GTK_TYPE_ACTION,
315 gtk_action_group_add_action (dag, action);
316 g_object_unref (action);
318 gtk_ui_manager_add_ui (manager, ui_id, "/menubar/DynamicMenu",
320 GTK_UI_MANAGER_MENUITEM, FALSE);
323 ensure_update (manager);
327 remove_cb (GtkWidget *button,
328 GtkUIManager *manager)
330 if (ui_id == 0 || dag == NULL)
333 gtk_ui_manager_remove_ui (manager, ui_id);
334 ensure_update (manager);
337 gtk_ui_manager_remove_action_group (manager, dag);
338 g_object_unref (dag);
343 create_window (GtkActionGroup *action_group)
348 GtkWidget *hbox, *spinbutton, *button;
349 GError *error = NULL;
351 merge = gtk_ui_manager_new ();
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);
359 box = gtk_vbox_new (FALSE, 0);
360 gtk_container_add (GTK_CONTAINER (window), box);
361 gtk_widget_show (box);
363 gtk_ui_manager_insert_action_group (merge, action_group, 0);
364 g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
366 gtk_window_add_accel_group (GTK_WINDOW (window),
367 gtk_ui_manager_get_accel_group (merge));
369 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
371 g_message ("building menus failed: %s", error->message);
372 g_error_free (error);
375 hbox = gtk_hbox_new (FALSE, 0);
376 gtk_box_pack_end (GTK_BOX (box), hbox, FALSE, FALSE, 0);
377 gtk_widget_show (hbox);
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);
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);
387 g_object_set_data (G_OBJECT (button), "spinbutton", spinbutton);
388 g_signal_connect (button, "clicked", G_CALLBACK (add_cb), merge);
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);
394 g_signal_connect (button, "clicked", G_CALLBACK (remove_cb), merge);
396 gtk_widget_show (window);
400 main (int argc, char **argv)
404 gtk_init (&argc, &argv);
406 if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
407 gtk_accel_map_load ("accels");
409 action = gtk_recent_action_new ("recent",
410 "Open Recent", "Open recent files",
412 g_signal_connect (action, "item-activated",
413 G_CALLBACK (recent_action),
415 g_signal_connect (action, "activate",
416 G_CALLBACK (recent_action),
419 action_group = gtk_action_group_new ("TestActions");
420 gtk_action_group_add_actions (action_group,
423 gtk_action_group_add_toggle_actions (action_group,
424 toggle_entries, n_toggle_entries,
426 gtk_action_group_add_radio_actions (action_group,
427 justify_entries, n_justify_entries,
429 G_CALLBACK (radio_action), NULL);
430 gtk_action_group_add_radio_actions (action_group,
431 toolbar_entries, n_toolbar_entries,
433 G_CALLBACK (radio_action), NULL);
434 gtk_action_group_add_action_with_accel (action_group, action, NULL);
436 create_window (action_group);
440 #ifdef DEBUG_UI_MANAGER
444 for (action = gtk_action_group_list_actions (action_group);
446 action = action->next)
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);
455 g_object_unref (action);
456 g_object_unref (action_group);
458 gtk_accel_map_save ("accels");