Debian Codegraph @

demos/gtk-demo/appwindow.c

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