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.
28 #include <gdk/gdkkeysyms.h>
33 create_menu (gint depth)
44 menu = gtk_menu_new ();
47 for (i = 0, j = 1; i < 5; i++, j++)
49 sprintf (buf, "item %2d - %d", depth, j);
50 menuitem = gtk_radio_menu_item_new_with_label (group, buf);
51 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
53 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
54 gtk_widget_show (menuitem);
56 gtk_widget_set_sensitive (menuitem, FALSE);
58 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (depth - 1));
65 change_orientation (GtkWidget *button,
69 GtkOrientation orientation;
71 parent = gtk_widget_get_parent (menubar);
72 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
73 gtk_orientable_set_orientation (GTK_ORIENTABLE (parent), 1 - orientation);
75 if (orientation == GTK_ORIENTATION_VERTICAL)
76 g_object_set (menubar, "pack-direction", GTK_PACK_DIRECTION_TTB, NULL);
78 g_object_set (menubar, "pack-direction", GTK_PACK_DIRECTION_LTR, NULL);
82 static GtkWidget *window = NULL;
85 do_menus (GtkWidget *do_widget)
97 GtkAccelGroup *accel_group;
99 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
100 gtk_window_set_screen (GTK_WINDOW (window),
101 gtk_widget_get_screen (do_widget));
102 gtk_window_set_title (GTK_WINDOW (window), "Menus");
103 g_signal_connect (window, "destroy",
104 G_CALLBACK(gtk_widget_destroyed), &window);
106 accel_group = gtk_accel_group_new ();
107 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
109 gtk_container_set_border_width (GTK_CONTAINER (window), 0);
111 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
112 gtk_container_add (GTK_CONTAINER (window), box);
113 gtk_widget_show (box);
115 box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
116 gtk_container_add (GTK_CONTAINER (box), box1);
117 gtk_widget_show (box1);
119 menubar = gtk_menu_bar_new ();
120 gtk_box_pack_start (GTK_BOX (box1), menubar, FALSE, TRUE, 0);
121 gtk_widget_show (menubar);
123 menu = create_menu (2);
125 menuitem = gtk_menu_item_new_with_label ("test\nline2");
126 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
127 gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
128 gtk_widget_show (menuitem);
130 menuitem = gtk_menu_item_new_with_label ("foo");
131 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (3));
132 gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
133 gtk_widget_show (menuitem);
135 menuitem = gtk_menu_item_new_with_label ("bar");
136 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (4));
137 gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
138 gtk_widget_show (menuitem);
140 box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
141 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
142 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
143 gtk_widget_show (box2);
145 button = gtk_button_new_with_label ("Flip");
146 g_signal_connect (button, "clicked",
147 G_CALLBACK (change_orientation), menubar);
148 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
149 gtk_widget_show (button);
151 button = gtk_button_new_with_label ("Close");
152 g_signal_connect_swapped (button, "clicked",
153 G_CALLBACK(gtk_widget_destroy), window);
154 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
155 gtk_widget_set_can_default (button, TRUE);
156 gtk_widget_grab_default (button);
157 gtk_widget_show (button);
160 if (!gtk_widget_get_visible (window))
162 gtk_widget_show (window);
166 gtk_widget_destroy (window);