1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24 
25 #include "config.h"
26 
27 #include "gdkdndprivate.h"
28 #include "gdkdisplay.h"
29 #include "gdkwindow.h"
30 #include "gdkintl.h"
31 #include "gdkenumtypes.h"
32 #include "gdkcursor.h"
33 
34 static struct {
35   GdkDragAction action;
36   const gchar  *name;
37   GdkCursor    *cursor;
38 } drag_cursors[] = {
39   { GDK_ACTION_DEFAULT, NULL,       NULL },
40   { GDK_ACTION_ASK,     "dnd-ask",  NULL },
41   { GDK_ACTION_COPY,    "dnd-copy", NULL },
42   { GDK_ACTION_MOVE,    "dnd-move", NULL },
43   { GDK_ACTION_LINK,    "dnd-link", NULL },
44   { 0,                  "dnd-none", NULL },
45 };
46 
47 enum {
48   CANCEL,
49   DROP_PERFORMED,
50   DND_FINISHED,
51   ACTION_CHANGED,
52   N_SIGNALS
53 };
54 
55 static guint signals[N_SIGNALS] = { 0 };
56 static GList *contexts = NULL;
57 
58 /**
59  * SECTION:dnd
60  * @title: Drag And Drop
61  * @short_description: Functions for controlling drag and drop handling
62  *
63  * These functions provide a low level interface for drag and drop.
64  * The X backend of GDK supports both the Xdnd and Motif drag and drop
65  * protocols transparently, the Win32 backend supports the WM_DROPFILES
66  * protocol.
67  *
68  * GTK+ provides a higher level abstraction based on top of these functions,
69  * and so they are not normally needed in GTK+ applications.
70  * See the [Drag and Drop][gtk3-Drag-and-Drop] section of
71  * the GTK+ documentation for more information.
72  */
73 
74 /**
75  * gdk_drag_context_list_targets:
76  * @context: a #GdkDragContext
77  *
78  * Retrieves the list of targets of the context.
79  *
80  * Returns: (transfer none) (element-type GdkAtom): a #GList of targets
81  *
82  * Since: 2.22
83  **/
84 GList *
gdk_drag_context_list_targets(GdkDragContext * context)85 gdk_drag_context_list_targets (GdkDragContext *context)
86 {
87   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
88 
89   return context->targets;
90 }
91 
92 /**
93  * gdk_drag_context_get_actions:
94  * @context: a #GdkDragContext
95  *
96  * Determines the bitmask of actions proposed by the source if
97  * gdk_drag_context_get_suggested_action() returns %GDK_ACTION_ASK.
98  *
99  * Returns: the #GdkDragAction flags
100  *
101  * Since: 2.22
102  **/
103 GdkDragAction
gdk_drag_context_get_actions(GdkDragContext * context)104 gdk_drag_context_get_actions (GdkDragContext *context)
105 {
106   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_ACTION_DEFAULT);
107 
108   return context->actions;
109 }
110 
111 /**
112  * gdk_drag_context_get_suggested_action:
113  * @context: a #GdkDragContext
114  *
115  * Determines the suggested drag action of the context.
116  *
117  * Returns: a #GdkDragAction value
118  *
119  * Since: 2.22
120  **/
121 GdkDragAction
gdk_drag_context_get_suggested_action(GdkDragContext * context)122 gdk_drag_context_get_suggested_action (GdkDragContext *context)
123 {
124   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), 0);
125 
126   return context->suggested_action;
127 }
128 
129 /**
130  * gdk_drag_context_get_selected_action:
131  * @context: a #GdkDragContext
132  *
133  * Determines the action chosen by the drag destination.
134  *
135  * Returns: a #GdkDragAction value
136  *
137  * Since: 2.22
138  **/
139 GdkDragAction
gdk_drag_context_get_selected_action(GdkDragContext * context)140 gdk_drag_context_get_selected_action (GdkDragContext *context)
141 {
142   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), 0);
143 
144   return context->action;
145 }
146 
147 /**
148  * gdk_drag_context_get_source_window:
149  * @context: a #GdkDragContext
150  *
151  * Returns the #GdkWindow where the DND operation started.
152  *
153  * Returns: (transfer none): a #GdkWindow
154  *
155  * Since: 2.22
156  **/
157 GdkWindow *
gdk_drag_context_get_source_window(GdkDragContext * context)158 gdk_drag_context_get_source_window (GdkDragContext *context)
159 {
160   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
161 
162   return context->source_window;
163 }
164 
165 /**
166  * gdk_drag_context_get_dest_window:
167  * @context: a #GdkDragContext
168  *
169  * Returns the destination window for the DND operation.
170  *
171  * Returns: (transfer none): a #GdkWindow
172  *
173  * Since: 3.0
174  **/
175 GdkWindow *
gdk_drag_context_get_dest_window(GdkDragContext * context)176 gdk_drag_context_get_dest_window (GdkDragContext *context)
177 {
178   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
179 
180   return context->dest_window;
181 }
182 
183 /**
184  * gdk_drag_context_get_protocol:
185  * @context: a #GdkDragContext
186  *
187  * Returns the drag protocol that is used by this context.
188  *
189  * Returns: the drag protocol
190  *
191  * Since: 3.0
192  */
193 GdkDragProtocol
gdk_drag_context_get_protocol(GdkDragContext * context)194 gdk_drag_context_get_protocol (GdkDragContext *context)
195 {
196   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_DRAG_PROTO_NONE);
197 
198   return context->protocol;
199 }
200 
201 /**
202  * gdk_drag_context_set_device:
203  * @context: a #GdkDragContext
204  * @device: a #GdkDevice
205  *
206  * Associates a #GdkDevice to @context, so all Drag and Drop events
207  * for @context are emitted as if they came from this device.
208  */
209 void
gdk_drag_context_set_device(GdkDragContext * context,GdkDevice * device)210 gdk_drag_context_set_device (GdkDragContext *context,
211                              GdkDevice      *device)
212 {
213   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
214   g_return_if_fail (GDK_IS_DEVICE (device));
215 
216   if (context->device)
217     g_object_unref (context->device);
218 
219   context->device = device;
220 
221   if (context->device)
222     g_object_ref (context->device);
223 }
224 
225 /**
226  * gdk_drag_context_get_device:
227  * @context: a #GdkDragContext
228  *
229  * Returns the #GdkDevice associated to the drag context.
230  *
231  * Returns: (transfer none): The #GdkDevice associated to @context.
232  **/
233 GdkDevice *
gdk_drag_context_get_device(GdkDragContext * context)234 gdk_drag_context_get_device (GdkDragContext *context)
235 {
236   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
237 
238   return context->device;
239 }
240 
G_DEFINE_TYPE(GdkDragContext,gdk_drag_context,G_TYPE_OBJECT)241 G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
242 
243 static void
244 gdk_drag_context_init (GdkDragContext *context)
245 {
246   contexts = g_list_prepend (contexts, context);
247 }
248 
249 static void
gdk_drag_context_finalize(GObject * object)250 gdk_drag_context_finalize (GObject *object)
251 {
252   GdkDragContext *context = GDK_DRAG_CONTEXT (object);
253 
254   contexts = g_list_remove (contexts, context);
255   g_list_free (context->targets);
256 
257   if (context->source_window)
258     g_object_unref (context->source_window);
259 
260   if (context->dest_window)
261     g_object_unref (context->dest_window);
262 
263   G_OBJECT_CLASS (gdk_drag_context_parent_class)->finalize (object);
264 }
265 
266 static void
gdk_drag_context_class_init(GdkDragContextClass * klass)267 gdk_drag_context_class_init (GdkDragContextClass *klass)
268 {
269   GObjectClass *object_class = G_OBJECT_CLASS (klass);
270 
271   object_class->finalize = gdk_drag_context_finalize;
272 
273   /**
274    * GdkDragContext::cancel:
275    * @context: The object on which the signal is emitted
276    * @reason: The reason the context was cancelled
277    *
278    * The drag and drop operation was cancelled.
279    *
280    * This signal will only be emitted if the #GdkDragContext manages
281    * the drag and drop operation. See gdk_drag_context_manage_dnd()
282    * for more information.
283    *
284    * Since: 3.20
285    */
286   signals[CANCEL] =
287     g_signal_new ("cancel",
288                   G_TYPE_FROM_CLASS (object_class),
289                   G_SIGNAL_RUN_LAST,
290                   G_STRUCT_OFFSET (GdkDragContextClass, cancel),
291                   NULL, NULL,
292                   NULL,
293                   G_TYPE_NONE, 1, GDK_TYPE_DRAG_CANCEL_REASON);
294 
295   /**
296    * GdkDragContext::drop-performed:
297    * @context: The object on which the signal is emitted
298    * @time: the time at which the drop happened.
299    *
300    * The drag and drop operation was performed on an accepting client.
301    *
302    * This signal will only be emitted if the #GdkDragContext manages
303    * the drag and drop operation. See gdk_drag_context_manage_dnd()
304    * for more information.
305    *
306    * Since: 3.20
307    */
308   signals[DROP_PERFORMED] =
309     g_signal_new ("drop-performed",
310                   G_TYPE_FROM_CLASS (object_class),
311                   G_SIGNAL_RUN_LAST,
312                   G_STRUCT_OFFSET (GdkDragContextClass, drop_performed),
313                   NULL, NULL,
314                   NULL,
315                   G_TYPE_NONE, 1, G_TYPE_INT);
316 
317   /**
318    * GdkDragContext::dnd-finished:
319    * @context: The object on which the signal is emitted
320    *
321    * The drag and drop operation was finished, the drag destination
322    * finished reading all data. The drag source can now free all
323    * miscellaneous data.
324    *
325    * This signal will only be emitted if the #GdkDragContext manages
326    * the drag and drop operation. See gdk_drag_context_manage_dnd()
327    * for more information.
328    *
329    * Since: 3.20
330    */
331   signals[DND_FINISHED] =
332     g_signal_new ("dnd-finished",
333                   G_TYPE_FROM_CLASS (object_class),
334                   G_SIGNAL_RUN_LAST,
335                   G_STRUCT_OFFSET (GdkDragContextClass, dnd_finished),
336                   NULL, NULL,
337                   NULL,
338                   G_TYPE_NONE, 0);
339 
340   /**
341    * GdkDragContext::action-changed:
342    * @context: The object on which the signal is emitted
343    * @action: The action currently chosen
344    *
345    * A new action is being chosen for the drag and drop operation.
346    *
347    * This signal will only be emitted if the #GdkDragContext manages
348    * the drag and drop operation. See gdk_drag_context_manage_dnd()
349    * for more information.
350    *
351    * Since: 3.20
352    */
353   signals[ACTION_CHANGED] =
354     g_signal_new ("action-changed",
355                   G_TYPE_FROM_CLASS (object_class),
356                   G_SIGNAL_RUN_LAST,
357                   G_STRUCT_OFFSET (GdkDragContextClass, action_changed),
358                   NULL, NULL,
359                   NULL,
360                   G_TYPE_NONE, 1, GDK_TYPE_DRAG_ACTION);
361 }
362 
363 /**
364  * gdk_drag_find_window_for_screen:
365  * @context: a #GdkDragContext
366  * @drag_window: a window which may be at the pointer position, but
367  *     should be ignored, since it is put up by the drag source as an icon
368  * @screen: the screen where the destination window is sought
369  * @x_root: the x position of the pointer in root coordinates
370  * @y_root: the y position of the pointer in root coordinates
371  * @dest_window: (out): location to store the destination window in
372  * @protocol: (out): location to store the DND protocol in
373  *
374  * Finds the destination window and DND protocol to use at the
375  * given pointer position.
376  *
377  * This function is called by the drag source to obtain the
378  * @dest_window and @protocol parameters for gdk_drag_motion().
379  *
380  * Since: 2.2
381  */
382 void
gdk_drag_find_window_for_screen(GdkDragContext * context,GdkWindow * drag_window,GdkScreen * screen,gint x_root,gint y_root,GdkWindow ** dest_window,GdkDragProtocol * protocol)383 gdk_drag_find_window_for_screen (GdkDragContext  *context,
384                                  GdkWindow       *drag_window,
385                                  GdkScreen       *screen,
386                                  gint             x_root,
387                                  gint             y_root,
388                                  GdkWindow      **dest_window,
389                                  GdkDragProtocol *protocol)
390 {
391   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
392 
393   *dest_window = GDK_DRAG_CONTEXT_GET_CLASS (context)
394       ->find_window (context, drag_window, screen, x_root, y_root, protocol);
395 }
396 
397 /**
398  * gdk_drag_status:
399  * @context: a #GdkDragContext
400  * @action: the selected action which will be taken when a drop happens,
401  *    or 0 to indicate that a drop will not be accepted
402  * @time_: the timestamp for this operation
403  *
404  * Selects one of the actions offered by the drag source.
405  *
406  * This function is called by the drag destination in response to
407  * gdk_drag_motion() called by the drag source.
408  */
409 void
gdk_drag_status(GdkDragContext * context,GdkDragAction action,guint32 time_)410 gdk_drag_status (GdkDragContext *context,
411                  GdkDragAction   action,
412                  guint32         time_)
413 {
414   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
415 
416   GDK_DRAG_CONTEXT_GET_CLASS (context)->drag_status (context, action, time_);
417 }
418 
419 /**
420  * gdk_drag_motion:
421  * @context: a #GdkDragContext
422  * @dest_window: the new destination window, obtained by
423  *     gdk_drag_find_window()
424  * @protocol: the DND protocol in use, obtained by gdk_drag_find_window()
425  * @x_root: the x position of the pointer in root coordinates
426  * @y_root: the y position of the pointer in root coordinates
427  * @suggested_action: the suggested action
428  * @possible_actions: the possible actions
429  * @time_: the timestamp for this operation
430  *
431  * Updates the drag context when the pointer moves or the
432  * set of actions changes.
433  *
434  * This function is called by the drag source.
435  *
436  * This function does not need to be called in managed drag and drop
437  * operations. See gdk_drag_context_manage_dnd() for more information.
438  *
439  * Returns:
440  */
441 gboolean
gdk_drag_motion(GdkDragContext * context,GdkWindow * dest_window,GdkDragProtocol protocol,gint x_root,gint y_root,GdkDragAction suggested_action,GdkDragAction possible_actions,guint32 time_)442 gdk_drag_motion (GdkDragContext *context,
443                  GdkWindow      *dest_window,
444                  GdkDragProtocol protocol,
445                  gint            x_root,
446                  gint            y_root,
447                  GdkDragAction   suggested_action,
448                  GdkDragAction   possible_actions,
449                  guint32         time_)
450 {
451   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), FALSE);
452 
453   return GDK_DRAG_CONTEXT_GET_CLASS (context)
454        ->drag_motion (context,
455                       dest_window,
456                       protocol,
457                       x_root,
458                       y_root,
459                       suggested_action,
460                       possible_actions,
461                       time_);
462 }
463 
464 /**
465  * gdk_drag_abort:
466  * @context: a #GdkDragContext
467  * @time_: the timestamp for this operation
468  *
469  * Aborts a drag without dropping.
470  *
471  * This function is called by the drag source.
472  *
473  * This function does not need to be called in managed drag and drop
474  * operations. See gdk_drag_context_manage_dnd() for more information.
475  */
476 void
gdk_drag_abort(GdkDragContext * context,guint32 time_)477 gdk_drag_abort (GdkDragContext *context,
478                 guint32         time_)
479 {
480   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
481 
482   GDK_DRAG_CONTEXT_GET_CLASS (context)->drag_abort (context, time_);
483 }
484 
485 /**
486  * gdk_drag_drop:
487  * @context: a #GdkDragContext
488  * @time_: the timestamp for this operation
489  *
490  * Drops on the current destination.
491  *
492  * This function is called by the drag source.
493  *
494  * This function does not need to be called in managed drag and drop
495  * operations. See gdk_drag_context_manage_dnd() for more information.
496  */
497 void
gdk_drag_drop(GdkDragContext * context,guint32 time_)498 gdk_drag_drop (GdkDragContext *context,
499                guint32         time_)
500 {
501   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
502 
503   GDK_DRAG_CONTEXT_GET_CLASS (context)->drag_drop (context, time_);
504 }
505 
506 /**
507  * gdk_drop_reply:
508  * @context: a #GdkDragContext
509  * @accepted: %TRUE if the drop is accepted
510  * @time_: the timestamp for this operation
511  *
512  * Accepts or rejects a drop.
513  *
514  * This function is called by the drag destination in response
515  * to a drop initiated by the drag source.
516  */
517 void
gdk_drop_reply(GdkDragContext * context,gboolean accepted,guint32 time_)518 gdk_drop_reply (GdkDragContext *context,
519                 gboolean        accepted,
520                 guint32         time_)
521 {
522   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
523 
524   GDK_DRAG_CONTEXT_GET_CLASS (context)->drop_reply (context, accepted, time_);
525 }
526 
527 /**
528  * gdk_drop_finish:
529  * @context: a #GdkDragContext
530  * @success: %TRUE if the data was successfully received
531  * @time_: the timestamp for this operation
532  *
533  * Ends the drag operation after a drop.
534  *
535  * This function is called by the drag destination.
536  */
537 void
gdk_drop_finish(GdkDragContext * context,gboolean success,guint32 time_)538 gdk_drop_finish (GdkDragContext *context,
539                  gboolean        success,
540                  guint32         time_)
541 {
542   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
543 
544   GDK_DRAG_CONTEXT_GET_CLASS (context)->drop_finish (context, success, time_);
545 }
546 
547 /**
548  * gdk_drag_drop_succeeded:
549  * @context: a #GdkDragContext
550  *
551  * Returns whether the dropped data has been successfully
552  * transferred. This function is intended to be used while
553  * handling a %GDK_DROP_FINISHED event, its return value is
554  * meaningless at other times.
555  *
556  * Returns: %TRUE if the drop was successful.
557  *
558  * Since: 2.6
559  **/
560 gboolean
gdk_drag_drop_succeeded(GdkDragContext * context)561 gdk_drag_drop_succeeded (GdkDragContext *context)
562 {
563   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), FALSE);
564 
565   return GDK_DRAG_CONTEXT_GET_CLASS (context)->drop_status (context);
566 }
567 
568 /**
569  * gdk_drag_get_selection:
570  * @context: a #GdkDragContext.
571  *
572  * Returns the selection atom for the current source window.
573  *
574  * Returns: (transfer none): the selection atom, or %GDK_NONE
575  */
576 GdkAtom
gdk_drag_get_selection(GdkDragContext * context)577 gdk_drag_get_selection (GdkDragContext *context)
578 {
579   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
580 
581   return GDK_DRAG_CONTEXT_GET_CLASS (context)->get_selection (context);
582 }
583 
584 /**
585  * gdk_drag_context_get_drag_window:
586  * @context: a #GdkDragContext
587  *
588  * Returns the window on which the drag icon should be rendered
589  * during the drag operation. Note that the window may not be
590  * available until the drag operation has begun. GDK will move
591  * the window in accordance with the ongoing drag operation.
592  * The window is owned by @context and will be destroyed when
593  * the drag operation is over.
594  *
595  * Returns: (nullable) (transfer none): the drag window, or %NULL
596  *
597  * Since: 3.20
598  */
599 GdkWindow *
gdk_drag_context_get_drag_window(GdkDragContext * context)600 gdk_drag_context_get_drag_window (GdkDragContext *context)
601 {
602   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
603 
604   if (GDK_DRAG_CONTEXT_GET_CLASS (context)->get_drag_window)
605     return GDK_DRAG_CONTEXT_GET_CLASS (context)->get_drag_window (context);
606 
607   return NULL;
608 }
609 
610 /**
611  * gdk_drag_context_set_hotspot:
612  * @context: a #GdkDragContext
613  * @hot_x: x coordinate of the drag window hotspot
614  * @hot_y: y coordinate of the drag window hotspot
615  *
616  * Sets the position of the drag window that will be kept
617  * under the cursor hotspot. Initially, the hotspot is at the
618  * top left corner of the drag window.
619  *
620  * Since: 3.20
621  */
622 void
gdk_drag_context_set_hotspot(GdkDragContext * context,gint hot_x,gint hot_y)623 gdk_drag_context_set_hotspot (GdkDragContext *context,
624                               gint            hot_x,
625                               gint            hot_y)
626 {
627   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
628 
629   if (GDK_DRAG_CONTEXT_GET_CLASS (context)->set_hotspot)
630     GDK_DRAG_CONTEXT_GET_CLASS (context)->set_hotspot (context, hot_x, hot_y);
631 }
632 
633 /**
634  * gdk_drag_drop_done:
635  * @context: a #GdkDragContext
636  * @success: whether the drag was ultimatively successful
637  *
638  * Inform GDK if the drop ended successfully. Passing %FALSE
639  * for @success may trigger a drag cancellation animation.
640  *
641  * This function is called by the drag source, and should
642  * be the last call before dropping the reference to the
643  * @context.
644  *
645  * The #GdkDragContext will only take the first gdk_drag_drop_done()
646  * call as effective, if this function is called multiple times,
647  * all subsequent calls will be ignored.
648  *
649  * Since: 3.20
650  */
651 void
gdk_drag_drop_done(GdkDragContext * context,gboolean success)652 gdk_drag_drop_done (GdkDragContext *context,
653                     gboolean        success)
654 {
655   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
656 
657   if (context->drop_done)
658     return;
659 
660   context->drop_done = TRUE;
661 
662   if (GDK_DRAG_CONTEXT_GET_CLASS (context)->drop_done)
663     GDK_DRAG_CONTEXT_GET_CLASS (context)->drop_done (context, success);
664 }
665 
666 /**
667  * gdk_drag_context_manage_dnd:
668  * @context: a #GdkDragContext
669  * @ipc_window: Window to use for IPC messaging/events
670  * @actions: the actions supported by the drag source
671  *
672  * Requests the drag and drop operation to be managed by @context.
673  * When a drag and drop operation becomes managed, the #GdkDragContext
674  * will internally handle all input and source-side #GdkEventDND events
675  * as required by the windowing system.
676  *
677  * Once the drag and drop operation is managed, the drag context will
678  * emit the following signals:
679  * - The #GdkDragContext::action-changed signal whenever the final action
680  *   to be performed by the drag and drop operation changes.
681  * - The #GdkDragContext::drop-performed signal after the user performs
682  *   the drag and drop gesture (typically by releasing the mouse button).
683  * - The #GdkDragContext::dnd-finished signal after the drag and drop
684  *   operation concludes (after all #GdkSelection transfers happen).
685  * - The #GdkDragContext::cancel signal if the drag and drop operation is
686  *   finished but doesn't happen over an accepting destination, or is
687  *   cancelled through other means.
688  *
689  * Returns: #TRUE if the drag and drop operation is managed.
690  *
691  * Since: 3.20
692  **/
693 gboolean
gdk_drag_context_manage_dnd(GdkDragContext * context,GdkWindow * ipc_window,GdkDragAction actions)694 gdk_drag_context_manage_dnd (GdkDragContext *context,
695                              GdkWindow      *ipc_window,
696                              GdkDragAction   actions)
697 {
698   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), FALSE);
699   g_return_val_if_fail (GDK_IS_WINDOW (ipc_window), FALSE);
700 
701   if (GDK_DRAG_CONTEXT_GET_CLASS (context)->manage_dnd)
702     return GDK_DRAG_CONTEXT_GET_CLASS (context)->manage_dnd (context, ipc_window, actions);
703 
704   return FALSE;
705 }
706 
707 void
gdk_drag_context_set_cursor(GdkDragContext * context,GdkCursor * cursor)708 gdk_drag_context_set_cursor (GdkDragContext *context,
709                              GdkCursor      *cursor)
710 {
711   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
712 
713   if (GDK_DRAG_CONTEXT_GET_CLASS (context)->set_cursor)
714     GDK_DRAG_CONTEXT_GET_CLASS (context)->set_cursor (context, cursor);
715 }
716 
717 void
gdk_drag_context_cancel(GdkDragContext * context,GdkDragCancelReason reason)718 gdk_drag_context_cancel (GdkDragContext      *context,
719                          GdkDragCancelReason  reason)
720 {
721   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
722 
723   g_signal_emit (context, signals[CANCEL], 0, reason);
724 }
725 
726 GList *
gdk_drag_context_list(void)727 gdk_drag_context_list (void)
728 {
729   return contexts;
730 }
731 
732 gboolean
gdk_drag_context_handle_source_event(GdkEvent * event)733 gdk_drag_context_handle_source_event (GdkEvent *event)
734 {
735   GdkDragContext *context;
736   GList *l;
737 
738   for (l = contexts; l; l = l->next)
739     {
740       context = l->data;
741 
742       if (!context->is_source)
743         continue;
744 
745       if (!GDK_DRAG_CONTEXT_GET_CLASS (context)->handle_event)
746         continue;
747 
748       if (GDK_DRAG_CONTEXT_GET_CLASS (context)->handle_event (context, event))
749         return TRUE;
750     }
751 
752   return FALSE;
753 }
754 
755 GdkCursor *
gdk_drag_get_cursor(GdkDragContext * context,GdkDragAction action)756 gdk_drag_get_cursor (GdkDragContext *context,
757                      GdkDragAction   action)
758 {
759   gint i;
760 
761   for (i = 0 ; i < G_N_ELEMENTS (drag_cursors) - 1; i++)
762     if (drag_cursors[i].action == action)
763       break;
764 
765   if (drag_cursors[i].cursor == NULL)
766     drag_cursors[i].cursor = gdk_cursor_new_from_name (context->display,
767                                                        drag_cursors[i].name);
768   return drag_cursors[i].cursor;
769 }
770 
771 static void
gdk_drag_context_commit_drag_status(GdkDragContext * context)772 gdk_drag_context_commit_drag_status (GdkDragContext *context)
773 {
774   GdkDragContextClass *context_class;
775 
776   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
777   g_return_if_fail (!context->is_source);
778 
779   context_class = GDK_DRAG_CONTEXT_GET_CLASS (context);
780 
781   if (context_class->commit_drag_status)
782     context_class->commit_drag_status (context);
783 }
784 
785 gboolean
gdk_drag_context_handle_dest_event(GdkEvent * event)786 gdk_drag_context_handle_dest_event (GdkEvent *event)
787 {
788   GdkDragContext *context = NULL;
789   GList *l;
790 
791   switch (event->type)
792     {
793     case GDK_DRAG_MOTION:
794     case GDK_DROP_START:
795       context = event->dnd.context;
796       break;
797     case GDK_SELECTION_NOTIFY:
798       for (l = contexts; l; l = l->next)
799         {
800           GdkDragContext *c = l->data;
801 
802           if (!c->is_source &&
803               event->selection.selection == gdk_drag_get_selection (c))
804             {
805               context = c;
806               break;
807             }
808         }
809       break;
810     default:
811       return FALSE;
812     }
813 
814   if (!context)
815     return FALSE;
816 
817   gdk_drag_context_commit_drag_status (context);
818   return TRUE;;
819 }
820