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