gtkgesture.c 52.5 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 19
/* 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>
 */
20 21 22

/**
 * SECTION:gtkgesture
Matthias Clasen's avatar
Matthias Clasen committed
23
 * @Short_description: Base class for gestures
24
 * @Title: GtkGesture
25
 * @See_also: #GtkEventController, #GtkGestureSingle
26 27 28 29 30 31 32 33 34 35 36 37
 *
 * #GtkGesture is the base object for gesture recognition, although this
 * object is quite generalized to serve as a base for multi-touch gestures,
 * it is suitable to implement single-touch and pointer-based gestures (using
 * the special %NULL #GdkEventSequence value for these).
 *
 * The number of touches that a #GtkGesture need to be recognized is controlled
 * by the #GtkGesture:n-points property, if a gesture is keeping track of less
 * or more than that number of sequences, it won't check wether the gesture
 * is recognized.
 *
 * As soon as the gesture has the expected number of touches, the gesture will
Carlos Garnacho's avatar
Carlos Garnacho committed
38 39 40
 * run the #GtkGesture::check signal regularly on input events until the gesture
 * is recognized, the criteria to consider a gesture as "recognized" is left to
 * #GtkGesture subclasses.
41 42
 *
 * A recognized gesture will then emit the following signals:
Carlos Garnacho's avatar
Carlos Garnacho committed
43 44 45
 * - #GtkGesture::begin when the gesture is recognized.
 * - A number of #GtkGesture::update, whenever an input event is processed.
 * - #GtkGesture::end when the gesture is no longer recognized.
46
 *
47 48
 * ## Event propagation
 *
49 50 51
 * In order to receive events, a gesture needs to either set a propagation phase
 * through gtk_event_controller_set_propagation_phase(), or feed those manually
 * through gtk_event_controller_handle_event().
52 53 54 55 56
 *
 * In the capture phase, events are propagated from the toplevel down to the
 * target widget, and gestures that are attached to containers above the widget
 * get a chance to interact with the event before it reaches the target.
 *
57 58
 * After the capture phase, GTK+ emits the traditional #GtkWidget::button-press-event,
 * #GtkWidget::button-release-event, #GtkWidget::touch-event, etc signals. Gestures
59
 * with the %GTK_PHASE_TARGET phase are fed events from the default #GtkWidget::event
60 61 62 63 64 65
 * handlers.
 *
 * In the bubble phase, events are propagated up from the target widget to the
 * toplevel, and gestures that are attached to containers above the widget get
 * a chance to interact with events that have not been handled yet.
 *
66 67 68
 * ## States of a sequence # {#touch-sequence-states}
 *
 * Whenever input interaction happens, a single event may trigger a cascade of
69 70
 * #GtkGestures, both across the parents of the widget receiving the event and
 * in parallel within an individual widget. It is a responsibility of the
71 72 73 74
 * widgets using those gestures to set the state of touch sequences accordingly
 * in order to enable cooperation of gestures around the #GdkEventSequences
 * triggering those.
 *
75 76 77 78
 * Within a widget, gestures can be grouped through gtk_gesture_group(),
 * grouped gestures synchronize the state of sequences, so calling
 * gtk_gesture_set_sequence_state() on one will effectively propagate
 * the state throughout the group.
79 80 81 82 83 84
 *
 * By default, all sequences start out in the #GTK_EVENT_SEQUENCE_NONE state,
 * sequences in this state trigger the gesture event handler, but event
 * propagation will continue unstopped by gestures.
 *
 * If a sequence enters into the #GTK_EVENT_SEQUENCE_DENIED state, the gesture
Carlos Garnacho's avatar
Carlos Garnacho committed
85 86 87
 * group will effectively ignore the sequence, letting events go unstopped
 * through the gesture, but the "slot" will still remain occupied while
 * the touch is active.
88 89 90
 *
 * If a sequence enters in the #GTK_EVENT_SEQUENCE_CLAIMED state, the gesture
 * group will grab all interaction on the sequence, by:
Carlos Garnacho's avatar
Carlos Garnacho committed
91
 * - Setting the same sequence to #GTK_EVENT_SEQUENCE_DENIED on every other gesture
92 93
 *   group within the widget, and every gesture on parent widgets in the propagation
 *   chain.
Carlos Garnacho's avatar
Carlos Garnacho committed
94
 * - calling #GtkGesture::cancel on every gesture in widgets underneath in the
95
 *   propagation chain.
Carlos Garnacho's avatar
Carlos Garnacho committed
96 97 98 99 100 101 102 103
 * - Stopping event propagation after the gesture group handles the event.
 *
 * Note: if a sequence is set early to #GTK_EVENT_SEQUENCE_CLAIMED on
 * #GDK_TOUCH_BEGIN/#GDK_BUTTON_PRESS (so those events are captured before
 * reaching the event widget, this implies #GTK_PHASE_CAPTURE), one similar
 * event will emulated if the sequence changes to #GTK_EVENT_SEQUENCE_DENIED.
 * This way event coherence is preserved before event propagation is unstopped
 * again.
104 105 106
 *
 * Sequence states can't be changed freely, see gtk_gesture_set_sequence_state()
 * to know about the possible lifetimes of a #GdkEventSequence.
107 108 109 110 111 112 113 114 115
 *
 * ## Touchpad gestures
 *
 * On the platforms that support it, #GtkGesture will handle transparently
 * touchpad gesture events. The only precautions users of #GtkGesture should do
 * to enable this support are:
 * - Enabling %GDK_TOUCHPAD_GESTURE_MASK on their #GdkWindows
 * - If the gesture has %GTK_PHASE_NONE, ensuring events of type
 *   %GDK_TOUCHPAD_SWIPE and %GDK_TOUCHPAD_PINCH are handled by the #GtkGesture
116 117
 */

Carlos Garnacho's avatar
Carlos Garnacho committed
118
#include "config.h"
119
#include "gtkgesture.h"
120
#include "gtkwidgetprivate.h"
121
#include "gtkeventcontrollerprivate.h"
122
#include "gtkgestureprivate.h"
Carlos Garnacho's avatar
Carlos Garnacho committed
123 124
#include "gtktypebuiltins.h"
#include "gtkprivate.h"
125
#include "gtkmain.h"
Carlos Garnacho's avatar
Carlos Garnacho committed
126 127 128 129 130 131 132
#include "gtkintl.h"

typedef struct _GtkGesturePrivate GtkGesturePrivate;
typedef struct _PointData PointData;

enum {
  PROP_N_POINTS = 1,
133
  PROP_WINDOW
Carlos Garnacho's avatar
Carlos Garnacho committed
134 135 136 137 138 139
};

enum {
  BEGIN,
  END,
  UPDATE,
140
  CANCEL,
Carlos Garnacho's avatar
Carlos Garnacho committed
141 142 143 144 145 146 147
  SEQUENCE_STATE_CHANGED,
  N_SIGNALS
};

struct _PointData
{
  GdkEvent *event;
148 149
  gdouble widget_x;
  gdouble widget_y;
150 151 152 153 154

  /* Acummulators for touchpad events */
  gdouble accum_dx;
  gdouble accum_dy;

155
  guint press_handled : 1;
Carlos Garnacho's avatar
Carlos Garnacho committed
156 157 158 159 160 161 162
  guint state : 2;
};

struct _GtkGesturePrivate
{
  GHashTable *points;
  GdkEventSequence *last_sequence;
163
  GdkWindow *user_window;
Carlos Garnacho's avatar
Carlos Garnacho committed
164 165
  GdkWindow *window;
  GdkDevice *device;
166
  GList *group_link;
Carlos Garnacho's avatar
Carlos Garnacho committed
167 168
  guint n_points;
  guint recognized : 1;
169
  guint touchpad : 1;
Carlos Garnacho's avatar
Carlos Garnacho committed
170 171 172 173 174 175
};

static guint signals[N_SIGNALS] = { 0 };

#define BUTTONS_MASK (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)

176 177 178
#define EVENT_IS_TOUCHPAD_GESTURE(e) ((e)->type == GDK_TOUCHPAD_SWIPE || \
                                      (e)->type == GDK_TOUCHPAD_PINCH)

179 180
GList * _gtk_gesture_get_group_link (GtkGesture *gesture);

Carlos Garnacho's avatar
Carlos Garnacho committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GtkGesture, gtk_gesture, GTK_TYPE_EVENT_CONTROLLER)

static void
gtk_gesture_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  GtkGesturePrivate *priv = gtk_gesture_get_instance_private (GTK_GESTURE (object));

  switch (prop_id)
    {
    case PROP_N_POINTS:
      g_value_set_uint (value, priv->n_points);
      break;
196 197
    case PROP_WINDOW:
      g_value_set_object (value, priv->user_window);
Carlos Garnacho's avatar
Carlos Garnacho committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_gesture_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  GtkGesturePrivate *priv = gtk_gesture_get_instance_private (GTK_GESTURE (object));

  switch (prop_id)
    {
    case PROP_N_POINTS:
      priv->n_points = g_value_get_uint (value);
      break;
217 218 219
    case PROP_WINDOW:
      gtk_gesture_set_window (GTK_GESTURE (object),
                              g_value_get_object (value));
Carlos Garnacho's avatar
Carlos Garnacho committed
220 221 222 223 224 225 226 227 228
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_gesture_finalize (GObject *object)
{
229 230 231 232 233
  GtkGesture *gesture = GTK_GESTURE (object);
  GtkGesturePrivate *priv = gtk_gesture_get_instance_private (gesture);

  gtk_gesture_ungroup (gesture);
  g_list_free (priv->group_link);
Carlos Garnacho's avatar
Carlos Garnacho committed
234 235 236 237 238 239

  g_hash_table_destroy (priv->points);

  G_OBJECT_CLASS (gtk_gesture_parent_class)->finalize (object);
}

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
static guint
_gtk_gesture_get_n_touchpad_points (GtkGesture *gesture,
                                    gboolean    only_active)
{
  GtkGesturePrivate *priv;
  PointData *data;

  priv = gtk_gesture_get_instance_private (gesture);

  if (!priv->touchpad)
    return 0;

  data = g_hash_table_lookup (priv->points, NULL);

  if (!data)
    return 0;

  if (only_active &&
      (data->state == GTK_EVENT_SEQUENCE_DENIED ||
       (data->event->type == GDK_TOUCHPAD_SWIPE &&
        data->event->touchpad_swipe.phase == GDK_TOUCHPAD_GESTURE_PHASE_END) ||
       (data->event->type == GDK_TOUCHPAD_PINCH &&
        data->event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_END)))
    return 0;

  switch (data->event->type)
    {
    case GDK_TOUCHPAD_SWIPE:
      return data->event->touchpad_swipe.n_fingers;
    case GDK_TOUCHPAD_PINCH:
      return data->event->touchpad_pinch.n_fingers;
    default:
      return 0;
    }
}

Carlos Garnacho's avatar
Carlos Garnacho committed
276
static guint
277 278
_gtk_gesture_get_n_touch_points (GtkGesture *gesture,
                                 gboolean    only_active)
Carlos Garnacho's avatar
Carlos Garnacho committed
279 280 281 282 283 284 285 286 287 288 289
{
  GtkGesturePrivate *priv;
  GHashTableIter iter;
  guint n_points = 0;
  PointData *data;

  priv = gtk_gesture_get_instance_private (gesture);
  g_hash_table_iter_init (&iter, priv->points);

  while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &data))
    {
290 291 292 293
      if (only_active &&
          (data->state == GTK_EVENT_SEQUENCE_DENIED ||
           data->event->type == GDK_TOUCH_END ||
           data->event->type == GDK_BUTTON_RELEASE))
Carlos Garnacho's avatar
Carlos Garnacho committed
294 295 296 297 298 299 300 301
        continue;

      n_points++;
    }

  return n_points;
}

302 303 304 305
static guint
_gtk_gesture_get_n_physical_points (GtkGesture *gesture,
                                    gboolean    only_active)
{
306 307 308 309 310 311 312 313
  GtkGesturePrivate *priv;

  priv = gtk_gesture_get_instance_private (gesture);

  if (priv->touchpad)
    return _gtk_gesture_get_n_touchpad_points (gesture, only_active);
  else
    return _gtk_gesture_get_n_touch_points (gesture, only_active);
314 315
}

Carlos Garnacho's avatar
Carlos Garnacho committed
316 317 318 319 320 321 322
static gboolean
gtk_gesture_check_impl (GtkGesture *gesture)
{
  GtkGesturePrivate *priv;
  guint n_points;

  priv = gtk_gesture_get_instance_private (gesture);
323
  n_points = _gtk_gesture_get_n_physical_points (gesture, TRUE);
Carlos Garnacho's avatar
Carlos Garnacho committed
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

  return n_points == priv->n_points;
}

static void
_gtk_gesture_set_recognized (GtkGesture       *gesture,
                             gboolean          recognized,
                             GdkEventSequence *sequence)
{
  GtkGesturePrivate *priv;

  priv = gtk_gesture_get_instance_private (gesture);

  if (priv->recognized == recognized)
    return;

  priv->recognized = recognized;

  if (recognized)
    g_signal_emit (gesture, signals[BEGIN], 0, sequence);
  else
    g_signal_emit (gesture, signals[END], 0, sequence);
}

static gboolean
_gtk_gesture_do_check (GtkGesture *gesture)
{
351 352
  GtkGestureClass *gesture_class;
  gboolean retval = FALSE;
Carlos Garnacho's avatar
Carlos Garnacho committed
353

354
  gesture_class = GTK_GESTURE_GET_CLASS (gesture);
Carlos Garnacho's avatar
Carlos Garnacho committed
355

356 357 358 359
  if (!gesture_class->check)
    return retval;

  retval = gesture_class->check (gesture);
Carlos Garnacho's avatar
Carlos Garnacho committed
360 361 362 363
  return retval;
}

static gboolean
364
_gtk_gesture_has_matching_touchpoints (GtkGesture *gesture)
Carlos Garnacho's avatar
Carlos Garnacho committed
365 366
{
  GtkGesturePrivate *priv = gtk_gesture_get_instance_private (gesture);
367
  guint active_n_points, current_n_points;
Carlos Garnacho's avatar
Carlos Garnacho committed
368

369 370
  current_n_points = _gtk_gesture_get_n_physical_points (gesture, FALSE);
  active_n_points = _gtk_gesture_get_n_physical_points (gesture, TRUE);
Carlos Garnacho's avatar
Carlos Garnacho committed
371

372 373
  return (active_n_points == priv->n_points &&
          current_n_points == priv->n_points);
374 375 376 377 378 379 380 381 382 383 384 385
}

static gboolean
_gtk_gesture_check_recognized (GtkGesture       *gesture,
                               GdkEventSequence *sequence)
{
  GtkGesturePrivate *priv = gtk_gesture_get_instance_private (gesture);
  gboolean has_matching_touchpoints;

  has_matching_touchpoints = _gtk_gesture_has_matching_touchpoints (gesture);

  if (priv->recognized && !has_matching_touchpoints)
Carlos Garnacho's avatar
Carlos Garnacho committed
386
    _gtk_gesture_set_recognized (gesture, FALSE, sequence);
387
  else if (!priv->recognized && has_matching_touchpoints &&
Carlos Garnacho's avatar
Carlos Garnacho committed
388 389 390 391 392 393
           _gtk_gesture_do_check (gesture))
    _gtk_gesture_set_recognized (gesture, TRUE, sequence);

  return priv->recognized;
}

394 395 396 397 398 399 400 401 402
/* Finds the first window pertaining to the controller's widget */
static GdkWindow *
_find_widget_window (GtkGesture *gesture,
                     GdkWindow  *window)
{
  GtkWidget *widget, *window_widget;

  widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));

403
  while (window && !gdk_window_is_destroyed (window))
404 405 406 407 408 409 410 411 412 413 414 415 416
    {
      gdk_window_get_user_data (window, (gpointer*) &window_widget);

      if (window_widget == widget ||
          gtk_widget_get_window (widget) == window)
        return window;

      window = gdk_window_get_effective_parent (window);
    }

  return NULL;
}

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
static void
_update_touchpad_deltas (PointData *data)
{
  GdkEvent *event = data->event;

  if (!event)
    return;

  if (event->type == GDK_TOUCHPAD_SWIPE)
    {
      if (event->touchpad_swipe.phase == GDK_TOUCHPAD_GESTURE_PHASE_BEGIN)
        data->accum_dx = data->accum_dy = 0;
      else if (event->touchpad_swipe.phase == GDK_TOUCHPAD_GESTURE_PHASE_UPDATE)
        {
          data->accum_dx += event->touchpad_swipe.dx;
          data->accum_dy += event->touchpad_swipe.dy;
        }
    }
  else if (event->type == GDK_TOUCHPAD_PINCH)
    {
      if (event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_BEGIN)
        data->accum_dx = data->accum_dy = 0;
      else if (event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_UPDATE)
        {
          data->accum_dx += event->touchpad_pinch.dx;
          data->accum_dy += event->touchpad_pinch.dy;
        }
    }
}

static void
_get_event_coordinates (PointData *data,
                        gdouble   *x,
                        gdouble   *y)
{
  gdouble event_x, event_y;

  g_assert (data->event != NULL);

  gdk_event_get_coords (data->event, &event_x, &event_y);
  event_x += data->accum_dx;
  event_y += data->accum_dy;

  if (x)
    *x = event_x;
  if (y)
    *y = event_y;
}

466 467 468 469 470 471 472 473 474 475 476
static void
_update_widget_coordinates (GtkGesture *gesture,
                            PointData  *data)
{
  GdkWindow *window, *event_widget_window;
  GtkWidget *event_widget, *widget;
  GtkAllocation allocation;
  gdouble event_x, event_y;
  gint wx, wy, x, y;

  event_widget = gtk_get_event_widget (data->event);
477 478 479 480

  if (!event_widget)
    return;

481 482
  widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
  event_widget_window = gtk_widget_get_window (event_widget);
483
  _get_event_coordinates (data, &event_x, &event_y);
484 485
  window = data->event->any.window;

486
  while (window && window != event_widget_window)
487 488 489 490 491 492 493
    {
      gdk_window_get_position (window, &wx, &wy);
      event_x += wx;
      event_y += wy;
      window = gdk_window_get_effective_parent (window);
    }

494 495 496
  if (!window)
    return;

497 498 499 500 501 502 503 504 505 506 507 508 509
  if (!gtk_widget_get_has_window (event_widget))
    {
      gtk_widget_get_allocation (event_widget, &allocation);
      event_x -= allocation.x;
      event_y -= allocation.y;
    }

  gtk_widget_translate_coordinates (event_widget, widget,
                                    event_x, event_y, &x, &y);
  data->widget_x = x;
  data->widget_y = y;
}

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
static GtkEventSequenceState
gtk_gesture_get_group_state (GtkGesture       *gesture,
                             GdkEventSequence *sequence)
{
  GtkEventSequenceState state = GTK_EVENT_SEQUENCE_NONE;
  GList *group_elem;

  group_elem = g_list_first (_gtk_gesture_get_group_link (gesture));

  for (; group_elem; group_elem = group_elem->next)
    {
      if (group_elem->data == gesture)
        continue;
      if (!gtk_gesture_handles_sequence (group_elem->data, sequence))
        continue;

      state = gtk_gesture_get_sequence_state (group_elem->data, sequence);
      break;
    }

  return state;
}

Carlos Garnacho's avatar
Carlos Garnacho committed
533 534 535 536 537 538
static gboolean
_gtk_gesture_update_point (GtkGesture     *gesture,
                           const GdkEvent *event,
                           gboolean        add)
{
  GdkEventSequence *sequence;
539
  GdkWindow *widget_window;
Carlos Garnacho's avatar
Carlos Garnacho committed
540 541
  GtkGesturePrivate *priv;
  GdkDevice *device;
542
  gboolean existed, touchpad;
Carlos Garnacho's avatar
Carlos Garnacho committed
543 544
  PointData *data;

545
  if (!gdk_event_get_coords (event, NULL, NULL))
Carlos Garnacho's avatar
Carlos Garnacho committed
546 547 548 549 550 551 552 553
    return FALSE;

  device = gdk_event_get_device (event);

  if (!device)
    return FALSE;

  priv = gtk_gesture_get_instance_private (gesture);
554
  widget_window = _find_widget_window (gesture, event->any.window);
Carlos Garnacho's avatar
Carlos Garnacho committed
555

556
  if (!widget_window)
557
    widget_window = event->any.window;
558

559 560
  touchpad = EVENT_IS_TOUCHPAD_GESTURE (event);

561 562 563 564 565 566 567 568 569 570 571
  if (add)
    {
      /* If the event happens with the wrong device, or
       * on the wrong window, ignore.
       */
      if (priv->device && priv->device != device)
        return FALSE;
      if (priv->window && priv->window != widget_window)
        return FALSE;
      if (priv->user_window && priv->user_window != widget_window)
        return FALSE;
572 573 574 575 576 577

      /* Make touchpad and touchscreen gestures mutually exclusive */
      if (touchpad && g_hash_table_size (priv->points) > 0)
        return FALSE;
      else if (!touchpad && priv->touchpad)
        return FALSE;
578 579
    }
  else if (!priv->device || !priv->window)
Carlos Garnacho's avatar
Carlos Garnacho committed
580 581 582
    return FALSE;

  sequence = gdk_event_get_event_sequence (event);
583 584 585
  existed = g_hash_table_lookup_extended (priv->points, sequence,
                                          NULL, (gpointer *) &data);
  if (!existed)
Carlos Garnacho's avatar
Carlos Garnacho committed
586
    {
587 588
      GtkEventSequenceState group_state;

Carlos Garnacho's avatar
Carlos Garnacho committed
589 590 591 592 593
      if (!add)
        return FALSE;

      if (g_hash_table_size (priv->points) == 0)
        {
594
          priv->window = widget_window;
Carlos Garnacho's avatar
Carlos Garnacho committed
595
          priv->device = device;
596
          priv->touchpad = touchpad;
Carlos Garnacho's avatar
Carlos Garnacho committed
597 598 599 600
        }

      data = g_new0 (PointData, 1);
      g_hash_table_insert (priv->points, sequence, data);
601 602 603

      group_state = gtk_gesture_get_group_state (gesture, sequence);
      gtk_gesture_set_sequence_state (gesture, sequence, group_state);
Carlos Garnacho's avatar
Carlos Garnacho committed
604 605 606 607 608 609
    }

  if (data->event)
    gdk_event_free (data->event);

  data->event = gdk_event_copy (event);
610
  _update_touchpad_deltas (data);
611
  _update_widget_coordinates (gesture, data);
Carlos Garnacho's avatar
Carlos Garnacho committed
612

613 614 615 616
  /* Deny the sequence right away if the expected
   * number of points is exceeded, so this sequence
   * can be tracked with gtk_gesture_handles_sequence().
   */
617
  if (!existed && _gtk_gesture_get_n_physical_points (gesture, FALSE) > priv->n_points)
618 619 620
    gtk_gesture_set_sequence_state (gesture, sequence,
                                    GTK_EVENT_SEQUENCE_DENIED);

Carlos Garnacho's avatar
Carlos Garnacho committed
621 622 623
  return TRUE;
}

624 625 626 627 628 629 630 631 632 633 634
static void
_gtk_gesture_check_empty (GtkGesture *gesture)
{
  GtkGesturePrivate *priv;

  priv = gtk_gesture_get_instance_private (gesture);

  if (g_hash_table_size (priv->points) == 0)
    {
      priv->window = NULL;
      priv->device = NULL;
635
      priv->touchpad = FALSE;
636 637 638
    }
}

Carlos Garnacho's avatar
Carlos Garnacho committed
639 640 641 642 643 644 645 646 647 648 649 650
static void
_gtk_gesture_remove_point (GtkGesture     *gesture,
                           const GdkEvent *event)
{
  GdkEventSequence *sequence;
  GtkGesturePrivate *priv;
  GdkDevice *device;

  sequence = gdk_event_get_event_sequence (event);
  device = gdk_event_get_device (event);
  priv = gtk_gesture_get_instance_private (gesture);

651 652 653
  if (priv->device != device)
    return;

Carlos Garnacho's avatar
Carlos Garnacho committed
654
  g_hash_table_remove (priv->points, sequence);
655 656
  _gtk_gesture_check_empty (gesture);
}
Carlos Garnacho's avatar
Carlos Garnacho committed
657

658 659 660 661 662 663 664 665 666 667 668
static void
_gtk_gesture_cancel_all (GtkGesture *gesture)
{
  GdkEventSequence *sequence;
  GtkGesturePrivate *priv;
  GHashTableIter iter;

  priv = gtk_gesture_get_instance_private (gesture);
  g_hash_table_iter_init (&iter, priv->points);

  while (g_hash_table_iter_next (&iter, (gpointer*) &sequence, NULL))
Carlos Garnacho's avatar
Carlos Garnacho committed
669
    {
670 671 672
      g_signal_emit (gesture, signals[CANCEL], 0, sequence);
      g_hash_table_iter_remove (&iter);
      _gtk_gesture_check_recognized (gesture, sequence);
Carlos Garnacho's avatar
Carlos Garnacho committed
673
    }
674 675

  _gtk_gesture_check_empty (gesture);
Carlos Garnacho's avatar
Carlos Garnacho committed
676 677
}

678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
static gboolean
gesture_within_window (GtkGesture *gesture,
                       GdkWindow  *parent)
{
  GdkWindow *window;
  GtkWidget *widget;

  widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
  window = gtk_widget_get_window (widget);

  while (window)
    {
      if (window == parent)
        return TRUE;

      window = gdk_window_get_effective_parent (window);
    }

  return FALSE;
}

699 700 701 702 703 704 705 706 707 708 709 710
static gboolean
gtk_gesture_filter_event (GtkEventController *controller,
                          const GdkEvent     *event)
{
  /* Even though GtkGesture handles these events, we want
   * touchpad gestures disabled by default, it will be
   * subclasses which punch the holes in for the events
   * they can possibly handle.
   */
  return EVENT_IS_TOUCHPAD_GESTURE (event);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
711 712 713 714 715 716 717
static gboolean
gtk_gesture_handle_event (GtkEventController *controller,
                          const GdkEvent     *event)
{
  GtkGesture *gesture = GTK_GESTURE (controller);
  GdkEventSequence *sequence;
  GtkGesturePrivate *priv;
718
  GdkDevice *source_device;
719
  gboolean was_recognized;
Carlos Garnacho's avatar
Carlos Garnacho committed
720

721 722 723 724 725
  source_device = gdk_event_get_source_device (event);

  if (!source_device)
    return FALSE;

Carlos Garnacho's avatar
Carlos Garnacho committed
726 727
  priv = gtk_gesture_get_instance_private (gesture);
  sequence = gdk_event_get_event_sequence (event);
728
  was_recognized = gtk_gesture_is_recognized (gesture);
Carlos Garnacho's avatar
Carlos Garnacho committed
729

730 731
  if (gtk_gesture_get_sequence_state (gesture, sequence) != GTK_EVENT_SEQUENCE_DENIED)
    priv->last_sequence = sequence;
Carlos Garnacho's avatar
Carlos Garnacho committed
732

733
  if (event->type == GDK_BUTTON_PRESS ||
734 735 736 737 738
      event->type == GDK_TOUCH_BEGIN ||
      (event->type == GDK_TOUCHPAD_SWIPE &&
       event->touchpad_swipe.phase == GDK_TOUCHPAD_GESTURE_PHASE_BEGIN) ||
      (event->type == GDK_TOUCHPAD_PINCH &&
       event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_BEGIN))
Carlos Garnacho's avatar
Carlos Garnacho committed
739
    {
740
      if (_gtk_gesture_update_point (gesture, event, TRUE))
741
        {
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
          gboolean triggered_recognition;

          triggered_recognition =
            !was_recognized && _gtk_gesture_has_matching_touchpoints (gesture);

          if (_gtk_gesture_check_recognized (gesture, sequence))
            {
              PointData *data;

              data = g_hash_table_lookup (priv->points, sequence);

              /* If the sequence was claimed early, the press event will be consumed */
              if (gtk_gesture_get_sequence_state (gesture, sequence) == GTK_EVENT_SEQUENCE_CLAIMED)
                data->press_handled = TRUE;
            }
          else if (triggered_recognition && g_hash_table_size (priv->points) == 0)
            {
              /* Recognition was triggered, but the gesture reset during
               * ::begin emission. Still, recognition was strictly triggered,
               * so the event should be consumed.
               */
              return TRUE;
            }
765
        }
766 767
    }
  else if (event->type == GDK_BUTTON_RELEASE ||
768 769 770 771 772
           event->type == GDK_TOUCH_END ||
           (event->type == GDK_TOUCHPAD_SWIPE &&
            event->touchpad_swipe.phase == GDK_TOUCHPAD_GESTURE_PHASE_END) ||
           (event->type == GDK_TOUCHPAD_PINCH &&
            event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_END))
773
    {
Carlos Garnacho's avatar
Carlos Garnacho committed
774 775
      if (_gtk_gesture_update_point (gesture, event, FALSE))
        {
776 777
          if (was_recognized &&
              _gtk_gesture_check_recognized (gesture, sequence))
Carlos Garnacho's avatar
Carlos Garnacho committed
778 779 780 781
            g_signal_emit (gesture, signals[UPDATE], 0, sequence);

          _gtk_gesture_remove_point (gesture, event);
        }
782 783
    }
  else if (event->type == GDK_MOTION_NOTIFY ||
784 785 786 787 788
           event->type == GDK_TOUCH_UPDATE ||
           (event->type == GDK_TOUCHPAD_SWIPE &&
            event->touchpad_swipe.phase == GDK_TOUCHPAD_GESTURE_PHASE_UPDATE) ||
           (event->type == GDK_TOUCHPAD_PINCH &&
            event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_UPDATE))
789 790 791 792 793
    {
      if (event->type == GDK_MOTION_NOTIFY)
        {
          if ((event->motion.state & BUTTONS_MASK) == 0)
            return FALSE;
Carlos Garnacho's avatar
Carlos Garnacho committed
794

795 796 797
          if (event->motion.is_hint)
            gdk_event_request_motions (&event->motion);
        }
Carlos Garnacho's avatar
Carlos Garnacho committed
798

799
      if (_gtk_gesture_update_point (gesture, event, FALSE) &&
Carlos Garnacho's avatar
Carlos Garnacho committed
800 801
          _gtk_gesture_check_recognized (gesture, sequence))
        g_signal_emit (gesture, signals[UPDATE], 0, sequence);
802 803 804
    }
  else if (event->type == GDK_TOUCH_CANCEL)
    {
805 806 807 808 809 810 811 812 813 814
      if (!priv->touchpad)
        _gtk_gesture_cancel_sequence (gesture, sequence);
    }
  else if ((event->type == GDK_TOUCHPAD_SWIPE &&
            event->touchpad_swipe.phase == GDK_TOUCHPAD_GESTURE_PHASE_CANCEL) ||
           (event->type == GDK_TOUCHPAD_PINCH &&
            event->touchpad_pinch.phase == GDK_TOUCHPAD_GESTURE_PHASE_CANCEL))
    {
      if (priv->touchpad)
        _gtk_gesture_cancel_sequence (gesture, sequence);
815 816 817
    }
  else if (event->type == GDK_GRAB_BROKEN)
    {
818 819 820 821 822
      if (!event->grab_broken.grab_window ||
          !gesture_within_window (gesture, event->grab_broken.grab_window))
        _gtk_gesture_cancel_all (gesture);

      return FALSE;
823 824 825
    }
  else
    {
Carlos Garnacho's avatar
Carlos Garnacho committed
826 827 828 829 830 831 832 833 834 835
      /* Unhandled event */
      return FALSE;
    }

  if (gtk_gesture_get_sequence_state (gesture, sequence) != GTK_EVENT_SEQUENCE_CLAIMED)
    return FALSE;

  return priv->recognized;
}

836 837 838 839 840 841
static void
gtk_gesture_reset (GtkEventController *controller)
{
  _gtk_gesture_cancel_all (GTK_GESTURE (controller));
}

Carlos Garnacho's avatar
Carlos Garnacho committed
842 843 844 845 846 847 848 849 850 851
static void
gtk_gesture_class_init (GtkGestureClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);

  object_class->get_property = gtk_gesture_get_property;
  object_class->set_property = gtk_gesture_set_property;
  object_class->finalize = gtk_gesture_finalize;

852
  controller_class->filter_event = gtk_gesture_filter_event;
Carlos Garnacho's avatar
Carlos Garnacho committed
853
  controller_class->handle_event = gtk_gesture_handle_event;
854
  controller_class->reset = gtk_gesture_reset;
Carlos Garnacho's avatar
Carlos Garnacho committed
855 856 857

  klass->check = gtk_gesture_check_impl;

858 859 860 861 862 863 864 865
  /**
   * GtkGesture:n-points:
   *
   * The number of touch points that trigger recognition on this gesture,
   * 
   *
   * Since: 3.14
   */
Carlos Garnacho's avatar
Carlos Garnacho committed
866 867 868 869 870 871 872 873
  g_object_class_install_property (object_class,
                                   PROP_N_POINTS,
                                   g_param_spec_uint ("n-points",
                                                      P_("Number of points"),
                                                      P_("Number of points needed "
                                                         "to trigger the gesture"),
                                                      1, G_MAXUINT, 1,
                                                      GTK_PARAM_READWRITE |
874 875 876 877 878 879 880 881 882
						      G_PARAM_CONSTRUCT_ONLY));
  /**
   * GtkGesture:window:
   *
   * If non-%NULL, the gesture will only listen for events that happen on
   * this #GdkWindow, or a child of it.
   *
   * Since: 3.14
   */
883 884
  g_object_class_install_property (object_class,
                                   PROP_WINDOW,
885 886 887 888 889
                                   g_param_spec_object ("window",
                                                        P_("GdkWindow to receive events about"),
                                                        P_("GdkWindow to receive events about"),
                                                        GDK_TYPE_WINDOW,
                                                        GTK_PARAM_READWRITE));
890
  /**
Carlos Garnacho's avatar
Carlos Garnacho committed
891
   * GtkGesture::begin:
892 893 894 895
   * @gesture: the object which received the signal
   * @sequence: the #GdkEventSequence that made the gesture to be recognized
   *
   * This signal is emitted when the gesture is recognized. This means the
Carlos Garnacho's avatar
Carlos Garnacho committed
896
   * number of touch sequences matches #GtkGesture:n-points, and the #GtkGesture::check
897 898 899 900 901 902 903 904
   * handler(s) returned #TRUE.
   *
   * Note: These conditions may also happen when an extra touch (eg. a third touch
   * on a 2-touches gesture) is lifted, in that situation @sequence won't pertain
   * to the current set of active touches, so don't rely on this being true.
   *
   * Since: 3.14
   */
Carlos Garnacho's avatar
Carlos Garnacho committed
905
  signals[BEGIN] =
906
    g_signal_new (I_("begin"),
Carlos Garnacho's avatar
Carlos Garnacho committed
907 908 909 910
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkGestureClass, begin),
                  NULL, NULL, NULL,
911
                  G_TYPE_NONE, 1, GDK_TYPE_EVENT_SEQUENCE);
912
  /**
Carlos Garnacho's avatar
Carlos Garnacho committed
913
   * GtkGesture::end:
914
   * @gesture: the object which received the signal
Matthias Clasen's avatar
Matthias Clasen committed
915
   * @sequence: the #GdkEventSequence that made gesture recognition to finish
916 917
   *
   * This signal is emitted when @gesture either stopped recognizing the event
Carlos Garnacho's avatar
Carlos Garnacho committed
918
   * sequences as something to be handled (the #GtkGesture::check handler returned
Matthias Clasen's avatar
Matthias Clasen committed
919
   * %FALSE), or the number of touch sequences became higher or lower than
920 921
   * #GtkGesture:n-points.
   *
922 923 924 925 926
   * Note: @sequence might not pertain to the group of sequences that were
   * previously triggering recognition on @gesture (ie. a just pressed touch
   * sequence that exceeds #GtkGesture:n-points). This situation may be detected
   * by checking through gtk_gesture_handles_sequence().
   *
927 928
   * Since: 3.14
   */
Carlos Garnacho's avatar
Carlos Garnacho committed
929
  signals[END] =
930
    g_signal_new (I_("end"),
Carlos Garnacho's avatar
Carlos Garnacho committed
931 932 933 934
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkGestureClass, end),
                  NULL, NULL, NULL,
935
                  G_TYPE_NONE, 1, GDK_TYPE_EVENT_SEQUENCE);
936
  /**
Carlos Garnacho's avatar
Carlos Garnacho committed
937
   * GtkGesture::update:
938
   * @gesture: the object which received the signal
Matthias Clasen's avatar
Matthias Clasen committed
939
   * @sequence: the #GdkEventSequence that was updated
940 941 942 943 944 945
   *
   * This signal is emitted whenever an event is handled while the gesture is
   * recognized. @sequence is guaranteed to pertain to the set of active touches.
   *
   * Since: 3.14
   */
Carlos Garnacho's avatar
Carlos Garnacho committed
946
  signals[UPDATE] =
947
    g_signal_new (I_("update"),
Carlos Garnacho's avatar
Carlos Garnacho committed
948 949 950 951
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkGestureClass, update),
                  NULL, NULL, NULL,
952
                  G_TYPE_NONE, 1, GDK_TYPE_EVENT_SEQUENCE);
953
  /**
Carlos Garnacho's avatar
Carlos Garnacho committed
954
   * GtkGesture::cancel:
955
   * @gesture: the object which received the signal
Matthias Clasen's avatar
Matthias Clasen committed
956
   * @sequence: the #GdkEventSequence that was cancelled
957 958 959 960 961 962 963 964 965 966
   *
   * This signal is emitted whenever a sequence is cancelled. This usually
   * happens on active touches when gtk_event_controller_reset() is called
   * on @gesture (manually, due to grabs...), or the individual @sequence
   * was claimed by parent widgets' controllers (see gtk_gesture_set_sequence_state()).
   *
   * @gesture must forget everything about @sequence as a reaction to this signal.
   *
   * Since: 3.14
   */
967
  signals[CANCEL] =
968
    g_signal_new (I_("cancel"),
969 970 971 972
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkGestureClass, cancel),
                  NULL, NULL, NULL,
973
                  G_TYPE_NONE, 1, GDK_TYPE_EVENT_SEQUENCE);
974
  /**
Carlos Garnacho's avatar
Carlos Garnacho committed
975
   * GtkGesture::sequence-state-changed:
976
   * @gesture: the object which received the signal
Matthias Clasen's avatar
Matthias Clasen committed
977
   * @sequence: the #GdkEventSequence that was cancelled
978 979 980 981 982 983 984 985
   * @state: the new sequence state
   *
   * This signal is emitted whenever a sequence state changes. See
   * gtk_gesture_set_sequence_state() to know more about the expectable
   * sequence lifetimes.
   *
   * Since: 3.14
   */
Carlos Garnacho's avatar
Carlos Garnacho committed
986
  signals[SEQUENCE_STATE_CHANGED] =
987
    g_signal_new (I_("sequence-state-changed"),
Carlos Garnacho's avatar
Carlos Garnacho committed
988 989 990 991
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkGestureClass, sequence_state_changed),
                  NULL, NULL, NULL,
992
                  G_TYPE_NONE, 2, GDK_TYPE_EVENT_SEQUENCE,
Carlos Garnacho's avatar
Carlos Garnacho committed
993 994 995
                  GTK_TYPE_EVENT_SEQUENCE_STATE);
}

996 997 998 999 1000 1001 1002 1003 1004 1005 1006
static void
free_point_data (gpointer data)
{
  PointData *point = data;

  if (point->event)
    gdk_event_free (point->event);

  g_free (point);
}

Carlos Garnacho's avatar
Carlos Garnacho committed
1007 1008 1009 1010 1011 1012 1013
static void
gtk_gesture_init (GtkGesture *gesture)
{
  GtkGesturePrivate *priv;

  priv = gtk_gesture_get_instance_private (gesture);
  priv->points = g_hash_table_new_full (NULL, NULL, NULL,
1014
                                        (GDestroyNotify) free_point_data);
1015
  gtk_event_controller_set_event_mask (GTK_EVENT_CONTROLLER (gesture),
1016 1017
                                       GDK_TOUCH_MASK |
                                       GDK_TOUCHPAD_GESTURE_MASK);
1018 1019

  priv->group_link = g_list_prepend (NULL, gesture);
Carlos Garnacho's avatar
Carlos Garnacho committed
1020 1021 1022 1023 1024 1025 1026 1027 1028
}

/**
 * gtk_gesture_get_device:
 * @gesture: a #GtkGesture
 *
 * Returns the master #GdkDevice that is currently operating
 * on @gesture, or %NULL if the gesture is not being interacted.
 *
1029
 * Returns: (nullable) (transfer none): a #GdkDevice, or %NULL
Carlos Garnacho's avatar
Carlos Garnacho committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
 *
 * Since: 3.14
 **/
GdkDevice *
gtk_gesture_get_device (GtkGesture *gesture)
{
  GtkGesturePrivate *priv;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), NULL);

  priv = gtk_gesture_get_instance_private (gesture);

  return priv->device;
}

/**
 * gtk_gesture_get_sequence_state:
 * @gesture: a #GtkGesture
 * @sequence: a #GdkEventSequence
 *
 * Returns the @sequence state, as seen by @gesture.
 *
Matthias Clasen's avatar
Matthias Clasen committed
1052
 * Returns: The sequence state in @gesture
Carlos Garnacho's avatar
Carlos Garnacho committed
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
 *
 * Since: 3.14
 **/
GtkEventSequenceState
gtk_gesture_get_sequence_state (GtkGesture       *gesture,
                                GdkEventSequence *sequence)
{
  GtkGesturePrivate *priv;
  PointData *data;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture),
                        GTK_EVENT_SEQUENCE_NONE);

  priv = gtk_gesture_get_instance_private (gesture);
  data = g_hash_table_lookup (priv->points, sequence);

  if (!data)
    return GTK_EVENT_SEQUENCE_NONE;

  return data->state;
}

/**
 * gtk_gesture_set_sequence_state:
 * @gesture: a #GtkGesture
 * @sequence: a #GdkEventSequence
 * @state: the sequence state
 *
1081 1082 1083 1084 1085 1086
 * Sets the state of @sequence in @gesture. Sequences start
 * in state #GTK_EVENT_SEQUENCE_NONE, and whenever they change
 * state, they can never go back to that state. Likewise,
 * sequences in state #GTK_EVENT_SEQUENCE_DENIED cannot turn
 * back to a not denied state. With these rules, the lifetime
 * of an event sequence is constrained to the next four:
Carlos Garnacho's avatar
Carlos Garnacho committed
1087
 *
1088 1089 1090 1091 1092
 * * None
 * * None → Denied
 * * None → Claimed
 * * None → Claimed → Denied
 *
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
 * Note: Due to event handling ordering, it may be unsafe to
 * set the state on another gesture within a #GtkGesture::begin
 * signal handler, as the callback might be executed before
 * the other gesture knows about the sequence. A safe way to
 * perform this could be:
 *
 * |[
 * static void
 * first_gesture_begin_cb (GtkGesture       *first_gesture,
 *                         GdkEventSequence *sequence,
 *                         gpointer          user_data)
 * {
 *   gtk_gesture_set_sequence_state (first_gesture, sequence, GTK_EVENT_SEQUENCE_ACCEPTED);
 *   gtk_gesture_set_sequence_state (second_gesture, sequence, GTK_EVENT_SEQUENCE_DENIED);
 * }
 *
 * static void
 * second_gesture_begin_cb (GtkGesture       *second_gesture,
 *                          GdkEventSequence *sequence,
 *                          gpointer          user_data)
 * {
 *   if (gtk_gesture_get_sequence_state (first_gesture, sequence) == GTK_EVENT_SEQUENCE_ACCEPTED)
 *     gtk_gesture_set_sequence_state (second_gesture, sequence, GTK_EVENT_SEQUENCE_DENIED);
 * }
 * ]|
 *
 * If both gestures are in the same group, just set the state on
 * the gesture emitting the event, the sequence will be already
 * be initialized to the group's global state when the second
 * gesture processes the event.
 *
Matthias Clasen's avatar
Matthias Clasen committed
1124 1125
 * Returns: %TRUE if @sequence is handled by @gesture,
 *          and the state is changed successfully
Carlos Garnacho's avatar
Carlos Garnacho committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
 *
 * Since: 3.14
 **/
gboolean
gtk_gesture_set_sequence_state (GtkGesture            *gesture,
                                GdkEventSequence      *sequence,
                                GtkEventSequenceState  state)
{
  GtkGesturePrivate *priv;
  PointData *data;

1137 1138 1139
  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);
  g_return_val_if_fail (state >= GTK_EVENT_SEQUENCE_NONE &&
                        state <= GTK_EVENT_SEQUENCE_DENIED, FALSE);
Carlos Garnacho's avatar
Carlos Garnacho committed
1140 1141 1142 1143

  priv = gtk_gesture_get_instance_private (gesture);
  data = g_hash_table_lookup (priv->points, sequence);

1144
  if (!data)
Carlos Garnacho's avatar
Carlos Garnacho committed
1145 1146
    return FALSE;

1147
  if (data->state == state)
1148
    return FALSE;
1149

1150 1151 1152 1153 1154 1155 1156 1157 1158
  /* denied sequences remain denied */
  if (data->state == GTK_EVENT_SEQUENCE_DENIED)
    return FALSE;

  /* Sequences can't go from claimed/denied to none */
  if (state == GTK_EVENT_SEQUENCE_NONE &&
      data->state != GTK_EVENT_SEQUENCE_NONE)
    return FALSE;

Carlos Garnacho's avatar
Carlos Garnacho committed
1159 1160
  data->state = state;
  g_signal_emit (gesture, signals[SEQUENCE_STATE_CHANGED], 0,
1161
                 sequence, state);
1162 1163 1164 1165

  if (state == GTK_EVENT_SEQUENCE_DENIED)
    _gtk_gesture_check_recognized (gesture, sequence);

Carlos Garnacho's avatar
Carlos Garnacho committed
1166 1167 1168
  return TRUE;
}

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
/**
 * gtk_gesture_set_state:
 * @gesture: a #GtkGesture
 * @state: the sequence state
 *
 * Sets the state of all sequences that @gesture is currently
 * interacting with. See gtk_gesture_set_sequence_state()
 * for more details on sequence states.
 *
 * Returns: %TRUE if the state of at least one sequence
Matthias Clasen's avatar
Matthias Clasen committed
1179
 *     was changed successfully
1180 1181 1182
 *
 * Since: 3.14
 */
1183 1184 1185 1186 1187 1188
gboolean
gtk_gesture_set_state (GtkGesture            *gesture,
                       GtkEventSequenceState  state)
{
  gboolean handled = FALSE;
  GtkGesturePrivate *priv;
1189
  GList *sequences, *l;
1190 1191 1192 1193 1194 1195

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);
  g_return_val_if_fail (state >= GTK_EVENT_SEQUENCE_NONE &&
                        state <= GTK_EVENT_SEQUENCE_DENIED, FALSE);

  priv = gtk_gesture_get_instance_private (gesture);
1196
  sequences = g_hash_table_get_keys (priv->points);
1197

1198 1199 1200 1201
  for (l = sequences; l; l = l->next)
    handled |= gtk_gesture_set_sequence_state (gesture, l->data, state);

  g_list_free (sequences);
1202 1203 1204 1205

  return handled;
}

Carlos Garnacho's avatar
Carlos Garnacho committed
1206 1207 1208 1209
/**
 * gtk_gesture_get_sequences:
 * @gesture: a #GtkGesture
 *
1210
 * Returns the list of #GdkEventSequences currently being interpreted
Matthias Clasen's avatar
Matthias Clasen committed
1211
 * by @gesture.
Carlos Garnacho's avatar
Carlos Garnacho committed
1212
 *
Carlos Garnacho's avatar
Carlos Garnacho committed
1213
 * Returns: (transfer container) (element-type GdkEventSequence): A list
1214
 *          of #GdkEventSequences, the list elements are owned by GTK+
Carlos Garnacho's avatar
Carlos Garnacho committed
1215 1216
 *          and must not be freed or modified, the list itself must be deleted
 *          through g_list_free()
Matthias Clasen's avatar
Matthias Clasen committed
1217 1218
 *
 * Since: 3.14
Carlos Garnacho's avatar
Carlos Garnacho committed
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
 **/
GList *
gtk_gesture_get_sequences (GtkGesture *gesture)
{
  GdkEventSequence *sequence;
  GtkGesturePrivate *priv;
  GList *sequences = NULL;
  GHashTableIter iter;
  PointData *data;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), NULL);

  priv = gtk_gesture_get_instance_private (gesture);
  g_hash_table_iter_init (&iter, priv->points);

  while (g_hash_table_iter_next (&iter, (gpointer *) &sequence, (gpointer *) &data))
    {
      if (data->state == GTK_EVENT_SEQUENCE_DENIED)
        continue;
      if (data->event->type == GDK_TOUCH_END ||
          data->event->type == GDK_BUTTON_RELEASE)
        continue;

      sequences = g_list_prepend (sequences, sequence);
    }

  return sequences;
}

/**
 * gtk_gesture_get_last_updated_sequence:
 * @gesture: a #GtkGesture
 *
 * Returns the #GdkEventSequence that was last updated on @gesture.
 *
1254
 * Returns: (transfer none) (nullable): The last updated sequence
Carlos Garnacho's avatar
Carlos Garnacho committed
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
 *
 * Since: 3.14
 **/
GdkEventSequence *
gtk_gesture_get_last_updated_sequence (GtkGesture *gesture)
{
  GtkGesturePrivate *priv;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), NULL);

  priv = gtk_gesture_get_instance_private (gesture);

  return priv->last_sequence;
}

Carlos Garnacho's avatar
Carlos Garnacho committed
1270 1271 1272 1273 1274 1275 1276
/**
 * gtk_gesture_get_last_event:
 * @gesture: a #GtkGesture
 * @sequence: a #GdkEventSequence
 *
 * Returns the last event that was processed for @sequence.
 *
1277 1278 1279 1280
 * Note that the returned pointer is only valid as long as the @sequence
 * is still interpreted by the @gesture. If in doubt, you should make
 * a copy of the event.
 *
1281
 * Returns: (transfer none) (nullable): The last event from @sequence
Carlos Garnacho's avatar
Carlos Garnacho committed
1282
 **/
Carlos Garnacho's avatar
Carlos Garnacho committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
const GdkEvent *
gtk_gesture_get_last_event (GtkGesture       *gesture,
                            GdkEventSequence *sequence)
{
  GtkGesturePrivate *priv;
  PointData *data;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), NULL);

  priv = gtk_gesture_get_instance_private (gesture);
  data = g_hash_table_lookup (priv->points, sequence);

  if (!data)
    return NULL;

  return data->event;
}

/**
 * gtk_gesture_get_point:
 * @gesture: a #GtkGesture
 * @sequence: (allow-none): a #GdkEventSequence, or %NULL for pointer events
 * @x: (out) (allow-none): return location for X axis of the sequence coordinates
 * @y: (out) (allow-none): return location for Y axis of the sequence coordinates
 *
 * If @sequence is currently being interpreted by @gesture, this
 * function returns %TRUE and fills in @x and @y with the last coordinates
1310 1311
 * stored for that event sequence. The coordinates are always relative to the
 * widget allocation.
Carlos Garnacho's avatar
Carlos Garnacho committed
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
 *
 * Returns: %TRUE if @sequence is currently interpreted
 *
 * Since: 3.14
 **/
gboolean
gtk_gesture_get_point (GtkGesture       *gesture,
                       GdkEventSequence *sequence,
                       gdouble          *x,
                       gdouble          *y)
{
  GtkGesturePrivate *priv;
  PointData *data;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);

  priv = gtk_gesture_get_instance_private (gesture);

  if (!g_hash_table_lookup_extended (priv->points, sequence,
                                     NULL, (gpointer *) &data))
    return FALSE;

1334 1335 1336 1337 1338 1339
  if (x)
    *x = data->widget_x;
  if (y)
    *y = data->widget_y;

  return TRUE;
Carlos Garnacho's avatar
Carlos Garnacho committed
1340 1341 1342
}

gboolean
1343 1344 1345
_gtk_gesture_get_last_update_time (GtkGesture       *gesture,
                                   GdkEventSequence *sequence,
                                   guint32          *evtime)
Carlos Garnacho's avatar
Carlos Garnacho committed
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
{
  GtkGesturePrivate *priv;
  PointData *data;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);

  priv = gtk_gesture_get_instance_private (gesture);

  if (!g_hash_table_lookup_extended (priv->points, sequence,
                                     NULL, (gpointer *) &data))
    return FALSE;

  if (evtime)
    *evtime = gdk_event_get_time (data->event);

  return TRUE;
};

/**
 * gtk_gesture_get_bounding_box:
 * @gesture: a #GtkGesture
 * @rect: (out): bounding box containing all active touches.
 *
 * If there are touch sequences being currently handled by @gesture,
Matthias Clasen's avatar
Matthias Clasen committed
1370 1371
 * this function returns %TRUE and fills in @rect with the bounding
 * box containing all active touches. Otherwise, %FALSE will be
Carlos Garnacho's avatar
Carlos Garnacho committed
1372 1373
 * returned.
 *
1374 1375 1376 1377 1378 1379
 * Note: This function will yield unexpected results on touchpad
 * gestures. Since there is no correlation between physical and
 * pixel distances, these will look as if constrained in an
 * infinitely small area, @rect width and height will thus be 0
 * regardless of the number of touchpoints.
 *
Matthias Clasen's avatar
Matthias Clasen committed
1380
 * Returns: %TRUE if there are active touches, %FALSE otherwise
Carlos Garnacho's avatar
Carlos Garnacho committed
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
 *
 * Since: 3.14
 **/
gboolean
gtk_gesture_get_bounding_box (GtkGesture   *gesture,
                              GdkRectangle *rect)
{
  GtkGesturePrivate *priv;
  gdouble x1, y1, x2, y2;
  GHashTableIter iter;
  guint n_points = 0;
  PointData *data;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);
  g_return_val_if_fail (rect != NULL, FALSE);

  priv = gtk_gesture_get_instance_private (gesture);
  x1 = y1 = G_MAXDOUBLE;
  x2 = y2 = -G_MAXDOUBLE;

  g_hash_table_iter_init (&iter, priv->points);

  while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &data))
    {
      gdouble x, y;

      if (data->state == GTK_EVENT_SEQUENCE_DENIED)
        continue;
      if (data->event->type == GDK_TOUCH_END ||
          data->event->type == GDK_BUTTON_RELEASE)
        continue;

      gdk_event_get_coords (data->event, &x, &y);
      n_points++;
      x1 = MIN (x1, x);
      y1 = MIN (y1, y);
      x2 = MAX (x2, x);
      y2 = MAX (y2, y);
    }

  if (n_points == 0)
    return FALSE;

  rect->x = x1;
  rect->y = y1;
  rect->width = x2 - x1;
  rect->height = y2 - y1;

  return TRUE;
}


/**
 * gtk_gesture_get_bounding_box_center:
 * @gesture: a #GtkGesture
 * @x: (out): X coordinate for the bounding box center
 * @y: (out): Y coordinate for the bounding box center
 *
 * If there are touch sequences being currently handled by @gesture,
Matthias Clasen's avatar
Matthias Clasen committed
1440 1441
 * this function returns %TRUE and fills in @x and @y with the center
 * of the bounding box containing all active touches. Otherwise, %FALSE
Carlos Garnacho's avatar
Carlos Garnacho committed
1442 1443
 * will be returned.
 *
Matthias Clasen's avatar
Matthias Clasen committed
1444
 * Returns: %FALSE if no active touches are present, %TRUE otherwise
Carlos Garnacho's avatar
Carlos Garnacho committed
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
 *
 * Since: 3.14
 **/
gboolean
gtk_gesture_get_bounding_box_center (GtkGesture *gesture,
                                     gdouble    *x,
                                     gdouble    *y)
{
  GdkRectangle rect;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);
  g_return_val_if_fail (x != NULL && y != NULL, FALSE);

  if (!gtk_gesture_get_bounding_box (gesture, &rect))
    return FALSE;

  *x = rect.x + rect.width / 2;
  *y = rect.y + rect.height / 2;
  return TRUE;
}

/**
 * gtk_gesture_is_active:
 * @gesture: a #GtkGesture
 *
 * Returns %TRUE if the gesture is currently active.
 * A gesture is active meanwhile there are touch sequences
 * interacting with it.
 *
Matthias Clasen's avatar
Matthias Clasen committed
1474
 * Returns: %TRUE if gesture is active
Carlos Garnacho's avatar
Carlos Garnacho committed
1475 1476 1477 1478 1479 1480 1481 1482
 *
 * Since: 3.14
 **/
gboolean
gtk_gesture_is_active (GtkGesture *gesture)
{
  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);

1483
  return _gtk_gesture_get_n_physical_points (gesture, TRUE) != 0;
Carlos Garnacho's avatar
Carlos Garnacho committed
1484 1485 1486 1487 1488 1489 1490 1491 1492
}

/**
 * gtk_gesture_is_recognized:
 * @gesture: a #GtkGesture
 *
 * Returns %TRUE if the gesture is currently recognized.
 * A gesture is recognized if there are as many interacting
 * touch sequences as required by @gesture, and #GtkGesture::check
Matthias Clasen's avatar
Matthias Clasen committed
1493
 * returned %TRUE for the sequences being currently interpreted.
Carlos Garnacho's avatar
Carlos Garnacho committed
1494
 *
Matthias Clasen's avatar
Matthias Clasen committed
1495
 * Returns: %TRUE if gesture is recognized
Carlos Garnacho's avatar
Carlos Garnacho committed
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
 *
 * Since: 3.14
 **/
gboolean
gtk_gesture_is_recognized (GtkGesture *gesture)
{
  GtkGesturePrivate *priv;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);

  priv = gtk_gesture_get_instance_private (gesture);

  return priv->recognized;
}

gboolean
1512
_gtk_gesture_check (GtkGesture *gesture)
Carlos Garnacho's avatar
Carlos Garnacho committed
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
{
  GtkGesturePrivate *priv;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);

  priv = gtk_gesture_get_instance_private (gesture);

  return _gtk_gesture_check_recognized (gesture, priv->last_sequence);
}

/**
 * gtk_gesture_handles_sequence:
 * @gesture: a #GtkGesture
1526
 * @sequence: (nullable): a #GdkEventSequence or %NULL
Carlos Garnacho's avatar
Carlos Garnacho committed
1527
 *
Matthias Clasen's avatar
Matthias Clasen committed
1528
 * Returns %TRUE if @gesture is currently handling events corresponding to
Carlos Garnacho's avatar
Carlos Garnacho committed
1529 1530
 * @sequence.
 *
1531
 * Returns: %TRUE if @gesture is handling @sequence, %FALSE otherwise
Carlos Garnacho's avatar
Carlos Garnacho committed
1532 1533 1534 1535 1536 1537 1538 1539
 *
 * Since: 3.14
 **/
gboolean
gtk_gesture_handles_sequence (GtkGesture       *gesture,
                              GdkEventSequence *sequence)
{
  GtkGesturePrivate *priv;
1540
  PointData *data;
Carlos Garnacho's avatar
Carlos Garnacho committed
1541 1542 1543 1544

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);

  priv = gtk_gesture_get_instance_private (gesture);
1545
  data = g_hash_table_lookup (priv->points, sequence);
Carlos Garnacho's avatar
Carlos Garnacho committed
1546

1547 1548 1549 1550 1551 1552 1553
  if (!data)
    return FALSE;

  if (data->state == GTK_EVENT_SEQUENCE_DENIED)
    return FALSE;

  return TRUE;
Carlos Garnacho's avatar
Carlos Garnacho committed
1554
}
1555 1556

gboolean
1557 1558
_gtk_gesture_cancel_sequence (GtkGesture       *gesture,
                              GdkEventSequence *sequence)
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
{
  GtkGesturePrivate *priv;
  PointData *data;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);

  priv = gtk_gesture_get_instance_private (gesture);
  data = g_hash_table_lookup (priv->points, sequence);

  if (!data)
    return FALSE;

  g_signal_emit (gesture, signals[CANCEL], 0, sequence);
  _gtk_gesture_remove_point (gesture, data->event);
1573 1574
  _gtk_gesture_check_recognized (gesture, sequence);

1575 1576
  return TRUE;
}
1577

1578 1579 1580 1581 1582 1583 1584 1585
/**
 * gtk_gesture_get_window:
 * @gesture: a #GtkGesture
 *
 * Returns the user-defined window that receives the events
 * handled by @gesture. See gtk_gesture_set_window() for more
 * information.
 *
1586
 * Returns: (nullable) (transfer none): the user defined window, or %NULL if none
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
 *
 * Since: 3.14
 **/
GdkWindow *
gtk_gesture_get_window (GtkGesture *gesture)
{
  GtkGesturePrivate *priv;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), NULL);

  priv = gtk_gesture_get_instance_private (gesture);

  return priv->user_window;
}

/**
 * gtk_gesture_set_window:
 * @gesture: a #GtkGesture
 * @window: (allow-none): a #GdkWindow, or %NULL
 *
 * Sets a specific window to receive events about, so @gesture
 * will effectively handle only events targeting @window, or
 * a child of it. @window must pertain to gtk_event_controller_get_widget().
 *
 * Since: 3.14
 **/
void
gtk_gesture_set_window (GtkGesture *gesture,
                        GdkWindow  *window)
{
  GtkGesturePrivate *priv;

  g_return_if_fail (GTK_IS_GESTURE (gesture));
  g_return_if_fail (!window || GDK_IS_WINDOW (window));

  priv = gtk_gesture_get_instance_private (gesture);

  if (window)
    {
      GtkWidget *window_widget;

      gdk_window_get_user_data (window, (gpointer*) &window_widget);
1629
      g_return_if_fail (window_widget ==
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
                        gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture)));
    }

  if (priv->user_window == window)
    return;

  priv->user_window = window;
  g_object_notify (G_OBJECT (gesture), "window");
}

1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
GList *
_gtk_gesture_get_group_link (GtkGesture *gesture)
{
  GtkGesturePrivate *priv;

  priv = gtk_gesture_get_instance_private (gesture);

  return priv->group_link;
}

1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
/**
 * gtk_gesture_group:
 * @gesture: a #GtkGesture
 * @group_gesture: #GtkGesture to group @gesture with
 *
 * Adds @gesture to the same group than @group_gesture. Gestures
 * are by default isolated in their own groups.
 *
 * When gestures are grouped, the state of #GdkEventSequences
 * is kept in sync for all of those, so calling gtk_gesture_set_sequence_state(),
 * on one will transfer the same value to the others.
 *
 * Groups also perform an "implicit grabbing" of sequences, if a
 * #GdkEventSequence state is set to #GTK_EVENT_SEQUENCE_CLAIMED on one group,
 * every other gesture group attached to the same #GtkWidget will switch the
 * state for that sequence to #GTK_EVENT_SEQUENCE_DENIED.
 *
 * Since: 3.14
 **/
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
void
gtk_gesture_group (GtkGesture *gesture,
                   GtkGesture *group_gesture)
{
  GList *link, *group_link, *next;

  g_return_if_fail (GTK_IS_GESTURE (gesture));
  g_return_if_fail (GTK_IS_GESTURE (group_gesture));
  g_return_if_fail (gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (group_gesture)) ==
                    gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture)));

  link = _gtk_gesture_get_group_link (gesture);

  if (link->prev || link->next)
    {
      if (gtk_gesture_is_grouped_with (gesture, group_gesture))
        return;
      gtk_gesture_ungroup (gesture);
    }

  group_link = _gtk_gesture_get_group_link (group_gesture);
  next = group_link->next;

  /* Rewire link so it's inserted after the group_gesture elem */
  link->prev = group_link;
  link->next = next;
  group_link->next = link;
  if (next)
    next->prev = link;
}

1700 1701 1702 1703
/**
 * gtk_gesture_ungroup:
 * @gesture: a #GtkGesture
 *
Carlos Garnacho's avatar
Carlos Garnacho committed
1704
 * Separates @gesture into an isolated group.
1705 1706 1707
 *
 * Since: 3.14
 **/
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
void
gtk_gesture_ungroup (GtkGesture *gesture)
{
  GList *link, *prev, *next;

  g_return_if_fail (GTK_IS_GESTURE (gesture));

  link = _gtk_gesture_get_group_link (gesture);
  prev = link->prev;
  next = link->next;

  /* Detach link from the group chain */
  if (prev)
    prev->next = next;
  if (next)
    next->prev = prev;

  link->next = link->prev = NULL;
}

1728 1729 1730 1731 1732 1733 1734
/**
 * gtk_gesture_get_group:
 * @gesture: a #GtkGesture
 *
 * Returns all gestures in the group of @gesture
 *
 * Returns: (element-type GtkGesture) (transfer container): The list
Matthias Clasen's avatar
Matthias Clasen committed
1735
 *   of #GtkGestures, free with g_list_free()
1736 1737 1738
 *
 * Since: 3.14
 **/
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
GList *
gtk_gesture_get_group (GtkGesture *gesture)
{
  GList *link;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), NULL);

  link = _gtk_gesture_get_group_link (gesture);

  return g_list_copy (g_list_first (link));
}

1751 1752 1753 1754 1755
/**
 * gtk_gesture_is_grouped_with:
 * @gesture: a #GtkGesture
 * @other: another #GtkGesture
 *
Matthias Clasen's avatar
Matthias Clasen committed
1756
 * Returns %TRUE if both gestures pertain to the same group.
1757 1758 1759 1760 1761
 *
 * Returns: whether the gestures are grouped
 *
 * Since: 3.14
 **/
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
gboolean
gtk_gesture_is_grouped_with (GtkGesture *gesture,
                             GtkGesture *other)
{
  GList *link;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);
  g_return_val_if_fail (GTK_IS_GESTURE (other), FALSE);

  link = _gtk_gesture_get_group_link (gesture);
  link = g_list_first (link);

  return g_list_find (link, other) != NULL;
}

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
gboolean
_gtk_gesture_handled_sequence_press (GtkGesture       *gesture,
                                     GdkEventSequence *sequence)
{
  GtkGesturePrivate *priv;
  PointData *data;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);

  priv = gtk_gesture_get_instance_private (gesture);
  data = g_hash_table_lookup (priv->points, sequence);

  if (!data)
    return FALSE;

  return data->press_handled;
}
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830

gboolean
_gtk_gesture_get_pointer_emulating_sequence (GtkGesture        *gesture,
                                             GdkEventSequence **sequence)
{
  GtkGesturePrivate *priv;
  GdkEventSequence *seq;
  GHashTableIter iter;
  PointData *data;

  g_return_val_if_fail (GTK_IS_GESTURE (gesture), FALSE);

  priv = gtk_gesture_get_instance_private (gesture);
  g_hash_table_iter_init (&iter, priv->points);

  while (g_hash_table_iter_next (&iter, (gpointer*) &seq, (gpointer*) &data))
    {
      switch (data->event->type)
        {
        case GDK_TOUCH_BEGIN:
        case GDK_TOUCH_UPDATE:
        case GDK_TOUCH_END:
          if (!data->event->touch.emulating_pointer)
            continue;
          /* Fall through */
        case GDK_BUTTON_PRESS:
        case GDK_BUTTON_RELEASE:
        case GDK_MOTION_NOTIFY:
          *sequence = seq;
          return TRUE;
        default:
          break;
        }
    }

  return FALSE;
}