4 * Copyright (c) 2004 Zach Gorman
5 * Copyright 2007 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * 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
24 #include "wine/test.h"
31 static void test_DrawTextCalcRect(void)
35 HFONT hFont, hOldFont;
37 static CHAR text[] = "Example text for testing DrawText in "
39 static WCHAR textW[] = {'W','i','d','e',' ','c','h','a','r',' ',
40 's','t','r','i','n','g','\0'};
41 static CHAR emptystring[] = "";
42 static WCHAR emptystringW[] = { 0 };
43 INT textlen, textheight;
44 RECT rect = { 0, 0, 100, 0 };
48 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
49 0, 0, 200, 200, 0, 0, 0, NULL);
50 ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
52 ok(hdc != 0, "GetDC error %u\n", GetLastError());
53 trace("hdc %p\n", hdc);
54 textlen = lstrlenA(text);
56 /* LOGFONT initialization */
57 memset(&lf, 0, sizeof(lf));
58 lf.lfCharSet = ANSI_CHARSET;
59 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
60 lf.lfWeight = FW_DONTCARE;
61 lf.lfHeight = 0; /* mapping mode dependent */
62 lf.lfQuality = DEFAULT_QUALITY;
63 lstrcpyA(lf.lfFaceName, "Arial");
65 /* DrawText in MM_HIENGLISH with DT_CALCRECT */
66 SetMapMode(hdc, MM_HIENGLISH);
67 lf.lfHeight = 100 * 9 / 72; /* 9 point */
68 hFont = CreateFontIndirectA(&lf);
69 ok(hFont != 0, "CreateFontIndirectA error %u\n",
71 hOldFont = SelectObject(hdc, hFont);
73 textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
74 DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
76 ok( textheight, "DrawTextA error %u\n", GetLastError());
78 trace("MM_HIENGLISH rect.bottom %d\n", rect.bottom);
79 todo_wine ok(rect.bottom < 0, "In MM_HIENGLISH, DrawText with "
80 "DT_CALCRECT should return a negative rectangle bottom. "
81 "(bot=%d)\n", rect.bottom);
83 SelectObject(hdc, hOldFont);
84 ret = DeleteObject(hFont);
85 ok( ret, "DeleteObject error %u\n", GetLastError());
88 /* DrawText in MM_TEXT with DT_CALCRECT */
89 SetMapMode(hdc, MM_TEXT);
90 lf.lfHeight = -MulDiv(9, GetDeviceCaps(hdc,
91 LOGPIXELSY), 72); /* 9 point */
92 hFont = CreateFontIndirectA(&lf);
93 ok(hFont != 0, "CreateFontIndirectA error %u\n",
95 hOldFont = SelectObject(hdc, hFont);
97 textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
98 DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
100 ok( textheight, "DrawTextA error %u\n", GetLastError());
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",
107 /* empty or null text should in some cases calc an empty rectangle */
108 /* note: testing the function's return value is useless, it differs
109 * ( 0 or 1) on every Windows version I tried */
110 SetRect( &rect, 10,10, 100, 100);
111 textheight = DrawTextExA(hdc, text, 0, &rect, DT_CALCRECT, NULL );
112 ok( !(rect.left == rect.right && rect.bottom == rect.top),
113 "rectangle should NOT be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
114 SetRect( &rect, 10,10, 100, 100);
116 textheight = DrawTextExA(hdc, emptystring, -1, &rect, DT_CALCRECT, NULL );
117 ok( (rect.left == rect.right && rect.bottom == rect.top),
118 "rectangle should be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
119 SetRect( &rect, 10,10, 100, 100);
121 textheight = DrawTextExA(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
122 ok( (rect.left == rect.right && rect.bottom == rect.top),
123 "rectangle should be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
124 SetRect( &rect, 10,10, 100, 100);
125 textheight = DrawTextExA(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
126 ok( !(rect.left == rect.right && rect.bottom == rect.top),
127 "rectangle should NOT be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
129 /* Wide char versions */
130 SetRect( &rect, 10,10, 100, 100);
132 textheight = DrawTextExW(hdc, textW, 0, &rect, DT_CALCRECT, NULL );
133 if( GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) {
134 ok( !(rect.left == rect.right && rect.bottom == rect.top),
135 "rectangle should NOT be empty got %d,%d-%d,%d\n",
136 rect.left, rect.top, rect.right, rect.bottom );
137 SetRect( &rect, 10,10, 100, 100);
138 textheight = DrawTextExW(hdc, emptystringW, -1, &rect, DT_CALCRECT, NULL );
139 ok( (rect.left == rect.right && rect.bottom == rect.top),
140 "rectangle should be empty got %d,%d-%d,%d\n",
141 rect.left, rect.top, rect.right, rect.bottom );
142 SetRect( &rect, 10,10, 100, 100);
143 textheight = DrawTextExW(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
144 ok( !(rect.left == rect.right && rect.bottom == rect.top),
145 "rectangle should NOT be empty got %d,%d-%d,%d\n",
146 rect.left, rect.top, rect.right, rect.bottom );
147 SetRect( &rect, 10,10, 100, 100);
148 textheight = DrawTextExW(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
149 ok( !(rect.left == rect.right && rect.bottom == rect.top),
150 "rectangle should NOT be empty got %d,%d-%d,%d\n",
151 rect.left, rect.top, rect.right, rect.bottom );
154 /* More test cases from bug 12226 */
155 SetRect(&rect, 0, 0, 0, 0);
156 textheight = DrawTextA(hdc, emptystring, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
157 todo_wine ok(textheight, "DrawTextA error %u\n", GetLastError());
158 ok(0 == rect.left, "expected 0, got %d\n", rect.left);
159 ok(0 == rect.right, "expected 0, got %d\n", rect.right);
160 ok(0 == rect.top, "expected 0, got %d\n", rect.top);
161 todo_wine ok(rect.bottom, "rect.bottom should not be 0\n");
163 SetRect(&rect, 0, 0, 0, 0);
164 textheight = DrawTextW(hdc, emptystringW, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
165 if (!textheight && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
167 win_skip( "DrawTextW not implemented\n" );
171 todo_wine ok(textheight, "DrawTextW error %u\n", GetLastError());
172 ok(0 == rect.left, "expected 0, got %d\n", rect.left);
173 ok(0 == rect.right, "expected 0, got %d\n", rect.right);
174 ok(0 == rect.top, "expected 0, got %d\n", rect.top);
175 todo_wine ok(rect.bottom, "rect.bottom should not be 0\n");
178 SelectObject(hdc, hOldFont);
179 ret = DeleteObject(hFont);
180 ok( ret, "DeleteObject error %u\n", GetLastError());
183 ret = ReleaseDC(hwnd, hdc);
184 ok( ret, "ReleaseDC error %u\n", GetLastError());
185 ret = DestroyWindow(hwnd);
186 ok( ret, "DestroyWindow error %u\n", GetLastError());
189 /* replace tabs by \t */
190 static void strfmt( const char *str, char *strout)
193 for(i=0,j=0;i<=strlen(str);i++,j++)
194 if((strout[j]=str[i])=='\t') {
201 #define TABTEST( tabval, tabcount, string, _exp) \
202 { int i,x_act, x_exp; char strdisp[64];\
203 for(i=0;i<8;i++) tabs[i]=(i+1)*(tabval); \
204 extent = GetTabbedTextExtentA( hdc, string, strlen( string), (tabcount), tabs); \
205 strfmt( string, strdisp); \
206 /* trace( "Extent is %08lx\n", extent); */\
207 x_act = LOWORD( extent); \
209 ok( x_act == x_exp, "Test case \"%s\". Text extent is %d, expected %d tab %d tabcount %d\n", \
210 strdisp, x_act, x_exp, tabval, tabcount); \
214 static void test_TabbedText(void)
221 INT tabs[8], cx, cy, tab, tabcount,t,align;
224 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
225 0, 0, 200, 200, 0, 0, 0, NULL);
226 ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
228 ok(hdc != 0, "GetDC error %u\n", GetLastError());
230 ret = GetTextMetricsA( hdc, &tm);
231 ok( ret, "GetTextMetrics error %u\n", GetLastError());
233 extent = GetTabbedTextExtentA( hdc, "x", 1, 1, tabs);
234 cx = LOWORD( extent);
235 cy = HIWORD( extent);
236 trace( "cx is %d cy is %d\n", cx, cy);
239 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
240 catch the one off errors */
242 /* test the special case tabcount =1 and the general array (80 of tabs */
243 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
244 TABTEST( align * tab, tabcount, "\t", tab)
245 TABTEST( align * tab, tabcount, "xxx\t", tab)
246 TABTEST( align * tab, tabcount, "\tx", tab+cx)
247 TABTEST( align * tab, tabcount, "\t\t", tab*2)
248 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
249 TABTEST( align * tab, tabcount, "x\tx", tab+cx)
250 TABTEST( align * tab, tabcount, "xx\tx", tab+cx)
251 TABTEST( align * tab, tabcount, "xxx\tx", tab+cx)
252 TABTEST( align * tab, tabcount, "xxxx\tx", t>0 ? tab + cx : 2*tab+cx)
253 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab+cx)
257 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
258 catch the one off errors */
260 /* test the special case tabcount =1 and the general array (8) of tabs */
261 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
262 TABTEST( align * tab, tabcount, "\t", tab)
263 TABTEST( align * tab, tabcount, "xxx\t", tab)
264 TABTEST( align * tab, tabcount, "\tx", tab)
265 TABTEST( align * tab, tabcount, "\t\t", tab*2)
266 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
267 TABTEST( align * tab, tabcount, "x\tx", tab)
268 TABTEST( align * tab, tabcount, "xx\tx", tab)
269 TABTEST( align * tab, tabcount, "xxx\tx", 4 * cx >= tab ? 2*tab :tab)
270 TABTEST( align * tab, tabcount, "xxxx\tx", 2*tab)
271 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab)
275 ReleaseDC( hwnd, hdc );
276 DestroyWindow( hwnd );
279 static void test_DrawState(void)
281 static const char text[] = "Sample text string";
286 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
287 0, 0, 200, 200, 0, 0, 0, NULL);
293 SetLastError(0xdeadbeef);
294 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, strlen(text),
295 0, 0, 10, 10, DST_TEXT);
296 ok(ret, "DrawState error %u\n", GetLastError());
298 SetLastError(0xdeadbeef);
299 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, 0,
300 0, 0, 10, 10, DST_TEXT);
301 ok(ret, "DrawState error %u\n", GetLastError());
303 SetLastError(0xdeadbeef);
304 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, strlen(text),
305 0, 0, 10, 10, DST_TEXT);
306 ok(!ret, "DrawState succeeded\n");
307 ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
309 SetLastError(0xdeadbeef);
310 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, 0,
311 0, 0, 10, 10, DST_TEXT);
312 ok(!ret, "DrawState succeeded\n");
313 ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
315 ReleaseDC(hwnd, hdc);
322 test_DrawTextCalcRect();