Debian Codegraph @

demos/gtk-demo/menus.c

raw
1 /* Menus
2 *
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.
10 *
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.
17 *
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
20 * have submenus.
21 */
22
23 #include <gtk/gtk.h>
24 #include <gdk/gdkkeysyms.h>
25
26 #include <stdio.h>
27
28 static GtkWidget *
29 create_menu (gint depth)
30 {
31 GtkWidget *menu;
32 GtkRadioMenuItem *last_item;
33 char buf[32];
34 int i, j;
35
36 if (depth < 1)
37 return NULL;
38
39 menu = gtk_menu_new ();
40 last_item = NULL;
41
42 for (i = 0, j = 1; i < 5; i++, j++)
43 {
44 GtkWidget *menu_item;
45
46 sprintf (buf, "item %2d - %d", depth, j);
47
48 menu_item = gtk_radio_menu_item_new_with_label_from_widget (NULL, buf);
49 gtk_radio_menu_item_join_group (GTK_RADIO_MENU_ITEM (menu_item), last_item);
50 last_item = GTK_RADIO_MENU_ITEM (menu_item);
51
52 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
53 gtk_widget_show (menu_item);
54 if (i == 3)
55 gtk_widget_set_sensitive (menu_item, FALSE);
56
57 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), create_menu (depth - 1));
58 }
59
60 return menu;
61 }
62
63 static void
64 change_orientation (GtkWidget *button,
65 GtkWidget *menubar)
66 {
67 GtkWidget *parent;
68 GtkOrientation orientation;
69
70 parent = gtk_widget_get_parent (menubar);
71 orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
72 gtk_orientable_set_orientation (GTK_ORIENTABLE (parent), 1 - orientation);
73
74 if (orientation == GTK_ORIENTATION_VERTICAL)
75 g_object_set (menubar, "pack-direction", GTK_PACK_DIRECTION_TTB, NULL);
76 else
77 g_object_set (menubar, "pack-direction", GTK_PACK_DIRECTION_LTR, NULL);
78
79 }
80
81 static GtkWidget *window = NULL;
82
83 GtkWidget *
84 do_menus (GtkWidget *do_widget)
85 {
86 GtkWidget *box;
87 GtkWidget *box1;
88 GtkWidget *box2;
89 GtkWidget *button;
90
91 if (!window)
92 {
93 GtkWidget *menubar;
94 GtkWidget *menu;
95 GtkWidget *menuitem;
96 GtkAccelGroup *accel_group;
97
98 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
99 gtk_window_set_screen (GTK_WINDOW (window),
100 gtk_widget_get_screen (do_widget));
101 gtk_window_set_title (GTK_WINDOW (window), "Menus");
102 g_signal_connect (window, "destroy",
103 G_CALLBACK(gtk_widget_destroyed), &window);
104
105 accel_group = gtk_accel_group_new ();
106 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
107
108 gtk_container_set_border_width (GTK_CONTAINER (window), 0);
109
110 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
111 gtk_container_add (GTK_CONTAINER (window), box);
112 gtk_widget_show (box);
113
114 box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
115 gtk_container_add (GTK_CONTAINER (box), box1);
116 gtk_widget_show (box1);
117
118 menubar = gtk_menu_bar_new ();
119 gtk_widget_set_hexpand (menubar, TRUE);
120 gtk_box_pack_start (GTK_BOX (box1), menubar, FALSE, TRUE, 0);
121 gtk_widget_show (menubar);
122
123 menu = create_menu (2);
124
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);
129
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);
134
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);
139
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);
144
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);
150
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);
158 }
159
160 if (!gtk_widget_get_visible (window))
161 gtk_widget_show (window);
162 else
163 gtk_widget_destroy (window);
164
165 return window;
166 }