1 #undef GTK_DISABLE_DEPRECATED
5 static GtkActionGroup *action_group = NULL;
6 static GtkToolbar *toolbar = NULL;
9 activate_action (GtkAction *action)
11 const gchar *name = gtk_action_get_name (action);
12 const gchar *typename = G_OBJECT_TYPE_NAME (action);
14 g_message ("Action %s (type=%s) activated", name, typename);
18 toggle_action (GtkAction *action)
20 const gchar *name = gtk_action_get_name (action);
21 const gchar *typename = G_OBJECT_TYPE_NAME (action);
23 g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
24 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
29 radio_action (GtkAction *action)
31 const gchar *name = gtk_action_get_name (action);
32 const gchar *typename = G_OBJECT_TYPE_NAME (action);
34 g_message ("Action %s (type=%s) activated (active=%d) (value %d)", name, typename,
35 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
36 gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action)));
40 toggle_cnp_actions (GtkAction *action)
44 sensitive = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
45 action = gtk_action_group_get_action (action_group, "cut");
46 g_object_set (action, "sensitive", sensitive, NULL);
47 action = gtk_action_group_get_action (action_group, "copy");
48 g_object_set (action, "sensitive", sensitive, NULL);
49 action = gtk_action_group_get_action (action_group, "paste");
50 g_object_set (action, "sensitive", sensitive, NULL);
52 action = gtk_action_group_get_action (action_group, "toggle-cnp");
54 g_object_set (action, "label", "Disable Cut and paste ops", NULL);
56 g_object_set (action, "label", "Enable Cut and paste ops", NULL);
60 show_accel_dialog (GtkAction *action)
62 g_message ("Sorry, accel dialog not available");
66 toolbar_style (GtkAction *action,
69 GtkToolbarStyle style;
71 g_return_if_fail (toolbar != NULL);
72 style = GPOINTER_TO_INT (user_data);
74 gtk_toolbar_set_style (toolbar, style);
78 toolbar_size_small (GtkAction *action)
80 g_return_if_fail (toolbar != NULL);
82 gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
86 toolbar_size_large (GtkAction *action)
88 g_return_if_fail (toolbar != NULL);
90 gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
93 /* convenience functions for declaring actions */
94 static GtkActionEntry entries[] = {
95 { "Menu1Action", NULL, "Menu _1" },
96 { "Menu2Action", NULL, "Menu _2" },
97 { "Menu3Action", NULL, "_Dynamic Menu" },
99 { "cut", GTK_STOCK_CUT, "C_ut", "<control>X",
100 "Cut the selected text to the clipboard", G_CALLBACK (activate_action) },
101 { "copy", GTK_STOCK_COPY, "_Copy", "<control>C",
102 "Copy the selected text to the clipboard", G_CALLBACK (activate_action) },
103 { "paste", GTK_STOCK_PASTE, "_Paste", "<control>V",
104 "Paste the text from the clipboard", G_CALLBACK (activate_action) },
105 { "quit", GTK_STOCK_QUIT, NULL, "<control>Q",
106 "Quit the application", G_CALLBACK (gtk_main_quit) },
107 { "customise-accels", NULL, "Customise _Accels", NULL,
108 "Customise keyboard shortcuts", G_CALLBACK (show_accel_dialog) },
109 { "toolbar-small-icons", NULL, "Small Icons", NULL,
110 NULL, G_CALLBACK (toolbar_size_small) },
111 { "toolbar-large-icons", NULL, "Large Icons", NULL,
112 NULL, G_CALLBACK (toolbar_size_large) }
114 static guint n_entries = G_N_ELEMENTS (entries);
116 static GtkToggleActionEntry toggle_entries[] = {
117 { "bold", GTK_STOCK_BOLD, "_Bold", "<control>B",
118 "Change to bold face",
119 G_CALLBACK (toggle_action), FALSE },
120 { "toggle-cnp", NULL, "Enable Cut/Copy/Paste", NULL,
121 "Change the sensitivity of the cut, copy and paste actions",
122 G_CALLBACK (toggle_cnp_actions), TRUE },
124 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
133 static GtkRadioActionEntry justify_entries[] = {
134 { "justify-left", GTK_STOCK_JUSTIFY_LEFT, "_Left", "<control>L",
135 "Left justify the text", JUSTIFY_LEFT },
136 { "justify-center", GTK_STOCK_JUSTIFY_CENTER, "C_enter", "<control>E",
137 "Center justify the text", JUSTIFY_CENTER },
138 { "justify-right", GTK_STOCK_JUSTIFY_RIGHT, "_Right", "<control>R",
139 "Right justify the text", JUSTIFY_RIGHT },
140 { "justify-fill", GTK_STOCK_JUSTIFY_FILL, "_Fill", "<control>J",
141 "Fill justify the text", JUSTIFY_FILL }
143 static guint n_justify_entries = G_N_ELEMENTS (justify_entries);
145 static GtkRadioActionEntry toolbar_entries[] = {
146 { "toolbar-icons", NULL, "Icons", NULL, NULL, GTK_TOOLBAR_ICONS },
147 { "toolbar-text", NULL, "Text", NULL, NULL, GTK_TOOLBAR_TEXT },
148 { "toolbar-both", NULL, "Both", NULL, NULL, GTK_TOOLBAR_BOTH },
149 { "toolbar-both-horiz", NULL, "Both Horizontal", NULL, NULL, GTK_TOOLBAR_BOTH_HORIZ }
151 static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries);
153 /* XML description of the menus for the test app. The parser understands
154 * a subset of the Bonobo UI XML format, and uses GMarkup for parsing */
155 static const gchar *ui_info =
157 " <menu name=\"Menu _1\" action=\"Menu1Action\">\n"
158 " <menuitem name=\"cut\" action=\"cut\" />\n"
159 " <menuitem name=\"copy\" action=\"copy\" />\n"
160 " <menuitem name=\"paste\" action=\"paste\" />\n"
161 " <separator name=\"sep1\" />\n"
162 " <menuitem name=\"bold1\" action=\"bold\" />\n"
163 " <menuitem name=\"bold2\" action=\"bold\" />\n"
164 " <separator name=\"sep2\" />\n"
165 " <menuitem name=\"toggle-cnp\" action=\"toggle-cnp\" />\n"
166 " <separator name=\"sep3\" />\n"
167 " <menuitem name=\"quit\" action=\"quit\" />\n"
169 " <menu name=\"Menu _2\" action=\"Menu2Action\">\n"
170 " <menuitem name=\"cut\" action=\"cut\" />\n"
171 " <menuitem name=\"copy\" action=\"copy\" />\n"
172 " <menuitem name=\"paste\" action=\"paste\" />\n"
173 " <separator name=\"sep4\"/>\n"
174 " <menuitem name=\"bold\" action=\"bold\" />\n"
175 " <separator name=\"sep5\"/>\n"
176 " <menuitem name=\"justify-left\" action=\"justify-left\" />\n"
177 " <menuitem name=\"justify-center\" action=\"justify-center\" />\n"
178 " <menuitem name=\"justify-right\" action=\"justify-right\" />\n"
179 " <menuitem name=\"justify-fill\" action=\"justify-fill\" />\n"
180 " <separator name=\"sep6\"/>\n"
181 " <menuitem name=\"customise-accels\" action=\"customise-accels\" />\n"
182 " <separator name=\"sep7\"/>\n"
183 " <menuitem action=\"toolbar-icons\" />\n"
184 " <menuitem action=\"toolbar-text\" />\n"
185 " <menuitem action=\"toolbar-both\" />\n"
186 " <menuitem action=\"toolbar-both-horiz\" />\n"
187 " <separator name=\"sep8\"/>\n"
188 " <menuitem action=\"toolbar-small-icons\" />\n"
189 " <menuitem action=\"toolbar-large-icons\" />\n"
191 " <menu name=\"DynamicMenu\" action=\"Menu3Action\" />\n"
193 " <toolbar name=\"toolbar\">\n"
194 " <toolitem name=\"cut\" action=\"cut\" />\n"
195 " <toolitem name=\"copy\" action=\"copy\" />\n"
196 " <toolitem name=\"paste\" action=\"paste\" />\n"
197 " <separator name=\"sep9\" />\n"
198 " <toolitem name=\"bold\" action=\"bold\" />\n"
199 " <separator name=\"sep10\" />\n"
200 " <toolitem name=\"justify-left\" action=\"justify-left\" />\n"
201 " <toolitem name=\"justify-center\" action=\"justify-center\" />\n"
202 " <toolitem name=\"justify-right\" action=\"justify-right\" />\n"
203 " <toolitem name=\"justify-fill\" action=\"justify-fill\" />\n"
204 " <separator name=\"sep11\"/>\n"
205 " <toolitem name=\"quit\" action=\"quit\" />\n"
207 " <popup name=\"popup\">\n"
208 " <menuitem name=\"popcut\" action=\"cut\" />\n"
209 " <menuitem name=\"popcopy\" action=\"copy\" />\n"
210 " <menuitem name=\"poppaste\" action=\"paste\" />\n"
214 add_widget (GtkUIManager *merge,
216 GtkContainer *container)
219 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
220 gtk_widget_show (widget);
222 if (GTK_IS_TOOLBAR (widget))
224 toolbar = GTK_TOOLBAR (widget);
225 gtk_toolbar_set_show_arrow (toolbar, TRUE);
229 static guint ui_id = 0;
230 static GtkActionGroup *dag = NULL;
233 ensure_update (GtkUIManager *manager)
239 timer = g_timer_new ();
240 g_timer_start (timer);
242 gtk_ui_manager_ensure_update (manager);
244 g_timer_stop (timer);
245 seconds = g_timer_elapsed (timer, µsecs);
246 g_timer_destroy (timer);
248 g_print ("Time: %fs\n", seconds);
252 add_cb (GtkWidget *button,
253 GtkUIManager *manager)
255 GtkWidget *spinbutton;
260 if (ui_id != 0 || dag != NULL)
263 spinbutton = g_object_get_data (G_OBJECT (button), "spinbutton");
264 num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spinbutton));
266 dag = gtk_action_group_new ("DynamicActions");
267 gtk_ui_manager_insert_action_group (manager, dag, 0);
269 ui_id = gtk_ui_manager_new_merge_id (manager);
271 for (i = 0; i < num; i++)
273 name = g_strdup_printf ("DynAction%u", i);
274 label = g_strdup_printf ("Dynamic Item %d", i);
276 action = g_object_new (GTK_TYPE_ACTION,
280 gtk_action_group_add_action (dag, action);
281 g_object_unref (action);
283 gtk_ui_manager_add_ui (manager, ui_id, "/menubar/DynamicMenu",
285 GTK_UI_MANAGER_MENUITEM, FALSE);
288 ensure_update (manager);
292 remove_cb (GtkWidget *button,
293 GtkUIManager *manager)
295 if (ui_id == 0 || dag == NULL)
298 gtk_ui_manager_remove_ui (manager, ui_id);
299 ensure_update (manager);
302 gtk_ui_manager_remove_action_group (manager, dag);
303 g_object_unref (dag);
308 create_window (GtkActionGroup *action_group)
313 GtkWidget *hbox, *spinbutton, *button;
314 GError *error = NULL;
316 merge = gtk_ui_manager_new ();
318 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
319 gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
320 gtk_window_set_title (GTK_WINDOW (window), "Action Test");
321 g_signal_connect_swapped (window, "destroy", G_CALLBACK (g_object_unref), merge);
322 g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
324 box = gtk_vbox_new (FALSE, 0);
325 gtk_container_add (GTK_CONTAINER (window), box);
326 gtk_widget_show (box);
328 gtk_ui_manager_insert_action_group (merge, action_group, 0);
329 g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
331 gtk_window_add_accel_group (GTK_WINDOW (window),
332 gtk_ui_manager_get_accel_group (merge));
334 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
336 g_message ("building menus failed: %s", error->message);
337 g_error_free (error);
340 hbox = gtk_hbox_new (FALSE, 0);
341 gtk_box_pack_end (GTK_BOX (box), hbox, FALSE, FALSE, 0);
342 gtk_widget_show (hbox);
344 spinbutton = gtk_spin_button_new_with_range (100, 10000, 100);
345 gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
346 gtk_widget_show (spinbutton);
348 button = gtk_button_new_with_label ("Add");
349 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
350 gtk_widget_show (button);
352 g_object_set_data (G_OBJECT (button), "spinbutton", spinbutton);
353 g_signal_connect (button, "clicked", G_CALLBACK (add_cb), merge);
355 button = gtk_button_new_with_label ("Remove");
356 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
357 gtk_widget_show (button);
359 g_signal_connect (button, "clicked", G_CALLBACK (remove_cb), merge);
361 gtk_widget_show (window);
365 main (int argc, char **argv)
367 gtk_init (&argc, &argv);
369 if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
370 gtk_accel_map_load ("accels");
372 action_group = gtk_action_group_new ("TestActions");
373 gtk_action_group_add_actions (action_group,
376 gtk_action_group_add_toggle_actions (action_group,
377 toggle_entries, n_toggle_entries,
379 gtk_action_group_add_radio_actions (action_group,
380 justify_entries, n_justify_entries,
382 G_CALLBACK (radio_action), NULL);
383 gtk_action_group_add_radio_actions (action_group,
384 toolbar_entries, n_toolbar_entries,
386 G_CALLBACK (radio_action), NULL);
388 create_window (action_group);
392 #ifdef DEBUG_UI_MANAGER
396 for (action = gtk_action_group_list_actions (action_group);
398 action = action->next)
400 GtkAction *a = action->data;
401 g_print ("action %s ref count %d\n",
402 gtk_action_get_name (a), G_OBJECT (a)->ref_count);
407 g_object_unref (action_group);
409 gtk_accel_map_save ("accels");