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 static GtkActionGroupEntry entries[] = {
37 { "FileMenu", "_File" }, /* name, label */
38 { "PreferencesMenu", "_Preferences" }, /* name, label */
39 { "ColorMenu", "_Color" }, /* name, label */
40 { "ShapeMenu", "_Shape" }, /* name, label */
41 { "HelpMenu", "_Help" }, /* name, label */
42 { "New", "_New", /* name, label */
43 GTK_STOCK_NEW, "<control>N", /* stock_id, accelerator */
44 "Create a new file", /* tooltip */
45 G_CALLBACK (activate_action) },
46 { "Open", "_Open", /* name, label */
47 GTK_STOCK_OPEN, "<control>O", /* stock_id, accelerator */
48 "Open a file", /* tooltip */
49 G_CALLBACK (activate_action) },
50 { "Save", "_Save", /* name, label */
51 GTK_STOCK_SAVE, "<control>S", /* stock_id, accelerator */
52 "Save current file", /* tooltip */
53 G_CALLBACK (activate_action) },
54 { "SaveAs", "Save _As...", /* name, label */
55 GTK_STOCK_SAVE, NULL, /* stock_id, accelerator */
56 "Save to a file", /* tooltip */
57 G_CALLBACK (activate_action) },
58 { "Quit", "_Quit", /* name, label */
59 GTK_STOCK_QUIT, "<control>Q", /* stock_id, accelerator */
61 G_CALLBACK (activate_action) },
62 { "Red", "_Red", /* name, label */
63 NULL, "<control>R", /* stock_id, accelerator */
64 "Blood", /* tooltip */
65 G_CALLBACK (activate_action), NULL,
66 GTK_ACTION_RADIO }, /* entry type */
67 { "Green", "_Green", /* name, label */
68 NULL, "<control>G", /* stock_id, accelerator */
69 "Grass", /* tooltip */
70 G_CALLBACK (activate_action), NULL,
71 GTK_ACTION_RADIO, "Red" }, /* entry type, radio group */
72 { "Blue", "_Blue", /* name, label */
73 NULL, "<control>B", /* stock_id, accelerator */
75 G_CALLBACK (activate_action), NULL,
76 GTK_ACTION_RADIO, "Red" }, /* entry type, radio group */
77 { "Square", "_Square", /* name, label */
78 NULL, "<control>S", /* stock_id, accelerator */
79 "Square", /* tooltip */
80 G_CALLBACK (activate_action), NULL,
81 GTK_ACTION_RADIO }, /* entry type */
82 { "Rectangle", "_Rectangle", /* name, label */
83 NULL, "<control>R", /* stock_id, accelerator */
84 "Rectangle", /* tooltip */
85 G_CALLBACK (activate_action), NULL,
86 GTK_ACTION_RADIO, "Square" }, /* entry type, radio group */
87 { "Oval", "_Oval", /* name, label */
88 NULL, "<control>O", /* stock_id, accelerator */
90 G_CALLBACK (activate_action), NULL,
91 GTK_ACTION_RADIO, "Square" }, /* entry type, radio group */
92 { "About", "_About", /* name, label */
93 NULL, "<control>A", /* stock_id, accelerator */
94 "About", /* tooltip */
95 G_CALLBACK (activate_action) },
96 { "Logo", NULL, /* name, label */
97 "demo-gtk-logo", NULL, /* stock_id, accelerator */
99 G_CALLBACK (activate_action) },
101 static guint n_entries = G_N_ELEMENTS (entries);
103 static const gchar *ui_info =
105 " <menu name='MenuBar'>"
106 " <submenu name='FileMenu'>"
107 " <menuitem name='New'/>"
108 " <menuitem name='Open'/>"
109 " <menuitem name='Save'/>"
110 " <menuitem name='SaveAs'/>"
111 " <separator name='Sep1'/>"
112 " <menuitem name='Quit'/>"
114 " <submenu name='PreferencesMenu'>"
115 " <submenu name='ColorMenu'>"
116 " <menuitem name='Red'/>"
117 " <menuitem name='Green'/>"
118 " <menuitem name='Blue'/>"
120 " <submenu name='ShapeMenu'>"
121 " <menuitem name='Square'/>"
122 " <menuitem name='Rectangle'/>"
123 " <menuitem name='Oval'/>"
126 " <submenu name='HelpMenu'>"
127 " <menuitem name='About'/>"
130 " <dockitem name='ToolBar'>"
131 " <toolitem name='Open'/>"
132 " <toolitem name='Quit'/>"
133 " <separator name='Sep1'/>"
134 " <toolitem name='Logo'/>"
140 /* This function registers our custom toolbar icons, so they can be themed.
142 * It's totally optional to do this, you could just manually insert icons
143 * and have them not be themeable, especially if you never expect people
147 register_stock_icons (void)
149 static gboolean registered = FALSE;
154 GtkIconFactory *factory;
157 static GtkStockItem items[] = {
165 /* Register our stock items */
166 gtk_stock_add (items, G_N_ELEMENTS (items));
168 /* Add our custom icon factory to the list of defaults */
169 factory = gtk_icon_factory_new ();
170 gtk_icon_factory_add_default (factory);
172 /* demo_find_file() looks in the the current directory first,
173 * so you can run gtk-demo without installing GTK, then looks
174 * in the location where the file is installed.
177 filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
180 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
184 /* Register icon to accompany stock item */
187 GtkIconSet *icon_set;
188 GdkPixbuf *transparent;
190 /* The gtk-logo-rgb icon has a white background, make it transparent */
191 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
193 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
194 gtk_icon_factory_add (factory, "demo-gtk-logo", icon_set);
195 gtk_icon_set_unref (icon_set);
196 g_object_unref (pixbuf);
197 g_object_unref (transparent);
200 g_warning ("failed to load GTK logo for toolbar");
202 /* Drop our reference to the factory, GTK will hold a reference. */
203 g_object_unref (factory);
208 update_statusbar (GtkTextBuffer *buffer,
209 GtkStatusbar *statusbar)
216 gtk_statusbar_pop (statusbar, 0); /* clear any previous message, underflow is allowed */
218 count = gtk_text_buffer_get_char_count (buffer);
220 gtk_text_buffer_get_iter_at_mark (buffer,
222 gtk_text_buffer_get_insert (buffer));
224 row = gtk_text_iter_get_line (&iter);
225 col = gtk_text_iter_get_line_offset (&iter);
227 msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
230 gtk_statusbar_push (statusbar, 0, msg);
236 mark_set_callback (GtkTextBuffer *buffer,
237 const GtkTextIter *new_location,
241 update_statusbar (buffer, GTK_STATUSBAR (data));
245 update_resize_grip (GtkWidget *widget,
246 GdkEventWindowState *event,
247 GtkStatusbar *statusbar)
249 if (event->changed_mask & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_FULLSCREEN))
250 gtk_statusbar_set_has_resize_grip (statusbar, !(event->new_window_state & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_FULLSCREEN)));
260 GtkWidget *statusbar;
264 GtkTextBuffer *buffer;
265 GtkActionGroup *action_group;
268 GError *error = NULL;
270 register_stock_icons ();
272 /* Create the toplevel window
275 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
276 gtk_window_set_title (GTK_WINDOW (window), "Application Window");
278 /* NULL window variable when window is closed */
279 g_signal_connect (window, "destroy",
280 G_CALLBACK (gtk_widget_destroyed),
283 table = gtk_table_new (1, 4, FALSE);
285 gtk_container_add (GTK_CONTAINER (window), table);
287 /* Create the menubar and toolbar
290 action_group = gtk_action_group_new ("AppWindowActions");
291 gtk_action_group_add_actions (action_group, entries, n_entries);
293 action = gtk_action_group_get_action (action_group, "Red");
294 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
295 action = gtk_action_group_get_action (action_group, "Square");
296 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
298 merge = gtk_ui_manager_new ();
299 gtk_ui_manager_insert_action_group (merge, action_group, 0);
300 gtk_window_add_accel_group (GTK_WINDOW (window),
301 gtk_ui_manager_get_accel_group (merge));
303 if (!gtk_ui_manager_add_ui_from_string (merge, ui_info, -1, &error))
305 g_message ("building menus failed: %s", error->message);
306 g_error_free (error);
309 bar = gtk_ui_manager_get_widget (merge, "/MenuBar");
310 gtk_widget_show (bar);
311 gtk_table_attach (GTK_TABLE (table),
313 /* X direction */ /* Y direction */
315 GTK_EXPAND | GTK_FILL, 0,
318 bar = gtk_ui_manager_get_widget (merge, "/ToolBar");
319 gtk_widget_show (bar);
320 gtk_table_attach (GTK_TABLE (table),
322 /* X direction */ /* Y direction */
324 GTK_EXPAND | GTK_FILL, 0,
330 sw = gtk_scrolled_window_new (NULL, NULL);
332 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
333 GTK_POLICY_AUTOMATIC,
334 GTK_POLICY_AUTOMATIC);
336 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
339 gtk_table_attach (GTK_TABLE (table),
341 /* X direction */ /* Y direction */
343 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
346 gtk_window_set_default_size (GTK_WINDOW (window),
349 contents = gtk_text_view_new ();
351 gtk_container_add (GTK_CONTAINER (sw),
354 /* Create statusbar */
356 statusbar = gtk_statusbar_new ();
357 gtk_table_attach (GTK_TABLE (table),
359 /* X direction */ /* Y direction */
361 GTK_EXPAND | GTK_FILL, 0,
364 /* Show text widget info in the statusbar */
365 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
367 g_signal_connect_object (buffer,
369 G_CALLBACK (update_statusbar),
373 g_signal_connect_object (buffer,
374 "mark_set", /* cursor moved */
375 G_CALLBACK (mark_set_callback),
379 g_signal_connect_object (window,
380 "window_state_event",
381 G_CALLBACK (update_resize_grip),
385 update_statusbar (buffer, GTK_STATUSBAR (statusbar));
388 if (!GTK_WIDGET_VISIBLE (window))
390 gtk_widget_show_all (window);
394 gtk_widget_destroy (window);