gtkgesturesingle.c 15.2 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:gtkgesturesingle
23
 * @Short_description: Base class for mouse/single-touch gestures
24 25
 * @Title: GtkGestureSingle
 *
Matthias Clasen's avatar
Matthias Clasen committed
26 27
 * #GtkGestureSingle is a subclass of #GtkGesture, optimized (although
 * not restricted) for dealing with mouse and single-touch gestures. Under
28 29 30 31
 * interaction, these gestures stick to the first interacting sequence, which
 * is accessible through gtk_gesture_single_get_current_sequence() while the
 * gesture is being interacted with.
 *
32 33 34 35 36 37
 * By default gestures react to both %GDK_BUTTON_PRIMARY and touch
 * events, gtk_gesture_single_set_touch_only() can be used to change the
 * touch behavior. Callers may also specify a different mouse button number
 * to interact with through gtk_gesture_single_set_button(), or react to any
 * mouse button by setting 0. While the gesture is active, the button being
 * currently pressed can be known through gtk_gesture_single_get_current_button().
38 39
 */

Carlos Garnacho's avatar
Carlos Garnacho committed
40
#include "config.h"
41 42
#include "gtkgesturesingle.h"
#include "gtkgesturesingleprivate.h"
Carlos Garnacho's avatar
Carlos Garnacho committed
43 44
#include "gtkprivate.h"
#include "gtkintl.h"
45
#include "gtkdebug.h"
Carlos Garnacho's avatar
Carlos Garnacho committed
46 47 48 49 50 51 52 53 54

typedef struct _GtkGestureSinglePrivate GtkGestureSinglePrivate;

struct _GtkGestureSinglePrivate
{
  GdkEventSequence *current_sequence;
  guint button;
  guint current_button;
  guint touch_only : 1;
55
  guint exclusive  : 1;
Carlos Garnacho's avatar
Carlos Garnacho committed
56 57 58 59
};

enum {
  PROP_TOUCH_ONLY = 1,
60
  PROP_EXCLUSIVE,
61 62
  PROP_BUTTON,
  LAST_PROP
Carlos Garnacho's avatar
Carlos Garnacho committed
63 64
};

65 66 67
static GParamSpec *properties[LAST_PROP] = { NULL, };

G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureSingle, gtk_gesture_single, GTK_TYPE_GESTURE)
Carlos Garnacho's avatar
Carlos Garnacho committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

static void
gtk_gesture_single_get_property (GObject    *object,
                                 guint       prop_id,
                                 GValue     *value,
                                 GParamSpec *pspec)
{
  GtkGestureSinglePrivate *priv;

  priv = gtk_gesture_single_get_instance_private (GTK_GESTURE_SINGLE (object));

  switch (prop_id)
    {
    case PROP_TOUCH_ONLY:
      g_value_set_boolean (value, priv->touch_only);
      break;
84 85 86
    case PROP_EXCLUSIVE:
      g_value_set_boolean (value, priv->exclusive);
      break;
Carlos Garnacho's avatar
Carlos Garnacho committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    case PROP_BUTTON:
      g_value_set_uint (value, priv->button);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_gesture_single_set_property (GObject      *object,
                                 guint         prop_id,
                                 const GValue *value,
                                 GParamSpec   *pspec)
{
  switch (prop_id)
    {
    case PROP_TOUCH_ONLY:
      gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (object),
                                         g_value_get_boolean (value));
      break;
107 108 109 110
    case PROP_EXCLUSIVE:
      gtk_gesture_single_set_exclusive (GTK_GESTURE_SINGLE (object),
                                        g_value_get_boolean (value));
      break;
Carlos Garnacho's avatar
Carlos Garnacho committed
111 112 113 114 115 116 117 118 119 120
    case PROP_BUTTON:
      gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (object),
                                     g_value_get_uint (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
121 122
gtk_gesture_single_cancel (GtkGesture       *gesture,
                           GdkEventSequence *sequence)
Carlos Garnacho's avatar
Carlos Garnacho committed
123 124 125
{
  GtkGestureSinglePrivate *priv;

126
  priv = gtk_gesture_single_get_instance_private (GTK_GESTURE_SINGLE (gesture));
Carlos Garnacho's avatar
Carlos Garnacho committed
127

128 129
  if (sequence == priv->current_sequence)
    priv->current_button = 0;
Carlos Garnacho's avatar
Carlos Garnacho committed
130 131 132 133 134 135 136 137 138 139 140
}

static gboolean
gtk_gesture_single_handle_event (GtkEventController *controller,
                                 const GdkEvent     *event)
{
  GdkEventSequence *sequence = NULL;
  GtkGestureSinglePrivate *priv;
  GdkDevice *source_device;
  GdkInputSource source;
  guint button = 0, i;
141
  gboolean retval, test_touchscreen = FALSE;
Carlos Garnacho's avatar
Carlos Garnacho committed
142 143 144 145 146 147 148 149 150

  source_device = gdk_event_get_source_device (event);

  if (!source_device)
    return FALSE;

  priv = gtk_gesture_single_get_instance_private (GTK_GESTURE_SINGLE (controller));
  source = gdk_device_get_source (source_device);

151
  if (source != GDK_SOURCE_TOUCHSCREEN)
152
    test_touchscreen = gtk_simulate_touchscreen ();
153

Carlos Garnacho's avatar
Carlos Garnacho committed
154 155 156 157 158
  switch (event->type)
    {
    case GDK_TOUCH_BEGIN:
    case GDK_TOUCH_END:
    case GDK_TOUCH_UPDATE:
159 160 161
      if (priv->exclusive && !event->touch.emulating_pointer)
        return FALSE;

Carlos Garnacho's avatar
Carlos Garnacho committed
162 163 164 165 166
      sequence = event->touch.sequence;
      button = 1;
      break;
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
167
      if (priv->touch_only && !test_touchscreen && source != GDK_SOURCE_TOUCHSCREEN)
Carlos Garnacho's avatar
Carlos Garnacho committed
168 169 170 171 172
        return FALSE;

      button = event->button.button;
      break;
    case GDK_MOTION_NOTIFY:
173 174
      if (!gtk_gesture_handles_sequence (GTK_GESTURE (controller), sequence))
        return FALSE;
175
      if (priv->touch_only && !test_touchscreen && source != GDK_SOURCE_TOUCHSCREEN)
Carlos Garnacho's avatar
Carlos Garnacho committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
        return FALSE;

      if (priv->current_button > 0 && priv->current_button <= 5 &&
          (event->motion.state & (GDK_BUTTON1_MASK << (priv->current_button - 1))))
        button = priv->current_button;
      else if (priv->current_button == 0)
        {
          /* No current button, find out from the mask */
          for (i = 0; i < 3; i++)
            {
              if ((event->motion.state & (GDK_BUTTON1_MASK << i)) == 0)
                continue;
              button = i + 1;
              break;
            }
        }

      break;
194 195 196 197 198
    case GDK_TOUCH_CANCEL:
    case GDK_GRAB_BROKEN:
      return GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_single_parent_class)->handle_event (controller,
                                                                                         event);
      break;
Carlos Garnacho's avatar
Carlos Garnacho committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    default:
      return FALSE;
    }

  if (button == 0 ||
      (priv->button != 0 && priv->button != button) ||
      (priv->current_button != 0 && priv->current_button != button))
    {
      if (gtk_gesture_is_active (GTK_GESTURE (controller)))
        gtk_event_controller_reset (controller);
      return FALSE;
    }

  if (event->type == GDK_BUTTON_PRESS || event->type == GDK_TOUCH_BEGIN ||
      event->type == GDK_MOTION_NOTIFY || event->type == GDK_TOUCH_UPDATE)
    {
      if (!gtk_gesture_is_active (GTK_GESTURE (controller)))
        priv->current_sequence = sequence;

      priv->current_button = button;
    }
220 221 222 223 224

  retval = GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_single_parent_class)->handle_event (controller, event);

  if (sequence == priv->current_sequence &&
      (event->type == GDK_BUTTON_RELEASE || event->type == GDK_TOUCH_END))
Carlos Garnacho's avatar
Carlos Garnacho committed
225
    priv->current_button = 0;
226 227
  else if (priv->current_sequence == sequence &&
           !gtk_gesture_handles_sequence (GTK_GESTURE (controller), sequence))
228 229 230 231 232 233
    {
      if (button == priv->current_button && event->type == GDK_BUTTON_PRESS)
        priv->current_button = 0;
      else if (sequence == priv->current_sequence && event->type == GDK_TOUCH_BEGIN)
        priv->current_sequence = NULL;
    }
Carlos Garnacho's avatar
Carlos Garnacho committed
234

235
  return retval;
Carlos Garnacho's avatar
Carlos Garnacho committed
236 237 238 239 240 241
}

static void
gtk_gesture_single_class_init (GtkGestureSingleClass *klass)
{
  GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
242
  GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);
Carlos Garnacho's avatar
Carlos Garnacho committed
243 244 245 246 247 248 249
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = gtk_gesture_single_get_property;
  object_class->set_property = gtk_gesture_single_set_property;

  controller_class->handle_event = gtk_gesture_single_handle_event;

250 251
  gesture_class->cancel = gtk_gesture_single_cancel;

252 253 254
  /**
   * GtkGestureSingle:touch-only:
   *
Matthias Clasen's avatar
Matthias Clasen committed
255
   * Whether the gesture handles only touch events.
256 257 258
   *
   * Since: 3.14
   */
259 260 261 262 263 264
  properties[PROP_TOUCH_ONLY] =
      g_param_spec_boolean ("touch-only",
                            P_("Handle only touch events"),
                            P_("Whether the gesture handles only touch events"),
                            FALSE,
                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
265

266 267 268 269 270 271 272 273
  /**
   * GtkGestureSingle:exclusive:
   *
   * Whether the gesture is exclusive. Exclusive gestures only listen to pointer
   * and pointer emulated events.
   *
   * Since: 3.14
   */
274 275 276 277 278 279
  properties[PROP_EXCLUSIVE] =
      g_param_spec_boolean ("exclusive",
                            P_("Whether the gesture is exclusive"),
                            P_("Whether the gesture is exclusive"),
                            FALSE,
                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
280

281 282 283 284 285 286 287
  /**
   * GtkGestureSingle:button:
   *
   * Mouse button number to listen to, or 0 to listen for any button.
   *
   * Since: 3.14
   */
288 289 290 291 292 293 294 295 296
  properties[PROP_BUTTON] =
      g_param_spec_uint ("button",
                         P_("Button number"),
                         P_("Button number to listen to"),
                         0, G_MAXUINT,
                         GDK_BUTTON_PRIMARY,
                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, LAST_PROP, properties);
Carlos Garnacho's avatar
Carlos Garnacho committed
297 298 299 300 301 302 303 304 305 306 307
}

static void
_gtk_gesture_single_update_evmask (GtkGestureSingle *gesture)
{
  GtkGestureSinglePrivate *priv;
  GdkEventMask evmask;

  priv = gtk_gesture_single_get_instance_private (gesture);
  evmask = GDK_TOUCH_MASK;

308
  if (!priv->touch_only || gtk_simulate_touchscreen ())
Carlos Garnacho's avatar
Carlos Garnacho committed
309 310 311 312 313 314 315 316 317 318 319 320
    evmask |= GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
      GDK_BUTTON_MOTION_MASK;

  gtk_event_controller_set_event_mask (GTK_EVENT_CONTROLLER (gesture), evmask);
}

static void
gtk_gesture_single_init (GtkGestureSingle *gesture)
{
  GtkGestureSinglePrivate *priv;

  priv = gtk_gesture_single_get_instance_private (gesture);
321 322
  priv->touch_only = FALSE;
  priv->button = GDK_BUTTON_PRIMARY;
Carlos Garnacho's avatar
Carlos Garnacho committed
323 324 325 326 327 328 329
  _gtk_gesture_single_update_evmask (gesture);
}

/**
 * gtk_gesture_single_get_touch_only:
 * @gesture: a #GtkGestureSingle
 *
Matthias Clasen's avatar
Matthias Clasen committed
330
 * Returns %TRUE if the gesture is only triggered by touch events.
Carlos Garnacho's avatar
Carlos Garnacho committed
331
 *
Matthias Clasen's avatar
Matthias Clasen committed
332
 * Returns: %TRUE if the gesture only handles touch events
Carlos Garnacho's avatar
Carlos Garnacho committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
 *
 * Since: 3.14
 **/
gboolean
gtk_gesture_single_get_touch_only (GtkGestureSingle *gesture)
{
  GtkGestureSinglePrivate *priv;

  g_return_val_if_fail (GTK_IS_GESTURE_SINGLE (gesture), FALSE);

  priv = gtk_gesture_single_get_instance_private (gesture);

  return priv->touch_only;
}

/**
 * gtk_gesture_single_set_touch_only:
 * @gesture: a #GtkGestureSingle
 * @touch_only: whether @gesture handles only touch events
 *
Matthias Clasen's avatar
Matthias Clasen committed
353 354
 * If @touch_only is %TRUE, @gesture will only handle events of type
 * #GDK_TOUCH_BEGIN, #GDK_TOUCH_UPDATE or #GDK_TOUCH_END. If %FALSE,
Carlos Garnacho's avatar
Carlos Garnacho committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
 * mouse events will be handled too.
 *
 * Since: 3.14
 **/
void
gtk_gesture_single_set_touch_only (GtkGestureSingle *gesture,
                                   gboolean          touch_only)
{
  GtkGestureSinglePrivate *priv;

  g_return_if_fail (GTK_IS_GESTURE_SINGLE (gesture));

  touch_only = touch_only != FALSE;
  priv = gtk_gesture_single_get_instance_private (gesture);

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

  priv->touch_only = touch_only;
  _gtk_gesture_single_update_evmask (gesture);
375
  g_object_notify_by_pspec (G_OBJECT (gesture), properties[PROP_TOUCH_ONLY]);
Carlos Garnacho's avatar
Carlos Garnacho committed
376 377
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
/**
 * gtk_gesture_single_get_exclusive:
 * @gesture: a #GtkGestureSingle
 *
 * Gets whether a gesture is exclusive. For more information, see
 * gtk_gesture_single_set_exclusive().
 *
 * Returns: Whether the gesture is exclusive
 *
 * Since: 3.14
 **/
gboolean
gtk_gesture_single_get_exclusive (GtkGestureSingle *gesture)
{
  GtkGestureSinglePrivate *priv;

  g_return_val_if_fail (GTK_IS_GESTURE_SINGLE (gesture), FALSE);

  priv = gtk_gesture_single_get_instance_private (gesture);

  return priv->exclusive;
}

/**
 * gtk_gesture_single_set_exclusive:
 * @gesture: a #GtkGestureSingle
Matthias Clasen's avatar
Matthias Clasen committed
404
 * @exclusive: %TRUE to make @gesture exclusive
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
 *
 * Sets whether @gesture is exclusive. An exclusive gesture will
 * only handle pointer and "pointer emulated" touch events, so at
 * any given time, there is only one sequence able to interact with
 * those.
 *
 * Since: 3.14
 **/
void
gtk_gesture_single_set_exclusive (GtkGestureSingle *gesture,
                                  gboolean          exclusive)
{
  GtkGestureSinglePrivate *priv;

  g_return_if_fail (GTK_IS_GESTURE_SINGLE (gesture));

  exclusive = exclusive != FALSE;
  priv = gtk_gesture_single_get_instance_private (gesture);

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

  priv->exclusive = exclusive;
  _gtk_gesture_single_update_evmask (gesture);
429
  g_object_notify_by_pspec (G_OBJECT (gesture), properties[PROP_EXCLUSIVE]);
430 431
}

Carlos Garnacho's avatar
Carlos Garnacho committed
432 433 434 435 436 437 438
/**
 * gtk_gesture_single_get_button:
 * @gesture: a #GtkGestureSingle
 *
 * Returns the button number @gesture listens for, or 0 if @gesture
 * reacts to any button press.
 *
Matthias Clasen's avatar
Matthias Clasen committed
439
 * Returns: The button number, or 0 for any button
Carlos Garnacho's avatar
Carlos Garnacho committed
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 466 467 468 469 470 471 472 473 474 475 476 477 478 479
 *
 * Since: 3.14
 **/
guint
gtk_gesture_single_get_button (GtkGestureSingle *gesture)
{
  GtkGestureSinglePrivate *priv;

  g_return_val_if_fail (GTK_IS_GESTURE_SINGLE (gesture), 0);

  priv = gtk_gesture_single_get_instance_private (gesture);

  return priv->button;
}

/**
 * gtk_gesture_single_set_button:
 * @gesture: a #GtkGestureSingle
 * @button: button number to listen to, or 0 for any button
 *
 * Sets the button number @gesture listens to. If non-0, every
 * button press from a different button number will be ignored.
 * Touch events implicitly match with button 1.
 *
 * Since: 3.14
 **/
void
gtk_gesture_single_set_button (GtkGestureSingle *gesture,
                               guint             button)
{
  GtkGestureSinglePrivate *priv;

  g_return_if_fail (GTK_IS_GESTURE_SINGLE (gesture));

  priv = gtk_gesture_single_get_instance_private (gesture);

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

  priv->button = button;
480
  g_object_notify_by_pspec (G_OBJECT (gesture), properties[PROP_BUTTON]);
Carlos Garnacho's avatar
Carlos Garnacho committed
481 482
}

483 484 485 486 487 488 489 490
/**
 * gtk_gesture_single_get_current_button:
 * @gesture: a #GtkGestureSingle
 *
 * Returns the button number currently interacting with @gesture, or 0 if there
 * is none.
 *
 * Returns: The current button number
Matthias Clasen's avatar
Matthias Clasen committed
491 492
 *
 * Since: 3.14
493
 **/
Carlos Garnacho's avatar
Carlos Garnacho committed
494 495 496 497 498 499 500 501 502 503 504 505
guint
gtk_gesture_single_get_current_button (GtkGestureSingle *gesture)
{
  GtkGestureSinglePrivate *priv;

  g_return_val_if_fail (GTK_IS_GESTURE_SINGLE (gesture), 0);

  priv = gtk_gesture_single_get_instance_private (gesture);

  return priv->current_button;
}

506 507 508 509
/**
 * gtk_gesture_single_get_current_sequence:
 * @gesture: a #GtkGestureSingle
 *
Matthias Clasen's avatar
Matthias Clasen committed
510 511
 * Returns the event sequence currently interacting with @gesture.
 * This is only meaningful if gtk_gesture_is_active() returns %TRUE.
512
 *
513
 * Returns: (nullable): the current sequence
Matthias Clasen's avatar
Matthias Clasen committed
514 515
 *
 * Since: 3.14
516
 **/
Carlos Garnacho's avatar
Carlos Garnacho committed
517 518 519 520 521 522 523 524 525 526 527
GdkEventSequence *
gtk_gesture_single_get_current_sequence (GtkGestureSingle *gesture)
{
  GtkGestureSinglePrivate *priv;

  g_return_val_if_fail (GTK_IS_GESTURE_SINGLE (gesture), NULL);

  priv = gtk_gesture_single_get_instance_private (gesture);

  return priv->current_sequence;
}