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