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