gtkgesturemultipress.c 14.3 KB
Newer Older
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) 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 23 24 25 26
/**
 * SECTION:gtkgesturemultipress
 * @Short_description: Multipress gesture
 * @Title: GtkGestureMultiPress
 *
 * #GtkGestureMultiPress is a #GtkGesture implementation able to recognize
 * multiple clicks on a nearby zone, which can be listened for through the
Matthias Clasen's avatar
Matthias Clasen committed
27 28 29
 * #GtkGestureMultiPress::pressed signal. Whenever time or distance between
 * clicks exceed the GTK+ defaults, #GtkGestureMultiPress::stopped is emitted,
 * and the click counter is reset.
30 31 32 33 34 35 36
 *
 * Callers may also restrict the area that is considered valid for a >1
 * touch/button press through gtk_gesture_multi_press_set_area(), so any
 * click happening outside that area is considered to be a first click of
 * its own.
 */

37
#include "config.h"
38
#include "gtkgestureprivate.h"
39
#include "gtkgesturemultipress.h"
40
#include "gtkgesturemultipressprivate.h"
41 42 43 44 45 46 47 48
#include "gtkprivate.h"
#include "gtkintl.h"

typedef struct _GtkGestureMultiPressPrivate GtkGestureMultiPressPrivate;

struct _GtkGestureMultiPressPrivate
{
  GdkRectangle rect;
49
  GdkDevice *current_device;
50 51 52 53
  gdouble initial_press_x;
  gdouble initial_press_y;
  guint double_click_timeout_id;
  guint n_presses;
54
  guint n_release;
55 56 57 58 59 60
  guint current_button;
  guint rect_is_set : 1;
};

enum {
  PRESSED,
61
  RELEASED,
62 63 64 65 66 67
  STOPPED,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

68
G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureMultiPress, gtk_gesture_multi_press, GTK_TYPE_GESTURE_SINGLE)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

static void
gtk_gesture_multi_press_finalize (GObject *object)
{
  GtkGestureMultiPressPrivate *priv;
  GtkGestureMultiPress *gesture;

  gesture = GTK_GESTURE_MULTI_PRESS (object);
  priv = gtk_gesture_multi_press_get_instance_private (gesture);

  if (priv->double_click_timeout_id)
    {
      g_source_remove (priv->double_click_timeout_id);
      priv->double_click_timeout_id = 0;
    }

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

static gboolean
gtk_gesture_multi_press_check (GtkGesture *gesture)
{
  GtkGestureMultiPress *multi_press;
  GtkGestureMultiPressPrivate *priv;
  GList *sequences;
  gboolean active;

  multi_press = GTK_GESTURE_MULTI_PRESS (gesture);
  priv = gtk_gesture_multi_press_get_instance_private (multi_press);
  sequences = gtk_gesture_get_sequences (gesture);

100
  active = g_list_length (sequences) == 1 || priv->double_click_timeout_id;
101 102 103 104 105
  g_list_free (sequences);

  return active;
}

106 107 108 109 110 111 112 113 114 115
static void
_gtk_gesture_multi_press_stop (GtkGestureMultiPress *gesture)
{
  GtkGestureMultiPressPrivate *priv;

  priv = gtk_gesture_multi_press_get_instance_private (gesture);

  if (priv->n_presses == 0)
    return;

116
  priv->current_device = NULL;
117 118
  priv->current_button = 0;
  priv->n_presses = 0;
119
  g_signal_emit (gesture, signals[STOPPED], 0);
120
  _gtk_gesture_check (GTK_GESTURE (gesture));
121 122
}

123 124 125 126 127 128 129 130
static gboolean
_double_click_timeout_cb (gpointer user_data)
{
  GtkGestureMultiPress *gesture = user_data;
  GtkGestureMultiPressPrivate *priv;

  priv = gtk_gesture_multi_press_get_instance_private (gesture);
  priv->double_click_timeout_id = 0;
131
  _gtk_gesture_multi_press_stop (gesture);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

  return FALSE;
}

static void
_gtk_gesture_multi_press_update_timeout (GtkGestureMultiPress *gesture)
{
  GtkGestureMultiPressPrivate *priv;
  guint double_click_time;
  GtkSettings *settings;
  GtkWidget *widget;

  priv = gtk_gesture_multi_press_get_instance_private (gesture);

  if (priv->double_click_timeout_id)
    g_source_remove (priv->double_click_timeout_id);

  widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
  settings = gtk_widget_get_settings (widget);
  g_object_get (settings, "gtk-double-click-time", &double_click_time, NULL);

  priv->double_click_timeout_id =
    gdk_threads_add_timeout (double_click_time,
                             _double_click_timeout_cb,
                             gesture);
}

static gboolean
_gtk_gesture_multi_press_check_within_threshold (GtkGestureMultiPress *gesture,
                                                 gdouble               x,
                                                 gdouble               y)
{
  GtkGestureMultiPressPrivate *priv;
  guint double_click_distance;
  GtkSettings *settings;
  GtkWidget *widget;

  priv = gtk_gesture_multi_press_get_instance_private (gesture);

  if (priv->n_presses == 0)
    return TRUE;

  widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
  settings = gtk_widget_get_settings (widget);
  g_object_get (settings,
                "gtk-double-click-distance", &double_click_distance,
                NULL);

  if (ABS (priv->initial_press_x - x) < double_click_distance &&
      ABS (priv->initial_press_y - y) < double_click_distance)
    {
      if (!priv->rect_is_set ||
          (x >= priv->rect.x && x < priv->rect.x + priv->rect.width &&
           y >= priv->rect.y && y < priv->rect.y + priv->rect.height))
        return TRUE;
    }

  return FALSE;
}

static void
193 194
gtk_gesture_multi_press_begin (GtkGesture       *gesture,
                               GdkEventSequence *sequence)
195 196 197 198
{
  GtkGestureMultiPress *multi_press;
  GtkGestureMultiPressPrivate *priv;
  guint n_presses, button = 1;
199
  GdkEventSequence *current;
200
  const GdkEvent *event;
201
  GdkDevice *device;
202 203
  gdouble x, y;

204 205 206
  if (!gtk_gesture_handles_sequence (gesture, sequence))
    return;

207 208 209
  multi_press = GTK_GESTURE_MULTI_PRESS (gesture);
  priv = gtk_gesture_multi_press_get_instance_private (multi_press);
  event = gtk_gesture_get_last_event (gesture, sequence);
210
  current = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
211
  device = gdk_event_get_source_device (event);
212

213 214 215 216 217
  if (event->type == GDK_BUTTON_PRESS)
    button = event->button.button;
  else if (event->type == GDK_TOUCH_BEGIN)
    button = 1;
  else
218 219
    return;

220 221 222 223 224
  /* Reset the gesture if the button number changes mid-recognition */
  if (priv->n_presses > 0 &&
      priv->current_button != button)
    _gtk_gesture_multi_press_stop (multi_press);

225 226 227 228 229
  /* Reset also if the device changed */
  if (priv->current_device && priv->current_device != device)
    _gtk_gesture_multi_press_stop (multi_press);

  priv->current_device = device;
230 231 232 233 234 235 236 237 238
  priv->current_button = button;
  _gtk_gesture_multi_press_update_timeout (multi_press);
  gtk_gesture_get_point (gesture, current, &x, &y);

  if (!_gtk_gesture_multi_press_check_within_threshold (multi_press, x, y))
    _gtk_gesture_multi_press_stop (multi_press);

  /* Increment later the real counter, just if the gesture is
   * reset on the pressed handler */
239
  n_presses = priv->n_release = priv->n_presses + 1;
240 241 242 243

  g_signal_emit (gesture, signals[PRESSED], 0, n_presses, x, y);

  if (priv->n_presses == 0)
244
    {
245 246
      priv->initial_press_x = x;
      priv->initial_press_y = y;
247
    }
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

  priv->n_presses++;
}

static void
gtk_gesture_multi_press_update (GtkGesture       *gesture,
                                GdkEventSequence *sequence)
{
  GtkGestureMultiPress *multi_press;
  GdkEventSequence *current;
  gdouble x, y;

  multi_press = GTK_GESTURE_MULTI_PRESS (gesture);
  current = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
  gtk_gesture_get_point (gesture, current, &x, &y);

  if (!_gtk_gesture_multi_press_check_within_threshold (multi_press, x, y))
    _gtk_gesture_multi_press_stop (multi_press);
}

268 269 270 271 272 273 274 275
static void
gtk_gesture_multi_press_end (GtkGesture       *gesture,
                             GdkEventSequence *sequence)
{
  GtkGestureMultiPress *multi_press;
  GtkGestureMultiPressPrivate *priv;
  GdkEventSequence *current;
  gdouble x, y;
276

277 278 279 280 281
  multi_press = GTK_GESTURE_MULTI_PRESS (gesture);
  priv = gtk_gesture_multi_press_get_instance_private (multi_press);
  current = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
  gtk_gesture_get_point (gesture, current, &x, &y);

282 283 284
  if (gtk_gesture_get_sequence_state (gesture, current) != GTK_EVENT_SEQUENCE_DENIED)
    g_signal_emit (gesture, signals[RELEASED], 0, priv->n_release, x, y);

285
  priv->n_release = 0;
286 287
}

288 289 290 291
static void
gtk_gesture_multi_press_cancel (GtkGesture       *gesture,
                                GdkEventSequence *sequence)
{
292
  _gtk_gesture_multi_press_stop (GTK_GESTURE_MULTI_PRESS (gesture));
293
  GTK_GESTURE_CLASS (gtk_gesture_multi_press_parent_class)->cancel (gesture, sequence);
294 295 296 297 298 299 300 301 302
}

static void
gtk_gesture_multi_press_reset (GtkEventController *controller)
{
  _gtk_gesture_multi_press_stop (GTK_GESTURE_MULTI_PRESS (controller));
  GTK_EVENT_CONTROLLER_CLASS (gtk_gesture_multi_press_parent_class)->reset (controller);
}

303 304 305
static void
gtk_gesture_multi_press_class_init (GtkGestureMultiPressClass *klass)
{
306
  GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
307 308 309 310 311 312
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass);

  object_class->finalize = gtk_gesture_multi_press_finalize;

  gesture_class->check = gtk_gesture_multi_press_check;
313
  gesture_class->begin = gtk_gesture_multi_press_begin;
314
  gesture_class->update = gtk_gesture_multi_press_update;
315
  gesture_class->end = gtk_gesture_multi_press_end;
316 317 318
  gesture_class->cancel = gtk_gesture_multi_press_cancel;

  controller_class->reset = gtk_gesture_multi_press_reset;
319

320
  /**
Carlos Garnacho's avatar
Carlos Garnacho committed
321
   * GtkGestureMultiPress::pressed:
322 323 324 325 326 327 328 329 330
   * @gesture: the object which received the signal
   * @n_press: how many touch/button presses happened with this one
   * @x: The X coordinate, in widget allocation coordinates
   * @y: The Y coordinate, in widget allocation coordinates
   *
   * This signal is emitted whenever a button or touch press happens.
   *
   * Since: 3.14
   */
331
  signals[PRESSED] =
332
    g_signal_new (I_("pressed"),
333 334 335 336
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkGestureMultiPressClass, pressed),
                  NULL, NULL, NULL,
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
                  G_TYPE_NONE, 3, G_TYPE_INT,
                  G_TYPE_DOUBLE, G_TYPE_DOUBLE);

  /**
   * GtkGestureMultiPress::released:
   * @gesture: the object which received the signal
   * @n_press: number of press that is paired with this release
   * @x: The X coordinate, in widget allocation coordinates
   * @y: The Y coordinate, in widget allocation coordinates
   *
   * This signal is emitted when a button or touch is released. @n_press
   * will report the number of press that is paired to this event, note
   * that #GtkGestureMultiPress::stopped may have been emitted between the
   * press and its release, @n_press will only start over at the next press.
   *
   * Since: 3.14
   */
  signals[RELEASED] =
355
    g_signal_new (I_("released"),
356 357 358 359
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkGestureMultiPressClass, released),
                  NULL, NULL, NULL,
360
                  G_TYPE_NONE, 3, G_TYPE_INT,
361
                  G_TYPE_DOUBLE, G_TYPE_DOUBLE);
362
  /**
Carlos Garnacho's avatar
Carlos Garnacho committed
363
   * GtkGestureMultiPress::stopped:
364 365 366 367 368 369 370
   * @gesture: the object which received the signal
   *
   * This signal is emitted whenever any time/distance threshold has
   * been exceeded.
   *
   * Since: 3.14
   */
371
  signals[STOPPED] =
372
    g_signal_new (I_("stopped"),
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkGestureMultiPressClass, stopped),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);
}

static void
gtk_gesture_multi_press_init (GtkGestureMultiPress *gesture)
{
}

/**
 * gtk_gesture_multi_press_new:
 * @widget: a #GtkWidget
 *
 * Returns a newly created #GtkGesture that recognizes single and multiple
 * presses.
 *
 * Returns: a newly created #GtkGestureMultiPress
 *
 * Since: 3.14
 **/
GtkGesture *
gtk_gesture_multi_press_new (GtkWidget *widget)
{
399 400
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);

401 402 403 404 405 406 407
  return g_object_new (GTK_TYPE_GESTURE_MULTI_PRESS,
                       "widget", widget,
                       NULL);
}

/**
 * gtk_gesture_multi_press_set_area:
408
 * @gesture: a #GtkGestureMultiPress
Matthias Clasen's avatar
Matthias Clasen committed
409
 * @rect: (allow-none): rectangle to receive coordinates on
410
 *
Matthias Clasen's avatar
Matthias Clasen committed
411
 * If @rect is non-%NULL, the press area will be checked to be
412 413
 * confined within the rectangle, otherwise the button count
 * will be reset so the press is seen as being the first one.
414
 * If @rect is %NULL, the area will be reset to an unrestricted
415 416 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
 * state.
 *
 * Note: The rectangle is only used to determine whether any
 * non-first click falls within the expected area. This is not
 * akin to an input shape.
 *
 * Since: 3.14
 **/
void
gtk_gesture_multi_press_set_area (GtkGestureMultiPress *gesture,
                                  const GdkRectangle   *rect)
{
  GtkGestureMultiPressPrivate *priv;

  g_return_if_fail (GTK_IS_GESTURE_MULTI_PRESS (gesture));

  priv = gtk_gesture_multi_press_get_instance_private (gesture);

  if (!rect)
    priv->rect_is_set = FALSE;
  else
    {
      priv->rect_is_set = TRUE;
      priv->rect = *rect;
    }
}

/**
 * gtk_gesture_multi_press_get_area:
 * @gesture: a #GtkGestureMultiPress
 * @rect: (out): return location for the press area
 *
 * If an area was set through gtk_gesture_multi_press_set_area(),
Matthias Clasen's avatar
Matthias Clasen committed
448
 * this function will return %TRUE and fill in @rect with the
449 450 451
 * press area. See gtk_gesture_multi_press_set_area() for more
 * details on what the press area represents.
 *
Matthias Clasen's avatar
Matthias Clasen committed
452 453 454
 * Returns: %TRUE if @rect was filled with the press area
 *
 * Since: 3.14
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
 **/
gboolean
gtk_gesture_multi_press_get_area (GtkGestureMultiPress *gesture,
                                  GdkRectangle         *rect)
{
  GtkGestureMultiPressPrivate *priv;

  g_return_val_if_fail (GTK_IS_GESTURE_MULTI_PRESS (gesture), FALSE);

  priv = gtk_gesture_multi_press_get_instance_private (gesture);

  if (rect)
    {
      if (priv->rect_is_set)
        *rect = priv->rect;
      else
        {
          rect->x = rect->y = G_MININT;
          rect->width = rect->height = G_MAXINT;
        }
    }

  return priv->rect_is_set;
}