Debian Codegraph @

examples/book/menus/popup/examplewindow.cc

raw
1 /* gtkmm example Copyright (C) 2002-2013 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(const Glib::RefPtr<Gtk::Application>& app)
21 : m_Box(Gtk::Orientation::VERTICAL),
22 m_Label("Right-click to see the popup menu.")
23 {
24 set_title("popup example");
25 set_default_size(200, 200);
26
27 set_child(m_Box);
28
29 // Catch button_press events:
30 m_Box.append(m_Label);
31 m_Label.set_expand();
32 m_refGesture = Gtk::GestureClick::create();
33 m_refGesture->set_button(GDK_BUTTON_SECONDARY);
34 m_refGesture->signal_pressed().connect(
35 sigc::mem_fun(*this, &ExampleWindow::on_label_pressed));
36 m_Label.add_controller(m_refGesture);
37
38 //Create actions:
39 auto refActionGroup = Gio::SimpleActionGroup::create();
40
41 //File|New sub menu:
42 //These menu actions would normally already exist for a main menu, because a
43 //context menu should not normally contain menu items that are only available
44 //via a context menu.
45
46 refActionGroup->add_action("edit",
47 sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
48
49 refActionGroup->add_action("process",
50 sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
51
52 refActionGroup->add_action("remove",
53 sigc::mem_fun(*this, &ExampleWindow::on_menu_file_popup_generic));
54
55 insert_action_group("examplepopup", refActionGroup);
56
57 // Set accelerator keys:
58 app->set_accel_for_action("examplepopup.edit", "<Primary>e");
59 app->set_accel_for_action("examplepopup.process", "<Primary>p");
60 app->set_accel_for_action("examplepopup.remove", "<Primary>r");
61
62 m_refBuilder = Gtk::Builder::create();
63
64 //Layout the actions in a menubar and toolbar:
65 Glib::ustring ui_info =
66 "<interface>"
67 " <menu id='menu-examplepopup'>"
68 " <section>"
69 " <item>"
70 " <attribute name='label' translatable='yes'>Edit</attribute>"
71 " <attribute name='action'>examplepopup.edit</attribute>"
72 " </item>"
73 " <item>"
74 " <attribute name='label' translatable='yes'>Process</attribute>"
75 " <attribute name='action'>examplepopup.process</attribute>"
76 " </item>"
77 " <item>"
78 " <attribute name='label' translatable='yes'>Remove</attribute>"
79 " <attribute name='action'>examplepopup.remove</attribute>"
80 " </item>"
81 " </section>"
82 " </menu>"
83 "</interface>";
84
85 try
86 {
87 m_refBuilder->add_from_string(ui_info);
88 }
89 catch(const Glib::Error& ex)
90 {
91 std::cerr << "building menus failed: " << ex.what();
92 }
93
94 //Get the menu:
95 auto gmenu = m_refBuilder->get_object<Gio::Menu>("menu-examplepopup");
96 if(!gmenu)
97 g_warning("GMenu not found");
98
99 m_MenuPopup.set_parent(m_Label);
100 m_MenuPopup.set_menu_model(gmenu);
101 m_MenuPopup.set_has_arrow(false);
102 }
103
104 ExampleWindow::~ExampleWindow()
105 {
106 m_MenuPopup.unparent();
107 }
108
109 void ExampleWindow::on_menu_file_popup_generic()
110 {
111 std::cout << "A popup menu item was selected." << std::endl;
112 }
113
114 void ExampleWindow::on_label_pressed(int /* n_press */, double x, double y)
115 {
116 const Gdk::Rectangle rect(x, y, 1, 1);
117 m_MenuPopup.set_pointing_to(rect);
118 m_MenuPopup.popup();
119 }