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