vcomp/tests: Add tests for atomic double functions.
[wine.git] / dlls / ole32 / ole2impl.c
blob4be79a7f71ca326c385027bd4f4ccb1732258739
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)
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 *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 *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 *src_cf = embed_source_clipboard_format;
131 return hr;
134 for (i = 0; i < sizeof(fmt_id)/sizeof(fmt_id[0]); i++)
136 init_fmtetc(&fmt, fmt_id[i], TYMED_ISTORAGE);
137 hr = IDataObject_QueryGetData(data, &fmt);
138 if(SUCCEEDED(hr))
140 *src_cf = fmt_id[i];
141 return hr;
145 /* IPersistStorage */
146 hr = IDataObject_QueryInterface(data, &IID_IPersistStorage, (void**)&persist);
147 if(FAILED(hr)) return hr;
149 hr = IPersistStorage_GetClassID(persist, &clsid);
150 if(FAILED(hr)) goto end;
152 hr = IStorage_SetClass(stg, &clsid);
153 if(FAILED(hr)) goto end;
155 hr = IPersistStorage_Save(persist, stg, FALSE);
156 if(FAILED(hr)) goto end;
158 hr = IPersistStorage_SaveCompleted(persist, NULL);
160 end:
161 IPersistStorage_Release(persist);
163 return hr;
166 /******************************************************************************
167 * OleCreateFromDataEx [OLE32.@]
169 * Creates an embedded object from data transfer object retrieved from
170 * the clipboard or OLE drag and drop.
172 HRESULT WINAPI OleCreateFromDataEx(IDataObject *data, REFIID iid, DWORD flags,
173 DWORD renderopt, ULONG num_cache_fmts, DWORD *adv_flags, FORMATETC *cache_fmts,
174 IAdviseSink *sink, DWORD *conns,
175 IOleClientSite *client_site, IStorage *stg, void **obj)
177 HRESULT hr;
178 UINT src_cf;
180 FIXME("(%p, %s, %08x, %08x, %d, %p, %p, %p, %p, %p, %p, %p): stub\n",
181 data, debugstr_guid(iid), flags, renderopt, num_cache_fmts, adv_flags, cache_fmts,
182 sink, conns, client_site, stg, obj);
184 hr = get_storage(data, stg, &src_cf);
185 if(FAILED(hr)) return hr;
187 hr = OleLoad(stg, iid, client_site, obj);
188 if(FAILED(hr)) return hr;
190 /* FIXME: Init cache */
192 return hr;
195 /******************************************************************************
196 * OleCreateFromData [OLE32.@]
198 HRESULT WINAPI OleCreateFromData(LPDATAOBJECT data, REFIID iid,
199 DWORD renderopt, LPFORMATETC fmt,
200 LPOLECLIENTSITE client_site, LPSTORAGE stg,
201 LPVOID* obj)
203 DWORD advf = ADVF_PRIMEFIRST;
205 return OleCreateFromDataEx(data, iid, 0, renderopt, fmt ? 1 : 0, fmt ? &advf : NULL,
206 fmt, NULL, NULL, client_site, stg, obj);
209 /******************************************************************************
210 * OleCreateLinkFromData [OLE32.@]
212 HRESULT WINAPI OleCreateLinkFromData(IDataObject *data, REFIID iid,
213 DWORD renderopt, FORMATETC *fmt,
214 IOleClientSite *client_site, IStorage *stg,
215 void **obj)
217 FIXME("%p,%s,%08x,%p,%p,%p,%p: semi-stub\n",
218 data, debugstr_guid(iid), renderopt, fmt, client_site, stg, obj);
219 return OleCreateFromData(data, iid, renderopt, fmt, client_site, stg, obj);
222 /******************************************************************************
223 * OleCreateStaticFromData [OLE32.@]
225 HRESULT WINAPI OleCreateStaticFromData(IDataObject *data, REFIID iid,
226 DWORD renderopt, FORMATETC *fmt,
227 IOleClientSite *client_site, IStorage *stg,
228 void **obj)
230 FIXME("%p,%s,%08x,%p,%p,%p,%p: semi-stub\n",
231 data, debugstr_guid(iid), renderopt, fmt, client_site, stg, obj);
232 return OleCreateFromData(data, iid, renderopt, fmt, client_site, stg, obj);
235 /******************************************************************************
236 * OleDuplicateData [OLE32.@]
238 * Duplicates clipboard data.
240 * PARAMS
241 * hSrc [I] Handle of the source clipboard data.
242 * cfFormat [I] The clipboard format of hSrc.
243 * uiFlags [I] Flags to pass to GlobalAlloc.
245 * RETURNS
246 * Success: handle to the duplicated data.
247 * Failure: NULL.
249 HANDLE WINAPI OleDuplicateData(HANDLE hSrc, CLIPFORMAT cfFormat,
250 UINT uiFlags)
252 HANDLE hDst = NULL;
254 TRACE("(%p,%x,%x)\n", hSrc, cfFormat, uiFlags);
256 if (!uiFlags) uiFlags = GMEM_MOVEABLE;
258 switch (cfFormat)
260 case CF_ENHMETAFILE:
261 hDst = CopyEnhMetaFileW(hSrc, NULL);
262 break;
263 case CF_METAFILEPICT:
264 hDst = CopyMetaFileW(hSrc, NULL);
265 break;
266 case CF_PALETTE:
268 LOGPALETTE * logpalette;
269 UINT nEntries = GetPaletteEntries(hSrc, 0, 0, NULL);
270 if (!nEntries) return NULL;
271 logpalette = HeapAlloc(GetProcessHeap(), 0,
272 FIELD_OFFSET(LOGPALETTE, palPalEntry[nEntries]));
273 if (!logpalette) return NULL;
274 if (!GetPaletteEntries(hSrc, 0, nEntries, logpalette->palPalEntry))
276 HeapFree(GetProcessHeap(), 0, logpalette);
277 return NULL;
279 logpalette->palVersion = 0x300;
280 logpalette->palNumEntries = (WORD)nEntries;
282 hDst = CreatePalette(logpalette);
284 HeapFree(GetProcessHeap(), 0, logpalette);
285 break;
287 case CF_BITMAP:
289 LONG size;
290 BITMAP bm;
291 if (!GetObjectW(hSrc, sizeof(bm), &bm))
292 return NULL;
293 size = GetBitmapBits(hSrc, 0, NULL);
294 if (!size) return NULL;
295 bm.bmBits = HeapAlloc(GetProcessHeap(), 0, size);
296 if (!bm.bmBits) return NULL;
297 if (GetBitmapBits(hSrc, size, bm.bmBits))
298 hDst = CreateBitmapIndirect(&bm);
299 HeapFree(GetProcessHeap(), 0, bm.bmBits);
300 break;
302 default:
304 SIZE_T size = GlobalSize(hSrc);
305 LPVOID pvSrc = NULL;
306 LPVOID pvDst = NULL;
308 /* allocate space for object */
309 if (!size) return NULL;
310 hDst = GlobalAlloc(uiFlags, size);
311 if (!hDst) return NULL;
313 /* lock pointers */
314 pvSrc = GlobalLock(hSrc);
315 if (!pvSrc)
317 GlobalFree(hDst);
318 return NULL;
320 pvDst = GlobalLock(hDst);
321 if (!pvDst)
323 GlobalUnlock(hSrc);
324 GlobalFree(hDst);
325 return NULL;
327 /* copy data */
328 memcpy(pvDst, pvSrc, size);
330 /* cleanup */
331 GlobalUnlock(hDst);
332 GlobalUnlock(hSrc);
336 TRACE("returning %p\n", hDst);
337 return hDst;