1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>Mouse Events</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-keyboardevents.html" title="Chapter 23. Keyboard and Mouse Events">
<link rel="prev" href="sec-keyboardevents-propagation.html" title="Event Propagation">
<link rel="next" href="chapter-chapter-timeouts.html" title="Chapter 24. Timeouts, I/O and Idle Functions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Mouse Events</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-keyboardevents-propagation.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 23. Keyboard and Mouse Events</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-chapter-timeouts.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-keyboardevents-mouse"></a>Mouse Events</h2></div></div></div>
<p>
Mouse events are similar to keyboard events. There are some differences, though.
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>You use <code class="classname">Gtk::GestureClick</code> instead of
<code class="classname">Gtk::EventControllerKey</code>.</p></li>
<li class="listitem"><p>There are other event classes that can be used for handling
mouse events, for instance <code class="classname">Gtk::EventControllerMotion</code> and
<code class="classname">Gtk::EventControllerScroll</code>.</p></li>
<li class="listitem"><p>Many signal handlers don't return a <code class="literal">bool</code>.
You can't stop those events from propagating.</p></li>
</ul></div>
<p>
</p>
<p>
The event classes that handle mouse events are also useful for handling
events from touchscreens. There are also many subclasses of
<code class="classname">Gtk::EventController</code> which are only (or mainly) useful
for touchscreens. Examples: <code class="classname">Gtk::GestureRotate</code>,
<code class="classname">Gtk::GestureZoom</code>, <code class="classname">Gtk::GestureSwipe</code>,
<code class="classname">Gtk::GestureLongPress</code>.
</p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1EventController.html" target="_top">Reference</a></p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="keyboardevents-mouse-example"></a>Example</h3></div></div></div>
<p>
This is an expanded version of the simple keyboard events example.
</p>
<div class="figure">
<a name="figure-keyboardevents-mouse"></a><p class="title"><b>Figure 23.3. Keyboard and Mouse Events</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/events_mouse.png" alt="Keyboard and Mouse Events"></div>
</div>
</div>
</div>
<br class="figure-break">
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/events/mouse/" target="_top">Source Code</a></p>
<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
~ExampleWindow() override;
private:
// Signal handlers:
void on_window_mouse_pressed(int n_press, double x, double y);
void on_first_button_mouse_pressed(int n_press, double x, double y);
void on_second_button_mouse_pressed(int n_press, double x, double y);
void on_third_button_mouse_pressed(int n_press, double x, double y);
void on_third_button_clicked();
void on_mouse_released(int n_press, double x, double y);
void on_mouse_motion(double x, double y);
bool on_mouse_scroll(double dx, double dy);
bool on_key_pressed(guint keyval, guint keycode, Gdk::ModifierType state);
Gtk::Box m_box;
Gtk::CheckButton m_first;
Gtk::CheckButton m_second;
Gtk::Button m_third;
Glib::RefPtr<Gtk::GestureClick> m_window_click;
Glib::RefPtr<Gtk::GestureClick> m_first_button_click;
Glib::RefPtr<Gtk::GestureClick> m_second_button_click;
};
#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include <map>
#include <iostream>
namespace
{
std::map<unsigned int, Glib::ustring> mouse_buttons
{
{0, "No button"},
{GDK_BUTTON_PRIMARY, "Primary"},
{GDK_BUTTON_MIDDLE, "Middle"},
{GDK_BUTTON_SECONDARY, "Secondary"},
};
}
ExampleWindow::ExampleWindow()
: m_first("First"),
m_second("Second"),
m_third("Click me")
{
set_title("Keyboard and Mouse Events");
m_box.set_margin(10);
set_child(m_box);
// Radio buttons
m_second.set_group(m_first);
m_first.set_active();
// Main container
m_box.set_orientation(Gtk::Orientation::HORIZONTAL);
m_box.append(m_first);
m_box.append(m_second);
m_box.append(m_third);
// Keyboard events
auto key_controller = Gtk::EventControllerKey::create();
key_controller->signal_key_pressed().connect(
sigc::mem_fun(*this, &ExampleWindow::on_key_pressed), false);
add_controller(key_controller);
// Mouse events, window
auto motion_controller = Gtk::EventControllerMotion::create();
motion_controller->signal_motion().connect(
sigc::mem_fun(*this, &ExampleWindow::on_mouse_motion));
add_controller(motion_controller);
m_window_click = Gtk::GestureClick::create();
m_window_click->set_button(0); // All mouse buttons
m_window_click->signal_pressed().connect(
sigc::mem_fun(*this, &ExampleWindow::on_window_mouse_pressed));
add_controller(m_window_click);
auto mouse_click = Gtk::GestureClick::create();
mouse_click->set_button(GDK_BUTTON_PRIMARY);
mouse_click->signal_released().connect(sigc::mem_fun(*this, &ExampleWindow::on_mouse_released));
add_controller(mouse_click);
auto mouse_scroll = Gtk::EventControllerScroll::create();
mouse_scroll->set_flags(Gtk::EventControllerScroll::Flags::VERTICAL);
// Handle mouse wheel rotations
mouse_scroll->signal_scroll().connect(
sigc::mem_fun(*this, &ExampleWindow::on_mouse_scroll), true);
add_controller(mouse_scroll);
// Mouse events, buttons
m_first_button_click = Gtk::GestureClick::create();
m_first_button_click->set_button(GDK_BUTTON_PRIMARY);
m_first_button_click->signal_pressed().connect(
sigc::mem_fun(*this, &ExampleWindow::on_first_button_mouse_pressed));
m_first.add_controller(m_first_button_click);
m_second_button_click = Gtk::GestureClick::create();
m_second_button_click->set_button(GDK_BUTTON_PRIMARY);
m_second_button_click->signal_pressed().connect(
sigc::mem_fun(*this, &ExampleWindow::on_second_button_mouse_pressed));
m_second.add_controller(m_second_button_click);
mouse_click = Gtk::GestureClick::create();
// This mouse event must be handled in the capture phase.
// The GtkButton C class handles the event in the capture phase and
// does not let it propagate to the bubble phase where events are
// handled by default.
mouse_click->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
mouse_click->set_button(GDK_BUTTON_PRIMARY);
mouse_click->signal_pressed().connect(
sigc::mem_fun(*this, &ExampleWindow::on_third_button_mouse_pressed));
m_third.add_controller(mouse_click);
m_third.signal_clicked().connect(
sigc::mem_fun(*this, &ExampleWindow::on_third_button_clicked));
}
void ExampleWindow::on_window_mouse_pressed(int n_press, double x, double y)
{
const auto current_button = mouse_buttons[m_window_click->get_current_button()];
std::cout << "Mouse pressed in window: " << current_button
<< ", " << n_press << ", " << x << ", " << y << std::endl;
}
void ExampleWindow::on_first_button_mouse_pressed(int n_press, double x, double y)
{
const auto current_button = mouse_buttons[m_first_button_click->get_current_button()];
std::cout << "Mouse pressed in first button: " << current_button
<< ", " << n_press << ", " << x << ", " << y << std::endl;
}
void ExampleWindow::on_second_button_mouse_pressed(int n_press, double x, double y)
{
const auto current_button = mouse_buttons[m_second_button_click->get_current_button()];
std::cout << "Mouse pressed in second button: " << current_button
<< ", " << n_press << ", " << x << ", " << y << std::endl;
}
void ExampleWindow::on_third_button_mouse_pressed(int n_press, double x, double y)
{
std::cout << "Mouse pressed in third button: "
<< n_press << ", " << x << ", " << y << std::endl;
}
void ExampleWindow::on_third_button_clicked()
{
std::cout << "Third button clicked" << std::endl;
}
void ExampleWindow::on_mouse_released(int n_press, double x, double y)
{
std::cout << "Mouse released: " << n_press << ", " << x << ", " << y << std::endl;
}
void ExampleWindow::on_mouse_motion(double x, double y)
{
std::cout << "Mouse motion: " << x << ", " << y << std::endl;
}
bool ExampleWindow::on_mouse_scroll(double dx, double dy)
{
std::cout << "Mouse scroll: " << dx << ", " << dy << std::endl;
return true; // handled
}
bool ExampleWindow::on_key_pressed(guint keyval, guint, Gdk::ModifierType state)
{
// Gdk::ModifierType::ALT_MASK -> the 'Alt' key(mask)
// GDK_KEY_1 -> the '1' key
// GDK_KEY_2 -> the '2' key
// Select the first radio button, when we press Alt + 1
if ((keyval == GDK_KEY_1) &&
(state & (Gdk::ModifierType::SHIFT_MASK | Gdk::ModifierType::CONTROL_MASK | Gdk::ModifierType::ALT_MASK)) == Gdk::ModifierType::ALT_MASK)
{
m_first.set_active();
// Returning true, cancels the propagation of the event
return true;
}
else if ((keyval == GDK_KEY_2) &&
(state & (Gdk::ModifierType::SHIFT_MASK | Gdk::ModifierType::CONTROL_MASK | Gdk::ModifierType::ALT_MASK)) == Gdk::ModifierType::ALT_MASK)
{
// and the second radio button, when we press Alt + 2
m_second.set_active();
return true;
}
else if (keyval == GDK_KEY_Escape)
{
// Close the window, when the 'Esc' key is pressed
set_visible(false);
return true;
}
// The event has not been handled
return false;
}
ExampleWindow::~ExampleWindow()
{
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create("org.gtkmm.example");
//Shows the window and returns when it is closed.
return app->make_window_and_run<ExampleWindow>(argc, argv);
}
</code></pre>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-keyboardevents-propagation.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-keyboardevents.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-chapter-timeouts.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Event Propagation </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Chapter 24. Timeouts, I/O and Idle Functions </td>
</tr>
</table>
</div>
</body>
</html>
|