include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / shell32 / dataobject.c
blobd0ed644c4370ccbf8d537c604b043b73c15095f3
1 /*
2 * IEnumFORMATETC, IDataObject
4 * selecting and dropping objects within the shell and/or common dialogs
6 * Copyright 1998, 1999 <juergen.schmied@metronet.de>
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
22 #include <string.h>
24 #define COBJMACROS
25 #include "windef.h"
26 #include "wingdi.h"
27 #include "pidl.h"
28 #include "winerror.h"
29 #include "shell32_main.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(shell);
34 /***********************************************************************
35 * IEnumFORMATETC implementation
38 typedef struct
40 /* IUnknown fields */
41 IEnumFORMATETC IEnumFORMATETC_iface;
42 LONG ref;
43 /* IEnumFORMATETC fields */
44 UINT posFmt;
45 UINT countFmt;
46 LPFORMATETC pFmt;
47 } IEnumFORMATETCImpl;
49 static inline IEnumFORMATETCImpl *impl_from_IEnumFORMATETC(IEnumFORMATETC *iface)
51 return CONTAINING_RECORD(iface, IEnumFORMATETCImpl, IEnumFORMATETC_iface);
54 static HRESULT WINAPI IEnumFORMATETC_fnQueryInterface(
55 LPENUMFORMATETC iface, REFIID riid, LPVOID* ppvObj)
57 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
58 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
60 *ppvObj = NULL;
62 if(IsEqualIID(riid, &IID_IUnknown) ||
63 IsEqualIID(riid, &IID_IEnumFORMATETC))
65 *ppvObj = &This->IEnumFORMATETC_iface;
68 if(*ppvObj)
70 IUnknown_AddRef((IUnknown*)(*ppvObj));
71 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
72 return S_OK;
74 TRACE("-- Interface: E_NOINTERFACE\n");
75 return E_NOINTERFACE;
78 static ULONG WINAPI IEnumFORMATETC_fnAddRef(LPENUMFORMATETC iface)
80 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
81 ULONG refCount = InterlockedIncrement(&This->ref);
83 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
85 return refCount;
88 static ULONG WINAPI IEnumFORMATETC_fnRelease(LPENUMFORMATETC iface)
90 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
91 ULONG refCount = InterlockedDecrement(&This->ref);
93 TRACE("(%p)->(%lu)\n", This, refCount + 1);
95 if (!refCount)
97 TRACE(" destroying IEnumFORMATETC(%p)\n",This);
98 SHFree (This->pFmt);
99 free(This);
101 return refCount;
104 static HRESULT WINAPI IEnumFORMATETC_fnNext(LPENUMFORMATETC iface, ULONG celt, FORMATETC *rgelt, ULONG *pceltFethed)
106 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
107 UINT i;
109 TRACE("(%p)->(%lu,%p)\n", This, celt, rgelt);
111 if(!This->pFmt)return S_FALSE;
112 if(!rgelt) return E_INVALIDARG;
113 if (pceltFethed) *pceltFethed = 0;
115 for(i = 0; This->posFmt < This->countFmt && celt > i; i++)
117 *rgelt++ = This->pFmt[This->posFmt++];
120 if (pceltFethed) *pceltFethed = i;
122 return ((i == celt) ? S_OK : S_FALSE);
125 static HRESULT WINAPI IEnumFORMATETC_fnSkip(LPENUMFORMATETC iface, ULONG celt)
127 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
128 TRACE("(%p)->(num=%lu)\n", This, celt);
130 if((This->posFmt + celt) >= This->countFmt) return S_FALSE;
131 This->posFmt += celt;
132 return S_OK;
135 static HRESULT WINAPI IEnumFORMATETC_fnReset(LPENUMFORMATETC iface)
137 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
138 TRACE("(%p)->()\n", This);
140 This->posFmt = 0;
141 return S_OK;
144 static HRESULT WINAPI IEnumFORMATETC_fnClone(LPENUMFORMATETC iface, LPENUMFORMATETC* ppenum)
146 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
147 TRACE("(%p)->(ppenum=%p)\n", This, ppenum);
149 if (!ppenum) return E_INVALIDARG;
150 *ppenum = IEnumFORMATETC_Constructor(This->countFmt, This->pFmt);
151 if(*ppenum)
152 IEnumFORMATETC_fnSkip(*ppenum, This->posFmt);
153 return S_OK;
156 static const IEnumFORMATETCVtbl efvt =
158 IEnumFORMATETC_fnQueryInterface,
159 IEnumFORMATETC_fnAddRef,
160 IEnumFORMATETC_fnRelease,
161 IEnumFORMATETC_fnNext,
162 IEnumFORMATETC_fnSkip,
163 IEnumFORMATETC_fnReset,
164 IEnumFORMATETC_fnClone
167 LPENUMFORMATETC IEnumFORMATETC_Constructor(UINT cfmt, const FORMATETC afmt[])
169 IEnumFORMATETCImpl* ef;
170 DWORD size=cfmt * sizeof(FORMATETC);
172 ef = calloc(1, sizeof(*ef));
174 if(ef)
176 ef->ref=1;
177 ef->IEnumFORMATETC_iface.lpVtbl = &efvt;
179 ef->countFmt = cfmt;
180 ef->pFmt = SHAlloc (size);
182 if (ef->pFmt)
183 memcpy(ef->pFmt, afmt, size);
186 TRACE("(%p)->(%u,%p)\n",ef, cfmt, afmt);
187 return &ef->IEnumFORMATETC_iface;
190 struct data
192 UINT cf;
193 HGLOBAL global;
196 typedef struct
198 IDataObject IDataObject_iface;
199 LONG ref;
201 struct data *data;
202 size_t data_count;
203 } IDataObjectImpl;
205 static inline IDataObjectImpl *impl_from_IDataObject(IDataObject *iface)
207 return CONTAINING_RECORD(iface, IDataObjectImpl, IDataObject_iface);
210 /***************************************************************************
211 * IDataObject_QueryInterface
213 static HRESULT WINAPI IDataObject_fnQueryInterface(IDataObject *iface, REFIID riid, LPVOID * ppvObj)
215 IDataObjectImpl *This = impl_from_IDataObject(iface);
216 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
218 *ppvObj = NULL;
220 if(IsEqualIID(riid, &IID_IUnknown) ||
221 IsEqualIID(riid, &IID_IDataObject))
223 *ppvObj = &This->IDataObject_iface;
226 if(*ppvObj)
228 IUnknown_AddRef((IUnknown*)*ppvObj);
229 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
230 return S_OK;
232 TRACE("-- Interface: E_NOINTERFACE\n");
233 return E_NOINTERFACE;
236 /**************************************************************************
237 * IDataObject_AddRef
239 static ULONG WINAPI IDataObject_fnAddRef(IDataObject *iface)
241 IDataObjectImpl *This = impl_from_IDataObject(iface);
242 ULONG refCount = InterlockedIncrement(&This->ref);
244 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
246 return refCount;
249 /**************************************************************************
250 * IDataObject_Release
252 static ULONG WINAPI IDataObject_fnRelease(IDataObject *iface)
254 IDataObjectImpl *obj = impl_from_IDataObject(iface);
255 ULONG refcount = InterlockedDecrement(&obj->ref);
257 TRACE("%p decreasing refcount to %lu.\n", obj, refcount);
259 if (!refcount)
261 for (size_t i = 0; i < obj->data_count; ++i)
262 GlobalFree(obj->data[i].global);
263 free(obj->data);
264 free(obj);
266 return refcount;
269 /**************************************************************************
270 * IDataObject_fnGetData
272 static HRESULT WINAPI IDataObject_fnGetData(IDataObject *iface, FORMATETC *format, STGMEDIUM *medium)
274 IDataObjectImpl *obj = impl_from_IDataObject(iface);
276 TRACE("iface %p, format %p, medium %p.\n", iface, format, medium);
278 if (!(format->tymed & TYMED_HGLOBAL))
280 FIXME("Unrecognized tymed %#lx, returning DV_E_FORMATETC.\n", format->tymed);
281 return DV_E_FORMATETC;
284 for (size_t i = 0; i < obj->data_count; ++i)
286 if (obj->data[i].cf == format->cfFormat)
288 HGLOBAL src_global = obj->data[i].global;
289 size_t size = GlobalSize(src_global);
290 HGLOBAL dst_global;
291 const void *src;
292 void *dst;
294 if (!(dst_global = GlobalAlloc(GMEM_MOVEABLE, size)))
295 return E_OUTOFMEMORY;
296 src = GlobalLock(src_global);
297 dst = GlobalLock(dst_global);
298 memcpy(dst, src, size);
299 GlobalUnlock(src_global);
300 GlobalUnlock(dst_global);
302 medium->tymed = TYMED_HGLOBAL;
303 medium->pUnkForRelease = NULL;
304 medium->hGlobal = dst_global;
305 return S_OK;
309 return DV_E_FORMATETC;
312 static HRESULT WINAPI IDataObject_fnGetDataHere(IDataObject *iface, LPFORMATETC pformatetc, STGMEDIUM *pmedium)
314 IDataObjectImpl *This = impl_from_IDataObject(iface);
315 FIXME("(%p)->()\n", This);
316 return E_NOTIMPL;
319 static HRESULT WINAPI IDataObject_fnQueryGetData(IDataObject *iface, FORMATETC *format)
321 IDataObjectImpl *obj = impl_from_IDataObject(iface);
323 TRACE("iface %p, format %p.\n", iface, format);
325 if (!(format->tymed & TYMED_HGLOBAL))
327 FIXME("Unrecognized tymed %#lx, returning S_FALSE.\n", format->tymed);
328 return S_FALSE;
331 for (size_t i = 0; i < obj->data_count; ++i)
333 if (obj->data[i].cf == format->cfFormat)
334 return S_OK;
337 return S_FALSE;
340 static HRESULT WINAPI IDataObject_fnGetCanonicalFormatEtc(IDataObject *iface, LPFORMATETC pformatectIn, LPFORMATETC pformatetcOut)
342 IDataObjectImpl *This = impl_from_IDataObject(iface);
343 FIXME("(%p)->()\n", This);
344 return E_NOTIMPL;
347 static HRESULT WINAPI IDataObject_fnSetData(IDataObject *iface,
348 FORMATETC *format, STGMEDIUM *medium, BOOL release)
350 IDataObjectImpl *obj = impl_from_IDataObject(iface);
351 struct data *new_array;
353 TRACE("iface %p, format %p, medium %p, release %d.\n", iface, format, medium, release);
355 if (!release)
356 return E_INVALIDARG;
358 if (format->tymed != TYMED_HGLOBAL)
360 FIXME("Unhandled format tymed %#lx.\n", format->tymed);
361 return E_NOTIMPL;
364 if (medium->tymed != TYMED_HGLOBAL)
366 FIXME("Unhandled medium tymed %#lx.\n", format->tymed);
367 return E_NOTIMPL;
370 if (medium->pUnkForRelease)
371 FIXME("Ignoring IUnknown %p.\n", medium->pUnkForRelease);
373 for (size_t i = 0; i < obj->data_count; ++i)
375 if (obj->data[i].cf == format->cfFormat)
377 GlobalFree(obj->data[i].global);
378 obj->data[i].global = medium->hGlobal;
379 return S_OK;
383 if (!(new_array = realloc(obj->data, (obj->data_count + 1) * sizeof(*obj->data))))
384 return E_OUTOFMEMORY;
385 obj->data = new_array;
386 obj->data[obj->data_count].cf = format->cfFormat;
387 obj->data[obj->data_count].global = medium->hGlobal;
388 ++obj->data_count;
389 return S_OK;
392 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(IDataObject *iface, DWORD direction, IEnumFORMATETC **out)
394 IDataObjectImpl *obj = impl_from_IDataObject(iface);
395 FORMATETC *formats;
397 TRACE("iface %p, direction %#lx, out %p.\n", iface, direction, out);
399 if (!(formats = calloc(obj->data_count, sizeof(*formats))))
400 return E_OUTOFMEMORY;
401 for (size_t i = 0; i < obj->data_count; ++i)
402 InitFormatEtc(formats[i], obj->data[i].cf, TYMED_HGLOBAL);
404 if (!(*out = IEnumFORMATETC_Constructor(obj->data_count, formats)))
406 free(formats);
407 return E_OUTOFMEMORY;
409 free(formats);
410 return S_OK;
413 static HRESULT WINAPI IDataObject_fnDAdvise(IDataObject *iface, FORMATETC *pformatetc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection)
415 IDataObjectImpl *This = impl_from_IDataObject(iface);
416 FIXME("(%p)->()\n", This);
417 return E_NOTIMPL;
419 static HRESULT WINAPI IDataObject_fnDUnadvise(IDataObject *iface, DWORD dwConnection)
421 IDataObjectImpl *This = impl_from_IDataObject(iface);
422 FIXME("(%p)->()\n", This);
423 return E_NOTIMPL;
425 static HRESULT WINAPI IDataObject_fnEnumDAdvise(IDataObject *iface, IEnumSTATDATA **ppenumAdvise)
427 IDataObjectImpl *This = impl_from_IDataObject(iface);
428 FIXME("(%p)->()\n", This);
429 return E_NOTIMPL;
432 static const IDataObjectVtbl dtovt =
434 IDataObject_fnQueryInterface,
435 IDataObject_fnAddRef,
436 IDataObject_fnRelease,
437 IDataObject_fnGetData,
438 IDataObject_fnGetDataHere,
439 IDataObject_fnQueryGetData,
440 IDataObject_fnGetCanonicalFormatEtc,
441 IDataObject_fnSetData,
442 IDataObject_fnEnumFormatEtc,
443 IDataObject_fnDAdvise,
444 IDataObject_fnDUnadvise,
445 IDataObject_fnEnumDAdvise
448 /**************************************************************************
449 * IDataObject_Constructor
451 IDataObject *IDataObject_Constructor(HWND hwnd, const ITEMIDLIST *root_pidl,
452 const ITEMIDLIST **pidls, UINT pidl_count)
454 IDataObjectImpl *obj;
456 if (!(obj = calloc(1, sizeof(*obj))))
457 return NULL;
459 obj->ref = 1;
460 obj->IDataObject_iface.lpVtbl = &dtovt;
461 obj->data = calloc(4, sizeof(*obj->data));
462 obj->data[0].cf = RegisterClipboardFormatW(CFSTR_SHELLIDLISTW);
463 obj->data[0].global = RenderSHELLIDLIST(root_pidl, pidls, pidl_count);
464 obj->data[1].cf = CF_HDROP;
465 obj->data[1].global = RenderHDROP(root_pidl, pidls, pidl_count);
466 obj->data[2].cf = RegisterClipboardFormatA(CFSTR_FILENAMEA);
467 obj->data[2].global = RenderFILENAMEA(root_pidl, pidls, pidl_count);
468 obj->data[3].cf = RegisterClipboardFormatW(CFSTR_FILENAMEW);
469 obj->data[3].global = RenderFILENAMEW(root_pidl, pidls, pidl_count);
470 obj->data_count = 4;
472 TRACE("Created data object %p.\n", obj);
473 return &obj->IDataObject_iface;