msvcrt: _filbuf should not lock any file.
[wine.git] / dlls / ole32 / ole2impl.c
blob5d92fec3c5f56fcf51945e8a97af828470599bc4
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
26 #define NONAMELESSSTRUCT
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"
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39 #define MAX_CLIPFORMAT_NAME 80
41 /******************************************************************************
42 * OleQueryCreateFromData [OLE32.@]
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.
51 HRESULT WINAPI OleQueryCreateFromData(IDataObject *data)
53 IEnumFORMATETC *enum_fmt;
54 FORMATETC fmt;
55 BOOL found_static = FALSE;
56 HRESULT hr;
58 hr = IDataObject_EnumFormatEtc(data, DATADIR_GET, &enum_fmt);
60 if(FAILED(hr)) return hr;
64 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
65 if(hr == S_OK)
67 if(fmt.cfFormat == embedded_object_clipboard_format ||
68 fmt.cfFormat == embed_source_clipboard_format ||
69 fmt.cfFormat == filename_clipboard_format)
71 IEnumFORMATETC_Release(enum_fmt);
72 return S_OK;
75 if(fmt.cfFormat == CF_METAFILEPICT ||
76 fmt.cfFormat == CF_BITMAP ||
77 fmt.cfFormat == CF_DIB)
78 found_static = TRUE;
80 } while (hr == S_OK);
82 IEnumFORMATETC_Release(enum_fmt);
84 return found_static ? OLE_S_STATIC : S_FALSE;
87 static inline void init_fmtetc(FORMATETC *fmt, CLIPFORMAT cf, TYMED tymed)
89 fmt->cfFormat = cf;
90 fmt->ptd = NULL;
91 fmt->dwAspect = DVASPECT_CONTENT;
92 fmt->lindex = -1;
93 fmt->tymed = tymed;
96 /***************************************************************************
97 * get_storage
99 * Retrieve an object's storage from a variety of sources.
101 * FIXME: CF_FILENAME.
103 static HRESULT get_storage(IDataObject *data, IStorage *stg, UINT *src_cf)
105 static const UINT fmt_id[] = { CF_METAFILEPICT, CF_BITMAP, CF_DIB };
106 UINT i;
107 HRESULT hr;
108 FORMATETC fmt;
109 STGMEDIUM med;
110 IPersistStorage *persist;
111 CLSID clsid;
113 *src_cf = 0;
115 /* CF_EMBEDEDOBJECT */
116 init_fmtetc(&fmt, embedded_object_clipboard_format, TYMED_ISTORAGE);
117 med.tymed = TYMED_ISTORAGE;
118 med.u.pstg = stg;
119 hr = IDataObject_GetDataHere(data, &fmt, &med);
120 if(SUCCEEDED(hr))
122 *src_cf = embedded_object_clipboard_format;
123 return hr;
126 /* CF_EMBEDSOURCE */
127 init_fmtetc(&fmt, embed_source_clipboard_format, TYMED_ISTORAGE);
128 med.tymed = TYMED_ISTORAGE;
129 med.u.pstg = stg;
130 hr = IDataObject_GetDataHere(data, &fmt, &med);
131 if(SUCCEEDED(hr))
133 *src_cf = embed_source_clipboard_format;
134 return hr;
137 for (i = 0; i < sizeof(fmt_id)/sizeof(fmt_id[0]); i++)
139 init_fmtetc(&fmt, fmt_id[i], TYMED_ISTORAGE);
140 hr = IDataObject_QueryGetData(data, &fmt);
141 if(SUCCEEDED(hr))
143 *src_cf = fmt_id[i];
144 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);
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 * OleDuplicateData [OLE32.@]
241 * Duplicates clipboard data.
243 * PARAMS
244 * hSrc [I] Handle of the source clipboard data.
245 * cfFormat [I] The clipboard format of hSrc.
246 * uiFlags [I] Flags to pass to GlobalAlloc.
248 * RETURNS
249 * Success: handle to the duplicated data.
250 * Failure: NULL.
252 HANDLE WINAPI OleDuplicateData(HANDLE hSrc, CLIPFORMAT cfFormat,
253 UINT uiFlags)
255 HANDLE hDst = NULL;
257 TRACE("(%p,%x,%x)\n", hSrc, cfFormat, uiFlags);
259 if (!uiFlags) uiFlags = GMEM_MOVEABLE;
261 switch (cfFormat)
263 case CF_ENHMETAFILE:
264 hDst = CopyEnhMetaFileW(hSrc, NULL);
265 break;
266 case CF_METAFILEPICT:
267 hDst = CopyMetaFileW(hSrc, NULL);
268 break;
269 case CF_PALETTE:
271 LOGPALETTE * logpalette;
272 UINT nEntries = GetPaletteEntries(hSrc, 0, 0, NULL);
273 if (!nEntries) return NULL;
274 logpalette = HeapAlloc(GetProcessHeap(), 0,
275 FIELD_OFFSET(LOGPALETTE, palPalEntry[nEntries]));
276 if (!logpalette) return NULL;
277 if (!GetPaletteEntries(hSrc, 0, nEntries, logpalette->palPalEntry))
279 HeapFree(GetProcessHeap(), 0, logpalette);
280 return NULL;
282 logpalette->palVersion = 0x300;
283 logpalette->palNumEntries = (WORD)nEntries;
285 hDst = CreatePalette(logpalette);
287 HeapFree(GetProcessHeap(), 0, logpalette);
288 break;
290 case CF_BITMAP:
292 LONG size;
293 BITMAP bm;
294 if (!GetObjectW(hSrc, sizeof(bm), &bm))
295 return NULL;
296 size = GetBitmapBits(hSrc, 0, NULL);
297 if (!size) return NULL;
298 bm.bmBits = HeapAlloc(GetProcessHeap(), 0, size);
299 if (!bm.bmBits) return NULL;
300 if (GetBitmapBits(hSrc, size, bm.bmBits))
301 hDst = CreateBitmapIndirect(&bm);
302 HeapFree(GetProcessHeap(), 0, bm.bmBits);
303 break;
305 default:
307 SIZE_T size = GlobalSize(hSrc);
308 LPVOID pvSrc = NULL;
309 LPVOID pvDst = NULL;
311 /* allocate space for object */
312 if (!size) return NULL;
313 hDst = GlobalAlloc(uiFlags, size);
314 if (!hDst) return NULL;
316 /* lock pointers */
317 pvSrc = GlobalLock(hSrc);
318 if (!pvSrc)
320 GlobalFree(hDst);
321 return NULL;
323 pvDst = GlobalLock(hDst);
324 if (!pvDst)
326 GlobalUnlock(hSrc);
327 GlobalFree(hDst);
328 return NULL;
330 /* copy data */
331 memcpy(pvDst, pvSrc, size);
333 /* cleanup */
334 GlobalUnlock(hDst);
335 GlobalUnlock(hSrc);
339 TRACE("returning %p\n", hDst);
340 return hDst;