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