riched20: Use ME_PointFromChar to calculate the caret position.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "wine/debug.h"
33 #include "ole2.h"
34 #include "olestd.h"
35 #include "compobj_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
38
39 #define MAX_CLIPFORMAT_NAME   80
40
41 /******************************************************************************
42  *              OleQueryCreateFromData [OLE32.@]
43  *
44  * Checks whether an object can become an embedded object.
45  * the clipboard or OLE drag and drop.
46  * Returns  : S_OK - Format that supports Embedded object creation are present.
47  *            OLE_E_STATIC - Format that supports static object creation are present.
48  *            S_FALSE - No acceptable format is available.
49  */
50
51 HRESULT WINAPI OleQueryCreateFromData(IDataObject *data)
52 {
53     IEnumFORMATETC *enum_fmt;
54     FORMATETC fmt;
55     BOOL found_static = FALSE;
56     HRESULT hr;
57
58     hr = IDataObject_EnumFormatEtc(data, DATADIR_GET, &enum_fmt);
59
60     if(FAILED(hr)) return hr;
61
62     do
63     {
64         hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
65         if(hr == S_OK)
66         {
67             if(fmt.cfFormat == embedded_object_clipboard_format ||
68                fmt.cfFormat == embed_source_clipboard_format ||
69                fmt.cfFormat == filename_clipboard_format)
70             {
71                 IEnumFORMATETC_Release(enum_fmt);
72                 return S_OK;
73             }
74
75             if(fmt.cfFormat == CF_METAFILEPICT ||
76                fmt.cfFormat == CF_BITMAP ||
77                fmt.cfFormat == CF_DIB)
78                 found_static = TRUE;
79         }
80     } while (hr == S_OK);
81
82     IEnumFORMATETC_Release(enum_fmt);
83
84     return found_static ? OLE_S_STATIC : S_FALSE;
85 }
86
87 static inline void init_fmtetc(FORMATETC *fmt, CLIPFORMAT cf, TYMED tymed)
88 {
89     fmt->cfFormat = cf;
90     fmt->ptd = NULL;
91     fmt->dwAspect = DVASPECT_CONTENT;
92     fmt->lindex = -1;
93     fmt->tymed = tymed;
94 }
95
96 /***************************************************************************
97  *         get_storage
98  *
99  * Retrieve an object's storage from a variety of sources.
100  *
101  * FIXME: CF_FILENAME.
102  */
103 static HRESULT get_storage(IDataObject *data, IStorage *stg, UINT *src_cf)
104 {
105     static const UINT fmt_id[] = { CF_METAFILEPICT, CF_BITMAP, CF_DIB };
106     UINT i;
107     HRESULT hr;
108     FORMATETC fmt;
109     STGMEDIUM med;
110     IPersistStorage *persist;
111     CLSID clsid;
112
113     *src_cf = 0;
114
115     /* CF_EMBEDEDOBJECT */
116     init_fmtetc(&fmt, embedded_object_clipboard_format, TYMED_ISTORAGE);
117     med.tymed = TYMED_ISTORAGE;
118     med.u.pstg = stg;
119     hr = IDataObject_GetDataHere(data, &fmt, &med);
120     if(SUCCEEDED(hr))
121     {
122         *src_cf = embedded_object_clipboard_format;
123         return hr;
124     }
125
126     /* CF_EMBEDSOURCE */
127     init_fmtetc(&fmt, embed_source_clipboard_format, TYMED_ISTORAGE);
128     med.tymed = TYMED_ISTORAGE;
129     med.u.pstg = stg;
130     hr = IDataObject_GetDataHere(data, &fmt, &med);
131     if(SUCCEEDED(hr))
132     {
133         *src_cf = embed_source_clipboard_format;
134         return hr;
135     }
136
137     for (i = 0; i < sizeof(fmt_id)/sizeof(fmt_id[0]); i++)
138     {
139         init_fmtetc(&fmt, fmt_id[i], TYMED_ISTORAGE);
140         hr = IDataObject_QueryGetData(data, &fmt);
141         if(SUCCEEDED(hr))
142         {
143             *src_cf = fmt_id[i];
144             return hr;
145         }
146     }
147
148     /* IPersistStorage */
149     hr = IDataObject_QueryInterface(data, &IID_IPersistStorage, (void**)&persist);
150     if(FAILED(hr)) return hr;
151
152     hr = IPersistStorage_GetClassID(persist, &clsid);
153     if(FAILED(hr)) goto end;
154
155     hr = IStorage_SetClass(stg, &clsid);
156     if(FAILED(hr)) goto end;
157
158     hr = IPersistStorage_Save(persist, stg, FALSE);
159     if(FAILED(hr)) goto end;
160
161     hr = IPersistStorage_SaveCompleted(persist, NULL);
162
163 end:
164     IPersistStorage_Release(persist);
165
166     return hr;
167 }
168
169 /******************************************************************************
170  *              OleCreateFromDataEx        [OLE32.@]
171  *
172  * Creates an embedded object from data transfer object retrieved from
173  * the clipboard or OLE drag and drop.
174  */
175 HRESULT WINAPI OleCreateFromDataEx(IDataObject *data, REFIID iid, DWORD flags,
176                                    DWORD renderopt, ULONG num_cache_fmts, DWORD *adv_flags, FORMATETC *cache_fmts,
177                                    IAdviseSink *sink, DWORD *conns,
178                                    IOleClientSite *client_site, IStorage *stg, void **obj)
179 {
180     HRESULT hr;
181     UINT src_cf;
182
183     FIXME("(%p, %s, %08x, %08x, %d, %p, %p, %p, %p, %p, %p, %p): stub\n",
184           data, debugstr_guid(iid), flags, renderopt, num_cache_fmts, adv_flags, cache_fmts,
185           sink, conns, client_site, stg, obj);
186
187     hr = get_storage(data, stg, &src_cf);
188     if(FAILED(hr)) return hr;
189
190     hr = OleLoad(stg, iid, client_site, obj);
191     if(FAILED(hr)) return hr;
192
193     /* FIXME: Init cache */
194
195     return hr;
196 }
197
198 /******************************************************************************
199  *              OleCreateFromData        [OLE32.@]
200  */
201 HRESULT WINAPI OleCreateFromData(LPDATAOBJECT data, REFIID iid,
202                                  DWORD renderopt, LPFORMATETC fmt,
203                                  LPOLECLIENTSITE client_site, LPSTORAGE stg,
204                                  LPVOID* obj)
205 {
206     DWORD advf = ADVF_PRIMEFIRST;
207
208     return OleCreateFromDataEx(data, iid, 0, renderopt, fmt ? 1 : 0, fmt ? &advf : NULL,
209                                fmt, NULL, NULL, client_site, stg, obj);
210 }
211
212 /******************************************************************************
213  *              OleCreateLinkFromData        [OLE32.@]
214  */
215 HRESULT WINAPI OleCreateLinkFromData(IDataObject *data, REFIID iid,
216                                      DWORD renderopt, FORMATETC *fmt,
217                                      IOleClientSite *client_site, IStorage *stg,
218                                      void **obj)
219 {
220     FIXME("%p,%s,%08x,%p,%p,%p,%p: semi-stub\n",
221           data, debugstr_guid(iid), renderopt, fmt, client_site, stg, obj);
222     return OleCreateFromData(data, iid, renderopt, fmt, client_site, stg, obj);
223 }
224
225 /******************************************************************************
226  *              OleCreateStaticFromData        [OLE32.@]
227  */
228 HRESULT WINAPI OleCreateStaticFromData(IDataObject *data, REFIID iid,
229                                        DWORD renderopt, FORMATETC *fmt,
230                                        IOleClientSite *client_site, IStorage *stg,
231                                        void **obj)
232 {
233     FIXME("%p,%s,%08x,%p,%p,%p,%p: semi-stub\n",
234           data, debugstr_guid(iid), renderopt, fmt, client_site, stg, obj);
235     return OleCreateFromData(data, iid, renderopt, fmt, client_site, stg, obj);
236 }
237
238 /******************************************************************************
239  *              OleDuplicateData        [OLE32.@]
240  *
241  * Duplicates clipboard data.
242  *
243  * PARAMS
244  *  hSrc     [I] Handle of the source clipboard data.
245  *  cfFormat [I] The clipboard format of hSrc.
246  *  uiFlags  [I] Flags to pass to GlobalAlloc.
247  *
248  * RETURNS
249  *  Success: handle to the duplicated data.
250  *  Failure: NULL.
251  */
252 HANDLE WINAPI OleDuplicateData(HANDLE hSrc, CLIPFORMAT cfFormat,
253                                   UINT uiFlags)
254 {
255     HANDLE hDst = NULL;
256
257     TRACE("(%p,%x,%x)\n", hSrc, cfFormat, uiFlags);
258
259     if (!uiFlags) uiFlags = GMEM_MOVEABLE;
260
261     switch (cfFormat)
262     {
263     case CF_ENHMETAFILE:
264         hDst = CopyEnhMetaFileW(hSrc, NULL);
265         break;
266     case CF_METAFILEPICT:
267         hDst = CopyMetaFileW(hSrc, NULL);
268         break;
269     case CF_PALETTE:
270         {
271             LOGPALETTE * logpalette;
272             UINT nEntries = GetPaletteEntries(hSrc, 0, 0, NULL);
273             if (!nEntries) return NULL;
274             logpalette = HeapAlloc(GetProcessHeap(), 0,
275                 FIELD_OFFSET(LOGPALETTE, palPalEntry[nEntries]));
276             if (!logpalette) return NULL;
277             if (!GetPaletteEntries(hSrc, 0, nEntries, logpalette->palPalEntry))
278             {
279                 HeapFree(GetProcessHeap(), 0, logpalette);
280                 return NULL;
281             }
282             logpalette->palVersion = 0x300;
283             logpalette->palNumEntries = (WORD)nEntries;
284
285             hDst = CreatePalette(logpalette);
286
287             HeapFree(GetProcessHeap(), 0, logpalette);
288             break;
289         }
290     case CF_BITMAP:
291         {
292             LONG size;
293             BITMAP bm;
294             if (!GetObjectW(hSrc, sizeof(bm), &bm))
295                 return NULL;
296             size = GetBitmapBits(hSrc, 0, NULL);
297             if (!size) return NULL;
298             bm.bmBits = HeapAlloc(GetProcessHeap(), 0, size);
299             if (!bm.bmBits) return NULL;
300             if (GetBitmapBits(hSrc, size, bm.bmBits))
301                 hDst = CreateBitmapIndirect(&bm);
302             HeapFree(GetProcessHeap(), 0, bm.bmBits);
303             break;
304         }
305     default:
306         {
307             SIZE_T size = GlobalSize(hSrc);
308             LPVOID pvSrc = NULL;
309             LPVOID pvDst = NULL;
310
311             /* allocate space for object */
312             if (!size) return NULL;
313             hDst = GlobalAlloc(uiFlags, size);
314             if (!hDst) return NULL;
315
316             /* lock pointers */
317             pvSrc = GlobalLock(hSrc);
318             if (!pvSrc)
319             {
320                 GlobalFree(hDst);
321                 return NULL;
322             }
323             pvDst = GlobalLock(hDst);
324             if (!pvDst)
325             {
326                 GlobalUnlock(hSrc);
327                 GlobalFree(hDst);
328                 return NULL;
329             }
330             /* copy data */
331             memcpy(pvDst, pvSrc, size);
332
333             /* cleanup */
334             GlobalUnlock(hDst);
335             GlobalUnlock(hSrc);
336         }
337     }
338
339     TRACE("returning %p\n", hDst);
340     return hDst;
341 }