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