xref: /reactos/dll/win32/riched20/clipboard.c (revision fb5d5ecd)
1 /*
2  * Richedit clipboard handling
3  *
4  * Copyright (C) 2006 Kevin Koltzau
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 #define NONAMELESSUNION
22 
23 #include "editor.h"
24 
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 
27 static UINT cfRTF = 0;
28 
29 typedef struct DataObjectImpl {
30     IDataObject IDataObject_iface;
31     LONG ref;
32 
33     FORMATETC *fmtetc;
34     UINT fmtetc_cnt;
35 
36     HANDLE unicode;
37     HANDLE rtf;
38 } DataObjectImpl;
39 
40 typedef struct EnumFormatImpl {
41     IEnumFORMATETC IEnumFORMATETC_iface;
42     LONG ref;
43 
44     FORMATETC *fmtetc;
45     UINT fmtetc_cnt;
46 
47     UINT cur;
48 } EnumFormatImpl;
49 
50 static HRESULT EnumFormatImpl_Create(const FORMATETC *fmtetc, UINT size, LPENUMFORMATETC *lplpformatetc);
51 
52 static inline DataObjectImpl *impl_from_IDataObject(IDataObject *iface)
53 {
54     return CONTAINING_RECORD(iface, DataObjectImpl, IDataObject_iface);
55 }
56 
57 static inline EnumFormatImpl *impl_from_IEnumFORMATETC(IEnumFORMATETC *iface)
58 {
59     return CONTAINING_RECORD(iface, EnumFormatImpl, IEnumFORMATETC_iface);
60 }
61 
62 static HRESULT WINAPI EnumFormatImpl_QueryInterface(IEnumFORMATETC *iface, REFIID riid, LPVOID *ppvObj)
63 {
64     EnumFormatImpl *This = impl_from_IEnumFORMATETC(iface);
65     TRACE("%p %s\n", This, debugstr_guid(riid));
66 
67     if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IEnumFORMATETC)) {
68         IEnumFORMATETC_AddRef(iface);
69         *ppvObj = &This->IEnumFORMATETC_iface;
70         return S_OK;
71     }
72     *ppvObj = NULL;
73     return E_NOINTERFACE;
74 }
75 
76 static ULONG WINAPI EnumFormatImpl_AddRef(IEnumFORMATETC *iface)
77 {
78     EnumFormatImpl *This = impl_from_IEnumFORMATETC(iface);
79     LONG ref = InterlockedIncrement(&This->ref);
80     TRACE("(%p) ref=%d\n", This, ref);
81     return ref;
82 }
83 
84 static ULONG WINAPI EnumFormatImpl_Release(IEnumFORMATETC *iface)
85 {
86     EnumFormatImpl *This = impl_from_IEnumFORMATETC(iface);
87     ULONG ref = InterlockedDecrement(&This->ref);
88     TRACE("(%p) ref=%d\n", This, ref);
89 
90     if(!ref) {
91         GlobalFree(This->fmtetc);
92         heap_free(This);
93     }
94 
95     return ref;
96 }
97 
98 static HRESULT WINAPI EnumFormatImpl_Next(IEnumFORMATETC *iface, ULONG celt,
99                                           FORMATETC *rgelt, ULONG *pceltFetched)
100 {
101     EnumFormatImpl *This = impl_from_IEnumFORMATETC(iface);
102     ULONG count = 0;
103     TRACE("(%p)->(%d %p %p)\n", This, celt, rgelt, pceltFetched);
104 
105     if(!rgelt)
106         return E_INVALIDARG;
107 
108     count = min(celt, This->fmtetc_cnt-This->cur);
109     if(count > 0) {
110         memcpy(rgelt, This->fmtetc+This->cur, count*sizeof(FORMATETC));
111         This->cur += count;
112     }
113     if(pceltFetched)
114         *pceltFetched = count;
115     return count == celt ? S_OK : S_FALSE;
116 }
117 
118 static HRESULT WINAPI EnumFormatImpl_Skip(IEnumFORMATETC *iface, ULONG celt)
119 {
120     EnumFormatImpl *This = impl_from_IEnumFORMATETC(iface);
121     ULONG count = 0;
122     TRACE("(%p)->(%d)\n", This, celt);
123 
124     count = min(celt, This->fmtetc_cnt-This->cur);
125     This->cur += count;
126     return count == celt ? S_OK : S_FALSE;
127 }
128 
129 static HRESULT WINAPI EnumFormatImpl_Reset(IEnumFORMATETC *iface)
130 {
131     EnumFormatImpl *This = impl_from_IEnumFORMATETC(iface);
132     TRACE("(%p)\n", This);
133 
134     This->cur = 0;
135     return S_OK;
136 }
137 
138 static HRESULT WINAPI EnumFormatImpl_Clone(IEnumFORMATETC *iface, IEnumFORMATETC **ppenum)
139 {
140     EnumFormatImpl *This = impl_from_IEnumFORMATETC(iface);
141     HRESULT hr;
142     TRACE("(%p)->(%p)\n", This, ppenum);
143 
144     if(!ppenum)
145         return E_INVALIDARG;
146     hr = EnumFormatImpl_Create(This->fmtetc, This->fmtetc_cnt, ppenum);
147     if(SUCCEEDED(hr))
148         hr = IEnumFORMATETC_Skip(*ppenum, This->cur);
149     return hr;
150 }
151 
152 static const IEnumFORMATETCVtbl VT_EnumFormatImpl = {
153     EnumFormatImpl_QueryInterface,
154     EnumFormatImpl_AddRef,
155     EnumFormatImpl_Release,
156     EnumFormatImpl_Next,
157     EnumFormatImpl_Skip,
158     EnumFormatImpl_Reset,
159     EnumFormatImpl_Clone
160 };
161 
162 static HRESULT EnumFormatImpl_Create(const FORMATETC *fmtetc, UINT fmtetc_cnt,
163                                      IEnumFORMATETC **formatetc)
164 {
165     EnumFormatImpl *ret;
166     TRACE("\n");
167 
168     ret = heap_alloc(sizeof(EnumFormatImpl));
169     ret->IEnumFORMATETC_iface.lpVtbl = &VT_EnumFormatImpl;
170     ret->ref = 1;
171     ret->cur = 0;
172     ret->fmtetc_cnt = fmtetc_cnt;
173     ret->fmtetc = GlobalAlloc(GMEM_ZEROINIT, fmtetc_cnt*sizeof(FORMATETC));
174     memcpy(ret->fmtetc, fmtetc, fmtetc_cnt*sizeof(FORMATETC));
175     *formatetc = &ret->IEnumFORMATETC_iface;
176     return S_OK;
177 }
178 
179 static HRESULT WINAPI DataObjectImpl_QueryInterface(IDataObject *iface, REFIID riid, LPVOID *ppvObj)
180 {
181     DataObjectImpl *This = impl_from_IDataObject(iface);
182     TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
183 
184     if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IDataObject)) {
185         IDataObject_AddRef(iface);
186         *ppvObj = &This->IDataObject_iface;
187         return S_OK;
188     }
189     *ppvObj = NULL;
190     return E_NOINTERFACE;
191 }
192 
193 static ULONG WINAPI DataObjectImpl_AddRef(IDataObject* iface)
194 {
195     DataObjectImpl *This = impl_from_IDataObject(iface);
196     ULONG ref = InterlockedIncrement(&This->ref);
197     TRACE("(%p) ref=%d\n", This, ref);
198     return ref;
199 }
200 
201 static ULONG WINAPI DataObjectImpl_Release(IDataObject* iface)
202 {
203     DataObjectImpl *This = impl_from_IDataObject(iface);
204     ULONG ref = InterlockedDecrement(&This->ref);
205     TRACE("(%p) ref=%d\n",This, ref);
206 
207     if(!ref) {
208         if(This->unicode) GlobalFree(This->unicode);
209         if(This->rtf) GlobalFree(This->rtf);
210         if(This->fmtetc) GlobalFree(This->fmtetc);
211         heap_free(This);
212     }
213 
214     return ref;
215 }
216 
217 static HRESULT WINAPI DataObjectImpl_GetData(IDataObject* iface, FORMATETC *pformatetc, STGMEDIUM *pmedium)
218 {
219     DataObjectImpl *This = impl_from_IDataObject(iface);
220     TRACE("(%p)->(fmt=0x%08x tym=0x%08x)\n", This, pformatetc->cfFormat, pformatetc->tymed);
221 
222     if(pformatetc->lindex != -1)
223         return DV_E_LINDEX;
224 
225     if(!(pformatetc->tymed & TYMED_HGLOBAL))
226         return DV_E_TYMED;
227 
228     if(This->unicode && pformatetc->cfFormat == CF_UNICODETEXT)
229         pmedium->u.hGlobal = This->unicode;
230     else if(This->rtf && pformatetc->cfFormat == cfRTF)
231         pmedium->u.hGlobal = This->rtf;
232     else
233         return DV_E_FORMATETC;
234 
235     pmedium->tymed = TYMED_HGLOBAL;
236     pmedium->pUnkForRelease = (LPUNKNOWN)iface;
237     IUnknown_AddRef(pmedium->pUnkForRelease);
238     return S_OK;
239 }
240 
241 static HRESULT WINAPI DataObjectImpl_GetDataHere(IDataObject* iface, FORMATETC *pformatetc, STGMEDIUM *pmedium)
242 {
243     DataObjectImpl *This = impl_from_IDataObject(iface);
244     FIXME("(%p): stub\n", This);
245     return E_NOTIMPL;
246 }
247 
248 static HRESULT WINAPI DataObjectImpl_QueryGetData(IDataObject* iface, FORMATETC *pformatetc)
249 {
250     DataObjectImpl *This = impl_from_IDataObject(iface);
251     UINT i;
252     BOOL foundFormat = FALSE;
253     TRACE("(%p)->(fmt=0x%08x tym=0x%08x)\n", This, pformatetc->cfFormat, pformatetc->tymed);
254 
255     if(pformatetc->lindex != -1)
256         return DV_E_LINDEX;
257 
258     for(i=0; i<This->fmtetc_cnt; i++) {
259         if(This->fmtetc[i].cfFormat == pformatetc->cfFormat) {
260             foundFormat = TRUE;
261             if(This->fmtetc[i].tymed == pformatetc->tymed)
262                 return S_OK;
263         }
264     }
265     return foundFormat?DV_E_FORMATETC:DV_E_TYMED;
266 }
267 
268 static HRESULT WINAPI DataObjectImpl_GetCanonicalFormatEtc(IDataObject* iface, FORMATETC *pformatetcIn,
269                                                            FORMATETC *pformatetcOut)
270 {
271     DataObjectImpl *This = impl_from_IDataObject(iface);
272     TRACE("(%p)->(%p,%p)\n", This, pformatetcIn, pformatetcOut);
273 
274     if(pformatetcOut) {
275         *pformatetcOut = *pformatetcIn;
276         pformatetcOut->ptd = NULL;
277     }
278     return DATA_S_SAMEFORMATETC;
279 }
280 
281 static HRESULT WINAPI DataObjectImpl_SetData(IDataObject* iface, FORMATETC *pformatetc,
282                                              STGMEDIUM *pmedium, BOOL fRelease)
283 {
284     DataObjectImpl *This = impl_from_IDataObject(iface);
285     FIXME("(%p): stub\n", This);
286     return E_NOTIMPL;
287 }
288 
289 static HRESULT WINAPI DataObjectImpl_EnumFormatEtc(IDataObject* iface, DWORD dwDirection,
290                                                    IEnumFORMATETC **ppenumFormatEtc)
291 {
292     DataObjectImpl *This = impl_from_IDataObject(iface);
293     TRACE("(%p)->(%d)\n", This, dwDirection);
294 
295     if(dwDirection != DATADIR_GET) {
296         FIXME("Unsupported direction: %d\n", dwDirection);
297         /* WinXP riched20 also returns E_NOTIMPL in this case */
298         *ppenumFormatEtc = NULL;
299         return E_NOTIMPL;
300     }
301     return EnumFormatImpl_Create(This->fmtetc, This->fmtetc_cnt, ppenumFormatEtc);
302 }
303 
304 static HRESULT WINAPI DataObjectImpl_DAdvise(IDataObject* iface, FORMATETC *pformatetc, DWORD advf,
305                                              IAdviseSink *pAdvSink, DWORD *pdwConnection)
306 {
307     DataObjectImpl *This = impl_from_IDataObject(iface);
308     FIXME("(%p): stub\n", This);
309     return E_NOTIMPL;
310 }
311 
312 static HRESULT WINAPI DataObjectImpl_DUnadvise(IDataObject* iface, DWORD dwConnection)
313 {
314     DataObjectImpl *This = impl_from_IDataObject(iface);
315     FIXME("(%p): stub\n", This);
316     return E_NOTIMPL;
317 }
318 
319 static HRESULT WINAPI DataObjectImpl_EnumDAdvise(IDataObject* iface, IEnumSTATDATA **ppenumAdvise)
320 {
321     DataObjectImpl *This = impl_from_IDataObject(iface);
322     FIXME("(%p): stub\n", This);
323     return E_NOTIMPL;
324 }
325 
326 static const IDataObjectVtbl VT_DataObjectImpl =
327 {
328     DataObjectImpl_QueryInterface,
329     DataObjectImpl_AddRef,
330     DataObjectImpl_Release,
331     DataObjectImpl_GetData,
332     DataObjectImpl_GetDataHere,
333     DataObjectImpl_QueryGetData,
334     DataObjectImpl_GetCanonicalFormatEtc,
335     DataObjectImpl_SetData,
336     DataObjectImpl_EnumFormatEtc,
337     DataObjectImpl_DAdvise,
338     DataObjectImpl_DUnadvise,
339     DataObjectImpl_EnumDAdvise
340 };
341 
342 static HGLOBAL get_unicode_text(ME_TextEditor *editor, const ME_Cursor *start, int nChars)
343 {
344     int pars = 0;
345     WCHAR *data;
346     HANDLE ret;
347     ME_DisplayItem *para;
348     int nEnd = ME_GetCursorOfs(start) + nChars;
349 
350     /* count paragraphs in range */
351     para = start->pPara;
352     while((para = para->member.para.next_para) &&
353           para->member.para.nCharOfs <= nEnd)
354         pars++;
355 
356     ret = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR) * (nChars + pars + 1));
357     data = GlobalLock(ret);
358     ME_GetTextW(editor, data, nChars + pars, start, nChars, TRUE, FALSE);
359     GlobalUnlock(ret);
360     return ret;
361 }
362 
363 typedef struct tagME_GlobalDestStruct
364 {
365   HGLOBAL hData;
366   int nLength;
367 } ME_GlobalDestStruct;
368 
369 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
370 {
371     ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
372     int nMaxSize;
373     BYTE *pDest;
374 
375     nMaxSize = GlobalSize(pData->hData);
376     if (pData->nLength+cb+1 >= cb) {
377         /* round up to 2^17 */
378         int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
379         pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
380     }
381     pDest = GlobalLock(pData->hData);
382     memcpy(pDest + pData->nLength, lpBuff, cb);
383     pData->nLength += cb;
384     pDest[pData->nLength] = '\0';
385     GlobalUnlock(pData->hData);
386     *pcb = cb;
387 
388     return 0;
389 }
390 
391 static HGLOBAL get_rtf_text(ME_TextEditor *editor, const ME_Cursor *start, int nChars)
392 {
393     EDITSTREAM es;
394     ME_GlobalDestStruct gds;
395 
396     gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
397     gds.nLength = 0;
398     es.dwCookie = (DWORD_PTR)&gds;
399     es.pfnCallback = ME_AppendToHGLOBAL;
400     ME_StreamOutRange(editor, SF_RTF, start, nChars, &es);
401     GlobalReAlloc(gds.hData, gds.nLength+1, 0);
402     return gds.hData;
403 }
404 
405 HRESULT ME_GetDataObject(ME_TextEditor *editor, const ME_Cursor *start, int nChars,
406                          IDataObject **dataobj)
407 {
408     DataObjectImpl *obj;
409     TRACE("(%p,%d,%d)\n", editor, ME_GetCursorOfs(start), nChars);
410 
411     obj = heap_alloc(sizeof(DataObjectImpl));
412     if(cfRTF == 0)
413         cfRTF = RegisterClipboardFormatA("Rich Text Format");
414 
415     obj->IDataObject_iface.lpVtbl = &VT_DataObjectImpl;
416     obj->ref = 1;
417     obj->unicode = get_unicode_text(editor, start, nChars);
418     obj->rtf = NULL;
419 
420     obj->fmtetc_cnt = 1;
421     if(editor->mode & TM_RICHTEXT)
422         obj->fmtetc_cnt++;
423     obj->fmtetc = GlobalAlloc(GMEM_ZEROINIT, obj->fmtetc_cnt*sizeof(FORMATETC));
424     InitFormatEtc(obj->fmtetc[0], CF_UNICODETEXT, TYMED_HGLOBAL);
425     if(editor->mode & TM_RICHTEXT) {
426         obj->rtf = get_rtf_text(editor, start, nChars);
427         InitFormatEtc(obj->fmtetc[1], cfRTF, TYMED_HGLOBAL);
428     }
429 
430     *dataobj = &obj->IDataObject_iface;
431     return S_OK;
432 }
433