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
/* Gestures
 *
 * Perform gestures on touchscreens and other input devices. This
 * demo reacts to long presses and swipes from all devices, plus
 * multi-touch rotate and zoom gestures.
 */

#include <ctk/ctk.h>

static CtkGesture *rotate = NULL;
static CtkGesture *zoom = NULL;
static gdouble swipe_x = 0;
static gdouble swipe_y = 0;
static gboolean long_pressed = FALSE;

static gboolean
touchpad_swipe_gesture_begin (CtkGesture       *gesture,
                              CdkEventSequence *sequence,<--- Parameter 'sequence' can be declared as pointer to const
                              CtkWidget        *widget G_GNUC_UNUSED)
{
  /* Disallow touchscreen events here */
  if (sequence != NULL)
    ctk_gesture_set_state (gesture, CTK_EVENT_SEQUENCE_DENIED);
  return sequence == NULL;
}

static void
swipe_gesture_swept (CtkGestureSwipe *gesture G_GNUC_UNUSED,
                     gdouble          velocity_x,
                     gdouble          velocity_y,
                     CtkWidget       *widget)
{
  swipe_x = velocity_x / 10;
  swipe_y = velocity_y / 10;
  ctk_widget_queue_draw (widget);
}

static void
long_press_gesture_pressed (CtkGestureLongPress *gesture G_GNUC_UNUSED,
                            gdouble              x G_GNUC_UNUSED,
                            gdouble              y G_GNUC_UNUSED,
                            CtkWidget           *widget)
{
  long_pressed = TRUE;
  ctk_widget_queue_draw (widget);
}

static void
long_press_gesture_end (CtkGesture       *gesture G_GNUC_UNUSED,
                        CdkEventSequence *sequence G_GNUC_UNUSED,
                        CtkWidget        *widget)
{
  long_pressed = FALSE;
  ctk_widget_queue_draw (widget);
}

static void
rotation_angle_changed (CtkGestureRotate *gesture G_GNUC_UNUSED,
                        gdouble           angle G_GNUC_UNUSED,
                        gdouble           delta G_GNUC_UNUSED,
                        CtkWidget        *widget)
{
  ctk_widget_queue_draw (widget);
}

static void
zoom_scale_changed (CtkGestureZoom *gesture G_GNUC_UNUSED,
                    gdouble         scale G_GNUC_UNUSED,
                    CtkWidget      *widget)
{
  ctk_widget_queue_draw (widget);
}

static gboolean
drawing_area_draw (CtkWidget *widget,
                   cairo_t   *cr)
{
  CtkAllocation allocation;

  ctk_widget_get_allocation (widget, &allocation);

  if (swipe_x != 0 || swipe_y != 0)
    {
      cairo_save (cr);
      cairo_set_line_width (cr, 6);
      cairo_move_to (cr, allocation.width / 2,
                     allocation.height / 2);
      cairo_rel_line_to (cr, swipe_x, swipe_y);
      cairo_set_source_rgba (cr, 1, 0, 0, 0.5);
      cairo_stroke (cr);
      cairo_restore (cr);
    }

  if (ctk_gesture_is_recognized (rotate) || ctk_gesture_is_recognized (zoom))
    {
      cairo_pattern_t *pat;
      cairo_matrix_t matrix;
      gdouble angle, scale;
      gdouble x_center, y_center;

      ctk_gesture_get_bounding_box_center (CTK_GESTURE (zoom), &x_center, &y_center);

      cairo_get_matrix (cr, &matrix);
      cairo_matrix_translate (&matrix, x_center, y_center);

      cairo_save (cr);

      angle = ctk_gesture_rotate_get_angle_delta (CTK_GESTURE_ROTATE (rotate));
      cairo_matrix_rotate (&matrix, angle);

      scale = ctk_gesture_zoom_get_scale_delta (CTK_GESTURE_ZOOM (zoom));
      cairo_matrix_scale (&matrix, scale, scale);

      cairo_set_matrix (cr, &matrix);
      cairo_rectangle (cr, -100, -100, 200, 200);

      pat = cairo_pattern_create_linear (-100, 0, 200, 0);
      cairo_pattern_add_color_stop_rgb (pat, 0, 0, 0, 1);
      cairo_pattern_add_color_stop_rgb (pat, 1, 1, 0, 0);
      cairo_set_source (cr, pat);
      cairo_fill (cr);

      cairo_restore (cr);

      cairo_pattern_destroy (pat);
    }

  if (long_pressed)
    {
      cairo_save (cr);
      cairo_arc (cr, allocation.width / 2,
                 allocation.height / 2,
                 50, 0, 2 * G_PI);

      cairo_set_source_rgba (cr, 0, 1, 0, 0.5);
      cairo_stroke (cr);

      cairo_restore (cr);
    }

  return TRUE;
}

CtkWidget *
do_gestures (CtkWidget *do_widget G_GNUC_UNUSED)
{
  static CtkWidget *window = NULL;

  if (!window)
    {
      CtkWidget *drawing_area;
      CtkGesture *gesture;

      window = ctk_window_new (CTK_WINDOW_TOPLEVEL);
      ctk_window_set_default_size (CTK_WINDOW (window), 400, 400);
      ctk_window_set_title (CTK_WINDOW (window), "Gestures");
      g_signal_connect (window, "destroy",
                        G_CALLBACK (ctk_widget_destroyed), &window);

      drawing_area = ctk_drawing_area_new ();
      ctk_container_add (CTK_CONTAINER (window), drawing_area);
      ctk_widget_add_events (drawing_area,
                             CDK_BUTTON_PRESS_MASK | CDK_BUTTON_RELEASE_MASK |
                             CDK_POINTER_MOTION_MASK | CDK_TOUCH_MASK);

      g_signal_connect (drawing_area, "draw",
                        G_CALLBACK (drawing_area_draw), NULL);

      /* Swipe */
      gesture = ctk_gesture_swipe_new (drawing_area);
      g_signal_connect (gesture, "swipe",
                        G_CALLBACK (swipe_gesture_swept), drawing_area);
      ctk_event_controller_set_propagation_phase (CTK_EVENT_CONTROLLER (gesture),
                                                  CTK_PHASE_BUBBLE);
      g_object_weak_ref (G_OBJECT (drawing_area), (GWeakNotify) g_object_unref, gesture);

      /* 3fg swipe for touchpads */
      gesture = g_object_new (CTK_TYPE_GESTURE_SWIPE,
                              "widget", drawing_area,
                              "n-points", 3,
                              NULL);
      g_signal_connect (gesture, "begin",
                        G_CALLBACK (touchpad_swipe_gesture_begin), drawing_area);<--- You might need to cast the function pointer here
      g_signal_connect (gesture, "swipe",
                        G_CALLBACK (swipe_gesture_swept), drawing_area);
      ctk_event_controller_set_propagation_phase (CTK_EVENT_CONTROLLER (gesture),
                                                  CTK_PHASE_BUBBLE);
      g_object_weak_ref (G_OBJECT (drawing_area), (GWeakNotify) g_object_unref, gesture);

      /* Long press */
      gesture = ctk_gesture_long_press_new (drawing_area);
      g_signal_connect (gesture, "pressed",
                        G_CALLBACK (long_press_gesture_pressed), drawing_area);
      g_signal_connect (gesture, "end",
                        G_CALLBACK (long_press_gesture_end), drawing_area);
      ctk_event_controller_set_propagation_phase (CTK_EVENT_CONTROLLER (gesture),
                                                  CTK_PHASE_BUBBLE);
      g_object_weak_ref (G_OBJECT (drawing_area), (GWeakNotify) g_object_unref, gesture);

      /* Rotate */
      rotate = gesture = ctk_gesture_rotate_new (drawing_area);
      g_signal_connect (gesture, "angle-changed",
                        G_CALLBACK (rotation_angle_changed), drawing_area);
      ctk_event_controller_set_propagation_phase (CTK_EVENT_CONTROLLER (gesture),
                                                  CTK_PHASE_BUBBLE);
      g_object_weak_ref (G_OBJECT (drawing_area), (GWeakNotify) g_object_unref, gesture);

      /* Zoom */
      zoom = gesture = ctk_gesture_zoom_new (drawing_area);
      g_signal_connect (gesture, "scale-changed",
                        G_CALLBACK (zoom_scale_changed), drawing_area);
      ctk_event_controller_set_propagation_phase (CTK_EVENT_CONTROLLER (gesture),
                                                  CTK_PHASE_BUBBLE);
      g_object_weak_ref (G_OBJECT (drawing_area), (GWeakNotify) g_object_unref, gesture);
    }

  if (!ctk_widget_get_visible (window))
    ctk_widget_show_all (window);
  else
    ctk_widget_destroy (window);

  return window;
}