1 /* Application main window
3 * Demonstrates a typical application window, with menubar, toolbar, statusbar.
8 static GtkWidget *window = NULL;
12 menuitem_cb (gpointer callback_data,
13 guint callback_action,
18 dialog = gtk_message_dialog_new (GTK_WINDOW (callback_data),
19 GTK_DIALOG_DESTROY_WITH_PARENT,
22 "You selected or toggled the menu item: \"%s\"",
23 gtk_item_factory_path_from_widget (widget));
25 /* Close dialog on user response */
26 g_signal_connect (G_OBJECT (dialog),
28 G_CALLBACK (gtk_widget_destroy),
31 gtk_widget_show (dialog);
35 static GtkItemFactoryEntry menu_items[] =
37 { "/_File", NULL, 0, 0, "<Branch>" },
38 { "/File/tearoff1", NULL, menuitem_cb, 0, "<Tearoff>" },
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 { "/_Help", NULL, 0, 0, "<LastBranch>" },
57 { "/Help/_About", NULL, menuitem_cb, 0 },
61 toolbar_cb (GtkWidget *button,
66 dialog = gtk_message_dialog_new (GTK_WINDOW (data),
67 GTK_DIALOG_DESTROY_WITH_PARENT,
70 "You selected a toolbar button");
72 /* Close dialog on user response */
73 g_signal_connect (G_OBJECT (dialog),
75 G_CALLBACK (gtk_widget_destroy),
78 gtk_widget_show (dialog);
81 /* This function registers our custom toolbar icons, so they can be themed.
83 * It's totally optional to do this, you could just manually insert icons
84 * and have them not be themeable, especially if you never expect people
88 register_stock_icons (void)
90 static gboolean registered = FALSE;
95 GtkIconFactory *factory;
97 static GtkStockItem items[] = {
105 /* Register our stock items */
106 gtk_stock_add (items, G_N_ELEMENTS (items));
108 /* Add our custom icon factory to the list of defaults */
109 factory = gtk_icon_factory_new ();
110 gtk_icon_factory_add_default (factory);
112 /* Try current directory */
113 pixbuf = gdk_pixbuf_new_from_file ("./gtk-logo-rgb.gif", NULL);
115 /* Try install directory */
117 pixbuf = gdk_pixbuf_new_from_file (DEMOCODEDIR"/gtk-logo-rgb.gif", NULL);
119 /* Register icon to accompany stock item */
122 GtkIconSet *icon_set;
123 GdkPixbuf *transparent;
125 /* The gtk-logo-rgb icon has a white background, make it transparent */
126 transparent = gdk_pixbuf_add_alpha (pixbuf, TRUE, 0xff, 0xff, 0xff);
128 icon_set = gtk_icon_set_new_from_pixbuf (transparent);
129 gtk_icon_factory_add (factory, "demo-gtk-logo", icon_set);
130 gtk_icon_set_unref (icon_set);
131 g_object_unref (G_OBJECT (pixbuf));
132 g_object_unref (G_OBJECT (transparent));
135 g_warning ("failed to load GTK logo for toolbar");
137 /* Drop our reference to the factory, GTK will hold a reference. */
138 g_object_unref (G_OBJECT (factory));
143 update_statusbar (GtkTextBuffer *buffer,
144 GtkStatusbar *statusbar)
151 gtk_statusbar_pop (statusbar, 0); /* clear any previous message, underflow is allowed */
153 count = gtk_text_buffer_get_char_count (buffer);
155 gtk_text_buffer_get_iter_at_mark (buffer,
157 gtk_text_buffer_get_insert (buffer));
159 row = gtk_text_iter_get_line (&iter);
160 col = gtk_text_iter_get_line_offset (&iter);
162 msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
165 gtk_statusbar_push (statusbar, 0, msg);
171 mark_set_callback (GtkTextBuffer *buffer,
172 const GtkTextIter *new_location,
176 update_statusbar (buffer, GTK_STATUSBAR (data));
186 GtkWidget *statusbar;
189 GtkTextBuffer *buffer;
190 GtkAccelGroup *accel_group;
191 GtkItemFactory *item_factory;
193 register_stock_icons ();
195 /* Create the toplevel window
198 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
199 gtk_window_set_title (GTK_WINDOW (window), "Application Window");
201 /* NULL window variable when window is closed */
202 g_signal_connect (G_OBJECT (window), "destroy",
203 G_CALLBACK (gtk_widget_destroyed),
206 table = gtk_table_new (1, 4, FALSE);
208 gtk_container_add (GTK_CONTAINER (window), table);
210 /* Create the menubar
213 accel_group = gtk_accel_group_new ();
214 gtk_accel_group_attach (accel_group, GTK_OBJECT (window));
215 gtk_accel_group_unref (accel_group);
217 item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>", accel_group);
219 /* Set up item factory to go away with the window */
220 gtk_object_ref (GTK_OBJECT (item_factory));
221 gtk_object_sink (GTK_OBJECT (item_factory));
222 g_object_set_data_full (G_OBJECT (window),
225 (GDestroyNotify) g_object_unref);
227 /* create menu items */
228 gtk_item_factory_create_items (item_factory, G_N_ELEMENTS (menu_items),
231 gtk_table_attach (GTK_TABLE (table),
232 gtk_item_factory_get_widget (item_factory, "<main>"),
233 /* X direction */ /* Y direction */
235 GTK_EXPAND | GTK_FILL, 0,
238 /* Create the toolbar
240 toolbar = gtk_toolbar_new ();
242 gtk_toolbar_insert_stock (GTK_TOOLBAR (toolbar),
244 "This is a demo button with an 'open' icon",
246 G_CALLBACK (toolbar_cb),
247 window, /* user data for callback */
248 -1); /* -1 means "append" */
250 gtk_toolbar_insert_stock (GTK_TOOLBAR (toolbar),
252 "This is a demo button with a 'quit' icon",
254 G_CALLBACK (toolbar_cb),
255 window, /* user data for callback */
256 -1); /* -1 means "append" */
258 gtk_toolbar_append_space (GTK_TOOLBAR (toolbar));
260 gtk_toolbar_insert_stock (GTK_TOOLBAR (toolbar),
262 "This is a demo button with a 'gtk' icon",
264 G_CALLBACK (toolbar_cb),
265 window, /* user data for callback */
266 -1); /* -1 means "append" */
268 gtk_table_attach (GTK_TABLE (table),
270 /* X direction */ /* Y direction */
272 GTK_EXPAND | GTK_FILL, 0,
278 sw = gtk_scrolled_window_new (NULL, NULL);
280 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
281 GTK_POLICY_AUTOMATIC,
282 GTK_POLICY_AUTOMATIC);
284 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
287 gtk_table_attach (GTK_TABLE (table),
289 /* X direction */ /* Y direction */
291 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
294 gtk_window_set_default_size (GTK_WINDOW (window),
297 contents = gtk_text_view_new ();
299 gtk_container_add (GTK_CONTAINER (sw),
302 /* Create statusbar */
304 statusbar = gtk_statusbar_new ();
305 gtk_table_attach (GTK_TABLE (table),
307 /* X direction */ /* Y direction */
309 GTK_EXPAND | GTK_FILL, 0,
312 /* Show text widget info in the statusbar */
313 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (contents));
315 g_signal_connect (G_OBJECT (buffer),
317 G_CALLBACK (update_statusbar),
320 g_signal_connect (G_OBJECT (buffer),
321 "mark_set", /* cursor moved */
322 G_CALLBACK (mark_set_callback),
325 update_statusbar (buffer, GTK_STATUSBAR (statusbar));
328 if (!GTK_WIDGET_VISIBLE (window))
330 gtk_widget_show_all (window);
334 gtk_widget_destroy (window);