Authors: Martin Fuchs <martin-fuchs@gmx.net>, Ge van Geldorp <ge@gse.nl>
[wine/multimedia.git] / dlls / shell32 / dataobject.c
blobb4ab270a38772c73fb6cd32ea155c680c31cbfad
1 /*
2 * IEnumFORMATETC, IDataObject
4 * selecting and droping 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <string.h>
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "windef.h"
27 #include "wingdi.h"
28 #include "pidl.h"
29 #include "winerror.h"
30 #include "shell32_main.h"
31 #include "wine/debug.h"
32 #include "undocshell.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
36 /***********************************************************************
37 * IEnumFORMATETC implementation
40 typedef struct
42 /* IUnknown fields */
43 ICOM_VFIELD(IEnumFORMATETC);
44 DWORD ref;
45 /* IEnumFORMATETC fields */
46 UINT posFmt;
47 UINT countFmt;
48 LPFORMATETC pFmt;
49 } IEnumFORMATETCImpl;
51 static HRESULT WINAPI IEnumFORMATETC_fnQueryInterface(LPENUMFORMATETC iface, REFIID riid, LPVOID* ppvObj);
52 static ULONG WINAPI IEnumFORMATETC_fnAddRef(LPENUMFORMATETC iface);
53 static ULONG WINAPI IEnumFORMATETC_fnRelease(LPENUMFORMATETC iface);
54 static HRESULT WINAPI IEnumFORMATETC_fnNext(LPENUMFORMATETC iface, ULONG celt, FORMATETC* rgelt, ULONG* pceltFethed);
55 static HRESULT WINAPI IEnumFORMATETC_fnSkip(LPENUMFORMATETC iface, ULONG celt);
56 static HRESULT WINAPI IEnumFORMATETC_fnReset(LPENUMFORMATETC iface);
57 static HRESULT WINAPI IEnumFORMATETC_fnClone(LPENUMFORMATETC iface, LPENUMFORMATETC* ppenum);
59 static struct ICOM_VTABLE(IEnumFORMATETC) efvt =
61 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
62 IEnumFORMATETC_fnQueryInterface,
63 IEnumFORMATETC_fnAddRef,
64 IEnumFORMATETC_fnRelease,
65 IEnumFORMATETC_fnNext,
66 IEnumFORMATETC_fnSkip,
67 IEnumFORMATETC_fnReset,
68 IEnumFORMATETC_fnClone
71 LPENUMFORMATETC IEnumFORMATETC_Constructor(UINT cfmt, const FORMATETC afmt[])
73 IEnumFORMATETCImpl* ef;
74 DWORD size=cfmt * sizeof(FORMATETC);
76 ef=(IEnumFORMATETCImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumFORMATETCImpl));
78 if(ef)
80 ef->ref=1;
81 ef->lpVtbl=&efvt;
83 ef->countFmt = cfmt;
84 ef->pFmt = SHAlloc (size);
86 if (ef->pFmt)
88 memcpy(ef->pFmt, afmt, size);
92 TRACE("(%p)->(%u,%p)\n",ef, cfmt, afmt);
93 return (LPENUMFORMATETC)ef;
96 static HRESULT WINAPI IEnumFORMATETC_fnQueryInterface(LPENUMFORMATETC iface, REFIID riid, LPVOID* ppvObj)
98 ICOM_THIS(IEnumFORMATETCImpl,iface);
99 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
101 *ppvObj = NULL;
103 if(IsEqualIID(riid, &IID_IUnknown))
105 *ppvObj = This;
107 else if(IsEqualIID(riid, &IID_IEnumFORMATETC))
109 *ppvObj = (IEnumFORMATETC*)This;
112 if(*ppvObj)
114 IUnknown_AddRef((IUnknown*)(*ppvObj));
115 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
116 return S_OK;
118 TRACE("-- Interface: E_NOINTERFACE\n");
119 return E_NOINTERFACE;
123 static ULONG WINAPI IEnumFORMATETC_fnAddRef(LPENUMFORMATETC iface)
125 ICOM_THIS(IEnumFORMATETCImpl,iface);
126 TRACE("(%p)->(count=%lu)\n",This, This->ref);
127 return ++(This->ref);
130 static ULONG WINAPI IEnumFORMATETC_fnRelease(LPENUMFORMATETC iface)
132 ICOM_THIS(IEnumFORMATETCImpl,iface);
133 TRACE("(%p)->()\n",This);
135 if (!--(This->ref))
137 TRACE(" destroying IEnumFORMATETC(%p)\n",This);
138 if (This->pFmt)
140 SHFree (This->pFmt);
142 HeapFree(GetProcessHeap(),0,This);
143 return 0;
145 return This->ref;
148 static HRESULT WINAPI IEnumFORMATETC_fnNext(LPENUMFORMATETC iface, ULONG celt, FORMATETC *rgelt, ULONG *pceltFethed)
150 ICOM_THIS(IEnumFORMATETCImpl,iface);
151 UINT i;
153 TRACE("(%p)->(%lu,%p)\n", This, celt, rgelt);
155 if(!This->pFmt)return S_FALSE;
156 if(!rgelt) return E_INVALIDARG;
157 if (pceltFethed) *pceltFethed = 0;
159 for(i = 0; This->posFmt < This->countFmt && celt > i; i++)
161 *rgelt++ = This->pFmt[This->posFmt++];
164 if (pceltFethed) *pceltFethed = i;
166 return ((i == celt) ? S_OK : S_FALSE);
169 static HRESULT WINAPI IEnumFORMATETC_fnSkip(LPENUMFORMATETC iface, ULONG celt)
171 ICOM_THIS(IEnumFORMATETCImpl,iface);
172 TRACE("(%p)->(num=%lu)\n", This, celt);
174 if((This->posFmt + celt) >= This->countFmt) return S_FALSE;
175 This->posFmt += celt;
176 return S_OK;
179 static HRESULT WINAPI IEnumFORMATETC_fnReset(LPENUMFORMATETC iface)
181 ICOM_THIS(IEnumFORMATETCImpl,iface);
182 TRACE("(%p)->()\n", This);
184 This->posFmt = 0;
185 return S_OK;
188 static HRESULT WINAPI IEnumFORMATETC_fnClone(LPENUMFORMATETC iface, LPENUMFORMATETC* ppenum)
190 ICOM_THIS(IEnumFORMATETCImpl,iface);
191 TRACE("(%p)->(ppenum=%p)\n", This, ppenum);
193 if (!ppenum) return E_INVALIDARG;
194 *ppenum = IEnumFORMATETC_Constructor(This->countFmt, This->pFmt);
195 if(*ppenum)
196 IEnumFORMATETC_fnSkip(*ppenum, This->posFmt);
197 return S_OK;
201 /***********************************************************************
202 * IDataObject implementation
205 /* number of supported formats */
206 #define MAX_FORMATS 4
208 typedef struct
210 /* IUnknown fields */
211 ICOM_VFIELD(IDataObject);
212 DWORD ref;
214 /* IDataObject fields */
215 LPITEMIDLIST pidl;
216 LPITEMIDLIST * apidl;
217 UINT cidl;
219 FORMATETC pFormatEtc[MAX_FORMATS];
220 UINT cfShellIDList;
221 UINT cfFileNameA;
222 UINT cfFileNameW;
224 } IDataObjectImpl;
226 static struct ICOM_VTABLE(IDataObject) dtovt;
228 /**************************************************************************
229 * IDataObject_Constructor
231 LPDATAOBJECT IDataObject_Constructor(HWND hwndOwner, LPCITEMIDLIST pMyPidl, LPCITEMIDLIST * apidl, UINT cidl)
233 IDataObjectImpl* dto;
235 dto = (IDataObjectImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDataObjectImpl));
237 if (dto)
239 dto->ref = 1;
240 dto->lpVtbl = &dtovt;
241 dto->pidl = ILClone(pMyPidl);
242 dto->apidl = _ILCopyaPidl(apidl, cidl);
243 dto->cidl = cidl;
245 dto->cfShellIDList = RegisterClipboardFormatA(CFSTR_SHELLIDLIST);
246 dto->cfFileNameA = RegisterClipboardFormatA(CFSTR_FILENAMEA);
247 dto->cfFileNameW = RegisterClipboardFormatA(CFSTR_FILENAMEW);
248 InitFormatEtc(dto->pFormatEtc[0], dto->cfShellIDList, TYMED_HGLOBAL);
249 InitFormatEtc(dto->pFormatEtc[1], CF_HDROP, TYMED_HGLOBAL);
250 InitFormatEtc(dto->pFormatEtc[2], dto->cfFileNameA, TYMED_HGLOBAL);
251 InitFormatEtc(dto->pFormatEtc[3], dto->cfFileNameW, TYMED_HGLOBAL);
254 TRACE("(%p)->(apidl=%p cidl=%u)\n",dto, apidl, cidl);
255 return (LPDATAOBJECT)dto;
258 /***************************************************************************
259 * IDataObject_QueryInterface
261 static HRESULT WINAPI IDataObject_fnQueryInterface(LPDATAOBJECT iface, REFIID riid, LPVOID * ppvObj)
263 ICOM_THIS(IDataObjectImpl,iface);
264 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
266 *ppvObj = NULL;
268 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
270 *ppvObj = This;
272 else if(IsEqualIID(riid, &IID_IDataObject)) /*IDataObject*/
274 *ppvObj = (IDataObject*)This;
277 if(*ppvObj)
279 IUnknown_AddRef((IUnknown*)*ppvObj);
280 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
281 return S_OK;
283 TRACE("-- Interface: E_NOINTERFACE\n");
284 return E_NOINTERFACE;
287 /**************************************************************************
288 * IDataObject_AddRef
290 static ULONG WINAPI IDataObject_fnAddRef(LPDATAOBJECT iface)
292 ICOM_THIS(IDataObjectImpl,iface);
293 TRACE("(%p)->(count=%lu)\n",This, This->ref);
294 return ++(This->ref);
297 /**************************************************************************
298 * IDataObject_Release
300 static ULONG WINAPI IDataObject_fnRelease(LPDATAOBJECT iface)
302 ICOM_THIS(IDataObjectImpl,iface);
303 TRACE("(%p)->()\n",This);
305 if (!--(This->ref))
307 TRACE(" destroying IDataObject(%p)\n",This);
308 _ILFreeaPidl(This->apidl, This->cidl);
309 ILFree(This->pidl),
310 HeapFree(GetProcessHeap(),0,This);
311 return 0;
313 return This->ref;
316 /**************************************************************************
317 * IDataObject_fnGetData
319 static HRESULT WINAPI IDataObject_fnGetData(LPDATAOBJECT iface, LPFORMATETC pformatetcIn, STGMEDIUM *pmedium)
321 ICOM_THIS(IDataObjectImpl,iface);
323 char szTemp[256];
325 szTemp[0]=0;
326 GetClipboardFormatNameA (pformatetcIn->cfFormat, szTemp, 256);
327 TRACE("(%p)->(%p %p format=%s)\n", This, pformatetcIn, pmedium, szTemp);
329 if (pformatetcIn->cfFormat == This->cfShellIDList)
331 if (This->cidl < 1) return(E_UNEXPECTED);
332 pmedium->u.hGlobal = RenderSHELLIDLIST(This->pidl, This->apidl, This->cidl);
334 else if (pformatetcIn->cfFormat == CF_HDROP)
336 if (This->cidl < 1) return(E_UNEXPECTED);
337 pmedium->u.hGlobal = RenderHDROP(This->pidl, This->apidl, This->cidl);
339 else if (pformatetcIn->cfFormat == This->cfFileNameA)
341 if (This->cidl < 1) return(E_UNEXPECTED);
342 pmedium->u.hGlobal = RenderFILENAMEA(This->pidl, This->apidl, This->cidl);
344 else if (pformatetcIn->cfFormat == This->cfFileNameW)
346 if (This->cidl < 1) return(E_UNEXPECTED);
347 pmedium->u.hGlobal = RenderFILENAMEW(This->pidl, This->apidl, This->cidl);
349 else
351 FIXME("-- expected clipformat not implemented\n");
352 return (E_INVALIDARG);
354 if (pmedium->u.hGlobal)
356 pmedium->tymed = TYMED_HGLOBAL;
357 pmedium->pUnkForRelease = NULL;
358 return S_OK;
360 return E_OUTOFMEMORY;
363 static HRESULT WINAPI IDataObject_fnGetDataHere(LPDATAOBJECT iface, LPFORMATETC pformatetc, STGMEDIUM *pmedium)
365 ICOM_THIS(IDataObjectImpl,iface);
366 FIXME("(%p)->()\n", This);
367 return E_NOTIMPL;
370 static HRESULT WINAPI IDataObject_fnQueryGetData(LPDATAOBJECT iface, LPFORMATETC pformatetc)
372 ICOM_THIS(IDataObjectImpl,iface);
373 UINT i;
375 TRACE("(%p)->(fmt=0x%08x tym=0x%08lx)\n", This, pformatetc->cfFormat, pformatetc->tymed);
377 if(!(DVASPECT_CONTENT & pformatetc->dwAspect))
378 return DV_E_DVASPECT;
380 /* check our formats table what we have */
381 for (i=0; i<MAX_FORMATS; i++)
383 if ((This->pFormatEtc[i].cfFormat == pformatetc->cfFormat)
384 && (This->pFormatEtc[i].tymed == pformatetc->tymed))
386 return S_OK;
390 return DV_E_TYMED;
393 static HRESULT WINAPI IDataObject_fnGetCanonicalFormatEtc(LPDATAOBJECT iface, LPFORMATETC pformatectIn, LPFORMATETC pformatetcOut)
395 ICOM_THIS(IDataObjectImpl,iface);
396 FIXME("(%p)->()\n", This);
397 return E_NOTIMPL;
400 static HRESULT WINAPI IDataObject_fnSetData(LPDATAOBJECT iface, LPFORMATETC pformatetc, STGMEDIUM *pmedium, BOOL fRelease)
402 ICOM_THIS(IDataObjectImpl,iface);
403 FIXME("(%p)->()\n", This);
404 return E_NOTIMPL;
407 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(LPDATAOBJECT iface, DWORD dwDirection, IEnumFORMATETC **ppenumFormatEtc)
409 ICOM_THIS(IDataObjectImpl,iface);
411 TRACE("(%p)->()\n", This);
412 *ppenumFormatEtc=NULL;
414 /* only get data */
415 if (DATADIR_GET == dwDirection)
417 *ppenumFormatEtc = IEnumFORMATETC_Constructor(MAX_FORMATS, This->pFormatEtc);
418 return (*ppenumFormatEtc) ? S_OK : E_FAIL;
421 return E_NOTIMPL;
424 static HRESULT WINAPI IDataObject_fnDAdvise(LPDATAOBJECT iface, FORMATETC *pformatetc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection)
426 ICOM_THIS(IDataObjectImpl,iface);
427 FIXME("(%p)->()\n", This);
428 return E_NOTIMPL;
430 static HRESULT WINAPI IDataObject_fnDUnadvise(LPDATAOBJECT iface, DWORD dwConnection)
432 ICOM_THIS(IDataObjectImpl,iface);
433 FIXME("(%p)->()\n", This);
434 return E_NOTIMPL;
436 static HRESULT WINAPI IDataObject_fnEnumDAdvise(LPDATAOBJECT iface, IEnumSTATDATA **ppenumAdvise)
438 ICOM_THIS(IDataObjectImpl,iface);
439 FIXME("(%p)->()\n", This);
440 return E_NOTIMPL;
443 static struct ICOM_VTABLE(IDataObject) dtovt =
445 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
446 IDataObject_fnQueryInterface,
447 IDataObject_fnAddRef,
448 IDataObject_fnRelease,
449 IDataObject_fnGetData,
450 IDataObject_fnGetDataHere,
451 IDataObject_fnQueryGetData,
452 IDataObject_fnGetCanonicalFormatEtc,
453 IDataObject_fnSetData,
454 IDataObject_fnEnumFormatEtc,
455 IDataObject_fnDAdvise,
456 IDataObject_fnDUnadvise,
457 IDataObject_fnEnumDAdvise