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_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 context->actions = GDK_ACTION_COPY;
982 context->suggested_action = GDK_ACTION_COPY;
983 current_dest_drag = context;
985 event->dnd.type = GDK_DROP_START;
986 event->dnd.context = current_dest_drag;
988 hdrop = (HANDLE) msg->wParam;
989 DragQueryPoint (hdrop, &pt);
990 ClientToScreen (msg->hwnd, &pt);
992 event->dnd.x_root = pt.x + _gdk_offset_x;
993 event->dnd.y_root = pt.y + _gdk_offset_y;
994 event->dnd.time = _gdk_win32_get_next_tick (msg->time);
996 nfiles = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
998 result = g_string_new (NULL);
999 for (i = 0; i < nfiles; i++)
1003 if (G_WIN32_HAVE_WIDECHAR_API ())
1005 wchar_t wfn[MAX_PATH];
1007 DragQueryFileW (hdrop, i, wfn, MAX_PATH);
1008 fileName = g_utf16_to_utf8 (wfn, -1, NULL, NULL, NULL);
1012 char cpfn[MAX_PATH];
1014 DragQueryFileA (hdrop, i, cpfn, MAX_PATH);
1015 fileName = g_locale_to_utf8 (cpfn, -1, NULL, NULL, NULL);
1018 /* Resolve shortcuts */
1019 if (resolve_link (msg->hwnd, fileName, &linkedFile))
1021 uri = g_filename_to_uri (linkedFile, NULL, NULL);
1022 g_free (linkedFile);
1025 g_string_append (result, uri);
1026 GDK_NOTE (DND, g_print ("... %s link to %s: %s\n",
1027 fileName, linkedFile, uri));
1033 uri = g_filename_to_uri (fileName, NULL, NULL);
1036 g_string_append (result, uri);
1037 GDK_NOTE (DND, g_print ("... %s: %s\n", fileName, uri));
1042 g_string_append (result, "\015\012");
1044 _gdk_dropfiles_store (result->str);
1045 g_string_free (result, FALSE);
1049 return GDK_FILTER_TRANSLATE;
1052 return GDK_FILTER_CONTINUE;
1055 /*************************************************************
1056 ************************** Public API ***********************
1057 *************************************************************/
1060 _gdk_dnd_init (void)
1064 hres = OleInitialize (NULL);
1066 if (! SUCCEEDED (hres))
1067 g_error ("OleInitialize failed");
1070 formats = g_new (FORMATETC, nformats);
1072 formats[0].cfFormat = CF_TEXT;
1073 formats[0].ptd = NULL;
1074 formats[0].dwAspect = DVASPECT_CONTENT;
1075 formats[0].lindex = -1;
1076 formats[0].tymed = TYMED_HGLOBAL;
1078 formats[1].cfFormat = CF_GDIOBJFIRST;
1079 formats[1].ptd = NULL;
1080 formats[1].dwAspect = DVASPECT_CONTENT;
1081 formats[1].lindex = -1;
1082 formats[1].tymed = TYMED_HGLOBAL;
1087 _gdk_win32_dnd_exit (void)
1097 local_send_leave (GdkDragContext *context,
1102 if ((current_dest_drag != NULL) &&
1103 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1104 (current_dest_drag->source_window == context->source_window))
1106 tmp_event.dnd.type = GDK_DRAG_LEAVE;
1107 tmp_event.dnd.window = context->dest_window;
1108 /* Pass ownership of context to the event */
1109 tmp_event.dnd.send_event = FALSE;
1110 tmp_event.dnd.context = current_dest_drag;
1111 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1113 current_dest_drag = NULL;
1115 gdk_event_put (&tmp_event);
1120 local_send_enter (GdkDragContext *context,
1124 GdkDragContextPrivateWin32 *private;
1125 GdkDragContext *new_context;
1127 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1129 if (current_dest_drag != NULL)
1131 gdk_drag_context_unref (current_dest_drag);
1132 current_dest_drag = NULL;
1135 new_context = gdk_drag_context_new ();
1136 new_context->protocol = GDK_DRAG_PROTO_LOCAL;
1137 new_context->is_source = FALSE;
1139 new_context->source_window = context->source_window;
1140 g_object_ref (new_context->source_window);
1141 new_context->dest_window = context->dest_window;
1142 g_object_ref (new_context->dest_window);
1144 new_context->targets = g_list_copy (context->targets);
1146 gdk_window_set_events (new_context->source_window,
1147 gdk_window_get_events (new_context->source_window) |
1148 GDK_PROPERTY_CHANGE_MASK);
1149 new_context->actions = context->actions;
1151 tmp_event.dnd.type = GDK_DRAG_ENTER;
1152 tmp_event.dnd.window = context->dest_window;
1153 tmp_event.dnd.send_event = FALSE;
1154 tmp_event.dnd.context = new_context;
1155 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1157 current_dest_drag = new_context;
1159 gdk_event_put (&tmp_event);
1163 local_send_motion (GdkDragContext *context,
1166 GdkDragAction action,
1171 if ((current_dest_drag != NULL) &&
1172 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1173 (current_dest_drag->source_window == context->source_window))
1175 tmp_event.dnd.type = GDK_DRAG_MOTION;
1176 tmp_event.dnd.window = current_dest_drag->dest_window;
1177 tmp_event.dnd.send_event = FALSE;
1178 tmp_event.dnd.context = current_dest_drag;
1179 tmp_event.dnd.time = time;
1181 current_dest_drag->suggested_action = action;
1182 current_dest_drag->actions = current_dest_drag->suggested_action;
1184 tmp_event.dnd.x_root = x_root;
1185 tmp_event.dnd.y_root = y_root;
1187 (GDK_DRAG_CONTEXT_PRIVATE_DATA (current_dest_drag))->last_x = x_root;
1188 (GDK_DRAG_CONTEXT_PRIVATE_DATA (current_dest_drag))->last_y = y_root;
1190 GDK_DRAG_CONTEXT_PRIVATE_DATA (context)->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1192 gdk_event_put (&tmp_event);
1197 local_send_drop (GdkDragContext *context,
1202 if ((current_dest_drag != NULL) &&
1203 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1204 (current_dest_drag->source_window == context->source_window))
1206 GdkDragContextPrivateWin32 *private;
1207 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (current_dest_drag);
1209 /* Pass ownership of context to the event */
1210 tmp_event.dnd.type = GDK_DROP_START;
1211 tmp_event.dnd.window = current_dest_drag->dest_window;
1212 tmp_event.dnd.send_event = FALSE;
1213 tmp_event.dnd.context = current_dest_drag;
1214 tmp_event.dnd.time = GDK_CURRENT_TIME;
1216 tmp_event.dnd.x_root = private->last_x;
1217 tmp_event.dnd.y_root = private->last_y;
1219 current_dest_drag = NULL;
1221 gdk_event_put (&tmp_event);
1227 gdk_drag_do_leave (GdkDragContext *context,
1230 if (context->dest_window)
1232 GDK_NOTE (DND, g_print ("gdk_drag_do_leave\n"));
1234 switch (context->protocol)
1236 case GDK_DRAG_PROTO_LOCAL:
1237 local_send_leave (context, time);
1243 g_object_unref (context->dest_window);
1244 context->dest_window = NULL;
1249 gdk_drag_begin (GdkWindow *window,
1254 GdkDragContext *new_context;
1256 g_return_val_if_fail (window != NULL, NULL);
1258 new_context = gdk_drag_context_new ();
1259 new_context->is_source = TRUE;
1260 new_context->source_window = window;
1261 g_object_ref (window);
1263 tmp_list = g_list_last (targets);
1264 new_context->targets = NULL;
1267 new_context->targets = g_list_prepend (new_context->targets,
1269 tmp_list = tmp_list->prev;
1272 new_context->actions = 0;
1276 source_drag_context *ctx;
1284 g_return_val_if_fail (window != NULL, NULL);
1286 GDK_NOTE (DND, g_print ("gdk_drag_begin\n"));
1288 ctx = source_context_new ();
1289 ctx->context->protocol = GDK_DRAG_PROTO_OLE2;
1290 ctx->context->source_window = window;
1291 g_object_ref (window);
1293 tmp_list = g_list_last (targets);
1294 ctx->context->targets = NULL;
1297 ctx->context->targets = g_list_prepend (ctx->context->targets,
1299 tmp_list = tmp_list->prev;
1302 ctx->context->actions = 0;
1304 dobj = data_object_new ();
1306 global = GlobalAlloc (GMEM_FIXED, sizeof (ctx));
1308 memcpy (&global, ctx, sizeof (ctx));
1310 medium.tymed = TYMED_HGLOBAL;
1311 medium.hGlobal = global;
1312 medium.pUnkForRelease = NULL;
1314 dobj->ido.lpVtbl->SetData (&dobj->ido, &formats[1], &medium, TRUE);
1316 hResult = DoDragDrop (&dobj->ido, &ctx->ids, DROPEFFECT_MOVE|DROPEFFECT_COPY, &dwEffect);
1318 GDK_NOTE (DND, g_print ("DoDragDrop returned %s\n",
1319 (hResult == DRAGDROP_S_DROP ? "DRAGDROP_S_DROP" :
1320 (hResult == DRAGDROP_S_CANCEL ? "DRAGDROP_S_CANCEL" :
1321 (hResult == E_UNEXPECTED ? "E_UNEXPECTED" :
1322 g_strdup_printf ("%#.8lx", hResult))))));
1324 dobj->ido.lpVtbl->Release (&dobj->ido);
1325 ctx->ids.lpVtbl->Release (&ctx->ids);
1327 return ctx->context;
1332 gdk_drag_get_protocol_for_display (GdkDisplay *display,
1334 GdkDragProtocol *protocol)
1338 GDK_NOTE (DND, g_print ("gdk_drag_get_protocol\n"));
1340 window = gdk_window_lookup (xid);
1342 if (GPOINTER_TO_INT (gdk_drawable_get_data (window, "gdk-dnd-registered")))
1344 *protocol = GDK_DRAG_PROTO_LOCAL;
1356 } find_window_enum_arg;
1358 static BOOL CALLBACK
1359 find_window_enum_proc (HWND hwnd,
1364 find_window_enum_arg *a = (find_window_enum_arg *) lparam;
1366 if (hwnd == a->ignore)
1369 if (!IsWindowVisible (hwnd))
1373 ClientToScreen (hwnd, &tl);
1374 GetClientRect (hwnd, &rect);
1377 ClientToScreen (hwnd, &br);
1379 if (a->x >= tl.x && a->y >= tl.y && a->x < br.x && a->y < br.y)
1389 gdk_drag_find_window_for_screen (GdkDragContext *context,
1390 GdkWindow *drag_window,
1394 GdkWindow **dest_window,
1395 GdkDragProtocol *protocol)
1397 find_window_enum_arg a;
1399 a.x = x_root - _gdk_offset_x;
1400 a.y = y_root - _gdk_offset_y;
1401 a.ignore = drag_window ? GDK_WINDOW_HWND (drag_window) : NULL;
1404 EnumWindows (find_window_enum_proc, (LPARAM) &a);
1406 if (a.result == NULL)
1407 *dest_window = NULL;
1410 *dest_window = gdk_win32_handle_table_lookup (GPOINTER_TO_UINT (a.result));
1413 *dest_window = gdk_window_get_toplevel (*dest_window);
1414 g_object_ref (*dest_window);
1417 if (context->source_window)
1418 *protocol = GDK_DRAG_PROTO_LOCAL;
1420 *protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
1424 g_print ("gdk_drag_find_window: %p %+d%+d: %p: %p %d\n",
1425 (drag_window ? GDK_WINDOW_HWND (drag_window) : NULL),
1428 (*dest_window ? GDK_WINDOW_HWND (*dest_window) : NULL),
1433 gdk_drag_motion (GdkDragContext *context,
1434 GdkWindow *dest_window,
1435 GdkDragProtocol protocol,
1438 GdkDragAction suggested_action,
1439 GdkDragAction possible_actions,
1442 GdkDragContextPrivateWin32 *private;
1444 g_return_val_if_fail (context != NULL, FALSE);
1446 GDK_NOTE (DND, g_print ("gdk_drag_motion\n"));
1448 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1450 if (context->dest_window != dest_window)
1452 GdkEvent temp_event;
1454 /* Send a leave to the last destination */
1455 gdk_drag_do_leave (context, time);
1456 private->drag_status = GDK_DRAG_STATUS_DRAG;
1458 /* Check if new destination accepts drags, and which protocol */
1461 context->dest_window = dest_window;
1462 g_object_ref (context->dest_window);
1463 context->protocol = protocol;
1467 case GDK_DRAG_PROTO_LOCAL:
1468 local_send_enter (context, time);
1474 context->suggested_action = suggested_action;
1478 context->dest_window = NULL;
1479 context->action = 0;
1482 /* Push a status event, to let the client know that
1486 temp_event.dnd.type = GDK_DRAG_STATUS;
1487 temp_event.dnd.window = context->source_window;
1488 /* We use this to signal a synthetic status. Perhaps
1489 * we should use an extra field...
1491 temp_event.dnd.send_event = TRUE;
1493 temp_event.dnd.context = context;
1494 temp_event.dnd.time = time;
1496 gdk_event_put (&temp_event);
1500 context->suggested_action = suggested_action;
1503 /* Send a drag-motion event */
1505 private->last_x = x_root;
1506 private->last_y = y_root;
1508 if (context->dest_window)
1510 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
1512 switch (context->protocol)
1514 case GDK_DRAG_PROTO_LOCAL:
1515 local_send_motion (context, x_root, y_root, suggested_action, time);
1518 case GDK_DRAG_PROTO_NONE:
1519 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
1534 gdk_drag_drop (GdkDragContext *context,
1537 g_return_if_fail (context != NULL);
1539 GDK_NOTE (DND, g_print ("gdk_drag_drop\n"));
1541 if (context->dest_window)
1543 switch (context->protocol)
1545 case GDK_DRAG_PROTO_LOCAL:
1546 local_send_drop (context, time);
1549 case GDK_DRAG_PROTO_NONE:
1550 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
1560 gdk_drag_abort (GdkDragContext *context,
1563 g_return_if_fail (context != NULL);
1565 GDK_NOTE (DND, g_print ("gdk_drag_abort\n"));
1567 gdk_drag_do_leave (context, time);
1570 /* Destination side */
1573 gdk_drag_status (GdkDragContext *context,
1574 GdkDragAction action,
1577 GdkDragContextPrivateWin32 *private;
1578 GdkDragContext *src_context;
1581 g_return_if_fail (context != NULL);
1583 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1585 context->action = action;
1587 src_context = gdk_drag_context_find (TRUE,
1588 context->source_window,
1589 context->dest_window);
1593 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (src_context);
1595 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1596 private->drag_status = GDK_DRAG_STATUS_DRAG;
1598 tmp_event.dnd.type = GDK_DRAG_STATUS;
1599 tmp_event.dnd.window = context->source_window;
1600 tmp_event.dnd.send_event = FALSE;
1601 tmp_event.dnd.context = src_context;
1602 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1604 if (action == GDK_ACTION_DEFAULT)
1607 src_context->action = action;
1609 gdk_event_put (&tmp_event);
1614 gdk_drop_reply (GdkDragContext *context,
1618 g_return_if_fail (context != NULL);
1620 GDK_NOTE (DND, g_print ("gdk_drop_reply\n"));
1622 if (context->dest_window)
1624 switch (context->protocol)
1626 case GDK_DRAG_PROTO_WIN32_DROPFILES:
1627 _gdk_dropfiles_store (NULL);
1637 gdk_drop_finish (GdkDragContext *context,
1641 GdkDragContextPrivateWin32 *private;
1642 GdkDragContext *src_context;
1645 g_return_if_fail (context != NULL);
1647 GDK_NOTE (DND, g_print ("gdk_drop_finish"));
1649 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1651 src_context = gdk_drag_context_find (TRUE,
1652 context->source_window,
1653 context->dest_window);
1656 tmp_event.dnd.type = GDK_DROP_FINISHED;
1657 tmp_event.dnd.window = src_context->source_window;
1658 tmp_event.dnd.send_event = FALSE;
1659 tmp_event.dnd.context = src_context;
1661 gdk_event_put (&tmp_event);
1667 static GdkFilterReturn
1668 gdk_destroy_filter (GdkXEvent *xev,
1672 MSG *msg = (MSG *) xev;
1674 if (msg->message == WM_DESTROY)
1676 IDropTarget *idtp = (IDropTarget *) data;
1678 GDK_NOTE (DND, g_print ("gdk_destroy_filter: WM_DESTROY: %p\n", msg->hwnd));
1680 idtp->lpVtbl->Release (idtp);
1682 RevokeDragDrop (msg->hwnd);
1683 CoLockObjectExternal (idtp, FALSE, TRUE);
1685 return GDK_FILTER_CONTINUE;
1690 gdk_window_register_dnd (GdkWindow *window)
1693 target_drag_context *ctx;
1697 g_return_if_fail (window != NULL);
1699 if (GPOINTER_TO_INT (gdk_drawable_get_data (window, "gdk-dnd-registered")))
1702 gdk_drawable_set_data (window, "gdk-dnd-registered", GINT_TO_POINTER(TRUE), NULL);
1704 GDK_NOTE (DND, g_print ("gdk_window_register_dnd: %p\n",
1705 GDK_WINDOW_HWND (window)));
1707 /* We always claim to accept dropped files, but in fact we might not,
1708 * of course. This function is called in such a way that it cannot know
1709 * whether the window (widget) in question actually accepts files
1710 * (in gtk, data of type text/uri-list) or not.
1712 gdk_window_add_filter (window, gdk_dropfiles_filter, NULL);
1713 DragAcceptFiles (GDK_WINDOW_HWND (window), TRUE);
1716 /* Register for OLE2 d&d */
1717 ctx = target_context_new ();
1718 ctx->context->protocol = GDK_DRAG_PROTO_OLE2;
1719 hres = CoLockObjectExternal ((IUnknown *) &ctx->idt, TRUE, FALSE);
1720 if (!SUCCEEDED (hres))
1721 OTHER_API_FAILED ("CoLockObjectExternal");
1724 hres = RegisterDragDrop (GDK_WINDOW_HWND (window), &ctx->idt);
1725 if (hres == DRAGDROP_E_ALREADYREGISTERED)
1727 g_print ("DRAGDROP_E_ALREADYREGISTERED\n");
1729 ctx->idt.lpVtbl->Release (&ctx->idt);
1731 CoLockObjectExternal ((IUnknown *) &ctx->idt, FALSE, FALSE);
1733 else if (!SUCCEEDED (hres))
1734 OTHER_API_FAILED ("RegisterDragDrop");
1737 gdk_window_add_filter (window, gdk_destroy_filter, &ctx->idt);
1743 /*************************************************************
1744 * gdk_drag_get_selection:
1745 * Returns the selection atom for the current source window
1749 *************************************************************/
1752 gdk_drag_get_selection (GdkDragContext *context)
1754 if (context->protocol == GDK_DRAG_PROTO_LOCAL)
1756 else if (context->protocol == GDK_DRAG_PROTO_WIN32_DROPFILES)
1757 return _gdk_win32_dropfiles;
1758 else if (context->protocol == GDK_DRAG_PROTO_OLE2)
1759 return _gdk_ole2_dnd;
1765 gdk_drag_drop_succeeded (GdkDragContext *context)
1767 GdkDragContextPrivateWin32 *private;
1769 g_return_val_if_fail (context != NULL, FALSE);
1771 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1773 /* FIXME: Can we set drop_failed when the drop has failed? */
1774 return !private->drop_failed;