Debian Codegraph @

demos/gtk-demo/example_menus.cc

raw
1 /* Menus
2 *
3 * There are several widgets involved in displaying menus. The
4 * Gtk::MenuBar 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 Gtk::Menu widget is the actual menu that pops up. Both Gtk::MenuBar
7 * and Gtk::Menu are subclasses of Gtk::MenuShell; a Gtk::MenuShell contains
8 * menu items (Gtk::MenuItem). 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 Gtk::MenuItem,
12 * Gtk::CheckMenuItem which can be checked/unchecked, Gtk::RadioMenuItem
13 * which is a check menu item that's in a mutually exclusive group,
14 * and Gtk::SeparatorMenuItem which is a separator bar.
15 *
16 * A Gtk::MenuItem can have a submenu, which is simply a Gtk::Menu to pop
17 * up when the menu item is selected. Typically, all menu items in a menu bar
18 * have submenus.
19 *
20 * Gtk::Builder provides a higher-level interface for creating menu bars
21 * and menus; while you can construct menus manually, most people don't
22 * do that. There's a separate demo for Gtk::Builder.
23 */
24
25 #include <gtkmm.h>
26 #include <iostream>
27
28 class Example_Menus : public Gtk::Window
29 {
30 public:
31 Example_Menus();
32 ~Example_Menus() override;
33
34 protected:
35 //signal handlers:
36 void on_button_clicked();
37 void on_item_activated(const Glib::ustring& item);
38
39 Gtk::Menu* create_menu(gint depth);
40
41 //Member widgets:
42 Gtk::Box m_VBox1, m_VBox_Sub1, m_VBox_Sub2;
43 Gtk::MenuBar m_MenuBar;
44 Gtk::Separator m_Separator;
45 Gtk::Button m_Button;
46 };
47
48 //Called by DemoWindow;
49 Gtk::Window* do_menus()
50 {
51 return new Example_Menus();
52 }
53
54 Example_Menus::Example_Menus()
55 : m_VBox1(Gtk::ORIENTATION_VERTICAL),
56 m_VBox_Sub1(Gtk::ORIENTATION_VERTICAL, 10),
57 m_VBox_Sub2(Gtk::ORIENTATION_VERTICAL, 10),
58 m_Separator(Gtk::ORIENTATION_HORIZONTAL),
59 m_Button("close")
60 {
61 set_title("menus");
62 set_border_width(0);
63
64 add(m_VBox1);
65
66 m_VBox1.pack_start(m_MenuBar, Gtk::PACK_SHRINK);
67
68 {
69 //Note:: It's generally easier to use the Gtk::Builder API.
70 Gtk::MenuItem* pMenuItem = Gtk::make_managed<Gtk::MenuItem>("test\nline2");
71 pMenuItem->set_submenu( *(create_menu(2)) );
72 m_MenuBar.append(*pMenuItem);
73 pMenuItem->show();
74
75 pMenuItem = Gtk::make_managed<Gtk::MenuItem>("foo");
76 pMenuItem->set_submenu( *(create_menu(3)) );
77 m_MenuBar.append(*pMenuItem);
78 pMenuItem->show();
79
80 pMenuItem = Gtk::make_managed<Gtk::MenuItem>("bar");
81 pMenuItem->set_submenu( *(create_menu(4)) );
82 m_MenuBar.append(*pMenuItem);
83 pMenuItem->show();
84 }
85
86
87 m_VBox_Sub1.set_border_width(10);
88 m_VBox1.pack_start(m_VBox_Sub1);
89
90 {
91 auto accel_group = Gtk::AccelGroup::create();
92 add_accel_group(accel_group);
93 auto pMenu = create_menu(1);
94 pMenu->set_accel_group(accel_group);
95
96 Gtk::MenuItem* pMenuItem = Gtk::make_managed<Gtk::SeparatorMenuItem>();
97 pMenu->append(*pMenuItem);
98 pMenuItem->show();
99
100 pMenuItem = Gtk::make_managed<Gtk::MenuItem>("accel");
101 pMenuItem->set_submenu(*pMenu);
102 m_MenuBar.append(*pMenuItem);
103 pMenuItem->show();
104
105 pMenuItem = Gtk::make_managed<Gtk::CheckMenuItem>("Accelerate Me");
106 pMenuItem->add_accelerator("activate", accel_group, GDK_KEY_F1,
107 Gdk::ModifierType(0), Gtk::ACCEL_VISIBLE);
108 pMenu->append(*pMenuItem);
109 pMenuItem->show();
110 pMenuItem->signal_activate().connect(
111 sigc::bind(sigc::mem_fun(*this, &Example_Menus::on_item_activated), "F1"));
112
113 pMenuItem = Gtk::make_managed<Gtk::CheckMenuItem>("Accelerator Locked");
114 pMenu->append(*pMenuItem);
115 pMenuItem->add_accelerator("activate", accel_group, GDK_KEY_F2,
116 Gdk::ModifierType(0), Gtk::ACCEL_VISIBLE | Gtk::ACCEL_LOCKED);
117 pMenuItem->show();
118 pMenuItem->signal_activate().connect(
119 sigc::bind(sigc::mem_fun(*this, &Example_Menus::on_item_activated), "F2"));
120
121 pMenuItem = Gtk::make_managed<Gtk::CheckMenuItem>("Accelerator Frozen");
122 pMenu->append(*pMenuItem);
123 pMenuItem->add_accelerator("activate", accel_group, GDK_KEY_F3,
124 Gdk::ModifierType(0), Gtk::ACCEL_VISIBLE);
125 pMenuItem->show();
126 pMenuItem->signal_activate().connect(
127 sigc::bind(sigc::mem_fun(*this, &Example_Menus::on_item_activated), "F3"));
128 }
129
130 m_VBox1.pack_start(m_Separator, Gtk::PACK_SHRINK);
131
132 m_VBox_Sub2.set_border_width(10);
133 m_VBox1.pack_start(m_VBox_Sub2, Gtk::PACK_SHRINK);
134
135 m_Button.signal_clicked().connect(sigc::mem_fun(*this, &Example_Menus::on_button_clicked));
136
137 m_VBox_Sub2.pack_start(m_Button);
138 m_Button.set_can_default(true);
139 m_Button.grab_default();
140
141 show_all();
142 }
143
144 Example_Menus::~Example_Menus()
145 {
146 }
147
148 Gtk::Menu* Example_Menus::create_menu(gint depth)
149 {
150 if (depth < 1)
151 return nullptr;
152
153 Gtk::Menu* pMenu = Gtk::make_managed<Gtk::Menu>();
154
155 {
156 Gtk::RadioMenuItem::Group radiogroup;
157
158 for(int i = 0, j = 1; i < 5; i++, j++)
159 {
160 char buf[32];
161 sprintf(buf, "item %2d - %d", depth, j);
162
163 Gtk::MenuItem* pMenuItem = Gtk::make_managed<Gtk::RadioMenuItem>(radiogroup, buf);
164 pMenu->append(*pMenuItem);
165 pMenuItem->show();
166
167 if(i == 3)
168 pMenuItem->set_sensitive(false);
169
170 Gtk::Menu* pSubMenu = create_menu(depth - 1);
171 if(pSubMenu)
172 pMenuItem->set_submenu(*pSubMenu);
173 }
174 }
175
176 return pMenu;
177 }
178
179 void Example_Menus::on_item_activated(const Glib::ustring& item)
180 {
181 std::cout << "Item " << item << " was activated" << std::endl;
182 }
183
184 void Example_Menus::on_button_clicked()
185 {
186 hide();
187 }