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
about_cb (GtkAction *action,
64
GdkPixbuf *pixbuf, *transparent;
67
const gchar *authors[] = {
75
const gchar *documentors[] = {
78
"Matthias Clasen <mclasen@redhat.com>",
83
const gchar *license =
84
"This library is free software; you can redistribute it and/or\n"
85
"modify it under the terms of the GNU Library General Public License as\n"
86
"published by the Free Software Foundation; either version 2 of the\n"
87
"License, or (at your option) any later version.\n"
89
"This library is distributed in the hope that it will be useful,\n"
90
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
91
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
92
"Library General Public License for more details.\n"
94
"You should have received a copy of the GNU Library General Public\n"
95
"License along with the Gnome Library; see the file COPYING.LIB. If not,\n"
96
"write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
97
"Boston, MA 02111-1307, USA.\n";
101
filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
104
pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
106
transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
107
g_object_unref (pixbuf);
110
gtk_show_about_dialog (GTK_WINDOW (window),
111
"program-name", "GTK+ Code Demos",
112
"version", PACKAGE_VERSION,
113
"copyright", "(C) 1997-2009 The GTK+ Team",
115
"website", "http://www.gtk.org",
116
"comments", "Program to demonstrate GTK+ functions.",
118
"documenters", documentors,
120
"title", "About GTK+ Code Demos",
123
g_object_unref (transparent);
133
GtkActionClass parent_class;
134
} ToolMenuActionClass;
136
G_DEFINE_TYPE(ToolMenuAction, tool_menu_action, GTK_TYPE_ACTION)
139
tool_menu_action_class_init (ToolMenuActionClass *class)
141
GTK_ACTION_CLASS (class)->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
145
tool_menu_action_init (ToolMenuAction *action)
149
static GtkActionEntry entries[] = {
150
{ "FileMenu", NULL, "_File" }, /* name, stock id, label */
151
{ "OpenMenu", NULL, "_Open" }, /* name, stock id, label */
152
{ "PreferencesMenu", NULL, "_Preferences" }, /* name, stock id, label */
153
{ "ColorMenu", NULL, "_Color" }, /* name, stock id, label */
154
{ "ShapeMenu", NULL, "_Shape" }, /* name, stock id, label */
155
{ "HelpMenu", NULL, "_Help" }, /* name, stock id, label */
156
{ "New", GTK_STOCK_NEW, /* name, stock id */
157
"_New", "<control>N", /* label, accelerator */
158
"Create a new file", /* tooltip */
159
G_CALLBACK (activate_action) },
160
{ "File1", NULL, /* name, stock id */
161
"File1", NULL, /* label, accelerator */
162
"Open first file", /* tooltip */
163
G_CALLBACK (activate_action) },
164
{ "Save", GTK_STOCK_SAVE, /* name, stock id */
165
"_Save","<control>S", /* label, accelerator */
166
"Save current file", /* tooltip */
167
G_CALLBACK (activate_action) },
168
{ "SaveAs", GTK_STOCK_SAVE, /* name, stock id */
169
"Save _As...", NULL, /* label, accelerator */
170
"Save to a file", /* tooltip */
171
G_CALLBACK (activate_action) },
172
{ "Quit", GTK_STOCK_QUIT, /* name, stock id */
173
"_Quit", "<control>Q", /* label, accelerator */
174
"Quit", /* tooltip */
175
G_CALLBACK (activate_action) },
176
{ "About", NULL, /* name, stock id */
177
"_About", "<control>A", /* label, accelerator */
178
"About", /* tooltip */
179
G_CALLBACK (about_cb) },
180
{ "Logo", "demo-gtk-logo", /* name, stock id */
181
NULL, NULL, /* label, accelerator */
182
"GTK+", /* tooltip */
183
G_CALLBACK (activate_action) },
185
static guint n_entries = G_N_ELEMENTS (entries);
188
static GtkToggleActionEntry toggle_entries[] = {
189
{ "Bold", GTK_STOCK_BOLD, /* name, stock id */
190
"_Bold", "<control>B", /* label, accelerator */
191
"Bold", /* tooltip */
192
G_CALLBACK (activate_action),
193
TRUE }, /* is_active */
195
static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
203
static GtkRadioActionEntry color_entries[] = {
204
{ "Red", NULL, /* name, stock id */
205
"_Red", "<control>R", /* label, accelerator */
206
"Blood", COLOR_RED }, /* tooltip, value */
207
{ "Green", NULL, /* name, stock id */
208
"_Green", "<control>G", /* label, accelerator */
209
"Grass", COLOR_GREEN }, /* tooltip, value */
210
{ "Blue", NULL, /* name, stock id */
211
"_Blue", "<control>B", /* label, accelerator */
212
"Sky", COLOR_BLUE }, /* tooltip, value */
214
static guint n_color_entries = G_N_ELEMENTS (color_entries);
222
static GtkRadioActionEntry shape_entries[] = {
223
{ "Square", NULL, /* name, stock id */
224
"_Square", "<control>S", /* label, accelerator */
225
"Square", SHAPE_SQUARE }, /* tooltip, value */
226
{ "Rectangle", NULL, /* name, stock id */
227
"_Rectangle", "<control>R", /* label, accelerator */
228
"Rectangle", SHAPE_RECTANGLE }, /* tooltip, value */
229
{ "Oval", NULL, /* name, stock id */
230
"_Oval", "<control>O", /* label, accelerator */
231
"Egg", SHAPE_OVAL }, /* tooltip, value */
233
static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
235
static const gchar *ui_info =
237
" <menubar name='MenuBar'>"
238
" <menu action='FileMenu'>"
239
" <menuitem action='New'/>"
240
" <menuitem action='Open'/>"
241
" <menuitem action='Save'/>"
242
" <menuitem action='SaveAs'/>"
244
" <menuitem action='Quit'/>"
246
" <menu action='PreferencesMenu'>"
247
" <menu action='ColorMenu'>"
248
" <menuitem action='Red'/>"
249
" <menuitem action='Green'/>"
250
" <menuitem action='Blue'/>"
252
" <menu action='ShapeMenu'>"
253
" <menuitem action='Square'/>"
254
" <menuitem action='Rectangle'/>"
255
" <menuitem action='Oval'/>"
257
" <menuitem action='Bold'/>"
259
" <menu action='HelpMenu'>"
260
" <menuitem action='About'/>"
263
" <toolbar name='ToolBar'>"
264
" <toolitem action='Open'>"
265
" <menu action='OpenMenu'>"
266
" <menuitem action='File1'/>"
269
" <toolitem action='Quit'/>"
270
" <separator action='Sep1'/>"
271
" <toolitem action='Logo'/>"
277
/* This function registers our custom toolbar icons, so they can be themed.
279
* It's totally optional to do this, you could just manually insert icons
280
* and have them not be themeable, especially if you never expect people
284
register_stock_icons (void)
286
static gboolean registered = FALSE;
291
GtkIconFactory *factory;
294
static GtkStockItem items[] = {
302
/* Register our stock items */
303
gtk_stock_add (items, G_N_ELEMENTS (items));
305
/* Add our custom icon factory to the list of defaults */
306
factory = gtk_icon_factory_new ();
307
gtk_icon_factory_add_default (factory);
309
/* demo_find_file() looks in the current directory first,
310
* so you can run gtk-demo without installing GTK, then looks
311
* in the location where the file is installed.
314
filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
317
pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
321
/* Register icon to accompany stock item */
324
GtkIconSet *icon_set;
325
GdkPixbuf *transparent;
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);
337
g_warning ("failed to load GTK logo for toolbar");
339
/* Drop our reference to the factory, GTK will hold a reference. */
340
g_object_unref (factory);
345
update_statusbar (GtkTextBuffer *buffer,
346
GtkStatusbar *statusbar)
353
gtk_statusbar_pop (statusbar, 0); /* clear any previous message,
354
* underflow is allowed
357
count = gtk_text_buffer_get_char_count (buffer);
359
gtk_text_buffer_get_iter_at_mark (buffer,
361
gtk_text_buffer_get_insert (buffer));
363
row = gtk_text_iter_get_line (&iter);
364
col = gtk_text_iter_get_line_offset (&iter);
366
msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
369
gtk_statusbar_push (statusbar, 0, msg);
375
mark_set_callback (GtkTextBuffer *buffer,
376
const GtkTextIter *new_location,
380
update_statusbar (buffer, GTK_STATUSBAR (data));
384
update_resize_grip (GtkWidget *widget,
385
GdkEventWindowState *event,
386
GtkStatusbar *statusbar)
388
if (event->changed_mask & (GDK_WINDOW_STATE_MAXIMIZED |
389
GDK_WINDOW_STATE_FULLSCREEN))
393
maximized = event->new_window_state & (GDK_WINDOW_STATE_MAXIMIZED |
394
GDK_WINDOW_STATE_FULLSCREEN);
395
gtk_statusbar_set_has_resize_grip (statusbar, !maximized);
401
do_appwindow (GtkWidget *do_widget)
406
GtkWidget *statusbar;
410
GtkTextBuffer *buffer;
411
GtkActionGroup *action_group;
412
GtkAction *open_action;
414
GError *error = NULL;
416
register_stock_icons ();
418
/* Create the toplevel window
421
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
422
gtk_window_set_screen (GTK_WINDOW (window),
423
gtk_widget_get_screen (do_widget));
424
gtk_window_set_title (GTK_WINDOW (window), "Application Window");
425
gtk_window_set_icon_name (GTK_WINDOW (window), "gtk-open");
427
/* NULL window variable when window is closed */
428
g_signal_connect (window, "destroy",
429
G_CALLBACK (gtk_widget_destroyed),
432
table = gtk_table_new (1, 5, FALSE);
434
gtk_container_add (GTK_CONTAINER (window), table);
436
/* Create the menubar and toolbar
439
action_group = gtk_action_group_new ("AppWindowActions");
440
open_action = g_object_new (tool_menu_action_get_type (),
443
"tooltip", "Open a file",
444
"stock-id", GTK_STOCK_OPEN,
446
gtk_action_group_add_action (action_group, open_action);
447
g_object_unref (open_action);
448
gtk_action_group_add_actions (action_group,
451
gtk_action_group_add_toggle_actions (action_group,
452
toggle_entries, n_toggle_entries,
454
gtk_action_group_add_radio_actions (action_group,
455
color_entries, n_color_entries,
457
G_CALLBACK (activate_radio_action),
459
gtk_action_group_add_radio_actions (action_group,
460
shape_entries, n_shape_entries,
462
G_CALLBACK (activate_radio_action),
465
merge = gtk_ui_manager_new ();
466
g_object_set_data_full (G_OBJECT (window), "ui-manager", merge,
468
gtk_ui_manager_insert_action_group (merge, action_group, 0);
469
gtk_window_add_accel_group (GTK_WINDOW (window),
470
gtk_ui_manager_get_accel_group (merge));
472
if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
474
g_message ("building menus failed: %s", error->message);
475
g_error_free (error);
478
bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
479
gtk_widget_show (bar);
480
gtk_table_attach (GTK_TABLE (table),
482
/* X direction */ /* Y direction */
484
GTK_EXPAND | GTK_FILL, 0,
487
bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
488
gtk_widget_show (bar);
489
gtk_table_attach (GTK_TABLE (table),
491
/* X direction */ /* Y direction */
493
GTK_EXPAND | GTK_FILL, 0,
499
infobar = gtk_info_bar_new ();
500
gtk_widget_set_no_show_all (infobar, TRUE);
501
messagelabel = gtk_label_new ("");
502
gtk_widget_show (messagelabel);
503
gtk_box_pack_start (GTK_BOX (gtk_info_bar_get_content_area (GTK_INFO_BAR (infobar))),
506
gtk_info_bar_add_button (GTK_INFO_BAR (infobar),
507
GTK_STOCK_OK, GTK_RESPONSE_OK);
508
g_signal_connect (infobar, "response",
509
G_CALLBACK (gtk_widget_hide), NULL);
511
gtk_table_attach (GTK_TABLE (table),
513
/* X direction */ /* Y direction */
515
GTK_EXPAND | GTK_FILL, 0,
518
sw = gtk_scrolled_window_new (NULL, NULL);
520
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
521
GTK_POLICY_AUTOMATIC,
522
GTK_POLICY_AUTOMATIC);
524
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
527
gtk_table_attach (GTK_TABLE (table),
529
/* X direction */ /* Y direction */
531
GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
534
gtk_window_set_default_size (GTK_WINDOW (window),
537
contents = gtk_text_view_new ();
538
gtk_widget_grab_focus (contents);
540
gtk_container_add (GTK_CONTAINER (sw),
543
/* Create statusbar */
545
statusbar = gtk_statusbar_new ();
546
gtk_table_attach (GTK_TABLE (table),
548
/* X direction */ /* Y direction */
550
GTK_EXPAND | GTK_FILL, 0,
553
/* Show text widget info in the statusbar */
554
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
556
g_signal_connect_object (buffer,
558
G_CALLBACK (update_statusbar),
562
g_signal_connect_object (buffer,
563
"mark_set", /* cursor moved */
564
G_CALLBACK (mark_set_callback),
568
g_signal_connect_object (window,
569
"window_state_event",
570
G_CALLBACK (update_resize_grip),
574
update_statusbar (buffer, GTK_STATUSBAR (statusbar));
577
if (!gtk_widget_get_visible (window))
579
gtk_widget_show_all (window);
583
gtk_widget_destroy (window);