[COMCTL32] Sync with Wine Staging 1.7.55. CORE-10536
[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 textRect.left += 2;
269 textRect.right -= 2;
270 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
271 HeapFree(GetProcessHeap(), 0, text);
272 }
273
274 if (created_font) DeleteObject(font);
275 if (hPrevFont) SelectObject(hDC, hPrevFont);
276 }
277
278 static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
279 {
280 PB_draw, /* BS_PUSHBUTTON */
281 PB_draw, /* BS_DEFPUSHBUTTON */
282 CB_draw, /* BS_CHECKBOX */
283 CB_draw, /* BS_AUTOCHECKBOX */
284 CB_draw, /* BS_RADIOBUTTON */
285 CB_draw, /* BS_3STATE */
286 CB_draw, /* BS_AUTO3STATE */
287 GB_draw, /* BS_GROUPBOX */
288 NULL, /* BS_USERBUTTON */
289 CB_draw, /* BS_AUTORADIOBUTTON */
290 NULL, /* Not defined */
291 NULL, /* BS_OWNERDRAW */
292 NULL, /* Not defined */
293 NULL, /* Not defined */
294 NULL, /* Not defined */
295 NULL, /* Not defined */
296 };
297
298 static BOOL BUTTON_Paint(HTHEME theme, HWND hwnd, HDC hParamDC)
299 {
300 PAINTSTRUCT ps;
301 HDC hDC;
302 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
303 DWORD dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
304 UINT dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
305 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
306 ButtonState drawState;
307 pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
308
309 if(IsWindowEnabled(hwnd))
310 {
311 if(state & BST_PUSHED) drawState = STATE_PRESSED;
312 else if(state & BST_HOT) drawState = STATE_HOT;
313 else if(state & BST_FOCUS) drawState = STATE_DEFAULTED;
314 else drawState = STATE_NORMAL;
315 }
316 else drawState = STATE_DISABLED;
317
318 hDC = hParamDC ? hParamDC : BeginPaint(hwnd, &ps);
319 if (paint) paint(theme, hwnd, hDC, drawState, dtFlags, state & BST_FOCUS);
320 if (!hParamDC) EndPaint(hwnd, &ps);
321 return TRUE;
322 }
323
324 /**********************************************************************
325 * The button control subclass window proc.
326 */
327 LRESULT CALLBACK THEMING_ButtonSubclassProc(HWND hwnd, UINT msg,
328 WPARAM wParam, LPARAM lParam,
329 ULONG_PTR dwRefData)
330 {
331 const WCHAR* themeClass = WC_BUTTONW;
332 HTHEME theme;
333 LRESULT result;
334
335 switch (msg)
336 {
337 case WM_CREATE:
338 result = THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
339 OpenThemeData(hwnd, themeClass);
340 return result;
341
342 case WM_DESTROY:
343 theme = GetWindowTheme(hwnd);
344 CloseThemeData (theme);
345 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
346
347 case WM_THEMECHANGED:
348 theme = GetWindowTheme(hwnd);
349 CloseThemeData (theme);
350 OpenThemeData(hwnd, themeClass);
351 break;
352
353 case WM_SYSCOLORCHANGE:
354 theme = GetWindowTheme(hwnd);
355 if (!theme) return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
356 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
357 * which will do the repaint. */
358 break;
359
360 case WM_PAINT:
361 theme = GetWindowTheme(hwnd);
362 if (theme && BUTTON_Paint(theme, hwnd, (HDC)wParam))
363 return 0;
364 else
365 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
366
367 case WM_ENABLE:
368 theme = GetWindowTheme(hwnd);
369 if (theme) {
370 RedrawWindow(hwnd, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
371 return 0;
372 } else
373 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
374
375 case WM_MOUSEMOVE:
376 {
377 TRACKMOUSEEVENT mouse_event;
378 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
379 mouse_event.dwFlags = TME_QUERY;
380 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
381 {
382 mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
383 mouse_event.hwndTrack = hwnd;
384 mouse_event.dwHoverTime = 1;
385 TrackMouseEvent(&mouse_event);
386 }
387 break;
388 }
389
390 case WM_MOUSEHOVER:
391 {
392 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
393 SetWindowLongW(hwnd, 0, state|BST_HOT);
394 InvalidateRect(hwnd, NULL, FALSE);
395 break;
396 }
397
398 case WM_MOUSELEAVE:
399 {
400 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
401 SetWindowLongW(hwnd, 0, state&(~BST_HOT));
402 InvalidateRect(hwnd, NULL, FALSE);
403 break;
404 }
405
406 case BM_SETCHECK:
407 case BM_SETSTATE:
408 theme = GetWindowTheme(hwnd);
409 if (theme) {
410 InvalidateRect(hwnd, NULL, FALSE);
411 }
412 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
413
414 default:
415 /* Call old proc */
416 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
417 }
418 return 0;
419 }