Implemented a Wine-only scheme for interprocess WinHelp message
[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         if(*ppenum)
193            IEnumFORMATETC_fnSkip(*ppenum, This->posFmt);
194         return S_OK;
195 }
196
197
198 /***********************************************************************
199 *   IDataObject implementation
200 */
201
202 /* number of supported formats */
203 #define MAX_FORMATS 3
204
205 typedef struct
206 {
207         /* IUnknown fields */
208         ICOM_VFIELD(IDataObject);
209         DWORD           ref;
210
211         /* IDataObject fields */
212         LPITEMIDLIST    pidl;
213         LPITEMIDLIST *  apidl;
214         UINT            cidl;
215
216         FORMATETC       pFormatEtc[MAX_FORMATS];
217         UINT            cfShellIDList;
218         UINT            cfFileName;
219
220 } IDataObjectImpl;
221
222 static struct ICOM_VTABLE(IDataObject) dtovt;
223
224 /**************************************************************************
225 *  IDataObject_Constructor
226 */
227 LPDATAOBJECT IDataObject_Constructor(HWND hwndOwner, LPITEMIDLIST pMyPidl, LPITEMIDLIST * apidl, UINT cidl)
228 {
229         IDataObjectImpl* dto;
230
231         dto = (IDataObjectImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDataObjectImpl));
232
233         if (dto)
234         {
235           dto->ref = 1;
236           ICOM_VTBL(dto) = &dtovt;
237           dto->pidl = ILClone(pMyPidl);
238           dto->apidl = _ILCopyaPidl(apidl, cidl);
239           dto->cidl = cidl;
240
241           dto->cfShellIDList = RegisterClipboardFormatA(CFSTR_SHELLIDLIST);
242           dto->cfFileName = RegisterClipboardFormatA(CFSTR_FILENAMEA);
243           InitFormatEtc(dto->pFormatEtc[0], dto->cfShellIDList, TYMED_HGLOBAL);
244           InitFormatEtc(dto->pFormatEtc[1], CF_HDROP, TYMED_HGLOBAL);
245           InitFormatEtc(dto->pFormatEtc[2], dto->cfFileName, TYMED_HGLOBAL);
246         }
247
248         TRACE("(%p)->(apidl=%p cidl=%u)\n",dto, apidl, cidl);
249         return (LPDATAOBJECT)dto;
250 }
251
252 /***************************************************************************
253 *  IDataObject_QueryInterface
254 */
255 static HRESULT WINAPI IDataObject_fnQueryInterface(LPDATAOBJECT iface, REFIID riid, LPVOID * ppvObj)
256 {
257         ICOM_THIS(IDataObjectImpl,iface);
258         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
259
260         *ppvObj = NULL;
261
262         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
263         {
264           *ppvObj = This;
265         }
266         else if(IsEqualIID(riid, &IID_IDataObject))  /*IDataObject*/
267         {
268           *ppvObj = (IDataObject*)This;
269         }
270
271         if(*ppvObj)
272         {
273           IUnknown_AddRef((IUnknown*)*ppvObj);
274           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
275           return S_OK;
276         }
277         TRACE("-- Interface: E_NOINTERFACE\n");
278         return E_NOINTERFACE;
279 }
280
281 /**************************************************************************
282 *  IDataObject_AddRef
283 */
284 static ULONG WINAPI IDataObject_fnAddRef(LPDATAOBJECT iface)
285 {
286         ICOM_THIS(IDataObjectImpl,iface);
287         TRACE("(%p)->(count=%lu)\n",This, This->ref);
288         return ++(This->ref);
289 }
290
291 /**************************************************************************
292 *  IDataObject_Release
293 */
294 static ULONG WINAPI IDataObject_fnRelease(LPDATAOBJECT iface)
295 {
296         ICOM_THIS(IDataObjectImpl,iface);
297         TRACE("(%p)->()\n",This);
298
299         if (!--(This->ref))
300         {
301           TRACE(" destroying IDataObject(%p)\n",This);
302           _ILFreeaPidl(This->apidl, This->cidl);
303           ILFree(This->pidl),
304           HeapFree(GetProcessHeap(),0,This);
305           return 0;
306         }
307         return This->ref;
308 }
309
310 /**************************************************************************
311 * IDataObject_fnGetData
312 */
313 static HRESULT WINAPI IDataObject_fnGetData(LPDATAOBJECT iface, LPFORMATETC pformatetcIn, STGMEDIUM *pmedium)
314 {
315         ICOM_THIS(IDataObjectImpl,iface);
316
317         char    szTemp[256];
318
319         szTemp[0]=0;
320         GetClipboardFormatNameA (pformatetcIn->cfFormat, szTemp, 256);
321         TRACE("(%p)->(%p %p format=%s)\n", This, pformatetcIn, pmedium, szTemp);
322
323         if (pformatetcIn->cfFormat == This->cfShellIDList)
324         {
325           if (This->cidl < 1) return(E_UNEXPECTED);
326           pmedium->u.hGlobal = RenderSHELLIDLIST(This->pidl, This->apidl, This->cidl);
327         }
328         else if (pformatetcIn->cfFormat == CF_HDROP)
329         {
330           if (This->cidl < 1) return(E_UNEXPECTED);
331           pmedium->u.hGlobal = RenderHDROP(This->pidl, This->apidl, This->cidl);
332         }
333         else if (pformatetcIn->cfFormat == This->cfFileName)
334         {
335           if (This->cidl < 1) return(E_UNEXPECTED);
336           pmedium->u.hGlobal = RenderFILENAME(This->pidl, This->apidl, This->cidl);
337         }
338         else
339         {
340           FIXME("-- expected clipformat not implemented\n");
341           return (E_INVALIDARG);
342         }
343         if (pmedium->u.hGlobal)
344         {
345           pmedium->tymed = TYMED_HGLOBAL;
346           pmedium->pUnkForRelease = NULL;
347           return S_OK;
348         }
349         return E_OUTOFMEMORY;
350 }
351
352 static HRESULT WINAPI IDataObject_fnGetDataHere(LPDATAOBJECT iface, LPFORMATETC pformatetc, STGMEDIUM *pmedium)
353 {
354         ICOM_THIS(IDataObjectImpl,iface);
355         FIXME("(%p)->()\n", This);
356         return E_NOTIMPL;
357 }
358
359 static HRESULT WINAPI IDataObject_fnQueryGetData(LPDATAOBJECT iface, LPFORMATETC pformatetc)
360 {
361         ICOM_THIS(IDataObjectImpl,iface);
362         UINT i;
363
364         TRACE("(%p)->(fmt=0x%08x tym=0x%08lx)\n", This, pformatetc->cfFormat, pformatetc->tymed);
365
366         if(!(DVASPECT_CONTENT & pformatetc->dwAspect))
367           return DV_E_DVASPECT;
368
369         /* check our formats table what we have */
370         for (i=0; i<MAX_FORMATS; i++)
371         {
372           if ((This->pFormatEtc[i].cfFormat == pformatetc->cfFormat)
373            && (This->pFormatEtc[i].tymed == pformatetc->tymed))
374           {
375             return S_OK;
376           }
377         }
378
379         return DV_E_TYMED;
380 }
381
382 static HRESULT WINAPI IDataObject_fnGetCanonicalFormatEtc(LPDATAOBJECT iface, LPFORMATETC pformatectIn, LPFORMATETC pformatetcOut)
383 {
384         ICOM_THIS(IDataObjectImpl,iface);
385         FIXME("(%p)->()\n", This);
386         return E_NOTIMPL;
387 }
388
389 static HRESULT WINAPI IDataObject_fnSetData(LPDATAOBJECT iface, LPFORMATETC pformatetc, STGMEDIUM *pmedium, BOOL fRelease)
390 {
391         ICOM_THIS(IDataObjectImpl,iface);
392         FIXME("(%p)->()\n", This);
393         return E_NOTIMPL;
394 }
395
396 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(LPDATAOBJECT iface, DWORD dwDirection, IEnumFORMATETC **ppenumFormatEtc)
397 {
398         ICOM_THIS(IDataObjectImpl,iface);
399
400         TRACE("(%p)->()\n", This);
401         *ppenumFormatEtc=NULL;
402
403         /* only get data */
404         if (DATADIR_GET == dwDirection)
405         {
406           *ppenumFormatEtc = IEnumFORMATETC_Constructor(MAX_FORMATS, This->pFormatEtc);
407           return (*ppenumFormatEtc) ? S_OK : E_FAIL;
408         }
409
410         return E_NOTIMPL;
411 }
412
413 static HRESULT WINAPI IDataObject_fnDAdvise(LPDATAOBJECT iface, FORMATETC *pformatetc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection)
414 {
415         ICOM_THIS(IDataObjectImpl,iface);
416         FIXME("(%p)->()\n", This);
417         return E_NOTIMPL;
418 }
419 static HRESULT WINAPI IDataObject_fnDUnadvise(LPDATAOBJECT iface, DWORD dwConnection)
420 {
421         ICOM_THIS(IDataObjectImpl,iface);
422         FIXME("(%p)->()\n", This);
423         return E_NOTIMPL;
424 }
425 static HRESULT WINAPI IDataObject_fnEnumDAdvise(LPDATAOBJECT iface, IEnumSTATDATA **ppenumAdvise)
426 {
427         ICOM_THIS(IDataObjectImpl,iface);
428         FIXME("(%p)->()\n", This);
429         return E_NOTIMPL;
430 }
431
432 static struct ICOM_VTABLE(IDataObject) dtovt =
433 {
434         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
435         IDataObject_fnQueryInterface,
436         IDataObject_fnAddRef,
437         IDataObject_fnRelease,
438         IDataObject_fnGetData,
439         IDataObject_fnGetDataHere,
440         IDataObject_fnQueryGetData,
441         IDataObject_fnGetCanonicalFormatEtc,
442         IDataObject_fnSetData,
443         IDataObject_fnEnumFormatEtc,
444         IDataObject_fnDAdvise,
445         IDataObject_fnDUnadvise,
446         IDataObject_fnEnumDAdvise
447 };