4 * Copyright (c) 2004 Zach Gorman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 #include "wine/test.h"
31 static void test_DrawTextCalcRect(void)
35 HFONT hFont
, hOldFont
;
37 const char text
[] = "Example text for testing DrawText in "
39 const WCHAR textW
[] = {'W','i','d','e',' ','c','h','a','r',' ',
40 's','t','r','i','n','g','\0'};
41 const WCHAR emptystringW
[] = { 0 };
42 INT textlen
,textheight
;
43 RECT rect
= { 0, 0, 100, 0 };
47 hwnd
= CreateWindowExA(0, "static", NULL
, WS_POPUP
,
48 0, 0, 200, 200, 0, 0, 0, NULL
);
49 ok(hwnd
!= 0, "CreateWindowExA error %lu\n", GetLastError());
51 ok(hdc
!= 0, "GetDC error %lu\n", GetLastError());
52 trace("hdc %p\n", hdc
);
53 textlen
= lstrlenA(text
);
55 /* LOGFONT initialization */
56 memset(&lf
, 0, sizeof(lf
));
57 lf
.lfCharSet
= ANSI_CHARSET
;
58 lf
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
59 lf
.lfWeight
= FW_DONTCARE
;
60 lf
.lfHeight
= 0; /* mapping mode dependent */
61 lf
.lfQuality
= DEFAULT_QUALITY
;
62 lstrcpyA(lf
.lfFaceName
, "Arial");
64 /* DrawText in MM_HIENGLISH with DT_CALCRECT */
65 SetMapMode(hdc
, MM_HIENGLISH
);
66 lf
.lfHeight
= 100 * 9 / 72; /* 9 point */
67 hFont
= CreateFontIndirectA(&lf
);
68 ok(hFont
!= 0, "CreateFontIndirectA error %lu\n",
70 hOldFont
= SelectObject(hdc
, hFont
);
72 textheight
= DrawTextA(hdc
, text
, textlen
, &rect
, DT_CALCRECT
|
73 DT_EXTERNALLEADING
| DT_WORDBREAK
| DT_NOCLIP
| DT_LEFT
|
75 ok( textheight
, "DrawTextA error %lu\n", GetLastError());
77 trace("MM_HIENGLISH rect.bottom %ld\n", rect
.bottom
);
78 todo_wine
ok(rect
.bottom
< 0, "In MM_HIENGLISH, DrawText with "
79 "DT_CALCRECT should return a negative rectangle bottom. "
80 "(bot=%ld)\n", rect
.bottom
);
82 SelectObject(hdc
, hOldFont
);
83 ret
= DeleteObject(hFont
);
84 ok( ret
, "DeleteObject error %lu\n", GetLastError());
87 /* DrawText in MM_TEXT with DT_CALCRECT */
88 SetMapMode(hdc
, MM_TEXT
);
89 lf
.lfHeight
= -MulDiv(9, GetDeviceCaps(hdc
,
90 LOGPIXELSY
), 72); /* 9 point */
91 hFont
= CreateFontIndirectA(&lf
);
92 ok(hFont
!= 0, "CreateFontIndirectA error %lu\n",
94 hOldFont
= SelectObject(hdc
, hFont
);
96 textheight
= DrawTextA(hdc
, text
, textlen
, &rect
, DT_CALCRECT
|
97 DT_EXTERNALLEADING
| DT_WORDBREAK
| DT_NOCLIP
| DT_LEFT
|
99 ok( textheight
, "DrawTextA error %lu\n", GetLastError());
101 trace("MM_TEXT rect.bottom %ld\n", rect
.bottom
);
102 ok(rect
.bottom
> 0, "In MM_TEXT, DrawText with DT_CALCRECT "
103 "should return a positive rectangle bottom. (bot=%ld)\n",
106 /* empty or null text should in some cases calc an empty rectangle */
107 /* note: testing the function's return value is useless, it differs
108 * ( 0 or 1) on every Windows version I tried */
109 SetRect( &rect
, 10,10, 100, 100);
110 textheight
= DrawTextExA(hdc
, (char *) text
, 0, &rect
, DT_CALCRECT
, NULL
);
111 ok( !(rect
.left
== rect
.right
&& rect
.bottom
== rect
.top
),
112 "rectangle should NOT be empty.\n");
113 SetRect( &rect
, 10,10, 100, 100);
115 textheight
= DrawTextExA(hdc
, "", -1, &rect
, DT_CALCRECT
, NULL
);
116 ok( (rect
.left
== rect
.right
&& rect
.bottom
== rect
.top
),
117 "rectangle should be empty.\n");
118 SetRect( &rect
, 10,10, 100, 100);
120 textheight
= DrawTextExA(hdc
, NULL
, -1, &rect
, DT_CALCRECT
, NULL
);
121 ok( (rect
.left
== rect
.right
&& rect
.bottom
== rect
.top
),
122 "rectangle should be empty.\n");
123 SetRect( &rect
, 10,10, 100, 100);
124 textheight
= DrawTextExA(hdc
, NULL
, 0, &rect
, DT_CALCRECT
, NULL
);
125 ok( !(rect
.left
== rect
.right
&& rect
.bottom
== rect
.top
),
126 "rectangle should NOT be empty.\n");
128 /* Wide char versions */
129 SetRect( &rect
, 10,10, 100, 100);
131 textheight
= DrawTextExW(hdc
, (WCHAR
*) textW
, 0, &rect
, DT_CALCRECT
, NULL
);
132 if( GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
) {
133 ok( !(rect
.left
== rect
.right
&& rect
.bottom
== rect
.top
),
134 "rectangle should NOT be empty.\n");
135 SetRect( &rect
, 10,10, 100, 100);
136 textheight
= DrawTextExW(hdc
, (WCHAR
*) emptystringW
, -1, &rect
, DT_CALCRECT
, NULL
);
137 ok( (rect
.left
== rect
.right
&& rect
.bottom
== rect
.top
),
138 "rectangle should be empty.\n");
139 SetRect( &rect
, 10,10, 100, 100);
140 textheight
= DrawTextExW(hdc
, NULL
, -1, &rect
, DT_CALCRECT
, NULL
);
141 ok( !(rect
.left
== rect
.right
&& rect
.bottom
== rect
.top
),
142 "rectangle should NOT be empty.\n");
143 SetRect( &rect
, 10,10, 100, 100);
144 textheight
= DrawTextExW(hdc
, NULL
, 0, &rect
, DT_CALCRECT
, NULL
);
145 ok( !(rect
.left
== rect
.right
&& rect
.bottom
== rect
.top
),
146 "rectangle should NOT be empty.\n");
149 SelectObject(hdc
, hOldFont
);
150 ret
= DeleteObject(hFont
);
151 ok( ret
, "DeleteObject error %lu\n", GetLastError());
154 ret
= ReleaseDC(hwnd
, hdc
);
155 ok( ret
, "ReleaseDC error %lu\n", GetLastError());
156 ret
= DestroyWindow(hwnd
);
157 ok( ret
, "DestroyWindow error %lu\n", GetLastError());
160 /* replace tabs by \t */
161 static void strfmt( char *str
, char *strout
)
164 for(i
=0,j
=0;i
<=strlen(str
);i
++,j
++)
165 if((strout
[j
]=str
[i
])=='\t') {
172 #define TABTEST( tabval, tabcount, string, _exp) \
173 { int i,x_act, x_exp; char strdisp[64];\
174 for(i=0;i<8;i++) tabs[i]=(i+1)*(tabval); \
175 extent = GetTabbedTextExtentA( hdc, string, strlen( string), (tabcount), tabs); \
176 strfmt( string, strdisp); \
177 /* trace( "Extent is %08lx\n", extent); */\
178 x_act = LOWORD( extent); \
180 ok( x_act == x_exp, "Test case \"%s\". Text extent is %d, expected %d tab %d tabcount %d\n", \
181 strdisp, x_act, x_exp, tabval, tabcount); \
185 static void test_TabbedText(void)
192 INT tabs
[8], cx
, cy
, tab
, tabcount
,t
,align
;
195 hwnd
= CreateWindowExA(0, "static", NULL
, WS_POPUP
,
196 0, 0, 200, 200, 0, 0, 0, NULL
);
197 ok(hwnd
!= 0, "CreateWindowExA error %lu\n", GetLastError());
199 ok(hdc
!= 0, "GetDC error %lu\n", GetLastError());
201 ret
= GetTextMetricsA( hdc
, &tm
);
202 ok( ret
, "GetTextMetrics error %lu\n", GetLastError());
204 extent
= GetTabbedTextExtentA( hdc
, "x", 1, 1, tabs
);
205 cx
= LOWORD( extent
);
206 cy
= HIWORD( extent
);
207 trace( "cx is %d cy is %d\n", cx
, cy
);
210 for( t
=-1; t
<=1; t
++) { /* slightly adjust the 4 char tabstop, to
211 catch the one off errors */
213 /* test the special case tabcount =1 and the general array (80 of tabs */
214 for( tabcount
= 1; tabcount
<= 8; tabcount
+=7) {
215 TABTEST( align
* tab
, tabcount
, "\t", tab
)
216 TABTEST( align
* tab
, tabcount
, "xxx\t", tab
)
217 TABTEST( align
* tab
, tabcount
, "\tx", tab
+cx
)
218 TABTEST( align
* tab
, tabcount
, "\t\t", tab
*2)
219 TABTEST( align
* tab
, tabcount
, "\tx\t", tab
*2)
220 TABTEST( align
* tab
, tabcount
, "x\tx", tab
+cx
)
221 TABTEST( align
* tab
, tabcount
, "xx\tx", tab
+cx
)
222 TABTEST( align
* tab
, tabcount
, "xxx\tx", tab
+cx
)
223 TABTEST( align
* tab
, tabcount
, "xxxx\tx", t
>0 ? tab
+ cx
: 2*tab
+cx
)
224 TABTEST( align
* tab
, tabcount
, "xxxxx\tx", 2*tab
+cx
)
228 for( t
=-1; t
<=1; t
++) { /* slightly adjust the 4 char tabstop, to
229 catch the one off errors */
231 /* test the special case tabcount =1 and the general array (8) 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
)
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
)
239 TABTEST( align
* tab
, tabcount
, "xx\tx", tab
)
240 TABTEST( align
* tab
, tabcount
, "xxx\tx", 4 * cx
>= tab
? 2*tab
:tab
)
241 TABTEST( align
* tab
, tabcount
, "xxxx\tx", 2*tab
)
242 TABTEST( align
* tab
, tabcount
, "xxxxx\tx", 2*tab
)
252 test_DrawTextCalcRect();