1 #undef GTK_DISABLE_DEPRECATED
5 # define _(String) (String)
6 # define N_(String) (String)
9 static GtkActionGroup *action_group = NULL;
10 static GtkToolbar *toolbar = NULL;
13 activate_action (GtkAction *action)
15 const gchar *name = gtk_action_get_name (action);
16 const gchar *typename = G_OBJECT_TYPE_NAME (action);
18 g_message ("Action %s (type=%s) activated", name, typename);
22 toggle_action (GtkAction *action)
24 const gchar *name = gtk_action_get_name (action);
25 const gchar *typename = G_OBJECT_TYPE_NAME (action);
27 g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
28 gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
32 toggle_cnp_actions (GtkAction *action)
36 sensitive = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
37 action = gtk_action_group_get_action (action_group, "cut");
38 g_object_set (action, "sensitive", sensitive, NULL);
39 action = gtk_action_group_get_action (action_group, "copy");
40 g_object_set (action, "sensitive", sensitive, NULL);
41 action = gtk_action_group_get_action (action_group, "paste");
42 g_object_set (action, "sensitive", sensitive, NULL);
44 action = gtk_action_group_get_action (action_group, "toggle-cnp");
46 g_object_set (action, "label", _("Disable Cut and paste ops"), NULL);
48 g_object_set (action, "label", _("Enable Cut and paste ops"), NULL);
52 show_accel_dialog (GtkAction *action)
54 g_message ("Sorry, accel dialog not available");
58 toolbar_style (GtkAction *action,
61 GtkToolbarStyle style;
63 g_return_if_fail (toolbar != NULL);
64 style = GPOINTER_TO_INT (user_data);
66 gtk_toolbar_set_style (toolbar, style);
70 toolbar_size (GtkAction *action,
75 g_return_if_fail (toolbar != NULL);
76 size = GPOINTER_TO_INT (user_data);
78 gtk_toolbar_set_icon_size (toolbar, size);
81 /* convenience functions for declaring actions */
82 static GtkActionGroupEntry entries[] = {
83 { "Menu1Action", N_("Menu _1"), NULL, NULL, NULL, NULL, NULL },
84 { "Menu2Action", N_("Menu _2"), NULL, NULL, NULL, NULL, NULL },
86 { "cut", N_("C_ut"), GTK_STOCK_CUT, "<control>X",
87 N_("Cut the selected text to the clipboard"),
88 G_CALLBACK (activate_action), NULL },
89 { "copy", N_("_Copy"), GTK_STOCK_COPY, "<control>C",
90 N_("Copy the selected text to the clipboard"),
91 G_CALLBACK (activate_action), NULL },
92 { "paste", N_("_Paste"), GTK_STOCK_PASTE, "<control>V",
93 N_("Paste the text from the clipboard"),
94 G_CALLBACK (activate_action), NULL },
95 { "bold", N_("_Bold"), GTK_STOCK_BOLD, "<control>B",
96 N_("Change to bold face"),
97 G_CALLBACK (toggle_action), NULL, GTK_ACTION_TOGGLE },
99 { "justify-left", N_("_Left"), GTK_STOCK_JUSTIFY_LEFT, "<control>L",
100 N_("Left justify the text"),
101 G_CALLBACK (toggle_action), NULL, GTK_ACTION_RADIO },
102 { "justify-center", N_("C_enter"), GTK_STOCK_JUSTIFY_CENTER, "<control>E",
103 N_("Center justify the text"),
104 G_CALLBACK (toggle_action), NULL, GTK_ACTION_RADIO, "justify-left" },
105 { "justify-right", N_("_Right"), GTK_STOCK_JUSTIFY_RIGHT, "<control>R",
106 N_("Right justify the text"),
107 G_CALLBACK (toggle_action), NULL, GTK_ACTION_RADIO, "justify-left" },
108 { "justify-fill", N_("_Fill"), GTK_STOCK_JUSTIFY_FILL, "<control>J",
109 N_("Fill justify the text"),
110 G_CALLBACK (toggle_action), NULL, GTK_ACTION_RADIO, "justify-left" },
111 { "quit", NULL, GTK_STOCK_QUIT, "<control>Q",
112 N_("Quit the application"),
113 G_CALLBACK (gtk_main_quit), NULL },
114 { "toggle-cnp", N_("Enable Cut/Copy/Paste"), NULL, NULL,
115 N_("Change the sensitivity of the cut, copy and paste actions"),
116 G_CALLBACK (toggle_cnp_actions), NULL, GTK_ACTION_TOGGLE },
117 { "customise-accels", N_("Customise _Accels"), NULL, NULL,
118 N_("Customise keyboard shortcuts"),
119 G_CALLBACK (show_accel_dialog), NULL },
120 { "toolbar-icons", N_("Icons"), NULL, NULL,
121 NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER (GTK_TOOLBAR_ICONS),
122 GTK_ACTION_RADIO, NULL },
123 { "toolbar-text", N_("Text"), NULL, NULL,
124 NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER (GTK_TOOLBAR_TEXT),
125 GTK_ACTION_RADIO, "toolbar-icons" },
126 { "toolbar-both", N_("Both"), NULL, NULL,
127 NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER (GTK_TOOLBAR_BOTH),
128 GTK_ACTION_RADIO, "toolbar-icons" },
129 { "toolbar-both-horiz", N_("Both Horizontal"), NULL, NULL,
130 NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER(GTK_TOOLBAR_BOTH_HORIZ),
131 GTK_ACTION_RADIO, "toolbar-icons" },
132 { "toolbar-small-icons", N_("Small Icons"), NULL, NULL,
134 G_CALLBACK (toolbar_size), GINT_TO_POINTER (GTK_ICON_SIZE_SMALL_TOOLBAR) },
135 { "toolbar-large-icons", N_("Large Icons"), NULL, NULL,
137 G_CALLBACK (toolbar_size), GINT_TO_POINTER (GTK_ICON_SIZE_LARGE_TOOLBAR) },
139 static guint n_entries = G_N_ELEMENTS (entries);
141 /* XML description of the menus for the test app. The parser understands
142 * a subset of the Bonobo UI XML format, and uses GMarkup for parsing */
143 static const gchar *ui_info =
145 " <menu name=\"Menu _1\" action=\"Menu1Action\">\n"
146 " <menuitem name=\"cut\" action=\"cut\" />\n"
147 " <menuitem name=\"copy\" action=\"copy\" />\n"
148 " <menuitem name=\"paste\" action=\"paste\" />\n"
149 " <separator name=\"sep1\" />\n"
150 " <menuitem name=\"bold1\" action=\"bold\" />\n"
151 " <menuitem name=\"bold2\" action=\"bold\" />\n"
152 " <separator name=\"sep2\" />\n"
153 " <menuitem name=\"toggle-cnp\" action=\"toggle-cnp\" />\n"
154 " <separator name=\"sep3\" />\n"
155 " <menuitem name=\"quit\" action=\"quit\" />\n"
157 " <menu name=\"Menu _2\" action=\"Menu2Action\">\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=\"sep4\"/>\n"
162 " <menuitem name=\"bold\" action=\"bold\" />\n"
163 " <separator name=\"sep5\"/>\n"
164 " <menuitem name=\"justify-left\" action=\"justify-left\" />\n"
165 " <menuitem name=\"justify-center\" action=\"justify-center\" />\n"
166 " <menuitem name=\"justify-right\" action=\"justify-right\" />\n"
167 " <menuitem name=\"justify-fill\" action=\"justify-fill\" />\n"
168 " <separator name=\"sep6\"/>\n"
169 " <menuitem name=\"customise-accels\" action=\"customise-accels\" />\n"
170 " <separator name=\"sep7\"/>\n"
171 " <menuitem action=\"toolbar-icons\" />\n"
172 " <menuitem action=\"toolbar-text\" />\n"
173 " <menuitem action=\"toolbar-both\" />\n"
174 " <menuitem action=\"toolbar-both-horiz\" />\n"
175 " <separator name=\"sep8\"/>\n"
176 " <menuitem action=\"toolbar-small-icons\" />\n"
177 " <menuitem action=\"toolbar-large-icons\" />\n"
180 " <toolbar name=\"toolbar\">\n"
181 " <toolitem name=\"cut\" action=\"cut\" />\n"
182 " <toolitem name=\"copy\" action=\"copy\" />\n"
183 " <toolitem name=\"paste\" action=\"paste\" />\n"
184 " <separator name=\"sep9\" />\n"
185 " <toolitem name=\"bold\" action=\"bold\" />\n"
186 " <separator name=\"sep10\" />\n"
187 " <toolitem name=\"justify-left\" action=\"justify-left\" />\n"
188 " <toolitem name=\"justify-center\" action=\"justify-center\" />\n"
189 " <toolitem name=\"justify-right\" action=\"justify-right\" />\n"
190 " <toolitem name=\"justify-fill\" action=\"justify-fill\" />\n"
191 " <separator name=\"sep11\"/>\n"
192 " <toolitem name=\"quit\" action=\"quit\" />\n"
196 add_widget (GtkUIManager *merge,
198 GtkContainer *container)
201 gtk_container_add (container, widget);
202 gtk_widget_show (widget);
204 if (GTK_IS_TOOLBAR (widget))
206 toolbar = GTK_TOOLBAR (widget);
207 gtk_toolbar_set_show_arrow (toolbar, TRUE);
212 create_window (GtkActionGroup *action_group)
217 GError *error = NULL;
219 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
220 gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
221 gtk_window_set_title (GTK_WINDOW (window), "Action Test");
222 g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
224 box = gtk_vbox_new (FALSE, 0);
225 gtk_container_add (GTK_CONTAINER (window), box);
226 gtk_widget_show (box);
228 merge = gtk_ui_manager_new ();
229 gtk_ui_manager_insert_action_group (merge, action_group, 0);
230 g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
232 gtk_window_add_accel_group (GTK_WINDOW (window),
233 gtk_ui_manager_get_accel_group (merge));
235 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
237 g_message ("building menus failed: %s", error->message);
238 g_error_free (error);
241 gtk_widget_show (window);
245 main (int argc, char **argv)
247 gtk_init (&argc, &argv);
249 if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
250 gtk_accel_map_load ("accels");
252 action_group = gtk_action_group_new ("TestActions");
253 gtk_action_group_add_actions (action_group, entries, n_entries);
255 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (gtk_action_group_get_action (action_group, "toggle-cnp")), TRUE);
257 create_window (action_group);
261 g_object_unref (action_group);
263 gtk_accel_map_save ("accels");