1 /* Application main window
3 * Demonstrates a typical application window, with menubar, toolbar, statusbar.
7 #include "demo-common.h"
9 static GtkWidget *window = NULL;
12 activate_action (GtkAction *action)
14 const gchar *name = gtk_action_get_name (action);
15 const gchar *typename = G_OBJECT_TYPE_NAME (action);
19 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
20 GTK_DIALOG_DESTROY_WITH_PARENT,
23 "You activated action: \"%s\" of type \"%s\"",
26 /* Close dialog on user response */
27 g_signal_connect (dialog,
29 G_CALLBACK (gtk_widget_destroy),
32 gtk_widget_show (dialog);
36 activate_radio_action (GtkAction *action, GtkRadioAction *current)
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));
47 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
48 GTK_DIALOG_DESTROY_WITH_PARENT,
51 "You activated radio action: \"%s\" of type \"%s\".\nCurrent value: %d",
52 name, typename, value);
54 /* Close dialog on user response */
55 g_signal_connect (dialog,
57 G_CALLBACK (gtk_widget_destroy),
60 gtk_widget_show (dialog);
65 activate_email (GtkAboutDialog *about,
69 g_print ("send mail to %s\n", link);
73 activate_url (GtkAboutDialog *about,
77 g_print ("show url %s\n", link);
81 about_cb (GtkAction *action,
84 GdkPixbuf *pixbuf, *transparent;
87 const gchar *authors[] = {
95 const gchar *documentors[] = {
98 "Matthias Clasen <mclasen@redhat.com>",
103 const gchar *license =
104 "This library is free software; you can redistribute it and/or\n"
105 "modify it under the terms of the GNU Library General Public License as\n"
106 "published by the Free Software Foundation; either version 2 of the\n"
107 "License, or (at your option) any later version.\n"
109 "This library is distributed in the hope that it will be useful,\n"
110 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
111 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
112 "Library General Public License for more details.\n"
114 "You should have received a copy of the GNU Library General Public\n"
115 "License along with the Gnome Library; see the file COPYING.LIB. If not,\n"
116 "write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
117 "Boston, MA 02111-1307, USA.\n";
121 filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
124 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
126 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
127 g_object_unref (pixbuf);
130 gtk_about_dialog_set_email_hook (activate_email, NULL, NULL);
131 gtk_about_dialog_set_url_hook (activate_url, NULL, NULL);
132 gtk_show_about_dialog (GTK_WINDOW (window),
133 "name", "GTK+ Code Demos",
135 "copyright", "(C) 1997-2004 The GTK+ Team",
137 "website", "http://www.gtk.org",
138 "comments", "Program to demonstrate GTK+ functions.",
140 "documenters", documentors,
144 g_object_unref (transparent);
148 static GtkActionEntry entries[] = {
149 { "FileMenu", NULL, "_File" }, /* name, stock id, label */
150 { "PreferencesMenu", NULL, "_Preferences" }, /* name, stock id, label */
151 { "ColorMenu", NULL, "_Color" }, /* name, stock id, label */
152 { "ShapeMenu", NULL, "_Shape" }, /* name, stock id, label */
153 { "HelpMenu", NULL, "_Help" }, /* name, stock id, label */
154 { "New", GTK_STOCK_NEW, /* name, stock id */
155 "_New", "<control>N", /* label, accelerator */
156 "Create a new file", /* tooltip */
157 G_CALLBACK (activate_action) },
158 { "Open", GTK_STOCK_OPEN, /* name, stock id */
159 "_Open","<control>O", /* label, accelerator */
160 "Open a file", /* tooltip */
161 G_CALLBACK (activate_action) },
162 { "Save", GTK_STOCK_SAVE, /* name, stock id */
163 "_Save","<control>S", /* label, accelerator */
164 "Save current file", /* tooltip */
165 G_CALLBACK (activate_action) },
166 { "SaveAs", GTK_STOCK_SAVE, /* name, stock id */
167 "Save _As...", NULL, /* label, accelerator */
168 "Save to a file", /* tooltip */
169 G_CALLBACK (activate_action) },
170 { "Quit", GTK_STOCK_QUIT, /* name, stock id */
171 "_Quit", "<control>Q", /* label, accelerator */
172 "Quit", /* tooltip */
173 G_CALLBACK (activate_action) },
174 { "About", NULL, /* name, stock id */
175 "_About", "<control>A", /* label, accelerator */
176 "About", /* tooltip */
177 G_CALLBACK (about_cb) },
178 { "Logo", "demo-gtk-logo", /* name, stock id */
179 NULL, NULL, /* label, accelerator */
180 "GTK+", /* tooltip */
181 G_CALLBACK (activate_action) },
183 static guint n_entries = G_N_ELEMENTS (entries);
186 static GtkToggleActionEntry toggle_entries[] = {
187 { "Bold", GTK_STOCK_BOLD, /* name, stock id */
188 "_Bold", "<control>B", /* label, accelerator */
189 "Bold", /* tooltip */
190 G_CALLBACK (activate_action),
191 TRUE }, /* is_active */
193 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
201 static GtkRadioActionEntry color_entries[] = {
202 { "Red", NULL, /* name, stock id */
203 "_Red", "<control>R", /* label, accelerator */
204 "Blood", COLOR_RED }, /* tooltip, value */
205 { "Green", NULL, /* name, stock id */
206 "_Green", "<control>G", /* label, accelerator */
207 "Grass", COLOR_GREEN }, /* tooltip, value */
208 { "Blue", NULL, /* name, stock id */
209 "_Blue", "<control>B", /* label, accelerator */
210 "Sky", COLOR_BLUE }, /* tooltip, value */
212 static guint n_color_entries = G_N_ELEMENTS (color_entries);
220 static GtkRadioActionEntry shape_entries[] = {
221 { "Square", NULL, /* name, stock id */
222 "_Square", "<control>S", /* label, accelerator */
223 "Square", SHAPE_SQUARE }, /* tooltip, value */
224 { "Rectangle", NULL, /* name, stock id */
225 "_Rectangle", "<control>R", /* label, accelerator */
226 "Rectangle", SHAPE_RECTANGLE }, /* tooltip, value */
227 { "Oval", NULL, /* name, stock id */
228 "_Oval", "<control>O", /* label, accelerator */
229 "Egg", SHAPE_OVAL }, /* tooltip, value */
231 static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
233 static const gchar *ui_info =
235 " <menubar name='MenuBar'>"
236 " <menu action='FileMenu'>"
237 " <menuitem action='New'/>"
238 " <menuitem action='Open'/>"
239 " <menuitem action='Save'/>"
240 " <menuitem action='SaveAs'/>"
242 " <menuitem action='Quit'/>"
244 " <menu action='PreferencesMenu'>"
245 " <menu action='ColorMenu'>"
246 " <menuitem action='Red'/>"
247 " <menuitem action='Green'/>"
248 " <menuitem action='Blue'/>"
250 " <menu action='ShapeMenu'>"
251 " <menuitem action='Square'/>"
252 " <menuitem action='Rectangle'/>"
253 " <menuitem action='Oval'/>"
255 " <menuitem action='Bold'/>"
257 " <menu action='HelpMenu'>"
258 " <menuitem action='About'/>"
261 " <toolbar name='ToolBar'>"
262 " <toolitem action='Open'/>"
263 " <toolitem action='Quit'/>"
264 " <separator action='Sep1'/>"
265 " <toolitem action='Logo'/>"
271 /* This function registers our custom toolbar icons, so they can be themed.
273 * It's totally optional to do this, you could just manually insert icons
274 * and have them not be themeable, especially if you never expect people
278 register_stock_icons (void)
280 static gboolean registered = FALSE;
285 GtkIconFactory *factory;
288 static GtkStockItem items[] = {
296 /* Register our stock items */
297 gtk_stock_add (items, G_N_ELEMENTS (items));
299 /* Add our custom icon factory to the list of defaults */
300 factory = gtk_icon_factory_new ();
301 gtk_icon_factory_add_default (factory);
303 /* demo_find_file() looks in the the current directory first,
304 * so you can run gtk-demo without installing GTK, then looks
305 * in the location where the file is installed.
308 filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
311 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
315 /* Register icon to accompany stock item */
318 GtkIconSet *icon_set;
319 GdkPixbuf *transparent;
321 /* The gtk-logo-rgb icon has a white background, make it transparent */
322 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
324 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
325 gtk_icon_factory_add (factory, "demo-gtk-logo", icon_set);
326 gtk_icon_set_unref (icon_set);
327 g_object_unref (pixbuf);
328 g_object_unref (transparent);
331 g_warning ("failed to load GTK logo for toolbar");
333 /* Drop our reference to the factory, GTK will hold a reference. */
334 g_object_unref (factory);
339 update_statusbar (GtkTextBuffer *buffer,
340 GtkStatusbar *statusbar)
347 gtk_statusbar_pop (statusbar, 0); /* clear any previous message, underflow is allowed */
349 count = gtk_text_buffer_get_char_count (buffer);
351 gtk_text_buffer_get_iter_at_mark (buffer,
353 gtk_text_buffer_get_insert (buffer));
355 row = gtk_text_iter_get_line (&iter);
356 col = gtk_text_iter_get_line_offset (&iter);
358 msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
361 gtk_statusbar_push (statusbar, 0, msg);
367 mark_set_callback (GtkTextBuffer *buffer,
368 const GtkTextIter *new_location,
372 update_statusbar (buffer, GTK_STATUSBAR (data));
376 update_resize_grip (GtkWidget *widget,
377 GdkEventWindowState *event,
378 GtkStatusbar *statusbar)
380 if (event->changed_mask & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_FULLSCREEN))
381 gtk_statusbar_set_has_resize_grip (statusbar, !(event->new_window_state & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_FULLSCREEN)));
386 do_appwindow (GtkWidget *do_widget)
391 GtkWidget *statusbar;
395 GtkTextBuffer *buffer;
396 GtkActionGroup *action_group;
398 GError *error = NULL;
400 register_stock_icons ();
402 /* Create the toplevel window
405 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
406 gtk_window_set_screen (GTK_WINDOW (window),
407 gtk_widget_get_screen (do_widget));
408 gtk_window_set_title (GTK_WINDOW (window), "Application Window");
410 /* NULL window variable when window is closed */
411 g_signal_connect (window, "destroy",
412 G_CALLBACK (gtk_widget_destroyed),
415 table = gtk_table_new (1, 4, FALSE);
417 gtk_container_add (GTK_CONTAINER (window), table);
419 /* Create the menubar and toolbar
422 action_group = gtk_action_group_new ("AppWindowActions");
423 gtk_action_group_add_actions (action_group,
426 gtk_action_group_add_toggle_actions (action_group,
427 toggle_entries, n_toggle_entries,
429 gtk_action_group_add_radio_actions (action_group,
430 color_entries, n_color_entries,
432 G_CALLBACK (activate_radio_action),
434 gtk_action_group_add_radio_actions (action_group,
435 shape_entries, n_shape_entries,
437 G_CALLBACK (activate_radio_action),
440 merge = gtk_ui_manager_new ();
441 g_object_set_data_full (G_OBJECT (window), "ui-manager", merge, g_object_unref);
442 gtk_ui_manager_insert_action_group (merge, action_group, 0);
443 gtk_window_add_accel_group (GTK_WINDOW (window),
444 gtk_ui_manager_get_accel_group (merge));
446 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
448 g_message ("building menus failed: %s", error->message);
449 g_error_free (error);
452 bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
453 gtk_widget_show (bar);
454 gtk_table_attach (GTK_TABLE (table),
456 /* X direction */ /* Y direction */
458 GTK_EXPAND | GTK_FILL, 0,
461 bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
462 gtk_toolbar_set_tooltips (GTK_TOOLBAR (bar), TRUE);
463 gtk_widget_show (bar);
464 gtk_table_attach (GTK_TABLE (table),
466 /* X direction */ /* Y direction */
468 GTK_EXPAND | GTK_FILL, 0,
474 sw = gtk_scrolled_window_new (NULL, NULL);
476 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
477 GTK_POLICY_AUTOMATIC,
478 GTK_POLICY_AUTOMATIC);
480 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
483 gtk_table_attach (GTK_TABLE (table),
485 /* X direction */ /* Y direction */
487 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
490 gtk_window_set_default_size (GTK_WINDOW (window),
493 contents = gtk_text_view_new ();
494 gtk_widget_grab_focus (contents);
496 gtk_container_add (GTK_CONTAINER (sw),
499 /* Create statusbar */
501 statusbar = gtk_statusbar_new ();
502 gtk_table_attach (GTK_TABLE (table),
504 /* X direction */ /* Y direction */
506 GTK_EXPAND | GTK_FILL, 0,
509 /* Show text widget info in the statusbar */
510 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
512 g_signal_connect_object (buffer,
514 G_CALLBACK (update_statusbar),
518 g_signal_connect_object (buffer,
519 "mark_set", /* cursor moved */
520 G_CALLBACK (mark_set_callback),
524 g_signal_connect_object (window,
525 "window_state_event",
526 G_CALLBACK (update_resize_grip),
530 update_statusbar (buffer, GTK_STATUSBAR (statusbar));
533 if (!GTK_WIDGET_VISIBLE (window))
535 gtk_widget_show_all (window);
539 gtk_widget_destroy (window);