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
31 #include "wine/debug.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
;
52 BOOL found_static
= FALSE
;
55 hr
= IDataObject_EnumFormatEtc(data
, DATADIR_GET
, &enum_fmt
);
57 if(FAILED(hr
)) return hr
;
61 hr
= IEnumFORMATETC_Next(enum_fmt
, 1, &fmt
, NULL
);
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
);
72 if(fmt
.cfFormat
== CF_METAFILEPICT
||
73 fmt
.cfFormat
== CF_BITMAP
||
74 fmt
.cfFormat
== CF_DIB
)
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
)
88 fmt
->dwAspect
= DVASPECT_CONTENT
;
93 /***************************************************************************
96 * Retrieve an object's storage from a variety of sources.
100 static HRESULT
get_storage(IDataObject
*data
, IStorage
*stg
, UINT
*src_cf
, BOOL other_fmts
)
102 static const UINT fmt_id
[] = { CF_METAFILEPICT
, CF_BITMAP
, CF_DIB
};
107 IPersistStorage
*persist
;
110 if (src_cf
) *src_cf
= 0;
112 /* CF_EMBEDEDOBJECT */
113 init_fmtetc(&fmt
, embedded_object_clipboard_format
, TYMED_ISTORAGE
);
114 med
.tymed
= TYMED_ISTORAGE
;
116 hr
= IDataObject_GetDataHere(data
, &fmt
, &med
);
119 if (src_cf
) *src_cf
= embedded_object_clipboard_format
;
124 init_fmtetc(&fmt
, embed_source_clipboard_format
, TYMED_ISTORAGE
);
125 med
.tymed
= TYMED_ISTORAGE
;
127 hr
= IDataObject_GetDataHere(data
, &fmt
, &med
);
130 if (src_cf
) *src_cf
= embed_source_clipboard_format
;
136 for (i
= 0; i
< sizeof(fmt_id
)/sizeof(fmt_id
[0]); i
++)
138 init_fmtetc(&fmt
, fmt_id
[i
], TYMED_ISTORAGE
);
139 hr
= IDataObject_QueryGetData(data
, &fmt
);
142 if (src_cf
) *src_cf
= fmt_id
[i
];
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
, TRUE
);
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 * OleCreateFromFileEx [OLE32.@]
241 HRESULT WINAPI
OleCreateFromFileEx(REFCLSID clsid
, const OLECHAR
*filename
, REFIID iid
, DWORD flags
,
242 DWORD renderopt
, ULONG num_fmts
, DWORD
*adv_flags
, FORMATETC
*fmts
, IAdviseSink
*sink
,
243 DWORD
*conns
, IOleClientSite
*client_site
, IStorage
*stg
, void **obj
)
248 IUnknown
*unk
= NULL
;
249 IOleCache
*cache
= NULL
;
252 TRACE("cls %s, %s, iid %s, flags %d, render opts %d, num fmts %d, adv flags %p, fmts %p\n", debugstr_guid(clsid
),
253 debugstr_w(filename
), debugstr_guid(iid
), flags
, renderopt
, num_fmts
, adv_flags
, fmts
);
254 TRACE("sink %p, conns %p, client site %p, storage %p, obj %p\n", sink
, conns
, client_site
, stg
, obj
);
255 for (i
= 0; i
< num_fmts
; i
++)
256 TRACE("\t%d: fmt %s adv flags %d\n", i
, debugstr_formatetc(fmts
+ i
), adv_flags
[i
]);
258 hr
= CreateFileMoniker( filename
, &mon
);
259 if (FAILED(hr
)) return hr
;
261 hr
= BindMoniker( mon
, 0, &IID_IDataObject
, (void**)&data
);
262 IMoniker_Release( mon
);
263 if (FAILED(hr
)) return hr
;
265 hr
= get_storage( data
, stg
, NULL
, FALSE
);
266 if (FAILED(hr
)) goto end
;
268 hr
= OleLoad( stg
, &IID_IUnknown
, client_site
, (void**)&unk
);
269 if (FAILED(hr
)) goto end
;
271 if (renderopt
== OLERENDER_FORMAT
)
273 hr
= IUnknown_QueryInterface( unk
, &IID_IOleCache
, (void**)&cache
);
274 if (FAILED(hr
)) goto end
;
276 for (i
= 0; i
< num_fmts
; i
++)
281 memset( &med
, 0, sizeof(med
) );
282 hr
= IDataObject_GetData( data
, fmts
+ i
, &med
);
283 if (FAILED(hr
)) goto end
;
284 hr
= IOleCache_Cache( cache
, fmts
+ i
, adv_flags
[i
], &dummy_conn
);
286 hr
= IOleCache_SetData( cache
, fmts
+ i
, &med
, TRUE
);
289 ReleaseStgMedium( &med
);
295 hr
= IUnknown_QueryInterface( unk
, iid
, obj
);
298 if (cache
) IOleCache_Release( cache
);
299 if (unk
) IUnknown_Release( unk
);
300 IDataObject_Release( data
);
304 /******************************************************************************
305 * OleCreateFromFile [OLE32.@]
307 HRESULT WINAPI
OleCreateFromFile(REFCLSID clsid
, const OLECHAR
*filename
, REFIID iid
, DWORD renderopt
,
308 FORMATETC
*fmt
, IOleClientSite
*client_site
, IStorage
*storage
, void **obj
)
310 DWORD advf
= ADVF_PRIMEFIRST
;
312 return OleCreateFromFileEx(clsid
, filename
, iid
, 0, renderopt
, fmt
? 1 : 0, fmt
? &advf
: NULL
, fmt
,
313 NULL
, NULL
, client_site
, storage
, obj
);
316 /******************************************************************************
317 * OleDuplicateData [OLE32.@]
319 * Duplicates clipboard data.
322 * hSrc [I] Handle of the source clipboard data.
323 * cfFormat [I] The clipboard format of hSrc.
324 * uiFlags [I] Flags to pass to GlobalAlloc.
327 * Success: handle to the duplicated data.
330 HANDLE WINAPI
OleDuplicateData(HANDLE hSrc
, CLIPFORMAT cfFormat
,
335 TRACE("(%p,%x,%x)\n", hSrc
, cfFormat
, uiFlags
);
337 if (!uiFlags
) uiFlags
= GMEM_MOVEABLE
;
342 hDst
= CopyEnhMetaFileW(hSrc
, NULL
);
344 case CF_METAFILEPICT
:
345 hDst
= CopyMetaFileW(hSrc
, NULL
);
349 LOGPALETTE
* logpalette
;
350 UINT nEntries
= GetPaletteEntries(hSrc
, 0, 0, NULL
);
351 if (!nEntries
) return NULL
;
352 logpalette
= HeapAlloc(GetProcessHeap(), 0,
353 FIELD_OFFSET(LOGPALETTE
, palPalEntry
[nEntries
]));
354 if (!logpalette
) return NULL
;
355 if (!GetPaletteEntries(hSrc
, 0, nEntries
, logpalette
->palPalEntry
))
357 HeapFree(GetProcessHeap(), 0, logpalette
);
360 logpalette
->palVersion
= 0x300;
361 logpalette
->palNumEntries
= (WORD
)nEntries
;
363 hDst
= CreatePalette(logpalette
);
365 HeapFree(GetProcessHeap(), 0, logpalette
);
372 if (!GetObjectW(hSrc
, sizeof(bm
), &bm
))
374 size
= GetBitmapBits(hSrc
, 0, NULL
);
375 if (!size
) return NULL
;
376 bm
.bmBits
= HeapAlloc(GetProcessHeap(), 0, size
);
377 if (!bm
.bmBits
) return NULL
;
378 if (GetBitmapBits(hSrc
, size
, bm
.bmBits
))
379 hDst
= CreateBitmapIndirect(&bm
);
380 HeapFree(GetProcessHeap(), 0, bm
.bmBits
);
385 SIZE_T size
= GlobalSize(hSrc
);
389 /* allocate space for object */
390 if (!size
) return NULL
;
391 hDst
= GlobalAlloc(uiFlags
, size
);
392 if (!hDst
) return NULL
;
395 pvSrc
= GlobalLock(hSrc
);
401 pvDst
= GlobalLock(hDst
);
409 memcpy(pvDst
, pvSrc
, size
);
417 TRACE("returning %p\n", hDst
);