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.
23 static GtkActionGroup *action_group = NULL;
24 static GtkToolbar *toolbar = NULL;
27 activate_action (GtkAction *action)
29 const gchar *name = gtk_action_get_name (action);
30 const gchar *typename = G_OBJECT_TYPE_NAME (action);
32 g_message ("Action %s (type=%s) activated", name, typename);
36 toggle_action (GtkAction *action)
38 const gchar *name = gtk_action_get_name (action);
39 const gchar *typename = G_OBJECT_TYPE_NAME (action);
41 g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
42 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
47 radio_action (GtkAction *action)
49 const gchar *name = gtk_action_get_name (action);
50 const gchar *typename = G_OBJECT_TYPE_NAME (action);
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)));
58 recent_action (GtkAction *action)
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));
64 g_message ("Action %s (type=%s) activated (uri=%s)",
66 uri ? uri : "no item selected");
71 toggle_cnp_actions (GtkAction *action)
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);
83 action = gtk_action_group_get_action (action_group, "toggle-cnp");
85 g_object_set (action, "label", "Disable Cut and paste ops", NULL);
87 g_object_set (action, "label", "Enable Cut and paste ops", NULL);
91 show_accel_dialog (GtkAction *action)
93 g_message ("Sorry, accel dialog not available");
97 toolbar_style (GtkAction *action)
99 GtkToolbarStyle style;
101 g_return_if_fail (toolbar != NULL);
103 radio_action (action);
105 style = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action));
107 gtk_toolbar_set_style (toolbar, style);
111 toolbar_size_small (GtkAction *action)
113 g_return_if_fail (toolbar != NULL);
115 gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
119 toolbar_size_large (GtkAction *action)
121 g_return_if_fail (toolbar != NULL);
123 gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
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" },
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) }
149 static guint n_entries = G_N_ELEMENTS (entries);
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 },
159 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
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 }
178 static guint n_justify_entries = G_N_ELEMENTS (justify_entries);
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 }
186 static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries);
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 =
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"
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"
228 " <menu name=\"DynamicMenu\" action=\"Menu3Action\" />\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"
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"
253 add_widget (GtkUIManager *merge,
255 GtkContainer *container)
258 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
259 gtk_widget_show (widget);
261 if (GTK_IS_TOOLBAR (widget))
263 toolbar = GTK_TOOLBAR (widget);
264 gtk_toolbar_set_show_arrow (toolbar, TRUE);
268 static guint ui_id = 0;
269 static GtkActionGroup *dag = NULL;
272 ensure_update (GtkUIManager *manager)
278 timer = g_timer_new ();
279 g_timer_start (timer);
281 gtk_ui_manager_ensure_update (manager);
283 g_timer_stop (timer);
284 seconds = g_timer_elapsed (timer, µsecs);
285 g_timer_destroy (timer);
287 g_print ("Time: %fs\n", seconds);
291 add_cb (GtkWidget *button,
292 GtkUIManager *manager)
294 GtkWidget *spinbutton;
299 if (ui_id != 0 || dag != NULL)
302 spinbutton = g_object_get_data (G_OBJECT (button), "spinbutton");
303 num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spinbutton));
305 dag = gtk_action_group_new ("DynamicActions");
306 gtk_ui_manager_insert_action_group (manager, dag, 0);
308 ui_id = gtk_ui_manager_new_merge_id (manager);
310 for (i = 0; i < num; i++)
312 name = g_strdup_printf ("DynAction%u", i);
313 label = g_strdup_printf ("Dynamic Item %d", i);
315 action = g_object_new (GTK_TYPE_ACTION,
319 gtk_action_group_add_action (dag, action);
320 g_object_unref (action);
322 gtk_ui_manager_add_ui (manager, ui_id, "/menubar/DynamicMenu",
324 GTK_UI_MANAGER_MENUITEM, FALSE);
327 ensure_update (manager);
331 remove_cb (GtkWidget *button,
332 GtkUIManager *manager)
334 if (ui_id == 0 || dag == NULL)
337 gtk_ui_manager_remove_ui (manager, ui_id);
338 ensure_update (manager);
341 gtk_ui_manager_remove_action_group (manager, dag);
342 g_object_unref (dag);
347 create_window (GtkActionGroup *action_group)
352 GtkWidget *hbox, *spinbutton, *button;
353 GError *error = NULL;
355 merge = gtk_ui_manager_new ();
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);
363 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
364 gtk_container_add (GTK_CONTAINER (window), box);
365 gtk_widget_show (box);
367 gtk_ui_manager_insert_action_group (merge, action_group, 0);
368 g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
370 gtk_window_add_accel_group (GTK_WINDOW (window),
371 gtk_ui_manager_get_accel_group (merge));
373 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
375 g_message ("building menus failed: %s", error->message);
376 g_error_free (error);
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);
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);
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);
391 g_object_set_data (G_OBJECT (button), "spinbutton", spinbutton);
392 g_signal_connect (button, "clicked", G_CALLBACK (add_cb), merge);
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);
398 g_signal_connect (button, "clicked", G_CALLBACK (remove_cb), merge);
400 gtk_widget_show (window);
404 main (int argc, char **argv)
408 gtk_init (&argc, &argv);
410 if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
411 gtk_accel_map_load ("accels");
413 action = gtk_recent_action_new ("recent",
414 "Open Recent", "Open recent files",
416 g_signal_connect (action, "item-activated",
417 G_CALLBACK (recent_action),
419 g_signal_connect (action, "activate",
420 G_CALLBACK (recent_action),
423 action_group = gtk_action_group_new ("TestActions");
424 gtk_action_group_add_actions (action_group,
427 gtk_action_group_add_toggle_actions (action_group,
428 toggle_entries, n_toggle_entries,
430 gtk_action_group_add_radio_actions (action_group,
431 justify_entries, n_justify_entries,
433 G_CALLBACK (radio_action), NULL);
434 gtk_action_group_add_radio_actions (action_group,
435 toolbar_entries, n_toolbar_entries,
437 G_CALLBACK (toolbar_style), NULL);
438 gtk_action_group_add_action_with_accel (action_group, action, NULL);
440 create_window (action_group);
444 #ifdef DEBUG_UI_MANAGER
448 for (action = gtk_action_group_list_actions (action_group);
450 action = action->next)
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);
459 g_object_unref (action);
460 g_object_unref (action_group);
462 gtk_accel_map_save ("accels");