]> Pileus Git - ~andy/gtk/blob - gtk/gtkdnd.c
Convert a bunch of visual related calls to use the screen vtable
[~andy/gtk] / gtk / gtkdnd.c
1 /* GTK - The GIMP Toolkit
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #include <math.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "gdkconfig.h"
34
35 #include "gdk/gdkkeysyms.h"
36
37 #ifdef GDK_WINDOWING_X11
38 #include <X11/Xlib.h>
39 #include <X11/keysym.h>
40 #include "gdk/x11/gdkx.h"
41 #endif
42
43 #include "gtkdnd.h"
44 #include "gtkiconfactory.h"
45 #include "gtkicontheme.h"
46 #include "gtkimage.h"
47 #include "gtkinvisible.h"
48 #include "gtkmain.h"
49 #include "gtkplug.h"
50 #include "gtkstock.h"
51 #include "gtktooltip.h"
52 #include "gtkwindow.h"
53 #include "gtkintl.h"
54 #include "gtkdndcursors.h"
55 #include "gtkselectionprivate.h"
56
57 static GSList *source_widgets = NULL;
58
59 typedef struct _GtkDragSourceSite GtkDragSourceSite;
60 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
61 typedef struct _GtkDragDestSite GtkDragDestSite;
62 typedef struct _GtkDragDestInfo GtkDragDestInfo;
63 typedef struct _GtkDragAnim GtkDragAnim;
64
65
66 typedef enum 
67 {
68   GTK_DRAG_STATUS_DRAG,
69   GTK_DRAG_STATUS_WAIT,
70   GTK_DRAG_STATUS_DROP
71 } GtkDragStatus;
72
73 struct _GtkDragSourceSite 
74 {
75   GdkModifierType    start_button_mask;
76   GtkTargetList     *target_list;        /* Targets for drag data */
77   GdkDragAction      actions;            /* Possible actions */
78
79   /* Drag icon */
80   GtkImageType icon_type;
81   union
82   {
83     GtkImagePixbufData pixbuf;
84     GtkImageStockData stock;
85     GtkImageIconNameData name;
86   } icon_data;
87
88   /* Stored button press information to detect drag beginning */
89   gint               state;
90   gint               x, y;
91 };
92   
93 struct _GtkDragSourceInfo 
94 {
95   GtkWidget         *widget;
96   GtkTargetList     *target_list; /* Targets for drag data */
97   GdkDragAction      possible_actions; /* Actions allowed by source */
98   GdkDragContext    *context;     /* drag context */
99   GtkWidget         *icon_window; /* Window for drag */
100   GtkWidget         *fallback_icon; /* Window for drag used on other screens */
101   GtkWidget         *ipc_widget;  /* GtkInvisible for grab, message passing */
102   GdkCursor         *cursor;      /* Cursor for drag */
103   gint hot_x, hot_y;              /* Hot spot for drag */
104   gint button;                    /* mouse button starting drag */
105
106   GtkDragStatus      status;      /* drag status */
107   GdkEvent          *last_event;  /* pending event */
108
109   gint               start_x, start_y; /* Initial position */
110   gint               cur_x, cur_y;     /* Current Position */
111   GdkScreen         *cur_screen;       /* Current screen for pointer */
112
113   guint32            grab_time;   /* timestamp for initial grab */
114   GList             *selections;  /* selections we've claimed */
115   
116   GtkDragDestInfo   *proxy_dest;  /* Set if this is a proxy drag */
117
118   guint              update_idle;      /* Idle function to update the drag */
119   guint              drop_timeout;     /* Timeout for aborting drop */
120   guint              destroy_icon : 1; /* If true, destroy icon_window */
121   guint              have_grab : 1;    /* Do we still have the pointer grab */
122   GdkPixbuf         *icon_pixbuf;
123   GdkCursor         *drag_cursors[6];
124 };
125
126 struct _GtkDragDestSite
127 {
128   GtkDestDefaults    flags;
129   GtkTargetList     *target_list;
130   GdkDragAction      actions;
131   GdkWindow         *proxy_window;
132   GdkDragProtocol    proxy_protocol;
133   guint              do_proxy     : 1;
134   guint              proxy_coords : 1;
135   guint              have_drag    : 1;
136   guint              track_motion : 1;
137 };
138
139 struct _GtkDragDestInfo
140 {
141   GtkWidget         *widget;              /* Widget in which drag is in */
142   GdkDragContext    *context;             /* Drag context */
143   GtkDragSourceInfo *proxy_source;        /* Set if this is a proxy drag */
144   GtkSelectionData  *proxy_data;          /* Set while retrieving proxied data */
145   guint32            proxy_drop_time;     /* Timestamp for proxied drop */
146   guint              proxy_drop_wait : 1; /* Set if we are waiting for a
147                                            * status reply before sending
148                                            * a proxied drop on.
149                                            */
150   guint              dropped : 1;         /* Set after we receive a drop */
151   gint               drop_x, drop_y;      /* Position of drop */
152 };
153
154 #define DROP_ABORT_TIME 300000
155
156 #define ANIM_STEP_TIME 50
157 #define ANIM_STEP_LENGTH 50
158 #define ANIM_MIN_STEPS 5
159 #define ANIM_MAX_STEPS 10
160
161 struct _GtkDragAnim 
162 {
163   GtkDragSourceInfo *info;
164   gint step;
165   gint n_steps;
166 };
167
168 typedef gboolean (* GtkDragDestCallback) (GtkWidget      *widget,
169                                           GdkDragContext *context,
170                                           gint            x,
171                                           gint            y,
172                                           guint32         time);
173
174 /* Enumeration for some targets we handle internally */
175
176 enum {
177   TARGET_MOTIF_SUCCESS = 0x40000000,
178   TARGET_MOTIF_FAILURE,
179   TARGET_DELETE
180 };
181
182 /* Forward declarations */
183 static void          gtk_drag_get_event_actions (GdkEvent        *event, 
184                                                  gint             button,
185                                                  GdkDragAction    actions,
186                                                  GdkDragAction   *suggested_action,
187                                                  GdkDragAction   *possible_actions);
188 static GdkCursor *   gtk_drag_get_cursor         (GdkDisplay     *display,
189                                                   GdkDragAction   action,
190                                                   GtkDragSourceInfo *info);
191 static void          gtk_drag_update_cursor      (GtkDragSourceInfo *info);
192 static GtkWidget    *gtk_drag_get_ipc_widget            (GtkWidget *widget);
193 static GtkWidget    *gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen);
194 static void          gtk_drag_release_ipc_widget (GtkWidget      *widget);
195
196 static void     gtk_drag_selection_received     (GtkWidget        *widget,
197                                                  GtkSelectionData *selection_data,
198                                                  guint             time,
199                                                  gpointer          data);
200 static gboolean gtk_drag_find_widget            (GtkWidget        *widget,
201                                                  GdkDragContext   *context,
202                                                  GtkDragDestInfo  *info,
203                                                  gint              x,
204                                                  gint              y,
205                                                  guint32           time,
206                                                  GtkDragDestCallback callback);
207 static void     gtk_drag_proxy_begin            (GtkWidget        *widget,
208                                                  GtkDragDestInfo  *dest_info,
209                                                  guint32           time);
210 static void     gtk_drag_dest_realized          (GtkWidget        *widget);
211 static void     gtk_drag_dest_hierarchy_changed (GtkWidget        *widget,
212                                                  GtkWidget        *previous_toplevel);
213 static void     gtk_drag_dest_site_destroy      (gpointer          data);
214 static void     gtk_drag_dest_leave             (GtkWidget        *widget,
215                                                  GdkDragContext   *context,
216                                                  guint             time);
217 static gboolean gtk_drag_dest_motion            (GtkWidget        *widget,
218                                                  GdkDragContext   *context,
219                                                  gint              x,
220                                                  gint              y,
221                                                  guint             time);
222 static gboolean gtk_drag_dest_drop              (GtkWidget        *widget,
223                                                  GdkDragContext   *context,
224                                                  gint              x,
225                                                  gint              y,
226                                                  guint             time);
227
228 static GtkDragDestInfo *  gtk_drag_get_dest_info     (GdkDragContext *context,
229                                                       gboolean        create);
230 static GtkDragSourceInfo *gtk_drag_get_source_info   (GdkDragContext *context,
231                                                       gboolean        create);
232 static void               gtk_drag_clear_source_info (GdkDragContext *context);
233
234 static void gtk_drag_source_check_selection    (GtkDragSourceInfo *info, 
235                                                 GdkAtom            selection,
236                                                 guint32            time);
237 static void gtk_drag_source_release_selections (GtkDragSourceInfo *info,
238                                                 guint32            time);
239 static void gtk_drag_drop                      (GtkDragSourceInfo *info,
240                                                 guint32            time);
241 static void gtk_drag_drop_finished             (GtkDragSourceInfo *info,
242                                                 GtkDragResult      result,
243                                                 guint              time);
244 static void gtk_drag_cancel                    (GtkDragSourceInfo *info,
245                                                 GtkDragResult      result,
246                                                 guint32            time);
247
248 static gboolean gtk_drag_source_event_cb       (GtkWidget         *widget,
249                                                 GdkEvent          *event,
250                                                 gpointer           data);
251 static void gtk_drag_source_site_destroy       (gpointer           data);
252 static void gtk_drag_selection_get             (GtkWidget         *widget, 
253                                                 GtkSelectionData  *selection_data,
254                                                 guint              sel_info,
255                                                 guint32            time,
256                                                 gpointer           data);
257 static gboolean gtk_drag_anim_timeout          (gpointer           data);
258 static void gtk_drag_remove_icon               (GtkDragSourceInfo *info);
259 static void gtk_drag_source_info_destroy       (GtkDragSourceInfo *info);
260 static void gtk_drag_add_update_idle           (GtkDragSourceInfo *info);
261
262 static void gtk_drag_update                    (GtkDragSourceInfo *info,
263                                                 GdkScreen         *screen,
264                                                 gint               x_root,
265                                                 gint               y_root,
266                                                 GdkEvent          *event);
267 static gboolean gtk_drag_motion_cb             (GtkWidget         *widget, 
268                                                 GdkEventMotion    *event, 
269                                                 gpointer           data);
270 static gboolean gtk_drag_key_cb                (GtkWidget         *widget, 
271                                                 GdkEventKey       *event, 
272                                                 gpointer           data);
273 static gboolean gtk_drag_grab_broken_event_cb  (GtkWidget          *widget,
274                                                 GdkEventGrabBroken *event,
275                                                 gpointer            data);
276 static void     gtk_drag_grab_notify_cb        (GtkWidget         *widget,
277                                                 gboolean           was_grabbed,
278                                                 gpointer           data);
279 static gboolean gtk_drag_button_release_cb     (GtkWidget         *widget, 
280                                                 GdkEventButton    *event, 
281                                                 gpointer           data);
282 static gboolean gtk_drag_abort_timeout         (gpointer           data);
283
284 static void     set_icon_stock_pixbuf          (GdkDragContext    *context,
285                                                 const gchar       *stock_id,
286                                                 GdkPixbuf         *pixbuf,
287                                                 gint               hot_x,
288                                                 gint               hot_y,
289                                                 gboolean           force_window);
290
291 /************************
292  * Cursor and Icon data *
293  ************************/
294
295 static struct {
296   GdkDragAction action;
297   const gchar  *name;
298   const guint8 *data;
299   GdkPixbuf    *pixbuf;
300   GdkCursor    *cursor;
301 } drag_cursors[] = {
302   { GDK_ACTION_DEFAULT, NULL },
303   { GDK_ACTION_ASK,   "dnd-ask",  dnd_cursor_ask,  NULL, NULL },
304   { GDK_ACTION_COPY,  "dnd-copy", dnd_cursor_copy, NULL, NULL },
305   { GDK_ACTION_MOVE,  "dnd-move", dnd_cursor_move, NULL, NULL },
306   { GDK_ACTION_LINK,  "dnd-link", dnd_cursor_link, NULL, NULL },
307   { 0              ,  "dnd-none", dnd_cursor_none, NULL, NULL },
308 };
309
310 static const gint n_drag_cursors = sizeof (drag_cursors) / sizeof (drag_cursors[0]);
311
312 /*********************
313  * Utility functions *
314  *********************/
315
316 static void
317 set_can_change_screen (GtkWidget *widget,
318                        gboolean   can_change_screen)
319 {
320   can_change_screen = can_change_screen != FALSE;
321   
322   g_object_set_data (G_OBJECT (widget), I_("gtk-dnd-can-change-screen"),
323                      GUINT_TO_POINTER (can_change_screen));
324 }
325
326 static gboolean
327 get_can_change_screen (GtkWidget *widget)
328 {
329   return g_object_get_data (G_OBJECT (widget), "gtk-dnd-can-change-screen") != NULL;
330
331 }
332
333 static GtkWidget *
334 gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen)
335 {
336   GtkWidget *result;
337   GSList *drag_widgets = g_object_get_data (G_OBJECT (screen), 
338                                             "gtk-dnd-ipc-widgets");
339   
340   if (drag_widgets)
341     {
342       GSList *tmp = drag_widgets;
343       result = drag_widgets->data;
344       drag_widgets = drag_widgets->next;
345       g_object_set_data (G_OBJECT (screen),
346                          I_("gtk-dnd-ipc-widgets"),
347                          drag_widgets);
348       g_slist_free_1 (tmp);
349     }
350   else
351     {
352       result = gtk_window_new (GTK_WINDOW_POPUP);
353       gtk_window_set_screen (GTK_WINDOW (result), screen);
354       gtk_window_resize (GTK_WINDOW (result), 1, 1);
355       gtk_window_move (GTK_WINDOW (result), -100, -100);
356       gtk_widget_show (result);
357     }  
358
359   return result;
360 }
361
362 static GtkWidget *
363 gtk_drag_get_ipc_widget (GtkWidget *widget)
364 {
365   GtkWidget *result;
366   GtkWidget *toplevel;
367
368   result = gtk_drag_get_ipc_widget_for_screen (gtk_widget_get_screen (widget));
369   
370   toplevel = gtk_widget_get_toplevel (widget);
371   
372   if (GTK_IS_WINDOW (toplevel))
373     {
374       if (gtk_window_has_group (GTK_WINDOW (toplevel)))
375         gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
376                                      GTK_WINDOW (result));
377     }
378
379   return result;
380 }
381
382 /* FIXME: modifying the XEvent window as in root_key_filter() isn't
383  * going to work with XGE/XI2, since the actual event to handle would
384  * be allocated/freed before GDK gets to translate the event.
385  * Active grabs on the keyboard are used instead at the moment...
386  */
387 #if defined (GDK_WINDOWING_X11) && !defined (XINPUT_2)
388
389 /*
390  * We want to handle a handful of keys during DND, e.g. Escape to abort.
391  * Grabbing the keyboard has the unfortunate side-effect of preventing
392  * useful things such as using Alt-Tab to cycle between windows or
393  * switching workspaces. Therefore, we just grab the few keys we are
394  * interested in. Note that we need to put the grabs on the root window
395  * in order for them to still work when the focus is moved to another
396  * app/workspace.
397  *
398  * GDK needs a little help to successfully deliver root key events...
399  */
400
401 static GdkFilterReturn
402 root_key_filter (GdkXEvent *xevent,
403                  GdkEvent  *event,
404                  gpointer   data)
405 {
406   XEvent *ev = (XEvent *) xevent;
407
408   if ((ev->type == KeyPress || ev->type == KeyRelease) &&
409       ev->xkey.root == ev->xkey.window)
410     ev->xkey.window = (Window)data;
411
412   return GDK_FILTER_CONTINUE;
413 }
414
415 typedef struct {
416   gint keysym;
417   gint modifiers;
418 } GrabKey;
419
420 static GrabKey grab_keys[] = {
421   { XK_Escape, 0 },
422   { XK_space, 0 },
423   { XK_KP_Space, 0 },
424   { XK_Return, 0 },
425   { XK_KP_Enter, 0 },
426   { XK_Up, 0 },
427   { XK_Up, Mod1Mask },
428   { XK_Down, 0 },
429   { XK_Down, Mod1Mask },
430   { XK_Left, 0 },
431   { XK_Left, Mod1Mask },
432   { XK_Right, 0 },
433   { XK_Right, Mod1Mask },
434   { XK_KP_Up, 0 },
435   { XK_KP_Up, Mod1Mask },
436   { XK_KP_Down, 0 },
437   { XK_KP_Down, Mod1Mask },
438   { XK_KP_Left, 0 },
439   { XK_KP_Left, Mod1Mask },
440   { XK_KP_Right, 0 },
441   { XK_KP_Right, Mod1Mask }
442 };
443
444 static void
445 grab_dnd_keys (GtkWidget *widget,
446                GdkDevice *device,
447                guint32    time)
448 {
449   guint i;
450   GdkWindow *window, *root;
451   gint keycode;
452
453   window = gtk_widget_get_window (widget);
454   root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
455
456   gdk_error_trap_push ();
457
458   for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
459     {
460       keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
461       XGrabKey (GDK_WINDOW_XDISPLAY (window),
462                 keycode, grab_keys[i].modifiers,
463                 GDK_WINDOW_XID (root),
464                 FALSE,
465                 GrabModeAsync,
466                 GrabModeAsync);
467     }
468
469   gdk_flush ();
470   gdk_error_trap_pop ();
471
472   gdk_window_add_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
473 }
474
475 static void
476 ungrab_dnd_keys (GtkWidget *widget,
477                  GdkDevice *device,
478                  guint32    time)
479 {
480   guint i;
481   GdkWindow *window, *root;
482   gint keycode;
483
484   window = gtk_widget_get_window (widget);
485   root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
486
487   gdk_window_remove_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
488
489   gdk_error_trap_push ();
490
491   for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
492     {
493       keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
494       XUngrabKey (GDK_WINDOW_XDISPLAY (window),
495                   keycode, grab_keys[i].modifiers,
496                   GDK_WINDOW_XID (root));
497     }
498
499   gdk_flush ();
500   gdk_error_trap_pop ();
501 }
502
503 #else /* GDK_WINDOWING_X11 && !XINPUT_2 */
504
505 static void
506 grab_dnd_keys (GtkWidget *widget,
507                GdkDevice *device,
508                guint32    time)
509 {
510   gdk_device_grab (device,
511                    gtk_widget_get_window (widget),
512                    GDK_OWNERSHIP_APPLICATION, FALSE,
513                    GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
514                    NULL, time);
515 }
516
517 static void
518 ungrab_dnd_keys (GtkWidget *widget,
519                  GdkDevice *device,
520                  guint32    time)
521 {
522   gdk_device_ungrab (device, time);
523 }
524
525 #endif /* GDK_WINDOWING_X11 */
526
527
528 /***************************************************************
529  * gtk_drag_release_ipc_widget:
530  *     Releases widget retrieved with gtk_drag_get_ipc_widget ()
531  *   arguments:
532  *     widget: the widget to release.
533  *   results:
534  ***************************************************************/
535
536 static void
537 gtk_drag_release_ipc_widget (GtkWidget *widget)
538 {
539   GtkWindow *window = GTK_WINDOW (widget);
540   GdkScreen *screen = gtk_widget_get_screen (widget);
541   GdkDragContext *context = g_object_get_data (G_OBJECT (widget), "drag-context");
542   GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
543                                             "gtk-dnd-ipc-widgets");
544   GdkDevice *pointer, *keyboard;
545
546   if (context)
547     {
548       pointer = gdk_drag_context_get_device (context);
549       keyboard = gdk_device_get_associated_device (pointer);
550
551       if (keyboard)
552         ungrab_dnd_keys (widget, keyboard, GDK_CURRENT_TIME);
553     }
554
555   if (gtk_window_has_group (window))
556     gtk_window_group_remove_window (gtk_window_get_group (window),
557                                     window);
558   drag_widgets = g_slist_prepend (drag_widgets, widget);
559   g_object_set_data (G_OBJECT (screen),
560                      I_("gtk-dnd-ipc-widgets"),
561                      drag_widgets);
562 }
563
564 static guint32
565 gtk_drag_get_event_time (GdkEvent *event)
566 {
567   guint32 tm = GDK_CURRENT_TIME;
568   
569   if (event)
570     switch (event->type)
571       {
572       case GDK_MOTION_NOTIFY:
573         tm = event->motion.time; break;
574       case GDK_BUTTON_PRESS:
575       case GDK_2BUTTON_PRESS:
576       case GDK_3BUTTON_PRESS:
577       case GDK_BUTTON_RELEASE:
578         tm = event->button.time; break;
579       case GDK_KEY_PRESS:
580       case GDK_KEY_RELEASE:
581         tm = event->key.time; break;
582       case GDK_ENTER_NOTIFY:
583       case GDK_LEAVE_NOTIFY:
584         tm = event->crossing.time; break;
585       case GDK_PROPERTY_NOTIFY:
586         tm = event->property.time; break;
587       case GDK_SELECTION_CLEAR:
588       case GDK_SELECTION_REQUEST:
589       case GDK_SELECTION_NOTIFY:
590         tm = event->selection.time; break;
591       case GDK_PROXIMITY_IN:
592       case GDK_PROXIMITY_OUT:
593         tm = event->proximity.time; break;
594       default:                  /* use current time */
595         break;
596       }
597   
598   return tm;
599 }
600
601 static void
602 gtk_drag_get_event_actions (GdkEvent *event, 
603                             gint button, 
604                             GdkDragAction  actions,
605                             GdkDragAction *suggested_action,
606                             GdkDragAction *possible_actions)
607 {
608   *suggested_action = 0;
609   *possible_actions = 0;
610
611   if (event)
612     {
613       GdkModifierType state = 0;
614       
615       switch (event->type)
616         {
617         case GDK_MOTION_NOTIFY:
618           state = event->motion.state;
619           break;
620         case GDK_BUTTON_PRESS:
621         case GDK_2BUTTON_PRESS:
622         case GDK_3BUTTON_PRESS:
623         case GDK_BUTTON_RELEASE:
624           state = event->button.state;
625           break;
626         case GDK_KEY_PRESS:
627         case GDK_KEY_RELEASE:
628           state = event->key.state;
629           break;
630         case GDK_ENTER_NOTIFY:
631         case GDK_LEAVE_NOTIFY:
632           state = event->crossing.state;
633           break;
634         default:
635           break;
636         }
637
638       if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
639         {
640           *suggested_action = GDK_ACTION_ASK;
641           *possible_actions = actions;
642         }
643       else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
644         {
645           if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
646             {
647               if (actions & GDK_ACTION_LINK)
648                 {
649                   *suggested_action = GDK_ACTION_LINK;
650                   *possible_actions = GDK_ACTION_LINK;
651                 }
652             }
653           else if (state & GDK_CONTROL_MASK)
654             {
655               if (actions & GDK_ACTION_COPY)
656                 {
657                   *suggested_action = GDK_ACTION_COPY;
658                   *possible_actions = GDK_ACTION_COPY;
659                 }
660               return;
661             }
662           else
663             {
664               if (actions & GDK_ACTION_MOVE)
665                 {
666                   *suggested_action = GDK_ACTION_MOVE;
667                   *possible_actions = GDK_ACTION_MOVE;
668                 }
669               return;
670             }
671         }
672       else
673         {
674           *possible_actions = actions;
675
676           if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
677             *suggested_action = GDK_ACTION_ASK;
678           else if (actions & GDK_ACTION_COPY)
679             *suggested_action =  GDK_ACTION_COPY;
680           else if (actions & GDK_ACTION_MOVE)
681             *suggested_action = GDK_ACTION_MOVE;
682           else if (actions & GDK_ACTION_LINK)
683             *suggested_action = GDK_ACTION_LINK;
684         }
685     }
686   else
687     {
688       *possible_actions = actions;
689       
690       if (actions & GDK_ACTION_COPY)
691         *suggested_action =  GDK_ACTION_COPY;
692       else if (actions & GDK_ACTION_MOVE)
693         *suggested_action = GDK_ACTION_MOVE;
694       else if (actions & GDK_ACTION_LINK)
695         *suggested_action = GDK_ACTION_LINK;
696     }
697 }
698
699 static gboolean
700 gtk_drag_can_use_rgba_cursor (GdkDisplay *display, 
701                               gint        width,
702                               gint        height)
703 {
704   guint max_width, max_height;
705   
706   if (!gdk_display_supports_cursor_color (display))
707     return FALSE;
708
709   if (!gdk_display_supports_cursor_alpha (display))
710     return FALSE;
711
712   gdk_display_get_maximal_cursor_size (display, 
713                                        &max_width,
714                                        &max_height);
715   if (width > max_width || height > max_height)
716     {
717        /* can't use rgba cursor (too large) */
718       return FALSE;
719     }
720
721   return TRUE;
722 }
723
724 static GdkCursor *
725 gtk_drag_get_cursor (GdkDisplay        *display,
726                      GdkDragAction      action,
727                      GtkDragSourceInfo *info)
728 {
729   gint i;
730
731   /* reconstruct the cursors for each new drag (thus !info),
732    * to catch cursor theme changes 
733    */ 
734   if (!info)
735     {
736       for (i = 0 ; i < n_drag_cursors - 1; i++)
737         if (drag_cursors[i].cursor != NULL)
738           {
739             gdk_cursor_unref (drag_cursors[i].cursor);
740             drag_cursors[i].cursor = NULL;
741           }
742     }
743  
744   for (i = 0 ; i < n_drag_cursors - 1; i++)
745     if (drag_cursors[i].action == action)
746       break;
747
748   if (drag_cursors[i].pixbuf == NULL)
749     drag_cursors[i].pixbuf = 
750       gdk_pixbuf_new_from_inline (-1, drag_cursors[i].data, FALSE, NULL);
751
752   if (drag_cursors[i].cursor != NULL)
753     {
754       if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
755         {
756           gdk_cursor_unref (drag_cursors[i].cursor);
757           drag_cursors[i].cursor = NULL;
758         }
759     }
760   
761   if (drag_cursors[i].cursor == NULL)
762     drag_cursors[i].cursor = gdk_cursor_new_from_name (display, drag_cursors[i].name);
763   
764   if (drag_cursors[i].cursor == NULL)
765     drag_cursors[i].cursor = gdk_cursor_new_from_pixbuf (display, drag_cursors[i].pixbuf, 0, 0);
766
767   if (info && info->icon_pixbuf) 
768     {
769       gint cursor_width, cursor_height;
770       gint icon_width, icon_height;
771       gint width, height;
772       GdkPixbuf *cursor_pixbuf, *pixbuf;
773       gint hot_x, hot_y;
774       gint icon_x, icon_y, ref_x, ref_y;
775
776       if (info->drag_cursors[i] != NULL)
777         {
778           if (display == gdk_cursor_get_display (info->drag_cursors[i]))
779             return info->drag_cursors[i];
780           
781           gdk_cursor_unref (info->drag_cursors[i]);
782           info->drag_cursors[i] = NULL;
783         }
784
785       icon_x = info->hot_x;
786       icon_y = info->hot_y;
787       icon_width = gdk_pixbuf_get_width (info->icon_pixbuf);
788       icon_height = gdk_pixbuf_get_height (info->icon_pixbuf);
789
790       hot_x = hot_y = 0;
791       cursor_pixbuf = gdk_cursor_get_image (drag_cursors[i].cursor);
792       if (!cursor_pixbuf)
793         cursor_pixbuf = g_object_ref (drag_cursors[i].pixbuf);
794       else
795         {
796           if (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"))
797             hot_x = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"));
798           
799           if (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"))
800             hot_y = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"));
801
802 #if 0     
803           /* The code below is an attempt to let cursor themes
804            * determine the attachment of the icon to enable things
805            * like the following:
806            *
807            *    +-----+
808            *    |     |
809            *    |     ||
810            *    +-----+|
811            *        ---+
812            * 
813            * It does not work since Xcursor doesn't allow to attach
814            * any additional information to cursors in a retrievable
815            * way  (there are comments, but no way to get at them
816            * short of searching for the actual cursor file).
817            * If this code ever gets used, the icon_window placement
818            * must be changed to recognize these placement options
819            * as well. Note that this code ignores info->hot_x/y.
820            */ 
821           for (j = 0; j < 10; j++)
822             {
823               const gchar *opt;
824               gchar key[32];
825               gchar **toks;
826               GtkAnchorType icon_anchor;
827
828               g_snprintf (key, 32, "comment%d", j);
829               opt = gdk_pixbuf_get_option (cursor_pixbuf, key);
830               if (opt && g_str_has_prefix ("icon-attach:", opt))
831                 {
832                   toks = g_strsplit (opt + strlen ("icon-attach:"), "'", -1);
833                   if (g_strv_length (toks) != 3)
834                     {
835                       g_strfreev (toks);
836                       break;
837                     }
838                   icon_anchor = atoi (toks[0]);
839                   icon_x = atoi (toks[1]);
840                   icon_y = atoi (toks[2]);
841                   
842                   switch (icon_anchor)
843                     {
844                     case GTK_ANCHOR_NORTH:
845                     case GTK_ANCHOR_CENTER:
846                     case GTK_ANCHOR_SOUTH:
847                       icon_x += icon_width / 2;
848                       break;
849                     case GTK_ANCHOR_NORTH_EAST:
850                     case GTK_ANCHOR_EAST:
851                     case GTK_ANCHOR_SOUTH_EAST:
852                       icon_x += icon_width;
853                       break;
854                     default: ;
855                     }
856                   
857                   switch (icon_anchor)
858                     {
859                     case GTK_ANCHOR_WEST:
860                     case GTK_ANCHOR_CENTER:
861                     case GTK_ANCHOR_EAST:
862                       icon_y += icon_height / 2;
863                       break;
864                     case GTK_ANCHOR_SOUTH_WEST:
865                     case GTK_ANCHOR_SOUTH:
866                     case GTK_ANCHOR_SOUTH_EAST:
867                       icon_x += icon_height;
868                       break;
869                     default: ;
870                     }
871
872                   g_strfreev (toks);
873                   break;
874                 }
875             }
876 #endif
877         }
878
879       cursor_width = gdk_pixbuf_get_width (cursor_pixbuf);
880       cursor_height = gdk_pixbuf_get_height (cursor_pixbuf);
881       
882       ref_x = MAX (hot_x, icon_x);
883       ref_y = MAX (hot_y, icon_y);
884       width = ref_x + MAX (cursor_width - hot_x, icon_width - icon_x);
885       height = ref_y + MAX (cursor_height - hot_y, icon_height - icon_y);
886          
887       if (gtk_drag_can_use_rgba_cursor (display, width, height))
888         {
889           /* Composite cursor and icon so that both hotspots
890            * end up at (ref_x, ref_y)
891            */
892           pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
893                                    width, height); 
894           
895           gdk_pixbuf_fill (pixbuf, 0xff000000);
896           
897           gdk_pixbuf_composite (info->icon_pixbuf, pixbuf,
898                                 ref_x - icon_x, ref_y - icon_y, 
899                                 icon_width, icon_height,
900                                 ref_x - icon_x, ref_y - icon_y, 
901                                 1.0, 1.0, 
902                                 GDK_INTERP_BILINEAR, 255);
903           
904           gdk_pixbuf_composite (cursor_pixbuf, pixbuf,
905                                 ref_x - hot_x, ref_y - hot_y, 
906                                 cursor_width, cursor_height,
907                                 ref_x - hot_x, ref_y - hot_y,
908                                 1.0, 1.0, 
909                                 GDK_INTERP_BILINEAR, 255);
910           
911           info->drag_cursors[i] = 
912             gdk_cursor_new_from_pixbuf (display, pixbuf, ref_x, ref_y);
913           
914           g_object_unref (pixbuf);
915         }
916       
917       g_object_unref (cursor_pixbuf);
918       
919       if (info->drag_cursors[i] != NULL)
920         return info->drag_cursors[i];
921     }
922  
923   return drag_cursors[i].cursor;
924 }
925
926 static void
927 gtk_drag_update_cursor (GtkDragSourceInfo *info)
928 {
929   GdkCursor *cursor;
930   gint i;
931
932   if (!info->have_grab)
933     return;
934
935   for (i = 0 ; i < n_drag_cursors - 1; i++)
936     if (info->cursor == drag_cursors[i].cursor ||
937         info->cursor == info->drag_cursors[i])
938       break;
939   
940   if (i == n_drag_cursors)
941     return;
942
943   cursor = gtk_drag_get_cursor (gdk_cursor_get_display (info->cursor), 
944                                 drag_cursors[i].action, info);
945   
946   if (cursor != info->cursor)
947     {
948       GdkDevice *pointer;
949
950       pointer = gdk_drag_context_get_device (info->context);
951       gdk_device_grab (pointer,
952                        gtk_widget_get_window (info->ipc_widget),
953                        GDK_OWNERSHIP_APPLICATION, FALSE,
954                        GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
955                        cursor, info->grab_time);
956       info->cursor = cursor;
957     }
958 }
959
960 /********************
961  * Destination side *
962  ********************/
963
964 /*************************************************************
965  * gtk_drag_get_data:
966  *     Get the data for a drag or drop
967  *   arguments:
968  *     context - drag context
969  *     target  - format to retrieve the data in.
970  *     time    - timestamp of triggering event.
971  *     
972  *   results:
973  *************************************************************/
974
975 void 
976 gtk_drag_get_data (GtkWidget      *widget,
977                    GdkDragContext *context,
978                    GdkAtom         target,
979                    guint32         time)
980 {
981   GtkWidget *selection_widget;
982
983   g_return_if_fail (GTK_IS_WIDGET (widget));
984   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
985
986   selection_widget = gtk_drag_get_ipc_widget (widget);
987
988   g_object_ref (context);
989   g_object_ref (widget);
990   
991   g_signal_connect (selection_widget, "selection-received",
992                     G_CALLBACK (gtk_drag_selection_received), widget);
993
994   g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
995
996   gtk_selection_convert (selection_widget,
997                          gdk_drag_get_selection (context),
998                          target,
999                          time);
1000 }
1001
1002
1003 /**
1004  * gtk_drag_get_source_widget:
1005  * @context: a (destination side) drag context
1006  *
1007  * Determines the source widget for a drag.
1008  *
1009  * Return value: (transfer none): if the drag is occurring
1010  *     within a single application, a pointer to the source widget.
1011  *     Otherwise, %NULL.
1012  */
1013 GtkWidget *
1014 gtk_drag_get_source_widget (GdkDragContext *context)
1015 {
1016   GSList *tmp_list;
1017
1018   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
1019   
1020   tmp_list = source_widgets;
1021   while (tmp_list)
1022     {
1023       GtkWidget *ipc_widget = tmp_list->data;
1024
1025       if (gtk_widget_get_window (ipc_widget) == gdk_drag_context_get_source_window (context))
1026         {
1027           GtkDragSourceInfo *info;
1028           info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
1029
1030           return info ? info->widget : NULL;
1031         }
1032
1033       tmp_list = tmp_list->next;
1034     }
1035
1036   return NULL;
1037 }
1038
1039 /*************************************************************
1040  * gtk_drag_finish:
1041  *     Notify the drag source that the transfer of data
1042  *     is complete.
1043  *   arguments:
1044  *     context: The drag context for this drag
1045  *     success: Was the data successfully transferred?
1046  *     time:    The timestamp to use when notifying the destination.
1047  *   results:
1048  *************************************************************/
1049
1050 void 
1051 gtk_drag_finish (GdkDragContext *context,
1052                  gboolean        success,
1053                  gboolean        del,
1054                  guint32         time)
1055 {
1056   GdkAtom target = GDK_NONE;
1057
1058   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1059
1060   if (success && del)
1061     {
1062       target = gdk_atom_intern_static_string ("DELETE");
1063     }
1064   else if (gdk_drag_context_get_protocol (context) == GDK_DRAG_PROTO_MOTIF)
1065     {
1066       target = gdk_atom_intern_static_string (success ? 
1067                                               "XmTRANSFER_SUCCESS" : 
1068                                               "XmTRANSFER_FAILURE");
1069     }
1070
1071   if (target != GDK_NONE)
1072     {
1073       GtkWidget *selection_widget = gtk_drag_get_ipc_widget_for_screen (gdk_window_get_screen (gdk_drag_context_get_source_window (context)));
1074
1075       g_object_ref (context);
1076       
1077       g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1078       g_signal_connect (selection_widget, "selection-received",
1079                         G_CALLBACK (gtk_drag_selection_received),
1080                         NULL);
1081       
1082       gtk_selection_convert (selection_widget,
1083                              gdk_drag_get_selection (context),
1084                              target,
1085                              time);
1086     }
1087   
1088   if (!(success && del))
1089     gdk_drop_finish (context, success, time);
1090 }
1091
1092 /*************************************************************
1093  * gtk_drag_highlight_draw:
1094  *     Callback for expose_event for highlighted widgets.
1095  *   arguments:
1096  *     widget:
1097  *     event:
1098  *     data:
1099  *   results:
1100  *************************************************************/
1101
1102 static gboolean
1103 gtk_drag_highlight_draw (GtkWidget *widget,
1104                          cairo_t   *cr,
1105                          gpointer   data)
1106 {
1107   int width = gtk_widget_get_allocated_width (widget);
1108   int height = gtk_widget_get_allocated_height (widget);
1109
1110   gtk_paint_shadow (gtk_widget_get_style (widget), cr,
1111                     GTK_STATE_NORMAL, GTK_SHADOW_OUT,
1112                     widget, "dnd",
1113                     0, 0, width, height);
1114
1115   cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
1116   cairo_set_line_width (cr, 1.0);
1117   cairo_rectangle (cr,
1118                    0.5, 0.5,
1119                    width - 1, height - 1);
1120   cairo_stroke (cr);
1121
1122   return FALSE;
1123 }
1124
1125 /*************************************************************
1126  * gtk_drag_highlight:
1127  *     Highlight the given widget in the default manner.
1128  *   arguments:
1129  *     widget:
1130  *   results:
1131  *************************************************************/
1132
1133 void 
1134 gtk_drag_highlight (GtkWidget  *widget)
1135 {
1136   g_return_if_fail (GTK_IS_WIDGET (widget));
1137
1138   g_signal_connect_after (widget, "draw",
1139                           G_CALLBACK (gtk_drag_highlight_draw),
1140                           NULL);
1141
1142   gtk_widget_queue_draw (widget);
1143 }
1144
1145 /*************************************************************
1146  * gtk_drag_unhighlight:
1147  *     Refresh the given widget to remove the highlight.
1148  *   arguments:
1149  *     widget:
1150  *   results:
1151  *************************************************************/
1152
1153 void 
1154 gtk_drag_unhighlight (GtkWidget *widget)
1155 {
1156   g_return_if_fail (GTK_IS_WIDGET (widget));
1157
1158   g_signal_handlers_disconnect_by_func (widget,
1159                                         gtk_drag_highlight_draw,
1160                                         NULL);
1161   
1162   gtk_widget_queue_draw (widget);
1163 }
1164
1165 static void
1166 gtk_drag_dest_set_internal (GtkWidget       *widget,
1167                             GtkDragDestSite *site)
1168 {
1169   GtkDragDestSite *old_site;
1170   
1171   g_return_if_fail (widget != NULL);
1172
1173   /* HACK, do this in the destroy */
1174   old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1175   if (old_site)
1176     {
1177       g_signal_handlers_disconnect_by_func (widget,
1178                                             gtk_drag_dest_realized,
1179                                             old_site);
1180       g_signal_handlers_disconnect_by_func (widget,
1181                                             gtk_drag_dest_hierarchy_changed,
1182                                             old_site);
1183
1184       site->track_motion = old_site->track_motion;
1185     }
1186
1187   if (gtk_widget_get_realized (widget))
1188     gtk_drag_dest_realized (widget);
1189
1190   g_signal_connect (widget, "realize",
1191                     G_CALLBACK (gtk_drag_dest_realized), site);
1192   g_signal_connect (widget, "hierarchy-changed",
1193                     G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
1194
1195   g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
1196                           site, gtk_drag_dest_site_destroy);
1197 }
1198
1199 /**
1200  * gtk_drag_dest_set:
1201  * @widget: a #GtkWidget
1202  * @flags: which types of default drag behavior to use
1203  * @targets: (allow-none) (array length=n_targets): a pointer to an array of #GtkTargetEntry<!-- -->s
1204  *     indicating the drop types that this @widget will accept, or %NULL.
1205  *     Later you can access the list with gtk_drag_dest_get_target_list()
1206  *     and gtk_drag_dest_find_target().
1207  * @n_targets: the number of entries in @targets
1208  * @actions: a bitmask of possible actions for a drop onto this @widget.
1209  *
1210  * Sets a widget as a potential drop destination, and adds default behaviors.
1211  *
1212  * The default behaviors listed in @flags have an effect similar
1213  * to installing default handlers for the widget's drag-and-drop signals
1214  * (#GtkWidget:drag-motion, #GtkWidget:drag-drop, ...). They all exist
1215  * for convenience. When passing #GTK_DEST_DEFAULT_ALL for instance it is
1216  * sufficient to connect to the widget's #GtkWidget::drag-data-received
1217  * signal to get primitive, but consistent drag-and-drop support.
1218  *
1219  * Things become more complicated when you try to preview the dragged data,
1220  * as described in the documentation for #GtkWidget:drag-motion. The default
1221  * behaviors described by @flags make some assumptions, that can conflict
1222  * with your own signal handlers. For instance #GTK_DEST_DEFAULT_DROP causes
1223  * invokations of gdk_drag_status() in the context of #GtkWidget:drag-motion,
1224  * and invokations of gtk_drag_finish() in #GtkWidget:drag-data-received.
1225  * Especially the later is dramatic, when your own #GtkWidget:drag-motion
1226  * handler calls gtk_drag_get_data() to inspect the dragged data.
1227  *
1228  * There's no way to set a default action here, you can use the
1229  * #GtkWidget:drag-motion callback for that. Here's an example which selects
1230  * the action to use depending on whether the control key is pressed or not:
1231  * |[
1232  * static void
1233  * drag_motion (GtkWidget *widget,
1234  *              GdkDragContext *context,
1235  *              gint x,
1236  *              gint y,
1237  *              guint time)
1238  * {
1239  *   GdkModifierType mask;
1240  *
1241  *   gdk_window_get_pointer (gtk_widget_get_window (widget),
1242  *                           NULL, NULL, &mask);
1243  *   if (mask & GDK_CONTROL_MASK)
1244  *     gdk_drag_status (context, GDK_ACTION_COPY, time);
1245  *   else
1246  *     gdk_drag_status (context, GDK_ACTION_MOVE, time);
1247  * }
1248  * ]|
1249  */
1250 void
1251 gtk_drag_dest_set (GtkWidget            *widget,
1252                    GtkDestDefaults       flags,
1253                    const GtkTargetEntry *targets,
1254                    gint                  n_targets,
1255                    GdkDragAction         actions)
1256 {
1257   GtkDragDestSite *site;
1258   
1259   g_return_if_fail (GTK_IS_WIDGET (widget));
1260
1261   site = g_slice_new0 (GtkDragDestSite);
1262
1263   site->flags = flags;
1264   site->have_drag = FALSE;
1265   if (targets)
1266     site->target_list = gtk_target_list_new (targets, n_targets);
1267   else
1268     site->target_list = NULL;
1269   site->actions = actions;
1270   site->do_proxy = FALSE;
1271   site->proxy_window = NULL;
1272   site->track_motion = FALSE;
1273
1274   gtk_drag_dest_set_internal (widget, site);
1275 }
1276
1277 /*************************************************************
1278  * gtk_drag_dest_set_proxy:
1279  *     Set up this widget to proxy drags elsewhere.
1280  *   arguments:
1281  *     widget:          
1282  *     proxy_window:    window to which forward drag events
1283  *     protocol:        Drag protocol which the dest widget accepts
1284  *     use_coordinates: If true, send the same coordinates to the
1285  *                      destination, because it is a embedded 
1286  *                      subwindow.
1287  *   results:
1288  *************************************************************/
1289
1290 void 
1291 gtk_drag_dest_set_proxy (GtkWidget      *widget,
1292                          GdkWindow      *proxy_window,
1293                          GdkDragProtocol protocol,
1294                          gboolean        use_coordinates)
1295 {
1296   GtkDragDestSite *site;
1297   
1298   g_return_if_fail (GTK_IS_WIDGET (widget));
1299   g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
1300
1301   site = g_slice_new (GtkDragDestSite);
1302
1303   site->flags = 0;
1304   site->have_drag = FALSE;
1305   site->target_list = NULL;
1306   site->actions = 0;
1307   site->proxy_window = proxy_window;
1308   if (proxy_window)
1309     g_object_ref (proxy_window);
1310   site->do_proxy = TRUE;
1311   site->proxy_protocol = protocol;
1312   site->proxy_coords = use_coordinates;
1313   site->track_motion = FALSE;
1314
1315   gtk_drag_dest_set_internal (widget, site);
1316 }
1317
1318 /*************************************************************
1319  * gtk_drag_dest_unset
1320  *     Unregister this widget as a drag target.
1321  *   arguments:
1322  *     widget:
1323  *   results:
1324  *************************************************************/
1325
1326 void 
1327 gtk_drag_dest_unset (GtkWidget *widget)
1328 {
1329   GtkDragDestSite *old_site;
1330
1331   g_return_if_fail (GTK_IS_WIDGET (widget));
1332
1333   old_site = g_object_get_data (G_OBJECT (widget),
1334                                 "gtk-drag-dest");
1335   if (old_site)
1336     {
1337       g_signal_handlers_disconnect_by_func (widget,
1338                                             gtk_drag_dest_realized,
1339                                             old_site);
1340       g_signal_handlers_disconnect_by_func (widget,
1341                                             gtk_drag_dest_hierarchy_changed,
1342                                             old_site);
1343     }
1344
1345   g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1346 }
1347
1348 /**
1349  * gtk_drag_dest_get_target_list:
1350  * @widget: a #GtkWidget
1351  * 
1352  * Returns the list of targets this widget can accept from
1353  * drag-and-drop.
1354  * 
1355  * Return value: the #GtkTargetList, or %NULL if none
1356  **/
1357 GtkTargetList*
1358 gtk_drag_dest_get_target_list (GtkWidget *widget)
1359 {
1360   GtkDragDestSite *site;
1361
1362   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1363   
1364   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1365
1366   return site ? site->target_list : NULL;  
1367 }
1368
1369 /**
1370  * gtk_drag_dest_set_target_list:
1371  * @widget: a #GtkWidget that's a drag destination
1372  * @target_list: (allow-none): list of droppable targets, or %NULL for none
1373  * 
1374  * Sets the target types that this widget can accept from drag-and-drop.
1375  * The widget must first be made into a drag destination with
1376  * gtk_drag_dest_set().
1377  **/
1378 void
1379 gtk_drag_dest_set_target_list (GtkWidget      *widget,
1380                                GtkTargetList  *target_list)
1381 {
1382   GtkDragDestSite *site;
1383
1384   g_return_if_fail (GTK_IS_WIDGET (widget));
1385   
1386   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1387   
1388   if (!site)
1389     {
1390       g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
1391                  "to make the widget into a drag destination");
1392       return;
1393     }
1394
1395   if (target_list)
1396     gtk_target_list_ref (target_list);
1397   
1398   if (site->target_list)
1399     gtk_target_list_unref (site->target_list);
1400
1401   site->target_list = target_list;
1402 }
1403
1404 /**
1405  * gtk_drag_dest_add_text_targets:
1406  * @widget: a #GtkWidget that's a drag destination
1407  *
1408  * Add the text targets supported by #GtkSelection to
1409  * the target list of the drag destination. The targets
1410  * are added with @info = 0. If you need another value, 
1411  * use gtk_target_list_add_text_targets() and
1412  * gtk_drag_dest_set_target_list().
1413  * 
1414  * Since: 2.6
1415  **/
1416 void
1417 gtk_drag_dest_add_text_targets (GtkWidget *widget)
1418 {
1419   GtkTargetList *target_list;
1420
1421   target_list = gtk_drag_dest_get_target_list (widget);
1422   if (target_list)
1423     gtk_target_list_ref (target_list);
1424   else
1425     target_list = gtk_target_list_new (NULL, 0);
1426   gtk_target_list_add_text_targets (target_list, 0);
1427   gtk_drag_dest_set_target_list (widget, target_list);
1428   gtk_target_list_unref (target_list);
1429 }
1430
1431 /**
1432  * gtk_drag_dest_add_image_targets:
1433  * @widget: a #GtkWidget that's a drag destination
1434  *
1435  * Add the image targets supported by #GtkSelection to
1436  * the target list of the drag destination. The targets
1437  * are added with @info = 0. If you need another value, 
1438  * use gtk_target_list_add_image_targets() and
1439  * gtk_drag_dest_set_target_list().
1440  * 
1441  * Since: 2.6
1442  **/
1443 void
1444 gtk_drag_dest_add_image_targets (GtkWidget *widget)
1445 {
1446   GtkTargetList *target_list;
1447
1448   target_list = gtk_drag_dest_get_target_list (widget);
1449   if (target_list)
1450     gtk_target_list_ref (target_list);
1451   else
1452     target_list = gtk_target_list_new (NULL, 0);
1453   gtk_target_list_add_image_targets (target_list, 0, FALSE);
1454   gtk_drag_dest_set_target_list (widget, target_list);
1455   gtk_target_list_unref (target_list);
1456 }
1457
1458 /**
1459  * gtk_drag_dest_add_uri_targets:
1460  * @widget: a #GtkWidget that's a drag destination
1461  *
1462  * Add the URI targets supported by #GtkSelection to
1463  * the target list of the drag destination. The targets
1464  * are added with @info = 0. If you need another value, 
1465  * use gtk_target_list_add_uri_targets() and
1466  * gtk_drag_dest_set_target_list().
1467  * 
1468  * Since: 2.6
1469  **/
1470 void
1471 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
1472 {
1473   GtkTargetList *target_list;
1474
1475   target_list = gtk_drag_dest_get_target_list (widget);
1476   if (target_list)
1477     gtk_target_list_ref (target_list);
1478   else
1479     target_list = gtk_target_list_new (NULL, 0);
1480   gtk_target_list_add_uri_targets (target_list, 0);
1481   gtk_drag_dest_set_target_list (widget, target_list);
1482   gtk_target_list_unref (target_list);
1483 }
1484
1485 /**
1486  * gtk_drag_dest_set_track_motion:
1487  * @widget: a #GtkWidget that's a drag destination
1488  * @track_motion: whether to accept all targets
1489  * 
1490  * Tells the widget to emit ::drag-motion and ::drag-leave
1491  * events regardless of the targets and the %GTK_DEST_DEFAULT_MOTION
1492  * flag. 
1493  *
1494  * This may be used when a widget wants to do generic
1495  * actions regardless of the targets that the source offers.
1496  *
1497  * Since: 2.10
1498  **/
1499 void
1500 gtk_drag_dest_set_track_motion (GtkWidget *widget,
1501                                 gboolean   track_motion)
1502 {
1503   GtkDragDestSite *site;
1504
1505   g_return_if_fail (GTK_IS_WIDGET (widget));
1506
1507   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1508   
1509   g_return_if_fail (site != NULL);
1510
1511   site->track_motion = track_motion != FALSE;
1512 }
1513
1514 /**
1515  * gtk_drag_dest_get_track_motion:
1516  * @widget: a #GtkWidget that's a drag destination
1517  * 
1518  * Returns whether the widget has been configured to always
1519  * emit ::drag-motion signals.
1520  * 
1521  * Return Value: %TRUE if the widget always emits ::drag-motion events
1522  *
1523  * Since: 2.10
1524  **/
1525 gboolean
1526 gtk_drag_dest_get_track_motion (GtkWidget *widget)
1527 {
1528   GtkDragDestSite *site;
1529
1530   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1531
1532   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1533
1534   if (site)
1535     return site->track_motion;
1536
1537   return FALSE;
1538 }
1539
1540 /*************************************************************
1541  * _gtk_drag_dest_handle_event:
1542  *     Called from widget event handling code on Drag events
1543  *     for destinations.
1544  *
1545  *   arguments:
1546  *     toplevel: Toplevel widget that received the event
1547  *     event:
1548  *   results:
1549  *************************************************************/
1550
1551 void
1552 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
1553                             GdkEvent  *event)
1554 {
1555   GtkDragDestInfo *info;
1556   GdkDragContext *context;
1557
1558   g_return_if_fail (toplevel != NULL);
1559   g_return_if_fail (event != NULL);
1560
1561   context = event->dnd.context;
1562
1563   info = gtk_drag_get_dest_info (context, TRUE);
1564
1565   /* Find the widget for the event */
1566   switch (event->type)
1567     {
1568     case GDK_DRAG_ENTER:
1569       break;
1570       
1571     case GDK_DRAG_LEAVE:
1572       if (info->widget)
1573         {
1574           gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1575           info->widget = NULL;
1576         }
1577       break;
1578       
1579     case GDK_DRAG_MOTION:
1580     case GDK_DROP_START:
1581       {
1582         GdkWindow *window;
1583         gint tx, ty;
1584         gboolean found;
1585
1586         if (event->type == GDK_DROP_START)
1587           {
1588             info->dropped = TRUE;
1589             /* We send a leave here so that the widget unhighlights
1590              * properly.
1591              */
1592             if (info->widget)
1593               {
1594                 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1595                 info->widget = NULL;
1596               }
1597           }
1598
1599         window = gtk_widget_get_window (toplevel);
1600
1601 #ifdef GDK_WINDOWING_X11
1602         /* Hackaround for: http://bugzilla.gnome.org/show_bug.cgi?id=136112
1603          *
1604          * Currently gdk_window_get_position doesn't provide reliable
1605          * information for embedded windows, so we call the much more
1606          * expensive gdk_window_get_origin().
1607          */
1608         if (GTK_IS_PLUG (toplevel))
1609           gdk_window_get_origin (window, &tx, &ty);
1610         else
1611 #endif /* GDK_WINDOWING_X11 */
1612           gdk_window_get_position (window, &tx, &ty);
1613
1614         found = gtk_drag_find_widget (toplevel,
1615                                       context,
1616                                       info,
1617                                       event->dnd.x_root - tx,
1618                                       event->dnd.y_root - ty,
1619                                       event->dnd.time,
1620                                       (event->type == GDK_DRAG_MOTION) ?
1621                                       gtk_drag_dest_motion :
1622                                       gtk_drag_dest_drop);
1623
1624         if (info->widget && !found)
1625           {
1626             gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1627             info->widget = NULL;
1628           }
1629         
1630         /* Send a reply.
1631          */
1632         if (event->type == GDK_DRAG_MOTION)
1633           {
1634             if (!found)
1635               gdk_drag_status (context, 0, event->dnd.time);
1636           }
1637         else if (event->type == GDK_DROP_START && !info->proxy_source)
1638           {
1639             gdk_drop_reply (context, found, event->dnd.time);
1640             if ((gdk_drag_context_get_protocol (context) == GDK_DRAG_PROTO_MOTIF) && !found)
1641               gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
1642           }
1643       }
1644       break;
1645
1646     default:
1647       g_assert_not_reached ();
1648     }
1649 }
1650
1651 /**
1652  * gtk_drag_dest_find_target:
1653  * @widget: drag destination widget
1654  * @context: drag context
1655  * @target_list: (allow-none): list of droppable targets, or %NULL to use
1656  *    gtk_drag_dest_get_target_list (@widget).
1657  *
1658  * Looks for a match between the supported targets of @context and the
1659  * @dest_target_list, returning the first matching target, otherwise
1660  * returning %GDK_NONE. @dest_target_list should usually be the return
1661  * value from gtk_drag_dest_get_target_list(), but some widgets may
1662  * have different valid targets for different parts of the widget; in
1663  * that case, they will have to implement a drag_motion handler that
1664  * passes the correct target list to this function.
1665  *
1666  * Return value: first target that the source offers and the dest can
1667  *     accept, or %GDK_NONE
1668  **/
1669 GdkAtom
1670 gtk_drag_dest_find_target (GtkWidget      *widget,
1671                            GdkDragContext *context,
1672                            GtkTargetList  *target_list)
1673 {
1674   GList *tmp_target;
1675   GList *tmp_source = NULL;
1676   GtkWidget *source_widget;
1677
1678   g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1679   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1680
1681
1682   source_widget = gtk_drag_get_source_widget (context);
1683
1684   if (target_list == NULL)
1685     target_list = gtk_drag_dest_get_target_list (widget);
1686   
1687   if (target_list == NULL)
1688     return GDK_NONE;
1689   
1690   tmp_target = target_list->list;
1691   while (tmp_target)
1692     {
1693       GtkTargetPair *pair = tmp_target->data;
1694       tmp_source = gdk_drag_context_list_targets (context);
1695       while (tmp_source)
1696         {
1697           if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1698             {
1699               if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1700                   (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)) &&
1701                   (!(pair->flags & GTK_TARGET_OTHER_APP) || !source_widget) &&
1702                   (!(pair->flags & GTK_TARGET_OTHER_WIDGET) || (source_widget != widget)))
1703                 return pair->target;
1704               else
1705                 break;
1706             }
1707           tmp_source = tmp_source->next;
1708         }
1709       tmp_target = tmp_target->next;
1710     }
1711
1712   return GDK_NONE;
1713 }
1714
1715 static void
1716 gtk_drag_selection_received (GtkWidget        *widget,
1717                              GtkSelectionData *selection_data,
1718                              guint             time,
1719                              gpointer          data)
1720 {
1721   GdkDragContext *context;
1722   GtkDragDestInfo *info;
1723   GtkWidget *drop_widget;
1724   GdkAtom target;
1725
1726   drop_widget = data;
1727
1728   context = g_object_get_data (G_OBJECT (widget), "drag-context");
1729   info = gtk_drag_get_dest_info (context, FALSE);
1730
1731   if (info->proxy_data && 
1732       gtk_selection_data_get_target (info->proxy_data) == gtk_selection_data_get_target (selection_data))
1733     {
1734       gtk_selection_data_set (info->proxy_data,
1735                               gtk_selection_data_get_data_type (selection_data),
1736                               gtk_selection_data_get_format (selection_data),
1737                               gtk_selection_data_get_data (selection_data),
1738                               gtk_selection_data_get_length (selection_data));
1739       gtk_main_quit ();
1740       return;
1741     }
1742
1743   target = gtk_selection_data_get_target (selection_data);
1744   if (target == gdk_atom_intern_static_string ("DELETE"))
1745     {
1746       gtk_drag_finish (context, TRUE, FALSE, time);
1747     }
1748   else if ((target == gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS")) ||
1749            (target == gdk_atom_intern_static_string ("XmTRANSFER_FAILURE")))
1750     {
1751       /* Do nothing */
1752     }
1753   else
1754     {
1755       GtkDragDestSite *site;
1756
1757       site = g_object_get_data (G_OBJECT (drop_widget), "gtk-drag-dest");
1758
1759       if (site && site->target_list)
1760         {
1761           guint target_info;
1762
1763           if (gtk_target_list_find (site->target_list, 
1764                                     target,
1765                                     &target_info))
1766             {
1767               if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
1768                   gtk_selection_data_get_length (selection_data) >= 0)
1769                 g_signal_emit_by_name (drop_widget,
1770                                        "drag-data-received",
1771                                        context, info->drop_x, info->drop_y,
1772                                        selection_data,
1773                                        target_info, time);
1774             }
1775         }
1776       else
1777         {
1778           g_signal_emit_by_name (drop_widget,
1779                                  "drag-data-received",
1780                                  context, info->drop_x, info->drop_y,
1781                                  selection_data,
1782                                  0, time);
1783         }
1784       
1785       if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1786         {
1787
1788           gtk_drag_finish (context, 
1789                            (gtk_selection_data_get_length (selection_data) >= 0),
1790                            (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
1791                            time);
1792         }
1793       
1794       g_object_unref (drop_widget);
1795     }
1796
1797   g_signal_handlers_disconnect_by_func (widget,
1798                                         gtk_drag_selection_received,
1799                                         data);
1800   
1801   g_object_set_data (G_OBJECT (widget), I_("drag-context"), NULL);
1802   g_object_unref (context);
1803
1804   gtk_drag_release_ipc_widget (widget);
1805 }
1806
1807 /*************************************************************
1808  * gtk_drag_find_widget:
1809  *     Function used to locate widgets for
1810  *     DRAG_MOTION and DROP_START events.
1811  *************************************************************/
1812
1813 static gboolean
1814 gtk_drag_find_widget (GtkWidget           *widget,
1815                       GdkDragContext      *context,
1816                       GtkDragDestInfo     *info,
1817                       gint                 x,
1818                       gint                 y,
1819                       guint32              time,
1820                       GtkDragDestCallback  callback)
1821 {
1822   if (!gtk_widget_get_mapped (widget) ||
1823       !gtk_widget_get_sensitive (widget))
1824     return FALSE;
1825
1826   /* Get the widget at the pointer coordinates and travel up
1827    * the widget hierarchy from there.
1828    */
1829   widget = _gtk_widget_find_at_coords (gtk_widget_get_window (widget),
1830                                        x, y, &x, &y);
1831   if (!widget)
1832     return FALSE;
1833
1834   while (widget)
1835     {
1836       GtkWidget *parent;
1837       GList *hierarchy = NULL;
1838       gboolean found = FALSE;
1839
1840       if (!gtk_widget_get_mapped (widget) ||
1841           !gtk_widget_get_sensitive (widget))
1842         return FALSE;
1843
1844       /* need to reference the entire hierarchy temporarily in case the
1845        * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
1846        */
1847       for (parent = widget;
1848            parent;
1849            parent = gtk_widget_get_parent (parent))
1850         {
1851           hierarchy = g_list_prepend (hierarchy, g_object_ref (parent));
1852         }
1853
1854       /* If the current widget is registered as a drop site, check to
1855        * emit "drag-motion" to check if we are actually in a drop
1856        * site.
1857        */
1858       if (g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
1859         {
1860           found = callback (widget, context, x, y, time);
1861
1862           /* If so, send a "drag-leave" to the last widget */
1863           if (found)
1864             {
1865               if (info->widget && info->widget != widget)
1866                 {
1867                   gtk_drag_dest_leave (info->widget, context, time);
1868                 }
1869
1870               info->widget = widget;
1871             }
1872         }
1873
1874       if (!found)
1875         {
1876           /* Get the parent before unreffing the hierarchy because
1877            * invoking the callback might have destroyed the widget
1878            */
1879           parent = gtk_widget_get_parent (widget);
1880
1881           /* The parent might be going away when unreffing the
1882            * hierarchy, so also protect againt that
1883            */
1884           if (parent)
1885             g_object_add_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1886         }
1887
1888       g_list_foreach (hierarchy, (GFunc) g_object_unref, NULL);
1889       g_list_free (hierarchy);
1890
1891       if (found)
1892         return TRUE;
1893
1894       if (parent)
1895         g_object_remove_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1896       else
1897         return FALSE;
1898
1899       if (!gtk_widget_translate_coordinates (widget, parent, x, y, &x, &y))
1900         return FALSE;
1901
1902       widget = parent;
1903     }
1904
1905   return FALSE;
1906 }
1907
1908 static void
1909 gtk_drag_proxy_begin (GtkWidget       *widget,
1910                       GtkDragDestInfo *dest_info,
1911                       guint32          time)
1912 {
1913   GtkDragSourceInfo *source_info;
1914   GList *tmp_list;
1915   GdkDragContext *context;
1916   GtkWidget *ipc_widget;
1917
1918   if (dest_info->proxy_source)
1919     {
1920       gdk_drag_abort (dest_info->proxy_source->context, time);
1921       gtk_drag_source_info_destroy (dest_info->proxy_source);
1922       dest_info->proxy_source = NULL;
1923     }
1924   
1925   ipc_widget = gtk_drag_get_ipc_widget (widget);
1926   context = gdk_drag_begin (gtk_widget_get_window (ipc_widget),
1927                             gdk_drag_context_list_targets (dest_info->context));
1928
1929   source_info = gtk_drag_get_source_info (context, TRUE);
1930
1931   source_info->ipc_widget = ipc_widget;
1932   source_info->widget = g_object_ref (widget);
1933
1934   source_info->target_list = gtk_target_list_new (NULL, 0);
1935   tmp_list = gdk_drag_context_list_targets (dest_info->context);
1936   while (tmp_list)
1937     {
1938       gtk_target_list_add (source_info->target_list,
1939                            GDK_POINTER_TO_ATOM (tmp_list->data), 0, 0);
1940       tmp_list = tmp_list->next;
1941     }
1942
1943   source_info->proxy_dest = dest_info;
1944   
1945   g_signal_connect (ipc_widget,
1946                     "selection-get",
1947                     G_CALLBACK (gtk_drag_selection_get),
1948                     source_info);
1949   
1950   dest_info->proxy_source = source_info;
1951 }
1952
1953 static void
1954 gtk_drag_dest_info_destroy (gpointer data)
1955 {
1956   g_slice_free (GtkDragDestInfo, data);
1957 }
1958
1959 static GtkDragDestInfo *
1960 gtk_drag_get_dest_info (GdkDragContext *context,
1961                         gboolean        create)
1962 {
1963   GtkDragDestInfo *info;
1964   static GQuark info_quark = 0;
1965   if (!info_quark)
1966     info_quark = g_quark_from_static_string ("gtk-dest-info");
1967   
1968   info = g_object_get_qdata (G_OBJECT (context), info_quark);
1969   if (!info && create)
1970     {
1971       info = g_slice_new0 (GtkDragDestInfo);
1972       info->context = context;
1973       g_object_set_qdata_full (G_OBJECT (context), info_quark,
1974                                info, gtk_drag_dest_info_destroy);
1975     }
1976
1977   return info;
1978 }
1979
1980 static GQuark dest_info_quark = 0;
1981
1982 static GtkDragSourceInfo *
1983 gtk_drag_get_source_info (GdkDragContext *context,
1984                           gboolean        create)
1985 {
1986   GtkDragSourceInfo *info;
1987   if (!dest_info_quark)
1988     dest_info_quark = g_quark_from_static_string ("gtk-source-info");
1989   
1990   info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
1991   if (!info && create)
1992     {
1993       info = g_new0 (GtkDragSourceInfo, 1);
1994       info->context = context;
1995       g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
1996     }
1997
1998   return info;
1999 }
2000
2001 static void
2002 gtk_drag_clear_source_info (GdkDragContext *context)
2003 {
2004   g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
2005 }
2006
2007 static void
2008 gtk_drag_dest_realized (GtkWidget *widget)
2009 {
2010   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2011
2012   if (gtk_widget_is_toplevel (toplevel))
2013     gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2014 }
2015
2016 static void
2017 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
2018                                  GtkWidget *previous_toplevel)
2019 {
2020   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2021
2022   if (gtk_widget_is_toplevel (toplevel) && gtk_widget_get_realized (toplevel))
2023     gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2024 }
2025
2026 static void
2027 gtk_drag_dest_site_destroy (gpointer data)
2028 {
2029   GtkDragDestSite *site = data;
2030
2031   if (site->proxy_window)
2032     g_object_unref (site->proxy_window);
2033     
2034   if (site->target_list)
2035     gtk_target_list_unref (site->target_list);
2036
2037   g_slice_free (GtkDragDestSite, site);
2038 }
2039
2040 /*
2041  * Default drag handlers
2042  */
2043 static void  
2044 gtk_drag_dest_leave (GtkWidget      *widget,
2045                      GdkDragContext *context,
2046                      guint           time)
2047 {
2048   GtkDragDestSite *site;
2049
2050   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2051   g_return_if_fail (site != NULL);
2052
2053   if (site->do_proxy)
2054     {
2055       GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2056
2057       if (info->proxy_source && info->proxy_source->widget == widget && !info->dropped)
2058         {
2059           gdk_drag_abort (info->proxy_source->context, time);
2060           gtk_drag_source_info_destroy (info->proxy_source);
2061           info->proxy_source = NULL;
2062         }
2063       
2064       return;
2065     }
2066   else
2067     {
2068       if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
2069         gtk_drag_unhighlight (widget);
2070
2071       if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
2072           site->track_motion)
2073         g_signal_emit_by_name (widget, "drag-leave", context, time);
2074       
2075       site->have_drag = FALSE;
2076     }
2077 }
2078
2079 static gboolean
2080 gtk_drag_dest_motion (GtkWidget      *widget,
2081                       GdkDragContext *context,
2082                       gint            x,
2083                       gint            y,
2084                       guint           time)
2085 {
2086   GtkDragDestSite *site;
2087   GdkDragAction action = 0;
2088   gboolean retval;
2089
2090   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2091   g_return_val_if_fail (site != NULL, FALSE);
2092
2093   if (site->do_proxy)
2094     {
2095       GdkAtom selection;
2096       GdkEvent *current_event;
2097       GdkWindow *dest_window;
2098       GdkDragProtocol proto;
2099         
2100       GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2101
2102       if (!info->proxy_source || info->proxy_source->widget != widget)
2103         gtk_drag_proxy_begin (widget, info, time);
2104
2105       current_event = gtk_get_current_event ();
2106
2107       if (site->proxy_window)
2108         {
2109           dest_window = site->proxy_window;
2110           proto = site->proxy_protocol;
2111         }
2112       else
2113         {
2114           gdk_drag_find_window_for_screen (info->proxy_source->context,
2115                                            NULL,
2116                                            gdk_window_get_screen (current_event->dnd.window),
2117                                            current_event->dnd.x_root, 
2118                                            current_event->dnd.y_root,
2119                                            &dest_window, &proto);
2120         }
2121       
2122       gdk_drag_motion (info->proxy_source->context, 
2123                        dest_window, proto,
2124                        current_event->dnd.x_root, 
2125                        current_event->dnd.y_root, 
2126                        gdk_drag_context_get_suggested_action (context),
2127                        gdk_drag_context_get_actions (context),
2128                        time);
2129
2130       if (!site->proxy_window && dest_window)
2131         g_object_unref (dest_window);
2132
2133       selection = gdk_drag_get_selection (info->proxy_source->context);
2134       if (selection && 
2135           selection != gdk_drag_get_selection (info->context))
2136         gtk_drag_source_check_selection (info->proxy_source, selection, time);
2137
2138       gdk_event_free (current_event);
2139       
2140       return TRUE;
2141     }
2142
2143   if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
2144     {
2145       if (gdk_drag_context_get_suggested_action (context) & site->actions)
2146         action = gdk_drag_context_get_suggested_action (context);
2147       else
2148         {
2149           gint i;
2150           
2151           for (i = 0; i < 8; i++)
2152             {
2153               if ((site->actions & (1 << i)) &&
2154                   (gdk_drag_context_get_actions (context) & (1 << i)))
2155                 {
2156                   action = (1 << i);
2157                   break;
2158                 }
2159             }
2160         }
2161
2162       if (action && gtk_drag_dest_find_target (widget, context, NULL))
2163         {
2164           if (!site->have_drag)
2165             {
2166               site->have_drag = TRUE;
2167               if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
2168                 gtk_drag_highlight (widget);
2169             }
2170
2171           gdk_drag_status (context, action, time);
2172         }
2173       else
2174         {
2175           gdk_drag_status (context, 0, time);
2176           if (!site->track_motion)
2177             return TRUE;
2178         }
2179     }
2180
2181   g_signal_emit_by_name (widget, "drag-motion",
2182                          context, x, y, time, &retval);
2183
2184   return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
2185 }
2186
2187 static gboolean
2188 gtk_drag_dest_drop (GtkWidget        *widget,
2189                     GdkDragContext   *context,
2190                     gint              x,
2191                     gint              y,
2192                     guint             time)
2193 {
2194   GtkDragDestSite *site;
2195   GtkDragDestInfo *info;
2196
2197   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2198   g_return_val_if_fail (site != NULL, FALSE);
2199
2200   info = gtk_drag_get_dest_info (context, FALSE);
2201   g_return_val_if_fail (info != NULL, FALSE);
2202
2203   info->drop_x = x;
2204   info->drop_y = y;
2205
2206   if (site->do_proxy)
2207     {
2208       if (info->proxy_source || 
2209           (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_ROOTWIN))
2210         {
2211           gtk_drag_drop (info->proxy_source, time);
2212         }
2213       else
2214         {
2215           /* We need to synthesize a motion event, wait for a status,
2216            * and, if we get a good one, do a drop.
2217            */
2218           
2219           GdkEvent *current_event;
2220           GdkAtom selection;
2221           GdkWindow *dest_window;
2222           GdkDragProtocol proto;
2223           
2224           gtk_drag_proxy_begin (widget, info, time);
2225           info->proxy_drop_wait = TRUE;
2226           info->proxy_drop_time = time;
2227           
2228           current_event = gtk_get_current_event ();
2229
2230           if (site->proxy_window)
2231             {
2232               dest_window = site->proxy_window;
2233               proto = site->proxy_protocol;
2234             }
2235           else
2236             {
2237               gdk_drag_find_window_for_screen (info->proxy_source->context,
2238                                                NULL,
2239                                                gdk_window_get_screen (current_event->dnd.window),
2240                                                current_event->dnd.x_root, 
2241                                                current_event->dnd.y_root,
2242                                                &dest_window, &proto);
2243             }
2244
2245           gdk_drag_motion (info->proxy_source->context, 
2246                            dest_window, proto,
2247                            current_event->dnd.x_root, 
2248                            current_event->dnd.y_root, 
2249                            gdk_drag_context_get_suggested_action (context),
2250                            gdk_drag_context_get_actions (context),
2251                            time);
2252
2253           if (!site->proxy_window && dest_window)
2254             g_object_unref (dest_window);
2255
2256           selection = gdk_drag_get_selection (info->proxy_source->context);
2257           if (selection && 
2258               selection != gdk_drag_get_selection (info->context))
2259             gtk_drag_source_check_selection (info->proxy_source, selection, time);
2260
2261           gdk_event_free (current_event);
2262         }
2263
2264       return TRUE;
2265     }
2266   else
2267     {
2268       gboolean retval;
2269
2270       if (site->flags & GTK_DEST_DEFAULT_DROP)
2271         {
2272           GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
2273
2274           if (target == GDK_NONE)
2275             {
2276               gtk_drag_finish (context, FALSE, FALSE, time);
2277               return TRUE;
2278             }
2279           else 
2280             gtk_drag_get_data (widget, context, target, time);
2281         }
2282
2283       g_signal_emit_by_name (widget, "drag-drop",
2284                              context, x, y, time, &retval);
2285
2286       return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
2287     }
2288 }
2289
2290 /***************
2291  * Source side *
2292  ***************/
2293
2294 /* Like GtkDragBegin, but also takes a GtkDragSourceSite,
2295  * so that we can set the icon from the source site information
2296  */
2297 static GdkDragContext *
2298 gtk_drag_begin_internal (GtkWidget         *widget,
2299                          GtkDragSourceSite *site,
2300                          GtkTargetList     *target_list,
2301                          GdkDragAction      actions,
2302                          gint               button,
2303                          GdkEvent          *event)
2304 {
2305   GtkDragSourceInfo *info;
2306   GList *targets = NULL;
2307   GList *tmp_list;
2308   guint32 time = GDK_CURRENT_TIME;
2309   GdkDragAction possible_actions, suggested_action;
2310   GdkDragContext *context;
2311   GtkWidget *ipc_widget;
2312   GdkCursor *cursor;
2313   GdkDevice *pointer, *keyboard;
2314   GdkWindow *ipc_window;
2315
2316   pointer = keyboard = NULL;
2317   ipc_widget = gtk_drag_get_ipc_widget (widget);
2318   
2319   gtk_drag_get_event_actions (event, button, actions,
2320                               &suggested_action, &possible_actions);
2321   
2322   cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget), 
2323                                 suggested_action,
2324                                 NULL);
2325   
2326   if (event)
2327     {
2328       time = gdk_event_get_time (event);
2329       if (time == GDK_CURRENT_TIME)
2330         time = gtk_get_current_event_time ();
2331
2332       pointer = gdk_event_get_device (event);
2333
2334       if (gdk_device_get_source (pointer) == GDK_SOURCE_KEYBOARD)
2335         {
2336           keyboard = pointer;
2337           pointer = gdk_device_get_associated_device (keyboard);
2338         }
2339       else
2340         keyboard = gdk_device_get_associated_device (pointer);
2341     }
2342   else
2343     {
2344       GdkDeviceManager *device_manager;
2345
2346       device_manager = gdk_display_get_device_manager (gtk_widget_get_display (widget));
2347       pointer = gdk_device_manager_get_client_pointer (device_manager);
2348       keyboard = gdk_device_get_associated_device (pointer);
2349     }
2350
2351   if (!pointer)
2352     return NULL;
2353
2354   ipc_window = gtk_widget_get_window (ipc_widget);
2355
2356   if (gdk_device_grab (pointer, ipc_window,
2357                        GDK_OWNERSHIP_APPLICATION, FALSE,
2358                        GDK_POINTER_MOTION_MASK |
2359                        GDK_BUTTON_RELEASE_MASK,
2360                        cursor, time) != GDK_GRAB_SUCCESS)
2361     {
2362       gtk_drag_release_ipc_widget (ipc_widget);
2363       return NULL;
2364     }
2365
2366   if (keyboard)
2367     grab_dnd_keys (ipc_widget, keyboard, time);
2368
2369   /* We use a GTK grab here to override any grabs that the widget
2370    * we are dragging from might have held
2371    */
2372   gtk_device_grab_add (ipc_widget, pointer, FALSE);
2373
2374   tmp_list = g_list_last (target_list->list);
2375   while (tmp_list)
2376     {
2377       GtkTargetPair *pair = tmp_list->data;
2378       targets = g_list_prepend (targets, 
2379                                 GINT_TO_POINTER (pair->target));
2380       tmp_list = tmp_list->prev;
2381     }
2382
2383   source_widgets = g_slist_prepend (source_widgets, ipc_widget);
2384
2385   context = gdk_drag_begin (ipc_window, targets);
2386   gdk_drag_context_set_device (context, pointer);
2387   g_list_free (targets);
2388   
2389   info = gtk_drag_get_source_info (context, TRUE);
2390   
2391   info->ipc_widget = ipc_widget;
2392   g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), info);
2393
2394   info->widget = g_object_ref (widget);
2395   
2396   info->button = button;
2397   info->cursor = cursor;
2398   info->target_list = target_list;
2399   gtk_target_list_ref (target_list);
2400
2401   info->possible_actions = actions;
2402
2403   info->status = GTK_DRAG_STATUS_DRAG;
2404   info->last_event = NULL;
2405   info->selections = NULL;
2406   info->icon_window = NULL;
2407   info->destroy_icon = FALSE;
2408
2409   /* Set cur_x, cur_y here so if the "drag-begin" signal shows
2410    * the drag icon, it will be in the right place
2411    */
2412   if (event && event->type == GDK_MOTION_NOTIFY)
2413     {
2414       info->cur_screen = gtk_widget_get_screen (widget);
2415       info->cur_x = event->motion.x_root;
2416       info->cur_y = event->motion.y_root;
2417     }
2418   else
2419     {
2420       gdk_display_get_device_state (gtk_widget_get_display (widget), pointer,
2421                                     &info->cur_screen, &info->cur_x, &info->cur_y, NULL);
2422     }
2423
2424   g_signal_emit_by_name (widget, "drag-begin", info->context);
2425
2426   /* Ensure that we have an icon before we start the drag; the
2427    * application may have set one in ::drag_begin, or it may
2428    * not have set one.
2429    */
2430   if (!info->icon_window && !info->icon_pixbuf)
2431     {
2432       if (!site || site->icon_type == GTK_IMAGE_EMPTY)
2433         gtk_drag_set_icon_default (context);
2434       else
2435         switch (site->icon_type)
2436           {
2437           case GTK_IMAGE_PIXBUF:
2438             gtk_drag_set_icon_pixbuf (context,
2439                                       site->icon_data.pixbuf.pixbuf,
2440                                       -2, -2);
2441             break;
2442           case GTK_IMAGE_STOCK:
2443             gtk_drag_set_icon_stock (context,
2444                                      site->icon_data.stock.stock_id,
2445                                      -2, -2);
2446             break;
2447           case GTK_IMAGE_ICON_NAME:
2448             gtk_drag_set_icon_name (context,
2449                                     site->icon_data.name.icon_name,
2450                                     -2, -2);
2451             break;
2452           case GTK_IMAGE_EMPTY:
2453           default:
2454             g_assert_not_reached();
2455             break;
2456           }
2457     }
2458
2459   /* We need to composite the icon into the cursor, if we are
2460    * not using an icon window.
2461    */
2462   if (info->icon_pixbuf)  
2463     {
2464       cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget), 
2465                                     suggested_action,
2466                                     info);
2467   
2468       if (cursor != info->cursor)
2469         {
2470           gdk_device_grab (pointer, gtk_widget_get_window (widget),
2471                            GDK_OWNERSHIP_APPLICATION, FALSE,
2472                            GDK_POINTER_MOTION_MASK |
2473                            GDK_BUTTON_RELEASE_MASK,
2474                            cursor, time);
2475           info->cursor = cursor;
2476         }
2477     }
2478     
2479   if (event && event->type == GDK_MOTION_NOTIFY)
2480     gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);
2481   else
2482     gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, event);
2483
2484   info->start_x = info->cur_x;
2485   info->start_y = info->cur_y;
2486
2487   g_signal_connect (info->ipc_widget, "grab-broken-event",
2488                     G_CALLBACK (gtk_drag_grab_broken_event_cb), info);
2489   g_signal_connect (info->ipc_widget, "grab-notify",
2490                     G_CALLBACK (gtk_drag_grab_notify_cb), info);
2491   g_signal_connect (info->ipc_widget, "button-release-event",
2492                     G_CALLBACK (gtk_drag_button_release_cb), info);
2493   g_signal_connect (info->ipc_widget, "motion-notify-event",
2494                     G_CALLBACK (gtk_drag_motion_cb), info);
2495   g_signal_connect (info->ipc_widget, "key-press-event",
2496                     G_CALLBACK (gtk_drag_key_cb), info);
2497   g_signal_connect (info->ipc_widget, "key-release-event",
2498                     G_CALLBACK (gtk_drag_key_cb), info);
2499   g_signal_connect (info->ipc_widget, "selection-get",
2500                     G_CALLBACK (gtk_drag_selection_get), info);
2501
2502   info->have_grab = TRUE;
2503   info->grab_time = time;
2504
2505   return info->context;
2506 }
2507
2508 /**
2509  * gtk_drag_begin:
2510  * @widget: the source widget.
2511  * @targets: The targets (data formats) in which the
2512  *    source can provide the data.
2513  * @actions: A bitmask of the allowed drag actions for this drag.
2514  * @button: The button the user clicked to start the drag.
2515  * @event: The event that triggered the start of the drag.
2516  *
2517  * Initiates a drag on the source side. The function
2518  * only needs to be used when the application is
2519  * starting drags itself, and is not needed when
2520  * gtk_drag_source_set() is used.
2521  *
2522  * The @event is used to retrieve the timestamp that will be used internally to
2523  * grab the pointer.  If @event is #NULL, then GDK_CURRENT_TIME will be used.
2524  * However, you should try to pass a real event in all cases, since that can be
2525  * used by GTK+ to get information about the start position of the drag, for
2526  * example if the @event is a GDK_MOTION_NOTIFY.
2527  *
2528  * Generally there are three cases when you want to start a drag by hand by
2529  * calling this function:
2530  *
2531  * 1. During a button-press-event handler, if you want to start a drag
2532  * immediately when the user presses the mouse button.  Pass the @event
2533  * that you have in your button-press-event handler.
2534  *
2535  * 2. During a motion-notify-event handler, if you want to start a drag
2536  * when the mouse moves past a certain threshold distance after a button-press.
2537  * Pass the @event that you have in your motion-notify-event handler.
2538  *
2539  * 3. During a timeout handler, if you want to start a drag after the mouse
2540  * button is held down for some time.  Try to save the last event that you got
2541  * from the mouse, using gdk_event_copy(), and pass it to this function
2542  * (remember to free the event with gdk_event_free() when you are done).
2543  * If you can really not pass a real event, pass #NULL instead.
2544  *
2545  * Return value: the context for this drag.
2546  **/
2547 GdkDragContext *
2548 gtk_drag_begin (GtkWidget         *widget,
2549                 GtkTargetList     *targets,
2550                 GdkDragAction      actions,
2551                 gint               button,
2552                 GdkEvent          *event)
2553 {
2554   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2555   g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
2556   g_return_val_if_fail (targets != NULL, NULL);
2557
2558   return gtk_drag_begin_internal (widget, NULL, targets,
2559                                   actions, button, event);
2560 }
2561
2562 /**
2563  * gtk_drag_source_set:
2564  * @widget: a #GtkWidget
2565  * @start_button_mask: the bitmask of buttons that can start the drag
2566  * @targets: (allow-none) (array length=n_targets): the table of targets that the drag will support,
2567  *     may be %NULL
2568  * @n_targets: the number of items in @targets
2569  * @actions: the bitmask of possible actions for a drag from this widget
2570  *
2571  * Sets up a widget so that GTK+ will start a drag operation when the user
2572  * clicks and drags on the widget. The widget must have a window.
2573  */
2574 void
2575 gtk_drag_source_set (GtkWidget            *widget,
2576                      GdkModifierType       start_button_mask,
2577                      const GtkTargetEntry *targets,
2578                      gint                  n_targets,
2579                      GdkDragAction         actions)
2580 {
2581   GtkDragSourceSite *site;
2582
2583   g_return_if_fail (GTK_IS_WIDGET (widget));
2584
2585   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2586
2587   gtk_widget_add_events (widget,
2588                          gtk_widget_get_events (widget) |
2589                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2590                          GDK_BUTTON_MOTION_MASK);
2591
2592   if (site)
2593     {
2594       if (site->target_list)
2595         gtk_target_list_unref (site->target_list);
2596     }
2597   else
2598     {
2599       site = g_slice_new0 (GtkDragSourceSite);
2600
2601       site->icon_type = GTK_IMAGE_EMPTY;
2602       
2603       g_signal_connect (widget, "button-press-event",
2604                         G_CALLBACK (gtk_drag_source_event_cb),
2605                         site);
2606       g_signal_connect (widget, "button-release-event",
2607                         G_CALLBACK (gtk_drag_source_event_cb),
2608                         site);
2609       g_signal_connect (widget, "motion-notify-event",
2610                         G_CALLBACK (gtk_drag_source_event_cb),
2611                         site);
2612       
2613       g_object_set_data_full (G_OBJECT (widget),
2614                               I_("gtk-site-data"), 
2615                               site, gtk_drag_source_site_destroy);
2616     }
2617
2618   site->start_button_mask = start_button_mask;
2619
2620   site->target_list = gtk_target_list_new (targets, n_targets);
2621
2622   site->actions = actions;
2623 }
2624
2625 /*************************************************************
2626  * gtk_drag_source_unset
2627  *     Unregister this widget as a drag source.
2628  *   arguments:
2629  *     widget:
2630  *   results:
2631  *************************************************************/
2632
2633 void 
2634 gtk_drag_source_unset (GtkWidget *widget)
2635 {
2636   GtkDragSourceSite *site;
2637
2638   g_return_if_fail (GTK_IS_WIDGET (widget));
2639
2640   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2641
2642   if (site)
2643     {
2644       g_signal_handlers_disconnect_by_func (widget,
2645                                             gtk_drag_source_event_cb,
2646                                             site);
2647       g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
2648     }
2649 }
2650
2651 /**
2652  * gtk_drag_source_get_target_list:
2653  * @widget: a #GtkWidget
2654  *
2655  * Gets the list of targets this widget can provide for
2656  * drag-and-drop.
2657  *
2658  * Return value: the #GtkTargetList, or %NULL if none
2659  *
2660  * Since: 2.4
2661  **/
2662 GtkTargetList *
2663 gtk_drag_source_get_target_list (GtkWidget *widget)
2664 {
2665   GtkDragSourceSite *site;
2666
2667   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2668
2669   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2670
2671   return site ? site->target_list : NULL;
2672 }
2673
2674 /**
2675  * gtk_drag_source_set_target_list:
2676  * @widget: a #GtkWidget that's a drag source
2677  * @target_list: (allow-none): list of draggable targets, or %NULL for none
2678  *
2679  * Changes the target types that this widget offers for drag-and-drop.
2680  * The widget must first be made into a drag source with
2681  * gtk_drag_source_set().
2682  *
2683  * Since: 2.4
2684  **/
2685 void
2686 gtk_drag_source_set_target_list (GtkWidget     *widget,
2687                                  GtkTargetList *target_list)
2688 {
2689   GtkDragSourceSite *site;
2690
2691   g_return_if_fail (GTK_IS_WIDGET (widget));
2692
2693   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2694   if (site == NULL)
2695     {
2696       g_warning ("gtk_drag_source_set_target_list() requires the widget "
2697                  "to already be a drag source.");
2698       return;
2699     }
2700
2701   if (target_list)
2702     gtk_target_list_ref (target_list);
2703
2704   if (site->target_list)
2705     gtk_target_list_unref (site->target_list);
2706
2707   site->target_list = target_list;
2708 }
2709
2710 /**
2711  * gtk_drag_source_add_text_targets:
2712  * @widget: a #GtkWidget that's is a drag source
2713  *
2714  * Add the text targets supported by #GtkSelection to
2715  * the target list of the drag source.  The targets
2716  * are added with @info = 0. If you need another value, 
2717  * use gtk_target_list_add_text_targets() and
2718  * gtk_drag_source_set_target_list().
2719  * 
2720  * Since: 2.6
2721  **/
2722 void
2723 gtk_drag_source_add_text_targets (GtkWidget *widget)
2724 {
2725   GtkTargetList *target_list;
2726
2727   target_list = gtk_drag_source_get_target_list (widget);
2728   if (target_list)
2729     gtk_target_list_ref (target_list);
2730   else
2731     target_list = gtk_target_list_new (NULL, 0);
2732   gtk_target_list_add_text_targets (target_list, 0);
2733   gtk_drag_source_set_target_list (widget, target_list);
2734   gtk_target_list_unref (target_list);
2735 }
2736
2737 /**
2738  * gtk_drag_source_add_image_targets:
2739  * @widget: a #GtkWidget that's is a drag source
2740  *
2741  * Add the writable image targets supported by #GtkSelection to
2742  * the target list of the drag source. The targets
2743  * are added with @info = 0. If you need another value, 
2744  * use gtk_target_list_add_image_targets() and
2745  * gtk_drag_source_set_target_list().
2746  * 
2747  * Since: 2.6
2748  **/
2749 void
2750 gtk_drag_source_add_image_targets (GtkWidget *widget)
2751 {
2752   GtkTargetList *target_list;
2753
2754   target_list = gtk_drag_source_get_target_list (widget);
2755   if (target_list)
2756     gtk_target_list_ref (target_list);
2757   else
2758     target_list = gtk_target_list_new (NULL, 0);
2759   gtk_target_list_add_image_targets (target_list, 0, TRUE);
2760   gtk_drag_source_set_target_list (widget, target_list);
2761   gtk_target_list_unref (target_list);
2762 }
2763
2764 /**
2765  * gtk_drag_source_add_uri_targets:
2766  * @widget: a #GtkWidget that's is a drag source
2767  *
2768  * Add the URI targets supported by #GtkSelection to
2769  * the target list of the drag source.  The targets
2770  * are added with @info = 0. If you need another value, 
2771  * use gtk_target_list_add_uri_targets() and
2772  * gtk_drag_source_set_target_list().
2773  * 
2774  * Since: 2.6
2775  **/
2776 void
2777 gtk_drag_source_add_uri_targets (GtkWidget *widget)
2778 {
2779   GtkTargetList *target_list;
2780
2781   target_list = gtk_drag_source_get_target_list (widget);
2782   if (target_list)
2783     gtk_target_list_ref (target_list);
2784   else
2785     target_list = gtk_target_list_new (NULL, 0);
2786   gtk_target_list_add_uri_targets (target_list, 0);
2787   gtk_drag_source_set_target_list (widget, target_list);
2788   gtk_target_list_unref (target_list);
2789 }
2790
2791 static void
2792 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
2793 {
2794   switch (site->icon_type)
2795     {
2796     case GTK_IMAGE_EMPTY:
2797       break;
2798     case GTK_IMAGE_PIXBUF:
2799       g_object_unref (site->icon_data.pixbuf.pixbuf);
2800       break;
2801     case GTK_IMAGE_STOCK:
2802       g_free (site->icon_data.stock.stock_id);
2803       break;
2804     case GTK_IMAGE_ICON_NAME:
2805       g_free (site->icon_data.name.icon_name);
2806       break;
2807     default:
2808       g_assert_not_reached();
2809       break;
2810     }
2811   site->icon_type = GTK_IMAGE_EMPTY;
2812 }
2813
2814 /**
2815  * gtk_drag_source_set_icon_pixbuf:
2816  * @widget: a #GtkWidget
2817  * @pixbuf: the #GdkPixbuf for the drag icon
2818  * 
2819  * Sets the icon that will be used for drags from a particular widget
2820  * from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will 
2821  * release it when it is no longer needed.
2822  **/
2823 void 
2824 gtk_drag_source_set_icon_pixbuf (GtkWidget   *widget,
2825                                  GdkPixbuf   *pixbuf)
2826 {
2827   GtkDragSourceSite *site;
2828
2829   g_return_if_fail (GTK_IS_WIDGET (widget));
2830   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2831
2832   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2833   g_return_if_fail (site != NULL); 
2834   g_object_ref (pixbuf);
2835
2836   gtk_drag_source_unset_icon (site);
2837
2838   site->icon_type = GTK_IMAGE_PIXBUF;
2839   site->icon_data.pixbuf.pixbuf = pixbuf;
2840 }
2841
2842 /**
2843  * gtk_drag_source_set_icon_stock:
2844  * @widget: a #GtkWidget
2845  * @stock_id: the ID of the stock icon to use
2846  *
2847  * Sets the icon that will be used for drags from a particular source
2848  * to a stock icon. 
2849  **/
2850 void 
2851 gtk_drag_source_set_icon_stock (GtkWidget   *widget,
2852                                 const gchar *stock_id)
2853 {
2854   GtkDragSourceSite *site;
2855
2856   g_return_if_fail (GTK_IS_WIDGET (widget));
2857   g_return_if_fail (stock_id != NULL);
2858
2859   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2860   g_return_if_fail (site != NULL);
2861   
2862   gtk_drag_source_unset_icon (site);
2863
2864   site->icon_type = GTK_IMAGE_STOCK;
2865   site->icon_data.stock.stock_id = g_strdup (stock_id);
2866 }
2867
2868 /**
2869  * gtk_drag_source_set_icon_name:
2870  * @widget: a #GtkWidget
2871  * @icon_name: name of icon to use
2872  * 
2873  * Sets the icon that will be used for drags from a particular source
2874  * to a themed icon. See the docs for #GtkIconTheme for more details.
2875  *
2876  * Since: 2.8
2877  **/
2878 void 
2879 gtk_drag_source_set_icon_name (GtkWidget   *widget,
2880                                const gchar *icon_name)
2881 {
2882   GtkDragSourceSite *site;
2883
2884   g_return_if_fail (GTK_IS_WIDGET (widget));
2885   g_return_if_fail (icon_name != NULL);
2886
2887   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2888   g_return_if_fail (site != NULL);
2889
2890   gtk_drag_source_unset_icon (site);
2891
2892   site->icon_type = GTK_IMAGE_ICON_NAME;
2893   site->icon_data.name.icon_name = g_strdup (icon_name);
2894 }
2895
2896 static void
2897 gtk_drag_get_icon (GtkDragSourceInfo *info,
2898                    GtkWidget        **icon_window,
2899                    gint              *hot_x,
2900                    gint              *hot_y)
2901 {
2902   if (get_can_change_screen (info->icon_window))
2903     gtk_window_set_screen (GTK_WINDOW (info->icon_window),
2904                            info->cur_screen);
2905       
2906   if (gtk_widget_get_screen (info->icon_window) != info->cur_screen)
2907     {
2908       if (!info->fallback_icon)
2909         {
2910           gint save_hot_x, save_hot_y;
2911           gboolean save_destroy_icon;
2912           GtkWidget *save_icon_window;
2913           
2914           /* HACK to get the appropriate icon
2915            */
2916           save_icon_window = info->icon_window;
2917           save_hot_x = info->hot_x;
2918           save_hot_y = info->hot_x;
2919           save_destroy_icon = info->destroy_icon;
2920
2921           info->icon_window = NULL;
2922           set_icon_stock_pixbuf (info->context, 
2923                                  GTK_STOCK_DND, NULL, -2, -2, TRUE);
2924           info->fallback_icon = info->icon_window;
2925           
2926           info->icon_window = save_icon_window;
2927           info->hot_x = save_hot_x;
2928           info->hot_y = save_hot_y;
2929           info->destroy_icon = save_destroy_icon;
2930         }
2931       
2932       gtk_widget_hide (info->icon_window);
2933       
2934       *icon_window = info->fallback_icon;
2935       gtk_window_set_screen (GTK_WINDOW (*icon_window), info->cur_screen);
2936       
2937       *hot_x = -2;
2938       *hot_y = -2;
2939     }
2940   else
2941     {
2942       if (info->fallback_icon)
2943         gtk_widget_hide (info->fallback_icon);
2944       
2945       *icon_window = info->icon_window;
2946       *hot_x = info->hot_x;
2947       *hot_y = info->hot_y;
2948     }
2949 }
2950
2951 static void
2952 gtk_drag_update_icon (GtkDragSourceInfo *info)
2953 {
2954   if (info->icon_window)
2955     {
2956       GtkWidget *icon_window;
2957       gint hot_x, hot_y;
2958   
2959       gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
2960       
2961       gtk_window_move (GTK_WINDOW (icon_window), 
2962                        info->cur_x - hot_x, 
2963                        info->cur_y - hot_y);
2964
2965       if (gtk_widget_get_visible (icon_window))
2966         gdk_window_raise (gtk_widget_get_window (icon_window));
2967       else
2968         gtk_widget_show (icon_window);
2969     }
2970 }
2971
2972 static void 
2973 gtk_drag_set_icon_window (GdkDragContext *context,
2974                           GtkWidget      *widget,
2975                           gint            hot_x,
2976                           gint            hot_y,
2977                           gboolean        destroy_on_release)
2978 {
2979   GtkDragSourceInfo *info;
2980
2981   info = gtk_drag_get_source_info (context, FALSE);
2982   if (info == NULL)
2983     {
2984       if (destroy_on_release)
2985         gtk_widget_destroy (widget);
2986       return;
2987     }
2988
2989   gtk_drag_remove_icon (info);
2990
2991   if (widget)
2992     g_object_ref (widget);  
2993   
2994   info->icon_window = widget;
2995   info->hot_x = hot_x;
2996   info->hot_y = hot_y;
2997   info->destroy_icon = destroy_on_release;
2998
2999   if (widget && info->icon_pixbuf)
3000     {
3001       g_object_unref (info->icon_pixbuf);
3002       info->icon_pixbuf = NULL;
3003     }
3004
3005   gtk_drag_update_cursor (info);
3006   gtk_drag_update_icon (info);
3007 }
3008
3009 /**
3010  * gtk_drag_set_icon_widget:
3011  * @context: the context for a drag. (This must be called 
3012           with a  context for the source side of a drag)
3013  * @widget: a toplevel window to use as an icon.
3014  * @hot_x: the X offset within @widget of the hotspot.
3015  * @hot_y: the Y offset within @widget of the hotspot.
3016  * 
3017  * Changes the icon for a widget to a given widget. GTK+
3018  * will not destroy the icon, so if you don't want
3019  * it to persist, you should connect to the "drag-end" 
3020  * signal and destroy it yourself.
3021  **/
3022 void 
3023 gtk_drag_set_icon_widget (GdkDragContext    *context,
3024                           GtkWidget         *widget,
3025                           gint               hot_x,
3026                           gint               hot_y)
3027 {
3028   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3029   g_return_if_fail (GTK_IS_WIDGET (widget));
3030
3031   gtk_drag_set_icon_window (context, widget, hot_x, hot_y, FALSE);
3032 }
3033
3034 static void
3035 icon_window_realize (GtkWidget *window,
3036                      GdkPixbuf *pixbuf)
3037 {
3038   cairo_surface_t *surface;
3039   cairo_pattern_t *pattern;
3040   cairo_t *cr;
3041
3042   surface = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3043                                                CAIRO_CONTENT_COLOR,
3044                                                gdk_pixbuf_get_width (pixbuf),
3045                                                gdk_pixbuf_get_height (pixbuf));
3046
3047   cr = cairo_create (surface);
3048   cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3049   gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3050   cairo_paint (cr);
3051   cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3052   cairo_paint (cr);
3053   cairo_pop_group_to_source (cr);
3054   cairo_paint (cr);
3055   cairo_destroy (cr);
3056
3057   pattern = cairo_pattern_create_for_surface (surface);
3058   gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3059   cairo_pattern_destroy (pattern);
3060
3061   cairo_surface_destroy (surface);
3062
3063   if (gdk_pixbuf_get_has_alpha (pixbuf))
3064     {
3065       cairo_region_t *region;
3066
3067       surface = cairo_image_surface_create (CAIRO_FORMAT_A1,
3068                                             gdk_pixbuf_get_width (pixbuf),
3069                                             gdk_pixbuf_get_height (pixbuf));
3070       
3071       cr = cairo_create (surface);
3072       gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3073       cairo_paint (cr);
3074       cairo_destroy (cr);
3075
3076       region = gdk_cairo_region_create_from_surface (surface);
3077       gtk_widget_shape_combine_region (window, region);
3078       cairo_region_destroy (region);
3079
3080       cairo_surface_destroy (surface);
3081     }
3082 }
3083
3084 static void
3085 set_icon_stock_pixbuf (GdkDragContext    *context,
3086                        const gchar       *stock_id,
3087                        GdkPixbuf         *pixbuf,
3088                        gint               hot_x,
3089                        gint               hot_y,
3090                        gboolean           force_window)
3091 {
3092   GtkWidget *window;
3093   gint width, height;
3094   GdkScreen *screen;
3095   GdkDisplay *display;
3096
3097   g_return_if_fail (context != NULL);
3098   g_return_if_fail (pixbuf != NULL || stock_id != NULL);
3099   g_return_if_fail (pixbuf == NULL || stock_id == NULL);
3100
3101   screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3102
3103   window = gtk_window_new (GTK_WINDOW_POPUP);
3104   gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3105   gtk_window_set_screen (GTK_WINDOW (window), screen);
3106   set_can_change_screen (window, TRUE);
3107
3108   gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3109   gtk_widget_set_app_paintable (window, TRUE);
3110
3111   if (stock_id)
3112     {
3113       pixbuf = gtk_widget_render_icon_pixbuf (window, stock_id,
3114                                               GTK_ICON_SIZE_DND);
3115
3116       if (!pixbuf)
3117         {
3118           g_warning ("Cannot load drag icon from stock_id %s", stock_id);
3119           gtk_widget_destroy (window);
3120           return;
3121         }
3122
3123     }
3124   else
3125     g_object_ref (pixbuf);
3126
3127   display = gdk_window_get_display (gdk_drag_context_get_source_window (context));
3128   width = gdk_pixbuf_get_width (pixbuf);
3129   height = gdk_pixbuf_get_height (pixbuf);
3130
3131   if (!force_window &&
3132       gtk_drag_can_use_rgba_cursor (display, width + 2, height + 2))
3133     {
3134       GtkDragSourceInfo *info;
3135
3136       gtk_widget_destroy (window);
3137
3138       info = gtk_drag_get_source_info (context, FALSE);
3139
3140       if (info->icon_pixbuf)
3141         g_object_unref (info->icon_pixbuf);
3142       info->icon_pixbuf = pixbuf;
3143
3144       gtk_drag_set_icon_window (context, NULL, hot_x, hot_y, TRUE);
3145     }
3146   else
3147     {
3148       gtk_widget_set_size_request (window, width, height);
3149
3150       g_signal_connect_closure (window, "realize",
3151                                 g_cclosure_new (G_CALLBACK (icon_window_realize),
3152                                                 pixbuf,
3153                                                 (GClosureNotify)g_object_unref),
3154                                 FALSE);
3155                     
3156       gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3157    }
3158 }
3159
3160 /**
3161  * gtk_drag_set_icon_pixbuf:
3162  * @context: the context for a drag. (This must be called 
3163  *            with a  context for the source side of a drag)
3164  * @pixbuf: the #GdkPixbuf to use as the drag icon.
3165  * @hot_x: the X offset within @widget of the hotspot.
3166  * @hot_y: the Y offset within @widget of the hotspot.
3167  * 
3168  * Sets @pixbuf as the icon for a given drag.
3169  **/
3170 void 
3171 gtk_drag_set_icon_pixbuf  (GdkDragContext *context,
3172                            GdkPixbuf      *pixbuf,
3173                            gint            hot_x,
3174                            gint            hot_y)
3175 {
3176   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3177   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3178
3179   set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3180 }
3181
3182 /**
3183  * gtk_drag_set_icon_stock:
3184  * @context: the context for a drag. (This must be called 
3185  *            with a  context for the source side of a drag)
3186  * @stock_id: the ID of the stock icon to use for the drag.
3187  * @hot_x: the X offset within the icon of the hotspot.
3188  * @hot_y: the Y offset within the icon of the hotspot.
3189  * 
3190  * Sets the icon for a given drag from a stock ID.
3191  **/
3192 void 
3193 gtk_drag_set_icon_stock  (GdkDragContext *context,
3194                           const gchar    *stock_id,
3195                           gint            hot_x,
3196                           gint            hot_y)
3197 {
3198   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3199   g_return_if_fail (stock_id != NULL);
3200   
3201   set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y, FALSE);
3202 }
3203
3204 /* XXX: This function is in gdk, too. Should it be in Cairo? */
3205 static gboolean
3206 _gtk_cairo_surface_extents (cairo_surface_t *surface,
3207                             GdkRectangle *extents)
3208 {
3209   double x1, x2, y1, y2;
3210   cairo_t *cr;
3211
3212   g_return_val_if_fail (surface != NULL, FALSE);
3213   g_return_val_if_fail (extents != NULL, FALSE);
3214
3215   cr = cairo_create (surface);
3216   cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
3217
3218   x1 = floor (x1);
3219   y1 = floor (y1);
3220   x2 = ceil (x2);
3221   y2 = ceil (y2);
3222   x2 -= x1;
3223   y2 -= y1;
3224   
3225   if (x1 < G_MININT || x1 > G_MAXINT ||
3226       y1 < G_MININT || y1 > G_MAXINT ||
3227       x2 > G_MAXINT || y2 > G_MAXINT)
3228     {
3229       extents->x = extents->y = extents->width = extents->height = 0;
3230       return FALSE;
3231     }
3232
3233   extents->x = x1;
3234   extents->y = y1;
3235   extents->width = x2;
3236   extents->height = y2;
3237
3238   return TRUE;
3239 }
3240
3241 /**
3242  * gtk_drag_set_icon_surface:
3243  * @context: the context for a drag. (This must be called
3244  *            with a context for the source side of a drag)
3245  * @surface: the surface to use as icon
3246  *
3247  * Sets @surface as the icon for a given drag. GTK+ retains
3248  * references for the arguments, and will release them when
3249  * they are no longer needed.
3250  *
3251  * To position the surface relative to the mouse, use
3252  * cairo_surface_set_device_offset() on @surface. The mouse
3253  * cursor will be positioned at the (0,0) coordinate of the
3254  * surface.
3255  **/
3256 void 
3257 gtk_drag_set_icon_surface (GdkDragContext    *context,
3258                            cairo_surface_t   *surface)
3259 {
3260   GtkWidget *window;
3261   GdkScreen *screen;
3262   GdkRectangle extents;
3263   cairo_pattern_t *pattern;
3264       
3265   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3266   g_return_if_fail (surface != NULL);
3267
3268   _gtk_cairo_surface_extents (surface, &extents);
3269
3270
3271   screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3272
3273   window = gtk_window_new (GTK_WINDOW_POPUP);
3274   gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3275   gtk_window_set_screen (GTK_WINDOW (window), screen);
3276   set_can_change_screen (window, TRUE);
3277
3278   gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3279   gtk_widget_set_app_paintable (window, TRUE);
3280
3281   gtk_widget_set_size_request (window, extents.width, extents.height);
3282   gtk_widget_realize (window);
3283
3284   if (cairo_surface_get_content (surface) != CAIRO_CONTENT_COLOR)
3285     {
3286       cairo_surface_t *saturated;
3287       cairo_region_t *region;
3288       cairo_t *cr;
3289
3290       region = gdk_cairo_region_create_from_surface (surface);
3291       cairo_region_translate (region, -extents.x, -extents.y);
3292
3293       gtk_widget_shape_combine_region (window, region);
3294       cairo_region_destroy (region);
3295
3296       /* Need to saturate the colors, so it doesn't look like semi-transparent
3297        * pixels were painted on black. */
3298       saturated = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3299                                                      CAIRO_CONTENT_COLOR,
3300                                                      extents.width,
3301                                                      extents.height);
3302       
3303       cr = cairo_create (saturated);
3304       cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3305       cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
3306       cairo_paint (cr);
3307       cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3308       cairo_paint (cr);
3309       cairo_pop_group_to_source (cr);
3310       cairo_paint (cr);
3311       cairo_destroy (cr);
3312     
3313       pattern = cairo_pattern_create_for_surface (saturated);
3314
3315       cairo_surface_destroy (saturated);
3316     }
3317   else
3318     {
3319       cairo_matrix_t matrix;
3320
3321       pattern = cairo_pattern_create_for_surface (surface);
3322       cairo_matrix_init_translate (&matrix, extents.x, extents.y);
3323       cairo_pattern_set_matrix (pattern, &matrix);
3324     }
3325
3326   gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3327
3328   gtk_drag_set_icon_window (context, window, extents.x, extents.y, TRUE);
3329 }
3330
3331 /**
3332  * gtk_drag_set_icon_name:
3333  * @context: the context for a drag. (This must be called 
3334  *            with a context for the source side of a drag)
3335  * @icon_name: name of icon to use
3336  * @hot_x: the X offset of the hotspot within the icon
3337  * @hot_y: the Y offset of the hotspot within the icon
3338  * 
3339  * Sets the icon for a given drag from a named themed icon. See
3340  * the docs for #GtkIconTheme for more details. Note that the
3341  * size of the icon depends on the icon theme (the icon is
3342  * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus 
3343  * @hot_x and @hot_y have to be used with care.
3344  *
3345  * Since: 2.8
3346  **/
3347 void 
3348 gtk_drag_set_icon_name (GdkDragContext *context,
3349                         const gchar    *icon_name,
3350                         gint            hot_x,
3351                         gint            hot_y)
3352 {
3353   GdkScreen *screen;
3354   GtkSettings *settings;
3355   GtkIconTheme *icon_theme;
3356   GdkPixbuf *pixbuf;
3357   gint width, height, icon_size;
3358
3359   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3360   g_return_if_fail (icon_name != NULL);
3361
3362   screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3363   g_return_if_fail (screen != NULL);
3364
3365   settings = gtk_settings_get_for_screen (screen);
3366   if (gtk_icon_size_lookup_for_settings (settings,
3367                                          GTK_ICON_SIZE_DND,
3368                                          &width, &height))
3369     icon_size = MAX (width, height);
3370   else 
3371     icon_size = 32; /* default value for GTK_ICON_SIZE_DND */ 
3372
3373   icon_theme = gtk_icon_theme_get_for_screen (screen);
3374
3375   pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
3376                                      icon_size, 0, NULL);
3377   if (pixbuf)
3378     set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3379   else
3380     g_warning ("Cannot load drag icon from icon name %s", icon_name);
3381 }
3382
3383 /**
3384  * gtk_drag_set_icon_default:
3385  * @context: the context for a drag. (This must be called 
3386              with a  context for the source side of a drag)
3387  * 
3388  * Sets the icon for a particular drag to the default
3389  * icon.
3390  **/
3391 void 
3392 gtk_drag_set_icon_default (GdkDragContext *context)
3393 {
3394   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3395
3396   gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
3397 }
3398
3399 /*************************************************************
3400  * _gtk_drag_source_handle_event:
3401  *     Called from widget event handling code on Drag events
3402  *     for drag sources.
3403  *
3404  *   arguments:
3405  *     toplevel: Toplevel widget that received the event
3406  *     event:
3407  *   results:
3408  *************************************************************/
3409
3410 void
3411 _gtk_drag_source_handle_event (GtkWidget *widget,
3412                                GdkEvent  *event)
3413 {
3414   GtkDragSourceInfo *info;
3415   GdkDragContext *context;
3416
3417   g_return_if_fail (widget != NULL);
3418   g_return_if_fail (event != NULL);
3419
3420   context = event->dnd.context;
3421   info = gtk_drag_get_source_info (context, FALSE);
3422   if (!info)
3423     return;
3424
3425   switch (event->type)
3426     {
3427     case GDK_DRAG_STATUS:
3428       {
3429         GdkCursor *cursor;
3430
3431         if (info->proxy_dest)
3432           {
3433             if (!event->dnd.send_event)
3434               {
3435                 if (info->proxy_dest->proxy_drop_wait)
3436                   {
3437                     gboolean result = gdk_drag_context_get_selected_action (context) != 0;
3438                     
3439                     /* Aha - we can finally pass the MOTIF DROP on... */
3440                     gdk_drop_reply (info->proxy_dest->context, result, info->proxy_dest->proxy_drop_time);
3441                     if (result)
3442                       gdk_drag_drop (info->context, info->proxy_dest->proxy_drop_time);
3443                     else
3444                       gtk_drag_finish (info->proxy_dest->context, FALSE, FALSE, info->proxy_dest->proxy_drop_time);
3445                   }
3446                 else
3447                   {
3448                     gdk_drag_status (info->proxy_dest->context,
3449                                      gdk_drag_context_get_selected_action (event->dnd.context),
3450                                      event->dnd.time);
3451                   }
3452               }
3453           }
3454         else if (info->have_grab)
3455           {
3456             cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
3457                                           gdk_drag_context_get_selected_action (event->dnd.context),
3458                                           info);
3459             if (info->cursor != cursor)
3460               {
3461                 GdkDevice *pointer;
3462
3463                 pointer = gdk_drag_context_get_device (context);
3464                 gdk_device_grab (pointer, gtk_widget_get_window (widget),
3465                                  GDK_OWNERSHIP_APPLICATION, FALSE,
3466                                  GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3467                                  cursor, info->grab_time);
3468                 info->cursor = cursor;
3469               }
3470             
3471             gtk_drag_add_update_idle (info);
3472           }
3473       }
3474       break;
3475       
3476     case GDK_DROP_FINISHED:
3477       gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, event->dnd.time);
3478       break;
3479     default:
3480       g_assert_not_reached ();
3481     }
3482 }
3483
3484 /*************************************************************
3485  * gtk_drag_source_check_selection:
3486  *     Check if we've set up handlers/claimed the selection
3487  *     for a given drag. If not, add them.
3488  *   arguments:
3489  *     
3490  *   results:
3491  *************************************************************/
3492
3493 static void
3494 gtk_drag_source_check_selection (GtkDragSourceInfo *info, 
3495                                  GdkAtom            selection,
3496                                  guint32            time)
3497 {
3498   GList *tmp_list;
3499
3500   tmp_list = info->selections;
3501   while (tmp_list)
3502     {
3503       if (GDK_POINTER_TO_ATOM (tmp_list->data) == selection)
3504         return;
3505       tmp_list = tmp_list->next;
3506     }
3507
3508   gtk_selection_owner_set_for_display (gtk_widget_get_display (info->widget),
3509                                        info->ipc_widget,
3510                                        selection,
3511                                        time);
3512   info->selections = g_list_prepend (info->selections,
3513                                      GUINT_TO_POINTER (selection));
3514
3515   tmp_list = info->target_list->list;
3516   while (tmp_list)
3517     {
3518       GtkTargetPair *pair = tmp_list->data;
3519
3520       gtk_selection_add_target (info->ipc_widget,
3521                                 selection,
3522                                 pair->target,
3523                                 pair->info);
3524       tmp_list = tmp_list->next;
3525     }
3526   
3527   if (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_MOTIF)
3528     {
3529       gtk_selection_add_target (info->ipc_widget,
3530                                 selection,
3531                                 gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS"),
3532                                 TARGET_MOTIF_SUCCESS);
3533       gtk_selection_add_target (info->ipc_widget,
3534                                 selection,
3535                                 gdk_atom_intern_static_string ("XmTRANSFER_FAILURE"),
3536                                 TARGET_MOTIF_FAILURE);
3537     }
3538
3539   gtk_selection_add_target (info->ipc_widget,
3540                             selection,
3541                             gdk_atom_intern_static_string ("DELETE"),
3542                             TARGET_DELETE);
3543 }
3544
3545 /*************************************************************
3546  * gtk_drag_drop_finished:
3547  *     Clean up from the drag, and display snapback, if necessary.
3548  *   arguments:
3549  *     info:
3550  *     success:
3551  *     time:
3552  *   results:
3553  *************************************************************/
3554
3555 static void
3556 gtk_drag_drop_finished (GtkDragSourceInfo *info,
3557                         GtkDragResult      result,
3558                         guint              time)
3559 {
3560   gboolean success;
3561
3562   success = (result == GTK_DRAG_RESULT_SUCCESS);
3563   gtk_drag_source_release_selections (info, time); 
3564
3565   if (info->proxy_dest)
3566     {
3567       /* The time from the event isn't reliable for Xdnd drags */
3568       gtk_drag_finish (info->proxy_dest->context, success, FALSE, 
3569                        info->proxy_dest->proxy_drop_time);
3570       gtk_drag_source_info_destroy (info);
3571     }
3572   else
3573     {
3574       if (!success)
3575         g_signal_emit_by_name (info->widget, "drag-failed",
3576                                info->context, result, &success);
3577
3578       if (success)
3579         {
3580           gtk_drag_source_info_destroy (info);
3581         }
3582       else
3583         {
3584           GtkDragAnim *anim = g_slice_new0 (GtkDragAnim);
3585           anim->info = info;
3586           anim->step = 0;
3587
3588           anim->n_steps = MAX (info->cur_x - info->start_x,
3589                info->cur_y - info->start_y) / ANIM_STEP_LENGTH;
3590           anim->n_steps = CLAMP (anim->n_steps, ANIM_MIN_STEPS, ANIM_MAX_STEPS);
3591
3592           info->cur_screen = gtk_widget_get_screen (info->widget);
3593
3594           if (!info->icon_window)
3595             set_icon_stock_pixbuf (info->context, NULL, info->icon_pixbuf, 
3596                                    0, 0, TRUE);
3597
3598           gtk_drag_update_icon (info);
3599
3600           /* Mark the context as dead, so if the destination decides
3601            * to respond really late, we still are OK.
3602            */
3603           gtk_drag_clear_source_info (info->context);
3604           gdk_threads_add_timeout (ANIM_STEP_TIME, gtk_drag_anim_timeout, anim);
3605         }
3606     }
3607 }
3608
3609 static void
3610 gtk_drag_source_release_selections (GtkDragSourceInfo *info,
3611                                     guint32            time)
3612 {
3613   GdkDisplay *display = gtk_widget_get_display (info->widget);
3614   GList *tmp_list = info->selections;
3615   
3616   while (tmp_list)
3617     {
3618       GdkAtom selection = GDK_POINTER_TO_ATOM (tmp_list->data);
3619       if (gdk_selection_owner_get_for_display (display, selection) == gtk_widget_get_window (info->ipc_widget))
3620         gtk_selection_owner_set_for_display (display, NULL, selection, time);
3621
3622       tmp_list = tmp_list->next;
3623     }
3624
3625   g_list_free (info->selections);
3626   info->selections = NULL;
3627 }
3628
3629 /*************************************************************
3630  * gtk_drag_drop:
3631  *     Send a drop event.
3632  *   arguments:
3633  *     
3634  *   results:
3635  *************************************************************/
3636
3637 static void
3638 gtk_drag_drop (GtkDragSourceInfo *info, 
3639                guint32            time)
3640 {
3641   if (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_ROOTWIN)
3642     {
3643       GtkSelectionData selection_data;
3644       GList *tmp_list;
3645       /* GTK+ traditionally has used application/x-rootwin-drop, but the
3646        * XDND spec specifies x-rootwindow-drop.
3647        */
3648       GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3649       GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3650       
3651       tmp_list = info->target_list->list;
3652       while (tmp_list)
3653         {
3654           GtkTargetPair *pair = tmp_list->data;
3655           
3656           if (pair->target == target1 || pair->target == target2)
3657             {
3658               selection_data.selection = GDK_NONE;
3659               selection_data.target = pair->target;
3660               selection_data.data = NULL;
3661               selection_data.length = -1;
3662               
3663               g_signal_emit_by_name (info->widget, "drag-data-get",
3664                                      info->context, &selection_data,
3665                                      pair->info,
3666                                      time);
3667               
3668               /* FIXME: Should we check for length >= 0 here? */
3669               gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3670               return;
3671             }
3672           tmp_list = tmp_list->next;
3673         }
3674       gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3675     }
3676   else
3677     {
3678       if (info->icon_window)
3679         gtk_widget_hide (info->icon_window);
3680         
3681       gdk_drag_drop (info->context, time);
3682       info->drop_timeout = gdk_threads_add_timeout (DROP_ABORT_TIME,
3683                                           gtk_drag_abort_timeout,
3684                                           info);
3685     }
3686 }
3687
3688 /*
3689  * Source side callbacks.
3690  */
3691
3692 static gboolean
3693 gtk_drag_source_event_cb (GtkWidget      *widget,
3694                           GdkEvent       *event,
3695                           gpointer        data)
3696 {
3697   GtkDragSourceSite *site;
3698   gboolean retval = FALSE;
3699   site = (GtkDragSourceSite *)data;
3700
3701   switch (event->type)
3702     {
3703     case GDK_BUTTON_PRESS:
3704       if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3705         {
3706           site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
3707           site->x = event->button.x;
3708           site->y = event->button.y;
3709         }
3710       break;
3711       
3712     case GDK_BUTTON_RELEASE:
3713       if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3714         site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
3715       break;
3716       
3717     case GDK_MOTION_NOTIFY:
3718       if (site->state & event->motion.state & site->start_button_mask)
3719         {
3720           /* FIXME: This is really broken and can leave us
3721            * with a stuck grab
3722            */
3723           int i;
3724           for (i=1; i<6; i++)
3725             {
3726               if (site->state & event->motion.state & 
3727                   GDK_BUTTON1_MASK << (i - 1))
3728                 break;
3729             }
3730
3731           if (gtk_drag_check_threshold (widget, site->x, site->y,
3732                                         event->motion.x, event->motion.y))
3733             {
3734               site->state = 0;
3735               gtk_drag_begin_internal (widget, site, site->target_list,
3736                                        site->actions, 
3737                                        i, event);
3738
3739               retval = TRUE;
3740             }
3741         }
3742       break;
3743       
3744     default:                    /* hit for 2/3BUTTON_PRESS */
3745       break;
3746     }
3747   
3748   return retval;
3749 }
3750
3751 static void 
3752 gtk_drag_source_site_destroy (gpointer data)
3753 {
3754   GtkDragSourceSite *site = data;
3755
3756   if (site->target_list)
3757     gtk_target_list_unref (site->target_list);
3758
3759   gtk_drag_source_unset_icon (site);
3760   g_slice_free (GtkDragSourceSite, site);
3761 }
3762
3763 static void
3764 gtk_drag_selection_get (GtkWidget        *widget, 
3765                         GtkSelectionData *selection_data,
3766                         guint             sel_info,
3767                         guint32           time,
3768                         gpointer          data)
3769 {
3770   GtkDragSourceInfo *info = data;
3771   static GdkAtom null_atom = GDK_NONE;
3772   guint target_info;
3773
3774   if (!null_atom)
3775     null_atom = gdk_atom_intern_static_string ("NULL");
3776
3777   switch (sel_info)
3778     {
3779     case TARGET_DELETE:
3780       g_signal_emit_by_name (info->widget,
3781                              "drag-data-delete", 
3782                              info->context);
3783       gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3784       break;
3785     case TARGET_MOTIF_SUCCESS:
3786       gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3787       gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3788       break;
3789     case TARGET_MOTIF_FAILURE:
3790       gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3791       gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3792       break;
3793     default:
3794       if (info->proxy_dest)
3795         {
3796           /* This is sort of dangerous and needs to be thought
3797            * through better
3798            */
3799           info->proxy_dest->proxy_data = selection_data;
3800           gtk_drag_get_data (info->widget,
3801                              info->proxy_dest->context,
3802                              gtk_selection_data_get_target (selection_data),
3803                              time);
3804           gtk_main ();
3805           info->proxy_dest->proxy_data = NULL;
3806         }
3807       else
3808         {
3809           if (gtk_target_list_find (info->target_list, 
3810                                     gtk_selection_data_get_target (selection_data),
3811                                     &target_info))
3812             {
3813               g_signal_emit_by_name (info->widget, "drag-data-get",
3814                                      info->context,
3815                                      selection_data,
3816                                      target_info,
3817                                      time);
3818             }
3819         }
3820       break;
3821     }
3822 }
3823
3824 static gboolean
3825 gtk_drag_anim_timeout (gpointer data)
3826 {
3827   GtkDragAnim *anim;
3828   GtkDragSourceInfo *info;
3829   gint x, y;
3830   gboolean retval;
3831
3832   anim = data;
3833   info = anim->info;
3834
3835   if (anim->step == anim->n_steps)
3836     {
3837       gtk_drag_source_info_destroy (anim->info);
3838       g_slice_free (GtkDragAnim, anim);
3839
3840       retval = FALSE;
3841     }
3842   else
3843     {
3844       x = (info->start_x * (anim->step + 1) +
3845            info->cur_x * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3846       y = (info->start_y * (anim->step + 1) +
3847            info->cur_y * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3848       if (info->icon_window)
3849         {
3850           GtkWidget *icon_window;
3851           gint hot_x, hot_y;
3852
3853           gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
3854           gtk_window_move (GTK_WINDOW (icon_window),
3855                            x - hot_x,
3856                            y - hot_y);
3857         }
3858
3859       anim->step++;
3860
3861       retval = TRUE;
3862     }
3863
3864   return retval;
3865 }
3866
3867 static void
3868 gtk_drag_remove_icon (GtkDragSourceInfo *info)
3869 {
3870   if (info->icon_window)
3871     {
3872       gtk_widget_hide (info->icon_window);
3873       if (info->destroy_icon)
3874         gtk_widget_destroy (info->icon_window);
3875
3876       if (info->fallback_icon)
3877         {
3878           gtk_widget_destroy (info->fallback_icon);
3879           info->fallback_icon = NULL;
3880         }
3881
3882       g_object_unref (info->icon_window);
3883       info->icon_window = NULL;
3884     }
3885 }
3886
3887 static void
3888 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
3889 {
3890   gint i;
3891
3892   for (i = 0; i < n_drag_cursors; i++)
3893     {
3894       if (info->drag_cursors[i] != NULL)
3895         {
3896           gdk_cursor_unref (info->drag_cursors[i]);
3897           info->drag_cursors[i] = NULL;
3898         }
3899     }
3900
3901   gtk_drag_remove_icon (info);
3902
3903   if (info->icon_pixbuf)
3904     {
3905       g_object_unref (info->icon_pixbuf);
3906       info->icon_pixbuf = NULL;
3907     }
3908
3909   g_signal_handlers_disconnect_by_func (info->ipc_widget,
3910                                         gtk_drag_grab_broken_event_cb,
3911                                         info);
3912   g_signal_handlers_disconnect_by_func (info->ipc_widget,
3913                                         gtk_drag_grab_notify_cb,
3914                                         info);
3915   g_signal_handlers_disconnect_by_func (info->ipc_widget,
3916                                         gtk_drag_button_release_cb,
3917                                         info);
3918   g_signal_handlers_disconnect_by_func (info->ipc_widget,
3919                                         gtk_drag_motion_cb,
3920                                         info);
3921   g_signal_handlers_disconnect_by_func (info->ipc_widget,
3922                                         gtk_drag_key_cb,
3923                                         info);
3924   g_signal_handlers_disconnect_by_func (info->ipc_widget,
3925                                         gtk_drag_selection_get,
3926                                         info);
3927
3928   if (!info->proxy_dest)
3929     g_signal_emit_by_name (info->widget, "drag-end", info->context);
3930
3931   if (info->widget)
3932     g_object_unref (info->widget);
3933
3934   gtk_selection_remove_all (info->ipc_widget);
3935   g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), NULL);
3936   source_widgets = g_slist_remove (source_widgets, info->ipc_widget);
3937   gtk_drag_release_ipc_widget (info->ipc_widget);
3938
3939   gtk_target_list_unref (info->target_list);
3940
3941   gtk_drag_clear_source_info (info->context);
3942   g_object_unref (info->context);
3943
3944   if (info->drop_timeout)
3945     g_source_remove (info->drop_timeout);
3946
3947   if (info->update_idle)
3948     g_source_remove (info->update_idle);
3949
3950   g_free (info);
3951 }
3952
3953 static gboolean
3954 gtk_drag_update_idle (gpointer data)
3955 {
3956   GtkDragSourceInfo *info = data;
3957   GdkWindow *dest_window;
3958   GdkDragProtocol protocol;
3959   GdkAtom selection;
3960
3961   GdkDragAction action;
3962   GdkDragAction possible_actions;
3963   guint32 time;
3964
3965   info->update_idle = 0;
3966     
3967   if (info->last_event)
3968     {
3969       time = gtk_drag_get_event_time (info->last_event);
3970       gtk_drag_get_event_actions (info->last_event,
3971                                   info->button, 
3972                                   info->possible_actions,
3973                                   &action, &possible_actions);
3974       gtk_drag_update_icon (info);
3975       gdk_drag_find_window_for_screen (info->context,
3976                                        info->icon_window ? gtk_widget_get_window (info->icon_window) : NULL,
3977                                        info->cur_screen, info->cur_x, info->cur_y,
3978                                        &dest_window, &protocol);
3979       
3980       if (!gdk_drag_motion (info->context, dest_window, protocol,
3981                             info->cur_x, info->cur_y, action, 
3982                             possible_actions,
3983                             time))
3984         {
3985           gdk_event_free ((GdkEvent *)info->last_event);
3986           info->last_event = NULL;
3987         }
3988   
3989       if (dest_window)
3990         g_object_unref (dest_window);
3991       
3992       selection = gdk_drag_get_selection (info->context);
3993       if (selection)
3994         gtk_drag_source_check_selection (info, selection, time);
3995
3996     }
3997
3998   return FALSE;
3999 }
4000
4001 static void
4002 gtk_drag_add_update_idle (GtkDragSourceInfo *info)
4003 {
4004   /* We use an idle lower than GDK_PRIORITY_REDRAW so that exposes
4005    * from the last move can catch up before we move again.
4006    */
4007   if (!info->update_idle)
4008     info->update_idle = gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW + 5,
4009                                          gtk_drag_update_idle,
4010                                          info,
4011                                          NULL);
4012 }
4013
4014 /**
4015  * gtk_drag_update:
4016  * @info: DragSourceInfo for the drag
4017  * @screen: new screen
4018  * @x_root: new X position 
4019  * @y_root: new y position
4020  * @event: event received requiring update
4021  * 
4022  * Updates the status of the drag; called when the
4023  * cursor moves or the modifier changes
4024  **/
4025 static void
4026 gtk_drag_update (GtkDragSourceInfo *info,
4027                  GdkScreen         *screen,
4028                  gint               x_root,
4029                  gint               y_root,
4030                  GdkEvent          *event)
4031 {
4032   info->cur_screen = screen;
4033   info->cur_x = x_root;
4034   info->cur_y = y_root;
4035   if (info->last_event)
4036     {
4037       gdk_event_free ((GdkEvent *)info->last_event);
4038       info->last_event = NULL;
4039     }
4040   if (event)
4041     info->last_event = gdk_event_copy ((GdkEvent *)event);
4042
4043   gtk_drag_add_update_idle (info);
4044 }
4045
4046 /*************************************************************
4047  * gtk_drag_end:
4048  *     Called when the user finishes to drag, either by
4049  *     releasing the mouse, or by pressing Esc.
4050  *   arguments:
4051  *     info: Source info for the drag
4052  *     time: Timestamp for ending the drag
4053  *   results:
4054  *************************************************************/
4055
4056 static void
4057 gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
4058 {
4059   GdkEvent *send_event;
4060   GtkWidget *source_widget = info->widget;
4061   GdkDevice *pointer, *keyboard;
4062
4063   pointer = gdk_drag_context_get_device (info->context);
4064   keyboard = gdk_device_get_associated_device (pointer);
4065
4066   /* Prevent ungrab before grab (see bug 623865) */
4067   if (info->grab_time == GDK_CURRENT_TIME)
4068     time = GDK_CURRENT_TIME;
4069
4070   if (info->update_idle)
4071     {
4072       g_source_remove (info->update_idle);
4073       info->update_idle = 0;
4074     }
4075   
4076   if (info->last_event)
4077     {
4078       gdk_event_free (info->last_event);
4079       info->last_event = NULL;
4080     }
4081   
4082   info->have_grab = FALSE;
4083   
4084   g_signal_handlers_disconnect_by_func (info->ipc_widget,
4085                                         gtk_drag_grab_broken_event_cb,
4086                                         info);
4087   g_signal_handlers_disconnect_by_func (info->ipc_widget,
4088                                         gtk_drag_grab_notify_cb,
4089                                         info);
4090   g_signal_handlers_disconnect_by_func (info->ipc_widget,
4091                                         gtk_drag_button_release_cb,
4092                                         info);
4093   g_signal_handlers_disconnect_by_func (info->ipc_widget,
4094                                         gtk_drag_motion_cb,
4095                                         info);
4096   g_signal_handlers_disconnect_by_func (info->ipc_widget,
4097                                         gtk_drag_key_cb,
4098                                         info);
4099
4100   gdk_device_ungrab (pointer, time);
4101   ungrab_dnd_keys (info->ipc_widget, keyboard, time);
4102   gtk_device_grab_remove (info->ipc_widget, pointer);
4103
4104   /* Send on a release pair to the original 
4105    * widget to convince it to release its grab. We need to
4106    * call gtk_propagate_event() here, instead of 
4107    * gtk_widget_event() because widget like GtkList may
4108    * expect propagation.
4109    */
4110
4111   send_event = gdk_event_new (GDK_BUTTON_RELEASE);
4112   send_event->button.window = g_object_ref (gtk_widget_get_root_window (source_widget));
4113   send_event->button.send_event = TRUE;
4114   send_event->button.time = time;
4115   send_event->button.x = 0;
4116   send_event->button.y = 0;
4117   send_event->button.axes = NULL;
4118   send_event->button.state = 0;
4119   send_event->button.button = info->button;
4120   send_event->button.device = pointer;
4121   send_event->button.x_root = 0;
4122   send_event->button.y_root = 0;
4123
4124   gtk_propagate_event (source_widget, send_event);
4125   gdk_event_free (send_event);
4126 }
4127
4128 /*************************************************************
4129  * gtk_drag_cancel:
4130  *    Called on cancellation of a drag, either by the user
4131  *    or programmatically.
4132  *   arguments:
4133  *     info: Source info for the drag
4134  *     time: Timestamp for ending the drag
4135  *   results:
4136  *************************************************************/
4137
4138 static void
4139 gtk_drag_cancel (GtkDragSourceInfo *info, GtkDragResult result, guint32 time)
4140 {
4141   gtk_drag_end (info, time);
4142   gdk_drag_abort (info->context, time);
4143   gtk_drag_drop_finished (info, result, time);
4144 }
4145
4146 /*************************************************************
4147  * gtk_drag_motion_cb:
4148  *     "motion-notify-event" callback during drag.
4149  *   arguments:
4150  *     
4151  *   results:
4152  *************************************************************/
4153
4154 static gboolean
4155 gtk_drag_motion_cb (GtkWidget      *widget, 
4156                     GdkEventMotion *event, 
4157                     gpointer        data)
4158 {
4159   GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4160   GdkScreen *screen;
4161   gint x_root, y_root;
4162
4163   if (event->is_hint)
4164     {
4165       GdkDisplay *display = gtk_widget_get_display (widget);
4166
4167       gdk_display_get_device_state (display, event->device,
4168                                     &screen, &x_root, &y_root, NULL);
4169       event->x_root = x_root;
4170       event->y_root = y_root;
4171     }
4172   else
4173     screen = gdk_event_get_screen ((GdkEvent *)event);
4174
4175   gtk_drag_update (info, screen, event->x_root, event->y_root, (GdkEvent *) event);
4176
4177   return TRUE;
4178 }
4179
4180 /*************************************************************
4181  * gtk_drag_key_cb:
4182  *     "key-press/release-event" callback during drag.
4183  *   arguments:
4184  *     
4185  *   results:
4186  *************************************************************/
4187
4188 #define BIG_STEP 20
4189 #define SMALL_STEP 1
4190
4191 static gboolean
4192 gtk_drag_key_cb (GtkWidget         *widget, 
4193                  GdkEventKey       *event, 
4194                  gpointer           data)
4195 {
4196   GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4197   GdkModifierType state;
4198   GdkWindow *root_window;
4199   GdkDevice *pointer;
4200   gint dx, dy;
4201
4202   dx = dy = 0;
4203   state = event->state & gtk_accelerator_get_default_mod_mask ();
4204   pointer = gdk_device_get_associated_device (gdk_event_get_device ((GdkEvent *) event));
4205
4206   if (event->type == GDK_KEY_PRESS)
4207     {
4208       switch (event->keyval)
4209         {
4210         case GDK_KEY_Escape:
4211           gtk_drag_cancel (info, GTK_DRAG_RESULT_USER_CANCELLED, event->time);
4212           return TRUE;
4213
4214         case GDK_KEY_space:
4215         case GDK_KEY_Return:
4216         case GDK_KEY_ISO_Enter:
4217         case GDK_KEY_KP_Enter:
4218         case GDK_KEY_KP_Space:
4219           gtk_drag_end (info, event->time);
4220           gtk_drag_drop (info, event->time);
4221           return TRUE;
4222
4223         case GDK_KEY_Up:
4224         case GDK_KEY_KP_Up:
4225           dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4226           break;
4227           
4228         case GDK_KEY_Down:
4229         case GDK_KEY_KP_Down:
4230           dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4231           break;
4232           
4233         case GDK_KEY_Left:
4234         case GDK_KEY_KP_Left:
4235           dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4236           break;
4237           
4238         case GDK_KEY_Right:
4239         case GDK_KEY_KP_Right:
4240           dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4241           break;
4242         }
4243       
4244     }
4245
4246   /* Now send a "motion" so that the modifier state is updated */
4247
4248   /* The state is not yet updated in the event, so we need
4249    * to query it here. We could use XGetModifierMapping, but
4250    * that would be overkill.
4251    */
4252   root_window = gtk_widget_get_root_window (widget);
4253   gdk_window_get_device_position (root_window, pointer, NULL, NULL, &state);
4254   event->state = state;
4255
4256   if (dx != 0 || dy != 0)
4257     {
4258       info->cur_x += dx;
4259       info->cur_y += dy;
4260       gdk_display_warp_device (gtk_widget_get_display (widget), pointer,
4261                                gtk_widget_get_screen (widget),
4262                                info->cur_x, info->cur_y);
4263     }
4264
4265   gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, (GdkEvent *)event);
4266
4267   return TRUE;
4268 }
4269
4270 static gboolean
4271 gtk_drag_grab_broken_event_cb (GtkWidget          *widget,
4272                                GdkEventGrabBroken *event,
4273                                gpointer            data)
4274 {
4275   GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4276
4277   /* Don't cancel if we break the implicit grab from the initial button_press.
4278    * Also, don't cancel if we re-grab on the widget or on our IPC window, for
4279    * example, when changing the drag cursor.
4280    */
4281   if (event->implicit
4282       || event->grab_window == gtk_widget_get_window (info->widget)
4283       || event->grab_window == gtk_widget_get_window (info->ipc_widget))
4284     return FALSE;
4285
4286   gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4287   return TRUE;
4288 }
4289
4290 static void
4291 gtk_drag_grab_notify_cb (GtkWidget        *widget,
4292                          gboolean          was_grabbed,
4293                          gpointer          data)
4294 {
4295   GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4296   GdkDevice *pointer;
4297
4298   pointer = gdk_drag_context_get_device (info->context);
4299
4300   if (gtk_widget_device_is_shadowed (widget, pointer))
4301     {
4302       /* We have to block callbacks to avoid recursion here, because
4303          gtk_drag_cancel calls gtk_grab_remove (via gtk_drag_end) */
4304       g_signal_handlers_block_by_func (widget, gtk_drag_grab_notify_cb, data);
4305       gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4306       g_signal_handlers_unblock_by_func (widget, gtk_drag_grab_notify_cb, data);
4307     }
4308 }
4309
4310
4311 /*************************************************************
4312  * gtk_drag_button_release_cb:
4313  *     "button-release-event" callback during drag.
4314  *   arguments:
4315  *     
4316  *   results:
4317  *************************************************************/
4318
4319 static gboolean
4320 gtk_drag_button_release_cb (GtkWidget      *widget, 
4321                             GdkEventButton *event, 
4322                             gpointer        data)
4323 {
4324   GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4325
4326   if (event->button != info->button)
4327     return FALSE;
4328
4329   if ((gdk_drag_context_get_selected_action (info->context) != 0) &&
4330       (gdk_drag_context_get_dest_window (info->context) != NULL))
4331     {
4332       gtk_drag_end (info, event->time);
4333       gtk_drag_drop (info, event->time);
4334     }
4335   else
4336     {
4337       gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4338     }
4339
4340   return TRUE;
4341 }
4342
4343 static gboolean
4344 gtk_drag_abort_timeout (gpointer data)
4345 {
4346   GtkDragSourceInfo *info = data;
4347   guint32 time = GDK_CURRENT_TIME;
4348
4349   if (info->proxy_dest)
4350     time = info->proxy_dest->proxy_drop_time;
4351
4352   info->drop_timeout = 0;
4353   gtk_drag_drop_finished (info, GTK_DRAG_RESULT_TIMEOUT_EXPIRED, time);
4354   
4355   return FALSE;
4356 }
4357
4358 /**
4359  * gtk_drag_check_threshold:
4360  * @widget: a #GtkWidget
4361  * @start_x: X coordinate of start of drag
4362  * @start_y: Y coordinate of start of drag
4363  * @current_x: current X coordinate
4364  * @current_y: current Y coordinate
4365  * 
4366  * Checks to see if a mouse drag starting at (@start_x, @start_y) and ending
4367  * at (@current_x, @current_y) has passed the GTK+ drag threshold, and thus
4368  * should trigger the beginning of a drag-and-drop operation.
4369  *
4370  * Return Value: %TRUE if the drag threshold has been passed.
4371  **/
4372 gboolean
4373 gtk_drag_check_threshold (GtkWidget *widget,
4374                           gint       start_x,
4375                           gint       start_y,
4376                           gint       current_x,
4377                           gint       current_y)
4378 {
4379   gint drag_threshold;
4380
4381   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4382
4383   g_object_get (gtk_widget_get_settings (widget),
4384                 "gtk-dnd-drag-threshold", &drag_threshold,
4385                 NULL);
4386   
4387   return (ABS (current_x - start_x) > drag_threshold ||
4388           ABS (current_y - start_y) > drag_threshold);
4389 }