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, see <http://www.gnu.org/licenses/>.
21 static GtkActionGroup *action_group = NULL;
22 static GtkToolbar *toolbar = NULL;
25 activate_action (GtkAction *action)
27 const gchar *name = gtk_action_get_name (action);
28 const gchar *typename = G_OBJECT_TYPE_NAME (action);
30 g_message ("Action %s (type=%s) activated", name, typename);
34 toggle_action (GtkAction *action)
36 const gchar *name = gtk_action_get_name (action);
37 const gchar *typename = G_OBJECT_TYPE_NAME (action);
39 g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
40 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
45 radio_action (GtkAction *action)
47 const gchar *name = gtk_action_get_name (action);
48 const gchar *typename = G_OBJECT_TYPE_NAME (action);
50 g_message ("Action %s (type=%s) activated (active=%d) (value %d)", name, typename,
51 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
52 gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action)));
56 recent_action (GtkAction *action)
58 const gchar *name = gtk_action_get_name (action);
59 const gchar *typename = G_OBJECT_TYPE_NAME (action);
60 gchar *uri = gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (action));
62 g_message ("Action %s (type=%s) activated (uri=%s)",
64 uri ? uri : "no item selected");
69 toggle_cnp_actions (GtkAction *action)
73 sensitive = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
74 action = gtk_action_group_get_action (action_group, "cut");
75 g_object_set (action, "sensitive", sensitive, NULL);
76 action = gtk_action_group_get_action (action_group, "copy");
77 g_object_set (action, "sensitive", sensitive, NULL);
78 action = gtk_action_group_get_action (action_group, "paste");
79 g_object_set (action, "sensitive", sensitive, NULL);
81 action = gtk_action_group_get_action (action_group, "toggle-cnp");
83 g_object_set (action, "label", "Disable Cut and paste ops", NULL);
85 g_object_set (action, "label", "Enable Cut and paste ops", NULL);
89 show_accel_dialog (GtkAction *action)
91 g_message ("Sorry, accel dialog not available");
95 toolbar_style (GtkAction *action)
97 GtkToolbarStyle style;
99 g_return_if_fail (toolbar != NULL);
101 radio_action (action);
103 style = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action));
105 gtk_toolbar_set_style (toolbar, style);
109 toolbar_size_small (GtkAction *action)
111 g_return_if_fail (toolbar != NULL);
113 gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
117 toolbar_size_large (GtkAction *action)
119 g_return_if_fail (toolbar != NULL);
121 gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
124 /* convenience functions for declaring actions */
125 static GtkActionEntry entries[] = {
126 { "Menu1Action", NULL, "Menu _1" },
127 { "Menu2Action", NULL, "Menu _2" },
128 { "Menu3Action", NULL, "_Dynamic Menu" },
130 { "attach", "mail-attachment", "_Attachment...", "<Control>m",
131 "Attach a file", G_CALLBACK (activate_action) },
132 { "cut", GTK_STOCK_CUT, "C_ut", "<control>X",
133 "Cut the selected text to the clipboard", G_CALLBACK (activate_action) },
134 { "copy", GTK_STOCK_COPY, "_Copy", "<control>C",
135 "Copy the selected text to the clipboard", G_CALLBACK (activate_action) },
136 { "paste", GTK_STOCK_PASTE, "_Paste", "<control>V",
137 "Paste the text from the clipboard", G_CALLBACK (activate_action) },
138 { "quit", GTK_STOCK_QUIT, NULL, "<control>Q",
139 "Quit the application", G_CALLBACK (gtk_main_quit) },
140 { "customise-accels", NULL, "Customise _Accels", NULL,
141 "Customise keyboard shortcuts", G_CALLBACK (show_accel_dialog) },
142 { "toolbar-small-icons", NULL, "Small Icons", NULL,
143 NULL, G_CALLBACK (toolbar_size_small) },
144 { "toolbar-large-icons", NULL, "Large Icons", NULL,
145 NULL, G_CALLBACK (toolbar_size_large) }
147 static guint n_entries = G_N_ELEMENTS (entries);
149 static GtkToggleActionEntry toggle_entries[] = {
150 { "bold", GTK_STOCK_BOLD, "_Bold", "<control>B",
151 "Change to bold face",
152 G_CALLBACK (toggle_action), FALSE },
153 { "toggle-cnp", NULL, "Enable Cut/Copy/Paste", NULL,
154 "Change the sensitivity of the cut, copy and paste actions",
155 G_CALLBACK (toggle_cnp_actions), TRUE },
157 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
166 static GtkRadioActionEntry justify_entries[] = {
167 { "justify-left", GTK_STOCK_JUSTIFY_LEFT, "_Left", "<control>L",
168 "Left justify the text", JUSTIFY_LEFT },
169 { "justify-center", GTK_STOCK_JUSTIFY_CENTER, "C_enter", "<control>E",
170 "Center justify the text", JUSTIFY_CENTER },
171 { "justify-right", GTK_STOCK_JUSTIFY_RIGHT, "_Right", "<control>R",
172 "Right justify the text", JUSTIFY_RIGHT },
173 { "justify-fill", GTK_STOCK_JUSTIFY_FILL, "_Fill", "<control>J",
174 "Fill justify the text", JUSTIFY_FILL }
176 static guint n_justify_entries = G_N_ELEMENTS (justify_entries);
178 static GtkRadioActionEntry toolbar_entries[] = {
179 { "toolbar-icons", NULL, "Icons", NULL, NULL, GTK_TOOLBAR_ICONS },
180 { "toolbar-text", NULL, "Text", NULL, NULL, GTK_TOOLBAR_TEXT },
181 { "toolbar-both", NULL, "Both", NULL, NULL, GTK_TOOLBAR_BOTH },
182 { "toolbar-both-horiz", NULL, "Both Horizontal", NULL, NULL, GTK_TOOLBAR_BOTH_HORIZ }
184 static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries);
186 /* XML description of the menus for the test app. The parser understands
187 * a subset of the Bonobo UI XML format, and uses GMarkup for parsing */
188 static const gchar *ui_info =
190 " <menu name=\"Menu _1\" action=\"Menu1Action\">\n"
191 " <menuitem name=\"cut\" action=\"cut\" />\n"
192 " <menuitem name=\"copy\" action=\"copy\" />\n"
193 " <menuitem name=\"paste\" action=\"paste\" />\n"
194 " <separator name=\"sep1\" />\n"
195 " <menuitem name=\"bold1\" action=\"bold\" />\n"
196 " <menuitem name=\"bold2\" action=\"bold\" />\n"
197 " <separator name=\"sep2\" />\n"
198 " <menuitem name=\"recent\" action=\"recent\" />\n"
199 " <separator name=\"sep3\" />\n"
200 " <menuitem name=\"toggle-cnp\" action=\"toggle-cnp\" />\n"
201 " <separator name=\"sep4\" />\n"
202 " <menuitem name=\"quit\" action=\"quit\" />\n"
204 " <menu name=\"Menu _2\" action=\"Menu2Action\">\n"
205 " <menuitem name=\"cut\" action=\"cut\" />\n"
206 " <menuitem name=\"copy\" action=\"copy\" />\n"
207 " <menuitem name=\"paste\" action=\"paste\" />\n"
208 " <separator name=\"sep5\"/>\n"
209 " <menuitem name=\"bold\" action=\"bold\" />\n"
210 " <separator name=\"sep6\"/>\n"
211 " <menuitem name=\"justify-left\" action=\"justify-left\" />\n"
212 " <menuitem name=\"justify-center\" action=\"justify-center\" />\n"
213 " <menuitem name=\"justify-right\" action=\"justify-right\" />\n"
214 " <menuitem name=\"justify-fill\" action=\"justify-fill\" />\n"
215 " <separator name=\"sep7\"/>\n"
216 " <menuitem name=\"customise-accels\" action=\"customise-accels\" />\n"
217 " <separator name=\"sep8\"/>\n"
218 " <menuitem action=\"toolbar-icons\" />\n"
219 " <menuitem action=\"toolbar-text\" />\n"
220 " <menuitem action=\"toolbar-both\" />\n"
221 " <menuitem action=\"toolbar-both-horiz\" />\n"
222 " <separator name=\"sep9\"/>\n"
223 " <menuitem action=\"toolbar-small-icons\" />\n"
224 " <menuitem action=\"toolbar-large-icons\" />\n"
226 " <menu name=\"DynamicMenu\" action=\"Menu3Action\" />\n"
228 " <toolbar name=\"toolbar\">\n"
229 " <toolitem name=\"attach\" action=\"attach\" />\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"
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"
251 add_widget (GtkUIManager *merge,
253 GtkContainer *container)
256 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
257 gtk_widget_show (widget);
259 if (GTK_IS_TOOLBAR (widget))
261 toolbar = GTK_TOOLBAR (widget);
262 gtk_toolbar_set_show_arrow (toolbar, TRUE);
266 static guint ui_id = 0;
267 static GtkActionGroup *dag = NULL;
270 ensure_update (GtkUIManager *manager)
276 timer = g_timer_new ();
277 g_timer_start (timer);
279 gtk_ui_manager_ensure_update (manager);
281 g_timer_stop (timer);
282 seconds = g_timer_elapsed (timer, µsecs);
283 g_timer_destroy (timer);
285 g_print ("Time: %fs\n", seconds);
289 add_cb (GtkWidget *button,
290 GtkUIManager *manager)
292 GtkWidget *spinbutton;
297 if (ui_id != 0 || dag != NULL)
300 spinbutton = g_object_get_data (G_OBJECT (button), "spinbutton");
301 num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spinbutton));
303 dag = gtk_action_group_new ("DynamicActions");
304 gtk_ui_manager_insert_action_group (manager, dag, 0);
306 ui_id = gtk_ui_manager_new_merge_id (manager);
308 for (i = 0; i < num; i++)
310 name = g_strdup_printf ("DynAction%u", i);
311 label = g_strdup_printf ("Dynamic Item %d", i);
313 action = g_object_new (GTK_TYPE_ACTION,
317 gtk_action_group_add_action (dag, action);
318 g_object_unref (action);
320 gtk_ui_manager_add_ui (manager, ui_id, "/menubar/DynamicMenu",
322 GTK_UI_MANAGER_MENUITEM, FALSE);
325 ensure_update (manager);
329 remove_cb (GtkWidget *button,
330 GtkUIManager *manager)
332 if (ui_id == 0 || dag == NULL)
335 gtk_ui_manager_remove_ui (manager, ui_id);
336 ensure_update (manager);
339 gtk_ui_manager_remove_action_group (manager, dag);
340 g_object_unref (dag);
345 create_window (GtkActionGroup *action_group)
350 GtkWidget *hbox, *spinbutton, *button;
351 GError *error = NULL;
353 merge = gtk_ui_manager_new ();
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);
361 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
362 gtk_container_add (GTK_CONTAINER (window), box);
363 gtk_widget_show (box);
365 gtk_ui_manager_insert_action_group (merge, action_group, 0);
366 g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
368 gtk_window_add_accel_group (GTK_WINDOW (window),
369 gtk_ui_manager_get_accel_group (merge));
371 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
373 g_message ("building menus failed: %s", error->message);
374 g_error_free (error);
377 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
378 gtk_box_pack_end (GTK_BOX (box), hbox, FALSE, FALSE, 0);
379 gtk_widget_show (hbox);
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);
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);
389 g_object_set_data (G_OBJECT (button), "spinbutton", spinbutton);
390 g_signal_connect (button, "clicked", G_CALLBACK (add_cb), merge);
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);
396 g_signal_connect (button, "clicked", G_CALLBACK (remove_cb), merge);
398 gtk_widget_show (window);
402 main (int argc, char **argv)
406 gtk_init (&argc, &argv);
408 if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
409 gtk_accel_map_load ("accels");
411 action = gtk_recent_action_new ("recent",
412 "Open Recent", "Open recent files",
414 g_signal_connect (action, "item-activated",
415 G_CALLBACK (recent_action),
417 g_signal_connect (action, "activate",
418 G_CALLBACK (recent_action),
421 action_group = gtk_action_group_new ("TestActions");
422 gtk_action_group_add_actions (action_group,
425 gtk_action_group_add_toggle_actions (action_group,
426 toggle_entries, n_toggle_entries,
428 gtk_action_group_add_radio_actions (action_group,
429 justify_entries, n_justify_entries,
431 G_CALLBACK (radio_action), NULL);
432 gtk_action_group_add_radio_actions (action_group,
433 toolbar_entries, n_toolbar_entries,
435 G_CALLBACK (toolbar_style), NULL);
436 gtk_action_group_add_action_with_accel (action_group, action, NULL);
438 create_window (action_group);
442 #ifdef DEBUG_UI_MANAGER
446 for (action = gtk_action_group_list_actions (action_group);
448 action = action->next)
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);
457 g_object_unref (action);
458 g_object_unref (action_group);
460 gtk_accel_map_save ("accels");