gtkdragsource.c 24.4 KB
Newer Older
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
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * 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/>.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

#include "config.h"

27
#include "gtkdragsourceprivate.h"
28
29

#include "gtkgesturedrag.h"
Matthias Clasen's avatar
Matthias Clasen committed
30
#include "gtkgesturesingleprivate.h"
31
#include "gtkimagedefinitionprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
32
33
#include "gtknative.h"
#include "gtkwidgetprivate.h"
34
#include "gtkintl.h"
Matthias Clasen's avatar
Matthias Clasen committed
35
#include "gtkimageprivate.h"
36
#include "gtkdragicon.h"
Matthias Clasen's avatar
Matthias Clasen committed
37
38
#include "gtkprivate.h"
#include "gtkmarshalers.h"
39
40
#include "gtkicontheme.h"
#include "gtkpicture.h"
41
#include "gtksettingsprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
42
#include "gtkgesturesingle.h"
43

Matthias Clasen's avatar
Matthias Clasen committed
44
/**
Matthias Clasen's avatar
Matthias Clasen committed
45
 * GtkDragSource:
Matthias Clasen's avatar
Matthias Clasen committed
46
 *
Matthias Clasen's avatar
Matthias Clasen committed
47
 * `GtkDragSource` is an event controller to initiate Drag-And-Drop operations.
48
 *
Matthias Clasen's avatar
Matthias Clasen committed
49
 * `GtkDragSource` can be set up with the necessary
Matthias Clasen's avatar
Matthias Clasen committed
50
51
 * ingredients for a DND operation ahead of time. This includes
 * the source for the data that is being transferred, in the form
Matthias Clasen's avatar
Matthias Clasen committed
52
 * of a [class@Gdk.ContentProvider], the desired action, and the icon to
Matthias Clasen's avatar
Matthias Clasen committed
53
54
 * use during the drag operation. After setting it up, the drag
 * source must be added to a widget as an event controller, using
Matthias Clasen's avatar
Matthias Clasen committed
55
 * [method@Gtk.Widget.add_controller].
Matthias Clasen's avatar
Matthias Clasen committed
56
 *
Matthias Clasen's avatar
Matthias Clasen committed
57
 * ```c
58
59
60
61
62
63
64
65
66
67
 * static void
 * my_widget_init (MyWidget *self)
 * {
 *   GtkDragSource *drag_source = gtk_drag_source_new ();
 *
 *   g_signal_connect (drag_source, "prepare", G_CALLBACK (on_drag_prepare), self);
 *   g_signal_connect (drag_source, "drag-begin", G_CALLBACK (on_drag_begin), self);
 *
 *   gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (drag_source));
 * }
Matthias Clasen's avatar
Matthias Clasen committed
68
 * ```
69
 *
Matthias Clasen's avatar
Matthias Clasen committed
70
71
72
73
74
 * Setting up the content provider and icon ahead of time only makes
 * sense when the data does not change. More commonly, you will want
 * to set them up just in time. To do so, `GtkDragSource` has
 * [signal@Gtk.DragSource::prepare] and [signal@Gtk.DragSource::drag-begin]
 * signals.
75
 *
Matthias Clasen's avatar
Matthias Clasen committed
76
77
 * The ::prepare signal is emitted before a drag is started, and
 * can be used to set the content provider and actions that the
78
79
 * drag should be started with.
 *
Matthias Clasen's avatar
Matthias Clasen committed
80
 * ```c
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 * static GdkContentProvider *
 * on_drag_prepare (GtkDragSource *source,
 *                  double         x,
 *                  double         y,
 *                  MyWidget      *self)
 * {
 *   // This widget supports two types of content: GFile objects
 *   // and GdkPixbuf objects; GTK will handle the serialization
 *   // of these types automatically
 *   GFile *file = my_widget_get_file (self);
 *   GdkPixbuf *pixbuf = my_widget_get_pixbuf (self);
 *
 *   return gdk_content_provider_new_union ((GdkContentProvider *[2]) {
 *       gdk_content_provider_new_typed (G_TYPE_FILE, file),
 *       gdk_content_provider_new_typed (GDK_TYPE_PIXBUF, pixbuf),
 *     }, 2);
 * }
Matthias Clasen's avatar
Matthias Clasen committed
98
 * ```
99
 *
Matthias Clasen's avatar
Matthias Clasen committed
100
 * The ::drag-begin signal is emitted after the `GdkDrag` object has
101
102
 * been created, and can be used to set up the drag icon.
 *
Matthias Clasen's avatar
Matthias Clasen committed
103
 * ```c
104
105
106
107
108
109
110
111
112
113
 * static void
 * on_drag_begin (GtkDragSource *source,
 *                GtkDrag       *drag,
 *                MyWidget      *self)
 * {
 *   // Set the widget as the drag icon
 *   GdkPaintable *paintable = gtk_widget_paintable_new (GTK_WIDGET (self));
 *   gtk_drag_source_set_icon (source, paintable, 0, 0);
 *   g_object_unref (paintable);
 * }
Matthias Clasen's avatar
Matthias Clasen committed
114
 * ```
Matthias Clasen's avatar
Matthias Clasen committed
115
 *
Matthias Clasen's avatar
Matthias Clasen committed
116
 * During the DND operation, `GtkDragSource` emits signals that
Matthias Clasen's avatar
Matthias Clasen committed
117
118
119
 * can be used to obtain updates about the status of the operation,
 * but it is not normally necessary to connect to any signals,
 * except for one case: when the supported actions include
120
 * %GDK_ACTION_MOVE, you need to listen for the
Matthias Clasen's avatar
Matthias Clasen committed
121
 * [signal@Gtk.DragSource::drag-end] signal and delete the
Matthias Clasen's avatar
Matthias Clasen committed
122
 * data after it has been transferred.
Matthias Clasen's avatar
Matthias Clasen committed
123
124
125
126
 */

struct _GtkDragSource
{
Matthias Clasen's avatar
Matthias Clasen committed
127
  GtkGestureSingle parent_instance;
Matthias Clasen's avatar
Matthias Clasen committed
128
129
130
131
132
133
134
135

  GdkContentProvider *content;
  GdkDragAction actions;

  GdkPaintable *paintable;
  int hot_x;
  int hot_y;

136
137
  double start_x;
  double start_y;
Matthias Clasen's avatar
Matthias Clasen committed
138
139
140
141
142
143

  GdkDrag *drag;
};

struct _GtkDragSourceClass
{
Matthias Clasen's avatar
Matthias Clasen committed
144
  GtkGestureSingleClass parent_class;
145

146
147
148
  GdkContentProvider *(* prepare) (GtkDragSource *source,
                                  double         x,
                                  double         y);
Matthias Clasen's avatar
Matthias Clasen committed
149
150
151
152
153
154
155
156
157
158
159
};

enum {
  PROP_CONTENT = 1,
  PROP_ACTIONS,
  NUM_PROPERTIES
};

static GParamSpec *properties[NUM_PROPERTIES];

enum {
160
  PREPARE,
Matthias Clasen's avatar
Matthias Clasen committed
161
162
  DRAG_BEGIN,
  DRAG_END,
163
  DRAG_CANCEL,
Matthias Clasen's avatar
Matthias Clasen committed
164
165
166
167
168
  NUM_SIGNALS
};

static guint signals[NUM_SIGNALS];

169
170
171
172
173
174
static void gtk_drag_source_dnd_finished_cb   (GdkDrag             *drag,
                                               GtkDragSource       *source);
static void gtk_drag_source_cancel_cb         (GdkDrag             *drag,
                                               GdkDragCancelReason  reason,
                                               GtkDragSource       *source);

175
176
177
static GdkContentProvider *gtk_drag_source_prepare (GtkDragSource *source,
                                                    double         x,
                                                    double         y);
178

179
180
static void gtk_drag_source_drag_begin (GtkDragSource *source);

Matthias Clasen's avatar
Matthias Clasen committed
181
G_DEFINE_TYPE (GtkDragSource, gtk_drag_source, GTK_TYPE_GESTURE_SINGLE);
Matthias Clasen's avatar
Matthias Clasen committed
182
183
184
185

static void
gtk_drag_source_init (GtkDragSource *source)
{
186
  source->actions = GDK_ACTION_COPY;
Matthias Clasen's avatar
Matthias Clasen committed
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
}

static void
gtk_drag_source_finalize (GObject *object)
{
  GtkDragSource *source = GTK_DRAG_SOURCE (object);

  g_clear_object (&source->content);
  g_clear_object (&source->paintable);

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

static void
gtk_drag_source_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  GtkDragSource *source = GTK_DRAG_SOURCE (object);
  
  switch (prop_id)
    {
    case PROP_CONTENT:
      gtk_drag_source_set_content (source, g_value_get_object (value));
      break;

    case PROP_ACTIONS:
      gtk_drag_source_set_actions (source, g_value_get_flags (value));
      break;

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

static void
gtk_drag_source_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  GtkDragSource *source = GTK_DRAG_SOURCE (object);

  switch (prop_id)
    {
    case PROP_CONTENT:
      g_value_set_object (value, gtk_drag_source_get_content (source));
      break;

    case PROP_ACTIONS:
      g_value_set_flags (value, gtk_drag_source_get_actions (source));
      break;

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

Matthias Clasen's avatar
Matthias Clasen committed
246
247
static gboolean
gtk_drag_source_filter_event (GtkEventController *controller,
Matthias Clasen's avatar
Matthias Clasen committed
248
                              GdkEvent           *event)
Matthias Clasen's avatar
Matthias Clasen committed
249
250
251
252
253
254
255
256
{
  /* Let touchpad swipe events go through, only if they match n-points  */
  if (gdk_event_get_event_type (event) == GDK_TOUCHPAD_SWIPE)
    {
      guint n_points;
      guint n_fingers;

      g_object_get (G_OBJECT (controller), "n-points", &n_points, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
257
      n_fingers = gdk_touchpad_event_get_n_fingers (event);
Matthias Clasen's avatar
Matthias Clasen committed
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

      if (n_fingers == n_points)
        return FALSE;
      else
        return TRUE;
    }

  return GTK_EVENT_CONTROLLER_CLASS (gtk_drag_source_parent_class)->filter_event (controller, event);
}

static void
gtk_drag_source_begin (GtkGesture       *gesture,
                       GdkEventSequence *sequence)
{
  GtkDragSource *source = GTK_DRAG_SOURCE (gesture);
  GdkEventSequence *current;

  current = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));

  gtk_gesture_get_point (gesture, current, &source->start_x, &source->start_y);
}

static void
gtk_drag_source_update (GtkGesture       *gesture,
                        GdkEventSequence *sequence)
{
  GtkDragSource *source = GTK_DRAG_SOURCE (gesture);
  GtkWidget *widget;
286
  double x, y;
Matthias Clasen's avatar
Matthias Clasen committed
287
288
289
290

  if (!gtk_gesture_is_recognized (gesture))
    return;

291
292
  gtk_gesture_get_point (gesture, sequence, &x, &y);

Matthias Clasen's avatar
Matthias Clasen committed
293
294
  widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));

295
  if (gtk_drag_check_threshold_double (widget, source->start_x, source->start_y, x, y))
Matthias Clasen's avatar
Matthias Clasen committed
296
    {
297
298
      gtk_drag_source_drag_begin (source);
    }
Matthias Clasen's avatar
Matthias Clasen committed
299
300
}

Matthias Clasen's avatar
Matthias Clasen committed
301
302
303
304
static void
gtk_drag_source_class_init (GtkDragSourceClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
Matthias Clasen's avatar
Matthias Clasen committed
305
306
  GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (class);
  GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (class);
Matthias Clasen's avatar
Matthias Clasen committed
307
308
309
310
311

  object_class->finalize = gtk_drag_source_finalize;
  object_class->set_property = gtk_drag_source_set_property;
  object_class->get_property = gtk_drag_source_get_property;

Matthias Clasen's avatar
Matthias Clasen committed
312
313
314
315
316
317
  controller_class->filter_event = gtk_drag_source_filter_event;

  gesture_class->begin = gtk_drag_source_begin;
  gesture_class->update = gtk_drag_source_update;
  gesture_class->end = NULL;

318
319
  class->prepare = gtk_drag_source_prepare;

Matthias Clasen's avatar
Matthias Clasen committed
320
  /**
Matthias Clasen's avatar
Matthias Clasen committed
321
   * GtkDragSource:content: (attributes org.gtk.Property.get=gtk_drag_source_get_content org.gtk.Propery.set=gtk_drag_source_set_content)
Matthias Clasen's avatar
Matthias Clasen committed
322
   *
Matthias Clasen's avatar
Matthias Clasen committed
323
324
   * The data that is offered by drag operations from this source.
   */
Matthias Clasen's avatar
Matthias Clasen committed
325
326
327
328
329
330
331
332
  properties[PROP_CONTENT] =
       g_param_spec_object ("content",
                            P_("Content"),
                            P_("The content provider for the dragged data"),
                           GDK_TYPE_CONTENT_PROVIDER,
                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
Matthias Clasen's avatar
Matthias Clasen committed
333
   * GtkDragSource:actions: (attributes org.gtk.Property.get=gtk_drag_source_get_actions org.gtk.Property.set=gtk_drag_source_set_actions)
Matthias Clasen's avatar
Matthias Clasen committed
334
335
336
   *
   * The actions that are supported by drag operations from the source.
   *
Matthias Clasen's avatar
Matthias Clasen committed
337
   * Note that you must handle the [signal@Gtk.DragSource::drag-end] signal
Matthias Clasen's avatar
Matthias Clasen committed
338
   * if the actions include %GDK_ACTION_MOVE.
Matthias Clasen's avatar
Matthias Clasen committed
339
   */
Matthias Clasen's avatar
Matthias Clasen committed
340
341
342
343
  properties[PROP_ACTIONS] =
       g_param_spec_flags ("actions",
                           P_("Actions"),
                           P_("Supported actions"),
344
                           GDK_TYPE_DRAG_ACTION, GDK_ACTION_COPY,
Matthias Clasen's avatar
Matthias Clasen committed
345
346
347
348
                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);

Matthias Clasen's avatar
Matthias Clasen committed
349
350
  /**
   * GtkDragSource::prepare:
Matthias Clasen's avatar
Matthias Clasen committed
351
   * @source: the `GtkDragSource`
Matthias Clasen's avatar
Matthias Clasen committed
352
353
354
   * @x: the X coordinate of the drag starting point
   * @y: the Y coordinate fo the drag starting point
   *
Matthias Clasen's avatar
Matthias Clasen committed
355
356
357
   * Emitted when a drag is about to be initiated.
   *
   * It returns the `GdkContentProvider` to use for the drag that is about
358
   * to start. The default handler for this signal returns the value of
Matthias Clasen's avatar
Matthias Clasen committed
359
360
   * the [property@Gtk.DragSource:content] property, so if you set up that
   * property ahead of time, you don't need to connect to this signal.
Matthias Clasen's avatar
Matthias Clasen committed
361
   *
Matthias Clasen's avatar
Matthias Clasen committed
362
   * Returns: (transfer full) (nullable): a `GdkContentProvider`
Matthias Clasen's avatar
Matthias Clasen committed
363
   */
364
365
366
367
  signals[PREPARE] =
      g_signal_new (I_("prepare"),
                    G_TYPE_FROM_CLASS (class),
                    G_SIGNAL_RUN_LAST,
368
369
                    G_STRUCT_OFFSET (GtkDragSourceClass, prepare),
                    g_signal_accumulator_first_wins, NULL,
370
                    NULL,
371
                    GDK_TYPE_CONTENT_PROVIDER, 2,
372
373
                    G_TYPE_DOUBLE, G_TYPE_DOUBLE);

Matthias Clasen's avatar
Matthias Clasen committed
374
375
  /**
   * GtkDragSource::drag-begin:
Matthias Clasen's avatar
Matthias Clasen committed
376
377
378
379
   * @source: the `GtkDragSource`
   * @drag: the `GdkDrag` object
   *
   * Emitted on the drag source when a drag is started.
Matthias Clasen's avatar
Matthias Clasen committed
380
   *
Matthias Clasen's avatar
Matthias Clasen committed
381
382
   * It can be used to e.g. set a custom drag icon with
   * [method@Gtk.DragSource.set_icon].
Matthias Clasen's avatar
Matthias Clasen committed
383
384
385
386
387
388
389
390
   */
  signals[DRAG_BEGIN] =
      g_signal_new (I_("drag-begin"),
                    G_TYPE_FROM_CLASS (class),
                    G_SIGNAL_RUN_LAST,
                    0,
                    NULL, NULL,
                    NULL,
391
392
                    G_TYPE_NONE, 1,
                    GDK_TYPE_DRAG);
Matthias Clasen's avatar
Matthias Clasen committed
393
394
395

  /**
   * GtkDragSource::drag-end:
Matthias Clasen's avatar
Matthias Clasen committed
396
397
   * @source: the `GtkDragSource`
   * @drag: the `GdkDrag` object
398
399
   * @delete_data: %TRUE if the drag was performing %GDK_ACTION_MOVE,
   *    and the data should be deleted
Matthias Clasen's avatar
Matthias Clasen committed
400
   *
Matthias Clasen's avatar
Matthias Clasen committed
401
402
403
404
405
406
   * Emitted on the drag source when a drag is finished.
   *
   * A typical reason to connect to this signal is to undo
   * things done in [signal@Gtk.DragSource::prepare] or
   * [signal@Gtk.DragSource::drag-begin] handlers.
   */
Matthias Clasen's avatar
Matthias Clasen committed
407
408
409
410
411
412
413
  signals[DRAG_END] =
      g_signal_new (I_("drag-end"),
                    G_TYPE_FROM_CLASS (class),
                    G_SIGNAL_RUN_LAST,
                    0,
                    NULL, NULL,
                    NULL,
414
415
416
                    G_TYPE_NONE, 2,
                    GDK_TYPE_DRAG,
                    G_TYPE_BOOLEAN);
Matthias Clasen's avatar
Matthias Clasen committed
417
418

  /**
419
   * GtkDragSource::drag-cancel:
Matthias Clasen's avatar
Matthias Clasen committed
420
421
   * @source: the `GtkDragSource`
   * @drag: the `GdkDrag` object
Matthias Clasen's avatar
Matthias Clasen committed
422
423
   * @reason: information on why the drag failed
   *
Matthias Clasen's avatar
Matthias Clasen committed
424
425
426
   * Emitted on the drag source when a drag has failed.
   *
   * The signal handler may handle a failed drag operation based on
Matthias Clasen's avatar
Matthias Clasen committed
427
   * the type of error. It should return %TRUE if the failure has been handled
428
   * and the default "drag operation failed" animation should not be shown.
Matthias Clasen's avatar
Matthias Clasen committed
429
430
431
   *
   * Returns: %TRUE if the failed drag operation has been already handled
   */
432
433
  signals[DRAG_CANCEL] =
      g_signal_new (I_("drag-cancel"),
Matthias Clasen's avatar
Matthias Clasen committed
434
435
436
437
                    G_TYPE_FROM_CLASS (class),
                    G_SIGNAL_RUN_LAST,
                    0,
                    _gtk_boolean_handled_accumulator, NULL,
438
439
440
                    _gtk_marshal_BOOLEAN__OBJECT_ENUM,
                    G_TYPE_BOOLEAN, 2,
                    GDK_TYPE_DRAG,
Matthias Clasen's avatar
Matthias Clasen committed
441
442
443
                    GDK_TYPE_DRAG_CANCEL_REASON);
}

444
static GdkContentProvider *
445
446
447
448
gtk_drag_source_prepare (GtkDragSource *source,
                         double         x,
                         double         y)
{
449
450
451
452
453
454
455
  if (source->actions == 0)
    return NULL;

  if (source->content == NULL)
    return NULL;

  return g_object_ref (source->content);
456
457
}

Matthias Clasen's avatar
Matthias Clasen committed
458
static void
Matthias Clasen's avatar
Matthias Clasen committed
459
460
drag_end (GtkDragSource *source,
          gboolean       success)
Matthias Clasen's avatar
Matthias Clasen committed
461
{
462
463
  gboolean delete_data;

Matthias Clasen's avatar
Matthias Clasen committed
464
465
466
  g_signal_handlers_disconnect_by_func (source->drag, gtk_drag_source_dnd_finished_cb, source);
  g_signal_handlers_disconnect_by_func (source->drag, gtk_drag_source_cancel_cb, source);

467
468
469
  delete_data = success && gdk_drag_get_selected_action (source->drag) == GDK_ACTION_MOVE;

  g_signal_emit (source, signals[DRAG_END], 0, source->drag, delete_data);
Matthias Clasen's avatar
Matthias Clasen committed
470

Matthias Clasen's avatar
Matthias Clasen committed
471
  gdk_drag_drop_done (source->drag, success);
Matthias Clasen's avatar
Matthias Clasen committed
472
  g_clear_object (&source->drag);
473
  g_object_unref (source);
Matthias Clasen's avatar
Matthias Clasen committed
474
475
476
477
478
479
}

static void
gtk_drag_source_dnd_finished_cb (GdkDrag       *drag,
                                 GtkDragSource *source)
{
Matthias Clasen's avatar
Matthias Clasen committed
480
  drag_end (source, TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
481
482
483
484
485
486
487
488
489
}

static void
gtk_drag_source_cancel_cb (GdkDrag             *drag,
                           GdkDragCancelReason  reason,
                           GtkDragSource       *source)
{
  gboolean success = FALSE;

490
  g_signal_emit (source, signals[DRAG_CANCEL], 0, source->drag, reason, &success);
491
  drag_end (source, success);
Matthias Clasen's avatar
Matthias Clasen committed
492
493
}

494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
static void
gtk_drag_source_ensure_icon (GtkDragSource *self,
                             GdkDrag       *drag)
{
  GdkContentProvider *provider;
  GtkWidget *icon, *child;
  GdkContentFormats *formats;
  const GType *types;
  gsize i, n_types;

  icon = gtk_drag_icon_get_for_drag (drag);
  /* If an icon has been set already, we don't need to set one. */
  if (gtk_drag_icon_get_child (GTK_DRAG_ICON (icon)))
    return;

509
510
511
512
513
514
515
516
517
  if (self->paintable)
    {
      gtk_drag_icon_set_from_paintable (drag,
                                        self->paintable,
                                        self->hot_x,
                                        self->hot_y);
      return;
    }

518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
  gdk_drag_set_hotspot (drag, -2, -2);

  provider = gdk_drag_get_content (drag);
  formats = gdk_content_provider_ref_formats (provider);
  types = gdk_content_formats_get_gtypes (formats, &n_types);
  for (i = 0; i < n_types; i++)
    {
      GValue value = G_VALUE_INIT;

      g_value_init (&value, types[i]);
      if (gdk_content_provider_get_value (provider, &value, NULL))
        {
          child = gtk_drag_icon_create_widget_for_value (&value);

          if (child)
            {
              gtk_drag_icon_set_child (GTK_DRAG_ICON (icon), child);
              g_value_unset (&value);
              gdk_content_formats_unref (formats);
              return;
            }
        }
      g_value_unset (&value);
    }

  gdk_content_formats_unref (formats);
  child = gtk_image_new_from_icon_name ("text-x-generic");
  gtk_image_set_icon_size (GTK_IMAGE (child), GTK_ICON_SIZE_LARGE);
  gtk_drag_icon_set_child (GTK_DRAG_ICON (icon), child);
}

549
550
static void
gtk_drag_source_drag_begin (GtkDragSource *source)
Matthias Clasen's avatar
Matthias Clasen committed
551
{
552
  GtkWidget *widget;
553
554
  GdkDevice *device, *pointer;
  GdkSeat *seat;
555
  double x, y;
Matthias Clasen's avatar
Matthias Clasen committed
556
557
558
559
  GtkNative *native;
  GdkSurface *surface;
  double px, py;
  int dx, dy;
560
  GdkContentProvider *content = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
561

562
563
  widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (source));
  device = gtk_gesture_get_device (GTK_GESTURE (source));
564
  seat = gdk_device_get_seat (device);
Matthias Clasen's avatar
Matthias Clasen committed
565

566
567
568
569
  if (device == gdk_seat_get_keyboard (seat))
    pointer = gdk_seat_get_pointer (seat);
  else
    pointer = device;
Matthias Clasen's avatar
Matthias Clasen committed
570
571
572
573

  native = gtk_widget_get_native (widget);
  surface = gtk_native_get_surface (native);

574
  gtk_widget_translate_coordinates (widget, GTK_WIDGET (native), source->start_x, source->start_y, &x, &y);
575
  gdk_surface_get_device_position (surface, pointer, &px, &py, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
576

577
578
  dx = round (px - x);
  dy = round (py - y);
Matthias Clasen's avatar
Matthias Clasen committed
579

580
581
  g_signal_emit (source, signals[PREPARE], 0, source->start_x, source->start_y, &content);
  if (!content)
582
583
    return;

584
  source->drag = gdk_drag_begin (surface, pointer, content, source->actions, dx, dy);
585
586

  g_object_unref (content);
587

Matthias Clasen's avatar
Matthias Clasen committed
588
  if (source->drag == NULL)
589
    return;
Matthias Clasen's avatar
Matthias Clasen committed
590
591
592

  gtk_widget_reset_controllers (widget);

593
  g_signal_emit (source, signals[DRAG_BEGIN], 0, source->drag);
Matthias Clasen's avatar
Matthias Clasen committed
594

595
  gtk_drag_source_ensure_icon (source, source->drag);
Matthias Clasen's avatar
Matthias Clasen committed
596

597
598
599
  /* Keep the source alive until the drag is done */
  g_object_ref (source);

Matthias Clasen's avatar
Matthias Clasen committed
600
601
602
603
604
605
606
607
608
  g_signal_connect (source->drag, "dnd-finished",
                    G_CALLBACK (gtk_drag_source_dnd_finished_cb), source);
  g_signal_connect (source->drag, "cancel",
                    G_CALLBACK (gtk_drag_source_cancel_cb), source);
}

/**
 * gtk_drag_source_new:
 *
Matthias Clasen's avatar
Matthias Clasen committed
609
 * Creates a new `GtkDragSource` object.
Matthias Clasen's avatar
Matthias Clasen committed
610
 *
Matthias Clasen's avatar
Matthias Clasen committed
611
 * Returns: the new `GtkDragSource`
Matthias Clasen's avatar
Matthias Clasen committed
612
613
 */
GtkDragSource *
614
gtk_drag_source_new (void)
Matthias Clasen's avatar
Matthias Clasen committed
615
{
616
  return g_object_new (GTK_TYPE_DRAG_SOURCE, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
617
618
619
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
620
621
 * gtk_drag_source_get_content: (attributes org.gtk.Method.get_property=content)
 * @source: a `GtkDragSource`
Matthias Clasen's avatar
Matthias Clasen committed
622
 *
Matthias Clasen's avatar
Matthias Clasen committed
623
 * Gets the current content provider of a `GtkDragSource`.
Matthias Clasen's avatar
Matthias Clasen committed
624
 *
Matthias Clasen's avatar
Matthias Clasen committed
625
 * Returns: (nullable) (transfer none): the `GdkContentProvider` of @source
Matthias Clasen's avatar
Matthias Clasen committed
626
627
628
629
630
631
 */
GdkContentProvider *
gtk_drag_source_get_content (GtkDragSource *source)
{
  g_return_val_if_fail (GTK_IS_DRAG_SOURCE (source), NULL);

632
  return source->content;
Matthias Clasen's avatar
Matthias Clasen committed
633
634
635
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
636
637
 * gtk_drag_source_set_content: (attributes org.gtk.Method.set_property=content)
 * @source: a `GtkDragSource`
Matthias Clasen's avatar
Matthias Clasen committed
638
 * @content: (nullable): a `GdkContentProvider`
Matthias Clasen's avatar
Matthias Clasen committed
639
 *
Matthias Clasen's avatar
Matthias Clasen committed
640
 * Sets a content provider on a `GtkDragSource`.
Matthias Clasen's avatar
Matthias Clasen committed
641
 *
Matthias Clasen's avatar
Matthias Clasen committed
642
643
 * When the data is requested in the cause of a DND operation,
 * it will be obtained from the content provider.
Matthias Clasen's avatar
Matthias Clasen committed
644
645
 *
 * This function can be called before a drag is started,
Matthias Clasen's avatar
Matthias Clasen committed
646
 * or in a handler for the [signal@Gtk.DragSource::prepare] signal.
Matthias Clasen's avatar
Matthias Clasen committed
647
648
 *
 * You may consider setting the content provider back to
Matthias Clasen's avatar
Matthias Clasen committed
649
 * %NULL in a [signal@Gtk.DragSource::drag-end] signal handler.
Matthias Clasen's avatar
Matthias Clasen committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
 */
void
gtk_drag_source_set_content (GtkDragSource      *source,
                             GdkContentProvider *content)
{
  g_return_if_fail (GTK_IS_DRAG_SOURCE (source));

  if (!g_set_object (&source->content, content))
    return;

  g_object_notify_by_pspec (G_OBJECT (source), properties[PROP_CONTENT]);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
664
665
 * gtk_drag_source_get_actions: (attributes org.gtk.Method.get_property=actions)
 * @source: a `GtkDragSource`
Matthias Clasen's avatar
Matthias Clasen committed
666
 *
Matthias Clasen's avatar
Matthias Clasen committed
667
 * Gets the actions that are currently set on the `GtkDragSource`.
Matthias Clasen's avatar
Matthias Clasen committed
668
669
670
671
672
673
674
675
676
677
678
679
 *
 * Returns: the actions set on @source
 */
GdkDragAction
gtk_drag_source_get_actions (GtkDragSource *source)
{
  g_return_val_if_fail (GTK_IS_DRAG_SOURCE (source), 0);

  return source->actions;
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
680
681
 * gtk_drag_source_set_actions: (attributes org.gtk.Method.set_property=actions)
 * @source: a `GtkDragSource`
Matthias Clasen's avatar
Matthias Clasen committed
682
683
 * @actions: the actions to offer
 *
Matthias Clasen's avatar
Matthias Clasen committed
684
 * Sets the actions on the `GtkDragSource`.
Matthias Clasen's avatar
Matthias Clasen committed
685
 *
Matthias Clasen's avatar
Matthias Clasen committed
686
687
688
689
 * During a DND operation, the actions are offered to potential
 * drop targets. If @actions include %GDK_ACTION_MOVE, you need
 * to listen to the [signal@Gtk.DragSource::drag-end] signal and
 * handle @delete_data being %TRUE.
Matthias Clasen's avatar
Matthias Clasen committed
690
691
 *
 * This function can be called before a drag is started,
Matthias Clasen's avatar
Matthias Clasen committed
692
 * or in a handler for the [signal@Gtk.DragSource::prepare] signal.
Matthias Clasen's avatar
Matthias Clasen committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
 */
void
gtk_drag_source_set_actions (GtkDragSource *source,
                             GdkDragAction  actions)
{
  g_return_if_fail (GTK_IS_DRAG_SOURCE (source));

  if (source->actions == actions)
    return;

  source->actions = actions;

  g_object_notify_by_pspec (G_OBJECT (source), properties[PROP_ACTIONS]);
}

/**
 * gtk_drag_source_set_icon:
Matthias Clasen's avatar
Matthias Clasen committed
710
 * @source: a `GtkDragSource`
Matthias Clasen's avatar
Matthias Clasen committed
711
 * @paintable: (nullable): the `GdkPaintable` to use as icon
Matthias Clasen's avatar
Matthias Clasen committed
712
713
714
715
716
717
718
719
720
 * @hot_x: the hotspot X coordinate on the icon
 * @hot_y: the hotspot Y coordinate on the icon
 *
 * Sets a paintable to use as icon during DND operations.
 *
 * The hotspot coordinates determine the point on the icon
 * that gets aligned with the hotspot of the cursor.
 *
 * If @paintable is %NULL, a default icon is used.
Matthias Clasen's avatar
Matthias Clasen committed
721
722
 *
 * This function can be called before a drag is started, or in
Matthias Clasen's avatar
Matthias Clasen committed
723
724
 * a [signal@Gtk.DragSource::prepare] or
 * [signal@Gtk.DragSource::drag-begin] signal handler.
Matthias Clasen's avatar
Matthias Clasen committed
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
 */
void
gtk_drag_source_set_icon (GtkDragSource *source,
                          GdkPaintable  *paintable,
                          int            hot_x,
                          int            hot_y)
{
  g_return_if_fail (GTK_IS_DRAG_SOURCE (source));

  g_set_object (&source->paintable, paintable);

  source->hot_x = hot_x;
  source->hot_y = hot_y;
}

/**
 * gtk_drag_source_get_drag:
Matthias Clasen's avatar
Matthias Clasen committed
742
 * @source: a `GtkDragSource`
Matthias Clasen's avatar
Matthias Clasen committed
743
 *
Matthias Clasen's avatar
Matthias Clasen committed
744
 * Returns the underlying `GdkDrag` object for an ongoing drag.
Matthias Clasen's avatar
Matthias Clasen committed
745
 *
Matthias Clasen's avatar
Matthias Clasen committed
746
 * Returns: (nullable) (transfer none): the `GdkDrag` of the current
Matthias Clasen's avatar
Matthias Clasen committed
747
 *   drag operation
Matthias Clasen's avatar
Matthias Clasen committed
748
749
750
751
752
753
754
755
756
757
758
 */
GdkDrag *
gtk_drag_source_get_drag (GtkDragSource *source)
{
  g_return_val_if_fail (GTK_IS_DRAG_SOURCE (source), NULL);

  return source->drag;
}

/**
 * gtk_drag_source_drag_cancel:
Matthias Clasen's avatar
Matthias Clasen committed
759
 * @source: a `GtkDragSource`
Matthias Clasen's avatar
Matthias Clasen committed
760
761
762
763
764
765
766
767
768
769
770
771
 *
 * Cancels a currently ongoing drag operation.
 */
void
gtk_drag_source_drag_cancel (GtkDragSource *source)
{
  g_return_if_fail (GTK_IS_DRAG_SOURCE (source));

  if (source->drag)
    {
      gboolean success = FALSE;

772
      g_signal_emit (source, signals[DRAG_CANCEL], 0, source->drag, GDK_DRAG_CANCEL_ERROR, &success);
773
      drag_end (source, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
774
775
    }
}
776
777
778

/**
 * gtk_drag_check_threshold: (method)
Matthias Clasen's avatar
Matthias Clasen committed
779
 * @widget: a `GtkWidget`
780
781
782
783
 * @start_x: X coordinate of start of drag
 * @start_y: Y coordinate of start of drag
 * @current_x: current X coordinate
 * @current_y: current Y coordinate
Matthias Clasen's avatar
Matthias Clasen committed
784
785
 *
 * Checks to see if a drag movement has passed the GTK drag threshold.
786
787
788
789
790
791
792
793
794
795
 *
 * Returns: %TRUE if the drag threshold has been passed.
 */
gboolean
gtk_drag_check_threshold (GtkWidget *widget,
                          int        start_x,
                          int        start_y,
                          int        current_x,
                          int        current_y)
{
Benjamin Otte's avatar
Benjamin Otte committed
796
  int drag_threshold;
797
798
799
800
801
802
803
804

  g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);

  drag_threshold = gtk_settings_get_dnd_drag_threshold (gtk_widget_get_settings (widget));

  return (ABS (current_x - start_x) > drag_threshold ||
          ABS (current_y - start_y) > drag_threshold);
}
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821

gboolean
gtk_drag_check_threshold_double (GtkWidget *widget,
                                 double     start_x,
                                 double     start_y,
                                 double     current_x,
                                 double     current_y)
{
  int drag_threshold;

  g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);

  drag_threshold = gtk_settings_get_dnd_drag_threshold (gtk_widget_get_settings (widget));

  return (ABS (current_x - start_x) > drag_threshold ||
          ABS (current_y - start_y) > drag_threshold);
}