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 #define CONST_VTABLE
25 
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <windef.h>
29 #include <winbase.h>
30 #include <objbase.h>
31 #include <richedit.h>
32 #include <tom.h>
33 #include <richole.h>
34 #include <initguid.h>
35 #include <imm.h>
36 #include <textserv.h>
37 #include <wine/test.h>
38 #include <oleauto.h>
39 #include <limits.h>
40 
41 static HMODULE hmoduleRichEdit;
42 static IID *pIID_ITextServices;
43 static IID *pIID_ITextHost;
44 static IID *pIID_ITextHost2;
45 static PCreateTextServices pCreateTextServices;
46 
47 /* Define C Macros for ITextServices calls. */
48 
49 /* Use a special table for x86 machines to convert the thiscall
50  * calling convention.  This isn't needed on other platforms. */
51 #ifdef __i386__
52 static ITextServicesVtbl itextServicesStdcallVtbl;
53 #define TXTSERV_VTABLE(This) (&itextServicesStdcallVtbl)
54 #else /* __i386__ */
55 #define TXTSERV_VTABLE(This) (This)->lpVtbl
56 #endif /* __i386__ */
57 
58 #define ITextServices_TxSendMessage(This,a,b,c,d) TXTSERV_VTABLE(This)->TxSendMessage(This,a,b,c,d)
59 #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)
60 #define ITextServices_TxGetHScroll(This,a,b,c,d,e) TXTSERV_VTABLE(This)->TxGetHScroll(This,a,b,c,d,e)
61 #define ITextServices_TxGetVScroll(This,a,b,c,d,e) TXTSERV_VTABLE(This)->TxGetVScroll(This,a,b,c,d,e)
62 #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)
63 #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)
64 #define ITextServices_OnTxInplaceActivate(This,a) TXTSERV_VTABLE(This)->OnTxInplaceActivate(This,a)
65 #define ITextServices_OnTxInplaceDeactivate(This) TXTSERV_VTABLE(This)->OnTxInplaceDeactivate(This)
66 #define ITextServices_OnTxUIActivate(This) TXTSERV_VTABLE(This)->OnTxUIActivate(This)
67 #define ITextServices_OnTxUIDeactivate(This) TXTSERV_VTABLE(This)->OnTxUIDeactivate(This)
68 #define ITextServices_TxGetText(This,a) TXTSERV_VTABLE(This)->TxGetText(This,a)
69 #define ITextServices_TxSetText(This,a) TXTSERV_VTABLE(This)->TxSetText(This,a)
70 #define ITextServices_TxGetCurTargetX(This,a) TXTSERV_VTABLE(This)->TxGetCurTargetX(This,a)
71 #define ITextServices_TxGetBaseLinePos(This,a) TXTSERV_VTABLE(This)->TxGetBaseLinePos(This,a)
72 #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)
73 #define ITextServices_TxGetDropTarget(This,a) TXTSERV_VTABLE(This)->TxGetDropTarget(This,a)
74 #define ITextServices_OnTxPropertyBitsChange(This,a,b) TXTSERV_VTABLE(This)->OnTxPropertyBitsChange(This,a,b)
75 #define ITextServices_TxGetCachedSize(This,a,b) TXTSERV_VTABLE(This)->TxGetCachedSize(This,a,b)
76 
77 /* Set the WINETEST_DEBUG environment variable to be greater than 1 for verbose
78  * function call traces of ITextHost. */
79 #define TRACECALL if(winetest_debug > 1) trace
80 
81 /************************************************************************/
82 /* ITextHost implementation for conformance testing. */
83 
84 typedef struct ITextHostTestImpl
85 {
86     ITextHost ITextHost_iface;
87     LONG refCount;
88 } ITextHostTestImpl;
89 
90 static inline ITextHostTestImpl *impl_from_ITextHost(ITextHost *iface)
91 {
92     return CONTAINING_RECORD(iface, ITextHostTestImpl, ITextHost_iface);
93 }
94 
95 static HRESULT WINAPI ITextHostImpl_QueryInterface(ITextHost *iface,
96                                                    REFIID riid,
97                                                    LPVOID *ppvObject)
98 {
99     ITextHostTestImpl *This = impl_from_ITextHost(iface);
100 
101     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, pIID_ITextHost)) {
102         *ppvObject = &This->ITextHost_iface;
103         ITextHost_AddRef((ITextHost *)*ppvObject);
104         return S_OK;
105     }
106 
107     return E_NOINTERFACE;
108 }
109 
110 static ULONG WINAPI ITextHostImpl_AddRef(ITextHost *iface)
111 {
112     ITextHostTestImpl *This = impl_from_ITextHost(iface);
113     ULONG refCount = InterlockedIncrement(&This->refCount);
114     return refCount;
115 }
116 
117 static ULONG WINAPI ITextHostImpl_Release(ITextHost *iface)
118 {
119     ITextHostTestImpl *This = impl_from_ITextHost(iface);
120     ULONG refCount = InterlockedDecrement(&This->refCount);
121 
122     if (!refCount)
123     {
124         CoTaskMemFree(This);
125         return 0;
126     } else {
127         return refCount;
128     }
129 }
130 
131 static HDC WINAPI ITextHostImpl_TxGetDC(ITextHost *iface)
132 {
133     ITextHostTestImpl *This = impl_from_ITextHost(iface);
134     TRACECALL("Call to TxGetDC(%p)\n", This);
135     return NULL;
136 }
137 
138 static INT WINAPI ITextHostImpl_TxReleaseDC(ITextHost *iface,
139                                             HDC hdc)
140 {
141     ITextHostTestImpl *This = impl_from_ITextHost(iface);
142     TRACECALL("Call to TxReleaseDC(%p)\n", This);
143     return 0;
144 }
145 
146 static BOOL WINAPI ITextHostImpl_TxShowScrollBar(ITextHost *iface,
147                                                  INT fnBar,
148                                                  BOOL fShow)
149 {
150     ITextHostTestImpl *This = impl_from_ITextHost(iface);
151     TRACECALL("Call to TxShowScrollBar(%p, fnBar=%d, fShow=%d)\n",
152                 This, fnBar, fShow);
153     return FALSE;
154 }
155 
156 static BOOL WINAPI ITextHostImpl_TxEnableScrollBar(ITextHost *iface,
157                                                    INT fuSBFlags,
158                                                    INT fuArrowflags)
159 {
160     ITextHostTestImpl *This = impl_from_ITextHost(iface);
161     TRACECALL("Call to TxEnableScrollBar(%p, fuSBFlags=%d, fuArrowflags=%d)\n",
162                This, fuSBFlags, fuArrowflags);
163     return FALSE;
164 }
165 
166 static BOOL WINAPI ITextHostImpl_TxSetScrollRange(ITextHost *iface,
167                                                   INT fnBar,
168                                                   LONG nMinPos,
169                                                   INT nMaxPos,
170                                                   BOOL fRedraw)
171 {
172     ITextHostTestImpl *This = impl_from_ITextHost(iface);
173     TRACECALL("Call to TxSetScrollRange(%p, fnBar=%d, nMinPos=%d, nMaxPos=%d, fRedraw=%d)\n",
174                This, fnBar, nMinPos, nMaxPos, fRedraw);
175     return FALSE;
176 }
177 
178 static BOOL WINAPI ITextHostImpl_TxSetScrollPos(ITextHost *iface,
179                                                 INT fnBar,
180                                                 INT nPos,
181                                                 BOOL fRedraw)
182 {
183     ITextHostTestImpl *This = impl_from_ITextHost(iface);
184     TRACECALL("Call to TxSetScrollPos(%p, fnBar=%d, nPos=%d, fRedraw=%d)\n",
185                This, fnBar, nPos, fRedraw);
186     return FALSE;
187 }
188 
189 static void WINAPI ITextHostImpl_TxInvalidateRect(ITextHost *iface,
190                                                   LPCRECT prc,
191                                                   BOOL fMode)
192 {
193     ITextHostTestImpl *This = impl_from_ITextHost(iface);
194     TRACECALL("Call to TxInvalidateRect(%p, prc=%p, fMode=%d)\n",
195                This, prc, fMode);
196 }
197 
198 static void WINAPI ITextHostImpl_TxViewChange(ITextHost *iface, BOOL fUpdate)
199 {
200     ITextHostTestImpl *This = impl_from_ITextHost(iface);
201     TRACECALL("Call to TxViewChange(%p, fUpdate=%d)\n",
202                This, fUpdate);
203 }
204 
205 static BOOL WINAPI ITextHostImpl_TxCreateCaret(ITextHost *iface,
206                                                HBITMAP hbmp,
207                                                INT xWidth, INT yHeight)
208 {
209     ITextHostTestImpl *This = impl_from_ITextHost(iface);
210     TRACECALL("Call to TxCreateCaret(%p, nbmp=%p, xWidth=%d, yHeight=%d)\n",
211                This, hbmp, xWidth, yHeight);
212     return FALSE;
213 }
214 
215 static BOOL WINAPI ITextHostImpl_TxShowCaret(ITextHost *iface, BOOL fShow)
216 {
217     ITextHostTestImpl *This = impl_from_ITextHost(iface);
218     TRACECALL("Call to TxShowCaret(%p, fShow=%d)\n",
219                This, fShow);
220     return FALSE;
221 }
222 
223 static BOOL WINAPI ITextHostImpl_TxSetCaretPos(ITextHost *iface,
224                                                INT x, INT y)
225 {
226     ITextHostTestImpl *This = impl_from_ITextHost(iface);
227     TRACECALL("Call to TxSetCaretPos(%p, x=%d, y=%d)\n", This, x, y);
228     return FALSE;
229 }
230 
231 static BOOL WINAPI ITextHostImpl_TxSetTimer(ITextHost *iface,
232                                             UINT idTimer, UINT uTimeout)
233 {
234     ITextHostTestImpl *This = impl_from_ITextHost(iface);
235     TRACECALL("Call to TxSetTimer(%p, idTimer=%u, uTimeout=%u)\n",
236               This, idTimer, uTimeout);
237     return FALSE;
238 }
239 
240 static void WINAPI ITextHostImpl_TxKillTimer(ITextHost *iface, UINT idTimer)
241 {
242     ITextHostTestImpl *This = impl_from_ITextHost(iface);
243     TRACECALL("Call to TxKillTimer(%p, idTimer=%u)\n", This, idTimer);
244 }
245 
246 static void WINAPI ITextHostImpl_TxScrollWindowEx(ITextHost *iface,
247                                                   INT dx, INT dy,
248                                                   LPCRECT lprcScroll,
249                                                   LPCRECT lprcClip,
250                                                   HRGN hRgnUpdate,
251                                                   LPRECT lprcUpdate,
252                                                   UINT fuScroll)
253 {
254     ITextHostTestImpl *This = impl_from_ITextHost(iface);
255     TRACECALL("Call to TxScrollWindowEx(%p, %d, %d, %p, %p, %p, %p, %d)\n",
256               This, dx, dy, lprcScroll, lprcClip, hRgnUpdate, lprcUpdate, fuScroll);
257 }
258 
259 static void WINAPI ITextHostImpl_TxSetCapture(ITextHost *iface, BOOL fCapture)
260 {
261     ITextHostTestImpl *This = impl_from_ITextHost(iface);
262     TRACECALL("Call to TxSetCapture(%p, fCapture=%d)\n", This, fCapture);
263 }
264 
265 static void WINAPI ITextHostImpl_TxSetFocus(ITextHost *iface)
266 {
267     ITextHostTestImpl *This = impl_from_ITextHost(iface);
268     TRACECALL("Call to TxSetFocus(%p)\n", This);
269 }
270 
271 static void WINAPI ITextHostImpl_TxSetCursor(ITextHost *iface,
272                                              HCURSOR hcur,
273                                              BOOL fText)
274 {
275     ITextHostTestImpl *This = impl_from_ITextHost(iface);
276     TRACECALL("Call to TxSetCursor(%p, hcur=%p, fText=%d)\n",
277               This, hcur, fText);
278 }
279 
280 static BOOL WINAPI ITextHostImpl_TxScreenToClient(ITextHost *iface,
281                                                   LPPOINT lppt)
282 {
283     ITextHostTestImpl *This = impl_from_ITextHost(iface);
284     TRACECALL("Call to TxScreenToClient(%p, lppt=%p)\n", This, lppt);
285     return FALSE;
286 }
287 
288 static BOOL WINAPI ITextHostImpl_TxClientToScreen(ITextHost *iface,
289                                                   LPPOINT lppt)
290 {
291     ITextHostTestImpl *This = impl_from_ITextHost(iface);
292     TRACECALL("Call to TxClientToScreen(%p, lppt=%p)\n", This, lppt);
293     return FALSE;
294 }
295 
296 static HRESULT WINAPI ITextHostImpl_TxActivate(ITextHost *iface,
297                                                LONG *plOldState)
298 {
299     ITextHostTestImpl *This = impl_from_ITextHost(iface);
300     TRACECALL("Call to TxActivate(%p, plOldState=%p)\n", This, plOldState);
301     return E_NOTIMPL;
302 }
303 
304 static HRESULT WINAPI ITextHostImpl_TxDeactivate(ITextHost *iface,
305                                                  LONG lNewState)
306 {
307     ITextHostTestImpl *This = impl_from_ITextHost(iface);
308     TRACECALL("Call to TxDeactivate(%p, lNewState=%d)\n", This, lNewState);
309     return E_NOTIMPL;
310 }
311 
312 static HRESULT WINAPI ITextHostImpl_TxGetClientRect(ITextHost *iface,
313                                                     LPRECT prc)
314 {
315     ITextHostTestImpl *This = impl_from_ITextHost(iface);
316     TRACECALL("Call to TxGetClientRect(%p, prc=%p)\n", This, prc);
317     return E_NOTIMPL;
318 }
319 
320 static HRESULT WINAPI ITextHostImpl_TxGetViewInset(ITextHost *iface,
321                                                    LPRECT prc)
322 {
323     ITextHostTestImpl *This = impl_from_ITextHost(iface);
324     TRACECALL("Call to TxGetViewInset(%p, prc=%p)\n", This, prc);
325     return E_NOTIMPL;
326 }
327 
328 static HRESULT WINAPI ITextHostImpl_TxGetCharFormat(ITextHost *iface,
329                                                     const CHARFORMATW **ppCF)
330 {
331     ITextHostTestImpl *This = impl_from_ITextHost(iface);
332     TRACECALL("Call to TxGetCharFormat(%p, ppCF=%p)\n", This, ppCF);
333     return E_NOTIMPL;
334 }
335 
336 static HRESULT WINAPI ITextHostImpl_TxGetParaFormat(ITextHost *iface,
337                                                     const PARAFORMAT **ppPF)
338 {
339     ITextHostTestImpl *This = impl_from_ITextHost(iface);
340     TRACECALL("Call to TxGetParaFormat(%p, ppPF=%p)\n", This, ppPF);
341     return E_NOTIMPL;
342 }
343 
344 static COLORREF WINAPI ITextHostImpl_TxGetSysColor(ITextHost *iface,
345                                                    int nIndex)
346 {
347     ITextHostTestImpl *This = impl_from_ITextHost(iface);
348     TRACECALL("Call to TxGetSysColor(%p, nIndex=%d)\n", This, nIndex);
349     return E_NOTIMPL;
350 }
351 
352 static HRESULT WINAPI ITextHostImpl_TxGetBackStyle(ITextHost *iface,
353                                                    TXTBACKSTYLE *pStyle)
354 {
355     ITextHostTestImpl *This = impl_from_ITextHost(iface);
356     TRACECALL("Call to TxGetBackStyle(%p, pStyle=%p)\n", This, pStyle);
357     return E_NOTIMPL;
358 }
359 
360 static HRESULT WINAPI ITextHostImpl_TxGetMaxLength(ITextHost *iface,
361                                                    DWORD *pLength)
362 {
363     ITextHostTestImpl *This = impl_from_ITextHost(iface);
364     TRACECALL("Call to TxGetMaxLength(%p, pLength=%p)\n", This, pLength);
365     return E_NOTIMPL;
366 }
367 
368 static HRESULT WINAPI ITextHostImpl_TxGetScrollBars(ITextHost *iface,
369                                                     DWORD *pdwScrollBar)
370 {
371     ITextHostTestImpl *This = impl_from_ITextHost(iface);
372     TRACECALL("Call to TxGetScrollBars(%p, pdwScrollBar=%p)\n",
373                This, pdwScrollBar);
374     return E_NOTIMPL;
375 }
376 
377 static HRESULT WINAPI ITextHostImpl_TxGetPasswordChar(ITextHost *iface,
378                                                       WCHAR *pch)
379 {
380     ITextHostTestImpl *This = impl_from_ITextHost(iface);
381     TRACECALL("Call to TxGetPasswordChar(%p, pch=%p)\n", This, pch);
382     return E_NOTIMPL;
383 }
384 
385 static HRESULT WINAPI ITextHostImpl_TxGetAcceleratorPos(ITextHost *iface,
386                                                         LONG *pch)
387 {
388     ITextHostTestImpl *This = impl_from_ITextHost(iface);
389     TRACECALL("Call to TxGetAcceleratorPos(%p, pch=%p)\n", This, pch);
390     return E_NOTIMPL;
391 }
392 
393 static HRESULT WINAPI ITextHostImpl_TxGetExtent(ITextHost *iface,
394                                                 LPSIZEL lpExtent)
395 {
396     ITextHostTestImpl *This = impl_from_ITextHost(iface);
397     TRACECALL("Call to TxGetExtent(%p, lpExtent=%p)\n", This, lpExtent);
398     return E_NOTIMPL;
399 }
400 
401 static HRESULT WINAPI ITextHostImpl_OnTxCharFormatChange(ITextHost *iface,
402                                                          const CHARFORMATW *pcf)
403 {
404     ITextHostTestImpl *This = impl_from_ITextHost(iface);
405     TRACECALL("Call to OnTxCharFormatChange(%p, pcf=%p)\n", This, pcf);
406     return E_NOTIMPL;
407 }
408 
409 static HRESULT WINAPI ITextHostImpl_OnTxParaFormatChange(ITextHost *iface,
410                                                          const PARAFORMAT *ppf)
411 {
412     ITextHostTestImpl *This = impl_from_ITextHost(iface);
413     TRACECALL("Call to OnTxParaFormatChange(%p, ppf=%p)\n", This, ppf);
414     return E_NOTIMPL;
415 }
416 
417 /* This must return S_OK for the native ITextServices object to
418    initialize. */
419 static HRESULT WINAPI ITextHostImpl_TxGetPropertyBits(ITextHost *iface,
420                                                       DWORD dwMask,
421                                                       DWORD *pdwBits)
422 {
423     ITextHostTestImpl *This = impl_from_ITextHost(iface);
424     TRACECALL("Call to TxGetPropertyBits(%p, dwMask=0x%08x, pdwBits=%p)\n",
425               This, dwMask, pdwBits);
426     *pdwBits = 0;
427     return S_OK;
428 }
429 
430 static HRESULT WINAPI ITextHostImpl_TxNotify(ITextHost *iface, DWORD iNotify,
431                                              void *pv)
432 {
433     ITextHostTestImpl *This = impl_from_ITextHost(iface);
434     TRACECALL("Call to TxNotify(%p, iNotify=%d, pv=%p)\n", This, iNotify, pv);
435     return E_NOTIMPL;
436 }
437 
438 static HIMC WINAPI ITextHostImpl_TxImmGetContext(ITextHost *iface)
439 {
440     ITextHostTestImpl *This = impl_from_ITextHost(iface);
441     TRACECALL("Call to TxImmGetContext(%p)\n", This);
442     return 0;
443 }
444 
445 static void WINAPI ITextHostImpl_TxImmReleaseContext(ITextHost *iface, HIMC himc)
446 {
447     ITextHostTestImpl *This = impl_from_ITextHost(iface);
448     TRACECALL("Call to TxImmReleaseContext(%p, himc=%p)\n", This, himc);
449 }
450 
451 /* This function must set the variable pointed to by *lSelBarWidth.
452    Otherwise an uninitialized value will be used to calculate
453    positions and sizes even if E_NOTIMPL is returned. */
454 static HRESULT WINAPI ITextHostImpl_TxGetSelectionBarWidth(ITextHost *iface,
455                                                            LONG *lSelBarWidth)
456 {
457     ITextHostTestImpl *This = impl_from_ITextHost(iface);
458     TRACECALL("Call to TxGetSelectionBarWidth(%p, lSelBarWidth=%p)\n",
459                 This, lSelBarWidth);
460     *lSelBarWidth = 0;
461     return E_NOTIMPL;
462 }
463 
464 static ITextHostVtbl itextHostVtbl = {
465     ITextHostImpl_QueryInterface,
466     ITextHostImpl_AddRef,
467     ITextHostImpl_Release,
468     ITextHostImpl_TxGetDC,
469     ITextHostImpl_TxReleaseDC,
470     ITextHostImpl_TxShowScrollBar,
471     ITextHostImpl_TxEnableScrollBar,
472     ITextHostImpl_TxSetScrollRange,
473     ITextHostImpl_TxSetScrollPos,
474     ITextHostImpl_TxInvalidateRect,
475     ITextHostImpl_TxViewChange,
476     ITextHostImpl_TxCreateCaret,
477     ITextHostImpl_TxShowCaret,
478     ITextHostImpl_TxSetCaretPos,
479     ITextHostImpl_TxSetTimer,
480     ITextHostImpl_TxKillTimer,
481     ITextHostImpl_TxScrollWindowEx,
482     ITextHostImpl_TxSetCapture,
483     ITextHostImpl_TxSetFocus,
484     ITextHostImpl_TxSetCursor,
485     ITextHostImpl_TxScreenToClient,
486     ITextHostImpl_TxClientToScreen,
487     ITextHostImpl_TxActivate,
488     ITextHostImpl_TxDeactivate,
489     ITextHostImpl_TxGetClientRect,
490     ITextHostImpl_TxGetViewInset,
491     ITextHostImpl_TxGetCharFormat,
492     ITextHostImpl_TxGetParaFormat,
493     ITextHostImpl_TxGetSysColor,
494     ITextHostImpl_TxGetBackStyle,
495     ITextHostImpl_TxGetMaxLength,
496     ITextHostImpl_TxGetScrollBars,
497     ITextHostImpl_TxGetPasswordChar,
498     ITextHostImpl_TxGetAcceleratorPos,
499     ITextHostImpl_TxGetExtent,
500     ITextHostImpl_OnTxCharFormatChange,
501     ITextHostImpl_OnTxParaFormatChange,
502     ITextHostImpl_TxGetPropertyBits,
503     ITextHostImpl_TxNotify,
504     ITextHostImpl_TxImmGetContext,
505     ITextHostImpl_TxImmReleaseContext,
506     ITextHostImpl_TxGetSelectionBarWidth
507 };
508 
509 static void *wrapperCodeMem = NULL;
510 
511 #include "pshpack1.h"
512 
513 /* Code structure for x86 byte code */
514 typedef struct
515 {
516     BYTE pop_eax;  /* popl  %eax  */
517     BYTE push_ecx; /* pushl %ecx  */
518     BYTE push_eax; /* pushl %eax  */
519     BYTE jmp_func; /* jmp   $func */
520     DWORD func;
521 } THISCALL_TO_STDCALL_THUNK;
522 
523 typedef struct
524 {
525     BYTE pop_eax;               /* popl  %eax */
526     BYTE pop_ecx;               /* popl  %ecx */
527     BYTE push_eax;              /* pushl %eax */
528     BYTE mov_vtable_eax[2];     /* movl (%ecx), %eax */
529     BYTE jmp_eax[2];            /* jmp *$vtablefunc_offset(%eax) */
530     int  vtablefunc_offset;
531 } STDCALL_TO_THISCALL_THUNK;
532 
533 #include "poppack.h"
534 
535 static void setup_thiscall_wrappers(void)
536 {
537 #ifdef __i386__
538     void** pVtable;
539     void** pVtableEnd;
540     THISCALL_TO_STDCALL_THUNK *thunk;
541     STDCALL_TO_THISCALL_THUNK *thunk2;
542 
543     wrapperCodeMem = VirtualAlloc(NULL,
544                                   (sizeof(ITextHostVtbl)/sizeof(void*) - 3)
545                                     * sizeof(THISCALL_TO_STDCALL_THUNK)
546                                   +(sizeof(ITextServicesVtbl)/sizeof(void*) - 3)
547                                     * sizeof(STDCALL_TO_THISCALL_THUNK),
548                                   MEM_COMMIT, PAGE_EXECUTE_READWRITE);
549     thunk = wrapperCodeMem;
550 
551     /* Wrap all ITextHostImpl methods with code to perform a thiscall to
552      * stdcall conversion. The thiscall calling convention places the This
553      * pointer in ecx on the x86 platform, and the stdcall calling convention
554      * pushes the This pointer on the stack as the first argument.
555      *
556      * The byte code does the conversion then jumps to the real function.
557      *
558      * Each wrapper needs to be modified so that the function to jump to is
559      * modified in the byte code. */
560 
561     /* Skip QueryInterface, AddRef, and Release native actually
562      * defined them with the stdcall calling convention. */
563     pVtable = (void**)&itextHostVtbl + 3;
564     pVtableEnd = (void**)(&itextHostVtbl + 1);
565     while (pVtable != pVtableEnd) {
566         /* write byte code to executable memory */
567         thunk->pop_eax = 0x58;  /* popl  %eax  */
568         thunk->push_ecx = 0x51; /* pushl %ecx  */
569         thunk->push_eax = 0x50; /* pushl %eax  */
570         thunk->jmp_func = 0xe9; /* jmp   $func */
571         /* The address needs to be relative to the end of the jump instructions. */
572         thunk->func = (char*)*pVtable - (char*)(&thunk->func + 1);
573         *pVtable = thunk;
574         pVtable++;
575         thunk++;
576     }
577 
578     /* Setup an ITextServices standard call vtable that will call the
579      * native thiscall vtable when the methods are called. */
580 
581     /* QueryInterface, AddRef, and Release should be called directly on the
582      * real vtable since they use the stdcall calling convention. */
583     thunk2 = (STDCALL_TO_THISCALL_THUNK *)thunk;
584     pVtable = (void**)&itextServicesStdcallVtbl + 3;
585     pVtableEnd = (void**)(&itextServicesStdcallVtbl + 1);
586     while (pVtable != pVtableEnd) {
587         /* write byte code to executable memory */
588         thunk2->pop_eax = 0x58;               /* popl  %eax */
589         thunk2->pop_ecx = 0x59;               /* popl  %ecx */
590         thunk2->push_eax = 0x50;              /* pushl %eax */
591         thunk2->mov_vtable_eax[0] = 0x8b;     /* movl (%ecx), %eax */
592         thunk2->mov_vtable_eax[1] = 0x01;
593         thunk2->jmp_eax[0] = 0xff;            /* jmp *$vtablefunc_offset(%eax) */
594         thunk2->jmp_eax[1] = 0xa0;
595         thunk2->vtablefunc_offset = (char*)pVtable - (char*)&itextServicesStdcallVtbl;
596         *pVtable = thunk2;
597         pVtable++;
598         thunk2++;
599     }
600 #endif /* __i386__ */
601 }
602 
603 /*************************************************************************/
604 /* Conformance test functions. */
605 
606 /* Initialize the test texthost structure */
607 static BOOL init_texthost(ITextServices **txtserv, ITextHost **ret)
608 {
609     ITextHostTestImpl *dummyTextHost;
610     IUnknown *init;
611     HRESULT result;
612 
613     dummyTextHost = CoTaskMemAlloc(sizeof(*dummyTextHost));
614     if (dummyTextHost == NULL) {
615         win_skip("Insufficient memory to create ITextHost interface\n");
616         return FALSE;
617     }
618     dummyTextHost->ITextHost_iface.lpVtbl = &itextHostVtbl;
619     dummyTextHost->refCount = 1;
620 
621     /* MSDN states that an IUnknown object is returned by
622        CreateTextServices which is then queried to obtain a
623        ITextServices object. */
624     result = pCreateTextServices(NULL, &dummyTextHost->ITextHost_iface, &init);
625     ok(result == S_OK, "Did not return S_OK when created (result =  %x)\n", result);
626     if (result != S_OK) {
627         CoTaskMemFree(dummyTextHost);
628         win_skip("CreateTextServices failed.\n");
629         return FALSE;
630     }
631 
632     result = IUnknown_QueryInterface(init, pIID_ITextServices, (void**)txtserv);
633     ok((result == S_OK) && (*txtserv != NULL), "Querying interface failed (result = %x, txtserv = %p)\n", result, *txtserv);
634     IUnknown_Release(init);
635     if (!((result == S_OK) && (*txtserv != NULL))) {
636         CoTaskMemFree(dummyTextHost);
637         win_skip("Could not retrieve ITextServices interface\n");
638         return FALSE;
639     }
640 
641     *ret = &dummyTextHost->ITextHost_iface;
642     return TRUE;
643 }
644 
645 static void test_TxGetText(void)
646 {
647     ITextServices *txtserv;
648     ITextHost *host;
649     HRESULT hres;
650     BSTR rettext;
651 
652     if (!init_texthost(&txtserv, &host))
653         return;
654 
655     hres = ITextServices_TxGetText(txtserv, &rettext);
656     ok(hres == S_OK, "ITextServices_TxGetText failed (result = %x)\n", hres);
657 
658     ITextServices_Release(txtserv);
659     ITextHost_Release(host);
660 }
661 
662 static void test_TxSetText(void)
663 {
664     ITextServices *txtserv;
665     ITextHost *host;
666     HRESULT hres;
667     BSTR rettext;
668     WCHAR settext[] = {'T','e','s','t',0};
669 
670     if (!init_texthost(&txtserv, &host))
671         return;
672 
673     hres = ITextServices_TxSetText(txtserv, settext);
674     ok(hres == S_OK, "ITextServices_TxSetText failed (result = %x)\n", hres);
675 
676     hres = ITextServices_TxGetText(txtserv, &rettext);
677     ok(hres == S_OK, "ITextServices_TxGetText failed (result = %x)\n", hres);
678 
679     ok(SysStringLen(rettext) == 4,
680                  "String returned of wrong length (expected 4, got %d)\n", SysStringLen(rettext));
681     ok(memcmp(rettext,settext,SysStringByteLen(rettext)) == 0,
682                  "String returned differs\n");
683 
684     /* Null-pointer should behave the same as empty-string */
685 
686     hres = ITextServices_TxSetText(txtserv, 0);
687     ok(hres == S_OK, "ITextServices_TxSetText failed (result = %x)\n", hres);
688 
689     hres = ITextServices_TxGetText(txtserv, &rettext);
690     ok(hres == S_OK, "ITextServices_TxGetText failed (result = %x)\n", hres);
691     ok(SysStringLen(rettext) == 0,
692                  "String returned of wrong length (expected 0, got %d)\n", SysStringLen(rettext));
693 
694     SysFreeString(rettext);
695     ITextServices_Release(txtserv);
696     ITextHost_Release(host);
697 }
698 
699 static void test_TxGetNaturalSize(void)
700 {
701     ITextServices *txtserv;
702     ITextHost *host;
703     HRESULT result;
704     BOOL ret;
705 
706     /* This value is used when calling TxGetNaturalSize.  MSDN says
707        that this is not supported however a null pointer cannot be
708        used as it will cause a segmentation violation.  The values in
709        the structure being pointed to are required to be INT_MAX
710        otherwise calculations can give wrong values. */
711     const SIZEL psizelExtent = {INT_MAX,INT_MAX};
712 
713     static const WCHAR oneA[] = {'A',0};
714 
715     /* Results of measurements */
716     LONG xdim, ydim;
717 
718     /* The device context to do the tests in */
719     HDC hdcDraw;
720 
721     /* Variables with the text metric information */
722     INT charwidth_caps_text[26];
723     TEXTMETRICA tmInfo_text;
724 
725     if (!init_texthost(&txtserv, &host))
726         return;
727 
728     hdcDraw = GetDC(NULL);
729     SaveDC(hdcDraw);
730 
731     /* Populate the metric strucs */
732     SetMapMode(hdcDraw,MM_TEXT);
733     GetTextMetricsA(hdcDraw, &tmInfo_text);
734     SetLastError(0xdeadbeef);
735     ret = GetCharWidth32A(hdcDraw,'A','Z',charwidth_caps_text);
736     if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
737         win_skip("GetCharWidth32 is not available\n");
738         goto cleanup;
739     }
740 
741     /* Make measurements in MM_TEXT */
742     SetMapMode(hdcDraw,MM_TEXT);
743     xdim = 0; ydim = 0;
744 
745     result = ITextServices_TxSetText(txtserv, oneA);
746     ok(result == S_OK, "ITextServices_TxSetText failed (result = %x)\n", result);
747     if (result != S_OK) {
748         skip("Could not set text\n");
749         goto cleanup;
750     }
751 
752     SetLastError(0xdeadbeef);
753     result = ITextServices_TxGetNaturalSize(txtserv, DVASPECT_CONTENT,
754                                             hdcDraw, NULL, NULL,
755                                             TXTNS_FITTOCONTENT, &psizelExtent,
756                                             &xdim, &ydim);
757     todo_wine ok(result == S_OK || broken(result == E_FAIL), /* WINXP Arabic Language */
758         "TxGetNaturalSize gave unexpected return value (result = %x)\n", result);
759     if (result == S_OK) {
760     todo_wine ok(ydim == tmInfo_text.tmHeight,
761                  "Height calculated incorrectly (expected %d, got %d)\n",
762                  tmInfo_text.tmHeight, ydim);
763     /* The native DLL adds one pixel extra when calculating widths. */
764     todo_wine ok(xdim >= charwidth_caps_text[0] && xdim <= charwidth_caps_text[0] + 1,
765                  "Width calculated incorrectly (expected %d {+1}, got %d)\n",
766                  charwidth_caps_text[0], xdim);
767     } else
768         skip("TxGetNaturalSize measurements not performed (xdim = %d, ydim = %d, result = %x, error = %x)\n",
769              xdim, ydim, result, GetLastError());
770 
771 cleanup:
772     RestoreDC(hdcDraw,1);
773     ReleaseDC(NULL,hdcDraw);
774     ITextServices_Release(txtserv);
775     ITextHost_Release(host);
776 }
777 
778 static void test_TxDraw(void)
779 {
780     ITextServices *txtserv;
781     ITextHost *host;
782     HDC tmphdc = GetDC(NULL);
783     DWORD dwAspect = DVASPECT_CONTENT;
784     HDC hicTargetDev = NULL; /* Means "default" device */
785     DVTARGETDEVICE *ptd = NULL;
786     void *pvAspect = NULL;
787     HRESULT result;
788     RECTL client = {0,0,100,100};
789 
790 
791     if (!init_texthost(&txtserv, &host))
792         return;
793 
794     todo_wine {
795         result = ITextServices_TxDraw(txtserv, dwAspect, 0, pvAspect, ptd,
796                                       tmphdc, hicTargetDev, &client, NULL,
797                                       NULL, NULL, 0, 0);
798         ok(result == S_OK, "TxDraw failed (result = %x)\n", result);
799     }
800 
801     ITextServices_Release(txtserv);
802     ITextHost_Release(host);
803 }
804 
805 DEFINE_GUID(expected_iid_itextservices, 0x8d33f740, 0xcf58, 0x11ce, 0xa8, 0x9d, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5);
806 DEFINE_GUID(expected_iid_itexthost, 0x13e670f4,0x1a5a,0x11cf,0xab,0xeb,0x00,0xaa,0x00,0xb6,0x5e,0xa1);
807 DEFINE_GUID(expected_iid_itexthost2, 0x13e670f5,0x1a5a,0x11cf,0xab,0xeb,0x00,0xaa,0x00,0xb6,0x5e,0xa1);
808 
809 static void test_IIDs(void)
810 {
811     ok(IsEqualIID(pIID_ITextServices, &expected_iid_itextservices),
812        "unexpected value for IID_ITextServices: %s\n", wine_dbgstr_guid(pIID_ITextServices));
813     ok(IsEqualIID(pIID_ITextHost, &expected_iid_itexthost),
814        "unexpected value for IID_ITextHost: %s\n", wine_dbgstr_guid(pIID_ITextHost));
815     ok(IsEqualIID(pIID_ITextHost2, &expected_iid_itexthost2),
816        "unexpected value for IID_ITextHost2: %s\n", wine_dbgstr_guid(pIID_ITextHost2));
817 }
818 
819 /* Outer IUnknown for COM aggregation tests */
820 struct unk_impl {
821     IUnknown IUnknown_iface;
822     LONG ref;
823     IUnknown *inner_unk;
824 };
825 
826 static inline struct unk_impl *impl_from_IUnknown(IUnknown *iface)
827 {
828     return CONTAINING_RECORD(iface, struct unk_impl, IUnknown_iface);
829 }
830 
831 static HRESULT WINAPI unk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
832 {
833     struct unk_impl *This = impl_from_IUnknown(iface);
834 
835     return IUnknown_QueryInterface(This->inner_unk, riid, ppv);
836 }
837 
838 static ULONG WINAPI unk_AddRef(IUnknown *iface)
839 {
840     struct unk_impl *This = impl_from_IUnknown(iface);
841 
842     return InterlockedIncrement(&This->ref);
843 }
844 
845 static ULONG WINAPI unk_Release(IUnknown *iface)
846 {
847     struct unk_impl *This = impl_from_IUnknown(iface);
848 
849     return InterlockedDecrement(&This->ref);
850 }
851 
852 static const IUnknownVtbl unk_vtbl =
853 {
854     unk_QueryInterface,
855     unk_AddRef,
856     unk_Release
857 };
858 
859 static void test_COM(void)
860 {
861     struct unk_impl unk_obj = {{&unk_vtbl}, 19, NULL};
862     struct ITextHostTestImpl texthost = {{&itextHostVtbl}, 1};
863     ITextServices *textsrv;
864     ULONG refcount;
865     HRESULT hr;
866 
867     /* COM aggregation */
868     hr = pCreateTextServices(&unk_obj.IUnknown_iface, &texthost.ITextHost_iface,
869                              &unk_obj.inner_unk);
870     ok(hr == S_OK, "CreateTextServices failed: %08x\n", hr);
871     hr = IUnknown_QueryInterface(unk_obj.inner_unk, pIID_ITextServices, (void**)&textsrv);
872     ok(hr == S_OK, "QueryInterface for IID_ITextServices failed: %08x\n", hr);
873     refcount = ITextServices_AddRef(textsrv);
874     ok(refcount == unk_obj.ref, "CreateTextServices just pretends to support COM aggregation\n");
875     refcount = ITextServices_Release(textsrv);
876     ok(refcount == unk_obj.ref, "CreateTextServices just pretends to support COM aggregation\n");
877     refcount = ITextServices_Release(textsrv);
878     ok(refcount == 19, "Refcount should be back at 19 but is %u\n", refcount);
879 
880     IUnknown_Release(unk_obj.inner_unk);
881 }
882 
883 static ULONG get_refcount(IUnknown *iface)
884 {
885     IUnknown_AddRef(iface);
886     return IUnknown_Release(iface);
887 }
888 
889 static void test_QueryInterface(void)
890 {
891     ITextServices *txtserv;
892     ITextHost *host;
893     HRESULT hres;
894     IRichEditOle *reole, *txtsrv_reole;
895     ITextDocument *txtdoc, *txtsrv_txtdoc;
896     ULONG refcount;
897 
898     if(!init_texthost(&txtserv, &host))
899         return;
900 
901     refcount = get_refcount((IUnknown *)txtserv);
902     ok(refcount == 1, "got wrong ref count: %d\n", refcount);
903 
904     /* IID_IRichEditOle */
905     hres = ITextServices_QueryInterface(txtserv, &IID_IRichEditOle, (void **)&txtsrv_reole);
906     ok(hres == S_OK, "ITextServices_QueryInterface: 0x%08x\n", hres);
907     refcount = get_refcount((IUnknown *)txtserv);
908     ok(refcount == 2, "got wrong ref count: %d\n", refcount);
909     refcount = get_refcount((IUnknown *)txtsrv_reole);
910     ok(refcount == 2, "got wrong ref count: %d\n", refcount);
911 
912     hres = IRichEditOle_QueryInterface(txtsrv_reole, &IID_ITextDocument, (void **)&txtdoc);
913     ok(hres == S_OK, "IRichEditOle_QueryInterface: 0x%08x\n", hres);
914     refcount = get_refcount((IUnknown *)txtserv);
915     ok(refcount == 3, "got wrong ref count: %d\n", refcount);
916     refcount = get_refcount((IUnknown *)txtsrv_reole);
917     ok(refcount == 3, "got wrong ref count: %d\n", refcount);
918 
919     ITextDocument_Release(txtdoc);
920     refcount = get_refcount((IUnknown *)txtserv);
921     ok(refcount == 2, "got wrong ref count: %d\n", refcount);
922     IRichEditOle_Release(txtsrv_reole);
923     refcount = get_refcount((IUnknown *)txtserv);
924     ok(refcount == 1, "got wrong ref count: %d\n", refcount);
925 
926     /* IID_ITextDocument */
927     hres = ITextServices_QueryInterface(txtserv, &IID_ITextDocument, (void **)&txtsrv_txtdoc);
928     ok(hres == S_OK, "ITextServices_QueryInterface: 0x%08x\n", hres);
929     refcount = get_refcount((IUnknown *)txtserv);
930     ok(refcount == 2, "got wrong ref count: %d\n", refcount);
931     refcount = get_refcount((IUnknown *)txtsrv_txtdoc);
932     ok(refcount == 2, "got wrong ref count: %d\n", refcount);
933 
934     hres = ITextDocument_QueryInterface(txtsrv_txtdoc, &IID_IRichEditOle, (void **)&reole);
935     ok(hres == S_OK, "ITextDocument_QueryInterface: 0x%08x\n", hres);
936     refcount = get_refcount((IUnknown *)txtserv);
937     ok(refcount == 3, "got wrong ref count: %d\n", refcount);
938     refcount = get_refcount((IUnknown *)txtsrv_txtdoc);
939     ok(refcount == 3, "got wrong ref count: %d\n", refcount);
940 
941     IRichEditOle_Release(reole);
942     refcount = get_refcount((IUnknown *)txtserv);
943     ok(refcount == 2, "got wrong ref count: %d\n", refcount);
944     ITextDocument_Release(txtsrv_txtdoc);
945     refcount = get_refcount((IUnknown *)txtserv);
946     ok(refcount == 1, "got wrong ref count: %d\n", refcount);
947 
948     ITextServices_Release(txtserv);
949     ITextHost_Release(host);
950 }
951 
952 START_TEST( txtsrv )
953 {
954     ITextServices *txtserv;
955     ITextHost *host;
956 
957     setup_thiscall_wrappers();
958 
959     /* Must explicitly LoadLibrary(). The test has no references to functions in
960      * RICHED20.DLL, so the linker doesn't actually link to it. */
961     hmoduleRichEdit = LoadLibraryA("riched20.dll");
962     ok(hmoduleRichEdit != NULL, "error: %d\n", (int) GetLastError());
963 
964     pIID_ITextServices = (IID*)GetProcAddress(hmoduleRichEdit, "IID_ITextServices");
965     pIID_ITextHost = (IID*)GetProcAddress(hmoduleRichEdit, "IID_ITextHost");
966     pIID_ITextHost2 = (IID*)GetProcAddress(hmoduleRichEdit, "IID_ITextHost2");
967     pCreateTextServices = (void*)GetProcAddress(hmoduleRichEdit, "CreateTextServices");
968 
969     test_IIDs();
970     test_COM();
971 
972     if (init_texthost(&txtserv, &host))
973     {
974         ITextServices_Release(txtserv);
975         ITextHost_Release(host);
976 
977         test_TxGetText();
978         test_TxSetText();
979         test_TxGetNaturalSize();
980         test_TxDraw();
981         test_QueryInterface();
982     }
983     if (wrapperCodeMem) VirtualFree(wrapperCodeMem, 0, MEM_RELEASE);
984 }
985