Debian Codegraph @

examples/book/events/mouse/examplewindow.cc

raw
1 /* gtkmm example Copyright (C) 2024 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 <map>
19 #include <iostream>
20
21 namespace
22 {
23 std::map<unsigned int, Glib::ustring> mouse_buttons
24 {
25 {0, "No button"},
26 {GDK_BUTTON_PRIMARY, "Primary"},
27 {GDK_BUTTON_MIDDLE, "Middle"},
28 {GDK_BUTTON_SECONDARY, "Secondary"},
29 };
30 }
31
32 ExampleWindow::ExampleWindow()
33 : m_first("First"),
34 m_second("Second"),
35 m_third("Click me")
36 {
37 set_title("Keyboard and Mouse Events");
38 m_box.set_margin(10);
39 set_child(m_box);
40
41 // Radio buttons
42 m_second.set_group(m_first);
43 m_first.set_active();
44
45 // Main container
46 m_box.set_orientation(Gtk::Orientation::HORIZONTAL);
47 m_box.append(m_first);
48 m_box.append(m_second);
49 m_box.append(m_third);
50
51 // Keyboard events
52 auto key_controller = Gtk::EventControllerKey::create();
53 key_controller->signal_key_pressed().connect(
54 sigc::mem_fun(*this, &ExampleWindow::on_key_pressed), false);
55 add_controller(key_controller);
56
57 // Mouse events, window
58 auto motion_controller = Gtk::EventControllerMotion::create();
59 motion_controller->signal_motion().connect(
60 sigc::mem_fun(*this, &ExampleWindow::on_mouse_motion));
61 add_controller(motion_controller);
62
63 m_window_click = Gtk::GestureClick::create();
64 m_window_click->set_button(0); // All mouse buttons
65 m_window_click->signal_pressed().connect(
66 sigc::mem_fun(*this, &ExampleWindow::on_window_mouse_pressed));
67 add_controller(m_window_click);
68
69 auto mouse_click = Gtk::GestureClick::create();
70 mouse_click->set_button(GDK_BUTTON_PRIMARY);
71 mouse_click->signal_released().connect(sigc::mem_fun(*this, &ExampleWindow::on_mouse_released));
72 add_controller(mouse_click);
73
74 auto mouse_scroll = Gtk::EventControllerScroll::create();
75 mouse_scroll->set_flags(Gtk::EventControllerScroll::Flags::VERTICAL);
76 // Handle mouse wheel rotations
77 mouse_scroll->signal_scroll().connect(
78 sigc::mem_fun(*this, &ExampleWindow::on_mouse_scroll), true);
79 add_controller(mouse_scroll);
80
81 // Mouse events, buttons
82 m_first_button_click = Gtk::GestureClick::create();
83 m_first_button_click->set_button(GDK_BUTTON_PRIMARY);
84 m_first_button_click->signal_pressed().connect(
85 sigc::mem_fun(*this, &ExampleWindow::on_first_button_mouse_pressed));
86 m_first.add_controller(m_first_button_click);
87
88 m_second_button_click = Gtk::GestureClick::create();
89 m_second_button_click->set_button(GDK_BUTTON_PRIMARY);
90 m_second_button_click->signal_pressed().connect(
91 sigc::mem_fun(*this, &ExampleWindow::on_second_button_mouse_pressed));
92 m_second.add_controller(m_second_button_click);
93
94 mouse_click = Gtk::GestureClick::create();
95 // This mouse event must be handled in the capture phase.
96 // The GtkButton C class handles the event in the capture phase and
97 // does not let it propagate to the bubble phase where events are
98 // handled by default.
99 mouse_click->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
100 mouse_click->set_button(GDK_BUTTON_PRIMARY);
101 mouse_click->signal_pressed().connect(
102 sigc::mem_fun(*this, &ExampleWindow::on_third_button_mouse_pressed));
103 m_third.add_controller(mouse_click);
104 m_third.signal_clicked().connect(
105 sigc::mem_fun(*this, &ExampleWindow::on_third_button_clicked));
106 }
107
108 void ExampleWindow::on_window_mouse_pressed(int n_press, double x, double y)
109 {
110 const auto current_button = mouse_buttons[m_window_click->get_current_button()];
111 std::cout << "Mouse pressed in window: " << current_button
112 << ", " << n_press << ", " << x << ", " << y << std::endl;
113 }
114
115 void ExampleWindow::on_first_button_mouse_pressed(int n_press, double x, double y)
116 {
117 const auto current_button = mouse_buttons[m_first_button_click->get_current_button()];
118 std::cout << "Mouse pressed in first button: " << current_button
119 << ", " << n_press << ", " << x << ", " << y << std::endl;
120 }
121
122 void ExampleWindow::on_second_button_mouse_pressed(int n_press, double x, double y)
123 {
124 const auto current_button = mouse_buttons[m_second_button_click->get_current_button()];
125 std::cout << "Mouse pressed in second button: " << current_button
126 << ", " << n_press << ", " << x << ", " << y << std::endl;
127 }
128
129 void ExampleWindow::on_third_button_mouse_pressed(int n_press, double x, double y)
130 {
131 std::cout << "Mouse pressed in third button: "
132 << n_press << ", " << x << ", " << y << std::endl;
133 }
134
135 void ExampleWindow::on_third_button_clicked()
136 {
137 std::cout << "Third button clicked" << std::endl;
138 }
139
140 void ExampleWindow::on_mouse_released(int n_press, double x, double y)
141 {
142 std::cout << "Mouse released: " << n_press << ", " << x << ", " << y << std::endl;
143 }
144
145 void ExampleWindow::on_mouse_motion(double x, double y)
146 {
147 std::cout << "Mouse motion: " << x << ", " << y << std::endl;
148 }
149
150 bool ExampleWindow::on_mouse_scroll(double dx, double dy)
151 {
152 std::cout << "Mouse scroll: " << dx << ", " << dy << std::endl;
153 return true; // handled
154 }
155
156 bool ExampleWindow::on_key_pressed(guint keyval, guint, Gdk::ModifierType state)
157 {
158 // Gdk::ModifierType::ALT_MASK -> the 'Alt' key(mask)
159 // GDK_KEY_1 -> the '1' key
160 // GDK_KEY_2 -> the '2' key
161
162 // Select the first radio button, when we press Alt + 1
163 if ((keyval == GDK_KEY_1) &&
164 (state & (Gdk::ModifierType::SHIFT_MASK | Gdk::ModifierType::CONTROL_MASK | Gdk::ModifierType::ALT_MASK)) == Gdk::ModifierType::ALT_MASK)
165 {
166 m_first.set_active();
167 // Returning true, cancels the propagation of the event
168 return true;
169 }
170 else if ((keyval == GDK_KEY_2) &&
171 (state & (Gdk::ModifierType::SHIFT_MASK | Gdk::ModifierType::CONTROL_MASK | Gdk::ModifierType::ALT_MASK)) == Gdk::ModifierType::ALT_MASK)
172 {
173 // and the second radio button, when we press Alt + 2
174 m_second.set_active();
175 return true;
176 }
177 else if (keyval == GDK_KEY_Escape)
178 {
179 // Close the window, when the 'Esc' key is pressed
180 close();
181 return true;
182 }
183
184 // The event has not been handled
185 return false;
186 }
187
188 ExampleWindow::~ExampleWindow()
189 {
190 }