]> Pileus Git - ~andy/gtk/blob - tests/testactions.c
Use gssize for length, not gsize.
[~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, GTK_ACTION_TOGGLE },
98
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,
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 "  <menubar>\n"
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"
156 "    </menu>\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"
178 "    </menu>\n"
179 "  </menubar>\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"
193 "  </toolbar>\n";
194
195 static void
196 add_widget (GtkUIManager *merge,
197             GtkWidget   *widget,
198             GtkContainer *container)
199 {
200
201   gtk_container_add (container, widget);
202   gtk_widget_show (widget);
203
204   if (GTK_IS_TOOLBAR (widget)) 
205     {
206       toolbar = GTK_TOOLBAR (widget);
207       gtk_toolbar_set_show_arrow (toolbar, TRUE);
208     }
209 }
210
211 static void
212 create_window (GtkActionGroup *action_group)
213 {
214   GtkUIManager *merge;
215   GtkWidget *window;
216   GtkWidget *box;
217   GError *error = NULL;
218
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);
223
224   box = gtk_vbox_new (FALSE, 0);
225   gtk_container_add (GTK_CONTAINER (window), box);
226   gtk_widget_show (box);
227
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);
231
232   gtk_window_add_accel_group (GTK_WINDOW (window), 
233                               gtk_ui_manager_get_accel_group (merge));
234
235   if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
236     {
237       g_message ("building menus failed: %s", error->message);
238       g_error_free (error);
239     }
240
241   gtk_widget_show (window);
242 }
243
244 int
245 main (int argc, char **argv)
246 {
247   gtk_init (&argc, &argv);
248
249   if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
250     gtk_accel_map_load ("accels");
251
252   action_group = gtk_action_group_new ("TestActions");
253   gtk_action_group_add_actions (action_group, entries, n_entries);
254
255   gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (gtk_action_group_get_action (action_group, "toggle-cnp")), TRUE);
256
257   create_window (action_group);
258
259   gtk_main ();
260
261   g_object_unref (action_group);
262
263   gtk_accel_map_save ("accels");
264
265   return 0;
266 }