1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 1998-2002 Tor Lillqvist
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 /* #define OLE2_DND */
36 #include "gdkproperty.h"
37 #include "gdkinternals.h"
38 #include "gdkprivate-win32.h"
51 typedef struct _GdkDragContextPrivateWin32 GdkDragContextPrivateWin32;
55 GDK_DRAG_STATUS_MOTION_WAIT,
56 GDK_DRAG_STATUS_ACTION_WAIT,
67 #define PRINT_GUID(guid) \
68 g_print ("guid = %.08lx-%.04x-%.04x-%.02x%.02x-%.02x%.02x%.02x%.02x%.02x%.02x", \
69 ((gulong *) guid)[0], \
70 ((gushort *) guid)[2], \
71 ((gushort *) guid)[3], \
72 ((guchar *) guid)[8], \
73 ((guchar *) guid)[9], \
74 ((guchar *) guid)[10], \
75 ((guchar *) guid)[11], \
76 ((guchar *) guid)[12], \
77 ((guchar *) guid)[13], \
78 ((guchar *) guid)[14], \
79 ((guchar *) guid)[15]);
82 static FORMATETC *formats;
87 /* Structure that holds information about a drag in progress.
88 * this is used on both source and destination sides.
90 struct _GdkDragContextPrivateWin32 {
94 guint16 last_x; /* Coordinates from last event */
97 guint drag_status : 4; /* Current status of drag */
98 guint drop_failed : 1; /* Whether the drop was unsuccessful */
101 #define GDK_DRAG_CONTEXT_PRIVATE_DATA(context) ((GdkDragContextPrivateWin32 *) GDK_DRAG_CONTEXT (context)->windowing_data)
103 static GdkDragContext *current_dest_drag = NULL;
105 static void gdk_drag_context_init (GdkDragContext *dragcontext);
106 static void gdk_drag_context_class_init (GdkDragContextClass *klass);
107 static void gdk_drag_context_finalize (GObject *object);
109 static gpointer parent_class = NULL;
110 static GList *contexts;
113 gdk_drag_context_get_type (void)
115 static GType object_type = 0;
119 static const GTypeInfo object_info =
121 sizeof (GdkDragContextClass),
122 (GBaseInitFunc) NULL,
123 (GBaseFinalizeFunc) NULL,
124 (GClassInitFunc) gdk_drag_context_class_init,
125 NULL, /* class_finalize */
126 NULL, /* class_data */
127 sizeof (GdkDragContext),
129 (GInstanceInitFunc) gdk_drag_context_init,
132 object_type = g_type_register_static (G_TYPE_OBJECT,
141 gdk_drag_context_init (GdkDragContext *dragcontext)
143 GdkDragContextPrivateWin32 *private = g_new0 (GdkDragContextPrivateWin32, 1);
145 dragcontext->windowing_data = private;
147 private->ref_count = 1;
150 contexts = g_list_prepend (contexts, dragcontext);
154 gdk_drag_context_class_init (GdkDragContextClass *klass)
156 GObjectClass *object_class = G_OBJECT_CLASS (klass);
158 parent_class = g_type_class_peek_parent (klass);
160 object_class->finalize = gdk_drag_context_finalize;
164 gdk_drag_context_finalize (GObject *object)
166 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
167 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
169 GDK_NOTE (DND, g_print ("gdk_drag_context_finalize\n"));
171 g_list_free (context->targets);
173 if (context->source_window)
175 g_object_unref (context->source_window);
178 if (context->dest_window)
179 g_object_unref (context->dest_window);
181 contexts = g_list_remove (contexts, context);
183 if (context == current_dest_drag)
184 current_dest_drag = NULL;
188 G_OBJECT_CLASS (parent_class)->finalize (object);
194 gdk_drag_context_new (void)
196 return g_object_new (gdk_drag_context_get_type (), NULL);
200 gdk_drag_context_ref (GdkDragContext *context)
202 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
204 g_object_ref (context);
208 gdk_drag_context_unref (GdkDragContext *context)
210 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
212 g_object_unref (context);
215 static GdkDragContext *
216 gdk_drag_context_find (gboolean is_source,
220 GList *tmp_list = contexts;
221 GdkDragContext *context;
222 GdkDragContextPrivateWin32 *private;
226 context = (GdkDragContext *)tmp_list->data;
227 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
229 if ((!context->is_source == !is_source) &&
230 ((source == NULL) || (context->source_window && (context->source_window == source))) &&
231 ((dest == NULL) || (context->dest_window && (context->dest_window == dest))))
234 tmp_list = tmp_list->next;
245 GdkDragContext *context;
246 } target_drag_context;
252 GdkDragContext *context;
253 } source_drag_context;
268 static enum_formats *enum_formats_new (void);
270 static ULONG STDMETHODCALLTYPE
271 idroptarget_addref (LPDROPTARGET This)
273 target_drag_context *ctx = (target_drag_context *) This;
274 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (ctx->context);
275 int ref_count = ++private->ref_count;
277 gdk_drag_context_ref (ctx->context);
278 GDK_NOTE (DND, g_print ("idroptarget_addref %p %d\n", This, ref_count));
283 static HRESULT STDMETHODCALLTYPE
284 idroptarget_queryinterface (LPDROPTARGET This,
288 GDK_NOTE (DND, g_print ("idroptarget_queryinterface %p\n", This));
294 if (IsEqualGUID (riid, &IID_IUnknown))
296 g_print ("...IUnknown\n");
297 idroptarget_addref (This);
301 else if (IsEqualGUID (riid, &IID_IDropTarget))
303 g_print ("...IDropTarget\n");
304 idroptarget_addref (This);
310 g_print ("...Huh?\n");
311 return E_NOINTERFACE;
315 static ULONG STDMETHODCALLTYPE
316 idroptarget_release (LPDROPTARGET This)
318 target_drag_context *ctx = (target_drag_context *) This;
319 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (ctx->context);
320 int ref_count = --private->ref_count;
322 gdk_drag_context_unref (ctx->context);
323 GDK_NOTE (DND, g_print ("idroptarget_release %p %d\n", This, ref_count));
331 static HRESULT STDMETHODCALLTYPE
332 idroptarget_dragenter (LPDROPTARGET This,
333 LPDATAOBJECT pDataObj,
338 GDK_NOTE (DND, g_print ("idroptarget_dragenter %p\n", This));
343 static HRESULT STDMETHODCALLTYPE
344 idroptarget_dragover (LPDROPTARGET This,
349 GDK_NOTE (DND, g_print ("idroptarget_dragover %p\n", This));
354 static HRESULT STDMETHODCALLTYPE
355 idroptarget_dragleave (LPDROPTARGET This)
357 GDK_NOTE (DND, g_print ("idroptarget_dragleave %p\n", This));
362 static HRESULT STDMETHODCALLTYPE
363 idroptarget_drop (LPDROPTARGET This,
364 LPDATAOBJECT pDataObj,
369 GDK_NOTE (DND, g_print ("idroptarget_drop %p\n", This));
374 static ULONG STDMETHODCALLTYPE
375 idropsource_addref (LPDROPSOURCE This)
377 source_drag_context *ctx = (source_drag_context *) This;
378 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (ctx->context);
380 gdk_drag_context_ref (ctx->context);
381 GDK_NOTE (DND, g_print ("idropsource_addref %p %d\n",
382 This, private->ref_count));
384 return private->ref_count;
387 static HRESULT STDMETHODCALLTYPE
388 idropsource_queryinterface (LPDROPSOURCE This,
392 GDK_NOTE (DND, g_print ("idropsource_queryinterface %p\n", This));
397 if (IsEqualGUID (riid, &IID_IUnknown))
399 g_print ("...IUnknown\n");
400 idropsource_addref (This);
404 else if (IsEqualGUID (riid, &IID_IDropSource))
406 g_print ("...IDropSource\n");
407 idropsource_addref (This);
413 g_print ("...Huh?\n");
414 return E_NOINTERFACE;
418 static ULONG STDMETHODCALLTYPE
419 idropsource_release (LPDROPSOURCE This)
421 source_drag_context *ctx = (source_drag_context *) This;
422 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (ctx->context);
423 int ref_count = --private->ref_count;
425 gdk_drag_context_unref (ctx->context);
426 GDK_NOTE (DND, g_print ("idropsource_release %p %d\n", This, ref_count));
434 static HRESULT STDMETHODCALLTYPE
435 idropsource_querycontinuedrag (LPDROPSOURCE This,
439 GDK_NOTE (DND, g_print ("idropsource_querycontinuedrag %p\n", This));
444 static HRESULT STDMETHODCALLTYPE
445 idropsource_givefeedback (LPDROPSOURCE This,
448 GDK_NOTE (DND, g_print ("idropsource_givefeedback %p\n", This));
453 static ULONG STDMETHODCALLTYPE
454 idataobject_addref (LPDATAOBJECT This)
456 data_object *dobj = (data_object *) This;
457 int ref_count = ++dobj->ref_count;
459 GDK_NOTE (DND, g_print ("idataobject_addref %p %d\n", This, ref_count));
464 static HRESULT STDMETHODCALLTYPE
465 idataobject_queryinterface (LPDATAOBJECT This,
469 GDK_NOTE (DND, g_print ("idataobject_queryinterface %p\n", This));
474 if (IsEqualGUID (riid, &IID_IUnknown))
476 g_print ("...IUnknown\n");
477 idataobject_addref (This);
481 else if (IsEqualGUID (riid, &IID_IDataObject))
483 g_print ("...IDataObject\n");
484 idataobject_addref (This);
490 g_print ("...Huh?\n");
491 return E_NOINTERFACE;
495 static ULONG STDMETHODCALLTYPE
496 idataobject_release (LPDATAOBJECT This)
498 data_object *dobj = (data_object *) This;
499 int ref_count = --dobj->ref_count;
501 GDK_NOTE (DND, g_print ("idataobject_release %p %d\n", This, ref_count));
509 static HRESULT STDMETHODCALLTYPE
510 idataobject_getdata (LPDATAOBJECT This,
511 LPFORMATETC pFormatEtc,
514 GDK_NOTE (DND, g_print ("idataobject_getdata %p\n", This));
519 static HRESULT STDMETHODCALLTYPE
520 idataobject_getdatahere (LPDATAOBJECT This,
521 LPFORMATETC pFormatEtc,
524 GDK_NOTE (DND, g_print ("idataobject_getdatahere %p\n", This));
529 static HRESULT STDMETHODCALLTYPE
530 idataobject_querygetdata (LPDATAOBJECT This,
531 LPFORMATETC pFormatEtc)
535 GDK_NOTE (DND, g_print ("idataobject_querygetdata %p %#x", This, pFormatEtc->cfFormat));
537 for (i = 0; i < nformats; i++)
538 if (pFormatEtc->cfFormat == formats[i].cfFormat)
540 GDK_NOTE (DND, g_print (" S_OK\n"));
544 GDK_NOTE (DND, g_print (" DV_E_FORMATETC\n"));
545 return DV_E_FORMATETC;
548 static HRESULT STDMETHODCALLTYPE
549 idataobject_getcanonicalformatetc (LPDATAOBJECT This,
550 LPFORMATETC pFormatEtcIn,
551 LPFORMATETC pFormatEtcOut)
553 GDK_NOTE (DND, g_print ("idataobject_getcanonicalformatetc %p\n", This));
558 static HRESULT STDMETHODCALLTYPE
559 idataobject_setdata (LPDATAOBJECT This,
560 LPFORMATETC pFormatEtc,
564 GDK_NOTE (DND, g_print ("idataobject_setdata %p\n", This));
569 static HRESULT STDMETHODCALLTYPE
570 idataobject_enumformatetc (LPDATAOBJECT This,
572 LPENUMFORMATETC *ppEnumFormatEtc)
574 GDK_NOTE (DND, g_print ("idataobject_enumformatetc %p\n", This));
576 if (dwDirection != DATADIR_GET)
579 *ppEnumFormatEtc = &enum_formats_new ()->ief;
583 static HRESULT STDMETHODCALLTYPE
584 idataobject_dadvise (LPDATAOBJECT This,
585 LPFORMATETC pFormatetc,
587 LPADVISESINK pAdvSink,
588 DWORD *pdwConnection)
590 GDK_NOTE (DND, g_print ("idataobject_dadvise %p\n", This));
595 static HRESULT STDMETHODCALLTYPE
596 idataobject_dunadvise (LPDATAOBJECT This,
599 GDK_NOTE (DND, g_print ("idataobject_dunadvise %p\n", This));
604 static HRESULT STDMETHODCALLTYPE
605 idataobject_enumdadvise (LPDATAOBJECT This,
606 LPENUMSTATDATA *ppenumAdvise)
608 GDK_NOTE (DND, g_print ("idataobject_enumdadvise %p\n", This));
613 static ULONG STDMETHODCALLTYPE
614 ienumformatetc_addref (LPENUMFORMATETC This)
616 enum_formats *en = (enum_formats *) This;
617 int ref_count = ++en->ref_count;
619 GDK_NOTE (DND, g_print ("ienumformatetc_addref %p %d\n", This, ref_count));
624 static HRESULT STDMETHODCALLTYPE
625 ienumformatetc_queryinterface (LPENUMFORMATETC This,
629 GDK_NOTE (DND, g_print ("ienumformatetc_queryinterface %p\n", This));
634 if (IsEqualGUID (riid, &IID_IUnknown))
636 g_print ("...IUnknown\n");
637 ienumformatetc_addref (This);
641 else if (IsEqualGUID (riid, &IID_IEnumFORMATETC))
643 g_print ("...IEnumFORMATETC\n");
644 ienumformatetc_addref (This);
650 g_print ("...Huh?\n");
651 return E_NOINTERFACE;
655 static ULONG STDMETHODCALLTYPE
656 ienumformatetc_release (LPENUMFORMATETC This)
658 enum_formats *en = (enum_formats *) This;
659 int ref_count = --en->ref_count;
661 GDK_NOTE (DND, g_print ("ienumformatetc_release %p %d\n", This, ref_count));
669 static HRESULT STDMETHODCALLTYPE
670 ienumformatetc_next (LPENUMFORMATETC This,
675 enum_formats *en = (enum_formats *) This;
678 GDK_NOTE (DND, g_print ("ienumformatetc_next %p %d %ld\n", This, en->ix, celt));
681 for (i = 0; i < celt; i++)
683 if (en->ix >= nformats)
685 elts[i] = formats[en->ix++];
698 static HRESULT STDMETHODCALLTYPE
699 ienumformatetc_skip (LPENUMFORMATETC This,
702 enum_formats *en = (enum_formats *) This;
704 GDK_NOTE (DND, g_print ("ienumformatetc_skip %p %d %ld\n", This, en->ix, celt));
710 static HRESULT STDMETHODCALLTYPE
711 ienumformatetc_reset (LPENUMFORMATETC This)
713 enum_formats *en = (enum_formats *) This;
715 GDK_NOTE (DND, g_print ("ienumformatetc_reset %p\n", This));
722 static HRESULT STDMETHODCALLTYPE
723 ienumformatetc_clone (LPENUMFORMATETC This,
724 LPENUMFORMATETC *ppEnumFormatEtc)
726 enum_formats *en = (enum_formats *) This;
729 GDK_NOTE (DND, g_print ("ienumformatetc_clone %p\n", This));
731 new = enum_formats_new ();
735 *ppEnumFormatEtc = &new->ief;
740 static IDropTargetVtbl idt_vtbl = {
741 idroptarget_queryinterface,
744 idroptarget_dragenter,
745 idroptarget_dragover,
746 idroptarget_dragleave,
750 static IDropSourceVtbl ids_vtbl = {
751 idropsource_queryinterface,
754 idropsource_querycontinuedrag,
755 idropsource_givefeedback
758 static IDataObjectVtbl ido_vtbl = {
759 idataobject_queryinterface,
763 idataobject_getdatahere,
764 idataobject_querygetdata,
765 idataobject_getcanonicalformatetc,
767 idataobject_enumformatetc,
769 idataobject_dunadvise,
770 idataobject_enumdadvise
773 static IEnumFORMATETCVtbl ief_vtbl = {
774 ienumformatetc_queryinterface,
775 ienumformatetc_addref,
776 ienumformatetc_release,
779 ienumformatetc_reset,
784 static target_drag_context *
785 target_context_new (void)
787 target_drag_context *result;
789 result = g_new0 (target_drag_context, 1);
791 result->idt.lpVtbl = &idt_vtbl;
793 result->context = gdk_drag_context_new ();
794 result->context->is_source = FALSE;
796 GDK_NOTE (DND, g_print ("target_context_new: %p\n", result));
801 static source_drag_context *
802 source_context_new (void)
804 source_drag_context *result;
806 result = g_new0 (source_drag_context, 1);
808 result->ids.lpVtbl = &ids_vtbl;
810 result->context = gdk_drag_context_new ();
811 result->context->is_source = TRUE;
813 GDK_NOTE (DND, g_print ("source_context_new: %p\n", result));
819 data_object_new (void)
823 result = g_new0 (data_object, 1);
825 result->ido.lpVtbl = &ido_vtbl;
826 result->ref_count = 1;
828 GDK_NOTE (DND, g_print ("data_object_new: %p\n", result));
834 static enum_formats *
835 enum_formats_new (void)
837 enum_formats *result;
839 result = g_new0 (enum_formats, 1);
841 result->ief.lpVtbl = &ief_vtbl;
842 result->ref_count = 1;
845 GDK_NOTE (DND, g_print ("enum_formats_new: %p\n", result));
852 /* From MS Knowledge Base article Q130698 */
855 resolve_link (HWND hWnd,
856 guchar *lpszLinkName,
860 IShellLinkA *pslA = NULL;
861 IShellLinkW *pslW = NULL;
862 IPersistFile *ppf = NULL;
864 /* Assume failure to start with: */
867 /* Call CoCreateInstance to obtain the IShellLink interface
868 * pointer. This call fails if CoInitialize is not called, so it is
869 * assumed that CoInitialize has been called.
872 if (G_WIN32_HAVE_WIDECHAR_API ())
873 hres = CoCreateInstance (&CLSID_ShellLink,
875 CLSCTX_INPROC_SERVER,
879 hres = CoCreateInstance (&CLSID_ShellLink,
881 CLSCTX_INPROC_SERVER,
885 if (SUCCEEDED (hres))
888 /* The IShellLink interface supports the IPersistFile
889 * interface. Get an interface pointer to it.
891 if (G_WIN32_HAVE_WIDECHAR_API ())
892 hres = pslW->lpVtbl->QueryInterface (pslW,
896 hres = pslA->lpVtbl->QueryInterface (pslA,
901 if (SUCCEEDED (hres))
903 /* Convert the given link name string to wide character string. */
904 wchar_t *wsz = g_utf8_to_utf16 (lpszLinkName, -1, NULL, NULL, NULL);
907 hres = ppf->lpVtbl->Load (ppf, wsz, STGM_READ);
911 if (SUCCEEDED (hres))
913 /* Resolve the link by calling the Resolve()
914 * interface function.
916 if (G_WIN32_HAVE_WIDECHAR_API ())
917 hres = pslW->lpVtbl->Resolve (pslW, hWnd, SLR_ANY_MATCH | SLR_NO_UI);
919 hres = pslA->lpVtbl->Resolve (pslA, hWnd, SLR_ANY_MATCH | SLR_NO_UI);
922 if (SUCCEEDED (hres))
924 if (G_WIN32_HAVE_WIDECHAR_API ())
926 wchar_t wtarget[MAX_PATH];
928 hres = pslW->lpVtbl->GetPath (pslW, wtarget, MAX_PATH, NULL, 0);
929 if (SUCCEEDED (hres))
930 *lpszPath = g_utf16_to_utf8 (wtarget, -1, NULL, NULL, NULL);
934 guchar cptarget[MAX_PATH];
936 hres = pslA->lpVtbl->GetPath (pslA, cptarget, MAX_PATH, NULL, 0);
937 if (SUCCEEDED (hres))
938 *lpszPath = g_locale_to_utf8 (cptarget, -1, NULL, NULL, NULL);
943 ppf->lpVtbl->Release (ppf);
945 pslW->lpVtbl->Release (pslW);
947 pslA->lpVtbl->Release (pslA);
949 return SUCCEEDED (hres);
952 static GdkFilterReturn
953 gdk_dropfiles_filter (GdkXEvent *xev,
957 GdkDragContext *context;
958 GdkDragContextPrivateWin32 *private;
960 MSG *msg = (MSG *) xev;
964 guchar *fileName, *linkedFile;
966 if (msg->message == WM_DROPFILES)
968 GDK_NOTE (DND, g_print ("WM_DROPFILES: %p\n", msg->hwnd));
970 context = gdk_drag_context_new ();
971 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
972 context->protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
973 context->is_source = FALSE;
974 context->source_window = _gdk_parent_root;
975 g_object_ref (context->source_window);
976 context->dest_window = event->any.window;
977 g_object_ref (context->dest_window);
978 /* WM_DROPFILES drops are always file names */
980 g_list_append (NULL, GUINT_TO_POINTER (_text_uri_list));
981 current_dest_drag = context;
983 event->dnd.type = GDK_DROP_START;
984 event->dnd.context = current_dest_drag;
986 hdrop = (HANDLE) msg->wParam;
987 DragQueryPoint (hdrop, &pt);
988 ClientToScreen (msg->hwnd, &pt);
990 event->dnd.x_root = pt.x;
991 event->dnd.y_root = pt.y;
992 event->dnd.time = _gdk_win32_get_next_tick (msg->time);
994 nfiles = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
996 result = g_string_new (NULL);
997 for (i = 0; i < nfiles; i++)
1001 if (G_WIN32_HAVE_WIDECHAR_API ())
1003 wchar_t wfn[MAX_PATH];
1005 DragQueryFileW (hdrop, i, wfn, MAX_PATH);
1006 fileName = g_utf16_to_utf8 (wfn, -1, NULL, NULL, NULL);
1010 char cpfn[MAX_PATH];
1012 DragQueryFileA (hdrop, i, cpfn, MAX_PATH);
1013 fileName = g_locale_to_utf8 (cpfn, -1, NULL, NULL, NULL);
1016 /* Resolve shortcuts */
1017 if (resolve_link (msg->hwnd, fileName, &linkedFile))
1019 uri = g_filename_to_uri (linkedFile, NULL, NULL);
1020 g_free (linkedFile);
1023 g_string_append (result, uri);
1024 GDK_NOTE (DND, g_print ("... %s link to %s: %s\n",
1025 fileName, linkedFile, uri));
1031 uri = g_filename_to_uri (fileName, NULL, NULL);
1034 g_string_append (result, uri);
1035 GDK_NOTE (DND, g_print ("... %s: %s\n", fileName, uri));
1040 g_string_append (result, "\015\012");
1042 _gdk_dropfiles_store (result->str);
1043 g_string_free (result, FALSE);
1047 return GDK_FILTER_TRANSLATE;
1050 return GDK_FILTER_CONTINUE;
1053 /*************************************************************
1054 ************************** Public API ***********************
1055 *************************************************************/
1058 _gdk_dnd_init (void)
1062 hres = OleInitialize (NULL);
1064 if (! SUCCEEDED (hres))
1065 g_error ("OleInitialize failed");
1068 formats = g_new (FORMATETC, nformats);
1070 formats[0].cfFormat = CF_TEXT;
1071 formats[0].ptd = NULL;
1072 formats[0].dwAspect = DVASPECT_CONTENT;
1073 formats[0].lindex = -1;
1074 formats[0].tymed = TYMED_HGLOBAL;
1076 formats[1].cfFormat = CF_GDIOBJFIRST;
1077 formats[1].ptd = NULL;
1078 formats[1].dwAspect = DVASPECT_CONTENT;
1079 formats[1].lindex = -1;
1080 formats[1].tymed = TYMED_HGLOBAL;
1085 _gdk_win32_dnd_exit (void)
1095 local_send_leave (GdkDragContext *context,
1100 if ((current_dest_drag != NULL) &&
1101 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1102 (current_dest_drag->source_window == context->source_window))
1104 tmp_event.dnd.type = GDK_DRAG_LEAVE;
1105 tmp_event.dnd.window = context->dest_window;
1106 /* Pass ownership of context to the event */
1107 tmp_event.dnd.send_event = FALSE;
1108 tmp_event.dnd.context = current_dest_drag;
1109 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1111 current_dest_drag = NULL;
1113 gdk_event_put (&tmp_event);
1118 local_send_enter (GdkDragContext *context,
1122 GdkDragContextPrivateWin32 *private;
1123 GdkDragContext *new_context;
1125 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1127 if (current_dest_drag != NULL)
1129 gdk_drag_context_unref (current_dest_drag);
1130 current_dest_drag = NULL;
1133 new_context = gdk_drag_context_new ();
1134 new_context->protocol = GDK_DRAG_PROTO_LOCAL;
1135 new_context->is_source = FALSE;
1137 new_context->source_window = context->source_window;
1138 g_object_ref (new_context->source_window);
1139 new_context->dest_window = context->dest_window;
1140 g_object_ref (new_context->dest_window);
1142 new_context->targets = g_list_copy (context->targets);
1144 gdk_window_set_events (new_context->source_window,
1145 gdk_window_get_events (new_context->source_window) |
1146 GDK_PROPERTY_CHANGE_MASK);
1147 new_context->actions = context->actions;
1149 tmp_event.dnd.type = GDK_DRAG_ENTER;
1150 tmp_event.dnd.window = context->dest_window;
1151 tmp_event.dnd.send_event = FALSE;
1152 tmp_event.dnd.context = new_context;
1153 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1155 current_dest_drag = new_context;
1157 gdk_event_put (&tmp_event);
1161 local_send_motion (GdkDragContext *context,
1164 GdkDragAction action,
1169 if ((current_dest_drag != NULL) &&
1170 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1171 (current_dest_drag->source_window == context->source_window))
1173 tmp_event.dnd.type = GDK_DRAG_MOTION;
1174 tmp_event.dnd.window = current_dest_drag->dest_window;
1175 tmp_event.dnd.send_event = FALSE;
1176 tmp_event.dnd.context = current_dest_drag;
1177 tmp_event.dnd.time = time;
1179 current_dest_drag->suggested_action = action;
1180 current_dest_drag->actions = current_dest_drag->suggested_action;
1182 tmp_event.dnd.x_root = x_root;
1183 tmp_event.dnd.y_root = y_root;
1185 (GDK_DRAG_CONTEXT_PRIVATE_DATA (current_dest_drag))->last_x = x_root;
1186 (GDK_DRAG_CONTEXT_PRIVATE_DATA (current_dest_drag))->last_y = y_root;
1188 GDK_DRAG_CONTEXT_PRIVATE_DATA (context)->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1190 gdk_event_put (&tmp_event);
1195 local_send_drop (GdkDragContext *context,
1200 if ((current_dest_drag != NULL) &&
1201 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1202 (current_dest_drag->source_window == context->source_window))
1204 GdkDragContextPrivateWin32 *private;
1205 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (current_dest_drag);
1207 /* Pass ownership of context to the event */
1208 tmp_event.dnd.type = GDK_DROP_START;
1209 tmp_event.dnd.window = current_dest_drag->dest_window;
1210 tmp_event.dnd.send_event = FALSE;
1211 tmp_event.dnd.context = current_dest_drag;
1212 tmp_event.dnd.time = GDK_CURRENT_TIME;
1214 tmp_event.dnd.x_root = private->last_x;
1215 tmp_event.dnd.y_root = private->last_y;
1217 current_dest_drag = NULL;
1219 gdk_event_put (&tmp_event);
1225 gdk_drag_do_leave (GdkDragContext *context,
1228 if (context->dest_window)
1230 GDK_NOTE (DND, g_print ("gdk_drag_do_leave\n"));
1232 switch (context->protocol)
1234 case GDK_DRAG_PROTO_LOCAL:
1235 local_send_leave (context, time);
1241 g_object_unref (context->dest_window);
1242 context->dest_window = NULL;
1247 gdk_drag_begin (GdkWindow *window,
1252 GdkDragContext *new_context;
1254 g_return_val_if_fail (window != NULL, NULL);
1256 new_context = gdk_drag_context_new ();
1257 new_context->is_source = TRUE;
1258 new_context->source_window = window;
1259 g_object_ref (window);
1261 tmp_list = g_list_last (targets);
1262 new_context->targets = NULL;
1265 new_context->targets = g_list_prepend (new_context->targets,
1267 tmp_list = tmp_list->prev;
1270 new_context->actions = 0;
1274 source_drag_context *ctx;
1282 g_return_val_if_fail (window != NULL, NULL);
1284 GDK_NOTE (DND, g_print ("gdk_drag_begin\n"));
1286 ctx = source_context_new ();
1287 ctx->context->protocol = GDK_DRAG_PROTO_OLE2;
1288 ctx->context->source_window = window;
1289 g_object_ref (window);
1291 tmp_list = g_list_last (targets);
1292 ctx->context->targets = NULL;
1295 ctx->context->targets = g_list_prepend (ctx->context->targets,
1297 tmp_list = tmp_list->prev;
1300 ctx->context->actions = 0;
1302 dobj = data_object_new ();
1304 global = GlobalAlloc (GMEM_FIXED, sizeof (ctx));
1306 memcpy (&global, ctx, sizeof (ctx));
1308 medium.tymed = TYMED_HGLOBAL;
1309 medium.hGlobal = global;
1310 medium.pUnkForRelease = NULL;
1312 dobj->ido.lpVtbl->SetData (&dobj->ido, &formats[1], &medium, TRUE);
1314 hResult = DoDragDrop (&dobj->ido, &ctx->ids, DROPEFFECT_MOVE|DROPEFFECT_COPY, &dwEffect);
1316 GDK_NOTE (DND, g_print ("DoDragDrop returned %s\n",
1317 (hResult == DRAGDROP_S_DROP ? "DRAGDROP_S_DROP" :
1318 (hResult == DRAGDROP_S_CANCEL ? "DRAGDROP_S_CANCEL" :
1319 (hResult == E_UNEXPECTED ? "E_UNEXPECTED" :
1320 g_strdup_printf ("%#.8lx", hResult))))));
1322 dobj->ido.lpVtbl->Release (&dobj->ido);
1323 ctx->ids.lpVtbl->Release (&ctx->ids);
1325 return ctx->context;
1330 gdk_drag_get_protocol_for_display (GdkDisplay *display,
1332 GdkDragProtocol *protocol)
1336 GDK_NOTE (DND, g_print ("gdk_drag_get_protocol\n"));
1338 window = gdk_window_lookup (xid);
1340 if (GPOINTER_TO_INT (gdk_drawable_get_data (window, "gdk-dnd-registered")))
1342 *protocol = GDK_DRAG_PROTO_LOCAL;
1354 } find_window_enum_arg;
1356 static BOOL CALLBACK
1357 find_window_enum_proc (HWND hwnd,
1362 find_window_enum_arg *a = (find_window_enum_arg *) lparam;
1364 if (hwnd == a->ignore)
1367 if (!IsWindowVisible (hwnd))
1371 ClientToScreen (hwnd, &tl);
1372 GetClientRect (hwnd, &rect);
1375 ClientToScreen (hwnd, &br);
1377 if (a->x >= tl.x && a->y >= tl.y && a->x < br.x && a->y < br.y)
1387 gdk_drag_find_window_for_screen (GdkDragContext *context,
1388 GdkWindow *drag_window,
1392 GdkWindow **dest_window,
1393 GdkDragProtocol *protocol)
1395 find_window_enum_arg a;
1397 a.x = x_root - _gdk_offset_x;
1398 a.y = y_root - _gdk_offset_y;
1399 a.ignore = drag_window ? GDK_WINDOW_HWND (drag_window) : NULL;
1402 EnumWindows (find_window_enum_proc, (LPARAM) &a);
1404 if (a.result == NULL)
1405 *dest_window = NULL;
1408 *dest_window = gdk_win32_handle_table_lookup (GPOINTER_TO_UINT (a.result));
1411 *dest_window = gdk_window_get_toplevel (*dest_window);
1412 g_object_ref (*dest_window);
1415 if (context->source_window)
1416 *protocol = GDK_DRAG_PROTO_LOCAL;
1418 *protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
1422 g_print ("gdk_drag_find_window: %p %+d%+d: %p: %p %d\n",
1423 (drag_window ? GDK_WINDOW_HWND (drag_window) : NULL),
1426 (*dest_window ? GDK_WINDOW_HWND (*dest_window) : NULL),
1431 gdk_drag_motion (GdkDragContext *context,
1432 GdkWindow *dest_window,
1433 GdkDragProtocol protocol,
1436 GdkDragAction suggested_action,
1437 GdkDragAction possible_actions,
1440 GdkDragContextPrivateWin32 *private;
1442 g_return_val_if_fail (context != NULL, FALSE);
1444 GDK_NOTE (DND, g_print ("gdk_drag_motion\n"));
1446 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1448 if (context->dest_window != dest_window)
1450 GdkEvent temp_event;
1452 /* Send a leave to the last destination */
1453 gdk_drag_do_leave (context, time);
1454 private->drag_status = GDK_DRAG_STATUS_DRAG;
1456 /* Check if new destination accepts drags, and which protocol */
1459 context->dest_window = dest_window;
1460 g_object_ref (context->dest_window);
1461 context->protocol = protocol;
1465 case GDK_DRAG_PROTO_LOCAL:
1466 local_send_enter (context, time);
1472 context->suggested_action = suggested_action;
1476 context->dest_window = NULL;
1477 context->action = 0;
1480 /* Push a status event, to let the client know that
1484 temp_event.dnd.type = GDK_DRAG_STATUS;
1485 temp_event.dnd.window = context->source_window;
1486 /* We use this to signal a synthetic status. Perhaps
1487 * we should use an extra field...
1489 temp_event.dnd.send_event = TRUE;
1491 temp_event.dnd.context = context;
1492 temp_event.dnd.time = time;
1494 gdk_event_put (&temp_event);
1498 context->suggested_action = suggested_action;
1501 /* Send a drag-motion event */
1503 private->last_x = x_root;
1504 private->last_y = y_root;
1506 if (context->dest_window)
1508 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
1510 switch (context->protocol)
1512 case GDK_DRAG_PROTO_LOCAL:
1513 local_send_motion (context, x_root, y_root, suggested_action, time);
1516 case GDK_DRAG_PROTO_NONE:
1517 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
1532 gdk_drag_drop (GdkDragContext *context,
1535 g_return_if_fail (context != NULL);
1537 GDK_NOTE (DND, g_print ("gdk_drag_drop\n"));
1539 if (context->dest_window)
1541 switch (context->protocol)
1543 case GDK_DRAG_PROTO_LOCAL:
1544 local_send_drop (context, time);
1547 case GDK_DRAG_PROTO_NONE:
1548 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
1558 gdk_drag_abort (GdkDragContext *context,
1561 g_return_if_fail (context != NULL);
1563 GDK_NOTE (DND, g_print ("gdk_drag_abort\n"));
1565 gdk_drag_do_leave (context, time);
1568 /* Destination side */
1571 gdk_drag_status (GdkDragContext *context,
1572 GdkDragAction action,
1575 GdkDragContextPrivateWin32 *private;
1576 GdkDragContext *src_context;
1579 g_return_if_fail (context != NULL);
1581 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1583 context->action = action;
1585 src_context = gdk_drag_context_find (TRUE,
1586 context->source_window,
1587 context->dest_window);
1591 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (src_context);
1593 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1594 private->drag_status = GDK_DRAG_STATUS_DRAG;
1596 tmp_event.dnd.type = GDK_DRAG_STATUS;
1597 tmp_event.dnd.window = context->source_window;
1598 tmp_event.dnd.send_event = FALSE;
1599 tmp_event.dnd.context = src_context;
1600 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1602 if (action == GDK_ACTION_DEFAULT)
1605 src_context->action = action;
1607 gdk_event_put (&tmp_event);
1612 gdk_drop_reply (GdkDragContext *context,
1616 g_return_if_fail (context != NULL);
1618 GDK_NOTE (DND, g_print ("gdk_drop_reply\n"));
1620 if (context->dest_window)
1622 switch (context->protocol)
1624 case GDK_DRAG_PROTO_WIN32_DROPFILES:
1625 _gdk_dropfiles_store (NULL);
1635 gdk_drop_finish (GdkDragContext *context,
1639 GdkDragContextPrivateWin32 *private;
1640 GdkDragContext *src_context;
1643 g_return_if_fail (context != NULL);
1645 GDK_NOTE (DND, g_print ("gdk_drop_finish"));
1647 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1649 src_context = gdk_drag_context_find (TRUE,
1650 context->source_window,
1651 context->dest_window);
1654 tmp_event.dnd.type = GDK_DROP_FINISHED;
1655 tmp_event.dnd.window = src_context->source_window;
1656 tmp_event.dnd.send_event = FALSE;
1657 tmp_event.dnd.context = src_context;
1659 gdk_event_put (&tmp_event);
1665 static GdkFilterReturn
1666 gdk_destroy_filter (GdkXEvent *xev,
1670 MSG *msg = (MSG *) xev;
1672 if (msg->message == WM_DESTROY)
1674 IDropTarget *idtp = (IDropTarget *) data;
1676 GDK_NOTE (DND, g_print ("gdk_destroy_filter: WM_DESTROY: %p\n", msg->hwnd));
1678 idtp->lpVtbl->Release (idtp);
1680 RevokeDragDrop (msg->hwnd);
1681 CoLockObjectExternal (idtp, FALSE, TRUE);
1683 return GDK_FILTER_CONTINUE;
1688 gdk_window_register_dnd (GdkWindow *window)
1691 target_drag_context *ctx;
1695 g_return_if_fail (window != NULL);
1697 if (GPOINTER_TO_INT (gdk_drawable_get_data (window, "gdk-dnd-registered")))
1700 gdk_drawable_set_data (window, "gdk-dnd-registered", GINT_TO_POINTER(TRUE), NULL);
1702 GDK_NOTE (DND, g_print ("gdk_window_register_dnd: %p\n",
1703 GDK_WINDOW_HWND (window)));
1705 /* We always claim to accept dropped files, but in fact we might not,
1706 * of course. This function is called in such a way that it cannot know
1707 * whether the window (widget) in question actually accepts files
1708 * (in gtk, data of type text/uri-list) or not.
1710 gdk_window_add_filter (window, gdk_dropfiles_filter, NULL);
1711 DragAcceptFiles (GDK_WINDOW_HWND (window), TRUE);
1714 /* Register for OLE2 d&d */
1715 ctx = target_context_new ();
1716 ctx->context->protocol = GDK_DRAG_PROTO_OLE2;
1717 hres = CoLockObjectExternal ((IUnknown *) &ctx->idt, TRUE, FALSE);
1718 if (!SUCCEEDED (hres))
1719 OTHER_API_FAILED ("CoLockObjectExternal");
1722 hres = RegisterDragDrop (GDK_WINDOW_HWND (window), &ctx->idt);
1723 if (hres == DRAGDROP_E_ALREADYREGISTERED)
1725 g_print ("DRAGDROP_E_ALREADYREGISTERED\n");
1727 ctx->idt.lpVtbl->Release (&ctx->idt);
1729 CoLockObjectExternal ((IUnknown *) &ctx->idt, FALSE, FALSE);
1731 else if (!SUCCEEDED (hres))
1732 OTHER_API_FAILED ("RegisterDragDrop");
1735 gdk_window_add_filter (window, gdk_destroy_filter, &ctx->idt);
1741 /*************************************************************
1742 * gdk_drag_get_selection:
1743 * Returns the selection atom for the current source window
1747 *************************************************************/
1750 gdk_drag_get_selection (GdkDragContext *context)
1752 if (context->protocol == GDK_DRAG_PROTO_LOCAL)
1754 else if (context->protocol == GDK_DRAG_PROTO_WIN32_DROPFILES)
1755 return _gdk_win32_dropfiles;
1756 else if (context->protocol == GDK_DRAG_PROTO_OLE2)
1757 return _gdk_ole2_dnd;
1763 gdk_drag_drop_succeeded (GdkDragContext *context)
1765 GdkDragContextPrivateWin32 *private;
1767 g_return_val_if_fail (context != NULL, FALSE);
1769 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1771 /* FIXME: Can we set drop_failed when the drop has failed? */
1772 return !private->drop_failed;