wined3d: Shaders will never have a NULL function.
[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
35 static HMODULE hmoduleRichEdit;
36
37 /* Define C Macros for ITextServices calls. */
38
39 /* Use a special table for x86 machines to convert the thiscall
40  * calling convention.  This isn't needed on other platforms. */
41 #ifdef __i386__
42 #define TXTSERV_VTABLE(This) (&itextServicesStdcallVtbl)
43 #else /* __i386__ */
44 #define TXTSERV_VTABLE(This) (This)->lpVtbl
45 #endif /* __i386__ */
46
47 #define ITextServices_TxSendMessage(This,a,b,c,d) TXTSERV_VTABLE(This)->TxSendMessage(This,a,b,c,d)
48 #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)
49 #define ITextServices_TxGetHScroll(This,a,b,c,d,e) TXTSERV_VTABLE(This)->TxGetHScroll(This,a,b,c,d,e)
50 #define ITextServices_TxGetVScroll(This,a,b,c,d,e) TXTSERV_VTABLE(This)->TxGetVScroll(This,a,b,c,d,e)
51 #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)
52 #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)
53 #define ITextServices_OnTxInplaceActivate(This,a) TXTSERV_VTABLE(This)->OnTxInplaceActivate(This,a)
54 #define ITextServices_OnTxInplaceDeactivate(This) TXTSERV_VTABLE(This)->OnTxInplaceDeactivate(This)
55 #define ITextServices_OnTxUIActivate(This) TXTSERV_VTABLE(This)->OnTxUIActivate(This)
56 #define ITextServices_OnTxUIDeactivate(This) TXTSERV_VTABLE(This)->OnTxUIDeactivate(This)
57 #define ITextServices_TxGetText(This,a) TXTSERV_VTABLE(This)->TxGetText(This,a)
58 #define ITextServices_TxSetText(This,a) TXTSERV_VTABLE(This)->TxSetText(This,a)
59 #define ITextServices_TxGetCurrentTargetX(This,a) TXTSERV_VTABLE(This)->TxGetCurrentTargetX(This,a)
60 #define ITextServices_TxGetBaseLinePos(This,a) TXTSERV_VTABLE(This)->TxGetBaseLinePos(This,a)
61 #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)
62 #define ITextServices_TxGetDropTarget(This,a) TXTSERV_VTABLE(This)->TxGetDropTarget(This,a)
63 #define ITextServices_OnTxPropertyBitsChange(This,a,b) TXTSERV_VTABLE(This)->OnTxPropertyBitsChange(This,a,b)
64 #define ITextServices_TxGetCachedSize(This,a,b) TXTSERV_VTABLE(This)->TxGetCachedSize(This,a,b)
65
66 /* Set the WINETEST_DEBUG environment variable to be greater than 1 for verbose
67  * function call traces of ITextHost. */
68 #define TRACECALL if(winetest_debug > 1) trace
69
70 /************************************************************************/
71 /* ITextHost implementation for conformance testing. */
72
73 typedef struct ITextHostTestImpl
74 {
75     ITextHostVtbl *lpVtbl;
76     LONG refCount;
77 } ITextHostTestImpl;
78
79 static HRESULT WINAPI ITextHostImpl_QueryInterface(ITextHost *iface,
80                                                    REFIID riid,
81                                                    LPVOID *ppvObject)
82 {
83     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
84
85     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_ITextHost)) {
86         *ppvObject = This;
87         ITextHost_AddRef((ITextHost *)*ppvObject);
88         return S_OK;
89     }
90
91     return E_NOINTERFACE;
92 }
93
94 static ULONG WINAPI ITextHostImpl_AddRef(ITextHost *iface)
95 {
96     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
97     ULONG refCount = InterlockedIncrement(&This->refCount);
98     return refCount;
99 }
100
101 static ULONG WINAPI ITextHostImpl_Release(ITextHost *iface)
102 {
103     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
104     ULONG refCount = InterlockedDecrement(&This->refCount);
105
106     if (!refCount)
107     {
108         CoTaskMemFree(This);
109         return 0;
110     } else {
111         return refCount;
112     }
113 }
114
115 static HDC WINAPI ITextHostImpl_TxGetDC(ITextHost *iface)
116 {
117     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
118     TRACECALL("Call to TxGetDC(%p)\n", This);
119     return NULL;
120 }
121
122 static INT WINAPI ITextHostImpl_TxReleaseDC(ITextHost *iface,
123                                      HDC hdc)
124 {
125     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
126     TRACECALL("Call to TxReleaseDC(%p)\n", This);
127     return 0;
128 }
129
130 static BOOL WINAPI ITextHostImpl_TxShowScrollBar(ITextHost *iface,
131                                           INT fnBar,
132                                           BOOL fShow)
133 {
134     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
135     TRACECALL("Call to TxShowScrollBar(%p, fnBar=%d, fShow=%d)\n",
136                 This, fnBar, fShow);
137     return E_NOTIMPL;
138 }
139
140 static BOOL WINAPI ITextHostImpl_TxEnableScrollBar(ITextHost *iface,
141                                             INT fuSBFlags,
142                                             INT fuArrowflags)
143 {
144     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
145     TRACECALL("Call to TxEnableScrollBar(%p, fuSBFlags=%d, fuArrowflags=%d)\n",
146                This, fuSBFlags, fuArrowflags);
147     return E_NOTIMPL;
148 }
149
150 static BOOL WINAPI ITextHostImpl_TxSetScrollRange(ITextHost *iface,
151                                            INT fnBar,
152                                            LONG nMinPos,
153                                            INT nMaxPos,
154                                            BOOL fRedraw)
155 {
156     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
157     TRACECALL("Call to TxSetScrollRange(%p, fnBar=%d, nMinPos=%d, nMaxPos=%d, fRedraw=%d)\n",
158                This, fnBar, nMinPos, nMaxPos, fRedraw);
159     return E_NOTIMPL;
160 }
161
162 static BOOL WINAPI ITextHostImpl_TxSetScrollPos(ITextHost *iface,
163                                          INT fnBar,
164                                          INT nPos,
165                                          BOOL fRedraw)
166 {
167     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
168     TRACECALL("Call to TxSetScrollPos(%p, fnBar=%d, nPos=%d, fRedraw=%d)\n",
169                This, fnBar, nPos, fRedraw);
170     return E_NOTIMPL;
171 }
172
173 static void WINAPI ITextHostImpl_TxInvalidateRect(ITextHost *iface,
174                                            LPCRECT prc,
175                                            BOOL fMode)
176 {
177     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
178     TRACECALL("Call to TxInvalidateRect(%p, prc=%p, fMode=%d)\n",
179                This, prc, fMode);
180 }
181
182 static void WINAPI ITextHostImpl_TxViewChange(ITextHost *iface, BOOL fUpdate)
183 {
184     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
185     TRACECALL("Call to TxViewChange(%p, fUpdate=%d)\n",
186                This, fUpdate);
187 }
188
189 static BOOL WINAPI ITextHostImpl_TxCreateCaret(ITextHost *iface,
190                                         HBITMAP hbmp,
191                                         INT xWidth, INT yHeight)
192 {
193     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
194     TRACECALL("Call to TxCreateCaret(%p, nbmp=%p, xWidth=%d, yHeight=%d)\n",
195                This, hbmp, xWidth, yHeight);
196     return E_NOTIMPL;
197 }
198
199 static BOOL WINAPI ITextHostImpl_TxShowCaret(ITextHost *iface, BOOL fShow)
200 {
201     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
202     TRACECALL("Call to TxShowCaret(%p, fShow=%d)\n",
203                This, fShow);
204     return E_NOTIMPL;
205 }
206
207 static BOOL WINAPI ITextHostImpl_TxSetCaretPos(ITextHost *iface,
208                                          INT x, INT y)
209 {
210     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
211     TRACECALL("Call to TxSetCaretPos(%p, x=%d, y=%d)\n", This, x, y);
212     return E_NOTIMPL;
213 }
214
215 static BOOL WINAPI ITextHostImpl_TxSetTimer(ITextHost *iface,
216                                      UINT idTimer, UINT uTimeout)
217 {
218     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
219     TRACECALL("Call to TxSetTimer(%p, idTimer=%u, uTimeout=%u)\n",
220               This, idTimer, uTimeout);
221     return E_NOTIMPL;
222 }
223
224 static void WINAPI ITextHostImpl_TxKillTimer(ITextHost *iface, UINT idTimer)
225 {
226     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
227     TRACECALL("Call to TxKillTimer(%p, idTimer=%u)\n", This, idTimer);
228 }
229
230 static void WINAPI ITextHostImpl_TxScrollWindowEx(ITextHost *iface,
231                                            INT dx, INT dy,
232                                            LPCRECT lprcScroll,
233                                            LPCRECT lprcClip,
234                                            HRGN hRgnUpdate,
235                                            LPRECT lprcUpdate,
236                                            UINT fuScroll)
237 {
238     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
239     TRACECALL("Call to TxScrollWindowEx(%p, %d, %d, %p, %p, %p, %p, %d)\n",
240               This, dx, dy, lprcScroll, lprcClip, hRgnUpdate, lprcUpdate, fuScroll);
241 }
242
243 static void WINAPI ITextHostImpl_TxSetCapture(ITextHost *iface, BOOL fCapture)
244 {
245     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
246     TRACECALL("Call to TxSetCapture(%p, fCapture=%d)\n", This, fCapture);
247 }
248
249 static void WINAPI ITextHostImpl_TxSetFocus(ITextHost *iface)
250 {
251     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
252     TRACECALL("Call to TxSetFocus(%p)\n", This);
253 }
254
255 static void WINAPI ITextHostImpl_TxSetCursor(ITextHost *iface,
256                                       HCURSOR hcur,
257                                       BOOL fText)
258 {
259     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
260     TRACECALL("Call to TxSetCursor(%p, hcur=%p, fText=%d)\n",
261               This, hcur, fText);
262 }
263
264 static BOOL WINAPI ITextHostImpl_TxScreenToClient(ITextHost *iface,
265                                            LPPOINT lppt)
266 {
267     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
268     TRACECALL("Call to TxScreenToClient(%p, lppt=%p)\n", This, lppt);
269     return E_NOTIMPL;
270 }
271
272 static BOOL WINAPI ITextHostImpl_TxClientToScreen(ITextHost *iface,
273                                            LPPOINT lppt)
274 {
275     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
276     TRACECALL("Call to TxClientToScreen(%p, lppt=%p)\n", This, lppt);
277     return E_NOTIMPL;
278 }
279
280 static HRESULT WINAPI ITextHostImpl_TxActivate(ITextHost *iface,
281                                         LONG *plOldState)
282 {
283     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
284     TRACECALL("Call to TxActivate(%p, plOldState=%p)\n", This, plOldState);
285     return E_NOTIMPL;
286 }
287
288 static HRESULT WINAPI ITextHostImpl_TxDeactivate(ITextHost *iface,
289                                           LONG lNewState)
290 {
291     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
292     TRACECALL("Call to TxDeactivate(%p, lNewState=%d)\n", This, lNewState);
293     return E_NOTIMPL;
294 }
295
296 static HRESULT WINAPI ITextHostImpl_TxGetClientRect(ITextHost *iface,
297                                              LPRECT prc)
298 {
299     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
300     TRACECALL("Call to TxGetClientRect(%p, prc=%p)\n", This, prc);
301     return E_NOTIMPL;
302 }
303
304 static HRESULT WINAPI ITextHostImpl_TxGetViewInset(ITextHost *iface,
305                                             LPRECT prc)
306 {
307     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
308     TRACECALL("Call to TxGetViewInset(%p, prc=%p)\n", This, prc);
309     return E_NOTIMPL;
310 }
311
312 static HRESULT WINAPI ITextHostImpl_TxGetCharFormat(ITextHost *iface,
313                                              const CHARFORMATW **ppCF)
314 {
315     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
316     TRACECALL("Call to TxGetCharFormat(%p, ppCF=%p)\n", This, ppCF);
317     return E_NOTIMPL;
318 }
319
320 static HRESULT WINAPI ITextHostImpl_TxGetParaFormat(ITextHost *iface,
321                                              const PARAFORMAT **ppPF)
322 {
323     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
324     TRACECALL("Call to TxGetParaFormat(%p, ppPF=%p)\n", This, ppPF);
325     return E_NOTIMPL;
326 }
327
328 static COLORREF WINAPI ITextHostImpl_TxGetSysColor(ITextHost *iface,
329                                             int nIndex)
330 {
331     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
332     TRACECALL("Call to TxGetSysColor(%p, nIndex=%d)\n", This, nIndex);
333     return E_NOTIMPL;
334 }
335
336 static HRESULT WINAPI ITextHostImpl_TxGetBackStyle(ITextHost *iface,
337                                             TXTBACKSTYLE *pStyle)
338 {
339     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
340     TRACECALL("Call to TxGetBackStyle(%p, pStyle=%p)\n", This, pStyle);
341     return E_NOTIMPL;
342 }
343
344 static HRESULT WINAPI ITextHostImpl_TxGetMaxLength(ITextHost *iface,
345                                             DWORD *pLength)
346 {
347     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
348     TRACECALL("Call to TxGetMaxLength(%p, pLength=%p)\n", This, pLength);
349     return E_NOTIMPL;
350 }
351
352 static HRESULT WINAPI ITextHostImpl_TxGetScrollBars(ITextHost *iface,
353                                              DWORD *pdwScrollBar)
354 {
355     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
356     TRACECALL("Call to TxGetScrollBars(%p, pdwScrollBar=%p)\n",
357                This, pdwScrollBar);
358     return E_NOTIMPL;
359 }
360
361 static HRESULT WINAPI ITextHostImpl_TxGetPasswordChar(ITextHost *iface,
362                                                WCHAR *pch)
363 {
364     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
365     TRACECALL("Call to TxGetPasswordChar(%p, pch=%p)\n", This, pch);
366     return E_NOTIMPL;
367 }
368
369 static HRESULT WINAPI ITextHostImpl_TxGetAcceleratorPos(ITextHost *iface,
370                                                  LONG *pch)
371 {
372     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
373     TRACECALL("Call to TxGetAcceleratorPos(%p, pch=%p)\n", This, pch);
374     return E_NOTIMPL;
375 }
376
377 static HRESULT WINAPI ITextHostImpl_TxGetExtent(ITextHost *iface,
378                                          LPSIZEL lpExtent)
379 {
380     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
381     TRACECALL("Call to TxGetExtent(%p, lpExtent=%p)\n", This, lpExtent);
382     return E_NOTIMPL;
383 }
384
385 static HRESULT WINAPI ITextHostImpl_OnTxCharFormatChange(ITextHost *iface,
386                                                   const CHARFORMATW *pcf)
387 {
388     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
389     TRACECALL("Call to OnTxCharFormatChange(%p, pcf=%p)\n", This, pcf);
390     return E_NOTIMPL;
391 }
392
393 static HRESULT WINAPI ITextHostImpl_OnTxParaFormatChange(ITextHost *iface,
394                                                   const PARAFORMAT *ppf)
395 {
396     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
397     TRACECALL("Call to OnTxParaFormatChange(%p, ppf=%p)\n", This, ppf);
398     return E_NOTIMPL;
399 }
400
401 /* This must return S_OK for the native ITextServices object to
402    initialize. */
403 static HRESULT WINAPI ITextHostImpl_TxGetPropertyBits(ITextHost *iface,
404                                                DWORD dwMask,
405                                                DWORD *pdwBits)
406 {
407     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
408     TRACECALL("Call to TxGetPropertyBits(%p, dwMask=0x%08x, pdwBits=%p)\n",
409               This, dwMask, pdwBits);
410     *pdwBits = 0;
411     return S_OK;
412 }
413
414 static HRESULT WINAPI ITextHostImpl_TxNotify(ITextHost *iface, DWORD iNotify,
415                                       void *pv)
416 {
417     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
418     TRACECALL("Call to TxNotify(%p, iNotify=%d, pv=%p)\n", This, iNotify, pv);
419     return E_NOTIMPL;
420 }
421
422 static HIMC WINAPI ITextHostImpl_TxImmGetContext(ITextHost *iface)
423 {
424     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
425     TRACECALL("Call to TxImmGetContext(%p)\n", This);
426     return 0;
427 }
428
429 static void WINAPI ITextHostImpl_TxImmReleaseContext(ITextHost *iface, HIMC himc)
430 {
431     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
432     TRACECALL("Call to TxImmReleaseContext(%p, himc=%p)\n", This, himc);
433 }
434
435 static HRESULT WINAPI ITextHostImpl_TxGetSelectionBarWidth(ITextHost *iface,
436                                                     LONG *lSelBarWidth)
437 {
438     ITextHostTestImpl *This = (ITextHostTestImpl *)iface;
439     TRACECALL("Call to TxGetSelectionBarWidth(%p, lSelBarWidth=%p)\n",
440                 This, lSelBarWidth);
441     return E_NOTIMPL;
442 }
443
444 static ITextServicesVtbl itextServicesStdcallVtbl;
445
446 static ITextHostVtbl itextHostVtbl = {
447     ITextHostImpl_QueryInterface,
448     ITextHostImpl_AddRef,
449     ITextHostImpl_Release,
450     ITextHostImpl_TxGetDC,
451     ITextHostImpl_TxReleaseDC,
452     ITextHostImpl_TxShowScrollBar,
453     ITextHostImpl_TxEnableScrollBar,
454     ITextHostImpl_TxSetScrollRange,
455     ITextHostImpl_TxSetScrollPos,
456     ITextHostImpl_TxInvalidateRect,
457     ITextHostImpl_TxViewChange,
458     ITextHostImpl_TxCreateCaret,
459     ITextHostImpl_TxShowCaret,
460     ITextHostImpl_TxSetCaretPos,
461     ITextHostImpl_TxSetTimer,
462     ITextHostImpl_TxKillTimer,
463     ITextHostImpl_TxScrollWindowEx,
464     ITextHostImpl_TxSetCapture,
465     ITextHostImpl_TxSetFocus,
466     ITextHostImpl_TxSetCursor,
467     ITextHostImpl_TxScreenToClient,
468     ITextHostImpl_TxClientToScreen,
469     ITextHostImpl_TxActivate,
470     ITextHostImpl_TxDeactivate,
471     ITextHostImpl_TxGetClientRect,
472     ITextHostImpl_TxGetViewInset,
473     ITextHostImpl_TxGetCharFormat,
474     ITextHostImpl_TxGetParaFormat,
475     ITextHostImpl_TxGetSysColor,
476     ITextHostImpl_TxGetBackStyle,
477     ITextHostImpl_TxGetMaxLength,
478     ITextHostImpl_TxGetScrollBars,
479     ITextHostImpl_TxGetPasswordChar,
480     ITextHostImpl_TxGetAcceleratorPos,
481     ITextHostImpl_TxGetExtent,
482     ITextHostImpl_OnTxCharFormatChange,
483     ITextHostImpl_OnTxParaFormatChange,
484     ITextHostImpl_TxGetPropertyBits,
485     ITextHostImpl_TxNotify,
486     ITextHostImpl_TxImmGetContext,
487     ITextHostImpl_TxImmReleaseContext,
488     ITextHostImpl_TxGetSelectionBarWidth
489 };
490
491 static ITextServices *txtserv = NULL;
492 static ITextHostTestImpl *dummyTextHost;
493 static void *wrapperCodeMem = NULL;
494
495 #include "pshpack1.h"
496
497 /* Code structure for x86 byte code */
498 typedef struct
499 {
500     BYTE pop_eax;  /* popl  %eax  */
501     BYTE push_ecx; /* pushl %ecx  */
502     BYTE push_eax; /* pushl %eax  */
503     BYTE jmp_func; /* jmp   $func */
504     DWORD func;
505 } THISCALL_TO_STDCALL_THUNK;
506
507 typedef struct
508 {
509     BYTE pop_eax;               /* popl  %eax */
510     BYTE pop_ecx;               /* popl  %ecx */
511     BYTE push_eax;              /* pushl %eax */
512     BYTE mov_vtable_eax[2];     /* movl (%ecx), %eax */
513     BYTE jmp_eax[2];            /* jmp *$vtablefunc_offset(%eax) */
514     int  vtablefunc_offset;
515 } STDCALL_TO_THISCALL_THUNK;
516
517 #include "poppack.h"
518
519 static void setup_thiscall_wrappers(void)
520 {
521 #ifdef __i386__
522     void** pVtable;
523     void** pVtableEnd;
524     THISCALL_TO_STDCALL_THUNK *thunk;
525     STDCALL_TO_THISCALL_THUNK *thunk2;
526
527     wrapperCodeMem = VirtualAlloc(NULL,
528                                   (sizeof(ITextHostVtbl)/sizeof(void*) - 3)
529                                     * sizeof(THISCALL_TO_STDCALL_THUNK)
530                                   +(sizeof(ITextServicesVtbl)/sizeof(void*) - 3)
531                                     * sizeof(STDCALL_TO_THISCALL_THUNK),
532                                   MEM_COMMIT, PAGE_EXECUTE_READWRITE);
533     thunk = wrapperCodeMem;
534
535     /* Wrap all ITextHostImpl methods with code to perform a thiscall to
536      * stdcall conversion. The thiscall calling convention places the This
537      * pointer in ecx on the x86 platform, and the stdcall calling convention
538      * pushes the This pointer on the stack as the first argument.
539      *
540      * The byte code does the conversion then jumps to the real function.
541      *
542      * Each wrapper needs to be modified so that the function to jump to is
543      * modified in the byte code. */
544
545     /* Skip QueryInterface, AddRef, and Release native actually
546      * defined them with the stdcall calling convention. */
547     pVtable = (void**)&itextHostVtbl + 3;
548     pVtableEnd = (void**)(&itextHostVtbl + 1);
549     while (pVtable != pVtableEnd) {
550         /* write byte code to executable memory */
551         thunk->pop_eax = 0x58;  /* popl  %eax  */
552         thunk->push_ecx = 0x51; /* pushl %ecx  */
553         thunk->push_eax = 0x50; /* pushl %eax  */
554         thunk->jmp_func = 0xe9; /* jmp   $func */
555         /* The address needs to be relative to the end of the jump instructions. */
556         thunk->func = (char*)*pVtable - (char*)(&thunk->func + 1);
557         *pVtable = thunk;
558         pVtable++;
559         thunk++;
560     }
561
562     /* Setup an ITextServices standard call vtable that will call the
563      * native thiscall vtable when the methods are called. */
564
565     /* QueryInterface, AddRef, and Release should be called directly on the
566      * real vtable since they use the stdcall calling convention. */
567     thunk2 = (STDCALL_TO_THISCALL_THUNK *)thunk;
568     pVtable = (void**)&itextServicesStdcallVtbl + 3;
569     pVtableEnd = (void**)(&itextServicesStdcallVtbl + 1);
570     while (pVtable != pVtableEnd) {
571         /* write byte code to executable memory */
572         thunk2->pop_eax = 0x58;               /* popl  %eax */
573         thunk2->pop_ecx = 0x59;               /* popl  %ecx */
574         thunk2->push_eax = 0x50;              /* pushl %eax */
575         thunk2->mov_vtable_eax[0] = 0x8b;     /* movl (%ecx), %eax */
576         thunk2->mov_vtable_eax[1] = 0x01;
577         thunk2->jmp_eax[0] = 0xff;            /* jmp *$vtablefunc_offset(%eax) */
578         thunk2->jmp_eax[1] = 0xa0;
579         thunk2->vtablefunc_offset = (char*)pVtable - (char*)&itextServicesStdcallVtbl;
580         *pVtable = thunk2;
581         pVtable++;
582         thunk2++;
583     }
584 #endif /* __i386__ */
585 }
586
587 /*************************************************************************/
588 /* Conformance test functions. */
589
590 /* Initialize the test texthost structure */
591 static BOOL init_texthost(void)
592 {
593     IUnknown *init;
594     HRESULT result;
595     PCreateTextServices pCreateTextServices;
596
597     dummyTextHost = CoTaskMemAlloc(sizeof(*dummyTextHost));
598     if (dummyTextHost == NULL) {
599         skip("Insufficient memory to create ITextHost interface\n");
600         return FALSE;
601     }
602     dummyTextHost->lpVtbl = &itextHostVtbl;
603     dummyTextHost->refCount = 1;
604
605     /* MSDN states that an IUnknown object is returned by
606        CreateTextServices which is then queried to obtain a
607        ITextServices object. */
608     pCreateTextServices = (void*)GetProcAddress(hmoduleRichEdit, "CreateTextServices");
609     result = (*pCreateTextServices)(NULL,(ITextHost*)dummyTextHost, &init);
610     ok(result == S_OK, "Did not return OK when created. Returned %x\n", result);
611     if (result != S_OK) {
612         CoTaskMemFree(dummyTextHost);
613         skip("CreateTextServices failed.\n");
614         return FALSE;
615     }
616
617     result = IUnknown_QueryInterface(init, &IID_ITextServices,
618                                      (void **)&txtserv);
619     ok((result == S_OK) && (txtserv != NULL), "Querying interface failed\n");
620     IUnknown_Release(init);
621     if (!((result == S_OK) && (txtserv != NULL))) {
622         CoTaskMemFree(dummyTextHost);
623         skip("Could not retrieve ITextServices interface\n");
624         return FALSE;
625     }
626
627     return TRUE;
628 }
629
630 static void test_TxGetText(void)
631 {
632     HRESULT hres;
633     BSTR rettext;
634
635     if (!init_texthost())
636         return;
637
638     hres = ITextServices_TxGetText(txtserv, &rettext);
639     todo_wine ok(hres == S_OK, "ITextServices_TxGetText failed\n");
640
641     IUnknown_Release(txtserv);
642     CoTaskMemFree(dummyTextHost);
643 }
644
645 START_TEST( txtsrv )
646 {
647     setup_thiscall_wrappers();
648
649     /* Must explicitly LoadLibrary(). The test has no references to functions in
650      * RICHED20.DLL, so the linker doesn't actually link to it. */
651     hmoduleRichEdit = LoadLibrary("RICHED20.DLL");
652     ok(hmoduleRichEdit != NULL, "error: %d\n", (int) GetLastError());
653
654     if (init_texthost())
655     {
656         IUnknown_Release(txtserv);
657         CoTaskMemFree(dummyTextHost);
658
659         test_TxGetText();
660     }
661     if (wrapperCodeMem) VirtualFree(wrapperCodeMem, 0, MEM_RELEASE);
662 }