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