[COMCTL32] -These "border sizes" are as good as hardcoded.
[reactos.git] / rostests / apitests / comctl32 / button.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for Button window class v6
5 * PROGRAMMERS: Giannis Adamopoulos
6 */
7
8 #include "wine/test.h"
9 #include <windows.h>
10 #include <commctrl.h>
11 #include <uxtheme.h>
12
13 #define ok_rect(rc, l,r,t,b) ok((rc.left == (l)) && (rc.right == (r)) && (rc.top == (t)) && (rc.bottom == (b)), "Wrong rect. expected %d, %d, %d, %d got %ld, %ld, %ld, %ld\n", l,t,r,b, rc.left, rc.top, rc.right, rc.bottom)
14 #define ok_size(s, width, height) ok((s.cx == (width) && s.cy == (height)), "Expected size (%lu,%lu) got (%lu,%lu)\n", width, height, s.cx, s.cy)
15
16 void Test_TextMargin()
17 {
18 RECT rc;
19 BOOL ret;
20 HWND hwnd1;
21
22 hwnd1 = CreateWindowW(L"Button", L"Test1", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
23 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
24 SetWindowTheme(hwnd1, L"", L"");
25
26 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
27 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
28 ok_rect(rc, 1, 1, 1, 1);
29
30 SetRect(&rc, 0,0,0,0);
31 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
32 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
33
34 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
35 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
36 ok_rect(rc, 0, 0, 0, 0);
37
38 SetRect(&rc, -1,-1,-1,-1);
39 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
40 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
41
42 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
43 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
44 ok_rect(rc, -1, -1, -1, -1);
45
46 SetRect(&rc, 1000,1000,1000,1000);
47 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
48 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
49
50 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
51 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
52 ok_rect(rc, 1000, 1000, 1000, 1000);
53
54 DestroyWindow(hwnd1);
55 }
56
57 void Test_Imagelist()
58 {
59 HWND hwnd1;
60 BOOL ret;
61 BUTTON_IMAGELIST imlData;
62
63 hwnd1 = CreateWindowW(L"Button", L"Test2", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
64 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
65
66 ret = SendMessageW(hwnd1, BCM_GETIMAGELIST, 0, (LPARAM)&imlData);
67 ok (ret == TRUE, "Expected BCM_GETIMAGELIST to succeed\n");
68 ok (imlData.himl == 0, "Expected 0 himl\n");
69 ok (imlData.uAlign == 0, "Expected 0 uAlign\n");
70 ok_rect(imlData.margin, 0, 0, 0, 0);
71
72 SetRect(&imlData.margin, 0,0,0,1);
73 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
74 ok (ret == FALSE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
75
76 imlData.himl = (HIMAGELIST)0xdead;
77 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
78 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
79
80 ret = SendMessageW(hwnd1, BCM_GETIMAGELIST, 0, (LPARAM)&imlData);
81 ok (ret == TRUE, "Expected BCM_GETIMAGELIST to succeed\n");
82 ok (imlData.himl == (HIMAGELIST)0xdead, "Expected 0 himl\n");
83 ok (imlData.uAlign == 0, "Expected 0 uAlign\n");
84 ok_rect(imlData.margin, 0, 0, 0, 1);
85
86 imlData.himl = 0;
87 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
88 ok (ret == FALSE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
89
90 DestroyWindow(hwnd1);
91 }
92
93 void Test_GetIdealSizeNoThemes()
94 {
95 HWND hwnd1;
96 BOOL ret;
97 SIZE s, textent;
98 HFONT font;
99 HDC hdc;
100 HANDLE hbmp;
101 HIMAGELIST himl;
102 BUTTON_IMAGELIST imlData;
103 RECT rc;
104 LOGFONTW lf;
105
106 hwnd1 = CreateWindowW(L"Button", L" ", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
107 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
108 SetWindowTheme(hwnd1, L"", L"");
109
110 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
111 hdc = GetDC(hwnd1);
112 SelectObject(hdc, font);
113 GetTextExtentPoint32W(hdc, L" ", 1, &textent);
114
115 memset(&s, 0, sizeof(s));
116 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
117 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
118 ok_size(s, textent.cx + 5 + 2,
119 textent.cy + 7 + 2); /* the last +2 is the text margin */
120
121 DestroyWindow(hwnd1);
122
123
124
125
126 hwnd1 = CreateWindowW(L"Button", L"", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
127 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
128 SetWindowTheme(hwnd1, L"", L"");
129
130 memset(&s, 0, sizeof(s));
131 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
132 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
133 ok (s.cx > 80, "Expected big cx\n");
134 ok (s.cy > 80, "Expected big cy\n");
135
136 s.cx = 1;
137 s.cy = 1;
138 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
139 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
140 ok (s.cx > 80, "Expected big cx\n");
141 ok (s.cy > 80, "Expected big cy\n");
142
143 hbmp = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(5), IMAGE_BITMAP, 0, 0, 0);
144 ok (hbmp != 0, "Expected LoadImage to succeed\n");
145
146 SendMessageW(hwnd1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
147
148 memset(&s, 0, sizeof(s));
149 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
150 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
151 ok (s.cx > 80, "Expected big cx\n");
152 ok (s.cy > 80, "Expected big cy\n");
153
154 himl = ImageList_LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(5), 1, 1, 0, IMAGE_BITMAP, 0);
155 ok (himl != 0, "Expected ImageList_LoadImage to succeed\n");
156
157 memset(&imlData, 0, sizeof(imlData));
158 imlData.himl = himl;
159 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
160 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
161
162 memset(&s, 0, sizeof(s));
163 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
164 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
165 ok (s.cx > 80, "Expected big cx\n");
166 ok (s.cy > 80, "Expected big cy\n");
167
168 DestroyWindow(hwnd1);
169
170
171
172
173 hwnd1 = CreateWindowW(L"Button", L" ", BS_BITMAP , 10, 10, 100, 100, 0, NULL, NULL, NULL);
174 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
175 SetWindowTheme(hwnd1, L"", L"");
176
177 SendMessageW(hwnd1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
178
179 memset(&s, 0, sizeof(s));
180 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
181 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
182
183 /* In xp and 2k3 the image is ignored, in vista+ its width is added to the text width */
184 ok_size(s, textent.cx + 5 + 2,
185 textent.cy + 7 + 2); /* the last +2 is the text margin */
186
187 DestroyWindow(hwnd1);
188
189
190
191
192 hwnd1 = CreateWindowW(L"Button", L" ", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
193 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
194 SetWindowTheme(hwnd1, L"", L"");
195
196 SetRect(&rc, 0,0,0,0);
197 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
198 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
199
200 memset(&s, 0, sizeof(s));
201 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
202 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
203 ok_size(s, textent.cx + 5,
204 textent.cy + 7);
205
206 SetRect(&rc, 50,50,50,50);
207 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
208 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
209
210 memset(&s, 0, sizeof(s));
211 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
212 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
213 ok_size(s, textent.cx + 5 + 100,
214 textent.cy + 7 + 100);
215
216 SetRect(&rc, 1,1,1,1);
217 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
218 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
219
220 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
221 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
222
223 memset(&s, 0, sizeof(s));
224 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
225 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
226 ok_size(s, textent.cx + 5 + 2 + 1, /* we get an extra pixel due to the iml */
227 textent.cy + 7 + 2);
228
229 s.cx = 1;
230 s.cy = 1;
231 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
232 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
233 ok_size(s, textent.cx + 5 + 2 + 1,
234 textent.cy + 7 + 2);
235
236 s.cx = 100;
237 s.cy = 100;
238 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
239 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
240 ok_size(s, textent.cx + 5 + 2 + 1,
241 textent.cy + 7 + 2);
242
243 SetRect(&imlData.margin, 1,1,1,1);
244 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
245 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
246
247 memset(&s, 0, sizeof(s));
248 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
249 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
250 /* expected width = text width + hardcoded value + text margins + image width + image margins */
251 ok_size(s, textent.cx + 5 + 2 + 1 + 2,
252 textent.cy + 7 + 2);
253
254 SetRect(&imlData.margin, 50,50,50,50);
255 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
256 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
257
258 memset(&s, 0, sizeof(s));
259 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
260 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
261 /* image + its margins is so big that the height is dictated by them */
262 ok_size(s, textent.cx + 5 + 2 + 1 + 100, 101);
263
264 DestroyWindow(hwnd1);
265
266
267
268
269 /* Test again with some real text to see if the formula is correct */
270 hwnd1 = CreateWindowW(L"Button", L"Some test text", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
271 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
272 SetWindowTheme(hwnd1, L"", L"");
273
274 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
275 hdc = GetDC(hwnd1);
276 SelectObject(hdc, font);
277 GetTextExtentPoint32W(hdc, L"Some test text", 14, &textent);
278
279 memset(&s, 0, sizeof(s));
280 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
281 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
282 ok_size(s, textent.cx + 5 + 2, /* the last +2 is the text margin */
283 textent.cy + 7 + 2);
284
285 /* The hardcoded values are independent of the margin */
286 lf.lfHeight = 200;
287 lf.lfWidth = 200;
288 wcscpy(lf.lfFaceName, L"Arial");
289 font = CreateFontIndirectW(&lf);
290 ok(font != NULL, "\n");
291 SendMessageW(hwnd1, WM_SETFONT, (WPARAM)font, FALSE);
292
293 SelectObject(hdc, font);
294 GetTextExtentPoint32W(hdc, L"Some test text", 14, &textent);
295
296 memset(&s, 0, sizeof(s));
297 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
298 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
299 ok_size(s, textent.cx + 5 + 2, /* the last +2 is the text margin */
300 textent.cy + 7 + 2);
301
302 DestroyWindow(hwnd1);
303 }
304
305 START_TEST(button)
306 {
307 LoadLibraryW(L"comctl32.dll"); /* same as statically linking to comctl32 and doing InitCommonControls */
308 Test_TextMargin();
309 Test_Imagelist();
310 Test_GetIdealSizeNoThemes();
311 }
312