[COMCTL32] -Reduce diff with user32 and move all data of the button in a single struct.
[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
115 GetClientRect(hwnd, &bgRect);
116 GetThemeBackgroundContentRect(theme, hDC, BP_PUSHBUTTON, state, &bgRect, &textRect);
117
118 if (prfFlag == 0 || (prfFlag & PRF_ERASEBKGND))
119 {
120 if (IsThemeBackgroundPartiallyTransparent(theme, BP_PUSHBUTTON, state))
121 DrawThemeParentBackground(hwnd, hDC, NULL);
122 }
123
124 DrawThemeBackground(theme, hDC, BP_PUSHBUTTON, state, &bgRect, NULL);
125 if (text)
126 {
127 DrawThemeText(theme, hDC, BP_PUSHBUTTON, state, text, lstrlenW(text), dtFlags, 0, &textRect);
128 HeapFree(GetProcessHeap(), 0, text);
129 }
130
131 if (focused)
132 {
133 MARGINS margins;
134 RECT focusRect = bgRect;
135
136 GetThemeMargins(theme, hDC, BP_PUSHBUTTON, state, TMT_CONTENTMARGINS, NULL, &margins);
137
138 focusRect.left += margins.cxLeftWidth;
139 focusRect.top += margins.cyTopHeight;
140 focusRect.right -= margins.cxRightWidth;
141 focusRect.bottom -= margins.cyBottomHeight;
142
143 DrawFocusRect( hDC, &focusRect );
144 }
145
146 if (hPrevFont) SelectObject(hDC, hPrevFont);
147 }
148
149 static void CB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused, LPARAM prfFlag)
150 {
151 static const int cb_states[3][5] =
152 {
153 { CBS_UNCHECKEDNORMAL, CBS_UNCHECKEDDISABLED, CBS_UNCHECKEDHOT, CBS_UNCHECKEDPRESSED, CBS_UNCHECKEDNORMAL },
154 { CBS_CHECKEDNORMAL, CBS_CHECKEDDISABLED, CBS_CHECKEDHOT, CBS_CHECKEDPRESSED, CBS_CHECKEDNORMAL },
155 { CBS_MIXEDNORMAL, CBS_MIXEDDISABLED, CBS_MIXEDHOT, CBS_MIXEDPRESSED, CBS_MIXEDNORMAL }
156 };
157
158 static const int rb_states[2][5] =
159 {
160 { RBS_UNCHECKEDNORMAL, RBS_UNCHECKEDDISABLED, RBS_UNCHECKEDHOT, RBS_UNCHECKEDPRESSED, RBS_UNCHECKEDNORMAL },
161 { RBS_CHECKEDNORMAL, RBS_CHECKEDDISABLED, RBS_CHECKEDHOT, RBS_CHECKEDPRESSED, RBS_CHECKEDNORMAL }
162 };
163
164 SIZE sz;
165 RECT bgRect, textRect;
166 HFONT font, hPrevFont = NULL;
167 LRESULT checkState = get_button_state(hwnd) & 3;
168 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
169 int part = ((dwStyle & BUTTON_TYPE) == BS_RADIOBUTTON) || ((dwStyle & BUTTON_TYPE) == BS_AUTORADIOBUTTON)
170 ? BP_RADIOBUTTON
171 : BP_CHECKBOX;
172 int state = (part == BP_CHECKBOX)
173 ? cb_states[ checkState ][ drawState ]
174 : rb_states[ checkState ][ drawState ];
175 WCHAR *text = get_button_text(hwnd);
176 LOGFONTW lf;
177 BOOL created_font = FALSE;
178
179 HRESULT hr = GetThemeFont(theme, hDC, part, state, TMT_FONT, &lf);
180 if (SUCCEEDED(hr)) {
181 font = CreateFontIndirectW(&lf);
182 if (!font)
183 TRACE("Failed to create font\n");
184 else {
185 TRACE("font = %s\n", debugstr_w(lf.lfFaceName));
186 hPrevFont = SelectObject(hDC, font);
187 created_font = TRUE;
188 }
189 } else {
190 font = get_button_font(hwnd);
191 hPrevFont = SelectObject(hDC, font);
192 }
193
194 if (FAILED(GetThemePartSize(theme, hDC, part, state, NULL, TS_DRAW, &sz)))
195 sz.cx = sz.cy = 13;
196
197 GetClientRect(hwnd, &bgRect);
198 GetThemeBackgroundContentRect(theme, hDC, part, state, &bgRect, &textRect);
199
200 if (dtFlags & DT_SINGLELINE) /* Center the checkbox / radio button to the text. */
201 bgRect.top = bgRect.top + (textRect.bottom - textRect.top - sz.cy) / 2;
202
203 /* adjust for the check/radio marker */
204 bgRect.bottom = bgRect.top + sz.cy;
205 bgRect.right = bgRect.left + sz.cx;
206 textRect.left = bgRect.right + 6;
207
208 if (prfFlag == 0 || (prfFlag & PRF_ERASEBKGND))
209 {
210 DrawThemeParentBackground(hwnd, hDC, NULL);
211 }
212
213 DrawThemeBackground(theme, hDC, part, state, &bgRect, NULL);
214 if (text)
215 {
216 DrawThemeText(theme, hDC, part, state, text, lstrlenW(text), dtFlags, 0, &textRect);
217
218 if (focused)
219 {
220 RECT focusRect;
221
222 focusRect = textRect;
223
224 DrawTextW(hDC, text, lstrlenW(text), &focusRect, dtFlags | DT_CALCRECT);
225
226 if (focusRect.right < textRect.right) focusRect.right++;
227 focusRect.bottom = textRect.bottom;
228
229 DrawFocusRect( hDC, &focusRect );
230 }
231
232 HeapFree(GetProcessHeap(), 0, text);
233 }
234
235 if (created_font) DeleteObject(font);
236 if (hPrevFont) SelectObject(hDC, hPrevFont);
237 }
238
239 static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused, LPARAM prfFlag)
240 {
241 static const int states[] = { GBS_NORMAL, GBS_DISABLED, GBS_NORMAL, GBS_NORMAL, GBS_NORMAL };
242
243 RECT bgRect, textRect, contentRect;
244 int state = states[ drawState ];
245 WCHAR *text = get_button_text(hwnd);
246 LOGFONTW lf;
247 HFONT font, hPrevFont = NULL;
248 BOOL created_font = FALSE;
249
250 HRESULT hr = GetThemeFont(theme, hDC, BP_GROUPBOX, state, TMT_FONT, &lf);
251 if (SUCCEEDED(hr)) {
252 font = CreateFontIndirectW(&lf);
253 if (!font)
254 TRACE("Failed to create font\n");
255 else {
256 hPrevFont = SelectObject(hDC, font);
257 created_font = TRUE;
258 }
259 } else {
260 font = get_button_font(hwnd);
261 hPrevFont = SelectObject(hDC, font);
262 }
263
264 GetClientRect(hwnd, &bgRect);
265 textRect = bgRect;
266
267 if (text)
268 {
269 SIZE textExtent;
270 GetTextExtentPoint32W(hDC, text, lstrlenW(text), &textExtent);
271 bgRect.top += (textExtent.cy / 2);
272 textRect.left += 10;
273 textRect.bottom = textRect.top + textExtent.cy;
274 textRect.right = textRect.left + textExtent.cx + 4;
275
276 ExcludeClipRect(hDC, textRect.left, textRect.top, textRect.right, textRect.bottom);
277 }
278
279 GetThemeBackgroundContentRect(theme, hDC, BP_GROUPBOX, state, &bgRect, &contentRect);
280 ExcludeClipRect(hDC, contentRect.left, contentRect.top, contentRect.right, contentRect.bottom);
281
282 if (prfFlag == 0 || (prfFlag & PRF_ERASEBKGND))
283 {
284 if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
285 DrawThemeParentBackground(hwnd, hDC, NULL);
286 }
287
288 DrawThemeBackground(theme, hDC, BP_GROUPBOX, state, &bgRect, NULL);
289
290 SelectClipRgn(hDC, NULL);
291
292 if (text)
293 {
294 InflateRect(&textRect, -2, 0);
295 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
296 HeapFree(GetProcessHeap(), 0, text);
297 }
298
299 if (created_font) DeleteObject(font);
300 if (hPrevFont) SelectObject(hDC, hPrevFont);
301 }
302
303 static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
304 {
305 PB_draw, /* BS_PUSHBUTTON */
306 PB_draw, /* BS_DEFPUSHBUTTON */
307 CB_draw, /* BS_CHECKBOX */
308 CB_draw, /* BS_AUTOCHECKBOX */
309 CB_draw, /* BS_RADIOBUTTON */
310 CB_draw, /* BS_3STATE */
311 CB_draw, /* BS_AUTO3STATE */
312 GB_draw, /* BS_GROUPBOX */
313 NULL, /* BS_USERBUTTON */
314 CB_draw, /* BS_AUTORADIOBUTTON */
315 NULL, /* Not defined */
316 NULL, /* BS_OWNERDRAW */
317 NULL, /* Not defined */
318 NULL, /* Not defined */
319 NULL, /* Not defined */
320 NULL, /* Not defined */
321 };
322
323 BOOL BUTTON_PaintWithTheme(HTHEME theme, HWND hwnd, HDC hParamDC, LPARAM prfFlag)
324 {
325 DWORD dwStyle;
326 DWORD dwStyleEx;
327 UINT dtFlags;
328 int state;
329 ButtonState drawState;
330 pfThemedPaint paint;
331
332 dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
333 paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
334 if (!paint)
335 return FALSE;
336
337 if (get_button_image(hwnd) != 0)
338 return FALSE;
339
340 dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
341 dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
342 state = get_button_state(hwnd);
343
344 if(IsWindowEnabled(hwnd))
345 {
346 if(state & BST_PUSHED) drawState = STATE_PRESSED;
347 else if(state & BST_HOT) drawState = STATE_HOT;
348 else if(state & BST_FOCUS) drawState = STATE_DEFAULTED;
349 else drawState = STATE_NORMAL;
350 }
351 else drawState = STATE_DISABLED;
352
353 paint(theme, hwnd, hParamDC, drawState, dtFlags, state & BST_FOCUS, prfFlag);
354 return TRUE;
355 }