gtkeventcontrollermotion.c 10.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* 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>
 */

/**
21
 * GtkEventControllerMotion:
22
 *
23
24
25
26
27
28
29
30
31
32
 * `GtkEventControllerMotion` is an event controller tracking the pointer
 * position.
 *
 * The event controller offers [signal@Gtk.EventControllerMotion::enter]
 * and [signal@Gtk.EventControllerMotion::leave] signals, as well as
 * [property@Gtk.EventControllerMotion:is-pointer] and
 * [property@Gtk.EventControllerMotion:contains-pointer] properties
 * which are updated to reflect changes in the pointer position as it
 * moves over the widget.
 */
33
34
35
#include "config.h"

#include "gtkintl.h"
36
37
#include "gtkprivate.h"
#include "gtkwidgetprivate.h"
38
#include "gtkmarshalers.h"
39
40
41
42
43
44
45
46
#include "gtkeventcontrollerprivate.h"
#include "gtkeventcontrollermotion.h"
#include "gtktypebuiltins.h"
#include "gtkmarshalers.h"

struct _GtkEventControllerMotion
{
  GtkEventController parent_instance;
47

48
49
  guint is_pointer             : 1;
  guint contains_pointer       : 1;
50
51
52
53
54
55
56
57
};

struct _GtkEventControllerMotionClass
{
  GtkEventControllerClass parent_class;
};

enum {
58
59
  ENTER,
  LEAVE,
60
61
62
63
  MOTION,
  N_SIGNALS
};

64
enum {
65
66
  PROP_IS_POINTER = 1,
  PROP_CONTAINS_POINTER,
67
68
69
70
71
  NUM_PROPERTIES
};

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

72
73
74
75
static guint signals[N_SIGNALS] = { 0 };

G_DEFINE_TYPE (GtkEventControllerMotion, gtk_event_controller_motion, GTK_TYPE_EVENT_CONTROLLER)

76
77
static gboolean
gtk_event_controller_motion_handle_event (GtkEventController *controller,
Matthias Clasen's avatar
Matthias Clasen committed
78
                                          GdkEvent           *event,
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
                                          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);
}

94
static void
95
update_pointer_focus (GtkEventController    *controller,
96
97
98
                      const GtkCrossingData *crossing,
                      double                 x,
                      double                 y)
99
{
100
101
102
103
  GtkEventControllerMotion *motion = GTK_EVENT_CONTROLLER_MOTION (controller);
  GtkWidget *widget = gtk_event_controller_get_widget (controller);
  gboolean is_pointer = FALSE;
  gboolean contains_pointer = FALSE;
104
105
  gboolean enter = FALSE;
  gboolean leave = FALSE;
106

107
  if (crossing->direction == GTK_CROSSING_IN)
108
    {
109
110
111
112
     if (crossing->new_descendent != NULL)
        {
          contains_pointer = TRUE;
        }
113
      if (crossing->new_target == widget)
114
115
116
117
118
119
120
121
122
        {
          contains_pointer = TRUE;
          is_pointer = TRUE;
        }
    }
  else
    {
      if (crossing->new_descendent != NULL ||
          crossing->new_target == widget)
123
        contains_pointer = TRUE;
124
      is_pointer = FALSE;
125
126
    }

127
128
  if (motion->contains_pointer != contains_pointer)
    {
129
130
      enter = contains_pointer;
      leave = !contains_pointer;
131
132
133
    }

  if (leave)
134
    g_signal_emit (controller, signals[LEAVE], 0);
135

136
  g_object_freeze_notify (G_OBJECT (motion));
137
  if (motion->is_pointer != is_pointer)
138
    {
139
      motion->is_pointer = is_pointer;
140
      g_object_notify_by_pspec (G_OBJECT (motion), props[PROP_IS_POINTER]);
141
    }
142
  if (motion->contains_pointer != contains_pointer)
143
    {
144
      motion->contains_pointer = contains_pointer;
145
      g_object_notify_by_pspec (G_OBJECT (motion), props[PROP_CONTAINS_POINTER]);
146
147
    }
  g_object_thaw_notify (G_OBJECT (motion));
148
149

  if (enter)
150
    g_signal_emit (controller, signals[ENTER], 0, x, y);
151
152
}

153
154
155
156
157
static void
gtk_event_controller_motion_handle_crossing (GtkEventController    *controller,
                                             const GtkCrossingData *crossing,
                                             double                 x,
                                             double                 y)
158
{
159
160
  if (crossing->type == GTK_CROSSING_POINTER)
    update_pointer_focus (controller, crossing, x, y);
161
162
}

163
164
165
166
167
168
169
170
171
172
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)
    {
173
174
    case PROP_IS_POINTER:
      g_value_set_boolean (value, controller->is_pointer);
175
176
      break;

177
178
    case PROP_CONTAINS_POINTER:
      g_value_set_boolean (value, controller->contains_pointer);
179
180
181
182
183
184
185
      break;

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

186
187
188
static void
gtk_event_controller_motion_class_init (GtkEventControllerMotionClass *klass)
{
189
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
190
191
  GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);

192
193
  object_class->get_property = gtk_event_controller_motion_get_property;

194
  controller_class->handle_event = gtk_event_controller_motion_handle_event;
195
  controller_class->handle_crossing = gtk_event_controller_motion_handle_crossing;
196

197
  /**
198
   * GtkEventControllerMotion:is-pointer: (attributes org.gtk.Property.get=gtk_event_controller_motion_is_pointer)
199
200
   *
   * Whether the pointer is in the controllers widget itself,
201
202
203
   * as opposed to in a descendent widget.
   *
   * See also [property@Gtk.EventControllerMotion:contains-pointer].
204
205
   *
   * When handling crossing events, this property is updated
206
207
   * before [signal@Gtk.EventControllerMotion::enter], but after
   * [signal@Gtk.EventControllerMotion::leave] is emitted.
208
   */
209
210
211
  props[PROP_IS_POINTER] =
      g_param_spec_boolean ("is-pointer",
                            P_("Is Pointer"),
212
213
214
215
216
                            P_("Whether the pointer is in the controllers widget"),
                            FALSE,
                            G_PARAM_READABLE);

  /**
217
   * GtkEventControllerMotion:contains-pointer: (attributes org.gtk.Property.get=gtk_event_controller_motion_contains_pointer)
218
   *
219
   * Whether the pointer is in the controllers widget or a descendant.
220
221
   *
   * See also [property@Gtk.EventControllerMotion:is-pointer].
222
223
   *
   * When handling crossing events, this property is updated
224
225
   * before [signal@Gtk.EventControllerMotion::enter], but after
   * [signal@Gtk.EventControllerMotion::leave] is emitted.
226
   */
227
228
229
  props[PROP_CONTAINS_POINTER] =
      g_param_spec_boolean ("contains-pointer",
                            P_("Contains Pointer"),
Jordi Mas's avatar
Jordi Mas committed
230
                            P_("Whether the pointer is in the controllers widget or a descendant"),
231
232
233
                            FALSE,
                            G_PARAM_READABLE);

234
235
  g_object_class_install_properties (object_class, NUM_PROPERTIES, props);

236
  /**
237
   * GtkEventControllerMotion::enter:
238
239
240
241
   * @controller: the object which received the signal
   * @x: coordinates of pointer location
   * @y: coordinates of pointer location
   *
242
   * Signals that the pointer has entered the widget.
243
   */
244
245
  signals[ENTER] =
    g_signal_new (I_("enter"),
246
                  GTK_TYPE_EVENT_CONTROLLER_MOTION,
247
                  G_SIGNAL_RUN_LAST,
248
                  0, NULL, NULL,
249
                  NULL,
250
                  G_TYPE_NONE, 2,
251
                  G_TYPE_DOUBLE,
252
                  G_TYPE_DOUBLE);
253

254
255
256
257
258
259
260
261
262
263
264
265
  /**
   * GtkEventControllerMotion::leave:
   * @controller: the object which received the signal
   *
   * Signals that the pointer has left the widget.
   */
  signals[LEAVE] =
    g_signal_new (I_("leave"),
                  GTK_TYPE_EVENT_CONTROLLER_MOTION,
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL,
                  NULL,
266
                  G_TYPE_NONE, 0);
267

268
269
270
271
272
273
274
275
276
277
278
279
280
  /**
   * 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,
281
                  _gtk_marshal_VOID__DOUBLE_DOUBLE,
282
                  G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
283
284
285
  g_signal_set_va_marshaller (signals[MOTION],
                              G_TYPE_FROM_CLASS (klass),
                              _gtk_marshal_VOID__DOUBLE_DOUBLEv);
286
287
288
289
290
291
292
293
294
295
}

static void
gtk_event_controller_motion_init (GtkEventControllerMotion *motion)
{
}

/**
 * gtk_event_controller_motion_new:
 *
296
 * Creates a new event controller that will handle motion events.
297
 *
298
 * Returns: a new `GtkEventControllerMotion`
299
300
 **/
GtkEventController *
301
gtk_event_controller_motion_new (void)
302
303
304
305
{
  return g_object_new (GTK_TYPE_EVENT_CONTROLLER_MOTION,
                       NULL);
}
306

307
/**
308
309
 * gtk_event_controller_motion_contains_pointer: (attributes org.gtk.Method.get_property=contains-pointer)
 * @self: a `GtkEventControllerMotion`
310
 *
311
 * Returns if a pointer is within @self or one of its children.
312
313
314
315
316
317
318
319
320
321
322
323
 *
 * 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;
}

/**
324
325
 * gtk_event_controller_motion_is_pointer: (attributes org.gtk.Method.get_property=is-pointer)
 * @self: a `GtkEventControllerMotion`
326
 *
327
 * Returns if a pointer is within @self, but not one of its children.
328
329
330
331
332
333
334
335
336
337
 *
 * 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;
}