1 /* Application main window
3 * Demonstrates a typical application window with menubar, toolbar, statusbar.
8 #include "demo-common.h"
10 static GtkWidget *window = NULL;
11 static GtkWidget *infobar = NULL;
12 static GtkWidget *messagelabel = NULL;
15 activate_action (GtkAction *action)
17 const gchar *name = gtk_action_get_name (action);
18 const gchar *typename = G_OBJECT_TYPE_NAME (action);
22 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
23 GTK_DIALOG_DESTROY_WITH_PARENT,
26 "You activated action: \"%s\" of type \"%s\"",
29 /* Close dialog on user response */
30 g_signal_connect (dialog,
32 G_CALLBACK (gtk_widget_destroy),
35 gtk_widget_show (dialog);
39 activate_radio_action (GtkAction *action, GtkRadioAction *current)
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));
50 text = g_strdup_printf ("You activated radio action: \"%s\" of type \"%s\".\n"
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);
61 activate_email (GtkAboutDialog *about,
66 text = g_strdup_printf ("send mail to %s", link);
67 g_print ("%s\n", text);
72 activate_url (GtkAboutDialog *about,
76 g_print ("show url %s\n", link);
80 about_cb (GtkAction *action,
83 GdkPixbuf *pixbuf, *transparent;
86 const gchar *authors[] = {
94 const gchar *documentors[] = {
97 "Matthias Clasen <mclasen@redhat.com>",
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"
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"
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";
120 filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
123 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
125 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
126 g_object_unref (pixbuf);
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",
136 "website", "http://www.gtk.org",
137 "comments", "Program to demonstrate GTK+ functions.",
139 "documenters", documentors,
141 "title", "About GTK+ Code Demos",
144 g_object_unref (transparent);
154 GtkActionClass parent_class;
155 } ToolMenuActionClass;
157 G_DEFINE_TYPE(ToolMenuAction, tool_menu_action, GTK_TYPE_ACTION)
160 tool_menu_action_class_init (ToolMenuActionClass *class)
162 GTK_ACTION_CLASS (class)->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
166 tool_menu_action_init (ToolMenuAction *action)
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) },
206 static guint n_entries = G_N_ELEMENTS (entries);
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 */
216 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
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 */
235 static guint n_color_entries = G_N_ELEMENTS (color_entries);
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 */
254 static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
256 static const gchar *ui_info =
258 " <menubar name='MenuBar'>"
259 " <menu action='FileMenu'>"
260 " <menuitem action='New'/>"
261 " <menuitem action='Open'/>"
262 " <menuitem action='Save'/>"
263 " <menuitem action='SaveAs'/>"
265 " <menuitem action='Quit'/>"
267 " <menu action='PreferencesMenu'>"
268 " <menu action='ColorMenu'>"
269 " <menuitem action='Red'/>"
270 " <menuitem action='Green'/>"
271 " <menuitem action='Blue'/>"
273 " <menu action='ShapeMenu'>"
274 " <menuitem action='Square'/>"
275 " <menuitem action='Rectangle'/>"
276 " <menuitem action='Oval'/>"
278 " <menuitem action='Bold'/>"
280 " <menu action='HelpMenu'>"
281 " <menuitem action='About'/>"
284 " <toolbar name='ToolBar'>"
285 " <toolitem action='Open'>"
286 " <menu action='OpenMenu'>"
287 " <menuitem action='File1'/>"
290 " <toolitem action='Quit'/>"
291 " <separator action='Sep1'/>"
292 " <toolitem action='Logo'/>"
298 /* This function registers our custom toolbar icons, so they can be themed.
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
305 register_stock_icons (void)
307 static gboolean registered = FALSE;
312 GtkIconFactory *factory;
315 static GtkStockItem items[] = {
323 /* Register our stock items */
324 gtk_stock_add (items, G_N_ELEMENTS (items));
326 /* Add our custom icon factory to the list of defaults */
327 factory = gtk_icon_factory_new ();
328 gtk_icon_factory_add_default (factory);
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.
335 filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
338 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
342 /* Register icon to accompany stock item */
345 GtkIconSet *icon_set;
346 GdkPixbuf *transparent;
348 /* The gtk-logo-rgb icon has a white background, make it transparent */
349 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
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);
358 g_warning ("failed to load GTK logo for toolbar");
360 /* Drop our reference to the factory, GTK will hold a reference. */
361 g_object_unref (factory);
366 update_statusbar (GtkTextBuffer *buffer,
367 GtkStatusbar *statusbar)
374 gtk_statusbar_pop (statusbar, 0); /* clear any previous message,
375 * underflow is allowed
378 count = gtk_text_buffer_get_char_count (buffer);
380 gtk_text_buffer_get_iter_at_mark (buffer,
382 gtk_text_buffer_get_insert (buffer));
384 row = gtk_text_iter_get_line (&iter);
385 col = gtk_text_iter_get_line_offset (&iter);
387 msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
390 gtk_statusbar_push (statusbar, 0, msg);
396 mark_set_callback (GtkTextBuffer *buffer,
397 const GtkTextIter *new_location,
401 update_statusbar (buffer, GTK_STATUSBAR (data));
405 update_resize_grip (GtkWidget *widget,
406 GdkEventWindowState *event,
407 GtkStatusbar *statusbar)
409 if (event->changed_mask & (GDK_WINDOW_STATE_MAXIMIZED |
410 GDK_WINDOW_STATE_FULLSCREEN))
414 maximized = event->new_window_state & (GDK_WINDOW_STATE_MAXIMIZED |
415 GDK_WINDOW_STATE_FULLSCREEN);
416 gtk_statusbar_set_has_resize_grip (statusbar, !maximized);
422 do_appwindow (GtkWidget *do_widget)
427 GtkWidget *statusbar;
431 GtkTextBuffer *buffer;
432 GtkActionGroup *action_group;
433 GtkAction *open_action;
435 GError *error = NULL;
437 register_stock_icons ();
439 /* Create the toplevel window
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");
448 /* NULL window variable when window is closed */
449 g_signal_connect (window, "destroy",
450 G_CALLBACK (gtk_widget_destroyed),
453 table = gtk_table_new (1, 5, FALSE);
455 gtk_container_add (GTK_CONTAINER (window), table);
457 /* Create the menubar and toolbar
460 action_group = gtk_action_group_new ("AppWindowActions");
461 open_action = g_object_new (tool_menu_action_get_type (),
464 "tooltip", "Open a file",
465 "stock-id", GTK_STOCK_OPEN,
467 gtk_action_group_add_action (action_group, open_action);
468 g_object_unref (open_action);
469 gtk_action_group_add_actions (action_group,
472 gtk_action_group_add_toggle_actions (action_group,
473 toggle_entries, n_toggle_entries,
475 gtk_action_group_add_radio_actions (action_group,
476 color_entries, n_color_entries,
478 G_CALLBACK (activate_radio_action),
480 gtk_action_group_add_radio_actions (action_group,
481 shape_entries, n_shape_entries,
483 G_CALLBACK (activate_radio_action),
486 merge = gtk_ui_manager_new ();
487 g_object_set_data_full (G_OBJECT (window), "ui-manager", merge,
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));
493 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
495 g_message ("building menus failed: %s", error->message);
496 g_error_free (error);
499 bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
500 gtk_widget_show (bar);
501 gtk_table_attach (GTK_TABLE (table),
503 /* X direction */ /* Y direction */
505 GTK_EXPAND | GTK_FILL, 0,
508 bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
509 gtk_widget_show (bar);
510 gtk_table_attach (GTK_TABLE (table),
512 /* X direction */ /* Y direction */
514 GTK_EXPAND | GTK_FILL, 0,
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))),
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);
532 gtk_table_attach (GTK_TABLE (table),
534 /* X direction */ /* Y direction */
536 GTK_EXPAND | GTK_FILL, 0,
539 sw = gtk_scrolled_window_new (NULL, NULL);
541 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
542 GTK_POLICY_AUTOMATIC,
543 GTK_POLICY_AUTOMATIC);
545 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
548 gtk_table_attach (GTK_TABLE (table),
550 /* X direction */ /* Y direction */
552 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
555 gtk_window_set_default_size (GTK_WINDOW (window),
558 contents = gtk_text_view_new ();
559 gtk_widget_grab_focus (contents);
561 gtk_container_add (GTK_CONTAINER (sw),
564 /* Create statusbar */
566 statusbar = gtk_statusbar_new ();
567 gtk_table_attach (GTK_TABLE (table),
569 /* X direction */ /* Y direction */
571 GTK_EXPAND | GTK_FILL, 0,
574 /* Show text widget info in the statusbar */
575 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
577 g_signal_connect_object (buffer,
579 G_CALLBACK (update_statusbar),
583 g_signal_connect_object (buffer,
584 "mark_set", /* cursor moved */
585 G_CALLBACK (mark_set_callback),
589 g_signal_connect_object (window,
590 "window_state_event",
591 G_CALLBACK (update_resize_grip),
595 update_statusbar (buffer, GTK_STATUSBAR (statusbar));
598 if (!gtk_widget_get_visible (window))
600 gtk_widget_show_all (window);
604 gtk_widget_destroy (window);