1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2014, Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author(s): Carlos Garnacho <carlosg@gnome.org>
18 */
19
20 /**
21 * SECTION:gtkgesturepan
22 * @Short_description: Pan gesture
23 * @Title: GtkGesturePan
24 *
25 * #GtkGesturePan is a #GtkGesture implementation able to recognize
26 * pan gestures, those are drags that are locked to happen along one
27 * axis. The axis that a #GtkGesturePan handles is defined at
28 * construct time, and can be changed through
29 * gtk_gesture_pan_set_orientation().
30 *
31 * When the gesture starts to be recognized, #GtkGesturePan will
32 * attempt to determine as early as possible whether the sequence
33 * is moving in the expected direction, and denying the sequence if
34 * this does not happen.
35 *
36 * Once a panning gesture along the expected axis is recognized,
37 * the #GtkGesturePan::pan signal will be emitted as input events
38 * are received, containing the offset in the given axis.
39 */
40
41 #include "config.h"
42 #include "gtkgesturepan.h"
43 #include "gtkgesturepanprivate.h"
44 #include "gtktypebuiltins.h"
45 #include "gtkprivate.h"
46 #include "gtkintl.h"
47 #include "gtkmarshalers.h"
48
49 typedef struct _GtkGesturePanPrivate GtkGesturePanPrivate;
50
51 struct _GtkGesturePanPrivate
52 {
53 guint orientation : 2;
54 guint panning : 1;
55 };
56
57 enum {
58 PROP_ORIENTATION = 1
59 };
60
61 enum {
62 PAN,
63 N_SIGNALS
64 };
65
66 static guint signals[N_SIGNALS] = { 0 };
67
G_DEFINE_TYPE_WITH_PRIVATE(GtkGesturePan,gtk_gesture_pan,GTK_TYPE_GESTURE_DRAG)68 G_DEFINE_TYPE_WITH_PRIVATE (GtkGesturePan, gtk_gesture_pan, GTK_TYPE_GESTURE_DRAG)
69
70 static void
71 gtk_gesture_pan_get_property (GObject *object,
72 guint prop_id,
73 GValue *value,
74 GParamSpec *pspec)
75 {
76 GtkGesturePanPrivate *priv;
77
78 priv = gtk_gesture_pan_get_instance_private (GTK_GESTURE_PAN (object));
79
80 switch (prop_id)
81 {
82 case PROP_ORIENTATION:
83 g_value_set_enum (value, priv->orientation);
84 break;
85 default:
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87 }
88 }
89
90 static void
gtk_gesture_pan_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)91 gtk_gesture_pan_set_property (GObject *object,
92 guint prop_id,
93 const GValue *value,
94 GParamSpec *pspec)
95 {
96 switch (prop_id)
97 {
98 case PROP_ORIENTATION:
99 gtk_gesture_pan_set_orientation (GTK_GESTURE_PAN (object),
100 g_value_get_enum (value));
101 break;
102 default:
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
104 }
105 }
106
107 static void
direction_from_offset(gdouble offset_x,gdouble offset_y,GtkOrientation orientation,GtkPanDirection * direction)108 direction_from_offset (gdouble offset_x,
109 gdouble offset_y,
110 GtkOrientation orientation,
111 GtkPanDirection *direction)
112 {
113 if (orientation == GTK_ORIENTATION_HORIZONTAL)
114 {
115 if (offset_x > 0)
116 *direction = GTK_PAN_DIRECTION_RIGHT;
117 else
118 *direction = GTK_PAN_DIRECTION_LEFT;
119 }
120 else if (orientation == GTK_ORIENTATION_VERTICAL)
121 {
122 if (offset_y > 0)
123 *direction = GTK_PAN_DIRECTION_DOWN;
124 else
125 *direction = GTK_PAN_DIRECTION_UP;
126 }
127 else
128 g_assert_not_reached ();
129 }
130
131 static gboolean
guess_direction(GtkGesturePan * gesture,gdouble offset_x,gdouble offset_y,GtkPanDirection * direction)132 guess_direction (GtkGesturePan *gesture,
133 gdouble offset_x,
134 gdouble offset_y,
135 GtkPanDirection *direction)
136 {
137 gdouble abs_x, abs_y;
138
139 abs_x = ABS (offset_x);
140 abs_y = ABS (offset_y);
141
142 #define FACTOR 2
143 if (abs_x > abs_y * FACTOR)
144 direction_from_offset (offset_x, offset_y,
145 GTK_ORIENTATION_HORIZONTAL, direction);
146 else if (abs_y > abs_x * FACTOR)
147 direction_from_offset (offset_x, offset_y,
148 GTK_ORIENTATION_VERTICAL, direction);
149 else
150 return FALSE;
151
152 return TRUE;
153 #undef FACTOR
154 }
155
156 static gboolean
check_orientation_matches(GtkGesturePan * gesture,GtkPanDirection direction)157 check_orientation_matches (GtkGesturePan *gesture,
158 GtkPanDirection direction)
159 {
160 GtkGesturePanPrivate *priv = gtk_gesture_pan_get_instance_private (gesture);
161
162 return (((direction == GTK_PAN_DIRECTION_LEFT ||
163 direction == GTK_PAN_DIRECTION_RIGHT) &&
164 priv->orientation == GTK_ORIENTATION_HORIZONTAL) ||
165 ((direction == GTK_PAN_DIRECTION_UP ||
166 direction == GTK_PAN_DIRECTION_DOWN) &&
167 priv->orientation == GTK_ORIENTATION_VERTICAL));
168 }
169
170 static void
gtk_gesture_pan_drag_update(GtkGestureDrag * gesture,gdouble offset_x,gdouble offset_y)171 gtk_gesture_pan_drag_update (GtkGestureDrag *gesture,
172 gdouble offset_x,
173 gdouble offset_y)
174 {
175 GtkGesturePanPrivate *priv;
176 GtkPanDirection direction;
177 GtkGesturePan *pan;
178 gdouble offset;
179
180 pan = GTK_GESTURE_PAN (gesture);
181 priv = gtk_gesture_pan_get_instance_private (pan);
182
183 if (!priv->panning)
184 {
185 if (!guess_direction (pan, offset_x, offset_y, &direction))
186 return;
187
188 if (!check_orientation_matches (pan, direction))
189 {
190 gtk_gesture_set_state (GTK_GESTURE (gesture),
191 GTK_EVENT_SEQUENCE_DENIED);
192 return;
193 }
194
195 priv->panning = TRUE;
196 }
197 else
198 direction_from_offset (offset_x, offset_y, priv->orientation, &direction);
199
200 offset = (priv->orientation == GTK_ORIENTATION_VERTICAL) ?
201 ABS (offset_y) : ABS (offset_x);
202 g_signal_emit (gesture, signals[PAN], 0, direction, offset);
203 }
204
205 static void
gtk_gesture_pan_drag_end(GtkGestureDrag * gesture,gdouble offset_x,gdouble offset_y)206 gtk_gesture_pan_drag_end (GtkGestureDrag *gesture,
207 gdouble offset_x,
208 gdouble offset_y)
209 {
210 GtkGesturePanPrivate *priv;
211
212 priv = gtk_gesture_pan_get_instance_private (GTK_GESTURE_PAN (gesture));
213 priv->panning = FALSE;
214 }
215
216 static void
gtk_gesture_pan_class_init(GtkGesturePanClass * klass)217 gtk_gesture_pan_class_init (GtkGesturePanClass *klass)
218 {
219 GtkGestureDragClass *drag_gesture_class = GTK_GESTURE_DRAG_CLASS (klass);
220 GObjectClass *object_class = G_OBJECT_CLASS (klass);
221
222 object_class->get_property = gtk_gesture_pan_get_property;
223 object_class->set_property = gtk_gesture_pan_set_property;
224
225 drag_gesture_class->drag_update = gtk_gesture_pan_drag_update;
226 drag_gesture_class->drag_end = gtk_gesture_pan_drag_end;
227
228 /**
229 * GtkGesturePan:orientation:
230 *
231 * The expected orientation of pan gestures.
232 *
233 * Since: 3.14
234 */
235 g_object_class_install_property (object_class,
236 PROP_ORIENTATION,
237 g_param_spec_enum ("orientation",
238 P_("Orientation"),
239 P_("Allowed orientations"),
240 GTK_TYPE_ORIENTATION,
241 GTK_ORIENTATION_HORIZONTAL,
242 GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
243
244 /**
245 * GtkGesturePan::pan:
246 * @gesture: The object which received the signal
247 * @direction: current direction of the pan gesture
248 * @offset: Offset along the gesture orientation
249 *
250 * This signal is emitted once a panning gesture along the
251 * expected axis is detected.
252 *
253 * Since: 3.14
254 */
255 signals[PAN] =
256 g_signal_new (I_("pan"),
257 G_TYPE_FROM_CLASS (klass),
258 G_SIGNAL_RUN_LAST,
259 G_STRUCT_OFFSET (GtkGesturePanClass, pan),
260 NULL, NULL,
261 _gtk_marshal_VOID__ENUM_DOUBLE,
262 G_TYPE_NONE, 2, GTK_TYPE_PAN_DIRECTION,
263 G_TYPE_DOUBLE);
264 g_signal_set_va_marshaller (signals[PAN],
265 G_TYPE_FROM_CLASS (klass),
266 _gtk_marshal_VOID__ENUM_DOUBLEv);
267 }
268
269 static void
gtk_gesture_pan_init(GtkGesturePan * gesture)270 gtk_gesture_pan_init (GtkGesturePan *gesture)
271 {
272 GtkGesturePanPrivate *priv;
273
274 priv = gtk_gesture_pan_get_instance_private (gesture);
275 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
276 }
277
278 /**
279 * gtk_gesture_pan_new:
280 * @widget: a #GtkWidget
281 * @orientation: expected orientation
282 *
283 * Returns a newly created #GtkGesture that recognizes pan gestures.
284 *
285 * Returns: a newly created #GtkGesturePan
286 *
287 * Since: 3.14
288 **/
289 GtkGesture *
gtk_gesture_pan_new(GtkWidget * widget,GtkOrientation orientation)290 gtk_gesture_pan_new (GtkWidget *widget,
291 GtkOrientation orientation)
292 {
293 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
294
295 return g_object_new (GTK_TYPE_GESTURE_PAN,
296 "widget", widget,
297 "orientation", orientation,
298 NULL);
299 }
300
301 /**
302 * gtk_gesture_pan_get_orientation:
303 * @gesture: A #GtkGesturePan
304 *
305 * Returns the orientation of the pan gestures that this @gesture expects.
306 *
307 * Returns: the expected orientation for pan gestures
308 *
309 * Since: 3.14
310 */
311 GtkOrientation
gtk_gesture_pan_get_orientation(GtkGesturePan * gesture)312 gtk_gesture_pan_get_orientation (GtkGesturePan *gesture)
313 {
314 GtkGesturePanPrivate *priv;
315
316 g_return_val_if_fail (GTK_IS_GESTURE_PAN (gesture), 0);
317
318 priv = gtk_gesture_pan_get_instance_private (gesture);
319
320 return priv->orientation;
321 }
322
323 /**
324 * gtk_gesture_pan_set_orientation:
325 * @gesture: A #GtkGesturePan
326 * @orientation: expected orientation
327 *
328 * Sets the orientation to be expected on pan gestures.
329 *
330 * Since: 3.14
331 */
332 void
gtk_gesture_pan_set_orientation(GtkGesturePan * gesture,GtkOrientation orientation)333 gtk_gesture_pan_set_orientation (GtkGesturePan *gesture,
334 GtkOrientation orientation)
335 {
336 GtkGesturePanPrivate *priv;
337
338 g_return_if_fail (GTK_IS_GESTURE_PAN (gesture));
339 g_return_if_fail (orientation == GTK_ORIENTATION_HORIZONTAL ||
340 orientation == GTK_ORIENTATION_VERTICAL);
341
342 priv = gtk_gesture_pan_get_instance_private (gesture);
343
344 if (priv->orientation == orientation)
345 return;
346
347 priv->orientation = orientation;
348 g_object_notify (G_OBJECT (gesture), "orientation");
349 }
350