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
68 WINE_DEFAULT_DEBUG_CHANNEL(button
);
70 /* GetWindowLong offsets for window extra information */
71 #define STATE_GWL_OFFSET 0
72 #define BUTTON_HFONT_GWL_OFFSET (sizeof(LONG))
73 #define HIMAGE_GWL_OFFSET (BUTTON_HFONT_GWL_OFFSET+sizeof(HFONT))
74 #define BUTTON_UISTATE_GWL_OFFSET (HIMAGE_GWL_OFFSET+sizeof(HFONT))
75 #define NB_EXTRA_BYTES (BUTTON_UISTATE_GWL_OFFSET+sizeof(LONG_PTR))
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 (*pfButtonPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
122 static const pfButtonPaint 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 /*********************************************************************
139 * button class descriptor
141 static const WCHAR buttonW
[] = {'B','u','t','t','o','n',0};
142 const struct builtin_class_descr BUTTON_builtin_class
=
145 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
147 ButtonWndProcA
, /* procA */
148 ButtonWndProcW
, /* procW */
150 WINPROC_BUTTON
, /* proc */
152 NB_EXTRA_BYTES
, /* extra */
153 IDC_ARROW
, /* cursor */
158 static inline LONG
get_button_state( HWND hwnd
)
160 return GetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
);
163 static inline void set_button_state( HWND hwnd
, LONG state
)
165 SetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
, state
);
170 static __inline
void set_ui_state( HWND hwnd
, LONG flags
)
172 SetWindowLongPtrW( hwnd
, BUTTON_UISTATE_GWL_OFFSET
, flags
);
175 static __inline LONG
get_ui_state( HWND hwnd
)
177 return GetWindowLongPtrW( hwnd
, BUTTON_UISTATE_GWL_OFFSET
);
180 #endif /* __REACTOS__ */
182 static inline HFONT
get_button_font( HWND hwnd
)
184 return (HFONT
)GetWindowLongPtrW( hwnd
, BUTTON_HFONT_GWL_OFFSET
);
187 static inline void set_button_font( HWND hwnd
, HFONT font
)
189 SetWindowLongPtrW( hwnd
, BUTTON_HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
192 static inline UINT
get_button_type( LONG window_style
)
194 return (window_style
& BS_TYPEMASK
);
197 /* paint a button of any type */
198 static inline void paint_button( HWND hwnd
, LONG style
, UINT action
)
200 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
202 HDC hdc
= GetDC( hwnd
);
203 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
204 ReleaseDC( hwnd
, hdc
);
208 /* retrieve the button text; returned buffer must be freed by caller */
209 static inline WCHAR
*get_button_text( HWND hwnd
)
212 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
213 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
218 /* Retrieve the UI state for the control */
219 static BOOL
button_update_uistate(HWND hwnd
, BOOL unicode
)
221 LONG flags
, prevflags
;
224 flags
= DefWindowProcW(hwnd
, WM_QUERYUISTATE
, 0, 0);
226 flags
= DefWindowProcA(hwnd
, WM_QUERYUISTATE
, 0, 0);
228 prevflags
= get_ui_state(hwnd
);
230 if (prevflags
!= flags
)
232 set_ui_state(hwnd
, flags
);
240 /***********************************************************************
241 * ButtonWndProc_common
243 LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
244 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
248 LONG style
= GetWindowLongPtrW( hWnd
, GWL_STYLE
);
249 UINT btn_type
= get_button_type( style
);
255 pWnd
= ValidateHwnd(hWnd
);
260 NtUserSetWindowFNID(hWnd
, FNID_BUTTON
);
264 if (pWnd
->fnid
!= FNID_BUTTON
)
266 ERR("Wrong window class for Button! fnId 0x%x\n",pWnd
->fnid
);
274 if (!IsWindow( hWnd
)) return 0;
277 pt
.x
= (short)LOWORD(lParam
);
278 pt
.y
= (short)HIWORD(lParam
);
286 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
287 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
289 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
290 case BS_GROUPBOX
: return DLGC_STATIC
;
291 default: return DLGC_BUTTON
;
295 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
299 if (btn_type
>= MAX_BTN_TYPE
)
300 return -1; /* abort */
302 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
303 if (btn_type
== BS_USERBUTTON
)
305 style
= (style
& ~BS_TYPEMASK
) | BS_PUSHBUTTON
;
307 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
309 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
312 set_button_state( hWnd
, BST_UNCHECKED
);
314 button_update_uistate( hWnd
, unicode
);
320 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
326 if (btn_type
== BS_OWNERDRAW
)
328 HDC hdc
= (HDC
)wParam
;
331 HWND parent
= GetParent(hWnd
);
332 if (!parent
) parent
= hWnd
;
334 hBrush
= GetControlColor( parent
, hWnd
, hdc
, WM_CTLCOLORBTN
);
336 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
337 if (!hBrush
) /* did the app forget to call defwindowproc ? */
338 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
339 (WPARAM
)hdc
, (LPARAM
)hWnd
);
341 GetClientRect(hWnd
, &rc
);
342 FillRect(hdc
, &rc
, hBrush
);
350 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
351 if (btnPaintFunc
[btn_type
])
353 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
354 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
355 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
357 if ( !wParam
) EndPaint( hWnd
, &ps
);
362 if (wParam
== VK_SPACE
)
364 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
365 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
370 case WM_LBUTTONDBLCLK
:
371 if(style
& BS_NOTIFY
||
372 btn_type
== BS_RADIOBUTTON
||
373 btn_type
== BS_USERBUTTON
||
374 btn_type
== BS_OWNERDRAW
)
376 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
383 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
384 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
388 if (wParam
!= VK_SPACE
)
394 BOOL TellParent
= FALSE
; //// ReactOS see note below.
396 state
= get_button_state( hWnd
);
397 if (!(state
& BUTTON_BTNPRESSED
)) break;
398 state
&= BUTTON_NSTATES
;
399 set_button_state( hWnd
, state
);
400 if (!(state
& BST_PUSHED
))
405 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
406 GetClientRect( hWnd
, &rect
);
407 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
409 state
= get_button_state( hWnd
);
412 case BS_AUTOCHECKBOX
:
413 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BST_CHECKED
), 0 );
415 case BS_AUTORADIOBUTTON
:
416 BUTTON_CheckAutoRadioButton( hWnd
);
419 SendMessageW( hWnd
, BM_SETCHECK
,
420 (state
& BST_INDETERMINATE
) ? 0 : ((state
& 3) + 1), 0 );
424 TellParent
= TRUE
; // <---- Fix CORE-10194, Notify parent after capture is released.
427 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
437 if (TellParent
) BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
442 case WM_CAPTURECHANGED
:
443 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
444 if (hWnd
== (HWND
)lParam
) break;
445 state
= get_button_state( hWnd
);
446 if (state
& BUTTON_BTNPRESSED
)
448 state
&= BUTTON_NSTATES
;
449 set_button_state( hWnd
, state
);
450 if (state
& BST_PUSHED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
455 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
457 GetClientRect( hWnd
, &rect
);
458 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
464 /* Clear an old text here as Windows does */
467 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
468 // Patch: http://source.winehq.org/patches/data/70889
469 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
472 if (style
& WS_VISIBLE
)
474 if (IsWindowVisible(hWnd
))
477 HDC hdc
= GetDC(hWnd
);
480 HWND parent
= GetParent(hWnd
);
481 UINT message
= (btn_type
== BS_PUSHBUTTON
||
482 btn_type
== BS_DEFPUSHBUTTON
||
483 btn_type
== BS_PUSHLIKE
||
484 btn_type
== BS_USERBUTTON
||
485 btn_type
== BS_OWNERDRAW
) ?
486 WM_CTLCOLORBTN
: WM_CTLCOLORSTATIC
;
488 if (!parent
) parent
= hWnd
;
490 hbrush
= GetControlColor(parent
, hWnd
, hdc
, message
);
492 hbrush
= (HBRUSH
)SendMessageW(parent
, message
,
493 (WPARAM
)hdc
, (LPARAM
)hWnd
);
494 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
495 hbrush
= (HBRUSH
)DefWindowProcW(parent
, message
,
496 (WPARAM
)hdc
, (LPARAM
)hWnd
);
499 GetClientRect(hWnd
, &client
);
501 /* FIXME: check other BS_* handlers */
502 if (btn_type
== BS_GROUPBOX
)
503 InflateRect(&rc
, -7, 1); /* GB_Paint does this */
504 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
505 /* Clip by client rect bounds */
506 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
507 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
508 FillRect(hdc
, &rc
, hbrush
);
509 ReleaseDC(hWnd
, hdc
);
512 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
513 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
514 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
515 InvalidateRect( hWnd
, NULL
, TRUE
);
517 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
518 return 1; /* success. FIXME: check text length */
522 set_button_font( hWnd
, (HFONT
)wParam
);
523 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
527 return (LRESULT
)get_button_font( hWnd
);
530 TRACE("WM_SETFOCUS %p\n",hWnd
);
531 set_button_state( hWnd
, get_button_state(hWnd
) | BST_FOCUS
);
532 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
533 if (style
& BS_NOTIFY
)
534 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
536 if (((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
)) &&
537 !(get_button_state(hWnd
) & BST_CHECKED
))
539 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
545 TRACE("WM_KILLFOCUS %p\n",hWnd
);
546 state
= get_button_state( hWnd
);
547 set_button_state( hWnd
, state
& ~BST_FOCUS
);
548 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
550 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
552 if (style
& BS_NOTIFY
)
553 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
555 InvalidateRect( hWnd
, NULL
, FALSE
);
558 case WM_SYSCOLORCHANGE
:
559 InvalidateRect( hWnd
, NULL
, FALSE
);
563 btn_type
= wParam
& BS_TYPEMASK
;
564 style
= (style
& ~BS_TYPEMASK
) | btn_type
;
566 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
568 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
571 /* Only redraw if lParam flag is set.*/
573 InvalidateRect( hWnd
, NULL
, TRUE
);
579 state
= get_button_state(hWnd
);
580 if (state
& BUTTON_BMCLICK
)
582 set_button_state(hWnd
, state
| BUTTON_BMCLICK
); // Tracked in STATE_GWL_OFFSET.
584 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
585 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
587 state
= get_button_state(hWnd
);
588 if (!(state
& BUTTON_BMCLICK
)) break;
589 state
&= ~BUTTON_BMCLICK
;
590 set_button_state(hWnd
, state
);
595 /* Check that image format matches button style */
596 switch (style
& (BS_BITMAP
|BS_ICON
))
599 if (wParam
!= IMAGE_BITMAP
) return 0;
602 if (wParam
!= IMAGE_ICON
) return 0;
607 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
608 InvalidateRect( hWnd
, NULL
, FALSE
);
609 return (LRESULT
)oldHbitmap
;
612 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
615 return get_button_state( hWnd
) & 3;
618 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
619 state
= get_button_state( hWnd
);
620 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
623 if (wParam
) style
|= WS_TABSTOP
;
624 else style
&= ~WS_TABSTOP
;
625 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
627 if (wParam
) WIN_SetStyle( hWnd
, WS_TABSTOP
, 0 );
628 else WIN_SetStyle( hWnd
, 0, WS_TABSTOP
);
631 if ((state
& 3) != wParam
)
633 set_button_state( hWnd
, (state
& ~3) | wParam
);
634 paint_button( hWnd
, btn_type
, ODA_SELECT
);
639 return get_button_state( hWnd
);
642 state
= get_button_state( hWnd
);
644 set_button_state( hWnd
, state
| BST_PUSHED
);
646 set_button_state( hWnd
, state
& ~BST_PUSHED
);
648 paint_button( hWnd
, btn_type
, ODA_SELECT
);
652 case WM_UPDATEUISTATE
:
654 DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
656 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
658 if (button_update_uistate(hWnd
, unicode
))
659 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
664 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
667 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
668 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
675 /***********************************************************************
677 * The button window procedure. This is just a wrapper which locks
678 * the passed HWND and calls the real window procedure (with a WND*
679 * pointer pointing to the locked windowstructure).
681 LRESULT WINAPI
ButtonWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
683 if (!IsWindow(hWnd
)) return 0;
684 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);
687 /***********************************************************************
690 LRESULT WINAPI
ButtonWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
692 if (!IsWindow(hWnd
)) return 0;
693 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
696 #endif /* __REACTOS__ */
698 /**********************************************************************
699 * Convert button styles to flags used by DrawText.
701 static UINT
BUTTON_BStoDT( DWORD style
, DWORD ex_style
)
703 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
705 /* "Convert" pushlike buttons to pushbuttons */
706 if (style
& BS_PUSHLIKE
)
707 style
&= ~BS_TYPEMASK
;
709 if (!(style
& BS_MULTILINE
))
710 dtStyle
|= DT_SINGLELINE
;
712 dtStyle
|= DT_WORDBREAK
;
714 switch (style
& BS_CENTER
)
716 case BS_LEFT
: /* DT_LEFT is 0 */ break;
717 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
718 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
720 /* Pushbutton's text is centered by default */
721 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
722 /* all other flavours have left aligned text */
725 if (ex_style
& WS_EX_RIGHT
) dtStyle
= DT_RIGHT
| (dtStyle
& ~(DT_LEFT
| DT_CENTER
));
727 /* DrawText ignores vertical alignment for multiline text,
728 * but we use these flags to align label manually.
730 if (get_button_type(style
) != BS_GROUPBOX
)
732 switch (style
& BS_VCENTER
)
734 case BS_TOP
: /* DT_TOP is 0 */ break;
735 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
736 case BS_VCENTER
: /* fall through */
737 default: dtStyle
|= DT_VCENTER
; break;
741 /* GroupBox's text is always single line and is top aligned. */
742 dtStyle
|= DT_SINGLELINE
;
747 /**********************************************************************
748 * BUTTON_CalcLabelRect
750 * Calculates label's rectangle depending on button style.
752 * Returns flags to be passed to DrawText.
753 * Calculated rectangle doesn't take into account button state
754 * (pushed, etc.). If there is nothing to draw (no text/image) output
755 * rectangle is empty, and return value is (UINT)-1.
757 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
759 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
760 LONG ex_style
= GetWindowLongPtrW( hwnd
, GWL_EXSTYLE
);
764 UINT dtStyle
= BUTTON_BStoDT( style
, ex_style
);
768 /* Calculate label rectangle according to label type */
769 switch (style
& (BS_ICON
|BS_BITMAP
))
773 HFONT hFont
, hPrevFont
= 0;
775 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
778 HeapFree( GetProcessHeap(), 0, text
);
782 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hdc
, hFont
);
784 DrawTextW(hdc
, text
, -1, &r
, ((dtStyle
| DT_CALCRECT
) & ~(DT_VCENTER
| DT_BOTTOM
)));
786 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
788 if (hPrevFont
) SelectObject( hdc
, hPrevFont
);
789 HeapFree( GetProcessHeap(), 0, text
);
791 if (get_ui_state(hwnd
) & UISF_HIDEACCEL
)
792 dtStyle
|= DT_HIDEPREFIX
;
798 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
801 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
803 r
.right
= r
.left
+ bm
.bmWidth
;
804 r
.bottom
= r
.top
+ bm
.bmHeight
;
806 DeleteObject(iconInfo
.hbmColor
);
807 DeleteObject(iconInfo
.hbmMask
);
811 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
814 r
.right
= r
.left
+ bm
.bmWidth
;
815 r
.bottom
= r
.top
+ bm
.bmHeight
;
825 /* Position label inside bounding rectangle according to
826 * alignment flags. (calculated rect is always left-top aligned).
827 * If label is aligned to any side - shift label in opposite
828 * direction to leave extra space for focus rectangle.
830 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
832 case DT_LEFT
: r
.left
++; r
.right
++; break;
833 case DT_CENTER
: n
= r
.right
- r
.left
;
834 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
835 r
.right
= r
.left
+ n
; break;
836 case DT_RIGHT
: n
= r
.right
- r
.left
;
837 r
.right
= rc
->right
- 1;
838 r
.left
= r
.right
- n
;
842 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
844 case DT_TOP
: r
.top
++; r
.bottom
++; break;
845 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
847 r
.top
= rc
->top
+ ((rc
->bottom
- 1 - rc
->top
) - n
) / 2;
849 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
851 r
.bottom
= r
.top
+ n
; break;
852 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
853 r
.bottom
= rc
->bottom
- 1;
854 r
.top
= r
.bottom
- n
;
863 /**********************************************************************
864 * BUTTON_DrawTextCallback
866 * Callback function used by DrawStateW function.
868 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
872 SetRect(&rc
, 0, 0, cx
, cy
);
873 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
878 /**********************************************************************
881 * Common function for drawing button label.
883 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
885 DRAWSTATEPROC lpOutputProc
= NULL
;
889 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
890 LONG state
= get_button_state( hwnd
);
891 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
894 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
895 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
896 * I don't have Win31 on hand to verify that, so I leave it as is.
899 if ((style
& BS_PUSHLIKE
) && (state
& BST_INDETERMINATE
))
901 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
905 switch (style
& (BS_ICON
|BS_BITMAP
))
908 /* DST_COMPLEX -- is 0 */
909 lpOutputProc
= BUTTON_DrawTextCallback
;
910 if (!(text
= get_button_text( hwnd
))) return;
912 wp
= (WPARAM
)dtFlags
;
915 if (dtFlags
& DT_HIDEPREFIX
)
916 flags
|= DSS_HIDEPREFIX
;
922 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
927 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
934 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
935 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
936 HeapFree( GetProcessHeap(), 0, text
);
939 /**********************************************************************
940 * Push Button Functions
942 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
945 UINT dtFlags
, uState
;
949 COLORREF oldTxtColor
;
951 LONG state
= get_button_state( hwnd
);
952 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
953 BOOL pushedState
= (state
& BST_PUSHED
);
957 GetClientRect( hwnd
, &rc
);
959 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
960 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
961 parent
= GetParent(hwnd
);
962 if (!parent
) parent
= hwnd
;
964 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
966 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
969 hrgn
= set_control_clipping( hDC
, &rc
);
971 hOldPen
= SelectObject(hDC
, GetStockObject(DC_PEN
));
972 SetDCPenColor(hDC
, GetSysColor(COLOR_WINDOWFRAME
));
974 hOldPen
= SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
976 hOldBrush
= SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
977 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
979 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
981 if (action
!= ODA_FOCUS
)
982 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
983 InflateRect( &rc
, -1, -1 );
986 /* completely skip the drawing if only focus has changed */
987 if (action
== ODA_FOCUS
) goto draw_focus
;
989 uState
= DFCS_BUTTONPUSH
;
993 else if (pushedState
)
995 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
998 uState
|= DFCS_PUSHED
;
1001 if (state
& (BST_CHECKED
| BST_INDETERMINATE
))
1002 uState
|= DFCS_CHECKED
;
1004 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
1006 /* draw button label */
1008 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
1010 if (dtFlags
== (UINT
)-1L)
1014 OffsetRect(&r
, 1, 1);
1016 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
1018 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
1020 SetTextColor( hDC
, oldTxtColor
);
1023 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1026 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1029 InflateRect( &rc
, -2, -2 );
1030 DrawFocusRect( hDC
, &rc
);
1037 SelectObject( hDC
, hOldPen
);
1038 SelectObject( hDC
, hOldBrush
);
1039 SetBkMode(hDC
, oldBkMode
);
1040 SelectClipRgn( hDC
, hrgn
);
1041 if (hrgn
) DeleteObject( hrgn
);
1044 /**********************************************************************
1045 * Check Box & Radio Button Functions
1048 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1050 RECT rbox
, rtext
, client
;
1052 int delta
, text_offset
, checkBoxWidth
, checkBoxHeight
;
1055 LONG state
= get_button_state( hwnd
);
1056 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1057 LONG ex_style
= GetWindowLongW( hwnd
, GWL_EXSTYLE
);
1061 if (style
& BS_PUSHLIKE
)
1063 PB_Paint( hwnd
, hDC
, action
);
1067 GetClientRect(hwnd
, &client
);
1068 rbox
= rtext
= client
;
1070 checkBoxWidth
= 12 * GetDeviceCaps( hDC
, LOGPIXELSX
) / 96 + 1;
1071 checkBoxHeight
= 12 * GetDeviceCaps( hDC
, LOGPIXELSY
) / 96 + 1;
1073 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1074 GetCharWidthW( hDC
, '0', '0', &text_offset
);
1077 parent
= GetParent(hwnd
);
1078 if (!parent
) parent
= hwnd
;
1080 hBrush
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1082 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
1083 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1084 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1085 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1086 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1088 hrgn
= set_control_clipping( hDC
, &client
);
1090 if (style
& BS_LEFTTEXT
|| ex_style
& WS_EX_RIGHT
)
1092 /* magic +4 is what CTL3D expects */
1094 rtext
.right
-= checkBoxWidth
+ text_offset
;;
1095 rbox
.left
= rbox
.right
- checkBoxWidth
;
1099 rtext
.left
+= checkBoxWidth
+ text_offset
;;
1100 rbox
.right
= checkBoxWidth
;
1103 /* Since WM_ERASEBKGND does nothing, first prepare background */
1104 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
1105 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
1109 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
1111 /* Only adjust rbox when rtext is valid */
1112 if (dtFlags
!= (UINT
)-1L)
1114 rbox
.top
= rtext
.top
;
1115 rbox
.bottom
= rtext
.bottom
;
1118 /* Draw the check-box bitmap */
1119 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
1123 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
1124 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
1125 else if (state
& BST_INDETERMINATE
) flags
= DFCS_BUTTON3STATE
;
1126 else flags
= DFCS_BUTTONCHECK
;
1128 if (state
& (BST_CHECKED
| BST_INDETERMINATE
)) flags
|= DFCS_CHECKED
;
1129 if (state
& BST_PUSHED
) flags
|= DFCS_PUSHED
;
1131 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
1133 /* rbox must have the correct height */
1134 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
1136 if (style
& BS_TOP
) {
1138 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1140 rbox
.top
-= -delta
/2 + 1;
1141 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1143 } else if (style
& BS_BOTTOM
) {
1145 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1147 rbox
.bottom
+= -delta
/2 + 1;
1148 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1150 } else { /* Default */
1152 int ofs
= (delta
/ 2);
1153 rbox
.bottom
-= ofs
+ 1;
1154 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1155 } else if (delta
< 0) {
1156 int ofs
= (-delta
/ 2);
1157 rbox
.top
-= ofs
+ 1;
1158 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1162 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
1165 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
1168 if (action
== ODA_DRAWENTIRE
)
1169 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
1172 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1175 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1180 IntersectRect(&rtext
, &rtext
, &client
);
1181 DrawFocusRect( hDC
, &rtext
);
1186 SelectClipRgn( hDC
, hrgn
);
1187 if (hrgn
) DeleteObject( hrgn
);
1191 /**********************************************************************
1192 * BUTTON_CheckAutoRadioButton
1194 * hwnd is checked, uncheck every other auto radio button in group
1196 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1198 HWND parent
, sibling
, start
;
1200 parent
= GetParent(hwnd
);
1201 /* make sure that starting control is not disabled or invisible */
1202 start
= sibling
= hwnd
;
1205 if (!sibling
) break;
1207 if ((SendMessageW(sibling
, WM_GETDLGCODE
, 0, 0) & (DLGC_BUTTON
| DLGC_RADIOBUTTON
)) == (DLGC_BUTTON
| DLGC_RADIOBUTTON
))
1209 if (SendMessageW( sibling
, WM_GETDLGCODE
, 0, 0 ) == (DLGC_BUTTON
| DLGC_RADIOBUTTON
))
1211 SendMessageW( sibling
, BM_SETCHECK
, sibling
== hwnd
? BST_CHECKED
: BST_UNCHECKED
, 0 );
1212 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1213 } while (sibling
!= start
);
1217 /**********************************************************************
1218 * Group Box Functions
1221 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1228 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1232 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1233 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1234 parent
= GetParent(hwnd
);
1235 if (!parent
) parent
= hwnd
;
1237 hbr
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1239 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1240 if (!hbr
) /* did the app forget to call defwindowproc ? */
1241 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1242 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1244 GetClientRect( hwnd
, &rc
);
1246 hrgn
= set_control_clipping( hDC
, &rc
);
1248 GetTextMetricsW (hDC
, &tm
);
1249 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1250 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1252 InflateRect(&rc
, -7, 1);
1253 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1255 if (dtFlags
!= (UINT
)-1L)
1257 /* Because buttons have CS_PARENTDC class style, there is a chance
1258 * that label will be drawn out of client rect.
1259 * But Windows doesn't clip label's rect, so do I.
1262 /* There is 1-pixel margin at the left, right, and bottom */
1263 rc
.left
--; rc
.right
++; rc
.bottom
++;
1264 FillRect(hDC
, &rc
, hbr
);
1265 rc
.left
++; rc
.right
--; rc
.bottom
--;
1267 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1269 SelectClipRgn( hDC
, hrgn
);
1270 if (hrgn
) DeleteObject( hrgn
);
1274 /**********************************************************************
1275 * User Button Functions
1278 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1283 LONG state
= get_button_state( hwnd
);
1286 GetClientRect( hwnd
, &rc
);
1288 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1290 parent
= GetParent(hwnd
);
1291 if (!parent
) parent
= hwnd
;
1293 hBrush
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1295 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1296 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1297 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1298 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1301 FillRect( hDC
, &rc
, hBrush
);
1302 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1305 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1307 DrawFocusRect( hDC
, &rc
);
1315 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_FOCUS
) ? BN_SETFOCUS
: BN_KILLFOCUS
);
1319 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_PUSHED
) ? BN_HILITE
: BN_UNHILITE
);
1323 BUTTON_NOTIFY_PARENT( hwnd
, BN_PAINT
);
1329 /**********************************************************************
1330 * Ownerdrawn Button Functions
1333 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1335 LONG state
= get_button_state( hwnd
);
1337 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1339 HFONT hFont
, hPrevFont
= 0;
1342 dis
.CtlType
= ODT_BUTTON
;
1345 dis
.itemAction
= action
;
1346 dis
.itemState
= ((state
& BST_FOCUS
) ? ODS_FOCUS
: 0) |
1347 ((state
& BST_PUSHED
) ? ODS_SELECTED
: 0) |
1348 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1349 dis
.hwndItem
= hwnd
;
1352 GetClientRect( hwnd
, &dis
.rcItem
);
1354 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1355 parent
= GetParent(hwnd
);
1356 if (!parent
) parent
= hwnd
;
1358 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1360 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1363 hrgn
= set_control_clipping( hDC
, &dis
.rcItem
);
1365 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1366 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1367 SelectClipRgn( hDC
, hrgn
);
1368 if (hrgn
) DeleteObject( hrgn
);