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)
108 IPersistStorage *persist;
113 /* CF_EMBEDEDOBJECT */
114 init_fmtetc(&fmt, embedded_object_clipboard_format, TYMED_ISTORAGE);
115 med.tymed = TYMED_ISTORAGE;
117 hr = IDataObject_GetDataHere(data, &fmt, &med);
120 *src_cf = embedded_object_clipboard_format;
125 init_fmtetc(&fmt, embed_source_clipboard_format, TYMED_ISTORAGE);
126 med.tymed = TYMED_ISTORAGE;
128 hr = IDataObject_GetDataHere(data, &fmt, &med);
131 *src_cf = embed_source_clipboard_format;
135 /* IPersistStorage */
136 hr = IDataObject_QueryInterface(data, &IID_IPersistStorage, (void**)&persist);
137 if(FAILED(hr)) return hr;
139 hr = IPersistStorage_GetClassID(persist, &clsid);
140 if(FAILED(hr)) goto end;
142 hr = IStorage_SetClass(stg, &clsid);
143 if(FAILED(hr)) goto end;
145 hr = IPersistStorage_Save(persist, stg, FALSE);
146 if(FAILED(hr)) goto end;
148 hr = IPersistStorage_SaveCompleted(persist, NULL);
151 IPersistStorage_Release(persist);
156 /******************************************************************************
157 * OleCreateFromDataEx [OLE32.@]
159 * Creates an embedded object from data transfer object retrieved from
160 * the clipboard or OLE drag and drop.
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)
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);
174 hr = get_storage(data, stg, &src_cf);
175 if(FAILED(hr)) return hr;
177 hr = OleLoad(stg, iid, client_site, obj);
178 if(FAILED(hr)) return hr;
180 /* FIXME: Init cache */
185 /******************************************************************************
186 * OleCreateFromData [OLE32.@]
188 HRESULT WINAPI OleCreateFromData(LPDATAOBJECT data, REFIID iid,
189 DWORD renderopt, LPFORMATETC fmt,
190 LPOLECLIENTSITE client_site, LPSTORAGE stg,
193 DWORD advf = ADVF_PRIMEFIRST;
195 return OleCreateFromDataEx(data, iid, 0, renderopt, fmt ? 1 : 0, fmt ? &advf : NULL,
196 fmt, NULL, NULL, client_site, stg, obj);
200 /******************************************************************************
201 * OleDuplicateData [OLE32.@]
203 * Duplicates clipboard data.
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.
211 * Success: handle to the duplicated data.
214 HANDLE WINAPI OleDuplicateData(HANDLE hSrc, CLIPFORMAT cfFormat,
219 TRACE("(%p,%x,%x)\n", hSrc, cfFormat, uiFlags);
221 if (!uiFlags) uiFlags = GMEM_MOVEABLE;
226 hDst = CopyEnhMetaFileW(hSrc, NULL);
228 case CF_METAFILEPICT:
229 hDst = CopyMetaFileW(hSrc, NULL);
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))
241 HeapFree(GetProcessHeap(), 0, logpalette);
244 logpalette->palVersion = 0x300;
245 logpalette->palNumEntries = (WORD)nEntries;
247 hDst = CreatePalette(logpalette);
249 HeapFree(GetProcessHeap(), 0, logpalette);
256 if (!GetObjectW(hSrc, sizeof(bm), &bm))
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);
269 SIZE_T size = GlobalSize(hSrc);
273 /* allocate space for object */
274 if (!size) return NULL;
275 hDst = GlobalAlloc(uiFlags, size);
276 if (!hDst) return NULL;
279 pvSrc = GlobalLock(hSrc);
285 pvDst = GlobalLock(hDst);
293 memcpy(pvDst, pvSrc, size);
301 TRACE("returning %p\n", hDst);