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