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
;
60 static UINT
get_drawtext_flags(DWORD style
, DWORD ex_style
)
64 if (style
& BS_PUSHLIKE
)
65 style
&= ~BUTTON_TYPE
;
67 if (!(style
& BS_MULTILINE
))
68 flags
|= DT_SINGLELINE
;
70 flags
|= DT_WORDBREAK
;
72 switch (style
& BS_CENTER
)
74 case BS_LEFT
: flags
|= DT_LEFT
; break;
75 case BS_RIGHT
: flags
|= DT_RIGHT
; break;
76 case BS_CENTER
: flags
|= DT_CENTER
; break;
78 flags
|= ((style
& BUTTON_TYPE
) <= BS_DEFPUSHBUTTON
)
79 ? DT_CENTER
: DT_LEFT
;
82 if (ex_style
& WS_EX_RIGHT
)
83 flags
= DT_RIGHT
| (flags
& ~(DT_LEFT
| DT_CENTER
));
85 if ((style
& BUTTON_TYPE
) != BS_GROUPBOX
)
87 switch (style
& BS_VCENTER
)
89 case BS_TOP
: flags
|= DT_TOP
; break;
90 case BS_BOTTOM
: flags
|= DT_BOTTOM
; break;
91 case BS_VCENTER
: /* fall through */
92 default: flags
|= DT_VCENTER
; break;
96 /* GroupBox's text is always single line and is top aligned. */
97 flags
|= DT_SINGLELINE
| DT_TOP
;
102 static inline WCHAR
*get_button_text(HWND hwnd
)
106 text
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
107 if (text
) InternalGetWindowText(hwnd
, text
, len
+ 1);
111 #ifdef __REACTOS__ /* r73885 */
112 static void PB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
114 static void PB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
)
117 static const int states
[] = { PBS_NORMAL
, PBS_DISABLED
, PBS_HOT
, PBS_PRESSED
, PBS_DEFAULTED
};
119 RECT bgRect
, textRect
;
120 #ifdef __REACTOS__ /* r73885 */
121 HFONT font
= get_button_font(hwnd
);
123 HFONT font
= (HFONT
)SendMessageW(hwnd
, WM_GETFONT
, 0, 0);
125 HFONT hPrevFont
= font
? SelectObject(hDC
, font
) : NULL
;
126 int state
= states
[ drawState
];
127 WCHAR
*text
= get_button_text(hwnd
);
128 #ifdef __REACTOS__ /* r74012 & r74406 */
129 PBUTTON_DATA pdata
= _GetButtonData(hwnd
);
135 GetClientRect(hwnd
, &bgRect
);
136 GetThemeBackgroundContentRect(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, &textRect
);
138 #ifdef __REACTOS__ /* r73885 & r74149 */
141 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_PUSHBUTTON
, state
))
142 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
145 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_PUSHBUTTON
, state
))
146 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
149 #ifdef __REACTOS__ /* r74406 */
150 parent
= GetParent(hwnd
);
151 if (!parent
) parent
= hwnd
;
152 hBrush
= (HBRUSH
)SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
153 FillRect( hDC
, &bgRect
, hBrush
);
156 DrawThemeBackground(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, NULL
);
158 #ifdef __REACTOS__ /* r74012 */
159 if (pdata
->imlData
.himl
&& ImageList_GetIconSize(pdata
->imlData
.himl
, &ImageSize
.cx
, &ImageSize
.cy
))
161 int left
= textRect
.left
+ pdata
->imlData
.margin
.left
;
162 int top
= textRect
.top
+ (textRect
.bottom
- textRect
.top
- ImageSize
.cy
) / 2;
163 textRect
.left
+= pdata
->imlData
.margin
.left
+ pdata
->imlData
.margin
.right
+ ImageSize
.cy
;
164 ImageList_Draw(pdata
->imlData
.himl
, 0, hDC
, left
, top
, 0);
170 DrawThemeText(theme
, hDC
, BP_PUSHBUTTON
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
171 HeapFree(GetProcessHeap(), 0, text
);
177 RECT focusRect
= bgRect
;
179 GetThemeMargins(theme
, hDC
, BP_PUSHBUTTON
, state
, TMT_CONTENTMARGINS
, NULL
, &margins
);
181 focusRect
.left
+= margins
.cxLeftWidth
;
182 focusRect
.top
+= margins
.cyTopHeight
;
183 focusRect
.right
-= margins
.cxRightWidth
;
184 focusRect
.bottom
-= margins
.cyBottomHeight
;
186 DrawFocusRect( hDC
, &focusRect
);
189 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
192 #ifdef __REACTOS__ /* r73885 */
193 static void CB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
195 static void CB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
)
198 static const int cb_states
[3][5] =
200 { CBS_UNCHECKEDNORMAL
, CBS_UNCHECKEDDISABLED
, CBS_UNCHECKEDHOT
, CBS_UNCHECKEDPRESSED
, CBS_UNCHECKEDNORMAL
},
201 { CBS_CHECKEDNORMAL
, CBS_CHECKEDDISABLED
, CBS_CHECKEDHOT
, CBS_CHECKEDPRESSED
, CBS_CHECKEDNORMAL
},
202 { CBS_MIXEDNORMAL
, CBS_MIXEDDISABLED
, CBS_MIXEDHOT
, CBS_MIXEDPRESSED
, CBS_MIXEDNORMAL
}
205 static const int rb_states
[2][5] =
207 { RBS_UNCHECKEDNORMAL
, RBS_UNCHECKEDDISABLED
, RBS_UNCHECKEDHOT
, RBS_UNCHECKEDPRESSED
, RBS_UNCHECKEDNORMAL
},
208 { RBS_CHECKEDNORMAL
, RBS_CHECKEDDISABLED
, RBS_CHECKEDHOT
, RBS_CHECKEDPRESSED
, RBS_CHECKEDNORMAL
}
212 RECT bgRect
, textRect
;
213 HFONT font
, hPrevFont
= NULL
;
214 #ifdef __REACTOS__ /* r73885 */
215 LRESULT checkState
= get_button_state(hwnd
) & 3;
217 LRESULT checkState
= SendMessageW(hwnd
, BM_GETCHECK
, 0, 0);
219 DWORD dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
220 int part
= ((dwStyle
& BUTTON_TYPE
) == BS_RADIOBUTTON
) || ((dwStyle
& BUTTON_TYPE
) == BS_AUTORADIOBUTTON
)
223 int state
= (part
== BP_CHECKBOX
)
224 ? cb_states
[ checkState
][ drawState
]
225 : rb_states
[ checkState
][ drawState
];
226 WCHAR
*text
= get_button_text(hwnd
);
228 BOOL created_font
= FALSE
;
229 #ifdef __REACTOS__ /* r74406 */
234 HRESULT hr
= GetThemeFont(theme
, hDC
, part
, state
, TMT_FONT
, &lf
);
236 font
= CreateFontIndirectW(&lf
);
238 TRACE("Failed to create font\n");
240 TRACE("font = %s\n", debugstr_w(lf
.lfFaceName
));
241 hPrevFont
= SelectObject(hDC
, font
);
245 #ifdef __REACTOS__ /* r73885 */
246 font
= get_button_font(hwnd
);
248 font
= (HFONT
)SendMessageW(hwnd
, WM_GETFONT
, 0, 0);
250 hPrevFont
= SelectObject(hDC
, font
);
253 if (FAILED(GetThemePartSize(theme
, hDC
, part
, state
, NULL
, TS_DRAW
, &sz
)))
256 GetClientRect(hwnd
, &bgRect
);
258 #ifdef __REACTOS__ /* r73885, r74149 and r74406 */
261 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
264 parent
= GetParent(hwnd
);
265 if (!parent
) parent
= hwnd
;
266 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
267 (WPARAM
)hDC
, (LPARAM
)hwnd
);
268 if (!hBrush
) /* did the app forget to call defwindowproc ? */
269 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
270 (WPARAM
)hDC
, (LPARAM
)hwnd
);
271 FillRect( hDC
, &bgRect
, hBrush
);
274 GetThemeBackgroundContentRect(theme
, hDC
, part
, state
, &bgRect
, &textRect
);
276 if (dtFlags
& DT_SINGLELINE
) /* Center the checkbox / radio button to the text. */
277 bgRect
.top
= bgRect
.top
+ (textRect
.bottom
- textRect
.top
- sz
.cy
) / 2;
279 /* adjust for the check/radio marker */
280 bgRect
.bottom
= bgRect
.top
+ sz
.cy
;
281 bgRect
.right
= bgRect
.left
+ sz
.cx
;
282 textRect
.left
= bgRect
.right
+ 6;
284 #ifndef __REACTOS__ /* r74406 */
285 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
288 DrawThemeBackground(theme
, hDC
, part
, state
, &bgRect
, NULL
);
291 DrawThemeText(theme
, hDC
, part
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
297 focusRect
= textRect
;
299 DrawTextW(hDC
, text
, lstrlenW(text
), &focusRect
, dtFlags
| DT_CALCRECT
);
301 if (focusRect
.right
< textRect
.right
) focusRect
.right
++;
302 focusRect
.bottom
= textRect
.bottom
;
304 DrawFocusRect( hDC
, &focusRect
);
307 HeapFree(GetProcessHeap(), 0, text
);
310 if (created_font
) DeleteObject(font
);
311 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
314 #ifdef __REACTOS__ /* r73885 */
315 static void GB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
, LPARAM prfFlag
)
317 static void GB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
, BOOL focused
)
320 static const int states
[] = { GBS_NORMAL
, GBS_DISABLED
, GBS_NORMAL
, GBS_NORMAL
, GBS_NORMAL
};
322 RECT bgRect
, textRect
, contentRect
;
323 int state
= states
[ drawState
];
324 WCHAR
*text
= get_button_text(hwnd
);
326 HFONT font
, hPrevFont
= NULL
;
327 BOOL created_font
= FALSE
;
328 #ifdef __REACTOS__ /* r74406 */
334 HRESULT hr
= GetThemeFont(theme
, hDC
, BP_GROUPBOX
, state
, TMT_FONT
, &lf
);
336 font
= CreateFontIndirectW(&lf
);
338 TRACE("Failed to create font\n");
340 hPrevFont
= SelectObject(hDC
, font
);
344 #ifdef __REACTOS__ /* r73885 */
345 font
= get_button_font(hwnd
);
347 font
= (HFONT
)SendMessageW(hwnd
, WM_GETFONT
, 0, 0);
349 hPrevFont
= SelectObject(hDC
, font
);
352 GetClientRect(hwnd
, &bgRect
);
358 GetTextExtentPoint32W(hDC
, text
, lstrlenW(text
), &textExtent
);
359 bgRect
.top
+= (textExtent
.cy
/ 2);
361 textRect
.bottom
= textRect
.top
+ textExtent
.cy
;
362 textRect
.right
= textRect
.left
+ textExtent
.cx
+ 4;
364 ExcludeClipRect(hDC
, textRect
.left
, textRect
.top
, textRect
.right
, textRect
.bottom
);
367 GetThemeBackgroundContentRect(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, &contentRect
);
368 ExcludeClipRect(hDC
, contentRect
.left
, contentRect
.top
, contentRect
.right
, contentRect
.bottom
);
370 #ifdef __REACTOS__ /* r73885 & r74149 */
373 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_GROUPBOX
, state
))
374 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
377 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_GROUPBOX
, state
))
378 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
381 #ifdef __REACTOS__ /* r74406 */
382 parent
= GetParent(hwnd
);
383 if (!parent
) parent
= hwnd
;
384 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
385 (WPARAM
)hDC
, (LPARAM
)hwnd
);
386 if (!hBrush
) /* did the app forget to call defwindowproc ? */
387 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
388 (WPARAM
)hDC
, (LPARAM
)hwnd
);
389 GetClientRect(hwnd
, &clientRect
);
390 FillRect( hDC
, &clientRect
, hBrush
);
393 DrawThemeBackground(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, NULL
);
395 SelectClipRgn(hDC
, NULL
);
399 InflateRect(&textRect
, -2, 0);
400 DrawThemeText(theme
, hDC
, BP_GROUPBOX
, state
, text
, lstrlenW(text
), 0, 0, &textRect
);
401 HeapFree(GetProcessHeap(), 0, text
);
404 if (created_font
) DeleteObject(font
);
405 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
408 static const pfThemedPaint btnThemedPaintFunc
[BUTTON_TYPE
+ 1] =
410 PB_draw
, /* BS_PUSHBUTTON */
411 PB_draw
, /* BS_DEFPUSHBUTTON */
412 CB_draw
, /* BS_CHECKBOX */
413 CB_draw
, /* BS_AUTOCHECKBOX */
414 CB_draw
, /* BS_RADIOBUTTON */
415 CB_draw
, /* BS_3STATE */
416 CB_draw
, /* BS_AUTO3STATE */
417 GB_draw
, /* BS_GROUPBOX */
418 NULL
, /* BS_USERBUTTON */
419 CB_draw
, /* BS_AUTORADIOBUTTON */
420 NULL
, /* Not defined */
421 NULL
, /* BS_OWNERDRAW */
422 NULL
, /* Not defined */
423 NULL
, /* Not defined */
424 NULL
, /* Not defined */
425 NULL
, /* Not defined */
428 #ifdef __REACTOS__ /* r73873 */
429 BOOL
BUTTON_PaintWithTheme(HTHEME theme
, HWND hwnd
, HDC hParamDC
, LPARAM prfFlag
)
431 static BOOL
BUTTON_Paint(HTHEME theme
, HWND hwnd
, HDC hParamDC
)
434 #ifdef __REACTOS__ /* r73873, r73897 and r74120 */
443 DWORD dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
444 DWORD dwStyleEx
= GetWindowLongW(hwnd
, GWL_EXSTYLE
);
445 UINT dtFlags
= get_drawtext_flags(dwStyle
, dwStyleEx
);
446 int state
= (int)SendMessageW(hwnd
, BM_GETSTATE
, 0, 0);
448 ButtonState drawState
;
449 #ifdef __REACTOS__ /* r73873, r73897, r73907 and r74120 */
452 dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
453 type
= dwStyle
& BUTTON_TYPE
;
455 if (type
!= BS_PUSHBUTTON
&& type
!= BS_DEFPUSHBUTTON
&& (dwStyle
& BS_PUSHLIKE
))
456 type
= BS_PUSHBUTTON
;
458 paint
= btnThemedPaintFunc
[type
];
462 if (get_button_image(hwnd
) != 0)
465 dwStyleEx
= GetWindowLongW(hwnd
, GWL_EXSTYLE
);
466 dtFlags
= get_drawtext_flags(dwStyle
, dwStyleEx
);
467 state
= get_button_state(hwnd
);
469 pfThemedPaint paint
= btnThemedPaintFunc
[ dwStyle
& BUTTON_TYPE
];
472 if(IsWindowEnabled(hwnd
))
474 if(state
& BST_PUSHED
)
475 drawState
= STATE_PRESSED
;
476 else if ((dwStyle
& BS_PUSHLIKE
) && (state
& (BST_CHECKED
|BST_INDETERMINATE
)))
477 drawState
= STATE_PRESSED
;
478 else if(state
& BST_HOT
)
479 drawState
= STATE_HOT
;
480 else if(state
& BST_FOCUS
)
481 drawState
= STATE_DEFAULTED
;
483 drawState
= STATE_NORMAL
;
486 drawState
= STATE_DISABLED
;
488 #ifndef __REACTOS__ /* r73873 */
489 hDC
= hParamDC
? hParamDC
: BeginPaint(hwnd
, &ps
);
490 if (paint
) paint(theme
, hwnd
, hDC
, drawState
, dtFlags
, state
& BST_FOCUS
);
491 if (!hParamDC
) EndPaint(hwnd
, &ps
);
494 #ifdef __REACTOS__ /* r74074 & r74120 */
495 if (drawState
== STATE_NORMAL
&& type
== BS_DEFPUSHBUTTON
)
497 drawState
= STATE_DEFAULTED
;
501 paint(theme
, hwnd
, hParamDC
, drawState
, dtFlags
, state
& BST_FOCUS
, prfFlag
);
505 #ifndef __REACTOS__ /* r73873 */
506 /**********************************************************************
507 * The button control subclass window proc.
509 LRESULT CALLBACK
THEMING_ButtonSubclassProc(HWND hwnd
, UINT msg
,
510 WPARAM wParam
, LPARAM lParam
,
513 const WCHAR
* themeClass
= WC_BUTTONW
;
520 result
= THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
521 OpenThemeData(hwnd
, themeClass
);
525 theme
= GetWindowTheme(hwnd
);
526 CloseThemeData (theme
);
527 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
529 case WM_THEMECHANGED
:
530 theme
= GetWindowTheme(hwnd
);
531 CloseThemeData (theme
);
532 OpenThemeData(hwnd
, themeClass
);
535 case WM_SYSCOLORCHANGE
:
536 theme
= GetWindowTheme(hwnd
);
537 if (!theme
) return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
538 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
539 * which will do the repaint. */
543 theme
= GetWindowTheme(hwnd
);
544 if (theme
&& BUTTON_Paint(theme
, hwnd
, (HDC
)wParam
))
547 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
550 theme
= GetWindowTheme(hwnd
);
552 RedrawWindow(hwnd
, NULL
, NULL
, RDW_FRAME
| RDW_INVALIDATE
| RDW_UPDATENOW
);
555 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
559 TRACKMOUSEEVENT mouse_event
;
560 mouse_event
.cbSize
= sizeof(TRACKMOUSEEVENT
);
561 mouse_event
.dwFlags
= TME_QUERY
;
562 if(!TrackMouseEvent(&mouse_event
) || !(mouse_event
.dwFlags
&(TME_HOVER
|TME_LEAVE
)))
564 mouse_event
.dwFlags
= TME_HOVER
|TME_LEAVE
;
565 mouse_event
.hwndTrack
= hwnd
;
566 mouse_event
.dwHoverTime
= 1;
567 TrackMouseEvent(&mouse_event
);
574 int state
= (int)SendMessageW(hwnd
, BM_GETSTATE
, 0, 0);
575 SetWindowLongW(hwnd
, 0, state
|BST_HOT
);
576 InvalidateRect(hwnd
, NULL
, FALSE
);
582 int state
= (int)SendMessageW(hwnd
, BM_GETSTATE
, 0, 0);
583 SetWindowLongW(hwnd
, 0, state
&(~BST_HOT
));
584 InvalidateRect(hwnd
, NULL
, FALSE
);
590 theme
= GetWindowTheme(hwnd
);
592 InvalidateRect(hwnd
, NULL
, FALSE
);
594 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
598 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
602 #endif /* !__REACTOS__ */