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
24 #include "wine/test.h"
31 static void test_DrawTextCalcRect(void)
35 HFONT hFont, hOldFont;
37 static CHAR text[] = "Example text for testing DrawText in "
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 };
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());
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",
71 hOldFont = SelectObject(hdc, hFont);
73 textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
74 DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
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",
95 hOldFont = SelectObject(hdc, hFont);
97 textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
98 DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
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",
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);
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);
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");
129 /* Wide char versions */
130 SetRect( &rect, 10,10, 100, 100);
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");
150 SelectObject(hdc, hOldFont);
151 ret = DeleteObject(hFont);
152 ok( ret, "DeleteObject error %u\n", GetLastError());
155 ret = ReleaseDC(hwnd, hdc);
156 ok( ret, "ReleaseDC error %u\n", GetLastError());
157 ret = DestroyWindow(hwnd);
158 ok( ret, "DestroyWindow error %u\n", GetLastError());
161 /* replace tabs by \t */
162 static void strfmt( const char *str, char *strout)
165 for(i=0,j=0;i<=strlen(str);i++,j++)
166 if((strout[j]=str[i])=='\t') {
173 #define TABTEST( tabval, tabcount, string, _exp) \
174 { int i,x_act, x_exp; char strdisp[64];\
175 for(i=0;i<8;i++) tabs[i]=(i+1)*(tabval); \
176 extent = GetTabbedTextExtentA( hdc, string, strlen( string), (tabcount), tabs); \
177 strfmt( string, strdisp); \
178 /* trace( "Extent is %08lx\n", extent); */\
179 x_act = LOWORD( extent); \
181 ok( x_act == x_exp, "Test case \"%s\". Text extent is %d, expected %d tab %d tabcount %d\n", \
182 strdisp, x_act, x_exp, tabval, tabcount); \
186 static void test_TabbedText(void)
193 INT tabs[8], cx, cy, tab, tabcount,t,align;
196 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
197 0, 0, 200, 200, 0, 0, 0, NULL);
198 ok(hwnd != 0, "CreateWindowExA error %u\n", GetLastError());
200 ok(hdc != 0, "GetDC error %u\n", GetLastError());
202 ret = GetTextMetricsA( hdc, &tm);
203 ok( ret, "GetTextMetrics error %u\n", GetLastError());
205 extent = GetTabbedTextExtentA( hdc, "x", 1, 1, tabs);
206 cx = LOWORD( extent);
207 cy = HIWORD( extent);
208 trace( "cx is %d cy is %d\n", cx, cy);
211 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
212 catch the one off errors */
214 /* test the special case tabcount =1 and the general array (80 of tabs */
215 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
216 TABTEST( align * tab, tabcount, "\t", tab)
217 TABTEST( align * tab, tabcount, "xxx\t", tab)
218 TABTEST( align * tab, tabcount, "\tx", tab+cx)
219 TABTEST( align * tab, tabcount, "\t\t", tab*2)
220 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
221 TABTEST( align * tab, tabcount, "x\tx", tab+cx)
222 TABTEST( align * tab, tabcount, "xx\tx", tab+cx)
223 TABTEST( align * tab, tabcount, "xxx\tx", tab+cx)
224 TABTEST( align * tab, tabcount, "xxxx\tx", t>0 ? tab + cx : 2*tab+cx)
225 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab+cx)
229 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
230 catch the one off errors */
232 /* test the special case tabcount =1 and the general array (8) of tabs */
233 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
234 TABTEST( align * tab, tabcount, "\t", tab)
235 TABTEST( align * tab, tabcount, "xxx\t", tab)
236 TABTEST( align * tab, tabcount, "\tx", tab)
237 TABTEST( align * tab, tabcount, "\t\t", tab*2)
238 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
239 TABTEST( align * tab, tabcount, "x\tx", tab)
240 TABTEST( align * tab, tabcount, "xx\tx", tab)
241 TABTEST( align * tab, tabcount, "xxx\tx", 4 * cx >= tab ? 2*tab :tab)
242 TABTEST( align * tab, tabcount, "xxxx\tx", 2*tab)
243 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab)
247 ReleaseDC( hwnd, hdc );
248 DestroyWindow( hwnd );
251 static void test_DrawState(void)
253 static const char text[] = "Sample text string";
258 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
259 0, 0, 200, 200, 0, 0, 0, NULL);
265 SetLastError(0xdeadbeef);
266 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, strlen(text),
267 0, 0, 10, 10, DST_TEXT);
268 ok(ret, "DrawState error %u\n", GetLastError());
270 SetLastError(0xdeadbeef);
271 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, (LPARAM)text, 0,
272 0, 0, 10, 10, DST_TEXT);
273 ok(ret, "DrawState error %u\n", GetLastError());
275 SetLastError(0xdeadbeef);
276 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, strlen(text),
277 0, 0, 10, 10, DST_TEXT);
278 ok(!ret, "DrawState succeeded\n");
279 ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
281 SetLastError(0xdeadbeef);
282 ret = DrawState(hdc, GetStockObject(DKGRAY_BRUSH), NULL, 0, 0,
283 0, 0, 10, 10, DST_TEXT);
284 ok(!ret, "DrawState succeeded\n");
285 ok(GetLastError() == 0xdeadbeef, "not expected error %u\n", GetLastError());
287 ReleaseDC(hwnd, hdc);
294 test_DrawTextCalcRect();