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