befcfc620504cc851fb27f50b00dfaed9b6a9c75
[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
25 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
26 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
27 ok_rect(rc, 1, 1, 1, 1);
28
29 SetRect(&rc, 0,0,0,0);
30 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
31 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
32
33 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
34 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
35 ok_rect(rc, 0, 0, 0, 0);
36
37 SetRect(&rc, -1,-1,-1,-1);
38 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
39 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
40
41 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
42 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
43 ok_rect(rc, -1, -1, -1, -1);
44
45 SetRect(&rc, 1000,1000,1000,1000);
46 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
47 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
48
49 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
50 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
51 ok_rect(rc, 1000, 1000, 1000, 1000);
52
53 DestroyWindow(hwnd1);
54 }
55
56 void Test_Imagelist()
57 {
58 HWND hwnd1;
59 BOOL ret;
60 BUTTON_IMAGELIST imlData;
61
62 hwnd1 = CreateWindowW(L"Button", L"Test2", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
63 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
64
65 ret = SendMessageW(hwnd1, BCM_GETIMAGELIST, 0, (LPARAM)&imlData);
66 ok (ret == TRUE, "Expected BCM_GETIMAGELIST to succeed\n");
67 ok (imlData.himl == 0, "Expected 0 himl\n");
68 ok (imlData.uAlign == 0, "Expected 0 uAlign\n");
69 ok_rect(imlData.margin, 0, 0, 0, 0);
70
71 SetRect(&imlData.margin, 0,0,0,1);
72 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
73 ok (ret == FALSE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
74
75 imlData.himl = (HIMAGELIST)0xdead;
76 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
77 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
78
79 ret = SendMessageW(hwnd1, BCM_GETIMAGELIST, 0, (LPARAM)&imlData);
80 ok (ret == TRUE, "Expected BCM_GETIMAGELIST to succeed\n");
81 ok (imlData.himl == (HIMAGELIST)0xdead, "Expected 0 himl\n");
82 ok (imlData.uAlign == 0, "Expected 0 uAlign\n");
83 ok_rect(imlData.margin, 0, 0, 0, 1);
84
85 imlData.himl = 0;
86 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
87 ok (ret == FALSE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
88
89 DestroyWindow(hwnd1);
90 }
91
92 void Test_GetIdealSizeNoThemes()
93 {
94 HWND hwnd1;
95 BOOL ret;
96 SIZE s, textent;
97 HFONT font;
98 HDC hdc;
99 WINDOWINFO wi;
100 HANDLE hbmp;
101 HIMAGELIST himl;
102 BUTTON_IMAGELIST imlData;
103 RECT rc;
104
105 hwnd1 = CreateWindowW(L"Button", L" ", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
106 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
107 SetWindowTheme(hwnd1, L"", L"");
108
109 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
110 hdc = GetDC(hwnd1);
111 SelectObject(hdc, font);
112 GetTextExtentPoint32W(hdc, L" ", 1, &textent);
113
114 memset(&wi, 0, sizeof(wi));
115 GetWindowInfo(hwnd1, &wi);
116
117 memset(&s, 0, sizeof(s));
118 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
119 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
120 ok_size(s, textent.cx + 2 * wi.cxWindowBorders - 1 + 2,
121 textent.cy + 2 * wi.cyWindowBorders + 1 + 2); /* the last +2 is the text margin */
122
123 DestroyWindow(hwnd1);
124
125
126
127
128 hwnd1 = CreateWindowW(L"Button", L"", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
129 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
130 SetWindowTheme(hwnd1, L"", L"");
131
132 memset(&s, 0, sizeof(s));
133 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
134 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
135 ok (s.cx > 80, "Expected big cx\n");
136 ok (s.cy > 80, "Expected big cy\n");
137
138 s.cx = 1;
139 s.cy = 1;
140 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
141 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
142 ok (s.cx > 80, "Expected big cx\n");
143 ok (s.cy > 80, "Expected big cy\n");
144
145 hbmp = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(5), IMAGE_BITMAP, 0, 0, 0);
146 ok (hbmp != 0, "Expected LoadImage to succeed\n");
147
148 SendMessageW(hwnd1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
149
150 memset(&s, 0, sizeof(s));
151 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
152 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
153 ok (s.cx > 80, "Expected big cx\n");
154 ok (s.cy > 80, "Expected big cy\n");
155
156 himl = ImageList_LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(5), 1, 1, 0, IMAGE_BITMAP, 0);
157 ok (himl != 0, "Expected ImageList_LoadImage to succeed\n");
158
159 memset(&imlData, 0, sizeof(imlData));
160 imlData.himl = himl;
161 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
162 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
163
164 memset(&s, 0, sizeof(s));
165 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
166 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
167 ok (s.cx > 80, "Expected big cx\n");
168 ok (s.cy > 80, "Expected big cy\n");
169
170 DestroyWindow(hwnd1);
171
172
173
174
175 hwnd1 = CreateWindowW(L"Button", L" ", BS_BITMAP , 10, 10, 100, 100, 0, NULL, NULL, NULL);
176 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
177 SetWindowTheme(hwnd1, L"", L"");
178
179 SendMessageW(hwnd1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
180
181 memset(&s, 0, sizeof(s));
182 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
183 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
184
185 /* In xp and 2k3 the image is ignored, in vista+ its width is added to the text width */
186 ok_size(s, textent.cx + 2 * wi.cxWindowBorders - 1 + 2,
187 textent.cy + 2 * wi.cyWindowBorders + 1 + 2); /* the last +2 is the text margin */
188
189 DestroyWindow(hwnd1);
190
191
192
193
194 hwnd1 = CreateWindowW(L"Button", L" ", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
195 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
196 SetWindowTheme(hwnd1, L"", L"");
197
198 SetRect(&rc, 0,0,0,0);
199 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
200 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
201
202 memset(&s, 0, sizeof(s));
203 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
204 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
205 ok_size(s, textent.cx + 2 * wi.cxWindowBorders - 1,
206 textent.cy + 2 * wi.cyWindowBorders + 1);
207
208 SetRect(&rc, 50,50,50,50);
209 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
210 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
211
212 memset(&s, 0, sizeof(s));
213 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
214 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
215 ok_size(s, textent.cx + 2 * wi.cxWindowBorders - 1 + 100,
216 textent.cy + 2 * wi.cyWindowBorders + 1 + 100);
217
218 SetRect(&rc, 0,0,0,0);
219 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
220 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
221
222 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
223 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
224
225 memset(&s, 0, sizeof(s));
226 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
227 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
228 ok_size(s, textent.cx + 2 * wi.cxWindowBorders, /* we get an extra pixel due to the iml */
229 textent.cy + 2 * wi.cyWindowBorders + 1);
230
231 s.cx = 1;
232 s.cy = 1;
233 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
234 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
235 ok_size(s, textent.cx + 2 * wi.cxWindowBorders,
236 textent.cy + 2 * wi.cyWindowBorders + 1);
237
238 s.cx = 100;
239 s.cy = 100;
240 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
241 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
242 ok_size(s, textent.cx + 2 * wi.cxWindowBorders,
243 textent.cy + 2 * wi.cyWindowBorders + 1);
244
245 DestroyWindow(hwnd1);
246
247
248
249
250 /* Test again with some real text to see if the formula is correct */
251 hwnd1 = CreateWindowW(L"Button", L"Some test text", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
252 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
253 SetWindowTheme(hwnd1, L"", L"");
254
255 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
256 hdc = GetDC(hwnd1);
257 SelectObject(hdc, font);
258 GetTextExtentPoint32W(hdc, L"Some test text", 14, &textent);
259
260 memset(&s, 0, sizeof(s));
261 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
262 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
263 ok_size(s, textent.cx + 2 * wi.cxWindowBorders - 1 + 2, /* the last +2 is the text margin */
264 textent.cy + 2 * wi.cyWindowBorders + 1 + 2);
265
266 DestroyWindow(hwnd1);
267 }
268
269 START_TEST(button)
270 {
271 LoadLibraryW(L"comctl32.dll"); /* same as statically linking to comctl32 and doing InitCommonControls */
272 Test_TextMargin();
273 Test_Imagelist();
274 Test_GetIdealSizeNoThemes();
275 }
276