1 /* File: button.c -- Button type widgets
3 * Copyright (C) 1993 Johannes Ruscheinski
4 * Copyright (C) 1993 David Metcalfe
5 * Copyright (C) 1994 Alexandre Julliard
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
23 * This code was audited for completeness against the documented features
24 * of Comctl32.dll version 6.0 on Oct. 3, 2004, by Dimitrie O. Paun.
26 * Unless otherwise noted, we believe this code to be complete, as per
27 * the specification mentioned above.
28 * If you discover missing features, or bugs, please note them below.
32 * - BS_NOTIFY: is it complete?
33 * - BS_RIGHTBUTTON: same as BS_LEFTTEXT
36 * - WM_CHAR: Checks a (manual or automatic) check box on '+' or '=', clears it on '-' key.
37 * - WM_SETFOCUS: For (manual or automatic) radio buttons, send the parent window BN_CLICKED
38 * - WM_NCCREATE: Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
49 * - BN_PUSHED/BN_HILITE
50 * + BN_KILLFOCUS: is it OK?
52 * + BN_SETFOCUS: is it OK?
53 * - BN_UNPUSHED/BN_UNHILITE
56 * Structures/Macros/Definitions
59 * - Button_GetIdealSize
60 * - Button_GetImageList
61 * - Button_GetTextMargin
62 * - Button_SetImageList
63 * - Button_SetTextMargin
77 #include "user_private.h"
78 #include "wine/debug.h"
80 WINE_DEFAULT_DEBUG_CHANNEL(button);
82 /* GetWindowLong offsets for window extra information */
83 #define STATE_GWL_OFFSET 0
84 #define HFONT_GWL_OFFSET (sizeof(LONG))
85 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
86 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
88 /* undocumented flags */
89 #define BUTTON_NSTATES 0x0F
90 #define BUTTON_BTNPRESSED 0x40
91 #define BUTTON_UNKNOWN2 0x20
92 #define BUTTON_UNKNOWN3 0x10
94 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
95 do { /* Notify parent which has created this button control */ \
96 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
97 SendMessageW(GetParent(hWnd), WM_COMMAND, \
98 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
102 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
103 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
104 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
105 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
106 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
107 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
108 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
110 #define MAX_BTN_TYPE 16
112 static const WORD maxCheckState[MAX_BTN_TYPE] =
114 BST_UNCHECKED, /* BS_PUSHBUTTON */
115 BST_UNCHECKED, /* BS_DEFPUSHBUTTON */
116 BST_CHECKED, /* BS_CHECKBOX */
117 BST_CHECKED, /* BS_AUTOCHECKBOX */
118 BST_CHECKED, /* BS_RADIOBUTTON */
119 BST_INDETERMINATE, /* BS_3STATE */
120 BST_INDETERMINATE, /* BS_AUTO3STATE */
121 BST_UNCHECKED, /* BS_GROUPBOX */
122 BST_UNCHECKED, /* BS_USERBUTTON */
123 BST_CHECKED, /* BS_AUTORADIOBUTTON */
124 BST_UNCHECKED, /* BS_PUSHBOX */
125 BST_UNCHECKED /* BS_OWNERDRAW */
128 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
130 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
132 PB_Paint, /* BS_PUSHBUTTON */
133 PB_Paint, /* BS_DEFPUSHBUTTON */
134 CB_Paint, /* BS_CHECKBOX */
135 CB_Paint, /* BS_AUTOCHECKBOX */
136 CB_Paint, /* BS_RADIOBUTTON */
137 CB_Paint, /* BS_3STATE */
138 CB_Paint, /* BS_AUTO3STATE */
139 GB_Paint, /* BS_GROUPBOX */
140 UB_Paint, /* BS_USERBUTTON */
141 CB_Paint, /* BS_AUTORADIOBUTTON */
142 NULL, /* BS_PUSHBOX */
143 OB_Paint /* BS_OWNERDRAW */
146 static HBITMAP hbitmapCheckBoxes = 0;
147 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
150 /*********************************************************************
151 * button class descriptor
153 static const WCHAR buttonW[] = {'B','u','t','t','o','n',0};
154 const struct builtin_class_descr BUTTON_builtin_class =
157 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
158 WINPROC_BUTTON, /* proc */
159 NB_EXTRA_BYTES, /* extra */
160 IDC_ARROW, /* cursor */
165 static inline LONG get_button_state( HWND hwnd )
167 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
170 static inline void set_button_state( HWND hwnd, LONG state )
172 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
175 static inline HFONT get_button_font( HWND hwnd )
177 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
180 static inline void set_button_font( HWND hwnd, HFONT font )
182 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
185 static inline UINT get_button_type( LONG window_style )
187 return (window_style & BS_TYPEMASK);
190 /* paint a button of any type */
191 static inline void paint_button( HWND hwnd, LONG style, UINT action )
193 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
195 HDC hdc = GetDC( hwnd );
196 btnPaintFunc[style]( hwnd, hdc, action );
197 ReleaseDC( hwnd, hdc );
201 /* retrieve the button text; returned buffer must be freed by caller */
202 static inline WCHAR *get_button_text( HWND hwnd )
205 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
206 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
210 /***********************************************************************
211 * ButtonWndProc_common
213 LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL unicode )
217 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
218 UINT btn_type = get_button_type( style );
222 if (!IsWindow( hWnd )) return 0;
224 pt.x = (short)LOWORD(lParam);
225 pt.y = (short)HIWORD(lParam);
233 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
234 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
236 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
237 case BS_GROUPBOX: return DLGC_STATIC;
238 default: return DLGC_BUTTON;
242 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
246 if (!hbitmapCheckBoxes)
249 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
250 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
251 checkBoxWidth = bmp.bmWidth / 4;
252 checkBoxHeight = bmp.bmHeight / 3;
254 if (btn_type >= MAX_BTN_TYPE)
255 return -1; /* abort */
257 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
258 if (btn_type == BS_USERBUTTON )
260 style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
261 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
263 set_button_state( hWnd, BST_UNCHECKED );
267 if (btn_type == BS_OWNERDRAW)
269 HDC hdc = (HDC)wParam;
272 HWND parent = GetParent(hWnd);
273 if (!parent) parent = hWnd;
274 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
275 if (!hBrush) /* did the app forget to call defwindowproc ? */
276 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
277 (WPARAM)hdc, (LPARAM)hWnd);
278 GetClientRect(hWnd, &rc);
279 FillRect(hdc, &rc, hBrush);
285 if (btnPaintFunc[btn_type])
288 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
289 int nOldMode = SetBkMode( hdc, OPAQUE );
290 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
291 SetBkMode(hdc, nOldMode); /* reset painting mode */
292 if( !wParam ) EndPaint( hWnd, &ps );
297 if (wParam == VK_SPACE)
299 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
300 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
305 case WM_LBUTTONDBLCLK:
306 if(style & BS_NOTIFY ||
307 btn_type == BS_RADIOBUTTON ||
308 btn_type == BS_USERBUTTON ||
309 btn_type == BS_OWNERDRAW)
311 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
318 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
319 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
323 if (wParam != VK_SPACE)
327 state = get_button_state( hWnd );
328 if (!(state & BUTTON_BTNPRESSED)) break;
329 state &= BUTTON_NSTATES;
330 set_button_state( hWnd, state );
331 if (!(state & BST_PUSHED))
336 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
338 GetClientRect( hWnd, &rect );
339 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
341 state = get_button_state( hWnd );
344 case BS_AUTOCHECKBOX:
345 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
347 case BS_AUTORADIOBUTTON:
348 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
351 SendMessageW( hWnd, BM_SETCHECK,
352 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
355 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
359 case WM_CAPTURECHANGED:
360 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
361 state = get_button_state( hWnd );
362 if (state & BUTTON_BTNPRESSED)
364 state &= BUTTON_NSTATES;
365 set_button_state( hWnd, state );
366 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
371 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
373 GetClientRect( hWnd, &rect );
374 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
380 /* Clear an old text here as Windows does */
381 HDC hdc = GetDC(hWnd);
384 HWND parent = GetParent(hWnd);
386 if (!parent) parent = hWnd;
387 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
388 (WPARAM)hdc, (LPARAM)hWnd);
389 if (!hbrush) /* did the app forget to call DefWindowProc ? */
390 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
391 (WPARAM)hdc, (LPARAM)hWnd);
393 GetClientRect(hWnd, &client);
395 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
396 /* Clip by client rect bounds */
397 if (rc.right > client.right) rc.right = client.right;
398 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
399 FillRect(hdc, &rc, hbrush);
400 ReleaseDC(hWnd, hdc);
402 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
403 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
404 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
405 InvalidateRect( hWnd, NULL, TRUE );
407 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
408 return 1; /* success. FIXME: check text length */
412 set_button_font( hWnd, (HFONT)wParam );
413 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
417 return (LRESULT)get_button_font( hWnd );
420 TRACE("WM_SETFOCUS %p\n",hWnd);
421 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
422 paint_button( hWnd, btn_type, ODA_FOCUS );
423 if (style & BS_NOTIFY)
424 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
428 TRACE("WM_KILLFOCUS %p\n",hWnd);
429 state = get_button_state( hWnd );
430 set_button_state( hWnd, state & ~BST_FOCUS );
431 paint_button( hWnd, btn_type, ODA_FOCUS );
433 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
435 if (style & BS_NOTIFY)
436 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
438 InvalidateRect( hWnd, NULL, FALSE );
441 case WM_SYSCOLORCHANGE:
442 InvalidateRect( hWnd, NULL, FALSE );
446 if ((wParam & BS_TYPEMASK) >= MAX_BTN_TYPE) break;
447 btn_type = wParam & BS_TYPEMASK;
448 style = (style & ~BS_TYPEMASK) | btn_type;
449 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
451 /* Only redraw if lParam flag is set.*/
453 InvalidateRect( hWnd, NULL, TRUE );
458 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
459 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
463 /* Check that image format matches button style */
464 switch (style & (BS_BITMAP|BS_ICON))
467 if (wParam != IMAGE_BITMAP) return 0;
470 if (wParam != IMAGE_ICON) return 0;
475 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
476 InvalidateRect( hWnd, NULL, FALSE );
477 return (LRESULT)oldHbitmap;
480 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
483 return get_button_state( hWnd ) & 3;
486 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
487 state = get_button_state( hWnd );
488 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
490 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
491 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
493 if ((state & 3) != wParam)
495 set_button_state( hWnd, (state & ~3) | wParam );
496 paint_button( hWnd, btn_type, ODA_SELECT );
498 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BST_CHECKED) && (style & WS_CHILD))
499 BUTTON_CheckAutoRadioButton( hWnd );
503 return get_button_state( hWnd );
506 state = get_button_state( hWnd );
508 set_button_state( hWnd, state | BST_PUSHED );
510 set_button_state( hWnd, state & ~BST_PUSHED );
512 paint_button( hWnd, btn_type, ODA_SELECT );
516 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
519 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
520 DefWindowProcA(hWnd, uMsg, wParam, lParam);
525 /**********************************************************************
526 * Convert button styles to flags used by DrawText.
528 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
530 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
532 /* "Convert" pushlike buttons to pushbuttons */
533 if (style & BS_PUSHLIKE)
534 style &= ~BS_TYPEMASK;
536 if (!(style & BS_MULTILINE))
537 dtStyle |= DT_SINGLELINE;
539 dtStyle |= DT_WORDBREAK;
541 switch (style & BS_CENTER)
543 case BS_LEFT: /* DT_LEFT is 0 */ break;
544 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
545 case BS_CENTER: dtStyle |= DT_CENTER; break;
547 /* Pushbutton's text is centered by default */
548 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
549 /* all other flavours have left aligned text */
552 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
554 /* DrawText ignores vertical alignment for multiline text,
555 * but we use these flags to align label manually.
557 if (get_button_type(style) != BS_GROUPBOX)
559 switch (style & BS_VCENTER)
561 case BS_TOP: /* DT_TOP is 0 */ break;
562 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
563 case BS_VCENTER: /* fall through */
564 default: dtStyle |= DT_VCENTER; break;
568 /* GroupBox's text is always single line and is top aligned. */
569 dtStyle |= DT_SINGLELINE;
574 /**********************************************************************
575 * BUTTON_CalcLabelRect
577 * Calculates label's rectangle depending on button style.
579 * Returns flags to be passed to DrawText.
580 * Calculated rectangle doesn't take into account button state
581 * (pushed, etc.). If there is nothing to draw (no text/image) output
582 * rectangle is empty, and return value is (UINT)-1.
584 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
586 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
587 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
591 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
595 /* Calculate label rectangle according to label type */
596 switch (style & (BS_ICON|BS_BITMAP))
599 if (!(text = get_button_text( hwnd ))) goto empty_rect;
602 HeapFree( GetProcessHeap(), 0, text );
605 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
606 HeapFree( GetProcessHeap(), 0, text );
610 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
613 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
615 r.right = r.left + bm.bmWidth;
616 r.bottom = r.top + bm.bmHeight;
618 DeleteObject(iconInfo.hbmColor);
619 DeleteObject(iconInfo.hbmMask);
623 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
626 r.right = r.left + bm.bmWidth;
627 r.bottom = r.top + bm.bmHeight;
637 /* Position label inside bounding rectangle according to
638 * alignment flags. (calculated rect is always left-top aligned).
639 * If label is aligned to any side - shift label in opposite
640 * direction to leave extra space for focus rectangle.
642 switch (dtStyle & (DT_CENTER|DT_RIGHT))
644 case DT_LEFT: r.left++; r.right++; break;
645 case DT_CENTER: n = r.right - r.left;
646 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
647 r.right = r.left + n; break;
648 case DT_RIGHT: n = r.right - r.left;
649 r.right = rc->right - 1;
650 r.left = r.right - n;
654 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
656 case DT_TOP: r.top++; r.bottom++; break;
657 case DT_VCENTER: n = r.bottom - r.top;
658 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
659 r.bottom = r.top + n; break;
660 case DT_BOTTOM: n = r.bottom - r.top;
661 r.bottom = rc->bottom - 1;
662 r.top = r.bottom - n;
671 /**********************************************************************
672 * BUTTON_DrawTextCallback
674 * Callback function used by DrawStateW function.
676 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
684 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
689 /**********************************************************************
692 * Common function for drawing button label.
694 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
696 DRAWSTATEPROC lpOutputProc = NULL;
700 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
701 LONG state = get_button_state( hwnd );
702 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
705 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
706 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
707 * I don't have Win31 on hand to verify that, so I leave it as is.
710 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
712 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
716 switch (style & (BS_ICON|BS_BITMAP))
719 /* DST_COMPLEX -- is 0 */
720 lpOutputProc = BUTTON_DrawTextCallback;
721 if (!(text = get_button_text( hwnd ))) return;
728 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
733 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
740 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
741 rc->right - rc->left, rc->bottom - rc->top, flags);
742 HeapFree( GetProcessHeap(), 0, text );
745 /**********************************************************************
746 * Push Button Functions
748 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
751 UINT dtFlags, uState;
755 COLORREF oldTxtColor;
757 LONG state = get_button_state( hwnd );
758 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
759 BOOL pushedState = (state & BST_PUSHED);
763 GetClientRect( hwnd, &rc );
765 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
766 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
767 parent = GetParent(hwnd);
768 if (!parent) parent = hwnd;
769 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
771 hrgn = set_control_clipping( hDC, &rc );
773 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
774 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
775 oldBkMode = SetBkMode(hDC, TRANSPARENT);
777 if (get_button_type(style) == BS_DEFPUSHBUTTON)
779 if (action != ODA_FOCUS)
780 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
781 InflateRect( &rc, -1, -1 );
784 /* completely skip the drawing if only focus has changed */
785 if (action == ODA_FOCUS) goto draw_focus;
787 uState = DFCS_BUTTONPUSH;
791 else if (pushedState)
793 if (get_button_type(style) == BS_DEFPUSHBUTTON )
796 uState |= DFCS_PUSHED;
799 if (state & (BST_CHECKED | BST_INDETERMINATE))
800 uState |= DFCS_CHECKED;
802 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
804 /* draw button label */
806 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
808 if (dtFlags == (UINT)-1L)
812 OffsetRect(&r, 1, 1);
814 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
816 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
818 SetTextColor( hDC, oldTxtColor );
821 if (action == ODA_FOCUS || (state & BST_FOCUS))
823 InflateRect( &rc, -2, -2 );
824 DrawFocusRect( hDC, &rc );
828 SelectObject( hDC, hOldPen );
829 SelectObject( hDC, hOldBrush );
830 SetBkMode(hDC, oldBkMode);
831 SelectClipRgn( hDC, hrgn );
832 if (hrgn) DeleteObject( hrgn );
835 /**********************************************************************
836 * Check Box & Radio Button Functions
839 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
841 RECT rbox, rtext, client;
846 LONG state = get_button_state( hwnd );
847 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
851 if (style & BS_PUSHLIKE)
853 PB_Paint( hwnd, hDC, action );
857 GetClientRect(hwnd, &client);
858 rbox = rtext = client;
860 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
862 parent = GetParent(hwnd);
863 if (!parent) parent = hwnd;
864 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
865 (WPARAM)hDC, (LPARAM)hwnd);
866 if (!hBrush) /* did the app forget to call defwindowproc ? */
867 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
868 (WPARAM)hDC, (LPARAM)hwnd );
869 hrgn = set_control_clipping( hDC, &client );
871 if (style & BS_LEFTTEXT)
873 /* magic +4 is what CTL3D expects */
875 rtext.right -= checkBoxWidth + 4;
876 rbox.left = rbox.right - checkBoxWidth;
880 rtext.left += checkBoxWidth + 4;
881 rbox.right = checkBoxWidth;
884 /* Since WM_ERASEBKGND does nothing, first prepare background */
885 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
886 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
890 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
892 /* Only adjust rbox when rtext is valid */
893 if (dtFlags != (UINT)-1L)
895 rbox.top = rtext.top;
896 rbox.bottom = rtext.bottom;
899 /* Draw the check-box bitmap */
900 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
904 if ((get_button_type(style) == BS_RADIOBUTTON) ||
905 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
906 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
907 else flags = DFCS_BUTTONCHECK;
909 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
910 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
912 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
914 /* rbox must have the correct height */
915 delta = rbox.bottom - rbox.top - checkBoxHeight;
917 if (style & BS_TOP) {
919 rbox.bottom = rbox.top + checkBoxHeight;
921 rbox.top -= -delta/2 + 1;
922 rbox.bottom = rbox.top + checkBoxHeight;
924 } else if (style & BS_BOTTOM) {
926 rbox.top = rbox.bottom - checkBoxHeight;
928 rbox.bottom += -delta/2 + 1;
929 rbox.top = rbox.bottom - checkBoxHeight;
931 } else { /* Default */
933 int ofs = (delta / 2);
934 rbox.bottom -= ofs + 1;
935 rbox.top = rbox.bottom - checkBoxHeight;
936 } else if (delta < 0) {
937 int ofs = (-delta / 2);
939 rbox.bottom = rbox.top + checkBoxHeight;
943 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
946 if (dtFlags == (UINT)-1L) /* Noting to draw */
949 if (action == ODA_DRAWENTIRE)
950 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
953 if (action == ODA_FOCUS || (state & BST_FOCUS))
957 IntersectRect(&rtext, &rtext, &client);
958 DrawFocusRect( hDC, &rtext );
960 SelectClipRgn( hDC, hrgn );
961 if (hrgn) DeleteObject( hrgn );
965 /**********************************************************************
966 * BUTTON_CheckAutoRadioButton
968 * hwnd is checked, uncheck every other auto radio button in group
970 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
972 HWND parent, sibling, start;
974 parent = GetParent(hwnd);
975 /* make sure that starting control is not disabled or invisible */
976 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
980 if ((hwnd != sibling) &&
981 ((GetWindowLongW( sibling, GWL_STYLE) & BS_TYPEMASK) == BS_AUTORADIOBUTTON))
982 SendMessageW( sibling, BM_SETCHECK, BST_UNCHECKED, 0 );
983 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
984 } while (sibling != start);
988 /**********************************************************************
989 * Group Box Functions
992 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
999 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1003 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1004 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1005 parent = GetParent(hwnd);
1006 if (!parent) parent = hwnd;
1007 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1008 if (!hbr) /* did the app forget to call defwindowproc ? */
1009 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1010 (WPARAM)hDC, (LPARAM)hwnd);
1011 GetClientRect( hwnd, &rc);
1013 hrgn = set_control_clipping( hDC, &rc );
1015 GetTextMetricsW (hDC, &tm);
1016 rcFrame.top += (tm.tmHeight / 2) - 1;
1017 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1019 InflateRect(&rc, -7, 1);
1020 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1022 if (dtFlags != (UINT)-1)
1024 /* Because buttons have CS_PARENTDC class style, there is a chance
1025 * that label will be drawn out of client rect.
1026 * But Windows doesn't clip label's rect, so do I.
1029 /* There is 1-pixel margin at the left, right, and bottom */
1030 rc.left--; rc.right++; rc.bottom++;
1031 FillRect(hDC, &rc, hbr);
1032 rc.left++; rc.right--; rc.bottom--;
1034 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1036 SelectClipRgn( hDC, hrgn );
1037 if (hrgn) DeleteObject( hrgn );
1041 /**********************************************************************
1042 * User Button Functions
1045 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1050 LONG state = get_button_state( hwnd );
1053 GetClientRect( hwnd, &rc);
1055 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1057 parent = GetParent(hwnd);
1058 if (!parent) parent = hwnd;
1059 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1060 if (!hBrush) /* did the app forget to call defwindowproc ? */
1061 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1062 (WPARAM)hDC, (LPARAM)hwnd);
1064 FillRect( hDC, &rc, hBrush );
1065 if (action == ODA_FOCUS || (state & BST_FOCUS))
1066 DrawFocusRect( hDC, &rc );
1071 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1075 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1079 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1085 /**********************************************************************
1086 * Ownerdrawn Button Functions
1089 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1091 LONG state = get_button_state( hwnd );
1093 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1095 HFONT hFont, hPrevFont = 0;
1098 dis.CtlType = ODT_BUTTON;
1101 dis.itemAction = action;
1102 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1103 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1104 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1105 dis.hwndItem = hwnd;
1108 GetClientRect( hwnd, &dis.rcItem );
1110 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1111 parent = GetParent(hwnd);
1112 if (!parent) parent = hwnd;
1113 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1115 hrgn = set_control_clipping( hDC, &dis.rcItem );
1117 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1118 if (hPrevFont) SelectObject(hDC, hPrevFont);
1119 SelectClipRgn( hDC, hrgn );
1120 if (hrgn) DeleteObject( hrgn );