Debian Codegraph @

examples/book/menus/main_menu/exampleapplication.cc

raw
1 /* gtkmm example Copyright (C) 2014 gtkmm development team
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include "exampleapplication.h"
18 #include "examplewindow.h"
19 #include <iostream>
20
21 ExampleApplication::ExampleApplication()
22 : Gtk::Application("org.gtkmm.example.main_menu")
23 {
24 Glib::set_application_name("Main Menu Example");
25 }
26
27 Glib::RefPtr<ExampleApplication> ExampleApplication::create()
28 {
29 return Glib::make_refptr_for_instance<ExampleApplication>(new ExampleApplication());
30 }
31
32 void ExampleApplication::on_startup()
33 {
34 //Call the base class's implementation:
35 Gtk::Application::on_startup();
36
37 //Create actions for menus and toolbars.
38 //We can use add_action() because Gtk::Application derives from Gio::ActionMap.
39
40 //File|New sub menu:
41 add_action("newstandard",
42 sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic));
43
44 add_action("newfoo",
45 sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic));
46
47 add_action("newgoo",
48 sigc::mem_fun(*this, &ExampleApplication::on_menu_file_new_generic));
49
50 //File menu:
51 add_action("quit", sigc::mem_fun(*this, &ExampleApplication::on_menu_file_quit));
52
53 //Help menu:
54 add_action("about", sigc::mem_fun(*this, &ExampleApplication::on_menu_help_about));
55
56 // Set accelerator keys:
57 set_accel_for_action("app.newstandard", "<Primary>n");
58 set_accel_for_action("app.quit", "<Primary>q");
59 set_accel_for_action("win.copy", "<Primary>c");
60 set_accel_for_action("win.paste", "<Primary>v");
61
62 m_refBuilder = Gtk::Builder::create();
63
64 //Layout the actions in a menubar and a menu:
65 Glib::ustring ui_info =
66 "<interface>"
67 " <!-- menubar -->"
68 " <menu id='menu-example'>"
69 " <submenu>"
70 " <attribute name='label' translatable='yes'>_File</attribute>"
71 " <section>"
72 " <item>"
73 " <attribute name='label' translatable='yes'>New _Standard</attribute>"
74 " <attribute name='action'>app.newstandard</attribute>"
75 " <attribute name='accel'>&lt;Primary&gt;n</attribute>"
76 " </item>"
77 " <item>"
78 " <attribute name='label' translatable='yes'>New _Foo</attribute>"
79 " <attribute name='action'>app.newfoo</attribute>"
80 " </item>"
81 " <item>"
82 " <attribute name='label' translatable='yes'>New _Goo</attribute>"
83 " <attribute name='action'>app.newgoo</attribute>"
84 " </item>"
85 " </section>"
86 " <section>"
87 " <item>"
88 " <attribute name='label' translatable='yes'>_Quit</attribute>"
89 " <attribute name='action'>app.quit</attribute>"
90 " <attribute name='accel'>&lt;Primary&gt;q</attribute>"
91 " </item>"
92 " </section>"
93 " </submenu>"
94 " <submenu>"
95 " <attribute name='label' translatable='yes'>_Edit</attribute>"
96 " <section>"
97 " <item>"
98 " <attribute name='label' translatable='yes'>_Copy</attribute>"
99 " <attribute name='action'>win.copy</attribute>"
100 " <attribute name='accel'>&lt;Primary&gt;c</attribute>"
101 " </item>"
102 " <item>"
103 " <attribute name='label' translatable='yes'>_Paste</attribute>"
104 " <attribute name='action'>win.paste</attribute>"
105 " <attribute name='accel'>&lt;Primary&gt;v</attribute>"
106 " </item>"
107 " <item>"
108 " <attribute name='label' translatable='yes'>_Something</attribute>"
109 " <attribute name='action'>win.something</attribute>"
110 " </item>"
111 " </section>"
112 " </submenu>"
113 " <submenu>"
114 " <attribute name='label' translatable='yes'>_Choices</attribute>"
115 " <section>"
116 " <item>"
117 " <attribute name='label' translatable='yes'>Choice _A</attribute>"
118 " <attribute name='action'>win.choice</attribute>"
119 " <attribute name='target'>a</attribute>"
120 " </item>"
121 " <item>"
122 " <attribute name='label' translatable='yes'>Choice _B</attribute>"
123 " <attribute name='action'>win.choice</attribute>"
124 " <attribute name='target'>b</attribute>"
125 " </item>"
126 " </section>"
127 " </submenu>"
128 " <submenu>"
129 " <attribute name='label' translatable='yes'>_Other Choices</attribute>"
130 " <section>"
131 " <item>"
132 " <attribute name='label' translatable='yes'>Choice 1</attribute>"
133 " <attribute name='action'>win.choiceother</attribute>"
134 " <attribute name='target' type='i'>1</attribute>"
135 " </item>"
136 " <item>"
137 " <attribute name='label' translatable='yes'>Choice 2</attribute>"
138 " <attribute name='action'>win.choiceother</attribute>"
139 " <attribute name='target' type='i'>2</attribute>"
140 " </item>"
141 " </section>"
142 " <section>"
143 " <item>"
144 " <attribute name='label' translatable='yes'>Some Toggle</attribute>"
145 " <attribute name='action'>win.sometoggle</attribute>"
146 " </item>"
147 " </section>"
148 " </submenu>"
149 " <submenu>"
150 " <attribute name='label' translatable='yes'>_Help</attribute>"
151 " <section>"
152 " <item>"
153 " <attribute name='label' translatable='yes'>_About Window</attribute>"
154 " <attribute name='action'>win.about</attribute>"
155 " </item>"
156 " <item>"
157 " <attribute name='label' translatable='yes'>_About App</attribute>"
158 " <attribute name='action'>app.about</attribute>"
159 " </item>"
160 " </section>"
161 " </submenu>"
162 " </menu>"
163 "</interface>";
164
165 try
166 {
167 m_refBuilder->add_from_string(ui_info);
168 }
169 catch (const Glib::Error& ex)
170 {
171 std::cerr << "Building menus failed: " << ex.what();
172 }
173
174 //Get the menubar and the app menu, and add them to the application:
175 auto gmenu = m_refBuilder->get_object<Gio::Menu>("menu-example");
176 if (!gmenu)
177 {
178 g_warning("GMenu not found");
179 }
180 else
181 {
182 set_menubar(gmenu);
183 }
184 }
185
186 void ExampleApplication::on_activate()
187 {
188 //std::cout << "debug1: " << G_STRFUNC << std::endl;
189 // The application has been started, so let's show a window.
190 // A real application might want to reuse this window in on_open(),
191 // when asked to open a file, if no changes have been made yet.
192 create_window();
193 }
194
195 void ExampleApplication::create_window()
196 {
197 // Since gtkmm 4.8, a Gtk::Window can be managed. If managed and not created
198 // by Gtk::Builder, it's deleted when its underlying C instance is destroyed.
199 // The underlying C instance is destroyed when it's closed, unless
200 // Gtk::Window::set_hide_on_close() has been called.
201 auto win = Gtk::make_managed<ExampleWindow>();
202
203 //Make sure that the application runs for as long this window is still open:
204 add_window(*win);
205
206 win->set_show_menubar();
207 win->set_visible(true);
208 }
209
210 void ExampleApplication::on_menu_file_new_generic()
211 {
212 std::cout << "A File|New menu item was selected." << std::endl;
213 }
214
215 void ExampleApplication::on_menu_file_quit()
216 {
217 std::cout << G_STRFUNC << std::endl;
218 quit(); // Not really necessary, when Gtk::Window::close() is called.
219
220 // Gio::Application::quit() will make Gio::Application::run() return,
221 // but it's a crude way of ending the program. The window is not removed
222 // from the application. Neither the window's nor the application's
223 // destructors will be called, because there will be remaining reference
224 // counts in both of them. If we want the destructors to be called, we
225 // must close the window.
226 auto windows = get_windows();
227 for (auto window : windows)
228 window->close();
229 }
230
231 void ExampleApplication::on_menu_help_about()
232 {
233 std::cout << "Help|About App was selected." << std::endl;
234 }