xref: /reactos/dll/win32/riched20/txtsrv.c (revision f04935d8)
1 /*
2  * RichEdit - functions and interfaces around CreateTextServices
3  *
4  * Copyright 2005, 2006, Maarten Lankhorst
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 "config.h"
22 #include "wine/port.h"
23 
24 #define COBJMACROS
25 
26 #include "editor.h"
27 #include "ole2.h"
28 #include "oleauto.h"
29 #include "richole.h"
30 #include "tom.h"
31 #include "imm.h"
32 #include "textserv.h"
33 #include "wine/debug.h"
34 #include "editstr.h"
35 
36 #ifdef __i386__  /* thiscall functions are i386-specific */
37 
38 #define THISCALL(func) __thiscall_ ## func
39 #define DEFINE_THISCALL_WRAPPER(func,args) \
40    extern typeof(func) THISCALL(func); \
41    __ASM_STDCALL_FUNC(__thiscall_ ## func, args, \
42                    "popl %eax\n\t" \
43                    "pushl %ecx\n\t" \
44                    "pushl %eax\n\t" \
45                    "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
46 #else /* __i386__ */
47 
48 #define THISCALL(func) func
49 #define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
50 
51 #endif /* __i386__ */
52 
53 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
54 
55 typedef struct ITextServicesImpl {
56    IUnknown IUnknown_inner;
57    ITextServices ITextServices_iface;
58    IUnknown *outer_unk;
59    LONG ref;
60    ITextHost *pMyHost;
61    CRITICAL_SECTION csTxtSrv;
62    ME_TextEditor *editor;
63    char spare[256];
64 } ITextServicesImpl;
65 
66 static inline ITextServicesImpl *impl_from_IUnknown(IUnknown *iface)
67 {
68    return CONTAINING_RECORD(iface, ITextServicesImpl, IUnknown_inner);
69 }
70 
71 static HRESULT WINAPI ITextServicesImpl_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
72 {
73    ITextServicesImpl *This = impl_from_IUnknown(iface);
74 
75    TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
76 
77    if (IsEqualIID(riid, &IID_IUnknown))
78       *ppv = &This->IUnknown_inner;
79    else if (IsEqualIID(riid, &IID_ITextServices))
80       *ppv = &This->ITextServices_iface;
81    else if (IsEqualIID(riid, &IID_IRichEditOle) || IsEqualIID(riid, &IID_ITextDocument)) {
82       if (!This->editor->reOle)
83          if (!CreateIRichEditOle(This->outer_unk, This->editor, (void **)(&This->editor->reOle)))
84             return E_OUTOFMEMORY;
85       if (IsEqualIID(riid, &IID_ITextDocument))
86          ME_GetITextDocumentInterface(This->editor->reOle, ppv);
87       else
88          *ppv = This->editor->reOle;
89    } else {
90       *ppv = NULL;
91       FIXME("Unknown interface: %s\n", debugstr_guid(riid));
92       return E_NOINTERFACE;
93    }
94 
95    IUnknown_AddRef((IUnknown*)*ppv);
96    return S_OK;
97 }
98 
99 static ULONG WINAPI ITextServicesImpl_AddRef(IUnknown *iface)
100 {
101    ITextServicesImpl *This = impl_from_IUnknown(iface);
102    LONG ref = InterlockedIncrement(&This->ref);
103 
104    TRACE("(%p) ref=%d\n", This, ref);
105 
106    return ref;
107 }
108 
109 static ULONG WINAPI ITextServicesImpl_Release(IUnknown *iface)
110 {
111    ITextServicesImpl *This = impl_from_IUnknown(iface);
112    LONG ref = InterlockedDecrement(&This->ref);
113 
114    TRACE("(%p) ref=%d\n", This, ref);
115 
116    if (!ref)
117    {
118       ME_DestroyEditor(This->editor);
119       This->csTxtSrv.DebugInfo->Spare[0] = 0;
120       DeleteCriticalSection(&This->csTxtSrv);
121       CoTaskMemFree(This);
122    }
123    return ref;
124 }
125 
126 static const IUnknownVtbl textservices_inner_vtbl =
127 {
128    ITextServicesImpl_QueryInterface,
129    ITextServicesImpl_AddRef,
130    ITextServicesImpl_Release
131 };
132 
133 static inline ITextServicesImpl *impl_from_ITextServices(ITextServices *iface)
134 {
135    return CONTAINING_RECORD(iface, ITextServicesImpl, ITextServices_iface);
136 }
137 
138 static HRESULT WINAPI fnTextSrv_QueryInterface(ITextServices *iface, REFIID riid, void **ppv)
139 {
140    ITextServicesImpl *This = impl_from_ITextServices(iface);
141    return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
142 }
143 
144 static ULONG WINAPI fnTextSrv_AddRef(ITextServices *iface)
145 {
146    ITextServicesImpl *This = impl_from_ITextServices(iface);
147    return IUnknown_AddRef(This->outer_unk);
148 }
149 
150 static ULONG WINAPI fnTextSrv_Release(ITextServices *iface)
151 {
152    ITextServicesImpl *This = impl_from_ITextServices(iface);
153    return IUnknown_Release(This->outer_unk);
154 }
155 
156 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxSendMessage(ITextServices *iface, UINT msg, WPARAM wparam,
157                                        LPARAM lparam, LRESULT *plresult)
158 {
159    ITextServicesImpl *This = impl_from_ITextServices(iface);
160    HRESULT hresult;
161    LRESULT lresult;
162 
163    lresult = ME_HandleMessage(This->editor, msg, wparam, lparam, TRUE, &hresult);
164    if (plresult) *plresult = lresult;
165    return hresult;
166 }
167 
168 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxDraw(ITextServices *iface, DWORD dwDrawAspect, LONG lindex,
169                                 void *pvAspect, DVTARGETDEVICE *ptd, HDC hdcDraw, HDC hdcTargetDev,
170                                 LPCRECTL lprcBounds, LPCRECTL lprcWBounds, LPRECT lprcUpdate,
171                                 BOOL (CALLBACK * pfnContinue)(DWORD), DWORD dwContinue,
172                                 LONG lViewId)
173 {
174    ITextServicesImpl *This = impl_from_ITextServices(iface);
175 
176    FIXME("%p: STUB\n", This);
177    return E_NOTIMPL;
178 }
179 
180 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetHScroll(ITextServices *iface, LONG *plMin, LONG *plMax, LONG *plPos,
181                                       LONG *plPage, BOOL *pfEnabled)
182 {
183    ITextServicesImpl *This = impl_from_ITextServices(iface);
184 
185    *plMin = This->editor->horz_si.nMin;
186    *plMax = This->editor->horz_si.nMax;
187    *plPos = This->editor->horz_si.nPos;
188    *plPage = This->editor->horz_si.nPage;
189    *pfEnabled = (This->editor->styleFlags & WS_HSCROLL) != 0;
190    return S_OK;
191 }
192 
193 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetVScroll(ITextServices *iface, LONG *plMin, LONG *plMax, LONG *plPos,
194                                       LONG *plPage, BOOL *pfEnabled)
195 {
196    ITextServicesImpl *This = impl_from_ITextServices(iface);
197 
198    *plMin = This->editor->vert_si.nMin;
199    *plMax = This->editor->vert_si.nMax;
200    *plPos = This->editor->vert_si.nPos;
201    *plPage = This->editor->vert_si.nPage;
202    *pfEnabled = (This->editor->styleFlags & WS_VSCROLL) != 0;
203    return S_OK;
204 }
205 
206 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxSetCursor(ITextServices *iface, DWORD dwDrawAspect, LONG lindex,
207                                        void *pvAspect, DVTARGETDEVICE *ptd, HDC hdcDraw,
208                                        HDC hicTargetDev, LPCRECT lprcClient, INT x, INT y)
209 {
210    ITextServicesImpl *This = impl_from_ITextServices(iface);
211 
212    FIXME("%p: STUB\n", This);
213    return E_NOTIMPL;
214 }
215 
216 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxQueryHitPoint(ITextServices *iface, DWORD dwDrawAspect, LONG lindex,
217                                          void *pvAspect, DVTARGETDEVICE *ptd, HDC hdcDraw,
218                                          HDC hicTargetDev, LPCRECT lprcClient, INT x, INT y,
219                                          DWORD *pHitResult)
220 {
221    ITextServicesImpl *This = impl_from_ITextServices(iface);
222 
223    FIXME("%p: STUB\n", This);
224    return E_NOTIMPL;
225 }
226 
227 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxInplaceActivate(ITextServices *iface, LPCRECT prcClient)
228 {
229    ITextServicesImpl *This = impl_from_ITextServices(iface);
230 
231    FIXME("%p: STUB\n", This);
232    return E_NOTIMPL;
233 }
234 
235 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxInplaceDeactivate(ITextServices *iface)
236 {
237    ITextServicesImpl *This = impl_from_ITextServices(iface);
238 
239    FIXME("%p: STUB\n", This);
240    return E_NOTIMPL;
241 }
242 
243 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxUIActivate(ITextServices *iface)
244 {
245    ITextServicesImpl *This = impl_from_ITextServices(iface);
246 
247    FIXME("%p: STUB\n", This);
248    return E_NOTIMPL;
249 }
250 
251 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxUIDeactivate(ITextServices *iface)
252 {
253    ITextServicesImpl *This = impl_from_ITextServices(iface);
254 
255    FIXME("%p: STUB\n", This);
256    return E_NOTIMPL;
257 }
258 
259 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetText(ITextServices *iface, BSTR *pbstrText)
260 {
261    ITextServicesImpl *This = impl_from_ITextServices(iface);
262    int length;
263 
264    length = ME_GetTextLength(This->editor);
265    if (length)
266    {
267       ME_Cursor start;
268       BSTR bstr;
269       bstr = SysAllocStringByteLen(NULL, length * sizeof(WCHAR));
270       if (bstr == NULL)
271          return E_OUTOFMEMORY;
272 
273       ME_CursorFromCharOfs(This->editor, 0, &start);
274       ME_GetTextW(This->editor, bstr, length, &start, INT_MAX, FALSE, FALSE);
275       *pbstrText = bstr;
276    } else {
277       *pbstrText = NULL;
278    }
279 
280    return S_OK;
281 }
282 
283 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxSetText(ITextServices *iface, LPCWSTR pszText)
284 {
285    ITextServicesImpl *This = impl_from_ITextServices(iface);
286    ME_Cursor cursor;
287 
288    ME_SetCursorToStart(This->editor, &cursor);
289    ME_InternalDeleteText(This->editor, &cursor, ME_GetTextLength(This->editor), FALSE);
290    if(pszText)
291        ME_InsertTextFromCursor(This->editor, 0, pszText, -1, This->editor->pBuffer->pDefaultStyle);
292    ME_SetSelection(This->editor, 0, 0);
293    This->editor->nModifyStep = 0;
294    OleFlushClipboard();
295    ME_EmptyUndoStack(This->editor);
296    ME_UpdateRepaint(This->editor, FALSE);
297 
298    return S_OK;
299 }
300 
301 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetCurTargetX(ITextServices *iface, LONG *x)
302 {
303    ITextServicesImpl *This = impl_from_ITextServices(iface);
304 
305    FIXME("%p: STUB\n", This);
306    return E_NOTIMPL;
307 }
308 
309 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetBaseLinePos(ITextServices *iface, LONG *x)
310 {
311    ITextServicesImpl *This = impl_from_ITextServices(iface);
312 
313    FIXME("%p: STUB\n", This);
314    return E_NOTIMPL;
315 }
316 
317 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetNaturalSize(ITextServices *iface, DWORD dwAspect, HDC hdcDraw,
318                                           HDC hicTargetDev, DVTARGETDEVICE *ptd, DWORD dwMode,
319                                           const SIZEL *psizelExtent, LONG *pwidth, LONG *pheight)
320 {
321    ITextServicesImpl *This = impl_from_ITextServices(iface);
322 
323    FIXME("%p: STUB\n", This);
324    return E_NOTIMPL;
325 }
326 
327 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetDropTarget(ITextServices *iface, IDropTarget **ppDropTarget)
328 {
329    ITextServicesImpl *This = impl_from_ITextServices(iface);
330 
331    FIXME("%p: STUB\n", This);
332    return E_NOTIMPL;
333 }
334 
335 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxPropertyBitsChange(ITextServices *iface, DWORD dwMask, DWORD dwBits)
336 {
337    ITextServicesImpl *This = impl_from_ITextServices(iface);
338 
339    FIXME("%p: STUB\n", This);
340    return E_NOTIMPL;
341 }
342 
343 DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetCachedSize(ITextServices *iface, DWORD *pdwWidth, DWORD *pdwHeight)
344 {
345    ITextServicesImpl *This = impl_from_ITextServices(iface);
346 
347    FIXME("%p: STUB\n", This);
348    return E_NOTIMPL;
349 }
350 
351 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxSendMessage,20)
352 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxDraw,52)
353 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetHScroll,24)
354 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetVScroll,24)
355 DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxSetCursor,40)
356 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxQueryHitPoint,44)
357 DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxInplaceActivate,8)
358 DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxInplaceDeactivate,4)
359 DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxUIActivate,4)
360 DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxUIDeactivate,4)
361 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetText,8)
362 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxSetText,8)
363 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetCurTargetX,8)
364 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetBaseLinePos,8)
365 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetNaturalSize,36)
366 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetDropTarget,8)
367 DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxPropertyBitsChange,12)
368 DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetCachedSize,12)
369 
370 static const ITextServicesVtbl textservices_vtbl =
371 {
372    fnTextSrv_QueryInterface,
373    fnTextSrv_AddRef,
374    fnTextSrv_Release,
375    THISCALL(fnTextSrv_TxSendMessage),
376    THISCALL(fnTextSrv_TxDraw),
377    THISCALL(fnTextSrv_TxGetHScroll),
378    THISCALL(fnTextSrv_TxGetVScroll),
379    THISCALL(fnTextSrv_OnTxSetCursor),
380    THISCALL(fnTextSrv_TxQueryHitPoint),
381    THISCALL(fnTextSrv_OnTxInplaceActivate),
382    THISCALL(fnTextSrv_OnTxInplaceDeactivate),
383    THISCALL(fnTextSrv_OnTxUIActivate),
384    THISCALL(fnTextSrv_OnTxUIDeactivate),
385    THISCALL(fnTextSrv_TxGetText),
386    THISCALL(fnTextSrv_TxSetText),
387    THISCALL(fnTextSrv_TxGetCurTargetX),
388    THISCALL(fnTextSrv_TxGetBaseLinePos),
389    THISCALL(fnTextSrv_TxGetNaturalSize),
390    THISCALL(fnTextSrv_TxGetDropTarget),
391    THISCALL(fnTextSrv_OnTxPropertyBitsChange),
392    THISCALL(fnTextSrv_TxGetCachedSize)
393 };
394 
395 /******************************************************************
396  *        CreateTextServices (RICHED20.4)
397  */
398 HRESULT WINAPI CreateTextServices(IUnknown  *pUnkOuter, ITextHost *pITextHost, IUnknown  **ppUnk)
399 {
400    ITextServicesImpl *ITextImpl;
401 
402    TRACE("%p %p --> %p\n", pUnkOuter, pITextHost, ppUnk);
403    if (pITextHost == NULL)
404       return E_POINTER;
405 
406    ITextImpl = CoTaskMemAlloc(sizeof(*ITextImpl));
407    if (ITextImpl == NULL)
408       return E_OUTOFMEMORY;
409    InitializeCriticalSection(&ITextImpl->csTxtSrv);
410    ITextImpl->csTxtSrv.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ITextServicesImpl.csTxtSrv");
411    ITextImpl->ref = 1;
412    ITextHost_AddRef(pITextHost);
413    ITextImpl->pMyHost = pITextHost;
414    ITextImpl->IUnknown_inner.lpVtbl = &textservices_inner_vtbl;
415    ITextImpl->ITextServices_iface.lpVtbl = &textservices_vtbl;
416    ITextImpl->editor = ME_MakeEditor(pITextHost, FALSE);
417 
418    if (pUnkOuter)
419       ITextImpl->outer_unk = pUnkOuter;
420    else
421       ITextImpl->outer_unk = &ITextImpl->IUnknown_inner;
422 
423    *ppUnk = &ITextImpl->IUnknown_inner;
424    return S_OK;
425 }
426