1 /** @file
2 * Collection of shorthands to deal with GDK events.
3 */
4 /* Authors:
5 * Krzysztof KosiĆski <tweenk.pl@gmail.com>
6 *
7 * Copyright (C) 2009 Authors
8 * Released under GNU GPL, read the file 'COPYING' for more information
9 */
11 #ifndef SEEN_UI_TOOL_EVENT_UTILS_H
12 #define SEEN_UI_TOOL_EVENT_UTILS_H
14 #include <gdk/gdk.h>
15 #include <2geom/point.h>
17 struct SPCanvas;
19 namespace Inkscape {
20 namespace UI {
22 inline bool state_held_shift(unsigned state) {
23 return state & GDK_SHIFT_MASK;
24 }
25 inline bool state_held_control(unsigned state) {
26 return state & GDK_CONTROL_MASK;
27 }
28 inline bool state_held_alt(unsigned state) {
29 return state & GDK_MOD1_MASK;
30 }
31 inline bool state_held_only_shift(unsigned state) {
32 return (state & GDK_SHIFT_MASK) && !(state & (GDK_CONTROL_MASK | GDK_MOD1_MASK));
33 }
34 inline bool state_held_only_control(unsigned state) {
35 return (state & GDK_CONTROL_MASK) && !(state & (GDK_SHIFT_MASK | GDK_MOD1_MASK));
36 }
37 inline bool state_held_only_alt(unsigned state) {
38 return (state & GDK_MOD1_MASK) && !(state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK));
39 }
40 inline bool state_held_any_modifiers(unsigned state) {
41 return state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK);
42 }
43 inline bool state_held_no_modifiers(unsigned state) {
44 return !state_held_any_modifiers(state);
45 }
46 template <unsigned button>
47 inline bool state_held_button(unsigned state) {
48 return (button == 0 || button > 5) ? false : state & (GDK_BUTTON1_MASK << (button-1));
49 }
52 /** Checks whether Shift was held when the event was generated. */
53 template <typename E>
54 inline bool held_shift(E const &event) {
55 return state_held_shift(event.state);
56 }
58 /** Checks whether Control was held when the event was generated. */
59 template <typename E>
60 inline bool held_control(E const &event) {
61 return state_held_control(event.state);
62 }
64 /** Checks whether Alt was held when the event was generated. */
65 template <typename E>
66 inline bool held_alt(E const &event) {
67 return state_held_alt(event.state);
68 }
70 /** True if from the set of Ctrl, Shift and Alt only Ctrl was held when the event
71 * was generated. */
72 template <typename E>
73 inline bool held_only_control(E const &event) {
74 return state_held_only_control(event.state);
75 }
77 /** True if from the set of Ctrl, Shift and Alt only Shift was held when the event
78 * was generated. */
79 template <typename E>
80 inline bool held_only_shift(E const &event) {
81 return state_held_only_shift(event.state);
82 }
84 /** True if from the set of Ctrl, Shift and Alt only Alt was held when the event
85 * was generated. */
86 template <typename E>
87 inline bool held_only_alt(E const &event) {
88 return state_held_only_alt(event.state);
89 }
91 template <typename E>
92 inline bool held_no_modifiers(E const &event) {
93 return state_held_no_modifiers(event.state);
94 }
96 template <typename E>
97 inline bool held_any_modifiers(E const &event) {
98 return state_held_any_modifiers(event.state);
99 }
101 template <typename E>
102 inline Geom::Point event_point(E const &event) {
103 return Geom::Point(event.x, event.y);
104 }
106 /** Use like this:
107 * @code if (held_button<2>(event->motion)) { ... @endcode */
108 template <unsigned button, typename E>
109 inline bool held_button(E const &event) {
110 return state_held_button<button>(event.state);
111 }
113 guint shortcut_key(GdkEventKey const &event);
114 unsigned combine_key_events(guint keyval, gint mask);
115 unsigned combine_motion_events(SPCanvas *canvas, GdkEventMotion &event, gint mask);
116 unsigned state_after_event(GdkEvent *event);
118 } // namespace UI
119 } // namespace Inkscape
121 #endif
123 /*
124 Local Variables:
125 mode:c++
126 c-file-style:"stroustrup"
127 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
128 indent-tabs-mode:nil
129 fill-column:99
130 End:
131 */
132 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :