txtsrv.c 35.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Unit test suite for windowless rich edit controls
 *
 * Copyright 2008 Maarten Lankhorst
 * Copyright 2008 Austin Lund
 * Copyright 2008 Dylan Smith
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS
24
#define CONST_VTABLE
25 26 27 28 29 30 31

#include <stdio.h>
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <objbase.h>
#include <richedit.h>
32 33
#include <tom.h>
#include <richole.h>
34
#include <initguid.h>
35
#include <imm.h>
36 37
#include <textserv.h>
#include <wine/test.h>
38
#include <oleauto.h>
39
#include <limits.h>
40 41

static HMODULE hmoduleRichEdit;
42 43 44
static IID *pIID_ITextServices;
static IID *pIID_ITextHost;
static IID *pIID_ITextHost2;
45
static PCreateTextServices pCreateTextServices;
46

47 48 49 50 51
/* Define C Macros for ITextServices calls. */

/* Use a special table for x86 machines to convert the thiscall
 * calling convention.  This isn't needed on other platforms. */
#ifdef __i386__
52
static ITextServicesVtbl itextServicesStdcallVtbl;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#define TXTSERV_VTABLE(This) (&itextServicesStdcallVtbl)
#else /* __i386__ */
#define TXTSERV_VTABLE(This) (This)->lpVtbl
#endif /* __i386__ */

#define ITextServices_TxSendMessage(This,a,b,c,d) TXTSERV_VTABLE(This)->TxSendMessage(This,a,b,c,d)
#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)
#define ITextServices_TxGetHScroll(This,a,b,c,d,e) TXTSERV_VTABLE(This)->TxGetHScroll(This,a,b,c,d,e)
#define ITextServices_TxGetVScroll(This,a,b,c,d,e) TXTSERV_VTABLE(This)->TxGetVScroll(This,a,b,c,d,e)
#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)
#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)
#define ITextServices_OnTxInplaceActivate(This,a) TXTSERV_VTABLE(This)->OnTxInplaceActivate(This,a)
#define ITextServices_OnTxInplaceDeactivate(This) TXTSERV_VTABLE(This)->OnTxInplaceDeactivate(This)
#define ITextServices_OnTxUIActivate(This) TXTSERV_VTABLE(This)->OnTxUIActivate(This)
#define ITextServices_OnTxUIDeactivate(This) TXTSERV_VTABLE(This)->OnTxUIDeactivate(This)
#define ITextServices_TxGetText(This,a) TXTSERV_VTABLE(This)->TxGetText(This,a)
#define ITextServices_TxSetText(This,a) TXTSERV_VTABLE(This)->TxSetText(This,a)
70
#define ITextServices_TxGetCurTargetX(This,a) TXTSERV_VTABLE(This)->TxGetCurTargetX(This,a)
71 72 73 74 75 76
#define ITextServices_TxGetBaseLinePos(This,a) TXTSERV_VTABLE(This)->TxGetBaseLinePos(This,a)
#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)
#define ITextServices_TxGetDropTarget(This,a) TXTSERV_VTABLE(This)->TxGetDropTarget(This,a)
#define ITextServices_OnTxPropertyBitsChange(This,a,b) TXTSERV_VTABLE(This)->OnTxPropertyBitsChange(This,a,b)
#define ITextServices_TxGetCachedSize(This,a,b) TXTSERV_VTABLE(This)->TxGetCachedSize(This,a,b)

77 78 79 80 81 82 83 84 85
/* Set the WINETEST_DEBUG environment variable to be greater than 1 for verbose
 * function call traces of ITextHost. */
#define TRACECALL if(winetest_debug > 1) trace

/************************************************************************/
/* ITextHost implementation for conformance testing. */

typedef struct ITextHostTestImpl
{
86
    ITextHost ITextHost_iface;
87 88 89
    LONG refCount;
} ITextHostTestImpl;

90 91 92 93 94
static inline ITextHostTestImpl *impl_from_ITextHost(ITextHost *iface)
{
    return CONTAINING_RECORD(iface, ITextHostTestImpl, ITextHost_iface);
}

95 96 97 98
static HRESULT WINAPI ITextHostImpl_QueryInterface(ITextHost *iface,
                                                   REFIID riid,
                                                   LPVOID *ppvObject)
{
99
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
100

101
    if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, pIID_ITextHost)) {
102
        *ppvObject = &This->ITextHost_iface;
103 104 105 106 107 108 109 110 111
        ITextHost_AddRef((ITextHost *)*ppvObject);
        return S_OK;
    }

    return E_NOINTERFACE;
}

static ULONG WINAPI ITextHostImpl_AddRef(ITextHost *iface)
{
112
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
113 114 115 116 117 118
    ULONG refCount = InterlockedIncrement(&This->refCount);
    return refCount;
}

static ULONG WINAPI ITextHostImpl_Release(ITextHost *iface)
{
119
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
120 121 122 123 124 125 126 127 128 129 130 131 132
    ULONG refCount = InterlockedDecrement(&This->refCount);

    if (!refCount)
    {
        CoTaskMemFree(This);
        return 0;
    } else {
        return refCount;
    }
}

static HDC WINAPI ITextHostImpl_TxGetDC(ITextHost *iface)
{
133
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
134 135 136 137 138
    TRACECALL("Call to TxGetDC(%p)\n", This);
    return NULL;
}

static INT WINAPI ITextHostImpl_TxReleaseDC(ITextHost *iface,
139
                                            HDC hdc)
140
{
141
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
142 143 144 145 146
    TRACECALL("Call to TxReleaseDC(%p)\n", This);
    return 0;
}

static BOOL WINAPI ITextHostImpl_TxShowScrollBar(ITextHost *iface,
147 148
                                                 INT fnBar,
                                                 BOOL fShow)
149
{
150
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
151 152
    TRACECALL("Call to TxShowScrollBar(%p, fnBar=%d, fShow=%d)\n",
                This, fnBar, fShow);
153
    return FALSE;
154 155 156
}

static BOOL WINAPI ITextHostImpl_TxEnableScrollBar(ITextHost *iface,
157 158
                                                   INT fuSBFlags,
                                                   INT fuArrowflags)
159
{
160
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
161 162
    TRACECALL("Call to TxEnableScrollBar(%p, fuSBFlags=%d, fuArrowflags=%d)\n",
               This, fuSBFlags, fuArrowflags);
163
    return FALSE;
164 165 166
}

static BOOL WINAPI ITextHostImpl_TxSetScrollRange(ITextHost *iface,
167 168 169 170
                                                  INT fnBar,
                                                  LONG nMinPos,
                                                  INT nMaxPos,
                                                  BOOL fRedraw)
171
{
172
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
173 174
    TRACECALL("Call to TxSetScrollRange(%p, fnBar=%d, nMinPos=%d, nMaxPos=%d, fRedraw=%d)\n",
               This, fnBar, nMinPos, nMaxPos, fRedraw);
175
    return FALSE;
176 177 178
}

static BOOL WINAPI ITextHostImpl_TxSetScrollPos(ITextHost *iface,
179 180 181
                                                INT fnBar,
                                                INT nPos,
                                                BOOL fRedraw)
182
{
183
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
184 185
    TRACECALL("Call to TxSetScrollPos(%p, fnBar=%d, nPos=%d, fRedraw=%d)\n",
               This, fnBar, nPos, fRedraw);
186
    return FALSE;
187 188 189
}

static void WINAPI ITextHostImpl_TxInvalidateRect(ITextHost *iface,
190 191
                                                  LPCRECT prc,
                                                  BOOL fMode)
192
{
193
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
194 195 196 197 198 199
    TRACECALL("Call to TxInvalidateRect(%p, prc=%p, fMode=%d)\n",
               This, prc, fMode);
}

static void WINAPI ITextHostImpl_TxViewChange(ITextHost *iface, BOOL fUpdate)
{
200
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
201 202 203 204 205
    TRACECALL("Call to TxViewChange(%p, fUpdate=%d)\n",
               This, fUpdate);
}

static BOOL WINAPI ITextHostImpl_TxCreateCaret(ITextHost *iface,
206 207
                                               HBITMAP hbmp,
                                               INT xWidth, INT yHeight)
208
{
209
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
210 211
    TRACECALL("Call to TxCreateCaret(%p, nbmp=%p, xWidth=%d, yHeight=%d)\n",
               This, hbmp, xWidth, yHeight);
212
    return FALSE;
213 214 215 216
}

static BOOL WINAPI ITextHostImpl_TxShowCaret(ITextHost *iface, BOOL fShow)
{
217
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
218 219
    TRACECALL("Call to TxShowCaret(%p, fShow=%d)\n",
               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 236
    TRACECALL("Call to TxSetTimer(%p, idTimer=%u, uTimeout=%u)\n",
              This, idTimer, uTimeout);
237
    return FALSE;
238 239 240 241
}

static void WINAPI ITextHostImpl_TxKillTimer(ITextHost *iface, UINT idTimer)
{
242
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
243 244 245 246
    TRACECALL("Call to TxKillTimer(%p, idTimer=%u)\n", This, idTimer);
}

static void WINAPI ITextHostImpl_TxScrollWindowEx(ITextHost *iface,
247 248 249 250 251 252
                                                  INT dx, INT dy,
                                                  LPCRECT lprcScroll,
                                                  LPCRECT lprcClip,
                                                  HRGN hRgnUpdate,
                                                  LPRECT lprcUpdate,
                                                  UINT fuScroll)
253
{
254
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
255 256 257 258 259 260
    TRACECALL("Call to TxScrollWindowEx(%p, %d, %d, %p, %p, %p, %p, %d)\n",
              This, dx, dy, lprcScroll, lprcClip, hRgnUpdate, lprcUpdate, fuScroll);
}

static void WINAPI ITextHostImpl_TxSetCapture(ITextHost *iface, BOOL fCapture)
{
261
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
262 263 264 265 266
    TRACECALL("Call to TxSetCapture(%p, fCapture=%d)\n", This, fCapture);
}

static void WINAPI ITextHostImpl_TxSetFocus(ITextHost *iface)
{
267
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
268 269 270 271
    TRACECALL("Call to TxSetFocus(%p)\n", This);
}

static void WINAPI ITextHostImpl_TxSetCursor(ITextHost *iface,
272 273
                                             HCURSOR hcur,
                                             BOOL fText)
274
{
275
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
276 277 278 279 280
    TRACECALL("Call to TxSetCursor(%p, hcur=%p, fText=%d)\n",
              This, hcur, fText);
}

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 301 302 303 304
    TRACECALL("Call to TxActivate(%p, plOldState=%p)\n", This, plOldState);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_TxDeactivate(ITextHost *iface,
305
                                                 LONG lNewState)
306
{
307
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
308 309 310 311 312
    TRACECALL("Call to TxDeactivate(%p, lNewState=%d)\n", This, lNewState);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_TxGetClientRect(ITextHost *iface,
313
                                                    LPRECT prc)
314
{
315
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
316 317 318 319 320
    TRACECALL("Call to TxGetClientRect(%p, prc=%p)\n", This, prc);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_TxGetViewInset(ITextHost *iface,
321
                                                   LPRECT prc)
322
{
323
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
324 325 326 327 328
    TRACECALL("Call to TxGetViewInset(%p, prc=%p)\n", This, prc);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_TxGetCharFormat(ITextHost *iface,
329
                                                    const CHARFORMATW **ppCF)
330
{
331
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
332 333 334 335 336
    TRACECALL("Call to TxGetCharFormat(%p, ppCF=%p)\n", This, ppCF);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_TxGetParaFormat(ITextHost *iface,
337
                                                    const PARAFORMAT **ppPF)
338
{
339
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
340 341 342 343 344
    TRACECALL("Call to TxGetParaFormat(%p, ppPF=%p)\n", This, ppPF);
    return E_NOTIMPL;
}

static COLORREF WINAPI ITextHostImpl_TxGetSysColor(ITextHost *iface,
345
                                                   int nIndex)
346
{
347
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
348 349 350 351 352
    TRACECALL("Call to TxGetSysColor(%p, nIndex=%d)\n", This, nIndex);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_TxGetBackStyle(ITextHost *iface,
353
                                                   TXTBACKSTYLE *pStyle)
354
{
355
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
356 357 358 359 360
    TRACECALL("Call to TxGetBackStyle(%p, pStyle=%p)\n", This, pStyle);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_TxGetMaxLength(ITextHost *iface,
361
                                                   DWORD *pLength)
362
{
363
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
364 365 366 367
    TRACECALL("Call to TxGetMaxLength(%p, pLength=%p)\n", This, pLength);
    return E_NOTIMPL;
}

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 374 375 376 377
               This, pdwScrollBar);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_TxGetPasswordChar(ITextHost *iface,
378
                                                      WCHAR *pch)
379
{
380
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
381 382 383 384 385
    TRACECALL("Call to TxGetPasswordChar(%p, pch=%p)\n", This, pch);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_TxGetAcceleratorPos(ITextHost *iface,
386
                                                        LONG *pch)
387
{
388
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
389 390 391 392 393
    TRACECALL("Call to TxGetAcceleratorPos(%p, pch=%p)\n", This, pch);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_TxGetExtent(ITextHost *iface,
394
                                                LPSIZEL lpExtent)
395
{
396
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
397 398 399 400 401
    TRACECALL("Call to TxGetExtent(%p, lpExtent=%p)\n", This, lpExtent);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_OnTxCharFormatChange(ITextHost *iface,
402
                                                         const CHARFORMATW *pcf)
403
{
404
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
405 406 407 408 409
    TRACECALL("Call to OnTxCharFormatChange(%p, pcf=%p)\n", This, pcf);
    return E_NOTIMPL;
}

static HRESULT WINAPI ITextHostImpl_OnTxParaFormatChange(ITextHost *iface,
410
                                                         const PARAFORMAT *ppf)
411
{
412
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
413 414 415 416 417 418 419
    TRACECALL("Call to OnTxParaFormatChange(%p, ppf=%p)\n", This, ppf);
    return E_NOTIMPL;
}

/* This must return S_OK for the native ITextServices object to
   initialize. */
static HRESULT WINAPI ITextHostImpl_TxGetPropertyBits(ITextHost *iface,
420 421
                                                      DWORD dwMask,
                                                      DWORD *pdwBits)
422
{
423
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
424 425 426 427 428 429 430
    TRACECALL("Call to TxGetPropertyBits(%p, dwMask=0x%08x, pdwBits=%p)\n",
              This, dwMask, pdwBits);
    *pdwBits = 0;
    return S_OK;
}

static HRESULT WINAPI ITextHostImpl_TxNotify(ITextHost *iface, DWORD iNotify,
431
                                             void *pv)
432
{
433
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
434 435 436 437 438 439
    TRACECALL("Call to TxNotify(%p, iNotify=%d, pv=%p)\n", This, iNotify, pv);
    return E_NOTIMPL;
}

static HIMC WINAPI ITextHostImpl_TxImmGetContext(ITextHost *iface)
{
440
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
441 442 443 444 445 446
    TRACECALL("Call to TxImmGetContext(%p)\n", This);
    return 0;
}

static void WINAPI ITextHostImpl_TxImmReleaseContext(ITextHost *iface, HIMC himc)
{
447
    ITextHostTestImpl *This = impl_from_ITextHost(iface);
448 449 450
    TRACECALL("Call to TxImmReleaseContext(%p, himc=%p)\n", This, himc);
}

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 459
    TRACECALL("Call to TxGetSelectionBarWidth(%p, lSelBarWidth=%p)\n",
                This, lSelBarWidth);
460
    *lSelBarWidth = 0;
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    return E_NOTIMPL;
}

static ITextHostVtbl itextHostVtbl = {
    ITextHostImpl_QueryInterface,
    ITextHostImpl_AddRef,
    ITextHostImpl_Release,
    ITextHostImpl_TxGetDC,
    ITextHostImpl_TxReleaseDC,
    ITextHostImpl_TxShowScrollBar,
    ITextHostImpl_TxEnableScrollBar,
    ITextHostImpl_TxSetScrollRange,
    ITextHostImpl_TxSetScrollPos,
    ITextHostImpl_TxInvalidateRect,
    ITextHostImpl_TxViewChange,
    ITextHostImpl_TxCreateCaret,
    ITextHostImpl_TxShowCaret,
    ITextHostImpl_TxSetCaretPos,
    ITextHostImpl_TxSetTimer,
    ITextHostImpl_TxKillTimer,
    ITextHostImpl_TxScrollWindowEx,
    ITextHostImpl_TxSetCapture,
    ITextHostImpl_TxSetFocus,
    ITextHostImpl_TxSetCursor,
    ITextHostImpl_TxScreenToClient,
    ITextHostImpl_TxClientToScreen,
    ITextHostImpl_TxActivate,
    ITextHostImpl_TxDeactivate,
    ITextHostImpl_TxGetClientRect,
    ITextHostImpl_TxGetViewInset,
    ITextHostImpl_TxGetCharFormat,
    ITextHostImpl_TxGetParaFormat,
    ITextHostImpl_TxGetSysColor,
    ITextHostImpl_TxGetBackStyle,
    ITextHostImpl_TxGetMaxLength,
496
    ITextHostImpl_TxGetScrollBars,
497 498 499 500 501 502 503 504 505 506 507 508 509 510
    ITextHostImpl_TxGetPasswordChar,
    ITextHostImpl_TxGetAcceleratorPos,
    ITextHostImpl_TxGetExtent,
    ITextHostImpl_OnTxCharFormatChange,
    ITextHostImpl_OnTxParaFormatChange,
    ITextHostImpl_TxGetPropertyBits,
    ITextHostImpl_TxNotify,
    ITextHostImpl_TxImmGetContext,
    ITextHostImpl_TxImmReleaseContext,
    ITextHostImpl_TxGetSelectionBarWidth
};

static void *wrapperCodeMem = NULL;

511 512
#include "pshpack1.h"

513 514 515 516 517 518 519 520 521 522
/* Code structure for x86 byte code */
typedef struct
{
    BYTE pop_eax;  /* popl  %eax  */
    BYTE push_ecx; /* pushl %ecx  */
    BYTE push_eax; /* pushl %eax  */
    BYTE jmp_func; /* jmp   $func */
    DWORD func;
} THISCALL_TO_STDCALL_THUNK;

523 524 525 526 527 528 529 530 531 532 533
typedef struct
{
    BYTE pop_eax;               /* popl  %eax */
    BYTE pop_ecx;               /* popl  %ecx */
    BYTE push_eax;              /* pushl %eax */
    BYTE mov_vtable_eax[2];     /* movl (%ecx), %eax */
    BYTE jmp_eax[2];            /* jmp *$vtablefunc_offset(%eax) */
    int  vtablefunc_offset;
} STDCALL_TO_THISCALL_THUNK;

#include "poppack.h"
534 535 536 537 538 539 540

static void setup_thiscall_wrappers(void)
{
#ifdef __i386__
    void** pVtable;
    void** pVtableEnd;
    THISCALL_TO_STDCALL_THUNK *thunk;
541
    STDCALL_TO_THISCALL_THUNK *thunk2;
542 543 544

    wrapperCodeMem = VirtualAlloc(NULL,
                                  (sizeof(ITextHostVtbl)/sizeof(void*) - 3)
545 546 547
                                    * sizeof(THISCALL_TO_STDCALL_THUNK)
                                  +(sizeof(ITextServicesVtbl)/sizeof(void*) - 3)
                                    * sizeof(STDCALL_TO_THISCALL_THUNK),
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
                                  MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    thunk = wrapperCodeMem;

    /* Wrap all ITextHostImpl methods with code to perform a thiscall to
     * stdcall conversion. The thiscall calling convention places the This
     * pointer in ecx on the x86 platform, and the stdcall calling convention
     * pushes the This pointer on the stack as the first argument.
     *
     * The byte code does the conversion then jumps to the real function.
     *
     * Each wrapper needs to be modified so that the function to jump to is
     * modified in the byte code. */

    /* Skip QueryInterface, AddRef, and Release native actually
     * defined them with the stdcall calling convention. */
    pVtable = (void**)&itextHostVtbl + 3;
564
    pVtableEnd = (void**)(&itextHostVtbl + 1);
565 566 567 568 569 570 571 572 573 574 575 576
    while (pVtable != pVtableEnd) {
        /* write byte code to executable memory */
        thunk->pop_eax = 0x58;  /* popl  %eax  */
        thunk->push_ecx = 0x51; /* pushl %ecx  */
        thunk->push_eax = 0x50; /* pushl %eax  */
        thunk->jmp_func = 0xe9; /* jmp   $func */
        /* The address needs to be relative to the end of the jump instructions. */
        thunk->func = (char*)*pVtable - (char*)(&thunk->func + 1);
        *pVtable = thunk;
        pVtable++;
        thunk++;
    }
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599

    /* Setup an ITextServices standard call vtable that will call the
     * native thiscall vtable when the methods are called. */

    /* QueryInterface, AddRef, and Release should be called directly on the
     * real vtable since they use the stdcall calling convention. */
    thunk2 = (STDCALL_TO_THISCALL_THUNK *)thunk;
    pVtable = (void**)&itextServicesStdcallVtbl + 3;
    pVtableEnd = (void**)(&itextServicesStdcallVtbl + 1);
    while (pVtable != pVtableEnd) {
        /* write byte code to executable memory */
        thunk2->pop_eax = 0x58;               /* popl  %eax */
        thunk2->pop_ecx = 0x59;               /* popl  %ecx */
        thunk2->push_eax = 0x50;              /* pushl %eax */
        thunk2->mov_vtable_eax[0] = 0x8b;     /* movl (%ecx), %eax */
        thunk2->mov_vtable_eax[1] = 0x01;
        thunk2->jmp_eax[0] = 0xff;            /* jmp *$vtablefunc_offset(%eax) */
        thunk2->jmp_eax[1] = 0xa0;
        thunk2->vtablefunc_offset = (char*)pVtable - (char*)&itextServicesStdcallVtbl;
        *pVtable = thunk2;
        pVtable++;
        thunk2++;
    }
600 601 602 603 604 605 606
#endif /* __i386__ */
}

/*************************************************************************/
/* Conformance test functions. */

/* Initialize the test texthost structure */
607
static BOOL init_texthost(ITextServices **txtserv, ITextHost **ret)
608
{
609
    ITextHostTestImpl *dummyTextHost;
610 611 612 613 614
    IUnknown *init;
    HRESULT result;

    dummyTextHost = CoTaskMemAlloc(sizeof(*dummyTextHost));
    if (dummyTextHost == NULL) {
615
        win_skip("Insufficient memory to create ITextHost interface\n");
616 617
        return FALSE;
    }
618
    dummyTextHost->ITextHost_iface.lpVtbl = &itextHostVtbl;
619 620 621 622 623
    dummyTextHost->refCount = 1;

    /* MSDN states that an IUnknown object is returned by
       CreateTextServices which is then queried to obtain a
       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 627
    if (result != S_OK) {
        CoTaskMemFree(dummyTextHost);
628
        win_skip("CreateTextServices failed.\n");
629 630 631
        return FALSE;
    }

632 633
    result = IUnknown_QueryInterface(init, pIID_ITextServices, (void**)txtserv);
    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 639 640
        return FALSE;
    }

641
    *ret = &dummyTextHost->ITextHost_iface;
642 643 644
    return TRUE;
}

645 646
static void test_TxGetText(void)
{
647 648
    ITextServices *txtserv;
    ITextHost *host;
649 650 651
    HRESULT hres;
    BSTR rettext;

652
    if (!init_texthost(&txtserv, &host))
653 654 655
        return;

    hres = ITextServices_TxGetText(txtserv, &rettext);
656
    ok(hres == S_OK, "ITextServices_TxGetText failed (result = %x)\n", hres);
657

658 659
    ITextServices_Release(txtserv);
    ITextHost_Release(host);
660 661
}

662 663
static void test_TxSetText(void)
{
664 665
    ITextServices *txtserv;
    ITextHost *host;
666 667 668 669
    HRESULT hres;
    BSTR rettext;
    WCHAR settext[] = {'T','e','s','t',0};

670
    if (!init_texthost(&txtserv, &host))
671 672 673
        return;

    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 683
                 "String returned differs\n");

684 685 686 687 688 689 690 691 692 693
    /* Null-pointer should behave the same as empty-string */

    hres = ITextServices_TxSetText(txtserv, 0);
    ok(hres == S_OK, "ITextServices_TxSetText failed (result = %x)\n", hres);

    hres = ITextServices_TxGetText(txtserv, &rettext);
    ok(hres == S_OK, "ITextServices_TxGetText failed (result = %x)\n", hres);
    ok(SysStringLen(rettext) == 0,
                 "String returned of wrong length (expected 0, got %d)\n", SysStringLen(rettext));

694
    SysFreeString(rettext);
695 696
    ITextServices_Release(txtserv);
    ITextHost_Release(host);
697 698
}

699 700 701 702
static void test_TxGetNaturalSize(void)
{
    ITextServices *txtserv;
    ITextHost *host;
703
    HRESULT result;
704
    BOOL ret;
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722

    /* This value is used when calling TxGetNaturalSize.  MSDN says
       that this is not supported however a null pointer cannot be
       used as it will cause a segmentation violation.  The values in
       the structure being pointed to are required to be INT_MAX
       otherwise calculations can give wrong values. */
    const SIZEL psizelExtent = {INT_MAX,INT_MAX};

    static const WCHAR oneA[] = {'A',0};

    /* Results of measurements */
    LONG xdim, ydim;

    /* The device context to do the tests in */
    HDC hdcDraw;

    /* Variables with the text metric information */
    INT charwidth_caps_text[26];
723
    TEXTMETRICA tmInfo_text;
724

725
    if (!init_texthost(&txtserv, &host))
726 727 728 729 730 731 732
        return;

    hdcDraw = GetDC(NULL);
    SaveDC(hdcDraw);

    /* Populate the metric strucs */
    SetMapMode(hdcDraw,MM_TEXT);
733
    GetTextMetricsA(hdcDraw, &tmInfo_text);
734
    SetLastError(0xdeadbeef);
735
    ret = GetCharWidth32A(hdcDraw,'A','Z',charwidth_caps_text);
736 737
    if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
        win_skip("GetCharWidth32 is not available\n");
738
        goto cleanup;
739
    }
740 741 742 743 744 745

    /* Make measurements in MM_TEXT */
    SetMapMode(hdcDraw,MM_TEXT);
    xdim = 0; ydim = 0;

    result = ITextServices_TxSetText(txtserv, oneA);
746 747 748 749 750
    ok(result == S_OK, "ITextServices_TxSetText failed (result = %x)\n", result);
    if (result != S_OK) {
        skip("Could not set text\n");
        goto cleanup;
    }
751

752
    SetLastError(0xdeadbeef);
753 754 755 756
    result = ITextServices_TxGetNaturalSize(txtserv, DVASPECT_CONTENT,
                                            hdcDraw, NULL, NULL,
                                            TXTNS_FITTOCONTENT, &psizelExtent,
                                            &xdim, &ydim);
757 758 759
    todo_wine ok(result == S_OK || broken(result == E_FAIL), /* WINXP Arabic Language */
        "TxGetNaturalSize gave unexpected return value (result = %x)\n", result);
    if (result == S_OK) {
760 761 762 763 764 765 766
    todo_wine ok(ydim == tmInfo_text.tmHeight,
                 "Height calculated incorrectly (expected %d, got %d)\n",
                 tmInfo_text.tmHeight, ydim);
    /* The native DLL adds one pixel extra when calculating widths. */
    todo_wine ok(xdim >= charwidth_caps_text[0] && xdim <= charwidth_caps_text[0] + 1,
                 "Width calculated incorrectly (expected %d {+1}, got %d)\n",
                 charwidth_caps_text[0], xdim);
767 768 769
    } else
        skip("TxGetNaturalSize measurements not performed (xdim = %d, ydim = %d, result = %x, error = %x)\n",
             xdim, ydim, result, GetLastError());
770

771
cleanup:
772 773
    RestoreDC(hdcDraw,1);
    ReleaseDC(NULL,hdcDraw);
774 775
    ITextServices_Release(txtserv);
    ITextHost_Release(host);
776 777
}

778 779
static void test_TxDraw(void)
{
780 781
    ITextServices *txtserv;
    ITextHost *host;
782 783 784 785 786 787 788 789
    HDC tmphdc = GetDC(NULL);
    DWORD dwAspect = DVASPECT_CONTENT;
    HDC hicTargetDev = NULL; /* Means "default" device */
    DVTARGETDEVICE *ptd = NULL;
    void *pvAspect = NULL;
    HRESULT result;
    RECTL client = {0,0,100,100};

790 791

    if (!init_texthost(&txtserv, &host))
792 793 794 795 796 797
        return;

    todo_wine {
        result = ITextServices_TxDraw(txtserv, dwAspect, 0, pvAspect, ptd,
                                      tmphdc, hicTargetDev, &client, NULL,
                                      NULL, NULL, 0, 0);
798
        ok(result == S_OK, "TxDraw failed (result = %x)\n", result);
799 800
    }

801 802
    ITextServices_Release(txtserv);
    ITextHost_Release(host);
803 804
}

805 806 807 808 809 810 811
DEFINE_GUID(expected_iid_itextservices, 0x8d33f740, 0xcf58, 0x11ce, 0xa8, 0x9d, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5);
DEFINE_GUID(expected_iid_itexthost, 0x13e670f4,0x1a5a,0x11cf,0xab,0xeb,0x00,0xaa,0x00,0xb6,0x5e,0xa1);
DEFINE_GUID(expected_iid_itexthost2, 0x13e670f5,0x1a5a,0x11cf,0xab,0xeb,0x00,0xaa,0x00,0xb6,0x5e,0xa1);

static void test_IIDs(void)
{
    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 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
/* Outer IUnknown for COM aggregation tests */
struct unk_impl {
    IUnknown IUnknown_iface;
    LONG ref;
    IUnknown *inner_unk;
};

static inline struct unk_impl *impl_from_IUnknown(IUnknown *iface)
{
    return CONTAINING_RECORD(iface, struct unk_impl, IUnknown_iface);
}

static HRESULT WINAPI unk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
{
    struct unk_impl *This = impl_from_IUnknown(iface);

    return IUnknown_QueryInterface(This->inner_unk, riid, ppv);
}

static ULONG WINAPI unk_AddRef(IUnknown *iface)
{
    struct unk_impl *This = impl_from_IUnknown(iface);

    return InterlockedIncrement(&This->ref);
}

static ULONG WINAPI unk_Release(IUnknown *iface)
{
    struct unk_impl *This = impl_from_IUnknown(iface);

    return InterlockedDecrement(&This->ref);
}

static const IUnknownVtbl unk_vtbl =
{
    unk_QueryInterface,
    unk_AddRef,
    unk_Release
};

static void test_COM(void)
{
    struct unk_impl unk_obj = {{&unk_vtbl}, 19, NULL};
    struct ITextHostTestImpl texthost = {{&itextHostVtbl}, 1};
    ITextServices *textsrv;
    ULONG refcount;
    HRESULT hr;

    /* COM aggregation */
    hr = pCreateTextServices(&unk_obj.IUnknown_iface, &texthost.ITextHost_iface,
                             &unk_obj.inner_unk);
    ok(hr == S_OK, "CreateTextServices failed: %08x\n", hr);
    hr = IUnknown_QueryInterface(unk_obj.inner_unk, pIID_ITextServices, (void**)&textsrv);
    ok(hr == S_OK, "QueryInterface for IID_ITextServices failed: %08x\n", hr);
    refcount = ITextServices_AddRef(textsrv);
    ok(refcount == unk_obj.ref, "CreateTextServices just pretends to support COM aggregation\n");
    refcount = ITextServices_Release(textsrv);
    ok(refcount == unk_obj.ref, "CreateTextServices just pretends to support COM aggregation\n");
    refcount = ITextServices_Release(textsrv);
    ok(refcount == 19, "Refcount should be back at 19 but is %u\n", refcount);

    IUnknown_Release(unk_obj.inner_unk);
}
882

883 884 885 886 887 888 889 890
static ULONG get_refcount(IUnknown *iface)
{
    IUnknown_AddRef(iface);
    return IUnknown_Release(iface);
}

static void test_QueryInterface(void)
{
891 892
    ITextServices *txtserv;
    ITextHost *host;
893 894 895 896 897
    HRESULT hres;
    IRichEditOle *reole, *txtsrv_reole;
    ITextDocument *txtdoc, *txtsrv_txtdoc;
    ULONG refcount;

898
    if(!init_texthost(&txtserv, &host))
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
        return;

    refcount = get_refcount((IUnknown *)txtserv);
    ok(refcount == 1, "got wrong ref count: %d\n", refcount);

    /* IID_IRichEditOle */
    hres = ITextServices_QueryInterface(txtserv, &IID_IRichEditOle, (void **)&txtsrv_reole);
    ok(hres == S_OK, "ITextServices_QueryInterface: 0x%08x\n", hres);
    refcount = get_refcount((IUnknown *)txtserv);
    ok(refcount == 2, "got wrong ref count: %d\n", refcount);
    refcount = get_refcount((IUnknown *)txtsrv_reole);
    ok(refcount == 2, "got wrong ref count: %d\n", refcount);

    hres = IRichEditOle_QueryInterface(txtsrv_reole, &IID_ITextDocument, (void **)&txtdoc);
    ok(hres == S_OK, "IRichEditOle_QueryInterface: 0x%08x\n", hres);
    refcount = get_refcount((IUnknown *)txtserv);
    ok(refcount == 3, "got wrong ref count: %d\n", refcount);
    refcount = get_refcount((IUnknown *)txtsrv_reole);
    ok(refcount == 3, "got wrong ref count: %d\n", refcount);

    ITextDocument_Release(txtdoc);
    refcount = get_refcount((IUnknown *)txtserv);
    ok(refcount == 2, "got wrong ref count: %d\n", refcount);
    IRichEditOle_Release(txtsrv_reole);
    refcount = get_refcount((IUnknown *)txtserv);
    ok(refcount == 1, "got wrong ref count: %d\n", refcount);

    /* IID_ITextDocument */
    hres = ITextServices_QueryInterface(txtserv, &IID_ITextDocument, (void **)&txtsrv_txtdoc);
    ok(hres == S_OK, "ITextServices_QueryInterface: 0x%08x\n", hres);
    refcount = get_refcount((IUnknown *)txtserv);
    ok(refcount == 2, "got wrong ref count: %d\n", refcount);
    refcount = get_refcount((IUnknown *)txtsrv_txtdoc);
    ok(refcount == 2, "got wrong ref count: %d\n", refcount);

    hres = ITextDocument_QueryInterface(txtsrv_txtdoc, &IID_IRichEditOle, (void **)&reole);
    ok(hres == S_OK, "ITextDocument_QueryInterface: 0x%08x\n", hres);
    refcount = get_refcount((IUnknown *)txtserv);
    ok(refcount == 3, "got wrong ref count: %d\n", refcount);
    refcount = get_refcount((IUnknown *)txtsrv_txtdoc);
    ok(refcount == 3, "got wrong ref count: %d\n", refcount);

    IRichEditOle_Release(reole);
    refcount = get_refcount((IUnknown *)txtserv);
    ok(refcount == 2, "got wrong ref count: %d\n", refcount);
    ITextDocument_Release(txtsrv_txtdoc);
    refcount = get_refcount((IUnknown *)txtserv);
    ok(refcount == 1, "got wrong ref count: %d\n", refcount);

948 949
    ITextServices_Release(txtserv);
    ITextHost_Release(host);
950 951
}

952 953
START_TEST( txtsrv )
{
954 955 956
    ITextServices *txtserv;
    ITextHost *host;

957 958 959 960
    setup_thiscall_wrappers();

    /* Must explicitly LoadLibrary(). The test has no references to functions in
     * RICHED20.DLL, so the linker doesn't actually link to it. */
961
    hmoduleRichEdit = LoadLibraryA("riched20.dll");
962 963
    ok(hmoduleRichEdit != NULL, "error: %d\n", (int) GetLastError());

964 965 966
    pIID_ITextServices = (IID*)GetProcAddress(hmoduleRichEdit, "IID_ITextServices");
    pIID_ITextHost = (IID*)GetProcAddress(hmoduleRichEdit, "IID_ITextHost");
    pIID_ITextHost2 = (IID*)GetProcAddress(hmoduleRichEdit, "IID_ITextHost2");
967 968
    pCreateTextServices = (void*)GetProcAddress(hmoduleRichEdit, "CreateTextServices");

969
    test_IIDs();
970
    test_COM();
971

972
    if (init_texthost(&txtserv, &host))
973
    {
974 975
        ITextServices_Release(txtserv);
        ITextHost_Release(host);
976 977

        test_TxGetText();
978
        test_TxSetText();
979
        test_TxGetNaturalSize();
980
        test_TxDraw();
981
        test_QueryInterface();
982 983 984
    }
    if (wrapperCodeMem) VirtualFree(wrapperCodeMem, 0, MEM_RELEASE);
}