1 #undef GTK_DISABLE_DEPRECATED
4 static GtkActionGroup *action_group = NULL;
5 static GtkToolbar *toolbar = NULL;
8 activate_action (GtkAction *action)
10 const gchar *name = gtk_action_get_name (action);
11 const gchar *typename = G_OBJECT_TYPE_NAME (action);
13 g_message ("Action %s (type=%s) activated", name, typename);
17 toggle_action (GtkAction *action)
19 const gchar *name = gtk_action_get_name (action);
20 const gchar *typename = G_OBJECT_TYPE_NAME (action);
22 g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
23 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
28 radio_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 (active=%d) (value %d)", name, typename,
34 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
35 gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action)));
39 toggle_cnp_actions (GtkAction *action)
43 sensitive = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
44 action = gtk_action_group_get_action (action_group, "cut");
45 g_object_set (action, "sensitive", sensitive, NULL);
46 action = gtk_action_group_get_action (action_group, "copy");
47 g_object_set (action, "sensitive", sensitive, NULL);
48 action = gtk_action_group_get_action (action_group, "paste");
49 g_object_set (action, "sensitive", sensitive, NULL);
51 action = gtk_action_group_get_action (action_group, "toggle-cnp");
53 g_object_set (action, "label", "Disable Cut and paste ops", NULL);
55 g_object_set (action, "label", "Enable Cut and paste ops", NULL);
59 show_accel_dialog (GtkAction *action)
61 g_message ("Sorry, accel dialog not available");
65 toolbar_style (GtkAction *action,
68 GtkToolbarStyle style;
70 g_return_if_fail (toolbar != NULL);
71 style = GPOINTER_TO_INT (user_data);
73 gtk_toolbar_set_style (toolbar, style);
77 toolbar_size_small (GtkAction *action)
79 g_return_if_fail (toolbar != NULL);
81 gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
85 toolbar_size_large (GtkAction *action)
87 g_return_if_fail (toolbar != NULL);
89 gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
92 /* convenience functions for declaring actions */
93 static GtkActionEntry entries[] = {
94 { "Menu1Action", NULL, "Menu _1" },
95 { "Menu2Action", NULL, "Menu _2" },
97 { "cut", GTK_STOCK_CUT, "C_ut", "<control>X",
98 "Cut the selected text to the clipboard", G_CALLBACK (activate_action) },
99 { "copy", GTK_STOCK_COPY, "_Copy", "<control>C",
100 "Copy the selected text to the clipboard", G_CALLBACK (activate_action) },
101 { "paste", GTK_STOCK_PASTE, "_Paste", "<control>V",
102 "Paste the text from the clipboard", G_CALLBACK (activate_action) },
103 { "quit", GTK_STOCK_QUIT, NULL, "<control>Q",
104 "Quit the application", G_CALLBACK (gtk_main_quit) },
105 { "customise-accels", NULL, "Customise _Accels", NULL,
106 "Customise keyboard shortcuts", G_CALLBACK (show_accel_dialog) },
107 { "toolbar-small-icons", NULL, "Small Icons", NULL,
108 NULL, G_CALLBACK (toolbar_size_small) },
109 { "toolbar-large-icons", NULL, "Large Icons", NULL,
110 NULL, G_CALLBACK (toolbar_size_large) }
112 static guint n_entries = G_N_ELEMENTS (entries);
114 static GtkToggleActionEntry toggle_entries[] = {
115 { "bold", GTK_STOCK_BOLD, "_Bold", "<control>B",
116 "Change to bold face",
117 G_CALLBACK (toggle_action), FALSE },
118 { "toggle-cnp", NULL, "Enable Cut/Copy/Paste", NULL,
119 "Change the sensitivity of the cut, copy and paste actions",
120 G_CALLBACK (toggle_cnp_actions), TRUE },
122 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
131 static GtkRadioActionEntry justify_entries[] = {
132 { "justify-left", GTK_STOCK_JUSTIFY_LEFT, "_Left", "<control>L",
133 "Left justify the text", JUSTIFY_LEFT },
134 { "justify-center", GTK_STOCK_JUSTIFY_CENTER, "C_enter", "<control>E",
135 "Center justify the text", JUSTIFY_CENTER },
136 { "justify-right", GTK_STOCK_JUSTIFY_RIGHT, "_Right", "<control>R",
137 "Right justify the text", JUSTIFY_RIGHT },
138 { "justify-fill", GTK_STOCK_JUSTIFY_FILL, "_Fill", "<control>J",
139 "Fill justify the text", JUSTIFY_FILL }
141 static guint n_justify_entries = G_N_ELEMENTS (justify_entries);
143 static GtkRadioActionEntry toolbar_entries[] = {
144 { "toolbar-icons", NULL, "Icons", NULL, NULL, GTK_TOOLBAR_ICONS },
145 { "toolbar-text", NULL, "Text", NULL, NULL, GTK_TOOLBAR_TEXT },
146 { "toolbar-both", NULL, "Both", NULL, NULL, GTK_TOOLBAR_BOTH },
147 { "toolbar-both-horiz", NULL, "Both Horizontal", NULL, NULL, GTK_TOOLBAR_BOTH_HORIZ }
149 static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries);
151 /* XML description of the menus for the test app. The parser understands
152 * a subset of the Bonobo UI XML format, and uses GMarkup for parsing */
153 static const gchar *ui_info =
155 " <menu name=\"Menu _1\" action=\"Menu1Action\">\n"
156 " <menuitem name=\"cut\" action=\"cut\" />\n"
157 " <menuitem name=\"copy\" action=\"copy\" />\n"
158 " <menuitem name=\"paste\" action=\"paste\" />\n"
159 " <separator name=\"sep1\" />\n"
160 " <menuitem name=\"bold1\" action=\"bold\" />\n"
161 " <menuitem name=\"bold2\" action=\"bold\" />\n"
162 " <separator name=\"sep2\" />\n"
163 " <menuitem name=\"toggle-cnp\" action=\"toggle-cnp\" />\n"
164 " <separator name=\"sep3\" />\n"
165 " <menuitem name=\"quit\" action=\"quit\" />\n"
167 " <menu name=\"Menu _2\" action=\"Menu2Action\">\n"
168 " <menuitem name=\"cut\" action=\"cut\" />\n"
169 " <menuitem name=\"copy\" action=\"copy\" />\n"
170 " <menuitem name=\"paste\" action=\"paste\" />\n"
171 " <separator name=\"sep4\"/>\n"
172 " <menuitem name=\"bold\" action=\"bold\" />\n"
173 " <separator name=\"sep5\"/>\n"
174 " <menuitem name=\"justify-left\" action=\"justify-left\" />\n"
175 " <menuitem name=\"justify-center\" action=\"justify-center\" />\n"
176 " <menuitem name=\"justify-right\" action=\"justify-right\" />\n"
177 " <menuitem name=\"justify-fill\" action=\"justify-fill\" />\n"
178 " <separator name=\"sep6\"/>\n"
179 " <menuitem name=\"customise-accels\" action=\"customise-accels\" />\n"
180 " <separator name=\"sep7\"/>\n"
181 " <menuitem action=\"toolbar-icons\" />\n"
182 " <menuitem action=\"toolbar-text\" />\n"
183 " <menuitem action=\"toolbar-both\" />\n"
184 " <menuitem action=\"toolbar-both-horiz\" />\n"
185 " <separator name=\"sep8\"/>\n"
186 " <menuitem action=\"toolbar-small-icons\" />\n"
187 " <menuitem action=\"toolbar-large-icons\" />\n"
190 " <toolbar name=\"toolbar\">\n"
191 " <toolitem name=\"cut\" action=\"cut\" />\n"
192 " <toolitem name=\"copy\" action=\"copy\" />\n"
193 " <toolitem name=\"paste\" action=\"paste\" />\n"
194 " <separator name=\"sep9\" />\n"
195 " <toolitem name=\"bold\" action=\"bold\" />\n"
196 " <separator name=\"sep10\" />\n"
197 " <toolitem name=\"justify-left\" action=\"justify-left\" />\n"
198 " <toolitem name=\"justify-center\" action=\"justify-center\" />\n"
199 " <toolitem name=\"justify-right\" action=\"justify-right\" />\n"
200 " <toolitem name=\"justify-fill\" action=\"justify-fill\" />\n"
201 " <separator name=\"sep11\"/>\n"
202 " <toolitem name=\"quit\" action=\"quit\" />\n"
206 add_widget (GtkUIManager *merge,
208 GtkContainer *container)
211 gtk_container_add (container, widget);
212 gtk_widget_show (widget);
214 if (GTK_IS_TOOLBAR (widget))
216 toolbar = GTK_TOOLBAR (widget);
217 gtk_toolbar_set_show_arrow (toolbar, TRUE);
222 create_window (GtkActionGroup *action_group)
227 GError *error = NULL;
229 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
230 gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
231 gtk_window_set_title (GTK_WINDOW (window), "Action Test");
232 g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
234 box = gtk_vbox_new (FALSE, 0);
235 gtk_container_add (GTK_CONTAINER (window), box);
236 gtk_widget_show (box);
238 merge = gtk_ui_manager_new ();
239 gtk_ui_manager_insert_action_group (merge, action_group, 0);
240 g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
242 gtk_window_add_accel_group (GTK_WINDOW (window),
243 gtk_ui_manager_get_accel_group (merge));
245 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
247 g_message ("building menus failed: %s", error->message);
248 g_error_free (error);
251 gtk_widget_show (window);
255 main (int argc, char **argv)
257 gtk_init (&argc, &argv);
259 if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
260 gtk_accel_map_load ("accels");
262 action_group = gtk_action_group_new ("TestActions");
263 gtk_action_group_add_actions (action_group,
266 gtk_action_group_add_toggle_actions (action_group,
267 toggle_entries, n_toggle_entries,
269 gtk_action_group_add_radio_actions (action_group,
270 justify_entries, n_justify_entries,
272 G_CALLBACK (radio_action), NULL);
273 gtk_action_group_add_radio_actions (action_group,
274 toolbar_entries, n_toolbar_entries,
276 G_CALLBACK (radio_action), NULL);
278 create_window (action_group);
282 g_object_unref (action_group);
284 gtk_accel_map_save ("accels");