Debian Codegraph @

examples/book/events/keyboard_propagation/examplewindow.cc

raw
1 /* gtkmm example Copyright (C) 2011 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 // In gtkmm3, this example used an Entry instead of m_label2. This would not work
18 // as intended in gtkmm4. GtkEntry in gtk4 marks a key press signal as handled
19 // in the target phase. It's not propagated further up to the Box and the Window.
20
21 #include "examplewindow.h"
22 #include <iostream>
23
24 ExampleWindow::ExampleWindow()
25 {
26 set_title("Event Propagation");
27 m_container.set_margin(10);
28 set_child(m_container);
29
30 m_frame.set_child(m_label2);
31 m_label2.set_selectable();
32 m_checkbutton_can_propagate_down.set_active();
33 m_checkbutton_can_propagate_up.set_active();
34
35 // Main container
36 m_container.set_orientation(Gtk::Orientation::VERTICAL);
37 m_container.append(m_label1);
38 m_container.append(m_frame);
39 m_container.append(m_checkbutton_can_propagate_down);
40 m_container.append(m_checkbutton_can_propagate_up);
41
42 // Event controllers
43 const bool after = false; // Run before or after the default signal handlers.
44
45 // Called in the capture phase of the event handling.
46 auto controller = Gtk::EventControllerKey::create();
47 controller->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
48 controller->signal_key_pressed().connect(
49 sigc::bind(sigc::mem_fun(*this, &ExampleWindow::label2_key_pressed), "capture"), after);
50 m_label2.add_controller(controller);
51
52 controller = Gtk::EventControllerKey::create();
53 controller->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
54 controller->signal_key_pressed().connect(
55 sigc::bind(sigc::mem_fun(*this, &ExampleWindow::box_key_pressed), "capture"), after);
56 m_container.add_controller(controller);
57
58 controller = Gtk::EventControllerKey::create();
59 controller->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
60 controller->signal_key_pressed().connect(
61 sigc::bind(sigc::mem_fun(*this, &ExampleWindow::window_key_pressed), "capture"), after);
62 add_controller(controller);
63
64 // Called in the target phase of the event handling.
65 controller = Gtk::EventControllerKey::create();
66 controller->set_propagation_phase(Gtk::PropagationPhase::TARGET);
67 controller->signal_key_pressed().connect(
68 sigc::bind(sigc::mem_fun(*this, &ExampleWindow::label2_key_pressed), "target"), after);
69 m_label2.add_controller(controller);
70
71 controller = Gtk::EventControllerKey::create();
72 controller->set_propagation_phase(Gtk::PropagationPhase::TARGET);
73 controller->signal_key_pressed().connect(
74 sigc::bind(sigc::mem_fun(*this, &ExampleWindow::box_key_pressed), "target"), after);
75 m_container.add_controller(controller);
76
77 controller = Gtk::EventControllerKey::create();
78 controller->set_propagation_phase(Gtk::PropagationPhase::TARGET);
79 controller->signal_key_pressed().connect(
80 sigc::bind(sigc::mem_fun(*this, &ExampleWindow::window_key_pressed), "target"), after);
81 add_controller(controller);
82
83 // Called in the bubble phase of the event handling.
84 // This is the default, if set_propagation_phase() is not called.
85 controller = Gtk::EventControllerKey::create();
86 controller->set_propagation_phase(Gtk::PropagationPhase::BUBBLE);
87 controller->signal_key_pressed().connect(
88 sigc::bind(sigc::mem_fun(*this, &ExampleWindow::label2_key_pressed), "bubble"), after);
89 m_label2.add_controller(controller);
90
91 controller = Gtk::EventControllerKey::create();
92 controller->set_propagation_phase(Gtk::PropagationPhase::BUBBLE);
93 controller->signal_key_pressed().connect(
94 sigc::bind(sigc::mem_fun(*this, &ExampleWindow::box_key_pressed), "bubble"), after);
95 m_container.add_controller(controller);
96
97 controller = Gtk::EventControllerKey::create();
98 controller->set_propagation_phase(Gtk::PropagationPhase::BUBBLE);
99 controller->signal_key_pressed().connect(
100 sigc::bind(sigc::mem_fun(*this, &ExampleWindow::window_key_pressed), "bubble"), after);
101 add_controller(controller);
102 }
103
104 // By changing the return value we allow, or don't allow, the event to propagate to other elements.
105 bool ExampleWindow::label2_key_pressed(guint keyval, guint, Gdk::ModifierType, const Glib::ustring& phase)
106 {
107 std::cout << "Label, " << phase << " phase" << std::endl;
108
109 if (phase == "bubble")
110 {
111 const gunichar unichar = gdk_keyval_to_unicode(keyval);
112 if (unichar != 0)
113 {
114 if (m_first)
115 {
116 m_label2.set_label("");
117 m_first = false;
118 }
119 if (unichar == '\b')
120 m_label2.set_label("");
121 else
122 {
123 const Glib::ustring newchar(1, unichar);
124 m_label2.set_label(m_label2.get_label() + newchar);
125 }
126 }
127
128 if (!m_checkbutton_can_propagate_up.get_active())
129 return true; // Don't propagate
130 }
131 return false;
132 }
133
134 bool ExampleWindow::box_key_pressed(guint, guint, Gdk::ModifierType, const Glib::ustring& phase)
135 {
136 std::cout << "Box, " << phase << " phase" << std::endl;
137
138 // Let it propagate
139 return false;
140 }
141
142 // This will set the second label's text in the first label every time a key is pressed.
143 bool ExampleWindow::window_key_pressed(guint, guint, Gdk::ModifierType, const Glib::ustring& phase)
144 {
145 if (phase == "capture")
146 std::cout << std::endl;
147 std::cout << "Window, " << phase << " phase";
148
149 // Checking if the second label is on focus, otherwise the label would get
150 // changed by pressing keys on the window (when the label is not on focus),
151 // even if m_checkbutton_can_propagate_up wasn't active.
152 if (phase == "bubble" && m_label2.has_focus())
153 {
154 m_label1.set_label(m_label2.get_label());
155 std::cout << ", " << m_label2.get_label();
156 }
157 std::cout << std::endl;
158
159 if (phase == "capture" && !m_checkbutton_can_propagate_down.get_active())
160 return true; // Don't propagate
161 return false;
162 }
163
164 ExampleWindow::~ExampleWindow()
165 {
166 }