* Sync up to trunk head (r64921).
[reactos.git] / 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
24 #define BUTTON_TYPE 0x0f /* bit mask for the available button types */
25
26 /* These are indices into a states array to determine the theme state for a given theme part. */
27 typedef enum
28 {
29 STATE_NORMAL,
30 STATE_DISABLED,
31 STATE_HOT,
32 STATE_PRESSED,
33 STATE_DEFAULTED
34 } ButtonState;
35
36 typedef void (*pfThemedPaint)(HTHEME theme, HWND hwnd, HDC hdc, ButtonState drawState, UINT dtFlags);
37
38 static UINT get_drawtext_flags(DWORD style, DWORD ex_style)
39 {
40 UINT flags = 0;
41
42 if (style & BS_PUSHLIKE)
43 style &= ~BUTTON_TYPE;
44
45 if (!(style & BS_MULTILINE))
46 flags |= DT_SINGLELINE;
47 else
48 flags |= DT_WORDBREAK;
49
50 switch (style & BS_CENTER)
51 {
52 case BS_LEFT: flags |= DT_LEFT; break;
53 case BS_RIGHT: flags |= DT_RIGHT; break;
54 case BS_CENTER: flags |= DT_CENTER; break;
55 default:
56 flags |= ((style & BUTTON_TYPE) <= BS_DEFPUSHBUTTON)
57 ? DT_CENTER : DT_LEFT;
58 }
59
60 if (ex_style & WS_EX_RIGHT)
61 flags = DT_RIGHT | (flags & ~(DT_LEFT | DT_CENTER));
62
63 if ((style & BUTTON_TYPE) != BS_GROUPBOX)
64 {
65 switch (style & BS_VCENTER)
66 {
67 case BS_TOP: flags |= DT_TOP; break;
68 case BS_BOTTOM: flags |= DT_BOTTOM; break;
69 case BS_VCENTER: /* fall through */
70 default: flags |= DT_VCENTER; break;
71 }
72 }
73 else
74 /* GroupBox's text is always single line and is top aligned. */
75 flags |= DT_SINGLELINE | DT_TOP;
76
77 return flags;
78 }
79
80 static inline WCHAR *get_button_text(HWND hwnd)
81 {
82 INT len = 512;
83 WCHAR *text;
84 text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
85 if (text) InternalGetWindowText(hwnd, text, len + 1);
86 return text;
87 }
88
89 static void PB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
90 {
91 static const int states[] = { PBS_NORMAL, PBS_DISABLED, PBS_HOT, PBS_PRESSED, PBS_DEFAULTED };
92
93 RECT bgRect, textRect;
94 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
95 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
96 int state = states[ drawState ];
97 WCHAR *text = get_button_text(hwnd);
98
99 GetClientRect(hwnd, &bgRect);
100 GetThemeBackgroundContentRect(theme, hDC, BP_PUSHBUTTON, state, &bgRect, &textRect);
101
102 if (IsThemeBackgroundPartiallyTransparent(theme, BP_PUSHBUTTON, state))
103 DrawThemeParentBackground(hwnd, hDC, NULL);
104 DrawThemeBackground(theme, hDC, BP_PUSHBUTTON, state, &bgRect, NULL);
105 if (text)
106 {
107 DrawThemeText(theme, hDC, BP_PUSHBUTTON, state, text, lstrlenW(text), dtFlags, 0, &textRect);
108 HeapFree(GetProcessHeap(), 0, text);
109 }
110
111 if (hPrevFont) SelectObject(hDC, hPrevFont);
112 }
113
114 static void CB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
115 {
116 static const int cb_states[3][5] =
117 {
118 { CBS_UNCHECKEDNORMAL, CBS_UNCHECKEDDISABLED, CBS_UNCHECKEDHOT, CBS_UNCHECKEDPRESSED, CBS_UNCHECKEDNORMAL },
119 { CBS_CHECKEDNORMAL, CBS_CHECKEDDISABLED, CBS_CHECKEDHOT, CBS_CHECKEDPRESSED, CBS_CHECKEDNORMAL },
120 { CBS_MIXEDNORMAL, CBS_MIXEDDISABLED, CBS_MIXEDHOT, CBS_MIXEDPRESSED, CBS_MIXEDNORMAL }
121 };
122
123 static const int rb_states[2][5] =
124 {
125 { RBS_UNCHECKEDNORMAL, RBS_UNCHECKEDDISABLED, RBS_UNCHECKEDHOT, RBS_UNCHECKEDPRESSED, RBS_UNCHECKEDNORMAL },
126 { RBS_CHECKEDNORMAL, RBS_CHECKEDDISABLED, RBS_CHECKEDHOT, RBS_CHECKEDPRESSED, RBS_CHECKEDNORMAL }
127 };
128
129 static const int cb_size = 13;
130
131 RECT bgRect, textRect;
132 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
133 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
134 LRESULT checkState = SendMessageW(hwnd, BM_GETCHECK, 0, 0);
135 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
136 int part = ((dwStyle & BUTTON_TYPE) == BS_RADIOBUTTON) || ((dwStyle & BUTTON_TYPE) == BS_AUTORADIOBUTTON)
137 ? BP_RADIOBUTTON
138 : BP_CHECKBOX;
139 int state = (part == BP_CHECKBOX)
140 ? cb_states[ checkState ][ drawState ]
141 : rb_states[ checkState ][ drawState ];
142 WCHAR *text = get_button_text(hwnd);
143
144 GetClientRect(hwnd, &bgRect);
145 GetThemeBackgroundContentRect(theme, hDC, part, state, &bgRect, &textRect);
146
147 if (dtFlags & DT_SINGLELINE) /* Center the checkbox / radio button to the text. */
148 bgRect.top = bgRect.top + (textRect.bottom - textRect.top - cb_size) / 2;
149
150 /* adjust for the check/radio marker */
151 bgRect.bottom = bgRect.top + cb_size;
152 bgRect.right = bgRect.left + cb_size;
153 textRect.left = bgRect.right + 6;
154
155 if (IsThemeBackgroundPartiallyTransparent(theme, part, state))
156 DrawThemeParentBackground(hwnd, hDC, NULL);
157 DrawThemeBackground(theme, hDC, part, state, &bgRect, NULL);
158 if (text)
159 {
160 DrawThemeText(theme, hDC, part, state, text, lstrlenW(text), dtFlags, 0, &textRect);
161 HeapFree(GetProcessHeap(), 0, text);
162 }
163
164 if (hPrevFont) SelectObject(hDC, hPrevFont);
165 }
166
167 static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
168 {
169 static const int states[] = { GBS_NORMAL, GBS_DISABLED, GBS_NORMAL, GBS_NORMAL, GBS_NORMAL };
170
171 RECT bgRect, textRect, contentRect;
172 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
173 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
174 int state = states[ drawState ];
175 WCHAR *text = get_button_text(hwnd);
176
177 GetClientRect(hwnd, &bgRect);
178 textRect = bgRect;
179
180 if (text)
181 {
182 SIZE textExtent;
183 GetTextExtentPoint32W(hDC, text, lstrlenW(text), &textExtent);
184 bgRect.top += (textExtent.cy / 2);
185 textRect.left += 10;
186 textRect.bottom = textRect.top + textExtent.cy;
187 textRect.right = textRect.left + textExtent.cx + 4;
188
189 ExcludeClipRect(hDC, textRect.left, textRect.top, textRect.right, textRect.bottom);
190 }
191
192 GetThemeBackgroundContentRect(theme, hDC, BP_GROUPBOX, state, &bgRect, &contentRect);
193 ExcludeClipRect(hDC, contentRect.left, contentRect.top, contentRect.right, contentRect.bottom);
194
195 if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
196 DrawThemeParentBackground(hwnd, hDC, NULL);
197 DrawThemeBackground(theme, hDC, BP_GROUPBOX, state, &bgRect, NULL);
198
199 SelectClipRgn(hDC, NULL);
200
201 if (text)
202 {
203 textRect.left += 2;
204 textRect.right -= 2;
205 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
206 HeapFree(GetProcessHeap(), 0, text);
207 }
208
209 if (hPrevFont) SelectObject(hDC, hPrevFont);
210 }
211
212 static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
213 {
214 PB_draw, /* BS_PUSHBUTTON */
215 PB_draw, /* BS_DEFPUSHBUTTON */
216 CB_draw, /* BS_CHECKBOX */
217 CB_draw, /* BS_AUTOCHECKBOX */
218 CB_draw, /* BS_RADIOBUTTON */
219 CB_draw, /* BS_3STATE */
220 CB_draw, /* BS_AUTO3STATE */
221 GB_draw, /* BS_GROUPBOX */
222 NULL, /* BS_USERBUTTON */
223 CB_draw, /* BS_AUTORADIOBUTTON */
224 NULL, /* Not defined */
225 NULL, /* BS_OWNERDRAW */
226 NULL, /* Not defined */
227 NULL, /* Not defined */
228 NULL, /* Not defined */
229 NULL, /* Not defined */
230 };
231
232 static BOOL BUTTON_Paint(HTHEME theme, HWND hwnd, HDC hParamDC)
233 {
234 PAINTSTRUCT ps;
235 HDC hDC;
236 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
237 DWORD dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
238 UINT dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
239 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
240 ButtonState drawState;
241 pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
242
243 if(!paint)
244 return FALSE;
245
246 if(IsWindowEnabled(hwnd))
247 {
248 if(state & BST_PUSHED) drawState = STATE_PRESSED;
249 else if(state & BST_HOT) drawState = STATE_HOT;
250 else if(state & BST_FOCUS) drawState = STATE_DEFAULTED;
251 else drawState = STATE_NORMAL;
252 }
253 else drawState = STATE_DISABLED;
254
255 hDC = hParamDC ? hParamDC : BeginPaint(hwnd, &ps);
256 paint(theme, hwnd, hDC, drawState, dtFlags);
257 if (!hParamDC) EndPaint(hwnd, &ps);
258 return TRUE;
259 }
260
261 /**********************************************************************
262 * The button control subclass window proc.
263 */
264 LRESULT CALLBACK THEMING_ButtonSubclassProc(HWND hwnd, UINT msg,
265 WPARAM wParam, LPARAM lParam,
266 ULONG_PTR dwRefData)
267 {
268 const WCHAR* themeClass = WC_BUTTONW;
269 HTHEME theme;
270 LRESULT result;
271
272 switch (msg)
273 {
274 case WM_CREATE:
275 result = THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
276 OpenThemeData(hwnd, themeClass);
277 return result;
278
279 case WM_DESTROY:
280 theme = GetWindowTheme(hwnd);
281 CloseThemeData (theme);
282 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
283
284 case WM_THEMECHANGED:
285 theme = GetWindowTheme(hwnd);
286 CloseThemeData (theme);
287 OpenThemeData(hwnd, themeClass);
288 break;
289
290 case WM_SYSCOLORCHANGE:
291 theme = GetWindowTheme(hwnd);
292 if (!theme) return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
293 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
294 * which will do the repaint. */
295 break;
296
297 case WM_PAINT:
298 theme = GetWindowTheme(hwnd);
299 if (theme && BUTTON_Paint(theme, hwnd, (HDC)wParam))
300 return 0;
301 else
302 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
303
304 case WM_ENABLE:
305 theme = GetWindowTheme(hwnd);
306 if (theme) RedrawWindow(hwnd, NULL, NULL,
307 RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
308 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
309
310 case WM_MOUSEMOVE:
311 {
312 TRACKMOUSEEVENT mouse_event;
313 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
314 mouse_event.dwFlags = TME_QUERY;
315 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
316 {
317 mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
318 mouse_event.hwndTrack = hwnd;
319 mouse_event.dwHoverTime = 1;
320 TrackMouseEvent(&mouse_event);
321 }
322 break;
323 }
324
325 case WM_MOUSEHOVER:
326 {
327 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
328 SetWindowLongW(hwnd, 0, state|BST_HOT);
329 InvalidateRect(hwnd, NULL, FALSE);
330 break;
331 }
332
333 case WM_MOUSELEAVE:
334 {
335 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
336 SetWindowLongW(hwnd, 0, state&(~BST_HOT));
337 InvalidateRect(hwnd, NULL, FALSE);
338 break;
339 }
340
341 default:
342 /* Call old proc */
343 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
344 }
345 return 0;
346 }