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
}
146 RECT bgRect
, textRect
;
147 HFONT font
, hPrevFont
= 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
)
153 int state
= (part
== BP_CHECKBOX
)
154 ? cb_states
[ checkState
][ drawState
]
155 : rb_states
[ checkState
][ drawState
];
156 WCHAR
*text
= get_button_text(hwnd
);
158 BOOL created_font
= FALSE
;
160 HRESULT hr
= GetThemeFont(theme
, hDC
, part
, state
, TMT_FONT
, &lf
);
162 font
= CreateFontIndirectW(&lf
);
164 TRACE("Failed to create font\n");
166 TRACE("font = %s\n", debugstr_w(lf
.lfFaceName
));
167 hPrevFont
= SelectObject(hDC
, font
);
171 font
= (HFONT
)SendMessageW(hwnd
, WM_GETFONT
, 0, 0);
172 hPrevFont
= SelectObject(hDC
, font
);
175 if (FAILED(GetThemePartSize(theme
, hDC
, part
, state
, NULL
, TS_DRAW
, &sz
)))
178 GetClientRect(hwnd
, &bgRect
);
179 GetThemeBackgroundContentRect(theme
, hDC
, part
, state
, &bgRect
, &textRect
);
181 if (dtFlags
& DT_SINGLELINE
) /* Center the checkbox / radio button to the text. */
182 bgRect
.top
= bgRect
.top
+ (textRect
.bottom
- textRect
.top
- sz
.cy
) / 2;
184 /* adjust for the check/radio marker */
185 bgRect
.bottom
= bgRect
.top
+ sz
.cy
;
186 bgRect
.right
= bgRect
.left
+ sz
.cx
;
187 textRect
.left
= bgRect
.right
+ 6;
189 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
191 DrawThemeBackground(theme
, hDC
, part
, state
, &bgRect
, NULL
);
194 DrawThemeText(theme
, hDC
, part
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
200 focusRect
= textRect
;
202 DrawTextW(hDC
, text
, lstrlenW(text
), &focusRect
, dtFlags
| DT_CALCRECT
);
204 if (focusRect
.right
< textRect
.right
) focusRect
.right
++;
205 focusRect
.bottom
= textRect
.bottom
;
207 DrawFocusRect( hDC
, &focusRect
);
210 HeapFree(GetProcessHeap(), 0, text
);
213 if (created_font
) DeleteObject(font
);
214 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
217 static void GB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
)
219 static const int states
[] = { GBS_NORMAL
, GBS_DISABLED
, GBS_NORMAL
, GBS_NORMAL
, GBS_NORMAL
};
221 RECT bgRect
, textRect
, contentRect
;
222 int state
= states
[ drawState
];
223 WCHAR
*text
= get_button_text(hwnd
);
225 HFONT font
, hPrevFont
= NULL
;
226 BOOL created_font
= FALSE
;
228 HRESULT hr
= GetThemeFont(theme
, hDC
, BP_GROUPBOX
, state
, TMT_FONT
, &lf
);
230 font
= CreateFontIndirectW(&lf
);
232 TRACE("Failed to create font\n");
234 hPrevFont
= SelectObject(hDC
, font
);
238 font
= (HFONT
)SendMessageW(hwnd
, WM_GETFONT
, 0, 0);
239 hPrevFont
= SelectObject(hDC
, font
);
242 GetClientRect(hwnd
, &bgRect
);
248 GetTextExtentPoint32W(hDC
, text
, lstrlenW(text
), &textExtent
);
249 bgRect
.top
+= (textExtent
.cy
/ 2);
251 textRect
.bottom
= textRect
.top
+ textExtent
.cy
;
252 textRect
.right
= textRect
.left
+ textExtent
.cx
+ 4;
254 ExcludeClipRect(hDC
, textRect
.left
, textRect
.top
, textRect
.right
, textRect
.bottom
);
257 GetThemeBackgroundContentRect(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, &contentRect
);
258 ExcludeClipRect(hDC
, contentRect
.left
, contentRect
.top
, contentRect
.right
, contentRect
.bottom
);
260 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_GROUPBOX
, state
))
261 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
262 DrawThemeBackground(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, NULL
);
264 SelectClipRgn(hDC
, NULL
);
268 InflateRect(&textRect
, -2, 0);
269 DrawThemeText(theme
, hDC
, BP_GROUPBOX
, state
, text
, lstrlenW(text
), 0, 0, &textRect
);
270 HeapFree(GetProcessHeap(), 0, text
);
273 if (created_font
) DeleteObject(font
);
274 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
277 static const pfThemedPaint btnThemedPaintFunc
[BUTTON_TYPE
+ 1] =
279 PB_draw
, /* BS_PUSHBUTTON */
280 PB_draw
, /* BS_DEFPUSHBUTTON */
281 CB_draw
, /* BS_CHECKBOX */
282 CB_draw
, /* BS_AUTOCHECKBOX */
283 CB_draw
, /* BS_RADIOBUTTON */
284 CB_draw
, /* BS_3STATE */
285 CB_draw
, /* BS_AUTO3STATE */
286 GB_draw
, /* BS_GROUPBOX */
287 NULL
, /* BS_USERBUTTON */
288 CB_draw
, /* BS_AUTORADIOBUTTON */
289 NULL
, /* Not defined */
290 NULL
, /* BS_OWNERDRAW */
291 NULL
, /* Not defined */
292 NULL
, /* Not defined */
293 NULL
, /* Not defined */
294 NULL
, /* Not defined */
297 BOOL
BUTTON_PaintWithTheme(HTHEME theme
, HWND hwnd
, HDC hParamDC
)
299 DWORD dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
300 DWORD dwStyleEx
= GetWindowLongW(hwnd
, GWL_EXSTYLE
);
301 UINT dtFlags
= get_drawtext_flags(dwStyle
, dwStyleEx
);
302 int state
= (int)SendMessageW(hwnd
, BM_GETSTATE
, 0, 0);
303 ButtonState drawState
;
304 pfThemedPaint paint
= btnThemedPaintFunc
[ dwStyle
& BUTTON_TYPE
];
308 if(IsWindowEnabled(hwnd
))
310 if(state
& BST_PUSHED
) drawState
= STATE_PRESSED
;
311 else if(state
& BST_HOT
) drawState
= STATE_HOT
;
312 else if(state
& BST_FOCUS
) drawState
= STATE_DEFAULTED
;
313 else drawState
= STATE_NORMAL
;
315 else drawState
= STATE_DISABLED
;
317 paint(theme
, hwnd
, hParamDC
, drawState
, dtFlags
, state
& BST_FOCUS
);