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