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
67 #include <wine/debug.h>
68 WINE_DEFAULT_DEBUG_CHANNEL(button
);
70 /* GetWindowLong offsets for window extra information */
71 #define STATE_GWL_OFFSET 0
72 #define HFONT_GWL_OFFSET (sizeof(LONG))
73 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
74 #define UISTATE_GWL_OFFSET (HIMAGE_GWL_OFFSET+sizeof(HFONT))
75 #define NB_EXTRA_BYTES (UISTATE_GWL_OFFSET+sizeof(LONG))
77 /* undocumented flags */
78 #define BUTTON_NSTATES 0x0F
79 #define BUTTON_BTNPRESSED 0x40
80 #define BUTTON_UNKNOWN2 0x20
81 #define BUTTON_UNKNOWN3 0x10
83 #define BUTTON_BMCLICK 0x100 // ReactOS Need to up to wine!
86 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
87 do { /* Notify parent which has created this button control */ \
88 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
89 SendMessageW(GetParent(hWnd), WM_COMMAND, \
90 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
94 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
95 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
96 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
97 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
98 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
99 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
100 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
102 #define MAX_BTN_TYPE 16
104 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
106 BST_UNCHECKED
, /* BS_PUSHBUTTON */
107 BST_UNCHECKED
, /* BS_DEFPUSHBUTTON */
108 BST_CHECKED
, /* BS_CHECKBOX */
109 BST_CHECKED
, /* BS_AUTOCHECKBOX */
110 BST_CHECKED
, /* BS_RADIOBUTTON */
111 BST_INDETERMINATE
, /* BS_3STATE */
112 BST_INDETERMINATE
, /* BS_AUTO3STATE */
113 BST_UNCHECKED
, /* BS_GROUPBOX */
114 BST_UNCHECKED
, /* BS_USERBUTTON */
115 BST_CHECKED
, /* BS_AUTORADIOBUTTON */
116 BST_UNCHECKED
, /* BS_PUSHBOX */
117 BST_UNCHECKED
/* BS_OWNERDRAW */
120 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
122 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
124 PB_Paint
, /* BS_PUSHBUTTON */
125 PB_Paint
, /* BS_DEFPUSHBUTTON */
126 CB_Paint
, /* BS_CHECKBOX */
127 CB_Paint
, /* BS_AUTOCHECKBOX */
128 CB_Paint
, /* BS_RADIOBUTTON */
129 CB_Paint
, /* BS_3STATE */
130 CB_Paint
, /* BS_AUTO3STATE */
131 GB_Paint
, /* BS_GROUPBOX */
132 UB_Paint
, /* BS_USERBUTTON */
133 CB_Paint
, /* BS_AUTORADIOBUTTON */
134 NULL
, /* BS_PUSHBOX */
135 OB_Paint
/* BS_OWNERDRAW */
138 static HBITMAP hbitmapCheckBoxes
= 0;
139 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
142 /*********************************************************************
143 * button class descriptor
145 static const WCHAR buttonW
[] = {'B','u','t','t','o','n',0};
146 const struct builtin_class_descr BUTTON_builtin_class
=
149 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
151 ButtonWndProcA
, /* procA */
152 ButtonWndProcW
, /* procW */
154 WINPROC_BUTTON
, /* proc */
156 NB_EXTRA_BYTES
, /* extra */
157 IDC_ARROW
, /* cursor */
162 static inline LONG
get_button_state( HWND hwnd
)
164 return GetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
);
167 static inline void set_button_state( HWND hwnd
, LONG state
)
169 SetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
, state
);
174 static __inline
void set_ui_state( HWND hwnd
, LONG flags
)
176 SetWindowLongPtrW( hwnd
, UISTATE_GWL_OFFSET
, flags
);
179 static __inline LONG
get_ui_state( HWND hwnd
)
181 return GetWindowLongPtrW( hwnd
, UISTATE_GWL_OFFSET
);
184 #endif /* __REACTOS__ */
186 static inline HFONT
get_button_font( HWND hwnd
)
188 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
191 static inline void set_button_font( HWND hwnd
, HFONT font
)
193 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
196 static inline UINT
get_button_type( LONG window_style
)
198 return (window_style
& BS_TYPEMASK
);
201 /* paint a button of any type */
202 static inline void paint_button( HWND hwnd
, LONG style
, UINT action
)
204 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
206 HDC hdc
= GetDC( hwnd
);
207 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
208 ReleaseDC( hwnd
, hdc
);
212 /* retrieve the button text; returned buffer must be freed by caller */
213 static inline WCHAR
*get_button_text( HWND hwnd
)
216 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
217 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
222 /* Retrieve the UI state for the control */
223 static BOOL
button_update_uistate(HWND hwnd
, BOOL unicode
)
225 LONG flags
, prevflags
;
228 flags
= DefWindowProcW(hwnd
, WM_QUERYUISTATE
, 0, 0);
230 flags
= DefWindowProcA(hwnd
, WM_QUERYUISTATE
, 0, 0);
232 prevflags
= get_ui_state(hwnd
);
234 if (prevflags
!= flags
)
236 set_ui_state(hwnd
, flags
);
244 /***********************************************************************
245 * ButtonWndProc_common
247 LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
248 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
252 LONG style
= GetWindowLongPtrW( hWnd
, GWL_STYLE
);
253 UINT btn_type
= get_button_type( style
);
259 pWnd
= ValidateHwnd(hWnd
);
264 NtUserSetWindowFNID(hWnd
, FNID_BUTTON
);
268 if (pWnd
->fnid
!= FNID_BUTTON
)
270 ERR("Wrong window class for Button! fnId 0x%x\n",pWnd
->fnid
);
277 if (!IsWindow( hWnd
)) return 0;
280 pt
.x
= (short)LOWORD(lParam
);
281 pt
.y
= (short)HIWORD(lParam
);
289 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
290 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
292 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
293 case BS_GROUPBOX
: return DLGC_STATIC
;
294 default: return DLGC_BUTTON
;
298 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
302 if (!hbitmapCheckBoxes
)
305 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
306 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
307 checkBoxWidth
= bmp
.bmWidth
/ 4;
308 checkBoxHeight
= bmp
.bmHeight
/ 3;
310 if (btn_type
>= MAX_BTN_TYPE
)
311 return -1; /* abort */
313 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
314 if (btn_type
== BS_USERBUTTON
)
316 style
= (style
& ~BS_TYPEMASK
) | BS_PUSHBUTTON
;
318 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
320 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
323 set_button_state( hWnd
, BST_UNCHECKED
);
325 button_update_uistate( hWnd
, unicode
);
331 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
337 if (btn_type
== BS_OWNERDRAW
)
339 HDC hdc
= (HDC
)wParam
;
342 HWND parent
= GetParent(hWnd
);
343 if (!parent
) parent
= hWnd
;
345 hBrush
= GetControlColor( parent
, hWnd
, hdc
, WM_CTLCOLORBTN
);
347 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
348 if (!hBrush
) /* did the app forget to call defwindowproc ? */
349 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
350 (WPARAM
)hdc
, (LPARAM
)hWnd
);
352 GetClientRect(hWnd
, &rc
);
353 FillRect(hdc
, &rc
, hBrush
);
359 if (btnPaintFunc
[btn_type
])
362 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
363 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
364 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
365 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
366 if( !wParam
) EndPaint( hWnd
, &ps
);
371 if (wParam
== VK_SPACE
)
373 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
374 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
379 case WM_LBUTTONDBLCLK
:
380 if(style
& BS_NOTIFY
||
381 btn_type
== BS_RADIOBUTTON
||
382 btn_type
== BS_USERBUTTON
||
383 btn_type
== BS_OWNERDRAW
)
385 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
392 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
393 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
397 if (wParam
!= VK_SPACE
)
401 state
= get_button_state( hWnd
);
402 if (!(state
& BUTTON_BTNPRESSED
)) break;
403 state
&= BUTTON_NSTATES
;
404 set_button_state( hWnd
, state
);
405 if (!(state
& BST_PUSHED
))
410 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
412 GetClientRect( hWnd
, &rect
);
413 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
415 state
= get_button_state( hWnd
);
418 case BS_AUTOCHECKBOX
:
419 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BST_CHECKED
), 0 );
421 case BS_AUTORADIOBUTTON
:
422 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
425 SendMessageW( hWnd
, BM_SETCHECK
,
426 (state
& BST_INDETERMINATE
) ? 0 : ((state
& 3) + 1), 0 );
429 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
433 case WM_CAPTURECHANGED
:
434 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
435 state
= get_button_state( hWnd
);
436 if (state
& BUTTON_BTNPRESSED
)
438 state
&= BUTTON_NSTATES
;
439 set_button_state( hWnd
, state
);
440 if (state
& BST_PUSHED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
445 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
447 GetClientRect( hWnd
, &rect
);
448 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
454 /* Clear an old text here as Windows does */
456 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
457 // Patch: http://source.winehq.org/patches/data/70889
458 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
461 if (style
& WS_VISIBLE
)
464 HDC hdc
= GetDC(hWnd
);
467 HWND parent
= GetParent(hWnd
);
469 UINT ctlMessage
= (btn_type
== BS_PUSHBUTTON
||
470 btn_type
== BS_DEFPUSHBUTTON
||
471 btn_type
== BS_PUSHLIKE
||
472 btn_type
== BS_USERBUTTON
||
473 btn_type
== BS_OWNERDRAW
) ?
474 WM_CTLCOLORBTN
: WM_CTLCOLORSTATIC
;
477 if (!parent
) parent
= hWnd
;
479 hbrush
= GetControlColor(parent
, hWnd
, hdc
, ctlMessage
);
481 hbrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
482 (WPARAM
)hdc
, (LPARAM
)hWnd
);
483 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
484 hbrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
485 (WPARAM
)hdc
, (LPARAM
)hWnd
);
488 GetClientRect(hWnd
, &client
);
490 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
491 /* Clip by client rect bounds */
492 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
493 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
494 FillRect(hdc
, &rc
, hbrush
);
495 ReleaseDC(hWnd
, hdc
);
500 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
501 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
502 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
503 InvalidateRect( hWnd
, NULL
, TRUE
);
505 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
506 return 1; /* success. FIXME: check text length */
510 set_button_font( hWnd
, (HFONT
)wParam
);
511 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
515 return (LRESULT
)get_button_font( hWnd
);
518 TRACE("WM_SETFOCUS %p\n",hWnd
);
519 set_button_state( hWnd
, get_button_state(hWnd
) | BST_FOCUS
);
520 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
521 if (style
& BS_NOTIFY
)
522 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
526 TRACE("WM_KILLFOCUS %p\n",hWnd
);
527 state
= get_button_state( hWnd
);
528 set_button_state( hWnd
, state
& ~BST_FOCUS
);
529 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
531 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
533 if (style
& BS_NOTIFY
)
534 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
536 InvalidateRect( hWnd
, NULL
, FALSE
);
539 case WM_SYSCOLORCHANGE
:
540 InvalidateRect( hWnd
, NULL
, FALSE
);
544 if ((wParam
& BS_TYPEMASK
) >= MAX_BTN_TYPE
) break;
545 btn_type
= wParam
& BS_TYPEMASK
;
546 style
= (style
& ~BS_TYPEMASK
) | btn_type
;
548 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
550 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
553 /* Only redraw if lParam flag is set.*/
555 InvalidateRect( hWnd
, NULL
, TRUE
);
561 state
= get_button_state(hWnd
);
562 if (state
& BUTTON_BMCLICK
)
564 set_button_state(hWnd
, state
| BUTTON_BMCLICK
); // Tracked in STATE_GWL_OFFSET.
566 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
567 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
569 state
= get_button_state(hWnd
);
570 if (!(state
& BUTTON_BMCLICK
)) break;
571 state
&= ~BUTTON_BMCLICK
;
572 set_button_state(hWnd
, state
);
577 /* Check that image format matches button style */
578 switch (style
& (BS_BITMAP
|BS_ICON
))
581 if (wParam
!= IMAGE_BITMAP
) return 0;
584 if (wParam
!= IMAGE_ICON
) return 0;
589 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
590 InvalidateRect( hWnd
, NULL
, FALSE
);
591 return (LRESULT
)oldHbitmap
;
594 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
597 return get_button_state( hWnd
) & 3;
600 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
601 state
= get_button_state( hWnd
);
602 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
605 if (wParam
) style
|= WS_TABSTOP
;
606 else style
&= ~WS_TABSTOP
;
607 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
609 if (wParam
) WIN_SetStyle( hWnd
, WS_TABSTOP
, 0 );
610 else WIN_SetStyle( hWnd
, 0, WS_TABSTOP
);
613 if ((state
& 3) != wParam
)
615 set_button_state( hWnd
, (state
& ~3) | wParam
);
616 paint_button( hWnd
, btn_type
, ODA_SELECT
);
618 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BST_CHECKED
) && (style
& WS_CHILD
))
619 BUTTON_CheckAutoRadioButton( hWnd
);
623 return get_button_state( hWnd
);
626 state
= get_button_state( hWnd
);
628 set_button_state( hWnd
, state
| BST_PUSHED
);
630 set_button_state( hWnd
, state
& ~BST_PUSHED
);
632 paint_button( hWnd
, btn_type
, ODA_SELECT
);
636 case WM_UPDATEUISTATE
:
638 DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
640 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
642 if (button_update_uistate(hWnd
, unicode
))
643 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
648 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
651 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
652 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
659 /***********************************************************************
661 * The button window procedure. This is just a wrapper which locks
662 * the passed HWND and calls the real window procedure (with a WND*
663 * pointer pointing to the locked windowstructure).
665 LRESULT WINAPI
ButtonWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
667 if (!IsWindow(hWnd
)) return 0;
668 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);
671 /***********************************************************************
674 LRESULT WINAPI
ButtonWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
676 if (!IsWindow(hWnd
)) return 0;
677 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
680 #endif /* __REACTOS__ */
682 /**********************************************************************
683 * Convert button styles to flags used by DrawText.
685 static UINT
BUTTON_BStoDT( DWORD style
, DWORD ex_style
)
687 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
689 /* "Convert" pushlike buttons to pushbuttons */
690 if (style
& BS_PUSHLIKE
)
691 style
&= ~BS_TYPEMASK
;
693 if (!(style
& BS_MULTILINE
))
694 dtStyle
|= DT_SINGLELINE
;
696 dtStyle
|= DT_WORDBREAK
;
698 switch (style
& BS_CENTER
)
700 case BS_LEFT
: /* DT_LEFT is 0 */ break;
701 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
702 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
704 /* Pushbutton's text is centered by default */
705 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
706 /* all other flavours have left aligned text */
709 if (ex_style
& WS_EX_RIGHT
) dtStyle
= DT_RIGHT
| (dtStyle
& ~(DT_LEFT
| DT_CENTER
));
711 /* DrawText ignores vertical alignment for multiline text,
712 * but we use these flags to align label manually.
714 if (get_button_type(style
) != BS_GROUPBOX
)
716 switch (style
& BS_VCENTER
)
718 case BS_TOP
: /* DT_TOP is 0 */ break;
719 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
720 case BS_VCENTER
: /* fall through */
721 default: dtStyle
|= DT_VCENTER
; break;
725 /* GroupBox's text is always single line and is top aligned. */
726 dtStyle
|= DT_SINGLELINE
;
731 /**********************************************************************
732 * BUTTON_CalcLabelRect
734 * Calculates label's rectangle depending on button style.
736 * Returns flags to be passed to DrawText.
737 * Calculated rectangle doesn't take into account button state
738 * (pushed, etc.). If there is nothing to draw (no text/image) output
739 * rectangle is empty, and return value is (UINT)-1.
741 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
743 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
744 LONG ex_style
= GetWindowLongPtrW( hwnd
, GWL_EXSTYLE
);
748 UINT dtStyle
= BUTTON_BStoDT( style
, ex_style
);
752 /* Calculate label rectangle according to label type */
753 switch (style
& (BS_ICON
|BS_BITMAP
))
756 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
759 HeapFree( GetProcessHeap(), 0, text
);
762 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
763 HeapFree( GetProcessHeap(), 0, text
);
765 if (get_ui_state(hwnd
) & UISF_HIDEACCEL
)
766 dtStyle
|= DT_HIDEPREFIX
;
771 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
774 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
776 r
.right
= r
.left
+ bm
.bmWidth
;
777 r
.bottom
= r
.top
+ bm
.bmHeight
;
779 DeleteObject(iconInfo
.hbmColor
);
780 DeleteObject(iconInfo
.hbmMask
);
784 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
787 r
.right
= r
.left
+ bm
.bmWidth
;
788 r
.bottom
= r
.top
+ bm
.bmHeight
;
798 /* Position label inside bounding rectangle according to
799 * alignment flags. (calculated rect is always left-top aligned).
800 * If label is aligned to any side - shift label in opposite
801 * direction to leave extra space for focus rectangle.
803 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
805 case DT_LEFT
: r
.left
++; r
.right
++; break;
806 case DT_CENTER
: n
= r
.right
- r
.left
;
807 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
808 r
.right
= r
.left
+ n
; break;
809 case DT_RIGHT
: n
= r
.right
- r
.left
;
810 r
.right
= rc
->right
- 1;
811 r
.left
= r
.right
- n
;
815 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
817 case DT_TOP
: r
.top
++; r
.bottom
++; break;
818 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
819 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
820 r
.bottom
= r
.top
+ n
; break;
821 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
822 r
.bottom
= rc
->bottom
- 1;
823 r
.top
= r
.bottom
- n
;
832 /**********************************************************************
833 * BUTTON_DrawTextCallback
835 * Callback function used by DrawStateW function.
837 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
845 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
850 /**********************************************************************
853 * Common function for drawing button label.
855 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
857 DRAWSTATEPROC lpOutputProc
= NULL
;
861 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
862 LONG state
= get_button_state( hwnd
);
863 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
866 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
867 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
868 * I don't have Win31 on hand to verify that, so I leave it as is.
871 if ((style
& BS_PUSHLIKE
) && (state
& BST_INDETERMINATE
))
873 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
877 switch (style
& (BS_ICON
|BS_BITMAP
))
880 /* DST_COMPLEX -- is 0 */
881 lpOutputProc
= BUTTON_DrawTextCallback
;
882 if (!(text
= get_button_text( hwnd
))) return;
884 wp
= (WPARAM
)dtFlags
;
887 if (dtFlags
& DT_HIDEPREFIX
)
888 flags
|= DSS_HIDEPREFIX
;
894 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
899 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
906 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
907 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
908 HeapFree( GetProcessHeap(), 0, text
);
911 /**********************************************************************
912 * Push Button Functions
914 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
917 UINT dtFlags
, uState
;
921 COLORREF oldTxtColor
;
923 LONG state
= get_button_state( hwnd
);
924 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
925 BOOL pushedState
= (state
& BST_PUSHED
);
929 GetClientRect( hwnd
, &rc
);
931 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
932 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
933 parent
= GetParent(hwnd
);
934 if (!parent
) parent
= hwnd
;
936 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
938 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
941 hrgn
= set_control_clipping( hDC
, &rc
);
943 hOldPen
= SelectObject(hDC
, GetStockObject(DC_PEN
));
944 SetDCPenColor(hDC
, GetSysColor(COLOR_WINDOWFRAME
));
946 hOldPen
= SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
948 hOldBrush
= SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
949 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
951 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
953 if (action
!= ODA_FOCUS
)
954 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
955 InflateRect( &rc
, -1, -1 );
958 /* completely skip the drawing if only focus has changed */
959 if (action
== ODA_FOCUS
) goto draw_focus
;
961 uState
= DFCS_BUTTONPUSH
;
965 else if (pushedState
)
967 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
970 uState
|= DFCS_PUSHED
;
973 if (state
& (BST_CHECKED
| BST_INDETERMINATE
))
974 uState
|= DFCS_CHECKED
;
976 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
978 /* draw button label */
980 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
982 if (dtFlags
== (UINT
)-1L)
986 OffsetRect(&r
, 1, 1);
988 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
990 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
992 SetTextColor( hDC
, oldTxtColor
);
995 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
998 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1001 InflateRect( &rc
, -2, -2 );
1002 DrawFocusRect( hDC
, &rc
);
1009 SelectObject( hDC
, hOldPen
);
1010 SelectObject( hDC
, hOldBrush
);
1011 SetBkMode(hDC
, oldBkMode
);
1012 SelectClipRgn( hDC
, hrgn
);
1013 if (hrgn
) DeleteObject( hrgn
);
1016 /**********************************************************************
1017 * Check Box & Radio Button Functions
1020 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1022 RECT rbox
, rtext
, client
;
1027 LONG state
= get_button_state( hwnd
);
1028 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1032 if (style
& BS_PUSHLIKE
)
1034 PB_Paint( hwnd
, hDC
, action
);
1038 GetClientRect(hwnd
, &client
);
1039 rbox
= rtext
= client
;
1041 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1043 parent
= GetParent(hwnd
);
1044 if (!parent
) parent
= hwnd
;
1046 hBrush
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1048 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
1049 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1050 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1051 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1052 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1054 hrgn
= set_control_clipping( hDC
, &client
);
1056 if (style
& BS_LEFTTEXT
)
1058 /* magic +4 is what CTL3D expects */
1060 rtext
.right
-= checkBoxWidth
+ 4;
1061 rbox
.left
= rbox
.right
- checkBoxWidth
;
1065 rtext
.left
+= checkBoxWidth
+ 4;
1066 rbox
.right
= checkBoxWidth
;
1069 /* Since WM_ERASEBKGND does nothing, first prepare background */
1070 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
1071 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
1075 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
1077 /* Only adjust rbox when rtext is valid */
1078 if (dtFlags
!= (UINT
)-1L)
1080 rbox
.top
= rtext
.top
;
1081 rbox
.bottom
= rtext
.bottom
;
1084 /* Draw the check-box bitmap */
1085 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
1089 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
1090 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
1091 else if (state
& BST_INDETERMINATE
) flags
= DFCS_BUTTON3STATE
;
1092 else flags
= DFCS_BUTTONCHECK
;
1094 if (state
& (BST_CHECKED
| BST_INDETERMINATE
)) flags
|= DFCS_CHECKED
;
1095 if (state
& BST_PUSHED
) flags
|= DFCS_PUSHED
;
1097 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
1099 /* rbox must have the correct height */
1100 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
1102 if (style
& BS_TOP
) {
1104 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1106 rbox
.top
-= -delta
/2 + 1;
1107 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1109 } else if (style
& BS_BOTTOM
) {
1111 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1113 rbox
.bottom
+= -delta
/2 + 1;
1114 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1116 } else { /* Default */
1118 int ofs
= (delta
/ 2);
1119 rbox
.bottom
-= ofs
+ 1;
1120 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1121 } else if (delta
< 0) {
1122 int ofs
= (-delta
/ 2);
1123 rbox
.top
-= ofs
+ 1;
1124 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1128 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
1131 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
1134 if (action
== ODA_DRAWENTIRE
)
1135 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
1138 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1141 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1146 IntersectRect(&rtext
, &rtext
, &client
);
1147 DrawFocusRect( hDC
, &rtext
);
1152 SelectClipRgn( hDC
, hrgn
);
1153 if (hrgn
) DeleteObject( hrgn
);
1157 /**********************************************************************
1158 * BUTTON_CheckAutoRadioButton
1160 * hwnd is checked, uncheck every other auto radio button in group
1162 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1164 HWND parent
, sibling
, start
;
1166 parent
= GetParent(hwnd
);
1167 /* make sure that starting control is not disabled or invisible */
1168 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1171 if (!sibling
) break;
1172 if ((hwnd
!= sibling
) &&
1173 ((GetWindowLongPtrW( sibling
, GWL_STYLE
) & BS_TYPEMASK
) == BS_AUTORADIOBUTTON
))
1174 SendMessageW( sibling
, BM_SETCHECK
, BST_UNCHECKED
, 0 );
1175 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1176 } while (sibling
!= start
);
1180 /**********************************************************************
1181 * Group Box Functions
1184 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1191 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1195 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1196 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1197 parent
= GetParent(hwnd
);
1198 if (!parent
) parent
= hwnd
;
1200 hbr
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1202 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1203 if (!hbr
) /* did the app forget to call defwindowproc ? */
1204 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1205 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1207 GetClientRect( hwnd
, &rc
);
1209 hrgn
= set_control_clipping( hDC
, &rc
);
1211 GetTextMetricsW (hDC
, &tm
);
1212 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1213 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1215 InflateRect(&rc
, -7, 1);
1216 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1218 if (dtFlags
!= (UINT
)-1L)
1220 /* Because buttons have CS_PARENTDC class style, there is a chance
1221 * that label will be drawn out of client rect.
1222 * But Windows doesn't clip label's rect, so do I.
1225 /* There is 1-pixel margin at the left, right, and bottom */
1226 rc
.left
--; rc
.right
++; rc
.bottom
++;
1227 FillRect(hDC
, &rc
, hbr
);
1228 rc
.left
++; rc
.right
--; rc
.bottom
--;
1230 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1232 SelectClipRgn( hDC
, hrgn
);
1233 if (hrgn
) DeleteObject( hrgn
);
1237 /**********************************************************************
1238 * User Button Functions
1241 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1246 LONG state
= get_button_state( hwnd
);
1249 GetClientRect( hwnd
, &rc
);
1251 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1253 parent
= GetParent(hwnd
);
1254 if (!parent
) parent
= hwnd
;
1256 hBrush
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1258 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1259 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1260 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1261 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1264 FillRect( hDC
, &rc
, hBrush
);
1265 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1268 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1270 DrawFocusRect( hDC
, &rc
);
1278 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_FOCUS
) ? BN_SETFOCUS
: BN_KILLFOCUS
);
1282 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_PUSHED
) ? BN_HILITE
: BN_UNHILITE
);
1286 BUTTON_NOTIFY_PARENT( hwnd
, BN_PAINT
);
1292 /**********************************************************************
1293 * Ownerdrawn Button Functions
1296 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1298 LONG state
= get_button_state( hwnd
);
1300 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1302 HFONT hFont
, hPrevFont
= 0;
1305 dis
.CtlType
= ODT_BUTTON
;
1308 dis
.itemAction
= action
;
1309 dis
.itemState
= ((state
& BST_FOCUS
) ? ODS_FOCUS
: 0) |
1310 ((state
& BST_PUSHED
) ? ODS_SELECTED
: 0) |
1311 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1312 dis
.hwndItem
= hwnd
;
1315 GetClientRect( hwnd
, &dis
.rcItem
);
1317 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1318 parent
= GetParent(hwnd
);
1319 if (!parent
) parent
= hwnd
;
1321 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1323 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1326 hrgn
= set_control_clipping( hDC
, &dis
.rcItem
);
1328 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1329 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1330 SelectClipRgn( hDC
, hrgn
);
1331 if (hrgn
) DeleteObject( hrgn
);