riched20/tests: Make sure return value is used (LLVM/Clang).
[wine] / dlls / riched20 / tests / txtsrv.c
1 /*
2  * Unit test suite for windowless rich edit controls
3  *
4  * Copyright 2008 Maarten Lankhorst
5  * Copyright 2008 Austin Lund
6  * Copyright 2008 Dylan Smith
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #define COBJMACROS
24
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <windef.h>
28 #include <winbase.h>
29 #include <objbase.h>
30 #include <richedit.h>
31 #include <initguid.h>
32 #include <textserv.h>
33 #include <wine/test.h>
34 #include <oleauto.h>
35 #include <limits.h>
36
37 static HMODULE hmoduleRichEdit;
38
39 /* Define C Macros for ITextServices calls. */
40
41 /* Use a special table for x86 machines to convert the thiscall
42  * calling convention.  This isn't needed on other platforms. */
43 #ifdef __i386__
44 static ITextServicesVtbl itextServicesStdcallVtbl;
45 #define TXTSERV_VTABLE(This) (&itextServicesStdcallVtbl)
46 #else /* __i386__ */
47 #define TXTSERV_VTABLE(This) (This)->lpVtbl
48 #endif /* __i386__ */
49
50 #define ITextServices_TxSendMessage(This,a,b,c,d) TXTSERV_VTABLE(This)->TxSendMessage(This,a,b,c,d)
51 #define ITextServices_TxDraw(This,a,b,c,d,e,f,g,h,i,j,k,l) TXTSERV_VTABLE(This)->TxDraw(This,a,b,c,d,e,f,g,h,i,j,k,l)
52 #define ITextServices_TxGetHScroll(This,a,b,c,d,e) TXTSERV_VTABLE(This)->TxGetHScroll(This,a,b,c,d,e)
53 #define ITextServices_TxGetVScroll(This,a,b,c,d,e) TXTSERV_VTABLE(This)->TxGetVScroll(This,a,b,c,d,e)
54 #define ITextServices_OnTxSetCursor(This,a,b,c,d,e,f,g,h,i) TXTSERV_VTABLE(This)->OnTxSetCursor(This,a,b,c,d,e,f,g,h,i)
55 #define ITextServices_TxQueryHitPoint(This,a,b,c,d,e,f,g,h,i,j) TXTSERV_VTABLE(This)->TxQueryHitPoint(This,a,b,c,d,e,f,g,h,i,j)
56 #define ITextServices_OnTxInplaceActivate(This,a) TXTSERV_VTABLE(This)->OnTxInplaceActivate(This,a)
57 #define ITextServices_OnTxInplaceDeactivate(This) TXTSERV_VTABLE(This)->OnTxInplaceDeactivate(This)
58 #define ITextServices_OnTxUIActivate(This) TXTSERV_VTABLE(This)->OnTxUIActivate(This)
59 #define ITextServices_OnTxUIDeactivate(This) TXTSERV_VTABLE(This)->OnTxUIDeactivate(This)
60 #define ITextServices_TxGetText(This,a) TXTSERV_VTABLE(This)->TxGetText(This,a)
61 #define ITextServices_TxSetText(This,a) TXTSERV_VTABLE(This)->TxSetText(This,a)
62 #define ITextServices_TxGetCurrentTargetX(This,a) TXTSERV_VTABLE(This)->TxGetCurrentTargetX(This,a)
63 #define ITextServices_TxGetBaseLinePos(This,a) TXTSERV_VTABLE(This)->TxGetBaseLinePos(This,a)
64 #define ITextServices_TxGetNaturalSize(This,a,b,c,d,e,f,g,h) TXTSERV_VTABLE(This)->TxGetNaturalSize(This,a,b,c,d,e,f,g,h)
65 #define ITextServices_TxGetDropTarget(This,a) TXTSERV_VTABLE(This)->TxGetDropTarget(This,a)
66 #define ITextServices_OnTxPropertyBitsChange(This,a,b) TXTSERV_VTABLE(This)->OnTxPropertyBitsChange(This,a,b)
67 #define ITextServices_TxGetCachedSize(This,a,b) TXTSERV_VTABLE(This)->TxGetCachedSize(This,a,b)
68
69 /* Set the WINETEST_DEBUG environment variable to be greater than 1 for verbose
70  * function call traces of ITextHost. */
71 #define TRACECALL if(winetest_debug > 1) trace
72
73 /************************************************************************/
74 /* ITextHost implementation for conformance testing. */
75
76 typedef struct ITextHostTestImpl
77 {
78     ITextHost ITextHost_iface;
79     LONG refCount;
80 } ITextHostTestImpl;
81
82 static inline ITextHostTestImpl *impl_from_ITextHost(ITextHost *iface)
83 {
84     return CONTAINING_RECORD(iface, ITextHostTestImpl, ITextHost_iface);
85 }
86
87 static HRESULT WINAPI ITextHostImpl_QueryInterface(ITextHost *iface,
88                                                    REFIID riid,
89                                                    LPVOID *ppvObject)
90 {
91     ITextHostTestImpl *This = impl_from_ITextHost(iface);
92
93     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_ITextHost)) {
94         *ppvObject = This;
95         ITextHost_AddRef((ITextHost *)*ppvObject);
96         return S_OK;
97     }
98
99     return E_NOINTERFACE;
100 }
101
102 static ULONG WINAPI ITextHostImpl_AddRef(ITextHost *iface)
103 {
104     ITextHostTestImpl *This = impl_from_ITextHost(iface);
105     ULONG refCount = InterlockedIncrement(&This->refCount);
106     return refCount;
107 }
108
109 static ULONG WINAPI ITextHostImpl_Release(ITextHost *iface)
110 {
111     ITextHostTestImpl *This = impl_from_ITextHost(iface);
112     ULONG refCount = InterlockedDecrement(&This->refCount);
113
114     if (!refCount)
115     {
116         CoTaskMemFree(This);
117         return 0;
118     } else {
119         return refCount;
120     }
121 }
122
123 static HDC WINAPI ITextHostImpl_TxGetDC(ITextHost *iface)
124 {
125     ITextHostTestImpl *This = impl_from_ITextHost(iface);
126     TRACECALL("Call to TxGetDC(%p)\n", This);
127     return NULL;
128 }
129
130 static INT WINAPI ITextHostImpl_TxReleaseDC(ITextHost *iface,
131                                             HDC hdc)
132 {
133     ITextHostTestImpl *This = impl_from_ITextHost(iface);
134     TRACECALL("Call to TxReleaseDC(%p)\n", This);
135     return 0;
136 }
137
138 static BOOL WINAPI ITextHostImpl_TxShowScrollBar(ITextHost *iface,
139                                                  INT fnBar,
140                                                  BOOL fShow)
141 {
142     ITextHostTestImpl *This = impl_from_ITextHost(iface);
143     TRACECALL("Call to TxShowScrollBar(%p, fnBar=%d, fShow=%d)\n",
144                 This, fnBar, fShow);
145     return FALSE;
146 }
147
148 static BOOL WINAPI ITextHostImpl_TxEnableScrollBar(ITextHost *iface,
149                                                    INT fuSBFlags,
150                                                    INT fuArrowflags)
151 {
152     ITextHostTestImpl *This = impl_from_ITextHost(iface);
153     TRACECALL("Call to TxEnableScrollBar(%p, fuSBFlags=%d, fuArrowflags=%d)\n",
154                This, fuSBFlags, fuArrowflags);
155     return FALSE;
156 }
157
158 static BOOL WINAPI ITextHostImpl_TxSetScrollRange(ITextHost *iface,
159                                                   INT fnBar,
160                                                   LONG nMinPos,
161                                                   INT nMaxPos,
162                                                   BOOL fRedraw)
163 {
164     ITextHostTestImpl *This = impl_from_ITextHost(iface);
165     TRACECALL("Call to TxSetScrollRange(%p, fnBar=%d, nMinPos=%d, nMaxPos=%d, fRedraw=%d)\n",
166                This, fnBar, nMinPos, nMaxPos, fRedraw);
167     return FALSE;
168 }
169
170 static BOOL WINAPI ITextHostImpl_TxSetScrollPos(ITextHost *iface,
171                                                 INT fnBar,
172                                                 INT nPos,
173                                                 BOOL fRedraw)
174 {
175     ITextHostTestImpl *This = impl_from_ITextHost(iface);
176     TRACECALL("Call to TxSetScrollPos(%p, fnBar=%d, nPos=%d, fRedraw=%d)\n",
177                This, fnBar, nPos, fRedraw);
178     return FALSE;
179 }
180
181 static void WINAPI ITextHostImpl_TxInvalidateRect(ITextHost *iface,
182                                                   LPCRECT prc,
183                                                   BOOL fMode)
184 {
185     ITextHostTestImpl *This = impl_from_ITextHost(iface);
186     TRACECALL("Call to TxInvalidateRect(%p, prc=%p, fMode=%d)\n",
187                This, prc, fMode);
188 }
189
190 static void WINAPI ITextHostImpl_TxViewChange(ITextHost *iface, BOOL fUpdate)
191 {
192     ITextHostTestImpl *This = impl_from_ITextHost(iface);
193     TRACECALL("Call to TxViewChange(%p, fUpdate=%d)\n",
194                This, fUpdate);
195 }
196
197 static BOOL WINAPI ITextHostImpl_TxCreateCaret(ITextHost *iface,
198                                                HBITMAP hbmp,
199                                                INT xWidth, INT yHeight)
200 {
201     ITextHostTestImpl *This = impl_from_ITextHost(iface);
202     TRACECALL("Call to TxCreateCaret(%p, nbmp=%p, xWidth=%d, yHeight=%d)\n",
203                This, hbmp, xWidth, yHeight);
204     return FALSE;
205 }
206
207 static BOOL WINAPI ITextHostImpl_TxShowCaret(ITextHost *iface, BOOL fShow)
208 {
209     ITextHostTestImpl *This = impl_from_ITextHost(iface);
210     TRACECALL("Call to TxShowCaret(%p, fShow=%d)\n",
211                This, fShow);
212     return FALSE;
213 }
214
215 static BOOL WINAPI ITextHostImpl_TxSetCaretPos(ITextHost *iface,
216                                                INT x, INT y)
217 {
218     ITextHostTestImpl *This = impl_from_ITextHost(iface);
219     TRACECALL("Call to TxSetCaretPos(%p, x=%d, y=%d)\n", This, x, y);
220     return FALSE;
221 }
222
223 static BOOL WINAPI ITextHostImpl_TxSetTimer(ITextHost *iface,
224                                             UINT idTimer, UINT uTimeout)
225 {
226     ITextHostTestImpl *This = impl_from_ITextHost(iface);
227     TRACECALL("Call to TxSetTimer(%p, idTimer=%u, uTimeout=%u)\n",
228               This, idTimer, uTimeout);
229     return FALSE;
230 }
231
232 static void WINAPI ITextHostImpl_TxKillTimer(ITextHost *iface, UINT idTimer)
233 {
234     ITextHostTestImpl *This = impl_from_ITextHost(iface);
235     TRACECALL("Call to TxKillTimer(%p, idTimer=%u)\n", This, idTimer);
236 }
237
238 static void WINAPI ITextHostImpl_TxScrollWindowEx(ITextHost *iface,
239                                                   INT dx, INT dy,
240                                                   LPCRECT lprcScroll,
241                                                   LPCRECT lprcClip,
242                                                   HRGN hRgnUpdate,
243                                                   LPRECT lprcUpdate,
244                                                   UINT fuScroll)
245 {
246     ITextHostTestImpl *This = impl_from_ITextHost(iface);
247     TRACECALL("Call to TxScrollWindowEx(%p, %d, %d, %p, %p, %p, %p, %d)\n",
248               This, dx, dy, lprcScroll, lprcClip, hRgnUpdate, lprcUpdate, fuScroll);
249 }
250
251 static void WINAPI ITextHostImpl_TxSetCapture(ITextHost *iface, BOOL fCapture)
252 {
253     ITextHostTestImpl *This = impl_from_ITextHost(iface);
254     TRACECALL("Call to TxSetCapture(%p, fCapture=%d)\n", This, fCapture);
255 }
256
257 static void WINAPI ITextHostImpl_TxSetFocus(ITextHost *iface)
258 {
259     ITextHostTestImpl *This = impl_from_ITextHost(iface);
260     TRACECALL("Call to TxSetFocus(%p)\n", This);
261 }
262
263 static void WINAPI ITextHostImpl_TxSetCursor(ITextHost *iface,
264                                              HCURSOR hcur,
265                                              BOOL fText)
266 {
267     ITextHostTestImpl *This = impl_from_ITextHost(iface);
268     TRACECALL("Call to TxSetCursor(%p, hcur=%p, fText=%d)\n",
269               This, hcur, fText);
270 }
271
272 static BOOL WINAPI ITextHostImpl_TxScreenToClient(ITextHost *iface,
273                                                   LPPOINT lppt)
274 {
275     ITextHostTestImpl *This = impl_from_ITextHost(iface);
276     TRACECALL("Call to TxScreenToClient(%p, lppt=%p)\n", This, lppt);
277     return FALSE;
278 }
279
280 static BOOL WINAPI ITextHostImpl_TxClientToScreen(ITextHost *iface,
281                                                   LPPOINT lppt)
282 {
283     ITextHostTestImpl *This = impl_from_ITextHost(iface);
284     TRACECALL("Call to TxClientToScreen(%p, lppt=%p)\n", This, lppt);
285     return FALSE;
286 }
287
288 static HRESULT WINAPI ITextHostImpl_TxActivate(ITextHost *iface,
289                                                LONG *plOldState)
290 {
291     ITextHostTestImpl *This = impl_from_ITextHost(iface);
292     TRACECALL("Call to TxActivate(%p, plOldState=%p)\n", This, plOldState);
293     return E_NOTIMPL;
294 }
295
296 static HRESULT WINAPI ITextHostImpl_TxDeactivate(ITextHost *iface,
297                                                  LONG lNewState)
298 {
299     ITextHostTestImpl *This = impl_from_ITextHost(iface);
300     TRACECALL("Call to TxDeactivate(%p, lNewState=%d)\n", This, lNewState);
301     return E_NOTIMPL;
302 }
303
304 static HRESULT WINAPI ITextHostImpl_TxGetClientRect(ITextHost *iface,
305                                                     LPRECT prc)
306 {
307     ITextHostTestImpl *This = impl_from_ITextHost(iface);
308     TRACECALL("Call to TxGetClientRect(%p, prc=%p)\n", This, prc);
309     return E_NOTIMPL;
310 }
311
312 static HRESULT WINAPI ITextHostImpl_TxGetViewInset(ITextHost *iface,
313                                                    LPRECT prc)
314 {
315     ITextHostTestImpl *This = impl_from_ITextHost(iface);
316     TRACECALL("Call to TxGetViewInset(%p, prc=%p)\n", This, prc);
317     return E_NOTIMPL;
318 }
319
320 static HRESULT WINAPI ITextHostImpl_TxGetCharFormat(ITextHost *iface,
321                                                     const CHARFORMATW **ppCF)
322 {
323     ITextHostTestImpl *This = impl_from_ITextHost(iface);
324     TRACECALL("Call to TxGetCharFormat(%p, ppCF=%p)\n", This, ppCF);
325     return E_NOTIMPL;
326 }
327
328 static HRESULT WINAPI ITextHostImpl_TxGetParaFormat(ITextHost *iface,
329                                                     const PARAFORMAT **ppPF)
330 {
331     ITextHostTestImpl *This = impl_from_ITextHost(iface);
332     TRACECALL("Call to TxGetParaFormat(%p, ppPF=%p)\n", This, ppPF);
333     return E_NOTIMPL;
334 }
335
336 static COLORREF WINAPI ITextHostImpl_TxGetSysColor(ITextHost *iface,
337                                                    int nIndex)
338 {
339     ITextHostTestImpl *This = impl_from_ITextHost(iface);
340     TRACECALL("Call to TxGetSysColor(%p, nIndex=%d)\n", This, nIndex);
341     return E_NOTIMPL;
342 }
343
344 static HRESULT WINAPI ITextHostImpl_TxGetBackStyle(ITextHost *iface,
345                                                    TXTBACKSTYLE *pStyle)
346 {
347     ITextHostTestImpl *This = impl_from_ITextHost(iface);
348     TRACECALL("Call to TxGetBackStyle(%p, pStyle=%p)\n", This, pStyle);
349     return E_NOTIMPL;
350 }
351
352 static HRESULT WINAPI ITextHostImpl_TxGetMaxLength(ITextHost *iface,
353                                                    DWORD *pLength)
354 {
355     ITextHostTestImpl *This = impl_from_ITextHost(iface);
356     TRACECALL("Call to TxGetMaxLength(%p, pLength=%p)\n", This, pLength);
357     return E_NOTIMPL;
358 }
359
360 static HRESULT WINAPI ITextHostImpl_TxGetScrollBars(ITextHost *iface,
361                                                     DWORD *pdwScrollBar)
362 {
363     ITextHostTestImpl *This = impl_from_ITextHost(iface);
364     TRACECALL("Call to TxGetScrollBars(%p, pdwScrollBar=%p)\n",
365                This, pdwScrollBar);
366     return E_NOTIMPL;
367 }
368
369 static HRESULT WINAPI ITextHostImpl_TxGetPasswordChar(ITextHost *iface,
370                                                       WCHAR *pch)
371 {
372     ITextHostTestImpl *This = impl_from_ITextHost(iface);
373     TRACECALL("Call to TxGetPasswordChar(%p, pch=%p)\n", This, pch);
374     return E_NOTIMPL;
375 }
376
377 static HRESULT WINAPI ITextHostImpl_TxGetAcceleratorPos(ITextHost *iface,
378                                                         LONG *pch)
379 {
380     ITextHostTestImpl *This = impl_from_ITextHost(iface);
381     TRACECALL("Call to TxGetAcceleratorPos(%p, pch=%p)\n", This, pch);
382     return E_NOTIMPL;
383 }
384
385 static HRESULT WINAPI ITextHostImpl_TxGetExtent(ITextHost *iface,
386                                                 LPSIZEL lpExtent)
387 {
388     ITextHostTestImpl *This = impl_from_ITextHost(iface);
389     TRACECALL("Call to TxGetExtent(%p, lpExtent=%p)\n", This, lpExtent);
390     return E_NOTIMPL;
391 }
392
393 static HRESULT WINAPI ITextHostImpl_OnTxCharFormatChange(ITextHost *iface,
394                                                          const CHARFORMATW *pcf)
395 {
396     ITextHostTestImpl *This = impl_from_ITextHost(iface);
397     TRACECALL("Call to OnTxCharFormatChange(%p, pcf=%p)\n", This, pcf);
398     return E_NOTIMPL;
399 }
400
401 static HRESULT WINAPI ITextHostImpl_OnTxParaFormatChange(ITextHost *iface,
402                                                          const PARAFORMAT *ppf)
403 {
404     ITextHostTestImpl *This = impl_from_ITextHost(iface);
405     TRACECALL("Call to OnTxParaFormatChange(%p, ppf=%p)\n", This, ppf);
406     return E_NOTIMPL;
407 }
408
409 /* This must return S_OK for the native ITextServices object to
410    initialize. */
411 static HRESULT WINAPI ITextHostImpl_TxGetPropertyBits(ITextHost *iface,
412                                                       DWORD dwMask,
413                                                       DWORD *pdwBits)
414 {
415     ITextHostTestImpl *This = impl_from_ITextHost(iface);
416     TRACECALL("Call to TxGetPropertyBits(%p, dwMask=0x%08x, pdwBits=%p)\n",
417               This, dwMask, pdwBits);
418     *pdwBits = 0;
419     return S_OK;
420 }
421
422 static HRESULT WINAPI ITextHostImpl_TxNotify(ITextHost *iface, DWORD iNotify,
423                                              void *pv)
424 {
425     ITextHostTestImpl *This = impl_from_ITextHost(iface);
426     TRACECALL("Call to TxNotify(%p, iNotify=%d, pv=%p)\n", This, iNotify, pv);
427     return E_NOTIMPL;
428 }
429
430 static HIMC WINAPI ITextHostImpl_TxImmGetContext(ITextHost *iface)
431 {
432     ITextHostTestImpl *This = impl_from_ITextHost(iface);
433     TRACECALL("Call to TxImmGetContext(%p)\n", This);
434     return 0;
435 }
436
437 static void WINAPI ITextHostImpl_TxImmReleaseContext(ITextHost *iface, HIMC himc)
438 {
439     ITextHostTestImpl *This = impl_from_ITextHost(iface);
440     TRACECALL("Call to TxImmReleaseContext(%p, himc=%p)\n", This, himc);
441 }
442
443 /* This function must set the variable pointed to by *lSelBarWidth.
444    Otherwise an uninitalized value will be used to calculate
445    positions and sizes even if E_NOTIMPL is returned. */
446 static HRESULT WINAPI ITextHostImpl_TxGetSelectionBarWidth(ITextHost *iface,
447                                                            LONG *lSelBarWidth)
448 {
449     ITextHostTestImpl *This = impl_from_ITextHost(iface);
450     TRACECALL("Call to TxGetSelectionBarWidth(%p, lSelBarWidth=%p)\n",
451                 This, lSelBarWidth);
452     *lSelBarWidth = 0;
453     return E_NOTIMPL;
454 }
455
456 static ITextHostVtbl itextHostVtbl = {
457     ITextHostImpl_QueryInterface,
458     ITextHostImpl_AddRef,
459     ITextHostImpl_Release,
460     ITextHostImpl_TxGetDC,
461     ITextHostImpl_TxReleaseDC,
462     ITextHostImpl_TxShowScrollBar,
463     ITextHostImpl_TxEnableScrollBar,
464     ITextHostImpl_TxSetScrollRange,
465     ITextHostImpl_TxSetScrollPos,
466     ITextHostImpl_TxInvalidateRect,
467     ITextHostImpl_TxViewChange,
468     ITextHostImpl_TxCreateCaret,
469     ITextHostImpl_TxShowCaret,
470     ITextHostImpl_TxSetCaretPos,
471     ITextHostImpl_TxSetTimer,
472     ITextHostImpl_TxKillTimer,
473     ITextHostImpl_TxScrollWindowEx,
474     ITextHostImpl_TxSetCapture,
475     ITextHostImpl_TxSetFocus,
476     ITextHostImpl_TxSetCursor,
477     ITextHostImpl_TxScreenToClient,
478     ITextHostImpl_TxClientToScreen,
479     ITextHostImpl_TxActivate,
480     ITextHostImpl_TxDeactivate,
481     ITextHostImpl_TxGetClientRect,
482     ITextHostImpl_TxGetViewInset,
483     ITextHostImpl_TxGetCharFormat,
484     ITextHostImpl_TxGetParaFormat,
485     ITextHostImpl_TxGetSysColor,
486     ITextHostImpl_TxGetBackStyle,
487     ITextHostImpl_TxGetMaxLength,
488     ITextHostImpl_TxGetScrollBars,
489     ITextHostImpl_TxGetPasswordChar,
490     ITextHostImpl_TxGetAcceleratorPos,
491     ITextHostImpl_TxGetExtent,
492     ITextHostImpl_OnTxCharFormatChange,
493     ITextHostImpl_OnTxParaFormatChange,
494     ITextHostImpl_TxGetPropertyBits,
495     ITextHostImpl_TxNotify,
496     ITextHostImpl_TxImmGetContext,
497     ITextHostImpl_TxImmReleaseContext,
498     ITextHostImpl_TxGetSelectionBarWidth
499 };
500
501 static ITextServices *txtserv = NULL;
502 static ITextHostTestImpl *dummyTextHost;
503 static void *wrapperCodeMem = NULL;
504
505 #include "pshpack1.h"
506
507 /* Code structure for x86 byte code */
508 typedef struct
509 {
510     BYTE pop_eax;  /* popl  %eax  */
511     BYTE push_ecx; /* pushl %ecx  */
512     BYTE push_eax; /* pushl %eax  */
513     BYTE jmp_func; /* jmp   $func */
514     DWORD func;
515 } THISCALL_TO_STDCALL_THUNK;
516
517 typedef struct
518 {
519     BYTE pop_eax;               /* popl  %eax */
520     BYTE pop_ecx;               /* popl  %ecx */
521     BYTE push_eax;              /* pushl %eax */
522     BYTE mov_vtable_eax[2];     /* movl (%ecx), %eax */
523     BYTE jmp_eax[2];            /* jmp *$vtablefunc_offset(%eax) */
524     int  vtablefunc_offset;
525 } STDCALL_TO_THISCALL_THUNK;
526
527 #include "poppack.h"
528
529 static void setup_thiscall_wrappers(void)
530 {
531 #ifdef __i386__
532     void** pVtable;
533     void** pVtableEnd;
534     THISCALL_TO_STDCALL_THUNK *thunk;
535     STDCALL_TO_THISCALL_THUNK *thunk2;
536
537     wrapperCodeMem = VirtualAlloc(NULL,
538                                   (sizeof(ITextHostVtbl)/sizeof(void*) - 3)
539                                     * sizeof(THISCALL_TO_STDCALL_THUNK)
540                                   +(sizeof(ITextServicesVtbl)/sizeof(void*) - 3)
541                                     * sizeof(STDCALL_TO_THISCALL_THUNK),
542                                   MEM_COMMIT, PAGE_EXECUTE_READWRITE);
543     thunk = wrapperCodeMem;
544
545     /* Wrap all ITextHostImpl methods with code to perform a thiscall to
546      * stdcall conversion. The thiscall calling convention places the This
547      * pointer in ecx on the x86 platform, and the stdcall calling convention
548      * pushes the This pointer on the stack as the first argument.
549      *
550      * The byte code does the conversion then jumps to the real function.
551      *
552      * Each wrapper needs to be modified so that the function to jump to is
553      * modified in the byte code. */
554
555     /* Skip QueryInterface, AddRef, and Release native actually
556      * defined them with the stdcall calling convention. */
557     pVtable = (void**)&itextHostVtbl + 3;
558     pVtableEnd = (void**)(&itextHostVtbl + 1);
559     while (pVtable != pVtableEnd) {
560         /* write byte code to executable memory */
561         thunk->pop_eax = 0x58;  /* popl  %eax  */
562         thunk->push_ecx = 0x51; /* pushl %ecx  */
563         thunk->push_eax = 0x50; /* pushl %eax  */
564         thunk->jmp_func = 0xe9; /* jmp   $func */
565         /* The address needs to be relative to the end of the jump instructions. */
566         thunk->func = (char*)*pVtable - (char*)(&thunk->func + 1);
567         *pVtable = thunk;
568         pVtable++;
569         thunk++;
570     }
571
572     /* Setup an ITextServices standard call vtable that will call the
573      * native thiscall vtable when the methods are called. */
574
575     /* QueryInterface, AddRef, and Release should be called directly on the
576      * real vtable since they use the stdcall calling convention. */
577     thunk2 = (STDCALL_TO_THISCALL_THUNK *)thunk;
578     pVtable = (void**)&itextServicesStdcallVtbl + 3;
579     pVtableEnd = (void**)(&itextServicesStdcallVtbl + 1);
580     while (pVtable != pVtableEnd) {
581         /* write byte code to executable memory */
582         thunk2->pop_eax = 0x58;               /* popl  %eax */
583         thunk2->pop_ecx = 0x59;               /* popl  %ecx */
584         thunk2->push_eax = 0x50;              /* pushl %eax */
585         thunk2->mov_vtable_eax[0] = 0x8b;     /* movl (%ecx), %eax */
586         thunk2->mov_vtable_eax[1] = 0x01;
587         thunk2->jmp_eax[0] = 0xff;            /* jmp *$vtablefunc_offset(%eax) */
588         thunk2->jmp_eax[1] = 0xa0;
589         thunk2->vtablefunc_offset = (char*)pVtable - (char*)&itextServicesStdcallVtbl;
590         *pVtable = thunk2;
591         pVtable++;
592         thunk2++;
593     }
594 #endif /* __i386__ */
595 }
596
597 /*************************************************************************/
598 /* Conformance test functions. */
599
600 /* Initialize the test texthost structure */
601 static BOOL init_texthost(void)
602 {
603     IUnknown *init;
604     HRESULT result;
605     PCreateTextServices pCreateTextServices;
606
607     dummyTextHost = CoTaskMemAlloc(sizeof(*dummyTextHost));
608     if (dummyTextHost == NULL) {
609         skip("Insufficient memory to create ITextHost interface\n");
610         return FALSE;
611     }
612     dummyTextHost->ITextHost_iface.lpVtbl = &itextHostVtbl;
613     dummyTextHost->refCount = 1;
614
615     /* MSDN states that an IUnknown object is returned by
616        CreateTextServices which is then queried to obtain a
617        ITextServices object. */
618     pCreateTextServices = (void*)GetProcAddress(hmoduleRichEdit, "CreateTextServices");
619     result = (*pCreateTextServices)(NULL, &dummyTextHost->ITextHost_iface, &init);
620     ok(result == S_OK, "Did not return S_OK when created (result =  %x)\n", result);
621     if (result != S_OK) {
622         CoTaskMemFree(dummyTextHost);
623         skip("CreateTextServices failed.\n");
624         return FALSE;
625     }
626
627     result = IUnknown_QueryInterface(init, &IID_ITextServices,
628                                      (void **)&txtserv);
629     ok((result == S_OK) && (txtserv != NULL), "Querying interface failed (result = %x, txtserv = %p)\n", result, txtserv);
630     IUnknown_Release(init);
631     if (!((result == S_OK) && (txtserv != NULL))) {
632         CoTaskMemFree(dummyTextHost);
633         skip("Could not retrieve ITextServices interface\n");
634         return FALSE;
635     }
636
637     return TRUE;
638 }
639
640 static void free_texthost(void)
641 {
642     IUnknown_Release(txtserv);
643     CoTaskMemFree(dummyTextHost);
644 }
645
646 static void test_TxGetText(void)
647 {
648     HRESULT hres;
649     BSTR rettext;
650
651     if (!init_texthost())
652         return;
653
654     hres = ITextServices_TxGetText(txtserv, &rettext);
655     ok(hres == S_OK, "ITextServices_TxGetText failed (result = %x)\n", hres);
656
657     free_texthost();
658 }
659
660 static void test_TxSetText(void)
661 {
662     HRESULT hres;
663     BSTR rettext;
664     WCHAR settext[] = {'T','e','s','t',0};
665
666     if (!init_texthost())
667         return;
668
669     hres = ITextServices_TxSetText(txtserv, settext);
670     ok(hres == S_OK, "ITextServices_TxSetText failed (result = %x)\n", hres);
671
672     hres = ITextServices_TxGetText(txtserv, &rettext);
673     ok(hres == S_OK, "ITextServices_TxGetText failed (result = %x)\n", hres);
674
675     ok(SysStringLen(rettext) == 4,
676                  "String returned of wrong length (expected 4, got %d)\n", SysStringLen(rettext));
677     ok(memcmp(rettext,settext,SysStringByteLen(rettext)) == 0,
678                  "String returned differs\n");
679
680     SysFreeString(rettext);
681     free_texthost();
682 }
683
684 static void test_TxGetNaturalSize(void) {
685     HRESULT result;
686     BOOL ret;
687
688     /* This value is used when calling TxGetNaturalSize.  MSDN says
689        that this is not supported however a null pointer cannot be
690        used as it will cause a segmentation violation.  The values in
691        the structure being pointed to are required to be INT_MAX
692        otherwise calculations can give wrong values. */
693     const SIZEL psizelExtent = {INT_MAX,INT_MAX};
694
695     static const WCHAR oneA[] = {'A',0};
696
697     /* Results of measurements */
698     LONG xdim, ydim;
699
700     /* The device context to do the tests in */
701     HDC hdcDraw;
702
703     /* Variables with the text metric information */
704     INT charwidth_caps_text[26];
705     TEXTMETRIC tmInfo_text;
706
707     if (!init_texthost())
708         return;
709
710     hdcDraw = GetDC(NULL);
711     SaveDC(hdcDraw);
712
713     /* Populate the metric strucs */
714     SetMapMode(hdcDraw,MM_TEXT);
715     GetTextMetrics(hdcDraw, &tmInfo_text);
716     SetLastError(0xdeadbeef);
717     ret = GetCharWidth32(hdcDraw,'A','Z',charwidth_caps_text);
718     if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
719         win_skip("GetCharWidth32 is not available\n");
720         goto cleanup;
721     }
722
723     /* Make measurements in MM_TEXT */
724     SetMapMode(hdcDraw,MM_TEXT);
725     xdim = 0; ydim = 0;
726
727     result = ITextServices_TxSetText(txtserv, oneA);
728     ok(result == S_OK, "ITextServices_TxSetText failed (result = %x)\n", result);
729     if (result != S_OK) {
730         skip("Could not set text\n");
731         goto cleanup;
732     }
733
734     SetLastError(0xdeadbeef);
735     result = ITextServices_TxGetNaturalSize(txtserv, DVASPECT_CONTENT,
736                                             hdcDraw, NULL, NULL,
737                                             TXTNS_FITTOCONTENT, &psizelExtent,
738                                             &xdim, &ydim);
739     todo_wine ok(result == S_OK || broken(result == E_FAIL), /* WINXP Arabic Language */
740         "TxGetNaturalSize gave unexpected return value (result = %x)\n", result);
741     if (result == S_OK) {
742     todo_wine ok(ydim == tmInfo_text.tmHeight,
743                  "Height calculated incorrectly (expected %d, got %d)\n",
744                  tmInfo_text.tmHeight, ydim);
745     /* The native DLL adds one pixel extra when calculating widths. */
746     todo_wine ok(xdim >= charwidth_caps_text[0] && xdim <= charwidth_caps_text[0] + 1,
747                  "Width calculated incorrectly (expected %d {+1}, got %d)\n",
748                  charwidth_caps_text[0], xdim);
749     } else
750         skip("TxGetNaturalSize measurements not performed (xdim = %d, ydim = %d, result = %x, error = %x)\n",
751              xdim, ydim, result, GetLastError());
752
753 cleanup:
754     RestoreDC(hdcDraw,1);
755     ReleaseDC(NULL,hdcDraw);
756     free_texthost();
757 }
758
759 static void test_TxDraw(void)
760 {
761     HDC tmphdc = GetDC(NULL);
762     DWORD dwAspect = DVASPECT_CONTENT;
763     HDC hicTargetDev = NULL; /* Means "default" device */
764     DVTARGETDEVICE *ptd = NULL;
765     void *pvAspect = NULL;
766     HRESULT result;
767     RECTL client = {0,0,100,100};
768
769     if (!init_texthost())
770         return;
771
772     todo_wine {
773         result = ITextServices_TxDraw(txtserv, dwAspect, 0, pvAspect, ptd,
774                                       tmphdc, hicTargetDev, &client, NULL,
775                                       NULL, NULL, 0, 0);
776         ok(result == S_OK, "TxDraw failed (result = %x)\n", result);
777     }
778
779     free_texthost();
780
781 }
782
783 START_TEST( txtsrv )
784 {
785     setup_thiscall_wrappers();
786
787     /* Must explicitly LoadLibrary(). The test has no references to functions in
788      * RICHED20.DLL, so the linker doesn't actually link to it. */
789     hmoduleRichEdit = LoadLibrary("RICHED20.DLL");
790     ok(hmoduleRichEdit != NULL, "error: %d\n", (int) GetLastError());
791
792     if (init_texthost())
793     {
794         free_texthost();
795
796         test_TxGetText();
797         test_TxSetText();
798         test_TxGetNaturalSize();
799         test_TxDraw();
800     }
801     if (wrapperCodeMem) VirtualFree(wrapperCodeMem, 0, MEM_RELEASE);
802 }