gtkgesturedrag.c 9.27 KB
Newer Older
Carlos Garnacho's avatar
Carlos Garnacho committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* GTK - The GIMP Toolkit
 * Copyright (C) 2014, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Author(s): Carlos Garnacho <carlosg@gnome.org>
 */
19
20
21
22
23
24
25
26
27

/**
 * SECTION:gtkgesturedrag
 * @Short_description: Drag gesture
 * @Title: GtkGestureDrag
 * @See_also: #GtkGestureSwipe
 *
 * #GtkGestureDrag is a #GtkGesture implementation that recognizes drag
 * operations. The drag operation itself can be tracked throught the
Timm Bäder's avatar
Timm Bäder committed
28
29
 * #GtkGestureDrag::drag-begin, #GtkGestureDrag::drag-update and
 * #GtkGestureDrag::drag-end signals, or the relevant coordinates be
30
31
32
 * extracted through gtk_gesture_drag_get_offset() and
 * gtk_gesture_drag_get_start_point().
 */
Carlos Garnacho's avatar
Carlos Garnacho committed
33
#include "config.h"
34
35
#include "gtkgesturedrag.h"
#include "gtkgesturedragprivate.h"
36
#include "gtkintl.h"
37
#include "gtkmarshalers.h"
Carlos Garnacho's avatar
Carlos Garnacho committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

typedef struct _GtkGestureDragPrivate GtkGestureDragPrivate;
typedef struct _EventData EventData;

struct _GtkGestureDragPrivate
{
  gdouble start_x;
  gdouble start_y;
  gdouble last_x;
  gdouble last_y;
};

enum {
  DRAG_BEGIN,
  DRAG_UPDATE,
  DRAG_END,
  N_SIGNALS
};

static guint signals[N_SIGNALS] = { 0 };

59
G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureDrag, gtk_gesture_drag, GTK_TYPE_GESTURE_SINGLE)
Carlos Garnacho's avatar
Carlos Garnacho committed
60

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
static gboolean
gtk_gesture_drag_filter_event (GtkEventController *controller,
                               const GdkEvent     *event)
{
  /* Let touchpad swipe events go through, only if they match n-points  */
  if (event->type == GDK_TOUCHPAD_SWIPE)
    {
      guint n_points;

      g_object_get (G_OBJECT (controller), "n-points", &n_points, NULL);

      if (event->touchpad_swipe.n_fingers == n_points)
        return FALSE;
      else
        return TRUE;
    }

  return GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_drag_parent_class)->filter_event (controller, event);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
81
82
83
84
85
static void
gtk_gesture_drag_begin (GtkGesture       *gesture,
                        GdkEventSequence *sequence)
{
  GtkGestureDragPrivate *priv;
Carlos Garnacho's avatar
Carlos Garnacho committed
86
87
88
  GdkEventSequence *current;

  current = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
Carlos Garnacho's avatar
Carlos Garnacho committed
89
90

  priv = gtk_gesture_drag_get_instance_private (GTK_GESTURE_DRAG (gesture));
Carlos Garnacho's avatar
Carlos Garnacho committed
91
92
93
94
  gtk_gesture_get_point (gesture, current, &priv->start_x, &priv->start_y);
  priv->last_x = priv->start_x;
  priv->last_y = priv->start_y;

Carlos Garnacho's avatar
Carlos Garnacho committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  g_signal_emit (gesture, signals[DRAG_BEGIN], 0, priv->start_x, priv->start_y);
}

static void
gtk_gesture_drag_update (GtkGesture       *gesture,
                         GdkEventSequence *sequence)
{
  GtkGestureDragPrivate *priv;
  gdouble x, y;

  priv = gtk_gesture_drag_get_instance_private (GTK_GESTURE_DRAG (gesture));
  gtk_gesture_get_point (gesture, sequence, &priv->last_x, &priv->last_y);
  x = priv->last_x - priv->start_x;
  y = priv->last_y - priv->start_y;

  g_signal_emit (gesture, signals[DRAG_UPDATE], 0, x, y);
}

static void
gtk_gesture_drag_end (GtkGesture       *gesture,
                      GdkEventSequence *sequence)
{
  GtkGestureDragPrivate *priv;
Carlos Garnacho's avatar
Carlos Garnacho committed
118
  GdkEventSequence *current;
Carlos Garnacho's avatar
Carlos Garnacho committed
119
120
  gdouble x, y;

Carlos Garnacho's avatar
Carlos Garnacho committed
121
122
  current = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));

Carlos Garnacho's avatar
Carlos Garnacho committed
123
  priv = gtk_gesture_drag_get_instance_private (GTK_GESTURE_DRAG (gesture));
Carlos Garnacho's avatar
Carlos Garnacho committed
124
  gtk_gesture_get_point (gesture, current, &priv->last_x, &priv->last_y);
Carlos Garnacho's avatar
Carlos Garnacho committed
125
126
127
128
129
130
131
132
133
134
  x = priv->last_x - priv->start_x;
  y = priv->last_y - priv->start_y;

  g_signal_emit (gesture, signals[DRAG_END], 0, x, y);
}

static void
gtk_gesture_drag_class_init (GtkGestureDragClass *klass)
{
  GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);
135
136
137
  GtkEventControllerClass *event_controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);

  event_controller_class->filter_event = gtk_gesture_drag_filter_event;
Carlos Garnacho's avatar
Carlos Garnacho committed
138
139
140
141
142

  gesture_class->begin = gtk_gesture_drag_begin;
  gesture_class->update = gtk_gesture_drag_update;
  gesture_class->end = gtk_gesture_drag_end;

143
  /**
Carlos Garnacho's avatar
Carlos Garnacho committed
144
   * GtkGestureDrag::drag-begin:
145
146
147
148
149
150
151
152
   * @gesture: the object which received the signal
   * @start_x: X coordinate, relative to the widget allocation
   * @start_y: Y coordinate, relative to the widget allocation
   *
   * This signal is emitted whenever dragging starts.
   *
   * Since: 3.14
   */
Carlos Garnacho's avatar
Carlos Garnacho committed
153
  signals[DRAG_BEGIN] =
154
    g_signal_new (I_("drag-begin"),
Carlos Garnacho's avatar
Carlos Garnacho committed
155
156
157
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkGestureDragClass, drag_begin),
158
159
                  NULL, NULL,
                  _gtk_marshal_VOID__DOUBLE_DOUBLE,
Carlos Garnacho's avatar
Carlos Garnacho committed
160
                  G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
161
162
163
  g_signal_set_va_marshaller (signals[DRAG_BEGIN],
                              G_TYPE_FROM_CLASS (klass),
                              _gtk_marshal_VOID__DOUBLE_DOUBLEv);
164
  /**
Carlos Garnacho's avatar
Carlos Garnacho committed
165
   * GtkGestureDrag::drag-update:
166
167
168
169
170
171
172
173
   * @gesture: the object which received the signal
   * @offset_x: X offset, relative to the start point
   * @offset_y: Y offset, relative to the start point
   *
   * This signal is emitted whenever the dragging point moves.
   *
   * Since: 3.14
   */
Carlos Garnacho's avatar
Carlos Garnacho committed
174
  signals[DRAG_UPDATE] =
175
    g_signal_new (I_("drag-update"),
Carlos Garnacho's avatar
Carlos Garnacho committed
176
177
178
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkGestureDragClass, drag_update),
179
180
                  NULL, NULL,
                  _gtk_marshal_VOID__DOUBLE_DOUBLE,
Carlos Garnacho's avatar
Carlos Garnacho committed
181
                  G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
182
183
184
  g_signal_set_va_marshaller (signals[DRAG_UPDATE],
                              G_TYPE_FROM_CLASS (klass),
                              _gtk_marshal_VOID__DOUBLE_DOUBLEv);
185
  /**
Carlos Garnacho's avatar
Carlos Garnacho committed
186
   * GtkGestureDrag::drag-end:
187
188
189
190
191
192
193
194
   * @gesture: the object which received the signal
   * @offset_x: X offset, relative to the start point
   * @offset_y: Y offset, relative to the start point
   *
   * This signal is emitted whenever the dragging is finished.
   *
   * Since: 3.14
   */
Carlos Garnacho's avatar
Carlos Garnacho committed
195
  signals[DRAG_END] =
196
    g_signal_new (I_("drag-end"),
Carlos Garnacho's avatar
Carlos Garnacho committed
197
198
199
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkGestureDragClass, drag_end),
200
201
                  NULL, NULL,
                  _gtk_marshal_VOID__DOUBLE_DOUBLE,
Carlos Garnacho's avatar
Carlos Garnacho committed
202
                  G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
203
204
205
  g_signal_set_va_marshaller (signals[DRAG_END],
                              G_TYPE_FROM_CLASS (klass),
                              _gtk_marshal_VOID__DOUBLE_DOUBLEv);
Carlos Garnacho's avatar
Carlos Garnacho committed
206
207
208
209
210
211
212
213
214
215
216
}

static void
gtk_gesture_drag_init (GtkGestureDrag *gesture)
{
}

/**
 * gtk_gesture_drag_new:
 * @widget: a #GtkWidget
 *
Matthias Clasen's avatar
Matthias Clasen committed
217
 * Returns a newly created #GtkGesture that recognizes drags.
Carlos Garnacho's avatar
Carlos Garnacho committed
218
219
220
221
222
223
224
225
 *
 * Returns: a newly created #GtkGestureDrag
 *
 * Since: 3.14
 **/
GtkGesture *
gtk_gesture_drag_new (GtkWidget *widget)
{
226
227
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);

Carlos Garnacho's avatar
Carlos Garnacho committed
228
229
230
231
232
233
234
235
  return g_object_new (GTK_TYPE_GESTURE_DRAG,
                       "widget", widget,
                       NULL);
}

/**
 * gtk_gesture_drag_get_start_point:
 * @gesture: a #GtkGesture
236
237
 * @x: (out) (nullable): X coordinate for the drag start point
 * @y: (out) (nullable): Y coordinate for the drag start point
Carlos Garnacho's avatar
Carlos Garnacho committed
238
239
240
241
242
 *
 * If the @gesture is active, this function returns %TRUE
 * and fills in @x and @y with the drag start coordinates,
 * in window-relative coordinates.
 *
Matthias Clasen's avatar
Matthias Clasen committed
243
 * Returns: %TRUE if the gesture is active
Carlos Garnacho's avatar
Carlos Garnacho committed
244
245
246
247
248
 *
 * Since: 3.14
 **/
gboolean
gtk_gesture_drag_get_start_point (GtkGestureDrag *gesture,
Carlos Garnacho's avatar
Carlos Garnacho committed
249
250
                                  gdouble        *x,
                                  gdouble        *y)
Carlos Garnacho's avatar
Carlos Garnacho committed
251
252
{
  GtkGestureDragPrivate *priv;
253
  GdkEventSequence *sequence;
Carlos Garnacho's avatar
Carlos Garnacho committed
254

255
256
257
258
259
  g_return_val_if_fail (GTK_IS_GESTURE_DRAG (gesture), FALSE);

  sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));

  if (!gtk_gesture_handles_sequence (GTK_GESTURE (gesture), sequence))
Carlos Garnacho's avatar
Carlos Garnacho committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    return FALSE;

  priv = gtk_gesture_drag_get_instance_private (gesture);

  if (x)
    *x = priv->start_x;

  if (y)
    *y = priv->start_y;

  return TRUE;
}

/**
Carlos Garnacho's avatar
Carlos Garnacho committed
274
 * gtk_gesture_drag_get_offset:
Carlos Garnacho's avatar
Carlos Garnacho committed
275
 * @gesture: a #GtkGesture
276
277
 * @x: (out) (nullable): X offset for the current point
 * @y: (out) (nullable): Y offset for the current point
Carlos Garnacho's avatar
Carlos Garnacho committed
278
279
280
281
282
 *
 * If the @gesture is active, this function returns %TRUE and
 * fills in @x and @y with the coordinates of the current point,
 * as an offset to the starting drag point.
 *
Matthias Clasen's avatar
Matthias Clasen committed
283
 * Returns: %TRUE if the gesture is active
Carlos Garnacho's avatar
Carlos Garnacho committed
284
285
286
287
 *
 * Since: 3.14
 **/
gboolean
Carlos Garnacho's avatar
Carlos Garnacho committed
288
289
290
gtk_gesture_drag_get_offset (GtkGestureDrag *gesture,
                             gdouble        *x,
                             gdouble        *y)
Carlos Garnacho's avatar
Carlos Garnacho committed
291
292
{
  GtkGestureDragPrivate *priv;
293
294
295
296
297
  GdkEventSequence *sequence;

  g_return_val_if_fail (GTK_IS_GESTURE_DRAG (gesture), FALSE);

  sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
Carlos Garnacho's avatar
Carlos Garnacho committed
298

299
  if (!gtk_gesture_handles_sequence (GTK_GESTURE (gesture), sequence))
Carlos Garnacho's avatar
Carlos Garnacho committed
300
301
302
303
304
    return FALSE;

  priv = gtk_gesture_drag_get_instance_private (gesture);

  if (x)
Carlos Garnacho's avatar
Carlos Garnacho committed
305
    *x = priv->last_x - priv->start_x;
Carlos Garnacho's avatar
Carlos Garnacho committed
306
307

  if (y)
Carlos Garnacho's avatar
Carlos Garnacho committed
308
    *y = priv->last_y - priv->start_y;
Carlos Garnacho's avatar
Carlos Garnacho committed
309
310
311

  return TRUE;
}