]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/appwindow.c
Updated
[~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
65 static GtkActionEntry entries[] = {
66   { "FileMenu", NULL, "_File" },               /* name, stock id, label */
67   { "PreferencesMenu", NULL, "_Preferences" }, /* name, stock id, label */
68   { "ColorMenu", NULL, "_Color"  },            /* name, stock id, label */
69   { "ShapeMenu", NULL, "_Shape" },             /* name, stock id, label */
70   { "HelpMenu", NULL, "_Help" },               /* name, stock id, label */
71   { "New", GTK_STOCK_NEW,                      /* name, stock id */
72     "_New", "<control>N",                      /* label, accelerator */
73     "Create a new file",                       /* tooltip */ 
74     G_CALLBACK (activate_action) },      
75   { "Open", GTK_STOCK_OPEN,                    /* name, stock id */
76     "_Open","<control>O",                      /* label, accelerator */     
77     "Open a file",                             /* tooltip */
78     G_CALLBACK (activate_action) }, 
79   { "Save", GTK_STOCK_SAVE,                    /* name, stock id */
80     "_Save","<control>S",                      /* label, accelerator */     
81     "Save current file",                       /* tooltip */
82     G_CALLBACK (activate_action) },
83   { "SaveAs", GTK_STOCK_SAVE,                  /* name, stock id */
84     "Save _As...", NULL,                       /* label, accelerator */     
85     "Save to a file",                          /* tooltip */
86     G_CALLBACK (activate_action) },
87   { "Quit", GTK_STOCK_QUIT,                    /* name, stock id */
88     "_Quit", "<control>Q",                     /* label, accelerator */     
89     "Quit",                                    /* tooltip */
90     G_CALLBACK (activate_action) },
91   { "About", NULL,                             /* name, stock id */
92     "_About", "<control>A",                    /* label, accelerator */     
93     "About",                                   /* tooltip */  
94     G_CALLBACK (activate_action) },
95   { "Logo", "demo-gtk-logo",                   /* name, stock id */
96      NULL, NULL,                               /* label, accelerator */     
97     "GTK+",                                    /* tooltip */
98     G_CALLBACK (activate_action) },
99 };
100 static guint n_entries = G_N_ELEMENTS (entries);
101
102
103 static GtkToggleActionEntry toggle_entries[] = {
104   { "Bold", GTK_STOCK_BOLD,                    /* name, stock id */
105      "_Bold", "<control>B",                    /* label, accelerator */     
106     "Bold",                                    /* tooltip */
107     G_CALLBACK (activate_action), 
108     TRUE },                                    /* is_active */
109 };
110 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
111
112 enum {
113   COLOR_RED,
114   COLOR_GREEN,
115   COLOR_BLUE
116 };
117
118 static GtkRadioActionEntry color_entries[] = {
119   { "Red", NULL,                               /* name, stock id */
120     "_Red", "<control>R",                      /* label, accelerator */     
121     "Blood", COLOR_RED },                      /* tooltip, value */
122   { "Green", NULL,                             /* name, stock id */
123     "_Green", "<control>G",                    /* label, accelerator */     
124     "Grass", COLOR_GREEN },                    /* tooltip, value */
125   { "Blue", NULL,                              /* name, stock id */
126     "_Blue", "<control>B",                     /* label, accelerator */     
127     "Sky", COLOR_BLUE },                       /* tooltip, value */
128 };
129 static guint n_color_entries = G_N_ELEMENTS (color_entries);
130
131 enum {
132   SHAPE_SQUARE,
133   SHAPE_RECTANGLE,
134   SHAPE_OVAL,
135 };
136
137 static GtkRadioActionEntry shape_entries[] = {
138   { "Square", NULL,                            /* name, stock id */
139     "_Square", "<control>S",                   /* label, accelerator */     
140     "Square",  SHAPE_SQUARE },                 /* tooltip, value */
141   { "Rectangle", NULL,                         /* name, stock id */
142     "_Rectangle", "<control>R",                /* label, accelerator */     
143     "Rectangle", SHAPE_RECTANGLE },            /* tooltip, value */
144   { "Oval", NULL,                              /* name, stock id */
145     "_Oval", "<control>O",                     /* label, accelerator */     
146     "Egg", SHAPE_OVAL },                       /* tooltip, value */  
147 };
148 static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
149
150 static const gchar *ui_info = 
151 "<ui>"
152 "  <menubar name='MenuBar'>"
153 "    <menu action='FileMenu'>"
154 "      <menuitem action='New'/>"
155 "      <menuitem action='Open'/>"
156 "      <menuitem action='Save'/>"
157 "      <menuitem action='SaveAs'/>"
158 "      <separator/>"
159 "      <menuitem action='Quit'/>"
160 "    </menu>"
161 "    <menu action='PreferencesMenu'>"
162 "      <menu action='ColorMenu'>"
163 "       <menuitem action='Red'/>"
164 "       <menuitem action='Green'/>"
165 "       <menuitem action='Blue'/>"
166 "      </menu>"
167 "      <menu action='ShapeMenu'>"
168 "        <menuitem action='Square'/>"
169 "        <menuitem action='Rectangle'/>"
170 "        <menuitem action='Oval'/>"
171 "      </menu>"
172 "      <menuitem action='Bold'/>"
173 "    </menu>"
174 "    <menu action='HelpMenu'>"
175 "      <menuitem action='About'/>"
176 "    </menu>"
177 "  </menubar>"
178 "  <toolbar  name='ToolBar'>"
179 "    <toolitem action='Open'/>"
180 "    <toolitem action='Quit'/>"
181 "    <separator action='Sep1'/>"
182 "    <toolitem action='Logo'/>"
183 "  </toolbar>"
184 "</ui>";
185
186
187
188 /* This function registers our custom toolbar icons, so they can be themed.
189  *
190  * It's totally optional to do this, you could just manually insert icons
191  * and have them not be themeable, especially if you never expect people
192  * to theme your app.
193  */
194 static void
195 register_stock_icons (void)
196 {
197   static gboolean registered = FALSE;
198   
199   if (!registered)
200     {
201       GdkPixbuf *pixbuf;
202       GtkIconFactory *factory;
203       char *filename;
204
205       static GtkStockItem items[] = {
206         { "demo-gtk-logo",
207           "_GTK!",
208           0, 0, NULL }
209       };
210       
211       registered = TRUE;
212
213       /* Register our stock items */
214       gtk_stock_add (items, G_N_ELEMENTS (items));
215       
216       /* Add our custom icon factory to the list of defaults */
217       factory = gtk_icon_factory_new ();
218       gtk_icon_factory_add_default (factory);
219
220       /* demo_find_file() looks in the the current directory first,
221        * so you can run gtk-demo without installing GTK, then looks
222        * in the location where the file is installed.
223        */
224       pixbuf = NULL;
225       filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
226       if (filename)
227         {
228           pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
229           g_free (filename);
230         }
231
232       /* Register icon to accompany stock item */
233       if (pixbuf != NULL)
234         {
235           GtkIconSet *icon_set;
236           GdkPixbuf *transparent;
237
238           /* The gtk-logo-rgb icon has a white background, make it transparent */
239           transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
240           
241           icon_set = gtk_icon_set_new_from_pixbuf (transparent);
242           gtk_icon_factory_add (factory, "demo-gtk-logo", icon_set);
243           gtk_icon_set_unref (icon_set);
244           g_object_unref (pixbuf);
245           g_object_unref (transparent);
246         }
247       else
248         g_warning ("failed to load GTK logo for toolbar");
249       
250       /* Drop our reference to the factory, GTK will hold a reference. */
251       g_object_unref (factory);
252     }
253 }
254
255 static void
256 update_statusbar (GtkTextBuffer *buffer,
257                   GtkStatusbar  *statusbar)
258 {
259   gchar *msg;
260   gint row, col;
261   gint count;
262   GtkTextIter iter;
263   
264   gtk_statusbar_pop (statusbar, 0); /* clear any previous message, underflow is allowed */
265
266   count = gtk_text_buffer_get_char_count (buffer);
267
268   gtk_text_buffer_get_iter_at_mark (buffer,
269                                     &iter,
270                                     gtk_text_buffer_get_insert (buffer));
271
272   row = gtk_text_iter_get_line (&iter);
273   col = gtk_text_iter_get_line_offset (&iter);
274
275   msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
276                          row, col, count);
277
278   gtk_statusbar_push (statusbar, 0, msg);
279
280   g_free (msg);
281 }
282
283 static void
284 mark_set_callback (GtkTextBuffer     *buffer,
285                    const GtkTextIter *new_location,
286                    GtkTextMark       *mark,
287                    gpointer           data)
288 {
289   update_statusbar (buffer, GTK_STATUSBAR (data));
290 }
291
292 static void
293 update_resize_grip (GtkWidget           *widget,
294                     GdkEventWindowState *event,
295                     GtkStatusbar        *statusbar)
296 {
297   if (event->changed_mask & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_FULLSCREEN))
298     gtk_statusbar_set_has_resize_grip (statusbar, !(event->new_window_state & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_FULLSCREEN)));
299 }
300                     
301
302 GtkWidget *
303 do_appwindow (GtkWidget *do_widget)
304 {  
305   if (!window)
306     {
307       GtkWidget *table;
308       GtkWidget *statusbar;
309       GtkWidget *contents;
310       GtkWidget *sw;
311       GtkWidget *bar;
312       GtkTextBuffer *buffer;
313       GtkActionGroup *action_group;
314       GtkUIManager *merge;
315       GError *error = NULL;
316
317       register_stock_icons ();
318       
319       /* Create the toplevel window
320        */
321       
322       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
323       gtk_window_set_screen (GTK_WINDOW (window),
324                              gtk_widget_get_screen (do_widget));
325       gtk_window_set_title (GTK_WINDOW (window), "Application Window");
326
327       /* NULL window variable when window is closed */
328       g_signal_connect (window, "destroy",
329                         G_CALLBACK (gtk_widget_destroyed),
330                         &window);
331
332       table = gtk_table_new (1, 4, FALSE);
333       
334       gtk_container_add (GTK_CONTAINER (window), table);
335       
336       /* Create the menubar and toolbar
337        */
338       
339       action_group = gtk_action_group_new ("AppWindowActions");
340       gtk_action_group_add_actions (action_group, 
341                                     entries, n_entries, 
342                                     NULL);
343       gtk_action_group_add_toggle_actions (action_group, 
344                                            toggle_entries, n_toggle_entries, 
345                                            NULL);
346       gtk_action_group_add_radio_actions (action_group, 
347                                           color_entries, n_color_entries, 
348                                           COLOR_RED,
349                                           G_CALLBACK (activate_radio_action), 
350                                           NULL);
351       gtk_action_group_add_radio_actions (action_group, 
352                                           shape_entries, n_shape_entries, 
353                                           SHAPE_SQUARE,
354                                           G_CALLBACK (activate_radio_action), 
355                                           NULL);
356
357       merge = gtk_ui_manager_new ();
358       g_object_set_data_full (G_OBJECT (window), "ui-manager", merge, g_object_unref);
359       gtk_ui_manager_insert_action_group (merge, action_group, 0);
360       gtk_window_add_accel_group (GTK_WINDOW (window), 
361                                   gtk_ui_manager_get_accel_group (merge));
362       
363       if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
364         {
365           g_message ("building menus failed: %s", error->message);
366           g_error_free (error);
367         }
368
369       bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
370       gtk_widget_show (bar);
371       gtk_table_attach (GTK_TABLE (table),
372                         bar, 
373                         /* X direction */          /* Y direction */
374                         0, 1,                      0, 1,
375                         GTK_EXPAND | GTK_FILL,     0,
376                         0,                         0);
377
378       bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
379       gtk_toolbar_set_tooltips (GTK_TOOLBAR (bar), TRUE);
380       gtk_widget_show (bar);
381       gtk_table_attach (GTK_TABLE (table),
382                         bar, 
383                         /* X direction */       /* Y direction */
384                         0, 1,                   1, 2,
385                         GTK_EXPAND | GTK_FILL,  0,
386                         0,                      0);
387
388       /* Create document
389        */
390
391       sw = gtk_scrolled_window_new (NULL, NULL);
392
393       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
394                                       GTK_POLICY_AUTOMATIC,
395                                       GTK_POLICY_AUTOMATIC);
396
397       gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
398                                            GTK_SHADOW_IN);
399       
400       gtk_table_attach (GTK_TABLE (table),
401                         sw,
402                         /* X direction */       /* Y direction */
403                         0, 1,                   2, 3,
404                         GTK_EXPAND | GTK_FILL,  GTK_EXPAND | GTK_FILL,
405                         0,                      0);
406
407       gtk_window_set_default_size (GTK_WINDOW (window),
408                                    200, 200);
409       
410       contents = gtk_text_view_new ();
411       gtk_widget_grab_focus (contents);
412       
413       gtk_container_add (GTK_CONTAINER (sw),
414                          contents);
415
416       /* Create statusbar */
417
418       statusbar = gtk_statusbar_new ();
419       gtk_table_attach (GTK_TABLE (table),
420                         statusbar,
421                         /* X direction */       /* Y direction */
422                         0, 1,                   3, 4,
423                         GTK_EXPAND | GTK_FILL,  0,
424                         0,                      0);
425
426       /* Show text widget info in the statusbar */
427       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
428       
429       g_signal_connect_object (buffer,
430                                "changed",
431                                G_CALLBACK (update_statusbar),
432                                statusbar,
433                                0);
434
435       g_signal_connect_object (buffer,
436                                "mark_set", /* cursor moved */
437                                G_CALLBACK (mark_set_callback),
438                                statusbar,
439                                0);
440
441       g_signal_connect_object (window, 
442                                "window_state_event", 
443                                G_CALLBACK (update_resize_grip),
444                                statusbar,
445                                0);
446       
447       update_statusbar (buffer, GTK_STATUSBAR (statusbar));
448     }
449
450   if (!GTK_WIDGET_VISIBLE (window))
451     {
452       gtk_widget_show_all (window);
453     }
454   else
455     {    
456       gtk_widget_destroy (window);
457       window = NULL;
458     }
459
460   return window;
461 }
462