user32: Fall back to downscaling the big icon for ICON_SMALL2.
[wine.git] / dlls / comctl32 / theme_button.c
blobbdeba9339b256790b7de039bc49b0b32df8d1c95
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 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(theme_button);
38 #define BUTTON_TYPE 0x0f /* bit mask for the available button types */
40 /* These are indices into a states array to determine the theme state for a given theme part. */
41 typedef enum
43 STATE_NORMAL,
44 STATE_DISABLED,
45 STATE_HOT,
46 STATE_PRESSED,
47 STATE_DEFAULTED
48 } ButtonState;
50 typedef void (*pfThemedPaint)(HTHEME theme, HWND hwnd, HDC hdc, ButtonState drawState, UINT dtFlags, BOOL focused);
52 static UINT get_drawtext_flags(DWORD style, DWORD ex_style)
54 UINT flags = 0;
56 if (style & BS_PUSHLIKE)
57 style &= ~BUTTON_TYPE;
59 if (!(style & BS_MULTILINE))
60 flags |= DT_SINGLELINE;
61 else
62 flags |= DT_WORDBREAK;
64 switch (style & BS_CENTER)
66 case BS_LEFT: flags |= DT_LEFT; break;
67 case BS_RIGHT: flags |= DT_RIGHT; break;
68 case BS_CENTER: flags |= DT_CENTER; break;
69 default:
70 flags |= ((style & BUTTON_TYPE) <= BS_DEFPUSHBUTTON)
71 ? DT_CENTER : DT_LEFT;
74 if (ex_style & WS_EX_RIGHT)
75 flags = DT_RIGHT | (flags & ~(DT_LEFT | DT_CENTER));
77 if ((style & BUTTON_TYPE) != BS_GROUPBOX)
79 switch (style & BS_VCENTER)
81 case BS_TOP: flags |= DT_TOP; break;
82 case BS_BOTTOM: flags |= DT_BOTTOM; break;
83 case BS_VCENTER: /* fall through */
84 default: flags |= DT_VCENTER; break;
87 else
88 /* GroupBox's text is always single line and is top aligned. */
89 flags |= DT_SINGLELINE | DT_TOP;
91 return flags;
94 static inline WCHAR *get_button_text(HWND hwnd)
96 INT len = 512;
97 WCHAR *text;
98 text = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
99 if (text) InternalGetWindowText(hwnd, text, len + 1);
100 return text;
103 static void PB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused)
105 static const int states[] = { PBS_NORMAL, PBS_DISABLED, PBS_HOT, PBS_PRESSED, PBS_DEFAULTED };
107 RECT bgRect, textRect;
108 HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
109 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
110 int state = states[ drawState ];
111 WCHAR *text = get_button_text(hwnd);
113 GetClientRect(hwnd, &bgRect);
114 GetThemeBackgroundContentRect(theme, hDC, BP_PUSHBUTTON, state, &bgRect, &textRect);
116 if (IsThemeBackgroundPartiallyTransparent(theme, BP_PUSHBUTTON, state))
117 DrawThemeParentBackground(hwnd, hDC, NULL);
118 DrawThemeBackground(theme, hDC, BP_PUSHBUTTON, state, &bgRect, NULL);
119 if (text)
121 DrawThemeText(theme, hDC, BP_PUSHBUTTON, state, text, lstrlenW(text), dtFlags, 0, &textRect);
122 HeapFree(GetProcessHeap(), 0, text);
125 if (focused)
127 MARGINS margins;
128 RECT focusRect = bgRect;
130 GetThemeMargins(theme, hDC, BP_PUSHBUTTON, state, TMT_CONTENTMARGINS, NULL, &margins);
132 focusRect.left += margins.cxLeftWidth;
133 focusRect.top += margins.cyTopHeight;
134 focusRect.right -= margins.cxRightWidth;
135 focusRect.bottom -= margins.cyBottomHeight;
137 DrawFocusRect( hDC, &focusRect );
140 if (hPrevFont) SelectObject(hDC, hPrevFont);
143 static void CB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused)
145 static const int cb_states[3][5] =
147 { CBS_UNCHECKEDNORMAL, CBS_UNCHECKEDDISABLED, CBS_UNCHECKEDHOT, CBS_UNCHECKEDPRESSED, CBS_UNCHECKEDNORMAL },
148 { CBS_CHECKEDNORMAL, CBS_CHECKEDDISABLED, CBS_CHECKEDHOT, CBS_CHECKEDPRESSED, CBS_CHECKEDNORMAL },
149 { CBS_MIXEDNORMAL, CBS_MIXEDDISABLED, CBS_MIXEDHOT, CBS_MIXEDPRESSED, CBS_MIXEDNORMAL }
152 static const int rb_states[2][5] =
154 { RBS_UNCHECKEDNORMAL, RBS_UNCHECKEDDISABLED, RBS_UNCHECKEDHOT, RBS_UNCHECKEDPRESSED, RBS_UNCHECKEDNORMAL },
155 { RBS_CHECKEDNORMAL, RBS_CHECKEDDISABLED, RBS_CHECKEDHOT, RBS_CHECKEDPRESSED, RBS_CHECKEDNORMAL }
158 static const int cb_size = 13;
160 RECT bgRect, textRect;
161 HFONT font, hPrevFont = NULL;
162 LRESULT checkState = SendMessageW(hwnd, BM_GETCHECK, 0, 0);
163 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
164 int part = ((dwStyle & BUTTON_TYPE) == BS_RADIOBUTTON) || ((dwStyle & BUTTON_TYPE) == BS_AUTORADIOBUTTON)
165 ? BP_RADIOBUTTON
166 : BP_CHECKBOX;
167 int state = (part == BP_CHECKBOX)
168 ? cb_states[ checkState ][ drawState ]
169 : rb_states[ checkState ][ drawState ];
170 WCHAR *text = get_button_text(hwnd);
171 LOGFONTW lf;
172 BOOL created_font = FALSE;
174 HRESULT hr = GetThemeFont(theme, hDC, part, state, TMT_FONT, &lf);
175 if (SUCCEEDED(hr)) {
176 font = CreateFontIndirectW(&lf);
177 if (!font)
178 TRACE("Failed to create font\n");
179 else {
180 TRACE("font = %s\n", debugstr_w(lf.lfFaceName));
181 hPrevFont = SelectObject(hDC, font);
182 created_font = TRUE;
184 } else {
185 font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
186 hPrevFont = SelectObject(hDC, font);
189 GetClientRect(hwnd, &bgRect);
190 GetThemeBackgroundContentRect(theme, hDC, part, state, &bgRect, &textRect);
192 if (dtFlags & DT_SINGLELINE) /* Center the checkbox / radio button to the text. */
193 bgRect.top = bgRect.top + (textRect.bottom - textRect.top - cb_size) / 2;
195 /* adjust for the check/radio marker */
196 bgRect.bottom = bgRect.top + cb_size;
197 bgRect.right = bgRect.left + cb_size;
198 textRect.left = bgRect.right + 6;
200 DrawThemeParentBackground(hwnd, hDC, NULL);
202 DrawThemeBackground(theme, hDC, part, state, &bgRect, NULL);
203 if (text)
205 DrawThemeText(theme, hDC, part, state, text, lstrlenW(text), dtFlags, 0, &textRect);
207 if (focused)
209 RECT focusRect;
211 focusRect = textRect;
213 DrawTextW(hDC, text, lstrlenW(text), &focusRect, dtFlags | DT_CALCRECT);
215 if (focusRect.right < textRect.right) focusRect.right++;
216 focusRect.bottom = textRect.bottom;
218 DrawFocusRect( hDC, &focusRect );
221 HeapFree(GetProcessHeap(), 0, text);
224 if (created_font) DeleteObject(font);
225 if (hPrevFont) SelectObject(hDC, hPrevFont);
228 static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused)
230 static const int states[] = { GBS_NORMAL, GBS_DISABLED, GBS_NORMAL, GBS_NORMAL, GBS_NORMAL };
232 RECT bgRect, textRect, contentRect;
233 int state = states[ drawState ];
234 WCHAR *text = get_button_text(hwnd);
235 LOGFONTW lf;
236 HFONT font, hPrevFont = NULL;
237 BOOL created_font = FALSE;
239 HRESULT hr = GetThemeFont(theme, hDC, BP_GROUPBOX, state, TMT_FONT, &lf);
240 if (SUCCEEDED(hr)) {
241 font = CreateFontIndirectW(&lf);
242 if (!font)
243 TRACE("Failed to create font\n");
244 else {
245 hPrevFont = SelectObject(hDC, font);
246 created_font = TRUE;
248 } else {
249 font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
250 hPrevFont = SelectObject(hDC, font);
253 GetClientRect(hwnd, &bgRect);
254 textRect = bgRect;
256 if (text)
258 SIZE textExtent;
259 GetTextExtentPoint32W(hDC, text, lstrlenW(text), &textExtent);
260 bgRect.top += (textExtent.cy / 2);
261 textRect.left += 10;
262 textRect.bottom = textRect.top + textExtent.cy;
263 textRect.right = textRect.left + textExtent.cx + 4;
265 ExcludeClipRect(hDC, textRect.left, textRect.top, textRect.right, textRect.bottom);
268 GetThemeBackgroundContentRect(theme, hDC, BP_GROUPBOX, state, &bgRect, &contentRect);
269 ExcludeClipRect(hDC, contentRect.left, contentRect.top, contentRect.right, contentRect.bottom);
271 if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
272 DrawThemeParentBackground(hwnd, hDC, NULL);
273 DrawThemeBackground(theme, hDC, BP_GROUPBOX, state, &bgRect, NULL);
275 SelectClipRgn(hDC, NULL);
277 if (text)
279 textRect.left += 2;
280 textRect.right -= 2;
281 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
282 HeapFree(GetProcessHeap(), 0, text);
285 if (created_font) DeleteObject(font);
286 if (hPrevFont) SelectObject(hDC, hPrevFont);
289 static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
291 PB_draw, /* BS_PUSHBUTTON */
292 PB_draw, /* BS_DEFPUSHBUTTON */
293 CB_draw, /* BS_CHECKBOX */
294 CB_draw, /* BS_AUTOCHECKBOX */
295 CB_draw, /* BS_RADIOBUTTON */
296 CB_draw, /* BS_3STATE */
297 CB_draw, /* BS_AUTO3STATE */
298 GB_draw, /* BS_GROUPBOX */
299 NULL, /* BS_USERBUTTON */
300 CB_draw, /* BS_AUTORADIOBUTTON */
301 NULL, /* Not defined */
302 NULL, /* BS_OWNERDRAW */
303 NULL, /* Not defined */
304 NULL, /* Not defined */
305 NULL, /* Not defined */
306 NULL, /* Not defined */
309 static BOOL BUTTON_Paint(HTHEME theme, HWND hwnd, HDC hParamDC)
311 PAINTSTRUCT ps;
312 HDC hDC;
313 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
314 DWORD dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
315 UINT dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
316 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
317 ButtonState drawState;
318 pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
320 if(!paint)
321 return FALSE;
323 if(IsWindowEnabled(hwnd))
325 if(state & BST_PUSHED) drawState = STATE_PRESSED;
326 else if(state & BST_HOT) drawState = STATE_HOT;
327 else if(state & BST_FOCUS) drawState = STATE_DEFAULTED;
328 else drawState = STATE_NORMAL;
330 else drawState = STATE_DISABLED;
332 hDC = hParamDC ? hParamDC : BeginPaint(hwnd, &ps);
333 paint(theme, hwnd, hDC, drawState, dtFlags, state & BST_FOCUS);
334 if (!hParamDC) EndPaint(hwnd, &ps);
335 return TRUE;
338 /**********************************************************************
339 * The button control subclass window proc.
341 LRESULT CALLBACK THEMING_ButtonSubclassProc(HWND hwnd, UINT msg,
342 WPARAM wParam, LPARAM lParam,
343 ULONG_PTR dwRefData)
345 const WCHAR* themeClass = WC_BUTTONW;
346 HTHEME theme;
347 LRESULT result;
349 switch (msg)
351 case WM_CREATE:
352 result = THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
353 OpenThemeData(hwnd, themeClass);
354 return result;
356 case WM_DESTROY:
357 theme = GetWindowTheme(hwnd);
358 CloseThemeData (theme);
359 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
361 case WM_THEMECHANGED:
362 theme = GetWindowTheme(hwnd);
363 CloseThemeData (theme);
364 OpenThemeData(hwnd, themeClass);
365 break;
367 case WM_SYSCOLORCHANGE:
368 theme = GetWindowTheme(hwnd);
369 if (!theme) return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
370 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
371 * which will do the repaint. */
372 break;
374 case WM_PAINT:
375 theme = GetWindowTheme(hwnd);
376 if (theme && BUTTON_Paint(theme, hwnd, (HDC)wParam))
377 return 0;
378 else
379 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
381 case WM_ENABLE:
382 theme = GetWindowTheme(hwnd);
383 if (theme) {
384 RedrawWindow(hwnd, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
385 return 0;
386 } else
387 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
389 case WM_MOUSEMOVE:
391 TRACKMOUSEEVENT mouse_event;
392 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
393 mouse_event.dwFlags = TME_QUERY;
394 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
396 mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
397 mouse_event.hwndTrack = hwnd;
398 mouse_event.dwHoverTime = 1;
399 TrackMouseEvent(&mouse_event);
401 break;
404 case WM_MOUSEHOVER:
406 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
407 SetWindowLongW(hwnd, 0, state|BST_HOT);
408 InvalidateRect(hwnd, NULL, FALSE);
409 break;
412 case WM_MOUSELEAVE:
414 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
415 SetWindowLongW(hwnd, 0, state&(~BST_HOT));
416 InvalidateRect(hwnd, NULL, FALSE);
417 break;
420 default:
421 /* Call old proc */
422 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
424 return 0;