1 /* Application main window
3 * Demonstrates a typical application window with menubar, toolbar, statusbar.
8 #include "demo-common.h"
10 static GtkWidget *window = NULL;
13 activate_action (GtkAction *action)
15 const gchar *name = gtk_action_get_name (action);
16 const gchar *typename = G_OBJECT_TYPE_NAME (action);
20 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
21 GTK_DIALOG_DESTROY_WITH_PARENT,
24 "You activated action: \"%s\" of type \"%s\"",
27 /* Close dialog on user response */
28 g_signal_connect (dialog,
30 G_CALLBACK (gtk_widget_destroy),
33 gtk_widget_show (dialog);
37 activate_radio_action (GtkAction *action, GtkRadioAction *current)
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));
48 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
49 GTK_DIALOG_DESTROY_WITH_PARENT,
52 "You activated radio action: \"%s\" of type \"%s\".\n"
54 name, typename, value);
56 /* Close dialog on user response */
57 g_signal_connect (dialog,
59 G_CALLBACK (gtk_widget_destroy),
62 gtk_widget_show (dialog);
67 activate_email (GtkAboutDialog *about,
71 g_print ("send mail to %s\n", link);
75 activate_url (GtkAboutDialog *about,
79 g_print ("show url %s\n", link);
83 about_cb (GtkAction *action,
86 GdkPixbuf *pixbuf, *transparent;
89 const gchar *authors[] = {
97 const gchar *documentors[] = {
100 "Matthias Clasen <mclasen@redhat.com>",
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"
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"
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";
123 filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
126 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
128 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
129 g_object_unref (pixbuf);
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",
139 "website", "http://www.gtk.org",
140 "comments", "Program to demonstrate GTK+ functions.",
142 "documenters", documentors,
146 g_object_unref (transparent);
156 GtkActionClass parent_class;
157 } ToolMenuActionClass;
159 G_DEFINE_TYPE(ToolMenuAction, tool_menu_action, GTK_TYPE_ACTION)
162 tool_menu_action_class_init (ToolMenuActionClass *class)
164 GTK_ACTION_CLASS (class)->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
168 tool_menu_action_init (ToolMenuAction *action)
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) },
208 static guint n_entries = G_N_ELEMENTS (entries);
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 */
218 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
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 */
237 static guint n_color_entries = G_N_ELEMENTS (color_entries);
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 */
256 static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
258 static const gchar *ui_info =
260 " <menubar name='MenuBar'>"
261 " <menu action='FileMenu'>"
262 " <menuitem action='New'/>"
263 " <menuitem action='Open'/>"
264 " <menuitem action='Save'/>"
265 " <menuitem action='SaveAs'/>"
267 " <menuitem action='Quit'/>"
269 " <menu action='PreferencesMenu'>"
270 " <menu action='ColorMenu'>"
271 " <menuitem action='Red'/>"
272 " <menuitem action='Green'/>"
273 " <menuitem action='Blue'/>"
275 " <menu action='ShapeMenu'>"
276 " <menuitem action='Square'/>"
277 " <menuitem action='Rectangle'/>"
278 " <menuitem action='Oval'/>"
280 " <menuitem action='Bold'/>"
282 " <menu action='HelpMenu'>"
283 " <menuitem action='About'/>"
286 " <toolbar name='ToolBar'>"
287 " <toolitem action='Open'>"
288 " <menu action='OpenMenu'>"
289 " <menuitem action='File1'/>"
292 " <toolitem action='Quit'/>"
293 " <separator action='Sep1'/>"
294 " <toolitem action='Logo'/>"
300 /* This function registers our custom toolbar icons, so they can be themed.
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
307 register_stock_icons (void)
309 static gboolean registered = FALSE;
314 GtkIconFactory *factory;
317 static GtkStockItem items[] = {
325 /* Register our stock items */
326 gtk_stock_add (items, G_N_ELEMENTS (items));
328 /* Add our custom icon factory to the list of defaults */
329 factory = gtk_icon_factory_new ();
330 gtk_icon_factory_add_default (factory);
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.
337 filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
340 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
344 /* Register icon to accompany stock item */
347 GtkIconSet *icon_set;
348 GdkPixbuf *transparent;
350 /* The gtk-logo-rgb icon has a white background, make it transparent */
351 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
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);
360 g_warning ("failed to load GTK logo for toolbar");
362 /* Drop our reference to the factory, GTK will hold a reference. */
363 g_object_unref (factory);
368 update_statusbar (GtkTextBuffer *buffer,
369 GtkStatusbar *statusbar)
376 gtk_statusbar_pop (statusbar, 0); /* clear any previous message,
377 * underflow is allowed
380 count = gtk_text_buffer_get_char_count (buffer);
382 gtk_text_buffer_get_iter_at_mark (buffer,
384 gtk_text_buffer_get_insert (buffer));
386 row = gtk_text_iter_get_line (&iter);
387 col = gtk_text_iter_get_line_offset (&iter);
389 msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
392 gtk_statusbar_push (statusbar, 0, msg);
398 mark_set_callback (GtkTextBuffer *buffer,
399 const GtkTextIter *new_location,
403 update_statusbar (buffer, GTK_STATUSBAR (data));
407 update_resize_grip (GtkWidget *widget,
408 GdkEventWindowState *event,
409 GtkStatusbar *statusbar)
411 if (event->changed_mask & (GDK_WINDOW_STATE_MAXIMIZED |
412 GDK_WINDOW_STATE_FULLSCREEN))
416 maximized = event->new_window_state & (GDK_WINDOW_STATE_MAXIMIZED |
417 GDK_WINDOW_STATE_FULLSCREEN);
418 gtk_statusbar_set_has_resize_grip (statusbar, !maximized);
424 do_appwindow (GtkWidget *do_widget)
429 GtkWidget *statusbar;
433 GtkTextBuffer *buffer;
434 GtkActionGroup *action_group;
435 GtkAction *open_action;
437 GError *error = NULL;
439 register_stock_icons ();
441 /* Create the toplevel window
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");
450 /* NULL window variable when window is closed */
451 g_signal_connect (window, "destroy",
452 G_CALLBACK (gtk_widget_destroyed),
455 table = gtk_table_new (1, 4, FALSE);
457 gtk_container_add (GTK_CONTAINER (window), table);
459 /* Create the menubar and toolbar
462 action_group = gtk_action_group_new ("AppWindowActions");
463 open_action = g_object_new (tool_menu_action_get_type (),
466 "tooltip", "Open a file",
467 "stock-id", GTK_STOCK_OPEN,
469 gtk_action_group_add_action (action_group, open_action);
470 g_object_unref (open_action);
471 gtk_action_group_add_actions (action_group,
474 gtk_action_group_add_toggle_actions (action_group,
475 toggle_entries, n_toggle_entries,
477 gtk_action_group_add_radio_actions (action_group,
478 color_entries, n_color_entries,
480 G_CALLBACK (activate_radio_action),
482 gtk_action_group_add_radio_actions (action_group,
483 shape_entries, n_shape_entries,
485 G_CALLBACK (activate_radio_action),
488 merge = gtk_ui_manager_new ();
489 g_object_set_data_full (G_OBJECT (window), "ui-manager", merge,
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 g_object_unref (open_action);
496 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
498 g_message ("building menus failed: %s", error->message);
499 g_error_free (error);
502 bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
503 gtk_widget_show (bar);
504 gtk_table_attach (GTK_TABLE (table),
506 /* X direction */ /* Y direction */
508 GTK_EXPAND | GTK_FILL, 0,
511 bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
512 gtk_toolbar_set_tooltips (GTK_TOOLBAR (bar), TRUE);
513 gtk_widget_show (bar);
514 gtk_table_attach (GTK_TABLE (table),
516 /* X direction */ /* Y direction */
518 GTK_EXPAND | GTK_FILL, 0,
524 sw = gtk_scrolled_window_new (NULL, NULL);
526 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
527 GTK_POLICY_AUTOMATIC,
528 GTK_POLICY_AUTOMATIC);
530 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
533 gtk_table_attach (GTK_TABLE (table),
535 /* X direction */ /* Y direction */
537 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
540 gtk_window_set_default_size (GTK_WINDOW (window),
543 contents = gtk_text_view_new ();
544 gtk_widget_grab_focus (contents);
546 gtk_container_add (GTK_CONTAINER (sw),
549 /* Create statusbar */
551 statusbar = gtk_statusbar_new ();
552 gtk_table_attach (GTK_TABLE (table),
554 /* X direction */ /* Y direction */
556 GTK_EXPAND | GTK_FILL, 0,
559 /* Show text widget info in the statusbar */
560 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
562 g_signal_connect_object (buffer,
564 G_CALLBACK (update_statusbar),
568 g_signal_connect_object (buffer,
569 "mark_set", /* cursor moved */
570 G_CALLBACK (mark_set_callback),
574 g_signal_connect_object (window,
575 "window_state_event",
576 G_CALLBACK (update_resize_grip),
580 update_statusbar (buffer, GTK_STATUSBAR (statusbar));
583 if (!GTK_WIDGET_VISIBLE (window))
585 gtk_widget_show_all (window);
589 gtk_widget_destroy (window);