comctl32/datetime: Fix length passed to GetLocaleInfoW().
[wine.git] / dlls / comctl32 / button.c
blob986958574422112261ae55f71ffe31aac2827724
1 /*
2 * Copyright (C) 1993 Johannes Ruscheinski
3 * Copyright (C) 1993 David Metcalfe
4 * Copyright (C) 1994 Alexandre Julliard
5 * Copyright (C) 2008 by Reece H. Dunn
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * TODO
22 * Styles
23 * - BS_NOTIFY: is it complete?
24 * - BS_RIGHTBUTTON: same as BS_LEFTTEXT
26 * Messages
27 * - WM_CHAR: Checks a (manual or automatic) check box on '+' or '=', clears it on '-' key.
28 * - WM_SETFOCUS: For (manual or automatic) radio buttons, send the parent window BN_CLICKED
29 * - WM_NCCREATE: Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
30 * - WM_SYSKEYUP
31 * - BCM_GETIDEALSIZE
32 * - BCM_GETIMAGELIST
33 * - BCM_GETTEXTMARGIN
34 * - BCM_SETIMAGELIST
35 * - BCM_SETTEXTMARGIN
37 * Notifications
38 * - BCN_HOTITEMCHANGE
39 * - BN_DISABLE
40 * - BN_PUSHED/BN_HILITE
41 * + BN_KILLFOCUS: is it OK?
42 * - BN_PAINT
43 * + BN_SETFOCUS: is it OK?
44 * - BN_UNPUSHED/BN_UNHILITE
45 * - NM_CUSTOMDRAW
47 * Structures/Macros/Definitions
48 * - BUTTON_IMAGELIST
49 * - NMBCHOTITEM
50 * - Button_GetIdealSize
51 * - Button_GetImageList
52 * - Button_GetTextMargin
53 * - Button_SetImageList
54 * - Button_SetTextMargin
57 #include <stdarg.h>
58 #include <string.h>
59 #include <stdlib.h>
61 #define OEMRESOURCE
63 #include "windef.h"
64 #include "winbase.h"
65 #include "wingdi.h"
66 #include "winuser.h"
67 #include "uxtheme.h"
68 #include "vssym32.h"
69 #include "wine/debug.h"
70 #include "wine/heap.h"
72 #include "comctl32.h"
74 WINE_DEFAULT_DEBUG_CHANNEL(button);
76 /* undocumented flags */
77 #define BUTTON_NSTATES 0x0F
78 #define BUTTON_BTNPRESSED 0x40
79 #define BUTTON_UNKNOWN2 0x20
80 #define BUTTON_UNKNOWN3 0x10
82 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
83 do { /* Notify parent which has created this button control */ \
84 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
85 SendMessageW(GetParent(hWnd), WM_COMMAND, \
86 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
87 (LPARAM)(hWnd)); \
88 } while(0)
90 typedef struct _BUTTON_INFO
92 HWND hwnd;
93 LONG state;
94 HFONT font;
95 union
97 HICON icon;
98 HBITMAP bitmap;
99 HANDLE image;
100 } u;
101 } BUTTON_INFO;
103 static UINT BUTTON_CalcLabelRect( const BUTTON_INFO *infoPtr, HDC hdc, RECT *rc );
104 static void PB_Paint( const BUTTON_INFO *infoPtr, HDC hDC, UINT action );
105 static void CB_Paint( const BUTTON_INFO *infoPtr, HDC hDC, UINT action );
106 static void GB_Paint( const BUTTON_INFO *infoPtr, HDC hDC, UINT action );
107 static void UB_Paint( const BUTTON_INFO *infoPtr, HDC hDC, UINT action );
108 static void OB_Paint( const BUTTON_INFO *infoPtr, HDC hDC, UINT action );
109 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
111 #define MAX_BTN_TYPE 16
113 static const WORD maxCheckState[MAX_BTN_TYPE] =
115 BST_UNCHECKED, /* BS_PUSHBUTTON */
116 BST_UNCHECKED, /* BS_DEFPUSHBUTTON */
117 BST_CHECKED, /* BS_CHECKBOX */
118 BST_CHECKED, /* BS_AUTOCHECKBOX */
119 BST_CHECKED, /* BS_RADIOBUTTON */
120 BST_INDETERMINATE, /* BS_3STATE */
121 BST_INDETERMINATE, /* BS_AUTO3STATE */
122 BST_UNCHECKED, /* BS_GROUPBOX */
123 BST_UNCHECKED, /* BS_USERBUTTON */
124 BST_CHECKED, /* BS_AUTORADIOBUTTON */
125 BST_UNCHECKED, /* BS_PUSHBOX */
126 BST_UNCHECKED, /* BS_OWNERDRAW */
127 BST_UNCHECKED, /* BS_SPLITBUTTON */
128 BST_UNCHECKED, /* BS_DEFSPLITBUTTON */
129 BST_UNCHECKED, /* BS_COMMANDLINK */
130 BST_UNCHECKED /* BS_DEFCOMMANDLINK */
133 /* These are indices into a states array to determine the theme state for a given theme part. */
134 typedef enum
136 STATE_NORMAL,
137 STATE_DISABLED,
138 STATE_HOT,
139 STATE_PRESSED,
140 STATE_DEFAULTED
141 } ButtonState;
143 typedef void (*pfPaint)( const BUTTON_INFO *infoPtr, HDC hdc, UINT action );
145 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
147 PB_Paint, /* BS_PUSHBUTTON */
148 PB_Paint, /* BS_DEFPUSHBUTTON */
149 CB_Paint, /* BS_CHECKBOX */
150 CB_Paint, /* BS_AUTOCHECKBOX */
151 CB_Paint, /* BS_RADIOBUTTON */
152 CB_Paint, /* BS_3STATE */
153 CB_Paint, /* BS_AUTO3STATE */
154 GB_Paint, /* BS_GROUPBOX */
155 UB_Paint, /* BS_USERBUTTON */
156 CB_Paint, /* BS_AUTORADIOBUTTON */
157 NULL, /* BS_PUSHBOX */
158 OB_Paint, /* BS_OWNERDRAW */
159 PB_Paint, /* BS_SPLITBUTTON */
160 PB_Paint, /* BS_DEFSPLITBUTTON */
161 PB_Paint, /* BS_COMMANDLINK */
162 PB_Paint /* BS_DEFCOMMANDLINK */
165 typedef void (*pfThemedPaint)( HTHEME theme, const BUTTON_INFO *infoPtr, HDC hdc, ButtonState drawState, UINT dtflags, BOOL focused);
167 static void PB_ThemedPaint( HTHEME theme, const BUTTON_INFO *infoPtr, HDC hdc, ButtonState drawState, UINT dtflags, BOOL focused);
168 static void CB_ThemedPaint( HTHEME theme, const BUTTON_INFO *infoPtr, HDC hdc, ButtonState drawState, UINT dtflags, BOOL focused);
169 static void GB_ThemedPaint( HTHEME theme, const BUTTON_INFO *infoPtr, HDC hdc, ButtonState drawState, UINT dtflags, BOOL focused);
171 static const pfThemedPaint btnThemedPaintFunc[MAX_BTN_TYPE] =
173 PB_ThemedPaint, /* BS_PUSHBUTTON */
174 PB_ThemedPaint, /* BS_DEFPUSHBUTTON */
175 CB_ThemedPaint, /* BS_CHECKBOX */
176 CB_ThemedPaint, /* BS_AUTOCHECKBOX */
177 CB_ThemedPaint, /* BS_RADIOBUTTON */
178 CB_ThemedPaint, /* BS_3STATE */
179 CB_ThemedPaint, /* BS_AUTO3STATE */
180 GB_ThemedPaint, /* BS_GROUPBOX */
181 NULL, /* BS_USERBUTTON */
182 CB_ThemedPaint, /* BS_AUTORADIOBUTTON */
183 NULL, /* BS_PUSHBOX */
184 NULL, /* BS_OWNERDRAW */
185 NULL, /* BS_SPLITBUTTON */
186 NULL, /* BS_DEFSPLITBUTTON */
187 NULL, /* BS_COMMANDLINK */
188 NULL, /* BS_DEFCOMMANDLINK */
191 static inline UINT get_button_type( LONG window_style )
193 return (window_style & BS_TYPEMASK);
196 /* paint a button of any type */
197 static inline void paint_button( BUTTON_INFO *infoPtr, LONG style, UINT action )
199 if (btnPaintFunc[style] && IsWindowVisible(infoPtr->hwnd))
201 HDC hdc = GetDC( infoPtr->hwnd );
202 btnPaintFunc[style]( infoPtr, hdc, action );
203 ReleaseDC( infoPtr->hwnd, hdc );
207 /* retrieve the button text; returned buffer must be freed by caller */
208 static inline WCHAR *get_button_text( const BUTTON_INFO *infoPtr )
210 INT len = GetWindowTextLengthW( infoPtr->hwnd );
211 WCHAR *buffer = heap_alloc( (len + 1) * sizeof(WCHAR) );
212 if (buffer)
213 GetWindowTextW( infoPtr->hwnd, buffer, len + 1 );
214 return buffer;
217 HRGN set_control_clipping( HDC hdc, const RECT *rect )
219 RECT rc = *rect;
220 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
222 if (GetClipRgn( hdc, hrgn ) != 1)
224 DeleteObject( hrgn );
225 hrgn = 0;
227 DPtoLP( hdc, (POINT *)&rc, 2 );
228 if (GetLayout( hdc ) & LAYOUT_RTL) /* compensate for the shifting done by IntersectClipRect */
230 rc.left++;
231 rc.right++;
233 IntersectClipRect( hdc, rc.left, rc.top, rc.right, rc.bottom );
234 return hrgn;
237 /**********************************************************************
238 * Convert button styles to flags used by DrawText.
240 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
242 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
244 /* "Convert" pushlike buttons to pushbuttons */
245 if (style & BS_PUSHLIKE)
246 style &= ~BS_TYPEMASK;
248 if (!(style & BS_MULTILINE))
249 dtStyle |= DT_SINGLELINE;
250 else
251 dtStyle |= DT_WORDBREAK;
253 switch (style & BS_CENTER)
255 case BS_LEFT: /* DT_LEFT is 0 */ break;
256 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
257 case BS_CENTER: dtStyle |= DT_CENTER; break;
258 default:
259 /* Pushbutton's text is centered by default */
260 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
261 /* all other flavours have left aligned text */
264 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
266 /* DrawText ignores vertical alignment for multiline text,
267 * but we use these flags to align label manually.
269 if (get_button_type(style) != BS_GROUPBOX)
271 switch (style & BS_VCENTER)
273 case BS_TOP: /* DT_TOP is 0 */ break;
274 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
275 case BS_VCENTER: /* fall through */
276 default: dtStyle |= DT_VCENTER; break;
279 else
280 /* GroupBox's text is always single line and is top aligned. */
281 dtStyle |= DT_SINGLELINE;
283 return dtStyle;
286 static LRESULT CALLBACK BUTTON_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
288 BUTTON_INFO *infoPtr = (BUTTON_INFO *)GetWindowLongPtrW(hWnd, 0);
289 RECT rect;
290 POINT pt;
291 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
292 UINT btn_type = get_button_type( style );
293 LONG state, new_state;
294 HANDLE oldHbitmap;
295 HTHEME theme;
297 if (!IsWindow( hWnd )) return 0;
299 if (!infoPtr && (uMsg != WM_NCCREATE))
300 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
302 pt.x = (short)LOWORD(lParam);
303 pt.y = (short)HIWORD(lParam);
305 switch (uMsg)
307 case WM_GETDLGCODE:
308 switch(btn_type)
310 case BS_COMMANDLINK:
311 case BS_USERBUTTON:
312 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
313 case BS_DEFCOMMANDLINK:
314 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
315 case BS_RADIOBUTTON:
316 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
317 case BS_GROUPBOX: return DLGC_STATIC;
318 case BS_SPLITBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON | DLGC_WANTARROWS;
319 case BS_DEFSPLITBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON | DLGC_WANTARROWS;
320 default: return DLGC_BUTTON;
323 case WM_ENABLE:
324 theme = GetWindowTheme( hWnd );
325 if (theme)
326 RedrawWindow( hWnd, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW );
327 else
328 paint_button( infoPtr, btn_type, ODA_DRAWENTIRE );
329 break;
331 case WM_NCCREATE:
332 infoPtr = heap_alloc_zero( sizeof(*infoPtr) );
333 SetWindowLongPtrW( hWnd, 0, (LONG_PTR)infoPtr );
334 infoPtr->hwnd = hWnd;
335 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
337 case WM_NCDESTROY:
338 SetWindowLongPtrW( hWnd, 0, 0 );
339 heap_free(infoPtr);
340 break;
342 case WM_CREATE:
343 if (btn_type >= MAX_BTN_TYPE)
344 return -1; /* abort */
346 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
347 if (btn_type == BS_USERBUTTON )
349 style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
350 SetWindowLongW( hWnd, GWL_STYLE, style );
352 infoPtr->state = BST_UNCHECKED;
353 OpenThemeData( hWnd, WC_BUTTONW );
354 return 0;
356 case WM_DESTROY:
357 theme = GetWindowTheme( hWnd );
358 CloseThemeData( theme );
359 break;
361 case WM_THEMECHANGED:
362 theme = GetWindowTheme( hWnd );
363 CloseThemeData( theme );
364 OpenThemeData( hWnd, WC_BUTTONW );
365 break;
367 case WM_ERASEBKGND:
368 if (btn_type == BS_OWNERDRAW)
370 HDC hdc = (HDC)wParam;
371 RECT rc;
372 HBRUSH hBrush;
373 HWND parent = GetParent(hWnd);
374 if (!parent) parent = hWnd;
375 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
376 if (!hBrush) /* did the app forget to call defwindowproc ? */
377 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
378 (WPARAM)hdc, (LPARAM)hWnd);
379 GetClientRect(hWnd, &rc);
380 FillRect(hdc, &rc, hBrush);
382 return 1;
384 case WM_PRINTCLIENT:
385 case WM_PAINT:
387 PAINTSTRUCT ps;
388 HDC hdc;
390 theme = GetWindowTheme( hWnd );
391 hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
393 if (theme && btnThemedPaintFunc[btn_type])
395 ButtonState drawState;
396 UINT dtflags;
398 if (IsWindowEnabled( hWnd ))
400 if (infoPtr->state & BST_PUSHED) drawState = STATE_PRESSED;
401 else if (infoPtr->state & BST_HOT) drawState = STATE_HOT;
402 else if (infoPtr->state & BST_FOCUS) drawState = STATE_DEFAULTED;
403 else drawState = STATE_NORMAL;
405 else
406 drawState = STATE_DISABLED;
408 dtflags = BUTTON_BStoDT(style, GetWindowLongW(hWnd, GWL_EXSTYLE));
409 btnThemedPaintFunc[btn_type](theme, infoPtr, hdc, drawState, dtflags, infoPtr->state & BST_FOCUS);
411 else if (btnPaintFunc[btn_type])
413 int nOldMode = SetBkMode( hdc, OPAQUE );
414 btnPaintFunc[btn_type]( infoPtr, hdc, ODA_DRAWENTIRE );
415 SetBkMode(hdc, nOldMode); /* reset painting mode */
418 if ( !wParam ) EndPaint( hWnd, &ps );
419 break;
422 case WM_KEYDOWN:
423 if (wParam == VK_SPACE)
425 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
426 infoPtr->state |= BUTTON_BTNPRESSED;
427 SetCapture( hWnd );
429 break;
431 case WM_LBUTTONDBLCLK:
432 if(style & BS_NOTIFY ||
433 btn_type == BS_RADIOBUTTON ||
434 btn_type == BS_USERBUTTON ||
435 btn_type == BS_OWNERDRAW)
437 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
438 break;
440 /* fall through */
441 case WM_LBUTTONDOWN:
442 SetCapture( hWnd );
443 SetFocus( hWnd );
444 infoPtr->state |= BUTTON_BTNPRESSED;
445 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
446 break;
448 case WM_KEYUP:
449 if (wParam != VK_SPACE)
450 break;
451 /* fall through */
452 case WM_LBUTTONUP:
453 state = infoPtr->state;
454 if (!(state & BUTTON_BTNPRESSED)) break;
455 infoPtr->state &= BUTTON_NSTATES;
456 if (!(state & BST_PUSHED))
458 ReleaseCapture();
459 break;
461 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
462 GetClientRect( hWnd, &rect );
463 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
465 switch(btn_type)
467 case BS_AUTOCHECKBOX:
468 SendMessageW( hWnd, BM_SETCHECK, !(infoPtr->state & BST_CHECKED), 0 );
469 break;
470 case BS_AUTORADIOBUTTON:
471 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
472 break;
473 case BS_AUTO3STATE:
474 SendMessageW( hWnd, BM_SETCHECK, (infoPtr->state & BST_INDETERMINATE) ? 0 :
475 ((infoPtr->state & 3) + 1), 0 );
476 break;
478 ReleaseCapture();
479 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
481 else
483 ReleaseCapture();
485 break;
487 case WM_CAPTURECHANGED:
488 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
489 if (hWnd == (HWND)lParam) break;
490 if (infoPtr->state & BUTTON_BTNPRESSED)
492 infoPtr->state &= BUTTON_NSTATES;
493 if (infoPtr->state & BST_PUSHED)
494 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
496 break;
498 case WM_MOUSEMOVE:
500 TRACKMOUSEEVENT mouse_event;
502 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
503 mouse_event.dwFlags = TME_QUERY;
504 if (!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags & (TME_HOVER | TME_LEAVE)))
506 mouse_event.dwFlags = TME_HOVER | TME_LEAVE;
507 mouse_event.hwndTrack = hWnd;
508 mouse_event.dwHoverTime = 1;
509 TrackMouseEvent(&mouse_event);
512 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
514 GetClientRect( hWnd, &rect );
515 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
517 break;
520 case WM_MOUSEHOVER:
522 infoPtr->state |= BST_HOT;
523 InvalidateRect( hWnd, NULL, FALSE );
524 break;
527 case WM_MOUSELEAVE:
529 infoPtr->state &= ~BST_HOT;
530 InvalidateRect( hWnd, NULL, FALSE );
531 break;
534 case WM_SETTEXT:
536 /* Clear an old text here as Windows does */
537 if (IsWindowVisible(hWnd))
539 HDC hdc = GetDC(hWnd);
540 HBRUSH hbrush;
541 RECT client, rc;
542 HWND parent = GetParent(hWnd);
543 UINT message = (btn_type == BS_PUSHBUTTON ||
544 btn_type == BS_DEFPUSHBUTTON ||
545 btn_type == BS_USERBUTTON ||
546 btn_type == BS_OWNERDRAW) ?
547 WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
549 if (!parent) parent = hWnd;
550 hbrush = (HBRUSH)SendMessageW(parent, message,
551 (WPARAM)hdc, (LPARAM)hWnd);
552 if (!hbrush) /* did the app forget to call DefWindowProc ? */
553 hbrush = (HBRUSH)DefWindowProcW(parent, message,
554 (WPARAM)hdc, (LPARAM)hWnd);
556 GetClientRect(hWnd, &client);
557 rc = client;
558 /* FIXME: check other BS_* handlers */
559 if (btn_type == BS_GROUPBOX)
560 InflateRect(&rc, -7, 1); /* GB_Paint does this */
561 BUTTON_CalcLabelRect(infoPtr, hdc, &rc);
562 /* Clip by client rect bounds */
563 if (rc.right > client.right) rc.right = client.right;
564 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
565 FillRect(hdc, &rc, hbrush);
566 ReleaseDC(hWnd, hdc);
569 DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
570 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
571 InvalidateRect( hWnd, NULL, TRUE );
572 else
573 paint_button( infoPtr, btn_type, ODA_DRAWENTIRE );
574 return 1; /* success. FIXME: check text length */
577 case WM_SETFONT:
578 infoPtr->font = (HFONT)wParam;
579 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
580 break;
582 case WM_GETFONT:
583 return (LRESULT)infoPtr->font;
585 case WM_SETFOCUS:
586 TRACE("WM_SETFOCUS %p\n",hWnd);
587 infoPtr->state |= BST_FOCUS;
588 paint_button( infoPtr, btn_type, ODA_FOCUS );
589 if (style & BS_NOTIFY)
590 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
591 break;
593 case WM_KILLFOCUS:
594 TRACE("WM_KILLFOCUS %p\n",hWnd);
595 infoPtr->state &= ~BST_FOCUS;
596 paint_button( infoPtr, btn_type, ODA_FOCUS );
598 if ((infoPtr->state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
599 ReleaseCapture();
600 if (style & BS_NOTIFY)
601 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
603 InvalidateRect( hWnd, NULL, FALSE );
604 break;
606 case WM_SYSCOLORCHANGE:
607 InvalidateRect( hWnd, NULL, FALSE );
608 break;
610 case BM_SETSTYLE:
611 btn_type = wParam & BS_TYPEMASK;
612 style = (style & ~BS_TYPEMASK) | btn_type;
613 SetWindowLongW( hWnd, GWL_STYLE, style );
615 /* Only redraw if lParam flag is set.*/
616 if (lParam)
617 InvalidateRect( hWnd, NULL, TRUE );
619 break;
621 case BM_CLICK:
622 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
623 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
624 break;
626 case BM_SETIMAGE:
627 /* Check that image format matches button style */
628 switch (style & (BS_BITMAP|BS_ICON))
630 case BS_BITMAP:
631 if (wParam != IMAGE_BITMAP) return 0;
632 break;
633 case BS_ICON:
634 if (wParam != IMAGE_ICON) return 0;
635 break;
636 default:
637 return 0;
639 oldHbitmap = infoPtr->u.image;
640 infoPtr->u.image = (HANDLE)lParam;
641 InvalidateRect( hWnd, NULL, FALSE );
642 return (LRESULT)oldHbitmap;
644 case BM_GETIMAGE:
645 return (LRESULT)infoPtr->u.image;
647 case BM_GETCHECK:
648 return infoPtr->state & 3;
650 case BM_SETCHECK:
651 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
652 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
654 style = wParam ? style | WS_TABSTOP : style & ~WS_TABSTOP;
655 SetWindowLongW( hWnd, GWL_STYLE, style );
657 if ((infoPtr->state & 3) != wParam)
659 infoPtr->state = (infoPtr->state & ~3) | wParam;
660 InvalidateRect( hWnd, NULL, FALSE );
662 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BST_CHECKED) && (style & WS_CHILD))
663 BUTTON_CheckAutoRadioButton( hWnd );
664 break;
666 case BM_GETSTATE:
667 return infoPtr->state;
669 case BM_SETSTATE:
670 state = infoPtr->state;
671 new_state = wParam ? BST_PUSHED : 0;
673 if ((state ^ new_state) & BST_PUSHED)
675 if (wParam)
676 state |= BST_PUSHED;
677 else
678 state &= ~BST_PUSHED;
680 if (btn_type == BS_USERBUTTON)
681 BUTTON_NOTIFY_PARENT( hWnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
682 infoPtr->state = state;
684 InvalidateRect( hWnd, NULL, FALSE );
686 break;
688 case WM_NCHITTEST:
689 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
690 /* fall through */
691 default:
692 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
694 return 0;
697 /**********************************************************************
698 * BUTTON_CalcLabelRect
700 * Calculates label's rectangle depending on button style.
702 * Returns flags to be passed to DrawText.
703 * Calculated rectangle doesn't take into account button state
704 * (pushed, etc.). If there is nothing to draw (no text/image) output
705 * rectangle is empty, and return value is (UINT)-1.
707 static UINT BUTTON_CalcLabelRect(const BUTTON_INFO *infoPtr, HDC hdc, RECT *rc)
709 LONG style = GetWindowLongW( infoPtr->hwnd, GWL_STYLE );
710 LONG ex_style = GetWindowLongW( infoPtr->hwnd, GWL_EXSTYLE );
711 WCHAR *text;
712 ICONINFO iconInfo;
713 BITMAP bm;
714 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
715 RECT r = *rc;
716 INT n;
718 /* Calculate label rectangle according to label type */
719 switch (style & (BS_ICON|BS_BITMAP))
721 case BS_TEXT:
723 HFONT hFont, hPrevFont = 0;
725 if (!(text = get_button_text( infoPtr ))) goto empty_rect;
726 if (!text[0])
728 heap_free( text );
729 goto empty_rect;
732 if ((hFont = infoPtr->font)) hPrevFont = SelectObject( hdc, hFont );
733 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
734 if (hPrevFont) SelectObject( hdc, hPrevFont );
735 heap_free( text );
736 break;
739 case BS_ICON:
740 if (!GetIconInfo(infoPtr->u.icon, &iconInfo))
741 goto empty_rect;
743 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
745 r.right = r.left + bm.bmWidth;
746 r.bottom = r.top + bm.bmHeight;
748 DeleteObject(iconInfo.hbmColor);
749 DeleteObject(iconInfo.hbmMask);
750 break;
752 case BS_BITMAP:
753 if (!GetObjectW( infoPtr->u.bitmap, sizeof(BITMAP), &bm))
754 goto empty_rect;
756 r.right = r.left + bm.bmWidth;
757 r.bottom = r.top + bm.bmHeight;
758 break;
760 default:
761 empty_rect:
762 rc->right = r.left;
763 rc->bottom = r.top;
764 return (UINT)-1;
767 /* Position label inside bounding rectangle according to
768 * alignment flags. (calculated rect is always left-top aligned).
769 * If label is aligned to any side - shift label in opposite
770 * direction to leave extra space for focus rectangle.
772 switch (dtStyle & (DT_CENTER|DT_RIGHT))
774 case DT_LEFT: r.left++; r.right++; break;
775 case DT_CENTER: n = r.right - r.left;
776 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
777 r.right = r.left + n; break;
778 case DT_RIGHT: n = r.right - r.left;
779 r.right = rc->right - 1;
780 r.left = r.right - n;
781 break;
784 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
786 case DT_TOP: r.top++; r.bottom++; break;
787 case DT_VCENTER: n = r.bottom - r.top;
788 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
789 r.bottom = r.top + n; break;
790 case DT_BOTTOM: n = r.bottom - r.top;
791 r.bottom = rc->bottom - 1;
792 r.top = r.bottom - n;
793 break;
796 *rc = r;
797 return dtStyle;
801 /**********************************************************************
802 * BUTTON_DrawTextCallback
804 * Callback function used by DrawStateW function.
806 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
808 RECT rc;
810 SetRect(&rc, 0, 0, cx, cy);
811 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
812 return TRUE;
816 /**********************************************************************
817 * BUTTON_DrawLabel
819 * Common function for drawing button label.
821 static void BUTTON_DrawLabel(const BUTTON_INFO *infoPtr, HDC hdc, UINT dtFlags, const RECT *rc)
823 DRAWSTATEPROC lpOutputProc = NULL;
824 LPARAM lp;
825 WPARAM wp = 0;
826 HBRUSH hbr = 0;
827 UINT flags = IsWindowEnabled(infoPtr->hwnd) ? DSS_NORMAL : DSS_DISABLED;
828 LONG state = infoPtr->state;
829 LONG style = GetWindowLongW( infoPtr->hwnd, GWL_STYLE );
830 WCHAR *text = NULL;
832 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
833 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
834 * I don't have Win31 on hand to verify that, so I leave it as is.
837 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
839 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
840 flags |= DSS_MONO;
843 switch (style & (BS_ICON|BS_BITMAP))
845 case BS_TEXT:
846 /* DST_COMPLEX -- is 0 */
847 lpOutputProc = BUTTON_DrawTextCallback;
848 if (!(text = get_button_text( infoPtr ))) return;
849 lp = (LPARAM)text;
850 wp = dtFlags;
851 break;
853 case BS_ICON:
854 flags |= DST_ICON;
855 lp = (LPARAM)infoPtr->u.icon;
856 break;
858 case BS_BITMAP:
859 flags |= DST_BITMAP;
860 lp = (LPARAM)infoPtr->u.bitmap;
861 break;
863 default:
864 return;
867 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
868 rc->right - rc->left, rc->bottom - rc->top, flags);
869 heap_free( text );
872 /**********************************************************************
873 * Push Button Functions
875 static void PB_Paint( const BUTTON_INFO *infoPtr, HDC hDC, UINT action )
877 RECT rc, r;
878 UINT dtFlags, uState;
879 HPEN hOldPen, hpen;
880 HBRUSH hOldBrush;
881 INT oldBkMode;
882 COLORREF oldTxtColor;
883 HFONT hFont;
884 LONG state = infoPtr->state;
885 LONG style = GetWindowLongW( infoPtr->hwnd, GWL_STYLE );
886 BOOL pushedState = (state & BST_PUSHED);
887 HWND parent;
888 HRGN hrgn;
890 GetClientRect( infoPtr->hwnd, &rc );
892 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
893 if ((hFont = infoPtr->font)) SelectObject( hDC, hFont );
894 parent = GetParent(infoPtr->hwnd);
895 if (!parent) parent = infoPtr->hwnd;
896 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)infoPtr->hwnd );
898 hrgn = set_control_clipping( hDC, &rc );
900 hpen = CreatePen( PS_SOLID, 1, GetSysColor(COLOR_WINDOWFRAME));
901 hOldPen = SelectObject(hDC, hpen);
902 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
903 oldBkMode = SetBkMode(hDC, TRANSPARENT);
905 if (get_button_type(style) == BS_DEFPUSHBUTTON)
907 if (action != ODA_FOCUS)
908 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
909 InflateRect( &rc, -1, -1 );
912 /* completely skip the drawing if only focus has changed */
913 if (action == ODA_FOCUS) goto draw_focus;
915 uState = DFCS_BUTTONPUSH;
917 if (style & BS_FLAT)
918 uState |= DFCS_MONO;
919 else if (pushedState)
921 if (get_button_type(style) == BS_DEFPUSHBUTTON )
922 uState |= DFCS_FLAT;
923 else
924 uState |= DFCS_PUSHED;
927 if (state & (BST_CHECKED | BST_INDETERMINATE))
928 uState |= DFCS_CHECKED;
930 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
932 /* draw button label */
933 r = rc;
934 dtFlags = BUTTON_CalcLabelRect(infoPtr, hDC, &r);
936 if (dtFlags == (UINT)-1L)
937 goto cleanup;
939 if (pushedState)
940 OffsetRect(&r, 1, 1);
942 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
944 BUTTON_DrawLabel(infoPtr, hDC, dtFlags, &r);
946 SetTextColor( hDC, oldTxtColor );
948 draw_focus:
949 if (action == ODA_FOCUS || (state & BST_FOCUS))
951 InflateRect( &rc, -2, -2 );
952 DrawFocusRect( hDC, &rc );
955 cleanup:
956 SelectObject( hDC, hOldPen );
957 SelectObject( hDC, hOldBrush );
958 SetBkMode(hDC, oldBkMode);
959 SelectClipRgn( hDC, hrgn );
960 if (hrgn) DeleteObject( hrgn );
961 DeleteObject( hpen );
964 /**********************************************************************
965 * Check Box & Radio Button Functions
968 static void CB_Paint( const BUTTON_INFO *infoPtr, HDC hDC, UINT action )
970 RECT rbox, rtext, client;
971 HBRUSH hBrush;
972 int delta, text_offset, checkBoxWidth, checkBoxHeight;
973 UINT dtFlags;
974 HFONT hFont;
975 LONG state = infoPtr->state;
976 LONG style = GetWindowLongW( infoPtr->hwnd, GWL_STYLE );
977 LONG ex_style = GetWindowLongW( infoPtr->hwnd, GWL_EXSTYLE );
978 HWND parent;
979 HRGN hrgn;
981 if (style & BS_PUSHLIKE)
983 PB_Paint( infoPtr, hDC, action );
984 return;
987 GetClientRect(infoPtr->hwnd, &client);
988 rbox = rtext = client;
990 checkBoxWidth = 12 * GetDeviceCaps( hDC, LOGPIXELSX ) / 96 + 1;
991 checkBoxHeight = 12 * GetDeviceCaps( hDC, LOGPIXELSY ) / 96 + 1;
993 if ((hFont = infoPtr->font)) SelectObject( hDC, hFont );
994 GetCharWidthW( hDC, '0', '0', &text_offset );
995 text_offset /= 2;
997 parent = GetParent(infoPtr->hwnd);
998 if (!parent) parent = infoPtr->hwnd;
999 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)infoPtr->hwnd);
1000 if (!hBrush) /* did the app forget to call defwindowproc ? */
1001 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)infoPtr->hwnd);
1002 hrgn = set_control_clipping( hDC, &client );
1004 if (style & BS_LEFTTEXT || ex_style & WS_EX_RIGHT)
1006 rtext.right -= checkBoxWidth + text_offset;
1007 rbox.left = rbox.right - checkBoxWidth;
1009 else
1011 rtext.left += checkBoxWidth + text_offset;
1012 rbox.right = checkBoxWidth;
1015 /* Since WM_ERASEBKGND does nothing, first prepare background */
1016 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
1017 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
1019 /* Draw label */
1020 client = rtext;
1021 dtFlags = BUTTON_CalcLabelRect(infoPtr, hDC, &rtext);
1023 /* Only adjust rbox when rtext is valid */
1024 if (dtFlags != (UINT)-1L)
1026 rbox.top = rtext.top;
1027 rbox.bottom = rtext.bottom;
1030 /* Draw the check-box bitmap */
1031 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
1033 UINT flags;
1035 if ((get_button_type(style) == BS_RADIOBUTTON) ||
1036 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
1037 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
1038 else flags = DFCS_BUTTONCHECK;
1040 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
1041 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
1043 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
1045 /* rbox must have the correct height */
1046 delta = rbox.bottom - rbox.top - checkBoxHeight;
1048 if (style & BS_TOP) {
1049 if (delta > 0) {
1050 rbox.bottom = rbox.top + checkBoxHeight;
1051 } else {
1052 rbox.top -= -delta/2 + 1;
1053 rbox.bottom = rbox.top + checkBoxHeight;
1055 } else if (style & BS_BOTTOM) {
1056 if (delta > 0) {
1057 rbox.top = rbox.bottom - checkBoxHeight;
1058 } else {
1059 rbox.bottom += -delta/2 + 1;
1060 rbox.top = rbox.bottom - checkBoxHeight;
1062 } else { /* Default */
1063 if (delta > 0) {
1064 int ofs = (delta / 2);
1065 rbox.bottom -= ofs + 1;
1066 rbox.top = rbox.bottom - checkBoxHeight;
1067 } else if (delta < 0) {
1068 int ofs = (-delta / 2);
1069 rbox.top -= ofs + 1;
1070 rbox.bottom = rbox.top + checkBoxHeight;
1074 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
1077 if (dtFlags == (UINT)-1L) /* Noting to draw */
1078 return;
1080 if (action == ODA_DRAWENTIRE)
1081 BUTTON_DrawLabel(infoPtr, hDC, dtFlags, &rtext);
1083 /* ... and focus */
1084 if (action == ODA_FOCUS || (state & BST_FOCUS))
1086 rtext.left--;
1087 rtext.right++;
1088 IntersectRect(&rtext, &rtext, &client);
1089 DrawFocusRect( hDC, &rtext );
1091 SelectClipRgn( hDC, hrgn );
1092 if (hrgn) DeleteObject( hrgn );
1096 /**********************************************************************
1097 * BUTTON_CheckAutoRadioButton
1099 * hwnd is checked, uncheck every other auto radio button in group
1101 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
1103 HWND parent, sibling, start;
1105 parent = GetParent(hwnd);
1106 /* make sure that starting control is not disabled or invisible */
1107 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1110 if (!sibling) break;
1111 if ((hwnd != sibling) &&
1112 ((GetWindowLongW( sibling, GWL_STYLE) & BS_TYPEMASK) == BS_AUTORADIOBUTTON))
1113 SendMessageW( sibling, BM_SETCHECK, BST_UNCHECKED, 0 );
1114 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1115 } while (sibling != start);
1119 /**********************************************************************
1120 * Group Box Functions
1123 static void GB_Paint( const BUTTON_INFO *infoPtr, HDC hDC, UINT action )
1125 RECT rc, rcFrame;
1126 HBRUSH hbr;
1127 HFONT hFont;
1128 UINT dtFlags;
1129 TEXTMETRICW tm;
1130 LONG style = GetWindowLongW( infoPtr->hwnd, GWL_STYLE );
1131 HWND parent;
1132 HRGN hrgn;
1134 if ((hFont = infoPtr->font)) SelectObject( hDC, hFont );
1135 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1136 parent = GetParent(infoPtr->hwnd);
1137 if (!parent) parent = infoPtr->hwnd;
1138 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)infoPtr->hwnd);
1139 if (!hbr) /* did the app forget to call defwindowproc ? */
1140 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)infoPtr->hwnd);
1141 GetClientRect( infoPtr->hwnd, &rc);
1142 rcFrame = rc;
1143 hrgn = set_control_clipping( hDC, &rc );
1145 GetTextMetricsW (hDC, &tm);
1146 rcFrame.top += (tm.tmHeight / 2) - 1;
1147 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1149 InflateRect(&rc, -7, 1);
1150 dtFlags = BUTTON_CalcLabelRect(infoPtr, hDC, &rc);
1152 if (dtFlags != (UINT)-1)
1154 /* Because buttons have CS_PARENTDC class style, there is a chance
1155 * that label will be drawn out of client rect.
1156 * But Windows doesn't clip label's rect, so do I.
1159 /* There is 1-pixel margin at the left, right, and bottom */
1160 rc.left--; rc.right++; rc.bottom++;
1161 FillRect(hDC, &rc, hbr);
1162 rc.left++; rc.right--; rc.bottom--;
1164 BUTTON_DrawLabel(infoPtr, hDC, dtFlags, &rc);
1166 SelectClipRgn( hDC, hrgn );
1167 if (hrgn) DeleteObject( hrgn );
1171 /**********************************************************************
1172 * User Button Functions
1175 static void UB_Paint( const BUTTON_INFO *infoPtr, HDC hDC, UINT action )
1177 RECT rc;
1178 HBRUSH hBrush;
1179 HFONT hFont;
1180 LONG state = infoPtr->state;
1181 HWND parent;
1183 GetClientRect( infoPtr->hwnd, &rc);
1185 if ((hFont = infoPtr->font)) SelectObject( hDC, hFont );
1187 parent = GetParent(infoPtr->hwnd);
1188 if (!parent) parent = infoPtr->hwnd;
1189 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)infoPtr->hwnd);
1190 if (!hBrush) /* did the app forget to call defwindowproc ? */
1191 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)infoPtr->hwnd);
1193 FillRect( hDC, &rc, hBrush );
1194 if (action == ODA_FOCUS || (state & BST_FOCUS))
1195 DrawFocusRect( hDC, &rc );
1197 switch (action)
1199 case ODA_FOCUS:
1200 BUTTON_NOTIFY_PARENT( infoPtr->hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1201 break;
1203 case ODA_SELECT:
1204 BUTTON_NOTIFY_PARENT( infoPtr->hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1205 break;
1207 default:
1208 break;
1213 /**********************************************************************
1214 * Ownerdrawn Button Functions
1217 static void OB_Paint( const BUTTON_INFO *infoPtr, HDC hDC, UINT action )
1219 LONG state = infoPtr->state;
1220 DRAWITEMSTRUCT dis;
1221 LONG_PTR id = GetWindowLongPtrW( infoPtr->hwnd, GWLP_ID );
1222 HWND parent;
1223 HFONT hFont;
1224 HRGN hrgn;
1226 dis.CtlType = ODT_BUTTON;
1227 dis.CtlID = id;
1228 dis.itemID = 0;
1229 dis.itemAction = action;
1230 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1231 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1232 (IsWindowEnabled(infoPtr->hwnd) ? 0: ODS_DISABLED);
1233 dis.hwndItem = infoPtr->hwnd;
1234 dis.hDC = hDC;
1235 dis.itemData = 0;
1236 GetClientRect( infoPtr->hwnd, &dis.rcItem );
1238 if ((hFont = infoPtr->font)) SelectObject( hDC, hFont );
1239 parent = GetParent(infoPtr->hwnd);
1240 if (!parent) parent = infoPtr->hwnd;
1241 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)infoPtr->hwnd );
1243 hrgn = set_control_clipping( hDC, &dis.rcItem );
1245 SendMessageW( GetParent(infoPtr->hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1246 SelectClipRgn( hDC, hrgn );
1247 if (hrgn) DeleteObject( hrgn );
1250 static void PB_ThemedPaint(HTHEME theme, const BUTTON_INFO *infoPtr, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused)
1252 static const int states[] = { PBS_NORMAL, PBS_DISABLED, PBS_HOT, PBS_PRESSED, PBS_DEFAULTED };
1254 RECT bgRect, textRect;
1255 HFONT font = infoPtr->font;
1256 HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
1257 int state = states[ drawState ];
1258 WCHAR *text = get_button_text(infoPtr);
1260 GetClientRect(infoPtr->hwnd, &bgRect);
1261 GetThemeBackgroundContentRect(theme, hDC, BP_PUSHBUTTON, state, &bgRect, &textRect);
1263 if (IsThemeBackgroundPartiallyTransparent(theme, BP_PUSHBUTTON, state))
1264 DrawThemeParentBackground(infoPtr->hwnd, hDC, NULL);
1265 DrawThemeBackground(theme, hDC, BP_PUSHBUTTON, state, &bgRect, NULL);
1266 if (text)
1268 DrawThemeText(theme, hDC, BP_PUSHBUTTON, state, text, lstrlenW(text), dtFlags, 0, &textRect);
1269 heap_free(text);
1272 if (focused)
1274 MARGINS margins;
1275 RECT focusRect = bgRect;
1277 GetThemeMargins(theme, hDC, BP_PUSHBUTTON, state, TMT_CONTENTMARGINS, NULL, &margins);
1279 focusRect.left += margins.cxLeftWidth;
1280 focusRect.top += margins.cyTopHeight;
1281 focusRect.right -= margins.cxRightWidth;
1282 focusRect.bottom -= margins.cyBottomHeight;
1284 DrawFocusRect( hDC, &focusRect );
1287 if (hPrevFont) SelectObject(hDC, hPrevFont);
1290 static void CB_ThemedPaint(HTHEME theme, const BUTTON_INFO *infoPtr, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused)
1292 static const int cb_states[3][5] =
1294 { CBS_UNCHECKEDNORMAL, CBS_UNCHECKEDDISABLED, CBS_UNCHECKEDHOT, CBS_UNCHECKEDPRESSED, CBS_UNCHECKEDNORMAL },
1295 { CBS_CHECKEDNORMAL, CBS_CHECKEDDISABLED, CBS_CHECKEDHOT, CBS_CHECKEDPRESSED, CBS_CHECKEDNORMAL },
1296 { CBS_MIXEDNORMAL, CBS_MIXEDDISABLED, CBS_MIXEDHOT, CBS_MIXEDPRESSED, CBS_MIXEDNORMAL }
1299 static const int rb_states[2][5] =
1301 { RBS_UNCHECKEDNORMAL, RBS_UNCHECKEDDISABLED, RBS_UNCHECKEDHOT, RBS_UNCHECKEDPRESSED, RBS_UNCHECKEDNORMAL },
1302 { RBS_CHECKEDNORMAL, RBS_CHECKEDDISABLED, RBS_CHECKEDHOT, RBS_CHECKEDPRESSED, RBS_CHECKEDNORMAL }
1305 SIZE sz;
1306 RECT bgRect, textRect;
1307 HFONT font, hPrevFont = NULL;
1308 int checkState = infoPtr->state & 3;
1309 DWORD dwStyle = GetWindowLongW(infoPtr->hwnd, GWL_STYLE);
1310 UINT btn_type = get_button_type( dwStyle );
1311 int part = (btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON) ? BP_RADIOBUTTON : BP_CHECKBOX;
1312 int state = (part == BP_CHECKBOX)
1313 ? cb_states[ checkState ][ drawState ]
1314 : rb_states[ checkState ][ drawState ];
1315 WCHAR *text = get_button_text(infoPtr);
1316 LOGFONTW lf;
1317 BOOL created_font = FALSE;
1319 HRESULT hr = GetThemeFont(theme, hDC, part, state, TMT_FONT, &lf);
1320 if (SUCCEEDED(hr)) {
1321 font = CreateFontIndirectW(&lf);
1322 if (!font)
1323 TRACE("Failed to create font\n");
1324 else {
1325 TRACE("font = %s\n", debugstr_w(lf.lfFaceName));
1326 hPrevFont = SelectObject(hDC, font);
1327 created_font = TRUE;
1329 } else {
1330 font = (HFONT)SendMessageW(infoPtr->hwnd, WM_GETFONT, 0, 0);
1331 hPrevFont = SelectObject(hDC, font);
1334 if (FAILED(GetThemePartSize(theme, hDC, part, state, NULL, TS_DRAW, &sz)))
1335 sz.cx = sz.cy = 13;
1337 GetClientRect(infoPtr->hwnd, &bgRect);
1338 GetThemeBackgroundContentRect(theme, hDC, part, state, &bgRect, &textRect);
1340 if (dtFlags & DT_SINGLELINE) /* Center the checkbox / radio button to the text. */
1341 bgRect.top = bgRect.top + (textRect.bottom - textRect.top - sz.cy) / 2;
1343 /* adjust for the check/radio marker */
1344 bgRect.bottom = bgRect.top + sz.cy;
1345 bgRect.right = bgRect.left + sz.cx;
1346 textRect.left = bgRect.right + 6;
1348 DrawThemeParentBackground(infoPtr->hwnd, hDC, NULL);
1350 DrawThemeBackground(theme, hDC, part, state, &bgRect, NULL);
1351 if (text)
1353 DrawThemeText(theme, hDC, part, state, text, lstrlenW(text), dtFlags, 0, &textRect);
1355 if (focused)
1357 RECT focusRect;
1359 focusRect = textRect;
1361 DrawTextW(hDC, text, lstrlenW(text), &focusRect, dtFlags | DT_CALCRECT);
1363 if (focusRect.right < textRect.right) focusRect.right++;
1364 focusRect.bottom = textRect.bottom;
1366 DrawFocusRect( hDC, &focusRect );
1369 heap_free(text);
1372 if (created_font) DeleteObject(font);
1373 if (hPrevFont) SelectObject(hDC, hPrevFont);
1376 static void GB_ThemedPaint(HTHEME theme, const BUTTON_INFO *infoPtr, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused)
1378 static const int states[] = { GBS_NORMAL, GBS_DISABLED, GBS_NORMAL, GBS_NORMAL, GBS_NORMAL };
1380 RECT bgRect, textRect, contentRect;
1381 int state = states[ drawState ];
1382 WCHAR *text = get_button_text(infoPtr);
1383 LOGFONTW lf;
1384 HFONT font, hPrevFont = NULL;
1385 BOOL created_font = FALSE;
1387 HRESULT hr = GetThemeFont(theme, hDC, BP_GROUPBOX, state, TMT_FONT, &lf);
1388 if (SUCCEEDED(hr)) {
1389 font = CreateFontIndirectW(&lf);
1390 if (!font)
1391 TRACE("Failed to create font\n");
1392 else {
1393 hPrevFont = SelectObject(hDC, font);
1394 created_font = TRUE;
1396 } else {
1397 font = (HFONT)SendMessageW(infoPtr->hwnd, WM_GETFONT, 0, 0);
1398 hPrevFont = SelectObject(hDC, font);
1401 GetClientRect(infoPtr->hwnd, &bgRect);
1402 textRect = bgRect;
1404 if (text)
1406 SIZE textExtent;
1407 GetTextExtentPoint32W(hDC, text, lstrlenW(text), &textExtent);
1408 bgRect.top += (textExtent.cy / 2);
1409 textRect.left += 10;
1410 textRect.bottom = textRect.top + textExtent.cy;
1411 textRect.right = textRect.left + textExtent.cx + 4;
1413 ExcludeClipRect(hDC, textRect.left, textRect.top, textRect.right, textRect.bottom);
1416 GetThemeBackgroundContentRect(theme, hDC, BP_GROUPBOX, state, &bgRect, &contentRect);
1417 ExcludeClipRect(hDC, contentRect.left, contentRect.top, contentRect.right, contentRect.bottom);
1419 if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
1420 DrawThemeParentBackground(infoPtr->hwnd, hDC, NULL);
1421 DrawThemeBackground(theme, hDC, BP_GROUPBOX, state, &bgRect, NULL);
1423 SelectClipRgn(hDC, NULL);
1425 if (text)
1427 InflateRect(&textRect, -2, 0);
1428 DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
1429 heap_free(text);
1432 if (created_font) DeleteObject(font);
1433 if (hPrevFont) SelectObject(hDC, hPrevFont);
1436 void BUTTON_Register(void)
1438 WNDCLASSW wndClass;
1440 memset(&wndClass, 0, sizeof(wndClass));
1441 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC;
1442 wndClass.lpfnWndProc = BUTTON_WindowProc;
1443 wndClass.cbClsExtra = 0;
1444 wndClass.cbWndExtra = sizeof(BUTTON_INFO *);
1445 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
1446 wndClass.hbrBackground = NULL;
1447 wndClass.lpszClassName = WC_BUTTONW;
1448 RegisterClassW(&wndClass);