| 1 | /* | 
|---|
| 2 | * Ole 2 Create functions implementation | 
|---|
| 3 | * | 
|---|
| 4 | * Copyright (C) 1999-2000 Abey George | 
|---|
| 5 | */ | 
|---|
| 6 |  | 
|---|
| 7 | #include <string.h> | 
|---|
| 8 | #include "winbase.h" | 
|---|
| 9 | #include "wingdi.h" | 
|---|
| 10 | #include "winuser.h" | 
|---|
| 11 | #include "debugtools.h" | 
|---|
| 12 | #include "ole2.h" | 
|---|
| 13 | #include "olestd.h" | 
|---|
| 14 | #include "winreg.h" | 
|---|
| 15 |  | 
|---|
| 16 | DEFAULT_DEBUG_CHANNEL(ole); | 
|---|
| 17 |  | 
|---|
| 18 | #define MAX_CLIPFORMAT_NAME   80 | 
|---|
| 19 |  | 
|---|
| 20 | /****************************************************************************** | 
|---|
| 21 | * Function : OleQueryCreateFromData [OLE32.117] | 
|---|
| 22 | * Author   : Abey George | 
|---|
| 23 | * Checks whether an object can become an embedded object. | 
|---|
| 24 | * the clipboard or OLE drag and drop. | 
|---|
| 25 | * Returns  : S_OK - Format that supports Embedded object creation are present. | 
|---|
| 26 | *            OLE_E_STATIC - Format that supports static object creation are present. | 
|---|
| 27 | *            S_FALSE - No acceptable format is available. | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | HRESULT WINAPI OleQueryCreateFromData(LPDATAOBJECT pSrcDataObject) | 
|---|
| 31 | { | 
|---|
| 32 | IEnumFORMATETC *pfmt; | 
|---|
| 33 | FORMATETC fmt; | 
|---|
| 34 | CHAR szFmtName[MAX_CLIPFORMAT_NAME]; | 
|---|
| 35 | BOOL bFoundStatic = FALSE; | 
|---|
| 36 |  | 
|---|
| 37 | HRESULT hr = IDataObject_EnumFormatEtc(pSrcDataObject, DATADIR_GET, &pfmt); | 
|---|
| 38 |  | 
|---|
| 39 | if (hr == S_OK) | 
|---|
| 40 | hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL); | 
|---|
| 41 |  | 
|---|
| 42 | while (hr == S_OK) | 
|---|
| 43 | { | 
|---|
| 44 | GetClipboardFormatNameA(fmt.cfFormat, szFmtName, MAX_CLIPFORMAT_NAME-1); | 
|---|
| 45 |  | 
|---|
| 46 | /* first, Check for Embedded Object, Embed Source or Filename */ | 
|---|
| 47 |  | 
|---|
| 48 | if (!strcmp(szFmtName, CF_EMBEDDEDOBJECT) || !strcmp(szFmtName, CF_EMBEDSOURCE) || !strcmp(szFmtName, CF_FILENAME)) | 
|---|
| 49 | return S_OK; | 
|---|
| 50 |  | 
|---|
| 51 | /* Check for Metafile, Bitmap or DIB */ | 
|---|
| 52 |  | 
|---|
| 53 | if (fmt.cfFormat == CF_METAFILEPICT || fmt.cfFormat == CF_BITMAP || fmt.cfFormat == CF_DIB) | 
|---|
| 54 | bFoundStatic = TRUE; | 
|---|
| 55 |  | 
|---|
| 56 | hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | /* Found a static format, but no embed format */ | 
|---|
| 60 |  | 
|---|
| 61 | if (bFoundStatic) | 
|---|
| 62 | return OLE_S_STATIC; | 
|---|
| 63 |  | 
|---|
| 64 | return S_FALSE; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | /****************************************************************************** | 
|---|
| 68 | * Function : OleCreateFromData        [OLE32.92] | 
|---|
| 69 | * Author   : Abey George | 
|---|
| 70 | * Creates an embedded object from data transfer object retrieved from | 
|---|
| 71 | * the clipboard or OLE drag and drop. | 
|---|
| 72 | * Returns  : S_OK - Embedded object was created successfully. | 
|---|
| 73 | *            OLE_E_STATIC - OLE can create only a static object | 
|---|
| 74 | *            DV_E_FORMATETC - No acceptable format is available (only error return code) | 
|---|
| 75 | * TODO : CF_FILENAME, CF_EMBEDEDOBJECT formats. Parameter renderopt is currently ignored. | 
|---|
| 76 | */ | 
|---|
| 77 |  | 
|---|
| 78 | HRESULT WINAPI OleCreateFromData(LPDATAOBJECT pSrcDataObject, REFIID riid, | 
|---|
| 79 | DWORD renderopt, LPFORMATETC pFormatEtc, | 
|---|
| 80 | LPOLECLIENTSITE pClientSite, LPSTORAGE pStg, | 
|---|
| 81 | LPVOID* ppvObj) | 
|---|
| 82 | { | 
|---|
| 83 | IEnumFORMATETC *pfmt; | 
|---|
| 84 | FORMATETC fmt; | 
|---|
| 85 | CHAR szFmtName[MAX_CLIPFORMAT_NAME]; | 
|---|
| 86 | STGMEDIUM std; | 
|---|
| 87 | HRESULT hr; | 
|---|
| 88 | HRESULT hr1; | 
|---|
| 89 |  | 
|---|
| 90 | hr = IDataObject_EnumFormatEtc(pSrcDataObject, DATADIR_GET, &pfmt); | 
|---|
| 91 |  | 
|---|
| 92 | if (hr == S_OK) | 
|---|
| 93 | { | 
|---|
| 94 | memset(&std, 0, sizeof(STGMEDIUM)); | 
|---|
| 95 |  | 
|---|
| 96 | hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL); | 
|---|
| 97 | while (hr == S_OK) | 
|---|
| 98 | { | 
|---|
| 99 | GetClipboardFormatNameA(fmt.cfFormat, szFmtName, MAX_CLIPFORMAT_NAME-1); | 
|---|
| 100 |  | 
|---|
| 101 | /* first, Check for Embedded Object, Embed Source or Filename */ | 
|---|
| 102 | /* TODO: Currently checks only for Embed Source. */ | 
|---|
| 103 |  | 
|---|
| 104 | if (!strcmp(szFmtName, CF_EMBEDSOURCE)) | 
|---|
| 105 | { | 
|---|
| 106 | std.tymed = TYMED_HGLOBAL; | 
|---|
| 107 |  | 
|---|
| 108 | if ((hr1 = IDataObject_GetData(pSrcDataObject, &fmt, &std)) == S_OK) | 
|---|
| 109 | { | 
|---|
| 110 | ILockBytes *ptrILockBytes = 0; | 
|---|
| 111 | IStorage *pStorage = 0; | 
|---|
| 112 | IOleObject *pOleObject = 0; | 
|---|
| 113 | IPersistStorage *pPersistStorage = 0; | 
|---|
| 114 | CLSID clsID; | 
|---|
| 115 |  | 
|---|
| 116 | /* Create ILock bytes */ | 
|---|
| 117 |  | 
|---|
| 118 | hr1 = CreateILockBytesOnHGlobal(std.u.hGlobal, FALSE, &ptrILockBytes); | 
|---|
| 119 |  | 
|---|
| 120 | /* Open storage on the ILock bytes */ | 
|---|
| 121 |  | 
|---|
| 122 | if (hr1 == S_OK) | 
|---|
| 123 | hr1 = StgOpenStorageOnILockBytes(ptrILockBytes, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &pStorage); | 
|---|
| 124 |  | 
|---|
| 125 | /* Get Class ID from the opened storage */ | 
|---|
| 126 |  | 
|---|
| 127 | if (hr1 == S_OK) | 
|---|
| 128 | hr1 = ReadClassStg(pStorage, &clsID); | 
|---|
| 129 |  | 
|---|
| 130 | /* Create default handler for Persist storage */ | 
|---|
| 131 |  | 
|---|
| 132 | if (hr1 == S_OK) | 
|---|
| 133 | hr1 = OleCreateDefaultHandler(&clsID, NULL, &IID_IPersistStorage, (LPVOID*)&pPersistStorage); | 
|---|
| 134 |  | 
|---|
| 135 | /* Load the storage to Persist storage */ | 
|---|
| 136 |  | 
|---|
| 137 | if (hr1 == S_OK) | 
|---|
| 138 | hr1 = IPersistStorage_Load(pPersistStorage, pStorage); | 
|---|
| 139 |  | 
|---|
| 140 | /* Query for IOleObject */ | 
|---|
| 141 |  | 
|---|
| 142 | if (hr1 == S_OK) | 
|---|
| 143 | hr1 = IPersistStorage_QueryInterface(pPersistStorage, &IID_IOleObject, (LPVOID*)&pOleObject); | 
|---|
| 144 |  | 
|---|
| 145 | /* Set client site with the IOleObject */ | 
|---|
| 146 |  | 
|---|
| 147 | if (hr1 == S_OK) | 
|---|
| 148 | hr1 = IOleObject_SetClientSite(pOleObject, pClientSite); | 
|---|
| 149 |  | 
|---|
| 150 | IPersistStorage_Release(pPersistStorage); | 
|---|
| 151 | /* Query for the requested interface */ | 
|---|
| 152 |  | 
|---|
| 153 | if (hr1 == S_OK) | 
|---|
| 154 | hr1 = IPersistStorage_QueryInterface(pPersistStorage, riid, ppvObj); | 
|---|
| 155 |  | 
|---|
| 156 | IPersistStorage_Release(pPersistStorage); | 
|---|
| 157 |  | 
|---|
| 158 | IStorage_Release(pStorage); | 
|---|
| 159 |  | 
|---|
| 160 | if (hr1 == S_OK) | 
|---|
| 161 | return S_OK; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | /* Return error */ | 
|---|
| 165 |  | 
|---|
| 166 | return DV_E_FORMATETC; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL); | 
|---|
| 170 | } | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | return DV_E_FORMATETC; | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|