]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkdnd-win32.c
Make it compile again for Windows
[~andy/gtk] / gdk / win32 / gdkdnd-win32.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3  * Copyright (C) 2001 Archaeopteryx Software Inc.
4  * Copyright (C) 1998-2002 Tor Lillqvist
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /*
23  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
24  * file for a list of people on the GTK+ Team.  See the ChangeLog
25  * files for a list of changes.  These files are distributed with
26  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27  */
28
29 #include "config.h"
30 #include <string.h>
31
32 #include <io.h>
33 #include <fcntl.h>
34
35 /*
36  * Comment from the old OLE2 DND code that is being merged in. Note
37  * that this comment might not fully reflect reality as the code
38  * obviously will have to be modified in this merge. Especially the
39  * talk about supporting other than UTF-8 text is bogus, that will not
40  * happen.
41  *
42  * Support for OLE-2 drag and drop added at Archaeopteryx Software, 2001
43  * For more information, contact Stephan R.A. Deibel (sdeibel@archaeopteryx.com)
44  *
45  * Notes on implementation:
46  *
47  * This is a first pass at OLE2 support. It only supports text and unicode text
48  * data types, and file list dnd (which is handled seperately as it predates OLE2
49  * both in this implementation and on Windows in general).
50  *
51  * As such, the data type conversion from gdk selection targets to OLE2 CF_* data
52  * type specifiers is partially hardwired. Fixing this is complicated by (a) the
53  * fact that the widget's declared selection types aren't accessible in calls here
54  * that need to declare the corresponding OLE2 data types, and (b) there isn't a
55  * 1-1 correspondence between gdk target types and OLE2 types. The former needs
56  * some redesign in gtk dnd (something a gdk/gtk expert should do; I have tried
57  * and failed!). As an example of the latter: gdk STRING, TEXT, COMPOUND_TEXT map
58  * to CF_TEXT, CF_OEMTEXT, and CF_UNICODETEXT but as a group and with conversions
59  * necessary for various combinations. Currently, the code here (and in
60  * gdkdnd-win32.c) can handle gdk STRING and TEXT but not COMPOUND_TEXT, and OLE2
61  * CF_TEXT and CF_UNICODETEXT but not CF_OEMTEXT. The necessary conversions are
62  * supplied by the implementation here.
63  *
64  * Note that in combination with another hack originated by Archaeopteryx
65  * Software, the text conversions here may go to utf-8 unicode as the standard
66  * within-gtk target or to single-byte ascii when the USE_ACP_TEXT compilation
67  * flag is TRUE. This mode was added to support applications that aren't using
68  * utf-8 across the gtk/gdk API but instead use single-byte ascii according to
69  * the current Windows code page. See gdkim-win32.c for more info on that.
70  *
71  */
72  
73 #define INITGUID
74
75 #include "gdkdnd.h"
76 #include "gdkproperty.h"
77 #include "gdkinternals.h"
78 #include "gdkprivate-win32.h"
79
80 #include <ole2.h>
81
82 #include <shlobj.h>
83 #include <shlguid.h>
84
85 #include <gdk/gdk.h>
86 #include <glib/gstdio.h>
87
88 typedef struct _GdkDragContextPrivateWin32 GdkDragContextPrivateWin32;
89
90 typedef enum {
91   GDK_DRAG_STATUS_DRAG,
92   GDK_DRAG_STATUS_MOTION_WAIT,
93   GDK_DRAG_STATUS_ACTION_WAIT,
94   GDK_DRAG_STATUS_DROP
95 } GdkDragStatus;
96
97 /* Structure that holds information about a drag in progress.
98  * this is used on both source and destination sides.
99  */
100 struct _GdkDragContextPrivateWin32 {
101   GdkDevice *device;
102   gboolean being_finalized;
103   gint ref_count;
104   IUnknown *iface;
105   DWORD last_key_state;
106   POINT last_pt;                /* Coordinates from last event */
107   guint drag_status : 4;        /* Current status of drag */
108   guint drop_failed : 1;        /* Whether the drop was unsuccessful */
109 };
110
111 #define PRIVATE_DATA(context) ((GdkDragContextPrivateWin32 *) GDK_DRAG_CONTEXT (context)->windowing_data)
112
113 static GList *contexts;
114 static GdkDragContext *current_dest_drag = NULL;
115
116 static void gdk_drag_context_init       (GdkDragContext      *dragcontext);
117 static void gdk_drag_context_class_init (GdkDragContextClass *klass);
118 static void gdk_drag_context_finalize   (GObject              *object);
119
120 static gpointer parent_class = NULL;
121
122 static gboolean use_ole2_dnd = FALSE;
123
124 G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
125
126 static void
127 gdk_drag_context_init (GdkDragContext *dragcontext)
128 {
129   GdkDragContextPrivateWin32 *private;
130
131   private = G_TYPE_INSTANCE_GET_PRIVATE (dragcontext,
132                                          GDK_TYPE_DRAG_CONTEXT,
133                                          GdkDragContextPrivateWin32);
134
135   dragcontext->windowing_data = private;
136
137   if (!use_ole2_dnd)
138     {
139       contexts = g_list_prepend (contexts, dragcontext);
140     }
141   else
142     {
143       private->being_finalized = FALSE;
144       private->ref_count = 1;
145       private->iface = NULL;
146     }
147
148   GDK_NOTE (DND, g_print ("gdk_drag_context_init %p\n", dragcontext));
149 }
150
151 static void
152 gdk_drag_context_class_init (GdkDragContextClass *klass)
153 {
154   GObjectClass *object_class = G_OBJECT_CLASS (klass);
155
156   parent_class = g_type_class_peek_parent (klass);
157
158   object_class->finalize = gdk_drag_context_finalize;
159
160   g_type_class_add_private (object_class, sizeof (GdkDragContextPrivateWin32));
161 }
162
163 static void
164 gdk_drag_context_finalize (GObject *object)
165 {
166   GdkDragContext *context = GDK_DRAG_CONTEXT (object);
167
168   GDK_NOTE (DND, g_print ("gdk_drag_context_finalize %p\n", object));
169
170   g_list_free (context->targets);
171
172   if (context->source_window)
173     g_object_unref (context->source_window);
174
175   if (context->dest_window)
176     g_object_unref (context->dest_window);
177
178   if (!use_ole2_dnd)
179     {
180       contexts = g_list_remove (contexts, context);
181
182       if (context == current_dest_drag)
183         current_dest_drag = NULL;
184     }
185   else
186     {
187       GdkDragContextPrivateWin32 *private = PRIVATE_DATA (context);
188       if (private->iface)
189         {
190           private->being_finalized = TRUE;
191           private->iface->lpVtbl->Release (private->iface);
192           private->iface = NULL;
193         }
194     }
195
196   G_OBJECT_CLASS (parent_class)->finalize (object);
197 }
198
199 /* Drag Contexts */
200
201 GdkDragContext *
202 gdk_drag_context_new (void)
203 {
204   return g_object_new (GDK_TYPE_DRAG_CONTEXT, NULL);
205 }
206
207 GdkDevice *
208 gdk_drag_context_get_device (GdkDragContext *context)
209 {
210   GdkDragContextPrivateWin32 *private;
211
212   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
213
214   private = PRIVATE_DATA (context);
215
216   return private->device;
217 }
218
219 void
220 gdk_drag_context_set_device (GdkDragContext *context,
221                              GdkDevice      *device)
222 {
223   GdkDragContextPrivateWin32 *private;
224
225   g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
226   g_return_if_fail (GDK_IS_DEVICE (device));
227
228   private = PRIVATE_DATA (context);
229
230   if (private->device)
231     {
232       g_object_unref (private->device);
233       private->device = NULL;
234     }
235
236   if (device)
237     private->device = g_object_ref (device);
238 }
239
240 static GdkDragContext *
241 gdk_drag_context_find (gboolean   is_source,
242                        GdkWindow *source,
243                        GdkWindow *dest)
244 {
245   GList *tmp_list = contexts;
246   GdkDragContext *context;
247   GdkDragContextPrivateWin32 *private;
248
249   while (tmp_list)
250     {
251       context = (GdkDragContext *)tmp_list->data;
252       private = PRIVATE_DATA (context);
253
254       if ((!context->is_source == !is_source) &&
255           ((source == NULL) || (context->source_window && (context->source_window == source))) &&
256           ((dest == NULL) || (context->dest_window && (context->dest_window == dest))))
257         return context;
258
259       tmp_list = tmp_list->next;
260     }
261
262   return NULL;
263 }
264
265 #define PRINT_GUID(guid) \
266   g_print ("%.08lx-%.04x-%.04x-%.02x%.02x-%.02x%.02x%.02x%.02x%.02x%.02x", \
267            ((gulong *)  guid)[0], \
268            ((gushort *) guid)[2], \
269            ((gushort *) guid)[3], \
270            ((guchar *)  guid)[8], \
271            ((guchar *)  guid)[9], \
272            ((guchar *)  guid)[10], \
273            ((guchar *)  guid)[11], \
274            ((guchar *)  guid)[12], \
275            ((guchar *)  guid)[13], \
276            ((guchar *)  guid)[14], \
277            ((guchar *)  guid)[15]);
278
279
280 static FORMATETC *formats;
281 static int nformats;
282
283 typedef struct {
284   IDropTarget idt;
285   GdkDragContext *context;
286 } target_drag_context;
287
288 typedef struct {
289   IDropSource ids;
290   GdkDragContext *context;
291 } source_drag_context;
292
293 typedef struct {
294   IDataObject ido;
295   int ref_count;
296   GdkDragContext *context;
297 } data_object;
298
299 typedef struct {
300   IEnumFORMATETC ief;
301   int ref_count;
302   int ix;
303 } enum_formats;
304
305 static source_drag_context *pending_src_context = NULL;
306 static IDataObject *dnd_data = NULL;
307
308 static enum_formats *enum_formats_new (void);
309
310 /* map windows -> target drag contexts. The table
311  * owns a ref to both objects.
312  */
313 static GHashTable* target_ctx_for_window = NULL;
314
315 static ULONG STDMETHODCALLTYPE
316 idroptarget_addref (LPDROPTARGET This)
317 {
318   target_drag_context *ctx = (target_drag_context *) This;
319   GdkDragContextPrivateWin32 *private = PRIVATE_DATA (ctx->context);
320   int ref_count = ++private->ref_count;
321
322   GDK_NOTE (DND, g_print ("idroptarget_addref %p %d\n", This, ref_count));
323   g_object_ref (G_OBJECT (ctx->context));
324
325   return ref_count;
326 }
327
328 static HRESULT STDMETHODCALLTYPE
329 idroptarget_queryinterface (LPDROPTARGET This,
330                             REFIID       riid,
331                             LPVOID      *ppvObject)
332 {
333   GDK_NOTE (DND, {
334       g_print ("idroptarget_queryinterface %p ", This);
335       PRINT_GUID (riid);
336     });
337
338   *ppvObject = NULL;
339
340   if (IsEqualGUID (riid, &IID_IUnknown))
341     {
342       GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
343       idroptarget_addref (This);
344       *ppvObject = This;
345       return S_OK;
346     }
347   else if (IsEqualGUID (riid, &IID_IDropTarget))
348     {
349       GDK_NOTE (DND, g_print ("...IDropTarget S_OK\n"));
350       idroptarget_addref (This);
351       *ppvObject = This;
352       return S_OK;
353     }
354   else
355     {
356       GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
357       return E_NOINTERFACE;
358     }
359 }
360
361 static ULONG STDMETHODCALLTYPE
362 idroptarget_release (LPDROPTARGET This)
363 {
364   target_drag_context *ctx = (target_drag_context *) This;
365   GdkDragContextPrivateWin32 *private = PRIVATE_DATA (ctx->context);
366   int ref_count = --private->ref_count;
367
368   GDK_NOTE (DND, g_print ("idroptarget_release %p %d\n", This, ref_count));
369
370   if (!private->being_finalized)
371     g_object_unref (G_OBJECT (ctx->context));
372
373   if (ref_count == 0)
374     g_free (This);
375
376   return ref_count;
377 }
378
379 #if 0
380
381 static GdkAtom
382 cf_to_atom (CLIPFORMAT cf)
383 {
384   switch (cf)
385     {
386     case CF_UNICODETEXT:
387       return _utf8_string;
388     case CF_HDROP:
389       return _text_uri_list;
390     case CF_DIB:
391       return _image_bmp;
392     }
393
394   if (cf == _cf_url)
395     return _text_uri_list;
396
397   if (cf == _cf_html_format || cf == _cf_text_html)
398     return _text_html;
399
400   return GDK_NONE;
401 }
402
403 #endif
404
405 static GdkDragAction
406 get_suggested_action (DWORD grfKeyState)
407 {
408   /* This is the yucky Windows standard: Force link action if both
409    * Control and Alt are down, copy if Control is down alone, move if
410    * Alt is down alone, or use default of move within the app or copy
411    * when origin of the drag is in another app.
412    */
413   if (grfKeyState & MK_CONTROL && grfKeyState & MK_SHIFT)
414     return GDK_ACTION_LINK; /* Link action not supported */
415   else if (grfKeyState & MK_CONTROL)
416     return GDK_ACTION_COPY;
417   else if (grfKeyState & MK_ALT)
418     return GDK_ACTION_MOVE;
419 #if 0 /* Default is always copy for now */
420   else if (_dnd_source_state == GDK_WIN32_DND_DRAGGING)
421     return GDK_ACTION_MOVE;
422 #endif
423   else
424     return GDK_ACTION_COPY;
425   /* Any way to determine when to add in DROPEFFECT_SCROLL? */
426 }
427
428 /* Process pending events -- we don't want to service non-GUI events
429  * forever so do one iteration and then do more only if there's a
430  * pending GDK event.
431  */
432 static void
433 process_pending_events ()
434 {
435   g_main_context_iteration (NULL, FALSE);
436   while (_gdk_event_queue_find_first (_gdk_display))
437     g_main_context_iteration (NULL, FALSE);
438 }
439
440 static DWORD
441 drop_effect_for_action (GdkDragAction action)
442 {
443   switch (action)
444     {
445     case GDK_ACTION_MOVE:
446       return DROPEFFECT_MOVE;
447     case GDK_ACTION_LINK:
448       return DROPEFFECT_LINK;
449     case GDK_ACTION_COPY:
450       return DROPEFFECT_COPY;
451     default:
452       return DROPEFFECT_NONE;
453     }
454 }
455
456 static void
457 dnd_event_put (GdkEventType    type,
458                GdkDragContext *context,
459                const POINTL    pt,
460                gboolean        to_dest_window)
461 {
462   GdkEvent *e;
463
464   e = gdk_event_new (type);
465
466   if (to_dest_window)
467     e->dnd.window = context->dest_window;
468   else
469     e->dnd.window = context->source_window;
470   e->dnd.send_event = FALSE;
471   e->dnd.context = g_object_ref (context);
472   e->dnd.time = GDK_CURRENT_TIME;
473   e->dnd.x_root = pt.x + _gdk_offset_x;
474   e->dnd.y_root = pt.x + _gdk_offset_y;
475
476   if (e->dnd.window != NULL)
477     g_object_ref (e->dnd.window);
478
479   gdk_event_set_device (e, gdk_drag_context_get_device (context));
480
481   GDK_NOTE (EVENTS, _gdk_win32_print_event (e));
482   gdk_event_put (e);
483   gdk_event_free (e);
484 }
485
486 static HRESULT STDMETHODCALLTYPE
487 idroptarget_dragenter (LPDROPTARGET This,
488                        LPDATAOBJECT pDataObj,
489                        DWORD        grfKeyState,
490                        POINTL       pt,
491                        LPDWORD      pdwEffect)
492 {
493   target_drag_context *ctx = (target_drag_context *) This;
494
495   GDK_NOTE (DND, g_print ("idroptarget_dragenter %p S_OK\n", This));
496
497   ctx->context->suggested_action = get_suggested_action (grfKeyState);
498   dnd_event_put (GDK_DRAG_ENTER, ctx->context, pt, TRUE);
499   process_pending_events ();
500   *pdwEffect = drop_effect_for_action (ctx->context->action);
501
502   /* Assume that target can accept the data: In fact it may fail but
503    * we are not really set up to query the target!
504    */
505   return S_OK;
506 }
507
508 static HRESULT STDMETHODCALLTYPE
509 idroptarget_dragover (LPDROPTARGET This,
510                       DWORD        grfKeyState,
511                       POINTL       pt,
512                       LPDWORD      pdwEffect)
513 {
514   target_drag_context *ctx = (target_drag_context *) This;
515
516   GDK_NOTE (DND, g_print ("idroptarget_dragover %p S_OK\n", This));
517
518   ctx->context->suggested_action = get_suggested_action (grfKeyState);
519   dnd_event_put (GDK_DRAG_MOTION, ctx->context, pt, TRUE);
520   process_pending_events ();
521   *pdwEffect = drop_effect_for_action (ctx->context->action);
522
523   return S_OK;
524 }
525
526 static HRESULT STDMETHODCALLTYPE
527 idroptarget_dragleave (LPDROPTARGET This)
528 {
529   target_drag_context *ctx = (target_drag_context *) This;
530   POINTL pt = { 0, 0 };
531
532   GDK_NOTE (DND, g_print ("idroptarget_dragleave %p S_OK\n", This));
533
534   dnd_event_put (GDK_DRAG_LEAVE, ctx->context, pt, TRUE);
535   process_pending_events ();
536
537   return S_OK;
538 }
539
540 static HRESULT STDMETHODCALLTYPE
541 idroptarget_drop (LPDROPTARGET This,
542                   LPDATAOBJECT pDataObj,
543                   DWORD        grfKeyState,
544                   POINTL       pt,
545                   LPDWORD      pdwEffect)
546 {
547   target_drag_context *ctx = (target_drag_context *) This;
548
549   GDK_NOTE (DND, g_print ("idroptarget_drop %p ", This));
550
551   if (pDataObj == NULL)
552     {
553       GDK_NOTE (DND, g_print ("E_POINTER\n"));
554       return E_POINTER;
555     }
556
557   dnd_data = pDataObj;
558
559   ctx->context->suggested_action = get_suggested_action (grfKeyState);
560   dnd_event_put (GDK_DROP_START, ctx->context, pt, TRUE);
561   process_pending_events ();
562
563   dnd_data = NULL;
564
565   /* Notify OLE of copy or move */
566   if (_dnd_target_state != GDK_WIN32_DND_DROPPED)
567     *pdwEffect = DROPEFFECT_NONE;
568   else
569     *pdwEffect = drop_effect_for_action (ctx->context->action);
570
571   GDK_NOTE (DND, g_print ("S_OK\n"));
572
573   return S_OK;
574 }
575
576 static ULONG STDMETHODCALLTYPE
577 idropsource_addref (LPDROPSOURCE This)
578 {
579   source_drag_context *ctx = (source_drag_context *) This;
580   GdkDragContextPrivateWin32 *private = PRIVATE_DATA (ctx->context);
581   int ref_count = ++private->ref_count;
582
583   GDK_NOTE (DND, g_print ("idropsource_addref %p %d\n", This, ref_count));
584   g_object_ref (G_OBJECT (ctx->context));
585
586   return ref_count;
587 }
588
589 static HRESULT STDMETHODCALLTYPE
590 idropsource_queryinterface (LPDROPSOURCE This,
591                             REFIID       riid,
592                             LPVOID      *ppvObject)
593 {
594   GDK_NOTE (DND, {
595       g_print ("idropsource_queryinterface %p ", This);
596       PRINT_GUID (riid);
597     });
598
599   *ppvObject = NULL;
600
601   if (IsEqualGUID (riid, &IID_IUnknown))
602     {
603       GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
604       idropsource_addref (This);
605       *ppvObject = This;
606       return S_OK;
607     }
608   else if (IsEqualGUID (riid, &IID_IDropSource))
609     {
610       GDK_NOTE (DND, g_print ("...IDropSource S_OK\n"));
611       idropsource_addref (This);
612       *ppvObject = This;
613       return S_OK;
614     }
615   else
616     {
617       GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
618       return E_NOINTERFACE;
619     }
620 }
621
622 static ULONG STDMETHODCALLTYPE
623 idropsource_release (LPDROPSOURCE This)
624 {
625   source_drag_context *ctx = (source_drag_context *) This;
626   GdkDragContextPrivateWin32 *private = PRIVATE_DATA (ctx->context);
627   int ref_count = --private->ref_count;
628
629   GDK_NOTE (DND, g_print ("idropsource_release %p %d\n", This, ref_count));
630
631   if (!private->being_finalized)
632     g_object_unref (G_OBJECT (ctx->context));
633
634   if (ref_count == 0)
635     g_free (This);
636
637   return ref_count;
638 }
639
640 /* Emit GDK events for any changes in mouse events or control key
641  * state since the last recorded state. Return true if any events
642  * have been emitted and false otherwise.
643  */
644 static gboolean
645 send_change_events (GdkDragContext *ctx,
646                     DWORD           key_state,
647                     gboolean        esc_pressed)
648 {
649   GdkDragContextPrivateWin32 *private = PRIVATE_DATA (ctx);
650   POINT pt;
651   gboolean changed = FALSE;
652   HWND hwnd = GDK_WINDOW_HWND (ctx->source_window);
653   LPARAM lparam;
654   WPARAM wparam;
655
656   if (!API_CALL (GetCursorPos, (&pt)))
657     return FALSE;
658
659   if (!API_CALL (ScreenToClient, (hwnd, &pt)))
660     return FALSE;
661
662   if (pt.x != private->last_pt.x || pt.y != private->last_pt.y ||
663       key_state != private->last_key_state)
664     {
665       lparam = MAKELPARAM (pt.x, pt.y);
666       wparam = key_state;
667       if (pt.x != private->last_pt.x || pt.y != private->last_pt.y)
668         {
669           GDK_NOTE (DND, g_print ("Sending WM_MOUSEMOVE (%ld,%ld)\n", pt.x, pt.y));
670           SendMessage (hwnd, WM_MOUSEMOVE, wparam, lparam);
671         }
672
673       if ((key_state & MK_LBUTTON) != (private->last_key_state & MK_LBUTTON))
674         {
675           if (key_state & MK_LBUTTON)
676             SendMessage (hwnd, WM_LBUTTONDOWN, wparam, lparam);
677           else
678             SendMessage (hwnd, WM_LBUTTONUP, wparam, lparam);
679         }
680       if ((key_state & MK_MBUTTON) != (private->last_key_state & MK_MBUTTON))
681         {
682           if (key_state & MK_MBUTTON)
683             SendMessage (hwnd, WM_MBUTTONDOWN, wparam, lparam);
684           else
685             SendMessage (hwnd, WM_MBUTTONUP, wparam, lparam);
686         }
687       if ((key_state & MK_RBUTTON) != (private->last_key_state & MK_RBUTTON))
688         {
689           if (key_state & MK_RBUTTON)
690             SendMessage (hwnd, WM_RBUTTONDOWN, wparam, lparam);
691           else
692             SendMessage (hwnd, WM_RBUTTONUP, wparam, lparam);
693         }
694       if ((key_state & MK_CONTROL) != (private->last_key_state & MK_CONTROL))
695         {
696           if (key_state & MK_CONTROL)
697             SendMessage (hwnd, WM_KEYDOWN, VK_CONTROL, 0);
698           else
699             SendMessage (hwnd, WM_KEYUP, VK_CONTROL, 0);
700         }
701       if ((key_state & MK_SHIFT) != (private->last_key_state & MK_SHIFT))
702         {
703           if (key_state & MK_CONTROL)
704             SendMessage (hwnd, WM_KEYDOWN, VK_SHIFT, 0);
705           else
706             SendMessage (hwnd, WM_KEYUP, VK_SHIFT, 0);
707         }
708
709       changed = TRUE;
710       private->last_key_state = key_state;
711       private->last_pt = pt;
712     }
713
714   if (esc_pressed)
715     {
716       GDK_NOTE (DND, g_print ("Sending a escape key down message to %p\n", hwnd));
717       SendMessage (hwnd, WM_KEYDOWN, VK_ESCAPE, 0);
718       changed = TRUE;
719     }
720
721   return changed;
722 }
723
724 static HRESULT STDMETHODCALLTYPE
725 idropsource_querycontinuedrag (LPDROPSOURCE This,
726                                BOOL         fEscapePressed,
727                                DWORD        grfKeyState)
728 {
729   source_drag_context *ctx = (source_drag_context *) This;
730
731   GDK_NOTE (DND, g_print ("idropsource_querycontinuedrag %p ", This));
732
733   if (send_change_events (ctx->context, grfKeyState, fEscapePressed))
734     process_pending_events ();
735
736   if (_dnd_source_state == GDK_WIN32_DND_DROPPED)
737     {
738       GDK_NOTE (DND, g_print ("DRAGDROP_S_DROP\n"));
739       return DRAGDROP_S_DROP;
740     }
741   else if (_dnd_source_state == GDK_WIN32_DND_NONE)
742     {
743       GDK_NOTE (DND, g_print ("DRAGDROP_S_CANCEL\n"));
744       return DRAGDROP_S_CANCEL;
745     }
746   else
747     {
748       GDK_NOTE (DND, g_print ("S_OK\n"));
749       return S_OK;
750     }
751 }
752
753 static HRESULT STDMETHODCALLTYPE
754 idropsource_givefeedback (LPDROPSOURCE This,
755                           DWORD        dwEffect)
756 {
757   source_drag_context *ctx = (source_drag_context *) This;
758   GdkDragAction suggested_action;
759
760   GDK_NOTE (DND, g_print ("idropsource_givefeedback %p DRAGDROP_S_USEDEFAULTCURSORS\n", This));
761
762   if (dwEffect == DROPEFFECT_MOVE)
763     suggested_action = GDK_ACTION_MOVE;
764   else
765     suggested_action = GDK_ACTION_COPY;
766   ctx->context->action = suggested_action;
767
768   if (dwEffect == DROPEFFECT_NONE)
769     {
770       if (ctx->context->dest_window != NULL)
771         {
772           g_object_unref (ctx->context->dest_window);
773           ctx->context->dest_window = NULL;
774         }
775     }
776   else
777     {
778       if (ctx->context->dest_window == NULL)
779         ctx->context->dest_window = g_object_ref (_gdk_root);
780     }
781
782   return DRAGDROP_S_USEDEFAULTCURSORS;
783 }
784
785 static ULONG STDMETHODCALLTYPE
786 idataobject_addref (LPDATAOBJECT This)
787 {
788   data_object *dobj = (data_object *) This;
789   int ref_count = ++dobj->ref_count;
790
791   GDK_NOTE (DND, g_print ("idataobject_addref %p %d\n", This, ref_count));
792
793   return ref_count;
794 }
795
796 static HRESULT STDMETHODCALLTYPE
797 idataobject_queryinterface (LPDATAOBJECT This,
798                             REFIID       riid,
799                             LPVOID      *ppvObject)
800 {
801   GDK_NOTE (DND, {
802       g_print ("idataobject_queryinterface %p ", This);
803       PRINT_GUID (riid);
804     });
805
806   *ppvObject = NULL;
807
808   if (IsEqualGUID (riid, &IID_IUnknown))
809     {
810       GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
811       idataobject_addref (This);
812       *ppvObject = This;
813       return S_OK;
814     }
815   else if (IsEqualGUID (riid, &IID_IDataObject))
816     {
817       GDK_NOTE (DND, g_print ("...IDataObject S_OK\n"));
818       idataobject_addref (This);
819       *ppvObject = This;
820       return S_OK;
821     }
822   else
823     {
824       GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
825       return E_NOINTERFACE;
826     }
827 }
828
829 static ULONG STDMETHODCALLTYPE
830 idataobject_release (LPDATAOBJECT This)
831 {
832   data_object *dobj = (data_object *) This;
833   int ref_count = --dobj->ref_count;
834
835   GDK_NOTE (DND, g_print ("idataobject_release %p %d\n", This, ref_count));
836
837   if (ref_count == 0)
838     g_free (This);
839
840   return ref_count;
841 }
842
843 static HRESULT
844 query (LPDATAOBJECT This,
845        LPFORMATETC  pFormatEtc)
846 {
847   int i;
848
849   if (!pFormatEtc)
850     return DV_E_FORMATETC;
851
852   if (pFormatEtc->lindex != -1)
853     return DV_E_LINDEX;
854
855   if ((pFormatEtc->tymed & TYMED_HGLOBAL) == 0)
856     return DV_E_TYMED;
857
858   if ((pFormatEtc->dwAspect & DVASPECT_CONTENT) == 0)
859     return DV_E_DVASPECT;
860
861   for (i = 0; i < nformats; i++)
862     if (pFormatEtc->cfFormat == formats[i].cfFormat)
863       return S_OK;
864
865   return DV_E_FORMATETC;
866 }
867
868 static FORMATETC *active_pFormatEtc = NULL;
869 static STGMEDIUM *active_pMedium = NULL;
870
871 static HRESULT STDMETHODCALLTYPE
872 idataobject_getdata (LPDATAOBJECT This,
873                      LPFORMATETC  pFormatEtc,
874                      LPSTGMEDIUM  pMedium)
875 {
876   data_object *ctx = (data_object *) This;
877   GdkAtom target;
878   HRESULT hr;
879   GdkEvent e;
880
881   GDK_NOTE (DND, g_print ("idataobject_getdata %p %s ",
882                           This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat)));
883
884   /* Check whether we can provide requested format */
885   hr = query (This, pFormatEtc);
886   if (hr != S_OK)
887     return hr;
888
889   /* Append a GDK_SELECTION_GET event and then hope the app sets the
890    * property associated with the _gdk_ole2_dnd atom
891    */
892
893   active_pFormatEtc = pFormatEtc;
894   active_pMedium = pMedium;
895
896   target = GDK_TARGET_STRING;
897
898   e.type = GDK_SELECTION_REQUEST;
899   e.selection.window = ctx->context->source_window;
900   e.selection.send_event = FALSE; /* ??? */
901   /* FIXME: Should really both selection and property be _gdk_ole2_dnd? */
902   e.selection.selection = _gdk_ole2_dnd;
903   /* FIXME: Target? */
904   e.selection.target = _utf8_string;
905   e.selection.property = _gdk_ole2_dnd;
906   e.selection.time = GDK_CURRENT_TIME;
907
908   g_object_ref (e.selection.window);
909
910   GDK_NOTE (EVENTS, _gdk_win32_print_event (&e));
911   gdk_event_put (&e);
912   process_pending_events ();
913
914   active_pFormatEtc = NULL;
915   active_pMedium = NULL;
916
917   if (pMedium->hGlobal == NULL) {
918     return E_UNEXPECTED;
919   }
920
921   return S_OK;
922 }
923
924 static HRESULT STDMETHODCALLTYPE
925 idataobject_getdatahere (LPDATAOBJECT This,
926                          LPFORMATETC  pFormatEtc,
927                          LPSTGMEDIUM  pMedium)
928 {
929   GDK_NOTE (DND, g_print ("idataobject_getdatahere %p %s E_UNEXPECTED\n",
930                           This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat)));
931
932   return E_UNEXPECTED;
933 }
934
935 static HRESULT STDMETHODCALLTYPE
936 idataobject_querygetdata (LPDATAOBJECT This,
937                           LPFORMATETC  pFormatEtc)
938 {
939   HRESULT hr;
940
941   hr = query (This, pFormatEtc);
942
943 #define CASE(x) case x: g_print (#x)
944   GDK_NOTE (DND, {
945       g_print ("idataobject_querygetdata %p %s \n",
946                This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat));
947       switch (hr)
948         {
949         CASE (DV_E_FORMATETC);
950         CASE (DV_E_LINDEX);
951         CASE (DV_E_TYMED);
952         CASE (DV_E_DVASPECT);
953         CASE (S_OK);
954         default: g_print ("%#lx", hr);
955         }
956     });
957
958   return hr;
959 }
960
961 static HRESULT STDMETHODCALLTYPE
962 idataobject_getcanonicalformatetc (LPDATAOBJECT This,
963                                    LPFORMATETC  pFormatEtcIn,
964                                    LPFORMATETC  pFormatEtcOut)
965 {
966   GDK_NOTE (DND, g_print ("idataobject_getcanonicalformatetc %p E_UNEXPECTED\n", This));
967
968   return E_UNEXPECTED;
969 }
970
971 static HRESULT STDMETHODCALLTYPE
972 idataobject_setdata (LPDATAOBJECT This,
973                      LPFORMATETC  pFormatEtc,
974                      LPSTGMEDIUM  pMedium,
975                      BOOL         fRelease)
976 {
977   GDK_NOTE (DND, g_print ("idataobject_setdata %p %s E_UNEXPECTED\n",
978                           This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat)));
979
980   return E_UNEXPECTED;
981 }
982
983 static HRESULT STDMETHODCALLTYPE
984 idataobject_enumformatetc (LPDATAOBJECT     This,
985                            DWORD            dwDirection,
986                            LPENUMFORMATETC *ppEnumFormatEtc)
987 {
988   GDK_NOTE (DND, g_print ("idataobject_enumformatetc %p ", This));
989
990   if (dwDirection != DATADIR_GET)
991     {
992       GDK_NOTE (DND, g_print ("E_NOTIMPL\n"));
993       return E_NOTIMPL;
994     }
995
996   *ppEnumFormatEtc = &enum_formats_new ()->ief;
997
998   GDK_NOTE (DND, g_print ("%p S_OK\n", *ppEnumFormatEtc));
999
1000   return S_OK;
1001 }
1002
1003 static HRESULT STDMETHODCALLTYPE
1004 idataobject_dadvise (LPDATAOBJECT This,
1005                      LPFORMATETC  pFormatetc,
1006                      DWORD        advf,
1007                      LPADVISESINK pAdvSink,
1008                      DWORD       *pdwConnection)
1009 {
1010   GDK_NOTE (DND, g_print ("idataobject_dadvise %p E_NOTIMPL\n", This));
1011
1012   return E_NOTIMPL;
1013 }
1014
1015 static HRESULT STDMETHODCALLTYPE
1016 idataobject_dunadvise (LPDATAOBJECT This,
1017                        DWORD         dwConnection)
1018 {
1019   GDK_NOTE (DND, g_print ("idataobject_dunadvise %p E_NOTIMPL\n", This));
1020
1021   return E_NOTIMPL;
1022 }
1023
1024 static HRESULT STDMETHODCALLTYPE
1025 idataobject_enumdadvise (LPDATAOBJECT    This,
1026                          LPENUMSTATDATA *ppenumAdvise)
1027 {
1028   GDK_NOTE (DND, g_print ("idataobject_enumdadvise %p OLE_E_ADVISENOTSUPPORTED\n", This));
1029
1030   return OLE_E_ADVISENOTSUPPORTED;
1031 }
1032
1033 static ULONG STDMETHODCALLTYPE
1034 ienumformatetc_addref (LPENUMFORMATETC This)
1035 {
1036   enum_formats *en = (enum_formats *) This;
1037   int ref_count = ++en->ref_count;
1038
1039   GDK_NOTE (DND, g_print ("ienumformatetc_addref %p %d\n", This, ref_count));
1040
1041   return ref_count;
1042 }
1043
1044 static HRESULT STDMETHODCALLTYPE
1045 ienumformatetc_queryinterface (LPENUMFORMATETC This,
1046                                REFIID          riid,
1047                                LPVOID         *ppvObject)
1048 {
1049   GDK_NOTE (DND, {
1050       g_print ("ienumformatetc_queryinterface %p", This);
1051       PRINT_GUID (riid);
1052     });
1053
1054   *ppvObject = NULL;
1055
1056   if (IsEqualGUID (riid, &IID_IUnknown))
1057     {
1058       GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
1059       ienumformatetc_addref (This);
1060       *ppvObject = This;
1061       return S_OK;
1062     }
1063   else if (IsEqualGUID (riid, &IID_IEnumFORMATETC))
1064     {
1065       GDK_NOTE (DND, g_print ("...IEnumFORMATETC S_OK\n"));
1066       ienumformatetc_addref (This);
1067       *ppvObject = This;
1068       return S_OK;
1069     }
1070   else
1071     {
1072       GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
1073       return E_NOINTERFACE;
1074     }
1075 }
1076
1077 static ULONG STDMETHODCALLTYPE
1078 ienumformatetc_release (LPENUMFORMATETC This)
1079 {
1080   enum_formats *en = (enum_formats *) This;
1081   int ref_count = --en->ref_count;
1082
1083   GDK_NOTE (DND, g_print ("ienumformatetc_release %p %d\n", This, ref_count));
1084
1085   if (ref_count == 0)
1086     g_free (This);
1087
1088   return ref_count;
1089 }
1090
1091 static HRESULT STDMETHODCALLTYPE
1092 ienumformatetc_next (LPENUMFORMATETC This,
1093                      ULONG           celt,
1094                      LPFORMATETC     elts,
1095                      ULONG          *nelt)
1096 {
1097   enum_formats *en = (enum_formats *) This;
1098   int i, n;
1099
1100   GDK_NOTE (DND, g_print ("ienumformatetc_next %p %d %ld ", This, en->ix, celt));
1101
1102   n = 0;
1103   for (i = 0; i < celt; i++)
1104     {
1105       if (en->ix >= nformats)
1106         break;
1107       elts[i] = formats[en->ix++];
1108       n++;
1109     }
1110
1111   if (nelt != NULL)
1112     *nelt = n;
1113
1114   GDK_NOTE (DND, g_print ("%s\n", (n == celt) ? "S_OK" : "S_FALSE"));
1115
1116   if (n == celt)
1117     return S_OK;
1118   else
1119     return S_FALSE;
1120 }
1121
1122 static HRESULT STDMETHODCALLTYPE
1123 ienumformatetc_skip (LPENUMFORMATETC This,
1124                      ULONG           celt)
1125 {
1126   enum_formats *en = (enum_formats *) This;
1127
1128   GDK_NOTE (DND, g_print ("ienumformatetc_skip %p %d %ld S_OK\n", This, en->ix, celt));
1129
1130   en->ix += celt;
1131
1132   return S_OK;
1133 }
1134
1135 static HRESULT STDMETHODCALLTYPE
1136 ienumformatetc_reset (LPENUMFORMATETC This)
1137 {
1138   enum_formats *en = (enum_formats *) This;
1139
1140   GDK_NOTE (DND, g_print ("ienumformatetc_reset %p S_OK\n", This));
1141
1142   en->ix = 0;
1143
1144   return S_OK;
1145 }
1146
1147 static HRESULT STDMETHODCALLTYPE
1148 ienumformatetc_clone (LPENUMFORMATETC  This,
1149                       LPENUMFORMATETC *ppEnumFormatEtc)
1150 {
1151   enum_formats *en = (enum_formats *) This;
1152   enum_formats *new;
1153
1154   GDK_NOTE (DND, g_print ("ienumformatetc_clone %p S_OK\n", This));
1155
1156   new = enum_formats_new ();
1157
1158   new->ix = en->ix;
1159
1160   *ppEnumFormatEtc = &new->ief;
1161
1162   return S_OK;
1163 }
1164
1165 static IDropTargetVtbl idt_vtbl = {
1166   idroptarget_queryinterface,
1167   idroptarget_addref,
1168   idroptarget_release,
1169   idroptarget_dragenter,
1170   idroptarget_dragover,
1171   idroptarget_dragleave,
1172   idroptarget_drop
1173 };
1174
1175 static IDropSourceVtbl ids_vtbl = {
1176   idropsource_queryinterface,
1177   idropsource_addref,
1178   idropsource_release,
1179   idropsource_querycontinuedrag,
1180   idropsource_givefeedback
1181 };
1182
1183 static IDataObjectVtbl ido_vtbl = {
1184   idataobject_queryinterface,
1185   idataobject_addref,
1186   idataobject_release,
1187   idataobject_getdata,
1188   idataobject_getdatahere,
1189   idataobject_querygetdata,
1190   idataobject_getcanonicalformatetc,
1191   idataobject_setdata,
1192   idataobject_enumformatetc,
1193   idataobject_dadvise,
1194   idataobject_dunadvise,
1195   idataobject_enumdadvise
1196 };
1197
1198 static IEnumFORMATETCVtbl ief_vtbl = {
1199   ienumformatetc_queryinterface,
1200   ienumformatetc_addref,
1201   ienumformatetc_release,
1202   ienumformatetc_next,
1203   ienumformatetc_skip,
1204   ienumformatetc_reset,
1205   ienumformatetc_clone
1206 };
1207
1208
1209 static target_drag_context *
1210 target_context_new (GdkWindow *window)
1211 {
1212   target_drag_context *result;
1213   GdkDragContextPrivateWin32 *private;
1214   GdkDevice *device;
1215   GdkDeviceManager *device_manager;
1216
1217   result = g_new0 (target_drag_context, 1);
1218
1219   result->idt.lpVtbl = &idt_vtbl;
1220
1221   result->context = gdk_drag_context_new ();
1222   result->context->protocol = GDK_DRAG_PROTO_OLE2;
1223   result->context->is_source = FALSE;
1224
1225   device_manager = gdk_display_get_device_manager (_gdk_display);
1226   device = gdk_device_manager_get_client_pointer (device_manager);
1227   gdk_drag_context_set_device (result->context, device);
1228
1229   result->context->source_window = NULL;
1230
1231   result->context->dest_window = window;
1232   g_object_ref (window);
1233
1234   /* FIXME: result->context->targets? */
1235
1236   result->context->actions = GDK_ACTION_DEFAULT | GDK_ACTION_COPY | GDK_ACTION_MOVE;
1237   result->context->suggested_action = GDK_ACTION_MOVE;
1238   result->context->action = GDK_ACTION_MOVE;
1239
1240   private = result->context->windowing_data;
1241   private->iface = (IUnknown *) &result->idt;
1242   idroptarget_addref (&result->idt);
1243
1244   GDK_NOTE (DND, g_print ("target_context_new: %p\n", result));
1245
1246   return result;
1247 }
1248
1249 static source_drag_context *
1250 source_context_new (GdkWindow *window,
1251                     GList     *targets)
1252 {
1253   source_drag_context *result;
1254   GdkDragContextPrivateWin32 *private;
1255   GdkDevice *device;
1256   GdkDeviceManager *device_manager;
1257
1258   result = g_new0 (source_drag_context, 1);
1259
1260   result->ids.lpVtbl = &ids_vtbl;
1261
1262   result->context = gdk_drag_context_new ();
1263   result->context->protocol = GDK_DRAG_PROTO_OLE2;
1264   result->context->is_source = TRUE;
1265
1266   device_manager = gdk_display_get_device_manager (_gdk_display);
1267   device = gdk_device_manager_get_client_pointer (device_manager);
1268   gdk_drag_context_set_device (result->context, device);
1269
1270   result->context->source_window = window;
1271   g_object_ref (window);
1272
1273   result->context->dest_window = NULL;
1274   result->context->targets = g_list_copy (targets);
1275
1276   private = result->context->windowing_data;
1277   private->iface = (IUnknown *) &result->ids;
1278   idropsource_addref (&result->ids);
1279
1280   GDK_NOTE (DND, g_print ("source_context_new: %p\n", result));
1281
1282   return result;
1283 }
1284
1285 static data_object *
1286 data_object_new (GdkDragContext *context)
1287 {
1288   data_object *result;
1289
1290   result = g_new0 (data_object, 1);
1291
1292   result->ido.lpVtbl = &ido_vtbl;
1293   result->ref_count = 1;
1294   result->context = context;
1295
1296   GDK_NOTE (DND, g_print ("data_object_new: %p\n", result));
1297
1298   return result;
1299 }
1300
1301 static enum_formats *
1302 enum_formats_new (void)
1303 {
1304   enum_formats *result;
1305
1306   result = g_new0 (enum_formats, 1);
1307
1308   result->ief.lpVtbl = &ief_vtbl;
1309   result->ref_count = 1;
1310   result->ix = 0;
1311
1312   return result;
1313 }
1314
1315 void
1316 _gdk_win32_ole2_dnd_property_change (GdkAtom       type,
1317                                      gint          format,
1318                                      const guchar *data,
1319                                      gint          nelements)
1320 {
1321   if (use_ole2_dnd)
1322     {
1323       HGLOBAL hdata = NULL;
1324
1325       if (active_pFormatEtc == NULL || active_pMedium == NULL)
1326         return;
1327
1328       /* Set up the data buffer for wide character text request */
1329       if (active_pFormatEtc->cfFormat == CF_UNICODETEXT)
1330         {
1331           gunichar2 *wdata;
1332           glong wlen;
1333
1334           wdata = g_utf8_to_utf16 ((const char *) data, -1, NULL, &wlen, NULL);
1335           hdata = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, (wlen + 1) * 2);
1336           if (hdata)
1337             {
1338               wchar_t *ptr = (wchar_t *) GlobalLock(hdata);
1339               memcpy (ptr, wdata, (wlen + 1) * 2);
1340               GlobalUnlock(hdata);
1341             }
1342           g_free (wdata);
1343         }
1344       else
1345         g_warning ("Only text handled for now");
1346
1347       /* Pack up data */
1348       active_pMedium->tymed = TYMED_HGLOBAL;
1349       active_pMedium->hGlobal = hdata;
1350       active_pMedium->pUnkForRelease = 0;
1351     }
1352 }
1353
1354 /* From MS Knowledge Base article Q130698 */
1355
1356 static gboolean
1357 resolve_link (HWND     hWnd,
1358               wchar_t *link,
1359               gchar  **lpszPath)
1360 {
1361   WIN32_FILE_ATTRIBUTE_DATA wfad;
1362   HRESULT hr;
1363   IShellLinkW *pslW = NULL;
1364   IPersistFile *ppf = NULL;
1365
1366   /* Check if the file is empty first because IShellLink::Resolve for
1367    * some reason succeeds with an empty file and returns an empty
1368    * "link target". (#524151)
1369    */
1370     if (!GetFileAttributesExW (link, GetFileExInfoStandard, &wfad) ||
1371         (wfad.nFileSizeHigh == 0 && wfad.nFileSizeLow == 0))
1372       return FALSE;
1373
1374   /* Assume failure to start with: */
1375   *lpszPath = 0;
1376
1377   /* Call CoCreateInstance to obtain the IShellLink interface
1378    * pointer. This call fails if CoInitialize is not called, so it is
1379    * assumed that CoInitialize has been called.
1380    */
1381
1382   hr = CoCreateInstance (&CLSID_ShellLink,
1383                          NULL,
1384                          CLSCTX_INPROC_SERVER,
1385                          &IID_IShellLinkW,
1386                          (LPVOID *)&pslW);
1387
1388   if (SUCCEEDED (hr))
1389    {
1390      /* The IShellLink interface supports the IPersistFile
1391       * interface. Get an interface pointer to it.
1392       */
1393      hr = pslW->lpVtbl->QueryInterface (pslW,
1394                                         &IID_IPersistFile,
1395                                         (LPVOID *) &ppf);
1396    }
1397
1398   if (SUCCEEDED (hr))
1399     {
1400       /* Load the file. */
1401       hr = ppf->lpVtbl->Load (ppf, link, STGM_READ);
1402     }
1403
1404   if (SUCCEEDED (hr))
1405     {
1406       /* Resolve the link by calling the Resolve()
1407        * interface function.
1408        */
1409       hr = pslW->lpVtbl->Resolve (pslW, hWnd, SLR_ANY_MATCH | SLR_NO_UI);
1410     }
1411
1412   if (SUCCEEDED (hr))
1413     {
1414       wchar_t wtarget[MAX_PATH];
1415
1416       hr = pslW->lpVtbl->GetPath (pslW, wtarget, MAX_PATH, NULL, 0);
1417       if (SUCCEEDED (hr))
1418         *lpszPath = g_utf16_to_utf8 (wtarget, -1, NULL, NULL, NULL);
1419     }
1420
1421   if (ppf)
1422     ppf->lpVtbl->Release (ppf);
1423
1424   if (pslW)
1425     pslW->lpVtbl->Release (pslW);
1426
1427   return SUCCEEDED (hr);
1428 }
1429
1430 #if 0
1431
1432 /* Check for filenames like C:\Users\tml\AppData\Local\Temp\d5qtkvvs.bmp */
1433 static gboolean
1434 filename_looks_tempish (const char *filename)
1435 {
1436   char *dirname;
1437   char *p;
1438   const char *q;
1439   gboolean retval = FALSE;
1440
1441   dirname = g_path_get_dirname (filename);
1442
1443   p = dirname;
1444   q = g_get_tmp_dir ();
1445
1446   while (*p && *q &&
1447          ((G_IS_DIR_SEPARATOR (*p) && G_IS_DIR_SEPARATOR (*q)) ||
1448           g_ascii_tolower (*p) == g_ascii_tolower (*q)))
1449     p++, q++;
1450
1451   if (!*p && !*q)
1452     retval = TRUE;
1453
1454   g_free (dirname);
1455
1456   return retval;
1457 }
1458
1459 static gboolean
1460 close_it (gpointer data)
1461 {
1462   close (GPOINTER_TO_INT (data));
1463
1464   return FALSE;
1465 }
1466
1467 #endif
1468
1469 static GdkFilterReturn
1470 gdk_dropfiles_filter (GdkXEvent *xev,
1471                       GdkEvent  *event,
1472                       gpointer   data)
1473 {
1474   GdkDragContext *context;
1475   GString *result;
1476   MSG *msg = (MSG *) xev;
1477   HANDLE hdrop;
1478   POINT pt;
1479   gint nfiles, i;
1480   gchar *fileName, *linkedFile;
1481   GdkDevice *device;
1482   GdkDeviceManager *device_manager;
1483
1484   if (msg->message == WM_DROPFILES)
1485     {
1486       GDK_NOTE (DND, g_print ("WM_DROPFILES: %p\n", msg->hwnd));
1487
1488       context = gdk_drag_context_new ();
1489       context->protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
1490       context->is_source = FALSE;
1491
1492       device_manager = gdk_display_get_device_manager (_gdk_display);
1493       device = gdk_device_manager_get_client_pointer (device_manager);
1494       gdk_drag_context_set_device (context, device);
1495
1496       context->source_window = _gdk_root;
1497       g_object_ref (context->source_window);
1498
1499       context->dest_window = event->any.window;
1500       g_object_ref (context->dest_window);
1501
1502       /* WM_DROPFILES drops are always file names */
1503       context->targets =
1504         g_list_append (NULL, _text_uri_list);
1505       context->actions = GDK_ACTION_COPY;
1506       context->suggested_action = GDK_ACTION_COPY;
1507       current_dest_drag = context;
1508
1509       event->dnd.type = GDK_DROP_START;
1510       event->dnd.context = current_dest_drag;
1511       gdk_event_set_device (event, gdk_drag_context_get_device (current_dest_drag));
1512
1513       hdrop = (HANDLE) msg->wParam;
1514       DragQueryPoint (hdrop, &pt);
1515       ClientToScreen (msg->hwnd, &pt);
1516
1517       event->dnd.x_root = pt.x + _gdk_offset_x;
1518       event->dnd.y_root = pt.y + _gdk_offset_y;
1519       event->dnd.time = _gdk_win32_get_next_tick (msg->time);
1520
1521       nfiles = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
1522
1523       result = g_string_new (NULL);
1524       for (i = 0; i < nfiles; i++)
1525         {
1526           gchar *uri;
1527           wchar_t wfn[MAX_PATH];
1528
1529           DragQueryFileW (hdrop, i, wfn, MAX_PATH);
1530           fileName = g_utf16_to_utf8 (wfn, -1, NULL, NULL, NULL);
1531
1532           /* Resolve shortcuts */
1533           if (resolve_link (msg->hwnd, wfn, &linkedFile))
1534             {
1535               uri = g_filename_to_uri (linkedFile, NULL, NULL);
1536               if (uri != NULL)
1537                 {
1538                   g_string_append (result, uri);
1539                   GDK_NOTE (DND, g_print ("... %s link to %s: %s\n",
1540                                           fileName, linkedFile, uri));
1541                   g_free (uri);
1542                 }
1543               g_free (fileName);
1544               fileName = linkedFile;
1545             }
1546           else
1547             {
1548               uri = g_filename_to_uri (fileName, NULL, NULL);
1549               if (uri != NULL)
1550                 {
1551                   g_string_append (result, uri);
1552                   GDK_NOTE (DND, g_print ("... %s: %s\n", fileName, uri));
1553                   g_free (uri);
1554                 }
1555             }
1556
1557 #if 0
1558           /* Awful hack to recognize temp files corresponding to
1559            * images dragged from Firefox... Open the file right here
1560            * so that it is less likely that Firefox manages to delete
1561            * it before the GTK+-using app (typically GIMP) has opened
1562            * it.
1563            *
1564            * Not compiled in for now, because it means images dragged
1565            * from Firefox would stay around in the temp folder which
1566            * is not what Firefox intended. I don't feel comfortable
1567            * with that, both from a geenral sanity point of view, and
1568            * from a privacy point of view. It's better to wait for
1569            * Firefox to fix the problem, for instance by deleting the
1570            * temp file after a longer delay, or to wait until we
1571            * implement the OLE2_DND...
1572            */
1573           if (filename_looks_tempish (fileName))
1574             {
1575               int fd = g_open (fileName, _O_RDONLY|_O_BINARY, 0);
1576               if (fd == -1)
1577                 {
1578                   GDK_NOTE (DND, g_print ("Could not open %s, maybe an image dragged from Firefox that it already deleted\n", fileName));
1579                 }
1580               else
1581                 {
1582                   GDK_NOTE (DND, g_print ("Opened %s as %d so that Firefox won't delete it\n", fileName, fd));
1583                   g_timeout_add_seconds (1, close_it, GINT_TO_POINTER (fd));
1584                 }
1585             }
1586 #endif
1587
1588           g_free (fileName);
1589           g_string_append (result, "\015\012");
1590         }
1591       _gdk_dropfiles_store (result->str);
1592       g_string_free (result, FALSE);
1593
1594       DragFinish (hdrop);
1595
1596       return GDK_FILTER_TRANSLATE;
1597     }
1598   else
1599     return GDK_FILTER_CONTINUE;
1600 }
1601
1602 static void
1603 add_format (GArray *fmts,
1604             CLIPFORMAT cf)
1605 {
1606   FORMATETC fmt;
1607
1608   fmt.cfFormat = cf;
1609   fmt.ptd = NULL;
1610   fmt.dwAspect = DVASPECT_CONTENT;
1611   fmt.lindex = -1;
1612   fmt.tymed = TYMED_HGLOBAL;
1613
1614   g_array_append_val (fmts, fmt);
1615 }
1616
1617
1618 void
1619 _gdk_dnd_init (void)
1620 {
1621   if (getenv ("GDK_WIN32_USE_EXPERIMENTAL_OLE2_DND"))
1622     use_ole2_dnd = TRUE;
1623
1624   if (use_ole2_dnd)
1625     {
1626       HRESULT hr;
1627       GArray *fmts;
1628
1629       hr = OleInitialize (NULL);
1630
1631       if (! SUCCEEDED (hr))
1632         g_error ("OleInitialize failed");
1633
1634       fmts = g_array_new (FALSE, FALSE, sizeof (FORMATETC));
1635
1636       /* The most important presumably */
1637       add_format (fmts, CF_UNICODETEXT);
1638
1639       /* Used for GTK+ internal DND, I think was the intent? Anyway, code below assumes
1640        * this is at index 1.
1641        */
1642       add_format (fmts, CF_GDIOBJFIRST);
1643
1644       add_format (fmts, CF_HDROP);
1645
1646       add_format (fmts, _cf_png);
1647       add_format (fmts, CF_DIB);
1648
1649       add_format (fmts, _cf_url);
1650       add_format (fmts, _cf_html_format);
1651       add_format (fmts, _cf_text_html);
1652
1653       nformats = fmts->len;
1654       formats = (FORMATETC*) g_array_free (fmts, FALSE);
1655
1656       target_ctx_for_window = g_hash_table_new (g_direct_hash, g_direct_equal);
1657     }
1658 }
1659
1660 void
1661 _gdk_win32_dnd_exit (void)
1662 {
1663   if (use_ole2_dnd)
1664     {
1665       OleUninitialize ();
1666     }
1667 }
1668
1669 /* Source side */
1670
1671 static void
1672 local_send_leave (GdkDragContext *context,
1673                   guint32         time)
1674 {
1675   GdkEvent *tmp_event;
1676
1677   GDK_NOTE (DND, g_print ("local_send_leave: context=%p current_dest_drag=%p\n",
1678                           context,
1679                           current_dest_drag));
1680
1681   if ((current_dest_drag != NULL) &&
1682       (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1683       (current_dest_drag->source_window == context->source_window))
1684     {
1685       tmp_event = gdk_event_new (GDK_DRAG_LEAVE);
1686
1687       tmp_event->dnd.window = g_object_ref (context->dest_window);
1688       /* Pass ownership of context to the event */
1689       tmp_event->dnd.send_event = FALSE;
1690       tmp_event->dnd.context = g_object_ref (current_dest_drag);
1691       tmp_event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1692       gdk_event_set_device (tmp_event, gdk_drag_context_get_device (context));
1693
1694       current_dest_drag = NULL;
1695
1696       GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
1697       gdk_event_put (tmp_event);
1698       gdk_event_free (tmp_event);
1699     }
1700 }
1701
1702 static void
1703 local_send_enter (GdkDragContext *context,
1704                   guint32         time)
1705 {
1706   GdkEvent *tmp_event;
1707   GdkDragContextPrivateWin32 *private;
1708   GdkDragContext *new_context;
1709   GdkDevice *device;
1710   GdkDeviceManager *device_manager;
1711
1712   GDK_NOTE (DND, g_print ("local_send_enter: context=%p current_dest_drag=%p\n",
1713                           context,
1714                           current_dest_drag));
1715
1716   private = PRIVATE_DATA (context);
1717
1718   if (current_dest_drag != NULL)
1719     {
1720       g_object_unref (G_OBJECT (current_dest_drag));
1721       current_dest_drag = NULL;
1722     }
1723
1724   new_context = gdk_drag_context_new ();
1725   new_context->protocol = GDK_DRAG_PROTO_LOCAL;
1726   new_context->is_source = FALSE;
1727
1728   device_manager = gdk_display_get_device_manager (_gdk_display);
1729   device = gdk_device_manager_get_client_pointer (device_manager);
1730   gdk_drag_context_set_device (new_context, device);
1731
1732   new_context->source_window = context->source_window;
1733   g_object_ref (new_context->source_window);
1734
1735   new_context->dest_window = context->dest_window;
1736   g_object_ref (new_context->dest_window);
1737
1738   new_context->targets = g_list_copy (context->targets);
1739
1740   gdk_window_set_events (new_context->source_window,
1741                          gdk_window_get_events (new_context->source_window) |
1742                          GDK_PROPERTY_CHANGE_MASK);
1743   new_context->actions = context->actions;
1744
1745   tmp_event = gdk_event_new (GDK_DRAG_ENTER);
1746   tmp_event->dnd.window = g_object_ref (context->dest_window);
1747   tmp_event->dnd.send_event = FALSE;
1748   tmp_event->dnd.context = g_object_ref (new_context);
1749   tmp_event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1750   gdk_event_set_device (tmp_event, gdk_drag_context_get_device (context));
1751
1752   current_dest_drag = new_context;
1753
1754   GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
1755   gdk_event_put (tmp_event);
1756   gdk_event_free (tmp_event);
1757 }
1758
1759 static void
1760 local_send_motion (GdkDragContext *context,
1761                    gint            x_root,
1762                    gint            y_root,
1763                    GdkDragAction   action,
1764                    guint32         time)
1765 {
1766   GdkEvent *tmp_event;
1767
1768   GDK_NOTE (DND, g_print ("local_send_motion: context=%p (%d,%d) current_dest_drag=%p\n",
1769                           context, x_root, y_root,
1770                           current_dest_drag));
1771
1772   if ((current_dest_drag != NULL) &&
1773       (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1774       (current_dest_drag->source_window == context->source_window))
1775     {
1776       tmp_event = gdk_event_new (GDK_DRAG_MOTION);
1777       tmp_event->dnd.window = g_object_ref (current_dest_drag->dest_window);
1778       tmp_event->dnd.send_event = FALSE;
1779       tmp_event->dnd.context = g_object_ref (current_dest_drag);
1780       tmp_event->dnd.time = time;
1781       gdk_event_set_device (tmp_event, gdk_drag_context_get_device (current_dest_drag));
1782
1783       current_dest_drag->suggested_action = action;
1784
1785       tmp_event->dnd.x_root = x_root;
1786       tmp_event->dnd.y_root = y_root;
1787
1788       PRIVATE_DATA (current_dest_drag)->last_pt.x = x_root - _gdk_offset_x;
1789       PRIVATE_DATA (current_dest_drag)->last_pt.y = y_root - _gdk_offset_y;
1790
1791       PRIVATE_DATA (context)->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1792
1793       GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
1794       gdk_event_put (tmp_event);
1795       gdk_event_free (tmp_event);
1796     }
1797 }
1798
1799 static void
1800 local_send_drop (GdkDragContext *context,
1801                  guint32         time)
1802 {
1803   GdkEvent *tmp_event;
1804
1805   GDK_NOTE (DND, g_print ("local_send_drop: context=%p current_dest_drag=%p\n",
1806                           context,
1807                           current_dest_drag));
1808
1809   if ((current_dest_drag != NULL) &&
1810       (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1811       (current_dest_drag->source_window == context->source_window))
1812     {
1813       GdkDragContextPrivateWin32 *private;
1814       private = PRIVATE_DATA (current_dest_drag);
1815
1816       /* Pass ownership of context to the event */
1817       tmp_event = gdk_event_new (GDK_DROP_START);
1818       tmp_event->dnd.window = g_object_ref (current_dest_drag->dest_window);
1819       tmp_event->dnd.send_event = FALSE;
1820       tmp_event->dnd.context = g_object_ref (current_dest_drag);
1821       tmp_event->dnd.time = GDK_CURRENT_TIME;
1822       gdk_event_set_device (tmp_event, gdk_drag_context_get_device (current_dest_drag));
1823
1824       tmp_event->dnd.x_root = private->last_pt.x + _gdk_offset_x;
1825       tmp_event->dnd.y_root = private->last_pt.y + _gdk_offset_y;
1826
1827       current_dest_drag = NULL;
1828
1829       GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
1830       gdk_event_put (tmp_event);
1831       gdk_event_free (tmp_event);
1832     }
1833
1834 }
1835
1836 static void
1837 gdk_drag_do_leave (GdkDragContext *context,
1838                    guint32         time)
1839 {
1840   if (context->dest_window)
1841     {
1842       GDK_NOTE (DND, g_print ("gdk_drag_do_leave\n"));
1843
1844       if (!use_ole2_dnd)
1845         {
1846           if (context->protocol == GDK_DRAG_PROTO_LOCAL)
1847             local_send_leave (context, time);
1848         }
1849
1850       g_object_unref (context->dest_window);
1851       context->dest_window = NULL;
1852     }
1853 }
1854
1855 GdkDragContext *
1856 gdk_drag_begin (GdkWindow *window,
1857                 GList     *targets)
1858 {
1859   if (!use_ole2_dnd)
1860     {
1861       GdkDragContext *new_context;
1862       GdkDevice *device;
1863       GdkDeviceManager *device_manager;
1864
1865       g_return_val_if_fail (window != NULL, NULL);
1866
1867       new_context = gdk_drag_context_new ();
1868
1869       device_manager = gdk_display_get_device_manager (_gdk_display);
1870       device = gdk_device_manager_get_client_pointer (device_manager);
1871       gdk_drag_context_set_device (new_context, device);
1872
1873       new_context->is_source = TRUE;
1874
1875       new_context->source_window = window;
1876       g_object_ref (window);
1877
1878       new_context->targets = g_list_copy (targets);
1879       new_context->actions = 0;
1880
1881       return new_context;
1882     }
1883   else
1884     {
1885       source_drag_context *ctx;
1886
1887       g_return_val_if_fail (window != NULL, NULL);
1888
1889       GDK_NOTE (DND, g_print ("gdk_drag_begin\n"));
1890
1891       ctx = source_context_new (window, targets);
1892
1893       _dnd_source_state = GDK_WIN32_DND_PENDING;
1894
1895       pending_src_context = ctx;
1896       g_object_ref (ctx->context);
1897
1898       return ctx->context;
1899     }
1900 }
1901
1902 void
1903 _gdk_win32_dnd_do_dragdrop (void)
1904 {
1905   if (use_ole2_dnd)
1906     {
1907       GdkDragContext* drag_ctx;
1908       GdkDragContextPrivateWin32 *private;
1909       BYTE kbd_state[256];
1910       data_object *dobj;
1911       HRESULT hr;
1912       DWORD dwEffect;
1913 #if 0
1914       HGLOBAL global;
1915       STGMEDIUM medium;
1916 #endif
1917
1918       if (pending_src_context == NULL)
1919         return;
1920
1921       drag_ctx = pending_src_context->context;
1922       private = PRIVATE_DATA (drag_ctx);
1923
1924       dobj = data_object_new (drag_ctx);
1925
1926       API_CALL (GetCursorPos, (&private->last_pt));
1927       API_CALL (ScreenToClient, (GDK_WINDOW_HWND (drag_ctx->source_window), &private->last_pt));
1928       private->last_key_state = 0;
1929       API_CALL (GetKeyboardState, (kbd_state));
1930
1931       if (kbd_state[VK_CONTROL])
1932         private->last_key_state |= MK_CONTROL;
1933       if (kbd_state[VK_SHIFT])
1934         private->last_key_state |= MK_SHIFT;
1935       if (kbd_state[VK_LBUTTON])
1936         private->last_key_state |= MK_LBUTTON;
1937       if (kbd_state[VK_MBUTTON])
1938         private->last_key_state |= MK_MBUTTON;
1939       if (kbd_state[VK_RBUTTON])
1940         private->last_key_state |= MK_RBUTTON;
1941
1942 #if 0
1943       global = GlobalAlloc (GMEM_FIXED, sizeof (ctx));
1944
1945       memcpy (&global, ctx, sizeof (ctx));
1946
1947       medium.tymed = TYMED_HGLOBAL;
1948       medium.hGlobal = global;
1949       medium.pUnkForRelease = NULL;
1950
1951       /* FIXME I wish I remember what I was thinking of here, i.e. what
1952        * the formats[1] signifies, i.e. the CF_GDIOBJFIRST FORMATETC?
1953        */
1954       dobj->ido.lpVtbl->SetData (&dobj->ido, &formats[1], &medium, TRUE);
1955 #endif
1956
1957       /* Start dragging with mainloop inside the OLE2 API. Exits only when done */
1958
1959       GDK_NOTE (DND, g_print ("Calling DoDragDrop\n"));
1960
1961       _gdk_win32_begin_modal_call ();
1962       hr = DoDragDrop (&dobj->ido, &pending_src_context->ids,
1963                        DROPEFFECT_COPY | DROPEFFECT_MOVE,
1964                        &dwEffect);
1965       _gdk_win32_end_modal_call ();
1966
1967       GDK_NOTE (DND, g_print ("DoDragDrop returned %s\n",
1968                               (hr == DRAGDROP_S_DROP ? "DRAGDROP_S_DROP" :
1969                                (hr == DRAGDROP_S_CANCEL ? "DRAGDROP_S_CANCEL" :
1970                                 (hr == E_UNEXPECTED ? "E_UNEXPECTED" :
1971                                  g_strdup_printf ("%#.8lx", hr))))));
1972
1973       /* Delete dnd selection after successful move */
1974       if (hr == DRAGDROP_S_DROP && dwEffect == DROPEFFECT_MOVE)
1975         {
1976           GdkEvent tmp_event;
1977
1978           tmp_event.type = GDK_SELECTION_REQUEST;
1979           tmp_event.selection.window = drag_ctx->source_window;
1980           tmp_event.selection.send_event = FALSE;
1981           tmp_event.selection.selection = _gdk_ole2_dnd;
1982           tmp_event.selection.target = _delete;
1983           tmp_event.selection.property = _gdk_ole2_dnd; /* ??? */
1984           tmp_event.selection.time = GDK_CURRENT_TIME; /* ??? */
1985           g_object_ref (tmp_event.selection.window);
1986
1987           GDK_NOTE (EVENTS, _gdk_win32_print_event (&tmp_event));
1988           gdk_event_put (&tmp_event);
1989         }
1990
1991 #if 0
1992       // Send a GDK_DROP_FINISHED to the source window
1993       GetCursorPos (&pt);
1994       ptl.x = pt.x;
1995       ptl.y = pt.y;
1996       if ( pending_src_context != NULL && pending_src_context->context != NULL
1997            && pending_src_context->context->source_window != NULL )
1998         push_dnd_event (GDK_DROP_FINISHED, pending_src_context->context, ptl, FALSE);
1999 #endif
2000
2001       dobj->ido.lpVtbl->Release (&dobj->ido);
2002       if (pending_src_context != NULL)
2003         {
2004           pending_src_context->ids.lpVtbl->Release (&pending_src_context->ids);
2005           pending_src_context = NULL;
2006         }
2007     }
2008 }
2009
2010 GdkNativeWindow
2011 gdk_drag_get_protocol_for_display (GdkDisplay      *display,
2012                                    GdkNativeWindow  xid,
2013                                    GdkDragProtocol *protocol)
2014 {
2015   GdkWindow *window;
2016
2017   window = gdk_window_lookup (xid);
2018   if (window &&
2019       gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2020     {
2021       if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
2022         {
2023           if (use_ole2_dnd)
2024             *protocol = GDK_DRAG_PROTO_OLE2;
2025           else
2026             *protocol = GDK_DRAG_PROTO_LOCAL;
2027
2028           return xid;
2029         }
2030     }
2031
2032   return 0;
2033 }
2034
2035 void
2036 gdk_drag_find_window_for_screen (GdkDragContext  *context,
2037                                  GdkWindow       *drag_window,
2038                                  GdkScreen       *screen,
2039                                  gint             x_root,
2040                                  gint             y_root,
2041                                  GdkWindow      **dest_window,
2042                                  GdkDragProtocol *protocol)
2043 {
2044   POINT pt;
2045   HWND hwnd;
2046
2047   pt.x = x_root - _gdk_offset_x;
2048   pt.y = y_root - _gdk_offset_y;
2049
2050   hwnd = WindowFromPoint (pt);
2051
2052   if (hwnd == NULL)
2053     *dest_window = NULL;
2054   else
2055     {
2056       *dest_window = gdk_win32_handle_table_lookup (hwnd);
2057       if (*dest_window)
2058         g_object_ref (*dest_window);
2059       else
2060         *dest_window = gdk_window_foreign_new_for_display (_gdk_display, hwnd);
2061
2062       if (use_ole2_dnd)
2063         *protocol = GDK_DRAG_PROTO_OLE2;
2064       else if (context->source_window)
2065         *protocol = GDK_DRAG_PROTO_LOCAL;
2066       else
2067         *protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
2068     }
2069
2070   GDK_NOTE (DND,
2071             g_print ("gdk_drag_find_window: %p %+d%+d: %p: %p %s\n",
2072                      (drag_window ? GDK_WINDOW_HWND (drag_window) : NULL),
2073                      x_root, y_root,
2074                      hwnd,
2075                      (*dest_window ? GDK_WINDOW_HWND (*dest_window) : NULL),
2076                      _gdk_win32_drag_protocol_to_string (*protocol)));
2077 }
2078
2079 gboolean
2080 gdk_drag_motion (GdkDragContext *context,
2081                  GdkWindow      *dest_window,
2082                  GdkDragProtocol protocol,
2083                  gint            x_root,
2084                  gint            y_root,
2085                  GdkDragAction   suggested_action,
2086                  GdkDragAction   possible_actions,
2087                  guint32         time)
2088 {
2089   GdkDragContextPrivateWin32 *private;
2090
2091   g_return_val_if_fail (context != NULL, FALSE);
2092
2093   context->actions = possible_actions;
2094
2095   GDK_NOTE (DND, g_print ("gdk_drag_motion: %s suggested=%s, possible=%s\n"
2096                           " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2097                           _gdk_win32_drag_protocol_to_string (protocol),
2098                           _gdk_win32_drag_action_to_string (suggested_action),
2099                           _gdk_win32_drag_action_to_string (possible_actions),
2100                           context,
2101                           _gdk_win32_drag_action_to_string (context->actions),
2102                           _gdk_win32_drag_action_to_string (context->suggested_action),
2103                           _gdk_win32_drag_action_to_string (context->action)));
2104
2105   private = PRIVATE_DATA (context);
2106
2107   if (!use_ole2_dnd)
2108     {
2109       if (context->dest_window == dest_window)
2110         {
2111           GdkDragContext *dest_context;
2112
2113           dest_context = gdk_drag_context_find (FALSE,
2114                                                 context->source_window,
2115                                                 dest_window);
2116
2117           if (dest_context)
2118             dest_context->actions = context->actions;
2119
2120           context->suggested_action = suggested_action;
2121         }
2122       else
2123         {
2124           GdkEvent *tmp_event;
2125
2126           /* Send a leave to the last destination */
2127           gdk_drag_do_leave (context, time);
2128           private->drag_status = GDK_DRAG_STATUS_DRAG;
2129
2130           /* Check if new destination accepts drags, and which protocol */
2131           if (dest_window)
2132             {
2133               context->dest_window = dest_window;
2134               g_object_ref (context->dest_window);
2135               context->protocol = protocol;
2136
2137               switch (protocol)
2138                 {
2139                 case GDK_DRAG_PROTO_LOCAL:
2140                   local_send_enter (context, time);
2141                   break;
2142
2143                 default:
2144                   break;
2145                 }
2146               context->suggested_action = suggested_action;
2147             }
2148           else
2149             {
2150               context->dest_window = NULL;
2151               context->action = 0;
2152             }
2153
2154           /* Push a status event, to let the client know that
2155            * the drag changed
2156            */
2157     tmp_event = gdk_event_new (GDK_DRAG_STATUS);
2158           tmp_event->dnd.window = g_object_ref (context->source_window);
2159           /* We use this to signal a synthetic status. Perhaps
2160            * we should use an extra field...
2161            */
2162           tmp_event->dnd.send_event = TRUE;
2163
2164           tmp_event->dnd.context = g_object_ref (context);
2165           tmp_event->dnd.time = time;
2166     gdk_event_set_device (tmp_event, gdk_drag_context_get_device (context));
2167
2168           GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
2169           gdk_event_put (tmp_event);
2170     gdk_event_free (tmp_event);
2171         }
2172
2173       /* Send a drag-motion event */
2174
2175       private->last_pt.x = x_root - _gdk_offset_x;
2176       private->last_pt.y = y_root - _gdk_offset_y;
2177
2178       if (context->dest_window)
2179         {
2180           if (private->drag_status == GDK_DRAG_STATUS_DRAG)
2181             {
2182               switch (context->protocol)
2183                 {
2184                 case GDK_DRAG_PROTO_LOCAL:
2185                   local_send_motion (context, x_root, y_root, suggested_action, time);
2186                   break;
2187
2188                 case GDK_DRAG_PROTO_NONE:
2189                   g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
2190                   break;
2191
2192                 default:
2193                   break;
2194                 }
2195             }
2196           else
2197             {
2198               GDK_NOTE (DND, g_print (" returning TRUE\n"
2199                                       " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2200                                       context,
2201                                       _gdk_win32_drag_action_to_string (context->actions),
2202                                       _gdk_win32_drag_action_to_string (context->suggested_action),
2203                                       _gdk_win32_drag_action_to_string (context->action)));
2204               return TRUE;
2205             }
2206         }
2207     }
2208
2209   GDK_NOTE (DND, g_print (" returning FALSE\n"
2210                           " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2211                           context,
2212                           _gdk_win32_drag_action_to_string (context->actions),
2213                           _gdk_win32_drag_action_to_string (context->suggested_action),
2214                           _gdk_win32_drag_action_to_string (context->action)));
2215   return FALSE;
2216 }
2217
2218 void
2219 gdk_drag_drop (GdkDragContext *context,
2220                guint32         time)
2221 {
2222   g_return_if_fail (context != NULL);
2223
2224   GDK_NOTE (DND, g_print ("gdk_drag_drop\n"));
2225
2226   if (!use_ole2_dnd)
2227     {
2228       if (context->dest_window &&
2229           context->protocol == GDK_DRAG_PROTO_LOCAL)
2230         local_send_drop (context, time);
2231     }
2232   else
2233     {
2234       _dnd_source_state = GDK_WIN32_DND_DROPPED;
2235     }
2236 }
2237
2238 void
2239 gdk_drag_abort (GdkDragContext *context,
2240                 guint32         time)
2241 {
2242   g_return_if_fail (context != NULL);
2243
2244   GDK_NOTE (DND, g_print ("gdk_drag_abort\n"));
2245
2246   if (use_ole2_dnd)
2247     _dnd_source_state = GDK_WIN32_DND_NONE;
2248 }
2249
2250 /* Destination side */
2251
2252 void
2253 gdk_drag_status (GdkDragContext *context,
2254                  GdkDragAction   action,
2255                  guint32         time)
2256 {
2257   GdkDragContextPrivateWin32 *private;
2258   GdkDragContext *src_context;
2259   GdkEvent *tmp_event;
2260
2261   g_return_if_fail (context != NULL);
2262
2263   private = PRIVATE_DATA (context);
2264
2265   GDK_NOTE (DND, g_print ("gdk_drag_status: %s\n"
2266                           " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2267                           _gdk_win32_drag_action_to_string (action),
2268                           context,
2269                           _gdk_win32_drag_action_to_string (context->actions),
2270                           _gdk_win32_drag_action_to_string (context->suggested_action),
2271                           _gdk_win32_drag_action_to_string (context->action)));
2272
2273   context->action = action;
2274
2275   if (!use_ole2_dnd)
2276     {
2277       src_context = gdk_drag_context_find (TRUE,
2278                                            context->source_window,
2279                                            context->dest_window);
2280
2281       if (src_context)
2282         {
2283           GdkDragContextPrivateWin32 *private = PRIVATE_DATA (src_context);
2284
2285           if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2286             private->drag_status = GDK_DRAG_STATUS_DRAG;
2287
2288           tmp_event = gdk_event_new (GDK_DRAG_STATUS);
2289           tmp_event->dnd.window = g_object_ref (context->source_window);
2290           tmp_event->dnd.send_event = FALSE;
2291           tmp_event->dnd.context = g_object_ref (src_context);
2292           tmp_event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2293     gdk_event_set_device (tmp_event, gdk_drag_context_get_device (src_context));
2294
2295           if (action == GDK_ACTION_DEFAULT)
2296             action = 0;
2297
2298           src_context->action = action;
2299
2300           GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
2301           gdk_event_put (tmp_event);
2302     gdk_event_free (tmp_event);
2303         }
2304     }
2305 }
2306
2307 void
2308 gdk_drop_reply (GdkDragContext *context,
2309                 gboolean        ok,
2310                 guint32         time)
2311 {
2312   g_return_if_fail (context != NULL);
2313
2314   GDK_NOTE (DND, g_print ("gdk_drop_reply\n"));
2315
2316   if (!use_ole2_dnd)
2317     if (context->dest_window)
2318       {
2319         if (context->protocol == GDK_DRAG_PROTO_WIN32_DROPFILES)
2320           _gdk_dropfiles_store (NULL);
2321       }
2322 }
2323
2324 void
2325 gdk_drop_finish (GdkDragContext *context,
2326                  gboolean        success,
2327                  guint32         time)
2328 {
2329   GdkDragContextPrivateWin32 *private;
2330   GdkDragContext *src_context;
2331   GdkEvent *tmp_event;
2332
2333   g_return_if_fail (context != NULL);
2334
2335   GDK_NOTE (DND, g_print ("gdk_drop_finish\n"));
2336
2337   private = PRIVATE_DATA (context);
2338
2339   if (!use_ole2_dnd)
2340     {
2341       src_context = gdk_drag_context_find (TRUE,
2342                                            context->source_window,
2343                                            context->dest_window);
2344       if (src_context)
2345         {
2346     tmp_event = gdk_event_new (GDK_DROP_FINISHED);
2347           tmp_event->dnd.window = g_object_ref (src_context->source_window);
2348           tmp_event->dnd.send_event = FALSE;
2349           tmp_event->dnd.context = g_object_ref (src_context);
2350     gdk_event_set_device (tmp_event, gdk_drag_context_get_device (src_context));
2351
2352           GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
2353           gdk_event_put (tmp_event);
2354     gdk_event_free (tmp_event);
2355         }
2356     }
2357   else
2358     {
2359       gdk_drag_do_leave (context, time);
2360
2361       if (success)
2362         _dnd_target_state = GDK_WIN32_DND_DROPPED;
2363       else
2364         _dnd_target_state = GDK_WIN32_DND_FAILED;
2365     }
2366 }
2367
2368 #if 0
2369
2370 static GdkFilterReturn
2371 gdk_destroy_filter (GdkXEvent *xev,
2372                     GdkEvent  *event,
2373                     gpointer   data)
2374 {
2375   MSG *msg = (MSG *) xev;
2376
2377   if (msg->message == WM_DESTROY)
2378     {
2379       IDropTarget *idtp = (IDropTarget *) data;
2380
2381       GDK_NOTE (DND, g_print ("gdk_destroy_filter: WM_DESTROY: %p\n", msg->hwnd));
2382 #if 0
2383       idtp->lpVtbl->Release (idtp);
2384 #endif
2385       RevokeDragDrop (msg->hwnd);
2386       CoLockObjectExternal ((IUnknown*) idtp, FALSE, TRUE);
2387     }
2388   return GDK_FILTER_CONTINUE;
2389 }
2390
2391 #endif
2392
2393 void
2394 gdk_window_register_dnd (GdkWindow *window)
2395 {
2396   target_drag_context *ctx;
2397   HRESULT hr;
2398
2399   g_return_if_fail (window != NULL);
2400
2401   if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
2402     return;
2403   else
2404     g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
2405
2406   GDK_NOTE (DND, g_print ("gdk_window_register_dnd: %p\n", GDK_WINDOW_HWND (window)));
2407
2408   if (!use_ole2_dnd)
2409     {
2410       /* We always claim to accept dropped files, but in fact we might not,
2411        * of course. This function is called in such a way that it cannot know
2412        * whether the window (widget) in question actually accepts files
2413        * (in gtk, data of type text/uri-list) or not.
2414        */
2415       gdk_window_add_filter (window, gdk_dropfiles_filter, NULL);
2416       DragAcceptFiles (GDK_WINDOW_HWND (window), TRUE);
2417     }
2418   else
2419     {
2420       /* Return if window is already setup for DND. */
2421       if (g_hash_table_lookup (target_ctx_for_window, GDK_WINDOW_HWND (window)) != NULL)
2422         return;
2423
2424       /* Register for OLE2 d&d : similarly, claim to accept all supported
2425        * data types because we cannot know from here what the window
2426        * actually accepts.
2427        */
2428       /* FIXME: This of course won't work with user-extensible data types! */
2429       ctx = target_context_new (window);
2430
2431       hr = CoLockObjectExternal ((IUnknown *) &ctx->idt, TRUE, FALSE);
2432       if (!SUCCEEDED (hr))
2433         OTHER_API_FAILED ("CoLockObjectExternal");
2434       else
2435         {
2436           hr = RegisterDragDrop (GDK_WINDOW_HWND (window), &ctx->idt);
2437           if (hr == DRAGDROP_E_ALREADYREGISTERED)
2438             {
2439               g_print ("DRAGDROP_E_ALREADYREGISTERED\n");
2440               CoLockObjectExternal ((IUnknown *) &ctx->idt, FALSE, FALSE);
2441             }
2442           else if (!SUCCEEDED (hr))
2443             OTHER_API_FAILED ("RegisterDragDrop");
2444           else
2445             {
2446               g_object_ref (window);
2447               g_hash_table_insert (target_ctx_for_window, GDK_WINDOW_HWND (window), ctx);
2448             }
2449         }
2450     }
2451 }
2452
2453 GdkAtom
2454 gdk_drag_get_selection (GdkDragContext *context)
2455 {
2456   switch (context->protocol)
2457     {
2458     case GDK_DRAG_PROTO_LOCAL:
2459       return _local_dnd;
2460     case GDK_DRAG_PROTO_WIN32_DROPFILES:
2461       return _gdk_win32_dropfiles;
2462     case GDK_DRAG_PROTO_OLE2:
2463       return _gdk_ole2_dnd;
2464     default:
2465       return GDK_NONE;
2466     }
2467 }
2468
2469 gboolean
2470 gdk_drag_drop_succeeded (GdkDragContext *context)
2471 {
2472   GdkDragContextPrivateWin32 *private;
2473
2474   g_return_val_if_fail (context != NULL, FALSE);
2475
2476   private = PRIVATE_DATA (context);
2477
2478   /* FIXME: Can we set drop_failed when the drop has failed? */
2479   return !private->drop_failed;
2480 }