configure: Changes from running autconf after previous patch.
[wine/hacks.git] / dlls / comctl32 / theme_button.c
blob113e851b14fde3fdd794e9a23bb71dfac6268434
1 /*
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
22 #include <stdarg.h>
23 #include <string.h>
24 #include <stdlib.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "uxtheme.h"
31 #include "tmschema.h"
32 #include "comctl32.h"
34 #define BUTTON_TYPE 0x0f /* bit mask for the available button types */
36 /* These are indices into a states array to determine the theme state for a given theme part. */
37 typedef enum
39 STATE_NORMAL,
40 STATE_DISABLED,
41 STATE_HOT,
42 STATE_PRESSED,
43 STATE_DEFAULTED
44 } ButtonState;
46 typedef void (*pfThemedPaint)(HTHEME theme, HWND hwnd, HDC hdc, ButtonState drawState, UINT dtFlags);
48 static UINT get_drawtext_flags(DWORD style, DWORD ex_style)
50 UINT flags = 0;
52 if (style & BS_PUSHLIKE)
53 style &= ~BUTTON_TYPE;
55 if (!(style & BS_MULTILINE))
56 flags |= DT_SINGLELINE;
57 else
58 flags |= DT_WORDBREAK;
60 switch (style & BS_CENTER)
62 case BS_LEFT: flags |= DT_LEFT; break;
63 case BS_RIGHT: flags |= DT_RIGHT; break;
64 case BS_CENTER: flags |= DT_CENTER; break;
65 default:
66 flags |= ((style & BUTTON_TYPE) <= BS_DEFPUSHBUTTON)
67 ? DT_CENTER : DT_LEFT;
70 if (ex_style & WS_EX_RIGHT)
71 flags = DT_RIGHT | (flags & ~(DT_LEFT | DT_CENTER));
73 if ((style & BUTTON_TYPE) != BS_GROUPBOX)
75 switch (style & BS_VCENTER)
77 case BS_TOP: flags |= DT_TOP; break;
78 case BS_BOTTOM: flags |= DT_BOTTOM; break;
79 case BS_VCENTER: /* fall through */
80 default: flags |= DT_VCENTER; break;
83 else
84 /* GroupBox's text is always single line and is top aligned. */
85 flags |= DT_SINGLELINE | DT_TOP;
87 return flags;
90 static inline WCHAR *get_button_text(HWND hwnd)
92 INT len = 512;
93 WCHAR *text;
94 text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
95 if (text) InternalGetWindowText(hwnd, text, len + 1);
96 return text;
99 static void PB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
101 static const int states[] = { PBS_NORMAL, PBS_DISABLED, PBS_HOT, PBS_PRESSED, PBS_DEFAULTED };
103 RECT bgRect, textRect;
104 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
105 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
106 int state = states[ drawState ];
107 WCHAR *text = get_button_text(hwnd);
109 GetClientRect(hwnd, &bgRect);
110 GetThemeBackgroundContentRect(theme, hDC, BP_PUSHBUTTON, state, &bgRect, &textRect);
112 if (IsThemeBackgroundPartiallyTransparent(theme, BP_PUSHBUTTON, state))
113 DrawThemeParentBackground(hwnd, hDC, NULL);
114 DrawThemeBackground(theme, hDC, BP_PUSHBUTTON, state, &bgRect, NULL);
115 if (text)
117 DrawThemeText(theme, hDC, BP_PUSHBUTTON, state, text, lstrlenW(text), dtFlags, 0, &textRect);
118 HeapFree(GetProcessHeap(), 0, text);
121 if (hPrevFont) SelectObject(hDC, hPrevFont);
124 static void CB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
126 static const int cb_states[3][5] =
128 { CBS_UNCHECKEDNORMAL, CBS_UNCHECKEDDISABLED, CBS_UNCHECKEDHOT, CBS_UNCHECKEDPRESSED, CBS_UNCHECKEDNORMAL },
129 { CBS_CHECKEDNORMAL, CBS_CHECKEDDISABLED, CBS_CHECKEDHOT, CBS_CHECKEDPRESSED, CBS_CHECKEDNORMAL },
130 { CBS_MIXEDNORMAL, CBS_MIXEDDISABLED, CBS_MIXEDHOT, CBS_MIXEDPRESSED, CBS_MIXEDNORMAL }
133 static const int rb_states[2][5] =
135 { RBS_UNCHECKEDNORMAL, RBS_UNCHECKEDDISABLED, RBS_UNCHECKEDHOT, RBS_UNCHECKEDPRESSED, RBS_UNCHECKEDNORMAL },
136 { RBS_CHECKEDNORMAL, RBS_CHECKEDDISABLED, RBS_CHECKEDHOT, RBS_CHECKEDPRESSED, RBS_CHECKEDNORMAL }
139 static const int cb_size = 13;
141 RECT bgRect, textRect;
142 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
143 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
144 LRESULT checkState = SendMessageW(hwnd, BM_GETCHECK, 0, 0);
145 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
146 int part = ((dwStyle & BUTTON_TYPE) == BS_RADIOBUTTON) || ((dwStyle & BUTTON_TYPE) == BS_AUTORADIOBUTTON)
147 ? BP_RADIOBUTTON
148 : BP_CHECKBOX;
149 int state = (part == BP_CHECKBOX)
150 ? cb_states[ checkState ][ drawState ]
151 : rb_states[ checkState ][ drawState ];
152 WCHAR *text = get_button_text(hwnd);
154 GetClientRect(hwnd, &bgRect);
155 GetThemeBackgroundContentRect(theme, hDC, part, state, &bgRect, &textRect);
157 if (dtFlags & DT_SINGLELINE) /* Center the checkbox / radio button to the text. */
158 bgRect.top = bgRect.top + (textRect.bottom - textRect.top - cb_size) / 2;
160 /* adjust for the check/radio marker */
161 bgRect.bottom = bgRect.top + cb_size;
162 bgRect.right = bgRect.left + cb_size;
163 textRect.left = bgRect.right + 6;
165 if (IsThemeBackgroundPartiallyTransparent(theme, part, state))
166 DrawThemeParentBackground(hwnd, hDC, NULL);
167 DrawThemeBackground(theme, hDC, part, state, &bgRect, NULL);
168 if (text)
170 DrawThemeText(theme, hDC, part, state, text, lstrlenW(text), dtFlags, 0, &textRect);
171 HeapFree(GetProcessHeap(), 0, text);
174 if (hPrevFont) SelectObject(hDC, hPrevFont);
177 static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
179 static const int states[] = { GBS_NORMAL, GBS_DISABLED, GBS_NORMAL, GBS_NORMAL, GBS_NORMAL };
181 RECT bgRect, textRect, contentRect;
182 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
183 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
184 int state = states[ drawState ];
185 WCHAR *text = get_button_text(hwnd);
187 GetClientRect(hwnd, &bgRect);
188 textRect = bgRect;
190 if (text)
192 SIZE textExtent;
193 GetTextExtentPoint32W(hDC, text, lstrlenW(text), &textExtent);
194 bgRect.top += (textExtent.cy / 2);
195 textRect.left += 10;
196 textRect.bottom = textRect.top + textExtent.cy;
197 textRect.right = textRect.left + textExtent.cx + 4;
199 ExcludeClipRect(hDC, textRect.left, textRect.top, textRect.right, textRect.bottom);
202 GetThemeBackgroundContentRect(theme, hDC, BP_GROUPBOX, state, &bgRect, &contentRect);
203 ExcludeClipRect(hDC, contentRect.left, contentRect.top, contentRect.right, contentRect.bottom);
205 if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
206 DrawThemeParentBackground(hwnd, hDC, NULL);
207 DrawThemeBackground(theme, hDC, BP_GROUPBOX, state, &bgRect, NULL);
209 SelectClipRgn(hDC, NULL);
211 if (text)
213 textRect.left += 2;
214 textRect.right -= 2;
215 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
216 HeapFree(GetProcessHeap(), 0, text);
219 if (hPrevFont) SelectObject(hDC, hPrevFont);
222 static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
224 PB_draw, /* BS_PUSHBUTTON */
225 PB_draw, /* BS_DEFPUSHBUTTON */
226 CB_draw, /* BS_CHECKBOX */
227 CB_draw, /* BS_AUTOCHECKBOX */
228 CB_draw, /* BS_RADIOBUTTON */
229 CB_draw, /* BS_3STATE */
230 CB_draw, /* BS_AUTO3STATE */
231 GB_draw, /* BS_GROUPBOX */
232 NULL, /* BS_USERBUTTON */
233 CB_draw, /* BS_AUTORADIOBUTTON */
234 NULL, /* Not defined */
235 NULL, /* BS_OWNERDRAW */
236 NULL, /* Not defined */
237 NULL, /* Not defined */
238 NULL, /* Not defined */
239 NULL, /* Not defined */
242 static BOOL BUTTON_Paint(HTHEME theme, HWND hwnd, HDC hParamDC)
244 PAINTSTRUCT ps;
245 HDC hDC;
246 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
247 DWORD dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
248 UINT dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
249 ButtonState drawState = IsWindowEnabled(hwnd) ? STATE_NORMAL : STATE_DISABLED;
250 pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
252 if (paint)
254 hDC = hParamDC ? hParamDC : BeginPaint(hwnd, &ps);
255 paint(theme, hwnd, hDC, drawState, dtFlags);
256 if (!hParamDC) EndPaint(hwnd, &ps);
257 return TRUE;
260 return FALSE; /* Delegate drawing to the non-themed code. */
263 /**********************************************************************
264 * The button control subclass window proc.
266 LRESULT CALLBACK THEMING_ButtonSubclassProc(HWND hwnd, UINT msg,
267 WPARAM wParam, LPARAM lParam,
268 ULONG_PTR dwRefData)
270 const WCHAR* themeClass = WC_BUTTONW;
271 HTHEME theme;
272 LRESULT result;
274 switch (msg)
276 case WM_CREATE:
277 result = THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
278 OpenThemeData(hwnd, themeClass);
279 return result;
281 case WM_DESTROY:
282 theme = GetWindowTheme(hwnd);
283 CloseThemeData (theme);
284 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
286 case WM_THEMECHANGED:
287 theme = GetWindowTheme(hwnd);
288 CloseThemeData (theme);
289 OpenThemeData(hwnd, themeClass);
290 break;
292 case WM_SYSCOLORCHANGE:
293 theme = GetWindowTheme(hwnd);
294 if (!theme) return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
295 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
296 * which will do the repaint. */
297 break;
299 case WM_PAINT:
300 theme = GetWindowTheme(hwnd);
301 if (theme && BUTTON_Paint(theme, hwnd, (HDC)wParam))
302 return 0;
303 else
304 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
306 case WM_ENABLE:
307 theme = GetWindowTheme(hwnd);
308 if (theme) RedrawWindow(hwnd, NULL, NULL,
309 RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
310 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
312 default:
313 /* Call old proc */
314 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
316 return 0;