comctl32: Always redraw background on checkbox repaint.
[wine.git] / dlls / comctl32 / theme_button.c
blob0f0db6d17886cb35b303c2fe60b428f22a3869ef
1 /*
2 * Theming - Button control
4 * Copyright (c) 2008 by Reece H. Dunn
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.
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.
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
22 #include <stdarg.h>
23 #include <string.h>
24 #include <stdlib.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "uxtheme.h"
31 #include "vssym32.h"
32 #include "comctl32.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(theme_button);
38 #define BUTTON_TYPE 0x0f /* bit mask for the available button types */
40 /* These are indices into a states array to determine the theme state for a given theme part. */
41 typedef enum
43 STATE_NORMAL,
44 STATE_DISABLED,
45 STATE_HOT,
46 STATE_PRESSED,
47 STATE_DEFAULTED
48 } ButtonState;
50 typedef void (*pfThemedPaint)(HTHEME theme, HWND hwnd, HDC hdc, ButtonState drawState, UINT dtFlags);
52 static UINT get_drawtext_flags(DWORD style, DWORD ex_style)
54 UINT flags = 0;
56 if (style & BS_PUSHLIKE)
57 style &= ~BUTTON_TYPE;
59 if (!(style & BS_MULTILINE))
60 flags |= DT_SINGLELINE;
61 else
62 flags |= DT_WORDBREAK;
64 switch (style & BS_CENTER)
66 case BS_LEFT: flags |= DT_LEFT; break;
67 case BS_RIGHT: flags |= DT_RIGHT; break;
68 case BS_CENTER: flags |= DT_CENTER; break;
69 default:
70 flags |= ((style & BUTTON_TYPE) <= BS_DEFPUSHBUTTON)
71 ? DT_CENTER : DT_LEFT;
74 if (ex_style & WS_EX_RIGHT)
75 flags = DT_RIGHT | (flags & ~(DT_LEFT | DT_CENTER));
77 if ((style & BUTTON_TYPE) != BS_GROUPBOX)
79 switch (style & BS_VCENTER)
81 case BS_TOP: flags |= DT_TOP; break;
82 case BS_BOTTOM: flags |= DT_BOTTOM; break;
83 case BS_VCENTER: /* fall through */
84 default: flags |= DT_VCENTER; break;
87 else
88 /* GroupBox's text is always single line and is top aligned. */
89 flags |= DT_SINGLELINE | DT_TOP;
91 return flags;
94 static inline WCHAR *get_button_text(HWND hwnd)
96 INT len = 512;
97 WCHAR *text;
98 text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
99 if (text) InternalGetWindowText(hwnd, text, len + 1);
100 return text;
103 static void PB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
105 static const int states[] = { PBS_NORMAL, PBS_DISABLED, PBS_HOT, PBS_PRESSED, PBS_DEFAULTED };
107 RECT bgRect, textRect;
108 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
109 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
110 int state = states[ drawState ];
111 WCHAR *text = get_button_text(hwnd);
113 GetClientRect(hwnd, &bgRect);
114 GetThemeBackgroundContentRect(theme, hDC, BP_PUSHBUTTON, state, &bgRect, &textRect);
116 if (IsThemeBackgroundPartiallyTransparent(theme, BP_PUSHBUTTON, state))
117 DrawThemeParentBackground(hwnd, hDC, NULL);
118 DrawThemeBackground(theme, hDC, BP_PUSHBUTTON, state, &bgRect, NULL);
119 if (text)
121 DrawThemeText(theme, hDC, BP_PUSHBUTTON, state, text, lstrlenW(text), dtFlags, 0, &textRect);
122 HeapFree(GetProcessHeap(), 0, text);
125 if (hPrevFont) SelectObject(hDC, hPrevFont);
128 static void CB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
130 static const int cb_states[3][5] =
132 { CBS_UNCHECKEDNORMAL, CBS_UNCHECKEDDISABLED, CBS_UNCHECKEDHOT, CBS_UNCHECKEDPRESSED, CBS_UNCHECKEDNORMAL },
133 { CBS_CHECKEDNORMAL, CBS_CHECKEDDISABLED, CBS_CHECKEDHOT, CBS_CHECKEDPRESSED, CBS_CHECKEDNORMAL },
134 { CBS_MIXEDNORMAL, CBS_MIXEDDISABLED, CBS_MIXEDHOT, CBS_MIXEDPRESSED, CBS_MIXEDNORMAL }
137 static const int rb_states[2][5] =
139 { RBS_UNCHECKEDNORMAL, RBS_UNCHECKEDDISABLED, RBS_UNCHECKEDHOT, RBS_UNCHECKEDPRESSED, RBS_UNCHECKEDNORMAL },
140 { RBS_CHECKEDNORMAL, RBS_CHECKEDDISABLED, RBS_CHECKEDHOT, RBS_CHECKEDPRESSED, RBS_CHECKEDNORMAL }
143 static const int cb_size = 13;
145 RECT bgRect, textRect;
146 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
147 HFONT hPrevFont = font ? SelectObject(hDC, font) : 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);
158 GetClientRect(hwnd, &bgRect);
159 GetThemeBackgroundContentRect(theme, hDC, part, state, &bgRect, &textRect);
161 if (dtFlags & DT_SINGLELINE) /* Center the checkbox / radio button to the text. */
162 bgRect.top = bgRect.top + (textRect.bottom - textRect.top - cb_size) / 2;
164 /* adjust for the check/radio marker */
165 bgRect.bottom = bgRect.top + cb_size;
166 bgRect.right = bgRect.left + cb_size;
167 textRect.left = bgRect.right + 6;
169 DrawThemeParentBackground(hwnd, hDC, NULL);
171 DrawThemeBackground(theme, hDC, part, state, &bgRect, NULL);
172 if (text)
174 DrawThemeText(theme, hDC, part, state, text, lstrlenW(text), dtFlags, 0, &textRect);
175 HeapFree(GetProcessHeap(), 0, text);
178 if (hPrevFont) SelectObject(hDC, hPrevFont);
181 static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
183 static const int states[] = { GBS_NORMAL, GBS_DISABLED, GBS_NORMAL, GBS_NORMAL, GBS_NORMAL };
185 RECT bgRect, textRect, contentRect;
186 int state = states[ drawState ];
187 WCHAR *text = get_button_text(hwnd);
188 LOGFONTW lf;
189 HFONT font, hPrevFont = NULL;
190 BOOL created_font = FALSE;
192 HRESULT hr = GetThemeFont(theme, hDC, BP_GROUPBOX, state, TMT_FONT, &lf);
193 if (SUCCEEDED(hr)) {
194 font = CreateFontIndirectW(&lf);
195 if (!font)
196 TRACE("Failed to create font\n");
197 else {
198 hPrevFont = SelectObject(hDC, font);
199 created_font = TRUE;
201 } else
202 font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
204 GetClientRect(hwnd, &bgRect);
205 textRect = bgRect;
207 if (text)
209 SIZE textExtent;
210 GetTextExtentPoint32W(hDC, text, lstrlenW(text), &textExtent);
211 bgRect.top += (textExtent.cy / 2);
212 textRect.left += 10;
213 textRect.bottom = textRect.top + textExtent.cy;
214 textRect.right = textRect.left + textExtent.cx + 4;
216 ExcludeClipRect(hDC, textRect.left, textRect.top, textRect.right, textRect.bottom);
219 GetThemeBackgroundContentRect(theme, hDC, BP_GROUPBOX, state, &bgRect, &contentRect);
220 ExcludeClipRect(hDC, contentRect.left, contentRect.top, contentRect.right, contentRect.bottom);
222 if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
223 DrawThemeParentBackground(hwnd, hDC, NULL);
224 DrawThemeBackground(theme, hDC, BP_GROUPBOX, state, &bgRect, NULL);
226 SelectClipRgn(hDC, NULL);
228 if (text)
230 textRect.left += 2;
231 textRect.right -= 2;
232 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
233 HeapFree(GetProcessHeap(), 0, text);
236 if (created_font) DeleteObject(font);
237 if (hPrevFont) SelectObject(hDC, hPrevFont);
240 static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
242 PB_draw, /* BS_PUSHBUTTON */
243 PB_draw, /* BS_DEFPUSHBUTTON */
244 CB_draw, /* BS_CHECKBOX */
245 CB_draw, /* BS_AUTOCHECKBOX */
246 CB_draw, /* BS_RADIOBUTTON */
247 CB_draw, /* BS_3STATE */
248 CB_draw, /* BS_AUTO3STATE */
249 GB_draw, /* BS_GROUPBOX */
250 NULL, /* BS_USERBUTTON */
251 CB_draw, /* BS_AUTORADIOBUTTON */
252 NULL, /* Not defined */
253 NULL, /* BS_OWNERDRAW */
254 NULL, /* Not defined */
255 NULL, /* Not defined */
256 NULL, /* Not defined */
257 NULL, /* Not defined */
260 static BOOL BUTTON_Paint(HTHEME theme, HWND hwnd, HDC hParamDC)
262 PAINTSTRUCT ps;
263 HDC hDC;
264 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
265 DWORD dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
266 UINT dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
267 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
268 ButtonState drawState;
269 pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
271 if(!paint)
272 return FALSE;
274 if(IsWindowEnabled(hwnd))
276 if(state & BST_PUSHED) drawState = STATE_PRESSED;
277 else if(state & BST_HOT) drawState = STATE_HOT;
278 else if(state & BST_FOCUS) drawState = STATE_DEFAULTED;
279 else drawState = STATE_NORMAL;
281 else drawState = STATE_DISABLED;
283 hDC = hParamDC ? hParamDC : BeginPaint(hwnd, &ps);
284 paint(theme, hwnd, hDC, drawState, dtFlags);
285 if (!hParamDC) EndPaint(hwnd, &ps);
286 return TRUE;
289 /**********************************************************************
290 * The button control subclass window proc.
292 LRESULT CALLBACK THEMING_ButtonSubclassProc(HWND hwnd, UINT msg,
293 WPARAM wParam, LPARAM lParam,
294 ULONG_PTR dwRefData)
296 const WCHAR* themeClass = WC_BUTTONW;
297 HTHEME theme;
298 LRESULT result;
300 switch (msg)
302 case WM_CREATE:
303 result = THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
304 OpenThemeData(hwnd, themeClass);
305 return result;
307 case WM_DESTROY:
308 theme = GetWindowTheme(hwnd);
309 CloseThemeData (theme);
310 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
312 case WM_THEMECHANGED:
313 theme = GetWindowTheme(hwnd);
314 CloseThemeData (theme);
315 OpenThemeData(hwnd, themeClass);
316 break;
318 case WM_SYSCOLORCHANGE:
319 theme = GetWindowTheme(hwnd);
320 if (!theme) return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
321 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
322 * which will do the repaint. */
323 break;
325 case WM_PAINT:
326 theme = GetWindowTheme(hwnd);
327 if (theme && BUTTON_Paint(theme, hwnd, (HDC)wParam))
328 return 0;
329 else
330 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
332 case WM_ENABLE:
333 theme = GetWindowTheme(hwnd);
334 if (theme) RedrawWindow(hwnd, NULL, NULL,
335 RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
336 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
338 case WM_MOUSEMOVE:
340 TRACKMOUSEEVENT mouse_event;
341 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
342 mouse_event.dwFlags = TME_QUERY;
343 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
345 mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
346 mouse_event.hwndTrack = hwnd;
347 mouse_event.dwHoverTime = 1;
348 TrackMouseEvent(&mouse_event);
350 break;
353 case WM_MOUSEHOVER:
355 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
356 SetWindowLongW(hwnd, 0, state|BST_HOT);
357 InvalidateRect(hwnd, NULL, FALSE);
358 break;
361 case WM_MOUSELEAVE:
363 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
364 SetWindowLongW(hwnd, 0, state&(~BST_HOT));
365 InvalidateRect(hwnd, NULL, FALSE);
366 break;
369 default:
370 /* Call old proc */
371 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
373 return 0;