[COMCTL32] Sync with Wine Staging 1.7.37. CORE-9246
[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 static const int cb_size = 13;
146
147 RECT bgRect, textRect;
148 HFONT font, hPrevFont = NULL;
149 LRESULT checkState = SendMessageW(hwnd, BM_GETCHECK, 0, 0);
150 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
151 int part = ((dwStyle & BUTTON_TYPE) == BS_RADIOBUTTON) || ((dwStyle & BUTTON_TYPE) == BS_AUTORADIOBUTTON)
152 ? BP_RADIOBUTTON
153 : BP_CHECKBOX;
154 int state = (part == BP_CHECKBOX)
155 ? cb_states[ checkState ][ drawState ]
156 : rb_states[ checkState ][ drawState ];
157 WCHAR *text = get_button_text(hwnd);
158 LOGFONTW lf;
159 BOOL created_font = FALSE;
160
161 HRESULT hr = GetThemeFont(theme, hDC, part, state, TMT_FONT, &lf);
162 if (SUCCEEDED(hr)) {
163 font = CreateFontIndirectW(&lf);
164 if (!font)
165 TRACE("Failed to create font\n");
166 else {
167 TRACE("font = %s\n", debugstr_w(lf.lfFaceName));
168 hPrevFont = SelectObject(hDC, font);
169 created_font = TRUE;
170 }
171 } else {
172 font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
173 hPrevFont = SelectObject(hDC, font);
174 }
175
176 GetClientRect(hwnd, &bgRect);
177 GetThemeBackgroundContentRect(theme, hDC, part, state, &bgRect, &textRect);
178
179 if (dtFlags & DT_SINGLELINE) /* Center the checkbox / radio button to the text. */
180 bgRect.top = bgRect.top + (textRect.bottom - textRect.top - cb_size) / 2;
181
182 /* adjust for the check/radio marker */
183 bgRect.bottom = bgRect.top + cb_size;
184 bgRect.right = bgRect.left + cb_size;
185 textRect.left = bgRect.right + 6;
186
187 DrawThemeParentBackground(hwnd, hDC, NULL);
188
189 DrawThemeBackground(theme, hDC, part, state, &bgRect, NULL);
190 if (text)
191 {
192 DrawThemeText(theme, hDC, part, state, text, lstrlenW(text), dtFlags, 0, &textRect);
193
194 if (focused)
195 {
196 RECT focusRect;
197
198 focusRect = textRect;
199
200 DrawTextW(hDC, text, lstrlenW(text), &focusRect, dtFlags | DT_CALCRECT);
201
202 if (focusRect.right < textRect.right) focusRect.right++;
203 focusRect.bottom = textRect.bottom;
204
205 DrawFocusRect( hDC, &focusRect );
206 }
207
208 HeapFree(GetProcessHeap(), 0, text);
209 }
210
211 if (created_font) DeleteObject(font);
212 if (hPrevFont) SelectObject(hDC, hPrevFont);
213 }
214
215 static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused)
216 {
217 static const int states[] = { GBS_NORMAL, GBS_DISABLED, GBS_NORMAL, GBS_NORMAL, GBS_NORMAL };
218
219 RECT bgRect, textRect, contentRect;
220 int state = states[ drawState ];
221 WCHAR *text = get_button_text(hwnd);
222 LOGFONTW lf;
223 HFONT font, hPrevFont = NULL;
224 BOOL created_font = FALSE;
225
226 HRESULT hr = GetThemeFont(theme, hDC, BP_GROUPBOX, state, TMT_FONT, &lf);
227 if (SUCCEEDED(hr)) {
228 font = CreateFontIndirectW(&lf);
229 if (!font)
230 TRACE("Failed to create font\n");
231 else {
232 hPrevFont = SelectObject(hDC, font);
233 created_font = TRUE;
234 }
235 } else {
236 font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
237 hPrevFont = SelectObject(hDC, font);
238 }
239
240 GetClientRect(hwnd, &bgRect);
241 textRect = bgRect;
242
243 if (text)
244 {
245 SIZE textExtent;
246 GetTextExtentPoint32W(hDC, text, lstrlenW(text), &textExtent);
247 bgRect.top += (textExtent.cy / 2);
248 textRect.left += 10;
249 textRect.bottom = textRect.top + textExtent.cy;
250 textRect.right = textRect.left + textExtent.cx + 4;
251
252 ExcludeClipRect(hDC, textRect.left, textRect.top, textRect.right, textRect.bottom);
253 }
254
255 GetThemeBackgroundContentRect(theme, hDC, BP_GROUPBOX, state, &bgRect, &contentRect);
256 ExcludeClipRect(hDC, contentRect.left, contentRect.top, contentRect.right, contentRect.bottom);
257
258 if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
259 DrawThemeParentBackground(hwnd, hDC, NULL);
260 DrawThemeBackground(theme, hDC, BP_GROUPBOX, state, &bgRect, NULL);
261
262 SelectClipRgn(hDC, NULL);
263
264 if (text)
265 {
266 textRect.left += 2;
267 textRect.right -= 2;
268 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
269 HeapFree(GetProcessHeap(), 0, text);
270 }
271
272 if (created_font) DeleteObject(font);
273 if (hPrevFont) SelectObject(hDC, hPrevFont);
274 }
275
276 static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
277 {
278 PB_draw, /* BS_PUSHBUTTON */
279 PB_draw, /* BS_DEFPUSHBUTTON */
280 CB_draw, /* BS_CHECKBOX */
281 CB_draw, /* BS_AUTOCHECKBOX */
282 CB_draw, /* BS_RADIOBUTTON */
283 CB_draw, /* BS_3STATE */
284 CB_draw, /* BS_AUTO3STATE */
285 GB_draw, /* BS_GROUPBOX */
286 NULL, /* BS_USERBUTTON */
287 CB_draw, /* BS_AUTORADIOBUTTON */
288 NULL, /* Not defined */
289 NULL, /* BS_OWNERDRAW */
290 NULL, /* Not defined */
291 NULL, /* Not defined */
292 NULL, /* Not defined */
293 NULL, /* Not defined */
294 };
295
296 static BOOL BUTTON_Paint(HTHEME theme, HWND hwnd, HDC hParamDC)
297 {
298 PAINTSTRUCT ps;
299 HDC hDC;
300 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
301 DWORD dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
302 UINT dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
303 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
304 ButtonState drawState;
305 pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
306
307 if(!paint)
308 return FALSE;
309
310 if(IsWindowEnabled(hwnd))
311 {
312 if(state & BST_PUSHED) drawState = STATE_PRESSED;
313 else if(state & BST_HOT) drawState = STATE_HOT;
314 else if(state & BST_FOCUS) drawState = STATE_DEFAULTED;
315 else drawState = STATE_NORMAL;
316 }
317 else drawState = STATE_DISABLED;
318
319 hDC = hParamDC ? hParamDC : BeginPaint(hwnd, &ps);
320 paint(theme, hwnd, hDC, drawState, dtFlags, state & BST_FOCUS);
321 if (!hParamDC) EndPaint(hwnd, &ps);
322 return TRUE;
323 }
324
325 /**********************************************************************
326 * The button control subclass window proc.
327 */
328 LRESULT CALLBACK THEMING_ButtonSubclassProc(HWND hwnd, UINT msg,
329 WPARAM wParam, LPARAM lParam,
330 ULONG_PTR dwRefData)
331 {
332 const WCHAR* themeClass = WC_BUTTONW;
333 HTHEME theme;
334 LRESULT result;
335
336 switch (msg)
337 {
338 case WM_CREATE:
339 result = THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
340 OpenThemeData(hwnd, themeClass);
341 return result;
342
343 case WM_DESTROY:
344 theme = GetWindowTheme(hwnd);
345 CloseThemeData (theme);
346 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
347
348 case WM_THEMECHANGED:
349 theme = GetWindowTheme(hwnd);
350 CloseThemeData (theme);
351 OpenThemeData(hwnd, themeClass);
352 break;
353
354 case WM_SYSCOLORCHANGE:
355 theme = GetWindowTheme(hwnd);
356 if (!theme) return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
357 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
358 * which will do the repaint. */
359 break;
360
361 case WM_PAINT:
362 theme = GetWindowTheme(hwnd);
363 if (theme && BUTTON_Paint(theme, hwnd, (HDC)wParam))
364 return 0;
365 else
366 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
367
368 case WM_ENABLE:
369 theme = GetWindowTheme(hwnd);
370 if (theme) RedrawWindow(hwnd, NULL, NULL,
371 RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
372 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
373
374 case WM_MOUSEMOVE:
375 {
376 TRACKMOUSEEVENT mouse_event;
377 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
378 mouse_event.dwFlags = TME_QUERY;
379 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
380 {
381 mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
382 mouse_event.hwndTrack = hwnd;
383 mouse_event.dwHoverTime = 1;
384 TrackMouseEvent(&mouse_event);
385 }
386 break;
387 }
388
389 case WM_MOUSEHOVER:
390 {
391 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
392 SetWindowLongW(hwnd, 0, state|BST_HOT);
393 InvalidateRect(hwnd, NULL, FALSE);
394 break;
395 }
396
397 case WM_MOUSELEAVE:
398 {
399 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
400 SetWindowLongW(hwnd, 0, state&(~BST_HOT));
401 InvalidateRect(hwnd, NULL, FALSE);
402 break;
403 }
404
405 default:
406 /* Call old proc */
407 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
408 }
409 return 0;
410 }