netcfgx: Add INetCfgLock support.
[wine/multimedia.git] / dlls / comctl32 / theme_button.c
blob01f16fb5931e448e50abdb81adf9c61cecbb87f5
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 "vssym32.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 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
250 ButtonState drawState;
251 pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
253 if(!paint)
254 return FALSE;
256 if(IsWindowEnabled(hwnd))
258 if(state & BST_PUSHED) drawState = STATE_PRESSED;
259 else if(state & BST_HOT) drawState = STATE_HOT;
260 else if(state & BST_FOCUS) drawState = STATE_DEFAULTED;
261 else drawState = STATE_NORMAL;
263 else drawState = STATE_DISABLED;
265 hDC = hParamDC ? hParamDC : BeginPaint(hwnd, &ps);
266 paint(theme, hwnd, hDC, drawState, dtFlags);
267 if (!hParamDC) EndPaint(hwnd, &ps);
268 return TRUE;
271 /**********************************************************************
272 * The button control subclass window proc.
274 LRESULT CALLBACK THEMING_ButtonSubclassProc(HWND hwnd, UINT msg,
275 WPARAM wParam, LPARAM lParam,
276 ULONG_PTR dwRefData)
278 const WCHAR* themeClass = WC_BUTTONW;
279 HTHEME theme;
280 LRESULT result;
282 switch (msg)
284 case WM_CREATE:
285 result = THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
286 OpenThemeData(hwnd, themeClass);
287 return result;
289 case WM_DESTROY:
290 theme = GetWindowTheme(hwnd);
291 CloseThemeData (theme);
292 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
294 case WM_THEMECHANGED:
295 theme = GetWindowTheme(hwnd);
296 CloseThemeData (theme);
297 OpenThemeData(hwnd, themeClass);
298 break;
300 case WM_SYSCOLORCHANGE:
301 theme = GetWindowTheme(hwnd);
302 if (!theme) return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
303 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
304 * which will do the repaint. */
305 break;
307 case WM_PAINT:
308 theme = GetWindowTheme(hwnd);
309 if (theme && BUTTON_Paint(theme, hwnd, (HDC)wParam))
310 return 0;
311 else
312 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
314 case WM_ENABLE:
315 theme = GetWindowTheme(hwnd);
316 if (theme) RedrawWindow(hwnd, NULL, NULL,
317 RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
318 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
320 case WM_MOUSEMOVE:
322 TRACKMOUSEEVENT mouse_event;
323 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
324 mouse_event.dwFlags = TME_QUERY;
325 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
327 mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
328 mouse_event.hwndTrack = hwnd;
329 mouse_event.dwHoverTime = 1;
330 TrackMouseEvent(&mouse_event);
332 break;
335 case WM_MOUSEHOVER:
337 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
338 SetWindowLongW(hwnd, 0, state|BST_HOT);
339 InvalidateRect(hwnd, NULL, FALSE);
340 break;
343 case WM_MOUSELEAVE:
345 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
346 SetWindowLongW(hwnd, 0, state&(~BST_HOT));
347 InvalidateRect(hwnd, NULL, FALSE);
348 break;
351 default:
352 /* Call old proc */
353 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
355 return 0;