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