wbemprox: Implement Win32_CDROMDrive.MediaType.
[wine/wine-gecko.git] / dlls / shell32 / dataobject.c
blobb8b5a8737702fcbfbe12e7db52fef2cb033cf54e
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 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "wingdi.h"
30 #include "pidl.h"
31 #include "winerror.h"
32 #include "shell32_main.h"
33 #include "wine/debug.h"
34 #include "undocshell.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38 /***********************************************************************
39 * IEnumFORMATETC implementation
42 typedef struct
44 /* IUnknown fields */
45 IEnumFORMATETC IEnumFORMATETC_iface;
46 LONG ref;
47 /* IEnumFORMATETC fields */
48 UINT posFmt;
49 UINT countFmt;
50 LPFORMATETC pFmt;
51 } IEnumFORMATETCImpl;
53 static inline IEnumFORMATETCImpl *impl_from_IEnumFORMATETC(IEnumFORMATETC *iface)
55 return CONTAINING_RECORD(iface, IEnumFORMATETCImpl, IEnumFORMATETC_iface);
58 static HRESULT WINAPI IEnumFORMATETC_fnQueryInterface(
59 LPENUMFORMATETC iface, REFIID riid, LPVOID* ppvObj)
61 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
62 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
64 *ppvObj = NULL;
66 if(IsEqualIID(riid, &IID_IUnknown) ||
67 IsEqualIID(riid, &IID_IEnumFORMATETC))
69 *ppvObj = &This->IEnumFORMATETC_iface;
72 if(*ppvObj)
74 IUnknown_AddRef((IUnknown*)(*ppvObj));
75 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
76 return S_OK;
78 TRACE("-- Interface: E_NOINTERFACE\n");
79 return E_NOINTERFACE;
82 static ULONG WINAPI IEnumFORMATETC_fnAddRef(LPENUMFORMATETC iface)
84 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
85 ULONG refCount = InterlockedIncrement(&This->ref);
87 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
89 return refCount;
92 static ULONG WINAPI IEnumFORMATETC_fnRelease(LPENUMFORMATETC iface)
94 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
95 ULONG refCount = InterlockedDecrement(&This->ref);
97 TRACE("(%p)->(%u)\n", This, refCount + 1);
99 if (!refCount)
101 TRACE(" destroying IEnumFORMATETC(%p)\n",This);
102 SHFree (This->pFmt);
103 HeapFree(GetProcessHeap(),0,This);
104 return 0;
106 return refCount;
109 static HRESULT WINAPI IEnumFORMATETC_fnNext(LPENUMFORMATETC iface, ULONG celt, FORMATETC *rgelt, ULONG *pceltFethed)
111 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
112 UINT i;
114 TRACE("(%p)->(%u,%p)\n", This, celt, rgelt);
116 if(!This->pFmt)return S_FALSE;
117 if(!rgelt) return E_INVALIDARG;
118 if (pceltFethed) *pceltFethed = 0;
120 for(i = 0; This->posFmt < This->countFmt && celt > i; i++)
122 *rgelt++ = This->pFmt[This->posFmt++];
125 if (pceltFethed) *pceltFethed = i;
127 return ((i == celt) ? S_OK : S_FALSE);
130 static HRESULT WINAPI IEnumFORMATETC_fnSkip(LPENUMFORMATETC iface, ULONG celt)
132 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
133 TRACE("(%p)->(num=%u)\n", This, celt);
135 if((This->posFmt + celt) >= This->countFmt) return S_FALSE;
136 This->posFmt += celt;
137 return S_OK;
140 static HRESULT WINAPI IEnumFORMATETC_fnReset(LPENUMFORMATETC iface)
142 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
143 TRACE("(%p)->()\n", This);
145 This->posFmt = 0;
146 return S_OK;
149 static HRESULT WINAPI IEnumFORMATETC_fnClone(LPENUMFORMATETC iface, LPENUMFORMATETC* ppenum)
151 IEnumFORMATETCImpl *This = impl_from_IEnumFORMATETC(iface);
152 TRACE("(%p)->(ppenum=%p)\n", This, ppenum);
154 if (!ppenum) return E_INVALIDARG;
155 *ppenum = IEnumFORMATETC_Constructor(This->countFmt, This->pFmt);
156 if(*ppenum)
157 IEnumFORMATETC_fnSkip(*ppenum, This->posFmt);
158 return S_OK;
161 static const IEnumFORMATETCVtbl efvt =
163 IEnumFORMATETC_fnQueryInterface,
164 IEnumFORMATETC_fnAddRef,
165 IEnumFORMATETC_fnRelease,
166 IEnumFORMATETC_fnNext,
167 IEnumFORMATETC_fnSkip,
168 IEnumFORMATETC_fnReset,
169 IEnumFORMATETC_fnClone
172 LPENUMFORMATETC IEnumFORMATETC_Constructor(UINT cfmt, const FORMATETC afmt[])
174 IEnumFORMATETCImpl* ef;
175 DWORD size=cfmt * sizeof(FORMATETC);
177 ef = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumFORMATETCImpl));
179 if(ef)
181 ef->ref=1;
182 ef->IEnumFORMATETC_iface.lpVtbl = &efvt;
184 ef->countFmt = cfmt;
185 ef->pFmt = SHAlloc (size);
187 if (ef->pFmt)
188 memcpy(ef->pFmt, afmt, size);
191 TRACE("(%p)->(%u,%p)\n",ef, cfmt, afmt);
192 return (LPENUMFORMATETC)ef;
196 /***********************************************************************
197 * IDataObject implementation
200 /* number of supported formats */
201 #define MAX_FORMATS 4
203 typedef struct
205 /* IUnknown fields */
206 IDataObject IDataObject_iface;
207 LONG ref;
209 /* IDataObject fields */
210 LPITEMIDLIST pidl;
211 LPITEMIDLIST * apidl;
212 UINT cidl;
214 FORMATETC pFormatEtc[MAX_FORMATS];
215 UINT cfShellIDList;
216 UINT cfFileNameA;
217 UINT cfFileNameW;
219 } IDataObjectImpl;
221 static inline IDataObjectImpl *impl_from_IDataObject(IDataObject *iface)
223 return CONTAINING_RECORD(iface, IDataObjectImpl, IDataObject_iface);
226 /***************************************************************************
227 * IDataObject_QueryInterface
229 static HRESULT WINAPI IDataObject_fnQueryInterface(IDataObject *iface, REFIID riid, LPVOID * ppvObj)
231 IDataObjectImpl *This = impl_from_IDataObject(iface);
232 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
234 *ppvObj = NULL;
236 if(IsEqualIID(riid, &IID_IUnknown) ||
237 IsEqualIID(riid, &IID_IDataObject))
239 *ppvObj = &This->IDataObject_iface;
242 if(*ppvObj)
244 IUnknown_AddRef((IUnknown*)*ppvObj);
245 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
246 return S_OK;
248 TRACE("-- Interface: E_NOINTERFACE\n");
249 return E_NOINTERFACE;
252 /**************************************************************************
253 * IDataObject_AddRef
255 static ULONG WINAPI IDataObject_fnAddRef(IDataObject *iface)
257 IDataObjectImpl *This = impl_from_IDataObject(iface);
258 ULONG refCount = InterlockedIncrement(&This->ref);
260 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
262 return refCount;
265 /**************************************************************************
266 * IDataObject_Release
268 static ULONG WINAPI IDataObject_fnRelease(IDataObject *iface)
270 IDataObjectImpl *This = impl_from_IDataObject(iface);
271 ULONG refCount = InterlockedDecrement(&This->ref);
273 TRACE("(%p)->(%u)\n", This, refCount + 1);
275 if (!refCount)
277 TRACE(" destroying IDataObject(%p)\n",This);
278 _ILFreeaPidl(This->apidl, This->cidl);
279 ILFree(This->pidl),
280 HeapFree(GetProcessHeap(),0,This);
282 return refCount;
285 /**************************************************************************
286 * IDataObject_fnGetData
288 static HRESULT WINAPI IDataObject_fnGetData(IDataObject *iface, LPFORMATETC pformatetcIn, STGMEDIUM *pmedium)
290 IDataObjectImpl *This = impl_from_IDataObject(iface);
292 char szTemp[256];
294 szTemp[0]=0;
295 GetClipboardFormatNameA (pformatetcIn->cfFormat, szTemp, 256);
296 TRACE("(%p)->(%p %p format=%s)\n", This, pformatetcIn, pmedium, szTemp);
298 if (pformatetcIn->cfFormat == This->cfShellIDList)
300 if (This->cidl < 1) return(E_UNEXPECTED);
301 pmedium->u.hGlobal = RenderSHELLIDLIST(This->pidl, This->apidl, This->cidl);
303 else if (pformatetcIn->cfFormat == CF_HDROP)
305 if (This->cidl < 1) return(E_UNEXPECTED);
306 pmedium->u.hGlobal = RenderHDROP(This->pidl, This->apidl, This->cidl);
308 else if (pformatetcIn->cfFormat == This->cfFileNameA)
310 if (This->cidl < 1) return(E_UNEXPECTED);
311 pmedium->u.hGlobal = RenderFILENAMEA(This->pidl, This->apidl, This->cidl);
313 else if (pformatetcIn->cfFormat == This->cfFileNameW)
315 if (This->cidl < 1) return(E_UNEXPECTED);
316 pmedium->u.hGlobal = RenderFILENAMEW(This->pidl, This->apidl, This->cidl);
318 else
320 FIXME("-- expected clipformat not implemented\n");
321 return (E_INVALIDARG);
323 if (pmedium->u.hGlobal)
325 pmedium->tymed = TYMED_HGLOBAL;
326 pmedium->pUnkForRelease = NULL;
327 return S_OK;
329 return E_OUTOFMEMORY;
332 static HRESULT WINAPI IDataObject_fnGetDataHere(IDataObject *iface, LPFORMATETC pformatetc, STGMEDIUM *pmedium)
334 IDataObjectImpl *This = impl_from_IDataObject(iface);
335 FIXME("(%p)->()\n", This);
336 return E_NOTIMPL;
339 static HRESULT WINAPI IDataObject_fnQueryGetData(IDataObject *iface, LPFORMATETC pformatetc)
341 IDataObjectImpl *This = impl_from_IDataObject(iface);
342 UINT i;
344 TRACE("(%p)->(fmt=0x%08x tym=0x%08x)\n", This, pformatetc->cfFormat, pformatetc->tymed);
346 if(!(DVASPECT_CONTENT & pformatetc->dwAspect))
347 return DV_E_DVASPECT;
349 /* check our formats table what we have */
350 for (i=0; i<MAX_FORMATS; i++)
352 if ((This->pFormatEtc[i].cfFormat == pformatetc->cfFormat)
353 && (This->pFormatEtc[i].tymed == pformatetc->tymed))
355 return S_OK;
359 return DV_E_TYMED;
362 static HRESULT WINAPI IDataObject_fnGetCanonicalFormatEtc(IDataObject *iface, LPFORMATETC pformatectIn, LPFORMATETC pformatetcOut)
364 IDataObjectImpl *This = impl_from_IDataObject(iface);
365 FIXME("(%p)->()\n", This);
366 return E_NOTIMPL;
369 static HRESULT WINAPI IDataObject_fnSetData(IDataObject *iface, LPFORMATETC pformatetc, STGMEDIUM *pmedium, BOOL fRelease)
371 IDataObjectImpl *This = impl_from_IDataObject(iface);
372 FIXME("(%p)->()\n", This);
373 return E_NOTIMPL;
376 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(IDataObject *iface, DWORD dwDirection, IEnumFORMATETC **ppenumFormatEtc)
378 IDataObjectImpl *This = impl_from_IDataObject(iface);
380 TRACE("(%p)->()\n", This);
381 *ppenumFormatEtc=NULL;
383 /* only get data */
384 if (DATADIR_GET == dwDirection)
386 *ppenumFormatEtc = IEnumFORMATETC_Constructor(MAX_FORMATS, This->pFormatEtc);
387 return (*ppenumFormatEtc) ? S_OK : E_FAIL;
390 return E_NOTIMPL;
393 static HRESULT WINAPI IDataObject_fnDAdvise(IDataObject *iface, FORMATETC *pformatetc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection)
395 IDataObjectImpl *This = impl_from_IDataObject(iface);
396 FIXME("(%p)->()\n", This);
397 return E_NOTIMPL;
399 static HRESULT WINAPI IDataObject_fnDUnadvise(IDataObject *iface, DWORD dwConnection)
401 IDataObjectImpl *This = impl_from_IDataObject(iface);
402 FIXME("(%p)->()\n", This);
403 return E_NOTIMPL;
405 static HRESULT WINAPI IDataObject_fnEnumDAdvise(IDataObject *iface, IEnumSTATDATA **ppenumAdvise)
407 IDataObjectImpl *This = impl_from_IDataObject(iface);
408 FIXME("(%p)->()\n", This);
409 return E_NOTIMPL;
412 static const IDataObjectVtbl dtovt =
414 IDataObject_fnQueryInterface,
415 IDataObject_fnAddRef,
416 IDataObject_fnRelease,
417 IDataObject_fnGetData,
418 IDataObject_fnGetDataHere,
419 IDataObject_fnQueryGetData,
420 IDataObject_fnGetCanonicalFormatEtc,
421 IDataObject_fnSetData,
422 IDataObject_fnEnumFormatEtc,
423 IDataObject_fnDAdvise,
424 IDataObject_fnDUnadvise,
425 IDataObject_fnEnumDAdvise
428 /**************************************************************************
429 * IDataObject_Constructor
431 IDataObject* IDataObject_Constructor(HWND hwndOwner,
432 LPCITEMIDLIST pMyPidl, LPCITEMIDLIST * apidl, UINT cidl)
434 IDataObjectImpl* dto;
436 dto = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDataObjectImpl));
438 if (dto)
440 dto->ref = 1;
441 dto->IDataObject_iface.lpVtbl = &dtovt;
442 dto->pidl = ILClone(pMyPidl);
443 dto->apidl = _ILCopyaPidl(apidl, cidl);
444 dto->cidl = cidl;
446 dto->cfShellIDList = RegisterClipboardFormatW(CFSTR_SHELLIDLISTW);
447 dto->cfFileNameA = RegisterClipboardFormatA(CFSTR_FILENAMEA);
448 dto->cfFileNameW = RegisterClipboardFormatW(CFSTR_FILENAMEW);
449 InitFormatEtc(dto->pFormatEtc[0], dto->cfShellIDList, TYMED_HGLOBAL);
450 InitFormatEtc(dto->pFormatEtc[1], CF_HDROP, TYMED_HGLOBAL);
451 InitFormatEtc(dto->pFormatEtc[2], dto->cfFileNameA, TYMED_HGLOBAL);
452 InitFormatEtc(dto->pFormatEtc[3], dto->cfFileNameW, TYMED_HGLOBAL);
455 TRACE("(%p)->(apidl=%p cidl=%u)\n",dto, apidl, cidl);
456 return &dto->IDataObject_iface;