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
23 WINE_DEFAULT_DEBUG_CHANNEL(theme_button
);
25 #define BUTTON_TYPE 0x0f /* bit mask for the available button types */
27 /* These are indices into a states array to determine the theme state for a given theme part. */
37 typedef void (*pfThemedPaint
)(HTHEME theme
, HWND hwnd
, HDC hdc
, ButtonState drawState
, UINT dtFlags
, BOOL focused
);
39 static UINT
get_drawtext_flags(DWORD style
, DWORD ex_style
)
43 if (style
& BS_PUSHLIKE
)
44 style
&= ~BUTTON_TYPE
;
46 if (!(style
& BS_MULTILINE
))
47 flags
|= DT_SINGLELINE
;
49 flags
|= DT_WORDBREAK
;
51 switch (style
& BS_CENTER
)
53 case BS_LEFT
: flags
|= DT_LEFT
; break;
54 case BS_RIGHT
: flags
|= DT_RIGHT
; break;
55 case BS_CENTER
: flags
|= DT_CENTER
; break;
57 flags
|= ((style
& BUTTON_TYPE
) <= BS_DEFPUSHBUTTON
)
58 ? DT_CENTER
: DT_LEFT
;
61 if (ex_style
& WS_EX_RIGHT
)
62 flags
= DT_RIGHT
| (flags
& ~(DT_LEFT
| DT_CENTER
));
64 if ((style
& BUTTON_TYPE
) != BS_GROUPBOX
)
66 switch (style
& BS_VCENTER
)
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;
75 /* GroupBox's text is always single line and is top aligned. */
76 flags
|= DT_SINGLELINE
| DT_TOP
;
81 static inline WCHAR
*get_button_text(HWND hwnd
)
85 text
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
86 if (text
) InternalGetWindowText(hwnd
, text
, len
+ 1);
90 static void PB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
)
92 static const int states
[] = { PBS_NORMAL
, PBS_DISABLED
, PBS_HOT
, PBS_PRESSED
, PBS_DEFAULTED
};
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
);
100 GetClientRect(hwnd
, &bgRect
);
101 GetThemeBackgroundContentRect(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, &textRect
);
103 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_PUSHBUTTON
, state
))
104 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
105 DrawThemeBackground(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, NULL
);
108 DrawThemeText(theme
, hDC
, BP_PUSHBUTTON
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
109 HeapFree(GetProcessHeap(), 0, text
);
115 RECT focusRect
= bgRect
;
117 GetThemeMargins(theme
, hDC
, BP_PUSHBUTTON
, state
, TMT_CONTENTMARGINS
, NULL
, &margins
);
119 focusRect
.left
+= margins
.cxLeftWidth
;
120 focusRect
.top
+= margins
.cyTopHeight
;
121 focusRect
.right
-= margins
.cxRightWidth
;
122 focusRect
.bottom
-= margins
.cyBottomHeight
;
124 DrawFocusRect( hDC
, &focusRect
);
127 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
130 static void CB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
)
132 static const int cb_states
[3][5] =
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
}
139 static const int rb_states
[2][5] =
141 { RBS_UNCHECKEDNORMAL
, RBS_UNCHECKEDDISABLED
, RBS_UNCHECKEDHOT
, RBS_UNCHECKEDPRESSED
, RBS_UNCHECKEDNORMAL
},
142 { RBS_CHECKEDNORMAL
, RBS_CHECKEDDISABLED
, RBS_CHECKEDHOT
, RBS_CHECKEDPRESSED
, RBS_CHECKEDNORMAL
}
145 static const int cb_size
= 13;
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
)
154 int state
= (part
== BP_CHECKBOX
)
155 ? cb_states
[ checkState
][ drawState
]
156 : rb_states
[ checkState
][ drawState
];
157 WCHAR
*text
= get_button_text(hwnd
);
159 BOOL created_font
= FALSE
;
161 HRESULT hr
= GetThemeFont(theme
, hDC
, part
, state
, TMT_FONT
, &lf
);
163 font
= CreateFontIndirectW(&lf
);
165 TRACE("Failed to create font\n");
167 TRACE("font = %s\n", debugstr_w(lf
.lfFaceName
));
168 hPrevFont
= SelectObject(hDC
, font
);
172 font
= (HFONT
)SendMessageW(hwnd
, WM_GETFONT
, 0, 0);
173 hPrevFont
= SelectObject(hDC
, font
);
176 GetClientRect(hwnd
, &bgRect
);
177 GetThemeBackgroundContentRect(theme
, hDC
, part
, state
, &bgRect
, &textRect
);
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;
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;
187 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
189 DrawThemeBackground(theme
, hDC
, part
, state
, &bgRect
, NULL
);
192 DrawThemeText(theme
, hDC
, part
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
198 focusRect
= textRect
;
200 DrawTextW(hDC
, text
, lstrlenW(text
), &focusRect
, dtFlags
| DT_CALCRECT
);
202 if (focusRect
.right
< textRect
.right
) focusRect
.right
++;
203 focusRect
.bottom
= textRect
.bottom
;
205 DrawFocusRect( hDC
, &focusRect
);
208 HeapFree(GetProcessHeap(), 0, text
);
211 if (created_font
) DeleteObject(font
);
212 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
215 static void GB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
)
217 static const int states
[] = { GBS_NORMAL
, GBS_DISABLED
, GBS_NORMAL
, GBS_NORMAL
, GBS_NORMAL
};
219 RECT bgRect
, textRect
, contentRect
;
220 int state
= states
[ drawState
];
221 WCHAR
*text
= get_button_text(hwnd
);
223 HFONT font
, hPrevFont
= NULL
;
224 BOOL created_font
= FALSE
;
226 HRESULT hr
= GetThemeFont(theme
, hDC
, BP_GROUPBOX
, state
, TMT_FONT
, &lf
);
228 font
= CreateFontIndirectW(&lf
);
230 TRACE("Failed to create font\n");
232 hPrevFont
= SelectObject(hDC
, font
);
236 font
= (HFONT
)SendMessageW(hwnd
, WM_GETFONT
, 0, 0);
237 hPrevFont
= SelectObject(hDC
, font
);
240 GetClientRect(hwnd
, &bgRect
);
246 GetTextExtentPoint32W(hDC
, text
, lstrlenW(text
), &textExtent
);
247 bgRect
.top
+= (textExtent
.cy
/ 2);
249 textRect
.bottom
= textRect
.top
+ textExtent
.cy
;
250 textRect
.right
= textRect
.left
+ textExtent
.cx
+ 4;
252 ExcludeClipRect(hDC
, textRect
.left
, textRect
.top
, textRect
.right
, textRect
.bottom
);
255 GetThemeBackgroundContentRect(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, &contentRect
);
256 ExcludeClipRect(hDC
, contentRect
.left
, contentRect
.top
, contentRect
.right
, contentRect
.bottom
);
258 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_GROUPBOX
, state
))
259 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
260 DrawThemeBackground(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, NULL
);
262 SelectClipRgn(hDC
, NULL
);
268 DrawThemeText(theme
, hDC
, BP_GROUPBOX
, state
, text
, lstrlenW(text
), 0, 0, &textRect
);
269 HeapFree(GetProcessHeap(), 0, text
);
272 if (created_font
) DeleteObject(font
);
273 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
276 static const pfThemedPaint btnThemedPaintFunc
[BUTTON_TYPE
+ 1] =
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 */
296 static BOOL
BUTTON_Paint(HTHEME theme
, HWND hwnd
, HDC hParamDC
)
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
];
310 if(IsWindowEnabled(hwnd
))
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
;
317 else drawState
= STATE_DISABLED
;
319 hDC
= hParamDC
? hParamDC
: BeginPaint(hwnd
, &ps
);
320 paint(theme
, hwnd
, hDC
, drawState
, dtFlags
, state
& BST_FOCUS
);
321 if (!hParamDC
) EndPaint(hwnd
, &ps
);
325 /**********************************************************************
326 * The button control subclass window proc.
328 LRESULT CALLBACK
THEMING_ButtonSubclassProc(HWND hwnd
, UINT msg
,
329 WPARAM wParam
, LPARAM lParam
,
332 const WCHAR
* themeClass
= WC_BUTTONW
;
339 result
= THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
340 OpenThemeData(hwnd
, themeClass
);
344 theme
= GetWindowTheme(hwnd
);
345 CloseThemeData (theme
);
346 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
348 case WM_THEMECHANGED
:
349 theme
= GetWindowTheme(hwnd
);
350 CloseThemeData (theme
);
351 OpenThemeData(hwnd
, themeClass
);
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. */
362 theme
= GetWindowTheme(hwnd
);
363 if (theme
&& BUTTON_Paint(theme
, hwnd
, (HDC
)wParam
))
366 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
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
);
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
)))
381 mouse_event
.dwFlags
= TME_HOVER
|TME_LEAVE
;
382 mouse_event
.hwndTrack
= hwnd
;
383 mouse_event
.dwHoverTime
= 1;
384 TrackMouseEvent(&mouse_event
);
391 int state
= (int)SendMessageW(hwnd
, BM_GETSTATE
, 0, 0);
392 SetWindowLongW(hwnd
, 0, state
|BST_HOT
);
393 InvalidateRect(hwnd
, NULL
, FALSE
);
399 int state
= (int)SendMessageW(hwnd
, BM_GETSTATE
, 0, 0);
400 SetWindowLongW(hwnd
, 0, state
&(~BST_HOT
));
401 InvalidateRect(hwnd
, NULL
, FALSE
);
407 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);