3 * Demonstrates a typical application window with menubar, toolbar, statusbar.
5 * This example uses GtkUIManager and GtkActionGroup.
11 static GtkWidget *window = NULL;
12 static GtkWidget *infobar = NULL;
13 static GtkWidget *messagelabel = NULL;
16 activate_action (GtkAction *action)
18 const gchar *name = gtk_action_get_name (action);
19 const gchar *typename = G_OBJECT_TYPE_NAME (action);
23 if (g_str_equal (name, "DarkTheme"))
25 gboolean value = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
26 GtkSettings *settings = gtk_settings_get_default ();
28 g_object_set (G_OBJECT (settings),
29 "gtk-application-prefer-dark-theme", value,
34 if (g_str_equal (name, "HideTitlebar"))
36 gboolean value = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
37 gtk_window_set_hide_titlebar_when_maximized (GTK_WINDOW (window), value);
41 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
42 GTK_DIALOG_DESTROY_WITH_PARENT,
45 "You activated action: \"%s\" of type \"%s\"",
48 /* Close dialog on user response */
49 g_signal_connect (dialog,
51 G_CALLBACK (gtk_widget_destroy),
54 gtk_widget_show (dialog);
58 activate_radio_action (GtkAction *action, GtkRadioAction *current)
60 const gchar *name = gtk_action_get_name (GTK_ACTION (current));
61 const gchar *typename = G_OBJECT_TYPE_NAME (GTK_ACTION (current));
62 gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (current));
63 gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
69 text = g_strdup_printf ("You activated radio action: \"%s\" of type \"%s\".\n"
71 name, typename, value);
72 gtk_label_set_text (GTK_LABEL (messagelabel), text);
73 gtk_info_bar_set_message_type (GTK_INFO_BAR (infobar), (GtkMessageType)value);
74 gtk_widget_show (infobar);
80 about_cb (GtkAction *action,
83 GdkPixbuf *pixbuf, *transparent;
85 const gchar *authors[] = {
93 const gchar *documentors[] = {
96 "Matthias Clasen <mclasen@redhat.com>",
101 pixbuf = gdk_pixbuf_new_from_resource ("/appwindow/gtk-logo-rgb.gif", NULL);
102 /* We asser the existence of the pixbuf as we load it from a custom resource. */
104 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
105 g_object_unref (pixbuf);
107 gtk_show_about_dialog (GTK_WINDOW (window),
108 "program-name", "GTK+ Code Demos",
109 "version", g_strdup_printf ("%s,\nRunning against GTK+ %d.%d.%d",
111 gtk_get_major_version (),
112 gtk_get_minor_version (),
113 gtk_get_micro_version ()),
114 "copyright", "(C) 1997-2009 The GTK+ Team",
115 "license-type", GTK_LICENSE_LGPL_2_1,
116 "website", "http://www.gtk.org",
117 "comments", "Program to demonstrate GTK+ functions.",
119 "documenters", documentors,
121 "title", "About GTK+ Code Demos",
124 g_object_unref (transparent);
134 GtkActionClass parent_class;
135 } ToolMenuActionClass;
137 G_DEFINE_TYPE(ToolMenuAction, tool_menu_action, GTK_TYPE_ACTION)
140 tool_menu_action_class_init (ToolMenuActionClass *class)
142 GTK_ACTION_CLASS (class)->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
146 tool_menu_action_init (ToolMenuAction *action)
150 static GtkActionEntry entries[] = {
151 { "FileMenu", NULL, "_File" }, /* name, stock id, label */
152 { "OpenMenu", NULL, "_Open" }, /* name, stock id, label */
153 { "PreferencesMenu", NULL, "_Preferences" }, /* name, stock id, label */
154 { "ColorMenu", NULL, "_Color" }, /* name, stock id, label */
155 { "ShapeMenu", NULL, "_Shape" }, /* name, stock id, label */
156 { "HelpMenu", NULL, "_Help" }, /* name, stock id, label */
157 { "New", GTK_STOCK_NEW, /* name, stock id */
158 "_New", "<control>N", /* label, accelerator */
159 "Create a new file", /* tooltip */
160 G_CALLBACK (activate_action) },
161 { "File1", NULL, /* name, stock id */
162 "File1", NULL, /* label, accelerator */
163 "Open first file", /* tooltip */
164 G_CALLBACK (activate_action) },
165 { "Save", GTK_STOCK_SAVE, /* name, stock id */
166 "_Save","<control>S", /* label, accelerator */
167 "Save current file", /* tooltip */
168 G_CALLBACK (activate_action) },
169 { "SaveAs", GTK_STOCK_SAVE, /* name, stock id */
170 "Save _As...", NULL, /* label, accelerator */
171 "Save to a file", /* tooltip */
172 G_CALLBACK (activate_action) },
173 { "Quit", GTK_STOCK_QUIT, /* name, stock id */
174 "_Quit", "<control>Q", /* label, accelerator */
175 "Quit", /* tooltip */
176 G_CALLBACK (activate_action) },
177 { "About", NULL, /* name, stock id */
178 "_About", "<control>A", /* label, accelerator */
179 "About", /* tooltip */
180 G_CALLBACK (about_cb) },
181 { "Logo", "demo-gtk-logo", /* name, stock id */
182 NULL, NULL, /* label, accelerator */
183 "GTK+", /* tooltip */
184 G_CALLBACK (activate_action) },
186 static guint n_entries = G_N_ELEMENTS (entries);
189 static GtkToggleActionEntry toggle_entries[] = {
190 { "Bold", GTK_STOCK_BOLD, /* name, stock id */
191 "_Bold", "<control>B", /* label, accelerator */
192 "Bold", /* tooltip */
193 G_CALLBACK (activate_action),
194 TRUE }, /* is_active */
195 { "DarkTheme", NULL, /* name, stock id */
196 "_Prefer Dark Theme", NULL, /* label, accelerator */
197 "Prefer Dark Theme", /* tooltip */
198 G_CALLBACK (activate_action),
199 FALSE }, /* is_active */
200 { "HideTitlebar", NULL,
201 "_Hide Titlebar when maximized", NULL,
202 "Hide Titlebar when maximized",
203 G_CALLBACK (activate_action),
206 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
214 static GtkRadioActionEntry color_entries[] = {
215 { "Red", NULL, /* name, stock id */
216 "_Red", "<control>R", /* label, accelerator */
217 "Blood", COLOR_RED }, /* tooltip, value */
218 { "Green", NULL, /* name, stock id */
219 "_Green", "<control>G", /* label, accelerator */
220 "Grass", COLOR_GREEN }, /* tooltip, value */
221 { "Blue", NULL, /* name, stock id */
222 "_Blue", "<control>B", /* label, accelerator */
223 "Sky", COLOR_BLUE }, /* tooltip, value */
225 static guint n_color_entries = G_N_ELEMENTS (color_entries);
233 static GtkRadioActionEntry shape_entries[] = {
234 { "Square", NULL, /* name, stock id */
235 "_Square", "<control>S", /* label, accelerator */
236 "Square", SHAPE_SQUARE }, /* tooltip, value */
237 { "Rectangle", NULL, /* name, stock id */
238 "_Rectangle", "<control>R", /* label, accelerator */
239 "Rectangle", SHAPE_RECTANGLE }, /* tooltip, value */
240 { "Oval", NULL, /* name, stock id */
241 "_Oval", "<control>O", /* label, accelerator */
242 "Egg", SHAPE_OVAL }, /* tooltip, value */
244 static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
246 static const gchar *ui_info =
248 " <menubar name='MenuBar'>"
249 " <menu action='FileMenu'>"
250 " <menuitem action='New'/>"
251 " <menuitem action='Open'/>"
252 " <menuitem action='Save'/>"
253 " <menuitem action='SaveAs'/>"
255 " <menuitem action='Quit'/>"
257 " <menu action='PreferencesMenu'>"
258 " <menuitem action='DarkTheme'/>"
259 " <menuitem action='HideTitlebar'/>"
260 " <menu action='ColorMenu'>"
261 " <menuitem action='Red'/>"
262 " <menuitem action='Green'/>"
263 " <menuitem action='Blue'/>"
265 " <menu action='ShapeMenu'>"
266 " <menuitem action='Square'/>"
267 " <menuitem action='Rectangle'/>"
268 " <menuitem action='Oval'/>"
270 " <menuitem action='Bold'/>"
272 " <menu action='HelpMenu'>"
273 " <menuitem action='About'/>"
276 " <toolbar name='ToolBar'>"
277 " <toolitem action='Open'>"
278 " <menu action='OpenMenu'>"
279 " <menuitem action='File1'/>"
282 " <toolitem action='Quit'/>"
283 " <separator action='Sep1'/>"
284 " <toolitem action='Logo'/>"
290 /* This function registers our custom toolbar icons, so they can be themed.
292 * It's totally optional to do this, you could just manually insert icons
293 * and have them not be themeable, especially if you never expect people
297 register_stock_icons (void)
299 static gboolean registered = FALSE;
304 GtkIconFactory *factory;
305 GtkIconSet *icon_set;
306 GdkPixbuf *transparent;
308 static GtkStockItem items[] = {
316 /* Register our stock items */
317 gtk_stock_add (items, G_N_ELEMENTS (items));
319 /* Add our custom icon factory to the list of defaults */
320 factory = gtk_icon_factory_new ();
321 gtk_icon_factory_add_default (factory);
323 pixbuf = gdk_pixbuf_new_from_resource ("/appwindow/gtk-logo-rgb.gif", NULL);
324 /* We assert the existence of the pixbuf as we load it from a custom resource. */
327 /* The gtk-logo-rgb icon has a white background, make it transparent */
328 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
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);
336 /* Drop our reference to the factory, GTK will hold a reference. */
337 g_object_unref (factory);
342 update_statusbar (GtkTextBuffer *buffer,
343 GtkStatusbar *statusbar)
350 gtk_statusbar_pop (statusbar, 0); /* clear any previous message,
351 * underflow is allowed
354 count = gtk_text_buffer_get_char_count (buffer);
356 gtk_text_buffer_get_iter_at_mark (buffer,
358 gtk_text_buffer_get_insert (buffer));
360 row = gtk_text_iter_get_line (&iter);
361 col = gtk_text_iter_get_line_offset (&iter);
363 msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
366 gtk_statusbar_push (statusbar, 0, msg);
372 mark_set_callback (GtkTextBuffer *buffer,
373 const GtkTextIter *new_location,
377 update_statusbar (buffer, GTK_STATUSBAR (data));
381 do_appwindow (GtkWidget *do_widget)
386 GtkWidget *statusbar;
390 GtkTextBuffer *buffer;
391 GtkActionGroup *action_group;
392 GtkAction *open_action;
394 GError *error = NULL;
396 register_stock_icons ();
398 /* Create the toplevel window
401 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
402 gtk_window_set_screen (GTK_WINDOW (window),
403 gtk_widget_get_screen (do_widget));
404 gtk_window_set_title (GTK_WINDOW (window), "Application Window");
405 gtk_window_set_icon_name (GTK_WINDOW (window), "document-open");
407 /* NULL window variable when window is closed */
408 g_signal_connect (window, "destroy",
409 G_CALLBACK (gtk_widget_destroyed),
412 table = gtk_grid_new ();
414 gtk_container_add (GTK_CONTAINER (window), table);
416 /* Create the menubar and toolbar
419 action_group = gtk_action_group_new ("AppWindowActions");
420 open_action = g_object_new (tool_menu_action_get_type (),
423 "tooltip", "Open a file",
424 "stock-id", GTK_STOCK_OPEN,
426 gtk_action_group_add_action (action_group, open_action);
427 g_object_unref (open_action);
428 gtk_action_group_add_actions (action_group,
431 gtk_action_group_add_toggle_actions (action_group,
432 toggle_entries, n_toggle_entries,
434 gtk_action_group_add_radio_actions (action_group,
435 color_entries, n_color_entries,
437 G_CALLBACK (activate_radio_action),
439 gtk_action_group_add_radio_actions (action_group,
440 shape_entries, n_shape_entries,
442 G_CALLBACK (activate_radio_action),
445 merge = gtk_ui_manager_new ();
446 g_object_set_data_full (G_OBJECT (window), "ui-manager", merge,
448 gtk_ui_manager_insert_action_group (merge, action_group, 0);
449 gtk_window_add_accel_group (GTK_WINDOW (window),
450 gtk_ui_manager_get_accel_group (merge));
452 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
454 g_message ("building menus failed: %s", error->message);
455 g_error_free (error);
458 bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
459 gtk_widget_show (bar);
460 gtk_widget_set_halign (bar, GTK_ALIGN_FILL);
461 gtk_grid_attach (GTK_GRID (table), bar, 0, 0, 1, 1);
463 bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
464 gtk_widget_show (bar);
465 gtk_widget_set_halign (bar, GTK_ALIGN_FILL);
466 gtk_grid_attach (GTK_GRID (table), bar, 0, 1, 1, 1);
471 infobar = gtk_info_bar_new ();
472 gtk_widget_set_no_show_all (infobar, TRUE);
473 messagelabel = gtk_label_new ("");
474 gtk_widget_show (messagelabel);
475 gtk_box_pack_start (GTK_BOX (gtk_info_bar_get_content_area (GTK_INFO_BAR (infobar))),
478 gtk_info_bar_add_button (GTK_INFO_BAR (infobar),
479 GTK_STOCK_OK, GTK_RESPONSE_OK);
480 g_signal_connect (infobar, "response",
481 G_CALLBACK (gtk_widget_hide), NULL);
483 gtk_widget_set_halign (infobar, GTK_ALIGN_FILL);
484 gtk_grid_attach (GTK_GRID (table), infobar, 0, 2, 1, 1);
486 sw = gtk_scrolled_window_new (NULL, NULL);
488 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
489 GTK_POLICY_AUTOMATIC,
490 GTK_POLICY_AUTOMATIC);
492 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
495 gtk_widget_set_halign (sw, GTK_ALIGN_FILL);
496 gtk_widget_set_valign (sw, GTK_ALIGN_FILL);
497 gtk_widget_set_hexpand (sw, TRUE);
498 gtk_widget_set_vexpand (sw, TRUE);
499 gtk_grid_attach (GTK_GRID (table), sw, 0, 3, 1, 1);
501 gtk_window_set_default_size (GTK_WINDOW (window),
504 contents = gtk_text_view_new ();
505 gtk_widget_grab_focus (contents);
507 gtk_container_add (GTK_CONTAINER (sw),
510 /* Create statusbar */
512 statusbar = gtk_statusbar_new ();
513 gtk_widget_set_halign (sw, GTK_ALIGN_FILL);
514 gtk_grid_attach (GTK_GRID (table), statusbar, 0, 4, 1, 1);
516 /* Show text widget info in the statusbar */
517 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
519 g_signal_connect_object (buffer,
521 G_CALLBACK (update_statusbar),
525 g_signal_connect_object (buffer,
526 "mark_set", /* cursor moved */
527 G_CALLBACK (mark_set_callback),
531 update_statusbar (buffer, GTK_STATUSBAR (statusbar));
534 if (!gtk_widget_get_visible (window))
536 gtk_widget_show_all (window);
540 gtk_widget_destroy (window);