[User32Test]
[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 #define MODIFIED(rect) (rect.left == 10 && rect.right != 100 && rect.top == 10 && rect.bottom != 100)
31 #define EMPTY(rect) (rect.left == rect.right && rect.bottom == rect.top)
32
33 static void test_DrawTextCalcRect(void)
34 {
35 HWND hwnd;
36 HDC hdc;
37 HFONT hFont, hOldFont;
38 LOGFONTA lf;
39 static CHAR text[] = "Example text for testing DrawText in "
40 "MM_HIENGLISH mode";
41 static WCHAR textW[] = {'W','i','d','e',' ','c','h','a','r',' ',
42 's','t','r','i','n','g','\0'};
43 static CHAR emptystring[] = "";
44 static WCHAR emptystringW[] = { 0 };
45 static CHAR wordbreak_text[] = "line1 line2";
46 static WCHAR wordbreak_textW[] = {'l','i','n','e','1',' ','l','i','n','e','2',0};
47 INT textlen, textheight, heightcheck;
48 RECT rect = { 0, 0, 100, 0 };
49 BOOL ret;
50 DRAWTEXTPARAMS dtp;
51 BOOL conform_xp = TRUE;
52
53 /* Initialization */
54 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
55 0, 0, 200, 200, 0, 0, 0, NULL);
56 ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
57 hdc = GetDC(hwnd);
58 ok(hdc != 0, "GetDC error %u\n", GetLastError());
59 trace("hdc %p\n", hdc);
60 textlen = lstrlenA(text);
61
62 /* LOGFONT initialization */
63 memset(&lf, 0, sizeof(lf));
64 lf.lfCharSet = ANSI_CHARSET;
65 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
66 lf.lfWeight = FW_DONTCARE;
67 lf.lfHeight = 0; /* mapping mode dependent */
68 lf.lfQuality = DEFAULT_QUALITY;
69 lstrcpyA(lf.lfFaceName, "Arial");
70
71 /* DrawText in MM_HIENGLISH with DT_CALCRECT */
72 SetMapMode(hdc, MM_HIENGLISH);
73 lf.lfHeight = 100 * 9 / 72; /* 9 point */
74 hFont = CreateFontIndirectA(&lf);
75 ok(hFont != 0, "CreateFontIndirectA error %u\n",
76 GetLastError());
77 hOldFont = SelectObject(hdc, hFont);
78
79 textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
80 DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
81 DT_NOPREFIX);
82 ok( textheight, "DrawTextA error %u\n", GetLastError());
83
84 trace("MM_HIENGLISH rect.bottom %d\n", rect.bottom);
85 ok(rect.bottom < 0, "In MM_HIENGLISH, DrawText with "
86 "DT_CALCRECT should return a negative rectangle bottom. "
87 "(bot=%d)\n", rect.bottom);
88
89 SelectObject(hdc, hOldFont);
90 ret = DeleteObject(hFont);
91 ok( ret, "DeleteObject error %u\n", GetLastError());
92
93
94 /* DrawText in MM_TEXT with DT_CALCRECT */
95 SetMapMode(hdc, MM_TEXT);
96 lf.lfHeight = -MulDiv(9, GetDeviceCaps(hdc,
97 LOGPIXELSY), 72); /* 9 point */
98 hFont = CreateFontIndirectA(&lf);
99 ok(hFont != 0, "CreateFontIndirectA error %u\n",
100 GetLastError());
101 hOldFont = SelectObject(hdc, hFont);
102
103 textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
104 DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
105 DT_NOPREFIX);
106 ok( textheight, "DrawTextA error %u\n", GetLastError());
107
108 trace("MM_TEXT rect.bottom %d\n", rect.bottom);
109 ok(rect.bottom > 0, "In MM_TEXT, DrawText with DT_CALCRECT "
110 "should return a positive rectangle bottom. (bot=%d)\n",
111 rect.bottom);
112
113 /* empty or null text should in some cases calc an empty rectangle */
114
115 SetRect( &rect, 10,10, 100, 100);
116 heightcheck = textheight = DrawTextExA(hdc, text, 0, &rect, DT_CALCRECT, NULL );
117 ok( !EMPTY(rect) && !MODIFIED(rect),
118 "rectangle should NOT be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
119 ok(textheight==0,"Got textheight from DrawTextExA\n");
120
121 SetRect( &rect, 10,10, 100, 100);
122 textheight = DrawTextA(hdc, text, 0, &rect, DT_CALCRECT);
123 ok( !EMPTY(rect) && !MODIFIED(rect),
124 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
125 rect.left, rect.top, rect.right, rect.bottom );
126 if (conform_xp)
127 ok(textheight==0,"Got textheight from DrawTextA\n");
128 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
129
130 SetRect( &rect, 10,10, 100, 100);
131 SetLastError( 0);
132 heightcheck = textheight = DrawTextExA(hdc, emptystring, -1, &rect, DT_CALCRECT, NULL );
133 ok( EMPTY(rect),
134 "rectangle should be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
135 ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");
136
137 SetRect( &rect, 10,10, 100, 100);
138 textheight = DrawTextA(hdc, emptystring, -1, &rect, DT_CALCRECT);
139 ok( EMPTY(rect),
140 "rectangle should be empty got %d,%d-%d,%d\n",
141 rect.left, rect.top, rect.right, rect.bottom );
142 ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
143 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
144
145 SetRect( &rect, 10,10, 100, 100);
146 SetLastError( 0);
147 heightcheck = textheight = DrawTextExA(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
148 ok( EMPTY(rect) || !MODIFIED(rect),
149 "rectangle should be empty or not modified got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
150 if (!textheight) /* Windows NT 4 */
151 {
152 if (conform_xp)
153 win_skip("XP conformity failed, skipping XP tests. Probably winNT\n");
154 conform_xp = FALSE;
155 }
156 else
157 ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");
158
159 SetRect( &rect, 10,10, 100, 100);
160 textheight = DrawTextA(hdc, NULL, -1, &rect, DT_CALCRECT);
161 ok( EMPTY(rect) || !MODIFIED(rect),
162 "rectangle should be empty or NOT modified got %d,%d-%d,%d\n",
163 rect.left, rect.top, rect.right, rect.bottom );
164 if (conform_xp)
165 ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
166 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
167
168 SetRect( &rect, 10,10, 100, 100);
169 heightcheck = textheight = DrawTextExA(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
170 ok( !EMPTY(rect) && !MODIFIED(rect),
171 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
172 if (conform_xp)
173 ok(textheight==0,"Got textheight from DrawTextExA\n");
174
175 SetRect( &rect, 10,10, 100, 100);
176 textheight = DrawTextA(hdc, NULL, 0, &rect, DT_CALCRECT);
177 ok( !EMPTY(rect) && !MODIFIED(rect),
178 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
179 rect.left, rect.top, rect.right, rect.bottom );
180 if (conform_xp)
181 ok(textheight==0,"Got textheight from DrawTextA\n");
182 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
183
184 /* DT_SINGLELINE tests */
185
186 SetRect( &rect, 10,10, 100, 100);
187 heightcheck = textheight = DrawTextExA(hdc, text, 0, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
188 ok( !EMPTY(rect) && !MODIFIED(rect),
189 "rectangle should NOT be empty got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
190 if (conform_xp)
191 ok(textheight==0,"Got textheight from DrawTextExA\n");
192
193 SetRect( &rect, 10,10, 100, 100);
194 textheight = DrawTextA(hdc, text, 0, &rect, DT_CALCRECT|DT_SINGLELINE);
195 ok( !EMPTY(rect) && !MODIFIED(rect),
196 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
197 rect.left, rect.top, rect.right, rect.bottom );
198 if (conform_xp)
199 ok(textheight==0,"Got textheight from DrawTextA\n");
200 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
201
202 SetRect( &rect, 10,10, 100, 100);
203 SetLastError( 0);
204 heightcheck = textheight = DrawTextExA(hdc, emptystring, -1, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
205 ok( !EMPTY(rect) && MODIFIED(rect),
206 "rectangle should be modified got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
207 ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");
208
209 SetRect( &rect, 10,10, 100, 100);
210 textheight = DrawTextA(hdc, emptystring, -1, &rect, DT_CALCRECT|DT_SINGLELINE);
211 ok( !EMPTY(rect) && MODIFIED (rect),
212 "rectangle should be modified got %d,%d-%d,%d\n",
213 rect.left, rect.top, rect.right, rect.bottom );
214 ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
215 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
216
217 SetRect( &rect, 10,10, 100, 100);
218 SetLastError( 0);
219 heightcheck = textheight = DrawTextExA(hdc, NULL, -1, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
220 ok( (!EMPTY(rect) && MODIFIED(rect)) || !MODIFIED(rect),
221 "rectangle should be modified got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
222 if (conform_xp)
223 ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");
224
225 SetRect( &rect, 10,10, 100, 100);
226 textheight = DrawTextA(hdc, NULL, -1, &rect, DT_CALCRECT|DT_SINGLELINE);
227 ok( (!EMPTY(rect) && MODIFIED(rect)) || !MODIFIED(rect),
228 "rectangle should be modified got %d,%d-%d,%d\n",
229 rect.left, rect.top, rect.right, rect.bottom );
230 if (conform_xp)
231 ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
232 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
233
234 SetRect( &rect, 10,10, 100, 100);
235 heightcheck = textheight = DrawTextExA(hdc, NULL, 0, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
236 ok( !EMPTY(rect) && !MODIFIED(rect),
237 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
238 if (conform_xp)
239 ok(textheight==0,"Got textheight from DrawTextExA\n");
240
241 SetRect( &rect, 10,10, 100, 100);
242 textheight = DrawTextA(hdc, NULL, 0, &rect, DT_CALCRECT|DT_SINGLELINE);
243 ok( !EMPTY(rect) && !MODIFIED(rect),
244 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
245 rect.left, rect.top, rect.right, rect.bottom );
246 if (conform_xp)
247 ok(textheight==0,"Got textheight from DrawTextA\n");
248 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
249
250 /* further tests with 0 count, NULL and empty strings */
251 heightcheck = textheight = DrawTextA(hdc, text, 0, &rect, 0);
252 if (conform_xp)
253 ok(textheight==0,"Got textheight from DrawTextA\n");
254 textheight = DrawTextExA(hdc, text, 0, &rect, 0, NULL );
255 if (conform_xp)
256 ok(textheight==0,"Got textheight from DrawTextExA\n");
257 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
258 heightcheck = textheight = DrawTextA(hdc, emptystring, 0, &rect, 0);
259 if (conform_xp)
260 ok(textheight==0,"Got textheight from DrawTextA\n");
261 textheight = DrawTextExA(hdc, emptystring, 0, &rect, 0, NULL );
262 if (conform_xp)
263 ok(textheight==0,"Got textheight from DrawTextExA\n");
264 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
265 heightcheck = textheight = DrawTextA(hdc, NULL, 0, &rect, 0);
266 if (conform_xp)
267 ok(textheight==0,"Got textheight from DrawTextA\n");
268 textheight = DrawTextExA(hdc, NULL, 0, &rect, 0, NULL );
269 if (conform_xp)
270 ok(textheight==0,"Got textheight from DrawTextExA\n");
271 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
272 heightcheck = textheight = DrawTextA(hdc, emptystring, -1, &rect, 0);
273 ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
274 textheight = DrawTextExA(hdc, emptystring, -1, &rect, 0, NULL );
275 ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");
276 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
277 heightcheck = textheight = DrawTextA(hdc, NULL, -1, &rect, 0);
278 if (conform_xp)
279 ok(textheight!=0,"Failed to get textheight from DrawTextA\n");
280 textheight = DrawTextExA(hdc, NULL, -1, &rect, 0, NULL );
281 if (conform_xp)
282 ok(textheight!=0,"Failed to get textheight from DrawTextExA\n");
283 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
284 heightcheck = textheight = DrawTextA(hdc, NULL, 10, &rect, 0);
285 ok(textheight==0,"Got textheight from DrawTextA\n");
286 textheight = DrawTextExA(hdc, NULL, 10, &rect, 0, NULL );
287 ok(textheight==0,"Got textheight from DrawTextA\n");
288 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
289
290
291 /* invalid dtp size test */
292 dtp.cbSize = -1; /* Invalid */
293 dtp.uiLengthDrawn = 1337;
294 textheight = DrawTextExA(hdc, text, 0, &rect, 0, &dtp);
295 ok(textheight==0,"Got textheight from DrawTextExA\n");
296 ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
297 dtp.uiLengthDrawn = 1337;
298 textheight = DrawTextExA(hdc, emptystring, 0, &rect, 0, &dtp);
299 ok(textheight==0,"Got textheight from DrawTextExA\n");
300 ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
301 dtp.uiLengthDrawn = 1337;
302 textheight = DrawTextExA(hdc, NULL, 0, &rect, 0, &dtp);
303 ok(textheight==0,"Got textheight from DrawTextExA\n");
304 ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
305 dtp.uiLengthDrawn = 1337;
306 textheight = DrawTextExA(hdc, emptystring, -1, &rect, 0, &dtp);
307 ok(textheight==0,"Got textheight from DrawTextExA\n");
308 ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
309 dtp.uiLengthDrawn = 1337;
310 textheight = DrawTextExA(hdc, NULL, -1, &rect, 0, &dtp);
311 ok(textheight==0,"Got textheight from DrawTextExA\n");
312 ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
313
314 /* Margin calculations */
315 dtp.cbSize = sizeof(dtp);
316 dtp.iLeftMargin = 0;
317 dtp.iRightMargin = 0;
318 SetRect( &rect, 0, 0, 0, 0);
319 DrawTextExA(hdc, text, -1, &rect, DT_CALCRECT, &dtp);
320 textlen = rect.right; /* Width without margin */
321 dtp.iLeftMargin = 8;
322 SetRect( &rect, 0, 0, 0, 0);
323 DrawTextExA(hdc, text, -1, &rect, DT_CALCRECT, &dtp);
324 ok(rect.right==dtp.iLeftMargin+textlen ,"Incorrect left margin calculated rc(%d,%d)\n", rect.left, rect.right);
325 dtp.iLeftMargin = 0;
326 dtp.iRightMargin = 8;
327 SetRect( &rect, 0, 0, 0, 0);
328 DrawTextExA(hdc, text, -1, &rect, DT_CALCRECT, &dtp);
329 ok(rect.right==dtp.iRightMargin+textlen ,"Incorrect right margin calculated rc(%d,%d)\n", rect.left, rect.right);
330
331 /* Wide char versions */
332 SetRect( &rect, 10,10, 100, 100);
333 SetLastError( 0);
334 heightcheck = textheight = DrawTextExW(hdc, textW, 0, &rect, DT_CALCRECT, NULL );
335 if( GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) {
336 ok( !EMPTY(rect) && !MODIFIED(rect),
337 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
338 rect.left, rect.top, rect.right, rect.bottom );
339 ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
340
341 SetRect( &rect, 10,10, 100, 100);
342 textheight = DrawTextW(hdc, textW, 0, &rect, DT_CALCRECT);
343 ok( !EMPTY(rect) && !MODIFIED(rect),
344 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
345 rect.left, rect.top, rect.right, rect.bottom );
346 ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
347 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
348
349 SetRect( &rect, 10,10, 100, 100);
350 heightcheck = textheight = DrawTextExW(hdc, emptystringW, -1, &rect, DT_CALCRECT, NULL );
351 ok( EMPTY(rect),
352 "rectangle should be empty got %d,%d-%d,%d\n",
353 rect.left, rect.top, rect.right, rect.bottom );
354 ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
355
356 SetRect( &rect, 10,10, 100, 100);
357 textheight = DrawTextW(hdc, emptystringW, -1, &rect, DT_CALCRECT);
358 ok( EMPTY(rect),
359 "rectangle should be empty got %d,%d-%d,%d\n",
360 rect.left, rect.top, rect.right, rect.bottom );
361 ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
362 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
363
364 SetRect( &rect, 10,10, 100, 100);
365 heightcheck = textheight = DrawTextExW(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
366 ok( !EMPTY(rect) && !MODIFIED(rect),
367 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
368 rect.left, rect.top, rect.right, rect.bottom );
369 if (textheight) /* windows 2000 */
370 {
371 if (conform_xp)
372 win_skip("XP conformity failed, skipping XP tests. Probably win 2000\n");
373 conform_xp = FALSE;
374 }
375 else
376 ok(textheight==0,"Got textheight from DrawTextExW\n");
377
378 SetRect( &rect, 10,10, 100, 100);
379 textheight = DrawTextW(hdc, NULL, 0, &rect, DT_CALCRECT);
380 ok( !EMPTY(rect) && !MODIFIED(rect),
381 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
382 rect.left, rect.top, rect.right, rect.bottom );
383 if (conform_xp)
384 ok(textheight==0,"Got textheight from DrawTextW\n");
385 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
386
387 if (conform_xp) {
388 /* Crashes on NT4 */
389 SetRect( &rect, 10,10, 100, 100);
390 heightcheck = textheight = DrawTextExW(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
391 ok( !EMPTY(rect) && !MODIFIED(rect),
392 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
393 rect.left, rect.top, rect.right, rect.bottom );
394 ok(textheight==0,"Got textheight from DrawTextExW\n");
395
396 SetRect( &rect, 10,10, 100, 100);
397 textheight = DrawTextW(hdc, NULL, -1, &rect, DT_CALCRECT);
398 ok( !EMPTY(rect) && !MODIFIED(rect),
399 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
400 rect.left, rect.top, rect.right, rect.bottom );
401 ok(textheight==0,"Got textheight from DrawTextW\n");
402 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
403 }
404
405
406 /* DT_SINGLELINE tests */
407
408 heightcheck = textheight = DrawTextExW(hdc, textW, 0, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
409 ok( !EMPTY(rect) && !MODIFIED(rect),
410 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
411 rect.left, rect.top, rect.right, rect.bottom );
412 ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
413
414 SetRect( &rect, 10,10, 100, 100);
415 textheight = DrawTextW(hdc, textW, 0, &rect, DT_CALCRECT|DT_SINGLELINE);
416 ok( !EMPTY(rect) && !MODIFIED(rect),
417 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
418 rect.left, rect.top, rect.right, rect.bottom );
419 ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
420 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
421
422 SetRect( &rect, 10,10, 100, 100);
423 heightcheck = textheight = DrawTextExW(hdc, emptystringW, -1, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
424 ok( !EMPTY(rect) && MODIFIED(rect),
425 "rectangle should be modified got %d,%d-%d,%d\n",
426 rect.left, rect.top, rect.right, rect.bottom );
427 ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
428
429 SetRect( &rect, 10,10, 100, 100);
430 textheight = DrawTextW(hdc, emptystringW, -1, &rect, DT_CALCRECT|DT_SINGLELINE);
431 ok( !EMPTY(rect) && MODIFIED(rect),
432 "rectangle should be modified got %d,%d-%d,%d\n",
433 rect.left, rect.top, rect.right, rect.bottom );
434 ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
435 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
436
437 if (conform_xp) {
438 /* Crashes on NT4 */
439 SetRect( &rect, 10,10, 100, 100);
440 heightcheck = textheight = DrawTextExW(hdc, NULL, -1, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
441 ok( !EMPTY(rect) && !MODIFIED(rect),
442 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
443 rect.left, rect.top, rect.right, rect.bottom );
444 ok(textheight==0,"Got textheight from DrawTextExW\n");
445
446 SetRect( &rect, 10,10, 100, 100);
447 textheight = DrawTextW(hdc, NULL, -1, &rect, DT_CALCRECT|DT_SINGLELINE);
448 ok( !EMPTY(rect) && !MODIFIED(rect),
449 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
450 rect.left, rect.top, rect.right, rect.bottom );
451 ok(textheight==0,"Got textheight from DrawTextW\n");
452 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
453 }
454
455 SetRect( &rect, 10,10, 100, 100);
456 heightcheck = textheight = DrawTextExW(hdc, NULL, 0, &rect, DT_CALCRECT|DT_SINGLELINE, NULL );
457 ok( !EMPTY(rect) && !MODIFIED(rect),
458 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
459 rect.left, rect.top, rect.right, rect.bottom );
460 if (conform_xp)
461 ok(textheight==0,"Got textheight from DrawTextExW\n");
462
463 SetRect( &rect, 10,10, 100, 100);
464 textheight = DrawTextW(hdc, NULL, 0, &rect, DT_CALCRECT|DT_SINGLELINE);
465 ok( !EMPTY(rect) && !MODIFIED(rect),
466 "rectangle should NOT be empty and NOT modified got %d,%d-%d,%d\n",
467 rect.left, rect.top, rect.right, rect.bottom );
468 if (conform_xp)
469 ok(textheight==0,"Got textheight from DrawTextW\n");
470 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
471
472 /* further tests with NULL and empty strings */
473 heightcheck = textheight = DrawTextW(hdc, textW, 0, &rect, 0);
474 ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
475 textheight = DrawTextExW(hdc, textW, 0, &rect, 0, NULL );
476 ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
477 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
478 heightcheck = textheight = DrawTextW(hdc, emptystringW, 0, &rect, 0);
479 ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
480 textheight = DrawTextExW(hdc, emptystringW, 0, &rect, 0, NULL );
481 ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
482 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
483 heightcheck = textheight = DrawTextW(hdc, NULL, 0, &rect, 0);
484 if (conform_xp)
485 ok(textheight==0,"Got textheight from DrawTextW\n");
486 textheight = DrawTextExW(hdc, NULL, 0, &rect, 0, NULL );
487 if (conform_xp)
488 ok(textheight==0,"Got textheight from DrawTextExW\n");
489 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
490 heightcheck = textheight = DrawTextW(hdc, emptystringW, -1, &rect, 0);
491 ok(textheight!=0,"Failed to get textheight from DrawTextW\n");
492 textheight = DrawTextExW(hdc, emptystringW, -1, &rect, 0, NULL );
493 ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
494 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
495 if (conform_xp) {
496 /* Crashes on NT4 */
497 heightcheck = textheight = DrawTextW(hdc, NULL, -1, &rect, 0);
498 ok(textheight==0,"Got textheight from DrawTextW\n");
499 textheight = DrawTextExW(hdc, NULL, -1, &rect, 0, NULL );
500 ok(textheight==0,"Got textheight from DrawTextExW\n");
501 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
502 heightcheck = textheight = DrawTextW(hdc, NULL, 10, &rect, 0);
503 ok(textheight==0,"Got textheight from DrawTextW\n");
504 textheight = DrawTextExW(hdc, NULL, 10, &rect, 0, NULL );
505 ok(textheight==0,"Got textheight from DrawTextW\n");
506 ok(textheight == heightcheck,"DrawTextEx and DrawText differ in return\n");
507 }
508
509 dtp.cbSize = -1; /* Invalid */
510 dtp.uiLengthDrawn = 1337;
511 textheight = DrawTextExW(hdc, textW, 0, &rect, 0, &dtp);
512 ok(textheight!=0,"Failed to get textheight from DrawTextExW\n");
513 ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
514 dtp.uiLengthDrawn = 1337;
515 textheight = DrawTextExW(hdc, emptystringW, 0, &rect, 0, &dtp);
516 if (conform_xp)
517 ok(textheight==0,"Got textheight from DrawTextExW\n");
518 ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
519 dtp.uiLengthDrawn = 1337;
520 textheight = DrawTextExW(hdc, NULL, 0, &rect, 0, &dtp);
521 if (conform_xp)
522 ok(textheight==0,"Got textheight from DrawTextExW\n");
523 ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
524 dtp.uiLengthDrawn = 1337;
525 textheight = DrawTextExW(hdc, emptystringW, -1, &rect, 0, &dtp);
526 ok(textheight==0,"Got textheight from DrawTextExW\n");
527 ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
528 if (conform_xp) {
529 /* Crashes on NT4 */
530 dtp.uiLengthDrawn = 1337;
531 textheight = DrawTextExW(hdc, NULL, -1, &rect, 0, &dtp);
532 ok(textheight==0,"Got textheight from DrawTextExW\n");
533 ok(dtp.uiLengthDrawn==1337, "invalid dtp.uiLengthDrawn = %i\n",dtp.uiLengthDrawn);
534 }
535 }
536
537 /* More test cases from bug 12226 */
538 SetRect(&rect, 0, 0, 0, 0);
539 textheight = DrawTextA(hdc, emptystring, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
540 ok(textheight, "DrawTextA error %u\n", GetLastError());
541 ok(0 == rect.left, "expected 0, got %d\n", rect.left);
542 ok(0 == rect.right, "expected 0, got %d\n", rect.right);
543 ok(0 == rect.top, "expected 0, got %d\n", rect.top);
544 ok(rect.bottom, "rect.bottom should not be 0\n");
545
546 SetRect(&rect, 0, 0, 0, 0);
547 textheight = DrawTextW(hdc, emptystringW, -1, &rect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE);
548 if (!textheight && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
549 {
550 win_skip( "DrawTextW not implemented\n" );
551 }
552 else
553 {
554 ok(textheight, "DrawTextW error %u\n", GetLastError());
555 ok(0 == rect.left, "expected 0, got %d\n", rect.left);
556 ok(0 == rect.right, "expected 0, got %d\n", rect.right);
557 ok(0 == rect.top, "expected 0, got %d\n", rect.top);
558 ok(rect.bottom, "rect.bottom should not be 0\n");
559 }
560
561 SetRect(&rect, 0, 0, 1, 1);
562 heightcheck = DrawTextA(hdc, wordbreak_text, -1, &rect, DT_CALCRECT);
563 SetRect(&rect, 0, 0, 1, 1);
564 textheight = DrawTextA(hdc, wordbreak_text, -1, &rect, DT_CALCRECT | DT_WORDBREAK);
565 ok(textheight == heightcheck * 2, "Got unexpected textheight %d, expected %d.\n",
566 textheight, heightcheck * 2);
567
568 SetRect(&rect, 0, 0, 1, 1);
569 heightcheck = DrawTextW(hdc, wordbreak_textW, -1, &rect, DT_CALCRECT);
570 SetRect(&rect, 0, 0, 1, 1);
571 textheight = DrawTextW(hdc, wordbreak_textW, -1, &rect, DT_CALCRECT | DT_WORDBREAK);
572 ok(textheight == heightcheck * 2, "Got unexpected textheight %d, expected %d.\n",
573 textheight, heightcheck * 2);
574
575 SelectObject(hdc, hOldFont);
576 ret = DeleteObject(hFont);
577 ok( ret, "DeleteObject error %u\n", GetLastError());
578
579 /* Clean up */
580 ret = ReleaseDC(hwnd, hdc);
581 ok( ret, "ReleaseDC error %u\n", GetLastError());
582 ret = DestroyWindow(hwnd);
583 ok( ret, "DestroyWindow error %u\n", GetLastError());
584 }
585
586 /* replace tabs by \t */
587 static void strfmt( const char *str, char *strout)
588 {
589 unsigned int i,j ;
590 for(i=0,j=0;i<=strlen(str);i++,j++)
591 if((strout[j]=str[i])=='\t') {
592 strout[j++]='\\';
593 strout[j]='t';
594 }
595 }
596
597
598 #define TABTEST( tabval, tabcount, string, _exp) \
599 { int i,x_act, x_exp; char strdisp[64];\
600 for(i=0;i<8;i++) tabs[i]=(i+1)*(tabval); \
601 extent = GetTabbedTextExtentA( hdc, string, strlen( string), (tabcount), tabs); \
602 strfmt( string, strdisp); \
603 /* trace( "Extent is %08lx\n", extent); */\
604 x_act = LOWORD( extent); \
605 x_exp = (_exp); \
606 ok( x_act == x_exp, "Test case \"%s\". Text extent is %d, expected %d tab %d tabcount %d\n", \
607 strdisp, x_act, x_exp, tabval, tabcount); \
608 } \
609
610
611 static void test_TabbedText(void)
612 {
613 HWND hwnd;
614 HDC hdc;
615 BOOL ret;
616 TEXTMETRICA tm;
617 DWORD extent;
618 INT tabs[8], cx, cy, tab, tabcount,t,align;
619
620 /* Initialization */
621 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
622 0, 0, 200, 200, 0, 0, 0, NULL);
623 ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
624 hdc = GetDC(hwnd);
625 ok(hdc != 0, "GetDC error %u\n", GetLastError());
626
627 ret = GetTextMetricsA( hdc, &tm);
628 ok( ret, "GetTextMetrics error %u\n", GetLastError());
629
630 extent = GetTabbedTextExtentA( hdc, "x", 0, 1, tabs);
631 ok( extent == 0, "GetTabbedTextExtentA returned non-zero on nCount == 0\n");
632
633 extent = GetTabbedTextExtentA( hdc, "x", 1, 1, tabs);
634 cx = LOWORD( extent);
635 cy = HIWORD( extent);
636 trace( "cx is %d cy is %d\n", cx, cy);
637
638 align=1;
639 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
640 catch the one off errors */
641 tab = (cx *4 + t);
642 /* test the special case tabcount =1 and the general array (80 of tabs */
643 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
644 TABTEST( align * tab, tabcount, "\t", tab)
645 TABTEST( align * tab, tabcount, "xxx\t", tab)
646 TABTEST( align * tab, tabcount, "\tx", tab+cx)
647 TABTEST( align * tab, tabcount, "\t\t", tab*2)
648 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
649 TABTEST( align * tab, tabcount, "x\tx", tab+cx)
650 TABTEST( align * tab, tabcount, "xx\tx", tab+cx)
651 TABTEST( align * tab, tabcount, "xxx\tx", tab+cx)
652 TABTEST( align * tab, tabcount, "xxxx\tx", t>0 ? tab + cx : 2*tab+cx)
653 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab+cx)
654 }
655 }
656 align=-1;
657 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
658 catch the one off errors */
659 tab = (cx *4 + t);
660 /* test the special case tabcount =1 and the general array (8) of tabs */
661 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
662 TABTEST( align * tab, tabcount, "\t", tab)
663 TABTEST( align * tab, tabcount, "xxx\t", tab)
664 TABTEST( align * tab, tabcount, "\tx", tab)
665 TABTEST( align * tab, tabcount, "\t\t", tab*2)
666 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
667 TABTEST( align * tab, tabcount, "x\tx", tab)
668 TABTEST( align * tab, tabcount, "xx\tx", tab)
669 TABTEST( align * tab, tabcount, "xxx\tx", 4 * cx >= tab ? 2*tab :tab)
670 TABTEST( align * tab, tabcount, "xxxx\tx", 2*tab)
671 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab)
672 }
673 }
674
675 ReleaseDC( hwnd, hdc );
676 DestroyWindow( hwnd );
677 }
678
679 static void test_DrawState(void)
680 {
681 static const char text[] = "Sample text string";
682 HWND hwnd;
683 HDC hdc;
684 BOOL ret;
685
686 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
687 0, 0, 200, 200, 0, 0, 0, NULL);
688 assert(hwnd);
689
690 hdc = GetDC(hwnd);
691 assert(hdc);
692
693 SetLastError(0xdeadbeef);
694 ret = DrawStateA(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, strlen(text),
695 0, 0, 10, 10, DST_TEXT);
696 ok(ret, "DrawState error %u\n", GetLastError());
697
698 SetLastError(0xdeadbeef);
699 ret = DrawStateA(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, 0,
700 0, 0, 10, 10, DST_TEXT);
701 ok(ret, "DrawState error %u\n", GetLastError());
702
703 SetLastError(0xdeadbeef);
704 ret = DrawStateA(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, strlen(text),
705 0, 0, 10, 10, DST_TEXT);
706 ok(!ret || broken(ret) /* win98 */, "DrawState succeeded\n");
707 ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
708
709 SetLastError(0xdeadbeef);
710 ret = DrawStateA(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, 0,
711 0, 0, 10, 10, DST_TEXT);
712 ok(!ret || broken(ret) /* win98 */, "DrawState succeeded\n");
713 ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
714
715 ReleaseDC(hwnd, hdc);
716 DestroyWindow(hwnd);
717 }
718
719 START_TEST(text)
720 {
721 test_TabbedText();
722 test_DrawTextCalcRect();
723 test_DrawState();
724 }