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
34 #define BUTTON_TYPE 0x0f /* bit mask for the available button types */
36 /* These are indices into a states array to determine the theme state for a given theme part. */
46 typedef void (*pfThemedPaint)(HTHEME theme, HWND hwnd, HDC hdc, ButtonState drawState, UINT dtFlags);
48 static UINT get_drawtext_flags(DWORD style, DWORD ex_style)
52 if (style & BS_PUSHLIKE)
53 style &= ~BUTTON_TYPE;
55 if (!(style & BS_MULTILINE))
56 flags |= DT_SINGLELINE;
58 flags |= DT_WORDBREAK;
60 switch (style & BS_CENTER)
62 case BS_LEFT: flags |= DT_LEFT; break;
63 case BS_RIGHT: flags |= DT_RIGHT; break;
64 case BS_CENTER: flags |= DT_CENTER; break;
66 flags |= ((style & BUTTON_TYPE) <= BS_DEFPUSHBUTTON)
67 ? DT_CENTER : DT_LEFT;
70 if (ex_style & WS_EX_RIGHT)
71 flags = DT_RIGHT | (flags & ~(DT_LEFT | DT_CENTER));
73 if ((style & BUTTON_TYPE) != BS_GROUPBOX)
75 switch (style & BS_VCENTER)
77 case BS_TOP: flags |= DT_TOP; break;
78 case BS_BOTTOM: flags |= DT_BOTTOM; break;
79 case BS_VCENTER: /* fall through */
80 default: flags |= DT_VCENTER; break;
84 /* GroupBox's text is always single line and is top aligned. */
85 flags |= DT_SINGLELINE | DT_TOP;
90 static inline WCHAR *get_button_text(HWND hwnd)
94 text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
95 if (text) InternalGetWindowText(hwnd, text, len + 1);
99 static void PB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
101 static const int states[] = { PBS_NORMAL, PBS_DISABLED, PBS_HOT, PBS_PRESSED, PBS_DEFAULTED };
103 RECT bgRect, textRect;
104 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
105 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
106 int state = states[ drawState ];
107 WCHAR *text = get_button_text(hwnd);
109 GetClientRect(hwnd, &bgRect);
110 GetThemeBackgroundContentRect(theme, hDC, BP_PUSHBUTTON, state, &bgRect, &textRect);
112 if (IsThemeBackgroundPartiallyTransparent(theme, BP_PUSHBUTTON, state))
113 DrawThemeParentBackground(hwnd, hDC, NULL);
114 DrawThemeBackground(theme, hDC, BP_PUSHBUTTON, state, &bgRect, NULL);
117 DrawThemeText(theme, hDC, BP_PUSHBUTTON, state, text, lstrlenW(text), dtFlags, 0, &textRect);
118 HeapFree(GetProcessHeap(), 0, text);
121 if (hPrevFont) SelectObject(hDC, hPrevFont);
124 static void CB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
126 static const int cb_states[3][5] =
128 { CBS_UNCHECKEDNORMAL, CBS_UNCHECKEDDISABLED, CBS_UNCHECKEDHOT, CBS_UNCHECKEDPRESSED, CBS_UNCHECKEDNORMAL },
129 { CBS_CHECKEDNORMAL, CBS_CHECKEDDISABLED, CBS_CHECKEDHOT, CBS_CHECKEDPRESSED, CBS_CHECKEDNORMAL },
130 { CBS_MIXEDNORMAL, CBS_MIXEDDISABLED, CBS_MIXEDHOT, CBS_MIXEDPRESSED, CBS_MIXEDNORMAL }
133 static const int rb_states[2][5] =
135 { RBS_UNCHECKEDNORMAL, RBS_UNCHECKEDDISABLED, RBS_UNCHECKEDHOT, RBS_UNCHECKEDPRESSED, RBS_UNCHECKEDNORMAL },
136 { RBS_CHECKEDNORMAL, RBS_CHECKEDDISABLED, RBS_CHECKEDHOT, RBS_CHECKEDPRESSED, RBS_CHECKEDNORMAL }
139 static const int cb_size = 13;
141 RECT bgRect, textRect;
142 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
143 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
144 LRESULT checkState = SendMessageW(hwnd, BM_GETCHECK, 0, 0);
145 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
146 int part = ((dwStyle & BUTTON_TYPE) == BS_RADIOBUTTON) || ((dwStyle & BUTTON_TYPE) == BS_AUTORADIOBUTTON)
149 int state = (part == BP_CHECKBOX)
150 ? cb_states[ checkState ][ drawState ]
151 : rb_states[ checkState ][ drawState ];
152 WCHAR *text = get_button_text(hwnd);
154 GetClientRect(hwnd, &bgRect);
155 GetThemeBackgroundContentRect(theme, hDC, part, state, &bgRect, &textRect);
157 if (dtFlags & DT_SINGLELINE) /* Center the checkbox / radio button to the text. */
158 bgRect.top = bgRect.top + (textRect.bottom - textRect.top - cb_size) / 2;
160 /* adjust for the check/radio marker */
161 bgRect.bottom = bgRect.top + cb_size;
162 bgRect.right = bgRect.left + cb_size;
163 textRect.left = bgRect.right + 6;
165 if (IsThemeBackgroundPartiallyTransparent(theme, part, state))
166 DrawThemeParentBackground(hwnd, hDC, NULL);
167 DrawThemeBackground(theme, hDC, part, state, &bgRect, NULL);
170 DrawThemeText(theme, hDC, part, state, text, lstrlenW(text), dtFlags, 0, &textRect);
171 HeapFree(GetProcessHeap(), 0, text);
174 if (hPrevFont) SelectObject(hDC, hPrevFont);
177 static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
179 static const int states[] = { GBS_NORMAL, GBS_DISABLED, GBS_NORMAL, GBS_NORMAL, GBS_NORMAL };
181 RECT bgRect, textRect, contentRect;
182 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
183 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
184 int state = states[ drawState ];
185 WCHAR *text = get_button_text(hwnd);
187 GetClientRect(hwnd, &bgRect);
193 GetTextExtentPoint32W(hDC, text, lstrlenW(text), &textExtent);
194 bgRect.top += (textExtent.cy / 2);
196 textRect.bottom = textRect.top + textExtent.cy;
197 textRect.right = textRect.left + textExtent.cx + 4;
199 ExcludeClipRect(hDC, textRect.left, textRect.top, textRect.right, textRect.bottom);
202 GetThemeBackgroundContentRect(theme, hDC, BP_GROUPBOX, state, &bgRect, &contentRect);
203 ExcludeClipRect(hDC, contentRect.left, contentRect.top, contentRect.right, contentRect.bottom);
205 if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
206 DrawThemeParentBackground(hwnd, hDC, NULL);
207 DrawThemeBackground(theme, hDC, BP_GROUPBOX, state, &bgRect, NULL);
209 SelectClipRgn(hDC, NULL);
215 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
216 HeapFree(GetProcessHeap(), 0, text);
219 if (hPrevFont) SelectObject(hDC, hPrevFont);
222 static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
224 PB_draw, /* BS_PUSHBUTTON */
225 PB_draw, /* BS_DEFPUSHBUTTON */
226 CB_draw, /* BS_CHECKBOX */
227 CB_draw, /* BS_AUTOCHECKBOX */
228 CB_draw, /* BS_RADIOBUTTON */
229 CB_draw, /* BS_3STATE */
230 CB_draw, /* BS_AUTO3STATE */
231 GB_draw, /* BS_GROUPBOX */
232 NULL, /* BS_USERBUTTON */
233 CB_draw, /* BS_AUTORADIOBUTTON */
234 NULL, /* Not defined */
235 NULL, /* BS_OWNERDRAW */
236 NULL, /* Not defined */
237 NULL, /* Not defined */
238 NULL, /* Not defined */
239 NULL, /* Not defined */
242 static BOOL BUTTON_Paint(HTHEME theme, HWND hwnd, HDC hParamDC)
246 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
247 DWORD dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
248 UINT dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
249 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
250 ButtonState drawState;
251 pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
256 if(IsWindowEnabled(hwnd))
258 if(state & BST_PUSHED) drawState = STATE_PRESSED;
259 else if(state & BST_HOT) drawState = STATE_HOT;
260 else if(state & BST_FOCUS) drawState = STATE_DEFAULTED;
261 else drawState = STATE_NORMAL;
263 else drawState = STATE_DISABLED;
265 hDC = hParamDC ? hParamDC : BeginPaint(hwnd, &ps);
266 paint(theme, hwnd, hDC, drawState, dtFlags);
267 if (!hParamDC) EndPaint(hwnd, &ps);
271 /**********************************************************************
272 * The button control subclass window proc.
274 LRESULT CALLBACK THEMING_ButtonSubclassProc(HWND hwnd, UINT msg,
275 WPARAM wParam, LPARAM lParam,
278 const WCHAR* themeClass = WC_BUTTONW;
285 result = THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
286 OpenThemeData(hwnd, themeClass);
290 theme = GetWindowTheme(hwnd);
291 CloseThemeData (theme);
292 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
294 case WM_THEMECHANGED:
295 theme = GetWindowTheme(hwnd);
296 CloseThemeData (theme);
297 OpenThemeData(hwnd, themeClass);
300 case WM_SYSCOLORCHANGE:
301 theme = GetWindowTheme(hwnd);
302 if (!theme) return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
303 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
304 * which will do the repaint. */
308 theme = GetWindowTheme(hwnd);
309 if (theme && BUTTON_Paint(theme, hwnd, (HDC)wParam))
312 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
315 theme = GetWindowTheme(hwnd);
316 if (theme) RedrawWindow(hwnd, NULL, NULL,
317 RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
318 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
322 TRACKMOUSEEVENT mouse_event;
323 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
324 mouse_event.dwFlags = TME_QUERY;
325 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
327 mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
328 mouse_event.hwndTrack = hwnd;
329 mouse_event.dwHoverTime = 1;
330 TrackMouseEvent(&mouse_event);
337 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
338 SetWindowLongW(hwnd, 0, state|BST_HOT);
339 InvalidateRect(hwnd, NULL, FALSE);
345 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
346 SetWindowLongW(hwnd, 0, state&(~BST_HOT));
347 InvalidateRect(hwnd, NULL, FALSE);
353 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);