push cea036dc4aae50c232dc1391dcc459ff0b060bcf
[wine/hacks.git] / dlls / user32 / tests / text.c
blob5523ff298b64bf09a21e0a171a8af1c48d23f109
1 /*
2 * DrawText tests
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
22 #include <assert.h>
24 #include "wine/test.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winerror.h"
31 static void test_DrawTextCalcRect(void)
33 HWND hwnd;
34 HDC hdc;
35 HFONT hFont, hOldFont;
36 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 static CHAR emptystring[] = "";
42 static WCHAR emptystringW[] = { 0 };
43 INT textlen, textheight;
44 RECT rect = { 0, 0, 100, 0 };
45 BOOL ret;
47 /* Initialization */
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());
51 hdc = GetDC(hwnd);
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",
70 GetLastError());
71 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());
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",
94 GetLastError());
95 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());
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 rect.bottom);
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);
115 SetLastError( 0);
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);
120 SetLastError( 0);
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);
131 SetLastError( 0);
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 if (0) {
143 SetRect( &rect, 10,10, 100, 100);
144 /* Crashes on NT4 */
145 textheight = DrawTextExW(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
146 ok( !(rect.left == rect.right && rect.bottom == rect.top),
147 "rectangle should NOT be empty got %d,%d-%d,%d\n",
148 rect.left, rect.top, rect.right, rect.bottom );
150 SetRect( &rect, 10,10, 100, 100);
151 textheight = DrawTextExW(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
152 ok( !(rect.left == rect.right && rect.bottom == rect.top),
153 "rectangle should NOT be empty got %d,%d-%d,%d\n",
154 rect.left, rect.top, rect.right, rect.bottom );
157 /* More test cases from bug 12226 */
158 SetRect(&rect, 0, 0, 0, 0);
159 textheight = DrawTextA(hdc, emptystring, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
160 todo_wine ok(textheight, "DrawTextA error %u\n", GetLastError());
161 ok(0 == rect.left, "expected 0, got %d\n", rect.left);
162 ok(0 == rect.right, "expected 0, got %d\n", rect.right);
163 ok(0 == rect.top, "expected 0, got %d\n", rect.top);
164 todo_wine ok(rect.bottom, "rect.bottom should not be 0\n");
166 SetRect(&rect, 0, 0, 0, 0);
167 textheight = DrawTextW(hdc, emptystringW, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
168 if (!textheight && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
170 win_skip( "DrawTextW not implemented\n" );
172 else
174 todo_wine ok(textheight, "DrawTextW error %u\n", GetLastError());
175 ok(0 == rect.left, "expected 0, got %d\n", rect.left);
176 ok(0 == rect.right, "expected 0, got %d\n", rect.right);
177 ok(0 == rect.top, "expected 0, got %d\n", rect.top);
178 todo_wine ok(rect.bottom, "rect.bottom should not be 0\n");
181 SelectObject(hdc, hOldFont);
182 ret = DeleteObject(hFont);
183 ok( ret, "DeleteObject error %u\n", GetLastError());
185 /* Clean up */
186 ret = ReleaseDC(hwnd, hdc);
187 ok( ret, "ReleaseDC error %u\n", GetLastError());
188 ret = DestroyWindow(hwnd);
189 ok( ret, "DestroyWindow error %u\n", GetLastError());
192 /* replace tabs by \t */
193 static void strfmt( const char *str, char *strout)
195 unsigned int i,j ;
196 for(i=0,j=0;i<=strlen(str);i++,j++)
197 if((strout[j]=str[i])=='\t') {
198 strout[j++]='\\';
199 strout[j]='t';
204 #define TABTEST( tabval, tabcount, string, _exp) \
205 { int i,x_act, x_exp; char strdisp[64];\
206 for(i=0;i<8;i++) tabs[i]=(i+1)*(tabval); \
207 extent = GetTabbedTextExtentA( hdc, string, strlen( string), (tabcount), tabs); \
208 strfmt( string, strdisp); \
209 /* trace( "Extent is %08lx\n", extent); */\
210 x_act = LOWORD( extent); \
211 x_exp = (_exp); \
212 ok( x_act == x_exp, "Test case \"%s\". Text extent is %d, expected %d tab %d tabcount %d\n", \
213 strdisp, x_act, x_exp, tabval, tabcount); \
217 static void test_TabbedText(void)
219 HWND hwnd;
220 HDC hdc;
221 BOOL ret;
222 TEXTMETRICA tm;
223 DWORD extent;
224 INT tabs[8], cx, cy, tab, tabcount,t,align;
226 /* Initialization */
227 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
228 0, 0, 200, 200, 0, 0, 0, NULL);
229 ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
230 hdc = GetDC(hwnd);
231 ok(hdc != 0, "GetDC error %u\n", GetLastError());
233 ret = GetTextMetricsA( hdc, &tm);
234 ok( ret, "GetTextMetrics error %u\n", GetLastError());
236 extent = GetTabbedTextExtentA( hdc, "x", 1, 1, tabs);
237 cx = LOWORD( extent);
238 cy = HIWORD( extent);
239 trace( "cx is %d cy is %d\n", cx, cy);
241 align=1;
242 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
243 catch the one off errors */
244 tab = (cx *4 + t);
245 /* test the special case tabcount =1 and the general array (80 of tabs */
246 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
247 TABTEST( align * tab, tabcount, "\t", tab)
248 TABTEST( align * tab, tabcount, "xxx\t", tab)
249 TABTEST( align * tab, tabcount, "\tx", tab+cx)
250 TABTEST( align * tab, tabcount, "\t\t", tab*2)
251 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
252 TABTEST( align * tab, tabcount, "x\tx", tab+cx)
253 TABTEST( align * tab, tabcount, "xx\tx", tab+cx)
254 TABTEST( align * tab, tabcount, "xxx\tx", tab+cx)
255 TABTEST( align * tab, tabcount, "xxxx\tx", t>0 ? tab + cx : 2*tab+cx)
256 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab+cx)
259 align=-1;
260 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
261 catch the one off errors */
262 tab = (cx *4 + t);
263 /* test the special case tabcount =1 and the general array (8) of tabs */
264 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
265 TABTEST( align * tab, tabcount, "\t", tab)
266 TABTEST( align * tab, tabcount, "xxx\t", tab)
267 TABTEST( align * tab, tabcount, "\tx", tab)
268 TABTEST( align * tab, tabcount, "\t\t", tab*2)
269 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
270 TABTEST( align * tab, tabcount, "x\tx", tab)
271 TABTEST( align * tab, tabcount, "xx\tx", tab)
272 TABTEST( align * tab, tabcount, "xxx\tx", 4 * cx >= tab ? 2*tab :tab)
273 TABTEST( align * tab, tabcount, "xxxx\tx", 2*tab)
274 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab)
278 ReleaseDC( hwnd, hdc );
279 DestroyWindow( hwnd );
282 static void test_DrawState(void)
284 static const char text[] = "Sample text string";
285 HWND hwnd;
286 HDC hdc;
287 BOOL ret;
289 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
290 0, 0, 200, 200, 0, 0, 0, NULL);
291 assert(hwnd);
293 hdc = GetDC(hwnd);
294 assert(hdc);
296 SetLastError(0xdeadbeef);
297 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, strlen(text),
298 0, 0, 10, 10, DST_TEXT);
299 ok(ret, "DrawState error %u\n", GetLastError());
301 SetLastError(0xdeadbeef);
302 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, 0,
303 0, 0, 10, 10, DST_TEXT);
304 ok(ret, "DrawState error %u\n", GetLastError());
306 SetLastError(0xdeadbeef);
307 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, strlen(text),
308 0, 0, 10, 10, DST_TEXT);
309 ok(!ret, "DrawState succeeded\n");
310 ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
312 SetLastError(0xdeadbeef);
313 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, 0,
314 0, 0, 10, 10, DST_TEXT);
315 ok(!ret, "DrawState succeeded\n");
316 ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
318 ReleaseDC(hwnd, hdc);
319 DestroyWindow(hwnd);
322 START_TEST(text)
324 test_TabbedText();
325 test_DrawTextCalcRect();
326 test_DrawState();