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
24 #define BUTTON_TYPE 0x0f /* bit mask for the available button types */
26 /* These are indices into a states array to determine the theme state for a given theme part. */
36 typedef void (*pfThemedPaint
)(HTHEME theme
, HWND hwnd
, HDC hdc
, ButtonState drawState
, UINT dtFlags
);
38 static UINT
get_drawtext_flags(DWORD style
, DWORD ex_style
)
42 if (style
& BS_PUSHLIKE
)
43 style
&= ~BUTTON_TYPE
;
45 if (!(style
& BS_MULTILINE
))
46 flags
|= DT_SINGLELINE
;
48 flags
|= DT_WORDBREAK
;
50 switch (style
& BS_CENTER
)
52 case BS_LEFT
: flags
|= DT_LEFT
; break;
53 case BS_RIGHT
: flags
|= DT_RIGHT
; break;
54 case BS_CENTER
: flags
|= DT_CENTER
; break;
56 flags
|= ((style
& BUTTON_TYPE
) <= BS_DEFPUSHBUTTON
)
57 ? DT_CENTER
: DT_LEFT
;
60 if (ex_style
& WS_EX_RIGHT
)
61 flags
= DT_RIGHT
| (flags
& ~(DT_LEFT
| DT_CENTER
));
63 if ((style
& BUTTON_TYPE
) != BS_GROUPBOX
)
65 switch (style
& BS_VCENTER
)
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;
74 /* GroupBox's text is always single line and is top aligned. */
75 flags
|= DT_SINGLELINE
| DT_TOP
;
80 static inline WCHAR
*get_button_text(HWND hwnd
)
84 text
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
85 if (text
) InternalGetWindowText(hwnd
, text
, len
+ 1);
89 static void PB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
)
91 static const int states
[] = { PBS_NORMAL
, PBS_DISABLED
, PBS_HOT
, PBS_PRESSED
, PBS_DEFAULTED
};
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
);
99 GetClientRect(hwnd
, &bgRect
);
100 GetThemeBackgroundContentRect(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, &textRect
);
102 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_PUSHBUTTON
, state
))
103 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
104 DrawThemeBackground(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, NULL
);
107 DrawThemeText(theme
, hDC
, BP_PUSHBUTTON
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
108 HeapFree(GetProcessHeap(), 0, text
);
111 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
114 static void CB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
)
116 static const int cb_states
[3][5] =
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
}
123 static const int rb_states
[2][5] =
125 { RBS_UNCHECKEDNORMAL
, RBS_UNCHECKEDDISABLED
, RBS_UNCHECKEDHOT
, RBS_UNCHECKEDPRESSED
, RBS_UNCHECKEDNORMAL
},
126 { RBS_CHECKEDNORMAL
, RBS_CHECKEDDISABLED
, RBS_CHECKEDHOT
, RBS_CHECKEDPRESSED
, RBS_CHECKEDNORMAL
}
129 static const int cb_size
= 13;
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
)
139 int state
= (part
== BP_CHECKBOX
)
140 ? cb_states
[ checkState
][ drawState
]
141 : rb_states
[ checkState
][ drawState
];
142 WCHAR
*text
= get_button_text(hwnd
);
144 GetClientRect(hwnd
, &bgRect
);
145 GetThemeBackgroundContentRect(theme
, hDC
, part
, state
, &bgRect
, &textRect
);
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;
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;
155 if (IsThemeBackgroundPartiallyTransparent(theme
, part
, state
))
156 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
157 DrawThemeBackground(theme
, hDC
, part
, state
, &bgRect
, NULL
);
160 DrawThemeText(theme
, hDC
, part
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
161 HeapFree(GetProcessHeap(), 0, text
);
164 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
167 static void GB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
)
169 static const int states
[] = { GBS_NORMAL
, GBS_DISABLED
, GBS_NORMAL
, GBS_NORMAL
, GBS_NORMAL
};
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
);
177 GetClientRect(hwnd
, &bgRect
);
183 GetTextExtentPoint32W(hDC
, text
, lstrlenW(text
), &textExtent
);
184 bgRect
.top
+= (textExtent
.cy
/ 2);
186 textRect
.bottom
= textRect
.top
+ textExtent
.cy
;
187 textRect
.right
= textRect
.left
+ textExtent
.cx
+ 4;
189 ExcludeClipRect(hDC
, textRect
.left
, textRect
.top
, textRect
.right
, textRect
.bottom
);
192 GetThemeBackgroundContentRect(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, &contentRect
);
193 ExcludeClipRect(hDC
, contentRect
.left
, contentRect
.top
, contentRect
.right
, contentRect
.bottom
);
195 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_GROUPBOX
, state
))
196 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
197 DrawThemeBackground(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, NULL
);
199 SelectClipRgn(hDC
, NULL
);
205 DrawThemeText(theme
, hDC
, BP_GROUPBOX
, state
, text
, lstrlenW(text
), 0, 0, &textRect
);
206 HeapFree(GetProcessHeap(), 0, text
);
209 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
212 static const pfThemedPaint btnThemedPaintFunc
[BUTTON_TYPE
+ 1] =
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 */
232 static BOOL
BUTTON_Paint(HTHEME theme
, HWND hwnd
, HDC hParamDC
)
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
];
246 if(IsWindowEnabled(hwnd
))
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
;
253 else drawState
= STATE_DISABLED
;
255 hDC
= hParamDC
? hParamDC
: BeginPaint(hwnd
, &ps
);
256 paint(theme
, hwnd
, hDC
, drawState
, dtFlags
);
257 if (!hParamDC
) EndPaint(hwnd
, &ps
);
261 /**********************************************************************
262 * The button control subclass window proc.
264 LRESULT CALLBACK
THEMING_ButtonSubclassProc(HWND hwnd
, UINT msg
,
265 WPARAM wParam
, LPARAM lParam
,
268 const WCHAR
* themeClass
= WC_BUTTONW
;
275 result
= THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
276 OpenThemeData(hwnd
, themeClass
);
280 theme
= GetWindowTheme(hwnd
);
281 CloseThemeData (theme
);
282 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
284 case WM_THEMECHANGED
:
285 theme
= GetWindowTheme(hwnd
);
286 CloseThemeData (theme
);
287 OpenThemeData(hwnd
, themeClass
);
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. */
298 theme
= GetWindowTheme(hwnd
);
299 if (theme
&& BUTTON_Paint(theme
, hwnd
, (HDC
)wParam
))
302 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
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
);
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
)))
317 mouse_event
.dwFlags
= TME_HOVER
|TME_LEAVE
;
318 mouse_event
.hwndTrack
= hwnd
;
319 mouse_event
.dwHoverTime
= 1;
320 TrackMouseEvent(&mouse_event
);
327 int state
= (int)SendMessageW(hwnd
, BM_GETSTATE
, 0, 0);
328 SetWindowLongW(hwnd
, 0, state
|BST_HOT
);
329 InvalidateRect(hwnd
, NULL
, FALSE
);
335 int state
= (int)SendMessageW(hwnd
, BM_GETSTATE
, 0, 0);
336 SetWindowLongW(hwnd
, 0, state
&(~BST_HOT
));
337 InvalidateRect(hwnd
, NULL
, FALSE
);
343 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);