]> Pileus Git - ~andy/gtk/blob - gtk/gtkdnd-quartz.c
Bug 658767 - Drag and Drop NSEvent capture is racy
[~andy/gtk] / gtk / gtkdnd-quartz.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 "gdk/gdk.h"
33
34 #include "gtkdnd.h"
35 #include "gtkiconfactory.h"
36 #include "gtkicontheme.h"
37 #include "gtkimageprivate.h"
38 #include "gtkinvisible.h"
39 #include "gtkmain.h"
40 #include "gtkplug.h"
41 #include "gtkstock.h"
42 #include "gtkwindow.h"
43 #include "gtkintl.h"
44 #include "gtkquartz.h"
45 #include "gdk/quartz/gdkquartz.h"
46 #include "gtkselectionprivate.h"
47
48 typedef struct _GtkDragSourceSite GtkDragSourceSite;
49 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
50 typedef struct _GtkDragDestSite GtkDragDestSite;
51 typedef struct _GtkDragDestInfo GtkDragDestInfo;
52 typedef struct _GtkDragFindData GtkDragFindData;
53
54 static void     gtk_drag_find_widget            (GtkWidget        *widget,
55                                                  GtkDragFindData  *data);
56 static void     gtk_drag_dest_site_destroy      (gpointer          data);
57 static void     gtk_drag_dest_leave             (GtkWidget        *widget,
58                                                  GdkDragContext   *context,
59                                                  guint             time);
60 static GtkDragDestInfo *gtk_drag_get_dest_info  (GdkDragContext   *context,
61                                                  gboolean          create);
62 static void gtk_drag_source_site_destroy        (gpointer           data);
63
64 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
65                                                     gboolean        create);
66
67 extern GdkDragContext *gdk_quartz_drag_source_context (); /* gdk/quartz/gdkdnd-quartz.c */
68
69 struct _GtkDragSourceSite 
70 {
71   GdkModifierType    start_button_mask;
72   GtkTargetList     *target_list;        /* Targets for drag data */
73   GdkDragAction      actions;            /* Possible actions */
74
75   /* Drag icon */
76   GtkImageType icon_type;
77   union
78   {
79     GtkImagePixbufData pixbuf;
80     GtkImageStockData stock;
81     GtkImageIconNameData name;
82   } icon_data;
83
84   /* Stored button press information to detect drag beginning */
85   gint               state;
86   gint               x, y;
87 };
88
89 struct _GtkDragSourceInfo 
90 {
91   GtkWidget         *source_widget;
92   GtkWidget         *widget;
93   GtkTargetList     *target_list; /* Targets for drag data */
94   GdkDragAction      possible_actions; /* Actions allowed by source */
95   GdkDragContext    *context;     /* drag context */
96   NSEvent           *nsevent;     /* what started it */
97   gint hot_x, hot_y;              /* Hot spot for drag */
98   GdkPixbuf         *icon_pixbuf;
99   gboolean           success;
100   gboolean           delete;
101 };
102
103 struct _GtkDragDestSite 
104 {
105   GtkDestDefaults    flags;
106   GtkTargetList     *target_list;
107   GdkDragAction      actions;
108   guint              have_drag : 1;
109   guint              track_motion : 1;
110 };
111
112 struct _GtkDragDestInfo 
113 {
114   GtkWidget         *widget;       /* Widget in which drag is in */
115   GdkDragContext    *context;      /* Drag context */
116   guint              dropped : 1;     /* Set after we receive a drop */
117   gint               drop_x, drop_y; /* Position of drop */
118 };
119
120 struct _GtkDragFindData 
121 {
122   gint x;
123   gint y;
124   GdkDragContext *context;
125   GtkDragDestInfo *info;
126   gboolean found;
127   gboolean toplevel;
128   gboolean (*callback) (GtkWidget *widget, GdkDragContext *context,
129                         gint x, gint y, guint32 time);
130   guint32 time;
131 };
132
133
134 @interface GtkDragSourceOwner : NSObject {
135   GtkDragSourceInfo *info;
136 }
137
138 @end
139
140 @implementation GtkDragSourceOwner
141 -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
142 {
143   guint target_info;
144   GtkSelectionData selection_data;
145
146   selection_data.selection = GDK_NONE;
147   selection_data.data = NULL;
148   selection_data.length = -1;
149   selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
150   selection_data.display = gdk_display_get_default ();
151
152   if (gtk_target_list_find (info->target_list, 
153                             selection_data.target, 
154                             &target_info)) 
155     {
156       g_signal_emit_by_name (info->widget, "drag-data-get",
157                              info->context,
158                              &selection_data,
159                              target_info,
160                              time);
161
162       if (selection_data.length >= 0)
163         _gtk_quartz_set_selection_data_for_pasteboard (sender, &selection_data);
164       
165       g_free (selection_data.data);
166     }
167 }
168
169 - (id)initWithInfo:(GtkDragSourceInfo *)anInfo
170 {
171   self = [super init];
172
173   if (self) 
174     {
175       info = anInfo;
176     }
177
178   return self;
179 }
180
181 @end
182
183 void 
184 gtk_drag_get_data (GtkWidget      *widget,
185                    GdkDragContext *context,
186                    GdkAtom         target,
187                    guint32         time)
188 {
189   id <NSDraggingInfo> dragging_info;
190   NSPasteboard *pasteboard;
191   GtkSelectionData *selection_data;
192   GtkDragDestInfo *info;
193   GtkDragDestSite *site;
194
195   dragging_info = gdk_quartz_drag_context_get_dragging_info_libgtk_only (context);
196   pasteboard = [dragging_info draggingPasteboard];
197
198   info = gtk_drag_get_dest_info (context, FALSE);
199   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
200
201   selection_data = _gtk_quartz_get_selection_data_from_pasteboard (pasteboard,
202                                                                    target, 0);
203
204   if (site && site->target_list)
205     {
206       guint target_info;
207       
208       if (gtk_target_list_find (site->target_list, 
209                                 selection_data->target,
210                                 &target_info))
211         {
212           if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
213               selection_data->length >= 0)
214             g_signal_emit_by_name (widget,
215                                    "drag-data-received",
216                                    context, info->drop_x, info->drop_y,
217                                    selection_data,
218                                    target_info, time);
219         }
220     }
221   else
222     {
223       g_signal_emit_by_name (widget,
224                              "drag-data-received",
225                              context, info->drop_x, info->drop_y,
226                              selection_data,
227                              0, time);
228     }
229   
230   if (site && site->flags & GTK_DEST_DEFAULT_DROP)
231     {
232       gtk_drag_finish (context, 
233                        (selection_data->length >= 0),
234                        (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
235                        time);
236     }      
237 }
238
239 void 
240 gtk_drag_finish (GdkDragContext *context,
241                  gboolean        success,
242                  gboolean        del,
243                  guint32         time)
244 {
245   GtkDragSourceInfo *info;
246   GdkDragContext* source_context = gdk_quartz_drag_source_context ();
247
248   if (source_context)
249     {
250       info = gtk_drag_get_source_info (source_context, FALSE);
251       if (info)
252         {
253           info->success = success;
254           info->delete = del;
255         }
256     }
257 }
258
259 static void
260 gtk_drag_dest_info_destroy (gpointer data)
261 {
262   GtkDragDestInfo *info = data;
263
264   g_free (info);
265 }
266
267 static GtkDragDestInfo *
268 gtk_drag_get_dest_info (GdkDragContext *context,
269                         gboolean        create)
270 {
271   GtkDragDestInfo *info;
272   static GQuark info_quark = 0;
273   if (!info_quark)
274     info_quark = g_quark_from_static_string ("gtk-dest-info");
275   
276   info = g_object_get_qdata (G_OBJECT (context), info_quark);
277   if (!info && create)
278     {
279       info = g_new (GtkDragDestInfo, 1);
280       info->widget = NULL;
281       info->context = context;
282       info->dropped = FALSE;
283       g_object_set_qdata_full (G_OBJECT (context), info_quark,
284                                info, gtk_drag_dest_info_destroy);
285     }
286
287   return info;
288 }
289
290 static GQuark dest_info_quark = 0;
291
292 static GtkDragSourceInfo *
293 gtk_drag_get_source_info (GdkDragContext *context,
294                           gboolean        create)
295 {
296   GtkDragSourceInfo *info;
297
298   if (!dest_info_quark)
299     dest_info_quark = g_quark_from_static_string ("gtk-source-info");
300   
301   info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
302   if (!info && create)
303     {
304       info = g_new0 (GtkDragSourceInfo, 1);
305       info->context = context;
306       g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
307     }
308
309   return info;
310 }
311
312 static void
313 gtk_drag_clear_source_info (GdkDragContext *context)
314 {
315   g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
316 }
317
318 GtkWidget *
319 gtk_drag_get_source_widget (GdkDragContext *context)
320 {
321   GtkDragSourceInfo *info;
322   GdkDragContext* real_source_context = gdk_quartz_drag_source_context();
323
324   if (!real_source_context)
325     return NULL;
326
327   info = gtk_drag_get_source_info (real_source_context, FALSE);
328   if (!info)
329      return NULL;
330
331   return info->source_widget;
332 }
333
334 /*************************************************************
335  * gtk_drag_highlight_draw:
336  *     Callback for expose_event for highlighted widgets.
337  *   arguments:
338  *     widget:
339  *     event:
340  *     data:
341  *   results:
342  *************************************************************/
343
344 static gboolean
345 gtk_drag_highlight_draw (GtkWidget *widget,
346                          cairo_t   *cr,
347                          gpointer   data)
348 {
349   int width = gtk_widget_get_allocated_width (widget);
350   int height = gtk_widget_get_allocated_height (widget);
351   GtkStyleContext *context = gtk_widget_get_style_context (widget);
352
353   gtk_style_context_save (context);
354   gtk_style_context_add_class (context, GTK_STYLE_CLASS_DND);
355
356   gtk_render_frame (context, cr, 0, 0, width, height);
357
358   gtk_style_context_restore (context);
359
360   cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
361   cairo_set_line_width (cr, 1.0);
362   cairo_rectangle (cr,
363                    0.5, 0.5,
364                    width - 1, height - 1);
365   cairo_stroke (cr);
366  
367   return FALSE;
368 }
369
370 /*************************************************************
371  * gtk_drag_highlight:
372  *     Highlight the given widget in the default manner.
373  *   arguments:
374  *     widget:
375  *   results:
376  *************************************************************/
377
378 void 
379 gtk_drag_highlight (GtkWidget  *widget)
380 {
381   g_return_if_fail (GTK_IS_WIDGET (widget));
382
383   g_signal_connect_after (widget, "draw",
384                           G_CALLBACK (gtk_drag_highlight_draw),
385                           NULL);
386
387   gtk_widget_queue_draw (widget);
388 }
389
390 /*************************************************************
391  * gtk_drag_unhighlight:
392  *     Refresh the given widget to remove the highlight.
393  *   arguments:
394  *     widget:
395  *   results:
396  *************************************************************/
397
398 void 
399 gtk_drag_unhighlight (GtkWidget *widget)
400 {
401   g_return_if_fail (GTK_IS_WIDGET (widget));
402
403   g_signal_handlers_disconnect_by_func (widget,
404                                         gtk_drag_highlight_draw,
405                                         NULL);
406   
407   gtk_widget_queue_draw (widget);
408 }
409
410 static NSWindow *
411 get_toplevel_nswindow (GtkWidget *widget)
412 {
413   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
414   GdkWindow *window = gtk_widget_get_window (toplevel);
415   
416   if (gtk_widget_is_toplevel (toplevel) && window)
417     return [gdk_quartz_window_get_nsview (window) window];
418   else
419     return NULL;
420 }
421
422 static void
423 register_types (GtkWidget *widget, GtkDragDestSite *site)
424 {
425   if (site->target_list)
426     {
427       NSWindow *nswindow = get_toplevel_nswindow (widget);
428       NSSet *types;
429       NSAutoreleasePool *pool;
430
431       if (!nswindow)
432         return;
433
434       pool = [[NSAutoreleasePool alloc] init];
435       types = _gtk_quartz_target_list_to_pasteboard_types (site->target_list);
436
437       [nswindow registerForDraggedTypes:[types allObjects]];
438
439       [types release];
440       [pool release];
441     }
442 }
443
444 static void
445 gtk_drag_dest_realized (GtkWidget *widget, 
446                         gpointer   user_data)
447 {
448   GtkDragDestSite *site = user_data;
449
450   register_types (widget, site);
451 }
452
453 static void
454 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
455                                  GtkWidget *previous_toplevel,
456                                  gpointer   user_data)
457 {
458   GtkDragDestSite *site = user_data;
459
460   register_types (widget, site);
461 }
462
463 static void
464 gtk_drag_dest_site_destroy (gpointer data)
465 {
466   GtkDragDestSite *site = data;
467     
468   if (site->target_list)
469     gtk_target_list_unref (site->target_list);
470
471   g_free (site);
472 }
473
474 void 
475 gtk_drag_dest_set (GtkWidget            *widget,
476                    GtkDestDefaults       flags,
477                    const GtkTargetEntry *targets,
478                    gint                  n_targets,
479                    GdkDragAction         actions)
480 {
481   GtkDragDestSite *old_site, *site;
482
483   g_return_if_fail (GTK_IS_WIDGET (widget));
484
485   old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
486
487   site = g_new (GtkDragDestSite, 1);
488   site->flags = flags;
489   site->have_drag = FALSE;
490   if (targets)
491     site->target_list = gtk_target_list_new (targets, n_targets);
492   else
493     site->target_list = NULL;
494   site->actions = actions;
495
496   if (old_site)
497     site->track_motion = old_site->track_motion;
498   else
499     site->track_motion = FALSE;
500
501   gtk_drag_dest_unset (widget);
502
503   if (gtk_widget_get_realized (widget))
504     gtk_drag_dest_realized (widget, site);
505
506   g_signal_connect (widget, "realize",
507                     G_CALLBACK (gtk_drag_dest_realized), site);
508   g_signal_connect (widget, "hierarchy-changed",
509                     G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
510
511   g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
512                           site, gtk_drag_dest_site_destroy);
513 }
514
515 void 
516 gtk_drag_dest_set_proxy (GtkWidget      *widget,
517                          GdkWindow      *proxy_window,
518                          GdkDragProtocol protocol,
519                          gboolean        use_coordinates)
520 {
521   g_warning ("gtk_drag_dest_set_proxy is not supported on Mac OS X.");
522 }
523
524 void 
525 gtk_drag_dest_unset (GtkWidget *widget)
526 {
527   GtkDragDestSite *old_site;
528
529   g_return_if_fail (GTK_IS_WIDGET (widget));
530
531   old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
532   if (old_site)
533     {
534       g_signal_handlers_disconnect_by_func (widget,
535                                             gtk_drag_dest_realized,
536                                             old_site);
537       g_signal_handlers_disconnect_by_func (widget,
538                                             gtk_drag_dest_hierarchy_changed,
539                                             old_site);
540     }
541
542   g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
543 }
544
545 GtkTargetList*
546 gtk_drag_dest_get_target_list (GtkWidget *widget)
547 {
548   GtkDragDestSite *site;
549
550   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
551   
552   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
553
554   return site ? site->target_list : NULL;  
555 }
556
557 void
558 gtk_drag_dest_set_target_list (GtkWidget      *widget,
559                                GtkTargetList  *target_list)
560 {
561   GtkDragDestSite *site;
562
563   g_return_if_fail (GTK_IS_WIDGET (widget));
564   
565   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
566   
567   if (!site)
568     {
569       g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
570                  "to make the widget into a drag destination");
571       return;
572     }
573
574   if (target_list)
575     gtk_target_list_ref (target_list);
576   
577   if (site->target_list)
578     gtk_target_list_unref (site->target_list);
579
580   site->target_list = target_list;
581
582   register_types (widget, site);
583 }
584
585 void
586 gtk_drag_dest_add_text_targets (GtkWidget *widget)
587 {
588   GtkTargetList *target_list;
589
590   target_list = gtk_drag_dest_get_target_list (widget);
591   if (target_list)
592     gtk_target_list_ref (target_list);
593   else
594     target_list = gtk_target_list_new (NULL, 0);
595   gtk_target_list_add_text_targets (target_list, 0);
596   gtk_drag_dest_set_target_list (widget, target_list);
597   gtk_target_list_unref (target_list);
598 }
599
600 void
601 gtk_drag_dest_add_image_targets (GtkWidget *widget)
602 {
603   GtkTargetList *target_list;
604
605   target_list = gtk_drag_dest_get_target_list (widget);
606   if (target_list)
607     gtk_target_list_ref (target_list);
608   else
609     target_list = gtk_target_list_new (NULL, 0);
610   gtk_target_list_add_image_targets (target_list, 0, FALSE);
611   gtk_drag_dest_set_target_list (widget, target_list);
612   gtk_target_list_unref (target_list);
613 }
614
615 void
616 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
617 {
618   GtkTargetList *target_list;
619
620   target_list = gtk_drag_dest_get_target_list (widget);
621   if (target_list)
622     gtk_target_list_ref (target_list);
623   else
624     target_list = gtk_target_list_new (NULL, 0);
625   gtk_target_list_add_uri_targets (target_list, 0);
626   gtk_drag_dest_set_target_list (widget, target_list);
627   gtk_target_list_unref (target_list);
628 }
629
630 static void
631 prepend_and_ref_widget (GtkWidget *widget,
632                         gpointer   data)
633 {
634   GSList **slist_p = data;
635
636   *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
637 }
638
639 static void
640 gtk_drag_find_widget (GtkWidget       *widget,
641                       GtkDragFindData *data)
642 {
643   GtkAllocation new_allocation;
644   gint allocation_to_window_x = 0;
645   gint allocation_to_window_y = 0;
646   gint x_offset = 0;
647   gint y_offset = 0;
648
649   if (data->found || !gtk_widget_get_mapped (widget) || !gtk_widget_get_sensitive (widget))
650     return;
651
652   /* Note that in the following code, we only count the
653    * position as being inside a WINDOW widget if it is inside
654    * widget->window; points that are outside of widget->window
655    * but within the allocation are not counted. This is consistent
656    * with the way we highlight drag targets.
657    *
658    * data->x,y are relative to widget->parent->window (if
659    * widget is not a toplevel, widget->window otherwise).
660    * We compute the allocation of widget in the same coordinates,
661    * clipping to widget->window, and all intermediate
662    * windows. If data->x,y is inside that, then we translate
663    * our coordinates to be relative to widget->window and
664    * recurse.
665    */  
666   gtk_widget_get_allocation (widget, &new_allocation);
667
668   if (gtk_widget_get_parent (widget))
669     {
670       gint tx, ty;
671       GdkWindow *window = gtk_widget_get_window (widget);
672       GdkWindow *parent_window;
673       GtkAllocation allocation;
674
675       parent_window = gtk_widget_get_window (gtk_widget_get_parent (widget));
676
677       /* Compute the offset from allocation-relative to
678        * window-relative coordinates.
679        */
680       gtk_widget_get_allocation (widget, &allocation);
681       allocation_to_window_x = allocation.x;
682       allocation_to_window_y = allocation.y;
683
684       if (gtk_widget_get_has_window (widget))
685         {
686           /* The allocation is relative to the parent window for
687            * window widgets, not to widget->window.
688            */
689           gdk_window_get_position (window, &tx, &ty);
690           
691           allocation_to_window_x -= tx;
692           allocation_to_window_y -= ty;
693         }
694
695       new_allocation.x = 0 + allocation_to_window_x;
696       new_allocation.y = 0 + allocation_to_window_y;
697       
698       while (window && window != parent_window)
699         {
700           GdkRectangle window_rect = { 0, 0, 0, 0 };
701           
702           window_rect.width = gdk_window_get_width (window);
703           window_rect.height = gdk_window_get_height (window);
704
705           gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
706
707           gdk_window_get_position (window, &tx, &ty);
708           new_allocation.x += tx;
709           x_offset += tx;
710           new_allocation.y += ty;
711           y_offset += ty;
712           
713           window = gdk_window_get_parent (window);
714         }
715
716       if (!window)              /* Window and widget heirarchies didn't match. */
717         return;
718     }
719
720   if (data->toplevel ||
721       ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
722        (data->x < new_allocation.x + new_allocation.width) && 
723        (data->y < new_allocation.y + new_allocation.height)))
724     {
725       /* First, check if the drag is in a valid drop site in
726        * one of our children 
727        */
728       if (GTK_IS_CONTAINER (widget))
729         {
730           GtkDragFindData new_data = *data;
731           GSList *children = NULL;
732           GSList *tmp_list;
733           
734           new_data.x -= x_offset;
735           new_data.y -= y_offset;
736           new_data.found = FALSE;
737           new_data.toplevel = FALSE;
738           
739           /* need to reference children temporarily in case the
740            * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
741            */
742           gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
743           for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
744             {
745               if (!new_data.found && gtk_widget_is_drawable (tmp_list->data))
746                 gtk_drag_find_widget (tmp_list->data, &new_data);
747               g_object_unref (tmp_list->data);
748             }
749           g_slist_free (children);
750           
751           data->found = new_data.found;
752         }
753
754       /* If not, and this widget is registered as a drop site, check to
755        * emit "drag-motion" to check if we are actually in
756        * a drop site.
757        */
758       if (!data->found &&
759           g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
760         {
761           data->found = data->callback (widget,
762                                         data->context,
763                                         data->x - x_offset - allocation_to_window_x,
764                                         data->y - y_offset - allocation_to_window_y,
765                                         data->time);
766           /* If so, send a "drag-leave" to the last widget */
767           if (data->found)
768             {
769               if (data->info->widget && data->info->widget != widget)
770                 {
771                   gtk_drag_dest_leave (data->info->widget, data->context, data->time);
772                 }
773               data->info->widget = widget;
774             }
775         }
776     }
777 }
778
779 static void  
780 gtk_drag_dest_leave (GtkWidget      *widget,
781                      GdkDragContext *context,
782                      guint           time)
783 {
784   GtkDragDestSite *site;
785
786   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
787   g_return_if_fail (site != NULL);
788
789   if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
790     gtk_drag_unhighlight (widget);
791   
792   if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
793       site->track_motion)
794     g_signal_emit_by_name (widget, "drag-leave", context, time);
795   
796   site->have_drag = FALSE;
797 }
798
799 static gboolean
800 gtk_drag_dest_motion (GtkWidget      *widget,
801                       GdkDragContext *context,
802                       gint            x,
803                       gint            y,
804                       guint           time)
805 {
806   GtkDragDestSite *site;
807   GdkDragAction action = 0;
808   gboolean retval;
809
810   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
811   g_return_val_if_fail (site != NULL, FALSE);
812
813   if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
814     {
815       if (gdk_drag_context_get_suggested_action (context) & site->actions)
816         action = gdk_drag_context_get_suggested_action (context);
817       
818       if (action && gtk_drag_dest_find_target (widget, context, NULL))
819         {
820           if (!site->have_drag)
821             {
822               site->have_drag = TRUE;
823               if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
824                 gtk_drag_highlight (widget);
825             }
826           
827           gdk_drag_status (context, action, time);
828         }
829       else
830         {
831           gdk_drag_status (context, 0, time);
832           if (!site->track_motion)
833             return TRUE;
834         }
835     }
836
837   g_signal_emit_by_name (widget, "drag-motion",
838                          context, x, y, time, &retval);
839
840   return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
841 }
842
843 static gboolean
844 gtk_drag_dest_drop (GtkWidget        *widget,
845                     GdkDragContext   *context,
846                     gint              x,
847                     gint              y,
848                     guint             time)
849 {
850   GtkDragDestSite *site;
851   GtkDragDestInfo *info;
852   gboolean retval;
853
854   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
855   g_return_val_if_fail (site != NULL, FALSE);
856
857   info = gtk_drag_get_dest_info (context, FALSE);
858   g_return_val_if_fail (info != NULL, FALSE);
859
860   info->drop_x = x;
861   info->drop_y = y;
862
863   if (site->flags & GTK_DEST_DEFAULT_DROP)
864     {
865       GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
866
867       if (target == GDK_NONE)
868         {
869           gtk_drag_finish (context, FALSE, FALSE, time);
870           return TRUE;
871         }
872       else
873         gtk_drag_get_data (widget, context, target, time);
874     }
875   
876   g_signal_emit_by_name (widget, "drag-drop",
877                          context, x, y, time, &retval);
878
879   return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
880 }
881
882 void
883 gtk_drag_dest_set_track_motion (GtkWidget *widget,
884                                 gboolean   track_motion)
885 {
886   GtkDragDestSite *site;
887
888   g_return_if_fail (GTK_IS_WIDGET (widget));
889
890   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
891   
892   g_return_if_fail (site != NULL);
893
894   site->track_motion = track_motion != FALSE;
895 }
896
897 gboolean
898 gtk_drag_dest_get_track_motion (GtkWidget *widget)
899 {
900   GtkDragDestSite *site;
901
902   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
903
904   site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
905
906   if (site)
907     return site->track_motion;
908
909   return FALSE;
910 }
911
912 void
913 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
914                              GdkEvent  *event)
915 {
916   GtkDragDestInfo *info;
917   GdkDragContext *context;
918
919   g_return_if_fail (toplevel != NULL);
920   g_return_if_fail (event != NULL);
921
922   context = event->dnd.context;
923
924   info = gtk_drag_get_dest_info (context, TRUE);
925
926   /* Find the widget for the event */
927   switch (event->type)
928     {
929     case GDK_DRAG_ENTER:
930       break;
931
932     case GDK_DRAG_LEAVE:
933       if (info->widget)
934         {
935           gtk_drag_dest_leave (info->widget, context, event->dnd.time);
936           info->widget = NULL;
937         }
938       break;
939
940     case GDK_DRAG_MOTION:
941     case GDK_DROP_START:
942       {
943         GtkDragFindData data;
944         gint tx, ty;
945
946         if (event->type == GDK_DROP_START)
947           {
948             info->dropped = TRUE;
949             /* We send a leave here so that the widget unhighlights
950              * properly.
951              */
952             if (info->widget)
953               {
954                 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
955                 info->widget = NULL;
956               }
957           }
958
959         gdk_window_get_position (gtk_widget_get_window (toplevel), &tx, &ty);
960         
961         data.x = event->dnd.x_root - tx;
962         data.y = event->dnd.y_root - ty;
963         data.context = context;
964         data.info = info;
965         data.found = FALSE;
966         data.toplevel = TRUE;
967         data.callback = (event->type == GDK_DRAG_MOTION) ?
968           gtk_drag_dest_motion : gtk_drag_dest_drop;
969         data.time = event->dnd.time;
970         
971         gtk_drag_find_widget (toplevel, &data);
972
973         if (info->widget && !data.found)
974           {
975             gtk_drag_dest_leave (info->widget, context, event->dnd.time);
976             info->widget = NULL;
977           }
978
979         /* Send a reply.
980          */
981         if (event->type == GDK_DRAG_MOTION)
982           {
983             if (!data.found)
984               gdk_drag_status (context, 0, event->dnd.time);
985           }
986
987         break;
988       default:
989         g_assert_not_reached ();
990       }
991     }
992 }
993
994
995 GdkAtom
996 gtk_drag_dest_find_target (GtkWidget      *widget,
997                            GdkDragContext *context,
998                            GtkTargetList  *target_list)
999 {
1000   id <NSDraggingInfo> dragging_info;
1001   NSPasteboard *pasteboard;
1002   GtkWidget *source_widget;
1003   GList *tmp_target;
1004   GList *tmp_source = NULL;
1005   GList *source_targets;
1006
1007   g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1008   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1009
1010   dragging_info = gdk_quartz_drag_context_get_dragging_info_libgtk_only (context);
1011   pasteboard = [dragging_info draggingPasteboard];
1012
1013   source_widget = gtk_drag_get_source_widget (context);
1014
1015   if (target_list == NULL)
1016     target_list = gtk_drag_dest_get_target_list (widget);
1017   
1018   if (target_list == NULL)
1019     return GDK_NONE;
1020
1021   source_targets = _gtk_quartz_pasteboard_types_to_atom_list ([pasteboard types]);
1022   tmp_target = target_list->list;
1023   while (tmp_target)
1024     {
1025       GtkTargetPair *pair = tmp_target->data;
1026       tmp_source = source_targets;
1027       while (tmp_source)
1028         {
1029           if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1030             {
1031               if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1032                   (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
1033                 {
1034                   g_list_free (source_targets);
1035                   return pair->target;
1036                 }
1037               else
1038                 break;
1039             }
1040           tmp_source = tmp_source->next;
1041         }
1042       tmp_target = tmp_target->next;
1043     }
1044
1045   g_list_free (source_targets);
1046   return GDK_NONE;
1047 }
1048
1049 static gboolean
1050 gtk_drag_begin_idle (gpointer arg)
1051 {
1052   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1053   GdkDragContext* context = (GdkDragContext*) arg;
1054   GtkDragSourceInfo* info = gtk_drag_get_source_info (context, FALSE);
1055   NSWindow *nswindow;
1056   NSPasteboard *pasteboard;
1057   GtkDragSourceOwner *owner;
1058   NSPoint point;
1059   NSSet *types;
1060   NSImage *drag_image;
1061
1062   g_assert (info != NULL);
1063
1064   pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
1065   owner = [[GtkDragSourceOwner alloc] initWithInfo:info];
1066
1067   types = _gtk_quartz_target_list_to_pasteboard_types (info->target_list);
1068
1069   [pasteboard declareTypes:[types allObjects] owner:owner];
1070
1071   [owner release];
1072   [types release];
1073
1074   if ((nswindow = get_toplevel_nswindow (info->source_widget)) == NULL)
1075      return FALSE;
1076   
1077   /* Ref the context. It's unreffed when the drag has been aborted */
1078   g_object_ref (info->context);
1079
1080   /* FIXME: If the event isn't a mouse event, use the global cursor position instead */
1081   point = [info->nsevent locationInWindow];
1082
1083   drag_image = _gtk_quartz_create_image_from_pixbuf (info->icon_pixbuf);
1084
1085   point.x -= info->hot_x;
1086   point.y -= info->hot_y;
1087
1088   [nswindow dragImage:drag_image
1089                    at:point
1090                offset:NSMakeSize(0, 0)
1091                 event:info->nsevent
1092            pasteboard:pasteboard
1093                source:nswindow
1094             slideBack:YES];
1095
1096   [info->nsevent release];
1097   [drag_image release];
1098
1099   [pool release];
1100
1101   return FALSE;
1102 }
1103 /* Fake protocol to let us call GdkNSView gdkWindow without including
1104  * gdk/GdkNSView.h (which we can't because it pulls in the internal-only
1105  * gdkwindow.h).
1106  */
1107 @protocol GdkNSView
1108 - (GdkWindow *)gdkWindow;
1109 @end
1110
1111 static GdkDragContext *
1112 gtk_drag_begin_internal (GtkWidget         *widget,
1113                          GtkDragSourceSite *site,
1114                          GtkTargetList     *target_list,
1115                          GdkDragAction      actions,
1116                          gint               button,
1117                          GdkEvent          *event)
1118 {
1119   GtkDragSourceInfo *info;
1120   GdkDevice *pointer;
1121   GdkWindow *window;
1122   GdkDragContext *context = gdk_drag_begin (gtk_widget_get_window (widget),
1123                                             NULL);
1124   NSWindow *nswindow = get_toplevel_nswindow (widget);
1125   NSPoint point = {0, 0};
1126   gdouble x, y;
1127   double time = (double)g_get_real_time ();
1128   NSEvent *nsevent;
1129   NSTimeInterval nstime;
1130
1131   if (event)
1132     {
1133       if (gdk_event_get_coords (event, &x, &y))
1134         {
1135           point.x = x;
1136           point.y = y;
1137         }
1138       time = (double)gdk_event_get_time (event);
1139     }
1140   nstime = [[NSDate dateWithTimeIntervalSince1970: time / 1000] timeIntervalSinceReferenceDate];
1141   nsevent = [NSEvent mouseEventWithType: NSLeftMouseDown
1142                       location: point
1143                       modifierFlags: 0
1144                       timestamp: nstime
1145                       windowNumber: [nswindow windowNumber]
1146                       context: [nswindow graphicsContext]
1147                       eventNumber: 0
1148                       clickCount: 1
1149                       pressure: 0.0 ];
1150
1151   window = [(id<GdkNSView>)[nswindow contentView] gdkWindow];
1152   g_return_val_if_fail(nsevent != NULL, NULL);
1153
1154   context = gdk_drag_begin (window, NULL);
1155   g_return_val_if_fail( context != NULL, NULL);
1156
1157   info = gtk_drag_get_source_info (context, TRUE);
1158   info->nsevent = nsevent;
1159   [info->nsevent retain];
1160
1161   info->source_widget = g_object_ref (widget);
1162   info->widget = g_object_ref (widget);
1163   info->target_list = target_list;
1164   gtk_target_list_ref (target_list);
1165
1166   info->possible_actions = actions;
1167   
1168   g_signal_emit_by_name (widget, "drag-begin", info->context);
1169
1170   /* Ensure that we have an icon before we start the drag; the
1171    * application may have set one in ::drag_begin, or it may
1172    * not have set one.
1173    */
1174   if (!info->icon_pixbuf)
1175     {
1176       if (!site || site->icon_type == GTK_IMAGE_EMPTY)
1177         gtk_drag_set_icon_default (context);
1178       else
1179         switch (site->icon_type)
1180           {
1181           case GTK_IMAGE_PIXBUF:
1182             gtk_drag_set_icon_pixbuf (context,
1183                                       site->icon_data.pixbuf.pixbuf,
1184                                       -2, -2);
1185             break;
1186           case GTK_IMAGE_STOCK:
1187             gtk_drag_set_icon_stock (context,
1188                                      site->icon_data.stock.stock_id,
1189                                      -2, -2);
1190             break;
1191           case GTK_IMAGE_ICON_NAME:
1192             gtk_drag_set_icon_name (context,
1193                                     site->icon_data.name.icon_name,
1194                                     -2, -2);
1195             break;
1196           case GTK_IMAGE_EMPTY:
1197           default:
1198             g_assert_not_reached();
1199             break;
1200           }
1201     }
1202
1203   /* drag will begin in an idle handler to avoid nested run loops */
1204
1205   g_idle_add_full (G_PRIORITY_HIGH_IDLE, gtk_drag_begin_idle, context, NULL);
1206
1207   pointer = gdk_drag_context_get_device (info->context);
1208   gdk_device_ungrab (pointer, 0);
1209
1210   return context;
1211 }
1212
1213 GdkDragContext *
1214 gtk_drag_begin (GtkWidget         *widget,
1215                 GtkTargetList     *targets,
1216                 GdkDragAction      actions,
1217                 gint               button,
1218                 GdkEvent          *event)
1219 {
1220   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1221   g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
1222   g_return_val_if_fail (targets != NULL, NULL);
1223
1224   return gtk_drag_begin_internal (widget, NULL, targets,
1225                                   actions, button, event);
1226 }
1227
1228
1229 static gboolean
1230 gtk_drag_source_event_cb (GtkWidget      *widget,
1231                           GdkEvent       *event,
1232                           gpointer        data)
1233 {
1234   GtkDragSourceSite *site;
1235   gboolean retval = FALSE;
1236   site = (GtkDragSourceSite *)data;
1237
1238   switch (event->type)
1239     {
1240     case GDK_BUTTON_PRESS:
1241       if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1242         {
1243           site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
1244           site->x = event->button.x;
1245           site->y = event->button.y;
1246         }
1247       break;
1248       
1249     case GDK_BUTTON_RELEASE:
1250       if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1251         site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
1252       break;
1253       
1254     case GDK_MOTION_NOTIFY:
1255       if (site->state & event->motion.state & site->start_button_mask)
1256         {
1257           /* FIXME: This is really broken and can leave us
1258            * with a stuck grab
1259            */
1260           int i;
1261           for (i=1; i<6; i++)
1262             {
1263               if (site->state & event->motion.state & 
1264                   GDK_BUTTON1_MASK << (i - 1))
1265                 break;
1266             }
1267
1268           if (gtk_drag_check_threshold (widget, site->x, site->y,
1269                                         event->motion.x, event->motion.y))
1270             {
1271               site->state = 0;
1272               gtk_drag_begin_internal (widget, site, site->target_list,
1273                                        site->actions, 
1274                                        i, event);
1275
1276               retval = TRUE;
1277             }
1278         }
1279       break;
1280       
1281     default:                    /* hit for 2/3BUTTON_PRESS */
1282       break;
1283     }
1284   
1285   return retval;
1286 }
1287
1288 void 
1289 gtk_drag_source_set (GtkWidget            *widget,
1290                      GdkModifierType       start_button_mask,
1291                      const GtkTargetEntry *targets,
1292                      gint                  n_targets,
1293                      GdkDragAction         actions)
1294 {
1295   GtkDragSourceSite *site;
1296
1297   g_return_if_fail (GTK_IS_WIDGET (widget));
1298
1299   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1300
1301   gtk_widget_add_events (widget,
1302                          gtk_widget_get_events (widget) |
1303                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1304                          GDK_BUTTON_MOTION_MASK);
1305
1306   if (site)
1307     {
1308       if (site->target_list)
1309         gtk_target_list_unref (site->target_list);
1310     }
1311   else
1312     {
1313       site = g_new0 (GtkDragSourceSite, 1);
1314
1315       site->icon_type = GTK_IMAGE_EMPTY;
1316       
1317       g_signal_connect (widget, "button-press-event",
1318                         G_CALLBACK (gtk_drag_source_event_cb),
1319                         site);
1320       g_signal_connect (widget, "button-release-event",
1321                         G_CALLBACK (gtk_drag_source_event_cb),
1322                         site);
1323       g_signal_connect (widget, "motion-notify-event",
1324                         G_CALLBACK (gtk_drag_source_event_cb),
1325                         site);
1326       
1327       g_object_set_data_full (G_OBJECT (widget),
1328                               I_("gtk-site-data"), 
1329                               site, gtk_drag_source_site_destroy);
1330     }
1331
1332   site->start_button_mask = start_button_mask;
1333
1334   site->target_list = gtk_target_list_new (targets, n_targets);
1335
1336   site->actions = actions;
1337 }
1338
1339 /*************************************************************
1340  * gtk_drag_source_unset
1341  *     Unregister this widget as a drag source.
1342  *   arguments:
1343  *     widget:
1344  *   results:
1345  *************************************************************/
1346
1347 void 
1348 gtk_drag_source_unset (GtkWidget *widget)
1349 {
1350   GtkDragSourceSite *site;
1351
1352   g_return_if_fail (GTK_IS_WIDGET (widget));
1353
1354   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1355
1356   if (site)
1357     {
1358       g_signal_handlers_disconnect_by_func (widget,
1359                                             gtk_drag_source_event_cb,
1360                                             site);
1361       g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
1362     }
1363 }
1364
1365 GtkTargetList *
1366 gtk_drag_source_get_target_list (GtkWidget *widget)
1367 {
1368   GtkDragSourceSite *site;
1369
1370   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1371
1372   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1373
1374   return site ? site->target_list : NULL;
1375
1376 }
1377
1378 void
1379 gtk_drag_source_set_target_list (GtkWidget     *widget,
1380                                  GtkTargetList *target_list)
1381 {
1382   GtkDragSourceSite *site;
1383
1384   g_return_if_fail (GTK_IS_WIDGET (widget));
1385
1386   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1387   if (site == NULL)
1388     {
1389       g_warning ("gtk_drag_source_set_target_list() requires the widget "
1390                  "to already be a drag source.");
1391       return;
1392     }
1393
1394   if (target_list)
1395     gtk_target_list_ref (target_list);
1396
1397   if (site->target_list)
1398     gtk_target_list_unref (site->target_list);
1399
1400   site->target_list = target_list;
1401 }
1402
1403 /**
1404  * gtk_drag_source_add_text_targets:
1405  * @widget: a #GtkWidget that's is a drag source
1406  *
1407  * Add the text targets supported by #GtkSelection to
1408  * the target list of the drag source.  The targets
1409  * are added with @info = 0. If you need another value, 
1410  * use gtk_target_list_add_text_targets() and
1411  * gtk_drag_source_set_target_list().
1412  * 
1413  * Since: 2.6
1414  **/
1415 void
1416 gtk_drag_source_add_text_targets (GtkWidget *widget)
1417 {
1418   GtkTargetList *target_list;
1419
1420   target_list = gtk_drag_source_get_target_list (widget);
1421   if (target_list)
1422     gtk_target_list_ref (target_list);
1423   else
1424     target_list = gtk_target_list_new (NULL, 0);
1425   gtk_target_list_add_text_targets (target_list, 0);
1426   gtk_drag_source_set_target_list (widget, target_list);
1427   gtk_target_list_unref (target_list);
1428 }
1429
1430 void
1431 gtk_drag_source_add_image_targets (GtkWidget *widget)
1432 {
1433   GtkTargetList *target_list;
1434
1435   target_list = gtk_drag_source_get_target_list (widget);
1436   if (target_list)
1437     gtk_target_list_ref (target_list);
1438   else
1439     target_list = gtk_target_list_new (NULL, 0);
1440   gtk_target_list_add_image_targets (target_list, 0, TRUE);
1441   gtk_drag_source_set_target_list (widget, target_list);
1442   gtk_target_list_unref (target_list);
1443 }
1444
1445 void
1446 gtk_drag_source_add_uri_targets (GtkWidget *widget)
1447 {
1448   GtkTargetList *target_list;
1449
1450   target_list = gtk_drag_source_get_target_list (widget);
1451   if (target_list)
1452     gtk_target_list_ref (target_list);
1453   else
1454     target_list = gtk_target_list_new (NULL, 0);
1455   gtk_target_list_add_uri_targets (target_list, 0);
1456   gtk_drag_source_set_target_list (widget, target_list);
1457   gtk_target_list_unref (target_list);
1458 }
1459
1460 static void
1461 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
1462 {
1463   switch (site->icon_type)
1464     {
1465     case GTK_IMAGE_EMPTY:
1466       break;
1467     case GTK_IMAGE_PIXBUF:
1468       g_object_unref (site->icon_data.pixbuf.pixbuf);
1469       break;
1470     case GTK_IMAGE_STOCK:
1471       g_free (site->icon_data.stock.stock_id);
1472       break;
1473     case GTK_IMAGE_ICON_NAME:
1474       g_free (site->icon_data.name.icon_name);
1475       break;
1476     default:
1477       g_assert_not_reached();
1478       break;
1479     }
1480   site->icon_type = GTK_IMAGE_EMPTY;
1481 }
1482
1483 static void 
1484 gtk_drag_source_site_destroy (gpointer data)
1485 {
1486   GtkDragSourceSite *site = data;
1487
1488   if (site->target_list)
1489     gtk_target_list_unref (site->target_list);
1490
1491   gtk_drag_source_unset_icon (site);
1492   g_free (site);
1493 }
1494
1495 void 
1496 gtk_drag_source_set_icon_pixbuf (GtkWidget   *widget,
1497                                  GdkPixbuf   *pixbuf)
1498 {
1499   GtkDragSourceSite *site;
1500
1501   g_return_if_fail (GTK_IS_WIDGET (widget));
1502   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1503
1504   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1505   g_return_if_fail (site != NULL); 
1506   g_object_ref (pixbuf);
1507
1508   gtk_drag_source_unset_icon (site);
1509
1510   site->icon_type = GTK_IMAGE_PIXBUF;
1511   site->icon_data.pixbuf.pixbuf = pixbuf;
1512 }
1513
1514 /**
1515  * gtk_drag_source_set_icon_stock:
1516  * @widget: a #GtkWidget
1517  * @stock_id: the ID of the stock icon to use
1518  *
1519  * Sets the icon that will be used for drags from a particular source
1520  * to a stock icon. 
1521  **/
1522 void 
1523 gtk_drag_source_set_icon_stock (GtkWidget   *widget,
1524                                 const gchar *stock_id)
1525 {
1526   GtkDragSourceSite *site;
1527
1528   g_return_if_fail (GTK_IS_WIDGET (widget));
1529   g_return_if_fail (stock_id != NULL);
1530
1531   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1532   g_return_if_fail (site != NULL);
1533   
1534   gtk_drag_source_unset_icon (site);
1535
1536   site->icon_type = GTK_IMAGE_STOCK;
1537   site->icon_data.stock.stock_id = g_strdup (stock_id);
1538 }
1539
1540 /**
1541  * gtk_drag_source_set_icon_name:
1542  * @widget: a #GtkWidget
1543  * @icon_name: name of icon to use
1544  * 
1545  * Sets the icon that will be used for drags from a particular source
1546  * to a themed icon. See the docs for #GtkIconTheme for more details.
1547  *
1548  * Since: 2.8
1549  **/
1550 void 
1551 gtk_drag_source_set_icon_name (GtkWidget   *widget,
1552                                const gchar *icon_name)
1553 {
1554   GtkDragSourceSite *site;
1555
1556   g_return_if_fail (GTK_IS_WIDGET (widget));
1557   g_return_if_fail (icon_name != NULL);
1558
1559   site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1560   g_return_if_fail (site != NULL);
1561
1562   gtk_drag_source_unset_icon (site);
1563
1564   site->icon_type = GTK_IMAGE_ICON_NAME;
1565   site->icon_data.name.icon_name = g_strdup (icon_name);
1566 }
1567
1568
1569 /**
1570  * gtk_drag_set_icon_widget:
1571  * @context: the context for a drag. (This must be called 
1572           with a  context for the source side of a drag)
1573  * @widget: a toplevel window to use as an icon.
1574  * @hot_x: the X offset within @widget of the hotspot.
1575  * @hot_y: the Y offset within @widget of the hotspot.
1576  * 
1577  * Changes the icon for a widget to a given widget. GTK+
1578  * will not destroy the icon, so if you don't want
1579  * it to persist, you should connect to the "drag-end" 
1580  * signal and destroy it yourself.
1581  **/
1582 void 
1583 gtk_drag_set_icon_widget (GdkDragContext    *context,
1584                           GtkWidget         *widget,
1585                           gint               hot_x,
1586                           gint               hot_y)
1587 {
1588   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1589   g_return_if_fail (GTK_IS_WIDGET (widget));
1590
1591   g_warning ("gtk_drag_set_icon_widget is not supported on Mac OS X");
1592 }
1593
1594 static void
1595 set_icon_stock_pixbuf (GdkDragContext    *context,
1596                        const gchar       *stock_id,
1597                        GdkPixbuf         *pixbuf,
1598                        gint               hot_x,
1599                        gint               hot_y)
1600 {
1601   GtkDragSourceInfo *info;
1602
1603   info = gtk_drag_get_source_info (context, FALSE);
1604
1605   if (stock_id)
1606     {
1607       pixbuf = gtk_widget_render_icon_pixbuf (info->widget, stock_id,
1608                                               GTK_ICON_SIZE_DND);
1609
1610       if (!pixbuf)
1611         {
1612           g_warning ("Cannot load drag icon from stock_id %s", stock_id);
1613           return;
1614         }
1615     }
1616   else
1617     g_object_ref (pixbuf);
1618
1619   if (info->icon_pixbuf)
1620     g_object_unref (info->icon_pixbuf);
1621   info->icon_pixbuf = pixbuf;
1622   info->hot_x = hot_x;
1623   info->hot_y = hot_y;
1624 }
1625
1626 /**
1627  * gtk_drag_set_icon_pixbuf:
1628  * @context: the context for a drag. (This must be called 
1629  *            with a  context for the source side of a drag)
1630  * @pixbuf: the #GdkPixbuf to use as the drag icon.
1631  * @hot_x: the X offset within @widget of the hotspot.
1632  * @hot_y: the Y offset within @widget of the hotspot.
1633  * 
1634  * Sets @pixbuf as the icon for a given drag.
1635  **/
1636 void 
1637 gtk_drag_set_icon_pixbuf  (GdkDragContext *context,
1638                            GdkPixbuf      *pixbuf,
1639                            gint            hot_x,
1640                            gint            hot_y)
1641 {
1642   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1643   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1644
1645   set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1646 }
1647
1648 /**
1649  * gtk_drag_set_icon_stock:
1650  * @context: the context for a drag. (This must be called 
1651  *            with a  context for the source side of a drag)
1652  * @stock_id: the ID of the stock icon to use for the drag.
1653  * @hot_x: the X offset within the icon of the hotspot.
1654  * @hot_y: the Y offset within the icon of the hotspot.
1655  * 
1656  * Sets the icon for a given drag from a stock ID.
1657  **/
1658 void 
1659 gtk_drag_set_icon_stock  (GdkDragContext *context,
1660                           const gchar    *stock_id,
1661                           gint            hot_x,
1662                           gint            hot_y)
1663 {
1664
1665   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1666   g_return_if_fail (stock_id != NULL);
1667
1668   set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y);
1669 }
1670
1671
1672 /* XXX: This function is in gdk, too. Should it be in Cairo? */
1673 static gboolean
1674 _gtk_cairo_surface_extents (cairo_surface_t *surface,
1675                             GdkRectangle *extents)
1676 {
1677   double x1, x2, y1, y2;
1678   cairo_t *cr;
1679
1680   g_return_val_if_fail (surface != NULL, FALSE);
1681   g_return_val_if_fail (extents != NULL, FALSE);
1682
1683   cr = cairo_create (surface);
1684   cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
1685
1686   x1 = floor (x1);
1687   y1 = floor (y1);
1688   x2 = ceil (x2);
1689   y2 = ceil (y2);
1690   x2 -= x1;
1691   y2 -= y1;
1692   
1693   if (x1 < G_MININT || x1 > G_MAXINT ||
1694       y1 < G_MININT || y1 > G_MAXINT ||
1695       x2 > G_MAXINT || y2 > G_MAXINT)
1696     {
1697       extents->x = extents->y = extents->width = extents->height = 0;
1698       return FALSE;
1699     }
1700
1701   extents->x = x1;
1702   extents->y = y1;
1703   extents->width = x2;
1704   extents->height = y2;
1705
1706   return TRUE;
1707 }
1708
1709 /**
1710  * gtk_drag_set_icon_surface:
1711  * @context: the context for a drag. (This must be called
1712  *            with a context for the source side of a drag)
1713  * @surface: the surface to use as icon
1714  *
1715  * Sets @surface as the icon for a given drag. GTK+ retains
1716  * references for the arguments, and will release them when
1717  * they are no longer needed.
1718  *
1719  * To position the surface relative to the mouse, use
1720  * cairo_surface_set_device_offset() on @surface. The mouse
1721  * cursor will be positioned at the (0,0) coordinate of the
1722  * surface.
1723  **/
1724 void
1725 gtk_drag_set_icon_surface (GdkDragContext  *context,
1726                            cairo_surface_t *surface)
1727 {
1728   GdkPixbuf *pixbuf;
1729   GdkRectangle extents;
1730   double x_offset, y_offset;
1731
1732   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1733   g_return_if_fail (surface != NULL);
1734
1735   _gtk_cairo_surface_extents (surface, &extents);
1736   cairo_surface_get_device_offset (surface, &x_offset, &y_offset);
1737
1738   pixbuf = gdk_pixbuf_get_from_surface (surface,
1739                                         extents.x, extents.y,
1740                                         extents.width, extents.height);
1741   gtk_drag_set_icon_pixbuf (context, pixbuf, -x_offset, -y_offset);
1742   g_object_unref (pixbuf);
1743 }
1744
1745 /**
1746  * gtk_drag_set_icon_name:
1747  * @context: the context for a drag. (This must be called 
1748  *            with a context for the source side of a drag)
1749  * @icon_name: name of icon to use
1750  * @hot_x: the X offset of the hotspot within the icon
1751  * @hot_y: the Y offset of the hotspot within the icon
1752  * 
1753  * Sets the icon for a given drag from a named themed icon. See
1754  * the docs for #GtkIconTheme for more details. Note that the
1755  * size of the icon depends on the icon theme (the icon is
1756  * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus 
1757  * @hot_x and @hot_y have to be used with care.
1758  *
1759  * Since: 2.8
1760  **/
1761 void 
1762 gtk_drag_set_icon_name (GdkDragContext *context,
1763                         const gchar    *icon_name,
1764                         gint            hot_x,
1765                         gint            hot_y)
1766 {
1767   GdkScreen *screen;
1768   GtkSettings *settings;
1769   GtkIconTheme *icon_theme;
1770   GdkPixbuf *pixbuf;
1771   gint width, height, icon_size;
1772
1773   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1774   g_return_if_fail (icon_name != NULL);
1775
1776   screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
1777   g_return_if_fail (screen != NULL);
1778
1779   settings = gtk_settings_get_for_screen (screen);
1780   if (gtk_icon_size_lookup_for_settings (settings,
1781                                          GTK_ICON_SIZE_DND,
1782                                          &width, &height))
1783     icon_size = MAX (width, height);
1784   else 
1785     icon_size = 32; /* default value for GTK_ICON_SIZE_DND */ 
1786
1787   icon_theme = gtk_icon_theme_get_for_screen (screen);
1788
1789   pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
1790                                      icon_size, 0, NULL);
1791   if (pixbuf)
1792     set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1793   else
1794     g_warning ("Cannot load drag icon from icon name %s", icon_name);
1795 }
1796
1797 /**
1798  * gtk_drag_set_icon_default:
1799  * @context: the context for a drag. (This must be called 
1800              with a  context for the source side of a drag)
1801  * 
1802  * Sets the icon for a particular drag to the default
1803  * icon.
1804  **/
1805 void 
1806 gtk_drag_set_icon_default (GdkDragContext    *context)
1807 {
1808   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1809
1810   gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
1811 }
1812
1813 static void
1814 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
1815 {
1816   NSPasteboard *pasteboard;
1817   NSAutoreleasePool *pool;
1818
1819   if (info->icon_pixbuf)
1820     g_object_unref (info->icon_pixbuf);
1821
1822   g_signal_emit_by_name (info->widget, "drag-end", 
1823                          info->context);
1824
1825   if (info->source_widget)
1826     g_object_unref (info->source_widget);
1827
1828   if (info->widget)
1829     g_object_unref (info->widget);
1830
1831   gtk_target_list_unref (info->target_list);
1832
1833   pool = [[NSAutoreleasePool alloc] init];
1834
1835   /* Empty the pasteboard, so that it will not accidentally access
1836    * info->context after it has been destroyed.
1837    */
1838   pasteboard = [NSPasteboard pasteboardWithName: NSDragPboard];
1839   [pasteboard declareTypes: nil owner: nil];
1840
1841   [pool release];
1842
1843   gtk_drag_clear_source_info (info->context);
1844   g_object_unref (info->context);
1845
1846   g_free (info);
1847   info = NULL;
1848 }
1849
1850 static gboolean
1851 drag_drop_finished_idle_cb (gpointer data)
1852 {
1853   gtk_drag_source_info_destroy (data);
1854   return FALSE;
1855 }
1856
1857 static void
1858 gtk_drag_drop_finished (GtkDragSourceInfo *info)
1859 {
1860   if (info->success && info->delete)
1861     g_signal_emit_by_name (info->source_widget, "drag-data-delete",
1862                            info->context);
1863
1864   /* Workaround for the fact that the NS API blocks until the drag is
1865    * over. This way the context is still valid when returning from
1866    * drag_begin, even if it will still be quite useless. See bug #501588.
1867   */
1868   g_idle_add (drag_drop_finished_idle_cb, info);
1869 }
1870
1871 /*************************************************************
1872  * _gtk_drag_source_handle_event:
1873  *     Called from widget event handling code on Drag events
1874  *     for drag sources.
1875  *
1876  *   arguments:
1877  *     toplevel: Toplevel widget that received the event
1878  *     event:
1879  *   results:
1880  *************************************************************/
1881
1882 void
1883 _gtk_drag_source_handle_event (GtkWidget *widget,
1884                                GdkEvent  *event)
1885 {
1886   GtkDragSourceInfo *info;
1887   GdkDragContext *context;
1888
1889   g_return_if_fail (widget != NULL);
1890   g_return_if_fail (event != NULL);
1891
1892   context = event->dnd.context;
1893   info = gtk_drag_get_source_info (context, FALSE);
1894   if (!info)
1895     return;
1896
1897   switch (event->type)
1898     {
1899     case GDK_DROP_FINISHED:
1900       gtk_drag_drop_finished (info);
1901       break;
1902     default:
1903       g_assert_not_reached ();
1904     }  
1905 }
1906
1907
1908 gboolean
1909 gtk_drag_check_threshold (GtkWidget *widget,
1910                           gint       start_x,
1911                           gint       start_y,
1912                           gint       current_x,
1913                           gint       current_y)
1914 {
1915   gint drag_threshold;
1916
1917   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1918
1919   g_object_get (gtk_widget_get_settings (widget),
1920                 "gtk-dnd-drag-threshold", &drag_threshold,
1921                 NULL);
1922   
1923   return (ABS (current_x - start_x) > drag_threshold ||
1924           ABS (current_y - start_y) > drag_threshold);
1925 }