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