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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/test.h"
30 static void test_DrawTextCalcRect(void)
34 HFONT hFont, hOldFont;
36 const char text[] = "Example text for testing DrawText in "
38 const WCHAR textW[] = {'W','i','d','e',' ','c','h','a','r',' ',
39 's','t','r','i','n','g','\0'};
40 const WCHAR emptystringW[] = { 0 };
41 INT textlen,textheight;
42 RECT rect = { 0, 0, 100, 0 };
46 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
47 0, 0, 200, 200, 0, 0, 0, NULL);
48 ok(hwnd != 0, "CreateWindowExA error %lu\n", GetLastError());
50 ok(hdc != 0, "GetDC error %lu\n", GetLastError());
51 trace("hdc %p\n", hdc);
52 textlen = lstrlenA(text);
54 /* LOGFONT initialization */
55 memset(&lf, 0, sizeof(lf));
56 lf.lfCharSet = ANSI_CHARSET;
57 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
58 lf.lfWeight = FW_DONTCARE;
59 lf.lfHeight = 0; /* mapping mode dependent */
60 lf.lfQuality = DEFAULT_QUALITY;
61 lstrcpyA(lf.lfFaceName, "Arial");
63 /* DrawText in MM_HIENGLISH with DT_CALCRECT */
64 SetMapMode(hdc, MM_HIENGLISH);
65 lf.lfHeight = 100 * 9 / 72; /* 9 point */
66 hFont = CreateFontIndirectA(&lf);
67 ok(hFont != 0, "CreateFontIndirectA error %lu\n",
69 hOldFont = SelectObject(hdc, hFont);
71 textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
72 DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
74 ok( textheight, "DrawTextA error %lu\n", GetLastError());
76 trace("MM_HIENGLISH rect.bottom %ld\n", rect.bottom);
77 todo_wine ok(rect.bottom < 0, "In MM_HIENGLISH, DrawText with "
78 "DT_CALCRECT should return a negative rectangle bottom. "
79 "(bot=%ld)\n", rect.bottom);
81 SelectObject(hdc, hOldFont);
82 ret = DeleteObject(hFont);
83 ok( ret, "DeleteObject error %lu\n", GetLastError());
86 /* DrawText in MM_TEXT with DT_CALCRECT */
87 SetMapMode(hdc, MM_TEXT);
88 lf.lfHeight = -MulDiv(9, GetDeviceCaps(hdc,
89 LOGPIXELSY), 72); /* 9 point */
90 hFont = CreateFontIndirectA(&lf);
91 ok(hFont != 0, "CreateFontIndirectA error %lu\n",
93 hOldFont = SelectObject(hdc, hFont);
95 textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
96 DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
98 ok( textheight, "DrawTextA error %lu\n", GetLastError());
100 trace("MM_TEXT rect.bottom %ld\n", rect.bottom);
101 ok(rect.bottom > 0, "In MM_TEXT, DrawText with DT_CALCRECT "
102 "should return a positive rectangle bottom. (bot=%ld)\n",
105 /* empty or null text should in some cases calc an empty rectangle */
106 /* note: testing the function's return value is useless, it differs
107 * ( 0 or 1) on every Windows version I tried */
108 SetRect( &rect, 10,10, 100, 100);
109 textheight = DrawTextExA(hdc, (char *) text, 0, &rect, DT_CALCRECT, NULL );
110 ok( !(rect.left == rect.right && rect.bottom == rect.top),
111 "rectangle should NOT be empty.\n");
112 SetRect( &rect, 10,10, 100, 100);
114 textheight = DrawTextExA(hdc, "", -1, &rect, DT_CALCRECT, NULL );
115 ok( (rect.left == rect.right && rect.bottom == rect.top),
116 "rectangle should be empty.\n");
117 SetRect( &rect, 10,10, 100, 100);
119 textheight = DrawTextExA(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
120 ok( (rect.left == rect.right && rect.bottom == rect.top),
121 "rectangle should be empty.\n");
122 SetRect( &rect, 10,10, 100, 100);
123 textheight = DrawTextExA(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
124 ok( !(rect.left == rect.right && rect.bottom == rect.top),
125 "rectangle should NOT be empty.\n");
127 /* Wide char versions */
128 SetRect( &rect, 10,10, 100, 100);
130 textheight = DrawTextExW(hdc, (WCHAR *) textW, 0, &rect, DT_CALCRECT, NULL );
131 if( GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) {
132 ok( !(rect.left == rect.right && rect.bottom == rect.top),
133 "rectangle should NOT be empty.\n");
134 SetRect( &rect, 10,10, 100, 100);
135 textheight = DrawTextExW(hdc, (WCHAR *) emptystringW, -1, &rect, DT_CALCRECT, NULL );
136 ok( (rect.left == rect.right && rect.bottom == rect.top),
137 "rectangle should be empty.\n");
138 SetRect( &rect, 10,10, 100, 100);
139 textheight = DrawTextExW(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
140 ok( !(rect.left == rect.right && rect.bottom == rect.top),
141 "rectangle should NOT be empty.\n");
142 SetRect( &rect, 10,10, 100, 100);
143 textheight = DrawTextExW(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
144 ok( !(rect.left == rect.right && rect.bottom == rect.top),
145 "rectangle should NOT be empty.\n");
148 SelectObject(hdc, hOldFont);
149 ret = DeleteObject(hFont);
150 ok( ret, "DeleteObject error %lu\n", GetLastError());
153 ret = ReleaseDC(hwnd, hdc);
154 ok( ret, "ReleaseDC error %lu\n", GetLastError());
155 ret = DestroyWindow(hwnd);
156 ok( ret, "DestroyWindow error %lu\n", GetLastError());
159 /* replace tabs by \t */
160 static void strfmt( char *str, char *strout)
163 for(i=0,j=0;i<=strlen(str);i++,j++)
164 if((strout[j]=str[i])=='\t') {
171 #define TABTEST( tabval, tabcount, string, _exp) \
172 { int i,x_act, x_exp; char strdisp[64];\
173 for(i=0;i<8;i++) tabs[i]=(i+1)*(tabval); \
174 extent = GetTabbedTextExtentA( hdc, string, strlen( string), (tabcount), tabs); \
175 strfmt( string, strdisp); \
176 /* trace( "Extent is %08lx\n", extent); */\
177 x_act = LOWORD( extent); \
179 ok( x_act == x_exp, "Test case \"%s\". Text extent is %d, expected %d tab %d tabcount %d\n", \
180 strdisp, x_act, x_exp, tabval, tabcount); \
184 static void test_TabbedText(void)
191 INT tabs[8], cx, cy, tab, tabcount,t,align;
194 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
195 0, 0, 200, 200, 0, 0, 0, NULL);
196 ok(hwnd != 0, "CreateWindowExA error %lu\n", GetLastError());
198 ok(hdc != 0, "GetDC error %lu\n", GetLastError());
200 ret = GetTextMetricsA( hdc, &tm);
201 ok( ret, "GetTextMetrics error %lu\n", GetLastError());
203 extent = GetTabbedTextExtentA( hdc, "x", 1, 1, tabs);
204 cx = LOWORD( extent);
205 cy = HIWORD( extent);
206 trace( "cx is %d cy is %d\n", cx, cy);
209 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
210 catch the one off errors */
212 /* test the special case tabcount =1 and the general array (80 of tabs */
213 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
214 TABTEST( align * tab, tabcount, "\t", tab)
215 TABTEST( align * tab, tabcount, "xxx\t", tab)
216 TABTEST( align * tab, tabcount, "\tx", tab+cx)
217 TABTEST( align * tab, tabcount, "\t\t", tab*2)
218 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
219 TABTEST( align * tab, tabcount, "x\tx", tab+cx)
220 TABTEST( align * tab, tabcount, "xx\tx", tab+cx)
221 TABTEST( align * tab, tabcount, "xxx\tx", tab+cx)
222 TABTEST( align * tab, tabcount, "xxxx\tx", t>0 ? tab + cx : 2*tab+cx)
223 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab+cx)
227 for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to
228 catch the one off errors */
230 /* test the special case tabcount =1 and the general array (8) of tabs */
231 for( tabcount = 1; tabcount <= 8; tabcount +=7) {
232 TABTEST( align * tab, tabcount, "\t", tab)
233 TABTEST( align * tab, tabcount, "xxx\t", tab)
234 TABTEST( align * tab, tabcount, "\tx", tab)
235 TABTEST( align * tab, tabcount, "\t\t", tab*2)
236 TABTEST( align * tab, tabcount, "\tx\t", tab*2)
237 TABTEST( align * tab, tabcount, "x\tx", tab)
238 TABTEST( align * tab, tabcount, "xx\tx", tab)
239 TABTEST( align * tab, tabcount, "xxx\tx", 4 * cx >= tab ? 2*tab :tab)
240 TABTEST( align * tab, tabcount, "xxxx\tx", 2*tab)
241 TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab)
251 test_DrawTextCalcRect();