Implemented SHCreateStdEnumFmtEtc.
[wine] / dlls / shell32 / dataobject.c
1 /*
2  *      IEnumFORMATETC, IDataObject
3  *
4  * selecting and droping objects within the shell and/or common dialogs
5  *
6  *      Copyright 1998, 1999    <juergen.schmied@metronet.de>
7  *
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.
12  *
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.
17  *
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
21  */
22 #include <string.h>
23
24 #include "pidl.h"
25 #include "winerror.h"
26 #include "shell32_main.h"
27 #include "wine/debug.h"
28 #include "undocshell.h"
29 #include "wine/obj_dataobject.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(shell);
32
33 /***********************************************************************
34 *   IEnumFORMATETC implementation
35 */
36
37 typedef struct
38 {
39     /* IUnknown fields */
40     ICOM_VFIELD(IEnumFORMATETC);
41     DWORD                        ref;
42     /* IEnumFORMATETC fields */
43     UINT        posFmt;
44     UINT        countFmt;
45     LPFORMATETC pFmt;
46 } IEnumFORMATETCImpl;
47
48 static HRESULT WINAPI IEnumFORMATETC_fnQueryInterface(LPENUMFORMATETC iface, REFIID riid, LPVOID* ppvObj);
49 static ULONG WINAPI IEnumFORMATETC_fnAddRef(LPENUMFORMATETC iface);
50 static ULONG WINAPI IEnumFORMATETC_fnRelease(LPENUMFORMATETC iface);
51 static HRESULT WINAPI IEnumFORMATETC_fnNext(LPENUMFORMATETC iface, ULONG celt, FORMATETC* rgelt, ULONG* pceltFethed);
52 static HRESULT WINAPI IEnumFORMATETC_fnSkip(LPENUMFORMATETC iface, ULONG celt);
53 static HRESULT WINAPI IEnumFORMATETC_fnReset(LPENUMFORMATETC iface);
54 static HRESULT WINAPI IEnumFORMATETC_fnClone(LPENUMFORMATETC iface, LPENUMFORMATETC* ppenum);
55
56 static struct ICOM_VTABLE(IEnumFORMATETC) efvt =
57 {
58     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
59         IEnumFORMATETC_fnQueryInterface,
60         IEnumFORMATETC_fnAddRef,
61     IEnumFORMATETC_fnRelease,
62     IEnumFORMATETC_fnNext,
63     IEnumFORMATETC_fnSkip,
64     IEnumFORMATETC_fnReset,
65     IEnumFORMATETC_fnClone
66 };
67
68 LPENUMFORMATETC IEnumFORMATETC_Constructor(UINT cfmt, const FORMATETC afmt[])
69 {
70         IEnumFORMATETCImpl* ef;
71         DWORD size=cfmt * sizeof(FORMATETC);
72
73         ef=(IEnumFORMATETCImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumFORMATETCImpl));
74
75         if(ef)
76         {
77           ef->ref=1;
78           ICOM_VTBL(ef)=&efvt;
79
80           ef->countFmt = cfmt;
81           ef->pFmt = SHAlloc (size);
82
83           if (ef->pFmt)
84           {
85             memcpy(ef->pFmt, afmt, size);
86           }
87         }
88
89         TRACE("(%p)->(%u,%p)\n",ef, cfmt, afmt);
90         return (LPENUMFORMATETC)ef;
91 }
92
93 static HRESULT WINAPI IEnumFORMATETC_fnQueryInterface(LPENUMFORMATETC iface, REFIID riid, LPVOID* ppvObj)
94 {
95         ICOM_THIS(IEnumFORMATETCImpl,iface);
96         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
97
98         *ppvObj = NULL;
99
100         if(IsEqualIID(riid, &IID_IUnknown))
101         {
102           *ppvObj = This;
103         }
104         else if(IsEqualIID(riid, &IID_IEnumFORMATETC))
105         {
106           *ppvObj = (IEnumFORMATETC*)This;
107         }
108
109         if(*ppvObj)
110         {
111           IUnknown_AddRef((IUnknown*)(*ppvObj));
112           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
113           return S_OK;
114         }
115         TRACE("-- Interface: E_NOINTERFACE\n");
116         return E_NOINTERFACE;
117
118 }
119
120 static ULONG WINAPI IEnumFORMATETC_fnAddRef(LPENUMFORMATETC iface)
121 {
122         ICOM_THIS(IEnumFORMATETCImpl,iface);
123         TRACE("(%p)->(count=%lu)\n",This, This->ref);
124         return ++(This->ref);
125 }
126
127 static ULONG WINAPI IEnumFORMATETC_fnRelease(LPENUMFORMATETC iface)
128 {
129         ICOM_THIS(IEnumFORMATETCImpl,iface);
130         TRACE("(%p)->()\n",This);
131
132         if (!--(This->ref))
133         {
134           TRACE(" destroying IEnumFORMATETC(%p)\n",This);
135           if (This->pFmt)
136           {
137             SHFree (This->pFmt);
138           }
139           HeapFree(GetProcessHeap(),0,This);
140           return 0;
141         }
142         return This->ref;
143 }
144
145 static HRESULT WINAPI IEnumFORMATETC_fnNext(LPENUMFORMATETC iface, ULONG celt, FORMATETC *rgelt, ULONG *pceltFethed)
146 {
147         ICOM_THIS(IEnumFORMATETCImpl,iface);
148         int i;
149
150         TRACE("(%p)->(%lu,%p)\n", This, celt, rgelt);
151
152         if(!This->pFmt)return S_FALSE;
153         if(!rgelt) return E_INVALIDARG;
154         if (pceltFethed)  *pceltFethed = 0;
155
156         for(i = 0; This->posFmt < This->countFmt && celt > i; i++)
157         {
158           *rgelt++ = This->pFmt[This->posFmt++];
159         }
160
161         if (pceltFethed) *pceltFethed = i;
162
163         return ((i == celt) ? S_OK : S_FALSE);
164 }
165
166 static HRESULT WINAPI IEnumFORMATETC_fnSkip(LPENUMFORMATETC iface, ULONG celt)
167 {
168         ICOM_THIS(IEnumFORMATETCImpl,iface);
169         TRACE("(%p)->(num=%lu)\n", This, celt);
170
171         if((This->posFmt + celt) >= This->countFmt) return S_FALSE;
172         This->posFmt += celt;
173         return S_OK;
174 }
175
176 static HRESULT WINAPI IEnumFORMATETC_fnReset(LPENUMFORMATETC iface)
177 {
178         ICOM_THIS(IEnumFORMATETCImpl,iface);
179         TRACE("(%p)->()\n", This);
180
181         This->posFmt = 0;
182         return S_OK;
183 }
184
185 static HRESULT WINAPI IEnumFORMATETC_fnClone(LPENUMFORMATETC iface, LPENUMFORMATETC* ppenum)
186 {
187         ICOM_THIS(IEnumFORMATETCImpl,iface);
188         TRACE("(%p)->(ppenum=%p)\n", This, ppenum);
189
190         if (!ppenum) return E_INVALIDARG;
191         *ppenum = IEnumFORMATETC_Constructor(This->countFmt, This->pFmt);
192         return S_OK;
193 }
194
195
196 /***********************************************************************
197 *   IDataObject implementation
198 */
199
200 /* number of supported formats */
201 #define MAX_FORMATS 3
202
203 typedef struct
204 {
205         /* IUnknown fields */
206         ICOM_VFIELD(IDataObject);
207         DWORD           ref;
208
209         /* IDataObject fields */
210         LPITEMIDLIST    pidl;
211         LPITEMIDLIST *  apidl;
212         UINT            cidl;
213
214         FORMATETC       pFormatEtc[MAX_FORMATS];
215         UINT            cfShellIDList;
216         UINT            cfFileName;
217
218 } IDataObjectImpl;
219
220 static struct ICOM_VTABLE(IDataObject) dtovt;
221
222 /**************************************************************************
223 *  IDataObject_Constructor
224 */
225 LPDATAOBJECT IDataObject_Constructor(HWND hwndOwner, LPITEMIDLIST pMyPidl, LPITEMIDLIST * apidl, UINT cidl)
226 {
227         IDataObjectImpl* dto;
228
229         dto = (IDataObjectImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDataObjectImpl));
230
231         if (dto)
232         {
233           dto->ref = 1;
234           ICOM_VTBL(dto) = &dtovt;
235           dto->pidl = ILClone(pMyPidl);
236           dto->apidl = _ILCopyaPidl(apidl, cidl);
237           dto->cidl = cidl;
238
239           dto->cfShellIDList = RegisterClipboardFormatA(CFSTR_SHELLIDLIST);
240           dto->cfFileName = RegisterClipboardFormatA(CFSTR_FILENAMEA);
241           InitFormatEtc(dto->pFormatEtc[0], dto->cfShellIDList, TYMED_HGLOBAL);
242           InitFormatEtc(dto->pFormatEtc[1], CF_HDROP, TYMED_HGLOBAL);
243           InitFormatEtc(dto->pFormatEtc[2], dto->cfFileName, TYMED_HGLOBAL);
244         }
245
246         TRACE("(%p)->(apidl=%p cidl=%u)\n",dto, apidl, cidl);
247         return (LPDATAOBJECT)dto;
248 }
249
250 /***************************************************************************
251 *  IDataObject_QueryInterface
252 */
253 static HRESULT WINAPI IDataObject_fnQueryInterface(LPDATAOBJECT iface, REFIID riid, LPVOID * ppvObj)
254 {
255         ICOM_THIS(IDataObjectImpl,iface);
256         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
257
258         *ppvObj = NULL;
259
260         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
261         {
262           *ppvObj = This;
263         }
264         else if(IsEqualIID(riid, &IID_IDataObject))  /*IDataObject*/
265         {
266           *ppvObj = (IDataObject*)This;
267         }
268
269         if(*ppvObj)
270         {
271           IUnknown_AddRef((IUnknown*)*ppvObj);
272           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
273           return S_OK;
274         }
275         TRACE("-- Interface: E_NOINTERFACE\n");
276         return E_NOINTERFACE;
277 }
278
279 /**************************************************************************
280 *  IDataObject_AddRef
281 */
282 static ULONG WINAPI IDataObject_fnAddRef(LPDATAOBJECT iface)
283 {
284         ICOM_THIS(IDataObjectImpl,iface);
285         TRACE("(%p)->(count=%lu)\n",This, This->ref);
286         return ++(This->ref);
287 }
288
289 /**************************************************************************
290 *  IDataObject_Release
291 */
292 static ULONG WINAPI IDataObject_fnRelease(LPDATAOBJECT iface)
293 {
294         ICOM_THIS(IDataObjectImpl,iface);
295         TRACE("(%p)->()\n",This);
296
297         if (!--(This->ref))
298         {
299           TRACE(" destroying IDataObject(%p)\n",This);
300           _ILFreeaPidl(This->apidl, This->cidl);
301           ILFree(This->pidl),
302           HeapFree(GetProcessHeap(),0,This);
303           return 0;
304         }
305         return This->ref;
306 }
307
308 /**************************************************************************
309 * IDataObject_fnGetData
310 */
311 static HRESULT WINAPI IDataObject_fnGetData(LPDATAOBJECT iface, LPFORMATETC pformatetcIn, STGMEDIUM *pmedium)
312 {
313         ICOM_THIS(IDataObjectImpl,iface);
314
315         char    szTemp[256];
316
317         szTemp[0]=0;
318         GetClipboardFormatNameA (pformatetcIn->cfFormat, szTemp, 256);
319         TRACE("(%p)->(%p %p format=%s)\n", This, pformatetcIn, pmedium, szTemp);
320
321         if (pformatetcIn->cfFormat == This->cfShellIDList)
322         {
323           if (This->cidl < 1) return(E_UNEXPECTED);
324           pmedium->u.hGlobal = RenderSHELLIDLIST(This->pidl, This->apidl, This->cidl);
325         }
326         else if (pformatetcIn->cfFormat == CF_HDROP)
327         {
328           if (This->cidl < 1) return(E_UNEXPECTED);
329           pmedium->u.hGlobal = RenderHDROP(This->pidl, This->apidl, This->cidl);
330         }
331         else if (pformatetcIn->cfFormat == This->cfFileName)
332         {
333           if (This->cidl < 1) return(E_UNEXPECTED);
334           pmedium->u.hGlobal = RenderFILENAME(This->pidl, This->apidl, This->cidl);
335         }
336         else
337         {
338           FIXME("-- expected clipformat not implemented\n");
339           return (E_INVALIDARG);
340         }
341         if (pmedium->u.hGlobal)
342         {
343           pmedium->tymed = TYMED_HGLOBAL;
344           pmedium->pUnkForRelease = NULL;
345           return S_OK;
346         }
347         return E_OUTOFMEMORY;
348 }
349
350 static HRESULT WINAPI IDataObject_fnGetDataHere(LPDATAOBJECT iface, LPFORMATETC pformatetc, STGMEDIUM *pmedium)
351 {
352         ICOM_THIS(IDataObjectImpl,iface);
353         FIXME("(%p)->()\n", This);
354         return E_NOTIMPL;
355 }
356
357 static HRESULT WINAPI IDataObject_fnQueryGetData(LPDATAOBJECT iface, LPFORMATETC pformatetc)
358 {
359         ICOM_THIS(IDataObjectImpl,iface);
360         UINT i;
361
362         TRACE("(%p)->(fmt=0x%08x tym=0x%08lx)\n", This, pformatetc->cfFormat, pformatetc->tymed);
363
364         if(!(DVASPECT_CONTENT & pformatetc->dwAspect))
365           return DV_E_DVASPECT;
366
367         /* check our formats table what we have */
368         for (i=0; i<MAX_FORMATS; i++)
369         {
370           if ((This->pFormatEtc[i].cfFormat == pformatetc->cfFormat)
371            && (This->pFormatEtc[i].tymed == pformatetc->tymed))
372           {
373             return S_OK;
374           }
375         }
376
377         return DV_E_TYMED;
378 }
379
380 static HRESULT WINAPI IDataObject_fnGetCanonicalFormatEtc(LPDATAOBJECT iface, LPFORMATETC pformatectIn, LPFORMATETC pformatetcOut)
381 {
382         ICOM_THIS(IDataObjectImpl,iface);
383         FIXME("(%p)->()\n", This);
384         return E_NOTIMPL;
385 }
386
387 static HRESULT WINAPI IDataObject_fnSetData(LPDATAOBJECT iface, LPFORMATETC pformatetc, STGMEDIUM *pmedium, BOOL fRelease)
388 {
389         ICOM_THIS(IDataObjectImpl,iface);
390         FIXME("(%p)->()\n", This);
391         return E_NOTIMPL;
392 }
393
394 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(LPDATAOBJECT iface, DWORD dwDirection, IEnumFORMATETC **ppenumFormatEtc)
395 {
396         ICOM_THIS(IDataObjectImpl,iface);
397
398         TRACE("(%p)->()\n", This);
399         *ppenumFormatEtc=NULL;
400
401         /* only get data */
402         if (DATADIR_GET == dwDirection)
403         {
404           *ppenumFormatEtc = IEnumFORMATETC_Constructor(MAX_FORMATS, This->pFormatEtc);
405           return (*ppenumFormatEtc) ? S_OK : E_FAIL;
406         }
407
408         return E_NOTIMPL;
409 }
410
411 static HRESULT WINAPI IDataObject_fnDAdvise(LPDATAOBJECT iface, FORMATETC *pformatetc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection)
412 {
413         ICOM_THIS(IDataObjectImpl,iface);
414         FIXME("(%p)->()\n", This);
415         return E_NOTIMPL;
416 }
417 static HRESULT WINAPI IDataObject_fnDUnadvise(LPDATAOBJECT iface, DWORD dwConnection)
418 {
419         ICOM_THIS(IDataObjectImpl,iface);
420         FIXME("(%p)->()\n", This);
421         return E_NOTIMPL;
422 }
423 static HRESULT WINAPI IDataObject_fnEnumDAdvise(LPDATAOBJECT iface, IEnumSTATDATA **ppenumAdvise)
424 {
425         ICOM_THIS(IDataObjectImpl,iface);
426         FIXME("(%p)->()\n", This);
427         return E_NOTIMPL;
428 }
429
430 static struct ICOM_VTABLE(IDataObject) dtovt =
431 {
432         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
433         IDataObject_fnQueryInterface,
434         IDataObject_fnAddRef,
435         IDataObject_fnRelease,
436         IDataObject_fnGetData,
437         IDataObject_fnGetDataHere,
438         IDataObject_fnQueryGetData,
439         IDataObject_fnGetCanonicalFormatEtc,
440         IDataObject_fnSetData,
441         IDataObject_fnEnumFormatEtc,
442         IDataObject_fnDAdvise,
443         IDataObject_fnDUnadvise,
444         IDataObject_fnEnumDAdvise
445 };