3 * There are several widgets involved in displaying menus. The
4 * GtkMenuBar widget is a menu bar, which normally appears horizontally
5 * at the top of an application, but can also be layed out vertically.
6 * The GtkMenu widget is the actual menu that pops up. Both GtkMenuBar
7 * and GtkMenu are subclasses of GtkMenuShell; a GtkMenuShell contains
8 * menu items (GtkMenuItem). Each menu item contains text and/or images
9 * and can be selected by the user.
11 * There are several kinds of menu item, including plain GtkMenuItem,
12 * GtkCheckMenuItem which can be checked/unchecked, GtkRadioMenuItem
13 * which is a check menu item that's in a mutually exclusive group,
14 * GtkSeparatorMenuItem which is a separator bar, GtkTearoffMenuItem
15 * which allows a GtkMenu to be torn off, and GtkImageMenuItem which
16 * can place a GtkImage or other widget next to the menu text.
18 * A GtkMenuItem can have a submenu, which is simply a GtkMenu to pop
19 * up when the menu item is selected. Typically, all menu items in a menu bar
22 * GtkUIManager provides a higher-level interface for creating menu bars
23 * and menus; while you can construct menus manually, most people don't
24 * do that. There's a separate demo for GtkUIManager.
29 #include <gdk/gdkkeysyms.h>
34 create_menu (gint depth,
46 menu = gtk_menu_new ();
51 menuitem = gtk_tearoff_menu_item_new ();
52 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
53 gtk_widget_show (menuitem);
56 for (i = 0, j = 1; i < 5; i++, j++)
58 sprintf (buf, "item %2d - %d", depth, j);
59 menuitem = gtk_radio_menu_item_new_with_label (group, buf);
60 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
62 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
63 gtk_widget_show (menuitem);
65 gtk_widget_set_sensitive (menuitem, FALSE);
67 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (depth - 1, TRUE));
74 change_orientation (GtkWidget *button,
78 GtkWidget *box = NULL;
80 parent = gtk_widget_get_parent (menubar);
82 if (GTK_IS_VBOX (parent))
84 box = gtk_widget_get_parent (parent);
86 g_object_ref (menubar);
87 gtk_container_remove (GTK_CONTAINER (parent), menubar);
88 gtk_container_add (GTK_CONTAINER (box), menubar);
89 gtk_box_reorder_child (GTK_BOX (box), menubar, 0);
90 g_object_unref (menubar);
91 g_object_set (menubar,
92 "pack-direction", GTK_PACK_DIRECTION_TTB,
99 children = gtk_container_get_children (GTK_CONTAINER (parent));
100 for (l = children; l; l = l->next)
102 if (GTK_IS_VBOX (l->data))
108 g_list_free (children);
110 g_object_ref (menubar);
111 gtk_container_remove (GTK_CONTAINER (parent), menubar);
112 gtk_container_add (GTK_CONTAINER (box), menubar);
113 gtk_box_reorder_child (GTK_BOX (box), menubar, 0);
114 g_object_unref (menubar);
115 g_object_set (menubar,
116 "pack-direction", GTK_PACK_DIRECTION_LTR,
122 do_menus (GtkWidget *do_widget)
124 static GtkWidget *window = NULL;
135 GtkAccelGroup *accel_group;
138 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
139 gtk_window_set_screen (GTK_WINDOW (window),
140 gtk_widget_get_screen (do_widget));
141 g_signal_connect (window, "destroy",
142 G_CALLBACK(gtk_widget_destroyed), &window);
143 g_signal_connect (window, "delete-event",
144 G_CALLBACK (gtk_true), NULL);
146 accel_group = gtk_accel_group_new ();
147 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
149 gtk_window_set_title (GTK_WINDOW (window), "menus");
150 gtk_container_set_border_width (GTK_CONTAINER (window), 0);
153 box = gtk_hbox_new (FALSE, 0);
154 gtk_container_add (GTK_CONTAINER (window), box);
155 gtk_widget_show (box);
157 box1 = gtk_vbox_new (FALSE, 0);
158 gtk_container_add (GTK_CONTAINER (box), box1);
159 gtk_widget_show (box1);
161 menubar = gtk_menu_bar_new ();
162 gtk_box_pack_start (GTK_BOX (box1), menubar, FALSE, TRUE, 0);
163 gtk_widget_show (menubar);
165 menu = create_menu (2, TRUE);
167 menuitem = gtk_menu_item_new_with_label ("test\nline2");
168 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
169 gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
170 gtk_widget_show (menuitem);
172 menuitem = gtk_menu_item_new_with_label ("foo");
173 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (3, TRUE));
174 gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
175 gtk_widget_show (menuitem);
177 menuitem = gtk_menu_item_new_with_label ("bar");
178 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (4, TRUE));
179 gtk_menu_item_set_right_justified (GTK_MENU_ITEM (menuitem), TRUE);
180 gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
181 gtk_widget_show (menuitem);
183 box2 = gtk_vbox_new (FALSE, 10);
184 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
185 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
186 gtk_widget_show (box2);
188 button = gtk_button_new_with_label ("Flip");
189 g_signal_connect (button, "clicked",
190 G_CALLBACK (change_orientation), menubar);
191 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
192 gtk_widget_show (button);
194 button = gtk_button_new_with_label ("Close");
195 g_signal_connect_swapped (button, "clicked",
196 G_CALLBACK(gtk_widget_destroy), window);
197 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
198 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
199 gtk_widget_grab_default (button);
200 gtk_widget_show (button);
203 if (!GTK_WIDGET_VISIBLE (window))
205 gtk_widget_show (window);
209 gtk_widget_destroy (window);