summaryrefslogtreecommitdiff
path: root/gtk/gtkeventcontrollermotion.c
blob: 54b158dbd36b13ad68b23c25b06a13aa8fa4c42c (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
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
351
352
353
354
355
356
/* GTK - The GIMP Toolkit
 * Copyright (C) 2017, 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): Matthias Clasen <mclasen@redhat.com>
 */

/**
 * SECTION:gtkeventcontrollermotion
 * @Short_description: Event controller for motion events
 * @Title: GtkEventControllerMotion
 * @See_also: #GtkEventController
 *
 * #GtkEventControllerMotion is an event controller meant for situations
 * where you need to track the position of the pointer.
 **/
#include "config.h"

#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtkwidgetprivate.h"
#include "gtkmarshalers.h"
#include "gtkeventcontrollerprivate.h"
#include "gtkeventcontrollermotion.h"
#include "gtktypebuiltins.h"
#include "gtkmarshalers.h"

struct _GtkEventControllerMotion
{
  GtkEventController parent_instance;

  const GdkEvent *current_event;
  const GtkCrossingData *current_crossing;

  guint is_pointer             : 1;
  guint contains_pointer       : 1;
};

struct _GtkEventControllerMotionClass
{
  GtkEventControllerClass parent_class;
};

enum {
  MOTION,
  POINTER_CHANGE,
  N_SIGNALS
};

enum {
  PROP_IS_POINTER = 1,
  PROP_CONTAINS_POINTER,
  NUM_PROPERTIES
};

static GParamSpec *props[NUM_PROPERTIES] = { NULL, };

static guint signals[N_SIGNALS] = { 0 };

G_DEFINE_TYPE (GtkEventControllerMotion, gtk_event_controller_motion, GTK_TYPE_EVENT_CONTROLLER)

static gboolean
gtk_event_controller_motion_handle_event (GtkEventController *controller,
                                          const GdkEvent     *event,
                                          double              x,
                                          double              y)
{
  GtkEventControllerClass *parent_class;
  GdkEventType type;

  type = gdk_event_get_event_type (event);
  if (type == GDK_MOTION_NOTIFY)
    g_signal_emit (controller, signals[MOTION], 0, x, y);

  parent_class = GTK_EVENT_CONTROLLER_CLASS (gtk_event_controller_motion_parent_class);

  return parent_class->handle_event (controller, event, x, y);
}

static void
update_pointer_focus (GtkEventController    *controller,
                      const GtkCrossingData *crossing)
{
  GtkEventControllerMotion *motion = GTK_EVENT_CONTROLLER_MOTION (controller);
  GtkWidget *widget = gtk_event_controller_get_widget (controller);
  gboolean is_pointer = FALSE;
  gboolean contains_pointer = FALSE;

  if (crossing->direction == GTK_CROSSING_IN)
    {
      if (crossing->new_target == widget)
        is_pointer = TRUE;
      if (crossing->new_target != NULL)
        contains_pointer = TRUE;
    }

  g_object_freeze_notify (G_OBJECT (motion));
  if (motion->is_pointer != is_pointer)
    {
      motion->is_pointer = is_pointer;
      g_object_notify (G_OBJECT (motion), "is-pointer");
    }
  if (motion->contains_pointer != contains_pointer)
    {
      motion->contains_pointer = contains_pointer;
      g_object_notify (G_OBJECT (motion), "contains-pointer");
    }
  g_object_thaw_notify (G_OBJECT (motion));
}

static void
gtk_event_controller_motion_handle_crossing (GtkEventController    *controller,
                                             const GtkCrossingData *crossing,
                                             double                 x,
                                             double                 y)
{
  GtkEventControllerMotion *motion = GTK_EVENT_CONTROLLER_MOTION (controller);

  if (crossing->type != GTK_CROSSING_POINTER)
    return;

  motion->current_crossing = crossing;

  update_pointer_focus (controller, crossing);

  g_signal_emit (controller, signals[POINTER_CHANGE], 0,
                 crossing->direction,
                 x,
                 y,
                 crossing->mode);

  motion->current_crossing = NULL;
}

static void
gtk_event_controller_motion_get_property (GObject    *object,
                                          guint       prop_id,
                                          GValue     *value,
                                          GParamSpec *pspec)
{
  GtkEventControllerMotion *controller = GTK_EVENT_CONTROLLER_MOTION (object);

  switch (prop_id)
    {
    case PROP_IS_POINTER:
      g_value_set_boolean (value, controller->is_pointer);
      break;

    case PROP_CONTAINS_POINTER:
      g_value_set_boolean (value, controller->contains_pointer);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_event_controller_motion_class_init (GtkEventControllerMotionClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);

  object_class->get_property = gtk_event_controller_motion_get_property;

  controller_class->handle_event = gtk_event_controller_motion_handle_event;
  controller_class->handle_crossing = gtk_event_controller_motion_handle_crossing;

  /**
   * GtkEventControllerMotion:is-pointer:
   *
   * Whether the pointer is in the controllers widget itself,
   * as opposed to in a descendent widget. See also
   * #GtkEventControllerMotion:contains-pointer.
   *
   * When handling crossing events, this property is updated
   * before #GtkEventControllerMotion::enter or
   * #GtkEventControllerMotion::leave are emitted.
   */
  props[PROP_IS_POINTER] =
      g_param_spec_boolean ("is-pointer",
                            P_("Is Pointer"),
                            P_("Whether the pointer is in the controllers widget"),
                            FALSE,
                            G_PARAM_READABLE);

  /**
   * GtkEventControllerMotion:contains-pointer:
   *
   * Whether the pointer is in the controllers widget or a descendant.
   * See also #GtkEventControllerMotion:is-pointer.
   *
   * When handling crossing events, this property is updated
   * before #GtkEventControllerMotion::enter or
   * #GtkEventControllerMotion::leave are emitted.
   */
  props[PROP_CONTAINS_POINTER] =
      g_param_spec_boolean ("contains-pointer",
                            P_("Contains Pointer"),
                            P_("Whether the pointer is inthe controllers widget or a descendant"),
                            FALSE,
                            G_PARAM_READABLE);

  g_object_class_install_properties (object_class, NUM_PROPERTIES, props);

  /**
   * GtkEventControllerMotion::pointer-change:
   * @controller: the object which received the signal
   * @direction: the direction of this crossing event
   * @x: coordinates of pointer location
   * @y: coordinates of pointer location
   * @mode: crossing mode
   *
   * This signal is emitted whenever the pointer focus changes
   * from or to a widget that is a descendant of the widget to
   * which @controller is attached.
   *
   * Handlers for this signal can use
   * gtk_event_controller_motion_get_pointer_origin() and
   * gtk_event_controller_motion_get_pointer_target() to find
   * the old and new pointer locations.
   */
  signals[POINTER_CHANGE] =
    g_signal_new (I_("pointer-change"),
                  GTK_TYPE_EVENT_CONTROLLER_MOTION,
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL,
                  NULL,
                  G_TYPE_NONE, 4,
                  GTK_TYPE_CROSSING_DIRECTION,
                  G_TYPE_DOUBLE,
                  G_TYPE_DOUBLE,
                  GDK_TYPE_CROSSING_MODE);

  /**
   * GtkEventControllerMotion::motion:
   * @controller: The object that received the signal
   * @x: the x coordinate
   * @y: the y coordinate
   *
   * Emitted when the pointer moves inside the widget.
   */
  signals[MOTION] =
    g_signal_new (I_("motion"),
                  GTK_TYPE_EVENT_CONTROLLER_MOTION,
                  G_SIGNAL_RUN_FIRST,
                  0, NULL, NULL,
                  _gtk_marshal_VOID__DOUBLE_DOUBLE,
                  G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
  g_signal_set_va_marshaller (signals[MOTION],
                              G_TYPE_FROM_CLASS (klass),
                              _gtk_marshal_VOID__DOUBLE_DOUBLEv);
}

static void
gtk_event_controller_motion_init (GtkEventControllerMotion *motion)
{
}

/**
 * gtk_event_controller_motion_new:
 *
 * Creates a new event controller that will handle motion events.
 *
 * Returns: a new #GtkEventControllerMotion
 **/
GtkEventController *
gtk_event_controller_motion_new (void)
{
  return g_object_new (GTK_TYPE_EVENT_CONTROLLER_MOTION,
                       NULL);
}

/**
 * gtk_event_controller_motion_get_pointer_origin:
 * @controller: a #GtkEventControllerMotion
 *
 * Returns the widget that contained the pointer before.
 *
 * This function can only be used in handlers for the
 * #GtkEventControllerMotion::pointer-change signal.
 *
 * Returns: (transfer none): the previous pointer focus
 */
GtkWidget *
gtk_event_controller_motion_get_pointer_origin (GtkEventControllerMotion *controller)
{
  g_return_val_if_fail (GTK_IS_EVENT_CONTROLLER_MOTION (controller), NULL);
  g_return_val_if_fail (controller->current_crossing != NULL, NULL);

  return controller->current_crossing->old_target;
}

/**
 * gtk_event_controller_motion_get_pointer_target:
 * @controller: a #GtkEventControllerMotion
 *
 * Returns the widget that will contain the pointer afterwards.
 *
 * This function can only be used in handlers for the
 * #GtkEventControllerMotion::pointer-change signal.
 *
 * Returns: (transfer none): the next pointer focus
 */
GtkWidget *
gtk_event_controller_motion_get_pointer_target (GtkEventControllerMotion *controller)
{
  g_return_val_if_fail (GTK_IS_EVENT_CONTROLLER_MOTION (controller), NULL);
  g_return_val_if_fail (controller->current_crossing != NULL, NULL);

  return controller->current_crossing->new_target;
}

/**
 * gtk_event_controller_motion_contains_pointer:
 * @self: a #GtkEventControllerMotion
 *
 * Returns the value of the GtkEventControllerMotion:contains-pointer property.
 *
 * Returns: %TRUE if a pointer is within @self or one of its children
 */
gboolean
gtk_event_controller_motion_contains_pointer (GtkEventControllerMotion *self)
{
  g_return_val_if_fail (GTK_IS_EVENT_CONTROLLER_MOTION (self), FALSE);

  return self->contains_pointer;
}

/**
 * gtk_event_controller_motion_is_pointer:
 * @self: a #GtkEventControllerKey
 *
 * Returns the value of the GtkEventControllerMotion:is-pointer property.
 *
 * Returns: %TRUE if a pointer is within @self but not one of its children
 */
gboolean
gtk_event_controller_motion_is_pointer (GtkEventControllerMotion *self)
{
  g_return_val_if_fail (GTK_IS_EVENT_CONTROLLER_MOTION (self), FALSE);

  return self->is_pointer;
}