Release 1.4-rc3.
[wine/multimedia.git] / dlls / ole32 / ole2impl.c
blobe4eccc1f1d6bbc8ad48fe87c0120f3674665c523
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 HRESULT hr;
106 FORMATETC fmt;
107 STGMEDIUM med;
108 IPersistStorage *persist;
109 CLSID clsid;
111 *src_cf = 0;
113 /* CF_EMBEDEDOBJECT */
114 init_fmtetc(&fmt, embedded_object_clipboard_format, TYMED_ISTORAGE);
115 med.tymed = TYMED_ISTORAGE;
116 med.u.pstg = stg;
117 hr = IDataObject_GetDataHere(data, &fmt, &med);
118 if(SUCCEEDED(hr))
120 *src_cf = embedded_object_clipboard_format;
121 return hr;
124 /* CF_EMBEDSOURCE */
125 init_fmtetc(&fmt, embed_source_clipboard_format, TYMED_ISTORAGE);
126 med.tymed = TYMED_ISTORAGE;
127 med.u.pstg = stg;
128 hr = IDataObject_GetDataHere(data, &fmt, &med);
129 if(SUCCEEDED(hr))
131 *src_cf = embed_source_clipboard_format;
132 return hr;
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);
150 end:
151 IPersistStorage_Release(persist);
153 return hr;
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)
167 HRESULT hr;
168 UINT src_cf;
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 */
182 return hr;
185 /******************************************************************************
186 * OleCreateFromData [OLE32.@]
188 HRESULT WINAPI OleCreateFromData(LPDATAOBJECT data, REFIID iid,
189 DWORD renderopt, LPFORMATETC fmt,
190 LPOLECLIENTSITE client_site, LPSTORAGE stg,
191 LPVOID* obj)
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.
205 * PARAMS
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.
210 * RETURNS
211 * Success: handle to the duplicated data.
212 * Failure: NULL.
214 HANDLE WINAPI OleDuplicateData(HANDLE hSrc, CLIPFORMAT cfFormat,
215 UINT uiFlags)
217 HANDLE hDst = NULL;
219 TRACE("(%p,%x,%x)\n", hSrc, cfFormat, uiFlags);
221 if (!uiFlags) uiFlags = GMEM_MOVEABLE;
223 switch (cfFormat)
225 case CF_ENHMETAFILE:
226 hDst = CopyEnhMetaFileW(hSrc, NULL);
227 break;
228 case CF_METAFILEPICT:
229 hDst = CopyMetaFileW(hSrc, NULL);
230 break;
231 case CF_PALETTE:
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);
242 return NULL;
244 logpalette->palVersion = 0x300;
245 logpalette->palNumEntries = (WORD)nEntries;
247 hDst = CreatePalette(logpalette);
249 HeapFree(GetProcessHeap(), 0, logpalette);
250 break;
252 case CF_BITMAP:
254 LONG size;
255 BITMAP bm;
256 if (!GetObjectW(hSrc, sizeof(bm), &bm))
257 return NULL;
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);
265 break;
267 default:
269 SIZE_T size = GlobalSize(hSrc);
270 LPVOID pvSrc = NULL;
271 LPVOID pvDst = NULL;
273 /* allocate space for object */
274 if (!size) return NULL;
275 hDst = GlobalAlloc(uiFlags, size);
276 if (!hDst) return NULL;
278 /* lock pointers */
279 pvSrc = GlobalLock(hSrc);
280 if (!pvSrc)
282 GlobalFree(hDst);
283 return NULL;
285 pvDst = GlobalLock(hDst);
286 if (!pvDst)
288 GlobalUnlock(hSrc);
289 GlobalFree(hDst);
290 return NULL;
292 /* copy data */
293 memcpy(pvDst, pvSrc, size);
295 /* cleanup */
296 GlobalUnlock(hDst);
297 GlobalUnlock(hSrc);
301 TRACE("returning %p\n", hDst);
302 return hDst;