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
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
32 #include "wine/debug.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;
55 BOOL found_static = FALSE;
58 hr = IDataObject_EnumFormatEtc(data, DATADIR_GET, &enum_fmt);
60 if(FAILED(hr)) return hr;
64 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
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);
75 if(fmt.cfFormat == CF_METAFILEPICT ||
76 fmt.cfFormat == CF_BITMAP ||
77 fmt.cfFormat == CF_DIB)
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)
91 fmt->dwAspect = DVASPECT_CONTENT;
96 /***************************************************************************
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 };
110 IPersistStorage *persist;
115 /* CF_EMBEDEDOBJECT */
116 init_fmtetc(&fmt, embedded_object_clipboard_format, TYMED_ISTORAGE);
117 med.tymed = TYMED_ISTORAGE;
119 hr = IDataObject_GetDataHere(data, &fmt, &med);
122 *src_cf = embedded_object_clipboard_format;
127 init_fmtetc(&fmt, embed_source_clipboard_format, TYMED_ISTORAGE);
128 med.tymed = TYMED_ISTORAGE;
130 hr = IDataObject_GetDataHere(data, &fmt, &med);
133 *src_cf = embed_source_clipboard_format;
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);
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);
164 IPersistStorage_Release(persist);
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)
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 */
198 /******************************************************************************
199 * OleCreateFromData [OLE32.@]
201 HRESULT WINAPI OleCreateFromData(LPDATAOBJECT data, REFIID iid,
202 DWORD renderopt, LPFORMATETC fmt,
203 LPOLECLIENTSITE client_site, LPSTORAGE stg,
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,
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,
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.
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.
249 * Success: handle to the duplicated data.
252 HANDLE WINAPI OleDuplicateData(HANDLE hSrc, CLIPFORMAT cfFormat,
257 TRACE("(%p,%x,%x)\n", hSrc, cfFormat, uiFlags);
259 if (!uiFlags) uiFlags = GMEM_MOVEABLE;
264 hDst = CopyEnhMetaFileW(hSrc, NULL);
266 case CF_METAFILEPICT:
267 hDst = CopyMetaFileW(hSrc, NULL);
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);
282 logpalette->palVersion = 0x300;
283 logpalette->palNumEntries = (WORD)nEntries;
285 hDst = CreatePalette(logpalette);
287 HeapFree(GetProcessHeap(), 0, logpalette);
294 if (!GetObjectW(hSrc, sizeof(bm), &bm))
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);
307 SIZE_T size = GlobalSize(hSrc);
311 /* allocate space for object */
312 if (!size) return NULL;
313 hDst = GlobalAlloc(uiFlags, size);
314 if (!hDst) return NULL;
317 pvSrc = GlobalLock(hSrc);
323 pvDst = GlobalLock(hDst);
331 memcpy(pvDst, pvSrc, size);
339 TRACE("returning %p\n", hDst);