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
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(themingbutton
);
37 #define BUTTON_TYPE 0x0f /* bit mask for the available button types */
39 /* These are indices into a states array to determine the theme state for a given theme part. */
49 typedef void (*pfThemedPaint
)(HTHEME theme
, HWND hwnd
, HDC hdc
, ButtonState drawState
, UINT dtFlags
);
51 static UINT
get_drawtext_flags(DWORD style
, DWORD ex_style
)
55 if (style
& BS_PUSHLIKE
)
56 style
&= ~BUTTON_TYPE
;
58 if (!(style
& BS_MULTILINE
))
59 flags
|= DT_SINGLELINE
;
61 flags
|= DT_WORDBREAK
;
63 switch (style
& BS_CENTER
)
65 case BS_LEFT
: flags
|= DT_LEFT
; break;
66 case BS_RIGHT
: flags
|= DT_RIGHT
; break;
67 case BS_CENTER
: flags
|= DT_CENTER
; break;
69 flags
|= ((style
& BUTTON_TYPE
) <= BS_DEFPUSHBUTTON
)
70 ? DT_CENTER
: DT_LEFT
;
73 if (ex_style
& WS_EX_RIGHT
)
74 flags
= DT_RIGHT
| (flags
& ~(DT_LEFT
| DT_CENTER
));
76 if ((style
& BUTTON_TYPE
) != BS_GROUPBOX
)
78 switch (style
& BS_VCENTER
)
80 case BS_TOP
: flags
|= DT_TOP
; break;
81 case BS_BOTTOM
: flags
|= DT_BOTTOM
; break;
82 case BS_VCENTER
: /* fall through */
83 default: flags
|= DT_VCENTER
; break;
87 /* GroupBox's text is always single line and is top aligned. */
88 flags
|= DT_SINGLELINE
| DT_TOP
;
93 static inline WCHAR
*get_button_text(HWND hwnd
)
97 text
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
98 if (text
) InternalGetWindowText(hwnd
, text
, len
+ 1);
102 static void PB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
)
104 static const int states
[] = { PBS_NORMAL
, PBS_DISABLED
, PBS_HOT
, PBS_PRESSED
, PBS_DEFAULTED
};
106 RECT bgRect
, textRect
;
107 HFONT font
= (HFONT
)SendMessageW(hwnd
, WM_GETFONT
, 0, 0);
108 HFONT hPrevFont
= font
? SelectObject(hDC
, font
) : NULL
;
109 int state
= states
[ drawState
];
110 WCHAR
*text
= get_button_text(hwnd
);
112 GetClientRect(hwnd
, &bgRect
);
113 GetThemeBackgroundContentRect(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, &textRect
);
115 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_PUSHBUTTON
, state
))
116 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
117 DrawThemeBackground(theme
, hDC
, BP_PUSHBUTTON
, state
, &bgRect
, NULL
);
120 DrawThemeText(theme
, hDC
, BP_PUSHBUTTON
, state
, text
, lstrlenW(text
), dtFlags
, 0, &textRect
);
121 HeapFree(GetProcessHeap(), 0, text
);
124 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
127 static void GB_draw(HTHEME theme
, HWND hwnd
, HDC hDC
, ButtonState drawState
, UINT dtFlags
)
129 static const int states
[] = { GBS_NORMAL
, GBS_DISABLED
, GBS_NORMAL
, GBS_NORMAL
, GBS_NORMAL
};
131 RECT bgRect
, textRect
;
132 HFONT font
= (HFONT
)SendMessageW(hwnd
, WM_GETFONT
, 0, 0);
133 HFONT hPrevFont
= font
? SelectObject(hDC
, font
) : NULL
;
134 int state
= states
[ drawState
];
135 WCHAR
*text
= get_button_text(hwnd
);
137 GetClientRect(hwnd
, &bgRect
);
143 GetTextExtentPoint32W(hDC
, text
, lstrlenW(text
), &textExtent
);
144 bgRect
.top
+= (textExtent
.cy
/ 2);
146 textRect
.bottom
= textRect
.top
+ textExtent
.cy
;
147 textRect
.right
= textRect
.left
+ textExtent
.cx
+ 4;
150 ExcludeClipRect(hDC
, textRect
.left
, textRect
.top
, textRect
.right
, textRect
.bottom
);
152 if (IsThemeBackgroundPartiallyTransparent(theme
, BP_GROUPBOX
, state
))
153 DrawThemeParentBackground(hwnd
, hDC
, NULL
);
154 DrawThemeBackground(theme
, hDC
, BP_GROUPBOX
, state
, &bgRect
, NULL
);
156 SelectClipRgn(hDC
, NULL
);
162 DrawThemeText(theme
, hDC
, BP_GROUPBOX
, state
, text
, lstrlenW(text
), 0, 0, &textRect
);
163 HeapFree(GetProcessHeap(), 0, text
);
166 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
169 static const pfThemedPaint btnThemedPaintFunc
[BUTTON_TYPE
+ 1] =
171 PB_draw
, /* BS_PUSHBUTTON */
172 PB_draw
, /* BS_DEFPUSHBUTTON */
173 NULL
, /* BS_CHECKBOX */
174 NULL
, /* BS_AUTOCHECKBOX */
175 NULL
, /* BS_RADIOBUTTON */
176 NULL
, /* BS_3STATE */
177 NULL
, /* BS_AUTO3STATE */
178 GB_draw
, /* BS_GROUPBOX */
179 NULL
, /* BS_USERBUTTON */
180 NULL
, /* BS_AUTORADIOBUTTON */
181 NULL
, /* Not defined */
182 NULL
, /* BS_OWNERDRAW */
183 NULL
, /* Not defined */
184 NULL
, /* Not defined */
185 NULL
, /* Not defined */
186 NULL
, /* Not defined */
189 static BOOL
BUTTON_Paint(HTHEME theme
, HWND hwnd
, HDC hParamDC
)
193 DWORD dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
194 DWORD dwStyleEx
= GetWindowLongW(hwnd
, GWL_EXSTYLE
);
195 UINT dtFlags
= get_drawtext_flags(dwStyle
, dwStyleEx
);
196 ButtonState drawState
= IsWindowEnabled(hwnd
) ? STATE_NORMAL
: STATE_DISABLED
;
197 pfThemedPaint paint
= btnThemedPaintFunc
[ dwStyle
& BUTTON_TYPE
];
201 hDC
= hParamDC
? hParamDC
: BeginPaint(hwnd
, &ps
);
202 paint(theme
, hwnd
, hDC
, drawState
, dtFlags
);
203 if (!hParamDC
) EndPaint(hwnd
, &ps
);
207 return FALSE
; /* Delegate drawing to the non-themed code. */
210 /**********************************************************************
211 * The button control subclass window proc.
213 LRESULT CALLBACK
THEMING_ButtonSubclassProc(HWND hwnd
, UINT msg
,
214 WPARAM wParam
, LPARAM lParam
,
217 const WCHAR
* themeClass
= WC_BUTTONW
;
224 result
= THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
225 OpenThemeData(hwnd
, themeClass
);
229 theme
= GetWindowTheme(hwnd
);
230 CloseThemeData (theme
);
231 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
233 case WM_THEMECHANGED
:
234 theme
= GetWindowTheme(hwnd
);
235 CloseThemeData (theme
);
236 OpenThemeData(hwnd
, themeClass
);
239 case WM_SYSCOLORCHANGE
:
240 theme
= GetWindowTheme(hwnd
);
241 if (!theme
) return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
242 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
243 * which will do the repaint. */
247 theme
= GetWindowTheme(hwnd
);
248 if (theme
&& BUTTON_Paint(theme
, hwnd
, (HDC
)wParam
))
251 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
254 theme
= GetWindowTheme(hwnd
);
255 if (theme
) RedrawWindow(hwnd
, NULL
, NULL
,
256 RDW_FRAME
| RDW_INVALIDATE
| RDW_UPDATENOW
);
257 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);
261 return THEMING_CallOriginalClass(hwnd
, msg
, wParam
, lParam
);