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