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"
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39 #define MAX_CLIPFORMAT_NAME 80
41 /******************************************************************************
42 * OleQueryCreateFromData [OLE32.@]
44 * Author : Abey George
45 * Checks whether an object can become an embedded object.
46 * the clipboard or OLE drag and drop.
47 * Returns : S_OK - Format that supports Embedded object creation are present.
48 * OLE_E_STATIC - Format that supports static object creation are present.
49 * S_FALSE - No acceptable format is available.
52 HRESULT WINAPI OleQueryCreateFromData(LPDATAOBJECT pSrcDataObject)
56 CHAR szFmtName[MAX_CLIPFORMAT_NAME];
57 BOOL bFoundStatic = FALSE;
59 HRESULT hr = IDataObject_EnumFormatEtc(pSrcDataObject, DATADIR_GET, &pfmt);
62 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
66 GetClipboardFormatNameA(fmt.cfFormat, szFmtName, MAX_CLIPFORMAT_NAME-1);
68 /* first, Check for Embedded Object, Embed Source or Filename */
70 if (!strcmp(szFmtName, CF_EMBEDDEDOBJECT) || !strcmp(szFmtName, CF_EMBEDSOURCE) || !strcmp(szFmtName, CF_FILENAME))
73 /* Check for Metafile, Bitmap or DIB */
75 if (fmt.cfFormat == CF_METAFILEPICT || fmt.cfFormat == CF_BITMAP || fmt.cfFormat == CF_DIB)
78 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
81 /* Found a static format, but no embed format */
89 /******************************************************************************
90 * OleCreateFromData [OLE32.@]
92 * Author : Abey George
93 * Creates an embedded object from data transfer object retrieved from
94 * the clipboard or OLE drag and drop.
95 * Returns : S_OK - Embedded object was created successfully.
96 * OLE_E_STATIC - OLE can create only a static object
97 * DV_E_FORMATETC - No acceptable format is available (only error return code)
98 * TODO : CF_FILENAME, CF_EMBEDEDOBJECT formats. Parameter renderopt is currently ignored.
101 HRESULT WINAPI OleCreateFromData(LPDATAOBJECT pSrcDataObject, REFIID riid,
102 DWORD renderopt, LPFORMATETC pFormatEtc,
103 LPOLECLIENTSITE pClientSite, LPSTORAGE pStg,
106 IEnumFORMATETC *pfmt;
108 CHAR szFmtName[MAX_CLIPFORMAT_NAME];
113 hr = IDataObject_EnumFormatEtc(pSrcDataObject, DATADIR_GET, &pfmt);
117 memset(&std, 0, sizeof(STGMEDIUM));
119 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
122 GetClipboardFormatNameA(fmt.cfFormat, szFmtName, MAX_CLIPFORMAT_NAME-1);
124 /* first, Check for Embedded Object, Embed Source or Filename */
125 /* TODO: Currently checks only for Embed Source. */
127 if (!strcmp(szFmtName, CF_EMBEDSOURCE))
129 std.tymed = TYMED_HGLOBAL;
131 if ((hr1 = IDataObject_GetData(pSrcDataObject, &fmt, &std)) == S_OK)
133 ILockBytes *ptrILockBytes = 0;
134 IStorage *pStorage = 0;
135 IOleObject *pOleObject = 0;
136 IPersistStorage *pPersistStorage = 0;
139 /* Create ILock bytes */
141 hr1 = CreateILockBytesOnHGlobal(std.u.hGlobal, FALSE, &ptrILockBytes);
143 /* Open storage on the ILock bytes */
146 hr1 = StgOpenStorageOnILockBytes(ptrILockBytes, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &pStorage);
148 /* Get Class ID from the opened storage */
151 hr1 = ReadClassStg(pStorage, &clsID);
153 /* Create default handler for Persist storage */
156 hr1 = OleCreateDefaultHandler(&clsID, NULL, &IID_IPersistStorage, (LPVOID*)&pPersistStorage);
158 /* Load the storage to Persist storage */
161 hr1 = IPersistStorage_Load(pPersistStorage, pStorage);
163 /* Query for IOleObject */
166 hr1 = IPersistStorage_QueryInterface(pPersistStorage, &IID_IOleObject, (LPVOID*)&pOleObject);
168 /* Set client site with the IOleObject */
171 hr1 = IOleObject_SetClientSite(pOleObject, pClientSite);
173 IPersistStorage_Release(pPersistStorage);
174 /* Query for the requested interface */
177 hr1 = IPersistStorage_QueryInterface(pPersistStorage, riid, ppvObj);
179 IPersistStorage_Release(pPersistStorage);
181 IStorage_Release(pStorage);
189 return DV_E_FORMATETC;
192 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
196 return DV_E_FORMATETC;
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);