]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkdnd.c
5a220c6423315d695f4c32582f273bac0dd27449
[~andy/gtk] / gdk / win32 / gdkdnd.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3  * Copyright (C) 1998-1999 Tor Lillqvist
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "config.h"
29
30 #include <string.h>
31
32 #define INITGUID
33
34 #include "gdkdnd.h"
35 #include "gdkproperty.h"
36 #include "gdkprivate.h"
37 #include "gdkx.h"
38
39 #ifdef OLE2_DND
40 #include <ole2.h>
41 #endif
42
43 #ifdef _MSC_VER                 /* These aren't in mingw32 */
44 #include <shlobj.h>
45 #include <shlguid.h>
46 #endif
47
48 #include <gdk/gdk.h>
49
50 typedef struct _GdkDragContextPrivate GdkDragContextPrivate;
51
52 typedef enum {
53   GDK_DRAG_STATUS_DRAG,
54   GDK_DRAG_STATUS_MOTION_WAIT,
55   GDK_DRAG_STATUS_ACTION_WAIT,
56   GDK_DRAG_STATUS_DROP
57 } GtkDragStatus;
58
59 typedef enum {
60   GDK_DRAG_SOURCE,
61   GDK_DRAG_TARGET
62 } GdkDragKind;
63
64 #ifdef OLE2_DND
65
66 #define PRINT_RIID(riid) \
67   g_print ("riid = %.08x-%.04x-%.04x-%.02x%.02x-%.02x%.02x%.02x%.02x%.02x%.02x", \
68            ((gulong *)  riid)[0], \
69            ((gushort *) riid)[2], \
70            ((gushort *) riid)[3], \
71            ((guchar *)  riid)[8], \
72            ((guchar *)  riid)[9], \
73            ((guchar *)  riid)[10], \
74            ((guchar *)  riid)[11], \
75            ((guchar *)  riid)[12], \
76            ((guchar *)  riid)[13], \
77            ((guchar *)  riid)[14], \
78            ((guchar *)  riid)[15]);
79
80
81 HRESULT STDMETHODCALLTYPE
82   m_query_interface_target (IDropTarget __RPC_FAR *This,
83                             /* [in] */ REFIID riid,
84                             /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject);
85
86 ULONG STDMETHODCALLTYPE
87   m_add_ref_target (IDropTarget __RPC_FAR *This);
88
89 ULONG STDMETHODCALLTYPE
90   m_release_target (IDropTarget __RPC_FAR *This);
91
92 HRESULT STDMETHODCALLTYPE 
93   m_drag_enter (IDropTarget __RPC_FAR *This,
94                 /* [unique][in] */ IDataObject __RPC_FAR *pDataObj,
95                 /* [in] */ DWORD grfKeyState,
96                 /* [in] */ POINTL pt,
97                 /* [out][in] */ DWORD __RPC_FAR *pdwEffect);
98
99 HRESULT STDMETHODCALLTYPE
100   m_drag_over (IDropTarget __RPC_FAR *This,
101                /* [in] */ DWORD grfKeyState,
102                /* [in] */ POINTL pt,
103                /* [out][in] */ DWORD __RPC_FAR *pdwEffect);
104
105 HRESULT STDMETHODCALLTYPE
106   m_drag_leave (IDropTarget __RPC_FAR *This);
107
108 HRESULT STDMETHODCALLTYPE
109   m_drop (IDropTarget __RPC_FAR *This,
110           /* [unique][in] */ IDataObject __RPC_FAR *pDataObj,
111           /* [in] */ DWORD grfKeyState,
112           /* [in] */ POINTL pt,
113           /* [out][in] */ DWORD __RPC_FAR *pdwEffect);
114
115 HRESULT STDMETHODCALLTYPE
116   m_query_interface_source (IDropSource __RPC_FAR *This,
117                             /* [in] */ REFIID riid,
118                             /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject);
119
120 ULONG STDMETHODCALLTYPE
121   m_add_ref_source (IDropSource __RPC_FAR *This);
122
123 ULONG STDMETHODCALLTYPE
124   m_release_source (IDropSource __RPC_FAR *This);
125
126 HRESULT STDMETHODCALLTYPE
127   m_query_continue_drag (IDropSource __RPC_FAR *This,
128                          /* [in] */ BOOL fEscapePressed,
129                          /* [in] */ DWORD grfKeyState);
130 HRESULT STDMETHODCALLTYPE
131   m_give_feedback (IDropSource __RPC_FAR *This,
132                    /* [in] */ DWORD dwEffect);
133
134 #endif /* OLE2_DND */
135
136 /* Structure that holds information about a drag in progress.
137  * this is used on both source and destination sides.
138  */
139 struct _GdkDragContextPrivate {
140   GdkDragContext context;
141
142   guint   ref_count;
143
144   guint16 last_x;               /* Coordinates from last event */
145   guint16 last_y;
146   HWND  dest_xid;
147   guint drag_status;            /* Current status of drag */
148 };
149
150 GdkDragContext *current_dest_drag = NULL;
151
152 /* Drag Contexts */
153
154 static GList *contexts;
155
156 GdkDragContext *
157 gdk_drag_context_new        (void)
158 {
159   GdkDragContextPrivate *result;
160
161   result = g_new0 (GdkDragContextPrivate, 1);
162
163   result->ref_count = 1;
164
165   contexts = g_list_prepend (contexts, result);
166
167   return (GdkDragContext *)result;
168 }
169
170 #ifdef OLE2_DND
171
172 typedef struct {
173   IDropTarget idt;
174   GdkDragContext *context;
175 } target_drag_context;
176
177 typedef struct {
178   IDropSource ids;
179   GdkDragContext *context;
180 } source_drag_context;
181
182 HRESULT STDMETHODCALLTYPE
183 m_query_interface_target (IDropTarget __RPC_FAR *This,
184                           /* [in] */ REFIID riid,
185                           /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject)
186 {
187   GDK_NOTE (DND, g_print ("m_query_interface_target\n"));
188
189   *ppvObject = NULL;
190
191   PRINT_RIID (riid);
192
193   if (IsEqualGUID (riid, &IID_IUnknown))
194     {
195       g_print ("...IUnknown\n");
196       m_add_ref_target (This);
197       *ppvObject = This;
198       return S_OK;
199     }
200   else if (IsEqualGUID (riid, &IID_IDropTarget))
201     {
202       g_print ("...IDropTarget\n");
203       m_add_ref_target (This);
204       *ppvObject = This;
205       return S_OK;
206     }
207   else
208     {
209       g_print ("...Huh?\n");
210       return E_NOINTERFACE;
211     }
212 }
213
214 ULONG STDMETHODCALLTYPE
215 m_add_ref_target (IDropTarget __RPC_FAR *This)
216 {
217   target_drag_context *ctx = (target_drag_context *) This;
218   GdkDragContextPrivate *private = (GdkDragContextPrivate *) ctx->context;
219
220   GDK_NOTE (DND, g_print ("m_add_ref_target\n"));
221   gdk_drag_context_ref (ctx->context);
222   
223   return private->ref_count;
224 }
225
226 ULONG STDMETHODCALLTYPE
227 m_release_target (IDropTarget __RPC_FAR *This)
228 {
229   target_drag_context *ctx = (target_drag_context *) This;
230   GdkDragContextPrivate *private = (GdkDragContextPrivate *) ctx->context;
231
232   GDK_NOTE (DND, g_print ("m_release_target\n"));
233   gdk_drag_context_unref (ctx->context);
234
235   if (private->ref_count == 1)
236     {
237       gdk_drag_context_unref (ctx->context);
238       return 0;
239     }
240   else
241     return private->ref_count - 1;
242 }
243
244 HRESULT STDMETHODCALLTYPE 
245 m_drag_enter (IDropTarget __RPC_FAR *This,
246               /* [unique][in] */ IDataObject __RPC_FAR *pDataObj,
247               /* [in] */ DWORD grfKeyState,
248               /* [in] */ POINTL pt,
249               /* [out][in] */ DWORD __RPC_FAR *pdwEffect)
250 {
251   GDK_NOTE (DND, g_print ("m_drag_enter\n"));
252   return E_UNEXPECTED;
253 }
254
255 HRESULT STDMETHODCALLTYPE
256 m_drag_over (IDropTarget __RPC_FAR *This,
257              /* [in] */ DWORD grfKeyState,
258              /* [in] */ POINTL pt,
259              /* [out][in] */ DWORD __RPC_FAR *pdwEffect)
260 {
261   GDK_NOTE (DND, g_print ("m_drag_over\n"));
262   return E_UNEXPECTED;
263 }
264
265 HRESULT STDMETHODCALLTYPE
266 m_drag_leave (IDropTarget __RPC_FAR *This)
267 {
268   GDK_NOTE (DND, g_print ("m_drag_leave\n"));
269   return E_UNEXPECTED;
270 }
271
272 HRESULT STDMETHODCALLTYPE
273 m_drop (IDropTarget __RPC_FAR *This,
274         /* [unique][in] */ IDataObject __RPC_FAR *pDataObj,
275         /* [in] */ DWORD grfKeyState,
276         /* [in] */ POINTL pt,
277         /* [out][in] */ DWORD __RPC_FAR *pdwEffect)
278 {
279   GDK_NOTE (DND, g_print ("m_drop\n"));
280   return E_UNEXPECTED;
281 }
282
283 HRESULT STDMETHODCALLTYPE
284 m_query_interface_source (IDropSource __RPC_FAR *This,
285                           /* [in] */ REFIID riid,
286                           /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject)
287 {
288   GDK_NOTE (DND, g_print ("m_query_interface_source\n"));
289
290   *ppvObject = NULL;
291
292   PRINT_RIID (riid);
293   if (IsEqualGUID (riid, &IID_IUnknown))
294     {
295       g_print ("...IUnknown\n");
296       m_add_ref_source (This);
297       *ppvObject = This;
298       return S_OK;
299     }
300   else if (IsEqualGUID (riid, &IID_IDropSource))
301     {
302       g_print ("...IDropSource\n");
303       m_add_ref_source (This);
304       *ppvObject = This;
305       return S_OK;
306     }
307   else
308     {
309       g_print ("...Huh?\n");
310       return E_NOINTERFACE;
311     }
312 }
313
314 ULONG STDMETHODCALLTYPE
315 m_add_ref_source (IDropSource __RPC_FAR *This)
316 {
317   source_drag_context *ctx = (source_drag_context *) This;
318   GdkDragContextPrivate *private = (GdkDragContextPrivate *) ctx->context;
319
320   GDK_NOTE (DND, g_print ("m_add_ref_source\n"));
321   gdk_drag_context_ref (ctx->context);
322   
323   return private->ref_count;
324 }
325
326 ULONG STDMETHODCALLTYPE
327 m_release_source (IDropSource __RPC_FAR *This)
328 {
329   source_drag_context *ctx = (source_drag_context *) This;
330   GdkDragContextPrivate *private = (GdkDragContextPrivate *) ctx->context;
331
332   GDK_NOTE (DND, g_print ("m_release_source\n"));
333   gdk_drag_context_unref (ctx->context);
334
335   if (private->ref_count == 1)
336     {
337       gdk_drag_context_unref (ctx->context);
338       return 0;
339     }
340   else
341     return private->ref_count - 1;
342 }
343
344 HRESULT STDMETHODCALLTYPE
345 m_query_continue_drag (IDropSource __RPC_FAR *This,
346                        /* [in] */ BOOL fEscapePressed,
347                        /* [in] */ DWORD grfKeyState)
348 {
349   GDK_NOTE (DND, g_print ("m_query_continue_drag\n"));
350   return E_UNEXPECTED;
351 }
352
353 HRESULT STDMETHODCALLTYPE
354 m_give_feedback (IDropSource __RPC_FAR *This,
355                  /* [in] */ DWORD dwEffect)
356 {
357   GDK_NOTE (DND, g_print ("m_give_feedback\n"));
358   return E_UNEXPECTED;
359 }
360
361 static IDropTargetVtbl idt_vtbl = {
362   m_query_interface_target,
363   m_add_ref_target,
364   m_release_target,
365   m_drag_enter,
366   m_drag_over,
367   m_drag_leave,
368   m_drop
369 };
370
371 static IDropSourceVtbl ids_vtbl = {
372   m_query_interface_source,
373   m_add_ref_source,
374   m_release_source,
375   m_query_continue_drag,
376   m_give_feedback
377 };
378
379 target_drag_context *
380 target_context_new (void)
381 {
382   target_drag_context *result;
383
384   result = g_new0 (target_drag_context, 1);
385
386   result->idt.lpVtbl = &idt_vtbl;
387
388   result->context = gdk_drag_context_new ();
389
390   return result;
391 }
392
393 source_drag_context *
394 source_context_new (void)
395 {
396   source_drag_context *result;
397
398   result = g_new0 (source_drag_context, 1);
399
400   result->ids.lpVtbl = &ids_vtbl;
401
402   result->context = gdk_drag_context_new ();
403
404   return result;
405 }
406
407 #endif /* OLE2_DND */
408
409 void
410 gdk_drag_context_ref (GdkDragContext *context)
411 {
412   g_return_if_fail (context != NULL);
413
414   ((GdkDragContextPrivate *)context)->ref_count++;
415 }
416
417 void
418 gdk_drag_context_unref (GdkDragContext *context)
419 {
420   GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
421   
422   g_return_if_fail (context != NULL);
423
424   private->ref_count--;
425
426   GDK_NOTE (DND, g_print ("gdk_drag_context_unref: %d%s\n",
427                           private->ref_count,
428                           (private->ref_count == 0 ? " freeing" : "")));
429
430   if (private->ref_count == 0)
431     {
432       g_dataset_destroy (private);
433       
434       g_list_free (context->targets);
435
436       if (context->source_window)
437         gdk_window_unref (context->source_window);
438
439       if (context->dest_window)
440         gdk_window_unref (context->dest_window);
441
442       contexts = g_list_remove (contexts, private);
443       g_free (private);
444     }
445 }
446
447 #if 0
448
449 static GdkDragContext *
450 gdk_drag_context_find (gboolean is_source,
451                        HWND     source_xid,
452                        HWND     dest_xid)
453 {
454   GList *tmp_list = contexts;
455   GdkDragContext *context;
456
457   while (tmp_list)
458     {
459       context = (GdkDragContext *)tmp_list->data;
460
461       if ((!context->is_source == !is_source) &&
462           ((source_xid == None) || (context->source_window &&
463             (GDK_WINDOW_XWINDOW (context->source_window) == source_xid))) &&
464           ((dest_xid == None) || (context->dest_window &&
465             (GDK_WINDOW_XWINDOW (context->dest_window) == dest_xid))))
466         return context;
467       
468       tmp_list = tmp_list->next;
469     }
470
471   return NULL;
472 }
473
474 #endif
475
476 #ifdef _MSC_VER
477
478 /* From MS Knowledge Base article Q130698 */
479
480 /* resolve_link() fills the filename and path buffer
481  * with relevant information
482  * hWnd         - calling app's window handle.
483  *
484  * lpszLinkName - name of the link file passed into the function.
485  *
486  * lpszPath     - the buffer that will receive the file pathname.
487  */
488
489 static HRESULT 
490 resolve_link(HWND hWnd,
491              LPCTSTR lpszLinkName,
492              LPSTR lpszPath,
493              LPSTR lpszDescription)
494 {
495   HRESULT hres;
496   IShellLink *psl;
497   WIN32_FIND_DATA wfd;
498
499   /* Assume Failure to start with: */
500   *lpszPath = 0;
501   if (lpszDescription)
502     *lpszDescription = 0;
503
504   /* Call CoCreateInstance to obtain the IShellLink interface
505    * pointer. This call fails if CoInitialize is not called, so it is
506    * assumed that CoInitialize has been called.
507    */
508
509   hres = CoCreateInstance (&CLSID_ShellLink,
510                            NULL,
511                            CLSCTX_INPROC_SERVER,
512                            &IID_IShellLink,
513                            (LPVOID *)&psl);
514   if (SUCCEEDED (hres))
515    {
516      IPersistFile *ppf;
517      
518      /* The IShellLink interface supports the IPersistFile
519       * interface. Get an interface pointer to it.
520       */
521      hres = psl->lpVtbl->QueryInterface (psl,
522                                          &IID_IPersistFile,
523                                          (LPVOID *) &ppf);
524      if (SUCCEEDED (hres))
525        {
526          WORD wsz[MAX_PATH];
527
528          /* Convert the given link name string to wide character string. */
529          MultiByteToWideChar (CP_ACP, 0,
530                               lpszLinkName,
531                               -1, wsz, MAX_PATH);
532          /* Load the file. */
533          hres = ppf->lpVtbl->Load (ppf, wsz, STGM_READ);
534          if (SUCCEEDED (hres))
535            {
536              /* Resolve the link by calling the Resolve()
537               * interface function.
538               */
539              hres = psl->lpVtbl->Resolve(psl,  hWnd,
540                                          SLR_ANY_MATCH |
541                                          SLR_NO_UI);
542              if (SUCCEEDED (hres))
543                {
544                  hres = psl->lpVtbl->GetPath (psl, lpszPath,
545                                               MAX_PATH,
546                                               (WIN32_FIND_DATA*)&wfd,
547                                               0);
548
549                  if (SUCCEEDED (hres) && lpszDescription != NULL)
550                    {
551                      hres = psl->lpVtbl->GetDescription (psl,
552                                                          lpszDescription,
553                                                          MAX_PATH );
554
555                      if (!SUCCEEDED (hres))
556                        return FALSE;
557                    }
558                }
559            }
560          ppf->lpVtbl->Release (ppf);
561        }
562      psl->lpVtbl->Release (psl);
563    }
564   return SUCCEEDED (hres);
565 }
566
567 #else
568
569 #define resolve_link(hWnd, lpszLinkName, lpszPath, lpszDescription) FALSE
570
571 #endif
572
573 static GdkFilterReturn
574 gdk_dropfiles_filter (GdkXEvent *xev,
575                       GdkEvent  *event,
576                       gpointer   data)
577 {
578   GdkDragContext *context;
579   GdkDragContextPrivate *private;
580   static GdkAtom text_uri_list_atom = GDK_NONE;
581   GString *result;
582   MSG *msg = (MSG *) xev;
583   HANDLE hdrop;
584   POINT pt;
585   gint nfiles, i, k;
586   guchar fileName[MAX_PATH], linkedFile[MAX_PATH];
587   
588   if (text_uri_list_atom == GDK_NONE)
589     text_uri_list_atom = gdk_atom_intern ("text/uri-list", FALSE);
590
591   if (msg->message == WM_DROPFILES)
592     {
593       GDK_NOTE (DND, g_print ("WM_DROPFILES: %#x\n", msg->hwnd));
594
595       context = gdk_drag_context_new ();
596       private = (GdkDragContextPrivate *) context;
597       context->protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
598       context->is_source = FALSE;
599       context->source_window = (GdkWindow *) &gdk_root_parent;
600       context->dest_window = event->any.window;
601       gdk_window_ref (context->dest_window);
602       /* WM_DROPFILES drops are always file names */
603       context->targets =
604         g_list_append (NULL, GUINT_TO_POINTER (text_uri_list_atom));
605       current_dest_drag = context;
606
607       event->dnd.type = GDK_DROP_START;
608       event->dnd.context = current_dest_drag;
609       gdk_drag_context_ref (current_dest_drag);
610       
611       hdrop = (HANDLE) msg->wParam;
612       DragQueryPoint (hdrop, &pt);
613       ClientToScreen (msg->hwnd, &pt);
614
615       event->dnd.x_root = pt.x;
616       event->dnd.y_root = pt.y;
617       event->dnd.time = msg->time;
618
619       nfiles = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
620
621       result = g_string_new (NULL);
622       for (i = 0; i < nfiles; i++)
623         {
624           g_string_append (result, "file:");
625           DragQueryFile (hdrop, i, fileName, MAX_PATH);
626
627           /* Resolve shortcuts */
628           if (resolve_link (msg->hwnd, fileName, linkedFile, NULL))
629             {
630               g_string_append (result, linkedFile);
631               GDK_NOTE (DND, g_print ("...%s link to %s\n",
632                                       fileName, linkedFile));
633             }
634           else
635             {
636               g_string_append (result, fileName);
637               GDK_NOTE (DND, g_print ("...%s\n", fileName));
638             }
639           g_string_append (result, "\015\012");
640         }
641       gdk_sel_prop_store ((GdkWindow *) &gdk_root_parent,
642                           text_uri_list_atom, 8, result->str, result->len + 1);
643
644       DragFinish (hdrop);
645       
646       return GDK_FILTER_TRANSLATE;
647     }
648   else
649     return GDK_FILTER_CONTINUE;
650 }
651
652 /*************************************************************
653  ************************** Public API ***********************
654  *************************************************************/
655
656 void
657 gdk_dnd_init (void)
658 {
659   HRESULT hres;
660 #ifdef OLE2_DND
661   hres = OleInitialize (NULL);
662
663   if (! SUCCEEDED (hres))
664     g_error ("OleInitialize failed");
665 #endif
666 }      
667
668 void
669 gdk_dnd_exit (void)
670 {
671 #ifdef OLE2_DND
672   OleUninitialize ();
673 #endif
674 }
675
676 /* Source side */
677
678 static void
679 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
680 {
681   if (context->dest_window)
682     {
683       GDK_NOTE (DND, g_print ("gdk_drag_do_leave\n"));
684       gdk_window_unref (context->dest_window);
685       context->dest_window = NULL;
686     }
687 }
688
689 GdkDragContext * 
690 gdk_drag_begin (GdkWindow     *window,
691                 GList         *targets)
692 {
693   GList *tmp_list;
694   GdkDragContext *new_context;
695   
696   g_return_val_if_fail (window != NULL, NULL);
697
698   GDK_NOTE (DND, g_print ("gdk_drag_begin\n"));
699
700   new_context = gdk_drag_context_new ();
701   new_context->is_source = TRUE;
702   new_context->source_window = window;
703   gdk_window_ref (window);
704
705   tmp_list = g_list_last (targets);
706   new_context->targets = NULL;
707   while (tmp_list)
708     {
709       new_context->targets = g_list_prepend (new_context->targets,
710                                              tmp_list->data);
711       tmp_list = tmp_list->prev;
712     }
713
714   new_context->actions = 0;
715
716   return new_context;
717 }
718
719 guint32
720 gdk_drag_get_protocol (guint32          xid,
721                        GdkDragProtocol *protocol)
722 {
723   /* This isn't used */
724   return 0;
725 }
726
727 void
728 gdk_drag_find_window (GdkDragContext  *context,
729                       GdkWindow       *drag_window,
730                       gint             x_root,
731                       gint             y_root,
732                       GdkWindow      **dest_window,
733                       GdkDragProtocol *protocol)
734 {
735   GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
736   GdkDrawablePrivate *drag_window_private = (GdkDrawablePrivate*) drag_window;
737   HWND recipient;
738   POINT pt;
739
740   GDK_NOTE (DND, g_print ("gdk_drag_find_window: %#x +%d+%d\n",
741                           (drag_window ? drag_window_private->xwindow : 0),
742                           x_root, y_root));
743
744   pt.x = x_root;
745   pt.y = y_root;
746   recipient = WindowFromPoint (pt);
747   if (recipient == NULL)
748     *dest_window = NULL;
749   else
750     {
751       *dest_window = gdk_window_lookup (recipient);
752       if (*dest_window)
753         gdk_window_ref (*dest_window);
754       *protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
755     }
756 }
757
758 gboolean
759 gdk_drag_motion (GdkDragContext *context,
760                  GdkWindow      *dest_window,
761                  GdkDragProtocol protocol,
762                  gint            x_root, 
763                  gint            y_root,
764                  GdkDragAction   suggested_action,
765                  GdkDragAction   possible_actions,
766                  guint32         time)
767 {
768   return FALSE;
769 }
770
771 void
772 gdk_drag_drop (GdkDragContext *context,
773                guint32         time)
774 {
775   g_return_if_fail (context != NULL);
776
777   g_warning ("gdk_drag_drop: not implemented\n");
778 }
779
780 void
781 gdk_drag_abort (GdkDragContext *context,
782                 guint32         time)
783 {
784   g_return_if_fail (context != NULL);
785
786   gdk_drag_do_leave (context, time);
787 }
788
789 /* Destination side */
790
791 void
792 gdk_drag_status (GdkDragContext   *context,
793                  GdkDragAction     action,
794                  guint32           time)
795 {
796   GDK_NOTE (DND, g_print ("gdk_drag_status\n"));
797 }
798
799 void 
800 gdk_drop_reply (GdkDragContext   *context,
801                 gboolean          ok,
802                 guint32           time)
803 {
804 }
805
806 void
807 gdk_drop_finish (GdkDragContext   *context,
808                  gboolean          success,
809                  guint32           time)
810 {
811 }
812
813 static GdkFilterReturn
814 gdk_destroy_filter (GdkXEvent *xev,
815                     GdkEvent  *event,
816                     gpointer   data)
817 {
818 #ifdef OLE2_DND
819   MSG *msg = (MSG *) xev;
820
821   if (msg->message == WM_DESTROY)
822     {
823       IDropTarget *idtp = (IDropTarget *) data;
824
825       GDK_NOTE (DND, g_print ("gdk_destroy_filter: WM_DESTROY: %#x\n", msg->hwnd));
826       RevokeDragDrop (msg->hwnd);
827       CoLockObjectExternal (idtp, FALSE, TRUE);
828     }
829 #endif
830   return GDK_FILTER_CONTINUE;
831 }
832
833 void
834 gdk_window_register_dnd (GdkWindow      *window)
835 {
836   GdkDrawablePrivate *private = (GdkDrawablePrivate *) window;
837 #ifdef OLE2_DND
838   target_drag_context *context;
839   HRESULT hres;
840 #endif
841
842   g_return_if_fail (window != NULL);
843
844   GDK_NOTE (DND, g_print ("gdk_window_register_dnd: %#x\n", private->xwindow));
845
846   /* We always claim to accept dropped files, but in fact we might not,
847    * of course. This function is called in such a way that it cannot know
848    * whether the window (widget) in question actually accepts files
849    * (in gtk, data of type text/uri-list) or not.
850    */
851   gdk_window_add_filter (window, gdk_dropfiles_filter, NULL);
852   DragAcceptFiles (private->xwindow, TRUE);
853
854 #ifdef OLE2_DND
855   /* Register for OLE2 d&d */
856   context = target_context_new ();
857   hres = CoLockObjectExternal ((IUnknown *) &context->idt, TRUE, FALSE);
858   if (!SUCCEEDED (hres))
859     g_warning ("gdk_window_register_dnd: CoLockObjectExternal failed");
860   else
861     {
862       hres = RegisterDragDrop (private->xwindow, &context->idt);
863       if (hres == DRAGDROP_E_ALREADYREGISTERED)
864         {
865           g_print ("DRAGDROP_E_ALREADYREGISTERED\n");
866           CoLockObjectExternal ((IUnknown *) &context->idt, FALSE, FALSE);
867         }
868       else if (!SUCCEEDED (hres))
869         g_warning ("gdk_window_register_dnd: RegisterDragDrop failed");
870       else
871         {
872           gdk_window_add_filter (window, gdk_destroy_filter, &context->idt);
873         }
874     }
875 #endif
876 }
877
878 /*************************************************************
879  * gdk_drag_get_selection:
880  *     Returns the selection atom for the current source window
881  *   arguments:
882  *
883  *   results:
884  *************************************************************/
885
886 GdkAtom
887 gdk_drag_get_selection (GdkDragContext *context)
888 {
889   if (context->protocol == GDK_DRAG_PROTO_WIN32_DROPFILES)
890     return gdk_win32_dropfiles_atom;
891   else if (context->protocol == GDK_DRAG_PROTO_OLE2)
892     return gdk_ole2_dnd_atom;
893   else
894     return GDK_NONE;
895 }