ole32: Implement OleCreateFromFileEx.
[wine.git] / dlls / ole32 / ole2impl.c
blobcdc54be8474d65057c1c68a1f31d900343f8ea41
1 /*
2 * Ole 2 Create functions implementation
4 * Copyright (C) 1999-2000 Abey George
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.
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.
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
21 #include <stdarg.h>
22 #include <string.h>
24 #define COBJMACROS
25 #define NONAMELESSUNION
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wine/debug.h"
32 #include "ole2.h"
33 #include "olestd.h"
34 #include "compobj_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ole);
38 /******************************************************************************
39 * OleQueryCreateFromData [OLE32.@]
41 * Checks whether an object can become an embedded object.
42 * the clipboard or OLE drag and drop.
43 * Returns : S_OK - Format that supports Embedded object creation are present.
44 * OLE_E_STATIC - Format that supports static object creation are present.
45 * S_FALSE - No acceptable format is available.
48 HRESULT WINAPI OleQueryCreateFromData(IDataObject *data)
50 IEnumFORMATETC *enum_fmt;
51 FORMATETC fmt;
52 BOOL found_static = FALSE;
53 HRESULT hr;
55 hr = IDataObject_EnumFormatEtc(data, DATADIR_GET, &enum_fmt);
57 if(FAILED(hr)) return hr;
61 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
62 if(hr == S_OK)
64 if(fmt.cfFormat == embedded_object_clipboard_format ||
65 fmt.cfFormat == embed_source_clipboard_format ||
66 fmt.cfFormat == filename_clipboard_format)
68 IEnumFORMATETC_Release(enum_fmt);
69 return S_OK;
72 if(fmt.cfFormat == CF_METAFILEPICT ||
73 fmt.cfFormat == CF_BITMAP ||
74 fmt.cfFormat == CF_DIB)
75 found_static = TRUE;
77 } while (hr == S_OK);
79 IEnumFORMATETC_Release(enum_fmt);
81 return found_static ? OLE_S_STATIC : S_FALSE;
84 static inline void init_fmtetc(FORMATETC *fmt, CLIPFORMAT cf, TYMED tymed)
86 fmt->cfFormat = cf;
87 fmt->ptd = NULL;
88 fmt->dwAspect = DVASPECT_CONTENT;
89 fmt->lindex = -1;
90 fmt->tymed = tymed;
93 /***************************************************************************
94 * get_storage
96 * Retrieve an object's storage from a variety of sources.
98 * FIXME: CF_FILENAME.
100 static HRESULT get_storage(IDataObject *data, IStorage *stg, UINT *src_cf, BOOL other_fmts)
102 static const UINT fmt_id[] = { CF_METAFILEPICT, CF_BITMAP, CF_DIB };
103 UINT i;
104 HRESULT hr;
105 FORMATETC fmt;
106 STGMEDIUM med;
107 IPersistStorage *persist;
108 CLSID clsid;
110 if (src_cf) *src_cf = 0;
112 /* CF_EMBEDEDOBJECT */
113 init_fmtetc(&fmt, embedded_object_clipboard_format, TYMED_ISTORAGE);
114 med.tymed = TYMED_ISTORAGE;
115 med.u.pstg = stg;
116 hr = IDataObject_GetDataHere(data, &fmt, &med);
117 if(SUCCEEDED(hr))
119 if (src_cf) *src_cf = embedded_object_clipboard_format;
120 return hr;
123 /* CF_EMBEDSOURCE */
124 init_fmtetc(&fmt, embed_source_clipboard_format, TYMED_ISTORAGE);
125 med.tymed = TYMED_ISTORAGE;
126 med.u.pstg = stg;
127 hr = IDataObject_GetDataHere(data, &fmt, &med);
128 if(SUCCEEDED(hr))
130 if (src_cf) *src_cf = embed_source_clipboard_format;
131 return hr;
134 if (other_fmts)
136 for (i = 0; i < sizeof(fmt_id)/sizeof(fmt_id[0]); i++)
138 init_fmtetc(&fmt, fmt_id[i], TYMED_ISTORAGE);
139 hr = IDataObject_QueryGetData(data, &fmt);
140 if (SUCCEEDED(hr))
142 if (src_cf) *src_cf = fmt_id[i];
143 return hr;
148 /* IPersistStorage */
149 hr = IDataObject_QueryInterface(data, &IID_IPersistStorage, (void**)&persist);
150 if(FAILED(hr)) return hr;
152 hr = IPersistStorage_GetClassID(persist, &clsid);
153 if(FAILED(hr)) goto end;
155 hr = IStorage_SetClass(stg, &clsid);
156 if(FAILED(hr)) goto end;
158 hr = IPersistStorage_Save(persist, stg, FALSE);
159 if(FAILED(hr)) goto end;
161 hr = IPersistStorage_SaveCompleted(persist, NULL);
163 end:
164 IPersistStorage_Release(persist);
166 return hr;
169 /******************************************************************************
170 * OleCreateFromDataEx [OLE32.@]
172 * Creates an embedded object from data transfer object retrieved from
173 * the clipboard or OLE drag and drop.
175 HRESULT WINAPI OleCreateFromDataEx(IDataObject *data, REFIID iid, DWORD flags,
176 DWORD renderopt, ULONG num_cache_fmts, DWORD *adv_flags, FORMATETC *cache_fmts,
177 IAdviseSink *sink, DWORD *conns,
178 IOleClientSite *client_site, IStorage *stg, void **obj)
180 HRESULT hr;
181 UINT src_cf;
183 FIXME("(%p, %s, %08x, %08x, %d, %p, %p, %p, %p, %p, %p, %p): stub\n",
184 data, debugstr_guid(iid), flags, renderopt, num_cache_fmts, adv_flags, cache_fmts,
185 sink, conns, client_site, stg, obj);
187 hr = get_storage(data, stg, &src_cf, TRUE);
188 if(FAILED(hr)) return hr;
190 hr = OleLoad(stg, iid, client_site, obj);
191 if(FAILED(hr)) return hr;
193 /* FIXME: Init cache */
195 return hr;
198 /******************************************************************************
199 * OleCreateFromData [OLE32.@]
201 HRESULT WINAPI OleCreateFromData(LPDATAOBJECT data, REFIID iid,
202 DWORD renderopt, LPFORMATETC fmt,
203 LPOLECLIENTSITE client_site, LPSTORAGE stg,
204 LPVOID* obj)
206 DWORD advf = ADVF_PRIMEFIRST;
208 return OleCreateFromDataEx(data, iid, 0, renderopt, fmt ? 1 : 0, fmt ? &advf : NULL,
209 fmt, NULL, NULL, client_site, stg, obj);
212 /******************************************************************************
213 * OleCreateLinkFromData [OLE32.@]
215 HRESULT WINAPI OleCreateLinkFromData(IDataObject *data, REFIID iid,
216 DWORD renderopt, FORMATETC *fmt,
217 IOleClientSite *client_site, IStorage *stg,
218 void **obj)
220 FIXME("%p,%s,%08x,%p,%p,%p,%p: semi-stub\n",
221 data, debugstr_guid(iid), renderopt, fmt, client_site, stg, obj);
222 return OleCreateFromData(data, iid, renderopt, fmt, client_site, stg, obj);
225 /******************************************************************************
226 * OleCreateStaticFromData [OLE32.@]
228 HRESULT WINAPI OleCreateStaticFromData(IDataObject *data, REFIID iid,
229 DWORD renderopt, FORMATETC *fmt,
230 IOleClientSite *client_site, IStorage *stg,
231 void **obj)
233 FIXME("%p,%s,%08x,%p,%p,%p,%p: semi-stub\n",
234 data, debugstr_guid(iid), renderopt, fmt, client_site, stg, obj);
235 return OleCreateFromData(data, iid, renderopt, fmt, client_site, stg, obj);
238 /******************************************************************************
239 * OleCreateFromFileEx [OLE32.@]
241 HRESULT WINAPI OleCreateFromFileEx(REFCLSID clsid, const OLECHAR *filename, REFIID iid, DWORD flags,
242 DWORD renderopt, ULONG num_fmts, DWORD *adv_flags, FORMATETC *fmts, IAdviseSink *sink,
243 DWORD *conns, IOleClientSite *client_site, IStorage *stg, void **obj)
245 HRESULT hr;
246 IMoniker *mon;
247 IDataObject *data;
248 IUnknown *unk = NULL;
249 IOleCache *cache = NULL;
250 ULONG i;
252 TRACE("cls %s, %s, iid %s, flags %d, render opts %d, num fmts %d, adv flags %p, fmts %p\n", debugstr_guid(clsid),
253 debugstr_w(filename), debugstr_guid(iid), flags, renderopt, num_fmts, adv_flags, fmts);
254 TRACE("sink %p, conns %p, client site %p, storage %p, obj %p\n", sink, conns, client_site, stg, obj);
255 for (i = 0; i < num_fmts; i++)
256 TRACE("\t%d: fmt %s adv flags %d\n", i, debugstr_formatetc(fmts + i), adv_flags[i]);
258 hr = CreateFileMoniker( filename, &mon );
259 if (FAILED(hr)) return hr;
261 hr = BindMoniker( mon, 0, &IID_IDataObject, (void**)&data );
262 IMoniker_Release( mon );
263 if (FAILED(hr)) return hr;
265 hr = get_storage( data, stg, NULL, FALSE );
266 if (FAILED(hr)) goto end;
268 hr = OleLoad( stg, &IID_IUnknown, client_site, (void**)&unk );
269 if (FAILED(hr)) goto end;
271 if (renderopt == OLERENDER_FORMAT)
273 hr = IUnknown_QueryInterface( unk, &IID_IOleCache, (void**)&cache );
274 if (FAILED(hr)) goto end;
276 for (i = 0; i < num_fmts; i++)
278 STGMEDIUM med;
279 DWORD dummy_conn;
281 memset( &med, 0, sizeof(med) );
282 hr = IDataObject_GetData( data, fmts + i, &med );
283 if (FAILED(hr)) goto end;
284 hr = IOleCache_Cache( cache, fmts + i, adv_flags[i], &dummy_conn );
285 if (SUCCEEDED(hr))
286 hr = IOleCache_SetData( cache, fmts + i, &med, TRUE );
287 if (FAILED(hr))
289 ReleaseStgMedium( &med );
290 goto end;
295 hr = IUnknown_QueryInterface( unk, iid, obj );
297 end:
298 if (cache) IOleCache_Release( cache );
299 if (unk) IUnknown_Release( unk );
300 IDataObject_Release( data );
301 return hr;
304 /******************************************************************************
305 * OleCreateFromFile [OLE32.@]
307 HRESULT WINAPI OleCreateFromFile(REFCLSID clsid, const OLECHAR *filename, REFIID iid, DWORD renderopt,
308 FORMATETC *fmt, IOleClientSite *client_site, IStorage *storage, void **obj)
310 DWORD advf = ADVF_PRIMEFIRST;
312 return OleCreateFromFileEx(clsid, filename, iid, 0, renderopt, fmt ? 1 : 0, fmt ? &advf : NULL, fmt,
313 NULL, NULL, client_site, storage, obj);
316 /******************************************************************************
317 * OleDuplicateData [OLE32.@]
319 * Duplicates clipboard data.
321 * PARAMS
322 * hSrc [I] Handle of the source clipboard data.
323 * cfFormat [I] The clipboard format of hSrc.
324 * uiFlags [I] Flags to pass to GlobalAlloc.
326 * RETURNS
327 * Success: handle to the duplicated data.
328 * Failure: NULL.
330 HANDLE WINAPI OleDuplicateData(HANDLE hSrc, CLIPFORMAT cfFormat,
331 UINT uiFlags)
333 HANDLE hDst = NULL;
335 TRACE("(%p,%x,%x)\n", hSrc, cfFormat, uiFlags);
337 if (!uiFlags) uiFlags = GMEM_MOVEABLE;
339 switch (cfFormat)
341 case CF_ENHMETAFILE:
342 hDst = CopyEnhMetaFileW(hSrc, NULL);
343 break;
344 case CF_METAFILEPICT:
345 hDst = CopyMetaFileW(hSrc, NULL);
346 break;
347 case CF_PALETTE:
349 LOGPALETTE * logpalette;
350 UINT nEntries = GetPaletteEntries(hSrc, 0, 0, NULL);
351 if (!nEntries) return NULL;
352 logpalette = HeapAlloc(GetProcessHeap(), 0,
353 FIELD_OFFSET(LOGPALETTE, palPalEntry[nEntries]));
354 if (!logpalette) return NULL;
355 if (!GetPaletteEntries(hSrc, 0, nEntries, logpalette->palPalEntry))
357 HeapFree(GetProcessHeap(), 0, logpalette);
358 return NULL;
360 logpalette->palVersion = 0x300;
361 logpalette->palNumEntries = (WORD)nEntries;
363 hDst = CreatePalette(logpalette);
365 HeapFree(GetProcessHeap(), 0, logpalette);
366 break;
368 case CF_BITMAP:
370 LONG size;
371 BITMAP bm;
372 if (!GetObjectW(hSrc, sizeof(bm), &bm))
373 return NULL;
374 size = GetBitmapBits(hSrc, 0, NULL);
375 if (!size) return NULL;
376 bm.bmBits = HeapAlloc(GetProcessHeap(), 0, size);
377 if (!bm.bmBits) return NULL;
378 if (GetBitmapBits(hSrc, size, bm.bmBits))
379 hDst = CreateBitmapIndirect(&bm);
380 HeapFree(GetProcessHeap(), 0, bm.bmBits);
381 break;
383 default:
385 SIZE_T size = GlobalSize(hSrc);
386 LPVOID pvSrc = NULL;
387 LPVOID pvDst = NULL;
389 /* allocate space for object */
390 if (!size) return NULL;
391 hDst = GlobalAlloc(uiFlags, size);
392 if (!hDst) return NULL;
394 /* lock pointers */
395 pvSrc = GlobalLock(hSrc);
396 if (!pvSrc)
398 GlobalFree(hDst);
399 return NULL;
401 pvDst = GlobalLock(hDst);
402 if (!pvDst)
404 GlobalUnlock(hSrc);
405 GlobalFree(hDst);
406 return NULL;
408 /* copy data */
409 memcpy(pvDst, pvSrc, size);
411 /* cleanup */
412 GlobalUnlock(hDst);
413 GlobalUnlock(hSrc);
417 TRACE("returning %p\n", hDst);
418 return hDst;