include: Use the hard-float calling convention for Windows APIs on ARM
[wine.git] / dlls / comctl32 / theme_button.c
blobf136b5df332ab2951b96b7f27f67e056fc4399a7
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 SIZE sz;
159 RECT bgRect, textRect;
160 HFONT font, hPrevFont = NULL;
161 LRESULT checkState = SendMessageW(hwnd, BM_GETCHECK, 0, 0);
162 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
163 int part = ((dwStyle & BUTTON_TYPE) == BS_RADIOBUTTON) || ((dwStyle & BUTTON_TYPE) == BS_AUTORADIOBUTTON)
164 ? BP_RADIOBUTTON
165 : BP_CHECKBOX;
166 int state = (part == BP_CHECKBOX)
167 ? cb_states[ checkState ][ drawState ]
168 : rb_states[ checkState ][ drawState ];
169 WCHAR *text = get_button_text(hwnd);
170 LOGFONTW lf;
171 BOOL created_font = FALSE;
173 HRESULT hr = GetThemeFont(theme, hDC, part, state, TMT_FONT, &lf);
174 if (SUCCEEDED(hr)) {
175 font = CreateFontIndirectW(&lf);
176 if (!font)
177 TRACE("Failed to create font\n");
178 else {
179 TRACE("font = %s\n", debugstr_w(lf.lfFaceName));
180 hPrevFont = SelectObject(hDC, font);
181 created_font = TRUE;
183 } else {
184 font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
185 hPrevFont = SelectObject(hDC, font);
188 if (FAILED(GetThemePartSize(theme, hDC, part, state, NULL, TS_DRAW, &sz)))
189 sz.cx = sz.cy = 13;
191 GetClientRect(hwnd, &bgRect);
192 GetThemeBackgroundContentRect(theme, hDC, part, state, &bgRect, &textRect);
194 if (dtFlags & DT_SINGLELINE) /* Center the checkbox / radio button to the text. */
195 bgRect.top = bgRect.top + (textRect.bottom - textRect.top - sz.cy) / 2;
197 /* adjust for the check/radio marker */
198 bgRect.bottom = bgRect.top + sz.cy;
199 bgRect.right = bgRect.left + sz.cx;
200 textRect.left = bgRect.right + 6;
202 DrawThemeParentBackground(hwnd, hDC, NULL);
204 DrawThemeBackground(theme, hDC, part, state, &bgRect, NULL);
205 if (text)
207 DrawThemeText(theme, hDC, part, state, text, lstrlenW(text), dtFlags, 0, &textRect);
209 if (focused)
211 RECT focusRect;
213 focusRect = textRect;
215 DrawTextW(hDC, text, lstrlenW(text), &focusRect, dtFlags | DT_CALCRECT);
217 if (focusRect.right < textRect.right) focusRect.right++;
218 focusRect.bottom = textRect.bottom;
220 DrawFocusRect( hDC, &focusRect );
223 HeapFree(GetProcessHeap(), 0, text);
226 if (created_font) DeleteObject(font);
227 if (hPrevFont) SelectObject(hDC, hPrevFont);
230 static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused)
232 static const int states[] = { GBS_NORMAL, GBS_DISABLED, GBS_NORMAL, GBS_NORMAL, GBS_NORMAL };
234 RECT bgRect, textRect, contentRect;
235 int state = states[ drawState ];
236 WCHAR *text = get_button_text(hwnd);
237 LOGFONTW lf;
238 HFONT font, hPrevFont = NULL;
239 BOOL created_font = FALSE;
241 HRESULT hr = GetThemeFont(theme, hDC, BP_GROUPBOX, state, TMT_FONT, &lf);
242 if (SUCCEEDED(hr)) {
243 font = CreateFontIndirectW(&lf);
244 if (!font)
245 TRACE("Failed to create font\n");
246 else {
247 hPrevFont = SelectObject(hDC, font);
248 created_font = TRUE;
250 } else {
251 font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
252 hPrevFont = SelectObject(hDC, font);
255 GetClientRect(hwnd, &bgRect);
256 textRect = bgRect;
258 if (text)
260 SIZE textExtent;
261 GetTextExtentPoint32W(hDC, text, lstrlenW(text), &textExtent);
262 bgRect.top += (textExtent.cy / 2);
263 textRect.left += 10;
264 textRect.bottom = textRect.top + textExtent.cy;
265 textRect.right = textRect.left + textExtent.cx + 4;
267 ExcludeClipRect(hDC, textRect.left, textRect.top, textRect.right, textRect.bottom);
270 GetThemeBackgroundContentRect(theme, hDC, BP_GROUPBOX, state, &bgRect, &contentRect);
271 ExcludeClipRect(hDC, contentRect.left, contentRect.top, contentRect.right, contentRect.bottom);
273 if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
274 DrawThemeParentBackground(hwnd, hDC, NULL);
275 DrawThemeBackground(theme, hDC, BP_GROUPBOX, state, &bgRect, NULL);
277 SelectClipRgn(hDC, NULL);
279 if (text)
281 InflateRect(&textRect, -2, 0);
282 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
283 HeapFree(GetProcessHeap(), 0, text);
286 if (created_font) DeleteObject(font);
287 if (hPrevFont) SelectObject(hDC, hPrevFont);
290 static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
292 PB_draw, /* BS_PUSHBUTTON */
293 PB_draw, /* BS_DEFPUSHBUTTON */
294 CB_draw, /* BS_CHECKBOX */
295 CB_draw, /* BS_AUTOCHECKBOX */
296 CB_draw, /* BS_RADIOBUTTON */
297 CB_draw, /* BS_3STATE */
298 CB_draw, /* BS_AUTO3STATE */
299 GB_draw, /* BS_GROUPBOX */
300 NULL, /* BS_USERBUTTON */
301 CB_draw, /* BS_AUTORADIOBUTTON */
302 NULL, /* Not defined */
303 NULL, /* BS_OWNERDRAW */
304 NULL, /* Not defined */
305 NULL, /* Not defined */
306 NULL, /* Not defined */
307 NULL, /* Not defined */
310 static BOOL BUTTON_Paint(HTHEME theme, HWND hwnd, HDC hParamDC)
312 PAINTSTRUCT ps;
313 HDC hDC;
314 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
315 DWORD dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
316 UINT dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
317 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
318 ButtonState drawState;
319 pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
321 if(IsWindowEnabled(hwnd))
323 if(state & BST_PUSHED) drawState = STATE_PRESSED;
324 else if(state & BST_HOT) drawState = STATE_HOT;
325 else if(state & BST_FOCUS) drawState = STATE_DEFAULTED;
326 else drawState = STATE_NORMAL;
328 else drawState = STATE_DISABLED;
330 hDC = hParamDC ? hParamDC : BeginPaint(hwnd, &ps);
331 if (paint) paint(theme, hwnd, hDC, drawState, dtFlags, state & BST_FOCUS);
332 if (!hParamDC) EndPaint(hwnd, &ps);
333 return TRUE;
336 /**********************************************************************
337 * The button control subclass window proc.
339 LRESULT CALLBACK THEMING_ButtonSubclassProc(HWND hwnd, UINT msg,
340 WPARAM wParam, LPARAM lParam,
341 ULONG_PTR dwRefData)
343 const WCHAR* themeClass = WC_BUTTONW;
344 HTHEME theme;
345 LRESULT result;
347 switch (msg)
349 case WM_CREATE:
350 result = THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
351 OpenThemeData(hwnd, themeClass);
352 return result;
354 case WM_DESTROY:
355 theme = GetWindowTheme(hwnd);
356 CloseThemeData (theme);
357 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
359 case WM_THEMECHANGED:
360 theme = GetWindowTheme(hwnd);
361 CloseThemeData (theme);
362 OpenThemeData(hwnd, themeClass);
363 break;
365 case WM_SYSCOLORCHANGE:
366 theme = GetWindowTheme(hwnd);
367 if (!theme) return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
368 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
369 * which will do the repaint. */
370 break;
372 case WM_PAINT:
373 theme = GetWindowTheme(hwnd);
374 if (theme && BUTTON_Paint(theme, hwnd, (HDC)wParam))
375 return 0;
376 else
377 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
379 case WM_ENABLE:
380 theme = GetWindowTheme(hwnd);
381 if (theme) {
382 RedrawWindow(hwnd, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
383 return 0;
384 } else
385 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
387 case WM_MOUSEMOVE:
389 TRACKMOUSEEVENT mouse_event;
390 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
391 mouse_event.dwFlags = TME_QUERY;
392 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
394 mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
395 mouse_event.hwndTrack = hwnd;
396 mouse_event.dwHoverTime = 1;
397 TrackMouseEvent(&mouse_event);
399 break;
402 case WM_MOUSEHOVER:
404 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
405 SetWindowLongW(hwnd, 0, state|BST_HOT);
406 InvalidateRect(hwnd, NULL, FALSE);
407 break;
410 case WM_MOUSELEAVE:
412 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
413 SetWindowLongW(hwnd, 0, state&(~BST_HOT));
414 InvalidateRect(hwnd, NULL, FALSE);
415 break;
418 default:
419 /* Call old proc */
420 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
422 return 0;