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