gtkdropcontrollermotion.c 11.9 KB
Newer Older
Benjamin Otte's avatar
Benjamin Otte committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright © 2020 Benjamin Otte
 *
 * 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.1 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/>.
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

/**
21
 * GtkDropControllerMotion:
Benjamin Otte's avatar
Benjamin Otte committed
22
 *
23
24
 * `GtkDropControllerMotion` is an event controller tracking
 * the pointer during Drag-and-Drop operations.
Benjamin Otte's avatar
Benjamin Otte committed
25
 *
26
27
 * It is modeled after [class@Gtk.EventControllerMotion] so if you
 * have used that, this should feel really familiar.
Benjamin Otte's avatar
Benjamin Otte committed
28
 *
29
 * This controller is not able to accept drops, use [class@Gtk.DropTarget]
Benjamin Otte's avatar
Benjamin Otte committed
30
 * for that purpose.
31
 */
Benjamin Otte's avatar
Benjamin Otte committed
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

#include "config.h"

#include "gtkdropcontrollermotion.h"

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

struct _GtkDropControllerMotion
{
  GtkEventController parent_instance;

  GdkDrop *drop;
  guint is_pointer             : 1;
  guint contains_pointer       : 1;
};

struct _GtkDropControllerMotionClass
{
  GtkEventControllerClass parent_class;
};

enum {
  ENTER,
  LEAVE,
  MOTION,
  N_SIGNALS
};

enum {
  PROP_0,
  PROP_CONTAINS_POINTER,
  PROP_DROP,
  PROP_IS_POINTER,
  NUM_PROPERTIES
};

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

static guint signals[N_SIGNALS] = { 0 };

G_DEFINE_TYPE (GtkDropControllerMotion, gtk_drop_controller_motion, GTK_TYPE_EVENT_CONTROLLER)

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

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

  parent_class = GTK_EVENT_CONTROLLER_CLASS (gtk_drop_controller_motion_parent_class);

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

static void
update_pointer_focus (GtkEventController    *controller,
                      const GtkCrossingData *crossing,
                      double                 x,
                      double                 y)
{
  GtkDropControllerMotion *self = GTK_DROP_CONTROLLER_MOTION (controller);
  GtkWidget *widget = gtk_event_controller_get_widget (controller);
  gboolean is_pointer = FALSE;
  gboolean contains_pointer = FALSE;
  gboolean enter = FALSE;
  gboolean leave = FALSE;

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

  if (self->contains_pointer != contains_pointer)
    {
      enter = contains_pointer;
      leave = !contains_pointer;
    }

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

  g_object_freeze_notify (G_OBJECT (self));
  if (self->is_pointer != is_pointer)
    {
      self->is_pointer = is_pointer;
      g_object_notify (G_OBJECT (self), "is-pointer");
    }
  if (self->contains_pointer != contains_pointer)
    {
      self->contains_pointer = contains_pointer;
      if (contains_pointer)
        self->drop = g_object_ref (crossing->drop);
      else
        g_clear_object (&self->drop);
      g_object_notify (G_OBJECT (self), "contains-pointer");
      g_object_notify (G_OBJECT (self), "drop");
    }
  g_object_thaw_notify (G_OBJECT (self));

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

static void
gtk_drop_controller_motion_handle_crossing (GtkEventController    *controller,
                                            const GtkCrossingData *crossing,
                                            double                 x,
                                            double                 y)
{
  if (crossing->type == GTK_CROSSING_DROP)
    update_pointer_focus (controller, crossing, x, y);
}

static void
gtk_drop_controller_motion_get_property (GObject    *object,
                                         guint       prop_id,
                                         GValue     *value,
                                         GParamSpec *pspec)
{
  GtkDropControllerMotion *self = GTK_DROP_CONTROLLER_MOTION (object);

  switch (prop_id)
    {
    case PROP_CONTAINS_POINTER:
      g_value_set_boolean (value, self->contains_pointer);
      break;

    case PROP_DROP:
      g_value_set_object (value, self->drop);
      break;

    case PROP_IS_POINTER:
      g_value_set_boolean (value, self->is_pointer);
      break;

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

static void
gtk_drop_controller_motion_class_init (GtkDropControllerMotionClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);

  object_class->get_property = gtk_drop_controller_motion_get_property;

  controller_class->handle_event = gtk_drop_controller_motion_handle_event;
  controller_class->handle_crossing = gtk_drop_controller_motion_handle_crossing;

  /**
211
212
213
214
   * GtkDropControllerMotion:contains-pointer: (attributes org.gtk.Property.get=gtk_drop_controller_motion_contains_pointer)
   *
   * Whether the pointer of a Drag-and-Drop operation is in
   * the controller's widget or a descendant.
Benjamin Otte's avatar
Benjamin Otte committed
215
   *
216
   * See also [property@Gtk.DropControllerMotion:is-pointer].
Benjamin Otte's avatar
Benjamin Otte committed
217
218
   *
   * When handling crossing events, this property is updated
219
220
   * before [signal@Gtk.DropControllerMotion::enter], but after
   * [signal@Gtk.DropControllerMotion::leave] is emitted.
Benjamin Otte's avatar
Benjamin Otte committed
221
222
223
224
   */
  props[PROP_CONTAINS_POINTER] =
      g_param_spec_boolean ("contains-pointer",
                            P_("Contains Pointer"),
Jordi Mas's avatar
Jordi Mas committed
225
                            P_("Whether the pointer is in the controllers widget or a descendant"),
Benjamin Otte's avatar
Benjamin Otte committed
226
227
228
229
                            FALSE,
                            G_PARAM_READABLE);

  /**
230
231
232
233
   * GtkDropControllerMotion:drop: (attributes org.gtk.Property.get=gtk_drop_controller_motion_get_drop)
   *
   * The ongoing drop operation over the controller's widget or
   * its descendant.
Benjamin Otte's avatar
Benjamin Otte committed
234
235
236
   *
   * If no drop operation is going on, this property returns %NULL.
   *
237
238
   * The event controller should not modify the @drop, but it might
   * want to query its properties.
Benjamin Otte's avatar
Benjamin Otte committed
239
240
   *
   * When handling crossing events, this property is updated
241
242
   * before [signal@Gtk.DropControllerMotion::enter], but after
   * [signal@Gtk.DropControllerMotion::leave] is emitted.
Benjamin Otte's avatar
Benjamin Otte committed
243
244
245
246
247
248
249
250
251
   */
  props[PROP_DROP] =
      g_param_spec_object ("drop",
                           P_("Drop"),
                           P_("The ongoing drop operation"),
                           GDK_TYPE_DROP,
                           G_PARAM_READABLE);

  /**
252
   * GtkDropControllerMotion:is-pointer: (attributes org.gtk.Property.get=gtk_drop_controller_motion_is_pointer)
Benjamin Otte's avatar
Benjamin Otte committed
253
254
   *
   * Whether the pointer is in the controllers widget itself,
255
256
257
   * as opposed to in a descendent widget.
   *
   * See also [property@Gtk.DropControllerMotion:contains-pointer].
Benjamin Otte's avatar
Benjamin Otte committed
258
259
   *
   * When handling crossing events, this property is updated
260
261
   * before [signal@Gtk.DropControllerMotion::enter], but after
   * [signal@Gtk.DropControllerMotion::leave] is emitted.
Benjamin Otte's avatar
Benjamin Otte committed
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
   */
  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);

  g_object_class_install_properties (object_class, NUM_PROPERTIES, props);

  /**
   * GtkDropControllerMotion::enter:
   * @self: the object which received the signal
   * @x: coordinates of pointer location
   * @y: coordinates of pointer location
   *
   * Signals that the pointer has entered the widget.
   */
  signals[ENTER] =
    g_signal_new (I_("enter"),
                  GTK_TYPE_DROP_CONTROLLER_MOTION,
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL,
                  NULL,
                  G_TYPE_NONE, 2,
                  G_TYPE_DOUBLE,
                  G_TYPE_DOUBLE);
  g_signal_set_va_marshaller (signals[ENTER],
                              G_TYPE_FROM_CLASS (klass),
                              _gtk_marshal_VOID__DOUBLE_DOUBLEv);

  /**
   * GtkDropControllerMotion::leave:
   * @self: the object which received the signal
   *
   * Signals that the pointer has left the widget.
   */
  signals[LEAVE] =
    g_signal_new (I_("leave"),
                  GTK_TYPE_DROP_CONTROLLER_MOTION,
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL,
                  NULL,
                  G_TYPE_NONE, 0);

  /**
   * GtkDropControllerMotion::motion:
   * @self: 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_DROP_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_drop_controller_motion_init (GtkDropControllerMotion *self)
{
}

/**
 * gtk_drop_controller_motion_new:
 *
 * Creates a new event controller that will handle pointer motion
 * events during drag and drop.
 *
340
 * Returns: a new `GtkDropControllerMotion`
Benjamin Otte's avatar
Benjamin Otte committed
341
342
343
344
345
346
347
348
349
 **/
GtkEventController *
gtk_drop_controller_motion_new (void)
{
  return g_object_new (GTK_TYPE_DROP_CONTROLLER_MOTION,
                       NULL);
}

/**
350
351
 * gtk_drop_controller_motion_contains_pointer: (attributes org.gtk.Method.get_property=contains-pointer)
 * @self: a `GtkDropControllerMotion`
Benjamin Otte's avatar
Benjamin Otte committed
352
 *
353
354
 * Returns if a Drag-and-Drop operation is within the widget
 * @self or one of its children.
Benjamin Otte's avatar
Benjamin Otte committed
355
356
357
358
359
360
361
362
363
364
365
366
 *
 * Returns: %TRUE if a dragging pointer is within @self or one of its children.
 */
gboolean
gtk_drop_controller_motion_contains_pointer (GtkDropControllerMotion *self)
{
  g_return_val_if_fail (GTK_IS_DROP_CONTROLLER_MOTION (self), FALSE);

  return self->contains_pointer;
}

/**
367
368
 * gtk_drop_controller_motion_get_drop: (attributes org.gtk.Method.get_property=drop)
 * @self: a `GtkDropControllerMotion`
Benjamin Otte's avatar
Benjamin Otte committed
369
 *
370
371
 * Returns the `GdkDrop` of a current Drag-and-Drop operation
 * over the widget of @self.
Benjamin Otte's avatar
Benjamin Otte committed
372
 *
373
 * Returns: (transfer none) (nullable): The `GdkDrop` currently
Matthias Clasen's avatar
Matthias Clasen committed
374
 *   happening within @self
Benjamin Otte's avatar
Benjamin Otte committed
375
376
377
378
379
380
381
382
383
384
 */
GdkDrop *
gtk_drop_controller_motion_get_drop (GtkDropControllerMotion *self)
{
  g_return_val_if_fail (GTK_IS_DROP_CONTROLLER_MOTION (self), FALSE);

  return self->drop;
}

/**
385
386
 * gtk_drop_controller_motion_is_pointer: (attributes org.gtk.Method.get_property=is-pointer)
 * @self: a `GtkDropControllerMotion`
Benjamin Otte's avatar
Benjamin Otte committed
387
 *
388
389
 * Returns if a Drag-and-Drop operation is within the widget
 * @self, not one of its children.
Benjamin Otte's avatar
Benjamin Otte committed
390
 *
391
392
 * Returns: %TRUE if a dragging pointer is within @self but
 *   not one of its children
Benjamin Otte's avatar
Benjamin Otte committed
393
394
395
396
397
398
399
400
 */
gboolean
gtk_drop_controller_motion_is_pointer (GtkDropControllerMotion *self)
{
  g_return_val_if_fail (GTK_IS_DROP_CONTROLLER_MOTION (self), FALSE);

  return self->is_pointer;
}