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 static GtkActionEntry entries[] = {
66 { "FileMenu", NULL, "_File" }, /* name, stock id, label */
67 { "PreferencesMenu", NULL, "_Preferences" }, /* name, stock id, label */
68 { "ColorMenu", NULL, "_Color" }, /* name, stock id, label */
69 { "ShapeMenu", NULL, "_Shape" }, /* name, stock id, label */
70 { "HelpMenu", NULL, "_Help" }, /* name, stock id, label */
71 { "New", GTK_STOCK_NEW, /* name, stock id */
72 "_New", "<control>N", /* label, accelerator */
73 "Create a new file", /* tooltip */
74 G_CALLBACK (activate_action) },
75 { "Open", GTK_STOCK_OPEN, /* name, stock id */
76 "_Open","<control>O", /* label, accelerator */
77 "Open a file", /* tooltip */
78 G_CALLBACK (activate_action) },
79 { "Save", GTK_STOCK_SAVE, /* name, stock id */
80 "_Save","<control>S", /* label, accelerator */
81 "Save current file", /* tooltip */
82 G_CALLBACK (activate_action) },
83 { "SaveAs", GTK_STOCK_SAVE, /* name, stock id */
84 "Save _As...", NULL, /* label, accelerator */
85 "Save to a file", /* tooltip */
86 G_CALLBACK (activate_action) },
87 { "Quit", GTK_STOCK_QUIT, /* name, stock id */
88 "_Quit", "<control>Q", /* label, accelerator */
90 G_CALLBACK (activate_action) },
91 { "About", NULL, /* name, stock id */
92 "_About", "<control>A", /* label, accelerator */
93 "About", /* tooltip */
94 G_CALLBACK (activate_action) },
95 { "Logo", "demo-gtk-logo", /* name, stock id */
96 NULL, NULL, /* label, accelerator */
98 G_CALLBACK (activate_action) },
100 static guint n_entries = G_N_ELEMENTS (entries);
103 static GtkToggleActionEntry toggle_entries[] = {
104 { "Bold", GTK_STOCK_BOLD, /* name, stock id */
105 "_Bold", "<control>B", /* label, accelerator */
106 "Bold", /* tooltip */
107 G_CALLBACK (activate_action),
108 TRUE }, /* is_active */
110 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
118 static GtkRadioActionEntry color_entries[] = {
119 { "Red", NULL, /* name, stock id */
120 "_Red", "<control>R", /* label, accelerator */
121 "Blood", COLOR_RED }, /* tooltip, value */
122 { "Green", NULL, /* name, stock id */
123 "_Green", "<control>G", /* label, accelerator */
124 "Grass", COLOR_GREEN }, /* tooltip, value */
125 { "Blue", NULL, /* name, stock id */
126 "_Blue", "<control>B", /* label, accelerator */
127 "Sky", COLOR_BLUE }, /* tooltip, value */
129 static guint n_color_entries = G_N_ELEMENTS (color_entries);
137 static GtkRadioActionEntry shape_entries[] = {
138 { "Square", NULL, /* name, stock id */
139 "_Square", "<control>S", /* label, accelerator */
140 "Square", SHAPE_SQUARE }, /* tooltip, value */
141 { "Rectangle", NULL, /* name, stock id */
142 "_Rectangle", "<control>R", /* label, accelerator */
143 "Rectangle", SHAPE_RECTANGLE }, /* tooltip, value */
144 { "Oval", NULL, /* name, stock id */
145 "_Oval", "<control>O", /* label, accelerator */
146 "Egg", SHAPE_OVAL }, /* tooltip, value */
148 static guint n_shape_entries = G_N_ELEMENTS (shape_entries);
150 static const gchar *ui_info =
152 " <menubar name='MenuBar'>"
153 " <menu action='FileMenu'>"
154 " <menuitem action='New'/>"
155 " <menuitem action='Open'/>"
156 " <menuitem action='Save'/>"
157 " <menuitem action='SaveAs'/>"
159 " <menuitem action='Quit'/>"
161 " <menu action='PreferencesMenu'>"
162 " <menu action='ColorMenu'>"
163 " <menuitem action='Red'/>"
164 " <menuitem action='Green'/>"
165 " <menuitem action='Blue'/>"
167 " <menu action='ShapeMenu'>"
168 " <menuitem action='Square'/>"
169 " <menuitem action='Rectangle'/>"
170 " <menuitem action='Oval'/>"
172 " <menuitem action='Bold'/>"
174 " <menu action='HelpMenu'>"
175 " <menuitem action='About'/>"
178 " <toolbar name='ToolBar'>"
179 " <toolitem action='Open'/>"
180 " <toolitem action='Quit'/>"
181 " <separator action='Sep1'/>"
182 " <toolitem action='Logo'/>"
188 /* This function registers our custom toolbar icons, so they can be themed.
190 * It's totally optional to do this, you could just manually insert icons
191 * and have them not be themeable, especially if you never expect people
195 register_stock_icons (void)
197 static gboolean registered = FALSE;
202 GtkIconFactory *factory;
205 static GtkStockItem items[] = {
213 /* Register our stock items */
214 gtk_stock_add (items, G_N_ELEMENTS (items));
216 /* Add our custom icon factory to the list of defaults */
217 factory = gtk_icon_factory_new ();
218 gtk_icon_factory_add_default (factory);
220 /* demo_find_file() looks in the the current directory first,
221 * so you can run gtk-demo without installing GTK, then looks
222 * in the location where the file is installed.
225 filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
228 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
232 /* Register icon to accompany stock item */
235 GtkIconSet *icon_set;
236 GdkPixbuf *transparent;
238 /* The gtk-logo-rgb icon has a white background, make it transparent */
239 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
241 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
242 gtk_icon_factory_add (factory, "demo-gtk-logo", icon_set);
243 gtk_icon_set_unref (icon_set);
244 g_object_unref (pixbuf);
245 g_object_unref (transparent);
248 g_warning ("failed to load GTK logo for toolbar");
250 /* Drop our reference to the factory, GTK will hold a reference. */
251 g_object_unref (factory);
256 update_statusbar (GtkTextBuffer *buffer,
257 GtkStatusbar *statusbar)
264 gtk_statusbar_pop (statusbar, 0); /* clear any previous message, underflow is allowed */
266 count = gtk_text_buffer_get_char_count (buffer);
268 gtk_text_buffer_get_iter_at_mark (buffer,
270 gtk_text_buffer_get_insert (buffer));
272 row = gtk_text_iter_get_line (&iter);
273 col = gtk_text_iter_get_line_offset (&iter);
275 msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
278 gtk_statusbar_push (statusbar, 0, msg);
284 mark_set_callback (GtkTextBuffer *buffer,
285 const GtkTextIter *new_location,
289 update_statusbar (buffer, GTK_STATUSBAR (data));
293 update_resize_grip (GtkWidget *widget,
294 GdkEventWindowState *event,
295 GtkStatusbar *statusbar)
297 if (event->changed_mask & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_FULLSCREEN))
298 gtk_statusbar_set_has_resize_grip (statusbar, !(event->new_window_state & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_FULLSCREEN)));
303 do_appwindow (GtkWidget *do_widget)
308 GtkWidget *statusbar;
312 GtkTextBuffer *buffer;
313 GtkActionGroup *action_group;
315 GError *error = NULL;
317 register_stock_icons ();
319 /* Create the toplevel window
322 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
323 gtk_window_set_screen (GTK_WINDOW (window),
324 gtk_widget_get_screen (do_widget));
325 gtk_window_set_title (GTK_WINDOW (window), "Application Window");
327 /* NULL window variable when window is closed */
328 g_signal_connect (window, "destroy",
329 G_CALLBACK (gtk_widget_destroyed),
332 table = gtk_table_new (1, 4, FALSE);
334 gtk_container_add (GTK_CONTAINER (window), table);
336 /* Create the menubar and toolbar
339 action_group = gtk_action_group_new ("AppWindowActions");
340 gtk_action_group_add_actions (action_group,
343 gtk_action_group_add_toggle_actions (action_group,
344 toggle_entries, n_toggle_entries,
346 gtk_action_group_add_radio_actions (action_group,
347 color_entries, n_color_entries,
349 G_CALLBACK (activate_radio_action),
351 gtk_action_group_add_radio_actions (action_group,
352 shape_entries, n_shape_entries,
354 G_CALLBACK (activate_radio_action),
357 merge = gtk_ui_manager_new ();
358 g_object_set_data_full (G_OBJECT (window), "ui-manager", merge, g_object_unref);
359 gtk_ui_manager_insert_action_group (merge, action_group, 0);
360 gtk_window_add_accel_group (GTK_WINDOW (window),
361 gtk_ui_manager_get_accel_group (merge));
363 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
365 g_message ("building menus failed: %s", error->message);
366 g_error_free (error);
369 bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
370 gtk_widget_show (bar);
371 gtk_table_attach (GTK_TABLE (table),
373 /* X direction */ /* Y direction */
375 GTK_EXPAND | GTK_FILL, 0,
378 bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
379 gtk_toolbar_set_tooltips (GTK_TOOLBAR (bar), TRUE);
380 gtk_widget_show (bar);
381 gtk_table_attach (GTK_TABLE (table),
383 /* X direction */ /* Y direction */
385 GTK_EXPAND | GTK_FILL, 0,
391 sw = gtk_scrolled_window_new (NULL, NULL);
393 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
394 GTK_POLICY_AUTOMATIC,
395 GTK_POLICY_AUTOMATIC);
397 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
400 gtk_table_attach (GTK_TABLE (table),
402 /* X direction */ /* Y direction */
404 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
407 gtk_window_set_default_size (GTK_WINDOW (window),
410 contents = gtk_text_view_new ();
411 gtk_widget_grab_focus (contents);
413 gtk_container_add (GTK_CONTAINER (sw),
416 /* Create statusbar */
418 statusbar = gtk_statusbar_new ();
419 gtk_table_attach (GTK_TABLE (table),
421 /* X direction */ /* Y direction */
423 GTK_EXPAND | GTK_FILL, 0,
426 /* Show text widget info in the statusbar */
427 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
429 g_signal_connect_object (buffer,
431 G_CALLBACK (update_statusbar),
435 g_signal_connect_object (buffer,
436 "mark_set", /* cursor moved */
437 G_CALLBACK (mark_set_callback),
441 g_signal_connect_object (window,
442 "window_state_event",
443 G_CALLBACK (update_resize_grip),
447 update_statusbar (buffer, GTK_STATUSBAR (statusbar));
450 if (!GTK_WIDGET_VISIBLE (window))
452 gtk_widget_show_all (window);
456 gtk_widget_destroy (window);