3 * Demonstrates a typical application window with menubar, toolbar, statusbar.
5 * This example uses GtkUIManager and GtkActionGroup.
10 #include "demo-common.h"
12 static GtkWidget *window = NULL;
13 static GtkWidget *infobar = NULL;
14 static GtkWidget *messagelabel = NULL;
17 activate_action (GtkAction *action)
19 const gchar *name = gtk_action_get_name (action);
20 const gchar *typename = G_OBJECT_TYPE_NAME (action);
24 if (g_str_equal (name, "DarkTheme"))
26 gboolean value = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
27 GtkSettings *settings = gtk_settings_get_default ();
29 g_object_set (G_OBJECT (settings),
30 "gtk-application-prefer-dark-theme", value,
35 if (g_str_equal (name, "HideTitlebar"))
37 gboolean value = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
38 gtk_window_set_hide_titlebar_when_maximized (GTK_WINDOW (window), value);
42 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
43 GTK_DIALOG_DESTROY_WITH_PARENT,
46 "You activated action: \"%s\" of type \"%s\"",
49 /* Close dialog on user response */
50 g_signal_connect (dialog,
52 G_CALLBACK (gtk_widget_destroy),
55 gtk_widget_show (dialog);
59 activate_radio_action (GtkAction *action, GtkRadioAction *current)
61 const gchar *name = gtk_action_get_name (GTK_ACTION (current));
62 const gchar *typename = G_OBJECT_TYPE_NAME (GTK_ACTION (current));
63 gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (current));
64 gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
70 text = g_strdup_printf ("You activated radio action: \"%s\" of type \"%s\".\n"
72 name, typename, value);
73 gtk_label_set_text (GTK_LABEL (messagelabel), text);
74 gtk_info_bar_set_message_type (GTK_INFO_BAR (infobar), (GtkMessageType)value);
75 gtk_widget_show (infobar);
81 about_cb (GtkAction *action,
84 GdkPixbuf *pixbuf, *transparent;
87 const gchar *authors[] = {
95 const gchar *documentors[] = {
98 "Matthias Clasen <mclasen@redhat.com>",
105 filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
108 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
110 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
111 g_object_unref (pixbuf);
114 gtk_show_about_dialog (GTK_WINDOW (window),
115 "program-name", "GTK+ Code Demos",
116 "version", g_strdup_printf ("%s,\nRunning against GTK+ %d.%d.%d",
118 gtk_get_major_version (),
119 gtk_get_minor_version (),
120 gtk_get_micro_version ()),
121 "copyright", "(C) 1997-2009 The GTK+ Team",
122 "license-type", GTK_LICENSE_LGPL_2_1,
123 "website", "http://www.gtk.org",
124 "comments", "Program to demonstrate GTK+ functions.",
126 "documenters", documentors,
128 "title", "About GTK+ Code Demos",
131 g_object_unref (transparent);
141 GtkActionClass parent_class;
142 } ToolMenuActionClass;
144 G_DEFINE_TYPE(ToolMenuAction, tool_menu_action, GTK_TYPE_ACTION)
147 tool_menu_action_class_init (ToolMenuActionClass *class)
149 GTK_ACTION_CLASS (class)->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
153 tool_menu_action_init (ToolMenuAction *action)
157 static GtkActionEntry entries[] = {
158 { "FileMenu", NULL, "_File" }, /* name, stock id, label */
159 { "OpenMenu", NULL, "_Open" }, /* name, stock id, label */
160 { "PreferencesMenu", NULL, "_Preferences" }, /* name, stock id, label */
161 { "ColorMenu", NULL, "_Color" }, /* name, stock id, label */
162 { "ShapeMenu", NULL, "_Shape" }, /* name, stock id, label */
163 { "HelpMenu", NULL, "_Help" }, /* name, stock id, label */
164 { "New", GTK_STOCK_NEW, /* name, stock id */
165 "_New", "<control>N", /* label, accelerator */
166 "Create a new file", /* tooltip */
167 G_CALLBACK (activate_action) },
168 { "File1", NULL, /* name, stock id */
169 "File1", NULL, /* label, accelerator */
170 "Open first file", /* tooltip */
171 G_CALLBACK (activate_action) },
172 { "Save", GTK_STOCK_SAVE, /* name, stock id */
173 "_Save","<control>S", /* label, accelerator */
174 "Save current file", /* tooltip */
175 G_CALLBACK (activate_action) },
176 { "SaveAs", GTK_STOCK_SAVE, /* name, stock id */
177 "Save _As...", NULL, /* label, accelerator */
178 "Save to a file", /* tooltip */
179 G_CALLBACK (activate_action) },
180 { "Quit", GTK_STOCK_QUIT, /* name, stock id */
181 "_Quit", "<control>Q", /* label, accelerator */
182 "Quit", /* tooltip */
183 G_CALLBACK (activate_action) },
184 { "About", NULL, /* name, stock id */
185 "_About", "<control>A", /* label, accelerator */
186 "About", /* tooltip */
187 G_CALLBACK (about_cb) },
188 { "Logo", "demo-gtk-logo", /* name, stock id */
189 NULL, NULL, /* label, accelerator */
190 "GTK+", /* tooltip */
191 G_CALLBACK (activate_action) },
193 static guint n_entries = G_N_ELEMENTS (entries);
196 static GtkToggleActionEntry toggle_entries[] = {
197 { "Bold", GTK_STOCK_BOLD, /* name, stock id */
198 "_Bold", "<control>B", /* label, accelerator */
199 "Bold", /* tooltip */
200 G_CALLBACK (activate_action),
201 TRUE }, /* is_active */
202 { "DarkTheme", NULL, /* name, stock id */
203 "_Prefer Dark Theme", NULL, /* label, accelerator */
204 "Prefer Dark Theme", /* tooltip */
205 G_CALLBACK (activate_action),
206 FALSE }, /* is_active */
207 { "HideTitlebar", NULL,
208 "_Hide Titlebar when maximized", NULL,
209 "Hide Titlebar when maximized",
210 G_CALLBACK (activate_action),
213 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
221 static GtkRadioActionEntry color_entries[] = {
222 { "Red", NULL, /* name, stock id */
223 "_Red", "<control>R", /* label, accelerator */
224 "Blood", COLOR_RED }, /* tooltip, value */
225 { "Green", NULL, /* name, stock id */
226 "_Green", "<control>G", /* label, accelerator */
227 "Grass", COLOR_GREEN }, /* tooltip, value */
228 { "Blue", NULL, /* name, stock id */
229 "_Blue", "<control>B", /* label, accelerator */
230 "Sky", COLOR_BLUE }, /* tooltip, value */
232 static guint n_color_entries = G_N_ELEMENTS (color_entries);
240 static GtkRadioActionEntry shape_entries[] = {
241 { "Square", NULL, /* name, stock id */
242 "_Square", "<control>S", /* label, accelerator */
243 "Square", SHAPE_SQUARE }, /* tooltip, value */
244 { "Rectangle", NULL, /* name, stock id */
245 "_Rectangle", "<control>R", /* label, accelerator */
246 "Rectangle", SHAPE_RECTANGLE }, /* tooltip, value */
247 { "Oval", NULL, /* name, stock id */
248 "_Oval", "<control>O", /* label, accelerator */
249 "Egg", SHAPE_OVAL }, /* tooltip, value */
251 static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
253 static const gchar *ui_info =
255 " <menubar name='MenuBar'>"
256 " <menu action='FileMenu'>"
257 " <menuitem action='New'/>"
258 " <menuitem action='Open'/>"
259 " <menuitem action='Save'/>"
260 " <menuitem action='SaveAs'/>"
262 " <menuitem action='Quit'/>"
264 " <menu action='PreferencesMenu'>"
265 " <menuitem action='DarkTheme'/>"
266 " <menuitem action='HideTitlebar'/>"
267 " <menu action='ColorMenu'>"
268 " <menuitem action='Red'/>"
269 " <menuitem action='Green'/>"
270 " <menuitem action='Blue'/>"
272 " <menu action='ShapeMenu'>"
273 " <menuitem action='Square'/>"
274 " <menuitem action='Rectangle'/>"
275 " <menuitem action='Oval'/>"
277 " <menuitem action='Bold'/>"
279 " <menu action='HelpMenu'>"
280 " <menuitem action='About'/>"
283 " <toolbar name='ToolBar'>"
284 " <toolitem action='Open'>"
285 " <menu action='OpenMenu'>"
286 " <menuitem action='File1'/>"
289 " <toolitem action='Quit'/>"
290 " <separator action='Sep1'/>"
291 " <toolitem action='Logo'/>"
297 /* This function registers our custom toolbar icons, so they can be themed.
299 * It's totally optional to do this, you could just manually insert icons
300 * and have them not be themeable, especially if you never expect people
304 register_stock_icons (void)
306 static gboolean registered = FALSE;
311 GtkIconFactory *factory;
314 static GtkStockItem items[] = {
322 /* Register our stock items */
323 gtk_stock_add (items, G_N_ELEMENTS (items));
325 /* Add our custom icon factory to the list of defaults */
326 factory = gtk_icon_factory_new ();
327 gtk_icon_factory_add_default (factory);
329 /* demo_find_file() looks in the current directory first,
330 * so you can run gtk-demo without installing GTK, then looks
331 * in the location where the file is installed.
334 filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
337 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
341 /* Register icon to accompany stock item */
344 GtkIconSet *icon_set;
345 GdkPixbuf *transparent;
347 /* The gtk-logo-rgb icon has a white background, make it transparent */
348 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
350 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
351 gtk_icon_factory_add (factory, "demo-gtk-logo", icon_set);
352 gtk_icon_set_unref (icon_set);
353 g_object_unref (pixbuf);
354 g_object_unref (transparent);
357 g_warning ("failed to load GTK logo for toolbar");
359 /* Drop our reference to the factory, GTK will hold a reference. */
360 g_object_unref (factory);
365 update_statusbar (GtkTextBuffer *buffer,
366 GtkStatusbar *statusbar)
373 gtk_statusbar_pop (statusbar, 0); /* clear any previous message,
374 * underflow is allowed
377 count = gtk_text_buffer_get_char_count (buffer);
379 gtk_text_buffer_get_iter_at_mark (buffer,
381 gtk_text_buffer_get_insert (buffer));
383 row = gtk_text_iter_get_line (&iter);
384 col = gtk_text_iter_get_line_offset (&iter);
386 msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
389 gtk_statusbar_push (statusbar, 0, msg);
395 mark_set_callback (GtkTextBuffer *buffer,
396 const GtkTextIter *new_location,
400 update_statusbar (buffer, GTK_STATUSBAR (data));
404 do_appwindow (GtkWidget *do_widget)
409 GtkWidget *statusbar;
413 GtkTextBuffer *buffer;
414 GtkActionGroup *action_group;
415 GtkAction *open_action;
417 GError *error = NULL;
419 register_stock_icons ();
421 /* Create the toplevel window
424 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
425 gtk_window_set_screen (GTK_WINDOW (window),
426 gtk_widget_get_screen (do_widget));
427 gtk_window_set_title (GTK_WINDOW (window), "Application Window");
428 gtk_window_set_icon_name (GTK_WINDOW (window), "document-open");
430 /* NULL window variable when window is closed */
431 g_signal_connect (window, "destroy",
432 G_CALLBACK (gtk_widget_destroyed),
435 table = gtk_grid_new ();
437 gtk_container_add (GTK_CONTAINER (window), table);
439 /* Create the menubar and toolbar
442 action_group = gtk_action_group_new ("AppWindowActions");
443 open_action = g_object_new (tool_menu_action_get_type (),
446 "tooltip", "Open a file",
447 "stock-id", GTK_STOCK_OPEN,
449 gtk_action_group_add_action (action_group, open_action);
450 g_object_unref (open_action);
451 gtk_action_group_add_actions (action_group,
454 gtk_action_group_add_toggle_actions (action_group,
455 toggle_entries, n_toggle_entries,
457 gtk_action_group_add_radio_actions (action_group,
458 color_entries, n_color_entries,
460 G_CALLBACK (activate_radio_action),
462 gtk_action_group_add_radio_actions (action_group,
463 shape_entries, n_shape_entries,
465 G_CALLBACK (activate_radio_action),
468 merge = gtk_ui_manager_new ();
469 g_object_set_data_full (G_OBJECT (window), "ui-manager", merge,
471 gtk_ui_manager_insert_action_group (merge, action_group, 0);
472 gtk_window_add_accel_group (GTK_WINDOW (window),
473 gtk_ui_manager_get_accel_group (merge));
475 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
477 g_message ("building menus failed: %s", error->message);
478 g_error_free (error);
481 bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
482 gtk_widget_show (bar);
483 gtk_widget_set_halign (bar, GTK_ALIGN_FILL);
484 gtk_grid_attach (GTK_GRID (table), bar, 0, 0, 1, 1);
486 bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
487 gtk_widget_show (bar);
488 gtk_widget_set_halign (bar, GTK_ALIGN_FILL);
489 gtk_grid_attach (GTK_GRID (table), bar, 0, 1, 1, 1);
494 infobar = gtk_info_bar_new ();
495 gtk_widget_set_no_show_all (infobar, TRUE);
496 messagelabel = gtk_label_new ("");
497 gtk_widget_show (messagelabel);
498 gtk_box_pack_start (GTK_BOX (gtk_info_bar_get_content_area (GTK_INFO_BAR (infobar))),
501 gtk_info_bar_add_button (GTK_INFO_BAR (infobar),
502 GTK_STOCK_OK, GTK_RESPONSE_OK);
503 g_signal_connect (infobar, "response",
504 G_CALLBACK (gtk_widget_hide), NULL);
506 gtk_widget_set_halign (infobar, GTK_ALIGN_FILL);
507 gtk_grid_attach (GTK_GRID (table), infobar, 0, 2, 1, 1);
509 sw = gtk_scrolled_window_new (NULL, NULL);
511 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
512 GTK_POLICY_AUTOMATIC,
513 GTK_POLICY_AUTOMATIC);
515 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
518 gtk_widget_set_halign (sw, GTK_ALIGN_FILL);
519 gtk_widget_set_valign (sw, GTK_ALIGN_FILL);
520 gtk_widget_set_hexpand (sw, TRUE);
521 gtk_widget_set_vexpand (sw, TRUE);
522 gtk_grid_attach (GTK_GRID (table), sw, 0, 3, 1, 1);
524 gtk_window_set_default_size (GTK_WINDOW (window),
527 contents = gtk_text_view_new ();
528 gtk_widget_grab_focus (contents);
530 gtk_container_add (GTK_CONTAINER (sw),
533 /* Create statusbar */
535 statusbar = gtk_statusbar_new ();
536 gtk_widget_set_halign (sw, GTK_ALIGN_FILL);
537 gtk_grid_attach (GTK_GRID (table), statusbar, 0, 4, 1, 1);
539 /* Show text widget info in the statusbar */
540 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
542 g_signal_connect_object (buffer,
544 G_CALLBACK (update_statusbar),
548 g_signal_connect_object (buffer,
549 "mark_set", /* cursor moved */
550 G_CALLBACK (mark_set_callback),
554 update_statusbar (buffer, GTK_STATUSBAR (statusbar));
557 if (!gtk_widget_get_visible (window))
559 gtk_widget_show_all (window);
563 gtk_widget_destroy (window);