ole32: Call CoCreateInstanceEx from CoCreateInstance instead of the other way around.
[wine.git] / dlls / winex11.drv / xdnd.c
blob72758a06bd6f4bd42a8f8df4961b117c0f1a5c7c
1 /*
2 * XDND handler code
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <stdarg.h>
30 #include <stdio.h>
32 #define NONAMELESSUNION
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
39 #define COBJMACROS
40 #include "x11drv.h"
41 #include "shellapi.h"
42 #include "shlobj.h" /* DROPFILES */
43 #include "oleidl.h"
44 #include "objidl.h"
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
48 #include "wine/list.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(xdnd);
52 /* Maximum wait time for selection notify */
53 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
54 #define SELECTION_WAIT 1000 /* us */
56 typedef struct tagXDNDDATA
58 int cf_win;
59 Atom cf_xdnd;
60 HANDLE contents;
61 struct list entry;
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 an ancestor of XDNDLastTargetWnd */
72 static HWND XDNDLastDropTargetWnd;
74 static void X11DRV_XDND_InsertXDNDData(int property, int format, HANDLE contents);
75 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
76 Atom *types, unsigned long count);
77 static BOOL X11DRV_XDND_HasHDROP(void);
78 static HRESULT X11DRV_XDND_SendDropFiles(HWND hwnd);
79 static void X11DRV_XDND_FreeDragDropOp(void);
81 static CRITICAL_SECTION xdnd_cs;
82 static CRITICAL_SECTION_DEBUG critsect_debug =
84 0, 0, &xdnd_cs,
85 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
86 0, 0, { (DWORD_PTR)(__FILE__ ": xdnd_cs") }
88 static CRITICAL_SECTION xdnd_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
91 /* Based on functions in dlls/ole32/ole2.c */
92 static HANDLE get_droptarget_local_handle(HWND hwnd)
94 static const WCHAR prop_marshalleddroptarget[] =
95 {'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};
96 HANDLE handle;
97 HANDLE local_handle = 0;
99 handle = GetPropW(hwnd, prop_marshalleddroptarget);
100 if (handle)
102 DWORD pid;
103 HANDLE process;
105 GetWindowThreadProcessId(hwnd, &pid);
106 process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
107 if (process)
109 DuplicateHandle(process, handle, GetCurrentProcess(), &local_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
110 CloseHandle(process);
113 return local_handle;
116 static HRESULT create_stream_from_map(HANDLE map, IStream **stream)
118 HRESULT hr = E_OUTOFMEMORY;
119 HGLOBAL hmem;
120 void *data;
121 MEMORY_BASIC_INFORMATION info;
123 data = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
124 if(!data) return hr;
126 VirtualQuery(data, &info, sizeof(info));
127 TRACE("size %d\n", (int)info.RegionSize);
129 hmem = GlobalAlloc(GMEM_MOVEABLE, info.RegionSize);
130 if(hmem)
132 memcpy(GlobalLock(hmem), data, info.RegionSize);
133 GlobalUnlock(hmem);
134 hr = CreateStreamOnHGlobal(hmem, TRUE, stream);
136 UnmapViewOfFile(data);
137 return hr;
140 static IDropTarget* get_droptarget_pointer(HWND hwnd)
142 IDropTarget *droptarget = NULL;
143 HANDLE map;
144 IStream *stream;
146 map = get_droptarget_local_handle(hwnd);
147 if(!map) return NULL;
149 if(SUCCEEDED(create_stream_from_map(map, &stream)))
151 CoUnmarshalInterface(stream, &IID_IDropTarget, (void**)&droptarget);
152 IStream_Release(stream);
154 CloseHandle(map);
155 return droptarget;
158 /**************************************************************************
159 * X11DRV_XDND_XdndActionToDROPEFFECT
161 static DWORD X11DRV_XDND_XdndActionToDROPEFFECT(long action)
163 /* In Windows, nothing but the given effects is allowed.
164 * In X the given action is just a hint, and you can always
165 * XdndActionCopy and XdndActionPrivate, so be more permissive. */
166 if (action == x11drv_atom(XdndActionCopy))
167 return DROPEFFECT_COPY;
168 else if (action == x11drv_atom(XdndActionMove))
169 return DROPEFFECT_MOVE | DROPEFFECT_COPY;
170 else if (action == x11drv_atom(XdndActionLink))
171 return DROPEFFECT_LINK | DROPEFFECT_COPY;
172 else if (action == x11drv_atom(XdndActionAsk))
173 /* FIXME: should we somehow ask the user what to do here? */
174 return DROPEFFECT_COPY | DROPEFFECT_MOVE | DROPEFFECT_LINK;
175 FIXME("unknown action %ld, assuming DROPEFFECT_COPY\n", action);
176 return DROPEFFECT_COPY;
179 /**************************************************************************
180 * X11DRV_XDND_DROPEFFECTToXdndAction
182 static long X11DRV_XDND_DROPEFFECTToXdndAction(DWORD effect)
184 if (effect == DROPEFFECT_COPY)
185 return x11drv_atom(XdndActionCopy);
186 else if (effect == DROPEFFECT_MOVE)
187 return x11drv_atom(XdndActionMove);
188 else if (effect == DROPEFFECT_LINK)
189 return x11drv_atom(XdndActionLink);
190 FIXME("unknown drop effect %u, assuming XdndActionCopy\n", effect);
191 return x11drv_atom(XdndActionCopy);
194 /**************************************************************************
195 * X11DRV_XDND_EnterEvent
197 * Handle an XdndEnter event.
199 void X11DRV_XDND_EnterEvent( HWND hWnd, XClientMessageEvent *event )
201 int version;
202 Atom *xdndtypes;
203 unsigned long count = 0;
205 version = (event->data.l[1] & 0xFF000000) >> 24;
206 TRACE("ver(%d) check-XdndTypeList(%ld) data=%ld,%ld,%ld,%ld,%ld\n",
207 version, (event->data.l[1] & 1),
208 event->data.l[0], event->data.l[1], event->data.l[2],
209 event->data.l[3], event->data.l[4]);
211 if (version > WINE_XDND_VERSION)
213 TRACE("Ignores unsupported version\n");
214 return;
217 XDNDAccepted = FALSE;
219 /* If the source supports more than 3 data types we retrieve
220 * the entire list. */
221 if (event->data.l[1] & 1)
223 Atom acttype;
224 int actfmt;
225 unsigned long bytesret;
227 /* Request supported formats from source window */
228 XGetWindowProperty(event->display, event->data.l[0], x11drv_atom(XdndTypeList),
229 0, 65535, FALSE, AnyPropertyType, &acttype, &actfmt, &count,
230 &bytesret, (unsigned char**)&xdndtypes);
232 else
234 count = 3;
235 xdndtypes = (Atom*) &event->data.l[2];
238 if (TRACE_ON(xdnd))
240 unsigned int i;
242 for (i = 0; i < count; i++)
244 if (xdndtypes[i] != 0)
246 char * pn = XGetAtomName(event->display, xdndtypes[i]);
247 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes[i], pn);
248 XFree(pn);
253 /* Do a one-time data read and cache results */
254 X11DRV_XDND_ResolveProperty(event->display, event->window,
255 event->data.l[1], xdndtypes, count);
257 if (event->data.l[1] & 1)
258 XFree(xdndtypes);
261 /**************************************************************************
262 * X11DRV_XDND_PositionEvent
264 * Handle an XdndPosition event.
266 void X11DRV_XDND_PositionEvent( HWND hWnd, XClientMessageEvent *event )
268 XClientMessageEvent e;
269 int accept = 0; /* Assume we're not accepting */
270 IDropTarget *dropTarget = NULL;
271 DWORD effect;
272 POINTL pointl;
273 HWND targetWindow;
274 HRESULT hr;
276 XDNDxy = root_to_virtual_screen( event->data.l[2] >> 16, event->data.l[2] & 0xFFFF );
277 targetWindow = WindowFromPoint(XDNDxy);
279 pointl.x = XDNDxy.x;
280 pointl.y = XDNDxy.y;
281 effect = X11DRV_XDND_XdndActionToDROPEFFECT(event->data.l[4]);
283 if (!XDNDAccepted || XDNDLastTargetWnd != targetWindow)
285 /* Notify OLE of DragEnter. Result determines if we accept */
286 HWND dropTargetWindow;
288 if (XDNDLastDropTargetWnd)
290 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
291 if (dropTarget)
293 hr = IDropTarget_DragLeave(dropTarget);
294 if (FAILED(hr))
295 WARN("IDropTarget_DragLeave failed, error 0x%08X\n", hr);
296 IDropTarget_Release(dropTarget);
299 dropTargetWindow = targetWindow;
302 dropTarget = get_droptarget_pointer(dropTargetWindow);
303 } while (dropTarget == NULL && (dropTargetWindow = GetParent(dropTargetWindow)) != NULL);
304 XDNDLastTargetWnd = targetWindow;
305 XDNDLastDropTargetWnd = dropTargetWindow;
306 if (dropTarget)
308 hr = IDropTarget_DragEnter(dropTarget, &XDNDDataObject,
309 MK_LBUTTON, pointl, &effect);
310 if (SUCCEEDED(hr))
312 if (effect != DROPEFFECT_NONE)
314 XDNDAccepted = TRUE;
315 TRACE("the application accepted the drop\n");
317 else
318 TRACE("the application refused the drop\n");
320 else
321 WARN("IDropTarget_DragEnter failed, error 0x%08X\n", hr);
322 IDropTarget_Release(dropTarget);
325 if (XDNDAccepted && XDNDLastTargetWnd == targetWindow)
327 /* If drag accepted notify OLE of DragOver */
328 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
329 if (dropTarget)
331 hr = IDropTarget_DragOver(dropTarget, MK_LBUTTON, pointl, &effect);
332 if (SUCCEEDED(hr))
333 XDNDDropEffect = effect;
334 else
335 WARN("IDropTarget_DragOver failed, error 0x%08X\n", hr);
336 IDropTarget_Release(dropTarget);
340 if (XDNDAccepted)
341 accept = 1;
342 else if (dropTarget == NULL &&
343 (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES) &&
344 (effect & DROPEFFECT_COPY) &&
345 X11DRV_XDND_HasHDROP())
347 accept = 1;
348 effect = DROPEFFECT_COPY;
349 XDNDDropEffect = effect;
352 TRACE("action req: %ld accept(%d) at x(%d),y(%d)\n",
353 event->data.l[4], accept, XDNDxy.x, XDNDxy.y);
356 * Let source know if we're accepting the drop by
357 * sending a status message.
359 e.type = ClientMessage;
360 e.display = event->display;
361 e.window = event->data.l[0];
362 e.message_type = x11drv_atom(XdndStatus);
363 e.format = 32;
364 e.data.l[0] = event->window;
365 e.data.l[1] = accept;
366 e.data.l[2] = 0; /* Empty Rect */
367 e.data.l[3] = 0; /* Empty Rect */
368 if (accept)
369 e.data.l[4] = X11DRV_XDND_DROPEFFECTToXdndAction(effect);
370 else
371 e.data.l[4] = None;
372 XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
375 /**************************************************************************
376 * X11DRV_XDND_DropEvent
378 * Handle an XdndDrop event.
380 void X11DRV_XDND_DropEvent( HWND hWnd, XClientMessageEvent *event )
382 XClientMessageEvent e;
383 IDropTarget *dropTarget;
384 DWORD effect = XDNDDropEffect;
385 int accept = 0; /* Assume we're not accepting */
387 TRACE("\n");
389 /* Notify OLE of Drop */
390 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
391 if (dropTarget)
393 HRESULT hr;
394 POINTL pointl;
396 pointl.x = XDNDxy.x;
397 pointl.y = XDNDxy.y;
398 hr = IDropTarget_Drop(dropTarget, &XDNDDataObject, MK_LBUTTON,
399 pointl, &effect);
400 if (SUCCEEDED(hr))
402 if (effect != DROPEFFECT_NONE)
404 TRACE("drop succeeded\n");
405 accept = 1;
407 else
408 TRACE("the application refused the drop\n");
410 else
411 WARN("drop failed, error 0x%08X\n", hr);
412 IDropTarget_Release(dropTarget);
414 else
416 /* Only send WM_DROPFILES if there is no drop target. Doing both
417 * causes winamp to duplicate the dropped files (#29081) */
418 if ((GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES) &&
419 (effect & DROPEFFECT_COPY) &&
420 X11DRV_XDND_HasHDROP())
422 HRESULT hr = X11DRV_XDND_SendDropFiles( hWnd );
423 if (SUCCEEDED(hr))
425 accept = 1;
426 effect = DROPEFFECT_COPY;
431 X11DRV_XDND_FreeDragDropOp();
433 /* Tell the target we are finished. */
434 memset(&e, 0, sizeof(e));
435 e.type = ClientMessage;
436 e.display = event->display;
437 e.window = event->data.l[0];
438 e.message_type = x11drv_atom(XdndFinished);
439 e.format = 32;
440 e.data.l[0] = event->window;
441 e.data.l[1] = accept;
442 if (accept)
443 e.data.l[2] = X11DRV_XDND_DROPEFFECTToXdndAction(effect);
444 else
445 e.data.l[2] = None;
446 XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
449 /**************************************************************************
450 * X11DRV_XDND_LeaveEvent
452 * Handle an XdndLeave event.
454 void X11DRV_XDND_LeaveEvent( HWND hWnd, XClientMessageEvent *event )
456 IDropTarget *dropTarget;
458 TRACE("DND Operation canceled\n");
460 /* Notify OLE of DragLeave */
461 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
462 if (dropTarget)
464 HRESULT hr = IDropTarget_DragLeave(dropTarget);
465 if (FAILED(hr))
466 WARN("IDropTarget_DragLeave failed, error 0x%08X\n", hr);
467 IDropTarget_Release(dropTarget);
470 X11DRV_XDND_FreeDragDropOp();
474 /**************************************************************************
475 * X11DRV_XDND_ResolveProperty
477 * Resolve all MIME types to windows clipboard formats. All data is cached.
479 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
480 Atom *types, unsigned long count)
482 unsigned int i, j;
483 BOOL res;
484 XEvent xe;
485 XDNDDATA *current, *next;
486 BOOL haveHDROP = FALSE;
488 TRACE("count(%ld)\n", count);
490 X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
492 for (i = 0; i < count; i++)
494 HANDLE contents;
495 UINT windowsFormat;
497 TRACE("requesting atom %ld from xwin %ld\n", types[i], xwin);
499 if (types[i] == 0)
500 continue;
502 XConvertSelection(display, x11drv_atom(XdndSelection), types[i],
503 x11drv_atom(XdndTarget), xwin, /*tm*/CurrentTime);
506 * Wait for SelectionNotify
508 for (j = 0; j < SELECTION_RETRIES; j++)
510 res = XCheckTypedWindowEvent(display, xwin, SelectionNotify, &xe);
511 if (res && xe.xselection.selection == x11drv_atom(XdndSelection)) break;
513 usleep(SELECTION_WAIT);
516 if (xe.xselection.property == None)
517 continue;
519 contents = X11DRV_CLIPBOARD_ImportSelection(display, types[i], xwin, x11drv_atom(XdndTarget), &windowsFormat);
520 if (contents)
521 X11DRV_XDND_InsertXDNDData(types[i], windowsFormat, contents);
524 /* On Windows when there is a CF_HDROP, there are no other CF_ formats.
525 * foobar2000 relies on this (spaces -> %20's without it).
527 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
529 if (current->cf_win == CF_HDROP)
531 haveHDROP = TRUE;
532 break;
535 if (haveHDROP)
537 LIST_FOR_EACH_ENTRY_SAFE(current, next, &xdndData, XDNDDATA, entry)
539 if (current->cf_win != CF_HDROP && current->cf_win < CF_MAX)
541 list_remove(&current->entry);
542 GlobalFree(current->contents);
543 HeapFree(GetProcessHeap(), 0, current);
550 /**************************************************************************
551 * X11DRV_XDND_InsertXDNDData
553 * Cache available XDND property
555 static void X11DRV_XDND_InsertXDNDData(int property, int format, HANDLE contents)
557 LPXDNDDATA current = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XDNDDATA));
559 if (current)
561 EnterCriticalSection(&xdnd_cs);
562 current->cf_xdnd = property;
563 current->cf_win = format;
564 current->contents = contents;
565 list_add_tail(&xdndData, &current->entry);
566 LeaveCriticalSection(&xdnd_cs);
571 /**************************************************************************
572 * X11DRV_XDND_HasHDROP
574 static BOOL X11DRV_XDND_HasHDROP(void)
576 LPXDNDDATA current = NULL;
577 BOOL found = FALSE;
579 EnterCriticalSection(&xdnd_cs);
581 /* Find CF_HDROP type if any */
582 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
584 if (current->cf_win == CF_HDROP)
586 found = TRUE;
587 break;
591 LeaveCriticalSection(&xdnd_cs);
593 return found;
596 /**************************************************************************
597 * X11DRV_XDND_SendDropFiles
599 static HRESULT X11DRV_XDND_SendDropFiles(HWND hwnd)
601 HRESULT hr;
602 LPXDNDDATA current = NULL;
603 BOOL found = FALSE;
605 EnterCriticalSection(&xdnd_cs);
607 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
609 if (current->cf_win == CF_HDROP)
611 found = TRUE;
612 break;
615 if (found)
617 HGLOBAL dropHandle = GlobalAlloc(GMEM_FIXED, GlobalSize(current->contents));
618 if (dropHandle)
620 DROPFILES *lpDrop = GlobalLock(dropHandle);
621 memcpy(lpDrop, GlobalLock(current->contents), GlobalSize(current->contents));
622 GlobalUnlock(current->contents);
623 lpDrop->pt.x = XDNDxy.x;
624 lpDrop->pt.y = XDNDxy.y;
625 lpDrop->fNC = !ScreenToClient(hwnd, &lpDrop->pt);
626 TRACE("Sending WM_DROPFILES: hWnd=0x%p, fNC=%d, x=%d, y=%d, files=%p(%s)\n", hwnd,
627 lpDrop->fNC, lpDrop->pt.x, lpDrop->pt.y, ((char*)lpDrop) + lpDrop->pFiles,
628 debugstr_w((WCHAR*)(((char*)lpDrop) + lpDrop->pFiles)));
629 GlobalUnlock(dropHandle);
630 if (PostMessageW(hwnd, WM_DROPFILES, (WPARAM)dropHandle, 0))
631 hr = S_OK;
632 else
634 hr = HRESULT_FROM_WIN32(GetLastError());
635 GlobalFree(dropHandle);
638 else
639 hr = HRESULT_FROM_WIN32(GetLastError());
641 else
642 hr = E_FAIL;
644 LeaveCriticalSection(&xdnd_cs);
646 return hr;
650 /**************************************************************************
651 * X11DRV_XDND_FreeDragDropOp
653 static void X11DRV_XDND_FreeDragDropOp(void)
655 LPXDNDDATA next;
656 LPXDNDDATA current;
658 TRACE("\n");
660 EnterCriticalSection(&xdnd_cs);
662 /** Free data cache */
663 LIST_FOR_EACH_ENTRY_SAFE(current, next, &xdndData, XDNDDATA, entry)
665 list_remove(&current->entry);
666 GlobalFree(current->contents);
667 HeapFree(GetProcessHeap(), 0, current);
670 XDNDxy.x = XDNDxy.y = 0;
671 XDNDLastTargetWnd = NULL;
672 XDNDLastDropTargetWnd = NULL;
673 XDNDAccepted = FALSE;
675 LeaveCriticalSection(&xdnd_cs);
679 /**************************************************************************
680 * X11DRV_XDND_DescribeClipboardFormat
682 static void X11DRV_XDND_DescribeClipboardFormat(int cfFormat, char *buffer, int size)
684 #define D(x) case x: lstrcpynA(buffer, #x, size); return;
685 switch (cfFormat)
687 D(CF_TEXT)
688 D(CF_BITMAP)
689 D(CF_METAFILEPICT)
690 D(CF_SYLK)
691 D(CF_DIF)
692 D(CF_TIFF)
693 D(CF_OEMTEXT)
694 D(CF_DIB)
695 D(CF_PALETTE)
696 D(CF_PENDATA)
697 D(CF_RIFF)
698 D(CF_WAVE)
699 D(CF_UNICODETEXT)
700 D(CF_ENHMETAFILE)
701 D(CF_HDROP)
702 D(CF_LOCALE)
703 D(CF_DIBV5)
705 #undef D
707 if (CF_PRIVATEFIRST <= cfFormat && cfFormat <= CF_PRIVATELAST)
709 lstrcpynA(buffer, "some private object", size);
710 return;
712 if (CF_GDIOBJFIRST <= cfFormat && cfFormat <= CF_GDIOBJLAST)
714 lstrcpynA(buffer, "some GDI object", size);
715 return;
718 GetClipboardFormatNameA(cfFormat, buffer, size);
722 /* The IDataObject singleton we feed to OLE follows */
724 static HRESULT WINAPI XDNDDATAOBJECT_QueryInterface(IDataObject *dataObject,
725 REFIID riid, void **ppvObject)
727 TRACE("(%p, %s, %p)\n", dataObject, debugstr_guid(riid), ppvObject);
728 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDataObject))
730 *ppvObject = dataObject;
731 IDataObject_AddRef(dataObject);
732 return S_OK;
734 *ppvObject = NULL;
735 return E_NOINTERFACE;
738 static ULONG WINAPI XDNDDATAOBJECT_AddRef(IDataObject *dataObject)
740 TRACE("(%p)\n", dataObject);
741 return 2;
744 static ULONG WINAPI XDNDDATAOBJECT_Release(IDataObject *dataObject)
746 TRACE("(%p)\n", dataObject);
747 return 1;
750 static HRESULT WINAPI XDNDDATAOBJECT_GetData(IDataObject *dataObject,
751 FORMATETC *formatEtc,
752 STGMEDIUM *pMedium)
754 HRESULT hr;
755 char formatDesc[1024];
757 TRACE("(%p, %p, %p)\n", dataObject, formatEtc, pMedium);
758 X11DRV_XDND_DescribeClipboardFormat(formatEtc->cfFormat,
759 formatDesc, sizeof(formatDesc));
760 TRACE("application is looking for %s\n", formatDesc);
762 hr = IDataObject_QueryGetData(dataObject, formatEtc);
763 if (SUCCEEDED(hr))
765 XDNDDATA *current;
766 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
768 if (current->cf_win == formatEtc->cfFormat)
770 pMedium->tymed = TYMED_HGLOBAL;
771 pMedium->u.hGlobal = GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, GlobalSize(current->contents));
772 if (pMedium->u.hGlobal == NULL)
773 return E_OUTOFMEMORY;
774 memcpy(GlobalLock(pMedium->u.hGlobal), GlobalLock(current->contents), GlobalSize(current->contents));
775 GlobalUnlock(pMedium->u.hGlobal);
776 GlobalUnlock(current->contents);
777 pMedium->pUnkForRelease = 0;
778 return S_OK;
782 return hr;
785 static HRESULT WINAPI XDNDDATAOBJECT_GetDataHere(IDataObject *dataObject,
786 FORMATETC *formatEtc,
787 STGMEDIUM *pMedium)
789 FIXME("(%p, %p, %p): stub\n", dataObject, formatEtc, pMedium);
790 return DATA_E_FORMATETC;
793 static HRESULT WINAPI XDNDDATAOBJECT_QueryGetData(IDataObject *dataObject,
794 FORMATETC *formatEtc)
796 char formatDesc[1024];
797 XDNDDATA *current;
799 TRACE("(%p, %p={.tymed=0x%x, .dwAspect=%d, .cfFormat=%d}\n",
800 dataObject, formatEtc, formatEtc->tymed, formatEtc->dwAspect, formatEtc->cfFormat);
801 X11DRV_XDND_DescribeClipboardFormat(formatEtc->cfFormat, formatDesc, sizeof(formatDesc));
803 if (formatEtc->tymed && !(formatEtc->tymed & TYMED_HGLOBAL))
805 FIXME("only HGLOBAL medium types supported right now\n");
806 return DV_E_TYMED;
808 if (formatEtc->dwAspect != DVASPECT_CONTENT)
810 FIXME("only the content aspect is supported right now\n");
811 return E_NOTIMPL;
814 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
816 if (current->cf_win == formatEtc->cfFormat)
818 TRACE("application found %s\n", formatDesc);
819 return S_OK;
822 TRACE("application didn't find %s\n", formatDesc);
823 return DV_E_FORMATETC;
826 static HRESULT WINAPI XDNDDATAOBJECT_GetCanonicalFormatEtc(IDataObject *dataObject,
827 FORMATETC *formatEtc,
828 FORMATETC *formatEtcOut)
830 FIXME("(%p, %p, %p): stub\n", dataObject, formatEtc, formatEtcOut);
831 formatEtcOut->ptd = NULL;
832 return E_NOTIMPL;
835 static HRESULT WINAPI XDNDDATAOBJECT_SetData(IDataObject *dataObject,
836 FORMATETC *formatEtc,
837 STGMEDIUM *pMedium, BOOL fRelease)
839 FIXME("(%p, %p, %p, %s): stub\n", dataObject, formatEtc,
840 pMedium, fRelease?"TRUE":"FALSE");
841 return E_NOTIMPL;
844 static HRESULT WINAPI XDNDDATAOBJECT_EnumFormatEtc(IDataObject *dataObject,
845 DWORD dwDirection,
846 IEnumFORMATETC **ppEnumFormatEtc)
848 DWORD count;
849 FORMATETC *formats;
851 TRACE("(%p, %u, %p)\n", dataObject, dwDirection, ppEnumFormatEtc);
853 if (dwDirection != DATADIR_GET)
855 FIXME("only the get direction is implemented\n");
856 return E_NOTIMPL;
859 count = list_count(&xdndData);
860 formats = HeapAlloc(GetProcessHeap(), 0, count * sizeof(FORMATETC));
861 if (formats)
863 XDNDDATA *current;
864 DWORD i = 0;
865 HRESULT hr;
866 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
868 formats[i].cfFormat = current->cf_win;
869 formats[i].ptd = NULL;
870 formats[i].dwAspect = DVASPECT_CONTENT;
871 formats[i].lindex = -1;
872 formats[i].tymed = TYMED_HGLOBAL;
873 i++;
875 hr = SHCreateStdEnumFmtEtc(count, formats, ppEnumFormatEtc);
876 HeapFree(GetProcessHeap(), 0, formats);
877 return hr;
879 else
880 return E_OUTOFMEMORY;
883 static HRESULT WINAPI XDNDDATAOBJECT_DAdvise(IDataObject *dataObject,
884 FORMATETC *formatEtc, DWORD advf,
885 IAdviseSink *adviseSink,
886 DWORD *pdwConnection)
888 FIXME("(%p, %p, %u, %p, %p): stub\n", dataObject, formatEtc, advf,
889 adviseSink, pdwConnection);
890 return OLE_E_ADVISENOTSUPPORTED;
893 static HRESULT WINAPI XDNDDATAOBJECT_DUnadvise(IDataObject *dataObject,
894 DWORD dwConnection)
896 FIXME("(%p, %u): stub\n", dataObject, dwConnection);
897 return OLE_E_ADVISENOTSUPPORTED;
900 static HRESULT WINAPI XDNDDATAOBJECT_EnumDAdvise(IDataObject *dataObject,
901 IEnumSTATDATA **pEnumAdvise)
903 FIXME("(%p, %p): stub\n", dataObject, pEnumAdvise);
904 return OLE_E_ADVISENOTSUPPORTED;
907 static IDataObjectVtbl xdndDataObjectVtbl =
909 XDNDDATAOBJECT_QueryInterface,
910 XDNDDATAOBJECT_AddRef,
911 XDNDDATAOBJECT_Release,
912 XDNDDATAOBJECT_GetData,
913 XDNDDATAOBJECT_GetDataHere,
914 XDNDDATAOBJECT_QueryGetData,
915 XDNDDATAOBJECT_GetCanonicalFormatEtc,
916 XDNDDATAOBJECT_SetData,
917 XDNDDATAOBJECT_EnumFormatEtc,
918 XDNDDATAOBJECT_DAdvise,
919 XDNDDATAOBJECT_DUnadvise,
920 XDNDDATAOBJECT_EnumDAdvise
923 static IDataObject XDNDDataObject = { &xdndDataObjectVtbl };