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