]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/appwindow.c
gtk-demo: Move resources into the tests they belong
[~andy/gtk] / demos / gtk-demo / appwindow.c
1 /* Application window
2  *
3  * Demonstrates a typical application window with menubar, toolbar, statusbar.
4  *
5  * This example uses GtkUIManager and GtkActionGroup.
6  */
7
8 #include <gtk/gtk.h>
9 #include "config.h"
10
11 static GtkWidget *window = NULL;
12 static GtkWidget *infobar = NULL;
13 static GtkWidget *messagelabel = NULL;
14
15 static void
16 activate_action (GtkAction *action)
17 {
18   const gchar *name = gtk_action_get_name (action);
19   const gchar *typename = G_OBJECT_TYPE_NAME (action);
20
21   GtkWidget *dialog;
22
23   if (g_str_equal (name, "DarkTheme"))
24     {
25       gboolean value = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
26       GtkSettings *settings = gtk_settings_get_default ();
27
28       g_object_set (G_OBJECT (settings),
29                     "gtk-application-prefer-dark-theme", value,
30                     NULL);
31       return;
32     }
33
34   if (g_str_equal (name, "HideTitlebar"))
35     {
36       gboolean value = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
37       gtk_window_set_hide_titlebar_when_maximized (GTK_WINDOW (window), value);
38       return;
39     }
40
41   dialog = gtk_message_dialog_new (GTK_WINDOW (window),
42                                    GTK_DIALOG_DESTROY_WITH_PARENT,
43                                    GTK_MESSAGE_INFO,
44                                    GTK_BUTTONS_CLOSE,
45                                    "You activated action: \"%s\" of type \"%s\"",
46                                     name, typename);
47
48   /* Close dialog on user response */
49   g_signal_connect (dialog,
50                     "response",
51                     G_CALLBACK (gtk_widget_destroy),
52                     NULL);
53
54   gtk_widget_show (dialog);
55 }
56
57 static void
58 activate_radio_action (GtkAction *action, GtkRadioAction *current)
59 {
60   const gchar *name = gtk_action_get_name (GTK_ACTION (current));
61   const gchar *typename = G_OBJECT_TYPE_NAME (GTK_ACTION (current));
62   gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (current));
63   gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
64
65   if (active)
66     {
67       gchar *text;
68
69       text = g_strdup_printf ("You activated radio action: \"%s\" of type \"%s\".\n"
70                               "Current value: %d",
71                               name, typename, value);
72       gtk_label_set_text (GTK_LABEL (messagelabel), text);
73       gtk_info_bar_set_message_type (GTK_INFO_BAR (infobar), (GtkMessageType)value);
74       gtk_widget_show (infobar);
75       g_free (text);
76     }
77 }
78
79 static void
80 about_cb (GtkAction *action,
81           GtkWidget *window)
82 {
83   GdkPixbuf *pixbuf, *transparent;
84
85   const gchar *authors[] = {
86     "Peter Mattis",
87     "Spencer Kimball",
88     "Josh MacDonald",
89     "and many more...",
90     NULL
91   };
92
93   const gchar *documentors[] = {
94     "Owen Taylor",
95     "Tony Gale",
96     "Matthias Clasen <mclasen@redhat.com>",
97     "and many more...",
98     NULL
99   };
100
101   pixbuf = gdk_pixbuf_new_from_resource ("/appwindow/gtk-logo-rgb.gif", NULL);
102   /* We asser the existence of the pixbuf as we load it from a custom resource. */
103   g_assert (pixbuf);
104   transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
105   g_object_unref (pixbuf);
106
107   gtk_show_about_dialog (GTK_WINDOW (window),
108                          "program-name", "GTK+ Code Demos",
109                          "version", g_strdup_printf ("%s,\nRunning against GTK+ %d.%d.%d",
110                                                      PACKAGE_VERSION,
111                                                      gtk_get_major_version (),
112                                                      gtk_get_minor_version (),
113                                                      gtk_get_micro_version ()),
114                          "copyright", "(C) 1997-2009 The GTK+ Team",
115                          "license-type", GTK_LICENSE_LGPL_2_1,
116                          "website", "http://www.gtk.org",
117                          "comments", "Program to demonstrate GTK+ functions.",
118                          "authors", authors,
119                          "documenters", documentors,
120                          "logo", transparent,
121                          "title", "About GTK+ Code Demos",
122                          NULL);
123
124   g_object_unref (transparent);
125 }
126
127 typedef struct
128 {
129   GtkAction action;
130 } ToolMenuAction;
131
132 typedef struct
133 {
134   GtkActionClass parent_class;
135 } ToolMenuActionClass;
136
137 G_DEFINE_TYPE(ToolMenuAction, tool_menu_action, GTK_TYPE_ACTION)
138
139 static void
140 tool_menu_action_class_init (ToolMenuActionClass *class)
141 {
142   GTK_ACTION_CLASS (class)->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
143 }
144
145 static void
146 tool_menu_action_init (ToolMenuAction *action)
147 {
148 }
149
150 static GtkActionEntry entries[] = {
151   { "FileMenu", NULL, "_File" },               /* name, stock id, label */
152   { "OpenMenu", NULL, "_Open" },               /* name, stock id, label */
153   { "PreferencesMenu", NULL, "_Preferences" }, /* name, stock id, label */
154   { "ColorMenu", NULL, "_Color"  },            /* name, stock id, label */
155   { "ShapeMenu", NULL, "_Shape" },             /* name, stock id, label */
156   { "HelpMenu", NULL, "_Help" },               /* name, stock id, label */
157   { "New", GTK_STOCK_NEW,                      /* name, stock id */
158     "_New", "<control>N",                      /* label, accelerator */
159     "Create a new file",                       /* tooltip */
160     G_CALLBACK (activate_action) },
161   { "File1", NULL,                             /* name, stock id */
162     "File1", NULL,                             /* label, accelerator */
163     "Open first file",                         /* tooltip */
164     G_CALLBACK (activate_action) },
165   { "Save", GTK_STOCK_SAVE,                    /* name, stock id */
166     "_Save","<control>S",                      /* label, accelerator */
167     "Save current file",                       /* tooltip */
168     G_CALLBACK (activate_action) },
169   { "SaveAs", GTK_STOCK_SAVE,                  /* name, stock id */
170     "Save _As...", NULL,                       /* label, accelerator */
171     "Save to a file",                          /* tooltip */
172     G_CALLBACK (activate_action) },
173   { "Quit", GTK_STOCK_QUIT,                    /* name, stock id */
174     "_Quit", "<control>Q",                     /* label, accelerator */
175     "Quit",                                    /* tooltip */
176     G_CALLBACK (activate_action) },
177   { "About", NULL,                             /* name, stock id */
178     "_About", "<control>A",                    /* label, accelerator */
179     "About",                                   /* tooltip */
180     G_CALLBACK (about_cb) },
181   { "Logo", "demo-gtk-logo",                   /* name, stock id */
182      NULL, NULL,                               /* label, accelerator */
183     "GTK+",                                    /* tooltip */
184     G_CALLBACK (activate_action) },
185 };
186 static guint n_entries = G_N_ELEMENTS (entries);
187
188
189 static GtkToggleActionEntry toggle_entries[] = {
190   { "Bold", GTK_STOCK_BOLD,                    /* name, stock id */
191      "_Bold", "<control>B",                    /* label, accelerator */
192     "Bold",                                    /* tooltip */
193     G_CALLBACK (activate_action),
194     TRUE },                                    /* is_active */
195   { "DarkTheme", NULL,                         /* name, stock id */
196      "_Prefer Dark Theme", NULL,               /* label, accelerator */
197     "Prefer Dark Theme",                       /* tooltip */
198     G_CALLBACK (activate_action),
199     FALSE },                                   /* is_active */
200   { "HideTitlebar", NULL,
201     "_Hide Titlebar when maximized", NULL,
202     "Hide Titlebar when maximized",
203     G_CALLBACK (activate_action),
204     FALSE }
205 };
206 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
207
208 enum {
209   COLOR_RED,
210   COLOR_GREEN,
211   COLOR_BLUE
212 };
213
214 static GtkRadioActionEntry color_entries[] = {
215   { "Red", NULL,                               /* name, stock id */
216     "_Red", "<control>R",                      /* label, accelerator */
217     "Blood", COLOR_RED },                      /* tooltip, value */
218   { "Green", NULL,                             /* name, stock id */
219     "_Green", "<control>G",                    /* label, accelerator */
220     "Grass", COLOR_GREEN },                    /* tooltip, value */
221   { "Blue", NULL,                              /* name, stock id */
222     "_Blue", "<control>B",                     /* label, accelerator */
223     "Sky", COLOR_BLUE },                       /* tooltip, value */
224 };
225 static guint n_color_entries = G_N_ELEMENTS (color_entries);
226
227 enum {
228   SHAPE_SQUARE,
229   SHAPE_RECTANGLE,
230   SHAPE_OVAL
231 };
232
233 static GtkRadioActionEntry shape_entries[] = {
234   { "Square", NULL,                            /* name, stock id */
235     "_Square", "<control>S",                   /* label, accelerator */
236     "Square",  SHAPE_SQUARE },                 /* tooltip, value */
237   { "Rectangle", NULL,                         /* name, stock id */
238     "_Rectangle", "<control>R",                /* label, accelerator */
239     "Rectangle", SHAPE_RECTANGLE },            /* tooltip, value */
240   { "Oval", NULL,                              /* name, stock id */
241     "_Oval", "<control>O",                     /* label, accelerator */
242     "Egg", SHAPE_OVAL },                       /* tooltip, value */
243 };
244 static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
245
246 static const gchar *ui_info =
247 "<ui>"
248 "  <menubar name='MenuBar'>"
249 "    <menu action='FileMenu'>"
250 "      <menuitem action='New'/>"
251 "      <menuitem action='Open'/>"
252 "      <menuitem action='Save'/>"
253 "      <menuitem action='SaveAs'/>"
254 "      <separator/>"
255 "      <menuitem action='Quit'/>"
256 "    </menu>"
257 "    <menu action='PreferencesMenu'>"
258 "      <menuitem action='DarkTheme'/>"
259 "      <menuitem action='HideTitlebar'/>"
260 "      <menu action='ColorMenu'>"
261 "       <menuitem action='Red'/>"
262 "       <menuitem action='Green'/>"
263 "       <menuitem action='Blue'/>"
264 "      </menu>"
265 "      <menu action='ShapeMenu'>"
266 "        <menuitem action='Square'/>"
267 "        <menuitem action='Rectangle'/>"
268 "        <menuitem action='Oval'/>"
269 "      </menu>"
270 "      <menuitem action='Bold'/>"
271 "    </menu>"
272 "    <menu action='HelpMenu'>"
273 "      <menuitem action='About'/>"
274 "    </menu>"
275 "  </menubar>"
276 "  <toolbar name='ToolBar'>"
277 "    <toolitem action='Open'>"
278 "      <menu action='OpenMenu'>"
279 "        <menuitem action='File1'/>"
280 "      </menu>"
281 "    </toolitem>"
282 "    <toolitem action='Quit'/>"
283 "    <separator action='Sep1'/>"
284 "    <toolitem action='Logo'/>"
285 "  </toolbar>"
286 "</ui>";
287
288
289
290 /* This function registers our custom toolbar icons, so they can be themed.
291  *
292  * It's totally optional to do this, you could just manually insert icons
293  * and have them not be themeable, especially if you never expect people
294  * to theme your app.
295  */
296 static void
297 register_stock_icons (void)
298 {
299   static gboolean registered = FALSE;
300
301   if (!registered)
302     {
303       GdkPixbuf *pixbuf;
304       GtkIconFactory *factory;
305       GtkIconSet *icon_set;
306       GdkPixbuf *transparent;
307
308       static GtkStockItem items[] = {
309         { "demo-gtk-logo",
310           "_GTK!",
311           0, 0, NULL }
312       };
313
314       registered = TRUE;
315
316       /* Register our stock items */
317       gtk_stock_add (items, G_N_ELEMENTS (items));
318
319       /* Add our custom icon factory to the list of defaults */
320       factory = gtk_icon_factory_new ();
321       gtk_icon_factory_add_default (factory);
322
323       pixbuf = gdk_pixbuf_new_from_resource ("/appwindow/gtk-logo-rgb.gif", NULL);
324       /* We assert the existence of the pixbuf as we load it from a custom resource. */
325       g_assert (pixbuf);
326
327       /* The gtk-logo-rgb icon has a white background, make it transparent */
328       transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
329
330       icon_set = gtk_icon_set_new_from_pixbuf (transparent);
331       gtk_icon_factory_add (factory, "demo-gtk-logo", icon_set);
332       gtk_icon_set_unref (icon_set);
333       g_object_unref (pixbuf);
334       g_object_unref (transparent);
335
336       /* Drop our reference to the factory, GTK will hold a reference. */
337       g_object_unref (factory);
338     }
339 }
340
341 static void
342 update_statusbar (GtkTextBuffer *buffer,
343                   GtkStatusbar  *statusbar)
344 {
345   gchar *msg;
346   gint row, col;
347   gint count;
348   GtkTextIter iter;
349
350   gtk_statusbar_pop (statusbar, 0); /* clear any previous message,
351                                      * underflow is allowed
352                                      */
353
354   count = gtk_text_buffer_get_char_count (buffer);
355
356   gtk_text_buffer_get_iter_at_mark (buffer,
357                                     &iter,
358                                     gtk_text_buffer_get_insert (buffer));
359
360   row = gtk_text_iter_get_line (&iter);
361   col = gtk_text_iter_get_line_offset (&iter);
362
363   msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
364                          row, col, count);
365
366   gtk_statusbar_push (statusbar, 0, msg);
367
368   g_free (msg);
369 }
370
371 static void
372 mark_set_callback (GtkTextBuffer     *buffer,
373                    const GtkTextIter *new_location,
374                    GtkTextMark       *mark,
375                    gpointer           data)
376 {
377   update_statusbar (buffer, GTK_STATUSBAR (data));
378 }
379
380 GtkWidget *
381 do_appwindow (GtkWidget *do_widget)
382 {
383   if (!window)
384     {
385       GtkWidget *table;
386       GtkWidget *statusbar;
387       GtkWidget *contents;
388       GtkWidget *sw;
389       GtkWidget *bar;
390       GtkTextBuffer *buffer;
391       GtkActionGroup *action_group;
392       GtkAction *open_action;
393       GtkUIManager *merge;
394       GError *error = NULL;
395
396       register_stock_icons ();
397
398       /* Create the toplevel window
399        */
400
401       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
402       gtk_window_set_screen (GTK_WINDOW (window),
403                              gtk_widget_get_screen (do_widget));
404       gtk_window_set_title (GTK_WINDOW (window), "Application Window");
405       gtk_window_set_icon_name (GTK_WINDOW (window), "document-open");
406
407       /* NULL window variable when window is closed */
408       g_signal_connect (window, "destroy",
409                         G_CALLBACK (gtk_widget_destroyed),
410                         &window);
411
412       table = gtk_grid_new ();
413
414       gtk_container_add (GTK_CONTAINER (window), table);
415
416       /* Create the menubar and toolbar
417        */
418
419       action_group = gtk_action_group_new ("AppWindowActions");
420       open_action = g_object_new (tool_menu_action_get_type (),
421                                   "name", "Open",
422                                   "label", "_Open",
423                                   "tooltip", "Open a file",
424                                   "stock-id", GTK_STOCK_OPEN,
425                                   NULL);
426       gtk_action_group_add_action (action_group, open_action);
427       g_object_unref (open_action);
428       gtk_action_group_add_actions (action_group,
429                                     entries, n_entries,
430                                     window);
431       gtk_action_group_add_toggle_actions (action_group,
432                                            toggle_entries, n_toggle_entries,
433                                            NULL);
434       gtk_action_group_add_radio_actions (action_group,
435                                           color_entries, n_color_entries,
436                                           COLOR_RED,
437                                           G_CALLBACK (activate_radio_action),
438                                           NULL);
439       gtk_action_group_add_radio_actions (action_group,
440                                           shape_entries, n_shape_entries,
441                                           SHAPE_SQUARE,
442                                           G_CALLBACK (activate_radio_action),
443                                           NULL);
444
445       merge = gtk_ui_manager_new ();
446       g_object_set_data_full (G_OBJECT (window), "ui-manager", merge,
447                               g_object_unref);
448       gtk_ui_manager_insert_action_group (merge, action_group, 0);
449       gtk_window_add_accel_group (GTK_WINDOW (window),
450                                   gtk_ui_manager_get_accel_group (merge));
451
452       if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
453         {
454           g_message ("building menus failed: %s", error->message);
455           g_error_free (error);
456         }
457
458       bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
459       gtk_widget_show (bar);
460       gtk_widget_set_halign (bar, GTK_ALIGN_FILL);
461       gtk_grid_attach (GTK_GRID (table), bar, 0, 0, 1, 1);
462
463       bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
464       gtk_widget_show (bar);
465       gtk_widget_set_halign (bar, GTK_ALIGN_FILL);
466       gtk_grid_attach (GTK_GRID (table), bar, 0, 1, 1, 1);
467
468       /* Create document
469        */
470
471       infobar = gtk_info_bar_new ();
472       gtk_widget_set_no_show_all (infobar, TRUE);
473       messagelabel = gtk_label_new ("");
474       gtk_widget_show (messagelabel);
475       gtk_box_pack_start (GTK_BOX (gtk_info_bar_get_content_area (GTK_INFO_BAR (infobar))),
476                           messagelabel,
477                           TRUE, TRUE, 0);
478       gtk_info_bar_add_button (GTK_INFO_BAR (infobar),
479                                GTK_STOCK_OK, GTK_RESPONSE_OK);
480       g_signal_connect (infobar, "response",
481                         G_CALLBACK (gtk_widget_hide), NULL);
482
483       gtk_widget_set_halign (infobar, GTK_ALIGN_FILL);
484       gtk_grid_attach (GTK_GRID (table), infobar, 0, 2, 1, 1);
485
486       sw = gtk_scrolled_window_new (NULL, NULL);
487
488       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
489                                       GTK_POLICY_AUTOMATIC,
490                                       GTK_POLICY_AUTOMATIC);
491
492       gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
493                                            GTK_SHADOW_IN);
494
495       gtk_widget_set_halign (sw, GTK_ALIGN_FILL);
496       gtk_widget_set_valign (sw, GTK_ALIGN_FILL);
497       gtk_widget_set_hexpand (sw, TRUE);
498       gtk_widget_set_vexpand (sw, TRUE);
499       gtk_grid_attach (GTK_GRID (table), sw, 0, 3, 1, 1);
500
501       gtk_window_set_default_size (GTK_WINDOW (window),
502                                    200, 200);
503
504       contents = gtk_text_view_new ();
505       gtk_widget_grab_focus (contents);
506
507       gtk_container_add (GTK_CONTAINER (sw),
508                          contents);
509
510       /* Create statusbar */
511
512       statusbar = gtk_statusbar_new ();
513       gtk_widget_set_halign (sw, GTK_ALIGN_FILL);
514       gtk_grid_attach (GTK_GRID (table), statusbar, 0, 4, 1, 1);
515
516       /* Show text widget info in the statusbar */
517       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
518
519       g_signal_connect_object (buffer,
520                                "changed",
521                                G_CALLBACK (update_statusbar),
522                                statusbar,
523                                0);
524
525       g_signal_connect_object (buffer,
526                                "mark_set", /* cursor moved */
527                                G_CALLBACK (mark_set_callback),
528                                statusbar,
529                                0);
530
531       update_statusbar (buffer, GTK_STATUSBAR (statusbar));
532     }
533
534   if (!gtk_widget_get_visible (window))
535     {
536       gtk_widget_show_all (window);
537     }
538   else
539     {
540       gtk_widget_destroy (window);
541       window = NULL;
542       infobar = NULL;
543       messagelabel = NULL;
544     }
545
546   return window;
547 }