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