]> Pileus Git - ~andy/gtk/blob - tests/testactions.c
A model-view separation for menus and toolbars, using the EggMenu code by
[~andy/gtk] / tests / testactions.c
1 #undef GTK_DISABLE_DEPRECATED
2 #include <gtk/gtk.h>
3
4 #ifndef _
5 #  define _(String) (String)
6 #  define N_(String) (String)
7 #endif
8
9 static GtkActionGroup *action_group = NULL;
10 static GtkToolbar *toolbar = NULL;
11
12 static void
13 activate_action (GtkAction *action)
14 {
15   const gchar *name = gtk_action_get_name (action);
16   const gchar *typename = G_OBJECT_TYPE_NAME (action);
17
18   g_message ("Action %s (type=%s) activated", name, typename);
19 }
20
21 static void
22 toggle_action (GtkAction *action)
23 {
24   const gchar *name = gtk_action_get_name (action);
25   const gchar *typename = G_OBJECT_TYPE_NAME (action);
26
27   g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
28              gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
29 }
30
31 static void
32 toggle_cnp_actions (GtkAction *action)
33 {
34   gboolean sensitive;
35
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);
43
44   action = gtk_action_group_get_action (action_group, "toggle-cnp");
45   if (sensitive)
46     g_object_set (action, "label", _("Disable Cut and paste ops"), NULL);
47   else
48     g_object_set (action, "label", _("Enable Cut and paste ops"), NULL);
49 }
50
51 static void
52 show_accel_dialog (GtkAction *action)
53 {
54   g_message ("Sorry, accel dialog not available");
55 }
56
57 static void
58 toolbar_style (GtkAction *action, 
59                gpointer   user_data)
60 {
61   GtkToolbarStyle style;
62
63   g_return_if_fail (toolbar != NULL);
64   style = GPOINTER_TO_INT (user_data);
65
66   gtk_toolbar_set_style (toolbar, style);
67 }
68
69 static void
70 toolbar_size (GtkAction *action, 
71               gpointer   user_data)
72 {
73   GtkIconSize size;
74
75   g_return_if_fail (toolbar != NULL);
76   size = GPOINTER_TO_INT (user_data);
77
78   gtk_toolbar_set_icon_size (toolbar, size);
79 }
80
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 },
85
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, TOGGLE_ACTION },
98
99   { "justify-left", N_("_Left"), GTK_STOCK_JUSTIFY_LEFT, "<control>L",
100     N_("Left justify the text"),
101     G_CALLBACK (toggle_action), NULL, RADIO_ACTION },
102   { "justify-center", N_("C_enter"), GTK_STOCK_JUSTIFY_CENTER, "<control>E",
103     N_("Center justify the text"),
104     G_CALLBACK (toggle_action), NULL, RADIO_ACTION, "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, RADIO_ACTION, "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, RADIO_ACTION, "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, TOGGLE_ACTION },
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     RADIO_ACTION, NULL },
123   { "toolbar-text", N_("Text"), NULL, NULL,
124     NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER (GTK_TOOLBAR_TEXT),
125     RADIO_ACTION, "toolbar-icons" },
126   { "toolbar-both", N_("Both"), NULL, NULL,
127     NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER (GTK_TOOLBAR_BOTH),
128     RADIO_ACTION, "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     RADIO_ACTION, "toolbar-icons" },
132   { "toolbar-small-icons", N_("Small Icons"), NULL, NULL,
133     NULL,
134     G_CALLBACK (toolbar_size), GINT_TO_POINTER (GTK_ICON_SIZE_SMALL_TOOLBAR) },
135   { "toolbar-large-icons", N_("Large Icons"), NULL, NULL,
136     NULL,
137     G_CALLBACK (toolbar_size), GINT_TO_POINTER (GTK_ICON_SIZE_LARGE_TOOLBAR) },
138 };
139 static guint n_entries = G_N_ELEMENTS (entries);
140
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 =
144 "<Root>\n"
145 "  <menu>\n"
146 "    <submenu name=\"Menu _1\" verb=\"Menu1Action\">\n"
147 "      <menuitem name=\"cut\" verb=\"cut\" />\n"
148 "      <menuitem name=\"copy\" verb=\"copy\" />\n"
149 "      <menuitem name=\"paste\" verb=\"paste\" />\n"
150 "      <separator name=\"sep1\" />\n"
151 "      <menuitem name=\"bold1\" verb=\"bold\" />\n"
152 "      <menuitem name=\"bold2\" verb=\"bold\" />\n"
153 "      <separator name=\"sep2\" />\n"
154 "      <menuitem name=\"toggle-cnp\" verb=\"toggle-cnp\" />\n"
155 "      <separator name=\"sep3\" />\n"
156 "      <menuitem name=\"quit\" verb=\"quit\" />\n"
157 "    </submenu>\n"
158 "    <submenu name=\"Menu _2\" verb=\"Menu2Action\">\n"
159 "      <menuitem name=\"cut\" verb=\"cut\" />\n"
160 "      <menuitem name=\"copy\" verb=\"copy\" />\n"
161 "      <menuitem name=\"paste\" verb=\"paste\" />\n"
162 "      <separator name=\"sep4\"/>\n"
163 "      <menuitem name=\"bold\" verb=\"bold\" />\n"
164 "      <separator name=\"sep5\"/>\n"
165 "      <menuitem name=\"justify-left\" verb=\"justify-left\" />\n"
166 "      <menuitem name=\"justify-center\" verb=\"justify-center\" />\n"
167 "      <menuitem name=\"justify-right\" verb=\"justify-right\" />\n"
168 "      <menuitem name=\"justify-fill\" verb=\"justify-fill\" />\n"
169 "      <separator name=\"sep6\"/>\n"
170 "      <menuitem  name=\"customise-accels\" verb=\"customise-accels\" />\n"
171 "      <separator name=\"sep7\"/>\n"
172 "      <menuitem verb=\"toolbar-icons\" />\n"
173 "      <menuitem verb=\"toolbar-text\" />\n"
174 "      <menuitem verb=\"toolbar-both\" />\n"
175 "      <menuitem verb=\"toolbar-both-horiz\" />\n"
176 "      <separator name=\"sep8\"/>\n"
177 "      <menuitem verb=\"toolbar-small-icons\" />\n"
178 "      <menuitem verb=\"toolbar-large-icons\" />\n"
179 "    </submenu>\n"
180 "  </menu>\n"
181 "  <dockitem name=\"toolbar\">\n"
182 "    <toolitem name=\"cut\" verb=\"cut\" />\n"
183 "    <toolitem name=\"copy\" verb=\"copy\" />\n"
184 "    <toolitem name=\"paste\" verb=\"paste\" />\n"
185 "    <separator name=\"sep9\" />\n"
186 "    <toolitem name=\"bold\" verb=\"bold\" />\n"
187 "    <separator name=\"sep10\" />\n"
188 "    <toolitem name=\"justify-left\" verb=\"justify-left\" />\n"
189 "    <toolitem name=\"justify-center\" verb=\"justify-center\" />\n"
190 "    <toolitem name=\"justify-right\" verb=\"justify-right\" />\n"
191 "    <toolitem name=\"justify-fill\" verb=\"justify-fill\" />\n"
192 "    <separator name=\"sep11\"/>\n"
193 "    <toolitem name=\"quit\" verb=\"quit\" />\n"
194 "  </dockitem>\n"
195 "</Root>\n";
196
197 static void
198 add_widget (GtkMenuMerge *merge,
199             GtkWidget   *widget,
200             GtkContainer *container)
201 {
202
203   gtk_container_add (container, widget);
204   gtk_widget_show (widget);
205
206   if (GTK_IS_TOOLBAR (widget)) 
207     {
208       toolbar = GTK_TOOLBAR (widget);
209       gtk_toolbar_set_show_arrow (toolbar, TRUE);
210     }
211 }
212
213 static void
214 create_window (GtkActionGroup *action_group)
215 {
216   GtkMenuMerge *merge;
217   GtkWidget *window;
218   GtkWidget *box;
219   GError *error = NULL;
220
221   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
222   gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
223   gtk_window_set_title (GTK_WINDOW (window), "Action Test");
224   g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
225
226   box = gtk_vbox_new (FALSE, 0);
227   gtk_container_add (GTK_CONTAINER (window), box);
228   gtk_widget_show (box);
229
230   merge = gtk_menu_merge_new ();
231   gtk_menu_merge_insert_action_group (merge, action_group, 0);
232   g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
233
234   gtk_window_add_accel_group (GTK_WINDOW (window), 
235                               gtk_menu_merge_get_accel_group (merge));
236
237   if (!gtk_menu_merge_add_ui_from_string (merge, ui_info, -1, &error))
238     {
239       g_message ("building menus failed: %s", error->message);
240       g_error_free (error);
241     }
242
243   gtk_widget_show (window);
244 }
245
246 int
247 main (int argc, char **argv)
248 {
249   gtk_init (&argc, &argv);
250
251   if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
252     gtk_accel_map_load ("accels");
253
254   action_group = gtk_action_group_new ("TestActions");
255   gtk_action_group_add_actions (action_group, entries, n_entries);
256
257   gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (gtk_action_group_get_action (action_group, "toggle-cnp")), TRUE);
258
259   create_window (action_group);
260
261   gtk_main ();
262
263   g_object_unref (action_group);
264
265   gtk_accel_map_save ("accels");
266
267   return 0;
268 }