]> Pileus Git - ~andy/gtk/blob - tests/testactions.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testactions.c
1 /* testactions.c
2  * Copyright (C) 2003  Matthias Clasen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19 #include <gtk/gtk.h>
20
21 static GtkActionGroup *action_group = NULL;
22 static GtkToolbar *toolbar = NULL;
23
24 static void
25 activate_action (GtkAction *action)
26 {
27   const gchar *name = gtk_action_get_name (action);
28   const gchar *typename = G_OBJECT_TYPE_NAME (action);
29
30   g_message ("Action %s (type=%s) activated", name, typename);
31 }
32
33 static void
34 toggle_action (GtkAction *action)
35 {
36   const gchar *name = gtk_action_get_name (action);
37   const gchar *typename = G_OBJECT_TYPE_NAME (action);
38
39   g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
40              gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
41 }
42
43
44 static void
45 radio_action (GtkAction *action)
46 {
47   const gchar *name = gtk_action_get_name (action);
48   const gchar *typename = G_OBJECT_TYPE_NAME (action);
49
50   g_message ("Action %s (type=%s) activated (active=%d) (value %d)", name, typename,
51              gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)),
52              gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action)));
53 }
54
55 static void
56 recent_action (GtkAction *action)
57 {
58   const gchar *name = gtk_action_get_name (action);
59   const gchar *typename = G_OBJECT_TYPE_NAME (action);
60   gchar *uri = gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (action));
61
62   g_message ("Action %s (type=%s) activated (uri=%s)",
63              name, typename,
64              uri ? uri : "no item selected");
65   g_free (uri);
66 }
67
68 static void
69 toggle_cnp_actions (GtkAction *action)
70 {
71   gboolean sensitive;
72
73   sensitive = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
74   action = gtk_action_group_get_action (action_group, "cut");
75   g_object_set (action, "sensitive", sensitive, NULL);
76   action = gtk_action_group_get_action (action_group, "copy");
77   g_object_set (action, "sensitive", sensitive, NULL);
78   action = gtk_action_group_get_action (action_group, "paste");
79   g_object_set (action, "sensitive", sensitive, NULL);
80
81   action = gtk_action_group_get_action (action_group, "toggle-cnp");
82   if (sensitive)
83     g_object_set (action, "label", "Disable Cut and paste ops", NULL);
84   else
85     g_object_set (action, "label", "Enable Cut and paste ops", NULL);
86 }
87
88 static void
89 show_accel_dialog (GtkAction *action)
90 {
91   g_message ("Sorry, accel dialog not available");
92 }
93
94 static void
95 toolbar_style (GtkAction *action)
96 {
97   GtkToolbarStyle style;
98
99   g_return_if_fail (toolbar != NULL);
100
101   radio_action (action);
102
103   style = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action));
104
105   gtk_toolbar_set_style (toolbar, style);
106 }
107
108 static void
109 toolbar_size_small (GtkAction *action)
110 {
111   g_return_if_fail (toolbar != NULL);
112
113   gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR);
114 }
115
116 static void
117 toolbar_size_large (GtkAction *action)
118 {
119   g_return_if_fail (toolbar != NULL);
120
121   gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
122 }
123
124 /* convenience functions for declaring actions */
125 static GtkActionEntry entries[] = {
126   { "Menu1Action", NULL, "Menu _1" },
127   { "Menu2Action", NULL, "Menu _2" },
128   { "Menu3Action", NULL, "_Dynamic Menu" },
129
130   { "attach", "mail-attachment", "_Attachment...", "<Control>m",
131     "Attach a file", G_CALLBACK (activate_action) },
132   { "cut", GTK_STOCK_CUT, "C_ut", "<control>X",
133     "Cut the selected text to the clipboard", G_CALLBACK (activate_action) },
134   { "copy", GTK_STOCK_COPY, "_Copy", "<control>C",
135     "Copy the selected text to the clipboard", G_CALLBACK (activate_action) },
136   { "paste", GTK_STOCK_PASTE, "_Paste", "<control>V",
137     "Paste the text from the clipboard", G_CALLBACK (activate_action) },
138   { "quit", GTK_STOCK_QUIT,  NULL, "<control>Q",
139     "Quit the application", G_CALLBACK (gtk_main_quit) },
140   { "customise-accels", NULL, "Customise _Accels", NULL,
141     "Customise keyboard shortcuts", G_CALLBACK (show_accel_dialog) },
142   { "toolbar-small-icons", NULL, "Small Icons", NULL, 
143     NULL, G_CALLBACK (toolbar_size_small) },
144   { "toolbar-large-icons", NULL, "Large Icons", NULL,
145     NULL, G_CALLBACK (toolbar_size_large) }
146 };
147 static guint n_entries = G_N_ELEMENTS (entries);
148
149 static GtkToggleActionEntry toggle_entries[] = {
150   { "bold", GTK_STOCK_BOLD, "_Bold", "<control>B",
151     "Change to bold face", 
152     G_CALLBACK (toggle_action), FALSE },
153   { "toggle-cnp", NULL, "Enable Cut/Copy/Paste", NULL,
154     "Change the sensitivity of the cut, copy and paste actions",
155     G_CALLBACK (toggle_cnp_actions), TRUE },
156 };
157 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
158
159 enum {
160   JUSTIFY_LEFT,
161   JUSTIFY_CENTER,
162   JUSTIFY_RIGHT,
163   JUSTIFY_FILL
164 };
165
166 static GtkRadioActionEntry justify_entries[] = {
167   { "justify-left", GTK_STOCK_JUSTIFY_LEFT, "_Left", "<control>L",
168     "Left justify the text", JUSTIFY_LEFT },
169   { "justify-center", GTK_STOCK_JUSTIFY_CENTER, "C_enter", "<control>E",
170     "Center justify the text", JUSTIFY_CENTER },
171   { "justify-right", GTK_STOCK_JUSTIFY_RIGHT, "_Right", "<control>R",
172     "Right justify the text", JUSTIFY_RIGHT },
173   { "justify-fill", GTK_STOCK_JUSTIFY_FILL, "_Fill", "<control>J",
174     "Fill justify the text", JUSTIFY_FILL }
175 };
176 static guint n_justify_entries = G_N_ELEMENTS (justify_entries);
177
178 static GtkRadioActionEntry toolbar_entries[] = {
179   { "toolbar-icons", NULL, "Icons", NULL, NULL, GTK_TOOLBAR_ICONS },
180   { "toolbar-text", NULL, "Text", NULL, NULL, GTK_TOOLBAR_TEXT },
181   { "toolbar-both", NULL, "Both", NULL, NULL, GTK_TOOLBAR_BOTH },
182   { "toolbar-both-horiz", NULL, "Both Horizontal", NULL, NULL, GTK_TOOLBAR_BOTH_HORIZ }
183 };
184 static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries);
185
186 /* XML description of the menus for the test app.  The parser understands
187  * a subset of the Bonobo UI XML format, and uses GMarkup for parsing */
188 static const gchar *ui_info =
189 "  <menubar>\n"
190 "    <menu name=\"Menu _1\" action=\"Menu1Action\">\n"
191 "      <menuitem name=\"cut\" action=\"cut\" />\n"
192 "      <menuitem name=\"copy\" action=\"copy\" />\n"
193 "      <menuitem name=\"paste\" action=\"paste\" />\n"
194 "      <separator name=\"sep1\" />\n"
195 "      <menuitem name=\"bold1\" action=\"bold\" />\n"
196 "      <menuitem name=\"bold2\" action=\"bold\" />\n"
197 "      <separator name=\"sep2\" />\n"
198 "      <menuitem name=\"recent\" action=\"recent\" />\n"
199 "      <separator name=\"sep3\" />\n"
200 "      <menuitem name=\"toggle-cnp\" action=\"toggle-cnp\" />\n"
201 "      <separator name=\"sep4\" />\n"
202 "      <menuitem name=\"quit\" action=\"quit\" />\n"
203 "    </menu>\n"
204 "    <menu name=\"Menu _2\" action=\"Menu2Action\">\n"
205 "      <menuitem name=\"cut\" action=\"cut\" />\n"
206 "      <menuitem name=\"copy\" action=\"copy\" />\n"
207 "      <menuitem name=\"paste\" action=\"paste\" />\n"
208 "      <separator name=\"sep5\"/>\n"
209 "      <menuitem name=\"bold\" action=\"bold\" />\n"
210 "      <separator name=\"sep6\"/>\n"
211 "      <menuitem name=\"justify-left\" action=\"justify-left\" />\n"
212 "      <menuitem name=\"justify-center\" action=\"justify-center\" />\n"
213 "      <menuitem name=\"justify-right\" action=\"justify-right\" />\n"
214 "      <menuitem name=\"justify-fill\" action=\"justify-fill\" />\n"
215 "      <separator name=\"sep7\"/>\n"
216 "      <menuitem  name=\"customise-accels\" action=\"customise-accels\" />\n"
217 "      <separator name=\"sep8\"/>\n"
218 "      <menuitem action=\"toolbar-icons\" />\n"
219 "      <menuitem action=\"toolbar-text\" />\n"
220 "      <menuitem action=\"toolbar-both\" />\n"
221 "      <menuitem action=\"toolbar-both-horiz\" />\n"
222 "      <separator name=\"sep9\"/>\n"
223 "      <menuitem action=\"toolbar-small-icons\" />\n"
224 "      <menuitem action=\"toolbar-large-icons\" />\n"
225 "    </menu>\n"
226 "    <menu name=\"DynamicMenu\" action=\"Menu3Action\" />\n"
227 "  </menubar>\n"
228 "  <toolbar name=\"toolbar\">\n"
229 "    <toolitem name=\"attach\" action=\"attach\" />\n"
230 "    <toolitem name=\"cut\" action=\"cut\" />\n"
231 "    <toolitem name=\"copy\" action=\"copy\" />\n"
232 "    <toolitem name=\"paste\" action=\"paste\" />\n"
233 "    <toolitem name=\"recent\" action=\"recent\" />\n"
234 "    <separator name=\"sep10\" />\n"
235 "    <toolitem name=\"bold\" action=\"bold\" />\n"
236 "    <separator name=\"sep11\" />\n"
237 "    <toolitem name=\"justify-left\" action=\"justify-left\" />\n"
238 "    <toolitem name=\"justify-center\" action=\"justify-center\" />\n"
239 "    <toolitem name=\"justify-right\" action=\"justify-right\" />\n"
240 "    <toolitem name=\"justify-fill\" action=\"justify-fill\" />\n"
241 "    <separator name=\"sep12\"/>\n"
242 "    <toolitem name=\"quit\" action=\"quit\" />\n"
243 "  </toolbar>\n"
244 "  <popup name=\"popup\">\n"
245 "    <menuitem name=\"popcut\" action=\"cut\" />\n"
246 "    <menuitem name=\"popcopy\" action=\"copy\" />\n"
247 "    <menuitem name=\"poppaste\" action=\"paste\" />\n"
248 "  </popup>\n";
249
250 static void
251 add_widget (GtkUIManager *merge,
252             GtkWidget   *widget,
253             GtkContainer *container)
254 {
255
256   gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
257   gtk_widget_show (widget);
258
259   if (GTK_IS_TOOLBAR (widget)) 
260     {
261       toolbar = GTK_TOOLBAR (widget);
262       gtk_toolbar_set_show_arrow (toolbar, TRUE);
263     }
264 }
265
266 static guint ui_id = 0;
267 static GtkActionGroup *dag = NULL;
268
269 static void
270 ensure_update (GtkUIManager *manager)
271 {
272   GTimer *timer;
273   double seconds;
274   gulong microsecs;
275   
276   timer = g_timer_new ();
277   g_timer_start (timer);
278   
279   gtk_ui_manager_ensure_update (manager);
280   
281   g_timer_stop (timer);
282   seconds = g_timer_elapsed (timer, &microsecs);
283   g_timer_destroy (timer);
284   
285   g_print ("Time: %fs\n", seconds);
286 }
287
288 static void
289 add_cb (GtkWidget *button,
290         GtkUIManager *manager)
291 {
292   GtkWidget *spinbutton;
293   GtkAction *action;
294   int i, num;
295   char *name, *label;
296   
297   if (ui_id != 0 || dag != NULL)
298     return;
299   
300   spinbutton = g_object_get_data (G_OBJECT (button), "spinbutton");
301   num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spinbutton));
302   
303   dag = gtk_action_group_new ("DynamicActions");
304   gtk_ui_manager_insert_action_group (manager, dag, 0);
305   
306   ui_id = gtk_ui_manager_new_merge_id (manager);
307   
308   for (i = 0; i < num; i++)
309     {
310       name = g_strdup_printf ("DynAction%u", i);
311       label = g_strdup_printf ("Dynamic Item %d", i);
312       
313       action = g_object_new (GTK_TYPE_ACTION,
314                              "name", name,
315                              "label", label,
316                              NULL);
317       gtk_action_group_add_action (dag, action);
318       g_object_unref (action);
319       
320       gtk_ui_manager_add_ui (manager, ui_id, "/menubar/DynamicMenu",
321                              name, name,
322                              GTK_UI_MANAGER_MENUITEM, FALSE);
323     }
324   
325   ensure_update (manager);
326 }
327
328 static void
329 remove_cb (GtkWidget *button,
330            GtkUIManager *manager)
331 {
332   if (ui_id == 0 || dag == NULL)
333     return;
334   
335   gtk_ui_manager_remove_ui (manager, ui_id);
336   ensure_update (manager);
337   ui_id = 0;
338   
339   gtk_ui_manager_remove_action_group (manager, dag);
340   g_object_unref (dag);
341   dag = NULL;
342 }
343
344 static void
345 create_window (GtkActionGroup *action_group)
346 {
347   GtkUIManager *merge;
348   GtkWidget *window;
349   GtkWidget *box;
350   GtkWidget *hbox, *spinbutton, *button;
351   GError *error = NULL;
352
353   merge = gtk_ui_manager_new ();
354
355   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
356   gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
357   gtk_window_set_title (GTK_WINDOW (window), "Action Test");
358   g_signal_connect_swapped (window, "destroy", G_CALLBACK (g_object_unref), merge);
359   g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
360
361   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
362   gtk_container_add (GTK_CONTAINER (window), box);
363   gtk_widget_show (box);
364
365   gtk_ui_manager_insert_action_group (merge, action_group, 0);
366   g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
367
368   gtk_window_add_accel_group (GTK_WINDOW (window), 
369                               gtk_ui_manager_get_accel_group (merge));
370
371   if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
372     {
373       g_message ("building menus failed: %s", error->message);
374       g_error_free (error);
375     }
376
377   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
378   gtk_box_pack_end (GTK_BOX (box), hbox, FALSE, FALSE, 0);
379   gtk_widget_show (hbox);
380   
381   spinbutton = gtk_spin_button_new_with_range (100, 10000, 100);
382   gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
383   gtk_widget_show (spinbutton);
384   
385   button = gtk_button_new_with_label ("Add");
386   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
387   gtk_widget_show (button);
388   
389   g_object_set_data (G_OBJECT (button), "spinbutton", spinbutton);
390   g_signal_connect (button, "clicked", G_CALLBACK (add_cb), merge);
391   
392   button = gtk_button_new_with_label ("Remove");
393   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
394   gtk_widget_show (button);
395   
396   g_signal_connect (button, "clicked", G_CALLBACK (remove_cb), merge);
397   
398   gtk_widget_show (window);
399 }
400
401 int
402 main (int argc, char **argv)
403 {
404   GtkAction *action;
405
406   gtk_init (&argc, &argv);
407
408   if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
409     gtk_accel_map_load ("accels");
410
411   action = gtk_recent_action_new ("recent",
412                                   "Open Recent", "Open recent files",
413                                   NULL);
414   g_signal_connect (action, "item-activated",
415                     G_CALLBACK (recent_action),
416                     NULL);
417   g_signal_connect (action, "activate",
418                     G_CALLBACK (recent_action),
419                     NULL);
420
421   action_group = gtk_action_group_new ("TestActions");
422   gtk_action_group_add_actions (action_group, 
423                                 entries, n_entries, 
424                                 NULL);
425   gtk_action_group_add_toggle_actions (action_group, 
426                                        toggle_entries, n_toggle_entries, 
427                                        NULL);
428   gtk_action_group_add_radio_actions (action_group, 
429                                       justify_entries, n_justify_entries, 
430                                       JUSTIFY_LEFT,
431                                       G_CALLBACK (radio_action), NULL);
432   gtk_action_group_add_radio_actions (action_group, 
433                                       toolbar_entries, n_toolbar_entries, 
434                                       GTK_TOOLBAR_BOTH,
435                                       G_CALLBACK (toolbar_style), NULL);
436   gtk_action_group_add_action_with_accel (action_group, action, NULL);
437
438   create_window (action_group);
439
440   gtk_main ();
441
442 #ifdef DEBUG_UI_MANAGER
443   {
444     GList *action;
445
446     for (action = gtk_action_group_list_actions (action_group);
447          action; 
448          action = action->next)
449       {
450         GtkAction *a = action->data;
451         g_print ("action %s ref count %d\n", 
452                  gtk_action_get_name (a), G_OBJECT (a)->ref_count);
453       }
454   }
455 #endif
456   
457   g_object_unref (action);
458   g_object_unref (action_group);
459
460   gtk_accel_map_save ("accels");
461
462   return 0;
463 }