1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 1998-1999 Tor Lillqvist
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/.
32 /* #define OLE2_DND */
37 #include "gdkproperty.h"
38 #include "gdkinternals.h"
39 #include "gdkprivate-win32.h"
52 typedef struct _GdkDragContextPrivateWin32 GdkDragContextPrivateWin32;
56 GDK_DRAG_STATUS_MOTION_WAIT,
57 GDK_DRAG_STATUS_ACTION_WAIT,
68 #define PRINT_GUID(guid) \
69 g_print ("guid = %.08x-%.04x-%.04x-%.02x%.02x-%.02x%.02x%.02x%.02x%.02x%.02x", \
70 ((gulong *) guid)[0], \
71 ((gushort *) guid)[2], \
72 ((gushort *) guid)[3], \
73 ((guchar *) guid)[8], \
74 ((guchar *) guid)[9], \
75 ((guchar *) guid)[10], \
76 ((guchar *) guid)[11], \
77 ((guchar *) guid)[12], \
78 ((guchar *) guid)[13], \
79 ((guchar *) guid)[14], \
80 ((guchar *) guid)[15]);
83 static FORMATETC *formats;
88 /* Structure that holds information about a drag in progress.
89 * this is used on both source and destination sides.
91 struct _GdkDragContextPrivateWin32 {
92 GdkAtom local_selection;
95 guint16 last_x; /* Coordinates from last event */
98 guint drag_status; /* Current status of drag */
101 #define GDK_DRAG_CONTEXT_PRIVATE_DATA(context) ((GdkDragContextPrivateWin32 *) GDK_DRAG_CONTEXT (context)->windowing_data)
103 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;
146 private->ref_count = 1;
148 contexts = g_list_prepend (contexts, dragcontext);
152 gdk_drag_context_class_init (GdkDragContextClass *klass)
154 GObjectClass *object_class = G_OBJECT_CLASS (klass);
156 parent_class = g_type_class_peek_parent (klass);
158 object_class->finalize = gdk_drag_context_finalize;
162 gdk_drag_context_finalize (GObject *object)
164 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
165 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
167 g_list_free (context->targets);
169 if (context->source_window)
171 gdk_window_unref (context->source_window);
174 if (context->dest_window)
175 gdk_window_unref (context->dest_window);
177 contexts = g_list_remove (contexts, context);
181 G_OBJECT_CLASS (parent_class)->finalize (object);
187 gdk_drag_context_new (void)
189 return g_object_new (gdk_drag_context_get_type (), NULL);
193 gdk_drag_context_ref (GdkDragContext *context)
195 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
197 g_object_ref (G_OBJECT (context));
201 gdk_drag_context_unref (GdkDragContext *context)
203 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
205 g_object_unref (G_OBJECT (context));
208 static GdkDragContext *
209 gdk_drag_context_find (gboolean is_source,
213 GList *tmp_list = contexts;
215 GdkDragContext *context;
216 GdkDragContextPrivateWin32 *private;
220 context = (GdkDragContext *)tmp_list->data;
221 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
223 if ((!context->is_source == !is_source) &&
224 ((source == NULL) || (context->source_window && (context->source_window == source))) &&
225 ((dest == NULL) || (context->dest_window && (context->dest_window == dest))))
228 tmp_list = tmp_list->next;
239 GdkDragContext *context;
240 } target_drag_context;
246 GdkDragContext *context;
247 } source_drag_context;
262 static enum_formats *enum_formats_new (void);
264 static ULONG STDMETHODCALLTYPE
265 idroptarget_addref (LPDROPTARGET This)
267 target_drag_context *ctx = (target_drag_context *) This;
268 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (ctx->context);
269 int ref_count = ++private->ref_count;
271 gdk_drag_context_ref (ctx->context);
272 GDK_NOTE (DND, g_print ("idroptarget_addref %#x %d\n", This, ref_count));
277 static HRESULT STDMETHODCALLTYPE
278 idroptarget_queryinterface (LPDROPTARGET This,
282 GDK_NOTE (DND, g_print ("idroptarget_queryinterface %#x\n", This));
288 if (IsEqualGUID (riid, &IID_IUnknown))
290 g_print ("...IUnknown\n");
291 idroptarget_addref (This);
295 else if (IsEqualGUID (riid, &IID_IDropTarget))
297 g_print ("...IDropTarget\n");
298 idroptarget_addref (This);
304 g_print ("...Huh?\n");
305 return E_NOINTERFACE;
309 static ULONG STDMETHODCALLTYPE
310 idroptarget_release (LPDROPTARGET This)
312 target_drag_context *ctx = (target_drag_context *) This;
313 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (ctx->context);
314 int ref_count = --private->ref_count;
316 gdk_drag_context_unref (ctx->context);
317 GDK_NOTE (DND, g_print ("idroptarget_release %#x %d\n", This, ref_count));
325 static HRESULT STDMETHODCALLTYPE
326 idroptarget_dragenter (LPDROPTARGET This,
327 LPDATAOBJECT pDataObj,
332 GDK_NOTE (DND, g_print ("idroptarget_dragenter %#x\n", This));
337 static HRESULT STDMETHODCALLTYPE
338 idroptarget_dragover (LPDROPTARGET This,
343 GDK_NOTE (DND, g_print ("idroptarget_dragover %#x\n", This));
348 static HRESULT STDMETHODCALLTYPE
349 idroptarget_dragleave (LPDROPTARGET This)
351 GDK_NOTE (DND, g_print ("idroptarget_dragleave %#x\n", This));
356 static HRESULT STDMETHODCALLTYPE
357 idroptarget_drop (LPDROPTARGET This,
358 LPDATAOBJECT pDataObj,
363 GDK_NOTE (DND, g_print ("idroptarget_drop %#x\n", This));
368 static ULONG STDMETHODCALLTYPE
369 idropsource_addref (LPDROPSOURCE This)
371 source_drag_context *ctx = (source_drag_context *) This;
372 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (ctx->context);
374 gdk_drag_context_ref (ctx->context);
375 GDK_NOTE (DND, g_print ("idropsource_addref %#x %d\n",
376 This, private->ref_count));
378 return private->ref_count;
381 static HRESULT STDMETHODCALLTYPE
382 idropsource_queryinterface (LPDROPSOURCE This,
386 GDK_NOTE (DND, g_print ("idropsource_queryinterface %#x\n", This));
391 if (IsEqualGUID (riid, &IID_IUnknown))
393 g_print ("...IUnknown\n");
394 idropsource_addref (This);
398 else if (IsEqualGUID (riid, &IID_IDropSource))
400 g_print ("...IDropSource\n");
401 idropsource_addref (This);
407 g_print ("...Huh?\n");
408 return E_NOINTERFACE;
412 static ULONG STDMETHODCALLTYPE
413 idropsource_release (LPDROPSOURCE This)
415 source_drag_context *ctx = (source_drag_context *) This;
416 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (ctx->context);
417 int ref_count = --private->ref_count;
419 gdk_drag_context_unref (ctx->context);
420 GDK_NOTE (DND, g_print ("idropsource_release %#x %d\n", This, ref_count));
428 static HRESULT STDMETHODCALLTYPE
429 idropsource_querycontinuedrag (LPDROPSOURCE This,
433 GDK_NOTE (DND, g_print ("idropsource_querycontinuedrag %#x\n", This));
438 static HRESULT STDMETHODCALLTYPE
439 idropsource_givefeedback (LPDROPSOURCE This,
442 GDK_NOTE (DND, g_print ("idropsource_givefeedback %#x\n", This));
447 static ULONG STDMETHODCALLTYPE
448 idataobject_addref (LPDATAOBJECT This)
450 data_object *dobj = (data_object *) This;
451 int ref_count = ++dobj->ref_count;
453 GDK_NOTE (DND, g_print ("idataobject_addref %#x %d\n", This, ref_count));
458 static HRESULT STDMETHODCALLTYPE
459 idataobject_queryinterface (LPDATAOBJECT This,
463 GDK_NOTE (DND, g_print ("idataobject_queryinterface %#x\n", This));
468 if (IsEqualGUID (riid, &IID_IUnknown))
470 g_print ("...IUnknown\n");
471 idataobject_addref (This);
475 else if (IsEqualGUID (riid, &IID_IDataObject))
477 g_print ("...IDataObject\n");
478 idataobject_addref (This);
484 g_print ("...Huh?\n");
485 return E_NOINTERFACE;
489 static ULONG STDMETHODCALLTYPE
490 idataobject_release (LPDATAOBJECT This)
492 data_object *dobj = (data_object *) This;
493 int ref_count = --dobj->ref_count;
495 GDK_NOTE (DND, g_print ("idataobject_release %#x %d\n", This, ref_count));
503 static HRESULT STDMETHODCALLTYPE
504 idataobject_getdata (LPDATAOBJECT This,
505 LPFORMATETC pFormatEtc,
508 GDK_NOTE (DND, g_print ("idataobject_getdata %#x\n", This));
513 static HRESULT STDMETHODCALLTYPE
514 idataobject_getdatahere (LPDATAOBJECT This,
515 LPFORMATETC pFormatEtc,
518 GDK_NOTE (DND, g_print ("idataobject_getdatahere %#x\n", This));
523 static HRESULT STDMETHODCALLTYPE
524 idataobject_querygetdata (LPDATAOBJECT This,
525 LPFORMATETC pFormatEtc)
529 GDK_NOTE (DND, g_print ("idataobject_querygetdata %#x %#x", This, pFormatEtc->cfFormat));
531 for (i = 0; i < nformats; i++)
532 if (pFormatEtc->cfFormat == formats[i].cfFormat)
534 GDK_NOTE (DND, g_print (" S_OK\n"));
538 GDK_NOTE (DND, g_print (" DV_E_FORMATETC\n"));
539 return DV_E_FORMATETC;
542 static HRESULT STDMETHODCALLTYPE
543 idataobject_getcanonicalformatetc (LPDATAOBJECT This,
544 LPFORMATETC pFormatEtcIn,
545 LPFORMATETC pFormatEtcOut)
547 GDK_NOTE (DND, g_print ("idataobject_getcanonicalformatetc %#x\n", This));
552 static HRESULT STDMETHODCALLTYPE
553 idataobject_setdata (LPDATAOBJECT This,
554 LPFORMATETC pFormatEtc,
558 GDK_NOTE (DND, g_print ("idataobject_setdata %#x\n", This));
563 static HRESULT STDMETHODCALLTYPE
564 idataobject_enumformatetc (LPDATAOBJECT This,
566 LPENUMFORMATETC *ppEnumFormatEtc)
568 GDK_NOTE (DND, g_print ("idataobject_enumformatetc %#x\n", This));
570 if (dwDirection != DATADIR_GET)
573 *ppEnumFormatEtc = &enum_formats_new ()->ief;
577 static HRESULT STDMETHODCALLTYPE
578 idataobject_dadvise (LPDATAOBJECT This,
579 LPFORMATETC pFormatetc,
581 LPADVISESINK pAdvSink,
582 DWORD *pdwConnection)
584 GDK_NOTE (DND, g_print ("idataobject_dadvise %#x\n", This));
589 static HRESULT STDMETHODCALLTYPE
590 idataobject_dunadvise (LPDATAOBJECT This,
593 GDK_NOTE (DND, g_print ("idataobject_dunadvise %#x\n", This));
598 static HRESULT STDMETHODCALLTYPE
599 idataobject_enumdadvise (LPDATAOBJECT This,
600 LPENUMSTATDATA *ppenumAdvise)
602 GDK_NOTE (DND, g_print ("idataobject_enumdadvise %#x\n", This));
607 static ULONG STDMETHODCALLTYPE
608 ienumformatetc_addref (LPENUMFORMATETC This)
610 enum_formats *en = (enum_formats *) This;
611 int ref_count = ++en->ref_count;
613 GDK_NOTE (DND, g_print ("ienumformatetc_addref %#x %d\n", This, ref_count));
618 static HRESULT STDMETHODCALLTYPE
619 ienumformatetc_queryinterface (LPENUMFORMATETC This,
623 GDK_NOTE (DND, g_print ("ienumformatetc_queryinterface %#x\n", This));
628 if (IsEqualGUID (riid, &IID_IUnknown))
630 g_print ("...IUnknown\n");
631 ienumformatetc_addref (This);
635 else if (IsEqualGUID (riid, &IID_IEnumFORMATETC))
637 g_print ("...IEnumFORMATETC\n");
638 ienumformatetc_addref (This);
644 g_print ("...Huh?\n");
645 return E_NOINTERFACE;
649 static ULONG STDMETHODCALLTYPE
650 ienumformatetc_release (LPENUMFORMATETC This)
652 enum_formats *en = (enum_formats *) This;
653 int ref_count = --en->ref_count;
655 GDK_NOTE (DND, g_print ("ienumformatetc_release %#x %d\n", This, ref_count));
663 static HRESULT STDMETHODCALLTYPE
664 ienumformatetc_next (LPENUMFORMATETC This,
669 enum_formats *en = (enum_formats *) This;
672 GDK_NOTE (DND, g_print ("ienumformatetc_next %#x %d %d\n", This, en->ix, celt));
675 for (i = 0; i < celt; i++)
677 if (en->ix >= nformats)
679 elts[i] = formats[en->ix++];
692 static HRESULT STDMETHODCALLTYPE
693 ienumformatetc_skip (LPENUMFORMATETC This,
696 enum_formats *en = (enum_formats *) This;
698 GDK_NOTE (DND, g_print ("ienumformatetc_skip %#x %d %d\n", This, en->ix, celt));
704 static HRESULT STDMETHODCALLTYPE
705 ienumformatetc_reset (LPENUMFORMATETC This)
707 enum_formats *en = (enum_formats *) This;
709 GDK_NOTE (DND, g_print ("ienumformatetc_reset %#x\n", This));
716 static HRESULT STDMETHODCALLTYPE
717 ienumformatetc_clone (LPENUMFORMATETC This,
718 LPENUMFORMATETC *ppEnumFormatEtc)
720 enum_formats *en = (enum_formats *) This;
723 GDK_NOTE (DND, g_print ("ienumformatetc_clone %#x\n", This));
725 new = enum_formats_new ();
729 *ppEnumFormatEtc = &new->ief;
734 static IDropTargetVtbl idt_vtbl = {
735 idroptarget_queryinterface,
738 idroptarget_dragenter,
739 idroptarget_dragover,
740 idroptarget_dragleave,
744 static IDropSourceVtbl ids_vtbl = {
745 idropsource_queryinterface,
748 idropsource_querycontinuedrag,
749 idropsource_givefeedback
752 static IDataObjectVtbl ido_vtbl = {
753 idataobject_queryinterface,
757 idataobject_getdatahere,
758 idataobject_querygetdata,
759 idataobject_getcanonicalformatetc,
761 idataobject_enumformatetc,
763 idataobject_dunadvise,
764 idataobject_enumdadvise
767 static IEnumFORMATETCVtbl ief_vtbl = {
768 ienumformatetc_queryinterface,
769 ienumformatetc_addref,
770 ienumformatetc_release,
773 ienumformatetc_reset,
777 #endif /* OLE2_DND */
779 static target_drag_context *
780 target_context_new (void)
782 target_drag_context *result;
784 result = g_new0 (target_drag_context, 1);
787 result->idt.lpVtbl = &idt_vtbl;
790 result->context = gdk_drag_context_new ();
791 result->context->is_source = FALSE;
793 GDK_NOTE (DND, g_print ("target_context_new: %p\n", result));
798 static source_drag_context *
799 source_context_new (void)
801 source_drag_context *result;
803 result = g_new0 (source_drag_context, 1);
806 result->ids.lpVtbl = &ids_vtbl;
809 result->context = gdk_drag_context_new ();
810 result->context->is_source = TRUE;
812 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: %#x\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: %#x\n", result));
852 /* From MS Knowledge Base article Q130698 */
854 /* resolve_link() fills the filename and path buffer
855 * with relevant information
856 * hWnd - calling app's window handle.
858 * lpszLinkName - name of the link file passed into the function.
860 * lpszPath - the buffer that will receive the file pathname.
864 resolve_link(HWND hWnd,
865 LPCTSTR lpszLinkName,
867 LPSTR lpszDescription)
873 /* Assume Failure to start with: */
876 *lpszDescription = 0;
878 /* Call CoCreateInstance to obtain the IShellLink interface
879 * pointer. This call fails if CoInitialize is not called, so it is
880 * assumed that CoInitialize has been called.
883 hres = CoCreateInstance (&CLSID_ShellLink,
885 CLSCTX_INPROC_SERVER,
888 if (SUCCEEDED (hres))
892 /* The IShellLink interface supports the IPersistFile
893 * interface. Get an interface pointer to it.
895 hres = psl->lpVtbl->QueryInterface (psl,
898 if (SUCCEEDED (hres))
902 /* Convert the given link name string to wide character string. */
903 MultiByteToWideChar (CP_ACP, 0,
907 hres = ppf->lpVtbl->Load (ppf, wsz, STGM_READ);
908 if (SUCCEEDED (hres))
910 /* Resolve the link by calling the Resolve()
911 * interface function.
913 hres = psl->lpVtbl->Resolve(psl, hWnd,
916 if (SUCCEEDED (hres))
918 hres = psl->lpVtbl->GetPath (psl, lpszPath,
920 (WIN32_FIND_DATA*)&wfd,
923 if (SUCCEEDED (hres) && lpszDescription != NULL)
925 hres = psl->lpVtbl->GetDescription (psl,
929 if (!SUCCEEDED (hres))
934 ppf->lpVtbl->Release (ppf);
936 psl->lpVtbl->Release (psl);
938 return SUCCEEDED (hres);
941 static GdkFilterReturn
942 gdk_dropfiles_filter (GdkXEvent *xev,
946 GdkDragContext *context;
947 GdkDragContextPrivateWin32 *private;
948 static GdkAtom text_uri_list_atom = GDK_NONE;
950 MSG *msg = (MSG *) xev;
954 guchar fileName[MAX_PATH], linkedFile[MAX_PATH];
956 if (text_uri_list_atom == GDK_NONE)
957 text_uri_list_atom = gdk_atom_intern ("text/uri-list", FALSE);
959 if (msg->message == WM_DROPFILES)
961 GDK_NOTE (DND, g_print ("WM_DROPFILES: %#x\n", (guint) msg->hwnd));
963 context = gdk_drag_context_new ();
964 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
965 context->protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
966 context->is_source = FALSE;
967 context->source_window = gdk_parent_root;
968 gdk_drawable_ref (context->source_window);
969 context->dest_window = event->any.window;
970 gdk_drawable_ref (context->dest_window);
971 /* WM_DROPFILES drops are always file names */
973 g_list_append (NULL, GUINT_TO_POINTER (text_uri_list_atom));
974 current_dest_drag = context;
976 event->dnd.type = GDK_DROP_START;
977 event->dnd.context = current_dest_drag;
978 gdk_drag_context_ref (current_dest_drag);
980 hdrop = (HANDLE) msg->wParam;
981 DragQueryPoint (hdrop, &pt);
982 ClientToScreen (msg->hwnd, &pt);
984 event->dnd.x_root = pt.x;
985 event->dnd.y_root = pt.y;
986 event->dnd.time = msg->time;
988 nfiles = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
990 result = g_string_new (NULL);
991 for (i = 0; i < nfiles; i++)
993 g_string_append (result, "file:");
994 DragQueryFile (hdrop, i, fileName, MAX_PATH);
996 /* Resolve shortcuts */
997 if (resolve_link (msg->hwnd, fileName, linkedFile, NULL))
999 g_string_append (result, linkedFile);
1000 GDK_NOTE (DND, g_print ("...%s link to %s\n",
1001 fileName, linkedFile));
1005 g_string_append (result, fileName);
1006 GDK_NOTE (DND, g_print ("...%s\n", fileName));
1008 g_string_append (result, "\015\012");
1010 gdk_sel_prop_store (gdk_parent_root, text_uri_list_atom, 8,
1011 result->str, result->len + 1);
1015 return GDK_FILTER_TRANSLATE;
1018 return GDK_FILTER_CONTINUE;
1021 /*************************************************************
1022 ************************** Public API ***********************
1023 *************************************************************/
1030 hres = OleInitialize (NULL);
1032 if (! SUCCEEDED (hres))
1033 g_error ("OleInitialize failed");
1036 formats = g_new (FORMATETC, nformats);
1038 formats[0].cfFormat = CF_TEXT;
1039 formats[0].ptd = NULL;
1040 formats[0].dwAspect = DVASPECT_CONTENT;
1041 formats[0].lindex = -1;
1042 formats[0].tymed = TYMED_HGLOBAL;
1044 formats[1].cfFormat = CF_GDIOBJFIRST;
1045 formats[1].ptd = NULL;
1046 formats[1].dwAspect = DVASPECT_CONTENT;
1047 formats[1].lindex = -1;
1048 formats[1].tymed = TYMED_HGLOBAL;
1053 gdk_win32_dnd_exit (void)
1063 local_send_leave (GdkDragContext *context,
1068 if ((current_dest_drag != NULL) &&
1069 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1070 (current_dest_drag->source_window == context->source_window))
1072 tmp_event.dnd.type = GDK_DRAG_LEAVE;
1073 tmp_event.dnd.window = context->dest_window;
1074 /* Pass ownership of context to the event */
1075 tmp_event.dnd.context = current_dest_drag;
1076 tmp_event.dnd.send_event = FALSE;
1077 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1079 current_dest_drag = NULL;
1081 gdk_event_put (&tmp_event);
1087 local_send_enter (GdkDragContext *context,
1091 GdkDragContextPrivateWin32 *private;
1092 GdkDragContext *new_context;
1094 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1096 if (!private->local_selection)
1097 private->local_selection = gdk_atom_intern ("LocalDndSelection", FALSE);
1099 if (current_dest_drag != NULL)
1101 gdk_drag_context_unref (current_dest_drag);
1102 current_dest_drag = NULL;
1105 new_context = gdk_drag_context_new ();
1106 new_context->protocol = GDK_DRAG_PROTO_LOCAL;
1107 new_context->is_source = FALSE;
1109 new_context->source_window = context->source_window;
1110 gdk_window_ref (new_context->source_window);
1111 new_context->dest_window = context->dest_window;
1112 gdk_window_ref (new_context->dest_window);
1115 new_context->targets = g_list_copy (context->targets);
1117 gdk_window_set_events (new_context->source_window,
1118 gdk_window_get_events (new_context->source_window) |
1119 GDK_PROPERTY_CHANGE_MASK);
1120 new_context->actions = context->actions;
1122 tmp_event.dnd.type = GDK_DRAG_ENTER;
1123 tmp_event.dnd.window = context->dest_window;
1124 tmp_event.dnd.send_event = FALSE;
1125 tmp_event.dnd.context = new_context;
1126 gdk_drag_context_ref (new_context);
1128 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1130 current_dest_drag = new_context;
1132 (GDK_DRAG_CONTEXT_PRIVATE_DATA (new_context))->local_selection =
1133 private->local_selection;
1135 gdk_event_put (&tmp_event);
1139 local_send_motion (GdkDragContext *context,
1142 GdkDragAction action,
1147 if ((current_dest_drag != NULL) &&
1148 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1149 (current_dest_drag->source_window == context->source_window))
1151 tmp_event.dnd.type = GDK_DRAG_MOTION;
1152 tmp_event.dnd.window = current_dest_drag->dest_window;
1153 tmp_event.dnd.send_event = FALSE;
1154 tmp_event.dnd.context = current_dest_drag;
1155 gdk_drag_context_ref (current_dest_drag);
1157 tmp_event.dnd.time = time;
1159 current_dest_drag->suggested_action = action;
1160 current_dest_drag->actions = current_dest_drag->suggested_action;
1162 tmp_event.dnd.x_root = x_root;
1163 tmp_event.dnd.y_root = y_root;
1165 (GDK_DRAG_CONTEXT_PRIVATE_DATA (current_dest_drag))->last_x = x_root;
1166 (GDK_DRAG_CONTEXT_PRIVATE_DATA (current_dest_drag))->last_y = y_root;
1168 GDK_DRAG_CONTEXT_PRIVATE_DATA (context)->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1170 gdk_event_put (&tmp_event);
1175 local_send_drop (GdkDragContext *context, guint32 time)
1179 if ((current_dest_drag != NULL) &&
1180 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1181 (current_dest_drag->source_window == context->source_window))
1183 GdkDragContextPrivateWin32 *private;
1184 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (current_dest_drag);
1186 tmp_event.dnd.type = GDK_DROP_START;
1187 tmp_event.dnd.window = current_dest_drag->dest_window;
1188 tmp_event.dnd.send_event = FALSE;
1190 tmp_event.dnd.context = current_dest_drag;
1191 gdk_drag_context_ref (current_dest_drag);
1193 tmp_event.dnd.time = GDK_CURRENT_TIME;
1195 tmp_event.dnd.x_root = private->last_x;
1196 tmp_event.dnd.y_root = private->last_y;
1198 gdk_event_put (&tmp_event);
1204 gdk_drag_do_leave (GdkDragContext *context,
1207 if (context->dest_window)
1209 GDK_NOTE (DND, g_print ("gdk_drag_do_leave\n"));
1211 switch (context->protocol)
1213 case GDK_DRAG_PROTO_LOCAL:
1214 local_send_leave (context, time);
1220 gdk_drawable_unref (context->dest_window);
1221 context->dest_window = NULL;
1226 gdk_drag_begin (GdkWindow *window,
1231 GdkDragContext *new_context;
1233 g_return_val_if_fail (window != NULL, NULL);
1235 new_context = gdk_drag_context_new ();
1236 new_context->is_source = TRUE;
1237 new_context->source_window = window;
1238 gdk_window_ref (window);
1240 tmp_list = g_list_last (targets);
1241 new_context->targets = NULL;
1244 new_context->targets = g_list_prepend (new_context->targets,
1246 tmp_list = tmp_list->prev;
1249 new_context->actions = 0;
1253 source_drag_context *ctx;
1262 g_return_val_if_fail (window != NULL, NULL);
1264 GDK_NOTE (DND, g_print ("gdk_drag_begin\n"));
1266 ctx = source_context_new ();
1267 ctx->context->protocol = GDK_DRAG_PROTO_OLE2;
1268 ctx->context->source_window = window;
1269 gdk_drawable_ref (window);
1271 tmp_list = g_list_last (targets);
1272 ctx->context->targets = NULL;
1275 ctx->context->targets = g_list_prepend (ctx->context->targets,
1277 tmp_list = tmp_list->prev;
1280 ctx->context->actions = 0;
1282 dobj = data_object_new ();
1284 global = GlobalAlloc (GMEM_FIXED, sizeof (ctx));
1286 memcpy (&global, ctx, sizeof (ctx));
1288 medium.tymed = TYMED_HGLOBAL;
1289 medium.hGlobal = global;
1290 medium.pUnkForRelease = NULL;
1292 dobj->ido.lpVtbl->SetData (&dobj->ido, &formats[1], &medium, TRUE);
1294 hResult = DoDragDrop (&dobj->ido, &ctx->ids, DROPEFFECT_MOVE|DROPEFFECT_COPY, &dwEffect);
1296 GDK_NOTE (DND, g_print ("DoDragDrop returned %s\n",
1297 (hResult == DRAGDROP_S_DROP ? "DRAGDROP_S_DROP" :
1298 (hResult == DRAGDROP_S_CANCEL ? "DRAGDROP_S_CANCEL" :
1299 (hResult == E_UNEXPECTED ? "E_UNEXPECTED" :
1300 g_strdup_printf ("%#.8x", hResult))))));
1302 dobj->ido.lpVtbl->Release (&dobj->ido);
1303 ctx->ids.lpVtbl->Release (&ctx->ids);
1305 return ctx->context;
1310 gdk_drag_get_protocol (guint32 xid,
1311 GdkDragProtocol *protocol)
1315 GDK_NOTE (DND, g_print ("gdk_drag_get_protocol\n"));
1317 window = gdk_window_lookup (xid);
1320 if (GPOINTER_TO_INT (gdk_drawable_get_data (window, "gdk-dnd-registered")))
1322 *protocol = GDK_DRAG_PROTO_LOCAL;
1330 gdk_drag_find_window (GdkDragContext *context,
1331 GdkWindow *drag_window,
1334 GdkWindow **dest_window,
1335 GdkDragProtocol *protocol)
1342 recipient = WindowFromPoint (pt);
1343 if (recipient == NULL)
1344 *dest_window = NULL;
1347 *dest_window = gdk_win32_handle_table_lookup (GPOINTER_TO_UINT(recipient));
1349 gdk_drawable_ref (*dest_window);
1351 if (context->source_window)
1352 *protocol = GDK_DRAG_PROTO_LOCAL;
1354 *protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
1357 GDK_NOTE (DND, g_print ("gdk_drag_find_window: %#x +%d+%d Protocol: %d\n",
1358 (drag_window ? (guint) GDK_WINDOW_HWND (drag_window) : 0),
1359 x_root, y_root, *protocol));
1364 gdk_drag_motion (GdkDragContext *context,
1365 GdkWindow *dest_window,
1366 GdkDragProtocol protocol,
1369 GdkDragAction suggested_action,
1370 GdkDragAction possible_actions,
1373 GdkDragContextPrivateWin32 *private;
1375 g_return_val_if_fail (context != NULL, FALSE);
1377 GDK_NOTE (DND, g_print ("gdk_drag_motion\n"));
1379 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1381 if (context->dest_window != dest_window)
1383 GdkEvent temp_event;
1385 /* Send a leave to the last destination */
1386 gdk_drag_do_leave (context, time);
1387 private->drag_status = GDK_DRAG_STATUS_DRAG;
1389 /* Check if new destination accepts drags, and which protocol */
1392 context->dest_window = dest_window;
1393 gdk_window_ref (context->dest_window);
1394 context->protocol = protocol;
1398 case GDK_DRAG_PROTO_LOCAL:
1399 local_send_enter (context, time);
1406 context->suggested_action = suggested_action;
1410 context->dest_window = NULL;
1411 context->action = 0;
1414 /* Push a status event, to let the client know that
1418 temp_event.dnd.type = GDK_DRAG_STATUS;
1419 temp_event.dnd.window = context->source_window;
1420 /* We use this to signal a synthetic status. Perhaps
1421 * we should use an extra field...
1423 temp_event.dnd.send_event = TRUE;
1425 temp_event.dnd.context = context;
1426 temp_event.dnd.time = time;
1428 gdk_event_put (&temp_event);
1432 context->suggested_action = suggested_action;
1435 /* Send a drag-motion event */
1437 private->last_x = x_root;
1438 private->last_y = y_root;
1440 if (context->dest_window)
1442 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
1444 switch (context->protocol)
1446 case GDK_DRAG_PROTO_LOCAL:
1447 local_send_motion (context, x_root, y_root, suggested_action, time);
1450 case GDK_DRAG_PROTO_NONE:
1451 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
1465 gdk_drag_drop (GdkDragContext *context,
1468 g_return_if_fail (context != NULL);
1470 GDK_NOTE (DND, g_print ("gdk_drag_drop\n"));
1472 if (context->dest_window)
1474 switch (context->protocol)
1476 case GDK_DRAG_PROTO_LOCAL:
1477 local_send_drop (context, time);
1480 case GDK_DRAG_PROTO_NONE:
1481 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
1490 gdk_drag_abort (GdkDragContext *context,
1493 g_return_if_fail (context != NULL);
1495 GDK_NOTE (DND, g_print ("gdk_drag_abort\n"));
1497 gdk_drag_do_leave (context, time);
1500 /* Destination side */
1503 gdk_drag_status (GdkDragContext *context,
1504 GdkDragAction action,
1507 GdkDragContextPrivateWin32 *private;
1508 GdkDragContext *src_context;
1511 g_return_if_fail (context != NULL);
1513 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1515 src_context = gdk_drag_context_find (TRUE,
1516 context->source_window,
1517 context->dest_window);
1521 GdkDragContextPrivateWin32 *private = GDK_DRAG_CONTEXT_PRIVATE_DATA (src_context);
1523 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1524 private->drag_status = GDK_DRAG_STATUS_DRAG;
1526 tmp_event.dnd.type = GDK_DRAG_STATUS;
1527 tmp_event.dnd.window = context->source_window;
1528 tmp_event.dnd.send_event = FALSE;
1529 tmp_event.dnd.context = src_context;
1530 gdk_drag_context_ref (src_context);
1532 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1534 if (action == GDK_ACTION_DEFAULT)
1537 src_context->action = action;
1539 gdk_event_put (&tmp_event);
1544 gdk_drop_reply (GdkDragContext *context,
1548 g_return_if_fail (context != NULL);
1550 GDK_NOTE (DND, g_print ("gdk_drop_reply\n"));
1554 gdk_drop_finish (GdkDragContext *context,
1558 GdkDragContextPrivateWin32 *private;
1559 GdkDragContext *src_context;
1562 g_return_if_fail (context != NULL);
1564 GDK_NOTE (DND, g_print ("gdk_drop_finish\n"));
1566 private = GDK_DRAG_CONTEXT_PRIVATE_DATA (context);
1568 src_context = gdk_drag_context_find (TRUE,
1569 context->source_window,
1570 context->dest_window);
1573 tmp_event.dnd.type = GDK_DROP_FINISHED;
1574 tmp_event.dnd.window = src_context->source_window;
1575 tmp_event.dnd.send_event = FALSE;
1576 tmp_event.dnd.context = src_context;
1577 gdk_drag_context_ref (src_context);
1579 gdk_event_put (&tmp_event);
1585 static GdkFilterReturn
1586 gdk_destroy_filter (GdkXEvent *xev,
1590 MSG *msg = (MSG *) xev;
1592 if (msg->message == WM_DESTROY)
1594 IDropTarget *idtp = (IDropTarget *) data;
1596 GDK_NOTE (DND, g_print ("gdk_destroy_filter: WM_DESTROY: %#x\n", msg->hwnd));
1598 idtp->lpVtbl->Release (idtp);
1600 RevokeDragDrop (msg->hwnd);
1601 CoLockObjectExternal (idtp, FALSE, TRUE);
1603 return GDK_FILTER_CONTINUE;
1608 gdk_window_register_dnd (GdkWindow *window)
1611 target_drag_context *ctx;
1615 g_return_if_fail (window != NULL);
1617 if (GPOINTER_TO_INT (gdk_drawable_get_data (window, "gdk-dnd-registered")))
1620 gdk_drawable_set_data (window, "gdk-dnd-registered", GINT_TO_POINTER(TRUE), NULL);
1622 GDK_NOTE (DND, g_print ("gdk_window_register_dnd: %#x\n",
1623 (guint) GDK_WINDOW_HWND (window)));
1625 /* We always claim to accept dropped files, but in fact we might not,
1626 * of course. This function is called in such a way that it cannot know
1627 * whether the window (widget) in question actually accepts files
1628 * (in gtk, data of type text/uri-list) or not.
1630 gdk_window_add_filter (window, gdk_dropfiles_filter, NULL);
1631 DragAcceptFiles (GDK_WINDOW_HWND (window), TRUE);
1634 /* Register for OLE2 d&d */
1635 ctx = target_context_new ();
1636 ctx->context->protocol = GDK_DRAG_PROTO_OLE2;
1637 hres = CoLockObjectExternal ((IUnknown *) &ctx->idt, TRUE, FALSE);
1638 if (!SUCCEEDED (hres))
1639 OTHER_API_FAILED ("CoLockObjectExternal");
1642 hres = RegisterDragDrop (GDK_WINDOW_HWND (window), &ctx->idt);
1643 if (hres == DRAGDROP_E_ALREADYREGISTERED)
1645 g_print ("DRAGDROP_E_ALREADYREGISTERED\n");
1647 ctx->idt.lpVtbl->Release (&ctx->idt);
1649 CoLockObjectExternal ((IUnknown *) &ctx->idt, FALSE, FALSE);
1651 else if (!SUCCEEDED (hres))
1652 OTHER_API_FAILED ("RegisterDragDrop");
1655 gdk_window_add_filter (window, gdk_destroy_filter, &ctx->idt);
1661 /*************************************************************
1662 * gdk_drag_get_selection:
1663 * Returns the selection atom for the current source window
1667 *************************************************************/
1670 gdk_drag_get_selection (GdkDragContext *context)
1672 if (context->protocol == GDK_DRAG_PROTO_LOCAL)
1673 return (GDK_DRAG_CONTEXT_PRIVATE_DATA (context))->local_selection;
1674 else if (context->protocol == GDK_DRAG_PROTO_WIN32_DROPFILES)
1675 return gdk_win32_dropfiles_atom;
1676 else if (context->protocol == GDK_DRAG_PROTO_OLE2)
1677 return gdk_ole2_dnd_atom;