1 /* Application main window
3 * Demonstrates a typical application window, with menubar, toolbar, statusbar.
7 #include "demo-common.h"
9 static GtkWidget *window = NULL;
13 menuitem_cb (gpointer callback_data,
14 guint callback_action,
19 dialog = gtk_message_dialog_new (GTK_WINDOW (callback_data),
20 GTK_DIALOG_DESTROY_WITH_PARENT,
23 "You selected or toggled the menu item: \"%s\"",
24 gtk_item_factory_path_from_widget (widget));
26 /* Close dialog on user response */
27 g_signal_connect (dialog,
29 G_CALLBACK (gtk_widget_destroy),
32 gtk_widget_show (dialog);
36 static GtkItemFactoryEntry menu_items[] =
38 { "/_File", NULL, 0, 0, "<Branch>" },
39 { "/File/_New", "<control>N", menuitem_cb, 0, "<StockItem>", GTK_STOCK_NEW },
40 { "/File/_Open", "<control>O", menuitem_cb, 0, "<StockItem>", GTK_STOCK_OPEN },
41 { "/File/_Save", "<control>S", menuitem_cb, 0, "<StockItem>", GTK_STOCK_SAVE },
42 { "/File/Save _As...", NULL, menuitem_cb, 0, "<StockItem>", GTK_STOCK_SAVE },
43 { "/File/sep1", NULL, menuitem_cb, 0, "<Separator>" },
44 { "/File/_Quit", "<control>Q", menuitem_cb, 0, "<StockItem>", GTK_STOCK_QUIT },
46 { "/_Preferences", NULL, 0, 0, "<Branch>" },
47 { "/_Preferences/_Color", NULL, 0, 0, "<Branch>" },
48 { "/_Preferences/Color/_Red", NULL, menuitem_cb, 0, "<RadioItem>" },
49 { "/_Preferences/Color/_Green", NULL, menuitem_cb, 0, "/Preferences/Color/Red" },
50 { "/_Preferences/Color/_Blue", NULL, menuitem_cb, 0, "/Preferences/Color/Red" },
51 { "/_Preferences/_Shape", NULL, 0, 0, "<Branch>" },
52 { "/_Preferences/Shape/_Square", NULL, menuitem_cb, 0, "<RadioItem>" },
53 { "/_Preferences/Shape/_Rectangle", NULL, menuitem_cb, 0, "/Preferences/Shape/Square" },
54 { "/_Preferences/Shape/_Oval", NULL, menuitem_cb, 0, "/Preferences/Shape/Rectangle" },
56 /* If you wanted this to be right justified you would use "<LastBranch>", not "<Branch>".
57 * Right justified help menu items are generally considered a bad idea now days.
59 { "/_Help", NULL, 0, 0, "<Branch>" },
60 { "/Help/_About", NULL, menuitem_cb, 0 },
64 toolbar_cb (GtkWidget *button,
69 dialog = gtk_message_dialog_new (GTK_WINDOW (data),
70 GTK_DIALOG_DESTROY_WITH_PARENT,
73 "You selected a toolbar button");
75 /* Close dialog on user response */
76 g_signal_connect (dialog,
78 G_CALLBACK (gtk_widget_destroy),
81 gtk_widget_show (dialog);
84 /* This function registers our custom toolbar icons, so they can be themed.
86 * It's totally optional to do this, you could just manually insert icons
87 * and have them not be themeable, especially if you never expect people
91 register_stock_icons (void)
93 static gboolean registered = FALSE;
98 GtkIconFactory *factory;
101 static GtkStockItem items[] = {
109 /* Register our stock items */
110 gtk_stock_add (items, G_N_ELEMENTS (items));
112 /* Add our custom icon factory to the list of defaults */
113 factory = gtk_icon_factory_new ();
114 gtk_icon_factory_add_default (factory);
116 /* demo_find_file() looks in the the current directory first,
117 * so you can run gtk-demo without installing GTK, then looks
118 * in the location where the file is installed.
121 filename = demo_find_file ("gtk-logo-rgb.gif", NULL);
124 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
128 /* Register icon to accompany stock item */
131 GtkIconSet *icon_set;
132 GdkPixbuf *transparent;
134 /* The gtk-logo-rgb icon has a white background, make it transparent */
135 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
137 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
138 gtk_icon_factory_add (factory, "demo-gtk-logo", icon_set);
139 gtk_icon_set_unref (icon_set);
140 g_object_unref (pixbuf);
141 g_object_unref (transparent);
144 g_warning ("failed to load GTK logo for toolbar");
146 /* Drop our reference to the factory, GTK will hold a reference. */
147 g_object_unref (factory);
152 update_statusbar (GtkTextBuffer *buffer,
153 GtkStatusbar *statusbar)
160 gtk_statusbar_pop (statusbar, 0); /* clear any previous message, underflow is allowed */
162 count = gtk_text_buffer_get_char_count (buffer);
164 gtk_text_buffer_get_iter_at_mark (buffer,
166 gtk_text_buffer_get_insert (buffer));
168 row = gtk_text_iter_get_line (&iter);
169 col = gtk_text_iter_get_line_offset (&iter);
171 msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
174 gtk_statusbar_push (statusbar, 0, msg);
180 mark_set_callback (GtkTextBuffer *buffer,
181 const GtkTextIter *new_location,
185 update_statusbar (buffer, GTK_STATUSBAR (data));
189 update_resize_grip (GtkWidget *widget,
190 GdkEventWindowState *event,
191 GtkStatusbar *statusbar)
193 if (event->changed_mask & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_FULLSCREEN))
194 gtk_statusbar_set_has_resize_grip (statusbar, !(event->new_window_state & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_FULLSCREEN)));
205 GtkWidget *statusbar;
208 GtkTextBuffer *buffer;
209 GtkAccelGroup *accel_group;
210 GtkItemFactory *item_factory;
212 register_stock_icons ();
214 /* Create the toplevel window
217 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
218 gtk_window_set_title (GTK_WINDOW (window), "Application Window");
220 /* NULL window variable when window is closed */
221 g_signal_connect (window, "destroy",
222 G_CALLBACK (gtk_widget_destroyed),
225 table = gtk_table_new (1, 4, FALSE);
227 gtk_container_add (GTK_CONTAINER (window), table);
229 /* Create the menubar
232 accel_group = gtk_accel_group_new ();
233 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
234 g_object_unref (accel_group);
236 item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>", accel_group);
238 /* Set up item factory to go away with the window */
239 g_object_ref (item_factory);
240 gtk_object_sink (GTK_OBJECT (item_factory));
241 g_object_set_data_full (G_OBJECT (window),
244 (GDestroyNotify) g_object_unref);
246 /* create menu items */
247 gtk_item_factory_create_items (item_factory, G_N_ELEMENTS (menu_items),
250 gtk_table_attach (GTK_TABLE (table),
251 gtk_item_factory_get_widget (item_factory, "<main>"),
252 /* X direction */ /* Y direction */
254 GTK_EXPAND | GTK_FILL, 0,
257 /* Create the toolbar
259 toolbar = gtk_toolbar_new ();
261 gtk_toolbar_insert_stock (GTK_TOOLBAR (toolbar),
263 "This is a demo button with an 'open' icon",
265 G_CALLBACK (toolbar_cb),
266 window, /* user data for callback */
267 -1); /* -1 means "append" */
269 gtk_toolbar_insert_stock (GTK_TOOLBAR (toolbar),
271 "This is a demo button with a 'quit' icon",
273 G_CALLBACK (toolbar_cb),
274 window, /* user data for callback */
275 -1); /* -1 means "append" */
277 gtk_toolbar_append_space (GTK_TOOLBAR (toolbar));
279 gtk_toolbar_insert_stock (GTK_TOOLBAR (toolbar),
281 "This is a demo button with a 'gtk' icon",
283 G_CALLBACK (toolbar_cb),
284 window, /* user data for callback */
285 -1); /* -1 means "append" */
287 gtk_table_attach (GTK_TABLE (table),
289 /* X direction */ /* Y direction */
291 GTK_EXPAND | GTK_FILL, 0,
297 sw = gtk_scrolled_window_new (NULL, NULL);
299 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
300 GTK_POLICY_AUTOMATIC,
301 GTK_POLICY_AUTOMATIC);
303 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
306 gtk_table_attach (GTK_TABLE (table),
308 /* X direction */ /* Y direction */
310 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
313 gtk_window_set_default_size (GTK_WINDOW (window),
316 contents = gtk_text_view_new ();
318 gtk_container_add (GTK_CONTAINER (sw),
321 /* Create statusbar */
323 statusbar = gtk_statusbar_new ();
324 gtk_table_attach (GTK_TABLE (table),
326 /* X direction */ /* Y direction */
328 GTK_EXPAND | GTK_FILL, 0,
331 /* Show text widget info in the statusbar */
332 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
334 g_signal_connect_object (buffer,
336 G_CALLBACK (update_statusbar),
340 g_signal_connect_object (buffer,
341 "mark_set", /* cursor moved */
342 G_CALLBACK (mark_set_callback),
346 g_signal_connect_object (window,
347 "window_state_event",
348 G_CALLBACK (update_resize_grip),
352 update_statusbar (buffer, GTK_STATUSBAR (statusbar));
355 if (!GTK_WIDGET_VISIBLE (window))
357 gtk_widget_show_all (window);
361 gtk_widget_destroy (window);