[COMCTL32] -Fix several tests for the v6 button when themes are enabled.
[reactos.git] / reactos / dll / win32 / comctl32 / theme_button.c
1 /*
2 * Theming - Button control
3 *
4 * Copyright (c) 2008 by Reece H. Dunn
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22 #include "comctl32.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(theme_button);
24
25 #define BUTTON_TYPE 0x0f /* bit mask for the available button types */
26
27 /* These are indices into a states array to determine the theme state for a given theme part. */
28 typedef enum
29 {
30 STATE_NORMAL,
31 STATE_DISABLED,
32 STATE_HOT,
33 STATE_PRESSED,
34 STATE_DEFAULTED
35 } ButtonState;
36
37 typedef void (*pfThemedPaint)(HTHEME theme, HWND hwnd, HDC hdc, ButtonState drawState, UINT dtFlags, BOOL focused, LPARAM prfFlag);
38
39 #define STATE_GWL_OFFSET 0
40 #define HFONT_GWL_OFFSET (sizeof(LONG))
41
42 static inline LONG get_button_state( HWND hwnd )
43 {
44 return GetWindowLongPtrW( hwnd, STATE_GWL_OFFSET );
45 }
46
47 static inline HFONT get_button_font( HWND hwnd )
48 {
49 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
50 }
51
52 static UINT get_drawtext_flags(DWORD style, DWORD ex_style)
53 {
54 UINT flags = 0;
55
56 if (style & BS_PUSHLIKE)
57 style &= ~BUTTON_TYPE;
58
59 if (!(style & BS_MULTILINE))
60 flags |= DT_SINGLELINE;
61 else
62 flags |= DT_WORDBREAK;
63
64 switch (style & BS_CENTER)
65 {
66 case BS_LEFT: flags |= DT_LEFT; break;
67 case BS_RIGHT: flags |= DT_RIGHT; break;
68 case BS_CENTER: flags |= DT_CENTER; break;
69 default:
70 flags |= ((style & BUTTON_TYPE) <= BS_DEFPUSHBUTTON)
71 ? DT_CENTER : DT_LEFT;
72 }
73
74 if (ex_style & WS_EX_RIGHT)
75 flags = DT_RIGHT | (flags & ~(DT_LEFT | DT_CENTER));
76
77 if ((style & BUTTON_TYPE) != BS_GROUPBOX)
78 {
79 switch (style & BS_VCENTER)
80 {
81 case BS_TOP: flags |= DT_TOP; break;
82 case BS_BOTTOM: flags |= DT_BOTTOM; break;
83 case BS_VCENTER: /* fall through */
84 default: flags |= DT_VCENTER; break;
85 }
86 }
87 else
88 /* GroupBox's text is always single line and is top aligned. */
89 flags |= DT_SINGLELINE | DT_TOP;
90
91 return flags;
92 }
93
94 static inline WCHAR *get_button_text(HWND hwnd)
95 {
96 INT len = 512;
97 WCHAR *text;
98 text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
99 if (text) InternalGetWindowText(hwnd, text, len + 1);
100 return text;
101 }
102
103 static void PB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused, LPARAM prfFlag)
104 {
105 static const int states[] = { PBS_NORMAL, PBS_DISABLED, PBS_HOT, PBS_PRESSED, PBS_DEFAULTED };
106
107 RECT bgRect, textRect;
108 HFONT font = get_button_font(hwnd);
109 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
110 int state = states[ drawState ];
111 WCHAR *text = get_button_text(hwnd);
112
113 GetClientRect(hwnd, &bgRect);
114 GetThemeBackgroundContentRect(theme, hDC, BP_PUSHBUTTON, state, &bgRect, &textRect);
115
116 if (prfFlag == 0 || (prfFlag & PRF_ERASEBKGND))
117 {
118 if (IsThemeBackgroundPartiallyTransparent(theme, BP_PUSHBUTTON, state))
119 DrawThemeParentBackground(hwnd, hDC, NULL);
120 }
121
122 DrawThemeBackground(theme, hDC, BP_PUSHBUTTON, state, &bgRect, NULL);
123 if (text)
124 {
125 DrawThemeText(theme, hDC, BP_PUSHBUTTON, state, text, lstrlenW(text), dtFlags, 0, &textRect);
126 HeapFree(GetProcessHeap(), 0, text);
127 }
128
129 if (focused)
130 {
131 MARGINS margins;
132 RECT focusRect = bgRect;
133
134 GetThemeMargins(theme, hDC, BP_PUSHBUTTON, state, TMT_CONTENTMARGINS, NULL, &margins);
135
136 focusRect.left += margins.cxLeftWidth;
137 focusRect.top += margins.cyTopHeight;
138 focusRect.right -= margins.cxRightWidth;
139 focusRect.bottom -= margins.cyBottomHeight;
140
141 DrawFocusRect( hDC, &focusRect );
142 }
143
144 if (hPrevFont) SelectObject(hDC, hPrevFont);
145 }
146
147 static void CB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused, LPARAM prfFlag)
148 {
149 static const int cb_states[3][5] =
150 {
151 { CBS_UNCHECKEDNORMAL, CBS_UNCHECKEDDISABLED, CBS_UNCHECKEDHOT, CBS_UNCHECKEDPRESSED, CBS_UNCHECKEDNORMAL },
152 { CBS_CHECKEDNORMAL, CBS_CHECKEDDISABLED, CBS_CHECKEDHOT, CBS_CHECKEDPRESSED, CBS_CHECKEDNORMAL },
153 { CBS_MIXEDNORMAL, CBS_MIXEDDISABLED, CBS_MIXEDHOT, CBS_MIXEDPRESSED, CBS_MIXEDNORMAL }
154 };
155
156 static const int rb_states[2][5] =
157 {
158 { RBS_UNCHECKEDNORMAL, RBS_UNCHECKEDDISABLED, RBS_UNCHECKEDHOT, RBS_UNCHECKEDPRESSED, RBS_UNCHECKEDNORMAL },
159 { RBS_CHECKEDNORMAL, RBS_CHECKEDDISABLED, RBS_CHECKEDHOT, RBS_CHECKEDPRESSED, RBS_CHECKEDNORMAL }
160 };
161
162 SIZE sz;
163 RECT bgRect, textRect;
164 HFONT font, hPrevFont = NULL;
165 LRESULT checkState = get_button_state(hwnd) & 3;
166 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
167 int part = ((dwStyle & BUTTON_TYPE) == BS_RADIOBUTTON) || ((dwStyle & BUTTON_TYPE) == BS_AUTORADIOBUTTON)
168 ? BP_RADIOBUTTON
169 : BP_CHECKBOX;
170 int state = (part == BP_CHECKBOX)
171 ? cb_states[ checkState ][ drawState ]
172 : rb_states[ checkState ][ drawState ];
173 WCHAR *text = get_button_text(hwnd);
174 LOGFONTW lf;
175 BOOL created_font = FALSE;
176
177 HRESULT hr = GetThemeFont(theme, hDC, part, state, TMT_FONT, &lf);
178 if (SUCCEEDED(hr)) {
179 font = CreateFontIndirectW(&lf);
180 if (!font)
181 TRACE("Failed to create font\n");
182 else {
183 TRACE("font = %s\n", debugstr_w(lf.lfFaceName));
184 hPrevFont = SelectObject(hDC, font);
185 created_font = TRUE;
186 }
187 } else {
188 font = get_button_font(hwnd);
189 hPrevFont = SelectObject(hDC, font);
190 }
191
192 if (FAILED(GetThemePartSize(theme, hDC, part, state, NULL, TS_DRAW, &sz)))
193 sz.cx = sz.cy = 13;
194
195 GetClientRect(hwnd, &bgRect);
196 GetThemeBackgroundContentRect(theme, hDC, part, state, &bgRect, &textRect);
197
198 if (dtFlags & DT_SINGLELINE) /* Center the checkbox / radio button to the text. */
199 bgRect.top = bgRect.top + (textRect.bottom - textRect.top - sz.cy) / 2;
200
201 /* adjust for the check/radio marker */
202 bgRect.bottom = bgRect.top + sz.cy;
203 bgRect.right = bgRect.left + sz.cx;
204 textRect.left = bgRect.right + 6;
205
206 if (prfFlag == 0 || (prfFlag & PRF_ERASEBKGND))
207 {
208 DrawThemeParentBackground(hwnd, hDC, NULL);
209 }
210
211 DrawThemeBackground(theme, hDC, part, state, &bgRect, NULL);
212 if (text)
213 {
214 DrawThemeText(theme, hDC, part, state, text, lstrlenW(text), dtFlags, 0, &textRect);
215
216 if (focused)
217 {
218 RECT focusRect;
219
220 focusRect = textRect;
221
222 DrawTextW(hDC, text, lstrlenW(text), &focusRect, dtFlags | DT_CALCRECT);
223
224 if (focusRect.right < textRect.right) focusRect.right++;
225 focusRect.bottom = textRect.bottom;
226
227 DrawFocusRect( hDC, &focusRect );
228 }
229
230 HeapFree(GetProcessHeap(), 0, text);
231 }
232
233 if (created_font) DeleteObject(font);
234 if (hPrevFont) SelectObject(hDC, hPrevFont);
235 }
236
237 static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused, LPARAM prfFlag)
238 {
239 static const int states[] = { GBS_NORMAL, GBS_DISABLED, GBS_NORMAL, GBS_NORMAL, GBS_NORMAL };
240
241 RECT bgRect, textRect, contentRect;
242 int state = states[ drawState ];
243 WCHAR *text = get_button_text(hwnd);
244 LOGFONTW lf;
245 HFONT font, hPrevFont = NULL;
246 BOOL created_font = FALSE;
247
248 HRESULT hr = GetThemeFont(theme, hDC, BP_GROUPBOX, state, TMT_FONT, &lf);
249 if (SUCCEEDED(hr)) {
250 font = CreateFontIndirectW(&lf);
251 if (!font)
252 TRACE("Failed to create font\n");
253 else {
254 hPrevFont = SelectObject(hDC, font);
255 created_font = TRUE;
256 }
257 } else {
258 font = get_button_font(hwnd);
259 hPrevFont = SelectObject(hDC, font);
260 }
261
262 GetClientRect(hwnd, &bgRect);
263 textRect = bgRect;
264
265 if (text)
266 {
267 SIZE textExtent;
268 GetTextExtentPoint32W(hDC, text, lstrlenW(text), &textExtent);
269 bgRect.top += (textExtent.cy / 2);
270 textRect.left += 10;
271 textRect.bottom = textRect.top + textExtent.cy;
272 textRect.right = textRect.left + textExtent.cx + 4;
273
274 ExcludeClipRect(hDC, textRect.left, textRect.top, textRect.right, textRect.bottom);
275 }
276
277 GetThemeBackgroundContentRect(theme, hDC, BP_GROUPBOX, state, &bgRect, &contentRect);
278 ExcludeClipRect(hDC, contentRect.left, contentRect.top, contentRect.right, contentRect.bottom);
279
280 if (prfFlag == 0 || (prfFlag & PRF_ERASEBKGND))
281 {
282 if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
283 DrawThemeParentBackground(hwnd, hDC, NULL);
284 }
285
286 DrawThemeBackground(theme, hDC, BP_GROUPBOX, state, &bgRect, NULL);
287
288 SelectClipRgn(hDC, NULL);
289
290 if (text)
291 {
292 InflateRect(&textRect, -2, 0);
293 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
294 HeapFree(GetProcessHeap(), 0, text);
295 }
296
297 if (created_font) DeleteObject(font);
298 if (hPrevFont) SelectObject(hDC, hPrevFont);
299 }
300
301 static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
302 {
303 PB_draw, /* BS_PUSHBUTTON */
304 PB_draw, /* BS_DEFPUSHBUTTON */
305 CB_draw, /* BS_CHECKBOX */
306 CB_draw, /* BS_AUTOCHECKBOX */
307 CB_draw, /* BS_RADIOBUTTON */
308 CB_draw, /* BS_3STATE */
309 CB_draw, /* BS_AUTO3STATE */
310 GB_draw, /* BS_GROUPBOX */
311 NULL, /* BS_USERBUTTON */
312 CB_draw, /* BS_AUTORADIOBUTTON */
313 NULL, /* Not defined */
314 NULL, /* BS_OWNERDRAW */
315 NULL, /* Not defined */
316 NULL, /* Not defined */
317 NULL, /* Not defined */
318 NULL, /* Not defined */
319 };
320
321 BOOL BUTTON_PaintWithTheme(HTHEME theme, HWND hwnd, HDC hParamDC, LPARAM prfFlag)
322 {
323 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
324 DWORD dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
325 UINT dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
326 int state = get_button_state(hwnd);
327 ButtonState drawState;
328 pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
329 if (!paint)
330 return FALSE;
331
332 if(IsWindowEnabled(hwnd))
333 {
334 if(state & BST_PUSHED) drawState = STATE_PRESSED;
335 else if(state & BST_HOT) drawState = STATE_HOT;
336 else if(state & BST_FOCUS) drawState = STATE_DEFAULTED;
337 else drawState = STATE_NORMAL;
338 }
339 else drawState = STATE_DISABLED;
340
341 paint(theme, hwnd, hParamDC, drawState, dtFlags, state & BST_FOCUS, prfFlag);
342 return TRUE;
343 }