text.c 10.8 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 30 31 32 33 34 35 36
 */

#include <assert.h>

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


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

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

    /* 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);
69
    ok(hFont != 0, "CreateFontIndirectA error %u\n",
70 71 72
       GetLastError());
    hOldFont = SelectObject(hdc, hFont);

73
    textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
74
       DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
75
       DT_NOPREFIX);
76
    ok( textheight, "DrawTextA error %u\n", GetLastError());
77

78
    trace("MM_HIENGLISH rect.bottom %d\n", rect.bottom);
79 80
    todo_wine ok(rect.bottom < 0, "In MM_HIENGLISH, DrawText with "
       "DT_CALCRECT should return a negative rectangle bottom. "
81
       "(bot=%d)\n", rect.bottom);
82 83

    SelectObject(hdc, hOldFont);
84
    ret = DeleteObject(hFont);
85
    ok( ret, "DeleteObject error %u\n", GetLastError());
86 87 88 89 90 91 92


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

97
    textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
98
       DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
99
       DT_NOPREFIX);
100
    ok( textheight, "DrawTextA error %u\n", GetLastError());
101

102
    trace("MM_TEXT rect.bottom %d\n", rect.bottom);
103
    ok(rect.bottom > 0, "In MM_TEXT, DrawText with DT_CALCRECT "
104
       "should return a positive rectangle bottom. (bot=%d)\n",
105 106
       rect.bottom);

107 108 109 110
    /* empty or null text should in some cases calc an empty rectangle */
    /* note: testing the function's return value is useless, it differs
     * ( 0 or 1) on every Windows version I tried */
    SetRect( &rect, 10,10, 100, 100);
111
    textheight = DrawTextExA(hdc, text, 0, &rect, DT_CALCRECT, NULL );
112 113 114 115
    ok( !(rect.left == rect.right && rect.bottom == rect.top),
            "rectangle should NOT be empty.\n");
    SetRect( &rect, 10,10, 100, 100);
    SetLastError( 0);
116
    textheight = DrawTextExA(hdc, emptystring, -1, &rect, DT_CALCRECT, NULL );
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    ok( (rect.left == rect.right && rect.bottom == rect.top),
            "rectangle should be empty.\n");
    SetRect( &rect, 10,10, 100, 100);
    SetLastError( 0);
    textheight = DrawTextExA(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
    ok( (rect.left == rect.right && rect.bottom == rect.top),
            "rectangle should be empty.\n");
    SetRect( &rect, 10,10, 100, 100);
    textheight = DrawTextExA(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
    ok( !(rect.left == rect.right && rect.bottom == rect.top),
            "rectangle should NOT be empty.\n");

    /* Wide char versions */
    SetRect( &rect, 10,10, 100, 100);
    SetLastError( 0);
132
    textheight = DrawTextExW(hdc, textW, 0, &rect, DT_CALCRECT, NULL );
133 134 135 136
    if( GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) {
        ok( !(rect.left == rect.right && rect.bottom == rect.top),
                "rectangle should NOT be empty.\n");
        SetRect( &rect, 10,10, 100, 100);
137
        textheight = DrawTextExW(hdc, emptystringW, -1, &rect, DT_CALCRECT, NULL );
138 139 140 141 142 143 144 145 146 147 148
        ok( (rect.left == rect.right && rect.bottom == rect.top),
                "rectangle should be empty.\n");
        SetRect( &rect, 10,10, 100, 100);
        textheight = DrawTextExW(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
        ok( !(rect.left == rect.right && rect.bottom == rect.top),
                "rectangle should NOT be empty.\n");
        SetRect( &rect, 10,10, 100, 100);
        textheight = DrawTextExW(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
        ok( !(rect.left == rect.right && rect.bottom == rect.top),
                "rectangle should NOT be empty.\n");
    }
149

150
    SelectObject(hdc, hOldFont);
151
    ret = DeleteObject(hFont);
152
    ok( ret, "DeleteObject error %u\n", GetLastError());
153 154

    /* Clean up */
155
    ret = ReleaseDC(hwnd, hdc);
156
    ok( ret, "ReleaseDC error %u\n", GetLastError());
157
    ret = DestroyWindow(hwnd);
158
    ok( ret, "DestroyWindow error %u\n", GetLastError());
159 160
}

161
/* replace tabs by \t */
162
static void strfmt( const char *str, char *strout)
163 164 165 166 167 168 169 170
{
    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';
        }
}
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185

#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); \
} \


186
static void test_TabbedText(void)
187 188 189 190 191 192 193 194 195 196 197
{
    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);
198
    ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
199
    hdc = GetDC(hwnd);
200
    ok(hdc != 0, "GetDC error %u\n", GetLastError());
201 202

    ret = GetTextMetricsA( hdc, &tm);
203
    ok( ret, "GetTextMetrics error %u\n", GetLastError());
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

    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)
        }
    }

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
    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);
    ok(!ret, "DrawState succeeded\n");
    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);
    ok(!ret, "DrawState succeeded\n");
    ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
286

287 288
    ReleaseDC(hwnd, hdc);
    DestroyWindow(hwnd);
289 290
}

291 292
START_TEST(text)
{
293
    test_TabbedText();
294
    test_DrawTextCalcRect();
295
    test_DrawState();
296
}