libwine: Remove __wine_main_arg* from the public header.
[wine/zf.git] / dlls / winemac.drv / dragdrop.c
bloba07b66ac14a925cbbcc84d978c9d6219d1146122
1 /*
2 * MACDRV Drag and drop code
4 * Copyright 2003 Ulrich Czekalla
5 * Copyright 2007 Damjan Jovanovic
6 * Copyright 2013 Ken Thomases for CodeWeavers Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #define NONAMELESSUNION
27 #include "macdrv.h"
29 #define COBJMACROS
30 #include "objidl.h"
31 #include "shellapi.h"
32 #include "shlobj.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dragdrop);
37 static IDataObject *active_data_object;
38 static HWND last_droptarget_hwnd;
41 typedef struct
43 IDataObject IDataObject_iface;
44 LONG ref;
45 CFTypeRef pasteboard;
46 } DragDropDataObject;
49 static inline DragDropDataObject *impl_from_IDataObject(IDataObject *iface)
51 return CONTAINING_RECORD(iface, DragDropDataObject, IDataObject_iface);
55 static HRESULT WINAPI dddo_QueryInterface(IDataObject* iface, REFIID riid, LPVOID *ppvObj)
57 DragDropDataObject *This = impl_from_IDataObject(iface);
59 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObj);
61 if (IsEqualIID(riid, &IID_IUnknown) || (IsEqualIID(riid, &IID_IDataObject)))
63 *ppvObj = iface;
64 IDataObject_AddRef(iface);
65 return S_OK;
68 *ppvObj = NULL;
69 return E_NOINTERFACE;
73 static ULONG WINAPI dddo_AddRef(IDataObject* iface)
75 DragDropDataObject *This = impl_from_IDataObject(iface);
76 ULONG refCount = InterlockedIncrement(&This->ref);
78 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
80 return refCount;
84 static ULONG WINAPI dddo_Release(IDataObject* iface)
86 DragDropDataObject *This = impl_from_IDataObject(iface);
87 ULONG refCount = InterlockedDecrement(&This->ref);
89 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
90 if (refCount)
91 return refCount;
93 TRACE("-- destroying DragDropDataObject (%p)\n", This);
94 CFRelease(This->pasteboard);
95 HeapFree(GetProcessHeap(), 0, This);
96 return 0;
100 static HRESULT WINAPI dddo_GetData(IDataObject* iface, FORMATETC* formatEtc, STGMEDIUM* medium)
102 DragDropDataObject *This = impl_from_IDataObject(iface);
103 HRESULT hr;
105 TRACE("This %p formatEtc %s\n", This, debugstr_format(formatEtc->cfFormat));
107 hr = IDataObject_QueryGetData(iface, formatEtc);
108 if (SUCCEEDED(hr))
110 medium->tymed = TYMED_HGLOBAL;
111 medium->u.hGlobal = macdrv_get_pasteboard_data(This->pasteboard, formatEtc->cfFormat);
112 medium->pUnkForRelease = NULL;
113 hr = medium->u.hGlobal ? S_OK : E_OUTOFMEMORY;
116 return hr;
120 static HRESULT WINAPI dddo_GetDataHere(IDataObject* iface, FORMATETC* formatEtc,
121 STGMEDIUM* medium)
123 FIXME("iface %p formatEtc %p medium %p; stub\n", iface, formatEtc, medium);
124 return DATA_E_FORMATETC;
128 static HRESULT WINAPI dddo_QueryGetData(IDataObject* iface, FORMATETC* formatEtc)
130 DragDropDataObject *This = impl_from_IDataObject(iface);
131 HRESULT hr = DV_E_FORMATETC;
133 TRACE("This %p formatEtc %p={.tymed=0x%x, .dwAspect=%d, .cfFormat=%s}\n",
134 This, formatEtc, formatEtc->tymed, formatEtc->dwAspect,
135 debugstr_format(formatEtc->cfFormat));
137 if (formatEtc->tymed && !(formatEtc->tymed & TYMED_HGLOBAL))
139 FIXME("only HGLOBAL medium types supported right now\n");
140 return DV_E_TYMED;
142 if (formatEtc->dwAspect != DVASPECT_CONTENT)
144 FIXME("only the content aspect is supported right now\n");
145 return E_NOTIMPL;
148 if (macdrv_pasteboard_has_format(This->pasteboard, formatEtc->cfFormat))
149 hr = S_OK;
151 TRACE(" -> 0x%x\n", hr);
152 return hr;
156 static HRESULT WINAPI dddo_GetConicalFormatEtc(IDataObject* iface, FORMATETC* formatEtc,
157 FORMATETC* formatEtcOut)
159 DragDropDataObject *This = impl_from_IDataObject(iface);
161 TRACE("This %p formatEtc %p={.tymed=0x%x, .dwAspect=%d, .cfFormat=%s}\n",
162 This, formatEtc, formatEtc->tymed, formatEtc->dwAspect,
163 debugstr_format(formatEtc->cfFormat));
165 *formatEtcOut = *formatEtc;
166 formatEtcOut->ptd = NULL;
167 return DATA_S_SAMEFORMATETC;
171 static HRESULT WINAPI dddo_SetData(IDataObject* iface, FORMATETC* formatEtc,
172 STGMEDIUM* medium, BOOL fRelease)
174 DragDropDataObject *This = impl_from_IDataObject(iface);
176 TRACE("This %p formatEtc %p={.tymed=0x%x, .dwAspect=%d, .cfFormat=%s} medium %p fRelease %d\n",
177 This, formatEtc, formatEtc->tymed, formatEtc->dwAspect,
178 debugstr_format(formatEtc->cfFormat), medium, fRelease);
180 return E_NOTIMPL;
184 static HRESULT WINAPI dddo_EnumFormatEtc(IDataObject* iface, DWORD direction,
185 IEnumFORMATETC** enumFormatEtc)
187 DragDropDataObject *This = impl_from_IDataObject(iface);
188 UINT *formats, count;
189 HRESULT hr;
191 TRACE("This %p direction %u enumFormatEtc %p\n", This, direction, enumFormatEtc);
193 if (direction != DATADIR_GET)
195 WARN("only the get direction is implemented\n");
196 return E_NOTIMPL;
199 formats = macdrv_get_pasteboard_formats(This->pasteboard, &count);
200 if (formats)
202 FORMATETC *formatEtcs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(FORMATETC));
203 if (formatEtcs)
205 INT i;
207 for (i = 0; i < count; i++)
209 formatEtcs[i].cfFormat = formats[i];
210 formatEtcs[i].ptd = NULL;
211 formatEtcs[i].dwAspect = DVASPECT_CONTENT;
212 formatEtcs[i].lindex = -1;
213 formatEtcs[i].tymed = TYMED_HGLOBAL;
216 hr = SHCreateStdEnumFmtEtc(count, formatEtcs, enumFormatEtc);
217 HeapFree(GetProcessHeap(), 0, formatEtcs);
219 else
220 hr = E_OUTOFMEMORY;
222 HeapFree(GetProcessHeap(), 0, formats);
224 else
225 hr = SHCreateStdEnumFmtEtc(0, NULL, enumFormatEtc);
227 TRACE(" -> 0x%x\n", hr);
228 return hr;
232 static HRESULT WINAPI dddo_DAdvise(IDataObject* iface, FORMATETC* formatEtc, DWORD advf,
233 IAdviseSink* pAdvSink, DWORD* pdwConnection)
235 FIXME("(%p, %p, %u, %p, %p): stub\n", iface, formatEtc, advf,
236 pAdvSink, pdwConnection);
237 return OLE_E_ADVISENOTSUPPORTED;
241 static HRESULT WINAPI dddo_DUnadvise(IDataObject* iface, DWORD dwConnection)
243 FIXME("(%p, %u): stub\n", iface, dwConnection);
244 return OLE_E_ADVISENOTSUPPORTED;
248 static HRESULT WINAPI dddo_EnumDAdvise(IDataObject* iface, IEnumSTATDATA** pEnumAdvise)
250 FIXME("(%p, %p): stub\n", iface, pEnumAdvise);
251 return OLE_E_ADVISENOTSUPPORTED;
255 static const IDataObjectVtbl dovt =
257 dddo_QueryInterface,
258 dddo_AddRef,
259 dddo_Release,
260 dddo_GetData,
261 dddo_GetDataHere,
262 dddo_QueryGetData,
263 dddo_GetConicalFormatEtc,
264 dddo_SetData,
265 dddo_EnumFormatEtc,
266 dddo_DAdvise,
267 dddo_DUnadvise,
268 dddo_EnumDAdvise
272 static IDataObject *create_data_object_for_pasteboard(CFTypeRef pasteboard)
274 DragDropDataObject *dddo;
276 dddo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dddo));
277 if (!dddo)
278 return NULL;
280 dddo->ref = 1;
281 dddo->IDataObject_iface.lpVtbl = &dovt;
282 dddo->pasteboard = CFRetain(pasteboard);
284 return &dddo->IDataObject_iface;
288 /**************************************************************************
289 * drag_operations_to_dropeffects
291 static DWORD drag_operations_to_dropeffects(uint32_t ops)
293 DWORD effects = DROPEFFECT_NONE;
294 if (ops & (DRAG_OP_COPY | DRAG_OP_GENERIC))
295 effects |= DROPEFFECT_COPY;
296 if (ops & DRAG_OP_MOVE)
297 effects |= DROPEFFECT_MOVE;
298 if (ops & (DRAG_OP_LINK | DRAG_OP_GENERIC))
299 effects |= DROPEFFECT_LINK;
300 return effects;
304 /**************************************************************************
305 * dropeffect_to_drag_operation
307 static uint32_t dropeffect_to_drag_operation(DWORD effect, uint32_t ops)
309 if (effect & DROPEFFECT_LINK && ops & DRAG_OP_LINK) return DRAG_OP_LINK;
310 if (effect & DROPEFFECT_COPY && ops & DRAG_OP_COPY) return DRAG_OP_COPY;
311 if (effect & DROPEFFECT_MOVE && ops & DRAG_OP_MOVE) return DRAG_OP_MOVE;
312 if (effect & DROPEFFECT_LINK && ops & DRAG_OP_GENERIC) return DRAG_OP_GENERIC;
313 if (effect & DROPEFFECT_COPY && ops & DRAG_OP_GENERIC) return DRAG_OP_GENERIC;
315 return DRAG_OP_NONE;
319 /* Based on functions in dlls/ole32/ole2.c */
320 static HANDLE get_droptarget_local_handle(HWND hwnd)
322 static const WCHAR prop_marshalleddroptarget[] =
323 {'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};
324 HANDLE handle;
325 HANDLE local_handle = 0;
327 handle = GetPropW(hwnd, prop_marshalleddroptarget);
328 if (handle)
330 DWORD pid;
331 HANDLE process;
333 GetWindowThreadProcessId(hwnd, &pid);
334 process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
335 if (process)
337 DuplicateHandle(process, handle, GetCurrentProcess(), &local_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
338 CloseHandle(process);
341 return local_handle;
345 static HRESULT create_stream_from_map(HANDLE map, IStream **stream)
347 HRESULT hr = E_OUTOFMEMORY;
348 HGLOBAL hmem;
349 void *data;
350 MEMORY_BASIC_INFORMATION info;
352 data = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
353 if(!data) return hr;
355 VirtualQuery(data, &info, sizeof(info));
356 TRACE("size %d\n", (int)info.RegionSize);
358 hmem = GlobalAlloc(GMEM_MOVEABLE, info.RegionSize);
359 if(hmem)
361 memcpy(GlobalLock(hmem), data, info.RegionSize);
362 GlobalUnlock(hmem);
363 hr = CreateStreamOnHGlobal(hmem, TRUE, stream);
365 UnmapViewOfFile(data);
366 return hr;
370 static IDropTarget* get_droptarget_pointer(HWND hwnd)
372 IDropTarget *droptarget = NULL;
373 HANDLE map;
374 IStream *stream;
376 map = get_droptarget_local_handle(hwnd);
377 if(!map) return NULL;
379 if(SUCCEEDED(create_stream_from_map(map, &stream)))
381 CoUnmarshalInterface(stream, &IID_IDropTarget, (void**)&droptarget);
382 IStream_Release(stream);
384 CloseHandle(map);
385 return droptarget;
389 /**************************************************************************
390 * query_drag_drop
392 BOOL query_drag_drop(macdrv_query* query)
394 BOOL ret = FALSE;
395 HWND hwnd = macdrv_get_window_hwnd(query->window);
396 struct macdrv_win_data *data = get_win_data(hwnd);
397 POINT pt;
398 IDropTarget *droptarget;
400 TRACE("win %p/%p x,y %d,%d op 0x%08x pasteboard %p\n", hwnd, query->window,
401 query->drag_drop.x, query->drag_drop.y, query->drag_drop.op, query->drag_drop.pasteboard);
403 if (!data)
405 WARN("no win_data for win %p/%p\n", hwnd, query->window);
406 return FALSE;
409 pt.x = query->drag_drop.x + data->whole_rect.left;
410 pt.y = query->drag_drop.y + data->whole_rect.top;
411 release_win_data(data);
413 droptarget = get_droptarget_pointer(last_droptarget_hwnd);
414 if (droptarget)
416 HRESULT hr;
417 POINTL pointl;
418 DWORD effect = drag_operations_to_dropeffects(query->drag_drop.op);
420 if (!active_data_object)
422 WARN("shouldn't happen: no active IDataObject\n");
423 active_data_object = create_data_object_for_pasteboard(query->drag_drop.pasteboard);
426 pointl.x = pt.x;
427 pointl.y = pt.y;
428 TRACE("Drop hwnd %p droptarget %p pointl (%d,%d) effect 0x%08x\n", last_droptarget_hwnd,
429 droptarget, pointl.x, pointl.y, effect);
430 hr = IDropTarget_Drop(droptarget, active_data_object, MK_LBUTTON, pointl, &effect);
431 if (SUCCEEDED(hr))
433 if (effect != DROPEFFECT_NONE)
435 TRACE("drop succeeded\n");
436 ret = TRUE;
438 else
439 TRACE("the application refused the drop\n");
441 else
442 WARN("drop failed, error 0x%08X\n", hr);
443 IDropTarget_Release(droptarget);
445 else
447 hwnd = WindowFromPoint(pt);
448 while (hwnd && !(GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_ACCEPTFILES))
449 hwnd = GetParent(hwnd);
450 if (hwnd)
452 HDROP hdrop = macdrv_get_pasteboard_data(query->drag_drop.pasteboard, CF_HDROP);
453 DROPFILES *dropfiles = GlobalLock(hdrop);
454 if (dropfiles)
456 RECT rect;
457 dropfiles->pt.x = pt.x;
458 dropfiles->pt.y = pt.y;
459 dropfiles->fNC = !(ScreenToClient(hwnd, &dropfiles->pt) &&
460 GetClientRect(hwnd, &rect) &&
461 PtInRect(&rect, dropfiles->pt));
463 TRACE("sending WM_DROPFILES: hwnd %p nc %d pt %s %s\n", hwnd,
464 dropfiles->fNC, wine_dbgstr_point(&dropfiles->pt),
465 debugstr_w((WCHAR*)((char*)dropfiles + dropfiles->pFiles)));
467 GlobalUnlock(hdrop);
469 ret = PostMessageW(hwnd, WM_DROPFILES, (WPARAM)hdrop, 0L);
470 /* hdrop is owned by the message and freed when the recipient calls DragFinish(). */
473 if (!ret) GlobalFree(hdrop);
477 if (active_data_object) IDataObject_Release(active_data_object);
478 active_data_object = NULL;
479 last_droptarget_hwnd = NULL;
480 return ret;
484 /**************************************************************************
485 * query_drag_exited
487 BOOL query_drag_exited(macdrv_query* query)
489 HWND hwnd = macdrv_get_window_hwnd(query->window);
490 IDropTarget *droptarget;
492 TRACE("win %p/%p\n", hwnd, query->window);
494 droptarget = get_droptarget_pointer(last_droptarget_hwnd);
495 if (droptarget)
497 HRESULT hr;
499 TRACE("DragLeave hwnd %p droptarget %p\n", last_droptarget_hwnd, droptarget);
500 hr = IDropTarget_DragLeave(droptarget);
501 if (FAILED(hr))
502 WARN("IDropTarget_DragLeave failed, error 0x%08X\n", hr);
503 IDropTarget_Release(droptarget);
506 if (active_data_object) IDataObject_Release(active_data_object);
507 active_data_object = NULL;
508 last_droptarget_hwnd = NULL;
510 return TRUE;
514 /**************************************************************************
515 * query_drag_operation
517 BOOL query_drag_operation(macdrv_query* query)
519 BOOL ret = FALSE;
520 HWND hwnd = macdrv_get_window_hwnd(query->window);
521 struct macdrv_win_data *data = get_win_data(hwnd);
522 POINT pt;
523 DWORD effect;
524 IDropTarget *droptarget;
525 HRESULT hr;
527 TRACE("win %p/%p x,y %d,%d offered_ops 0x%x pasteboard %p\n", hwnd, query->window,
528 query->drag_operation.x, query->drag_operation.y, query->drag_operation.offered_ops,
529 query->drag_operation.pasteboard);
531 if (!data)
533 WARN("no win_data for win %p/%p\n", hwnd, query->window);
534 return FALSE;
537 pt.x = query->drag_operation.x + data->whole_rect.left;
538 pt.y = query->drag_operation.y + data->whole_rect.top;
539 release_win_data(data);
541 effect = drag_operations_to_dropeffects(query->drag_operation.offered_ops);
543 /* Instead of the top-level window we got in the query, start with the deepest
544 child under the cursor. Travel up the hierarchy looking for a window that
545 has an associated IDropTarget. */
546 hwnd = WindowFromPoint(pt);
549 droptarget = get_droptarget_pointer(hwnd);
550 } while (!droptarget && (hwnd = GetParent(hwnd)));
552 if (last_droptarget_hwnd != hwnd)
554 if (last_droptarget_hwnd)
556 IDropTarget *old_droptarget = get_droptarget_pointer(last_droptarget_hwnd);
557 if (old_droptarget)
559 TRACE("DragLeave hwnd %p droptarget %p\n", last_droptarget_hwnd, old_droptarget);
560 hr = IDropTarget_DragLeave(old_droptarget);
561 if (FAILED(hr))
562 WARN("IDropTarget_DragLeave failed, error 0x%08X\n", hr);
563 IDropTarget_Release(old_droptarget);
567 last_droptarget_hwnd = hwnd;
569 if (droptarget)
571 POINTL pointl = { pt.x, pt.y };
573 if (!active_data_object)
574 active_data_object = create_data_object_for_pasteboard(query->drag_operation.pasteboard);
576 TRACE("DragEnter hwnd %p droptarget %p\n", hwnd, droptarget);
577 hr = IDropTarget_DragEnter(droptarget, active_data_object, MK_LBUTTON,
578 pointl, &effect);
579 if (SUCCEEDED(hr))
581 query->drag_operation.accepted_op = dropeffect_to_drag_operation(effect,
582 query->drag_operation.offered_ops);
583 TRACE(" effect %d accepted op %d\n", effect, query->drag_operation.accepted_op);
584 ret = TRUE;
586 else
587 WARN("IDropTarget_DragEnter failed, error 0x%08X\n", hr);
588 IDropTarget_Release(droptarget);
591 else if (droptarget)
593 POINTL pointl = { pt.x, pt.y };
595 TRACE("DragOver hwnd %p droptarget %p\n", hwnd, droptarget);
596 hr = IDropTarget_DragOver(droptarget, MK_LBUTTON, pointl, &effect);
597 if (SUCCEEDED(hr))
599 query->drag_operation.accepted_op = dropeffect_to_drag_operation(effect,
600 query->drag_operation.offered_ops);
601 TRACE(" effect %d accepted op %d\n", effect, query->drag_operation.accepted_op);
602 ret = TRUE;
604 else
605 WARN("IDropTarget_DragOver failed, error 0x%08X\n", hr);
606 IDropTarget_Release(droptarget);
609 if (!ret)
611 hwnd = WindowFromPoint(pt);
612 while (hwnd && !(GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_ACCEPTFILES))
613 hwnd = GetParent(hwnd);
614 if (hwnd)
616 FORMATETC formatEtc;
618 if (!active_data_object)
619 active_data_object = create_data_object_for_pasteboard(query->drag_operation.pasteboard);
621 formatEtc.cfFormat = CF_HDROP;
622 formatEtc.ptd = NULL;
623 formatEtc.dwAspect = DVASPECT_CONTENT;
624 formatEtc.lindex = -1;
625 formatEtc.tymed = TYMED_HGLOBAL;
626 if (SUCCEEDED(IDataObject_QueryGetData(active_data_object, &formatEtc)))
628 TRACE("WS_EX_ACCEPTFILES hwnd %p\n", hwnd);
629 query->drag_operation.accepted_op = DRAG_OP_GENERIC;
630 ret = TRUE;
635 TRACE(" -> %s\n", ret ? "TRUE" : "FALSE");
636 return ret;