text.c 31.9 KB
Newer Older
1 2 3 4
/*
 * DrawText tests
 *
 * Copyright (c) 2004 Zach Gorman
5
 * Copyright 2007 Dmitry Timoshkov
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23 24 25 26 27 28 29
 */

#include <assert.h>

#include "wine/test.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winerror.h"

30 31 32
#define MODIFIED(rect) (rect.left = 10 && rect.right != 100 && rect.top == 10 && rect.bottom != 100)
#define SAME(rect) (rect.left = 10 && rect.right == 100 && rect.top == 10 && rect.bottom == 100)
#define EMPTY(rect) (rect.left == rect.right && rect.bottom == rect.top)
33 34 35 36 37 38 39

static void test_DrawTextCalcRect(void)
{
    HWND hwnd;
    HDC hdc;
    HFONT hFont, hOldFont;
    LOGFONTA lf;
40
    static CHAR text[] = "Example text for testing DrawText in "
41
      "MM_HIENGLISH mode";
42
    static WCHAR textW[] = {'W','i','d','e',' ','c','h','a','r',' ',
43
        's','t','r','i','n','g','\0'};
44 45
    static CHAR emptystring[] = "";
    static WCHAR emptystringW[] = { 0 };
46
    INT textlen, textheight, heightcheck;
47
    RECT rect = { 0, 0, 100, 0 };
48
    BOOL ret;
49 50
    DRAWTEXTPARAMS dtp;
    BOOL conform_xp = TRUE;
51 52 53 54

    /* Initialization */
    hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
                           0, 0, 200, 200, 0, 0, 0, NULL);
55
    ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
56
    hdc = GetDC(hwnd);
57
    ok(hdc != 0, "GetDC error %u\n", GetLastError());
58
    trace("hdc %p\n", hdc);
59
    textlen = lstrlenA(text);
60 61 62 63 64 65 66 67 68 69 70 71 72 73

    /* LOGFONT initialization */
    memset(&lf, 0, sizeof(lf));
    lf.lfCharSet = ANSI_CHARSET;
    lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
    lf.lfWeight = FW_DONTCARE;
    lf.lfHeight = 0; /* mapping mode dependent */
    lf.lfQuality = DEFAULT_QUALITY;
    lstrcpyA(lf.lfFaceName, "Arial");

    /* DrawText in MM_HIENGLISH with DT_CALCRECT */
    SetMapMode(hdc, MM_HIENGLISH);
    lf.lfHeight = 100 * 9 / 72; /* 9 point */
    hFont = CreateFontIndirectA(&lf);
74
    ok(hFont != 0, "CreateFontIndirectA error %u\n",
75 76 77
       GetLastError());
    hOldFont = SelectObject(hdc, hFont);

78
    textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
79
       DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
80
       DT_NOPREFIX);
81
    ok( textheight, "DrawTextA error %u\n", GetLastError());
82

83
    trace("MM_HIENGLISH rect.bottom %d\n", rect.bottom);
84 85
    todo_wine ok(rect.bottom < 0, "In MM_HIENGLISH, DrawText with "
       "DT_CALCRECT should return a negative rectangle bottom. "
86
       "(bot=%d)\n", rect.bottom);
87 88

    SelectObject(hdc, hOldFont);
89
    ret = DeleteObject(hFont);
90
    ok( ret, "DeleteObject error %u\n", GetLastError());
91 92 93 94 95 96 97


    /* DrawText in MM_TEXT with DT_CALCRECT */
    SetMapMode(hdc, MM_TEXT);
    lf.lfHeight = -MulDiv(9, GetDeviceCaps(hdc,
       LOGPIXELSY), 72); /* 9 point */
    hFont = CreateFontIndirectA(&lf);
98
    ok(hFont != 0, "CreateFontIndirectA error %u\n",
99 100 101
       GetLastError());
    hOldFont = SelectObject(hdc, hFont);

102
    textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
103
       DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
104
       DT_NOPREFIX);
105
    ok( textheight, "DrawTextA error %u\n", GetLastError());
106

107
    trace("MM_TEXT rect.bottom %d\n", rect.bottom);
108
    ok(rect.bottom > 0, "In MM_TEXT, DrawText with DT_CALCRECT "
109
       "should return a positive rectangle bottom. (bot=%d)\n",
110 111
       rect.bottom);

112
    /* empty or null text should in some cases calc an empty rectangle */
113

114
    SetRect( &rect, 10,10, 100, 100);
115 116
    heightcheck = textheight = DrawTextExA(hdc, text, 0, &rect, DT_CALCRECT, NULL );
    ok( !EMPTY(rect) && !MODIFIED(rect),
117
        "rectangle should NOT be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
118 119
    if (textheight != 0)  /* Windows 98 */
    {
120
        win_skip("XP conformity failed, skipping XP tests. Probably win9x\n");
121 122 123 124 125 126 127 128 129 130 131 132 133 134
        conform_xp = FALSE;
    }
    else
        ok(textheight==0,"Got textheight from DrawTextExA\n");

    SetRect( &rect, 10,10, 100, 100);
    textheight = DrawTextA(hdc, text, 0, &rect, DT_CALCRECT);
    ok( !EMPTY(rect) && !MODIFIED(rect),
        "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
        rect.left, rect.top, rect.right, rect.bottom );
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

135 136
    SetRect( &rect, 10,10, 100, 100);
    SetLastError( 0);
137 138
    heightcheck = textheight = DrawTextExA(hdc, emptystring, -1, &rect, DT_CALCRECT, NULL );
    ok( EMPTY(rect),
139
        "rectangle should be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
140 141 142 143 144 145 146 147 148 149
    ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");

    SetRect( &rect, 10,10, 100, 100);
    textheight = DrawTextA(hdc, emptystring, -1, &rect, DT_CALCRECT);
    ok( EMPTY(rect),
        "rectangle should be empty got %d,%d-%d,%d\n",
        rect.left, rect.top, rect.right, rect.bottom );
    ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

150 151
    SetRect( &rect, 10,10, 100, 100);
    SetLastError( 0);
152 153 154 155 156 157
    heightcheck = textheight = DrawTextExA(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
    ok( EMPTY(rect) || !MODIFIED(rect),
        "rectangle should be empty or not modified got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
    if (!textheight) /* Windows NT 4 */
    {
        if (conform_xp)
158
            win_skip("XP conformity failed, skipping XP tests. Probably winNT\n");
159 160 161 162 163
        conform_xp = FALSE;
    }
    else
        ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");

164
    SetRect( &rect, 10,10, 100, 100);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    textheight = DrawTextA(hdc, NULL, -1, &rect, DT_CALCRECT);
    ok( EMPTY(rect) || !MODIFIED(rect),
        "rectangle should be empty or NOT modified got %d,%d-%d,%d\n",
        rect.left, rect.top, rect.right, rect.bottom );
    if (conform_xp)
        ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

    SetRect( &rect, 10,10, 100, 100);
    heightcheck = textheight = DrawTextExA(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
    ok( !EMPTY(rect) && !MODIFIED(rect),
        "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextExA\n");

    SetRect( &rect, 10,10, 100, 100);
    textheight = DrawTextA(hdc, NULL, 0, &rect, DT_CALCRECT);
    ok( !EMPTY(rect) && !MODIFIED(rect),
        "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
        rect.left, rect.top, rect.right, rect.bottom );
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

    /* DT_SINGLELINE tests */

    SetRect( &rect, 10,10, 100, 100);
    heightcheck = textheight = DrawTextExA(hdc, text, 0, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
    ok( !EMPTY(rect) && !MODIFIED(rect),
194
        "rectangle should NOT be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextExA\n");

    SetRect( &rect, 10,10, 100, 100);
    textheight = DrawTextA(hdc, text, 0, &rect, DT_CALCRECT|DT_SINGLELINE);
    ok( !EMPTY(rect) && !MODIFIED(rect),
        "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
        rect.left, rect.top, rect.right, rect.bottom );
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

    SetRect( &rect, 10,10, 100, 100);
    SetLastError( 0);
    heightcheck = textheight = DrawTextExA(hdc, emptystring, -1, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
    ok( !EMPTY(rect) && MODIFIED(rect),
        "rectangle should be modified got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
    ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");

    SetRect( &rect, 10,10, 100, 100);
    textheight = DrawTextA(hdc, emptystring, -1, &rect, DT_CALCRECT|DT_SINGLELINE);
    ok( !EMPTY(rect) && MODIFIED (rect),
        "rectangle should be modified got %d,%d-%d,%d\n",
        rect.left, rect.top, rect.right, rect.bottom );
    ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

    SetRect( &rect, 10,10, 100, 100);
    SetLastError( 0);
    heightcheck = textheight = DrawTextExA(hdc, NULL, -1, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
    ok( (!EMPTY(rect) && MODIFIED(rect)) || !MODIFIED(rect),
        "rectangle should be modified got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
    if (conform_xp)
        ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");

    SetRect( &rect, 10,10, 100, 100);
    textheight = DrawTextA(hdc, NULL, -1, &rect, DT_CALCRECT|DT_SINGLELINE);
    ok( (!EMPTY(rect) && MODIFIED(rect)) || !MODIFIED(rect),
        "rectangle should be modified got %d,%d-%d,%d\n",
        rect.left, rect.top, rect.right, rect.bottom );
    if (conform_xp)
        ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

    SetRect( &rect, 10,10, 100, 100);
    heightcheck = textheight = DrawTextExA(hdc, NULL, 0, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
    ok( !EMPTY(rect) && !MODIFIED(rect),
        "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextExA\n");

    SetRect( &rect, 10,10, 100, 100);
    textheight = DrawTextA(hdc, NULL, 0, &rect, DT_CALCRECT|DT_SINGLELINE);
    ok( !EMPTY(rect) && !MODIFIED(rect),
        "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
        rect.left, rect.top, rect.right, rect.bottom );
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

    /* further tests with  0 count, NULL and empty strings */
    heightcheck = textheight = DrawTextA(hdc, text, 0, &rect, 0);
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextA\n");
    textheight = DrawTextExA(hdc, text, 0, &rect, 0, NULL );
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextExA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
    heightcheck = textheight = DrawTextA(hdc, emptystring, 0, &rect, 0);
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextA\n");
    textheight = DrawTextExA(hdc, emptystring, 0, &rect, 0, NULL );
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextExA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
    heightcheck = textheight = DrawTextA(hdc, NULL, 0, &rect, 0);
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextA\n");
    textheight = DrawTextExA(hdc, NULL, 0, &rect, 0, NULL );
    if (conform_xp)
        ok(textheight==0,"Got textheight from DrawTextExA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
    heightcheck = textheight = DrawTextA(hdc, emptystring, -1, &rect, 0);
    ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
    textheight = DrawTextExA(hdc, emptystring, -1, &rect, 0, NULL );
    ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
    heightcheck = textheight = DrawTextA(hdc, NULL, -1, &rect, 0);
    if (conform_xp)
        ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
    textheight = DrawTextExA(hdc, NULL, -1, &rect, 0, NULL );
    if (conform_xp)
        ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
    heightcheck = textheight = DrawTextA(hdc, NULL, 10, &rect, 0);
    ok(textheight==0,"Got textheight from DrawTextA\n");
    textheight = DrawTextExA(hdc, NULL, 10, &rect, 0, NULL );
    ok(textheight==0,"Got textheight from DrawTextA\n");
    ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");


    /* invalid dtp size test */
    dtp.cbSize = -1; /* Invalid */
    dtp.uiLengthDrawn = 1337;
    textheight = DrawTextExA(hdc, text, 0, &rect, 0, &dtp);
    ok(textheight==0,"Got textheight from DrawTextExA\n");
    ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
    dtp.uiLengthDrawn = 1337;
    textheight = DrawTextExA(hdc, emptystring, 0, &rect, 0, &dtp);
    ok(textheight==0,"Got textheight from DrawTextExA\n");
    ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
    dtp.uiLengthDrawn = 1337;
    textheight = DrawTextExA(hdc, NULL, 0, &rect, 0, &dtp);
    ok(textheight==0,"Got textheight from DrawTextExA\n");
    ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
    dtp.uiLengthDrawn = 1337;
    textheight = DrawTextExA(hdc, emptystring, -1, &rect, 0, &dtp);
    ok(textheight==0,"Got textheight from DrawTextExA\n");
    ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
    dtp.uiLengthDrawn = 1337;
    textheight = DrawTextExA(hdc, NULL, -1, &rect, 0, &dtp);
    ok(textheight==0,"Got textheight from DrawTextExA\n");
    ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
318

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    /* Margin calculations */
    dtp.cbSize = sizeof(dtp);
    dtp.iLeftMargin = 0;
    dtp.iRightMargin = 0;
    SetRect( &rect, 0, 0, 0, 0);
    DrawTextExA(hdc, text, -1, &rect, DT_CALCRECT, &dtp);
    textlen = rect.right; /* Width without margin */
    dtp.iLeftMargin = 8;
    SetRect( &rect, 0, 0, 0, 0);
    DrawTextExA(hdc, text, -1, &rect, DT_CALCRECT, &dtp);
    ok(rect.right==dtp.iLeftMargin+textlen  ,"Incorrect left margin calculated  rc(%d,%d)\n", rect.left, rect.right);
    dtp.iLeftMargin = 0;
    dtp.iRightMargin = 8;
    SetRect( &rect, 0, 0, 0, 0);
    DrawTextExA(hdc, text, -1, &rect, DT_CALCRECT, &dtp);
    ok(rect.right==dtp.iRightMargin+textlen  ,"Incorrect right margin calculated rc(%d,%d)\n", rect.left, rect.right);

336 337 338
    /* Wide char versions */
    SetRect( &rect, 10,10, 100, 100);
    SetLastError( 0);
339
    heightcheck = textheight = DrawTextExW(hdc, textW, 0, &rect, DT_CALCRECT, NULL );
340
    if( GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) {
341 342
        ok( !EMPTY(rect) && !MODIFIED(rect),
            "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
343
            rect.left, rect.top, rect.right, rect.bottom );
344 345
        ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");

346
        SetRect( &rect, 10,10, 100, 100);
347 348 349 350 351 352 353 354 355 356
        textheight = DrawTextW(hdc, textW, 0, &rect, DT_CALCRECT);
        ok( !EMPTY(rect) && !MODIFIED(rect),
            "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
            rect.left, rect.top, rect.right, rect.bottom );
        ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
        ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

        SetRect( &rect, 10,10, 100, 100);
        heightcheck = textheight = DrawTextExW(hdc, emptystringW, -1, &rect, DT_CALCRECT, NULL );
        ok( EMPTY(rect),
357 358
            "rectangle should be empty got %d,%d-%d,%d\n",
            rect.left, rect.top, rect.right, rect.bottom );
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
        ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");

        SetRect( &rect, 10,10, 100, 100);
        textheight = DrawTextW(hdc, emptystringW, -1, &rect, DT_CALCRECT);
        ok( EMPTY(rect),
            "rectangle should be empty got %d,%d-%d,%d\n",
            rect.left, rect.top, rect.right, rect.bottom );
        ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
        ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

        SetRect( &rect, 10,10, 100, 100);
        heightcheck = textheight = DrawTextExW(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
        ok( !EMPTY(rect) && !MODIFIED(rect),
            "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
            rect.left, rect.top, rect.right, rect.bottom );
        if (textheight) /* windows 2000 */
        {
            if (conform_xp)
377
                win_skip("XP conformity failed, skipping XP tests. Probably win 2000\n");
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
            conform_xp = FALSE;
        }
        else
            ok(textheight==0,"Got textheight from DrawTextExW\n");

        SetRect( &rect, 10,10, 100, 100);
        textheight = DrawTextW(hdc, NULL, 0, &rect, DT_CALCRECT);
        ok( !EMPTY(rect) && !MODIFIED(rect),
            "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
            rect.left, rect.top, rect.right, rect.bottom );
        if (conform_xp)
            ok(textheight==0,"Got textheight from DrawTextW\n");
        ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

        if (conform_xp) {
            /* Crashes on NT4 */
394
            SetRect( &rect, 10,10, 100, 100);
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
            heightcheck = textheight = DrawTextExW(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
            ok( !EMPTY(rect) && !MODIFIED(rect),
                "rectangle should NOT be empty  and NOT modified got %d,%d-%d,%d\n",
                rect.left, rect.top, rect.right, rect.bottom );
            ok(textheight==0,"Got textheight from DrawTextExW\n");

            SetRect( &rect, 10,10, 100, 100);
            textheight = DrawTextW(hdc, NULL, -1, &rect, DT_CALCRECT);
            ok( !EMPTY(rect) && !MODIFIED(rect),
                "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
                rect.left, rect.top, rect.right, rect.bottom );
            ok(textheight==0,"Got textheight from DrawTextW\n");
            ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
        }


        /* DT_SINGLELINE tests */

        heightcheck = textheight = DrawTextExW(hdc, textW, 0, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
        ok( !EMPTY(rect) && !MODIFIED(rect),
            "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
            rect.left, rect.top, rect.right, rect.bottom );
        ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");

        SetRect( &rect, 10,10, 100, 100);
        textheight = DrawTextW(hdc, textW, 0, &rect, DT_CALCRECT|DT_SINGLELINE);
        ok( !EMPTY(rect) && !MODIFIED(rect),
            "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
            rect.left, rect.top, rect.right, rect.bottom );
        ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
        ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

        SetRect( &rect, 10,10, 100, 100);
        heightcheck = textheight = DrawTextExW(hdc, emptystringW, -1, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
        ok( !EMPTY(rect) && MODIFIED(rect),
            "rectangle should be modified got %d,%d-%d,%d\n",
            rect.left, rect.top, rect.right, rect.bottom );
        ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");

        SetRect( &rect, 10,10, 100, 100);
        textheight = DrawTextW(hdc, emptystringW, -1, &rect, DT_CALCRECT|DT_SINGLELINE);
        ok( !EMPTY(rect) && MODIFIED(rect),
            "rectangle should be modified got %d,%d-%d,%d\n",
            rect.left, rect.top, rect.right, rect.bottom );
        ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
        ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

        if (conform_xp) {
443
            /* Crashes on NT4 */
444 445 446 447 448 449 450 451 452 453 454
            SetRect( &rect, 10,10, 100, 100);
            heightcheck = textheight = DrawTextExW(hdc, NULL, -1, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
            ok( !EMPTY(rect) && !MODIFIED(rect),
                "rectangle should NOT be empty  and NOT modified got %d,%d-%d,%d\n",
                rect.left, rect.top, rect.right, rect.bottom );
            ok(textheight==0,"Got textheight from DrawTextExW\n");

            SetRect( &rect, 10,10, 100, 100);
            textheight = DrawTextW(hdc, NULL, -1, &rect, DT_CALCRECT|DT_SINGLELINE);
            ok( !EMPTY(rect) && !MODIFIED(rect),
                "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
455
                rect.left, rect.top, rect.right, rect.bottom );
456 457
            ok(textheight==0,"Got textheight from DrawTextW\n");
            ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
458
        }
459 460 461 462 463 464 465 466 467

        SetRect( &rect, 10,10, 100, 100);
        heightcheck = textheight = DrawTextExW(hdc, NULL, 0, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
        ok( !EMPTY(rect) && !MODIFIED(rect),
            "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
            rect.left, rect.top, rect.right, rect.bottom );
        if (conform_xp)
            ok(textheight==0,"Got textheight from DrawTextExW\n");

468
        SetRect( &rect, 10,10, 100, 100);
469 470 471
        textheight = DrawTextW(hdc, NULL, 0, &rect, DT_CALCRECT|DT_SINGLELINE);
        ok( !EMPTY(rect) && !MODIFIED(rect),
            "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
472
            rect.left, rect.top, rect.right, rect.bottom );
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
        if (conform_xp)
            ok(textheight==0,"Got textheight from DrawTextW\n");
        ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");

        /* further tests with NULL and empty strings */
        heightcheck = textheight = DrawTextW(hdc, textW, 0, &rect, 0);
        ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
        textheight = DrawTextExW(hdc, textW, 0, &rect, 0, NULL );
        ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
        ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
        heightcheck = textheight = DrawTextW(hdc, emptystringW, 0, &rect, 0);
        ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
        textheight = DrawTextExW(hdc, emptystringW, 0, &rect, 0, NULL );
        ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
        ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
        heightcheck = textheight = DrawTextW(hdc, NULL, 0, &rect, 0);
        if (conform_xp)
            ok(textheight==0,"Got textheight from DrawTextW\n");
        textheight = DrawTextExW(hdc, NULL, 0, &rect, 0, NULL );
        if (conform_xp)
            ok(textheight==0,"Got textheight from DrawTextExW\n");
        ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
        heightcheck = textheight = DrawTextW(hdc, emptystringW, -1, &rect, 0);
        ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
        textheight = DrawTextExW(hdc, emptystringW, -1, &rect, 0, NULL );
        ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
        ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
        if (conform_xp) {
            /* Crashes on NT4 */
            heightcheck = textheight = DrawTextW(hdc, NULL, -1, &rect, 0);
            ok(textheight==0,"Got textheight from DrawTextW\n");
            textheight = DrawTextExW(hdc, NULL, -1, &rect, 0, NULL );
            ok(textheight==0,"Got textheight from DrawTextExW\n");
            ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
            heightcheck = textheight = DrawTextW(hdc, NULL, 10, &rect, 0);
            ok(textheight==0,"Got textheight from DrawTextW\n");
            textheight = DrawTextExW(hdc, NULL, 10, &rect, 0, NULL );
            ok(textheight==0,"Got textheight from DrawTextW\n");
            ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
        }

        dtp.cbSize = -1; /* Invalid */
        dtp.uiLengthDrawn = 1337;
        textheight = DrawTextExW(hdc, textW, 0, &rect, 0, &dtp);
        ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
        ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
        dtp.uiLengthDrawn = 1337;
        textheight = DrawTextExW(hdc, emptystringW, 0, &rect, 0, &dtp);
        if (conform_xp)
            ok(textheight==0,"Got textheight from DrawTextExW\n");
        ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
        dtp.uiLengthDrawn = 1337;
        textheight = DrawTextExW(hdc, NULL, 0, &rect, 0, &dtp);
        if (conform_xp)
            ok(textheight==0,"Got textheight from DrawTextExW\n");
        ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
        dtp.uiLengthDrawn = 1337;
        textheight = DrawTextExW(hdc, emptystringW, -1, &rect, 0, &dtp);
        ok(textheight==0,"Got textheight from DrawTextExW\n");
        ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
        if (conform_xp) {
            /* Crashes on NT4 */
            dtp.uiLengthDrawn = 1337;
            textheight = DrawTextExW(hdc, NULL, -1, &rect, 0, &dtp);
            ok(textheight==0,"Got textheight from DrawTextExW\n");
            ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
        }
540
    }
541

542 543 544
    /* More test cases from bug 12226 */
    SetRect(&rect, 0, 0, 0, 0);
    textheight = DrawTextA(hdc, emptystring, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
545
    ok(textheight, "DrawTextA error %u\n", GetLastError());
546 547 548
    ok(0 == rect.left, "expected 0, got %d\n", rect.left);
    ok(0 == rect.right, "expected 0, got %d\n", rect.right);
    ok(0 == rect.top, "expected 0, got %d\n", rect.top);
549
    ok(rect.bottom, "rect.bottom should not be 0\n");
550 551 552

    SetRect(&rect, 0, 0, 0, 0);
    textheight = DrawTextW(hdc, emptystringW, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
553 554 555 556 557 558
    if (!textheight && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
    {
        win_skip( "DrawTextW not implemented\n" );
    }
    else
    {
559
        ok(textheight, "DrawTextW error %u\n", GetLastError());
560 561 562
        ok(0 == rect.left, "expected 0, got %d\n", rect.left);
        ok(0 == rect.right, "expected 0, got %d\n", rect.right);
        ok(0 == rect.top, "expected 0, got %d\n", rect.top);
563
        ok(rect.bottom, "rect.bottom should not be 0\n");
564
    }
565

566
    SelectObject(hdc, hOldFont);
567
    ret = DeleteObject(hFont);
568
    ok( ret, "DeleteObject error %u\n", GetLastError());
569 570

    /* Clean up */
571
    ret = ReleaseDC(hwnd, hdc);
572
    ok( ret, "ReleaseDC error %u\n", GetLastError());
573
    ret = DestroyWindow(hwnd);
574
    ok( ret, "DestroyWindow error %u\n", GetLastError());
575 576
}

577
/* replace tabs by \t */
578
static void strfmt( const char *str, char *strout)
579 580 581 582 583 584 585 586
{
    unsigned int i,j ;
    for(i=0,j=0;i<=strlen(str);i++,j++)
        if((strout[j]=str[i])=='\t') {
            strout[j++]='\\';
            strout[j]='t';
        }
}
587

588 589 590 591 592 593 594 595 596 597 598 599 600 601

#define TABTEST( tabval, tabcount, string, _exp) \
{ int i,x_act, x_exp; char strdisp[64];\
    for(i=0;i<8;i++) tabs[i]=(i+1)*(tabval); \
    extent = GetTabbedTextExtentA( hdc, string, strlen( string), (tabcount), tabs); \
    strfmt( string, strdisp); \
 /*   trace( "Extent is %08lx\n", extent); */\
    x_act = LOWORD( extent); \
    x_exp = (_exp); \
    ok( x_act == x_exp, "Test case \"%s\". Text extent is %d, expected %d tab %d tabcount %d\n", \
        strdisp, x_act, x_exp, tabval, tabcount); \
} \


602
static void test_TabbedText(void)
603 604 605 606 607 608 609 610 611 612 613
{
    HWND hwnd;
    HDC hdc;
    BOOL ret;
    TEXTMETRICA tm;
    DWORD extent;
    INT tabs[8], cx, cy, tab, tabcount,t,align;

    /* Initialization */
    hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
                           0, 0, 200, 200, 0, 0, 0, NULL);
614
    ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
615
    hdc = GetDC(hwnd);
616
    ok(hdc != 0, "GetDC error %u\n", GetLastError());
617 618

    ret = GetTextMetricsA( hdc, &tm);
619
    ok( ret, "GetTextMetrics error %u\n", GetLastError());
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662

    extent = GetTabbedTextExtentA( hdc, "x", 1, 1, tabs);
    cx = LOWORD( extent);
    cy = HIWORD( extent);
    trace( "cx is %d cy is %d\n", cx, cy);

    align=1;
    for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to 
                               catch the one off errors */
        tab =  (cx *4 + t);
        /* test the special case tabcount =1 and the general array (80 of tabs */
        for( tabcount = 1; tabcount <= 8; tabcount +=7) { 
            TABTEST( align * tab, tabcount, "\t", tab)
            TABTEST( align * tab, tabcount, "xxx\t", tab)
            TABTEST( align * tab, tabcount, "\tx", tab+cx)
            TABTEST( align * tab, tabcount, "\t\t", tab*2)
            TABTEST( align * tab, tabcount, "\tx\t", tab*2)
            TABTEST( align * tab, tabcount, "x\tx", tab+cx)
            TABTEST( align * tab, tabcount, "xx\tx", tab+cx)
            TABTEST( align * tab, tabcount, "xxx\tx", tab+cx)
            TABTEST( align * tab, tabcount, "xxxx\tx", t>0 ? tab + cx : 2*tab+cx)
            TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab+cx)
        }
    }
    align=-1;
    for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to 
                               catch the one off errors */
        tab =  (cx *4 + t);
        /* test the special case tabcount =1 and the general array (8) of tabs */
        for( tabcount = 1; tabcount <= 8; tabcount +=7) { 
            TABTEST( align * tab, tabcount, "\t", tab)
            TABTEST( align * tab, tabcount, "xxx\t", tab)
            TABTEST( align * tab, tabcount, "\tx", tab)
            TABTEST( align * tab, tabcount, "\t\t", tab*2)
            TABTEST( align * tab, tabcount, "\tx\t", tab*2)
            TABTEST( align * tab, tabcount, "x\tx", tab)
            TABTEST( align * tab, tabcount, "xx\tx", tab)
            TABTEST( align * tab, tabcount, "xxx\tx", 4 * cx >= tab ? 2*tab :tab)
            TABTEST( align * tab, tabcount, "xxxx\tx", 2*tab)
            TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab)
        }
    }

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
    ReleaseDC( hwnd, hdc );
    DestroyWindow( hwnd );
}

static void test_DrawState(void)
{
    static const char text[] = "Sample text string";
    HWND hwnd;
    HDC hdc;
    BOOL ret;

    hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
                           0, 0, 200, 200, 0, 0, 0, NULL);
    assert(hwnd);

    hdc = GetDC(hwnd);
    assert(hdc);

    SetLastError(0xdeadbeef);
    ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, strlen(text),
                    0, 0, 10, 10, DST_TEXT);
    ok(ret, "DrawState error %u\n", GetLastError());

    SetLastError(0xdeadbeef);
    ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, 0,
                    0, 0, 10, 10, DST_TEXT);
    ok(ret, "DrawState error %u\n", GetLastError());

    SetLastError(0xdeadbeef);
    ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, strlen(text),
                    0, 0, 10, 10, DST_TEXT);
694
    ok(!ret || broken(ret) /* win98 */, "DrawState succeeded\n");
695 696 697 698 699
    ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());

    SetLastError(0xdeadbeef);
    ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, 0,
                    0, 0, 10, 10, DST_TEXT);
700
    ok(!ret || broken(ret) /* win98 */, "DrawState succeeded\n");
701
    ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
702

703 704
    ReleaseDC(hwnd, hdc);
    DestroyWindow(hwnd);
705 706
}

707 708
START_TEST(text)
{
709
    test_TabbedText();
710
    test_DrawTextCalcRect();
711
    test_DrawState();
712
}