ole32: Don't pump all messages in CoWaitForMultipleHandles in threads without apartme...
[wine.git] / dlls / comctl32 / theme_button.c
blob327c76baecfc65ded8740ce125071d5ffc4d9c1a
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 textRect.left += 2;
282 textRect.right -= 2;
283 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
284 HeapFree(GetProcessHeap(), 0, text);
287 if (created_font) DeleteObject(font);
288 if (hPrevFont) SelectObject(hDC, hPrevFont);
291 static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
293 PB_draw, /* BS_PUSHBUTTON */
294 PB_draw, /* BS_DEFPUSHBUTTON */
295 CB_draw, /* BS_CHECKBOX */
296 CB_draw, /* BS_AUTOCHECKBOX */
297 CB_draw, /* BS_RADIOBUTTON */
298 CB_draw, /* BS_3STATE */
299 CB_draw, /* BS_AUTO3STATE */
300 GB_draw, /* BS_GROUPBOX */
301 NULL, /* BS_USERBUTTON */
302 CB_draw, /* BS_AUTORADIOBUTTON */
303 NULL, /* Not defined */
304 NULL, /* BS_OWNERDRAW */
305 NULL, /* Not defined */
306 NULL, /* Not defined */
307 NULL, /* Not defined */
308 NULL, /* Not defined */
311 static BOOL BUTTON_Paint(HTHEME theme, HWND hwnd, HDC hParamDC)
313 PAINTSTRUCT ps;
314 HDC hDC;
315 DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
316 DWORD dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
317 UINT dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
318 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
319 ButtonState drawState;
320 pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
322 if(!paint)
323 return FALSE;
325 if(IsWindowEnabled(hwnd))
327 if(state & BST_PUSHED) drawState = STATE_PRESSED;
328 else if(state & BST_HOT) drawState = STATE_HOT;
329 else if(state & BST_FOCUS) drawState = STATE_DEFAULTED;
330 else drawState = STATE_NORMAL;
332 else drawState = STATE_DISABLED;
334 hDC = hParamDC ? hParamDC : BeginPaint(hwnd, &ps);
335 paint(theme, hwnd, hDC, drawState, dtFlags, state & BST_FOCUS);
336 if (!hParamDC) EndPaint(hwnd, &ps);
337 return TRUE;
340 /**********************************************************************
341 * The button control subclass window proc.
343 LRESULT CALLBACK THEMING_ButtonSubclassProc(HWND hwnd, UINT msg,
344 WPARAM wParam, LPARAM lParam,
345 ULONG_PTR dwRefData)
347 const WCHAR* themeClass = WC_BUTTONW;
348 HTHEME theme;
349 LRESULT result;
351 switch (msg)
353 case WM_CREATE:
354 result = THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
355 OpenThemeData(hwnd, themeClass);
356 return result;
358 case WM_DESTROY:
359 theme = GetWindowTheme(hwnd);
360 CloseThemeData (theme);
361 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
363 case WM_THEMECHANGED:
364 theme = GetWindowTheme(hwnd);
365 CloseThemeData (theme);
366 OpenThemeData(hwnd, themeClass);
367 break;
369 case WM_SYSCOLORCHANGE:
370 theme = GetWindowTheme(hwnd);
371 if (!theme) return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
372 /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
373 * which will do the repaint. */
374 break;
376 case WM_PAINT:
377 theme = GetWindowTheme(hwnd);
378 if (theme && BUTTON_Paint(theme, hwnd, (HDC)wParam))
379 return 0;
380 else
381 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
383 case WM_ENABLE:
384 theme = GetWindowTheme(hwnd);
385 if (theme) {
386 RedrawWindow(hwnd, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
387 return 0;
388 } else
389 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
391 case WM_MOUSEMOVE:
393 TRACKMOUSEEVENT mouse_event;
394 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
395 mouse_event.dwFlags = TME_QUERY;
396 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
398 mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
399 mouse_event.hwndTrack = hwnd;
400 mouse_event.dwHoverTime = 1;
401 TrackMouseEvent(&mouse_event);
403 break;
406 case WM_MOUSEHOVER:
408 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
409 SetWindowLongW(hwnd, 0, state|BST_HOT);
410 InvalidateRect(hwnd, NULL, FALSE);
411 break;
414 case WM_MOUSELEAVE:
416 int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
417 SetWindowLongW(hwnd, 0, state&(~BST_HOT));
418 InvalidateRect(hwnd, NULL, FALSE);
419 break;
422 default:
423 /* Call old proc */
424 return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
426 return 0;