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 static inline LONG
get_button_state( HWND hwnd
)
41 return _GetButtonData(hwnd
)->state
;
44 static inline HFONT
get_button_font( HWND hwnd
)
46 return (HFONT
)_GetButtonData(hwnd
)->font
;
49 static inline LONG_PTR
get_button_image(HWND hwnd
)
51 return _GetButtonData(hwnd
)->image
;
54 static UINT
get_drawtext_flags(DWORD style
, DWORD ex_style
)
58 if (style
& BS_PUSHLIKE
)
59 style
&= ~BUTTON_TYPE
;
61 if (!(style
& BS_MULTILINE
))
62 flags
|= DT_SINGLELINE
;
64 flags
|= DT_WORDBREAK
;
66 switch (style
& BS_CENTER
)
68 case BS_LEFT
: flags
|= DT_LEFT
; break;
69 case BS_RIGHT
: flags
|= DT_RIGHT
; break;
70 case BS_CENTER
: flags
|= DT_CENTER
; break;
72 flags
|= ((style
& BUTTON_TYPE
) <= BS_DEFPUSHBUTTON
)
73 ? DT_CENTER
: DT_LEFT
;
76 if (ex_style
& WS_EX_RIGHT
)
77 flags
= DT_RIGHT
| (flags
& ~(DT_LEFT
| DT_CENTER
));
79 if ((style
& BUTTON_TYPE
) != BS_GROUPBOX
)
81 switch (style
& BS_VCENTER
)
83 case BS_TOP
: flags
|= DT_TOP
; break;
84 case BS_BOTTOM
: flags
|= DT_BOTTOM
; break;
85 case BS_VCENTER
: /* fall through */
86 default: flags
|= DT_VCENTER
; break;
90 /* GroupBox's text is always single line and is top aligned. */
91 flags
|= DT_SINGLELINE
| DT_TOP
;
96 static inline WCHAR
*get_button_text(HWND hwnd
)
100 text
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
101 if (text
) InternalGetWindowText(hwnd
, text
, len
+ 1);
105 static void PB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
107 static const int states
[] = { PBS_NORMAL
, PBS_DISABLED
, PBS_HOT
, PBS_PRESSED
, PBS_DEFAULTED
};
109 RECT bgRect
, textRect
;
110 HFONT font
= get_button_font(hwnd
);
111 HFONT hPrevFont
= font
? SelectObject(hDC
, font
) : NULL
;
112 int state
= states
[ drawState
];
113 WCHAR
*text
= get_button_text(hwnd
);
114 PBUTTON_DATA pdata
= _GetButtonData(hwnd
);
117 GetClientRect(hwnd
, &bgRect
);
118 GetThemeBackgroundContentRect(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, &textRect
);
120 if (prfFlag
== 0 || (prfFlag
& PRF_ERASEBKGND
))
122 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_PUSHBUTTON
, state
))
123 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
126 DrawThemeBackground(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, NULL
);
128 if (pdata
->imlData
.himl
&& ImageList_GetIconSize(pdata
->imlData
.himl
, &ImageSize
.cx
, &ImageSize
.cy
))
130 int left
= textRect
.left
+ pdata
->imlData
.margin
.left
;
131 int top
= textRect
.top
+ (textRect
.bottom
- textRect
.top
- ImageSize
.cy
) / 2;
132 textRect
.left
+= pdata
->imlData
.margin
.left
+ pdata
->imlData
.margin
.right
+ ImageSize
.cy
;
133 ImageList_Draw(pdata
->imlData
.himl
, 0, hDC
, left
, top
, 0);
138 DrawThemeText(theme
, hDC
, BP_PUSHBUTTON
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
139 HeapFree(GetProcessHeap(), 0, text
);
145 RECT focusRect
= bgRect
;
147 GetThemeMargins(theme
, hDC
, BP_PUSHBUTTON
, state
, TMT_CONTENTMARGINS
, NULL
, &margins
);
149 focusRect
.left
+= margins
.cxLeftWidth
;
150 focusRect
.top
+= margins
.cyTopHeight
;
151 focusRect
.right
-= margins
.cxRightWidth
;
152 focusRect
.bottom
-= margins
.cyBottomHeight
;
154 DrawFocusRect( hDC
, &focusRect
);
157 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
160 static void CB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
162 static const int cb_states
[3][5] =
164 { CBS_UNCHECKEDNORMAL
, CBS_UNCHECKEDDISABLED
, CBS_UNCHECKEDHOT
, CBS_UNCHECKEDPRESSED
, CBS_UNCHECKEDNORMAL
},
165 { CBS_CHECKEDNORMAL
, CBS_CHECKEDDISABLED
, CBS_CHECKEDHOT
, CBS_CHECKEDPRESSED
, CBS_CHECKEDNORMAL
},
166 { CBS_MIXEDNORMAL
, CBS_MIXEDDISABLED
, CBS_MIXEDHOT
, CBS_MIXEDPRESSED
, CBS_MIXEDNORMAL
}
169 static const int rb_states
[2][5] =
171 { RBS_UNCHECKEDNORMAL
, RBS_UNCHECKEDDISABLED
, RBS_UNCHECKEDHOT
, RBS_UNCHECKEDPRESSED
, RBS_UNCHECKEDNORMAL
},
172 { RBS_CHECKEDNORMAL
, RBS_CHECKEDDISABLED
, RBS_CHECKEDHOT
, RBS_CHECKEDPRESSED
, RBS_CHECKEDNORMAL
}
176 RECT bgRect
, textRect
;
177 HFONT font
, hPrevFont
= NULL
;
178 LRESULT checkState
= get_button_state(hwnd
) & 3;
179 DWORD dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
180 int part
= ((dwStyle
& BUTTON_TYPE
) == BS_RADIOBUTTON
) || ((dwStyle
& BUTTON_TYPE
) == BS_AUTORADIOBUTTON
)
183 int state
= (part
== BP_CHECKBOX
)
184 ? cb_states
[ checkState
][ drawState
]
185 : rb_states
[ checkState
][ drawState
];
186 WCHAR
*text
= get_button_text(hwnd
);
188 BOOL created_font
= FALSE
;
190 HRESULT hr
= GetThemeFont(theme
, hDC
, part
, state
, TMT_FONT
, &lf
);
192 font
= CreateFontIndirectW(&lf
);
194 TRACE("Failed to create font\n");
196 TRACE("font = %s\n", debugstr_w(lf
.lfFaceName
));
197 hPrevFont
= SelectObject(hDC
, font
);
201 font
= get_button_font(hwnd
);
202 hPrevFont
= SelectObject(hDC
, font
);
205 if (FAILED(GetThemePartSize(theme
, hDC
, part
, state
, NULL
, TS_DRAW
, &sz
)))
208 GetClientRect(hwnd
, &bgRect
);
209 GetThemeBackgroundContentRect(theme
, hDC
, part
, state
, &bgRect
, &textRect
);
211 if (dtFlags
& DT_SINGLELINE
) /* Center the checkbox / radio button to the text. */
212 bgRect
.top
= bgRect
.top
+ (textRect
.bottom
- textRect
.top
- sz
.cy
) / 2;
214 /* adjust for the check/radio marker */
215 bgRect
.bottom
= bgRect
.top
+ sz
.cy
;
216 bgRect
.right
= bgRect
.left
+ sz
.cx
;
217 textRect
.left
= bgRect
.right
+ 6;
219 if (prfFlag
== 0 || (prfFlag
& PRF_ERASEBKGND
))
221 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
224 DrawThemeBackground(theme
, hDC
, part
, state
, &bgRect
, NULL
);
227 DrawThemeText(theme
, hDC
, part
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
233 focusRect
= textRect
;
235 DrawTextW(hDC
, text
, lstrlenW(text
), &focusRect
, dtFlags
| DT_CALCRECT
);
237 if (focusRect
.right
< textRect
.right
) focusRect
.right
++;
238 focusRect
.bottom
= textRect
.bottom
;
240 DrawFocusRect( hDC
, &focusRect
);
243 HeapFree(GetProcessHeap(), 0, text
);
246 if (created_font
) DeleteObject(font
);
247 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
250 static void GB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
252 static const int states
[] = { GBS_NORMAL
, GBS_DISABLED
, GBS_NORMAL
, GBS_NORMAL
, GBS_NORMAL
};
254 RECT bgRect
, textRect
, contentRect
;
255 int state
= states
[ drawState
];
256 WCHAR
*text
= get_button_text(hwnd
);
258 HFONT font
, hPrevFont
= NULL
;
259 BOOL created_font
= FALSE
;
261 HRESULT hr
= GetThemeFont(theme
, hDC
, BP_GROUPBOX
, state
, TMT_FONT
, &lf
);
263 font
= CreateFontIndirectW(&lf
);
265 TRACE("Failed to create font\n");
267 hPrevFont
= SelectObject(hDC
, font
);
271 font
= get_button_font(hwnd
);
272 hPrevFont
= SelectObject(hDC
, font
);
275 GetClientRect(hwnd
, &bgRect
);
281 GetTextExtentPoint32W(hDC
, text
, lstrlenW(text
), &textExtent
);
282 bgRect
.top
+= (textExtent
.cy
/ 2);
284 textRect
.bottom
= textRect
.top
+ textExtent
.cy
;
285 textRect
.right
= textRect
.left
+ textExtent
.cx
+ 4;
287 ExcludeClipRect(hDC
, textRect
.left
, textRect
.top
, textRect
.right
, textRect
.bottom
);
290 GetThemeBackgroundContentRect(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, &contentRect
);
291 ExcludeClipRect(hDC
, contentRect
.left
, contentRect
.top
, contentRect
.right
, contentRect
.bottom
);
293 if (prfFlag
== 0 || (prfFlag
& PRF_ERASEBKGND
))
295 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_GROUPBOX
, state
))
296 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
299 DrawThemeBackground(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, NULL
);
301 SelectClipRgn(hDC
, NULL
);
305 InflateRect(&textRect
, -2, 0);
306 DrawThemeText(theme
, hDC
, BP_GROUPBOX
, state
, text
, lstrlenW(text
), 0, 0, &textRect
);
307 HeapFree(GetProcessHeap(), 0, text
);
310 if (created_font
) DeleteObject(font
);
311 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
314 static const pfThemedPaint btnThemedPaintFunc
[BUTTON_TYPE
+ 1] =
316 PB_draw
, /* BS_PUSHBUTTON */
317 PB_draw
, /* BS_DEFPUSHBUTTON */
318 CB_draw
, /* BS_CHECKBOX */
319 CB_draw
, /* BS_AUTOCHECKBOX */
320 CB_draw
, /* BS_RADIOBUTTON */
321 CB_draw
, /* BS_3STATE */
322 CB_draw
, /* BS_AUTO3STATE */
323 GB_draw
, /* BS_GROUPBOX */
324 NULL
, /* BS_USERBUTTON */
325 CB_draw
, /* BS_AUTORADIOBUTTON */
326 NULL
, /* Not defined */
327 NULL
, /* BS_OWNERDRAW */
328 NULL
, /* Not defined */
329 NULL
, /* Not defined */
330 NULL
, /* Not defined */
331 NULL
, /* Not defined */
334 BOOL
BUTTON_PaintWithTheme(HTHEME theme
, HWND hwnd
, HDC hParamDC
, LPARAM prfFlag
)
341 ButtonState drawState
;
344 dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
345 type
= dwStyle
& BUTTON_TYPE
;
347 if (type
!= BS_PUSHBUTTON
&& type
!= BS_DEFPUSHBUTTON
&& (dwStyle
& BS_PUSHLIKE
))
348 type
= BS_PUSHBUTTON
;
350 paint
= btnThemedPaintFunc
[type
];
354 if (get_button_image(hwnd
) != 0)
357 dwStyleEx
= GetWindowLongW(hwnd
, GWL_EXSTYLE
);
358 dtFlags
= get_drawtext_flags(dwStyle
, dwStyleEx
);
359 state
= get_button_state(hwnd
);
361 if(IsWindowEnabled(hwnd
))
363 if(state
& BST_PUSHED
) drawState
= STATE_PRESSED
;
364 else if(state
& BST_HOT
) drawState
= STATE_HOT
;
365 else if(state
& BST_FOCUS
) drawState
= STATE_DEFAULTED
;
366 else drawState
= STATE_NORMAL
;
368 else drawState
= STATE_DISABLED
;
370 if (drawState
== STATE_NORMAL
&& type
== BS_DEFPUSHBUTTON
)
372 drawState
= STATE_DEFAULTED
;
375 paint(theme
, hwnd
, hParamDC
, drawState
, dtFlags
, state
& BST_FOCUS
, prfFlag
);