]> Pileus Git - ~andy/gtk/blob - tests/testactions.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / tests / testactions.c
1 #undef GTK_DISABLE_DEPRECATED
2 #include <config.h>
3 #include <gtk/gtk.h>
4
5 static GtkActionGroup *action_group = NULL;
6 static GtkToolbar *toolbar = NULL;
7
8 static void
9 activate_action (GtkAction *action)
10 {
11   const gchar *name = gtk_action_get_name (action);
12   const gchar *typename = G_OBJECT_TYPE_NAME (action);
13
14   g_message ("Action %s (type=%s) activated", name, typename);
15 }
16
17 static void
18 toggle_action (GtkAction *action)
19 {
20   const gchar *name = gtk_action_get_name (action);
21   const gchar *typename = G_OBJECT_TYPE_NAME (action);
22
23   g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
24              gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
25 }
26
27
28 static void
29 radio_action (GtkAction *action)
30 {
31   const gchar *name = gtk_action_get_name (action);
32   const gchar *typename = G_OBJECT_TYPE_NAME (action);
33
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)));
37 }
38
39 static void
40 toggle_cnp_actions (GtkAction *action)
41 {
42   gboolean sensitive;
43
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);
51
52   action = gtk_action_group_get_action (action_group, "toggle-cnp");
53   if (sensitive)
54     g_object_set (action, "label", "Disable Cut and paste ops", NULL);
55   else
56     g_object_set (action, "label", "Enable Cut and paste ops", NULL);
57 }
58
59 static void
60 show_accel_dialog (GtkAction *action)
61 {
62   g_message ("Sorry, accel dialog not available");
63 }
64
65 static void
66 toolbar_style (GtkAction *action, 
67                gpointer   user_data)
68 {
69   GtkToolbarStyle style;
70
71   g_return_if_fail (toolbar != NULL);
72   style = GPOINTER_TO_INT (user_data);
73
74   gtk_toolbar_set_style (toolbar, style);
75 }
76
77 static void
78 toolbar_size_small (GtkAction *action)
79 {
80   g_return_if_fail (toolbar != NULL);
81
82   gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
83 }
84
85 static void
86 toolbar_size_large (GtkAction *action)
87 {
88   g_return_if_fail (toolbar != NULL);
89
90   gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
91 }
92
93 /* convenience functions for declaring actions */
94 static GtkActionEntry entries[] = {
95   { "Menu1Action", NULL, "Menu _1" },
96   { "Menu2Action", NULL, "Menu _2" },
97
98   { "cut", GTK_STOCK_CUT, "C_ut", "<control>X",
99     "Cut the selected text to the clipboard", G_CALLBACK (activate_action) },
100   { "copy", GTK_STOCK_COPY, "_Copy", "<control>C",
101     "Copy the selected text to the clipboard", G_CALLBACK (activate_action) },
102   { "paste", GTK_STOCK_PASTE, "_Paste", "<control>V",
103     "Paste the text from the clipboard", G_CALLBACK (activate_action) },
104   { "quit", GTK_STOCK_QUIT,  NULL, "<control>Q",
105     "Quit the application", G_CALLBACK (gtk_main_quit) },
106   { "customise-accels", NULL, "Customise _Accels", NULL,
107     "Customise keyboard shortcuts", G_CALLBACK (show_accel_dialog) },
108   { "toolbar-small-icons", NULL, "Small Icons", NULL, 
109     NULL, G_CALLBACK (toolbar_size_small) },
110   { "toolbar-large-icons", NULL, "Large Icons", NULL,
111     NULL, G_CALLBACK (toolbar_size_large) }
112 };
113 static guint n_entries = G_N_ELEMENTS (entries);
114
115 static GtkToggleActionEntry toggle_entries[] = {
116   { "bold", GTK_STOCK_BOLD, "_Bold", "<control>B",
117     "Change to bold face", 
118     G_CALLBACK (toggle_action), FALSE },
119   { "toggle-cnp", NULL, "Enable Cut/Copy/Paste", NULL,
120     "Change the sensitivity of the cut, copy and paste actions",
121     G_CALLBACK (toggle_cnp_actions), TRUE },
122 };
123 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
124
125 enum {
126   JUSTIFY_LEFT,
127   JUSTIFY_CENTER,
128   JUSTIFY_RIGHT,
129   JUSTIFY_FILL
130 };
131
132 static GtkRadioActionEntry justify_entries[] = {
133   { "justify-left", GTK_STOCK_JUSTIFY_LEFT, "_Left", "<control>L",
134     "Left justify the text", JUSTIFY_LEFT },
135   { "justify-center", GTK_STOCK_JUSTIFY_CENTER, "C_enter", "<control>E",
136     "Center justify the text", JUSTIFY_CENTER },
137   { "justify-right", GTK_STOCK_JUSTIFY_RIGHT, "_Right", "<control>R",
138     "Right justify the text", JUSTIFY_RIGHT },
139   { "justify-fill", GTK_STOCK_JUSTIFY_FILL, "_Fill", "<control>J",
140     "Fill justify the text", JUSTIFY_FILL }
141 };
142 static guint n_justify_entries = G_N_ELEMENTS (justify_entries);
143
144 static GtkRadioActionEntry toolbar_entries[] = {
145   { "toolbar-icons", NULL, "Icons", NULL, NULL, GTK_TOOLBAR_ICONS },
146   { "toolbar-text", NULL, "Text", NULL, NULL, GTK_TOOLBAR_TEXT },
147   { "toolbar-both", NULL, "Both", NULL, NULL, GTK_TOOLBAR_BOTH },
148   { "toolbar-both-horiz", NULL, "Both Horizontal", NULL, NULL, GTK_TOOLBAR_BOTH_HORIZ }
149 };
150 static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries);
151
152 /* XML description of the menus for the test app.  The parser understands
153  * a subset of the Bonobo UI XML format, and uses GMarkup for parsing */
154 static const gchar *ui_info =
155 "  <menubar>\n"
156 "    <menu name=\"Menu _1\" action=\"Menu1Action\">\n"
157 "      <menuitem name=\"cut\" action=\"cut\" />\n"
158 "      <menuitem name=\"copy\" action=\"copy\" />\n"
159 "      <menuitem name=\"paste\" action=\"paste\" />\n"
160 "      <separator name=\"sep1\" />\n"
161 "      <menuitem name=\"bold1\" action=\"bold\" />\n"
162 "      <menuitem name=\"bold2\" action=\"bold\" />\n"
163 "      <separator name=\"sep2\" />\n"
164 "      <menuitem name=\"toggle-cnp\" action=\"toggle-cnp\" />\n"
165 "      <separator name=\"sep3\" />\n"
166 "      <menuitem name=\"quit\" action=\"quit\" />\n"
167 "    </menu>\n"
168 "    <menu name=\"Menu _2\" action=\"Menu2Action\">\n"
169 "      <menuitem name=\"cut\" action=\"cut\" />\n"
170 "      <menuitem name=\"copy\" action=\"copy\" />\n"
171 "      <menuitem name=\"paste\" action=\"paste\" />\n"
172 "      <separator name=\"sep4\"/>\n"
173 "      <menuitem name=\"bold\" action=\"bold\" />\n"
174 "      <separator name=\"sep5\"/>\n"
175 "      <menuitem name=\"justify-left\" action=\"justify-left\" />\n"
176 "      <menuitem name=\"justify-center\" action=\"justify-center\" />\n"
177 "      <menuitem name=\"justify-right\" action=\"justify-right\" />\n"
178 "      <menuitem name=\"justify-fill\" action=\"justify-fill\" />\n"
179 "      <separator name=\"sep6\"/>\n"
180 "      <menuitem  name=\"customise-accels\" action=\"customise-accels\" />\n"
181 "      <separator name=\"sep7\"/>\n"
182 "      <menuitem action=\"toolbar-icons\" />\n"
183 "      <menuitem action=\"toolbar-text\" />\n"
184 "      <menuitem action=\"toolbar-both\" />\n"
185 "      <menuitem action=\"toolbar-both-horiz\" />\n"
186 "      <separator name=\"sep8\"/>\n"
187 "      <menuitem action=\"toolbar-small-icons\" />\n"
188 "      <menuitem action=\"toolbar-large-icons\" />\n"
189 "    </menu>\n"
190 "  </menubar>\n"
191 "  <toolbar name=\"toolbar\">\n"
192 "    <toolitem name=\"cut\" action=\"cut\" />\n"
193 "    <toolitem name=\"copy\" action=\"copy\" />\n"
194 "    <toolitem name=\"paste\" action=\"paste\" />\n"
195 "    <separator name=\"sep9\" />\n"
196 "    <toolitem name=\"bold\" action=\"bold\" />\n"
197 "    <separator name=\"sep10\" />\n"
198 "    <toolitem name=\"justify-left\" action=\"justify-left\" />\n"
199 "    <toolitem name=\"justify-center\" action=\"justify-center\" />\n"
200 "    <toolitem name=\"justify-right\" action=\"justify-right\" />\n"
201 "    <toolitem name=\"justify-fill\" action=\"justify-fill\" />\n"
202 "    <separator name=\"sep11\"/>\n"
203 "    <toolitem name=\"quit\" action=\"quit\" />\n"
204 "  </toolbar>\n";
205
206 static void
207 add_widget (GtkUIManager *merge,
208             GtkWidget   *widget,
209             GtkContainer *container)
210 {
211
212   gtk_container_add (container, widget);
213   gtk_widget_show (widget);
214
215   if (GTK_IS_TOOLBAR (widget)) 
216     {
217       toolbar = GTK_TOOLBAR (widget);
218       gtk_toolbar_set_show_arrow (toolbar, TRUE);
219     }
220 }
221
222 static void
223 create_window (GtkActionGroup *action_group)
224 {
225   GtkUIManager *merge;
226   GtkWidget *window;
227   GtkWidget *box;
228   GError *error = NULL;
229
230   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
231   gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
232   gtk_window_set_title (GTK_WINDOW (window), "Action Test");
233   g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
234
235   box = gtk_vbox_new (FALSE, 0);
236   gtk_container_add (GTK_CONTAINER (window), box);
237   gtk_widget_show (box);
238
239   merge = gtk_ui_manager_new ();
240   gtk_ui_manager_insert_action_group (merge, action_group, 0);
241   g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
242
243   gtk_window_add_accel_group (GTK_WINDOW (window), 
244                               gtk_ui_manager_get_accel_group (merge));
245
246   if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
247     {
248       g_message ("building menus failed: %s", error->message);
249       g_error_free (error);
250     }
251
252   gtk_widget_show (window);
253 }
254
255 int
256 main (int argc, char **argv)
257 {
258   gtk_init (&argc, &argv);
259
260   if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
261     gtk_accel_map_load ("accels");
262
263   action_group = gtk_action_group_new ("TestActions");
264   gtk_action_group_add_actions (action_group, 
265                                 entries, n_entries, 
266                                 NULL);
267   gtk_action_group_add_toggle_actions (action_group, 
268                                        toggle_entries, n_toggle_entries, 
269                                        NULL);
270   gtk_action_group_add_radio_actions (action_group, 
271                                       justify_entries, n_justify_entries, 
272                                       JUSTIFY_LEFT,
273                                       G_CALLBACK (radio_action), NULL);
274   gtk_action_group_add_radio_actions (action_group, 
275                                       toolbar_entries, n_toolbar_entries, 
276                                       GTK_TOOLBAR_BOTH,
277                                       G_CALLBACK (radio_action), NULL);
278
279   create_window (action_group);
280
281   gtk_main ();
282
283   g_object_unref (action_group);
284
285   gtk_accel_map_save ("accels");
286
287   return 0;
288 }