quartz: Change VideoRenderer_Run to handle EndOfStream slightly better.
[wine] / dlls / ole32 / ole2impl.c
1 /*
2  * Ole 2 Create functions implementation
3  *
4  * Copyright (C) 1999-2000 Abey George
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "wine/debug.h"
33 #include "ole2.h"
34 #include "olestd.h"
35 #include "compobj_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
38
39 #define MAX_CLIPFORMAT_NAME   80
40
41 /******************************************************************************
42  *              OleQueryCreateFromData [OLE32.@]
43  *
44  * Checks whether an object can become an embedded object.
45  * the clipboard or OLE drag and drop.
46  * Returns  : S_OK - Format that supports Embedded object creation are present.
47  *            OLE_E_STATIC - Format that supports static object creation are present.
48  *            S_FALSE - No acceptable format is available.
49  */
50
51 HRESULT WINAPI OleQueryCreateFromData(IDataObject *data)
52 {
53     IEnumFORMATETC *enum_fmt;
54     FORMATETC fmt;
55     BOOL found_static = FALSE;
56     HRESULT hr;
57
58     hr = IDataObject_EnumFormatEtc(data, DATADIR_GET, &enum_fmt);
59
60     if(FAILED(hr)) return hr;
61
62     do
63     {
64         hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
65         if(hr == S_OK)
66         {
67             if(fmt.cfFormat == embedded_object_clipboard_format ||
68                fmt.cfFormat == embed_source_clipboard_format ||
69                fmt.cfFormat == filename_clipboard_format)
70             {
71                 IEnumFORMATETC_Release(enum_fmt);
72                 return S_OK;
73             }
74
75             if(fmt.cfFormat == CF_METAFILEPICT ||
76                fmt.cfFormat == CF_BITMAP ||
77                fmt.cfFormat == CF_DIB)
78                 found_static = TRUE;
79         }
80     } while (hr == S_OK);
81
82     IEnumFORMATETC_Release(enum_fmt);
83
84     return found_static ? OLE_S_STATIC : S_FALSE;
85 }
86
87 static inline void init_fmtetc(FORMATETC *fmt, CLIPFORMAT cf, TYMED tymed)
88 {
89     fmt->cfFormat = cf;
90     fmt->ptd = NULL;
91     fmt->dwAspect = DVASPECT_CONTENT;
92     fmt->lindex = -1;
93     fmt->tymed = tymed;
94 }
95
96 /***************************************************************************
97  *         get_storage
98  *
99  * Retrieve an object's storage from a variety of sources.
100  *
101  * FIXME: CF_FILENAME.
102  */
103 static HRESULT get_storage(IDataObject *data, IStorage *stg, UINT *src_cf)
104 {
105     HRESULT hr;
106     FORMATETC fmt;
107     STGMEDIUM med;
108     IPersistStorage *persist;
109     CLSID clsid;
110
111     *src_cf = 0;
112
113     /* CF_EMBEDEDOBJECT */
114     init_fmtetc(&fmt, embedded_object_clipboard_format, TYMED_ISTORAGE);
115     med.tymed = TYMED_ISTORAGE;
116     med.u.pstg = stg;
117     hr = IDataObject_GetDataHere(data, &fmt, &med);
118     if(SUCCEEDED(hr))
119     {
120         *src_cf = embedded_object_clipboard_format;
121         return hr;
122     }
123
124     /* CF_EMBEDSOURCE */
125     init_fmtetc(&fmt, embed_source_clipboard_format, TYMED_ISTORAGE);
126     med.tymed = TYMED_ISTORAGE;
127     med.u.pstg = stg;
128     hr = IDataObject_GetDataHere(data, &fmt, &med);
129     if(SUCCEEDED(hr))
130     {
131         *src_cf = embed_source_clipboard_format;
132         return hr;
133     }
134
135     /* IPersistStorage */
136     hr = IDataObject_QueryInterface(data, &IID_IPersistStorage, (void**)&persist);
137     if(FAILED(hr)) return hr;
138
139     hr = IPersistStorage_GetClassID(persist, &clsid);
140     if(FAILED(hr)) goto end;
141
142     hr = IStorage_SetClass(stg, &clsid);
143     if(FAILED(hr)) goto end;
144
145     hr = IPersistStorage_Save(persist, stg, FALSE);
146     if(FAILED(hr)) goto end;
147
148     hr = IPersistStorage_SaveCompleted(persist, NULL);
149
150 end:
151     IPersistStorage_Release(persist);
152
153     return hr;
154 }
155
156 /******************************************************************************
157  *              OleCreateFromDataEx        [OLE32.@]
158  *
159  * Creates an embedded object from data transfer object retrieved from
160  * the clipboard or OLE drag and drop.
161  */
162 HRESULT WINAPI OleCreateFromDataEx(IDataObject *data, REFIID iid, DWORD flags,
163                                    DWORD renderopt, ULONG num_cache_fmts, DWORD *adv_flags, FORMATETC *cache_fmts,
164                                    IAdviseSink *sink, DWORD *conns,
165                                    IOleClientSite *client_site, IStorage *stg, void **obj)
166 {
167     HRESULT hr;
168     UINT src_cf;
169
170     FIXME("(%p, %s, %08x, %08x, %d, %p, %p, %p, %p, %p, %p, %p): stub\n",
171           data, debugstr_guid(iid), flags, renderopt, num_cache_fmts, adv_flags, cache_fmts,
172           sink, conns, client_site, stg, obj);
173
174     hr = get_storage(data, stg, &src_cf);
175     if(FAILED(hr)) return hr;
176
177     hr = OleLoad(stg, iid, client_site, obj);
178     if(FAILED(hr)) return hr;
179
180     /* FIXME: Init cache */
181
182     return hr;
183 }
184
185 /******************************************************************************
186  *              OleCreateFromData        [OLE32.@]
187  */
188 HRESULT WINAPI OleCreateFromData(LPDATAOBJECT data, REFIID iid,
189                                  DWORD renderopt, LPFORMATETC fmt,
190                                  LPOLECLIENTSITE client_site, LPSTORAGE stg,
191                                  LPVOID* obj)
192 {
193     DWORD advf = ADVF_PRIMEFIRST;
194
195     return OleCreateFromDataEx(data, iid, 0, renderopt, fmt ? 1 : 0, fmt ? &advf : NULL,
196                                fmt, NULL, NULL, client_site, stg, obj);
197 }
198
199
200 /******************************************************************************
201  *              OleDuplicateData        [OLE32.@]
202  *
203  * Duplicates clipboard data.
204  *
205  * PARAMS
206  *  hSrc     [I] Handle of the source clipboard data.
207  *  cfFormat [I] The clipboard format of hSrc.
208  *  uiFlags  [I] Flags to pass to GlobalAlloc.
209  *
210  * RETURNS
211  *  Success: handle to the duplicated data.
212  *  Failure: NULL.
213  */
214 HANDLE WINAPI OleDuplicateData(HANDLE hSrc, CLIPFORMAT cfFormat,
215                                   UINT uiFlags)
216 {
217     HANDLE hDst = NULL;
218
219     TRACE("(%p,%x,%x)\n", hSrc, cfFormat, uiFlags);
220
221     if (!uiFlags) uiFlags = GMEM_MOVEABLE;
222
223     switch (cfFormat)
224     {
225     case CF_ENHMETAFILE:
226         hDst = CopyEnhMetaFileW(hSrc, NULL);
227         break;
228     case CF_METAFILEPICT:
229         hDst = CopyMetaFileW(hSrc, NULL);
230         break;
231     case CF_PALETTE:
232         {
233             LOGPALETTE * logpalette;
234             UINT nEntries = GetPaletteEntries(hSrc, 0, 0, NULL);
235             if (!nEntries) return NULL;
236             logpalette = HeapAlloc(GetProcessHeap(), 0,
237                 FIELD_OFFSET(LOGPALETTE, palPalEntry[nEntries]));
238             if (!logpalette) return NULL;
239             if (!GetPaletteEntries(hSrc, 0, nEntries, logpalette->palPalEntry))
240             {
241                 HeapFree(GetProcessHeap(), 0, logpalette);
242                 return NULL;
243             }
244             logpalette->palVersion = 0x300;
245             logpalette->palNumEntries = (WORD)nEntries;
246
247             hDst = CreatePalette(logpalette);
248
249             HeapFree(GetProcessHeap(), 0, logpalette);
250             break;
251         }
252     case CF_BITMAP:
253         {
254             LONG size;
255             BITMAP bm;
256             if (!GetObjectW(hSrc, sizeof(bm), &bm))
257                 return NULL;
258             size = GetBitmapBits(hSrc, 0, NULL);
259             if (!size) return NULL;
260             bm.bmBits = HeapAlloc(GetProcessHeap(), 0, size);
261             if (!bm.bmBits) return NULL;
262             if (GetBitmapBits(hSrc, size, bm.bmBits))
263                 hDst = CreateBitmapIndirect(&bm);
264             HeapFree(GetProcessHeap(), 0, bm.bmBits);
265             break;
266         }
267     default:
268         {
269             SIZE_T size = GlobalSize(hSrc);
270             LPVOID pvSrc = NULL;
271             LPVOID pvDst = NULL;
272
273             /* allocate space for object */
274             if (!size) return NULL;
275             hDst = GlobalAlloc(uiFlags, size);
276             if (!hDst) return NULL;
277
278             /* lock pointers */
279             pvSrc = GlobalLock(hSrc);
280             if (!pvSrc)
281             {
282                 GlobalFree(hDst);
283                 return NULL;
284             }
285             pvDst = GlobalLock(hDst);
286             if (!pvDst)
287             {
288                 GlobalUnlock(hSrc);
289                 GlobalFree(hDst);
290                 return NULL;
291             }
292             /* copy data */
293             memcpy(pvDst, pvSrc, size);
294
295             /* cleanup */
296             GlobalUnlock(hDst);
297             GlobalUnlock(hSrc);
298         }
299     }
300
301     TRACE("returning %p\n", hDst);
302     return hDst;
303 }