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