Sync to trunk head(r38096)
[reactos.git] / rostests / winetests / user32 / text.c
1 /*
2 * DrawText tests
3 *
4 * Copyright (c) 2004 Zach Gorman
5 * Copyright 2007 Dmitry Timoshkov
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <assert.h>
23
24 #include "wine/test.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winerror.h"
29
30
31 static void test_DrawTextCalcRect(void)
32 {
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;
46
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);
55
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");
64
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);
72
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 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);
82
83 SelectObject(hdc, hOldFont);
84 ret = DeleteObject(hFont);
85 ok( ret, "DeleteObject error %u\n", GetLastError());
86
87
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);
96
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 rect.bottom);
106
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.\n");
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.\n");
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.\n");
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.\n");
128
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.\n");
136 SetRect( &rect, 10,10, 100, 100);
137 textheight = DrawTextExW(hdc, emptystringW, -1, &rect, DT_CALCRECT, NULL );
138 ok( (rect.left == rect.right && rect.bottom == rect.top),
139 "rectangle should be empty.\n");
140 SetRect( &rect, 10,10, 100, 100);
141 textheight = DrawTextExW(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
142 ok( !(rect.left == rect.right && rect.bottom == rect.top),
143 "rectangle should NOT be empty.\n");
144 SetRect( &rect, 10,10, 100, 100);
145 textheight = DrawTextExW(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
146 ok( !(rect.left == rect.right && rect.bottom == rect.top),
147 "rectangle should NOT be empty.\n");
148 }
149
150 /* More test cases from bug 12226 */
151 SetRect(&rect, 0, 0, 0, 0);
152 textheight = DrawTextA(hdc, emptystring, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
153 todo_wine ok(textheight, "DrawTextA error %u\n", GetLastError());
154 ok(0 == rect.left, "expected 0, got %d\n", rect.left);
155 ok(0 == rect.right, "expected 0, got %d\n", rect.right);
156 ok(0 == rect.top, "expected 0, got %d\n", rect.top);
157 todo_wine ok(rect.bottom, "rect.bottom should not be 0\n");
158
159 SetRect(&rect, 0, 0, 0, 0);
160 textheight = DrawTextW(hdc, emptystringW, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
161 todo_wine ok(textheight, "DrawTextW error %u\n", GetLastError());
162 ok(0 == rect.left, "expected 0, got %d\n", rect.left);
163 ok(0 == rect.right, "expected 0, got %d\n", rect.right);
164 ok(0 == rect.top, "expected 0, got %d\n", rect.top);
165 todo_wine ok(rect.bottom, "rect.bottom should not be 0\n");
166
167 SelectObject(hdc, hOldFont);
168 ret = DeleteObject(hFont);
169 ok( ret, "DeleteObject error %u\n", GetLastError());
170
171 /* Clean up */
172 ret = ReleaseDC(hwnd, hdc);
173 ok( ret, "ReleaseDC error %u\n", GetLastError());
174 ret = DestroyWindow(hwnd);
175 ok( ret, "DestroyWindow error %u\n", GetLastError());
176 }
177
178 /* replace tabs by \t */
179 static void strfmt( const char *str, char *strout)
180 {
181 unsigned int i,j ;
182 for(i=0,j=0;i<=strlen(str);i++,j++)
183 if((strout[j]=str[i])=='\t') {
184 strout[j++]='\\';
185 strout[j]='t';
186 }
187 }
188
189
190 #define TABTEST( tabval, tabcount, string, _exp) \
191 { int i,x_act, x_exp; char strdisp[64];\
192 for(i=0;i<8;i++) tabs[i]=(i+1)*(tabval); \
193 extent = GetTabbedTextExtentA( hdc, string, strlen( string), (tabcount), tabs); \
194 strfmt( string, strdisp); \
195 /* trace( "Extent is %08lx\n", extent); */\
196 x_act = LOWORD( extent); \
197 x_exp = (_exp); \
198 ok( x_act == x_exp, "Test case \"%s\". Text extent is %d, expected %d tab %d tabcount %d\n", \
199 strdisp, x_act, x_exp, tabval, tabcount); \
200 } \
201
202
203 static void test_TabbedText(void)
204 {
205 HWND hwnd;
206 HDC hdc;
207 BOOL ret;
208 TEXTMETRICA tm;
209 DWORD extent;
210 INT tabs[8], cx, cy, tab, tabcount,t,align;
211
212 /* Initialization */
213 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
214 0, 0, 200, 200, 0, 0, 0, NULL);
215 ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
216 hdc = GetDC(hwnd);
217 ok(hdc != 0, "GetDC error %u\n", GetLastError());
218
219 ret = GetTextMetricsA( hdc, &tm);
220 ok( ret, "GetTextMetrics error %u\n", GetLastError());
221
222 extent = GetTabbedTextExtentA( hdc, "x", 1, 1, tabs);
223 cx = LOWORD( extent);
224 cy = HIWORD( extent);
225 trace( "cx is %d cy is %d\n", cx, cy);
226
227 align=1;
228 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
229 catch the one off errors */
230 tab = (cx *4 + t);
231 /* test the special case tabcount =1 and the general array (80 of tabs */
232 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
233 TABTEST( align * tab, tabcount, "\t", tab)
234 TABTEST( align * tab, tabcount, "xxx\t", tab)
235 TABTEST( align * tab, tabcount, "\tx", tab+cx)
236 TABTEST( align * tab, tabcount, "\t\t", tab*2)
237 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
238 TABTEST( align * tab, tabcount, "x\tx", tab+cx)
239 TABTEST( align * tab, tabcount, "xx\tx", tab+cx)
240 TABTEST( align * tab, tabcount, "xxx\tx", tab+cx)
241 TABTEST( align * tab, tabcount, "xxxx\tx", t>0 ? tab + cx : 2*tab+cx)
242 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab+cx)
243 }
244 }
245 align=-1;
246 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
247 catch the one off errors */
248 tab = (cx *4 + t);
249 /* test the special case tabcount =1 and the general array (8) of tabs */
250 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
251 TABTEST( align * tab, tabcount, "\t", tab)
252 TABTEST( align * tab, tabcount, "xxx\t", tab)
253 TABTEST( align * tab, tabcount, "\tx", tab)
254 TABTEST( align * tab, tabcount, "\t\t", tab*2)
255 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
256 TABTEST( align * tab, tabcount, "x\tx", tab)
257 TABTEST( align * tab, tabcount, "xx\tx", tab)
258 TABTEST( align * tab, tabcount, "xxx\tx", 4 * cx >= tab ? 2*tab :tab)
259 TABTEST( align * tab, tabcount, "xxxx\tx", 2*tab)
260 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab)
261 }
262 }
263
264 ReleaseDC( hwnd, hdc );
265 DestroyWindow( hwnd );
266 }
267
268 static void test_DrawState(void)
269 {
270 static const char text[] = "Sample text string";
271 HWND hwnd;
272 HDC hdc;
273 BOOL ret;
274
275 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
276 0, 0, 200, 200, 0, 0, 0, NULL);
277 assert(hwnd);
278
279 hdc = GetDC(hwnd);
280 assert(hdc);
281
282 SetLastError(0xdeadbeef);
283 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, strlen(text),
284 0, 0, 10, 10, DST_TEXT);
285 ok(ret, "DrawState error %u\n", GetLastError());
286
287 SetLastError(0xdeadbeef);
288 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, 0,
289 0, 0, 10, 10, DST_TEXT);
290 ok(ret, "DrawState error %u\n", GetLastError());
291
292 SetLastError(0xdeadbeef);
293 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, strlen(text),
294 0, 0, 10, 10, DST_TEXT);
295 ok(!ret, "DrawState succeeded\n");
296 ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
297
298 SetLastError(0xdeadbeef);
299 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, 0,
300 0, 0, 10, 10, DST_TEXT);
301 ok(!ret, "DrawState succeeded\n");
302 ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
303
304 ReleaseDC(hwnd, hdc);
305 DestroyWindow(hwnd);
306 }
307
308 START_TEST(text)
309 {
310 test_TabbedText();
311 test_DrawTextCalcRect();
312 test_DrawState();
313 }