| 1 |
/* Gestures |
| 2 |
* |
| 3 |
* Perform gestures on touchscreens and other input devices. This |
| 4 |
* demo reacts to long presses and swipes from all devices, plus |
| 5 |
* multi-touch rotate and zoom gestures from touchscreens. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <gtkmm.h> |
| 9 |
|
| 10 |
class Example_Gestures : public Gtk::Window |
| 11 |
{ |
| 12 |
public: |
| 13 |
Example_Gestures(); |
| 14 |
~Example_Gestures() override; |
| 15 |
|
| 16 |
protected: |
| 17 |
|
| 18 |
// Signal handlers: |
| 19 |
bool on_drawing_area_draw(const ::Cairo::RefPtr< ::Cairo::Context>& cr); |
| 20 |
void on_gesture_swipe_swipe(double velocity_x, double velocity_y); |
| 21 |
void on_gesture_long_press_pressed(double x, double y); |
| 22 |
void on_gesture_long_press_end(GdkEventSequence* sequence); |
| 23 |
void on_gesture_rotate_angle_changed(double angle, double angle_delta); |
| 24 |
void on_gesture_zoom_scale_changed(double scale); |
| 25 |
|
| 26 |
// Child widget: |
| 27 |
Gtk::DrawingArea m_DrawingArea; |
| 28 |
|
| 29 |
// Gestures: |
| 30 |
Glib::RefPtr<Gtk::GestureSwipe> m_GestureSwipe; |
| 31 |
Glib::RefPtr<Gtk::GestureLongPress> m_GestureLongPress; |
| 32 |
Glib::RefPtr<Gtk::GestureRotate> m_GestureRotate; |
| 33 |
Glib::RefPtr<Gtk::GestureZoom> m_GestureZoom; |
| 34 |
|
| 35 |
double m_swipe_x; |
| 36 |
double m_swipe_y; |
| 37 |
bool m_long_pressed; |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
Gtk::Window* do_gestures() |
| 42 |
{ |
| 43 |
return new Example_Gestures(); |
| 44 |
} |
| 45 |
|
| 46 |
Example_Gestures::Example_Gestures() |
| 47 |
: |
| 48 |
m_DrawingArea(), |
| 49 |
m_swipe_x(0.0), m_swipe_y(0.0), m_long_pressed(false) |
| 50 |
{ |
| 51 |
set_title("Gestures demo"); |
| 52 |
set_default_size(400, 400); |
| 53 |
|
| 54 |
add(m_DrawingArea); |
| 55 |
m_DrawingArea.add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | |
| 56 |
Gdk::POINTER_MOTION_MASK | Gdk::TOUCH_MASK); |
| 57 |
m_DrawingArea.signal_draw().connect(sigc::mem_fun(*this, &Example_Gestures::on_drawing_area_draw)); |
| 58 |
|
| 59 |
// Create gestures. |
| 60 |
m_GestureSwipe = Gtk::GestureSwipe::create(m_DrawingArea); |
| 61 |
m_GestureSwipe->set_propagation_phase(Gtk::PHASE_BUBBLE); |
| 62 |
m_GestureSwipe->signal_swipe().connect(sigc::mem_fun(*this, &Example_Gestures::on_gesture_swipe_swipe)); |
| 63 |
m_GestureSwipe->set_touch_only(false); |
| 64 |
|
| 65 |
m_GestureLongPress = Gtk::GestureLongPress::create(m_DrawingArea); |
| 66 |
m_GestureLongPress->set_propagation_phase(Gtk::PHASE_BUBBLE); |
| 67 |
m_GestureLongPress->signal_pressed().connect(sigc::mem_fun(*this, &Example_Gestures::on_gesture_long_press_pressed)); |
| 68 |
m_GestureLongPress->signal_end().connect(sigc::mem_fun(*this, &Example_Gestures::on_gesture_long_press_end)); |
| 69 |
m_GestureLongPress->set_touch_only(false); |
| 70 |
|
| 71 |
m_GestureRotate = Gtk::GestureRotate::create(m_DrawingArea); |
| 72 |
m_GestureRotate->set_propagation_phase(Gtk::PHASE_BUBBLE); |
| 73 |
m_GestureRotate->signal_angle_changed().connect(sigc::mem_fun(*this, &Example_Gestures::on_gesture_rotate_angle_changed)); |
| 74 |
|
| 75 |
m_GestureZoom = Gtk::GestureZoom::create(m_DrawingArea); |
| 76 |
m_GestureZoom->set_propagation_phase(Gtk::PHASE_BUBBLE); |
| 77 |
m_GestureZoom->signal_scale_changed().connect(sigc::mem_fun(*this, &Example_Gestures::on_gesture_zoom_scale_changed)); |
| 78 |
|
| 79 |
show_all(); |
| 80 |
} |
| 81 |
|
| 82 |
Example_Gestures::~Example_Gestures() |
| 83 |
{ |
| 84 |
} |
| 85 |
|
| 86 |
bool Example_Gestures::on_drawing_area_draw(const ::Cairo::RefPtr< ::Cairo::Context>& cr) |
| 87 |
{ |
| 88 |
Gtk::Allocation allocation = m_DrawingArea.get_allocation(); |
| 89 |
|
| 90 |
// Coordinates for the center of the window |
| 91 |
const int xc = allocation.get_width() / 2; |
| 92 |
const int yc = allocation.get_height() / 2; |
| 93 |
|
| 94 |
if (m_GestureRotate->is_recognized() || m_GestureZoom->is_recognized()) |
| 95 |
{ |
| 96 |
Cairo::Matrix matrix = Cairo::translation_matrix(xc, yc); |
| 97 |
cr->save(); |
| 98 |
|
| 99 |
const double angle = m_GestureRotate->get_angle_delta(); |
| 100 |
matrix.rotate(angle); |
| 101 |
|
| 102 |
const double scale = m_GestureZoom->get_scale_delta(); |
| 103 |
matrix.scale(scale, scale); |
| 104 |
|
| 105 |
cr->set_matrix(matrix); |
| 106 |
cr->rectangle(-100, -100, 200, 200); |
| 107 |
|
| 108 |
::Cairo::RefPtr<Cairo::LinearGradient> pattern = Cairo::LinearGradient::create(-100, 0, 200, 0); |
| 109 |
pattern->add_color_stop_rgb(0, 0, 0, 1); |
| 110 |
pattern->add_color_stop_rgb(1, 1, 0, 0); |
| 111 |
cr->set_source(pattern); |
| 112 |
cr->fill(); |
| 113 |
cr->restore(); |
| 114 |
} |
| 115 |
|
| 116 |
if (m_swipe_x != 0.0 || m_swipe_y != 0.0) |
| 117 |
{ |
| 118 |
cr->save(); |
| 119 |
cr->set_line_width(6); |
| 120 |
cr->move_to(xc, yc); |
| 121 |
cr->rel_line_to(m_swipe_x, m_swipe_y); |
| 122 |
cr->set_source_rgba(1.0, 0.0, 0.0, 0.5); |
| 123 |
cr->stroke(); |
| 124 |
cr->restore(); |
| 125 |
} |
| 126 |
|
| 127 |
if (m_long_pressed) |
| 128 |
{ |
| 129 |
cr->save(); |
| 130 |
cr->arc(xc, yc, 50, 0, 2 * G_PI); |
| 131 |
cr->set_source_rgba(0.0, 1.0, 0.0, 0.5); |
| 132 |
cr->stroke(); |
| 133 |
cr->restore(); |
| 134 |
} |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
void Example_Gestures::on_gesture_swipe_swipe(double velocity_x, double velocity_y) |
| 140 |
{ |
| 141 |
m_swipe_x = 0.2 * velocity_x; |
| 142 |
m_swipe_y = 0.2 * velocity_y; |
| 143 |
m_DrawingArea.queue_draw(); |
| 144 |
} |
| 145 |
|
| 146 |
void Example_Gestures::on_gesture_long_press_pressed(double /* x */, double /* y */) |
| 147 |
{ |
| 148 |
m_long_pressed = true; |
| 149 |
m_DrawingArea.queue_draw(); |
| 150 |
} |
| 151 |
|
| 152 |
void Example_Gestures::on_gesture_long_press_end(GdkEventSequence* /* sequence */) |
| 153 |
{ |
| 154 |
m_long_pressed = false; |
| 155 |
m_DrawingArea.queue_draw(); |
| 156 |
} |
| 157 |
|
| 158 |
void Example_Gestures::on_gesture_rotate_angle_changed(double /* angle */, double /* angle_delta */) |
| 159 |
{ |
| 160 |
m_DrawingArea.queue_draw(); |
| 161 |
} |
| 162 |
|
| 163 |
void Example_Gestures::on_gesture_zoom_scale_changed(double /* scale */) |
| 164 |
{ |
| 165 |
m_DrawingArea.queue_draw(); |
| 166 |
} |