]> Pileus Git - ~andy/gtk/blob - tests/testactions.c
Add value parameter to allow setting the currently selected group member
[~andy/gtk] / tests / testactions.c
1 #undef GTK_DISABLE_DEPRECATED
2 #include <gtk/gtk.h>
3
4 static GtkActionGroup *action_group = NULL;
5 static GtkToolbar *toolbar = NULL;
6
7 static void
8 activate_action (GtkAction *action)
9 {
10   const gchar *name = gtk_action_get_name (action);
11   const gchar *typename = G_OBJECT_TYPE_NAME (action);
12
13   g_message ("Action %s (type=%s) activated", name, typename);
14 }
15
16 static void
17 toggle_action (GtkAction *action)
18 {
19   const gchar *name = gtk_action_get_name (action);
20   const gchar *typename = G_OBJECT_TYPE_NAME (action);
21
22   g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
23              gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
24 }
25
26
27 static void
28 radio_action (GtkAction *action)
29 {
30   const gchar *name = gtk_action_get_name (action);
31   const gchar *typename = G_OBJECT_TYPE_NAME (action);
32
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)));
36 }
37
38 static void
39 toggle_cnp_actions (GtkAction *action)
40 {
41   gboolean sensitive;
42
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);
50
51   action = gtk_action_group_get_action (action_group, "toggle-cnp");
52   if (sensitive)
53     g_object_set (action, "label", "Disable Cut and paste ops", NULL);
54   else
55     g_object_set (action, "label", "Enable Cut and paste ops", NULL);
56 }
57
58 static void
59 show_accel_dialog (GtkAction *action)
60 {
61   g_message ("Sorry, accel dialog not available");
62 }
63
64 static void
65 toolbar_style (GtkAction *action, 
66                gpointer   user_data)
67 {
68   GtkToolbarStyle style;
69
70   g_return_if_fail (toolbar != NULL);
71   style = GPOINTER_TO_INT (user_data);
72
73   gtk_toolbar_set_style (toolbar, style);
74 }
75
76 static void
77 toolbar_size_small (GtkAction *action)
78 {
79   g_return_if_fail (toolbar != NULL);
80
81   gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
82 }
83
84 static void
85 toolbar_size_large (GtkAction *action)
86 {
87   g_return_if_fail (toolbar != NULL);
88
89   gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
90 }
91
92 /* convenience functions for declaring actions */
93 static GtkActionEntry entries[] = {
94   { "Menu1Action", NULL, "Menu _1" },
95   { "Menu2Action", NULL, "Menu _2" },
96
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) }
111 };
112 static guint n_entries = G_N_ELEMENTS (entries);
113
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 },
121 };
122 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
123
124 enum {
125   JUSTIFY_LEFT,
126   JUSTIFY_CENTER,
127   JUSTIFY_RIGHT,
128   JUSTIFY_FILL
129 };
130
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 }
140 };
141 static guint n_justify_entries = G_N_ELEMENTS (justify_entries);
142
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 }
148 };
149 static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries);
150
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 =
154 "  <menubar>\n"
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"
166 "    </menu>\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"
188 "    </menu>\n"
189 "  </menubar>\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"
203 "  </toolbar>\n";
204
205 static void
206 add_widget (GtkUIManager *merge,
207             GtkWidget   *widget,
208             GtkContainer *container)
209 {
210
211   gtk_container_add (container, widget);
212   gtk_widget_show (widget);
213
214   if (GTK_IS_TOOLBAR (widget)) 
215     {
216       toolbar = GTK_TOOLBAR (widget);
217       gtk_toolbar_set_show_arrow (toolbar, TRUE);
218     }
219 }
220
221 static void
222 create_window (GtkActionGroup *action_group)
223 {
224   GtkUIManager *merge;
225   GtkWidget *window;
226   GtkWidget *box;
227   GError *error = NULL;
228
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);
233
234   box = gtk_vbox_new (FALSE, 0);
235   gtk_container_add (GTK_CONTAINER (window), box);
236   gtk_widget_show (box);
237
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);
241
242   gtk_window_add_accel_group (GTK_WINDOW (window), 
243                               gtk_ui_manager_get_accel_group (merge));
244
245   if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
246     {
247       g_message ("building menus failed: %s", error->message);
248       g_error_free (error);
249     }
250
251   gtk_widget_show (window);
252 }
253
254 int
255 main (int argc, char **argv)
256 {
257   gtk_init (&argc, &argv);
258
259   if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
260     gtk_accel_map_load ("accels");
261
262   action_group = gtk_action_group_new ("TestActions");
263   gtk_action_group_add_actions (action_group, 
264                                 entries, n_entries, 
265                                 NULL);
266   gtk_action_group_add_toggle_actions (action_group, 
267                                        toggle_entries, n_toggle_entries, 
268                                        NULL);
269   gtk_action_group_add_radio_actions (action_group, 
270                                       justify_entries, n_justify_entries, 
271                                       JUSTIFY_LEFT,
272                                       G_CALLBACK (radio_action), NULL);
273   gtk_action_group_add_radio_actions (action_group, 
274                                       toolbar_entries, n_toolbar_entries, 
275                                       GTK_TOOLBAR_BOTH,
276                                       G_CALLBACK (radio_action), NULL);
277
278   create_window (action_group);
279
280   gtk_main ();
281
282   g_object_unref (action_group);
283
284   gtk_accel_map_save ("accels");
285
286   return 0;
287 }