Debian Codegraph @

examples/book/menus/main_menu/examplewindow.cc

raw
1 /* gtkmm example Copyright (C) 2002 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 "examplewindow.h"
18 #include <iostream>
19
20 ExampleWindow::ExampleWindow()
21 : Gtk::ApplicationWindow(),
22 m_Box(Gtk::Orientation::VERTICAL)
23 {
24 set_title("Main menu example");
25 set_default_size(300, 100);
26
27 // ExampleApplication displays the menubar. Other stuff, such as a toolbar,
28 // is put into the box.
29 set_child(m_Box);
30
31 // Create actions for menus and toolbars.
32 // We can use add_action() because Gtk::ApplicationWindow derives from Gio::ActionMap.
33 // This Action Map uses a "win." prefix for the actions.
34 // Therefore, for instance, "win.copy", is used in ExampleApplication::on_startup()
35 // to layout the menu.
36
37 //Edit menu:
38 add_action("copy", sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
39 add_action("paste", sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
40 add_action("something", sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
41
42 //Choices menus, to demonstrate Radio items,
43 //using our convenience methods for string and int radio values:
44 m_refChoice = add_action_radio_string("choice",
45 sigc::mem_fun(*this, &ExampleWindow::on_menu_choices), "a");
46
47 m_refChoiceOther = add_action_radio_integer("choiceother",
48 sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_other), 1);
49
50 m_refToggle = add_action_bool("sometoggle",
51 sigc::mem_fun(*this, &ExampleWindow::on_menu_toggle), false);
52
53 //Help menu:
54 add_action("about", sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
55
56 //Create the toolbar and add it to a container widget:
57
58 m_refBuilder = Gtk::Builder::create();
59
60 Glib::ustring ui_info =
61 "<!-- Generated with glade 3.18.3 and then changed manually -->"
62 "<interface>"
63 " <object class='GtkBox' id='toolbar'>"
64 " <property name='can_focus'>False</property>"
65 " <child>"
66 " <object class='GtkButton' id='toolbutton_new'>"
67 " <property name='can_focus'>False</property>"
68 " <property name='tooltip_text' translatable='yes'>New Standard</property>"
69 " <property name='action_name'>app.newstandard</property>"
70 " <property name='icon_name'>document-new</property>"
71 " <property name='hexpand'>False</property>"
72 " <property name='vexpand'>False</property>"
73 " </object>"
74 " </child>"
75 " <child>"
76 " <object class='GtkButton' id='toolbutton_quit'>"
77 " <property name='can_focus'>False</property>"
78 " <property name='tooltip_text' translatable='yes'>Quit</property>"
79 " <property name='action_name'>app.quit</property>"
80 " <property name='icon_name'>application-exit</property>"
81 " <property name='hexpand'>False</property>"
82 " <property name='vexpand'>False</property>"
83 " </object>"
84 " </child>"
85 " </object>"
86 "</interface>";
87
88 try
89 {
90 m_refBuilder->add_from_string(ui_info);
91 }
92 catch (const Glib::Error& ex)
93 {
94 std::cerr << "Building toolbar failed: " << ex.what();
95 }
96
97 auto toolbar = m_refBuilder->get_widget<Gtk::Box>("toolbar");
98 if (!toolbar)
99 g_warning("toolbar not found");
100 else
101 m_Box.append(*toolbar);
102 }
103
104 ExampleWindow::~ExampleWindow()
105 {
106 }
107
108 void ExampleWindow::on_menu_others()
109 {
110 std::cout << "A menu item was selected." << std::endl;
111 }
112
113 void ExampleWindow::on_menu_choices(const Glib::ustring& parameter)
114 {
115 //The radio action's state does not change automatically:
116 m_refChoice->change_state(parameter);
117
118 Glib::ustring message;
119 if (parameter == "a")
120 message = "Choice a was selected.";
121 else
122 message = "Choice b was selected.";
123
124 std::cout << message << std::endl;
125 }
126
127 void ExampleWindow::on_menu_choices_other(int parameter)
128 {
129 //The radio action's state does not change automatically:
130 m_refChoiceOther->change_state(parameter);
131
132 Glib::ustring message;
133 if (parameter == 1)
134 message = "Choice 1 was selected.";
135 else
136 message = "Choice 2 was selected.";
137
138 std::cout << message << std::endl;
139 }
140
141 void ExampleWindow::on_menu_toggle()
142 {
143 bool active = false;
144 m_refToggle->get_state(active);
145
146 //The toggle action's state does not change automatically:
147 active = !active;
148 m_refToggle->change_state(active);
149
150 Glib::ustring message;
151 if (active)
152 message = "Toggle is active.";
153 else
154 message = "Toggle is not active.";
155
156 std::cout << message << std::endl;
157 }