summaryrefslogtreecommitdiff
path: root/gtk/gtkpressandhold.c
blob: 5635c6caf0c078ccef34ca2fef286eb74dc12dc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* GTK - The GIMP Toolkit
 * Copyright (C) 2012 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/>.
 */

#include "config.h"

#include "gdk.h"
#include "gtkpressandholdprivate.h"
#include "gtkintl.h"
#include "gtkprivate.h"

struct _GtkPressAndHoldPrivate
{
  gint hold_time;
  gint drag_threshold;

  GdkEventSequence *sequence;
  GdkDevice *device;
  guint button;
  guint timeout;
  gint start_x;
  gint start_y;
  gint x;
  gint y;
};

enum
{
  PROP_ZERO,
  PROP_HOLD_TIME,
  PROP_DRAG_THRESHOLD
};

enum
{
  HOLD,
  TAP,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

G_DEFINE_TYPE_WITH_PRIVATE (GtkPressAndHold, gtk_press_and_hold, G_TYPE_OBJECT)

static void
gtk_press_and_hold_init (GtkPressAndHold *pah)
{
  pah->priv = gtk_press_and_hold_get_instance_private (pah);
  pah->priv->hold_time = 1000;
  pah->priv->drag_threshold = 8;
}

static void
press_and_hold_finalize (GObject *object)
{
  GtkPressAndHold *pah = GTK_PRESS_AND_HOLD (object);

  if (pah->priv->timeout)
    g_source_remove (pah->priv->timeout);

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

static void
press_and_hold_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GtkPressAndHold *pah = GTK_PRESS_AND_HOLD (object);

  switch (prop_id)
    {
    case PROP_HOLD_TIME:
      g_value_set_int (value, pah->priv->hold_time);
      break;
    case PROP_DRAG_THRESHOLD:
      g_value_set_int (value, pah->priv->drag_threshold);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
press_and_hold_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GtkPressAndHold *pah = GTK_PRESS_AND_HOLD (object);

  switch (prop_id)
    {
    case PROP_HOLD_TIME:
      pah->priv->hold_time = g_value_get_int (value);
      break;
    case PROP_DRAG_THRESHOLD:
      pah->priv->hold_time = g_value_get_int (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_press_and_hold_class_init (GtkPressAndHoldClass *class)
{
  GObjectClass *object_class = (GObjectClass *)class;

  object_class->get_property = press_and_hold_get_property;
  object_class->set_property = press_and_hold_set_property;
  object_class->finalize = press_and_hold_finalize;

  signals[HOLD] =
    g_signal_new ("hold",
                  GTK_TYPE_PRESS_AND_HOLD,
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkPressAndHoldClass, hold),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);

  signals[TAP] =
    g_signal_new ("tap",
                  GTK_TYPE_PRESS_AND_HOLD,
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkPressAndHoldClass, tap),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);

  g_object_class_install_property (object_class, PROP_HOLD_TIME,
      g_param_spec_int ("hold-time", P_("Hold Time"), P_("Hold Time (in milliseconds)"),
                        0, G_MAXINT, 1000, GTK_PARAM_READWRITE));

  g_object_class_install_property (object_class, PROP_DRAG_THRESHOLD,
      g_param_spec_int ("drag-threshold", P_("Drag Threshold"), P_("Drag Threshold (in pixels)"),
                        1, G_MAXINT, 8, GTK_PARAM_READWRITE));
}

static void
press_and_hold_cancel (GtkPressAndHold *pah)
{
  GtkPressAndHoldPrivate *priv = pah->priv;

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

  priv->timeout = 0;
  priv->sequence = NULL;
  priv->device = NULL;
  priv->button = 0;
}

static gboolean
hold_action (gpointer data)
{
  GtkPressAndHold *pah = data;
  GtkPressAndHoldPrivate *priv = pah->priv;

  press_and_hold_cancel (pah);

  g_signal_emit (pah, signals[HOLD], 0, priv->x, priv->y);

  return G_SOURCE_REMOVE;
}

void
gtk_press_and_hold_process_event (GtkPressAndHold *pah,
                                  GdkEvent        *event)
{
  GtkPressAndHoldPrivate *priv = pah->priv;

  /* We're already tracking a different input, ignore */
  if ((event->type == GDK_TOUCH_BEGIN && priv->sequence != NULL) ||
      (event->type == GDK_BUTTON_PRESS && priv->device != NULL) ||
      (event->type == GDK_TOUCH_UPDATE && priv->sequence != event->touch.sequence) ||
      (event->type == GDK_TOUCH_END && priv->sequence != event->touch.sequence) ||
      (event->type == GDK_TOUCH_CANCEL && priv->sequence != event->touch.sequence) ||
      (event->type == GDK_BUTTON_RELEASE && priv->device != event->button.device) ||
      (event->type == GDK_BUTTON_RELEASE && priv->button != event->button.button) ||
      (event->type == GDK_MOTION_NOTIFY && priv->device != event->motion.device))
    return;

  if (event->type == GDK_TOUCH_BEGIN)
    {
      priv->sequence = event->touch.sequence;
      priv->x = event->touch.x;
      priv->y = event->touch.y;
      priv->start_x = priv->x;
      priv->start_y = priv->y;
      priv->timeout =
          gdk_threads_add_timeout (priv->hold_time, hold_action, pah);
    }
  else if (event->type == GDK_TOUCH_UPDATE)
    {
      priv->x = event->touch.x;
      priv->y = event->touch.y;
      if (ABS (priv->x - priv->start_x) > priv->drag_threshold ||
          ABS (priv->y - priv->start_y) > priv->drag_threshold)
        press_and_hold_cancel (pah);
    }
  else if (event->type == GDK_TOUCH_END)
    {
      press_and_hold_cancel (pah);
      g_signal_emit (pah, signals[TAP], 0, priv->x, priv->y);
    }
  else if (event->type == GDK_TOUCH_CANCEL)
    {
      press_and_hold_cancel (pah);
    }
  else if (event->type == GDK_BUTTON_PRESS)
    {
      priv->device = event->button.device;
      priv->button = event->button.button;
      priv->x = event->button.x;
      priv->y = event->button.y;
      priv->start_x = priv->x;
      priv->start_y = priv->y;
      priv->timeout =
          gdk_threads_add_timeout (priv->hold_time, hold_action, pah);
    }
  else if (event->type == GDK_BUTTON_RELEASE)
    {
      press_and_hold_cancel (pah);
    }
  else if (event->type == GDK_MOTION_NOTIFY)
    {
      priv->x = event->motion.x;
      priv->y = event->motion.y;
      if (ABS (priv->x - priv->start_x) > priv->drag_threshold ||
          ABS (priv->y - priv->start_y) > priv->drag_threshold)
        press_and_hold_cancel (pah);
    }
}

GtkPressAndHold *
gtk_press_and_hold_new (void)
{
  return (GtkPressAndHold *) g_object_new (GTK_TYPE_PRESS_AND_HOLD, NULL);
}