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
, LPARAM prfFlag
);
39 #define STATE_GWL_OFFSET 0
40 #define HFONT_GWL_OFFSET (sizeof(LONG))
42 static inline LONG
get_button_state( HWND hwnd
)
44 return GetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
);
47 static inline HFONT
get_button_font( HWND hwnd
)
49 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
52 static UINT
get_drawtext_flags(DWORD style
, DWORD ex_style
)
56 if (style
& BS_PUSHLIKE
)
57 style
&= ~BUTTON_TYPE
;
59 if (!(style
& BS_MULTILINE
))
60 flags
|= DT_SINGLELINE
;
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;
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;
88 /* GroupBox's text is always single line and is top aligned. */
89 flags
|= DT_SINGLELINE
| DT_TOP
;
94 static inline WCHAR
*get_button_text(HWND hwnd
)
98 text
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
99 if (text
) InternalGetWindowText(hwnd
, text
, len
+ 1);
103 static void PB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
105 static const int states
[] = { PBS_NORMAL
, PBS_DISABLED
, PBS_HOT
, PBS_PRESSED
, PBS_DEFAULTED
};
107 RECT bgRect
, textRect
;
108 HFONT font
= get_button_font(hwnd
);
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 (prfFlag
== 0 || (prfFlag
& PRF_ERASEBKGND
))
118 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_PUSHBUTTON
, state
))
119 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
122 DrawThemeBackground(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, NULL
);
125 DrawThemeText(theme
, hDC
, BP_PUSHBUTTON
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
126 HeapFree(GetProcessHeap(), 0, text
);
132 RECT focusRect
= bgRect
;
134 GetThemeMargins(theme
, hDC
, BP_PUSHBUTTON
, state
, TMT_CONTENTMARGINS
, NULL
, &margins
);
136 focusRect
.left
+= margins
.cxLeftWidth
;
137 focusRect
.top
+= margins
.cyTopHeight
;
138 focusRect
.right
-= margins
.cxRightWidth
;
139 focusRect
.bottom
-= margins
.cyBottomHeight
;
141 DrawFocusRect( hDC
, &focusRect
);
144 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
147 static void CB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
149 static const int cb_states
[3][5] =
151 { CBS_UNCHECKEDNORMAL
, CBS_UNCHECKEDDISABLED
, CBS_UNCHECKEDHOT
, CBS_UNCHECKEDPRESSED
, CBS_UNCHECKEDNORMAL
},
152 { CBS_CHECKEDNORMAL
, CBS_CHECKEDDISABLED
, CBS_CHECKEDHOT
, CBS_CHECKEDPRESSED
, CBS_CHECKEDNORMAL
},
153 { CBS_MIXEDNORMAL
, CBS_MIXEDDISABLED
, CBS_MIXEDHOT
, CBS_MIXEDPRESSED
, CBS_MIXEDNORMAL
}
156 static const int rb_states
[2][5] =
158 { RBS_UNCHECKEDNORMAL
, RBS_UNCHECKEDDISABLED
, RBS_UNCHECKEDHOT
, RBS_UNCHECKEDPRESSED
, RBS_UNCHECKEDNORMAL
},
159 { RBS_CHECKEDNORMAL
, RBS_CHECKEDDISABLED
, RBS_CHECKEDHOT
, RBS_CHECKEDPRESSED
, RBS_CHECKEDNORMAL
}
163 RECT bgRect
, textRect
;
164 HFONT font
, hPrevFont
= NULL
;
165 LRESULT checkState
= get_button_state(hwnd
) & 3;
166 DWORD dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
167 int part
= ((dwStyle
& BUTTON_TYPE
) == BS_RADIOBUTTON
) || ((dwStyle
& BUTTON_TYPE
) == BS_AUTORADIOBUTTON
)
170 int state
= (part
== BP_CHECKBOX
)
171 ? cb_states
[ checkState
][ drawState
]
172 : rb_states
[ checkState
][ drawState
];
173 WCHAR
*text
= get_button_text(hwnd
);
175 BOOL created_font
= FALSE
;
177 HRESULT hr
= GetThemeFont(theme
, hDC
, part
, state
, TMT_FONT
, &lf
);
179 font
= CreateFontIndirectW(&lf
);
181 TRACE("Failed to create font\n");
183 TRACE("font = %s\n", debugstr_w(lf
.lfFaceName
));
184 hPrevFont
= SelectObject(hDC
, font
);
188 font
= get_button_font(hwnd
);
189 hPrevFont
= SelectObject(hDC
, font
);
192 if (FAILED(GetThemePartSize(theme
, hDC
, part
, state
, NULL
, TS_DRAW
, &sz
)))
195 GetClientRect(hwnd
, &bgRect
);
196 GetThemeBackgroundContentRect(theme
, hDC
, part
, state
, &bgRect
, &textRect
);
198 if (dtFlags
& DT_SINGLELINE
) /* Center the checkbox / radio button to the text. */
199 bgRect
.top
= bgRect
.top
+ (textRect
.bottom
- textRect
.top
- sz
.cy
) / 2;
201 /* adjust for the check/radio marker */
202 bgRect
.bottom
= bgRect
.top
+ sz
.cy
;
203 bgRect
.right
= bgRect
.left
+ sz
.cx
;
204 textRect
.left
= bgRect
.right
+ 6;
206 if (prfFlag
== 0 || (prfFlag
& PRF_ERASEBKGND
))
208 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
211 DrawThemeBackground(theme
, hDC
, part
, state
, &bgRect
, NULL
);
214 DrawThemeText(theme
, hDC
, part
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
220 focusRect
= textRect
;
222 DrawTextW(hDC
, text
, lstrlenW(text
), &focusRect
, dtFlags
| DT_CALCRECT
);
224 if (focusRect
.right
< textRect
.right
) focusRect
.right
++;
225 focusRect
.bottom
= textRect
.bottom
;
227 DrawFocusRect( hDC
, &focusRect
);
230 HeapFree(GetProcessHeap(), 0, text
);
233 if (created_font
) DeleteObject(font
);
234 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
237 static void GB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
239 static const int states
[] = { GBS_NORMAL
, GBS_DISABLED
, GBS_NORMAL
, GBS_NORMAL
, GBS_NORMAL
};
241 RECT bgRect
, textRect
, contentRect
;
242 int state
= states
[ drawState
];
243 WCHAR
*text
= get_button_text(hwnd
);
245 HFONT font
, hPrevFont
= NULL
;
246 BOOL created_font
= FALSE
;
248 HRESULT hr
= GetThemeFont(theme
, hDC
, BP_GROUPBOX
, state
, TMT_FONT
, &lf
);
250 font
= CreateFontIndirectW(&lf
);
252 TRACE("Failed to create font\n");
254 hPrevFont
= SelectObject(hDC
, font
);
258 font
= get_button_font(hwnd
);
259 hPrevFont
= SelectObject(hDC
, font
);
262 GetClientRect(hwnd
, &bgRect
);
268 GetTextExtentPoint32W(hDC
, text
, lstrlenW(text
), &textExtent
);
269 bgRect
.top
+= (textExtent
.cy
/ 2);
271 textRect
.bottom
= textRect
.top
+ textExtent
.cy
;
272 textRect
.right
= textRect
.left
+ textExtent
.cx
+ 4;
274 ExcludeClipRect(hDC
, textRect
.left
, textRect
.top
, textRect
.right
, textRect
.bottom
);
277 GetThemeBackgroundContentRect(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, &contentRect
);
278 ExcludeClipRect(hDC
, contentRect
.left
, contentRect
.top
, contentRect
.right
, contentRect
.bottom
);
280 if (prfFlag
== 0 || (prfFlag
& PRF_ERASEBKGND
))
282 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_GROUPBOX
, state
))
283 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
286 DrawThemeBackground(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, NULL
);
288 SelectClipRgn(hDC
, NULL
);
292 InflateRect(&textRect
, -2, 0);
293 DrawThemeText(theme
, hDC
, BP_GROUPBOX
, state
, text
, lstrlenW(text
), 0, 0, &textRect
);
294 HeapFree(GetProcessHeap(), 0, text
);
297 if (created_font
) DeleteObject(font
);
298 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
301 static const pfThemedPaint btnThemedPaintFunc
[BUTTON_TYPE
+ 1] =
303 PB_draw
, /* BS_PUSHBUTTON */
304 PB_draw
, /* BS_DEFPUSHBUTTON */
305 CB_draw
, /* BS_CHECKBOX */
306 CB_draw
, /* BS_AUTOCHECKBOX */
307 CB_draw
, /* BS_RADIOBUTTON */
308 CB_draw
, /* BS_3STATE */
309 CB_draw
, /* BS_AUTO3STATE */
310 GB_draw
, /* BS_GROUPBOX */
311 NULL
, /* BS_USERBUTTON */
312 CB_draw
, /* BS_AUTORADIOBUTTON */
313 NULL
, /* Not defined */
314 NULL
, /* BS_OWNERDRAW */
315 NULL
, /* Not defined */
316 NULL
, /* Not defined */
317 NULL
, /* Not defined */
318 NULL
, /* Not defined */
321 BOOL
BUTTON_PaintWithTheme(HTHEME theme
, HWND hwnd
, HDC hParamDC
, LPARAM prfFlag
)
323 DWORD dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
324 DWORD dwStyleEx
= GetWindowLongW(hwnd
, GWL_EXSTYLE
);
325 UINT dtFlags
= get_drawtext_flags(dwStyle
, dwStyleEx
);
326 int state
= get_button_state(hwnd
);
327 ButtonState drawState
;
328 pfThemedPaint paint
= btnThemedPaintFunc
[ dwStyle
& BUTTON_TYPE
];
332 if(IsWindowEnabled(hwnd
))
334 if(state
& BST_PUSHED
) drawState
= STATE_PRESSED
;
335 else if(state
& BST_HOT
) drawState
= STATE_HOT
;
336 else if(state
& BST_FOCUS
) drawState
= STATE_DEFAULTED
;
337 else drawState
= STATE_NORMAL
;
339 else drawState
= STATE_DISABLED
;
341 paint(theme
, hwnd
, hParamDC
, drawState
, dtFlags
, state
& BST_FOCUS
, prfFlag
);