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