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