appwiz.cpl: Fix compilation on systems that don't support nameless unions.
[wine.git] / dlls / ole32 / ole2impl.c
blobda50f4a6387df9e390bcf7f8060bebecd07ba760
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"
36 WINE_DEFAULT_DEBUG_CHANNEL(ole);
38 #define MAX_CLIPFORMAT_NAME 80
40 /******************************************************************************
41 * OleQueryCreateFromData [OLE32.@]
43 * Author : Abey George
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(LPDATAOBJECT pSrcDataObject)
53 IEnumFORMATETC *pfmt;
54 FORMATETC fmt;
55 CHAR szFmtName[MAX_CLIPFORMAT_NAME];
56 BOOL bFoundStatic = FALSE;
58 HRESULT hr = IDataObject_EnumFormatEtc(pSrcDataObject, DATADIR_GET, &pfmt);
60 if (hr == S_OK)
61 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
63 while (hr == S_OK)
65 GetClipboardFormatNameA(fmt.cfFormat, szFmtName, MAX_CLIPFORMAT_NAME-1);
67 /* first, Check for Embedded Object, Embed Source or Filename */
69 if (!strcmp(szFmtName, CF_EMBEDDEDOBJECT) || !strcmp(szFmtName, CF_EMBEDSOURCE) || !strcmp(szFmtName, CF_FILENAME))
70 return S_OK;
72 /* Check for Metafile, Bitmap or DIB */
74 if (fmt.cfFormat == CF_METAFILEPICT || fmt.cfFormat == CF_BITMAP || fmt.cfFormat == CF_DIB)
75 bFoundStatic = TRUE;
77 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
80 /* Found a static format, but no embed format */
82 if (bFoundStatic)
83 return OLE_S_STATIC;
85 return S_FALSE;
88 /******************************************************************************
89 * OleCreateFromData [OLE32.@]
91 * Author : Abey George
92 * Creates an embedded object from data transfer object retrieved from
93 * the clipboard or OLE drag and drop.
94 * Returns : S_OK - Embedded object was created successfully.
95 * OLE_E_STATIC - OLE can create only a static object
96 * DV_E_FORMATETC - No acceptable format is available (only error return code)
97 * TODO : CF_FILENAME, CF_EMBEDEDOBJECT formats. Parameter renderopt is currently ignored.
100 HRESULT WINAPI OleCreateFromData(LPDATAOBJECT pSrcDataObject, REFIID riid,
101 DWORD renderopt, LPFORMATETC pFormatEtc,
102 LPOLECLIENTSITE pClientSite, LPSTORAGE pStg,
103 LPVOID* ppvObj)
105 IEnumFORMATETC *pfmt;
106 FORMATETC fmt;
107 CHAR szFmtName[MAX_CLIPFORMAT_NAME];
108 STGMEDIUM std;
109 HRESULT hr;
110 HRESULT hr1;
112 hr = IDataObject_EnumFormatEtc(pSrcDataObject, DATADIR_GET, &pfmt);
114 if (hr == S_OK)
116 memset(&std, 0, sizeof(STGMEDIUM));
118 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
119 while (hr == S_OK)
121 GetClipboardFormatNameA(fmt.cfFormat, szFmtName, MAX_CLIPFORMAT_NAME-1);
123 /* first, Check for Embedded Object, Embed Source or Filename */
124 /* TODO: Currently checks only for Embed Source. */
126 if (!strcmp(szFmtName, CF_EMBEDSOURCE))
128 std.tymed = TYMED_HGLOBAL;
130 if ((hr1 = IDataObject_GetData(pSrcDataObject, &fmt, &std)) == S_OK)
132 ILockBytes *ptrILockBytes = 0;
133 IStorage *pStorage = 0;
134 IOleObject *pOleObject = 0;
135 IPersistStorage *pPersistStorage = 0;
136 CLSID clsID;
138 /* Create ILock bytes */
140 hr1 = CreateILockBytesOnHGlobal(std.u.hGlobal, FALSE, &ptrILockBytes);
142 /* Open storage on the ILock bytes */
144 if (hr1 == S_OK)
145 hr1 = StgOpenStorageOnILockBytes(ptrILockBytes, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &pStorage);
147 /* Get Class ID from the opened storage */
149 if (hr1 == S_OK)
150 hr1 = ReadClassStg(pStorage, &clsID);
152 /* Create default handler for Persist storage */
154 if (hr1 == S_OK)
155 hr1 = OleCreateDefaultHandler(&clsID, NULL, &IID_IPersistStorage, (LPVOID*)&pPersistStorage);
157 /* Load the storage to Persist storage */
159 if (hr1 == S_OK)
160 hr1 = IPersistStorage_Load(pPersistStorage, pStorage);
162 /* Query for IOleObject */
164 if (hr1 == S_OK)
165 hr1 = IPersistStorage_QueryInterface(pPersistStorage, &IID_IOleObject, (LPVOID*)&pOleObject);
167 /* Set client site with the IOleObject */
169 if (hr1 == S_OK)
170 hr1 = IOleObject_SetClientSite(pOleObject, pClientSite);
172 IPersistStorage_Release(pPersistStorage);
173 /* Query for the requested interface */
175 if (hr1 == S_OK)
176 hr1 = IPersistStorage_QueryInterface(pPersistStorage, riid, ppvObj);
178 IPersistStorage_Release(pPersistStorage);
180 IStorage_Release(pStorage);
182 if (hr1 == S_OK)
183 return S_OK;
186 /* Return error */
188 return DV_E_FORMATETC;
191 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
195 return DV_E_FORMATETC;
199 /******************************************************************************
200 * OleDuplicateData [OLE32.@]
202 * Duplicates clipboard data.
204 * PARAMS
205 * hSrc [I] Handle of the source clipboard data.
206 * cfFormat [I] The clipboard format of hSrc.
207 * uiFlags [I] Flags to pass to GlobalAlloc.
209 * RETURNS
210 * Success: handle to the duplicated data.
211 * Failure: NULL.
213 HANDLE WINAPI OleDuplicateData(HANDLE hSrc, CLIPFORMAT cfFormat,
214 UINT uiFlags)
216 HANDLE hDst = NULL;
218 TRACE("(%p,%x,%x)\n", hSrc, cfFormat, uiFlags);
220 if (!uiFlags) uiFlags = GMEM_MOVEABLE;
222 switch (cfFormat)
224 case CF_ENHMETAFILE:
225 hDst = CopyEnhMetaFileW(hSrc, NULL);
226 break;
227 case CF_METAFILEPICT:
228 hDst = CopyMetaFileW(hSrc, NULL);
229 break;
230 case CF_PALETTE:
232 LOGPALETTE * logpalette;
233 UINT nEntries = GetPaletteEntries(hSrc, 0, 0, NULL);
234 if (!nEntries) return NULL;
235 logpalette = HeapAlloc(GetProcessHeap(), 0,
236 FIELD_OFFSET(LOGPALETTE, palPalEntry[nEntries]));
237 if (!logpalette) return NULL;
238 if (!GetPaletteEntries(hSrc, 0, nEntries, logpalette->palPalEntry))
240 HeapFree(GetProcessHeap(), 0, logpalette);
241 return NULL;
243 logpalette->palVersion = 0x300;
244 logpalette->palNumEntries = (WORD)nEntries;
246 hDst = CreatePalette(logpalette);
248 HeapFree(GetProcessHeap(), 0, logpalette);
249 break;
251 case CF_BITMAP:
253 LONG size;
254 BITMAP bm;
255 if (!GetObjectW(hSrc, sizeof(bm), &bm))
256 return NULL;
257 size = GetBitmapBits(hSrc, 0, NULL);
258 if (!size) return NULL;
259 bm.bmBits = HeapAlloc(GetProcessHeap(), 0, size);
260 if (!bm.bmBits) return NULL;
261 if (GetBitmapBits(hSrc, size, bm.bmBits))
262 hDst = CreateBitmapIndirect(&bm);
263 HeapFree(GetProcessHeap(), 0, bm.bmBits);
264 break;
266 default:
268 SIZE_T size = GlobalSize(hSrc);
269 LPVOID pvSrc = NULL;
270 LPVOID pvDst = NULL;
272 /* allocate space for object */
273 if (!size) return NULL;
274 hDst = GlobalAlloc(uiFlags, size);
275 if (!hDst) return NULL;
277 /* lock pointers */
278 pvSrc = GlobalLock(hSrc);
279 if (!pvSrc)
281 GlobalFree(hDst);
282 return NULL;
284 pvDst = GlobalLock(hDst);
285 if (!pvDst)
287 GlobalUnlock(hSrc);
288 GlobalFree(hDst);
289 return NULL;
291 /* copy data */
292 memcpy(pvDst, pvSrc, size);
294 /* cleanup */
295 GlobalUnlock(hDst);
296 GlobalUnlock(hSrc);
300 TRACE("returning %p\n", hDst);
301 return hDst;