4 * Copyright 2003 Ulrich Czekalla
5 * Copyright 2007 Damjan Jovanovic
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
32 #define NONAMELESSUNION
41 #include "shlobj.h" /* DROPFILES */
45 #include "wine/unicode.h"
46 #include "wine/debug.h"
47 #include "wine/list.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(xdnd);
51 /* Maximum wait time for selection notify */
52 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
53 #define SELECTION_WAIT 1000 /* us */
55 typedef struct tagXDNDDATA
62 } XDNDDATA, *LPXDNDDATA;
64 static struct list xdndData = LIST_INIT(xdndData);
65 static POINT XDNDxy = { 0, 0 };
66 static IDataObject XDNDDataObject;
67 static BOOL XDNDAccepted = FALSE;
68 static DWORD XDNDDropEffect = DROPEFFECT_NONE;
69 /* the last window the mouse was over */
70 static HWND XDNDLastTargetWnd;
71 /* might be a ancestor of XDNDLastTargetWnd */
72 static HWND XDNDLastDropTargetWnd;
74 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len);
75 static int X11DRV_XDND_DeconstructTextURIList(int property, void* data, int len);
76 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len);
77 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len);
78 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len);
79 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
80 Atom *types, unsigned long *count);
81 static void X11DRV_XDND_SendDropFiles(HWND hwnd);
82 static void X11DRV_XDND_FreeDragDropOp(void);
83 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len);
84 static WCHAR* X11DRV_XDND_URIToDOS(char *encodedURI);
86 static CRITICAL_SECTION xdnd_cs;
87 static CRITICAL_SECTION_DEBUG critsect_debug =
90 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
91 0, 0, { (DWORD_PTR)(__FILE__ ": xdnd_cs") }
93 static CRITICAL_SECTION xdnd_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
96 /* Based on functions in dlls/ole32/ole2.c */
97 static HANDLE get_droptarget_local_handle(HWND hwnd)
99 static const WCHAR prop_marshalleddroptarget[] =
100 {'W','i','n','e','M','a','r','s','h','a','l','l','e','d','D','r','o','p','T','a','r','g','e','t',0};
102 HANDLE local_handle = 0;
104 handle = GetPropW(hwnd, prop_marshalleddroptarget);
110 GetWindowThreadProcessId(hwnd, &pid);
111 process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
114 DuplicateHandle(process, handle, GetCurrentProcess(), &local_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
115 CloseHandle(process);
121 static HRESULT create_stream_from_map(HANDLE map, IStream **stream)
123 HRESULT hr = E_OUTOFMEMORY;
126 MEMORY_BASIC_INFORMATION info;
128 data = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
131 VirtualQuery(data, &info, sizeof(info));
132 TRACE("size %d\n", (int)info.RegionSize);
134 hmem = GlobalAlloc(GMEM_MOVEABLE, info.RegionSize);
137 memcpy(GlobalLock(hmem), data, info.RegionSize);
139 hr = CreateStreamOnHGlobal(hmem, TRUE, stream);
141 UnmapViewOfFile(data);
145 static IDropTarget* get_droptarget_pointer(HWND hwnd)
147 IDropTarget *droptarget = NULL;
151 map = get_droptarget_local_handle(hwnd);
152 if(!map) return NULL;
154 if(SUCCEEDED(create_stream_from_map(map, &stream)))
156 CoUnmarshalInterface(stream, &IID_IDropTarget, (void**)&droptarget);
157 IStream_Release(stream);
163 /**************************************************************************
164 * X11DRV_XDND_XdndActionToDROPEFFECT
166 static DWORD X11DRV_XDND_XdndActionToDROPEFFECT(long action)
168 /* In Windows, nothing but the given effects is allowed.
169 * In X the given action is just a hint, and you can always
170 * XdndActionCopy and XdndActionPrivate, so be more permissive. */
171 if (action == x11drv_atom(XdndActionCopy))
172 return DROPEFFECT_COPY;
173 else if (action == x11drv_atom(XdndActionMove))
174 return DROPEFFECT_MOVE | DROPEFFECT_COPY;
175 else if (action == x11drv_atom(XdndActionLink))
176 return DROPEFFECT_LINK | DROPEFFECT_COPY;
177 else if (action == x11drv_atom(XdndActionAsk))
178 /* FIXME: should we somehow ask the user what to do here? */
179 return DROPEFFECT_COPY | DROPEFFECT_MOVE | DROPEFFECT_LINK;
180 FIXME("unknown action %ld, assuming DROPEFFECT_COPY\n", action);
181 return DROPEFFECT_COPY;
184 /**************************************************************************
185 * X11DRV_XDND_DROPEFFECTToXdndAction
187 static long X11DRV_XDND_DROPEFFECTToXdndAction(DWORD effect)
189 if (effect == DROPEFFECT_COPY)
190 return x11drv_atom(XdndActionCopy);
191 else if (effect == DROPEFFECT_MOVE)
192 return x11drv_atom(XdndActionMove);
193 else if (effect == DROPEFFECT_LINK)
194 return x11drv_atom(XdndActionLink);
195 FIXME("unknown drop effect %u, assuming XdndActionCopy\n", effect);
196 return x11drv_atom(XdndActionCopy);
199 /**************************************************************************
200 * X11DRV_XDND_EnterEvent
202 * Handle an XdndEnter event.
204 void X11DRV_XDND_EnterEvent( HWND hWnd, XClientMessageEvent *event )
208 unsigned long count = 0;
210 version = (event->data.l[1] & 0xFF000000) >> 24;
211 TRACE("ver(%d) check-XdndTypeList(%ld) data=%ld,%ld,%ld,%ld,%ld\n",
212 version, (event->data.l[1] & 1),
213 event->data.l[0], event->data.l[1], event->data.l[2],
214 event->data.l[3], event->data.l[4]);
216 if (version > WINE_XDND_VERSION)
218 TRACE("Ignores unsupported version\n");
222 XDNDAccepted = FALSE;
224 /* If the source supports more than 3 data types we retrieve
225 * the entire list. */
226 if (event->data.l[1] & 1)
230 unsigned long bytesret;
232 /* Request supported formats from source window */
234 XGetWindowProperty(event->display, event->data.l[0], x11drv_atom(XdndTypeList),
235 0, 65535, FALSE, AnyPropertyType, &acttype, &actfmt, &count,
236 &bytesret, (unsigned char**)&xdndtypes);
242 xdndtypes = (Atom*) &event->data.l[2];
250 for (; i < count; i++)
252 if (xdndtypes[i] != 0)
254 char * pn = XGetAtomName(event->display, xdndtypes[i]);
255 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes[i], pn);
262 /* Do a one-time data read and cache results */
263 X11DRV_XDND_ResolveProperty(event->display, event->window,
264 event->data.l[1], xdndtypes, &count);
266 if (event->data.l[1] & 1)
270 /**************************************************************************
271 * X11DRV_XDND_PositionEvent
273 * Handle an XdndPosition event.
275 void X11DRV_XDND_PositionEvent( HWND hWnd, XClientMessageEvent *event )
277 XClientMessageEvent e;
278 int accept = 0; /* Assume we're not accepting */
279 IDropTarget *dropTarget = NULL;
285 XDNDxy.x = event->data.l[2] >> 16;
286 XDNDxy.y = event->data.l[2] & 0xFFFF;
287 targetWindow = WindowFromPoint(XDNDxy);
291 effect = X11DRV_XDND_XdndActionToDROPEFFECT(event->data.l[4]);
293 if (!XDNDAccepted || XDNDLastTargetWnd != targetWindow)
295 /* Notify OLE of DragEnter. Result determines if we accept */
296 HWND dropTargetWindow;
298 if (XDNDLastDropTargetWnd)
300 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
303 hr = IDropTarget_DragLeave(dropTarget);
305 WARN("IDropTarget_DragLeave failed, error 0x%08X\n", hr);
306 IDropTarget_Release(dropTarget);
309 dropTargetWindow = targetWindow;
312 dropTarget = get_droptarget_pointer(dropTargetWindow);
313 } while (dropTarget == NULL && (dropTargetWindow = GetParent(dropTargetWindow)) != NULL);
314 XDNDLastTargetWnd = targetWindow;
315 XDNDLastDropTargetWnd = dropTargetWindow;
318 hr = IDropTarget_DragEnter(dropTarget, &XDNDDataObject,
319 MK_LBUTTON, pointl, &effect);
322 if (effect != DROPEFFECT_NONE)
325 TRACE("the application accepted the drop\n");
328 TRACE("the application refused the drop\n");
331 WARN("IDropTarget_DragEnter failed, error 0x%08X\n", hr);
332 IDropTarget_Release(dropTarget);
335 if (XDNDAccepted && XDNDLastTargetWnd == targetWindow)
337 /* If drag accepted notify OLE of DragOver */
338 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
341 hr = IDropTarget_DragOver(dropTarget, MK_LBUTTON, pointl, &effect);
343 XDNDDropEffect = effect;
345 WARN("IDropTarget_DragOver failed, error 0x%08X\n", hr);
346 IDropTarget_Release(dropTarget);
352 if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
355 TRACE("action req: %ld accept(%d) at x(%d),y(%d)\n",
356 event->data.l[4], accept, XDNDxy.x, XDNDxy.y);
359 * Let source know if we're accepting the drop by
360 * sending a status message.
362 e.type = ClientMessage;
363 e.display = event->display;
364 e.window = event->data.l[0];
365 e.message_type = x11drv_atom(XdndStatus);
367 e.data.l[0] = event->window;
368 e.data.l[1] = accept;
369 e.data.l[2] = 0; /* Empty Rect */
370 e.data.l[3] = 0; /* Empty Rect */
372 e.data.l[4] = X11DRV_XDND_DROPEFFECTToXdndAction(effect);
376 XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
380 /**************************************************************************
381 * X11DRV_XDND_DropEvent
383 * Handle an XdndDrop event.
385 void X11DRV_XDND_DropEvent( HWND hWnd, XClientMessageEvent *event )
387 XClientMessageEvent e;
388 IDropTarget *dropTarget;
392 /* If we have a HDROP type we send a WM_ACCEPTFILES.*/
393 if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
394 X11DRV_XDND_SendDropFiles( hWnd );
396 /* Notify OLE of Drop */
397 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
402 DWORD effect = XDNDDropEffect;
406 hr = IDropTarget_Drop(dropTarget, &XDNDDataObject, MK_LBUTTON,
410 if (effect != DROPEFFECT_NONE)
411 TRACE("drop succeeded\n");
413 TRACE("the application refused the drop\n");
416 WARN("drop failed, error 0x%08X\n", hr);
417 IDropTarget_Release(dropTarget);
420 X11DRV_XDND_FreeDragDropOp();
422 /* Tell the target we are finished. */
423 memset(&e, 0, sizeof(e));
424 e.type = ClientMessage;
425 e.display = event->display;
426 e.window = event->data.l[0];
427 e.message_type = x11drv_atom(XdndFinished);
429 e.data.l[0] = event->window;
431 XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
435 /**************************************************************************
436 * X11DRV_XDND_LeaveEvent
438 * Handle an XdndLeave event.
440 void X11DRV_XDND_LeaveEvent( HWND hWnd, XClientMessageEvent *event )
442 IDropTarget *dropTarget;
444 TRACE("DND Operation canceled\n");
446 /* Notify OLE of DragLeave */
447 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
450 HRESULT hr = IDropTarget_DragLeave(dropTarget);
452 WARN("IDropTarget_DragLeave failed, error 0x%08X\n", hr);
453 IDropTarget_Release(dropTarget);
456 X11DRV_XDND_FreeDragDropOp();
460 /**************************************************************************
461 * X11DRV_XDND_ResolveProperty
463 * Resolve all MIME types to windows clipboard formats. All data is cached.
465 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
466 Atom *types, unsigned long *count)
473 unsigned long bytesret, icount;
475 unsigned char* data = NULL;
476 XDNDDATA *current, *next;
477 BOOL haveHDROP = FALSE;
479 TRACE("count(%ld)\n", *count);
481 X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
483 for (i = 0; i < *count; i++)
485 TRACE("requesting atom %ld from xwin %ld\n", types[i], xwin);
491 XConvertSelection(display, x11drv_atom(XdndSelection), types[i],
492 x11drv_atom(XdndTarget), xwin, /*tm*/CurrentTime);
496 * Wait for SelectionNotify
498 for (j = 0; j < SELECTION_RETRIES; j++)
501 res = XCheckTypedWindowEvent(display, xwin, SelectionNotify, &xe);
503 if (res && xe.xselection.selection == x11drv_atom(XdndSelection)) break;
505 usleep(SELECTION_WAIT);
508 if (xe.xselection.property == None)
512 XGetWindowProperty(display, xwin, x11drv_atom(XdndTarget), 0, 65535, FALSE,
513 AnyPropertyType, &acttype, &actfmt, &icount, &bytesret, &data);
516 entries += X11DRV_XDND_MapFormat(types[i], data, get_property_size( actfmt, icount ));
522 /* On Windows when there is a CF_HDROP, there are no other CF_ formats.
523 * foobar2000 relies on this (spaces -> %20's without it).
525 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
527 if (current->cf_win == CF_HDROP)
535 LIST_FOR_EACH_ENTRY_SAFE(current, next, &xdndData, XDNDDATA, entry)
537 if (current->cf_win != CF_HDROP && current->cf_win < CF_MAX)
539 list_remove(¤t->entry);
540 HeapFree(GetProcessHeap(), 0, current->data);
541 HeapFree(GetProcessHeap(), 0, current);
551 /**************************************************************************
552 * X11DRV_XDND_InsertXDNDData
554 * Cache available XDND property
556 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len)
558 LPXDNDDATA current = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XDNDDATA));
562 EnterCriticalSection(&xdnd_cs);
563 current->cf_xdnd = property;
564 current->cf_win = format;
565 current->data = data;
567 list_add_tail(&xdndData, ¤t->entry);
568 LeaveCriticalSection(&xdnd_cs);
573 /**************************************************************************
574 * X11DRV_XDND_MapFormat
576 * Map XDND MIME format to windows clipboard format.
578 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len)
583 TRACE("%d: %s\n", property, data);
585 /* Always include the raw type */
586 xdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
587 memcpy(xdata, data, len);
588 X11DRV_XDND_InsertXDNDData(property, property, xdata, len);
591 if (property == x11drv_atom(text_uri_list))
592 count += X11DRV_XDND_DeconstructTextURIList(property, data, len);
593 else if (property == x11drv_atom(text_plain))
594 count += X11DRV_XDND_DeconstructTextPlain(property, data, len);
595 else if (property == x11drv_atom(text_html))
596 count += X11DRV_XDND_DeconstructTextHTML(property, data, len);
602 /**************************************************************************
603 * X11DRV_XDND_DeconstructTextURIList
605 * Interpret text/uri-list data and add records to <dndfmt> linked list
607 static int X11DRV_XDND_DeconstructTextURIList(int property, void* data, int len)
609 char *uriList = data;
621 out = HeapAlloc(GetProcessHeap(), 0, capacity * sizeof(WCHAR));
627 while (end < len && uriList[end] != '\r')
629 if (end < (len - 1) && uriList[end+1] != '\n')
631 WARN("URI list line doesn't end in \\r\\n\n");
635 uri = HeapAlloc(GetProcessHeap(), 0, end - start + 1);
638 lstrcpynA(uri, &uriList[start], end - start + 1);
639 path = X11DRV_XDND_URIToDOS(uri);
640 TRACE("converted URI %s to DOS path %s\n", debugstr_a(uri), debugstr_w(path));
641 HeapFree(GetProcessHeap(), 0, uri);
645 int pathSize = strlenW(path) + 1;
646 if (pathSize > capacity-size)
648 capacity = 2*capacity + pathSize;
649 out = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, out, (capacity + 1)*sizeof(WCHAR));
653 memcpy(&out[size], path, pathSize * sizeof(WCHAR));
656 HeapFree(GetProcessHeap(), 0, path);
664 if (out && end >= len)
666 DROPFILES *dropFiles;
667 dropFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DROPFILES) + (size + 1)*sizeof(WCHAR));
670 dropFiles->pFiles = sizeof(DROPFILES);
671 dropFiles->pt.x = XDNDxy.x;
672 dropFiles->pt.y = XDNDxy.y;
674 dropFiles->fWide = TRUE;
676 memcpy(((char*)dropFiles) + dropFiles->pFiles, out, (size + 1)*sizeof(WCHAR));
677 X11DRV_XDND_InsertXDNDData(property, CF_HDROP, dropFiles, sizeof(DROPFILES) + (size + 1)*sizeof(WCHAR));
681 HeapFree(GetProcessHeap(), 0, out);
686 /**************************************************************************
687 * X11DRV_XDND_DeconstructTextPlain
689 * Interpret text/plain Data and add records to <dndfmt> linked list
691 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len)
695 /* Always supply plain text */
696 X11DRV_XDND_UnixToDos(&dostext, data, len);
697 X11DRV_XDND_InsertXDNDData(property, CF_TEXT, dostext, strlen(dostext));
699 TRACE("CF_TEXT (%d): %s\n", CF_TEXT, dostext);
705 /**************************************************************************
706 * X11DRV_XDND_DeconstructTextHTML
708 * Interpret text/html data and add records to <dndfmt> linked list
710 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len)
714 X11DRV_XDND_UnixToDos(&dostext, data, len);
716 X11DRV_XDND_InsertXDNDData(property,
717 RegisterClipboardFormatA("UniformResourceLocator"), dostext, strlen(dostext));
719 TRACE("UniformResourceLocator: %s\n", dostext);
725 /**************************************************************************
726 * X11DRV_XDND_SendDropFiles
728 static void X11DRV_XDND_SendDropFiles(HWND hwnd)
730 LPXDNDDATA current = NULL;
733 EnterCriticalSection(&xdnd_cs);
735 /* Find CF_HDROP type if any */
736 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
738 if (current->cf_win == CF_HDROP)
747 DROPFILES *lpDrop = current->data;
751 lpDrop->pt.x = XDNDxy.x;
752 lpDrop->pt.y = XDNDxy.y;
754 TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd,
755 ((char*)lpDrop) + lpDrop->pFiles, debugstr_w((WCHAR*)(((char*)lpDrop) + lpDrop->pFiles)));
757 PostMessageW(hwnd, WM_DROPFILES, (WPARAM)lpDrop, 0L);
761 LeaveCriticalSection(&xdnd_cs);
765 /**************************************************************************
766 * X11DRV_XDND_FreeDragDropOp
768 static void X11DRV_XDND_FreeDragDropOp(void)
775 EnterCriticalSection(&xdnd_cs);
777 /** Free data cache */
778 LIST_FOR_EACH_ENTRY_SAFE(current, next, &xdndData, XDNDDATA, entry)
780 list_remove(¤t->entry);
781 HeapFree(GetProcessHeap(), 0, current);
784 XDNDxy.x = XDNDxy.y = 0;
785 XDNDLastTargetWnd = NULL;
786 XDNDLastDropTargetWnd = NULL;
787 XDNDAccepted = FALSE;
789 LeaveCriticalSection(&xdnd_cs);
794 /**************************************************************************
795 * X11DRV_XDND_UnixToDos
797 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len)
800 unsigned int destlen, lines;
802 for (i = 0, lines = 0; i <= len; i++)
804 if (lpsrc[i] == '\n')
808 destlen = len + lines + 1;
812 char* lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, destlen);
813 for (i = 0, lines = 0; i <= len; i++)
815 if (lpsrc[i] == '\n')
816 lpstr[++lines + i] = '\r';
817 lpstr[lines + i] = lpsrc[i];
827 /**************************************************************************
828 * X11DRV_XDND_URIToDOS
830 static WCHAR* X11DRV_XDND_URIToDOS(char *encodedURI)
835 char *uri = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlen(encodedURI) + 1);
838 for (i = 0; encodedURI[i]; ++i)
840 if (encodedURI[i] == '%')
842 if (encodedURI[i+1] && encodedURI[i+2])
846 buffer[0] = encodedURI[i+1];
847 buffer[1] = encodedURI[i+2];
849 sscanf(buffer, "%x", &number);
855 WARN("invalid URI encoding in %s\n", debugstr_a(encodedURI));
856 HeapFree(GetProcessHeap(), 0, uri);
861 uri[j++] = encodedURI[i];
864 /* Read http://www.freedesktop.org/wiki/Draganddropwarts and cry... */
865 if (strncmp(uri, "file:/", 6) == 0)
871 /* file:///path/to/file (nautilus, thunar) */
872 ret = wine_get_dos_file_name(&uri[7]);
876 /* file://hostname/path/to/file (X file drag spec) */
878 char *path = strchr(&uri[7], '/');
882 if (strcmp(&uri[7], "localhost") == 0)
885 ret = wine_get_dos_file_name(path);
887 else if (gethostname(hostname, sizeof(hostname)) == 0)
889 if (strcmp(hostname, &uri[7]) == 0)
892 ret = wine_get_dos_file_name(path);
900 /* file:/path/to/file (konqueror) */
901 ret = wine_get_dos_file_name(&uri[5]);
904 HeapFree(GetProcessHeap(), 0, uri);
909 /**************************************************************************
910 * X11DRV_XDND_DescribeClipboardFormat
912 static void X11DRV_XDND_DescribeClipboardFormat(int cfFormat, char *buffer, int size)
914 #define D(x) case x: lstrcpynA(buffer, #x, size); return;
937 if (CF_PRIVATEFIRST <= cfFormat && cfFormat <= CF_PRIVATELAST)
939 lstrcpynA(buffer, "some private object", size);
942 if (CF_GDIOBJFIRST <= cfFormat && cfFormat <= CF_GDIOBJLAST)
944 lstrcpynA(buffer, "some GDI object", size);
948 GetClipboardFormatNameA(cfFormat, buffer, size);
952 /* The IDataObject singleton we feed to OLE follows */
954 static HRESULT WINAPI XDNDDATAOBJECT_QueryInterface(IDataObject *dataObject,
955 REFIID riid, void **ppvObject)
957 TRACE("(%p, %s, %p)\n", dataObject, debugstr_guid(riid), ppvObject);
958 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDataObject))
960 *ppvObject = dataObject;
961 IDataObject_AddRef(dataObject);
965 return E_NOINTERFACE;
968 static ULONG WINAPI XDNDDATAOBJECT_AddRef(IDataObject *dataObject)
970 TRACE("(%p)\n", dataObject);
974 static ULONG WINAPI XDNDDATAOBJECT_Release(IDataObject *dataObject)
976 TRACE("(%p)\n", dataObject);
980 static HRESULT WINAPI XDNDDATAOBJECT_GetData(IDataObject *dataObject,
981 FORMATETC *formatEtc,
985 char formatDesc[1024];
987 TRACE("(%p, %p, %p)\n", dataObject, formatEtc, pMedium);
988 X11DRV_XDND_DescribeClipboardFormat(formatEtc->cfFormat,
989 formatDesc, sizeof(formatDesc));
990 TRACE("application is looking for %s\n", formatDesc);
992 hr = IDataObject_QueryGetData(dataObject, formatEtc);
996 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
998 if (current->cf_win == formatEtc->cfFormat)
1000 pMedium->tymed = TYMED_HGLOBAL;
1001 pMedium->u.hGlobal = HeapAlloc(GetProcessHeap(), 0, current->size);
1002 if (pMedium->u.hGlobal == NULL)
1003 return E_OUTOFMEMORY;
1004 memcpy(pMedium->u.hGlobal, current->data, current->size);
1005 pMedium->pUnkForRelease = 0;
1013 static HRESULT WINAPI XDNDDATAOBJECT_GetDataHere(IDataObject *dataObject,
1014 FORMATETC *formatEtc,
1017 FIXME("(%p, %p, %p): stub\n", dataObject, formatEtc, pMedium);
1018 return DATA_E_FORMATETC;
1021 static HRESULT WINAPI XDNDDATAOBJECT_QueryGetData(IDataObject *dataObject,
1022 FORMATETC *formatEtc)
1024 char formatDesc[1024];
1027 TRACE("(%p, %p={.tymed=0x%x, .dwAspect=%d, .cfFormat=%d}\n",
1028 dataObject, formatEtc, formatEtc->tymed, formatEtc->dwAspect, formatEtc->cfFormat);
1029 X11DRV_XDND_DescribeClipboardFormat(formatEtc->cfFormat, formatDesc, sizeof(formatDesc));
1031 if (formatEtc->tymed && !(formatEtc->tymed & TYMED_HGLOBAL))
1033 FIXME("only HGLOBAL medium types supported right now\n");
1036 if (formatEtc->dwAspect != DVASPECT_CONTENT)
1038 FIXME("only the content aspect is supported right now\n");
1042 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
1044 if (current->cf_win == formatEtc->cfFormat)
1046 TRACE("application found %s\n", formatDesc);
1050 TRACE("application didn't find %s\n", formatDesc);
1051 return DV_E_FORMATETC;
1054 static HRESULT WINAPI XDNDDATAOBJECT_GetCanonicalFormatEtc(IDataObject *dataObject,
1055 FORMATETC *formatEtc,
1056 FORMATETC *formatEtcOut)
1058 FIXME("(%p, %p, %p): stub\n", dataObject, formatEtc, formatEtcOut);
1059 formatEtcOut->ptd = NULL;
1063 static HRESULT WINAPI XDNDDATAOBJECT_SetData(IDataObject *dataObject,
1064 FORMATETC *formatEtc,
1065 STGMEDIUM *pMedium, BOOL fRelease)
1067 FIXME("(%p, %p, %p, %s): stub\n", dataObject, formatEtc,
1068 pMedium, fRelease?"TRUE":"FALSE");
1072 static HRESULT WINAPI XDNDDATAOBJECT_EnumFormatEtc(IDataObject *dataObject,
1074 IEnumFORMATETC **ppEnumFormatEtc)
1079 TRACE("(%p, %u, %p)\n", dataObject, dwDirection, ppEnumFormatEtc);
1081 if (dwDirection != DATADIR_GET)
1083 FIXME("only the get direction is implemented\n");
1087 count = list_count(&xdndData);
1088 formats = HeapAlloc(GetProcessHeap(), 0, count * sizeof(FORMATETC));
1094 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
1096 formats[i].cfFormat = current->cf_win;
1097 formats[i].ptd = NULL;
1098 formats[i].dwAspect = DVASPECT_CONTENT;
1099 formats[i].lindex = -1;
1100 formats[i].tymed = TYMED_HGLOBAL;
1103 hr = SHCreateStdEnumFmtEtc(count, formats, ppEnumFormatEtc);
1104 HeapFree(GetProcessHeap(), 0, formats);
1108 return E_OUTOFMEMORY;
1111 static HRESULT WINAPI XDNDDATAOBJECT_DAdvise(IDataObject *dataObject,
1112 FORMATETC *formatEtc, DWORD advf,
1113 IAdviseSink *adviseSink,
1114 DWORD *pdwConnection)
1116 FIXME("(%p, %p, %u, %p, %p): stub\n", dataObject, formatEtc, advf,
1117 adviseSink, pdwConnection);
1118 return OLE_E_ADVISENOTSUPPORTED;
1121 static HRESULT WINAPI XDNDDATAOBJECT_DUnadvise(IDataObject *dataObject,
1124 FIXME("(%p, %u): stub\n", dataObject, dwConnection);
1125 return OLE_E_ADVISENOTSUPPORTED;
1128 static HRESULT WINAPI XDNDDATAOBJECT_EnumDAdvise(IDataObject *dataObject,
1129 IEnumSTATDATA **pEnumAdvise)
1131 FIXME("(%p, %p): stub\n", dataObject, pEnumAdvise);
1132 return OLE_E_ADVISENOTSUPPORTED;
1135 static IDataObjectVtbl xdndDataObjectVtbl =
1137 XDNDDATAOBJECT_QueryInterface,
1138 XDNDDATAOBJECT_AddRef,
1139 XDNDDATAOBJECT_Release,
1140 XDNDDATAOBJECT_GetData,
1141 XDNDDATAOBJECT_GetDataHere,
1142 XDNDDATAOBJECT_QueryGetData,
1143 XDNDDATAOBJECT_GetCanonicalFormatEtc,
1144 XDNDDATAOBJECT_SetData,
1145 XDNDDATAOBJECT_EnumFormatEtc,
1146 XDNDDATAOBJECT_DAdvise,
1147 XDNDDATAOBJECT_DUnadvise,
1148 XDNDDATAOBJECT_EnumDAdvise
1151 static IDataObject XDNDDataObject = { &xdndDataObjectVtbl };