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 #ifdef __REACTOS__ /* r73885 */
38 typedef void (*pfThemedPaint
)(HTHEME theme
, HWND hwnd
, HDC hdc
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
);
40 typedef void (*pfThemedPaint
)(HTHEME theme
, HWND hwnd
, HDC hdc
, ButtonState drawState
, UINT dtFlags
, BOOL focused
);
43 #ifdef __REACTOS__ /* r73885 & r73907 */
44 static inline LONG
get_button_state( HWND hwnd
)
46 return _GetButtonData(hwnd
)->state
;
49 static inline HFONT
get_button_font( HWND hwnd
)
51 return (HFONT
)_GetButtonData(hwnd
)->font
;
54 static inline LONG_PTR
get_button_image(HWND hwnd
)
56 return _GetButtonData(hwnd
)->image
;
59 BOOL
BUTTON_DrawIml(HDC hdc
, BUTTON_IMAGELIST
*pimlData
, RECT
*prc
, BOOL bOnlyCalc
);
60 DWORD
BUTTON_SendCustomDraw(HWND hwnd
, HDC hDC
, DWORD dwDrawStage
, RECT
* prc
);
63 static UINT
get_drawtext_flags(DWORD style
, DWORD ex_style
)
67 if (style
& BS_PUSHLIKE
)
68 style
&= ~BUTTON_TYPE
;
70 if (!(style
& BS_MULTILINE
))
71 flags
|= DT_SINGLELINE
;
73 flags
|= DT_WORDBREAK
;
75 switch (style
& BS_CENTER
)
77 case BS_LEFT
: flags
|= DT_LEFT
; break;
78 case BS_RIGHT
: flags
|= DT_RIGHT
; break;
79 case BS_CENTER
: flags
|= DT_CENTER
; break;
81 flags
|= ((style
& BUTTON_TYPE
) <= BS_DEFPUSHBUTTON
)
82 ? DT_CENTER
: DT_LEFT
;
85 if (ex_style
& WS_EX_RIGHT
)
86 flags
= DT_RIGHT
| (flags
& ~(DT_LEFT
| DT_CENTER
));
88 if ((style
& BUTTON_TYPE
) != BS_GROUPBOX
)
90 switch (style
& BS_VCENTER
)
92 case BS_TOP
: flags
|= DT_TOP
; break;
93 case BS_BOTTOM
: flags
|= DT_BOTTOM
; break;
94 case BS_VCENTER
: /* fall through */
95 default: flags
|= DT_VCENTER
; break;
99 /* GroupBox's text is always single line and is top aligned. */
100 flags
|= DT_SINGLELINE
| DT_TOP
;
105 static inline WCHAR
*get_button_text(HWND hwnd
)
109 text
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
110 if (text
) InternalGetWindowText(hwnd
, text
, len
+ 1);
114 static void PB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
116 static const int states
[] = { PBS_NORMAL
, PBS_DISABLED
, PBS_HOT
, PBS_PRESSED
, PBS_DEFAULTED
};
118 RECT bgRect
, textRect
;
119 HFONT font
= get_button_font(hwnd
);
120 HFONT hPrevFont
= font
? SelectObject(hDC
, font
) : NULL
;
121 int state
= states
[ drawState
];
123 PBUTTON_DATA pdata
= _GetButtonData(hwnd
);
127 GetClientRect(hwnd
, &bgRect
);
128 GetThemeBackgroundContentRect(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, &textRect
);
132 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_PUSHBUTTON
, state
))
133 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
136 parent
= GetParent(hwnd
);
137 if (!parent
) parent
= hwnd
;
138 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
140 cdrf
= BUTTON_SendCustomDraw(hwnd
, hDC
, CDDS_PREERASE
, &bgRect
);
141 if (cdrf
== CDRF_SKIPDEFAULT
)
144 DrawThemeBackground(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, NULL
);
146 if (cdrf
== CDRF_NOTIFYPOSTERASE
)
147 BUTTON_SendCustomDraw(hwnd
, hDC
, CDDS_POSTERASE
, &bgRect
);
149 cdrf
= BUTTON_SendCustomDraw(hwnd
, hDC
, CDDS_PREPAINT
, &bgRect
);
150 if (cdrf
== CDRF_SKIPDEFAULT
)
153 BUTTON_DrawIml(hDC
, &pdata
->imlData
, &textRect
, FALSE
);
155 text
= get_button_text(hwnd
);
158 DrawThemeText(theme
, hDC
, BP_PUSHBUTTON
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
159 HeapFree(GetProcessHeap(), 0, text
);
165 RECT focusRect
= bgRect
;
167 GetThemeMargins(theme
, hDC
, BP_PUSHBUTTON
, state
, TMT_CONTENTMARGINS
, NULL
, &margins
);
169 focusRect
.left
+= margins
.cxLeftWidth
;
170 focusRect
.top
+= margins
.cyTopHeight
;
171 focusRect
.right
-= margins
.cxRightWidth
;
172 focusRect
.bottom
-= margins
.cyBottomHeight
;
174 DrawFocusRect( hDC
, &focusRect
);
177 if (cdrf
== CDRF_NOTIFYPOSTPAINT
)
178 BUTTON_SendCustomDraw(hwnd
, hDC
, CDDS_POSTPAINT
, &bgRect
);
181 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
184 static void CB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
186 static const int cb_states
[3][5] =
188 { CBS_UNCHECKEDNORMAL
, CBS_UNCHECKEDDISABLED
, CBS_UNCHECKEDHOT
, CBS_UNCHECKEDPRESSED
, CBS_UNCHECKEDNORMAL
},
189 { CBS_CHECKEDNORMAL
, CBS_CHECKEDDISABLED
, CBS_CHECKEDHOT
, CBS_CHECKEDPRESSED
, CBS_CHECKEDNORMAL
},
190 { CBS_MIXEDNORMAL
, CBS_MIXEDDISABLED
, CBS_MIXEDHOT
, CBS_MIXEDPRESSED
, CBS_MIXEDNORMAL
}
193 static const int rb_states
[2][5] =
195 { RBS_UNCHECKEDNORMAL
, RBS_UNCHECKEDDISABLED
, RBS_UNCHECKEDHOT
, RBS_UNCHECKEDPRESSED
, RBS_UNCHECKEDNORMAL
},
196 { RBS_CHECKEDNORMAL
, RBS_CHECKEDDISABLED
, RBS_CHECKEDHOT
, RBS_CHECKEDPRESSED
, RBS_CHECKEDNORMAL
}
200 RECT bgRect
, textRect
;
201 HFONT font
, hPrevFont
= NULL
;
202 LRESULT checkState
= get_button_state(hwnd
) & 3;
203 DWORD dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
204 int part
= ((dwStyle
& BUTTON_TYPE
) == BS_RADIOBUTTON
) || ((dwStyle
& BUTTON_TYPE
) == BS_AUTORADIOBUTTON
)
207 int state
= (part
== BP_CHECKBOX
)
208 ? cb_states
[ checkState
][ drawState
]
209 : rb_states
[ checkState
][ drawState
];
212 BOOL created_font
= FALSE
;
217 HRESULT hr
= GetThemeFont(theme
, hDC
, part
, state
, TMT_FONT
, &lf
);
219 font
= CreateFontIndirectW(&lf
);
221 TRACE("Failed to create font\n");
223 TRACE("font = %s\n", debugstr_w(lf
.lfFaceName
));
224 hPrevFont
= SelectObject(hDC
, font
);
228 font
= get_button_font(hwnd
);
229 hPrevFont
= SelectObject(hDC
, font
);
232 if (FAILED(GetThemePartSize(theme
, hDC
, part
, state
, NULL
, TS_DRAW
, &sz
)))
235 GetClientRect(hwnd
, &bgRect
);
239 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
242 parent
= GetParent(hwnd
);
243 if (!parent
) parent
= hwnd
;
244 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
245 (WPARAM
)hDC
, (LPARAM
)hwnd
);
246 if (!hBrush
) /* did the app forget to call defwindowproc ? */
247 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
248 (WPARAM
)hDC
, (LPARAM
)hwnd
);
249 FillRect( hDC
, &bgRect
, hBrush
);
251 cdrf
= BUTTON_SendCustomDraw(hwnd
, hDC
, CDDS_PREERASE
, &bgRect
);
252 if (cdrf
== CDRF_SKIPDEFAULT
)
255 GetThemeBackgroundContentRect(theme
, hDC
, part
, state
, &bgRect
, &textRect
);
257 if (dtFlags
& DT_SINGLELINE
) /* Center the checkbox / radio button to the text. */
258 bgRect
.top
= bgRect
.top
+ (textRect
.bottom
- textRect
.top
- sz
.cy
) / 2;
260 /* adjust for the check/radio marker */
261 bgRect
.bottom
= bgRect
.top
+ sz
.cy
;
262 bgRect
.right
= bgRect
.left
+ sz
.cx
;
263 textRect
.left
= bgRect
.right
+ 6;
265 DrawThemeBackground(theme
, hDC
, part
, state
, &bgRect
, NULL
);
267 if (cdrf
== CDRF_NOTIFYPOSTERASE
)
268 BUTTON_SendCustomDraw(hwnd
, hDC
, CDDS_POSTERASE
, &bgRect
);
270 cdrf
= BUTTON_SendCustomDraw(hwnd
, hDC
, CDDS_PREPAINT
, &bgRect
);
271 if (cdrf
== CDRF_SKIPDEFAULT
)
274 text
= get_button_text(hwnd
);
277 DrawThemeText(theme
, hDC
, part
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
283 focusRect
= textRect
;
285 DrawTextW(hDC
, text
, lstrlenW(text
), &focusRect
, dtFlags
| DT_CALCRECT
);
287 if (focusRect
.right
< textRect
.right
) focusRect
.right
++;
288 focusRect
.bottom
= textRect
.bottom
;
290 DrawFocusRect( hDC
, &focusRect
);
293 HeapFree(GetProcessHeap(), 0, text
);
296 if (cdrf
== CDRF_NOTIFYPOSTPAINT
)
297 BUTTON_SendCustomDraw(hwnd
, hDC
, CDDS_POSTPAINT
, &bgRect
);
300 if (created_font
) DeleteObject(font
);
301 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
304 #ifdef __REACTOS__ /* r73885 */
305 static void GB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
307 static void GB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
)
310 static const int states
[] = { GBS_NORMAL
, GBS_DISABLED
, GBS_NORMAL
, GBS_NORMAL
, GBS_NORMAL
};
312 RECT bgRect
, textRect
, contentRect
;
313 int state
= states
[ drawState
];
314 WCHAR
*text
= get_button_text(hwnd
);
316 HFONT font
, hPrevFont
= NULL
;
317 BOOL created_font
= FALSE
;
318 #ifdef __REACTOS__ /* r74406 */
324 HRESULT hr
= GetThemeFont(theme
, hDC
, BP_GROUPBOX
, state
, TMT_FONT
, &lf
);
326 font
= CreateFontIndirectW(&lf
);
328 TRACE("Failed to create font\n");
330 hPrevFont
= SelectObject(hDC
, font
);
334 #ifdef __REACTOS__ /* r73885 */
335 font
= get_button_font(hwnd
);
337 font
= (HFONT
)SendMessageW(hwnd
, WM_GETFONT
, 0, 0);
339 hPrevFont
= SelectObject(hDC
, font
);
342 GetClientRect(hwnd
, &bgRect
);
348 GetTextExtentPoint32W(hDC
, text
, lstrlenW(text
), &textExtent
);
349 bgRect
.top
+= (textExtent
.cy
/ 2);
351 textRect
.bottom
= textRect
.top
+ textExtent
.cy
;
352 textRect
.right
= textRect
.left
+ textExtent
.cx
+ 4;
354 ExcludeClipRect(hDC
, textRect
.left
, textRect
.top
, textRect
.right
, textRect
.bottom
);
357 GetThemeBackgroundContentRect(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, &contentRect
);
358 ExcludeClipRect(hDC
, contentRect
.left
, contentRect
.top
, contentRect
.right
, contentRect
.bottom
);
360 #ifdef __REACTOS__ /* r73885 & r74149 */
363 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_GROUPBOX
, state
))
364 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
367 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_GROUPBOX
, state
))
368 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
371 #ifdef __REACTOS__ /* r74406 */
372 parent
= GetParent(hwnd
);
373 if (!parent
) parent
= hwnd
;
374 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
375 (WPARAM
)hDC
, (LPARAM
)hwnd
);
376 if (!hBrush
) /* did the app forget to call defwindowproc ? */
377 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
378 (WPARAM
)hDC
, (LPARAM
)hwnd
);
379 GetClientRect(hwnd
, &clientRect
);
380 FillRect( hDC
, &clientRect
, hBrush
);
383 DrawThemeBackground(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, NULL
);
385 SelectClipRgn(hDC
, NULL
);
389 InflateRect(&textRect
, -2, 0);
390 DrawThemeText(theme
, hDC
, BP_GROUPBOX
, state
, text
, lstrlenW(text
), 0, 0, &textRect
);
391 HeapFree(GetProcessHeap(), 0, text
);
394 if (created_font
) DeleteObject(font
);
395 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
398 static const pfThemedPaint btnThemedPaintFunc
[BUTTON_TYPE
+ 1] =
400 PB_draw
, /* BS_PUSHBUTTON */
401 PB_draw
, /* BS_DEFPUSHBUTTON */
402 CB_draw
, /* BS_CHECKBOX */
403 CB_draw
, /* BS_AUTOCHECKBOX */
404 CB_draw
, /* BS_RADIOBUTTON */
405 CB_draw
, /* BS_3STATE */
406 CB_draw
, /* BS_AUTO3STATE */
407 GB_draw
, /* BS_GROUPBOX */
408 NULL
, /* BS_USERBUTTON */
409 CB_draw
, /* BS_AUTORADIOBUTTON */
410 NULL
, /* Not defined */
411 NULL
, /* BS_OWNERDRAW */
412 NULL
, /* Not defined */
413 NULL
, /* Not defined */
414 NULL
, /* Not defined */
415 NULL
, /* Not defined */
418 #ifdef __REACTOS__ /* r73873 */
419 BOOL
BUTTON_PaintWithTheme(HTHEME theme
, HWND hwnd
, HDC hParamDC
, LPARAM prfFlag
)
421 static BOOL
BUTTON_Paint(HTHEME theme
, HWND hwnd
, HDC hParamDC
)
424 #ifdef __REACTOS__ /* r73873, r73897 and r74120 */
433 DWORD dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
434 DWORD dwStyleEx
= GetWindowLongW(hwnd
, GWL_EXSTYLE
);
435 UINT dtFlags
= get_drawtext_flags(dwStyle
, dwStyleEx
);
436 int state
= (int)SendMessageW(hwnd
, BM_GETSTATE
, 0, 0);
438 ButtonState drawState
;
439 #ifdef __REACTOS__ /* r73873, r73897, r73907 and r74120 */
442 dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
443 type
= dwStyle
& BUTTON_TYPE
;
445 if (type
!= BS_PUSHBUTTON
&& type
!= BS_DEFPUSHBUTTON
&& (dwStyle
& BS_PUSHLIKE
))
446 type
= BS_PUSHBUTTON
;
448 paint
= btnThemedPaintFunc
[type
];
452 if (get_button_image(hwnd
) != 0)
455 dwStyleEx
= GetWindowLongW(hwnd
, GWL_EXSTYLE
);
456 dtFlags
= get_drawtext_flags(dwStyle
, dwStyleEx
);
457 state
= get_button_state(hwnd
);
459 pfThemedPaint paint
= btnThemedPaintFunc
[ dwStyle
& BUTTON_TYPE
];
462 if(IsWindowEnabled(hwnd
))
464 if(state
& BST_PUSHED
)
465 drawState
= STATE_PRESSED
;
466 else if ((dwStyle
& BS_PUSHLIKE
) && (state
& (BST_CHECKED
|BST_INDETERMINATE
)))
467 drawState
= STATE_PRESSED
;
468 else if(state
& BST_HOT
)
469 drawState
= STATE_HOT
;
470 else if(state
& BST_FOCUS
)
471 drawState
= STATE_DEFAULTED
;
473 drawState
= STATE_NORMAL
;
476 drawState
= STATE_DISABLED
;
478 #ifndef __REACTOS__ /* r73873 */
479 hDC
= hParamDC
? hParamDC
: BeginPaint(hwnd
, &ps
);
480 if (paint
) paint(theme
, hwnd
, hDC
, drawState
, dtFlags
, state
& BST_FOCUS
);
481 if (!hParamDC
) EndPaint(hwnd
, &ps
);
484 #ifdef __REACTOS__ /* r74074 & r74120 */
485 if (drawState
== STATE_NORMAL
&& type
== BS_DEFPUSHBUTTON
)
487 drawState
= STATE_DEFAULTED
;
491 paint(theme
, hwnd
, hParamDC
, drawState
, dtFlags
, state
& BST_FOCUS
, prfFlag
);
495 #ifndef __REACTOS__ /* r73873 */
496 /**********************************************************************
497 * The button control subclass window proc.
499 LRESULT CALLBACK
THEMING_ButtonSubclassProc(HWND hwnd
, UINT msg
,
500 WPARAM wParam
, LPARAM lParam
,
503 const WCHAR
* themeClass
= WC_BUTTONW
;
510 result
= THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
511 OpenThemeData(hwnd
, themeClass
);
515 theme
= GetWindowTheme(hwnd
);
516 CloseThemeData (theme
);
517 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
519 case WM_THEMECHANGED
:
520 theme
= GetWindowTheme(hwnd
);
521 CloseThemeData (theme
);
522 OpenThemeData(hwnd
, themeClass
);
525 case WM_SYSCOLORCHANGE
:
526 theme
= GetWindowTheme(hwnd
);
527 if (!theme
) return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
528 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
529 * which will do the repaint. */
533 theme
= GetWindowTheme(hwnd
);
534 if (theme
&& BUTTON_Paint(theme
, hwnd
, (HDC
)wParam
))
537 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
540 theme
= GetWindowTheme(hwnd
);
542 RedrawWindow(hwnd
, NULL
, NULL
, RDW_FRAME
| RDW_INVALIDATE
| RDW_UPDATENOW
);
545 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
549 TRACKMOUSEEVENT mouse_event
;
550 mouse_event
.cbSize
= sizeof(TRACKMOUSEEVENT
);
551 mouse_event
.dwFlags
= TME_QUERY
;
552 if(!TrackMouseEvent(&mouse_event
) || !(mouse_event
.dwFlags
&(TME_HOVER
|TME_LEAVE
)))
554 mouse_event
.dwFlags
= TME_HOVER
|TME_LEAVE
;
555 mouse_event
.hwndTrack
= hwnd
;
556 mouse_event
.dwHoverTime
= 1;
557 TrackMouseEvent(&mouse_event
);
564 int state
= (int)SendMessageW(hwnd
, BM_GETSTATE
, 0, 0);
565 SetWindowLongW(hwnd
, 0, state
|BST_HOT
);
566 InvalidateRect(hwnd
, NULL
, FALSE
);
572 int state
= (int)SendMessageW(hwnd
, BM_GETSTATE
, 0, 0);
573 SetWindowLongW(hwnd
, 0, state
&(~BST_HOT
));
574 InvalidateRect(hwnd
, NULL
, FALSE
);
580 theme
= GetWindowTheme(hwnd
);
582 InvalidateRect(hwnd
, NULL
, FALSE
);
584 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
588 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
592 #endif /* !__REACTOS__ */