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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/* GTK - The GIMP Toolkit
* Copyright (C) 2012, One Laptop Per Child.
* 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>
*/
/**
* SECTION:gtkgesturelongpress
* @Short_description: "Press and Hold" gesture
* @Title: GtkGestureLongPress
*
* #GtkGestureLongPress is a #GtkGesture implementation able to recognize
* long presses, triggering the #GtkGestureLongPress::pressed after the
* timeout is exceeded.
*
* If the touchpoint is lifted before the timeout passes, or if it drifts
* too far of the initial press point, the #GtkGestureLongPress::cancelled
* signal will be emitted.
*/
#include "config.h"
#include "gtkgesturelongpress.h"
#include "gtkgesturelongpressprivate.h"
#include "gtkgestureprivate.h"
#include "gtkmarshalers.h"
#include "gtkdragsource.h"
#include "gtkprivate.h"
#include "gtkintl.h"
#include "gtkmarshalers.h"
typedef struct _GtkGestureLongPressPrivate GtkGestureLongPressPrivate;
enum {
PRESSED,
CANCELLED,
N_SIGNALS
};
enum {
PROP_DELAY_FACTOR = 1
};
struct _GtkGestureLongPressPrivate
{
gdouble initial_x;
gdouble initial_y;
gdouble delay_factor;
guint timeout_id;
guint delay;
guint cancelled : 1;
guint triggered : 1;
};
static guint signals[N_SIGNALS] = { 0 };
G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureLongPress, gtk_gesture_long_press, GTK_TYPE_GESTURE_SINGLE)
static void
gtk_gesture_long_press_init (GtkGestureLongPress *gesture)
{
GtkGestureLongPressPrivate *priv;
priv = gtk_gesture_long_press_get_instance_private (GTK_GESTURE_LONG_PRESS (gesture));
priv->delay_factor = 1.0;
}
static gboolean
gtk_gesture_long_press_check (GtkGesture *gesture)
{
GtkGestureLongPressPrivate *priv;
priv = gtk_gesture_long_press_get_instance_private (GTK_GESTURE_LONG_PRESS (gesture));
if (priv->cancelled)
return FALSE;
return GTK_GESTURE_CLASS (gtk_gesture_long_press_parent_class)->check (gesture);
}
static gboolean
_gtk_gesture_long_press_timeout (gpointer user_data)
{
GtkGestureLongPress *gesture = user_data;
GtkGestureLongPressPrivate *priv;
GdkEventSequence *sequence;
gdouble x, y;
priv = gtk_gesture_long_press_get_instance_private (gesture);
sequence = gtk_gesture_get_last_updated_sequence (GTK_GESTURE (gesture));
gtk_gesture_get_point (GTK_GESTURE (gesture), sequence, &x, &y);
priv->timeout_id = 0;
priv->triggered = TRUE;
g_signal_emit (gesture, signals[PRESSED], 0, x, y);
return G_SOURCE_REMOVE;
}
static void
gtk_gesture_long_press_begin (GtkGesture *gesture,
GdkEventSequence *sequence)
{
GtkGestureLongPressPrivate *priv;
GdkEvent *event;
GdkEventType event_type;
GtkWidget *widget;
int delay;
priv = gtk_gesture_long_press_get_instance_private (GTK_GESTURE_LONG_PRESS (gesture));
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
event = gtk_gesture_get_last_event (gesture, sequence);
if (!event)
return;
event_type = gdk_event_get_event_type (event);
if (event_type != GDK_BUTTON_PRESS &&
event_type != GDK_TOUCH_BEGIN)
return;
widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
g_object_get (gtk_widget_get_settings (widget),
"gtk-long-press-time", &delay,
NULL);
delay = (int)(priv->delay_factor * delay);
gtk_gesture_get_point (gesture, sequence,
&priv->initial_x, &priv->initial_y);
priv->timeout_id = g_timeout_add (delay, _gtk_gesture_long_press_timeout, gesture);
g_source_set_name_by_id (priv->timeout_id, "[gtk] _gtk_gesture_long_press_timeout");
}
static void
gtk_gesture_long_press_update (GtkGesture *gesture,
GdkEventSequence *sequence)
{
GtkGestureLongPressPrivate *priv;
GtkWidget *widget;
gdouble x, y;
widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
priv = gtk_gesture_long_press_get_instance_private (GTK_GESTURE_LONG_PRESS (gesture));
gtk_gesture_get_point (gesture, sequence, &x, &y);
if (gtk_drag_check_threshold (widget, priv->initial_x, priv->initial_y, x, y))
{
if (priv->timeout_id)
{
g_source_remove (priv->timeout_id);
priv->timeout_id = 0;
g_signal_emit (gesture, signals[CANCELLED], 0);
}
priv->cancelled = TRUE;
_gtk_gesture_check (gesture);
}
}
static void
gtk_gesture_long_press_end (GtkGesture *gesture,
GdkEventSequence *sequence)
{
GtkGestureLongPressPrivate *priv;
priv = gtk_gesture_long_press_get_instance_private (GTK_GESTURE_LONG_PRESS (gesture));
if (priv->timeout_id)
{
g_source_remove (priv->timeout_id);
priv->timeout_id = 0;
g_signal_emit (gesture, signals[CANCELLED], 0);
}
priv->cancelled = priv->triggered = FALSE;
}
static void
gtk_gesture_long_press_cancel (GtkGesture *gesture,
GdkEventSequence *sequence)
{
gtk_gesture_long_press_end (gesture, sequence);
GTK_GESTURE_CLASS (gtk_gesture_long_press_parent_class)->cancel (gesture, sequence);
}
static void
gtk_gesture_long_press_sequence_state_changed (GtkGesture *gesture,
GdkEventSequence *sequence,
GtkEventSequenceState state)
{
if (state == GTK_EVENT_SEQUENCE_DENIED)
gtk_gesture_long_press_end (gesture, sequence);
}
static void
gtk_gesture_long_press_finalize (GObject *object)
{
GtkGestureLongPressPrivate *priv;
priv = gtk_gesture_long_press_get_instance_private (GTK_GESTURE_LONG_PRESS (object));
if (priv->timeout_id)
g_source_remove (priv->timeout_id);
G_OBJECT_CLASS (gtk_gesture_long_press_parent_class)->finalize (object);
}
static void
gtk_gesture_long_press_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
switch (property_id)
{
case PROP_DELAY_FACTOR:
g_value_set_double (value, gtk_gesture_long_press_get_delay_factor (GTK_GESTURE_LONG_PRESS (object)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gtk_gesture_long_press_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
switch (property_id)
{
case PROP_DELAY_FACTOR:
gtk_gesture_long_press_set_delay_factor (GTK_GESTURE_LONG_PRESS (object),
g_value_get_double (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gtk_gesture_long_press_class_init (GtkGestureLongPressClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);
object_class->finalize = gtk_gesture_long_press_finalize;
object_class->get_property = gtk_gesture_long_press_get_property;
object_class->set_property = gtk_gesture_long_press_set_property;
gesture_class->check = gtk_gesture_long_press_check;
gesture_class->begin = gtk_gesture_long_press_begin;
gesture_class->update = gtk_gesture_long_press_update;
gesture_class->end = gtk_gesture_long_press_end;
gesture_class->cancel = gtk_gesture_long_press_cancel;
gesture_class->sequence_state_changed = gtk_gesture_long_press_sequence_state_changed;
g_object_class_install_property (object_class,
PROP_DELAY_FACTOR,
g_param_spec_double ("delay-factor",
P_("Delay factor"),
P_("Factor by which to modify the default timeout"),
0.5, 2.0, 1.0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY));
/**
* GtkGestureLongPress::pressed:
* @gesture: the object which received the signal
* @x: the X coordinate where the press happened, relative to the widget allocation
* @y: the Y coordinate where the press happened, relative to the widget allocation
*
* This signal is emitted whenever a press goes unmoved/unreleased longer than
* what the GTK+ defaults tell.
*/
signals[PRESSED] =
g_signal_new (I_("pressed"),
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkGestureLongPressClass, pressed),
NULL, NULL,
_gtk_marshal_VOID__DOUBLE_DOUBLE,
G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
g_signal_set_va_marshaller (signals[PRESSED],
G_TYPE_FROM_CLASS (klass),
_gtk_marshal_VOID__DOUBLE_DOUBLEv);
/**
* GtkGestureLongPress::cancelled:
* @gesture: the object which received the signal
*
* This signal is emitted whenever a press moved too far, or was released
* before #GtkGestureLongPress::pressed happened.
*/
signals[CANCELLED] =
g_signal_new (I_("cancelled"),
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkGestureLongPressClass, cancelled),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
}
/**
* gtk_gesture_long_press_new:
*
* Returns a newly created #GtkGesture that recognizes long presses.
*
* Returns: a newly created #GtkGestureLongPress
**/
GtkGesture *
gtk_gesture_long_press_new (void)
{
return g_object_new (GTK_TYPE_GESTURE_LONG_PRESS,
NULL);
}
/**
* gtk_gesture_long_press_set_delay_factor:
* @gesture: A #GtkGestureLongPress
* @delay_factor: The delay factor to apply
*
* Applies the given delay factor. The default long press time will be
* multiplied by this value. Valid values are in the range [0.5..2.0].
*/
void
gtk_gesture_long_press_set_delay_factor (GtkGestureLongPress *gesture,
double delay_factor)
{
GtkGestureLongPressPrivate *priv = gtk_gesture_long_press_get_instance_private (gesture);
g_return_if_fail (GTK_IS_GESTURE_LONG_PRESS (gesture));
g_return_if_fail (delay_factor >= 0.5);
g_return_if_fail (delay_factor <= 2.0);
priv->delay_factor = delay_factor;
g_object_notify (G_OBJECT (gesture), "delay-factor");
}
/**
* gtk_gesture_long_press_get_delay_factor:
* @gesture: A #GtkGestureLongPress
*
* Returns the delay factor as set by gtk_gesture_long_press_set_delay_factor().
*
* Returns: the delay factor
*/
double
gtk_gesture_long_press_get_delay_factor (GtkGestureLongPress *gesture)
{
GtkGestureLongPressPrivate *priv = gtk_gesture_long_press_get_instance_private (gesture);
g_return_val_if_fail (GTK_IS_GESTURE_LONG_PRESS (gesture), 0);
return priv->delay_factor;
}
|