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