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 /* The original code from user32 was kept in order to make it easier to bring changes from user32 */
140 /*********************************************************************
141 * button class descriptor
143 static const WCHAR buttonW
[] = {'B','u','t','t','o','n',0};
144 const struct builtin_class_descr BUTTON_builtin_class
=
147 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
149 ButtonWndProcA
, /* procA */
150 ButtonWndProcW
, /* procW */
152 WINPROC_BUTTON
, /* proc */
154 NB_EXTRA_BYTES
, /* extra */
155 IDC_ARROW
, /* cursor */
160 static inline LONG
get_button_state( HWND hwnd
)
162 return GetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
);
165 static inline void set_button_state( HWND hwnd
, LONG state
)
167 SetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
, state
);
172 static __inline
void set_ui_state( HWND hwnd
, LONG flags
)
174 SetWindowLongPtrW( hwnd
, UISTATE_GWL_OFFSET
, flags
);
177 static __inline LONG
get_ui_state( HWND hwnd
)
179 return GetWindowLongPtrW( hwnd
, UISTATE_GWL_OFFSET
);
182 #endif /* __REACTOS__ */
184 static inline HFONT
get_button_font( HWND hwnd
)
186 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
189 static inline void set_button_font( HWND hwnd
, HFONT font
)
191 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
194 static inline UINT
get_button_type( LONG window_style
)
196 return (window_style
& BS_TYPEMASK
);
199 /* paint a button of any type */
200 static inline void paint_button( HWND hwnd
, LONG style
, UINT action
)
202 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
204 HDC hdc
= GetDC( hwnd
);
205 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
206 ReleaseDC( hwnd
, hdc
);
212 #define NtUserAlterWindowStyle SetWindowLongPtrW
214 static inline void _SetButtonData(HWND hwnd
, PBUTTON_DATA data
)
216 SetWindowLongPtrW( hwnd
, 0, (LONG
)data
);
219 HRGN
set_control_clipping( HDC hdc
, const RECT
*rect
)
222 HRGN hrgn
= CreateRectRgn( 0, 0, 0, 0 );
224 if (GetClipRgn( hdc
, hrgn
) != 1)
226 DeleteObject( hrgn
);
229 DPtoLP( hdc
, (POINT
*)&rc
, 2 );
230 if (GetLayout( hdc
) & LAYOUT_RTL
) /* compensate for the shifting done by IntersectClipRect */
235 IntersectClipRect( hdc
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
239 BOOL
BUTTON_PaintWithTheme(HTHEME theme
, HWND hwnd
, HDC hParamDC
, LPARAM prfFlag
);
240 WCHAR
*get_button_text( HWND hwnd
);
242 static inline LONG_PTR
get_button_image(HWND hwnd
)
244 return _GetButtonData(hwnd
)->image
;
247 static inline LONG_PTR
set_button_image(HWND hwnd
, LONG_PTR image
)
249 PBUTTON_DATA data
= _GetButtonData(hwnd
);
250 LONG_PTR ret
= data
->image
;
255 static inline LONG
get_button_state( HWND hwnd
)
257 return _GetButtonData(hwnd
)->state
;
260 static inline void set_button_state( HWND hwnd
, LONG state
)
262 _GetButtonData(hwnd
)->state
= state
;
265 static __inline
void set_ui_state( HWND hwnd
, LONG flags
)
267 _GetButtonData(hwnd
)->ui_state
= flags
;
270 static __inline LONG
get_ui_state( HWND hwnd
)
272 return _GetButtonData(hwnd
)->ui_state
;
275 static inline HFONT
get_button_font( HWND hwnd
)
277 return (HFONT
)_GetButtonData(hwnd
)->font
;
280 static inline void set_button_font( HWND hwnd
, HFONT font
)
282 _GetButtonData(hwnd
)->font
= font
;
285 static inline UINT
get_button_type( LONG window_style
)
287 return (window_style
& BS_TYPEMASK
);
290 /* paint a button of any type */
291 static inline void paint_button( HWND hwnd
, LONG style
, UINT action
)
293 HTHEME theme
= GetWindowTheme(hwnd
);
295 HDC hdc
= GetDC( hwnd
);
296 /* GetDC appears to give a dc with a clip rect that includes the whoe parent, not sure if it is correct or not. */
297 GetClientRect(hwnd
, &rc
);
298 IntersectClipRect (hdc
, rc
.left
, rc
. top
, rc
.right
, rc
.bottom
);
299 if (theme
&& BUTTON_PaintWithTheme(theme
, hwnd
, hdc
, 0))
301 ReleaseDC( hwnd
, hdc
);
304 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
306 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
308 ReleaseDC( hwnd
, hdc
);
311 BOOL
BUTTON_GetIdealSize(HTHEME theme
, HWND hwnd
, SIZE
* psize
)
316 HFONT hFont
= 0, hPrevFont
= 0;
317 SIZE TextSize
, ImageSize
, ButtonSize
;
319 LOGFONTW logfont
= {0};
321 pdata
= _GetButtonData(hwnd
);
322 text
= get_button_text( hwnd
);
324 if (!pdata
|| !text
|| !hdc
|| !text
[0])
327 /* FIXME : Should use GetThemeTextExtent but unfortunately uses DrawTextW which is broken */
330 HRESULT hr
= GetThemeFont(theme
, hdc
, BP_PUSHBUTTON
, PBS_NORMAL
, TMT_FONT
, &logfont
);
333 hFont
= CreateFontIndirectW(&logfont
);
335 hPrevFont
= SelectObject( hdc
, hFont
);
341 hPrevFont
= SelectObject( hdc
, pdata
->font
);
344 GetTextExtentPoint32W(hdc
, text
, wcslen(text
), &TextSize
);
346 if (logfont
.lfHeight
== -1 && logfont
.lfWidth
== 0 && wcscmp(logfont
.lfFaceName
, L
"Arial") == 0 && wcsicmp(text
, L
"Start") == 0)
353 SelectObject( hdc
, hPrevFont
);
355 TextSize
.cy
+= pdata
->rcTextMargin
.top
+ pdata
->rcTextMargin
.bottom
;
356 TextSize
.cx
+= pdata
->rcTextMargin
.left
+ pdata
->rcTextMargin
.right
;
358 if (pdata
->imlData
.himl
&& ImageList_GetIconSize(pdata
->imlData
.himl
, &ImageSize
.cx
, &ImageSize
.cy
))
360 ImageSize
.cx
+= pdata
->imlData
.margin
.left
+ pdata
->imlData
.margin
.right
;
361 ImageSize
.cy
+= pdata
->imlData
.margin
.top
+ pdata
->imlData
.margin
.bottom
;
365 ImageSize
.cx
= ImageSize
.cy
= 0;
370 RECT rcContents
= {0};
371 RECT rcButtonExtent
= {0};
372 rcContents
.right
= ImageSize
.cx
+ TextSize
.cx
;
373 rcContents
.bottom
= max(ImageSize
.cy
, TextSize
.cy
);
374 GetThemeBackgroundExtent(theme
, hdc
, BP_PUSHBUTTON
, PBS_NORMAL
, &rcContents
, &rcButtonExtent
);
375 ButtonSize
.cx
= rcButtonExtent
.right
- rcButtonExtent
.left
;
376 ButtonSize
.cy
= rcButtonExtent
.bottom
- rcButtonExtent
.top
;
380 ButtonSize
.cx
= ImageSize
.cx
+ TextSize
.cx
+ 5;
381 ButtonSize
.cy
= max(ImageSize
.cy
, TextSize
.cy
+ 7);
391 HeapFree( GetProcessHeap(), 0, text
);
393 ReleaseDC(hwnd
, hdc
);
401 /* retrieve the button text; returned buffer must be freed by caller */
402 inline WCHAR
*get_button_text( HWND hwnd
)
405 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
406 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
411 /* Retrieve the UI state for the control */
412 static BOOL
button_update_uistate(HWND hwnd
, BOOL unicode
)
414 LONG flags
, prevflags
;
417 flags
= DefWindowProcW(hwnd
, WM_QUERYUISTATE
, 0, 0);
419 flags
= DefWindowProcA(hwnd
, WM_QUERYUISTATE
, 0, 0);
421 prevflags
= get_ui_state(hwnd
);
423 if (prevflags
!= flags
)
425 set_ui_state(hwnd
, flags
);
433 /***********************************************************************
434 * ButtonWndProc_common
436 LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
437 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
441 LONG style
= GetWindowLongPtrW( hWnd
, GWL_STYLE
);
442 UINT btn_type
= get_button_type( style
);
445 #if defined(__REACTOS__) && defined(_USER32_)
448 pWnd
= ValidateHwnd(hWnd
);
453 NtUserSetWindowFNID(hWnd
, FNID_BUTTON
);
457 if (pWnd
->fnid
!= FNID_BUTTON
)
459 ERR("Wrong window class for Button! fnId 0x%x\n",pWnd
->fnid
);
467 if (!IsWindow( hWnd
)) return 0;
470 pt
.x
= (short)LOWORD(lParam
);
471 pt
.y
= (short)HIWORD(lParam
);
478 PBUTTON_DATA data
= HeapAlloc( GetProcessHeap(), 0, sizeof(BUTTON_DATA
) );
481 ERR("Failed to alloc internal button data\n");
485 memset(data
, 0, sizeof(BUTTON_DATA
));
486 SetRect(&data
->rcTextMargin
, 1,1,1,1);
488 _SetButtonData(hWnd
, data
);
493 PBUTTON_DATA data
= _GetButtonData(hWnd
);
499 HeapFree( GetProcessHeap(), 0, data
);
500 _SetButtonData(hWnd
, NULL
);
503 OpenThemeData(hWnd
, WC_BUTTONW
);
506 CloseThemeData (GetWindowTheme(hWnd
));
508 case WM_THEMECHANGED
:
509 CloseThemeData (GetWindowTheme(hWnd
));
510 OpenThemeData(hWnd
, WC_BUTTONW
);
511 InvalidateRect(hWnd
, NULL
, TRUE
);
515 state
= get_button_state( hWnd
);
518 NMBCHOTITEM nmhotitem
;
521 set_button_state(hWnd
, state
);
523 nmhotitem
.hdr
.hwndFrom
= hWnd
;
524 nmhotitem
.hdr
.idFrom
= GetWindowLongPtrW (hWnd
, GWLP_ID
);
525 nmhotitem
.hdr
.code
= BCN_HOTITEMCHANGE
;
526 nmhotitem
.dwFlags
= HICF_LEAVING
;
527 SendMessageW(GetParent(hWnd
), WM_NOTIFY
, nmhotitem
.hdr
.idFrom
, (LPARAM
)&nmhotitem
);
529 InvalidateRect(hWnd
, NULL
, TRUE
);
535 TRACKMOUSEEVENT mouse_event
;
536 state
= get_button_state( hWnd
);
537 if ((state
& BST_HOT
) == 0)
539 NMBCHOTITEM nmhotitem
;
542 set_button_state(hWnd
, state
);
544 nmhotitem
.hdr
.hwndFrom
= hWnd
;
545 nmhotitem
.hdr
.idFrom
= GetWindowLongPtrW (hWnd
, GWLP_ID
);
546 nmhotitem
.hdr
.code
= BCN_HOTITEMCHANGE
;
547 nmhotitem
.dwFlags
= HICF_ENTERING
;
548 SendMessageW(GetParent(hWnd
), WM_NOTIFY
, nmhotitem
.hdr
.idFrom
, (LPARAM
)&nmhotitem
);
550 InvalidateRect(hWnd
, NULL
, TRUE
);
553 mouse_event
.cbSize
= sizeof(TRACKMOUSEEVENT
);
554 mouse_event
.dwFlags
= TME_QUERY
;
555 if(!TrackMouseEvent(&mouse_event
) || !(mouse_event
.dwFlags
&TME_LEAVE
))
557 mouse_event
.dwFlags
= TME_LEAVE
;
558 mouse_event
.hwndTrack
= hWnd
;
559 mouse_event
.dwHoverTime
= 1;
560 TrackMouseEvent(&mouse_event
);
564 case BCM_GETTEXTMARGIN
:
566 RECT
* prc
= (RECT
*)lParam
;
567 PBUTTON_DATA data
= _GetButtonData(hWnd
);
570 *prc
= data
->rcTextMargin
;
573 case BCM_SETTEXTMARGIN
:
575 RECT
* prc
= (RECT
*)lParam
;
576 PBUTTON_DATA data
= _GetButtonData(hWnd
);
579 data
->rcTextMargin
= *prc
;
582 case BCM_SETIMAGELIST
:
584 BUTTON_IMAGELIST
* pimldata
= (BUTTON_IMAGELIST
*)lParam
;
585 PBUTTON_DATA data
= _GetButtonData(hWnd
);
586 if (!data
|| !pimldata
|| !pimldata
->himl
)
588 data
->imlData
= *pimldata
;
591 case BCM_GETIMAGELIST
:
593 BUTTON_IMAGELIST
* pimldata
= (BUTTON_IMAGELIST
*)lParam
;
594 PBUTTON_DATA data
= _GetButtonData(hWnd
);
595 if (!data
|| !pimldata
)
597 *pimldata
= data
->imlData
;
600 case BCM_GETIDEALSIZE
:
602 HTHEME theme
= GetWindowTheme(hWnd
);
604 SIZE
* pSize
= (SIZE
*)lParam
;
606 if (btn_type
== BS_PUSHBUTTON
||
607 btn_type
== BS_DEFPUSHBUTTON
||
608 btn_type
== BS_USERBUTTON
)
610 ret
= BUTTON_GetIdealSize(theme
, hWnd
, pSize
);
615 GetClientRect(hWnd
, &rect
);
616 pSize
->cx
= rect
.right
;
617 pSize
->cy
= rect
.bottom
;
624 if (!_GetButtonData(hWnd
))
627 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
628 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
639 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
640 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
642 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
643 case BS_GROUPBOX
: return DLGC_STATIC
;
644 default: return DLGC_BUTTON
;
648 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
652 if (btn_type
>= MAX_BTN_TYPE
)
653 return -1; /* abort */
655 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
656 if (btn_type
== BS_USERBUTTON
)
658 style
= (style
& ~BS_TYPEMASK
) | BS_PUSHBUTTON
;
660 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
662 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
665 set_button_state( hWnd
, BST_UNCHECKED
);
667 button_update_uistate( hWnd
, unicode
);
671 #if defined(__REACTOS__) && defined(_USER32_)
673 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
678 if (btn_type
== BS_OWNERDRAW
)
680 HDC hdc
= (HDC
)wParam
;
683 HWND parent
= GetParent(hWnd
);
684 if (!parent
) parent
= hWnd
;
685 #if defined(__REACTOS__) && defined(_USER32_)
686 hBrush
= GetControlColor( parent
, hWnd
, hdc
, WM_CTLCOLORBTN
);
688 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
689 if (!hBrush
) /* did the app forget to call defwindowproc ? */
690 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
691 (WPARAM
)hdc
, (LPARAM
)hWnd
);
693 GetClientRect(hWnd
, &rc
);
694 FillRect(hdc
, &rc
, hBrush
);
702 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
704 HTHEME theme
= GetWindowTheme(hWnd
);
705 if (theme
&& BUTTON_PaintWithTheme(theme
, hWnd
, hdc
, uMsg
== WM_PRINTCLIENT
? lParam
: 0))
707 if ( !wParam
) EndPaint( hWnd
, &ps
);
711 if (btnPaintFunc
[btn_type
])
713 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
714 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
715 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
717 if ( !wParam
) EndPaint( hWnd
, &ps
);
722 if (wParam
== VK_SPACE
)
724 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
725 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
730 case WM_LBUTTONDBLCLK
:
731 if(style
& BS_NOTIFY
||
732 btn_type
== BS_RADIOBUTTON
||
733 btn_type
== BS_USERBUTTON
||
734 btn_type
== BS_OWNERDRAW
)
736 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
743 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
744 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
748 if (wParam
!= VK_SPACE
)
753 BOOL TellParent
= FALSE
; //// ReactOS see note below.
755 state
= get_button_state( hWnd
);
756 if (!(state
& BUTTON_BTNPRESSED
)) break;
757 state
&= BUTTON_NSTATES
;
758 set_button_state( hWnd
, state
);
759 if (!(state
& BST_PUSHED
))
764 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
765 GetClientRect( hWnd
, &rect
);
766 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
768 state
= get_button_state( hWnd
);
771 case BS_AUTOCHECKBOX
:
772 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BST_CHECKED
), 0 );
774 case BS_AUTORADIOBUTTON
:
775 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
778 SendMessageW( hWnd
, BM_SETCHECK
,
779 (state
& BST_INDETERMINATE
) ? 0 : ((state
& 3) + 1), 0 );
783 TellParent
= TRUE
; // <---- Fix CORE-10194, Notify parent after capture is released.
786 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
791 if (TellParent
) BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
800 case WM_CAPTURECHANGED
:
801 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
802 if (hWnd
== (HWND
)lParam
) break;
803 state
= get_button_state( hWnd
);
804 if (state
& BUTTON_BTNPRESSED
)
806 state
&= BUTTON_NSTATES
;
807 set_button_state( hWnd
, state
);
808 if (state
& BST_PUSHED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
813 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
815 GetClientRect( hWnd
, &rect
);
816 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
822 /* Clear an old text here as Windows does */
825 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
826 // Patch: http://source.winehq.org/patches/data/70889
827 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
830 if (style
& WS_VISIBLE
)
832 if (IsWindowVisible(hWnd
))
835 HDC hdc
= GetDC(hWnd
);
838 HWND parent
= GetParent(hWnd
);
839 UINT message
= (btn_type
== BS_PUSHBUTTON
||
840 btn_type
== BS_DEFPUSHBUTTON
||
841 btn_type
== BS_PUSHLIKE
||
842 btn_type
== BS_USERBUTTON
||
843 btn_type
== BS_OWNERDRAW
) ?
844 WM_CTLCOLORBTN
: WM_CTLCOLORSTATIC
;
846 if (!parent
) parent
= hWnd
;
847 #if defined(__REACTOS__) && defined(_USER32_)
848 hbrush
= GetControlColor(parent
, hWnd
, hdc
, message
);
850 hbrush
= (HBRUSH
)SendMessageW(parent
, message
,
851 (WPARAM
)hdc
, (LPARAM
)hWnd
);
852 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
853 hbrush
= (HBRUSH
)DefWindowProcW(parent
, message
,
854 (WPARAM
)hdc
, (LPARAM
)hWnd
);
857 GetClientRect(hWnd
, &client
);
859 /* FIXME: check other BS_* handlers */
860 if (btn_type
== BS_GROUPBOX
)
861 InflateRect(&rc
, -7, 1); /* GB_Paint does this */
862 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
863 /* Clip by client rect bounds */
864 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
865 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
866 FillRect(hdc
, &rc
, hbrush
);
867 ReleaseDC(hWnd
, hdc
);
870 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
871 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
872 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
873 InvalidateRect( hWnd
, NULL
, TRUE
);
875 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
876 return 1; /* success. FIXME: check text length */
880 set_button_font( hWnd
, (HFONT
)wParam
);
881 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
885 return (LRESULT
)get_button_font( hWnd
);
888 TRACE("WM_SETFOCUS %p\n",hWnd
);
889 set_button_state( hWnd
, get_button_state(hWnd
) | BST_FOCUS
);
891 if (btn_type
!= BS_OWNERDRAW
)
892 InvalidateRect(hWnd
, NULL
, FALSE
);
895 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
896 if (style
& BS_NOTIFY
)
897 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
901 TRACE("WM_KILLFOCUS %p\n",hWnd
);
902 state
= get_button_state( hWnd
);
903 set_button_state( hWnd
, state
& ~BST_FOCUS
);
905 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
908 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
910 if (style
& BS_NOTIFY
)
911 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
913 InvalidateRect( hWnd
, NULL
, FALSE
);
916 case WM_SYSCOLORCHANGE
:
917 InvalidateRect( hWnd
, NULL
, FALSE
);
921 btn_type
= wParam
& BS_TYPEMASK
;
922 style
= (style
& ~BS_TYPEMASK
) | btn_type
;
924 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
926 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
929 /* Only redraw if lParam flag is set.*/
931 InvalidateRect( hWnd
, NULL
, TRUE
);
937 state
= get_button_state(hWnd
);
938 if (state
& BUTTON_BMCLICK
)
940 set_button_state(hWnd
, state
| BUTTON_BMCLICK
); // Tracked in STATE_GWL_OFFSET.
942 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
943 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
945 state
= get_button_state(hWnd
);
946 if (!(state
& BUTTON_BMCLICK
)) break;
947 state
&= ~BUTTON_BMCLICK
;
948 set_button_state(hWnd
, state
);
953 /* Check that image format matches button style */
954 switch (style
& (BS_BITMAP
|BS_ICON
))
957 if (wParam
!= IMAGE_BITMAP
) return 0;
960 if (wParam
!= IMAGE_ICON
) return 0;
966 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
968 oldHbitmap
= (HBITMAP
)set_button_image(hWnd
, lParam
);
970 InvalidateRect( hWnd
, NULL
, FALSE
);
971 return (LRESULT
)oldHbitmap
;
975 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
977 return get_button_image(hWnd
);
981 return get_button_state( hWnd
) & 3;
984 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
985 state
= get_button_state( hWnd
);
986 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
989 if (wParam
) style
|= WS_TABSTOP
;
990 else style
&= ~WS_TABSTOP
;
991 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
993 if (wParam
) WIN_SetStyle( hWnd
, WS_TABSTOP
, 0 );
994 else WIN_SetStyle( hWnd
, 0, WS_TABSTOP
);
997 if ((state
& 3) != wParam
)
999 set_button_state( hWnd
, (state
& ~3) | wParam
);
1001 paint_button( hWnd
, btn_type
, ODA_SELECT
);
1003 InvalidateRect(hWnd
, NULL
, FALSE
);
1006 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BST_CHECKED
) && (style
& WS_CHILD
))
1007 BUTTON_CheckAutoRadioButton( hWnd
);
1011 return get_button_state( hWnd
);
1014 state
= get_button_state( hWnd
);
1016 set_button_state( hWnd
, state
| BST_PUSHED
);
1018 set_button_state( hWnd
, state
& ~BST_PUSHED
);
1021 paint_button( hWnd
, btn_type
, ODA_SELECT
);
1023 InvalidateRect(hWnd
, NULL
, FALSE
);
1028 case WM_UPDATEUISTATE
:
1030 DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
1032 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
1034 if (button_update_uistate(hWnd
, unicode
))
1035 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
1040 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
1043 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
1044 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
1051 /***********************************************************************
1053 * The button window procedure. This is just a wrapper which locks
1054 * the passed HWND and calls the real window procedure (with a WND*
1055 * pointer pointing to the locked windowstructure).
1057 LRESULT WINAPI
ButtonWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1059 if (!IsWindow(hWnd
)) return 0;
1060 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);
1063 /***********************************************************************
1066 LRESULT WINAPI
ButtonWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1068 if (!IsWindow(hWnd
)) return 0;
1069 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
1072 #endif /* __REACTOS__ */
1074 /**********************************************************************
1075 * Convert button styles to flags used by DrawText.
1077 static UINT
BUTTON_BStoDT( DWORD style
, DWORD ex_style
)
1079 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
1081 /* "Convert" pushlike buttons to pushbuttons */
1082 if (style
& BS_PUSHLIKE
)
1083 style
&= ~BS_TYPEMASK
;
1085 if (!(style
& BS_MULTILINE
))
1086 dtStyle
|= DT_SINGLELINE
;
1088 dtStyle
|= DT_WORDBREAK
;
1090 switch (style
& BS_CENTER
)
1092 case BS_LEFT
: /* DT_LEFT is 0 */ break;
1093 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
1094 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
1096 /* Pushbutton's text is centered by default */
1097 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
1098 /* all other flavours have left aligned text */
1101 if (ex_style
& WS_EX_RIGHT
) dtStyle
= DT_RIGHT
| (dtStyle
& ~(DT_LEFT
| DT_CENTER
));
1103 /* DrawText ignores vertical alignment for multiline text,
1104 * but we use these flags to align label manually.
1106 if (get_button_type(style
) != BS_GROUPBOX
)
1108 switch (style
& BS_VCENTER
)
1110 case BS_TOP
: /* DT_TOP is 0 */ break;
1111 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
1112 case BS_VCENTER
: /* fall through */
1113 default: dtStyle
|= DT_VCENTER
; break;
1117 /* GroupBox's text is always single line and is top aligned. */
1118 dtStyle
|= DT_SINGLELINE
;
1123 /**********************************************************************
1124 * BUTTON_CalcLabelRect
1126 * Calculates label's rectangle depending on button style.
1128 * Returns flags to be passed to DrawText.
1129 * Calculated rectangle doesn't take into account button state
1130 * (pushed, etc.). If there is nothing to draw (no text/image) output
1131 * rectangle is empty, and return value is (UINT)-1.
1133 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
1135 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1136 LONG ex_style
= GetWindowLongPtrW( hwnd
, GWL_EXSTYLE
);
1140 UINT dtStyle
= BUTTON_BStoDT( style
, ex_style
);
1144 /* Calculate label rectangle according to label type */
1145 switch (style
& (BS_ICON
|BS_BITMAP
))
1149 HFONT hFont
, hPrevFont
= 0;
1151 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
1154 HeapFree( GetProcessHeap(), 0, text
);
1158 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hdc
, hFont
);
1159 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
1160 if (hPrevFont
) SelectObject( hdc
, hPrevFont
);
1161 HeapFree( GetProcessHeap(), 0, text
);
1163 if (get_ui_state(hwnd
) & UISF_HIDEACCEL
)
1164 dtStyle
|= DT_HIDEPREFIX
;
1171 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
1173 if (!GetIconInfo((HICON
)get_button_image(hwnd
), &iconInfo
))
1177 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
1179 r
.right
= r
.left
+ bm
.bmWidth
;
1180 r
.bottom
= r
.top
+ bm
.bmHeight
;
1182 DeleteObject(iconInfo
.hbmColor
);
1183 DeleteObject(iconInfo
.hbmMask
);
1188 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
1190 if (!GetObjectW( (HANDLE
)get_button_image(hwnd
), sizeof(BITMAP
), &bm
))
1194 r
.right
= r
.left
+ bm
.bmWidth
;
1195 r
.bottom
= r
.top
+ bm
.bmHeight
;
1205 /* Position label inside bounding rectangle according to
1206 * alignment flags. (calculated rect is always left-top aligned).
1207 * If label is aligned to any side - shift label in opposite
1208 * direction to leave extra space for focus rectangle.
1210 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
1212 case DT_LEFT
: r
.left
++; r
.right
++; break;
1213 case DT_CENTER
: n
= r
.right
- r
.left
;
1214 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
1215 r
.right
= r
.left
+ n
; break;
1216 case DT_RIGHT
: n
= r
.right
- r
.left
;
1217 r
.right
= rc
->right
- 1;
1218 r
.left
= r
.right
- n
;
1222 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
1224 case DT_TOP
: r
.top
++; r
.bottom
++; break;
1225 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
1226 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
1227 r
.bottom
= r
.top
+ n
; break;
1228 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
1229 r
.bottom
= rc
->bottom
- 1;
1230 r
.top
= r
.bottom
- n
;
1239 /**********************************************************************
1240 * BUTTON_DrawTextCallback
1242 * Callback function used by DrawStateW function.
1244 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
1249 SetRect(&rc
, 0, 0, cx
, cy
);
1250 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
1253 HWND hwnd
= (HWND
)lp
;
1255 PBUTTON_DATA pdata
= _GetButtonData(hwnd
);
1259 if (!(text
= get_button_text( hwnd
))) return TRUE
;
1261 SetRect(&rc
, 0, 0, cx
, cy
);
1263 if (pdata
->imlData
.himl
&& ImageList_GetIconSize(pdata
->imlData
.himl
, &ImageSize
.cx
, &ImageSize
.cy
))
1265 int left
= pdata
->imlData
.margin
.left
;
1266 int top
= (cy
- ImageSize
.cy
) / 2;
1267 rc
.left
+= pdata
->imlData
.margin
.left
+ pdata
->imlData
.margin
.right
+ ImageSize
.cy
;
1268 ImageList_Draw(pdata
->imlData
.himl
, 0, hdc
, left
, top
, 0);
1271 DrawTextW(hdc
, text
, -1, &rc
, (UINT
)wp
);
1277 /**********************************************************************
1280 * Common function for drawing button label.
1282 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
1284 DRAWSTATEPROC lpOutputProc
= NULL
;
1288 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
1289 LONG state
= get_button_state( hwnd
);
1290 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1293 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
1294 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
1295 * I don't have Win31 on hand to verify that, so I leave it as is.
1298 if ((style
& BS_PUSHLIKE
) && (state
& BST_INDETERMINATE
))
1300 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
1304 switch (style
& (BS_ICON
|BS_BITMAP
))
1307 /* DST_COMPLEX -- is 0 */
1308 lpOutputProc
= BUTTON_DrawTextCallback
;
1310 if (!(text
= get_button_text( hwnd
))) return;
1316 wp
= (WPARAM
)dtFlags
;
1319 if (dtFlags
& DT_HIDEPREFIX
)
1320 flags
|= DSS_HIDEPREFIX
;
1327 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
1329 lp
= get_button_image(hwnd
);
1334 flags
|= DST_BITMAP
;
1336 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
1338 lp
= get_button_image(hwnd
);
1346 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
1347 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
1348 HeapFree( GetProcessHeap(), 0, text
);
1351 /**********************************************************************
1352 * Push Button Functions
1354 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1357 UINT dtFlags
, uState
;
1361 COLORREF oldTxtColor
;
1363 LONG state
= get_button_state( hwnd
);
1364 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1365 BOOL pushedState
= (state
& BST_PUSHED
);
1369 GetClientRect( hwnd
, &rc
);
1371 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
1372 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1373 parent
= GetParent(hwnd
);
1374 if (!parent
) parent
= hwnd
;
1375 #if defined(__REACTOS__) && defined(_USER32_)
1376 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1378 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1381 hrgn
= set_control_clipping( hDC
, &rc
);
1383 hOldPen
= SelectObject(hDC
, GetStockObject(DC_PEN
));
1384 SetDCPenColor(hDC
, GetSysColor(COLOR_WINDOWFRAME
));
1386 hOldPen
= SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
1388 hOldBrush
= SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
1389 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
1391 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
1393 if (action
!= ODA_FOCUS
)
1394 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
1395 InflateRect( &rc
, -1, -1 );
1398 /* completely skip the drawing if only focus has changed */
1399 if (action
== ODA_FOCUS
) goto draw_focus
;
1401 uState
= DFCS_BUTTONPUSH
;
1403 if (style
& BS_FLAT
)
1404 uState
|= DFCS_MONO
;
1405 else if (pushedState
)
1407 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
1408 uState
|= DFCS_FLAT
;
1410 uState
|= DFCS_PUSHED
;
1413 if (state
& (BST_CHECKED
| BST_INDETERMINATE
))
1414 uState
|= DFCS_CHECKED
;
1416 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
1418 /* draw button label */
1420 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
1422 if (dtFlags
== (UINT
)-1L)
1426 OffsetRect(&r
, 1, 1);
1428 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
1430 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
1432 SetTextColor( hDC
, oldTxtColor
);
1435 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1438 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1441 InflateRect( &rc
, -2, -2 );
1442 DrawFocusRect( hDC
, &rc
);
1449 SelectObject( hDC
, hOldPen
);
1450 SelectObject( hDC
, hOldBrush
);
1451 SetBkMode(hDC
, oldBkMode
);
1452 SelectClipRgn( hDC
, hrgn
);
1453 if (hrgn
) DeleteObject( hrgn
);
1456 /**********************************************************************
1457 * Check Box & Radio Button Functions
1460 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1462 RECT rbox
, rtext
, client
;
1464 int delta
, text_offset
, checkBoxWidth
, checkBoxHeight
;
1467 LONG state
= get_button_state( hwnd
);
1468 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1469 LONG ex_style
= GetWindowLongW( hwnd
, GWL_EXSTYLE
);
1473 if (style
& BS_PUSHLIKE
)
1475 PB_Paint( hwnd
, hDC
, action
);
1479 GetClientRect(hwnd
, &client
);
1480 rbox
= rtext
= client
;
1482 checkBoxWidth
= 12 * GetDeviceCaps( hDC
, LOGPIXELSX
) / 96 + 1;
1483 checkBoxHeight
= 12 * GetDeviceCaps( hDC
, LOGPIXELSY
) / 96 + 1;
1485 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1486 GetCharWidthW( hDC
, '0', '0', &text_offset
);
1489 parent
= GetParent(hwnd
);
1490 if (!parent
) parent
= hwnd
;
1491 #if defined(__REACTOS__) && defined(_USER32_)
1492 hBrush
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1494 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
1495 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1496 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1497 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1498 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1500 hrgn
= set_control_clipping( hDC
, &client
);
1502 if (style
& BS_LEFTTEXT
|| ex_style
& WS_EX_RIGHT
)
1504 /* magic +4 is what CTL3D expects */
1506 rtext
.right
-= checkBoxWidth
+ text_offset
;;
1507 rbox
.left
= rbox
.right
- checkBoxWidth
;
1511 rtext
.left
+= checkBoxWidth
+ text_offset
;;
1512 rbox
.right
= checkBoxWidth
;
1515 /* Since WM_ERASEBKGND does nothing, first prepare background */
1516 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
1517 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
1521 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
1523 /* Only adjust rbox when rtext is valid */
1524 if (dtFlags
!= (UINT
)-1L)
1526 rbox
.top
= rtext
.top
;
1527 rbox
.bottom
= rtext
.bottom
;
1530 /* Draw the check-box bitmap */
1531 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
1535 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
1536 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
1537 else if (state
& BST_INDETERMINATE
) flags
= DFCS_BUTTON3STATE
;
1538 else flags
= DFCS_BUTTONCHECK
;
1540 if (state
& (BST_CHECKED
| BST_INDETERMINATE
)) flags
|= DFCS_CHECKED
;
1541 if (state
& BST_PUSHED
) flags
|= DFCS_PUSHED
;
1543 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
1545 /* rbox must have the correct height */
1546 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
1548 if (style
& BS_TOP
) {
1550 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1552 rbox
.top
-= -delta
/2 + 1;
1553 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1555 } else if (style
& BS_BOTTOM
) {
1557 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1559 rbox
.bottom
+= -delta
/2 + 1;
1560 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1562 } else { /* Default */
1564 int ofs
= (delta
/ 2);
1565 rbox
.bottom
-= ofs
+ 1;
1566 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1567 } else if (delta
< 0) {
1568 int ofs
= (-delta
/ 2);
1569 rbox
.top
-= ofs
+ 1;
1570 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1574 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
1577 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
1580 if (action
== ODA_DRAWENTIRE
)
1581 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
1584 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1587 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1592 IntersectRect(&rtext
, &rtext
, &client
);
1593 DrawFocusRect( hDC
, &rtext
);
1598 SelectClipRgn( hDC
, hrgn
);
1599 if (hrgn
) DeleteObject( hrgn
);
1603 /**********************************************************************
1604 * BUTTON_CheckAutoRadioButton
1606 * hwnd is checked, uncheck every other auto radio button in group
1608 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1610 HWND parent
, sibling
, start
;
1612 parent
= GetParent(hwnd
);
1613 /* make sure that starting control is not disabled or invisible */
1614 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1617 if (!sibling
) break;
1618 if ((hwnd
!= sibling
) &&
1619 ((GetWindowLongPtrW( sibling
, GWL_STYLE
) & BS_TYPEMASK
) == BS_AUTORADIOBUTTON
))
1620 SendMessageW( sibling
, BM_SETCHECK
, BST_UNCHECKED
, 0 );
1621 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1622 } while (sibling
!= start
);
1626 /**********************************************************************
1627 * Group Box Functions
1630 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1637 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1641 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1642 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1643 parent
= GetParent(hwnd
);
1644 if (!parent
) parent
= hwnd
;
1645 #if defined(__REACTOS__) && defined(_USER32_)
1646 hbr
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1648 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1649 if (!hbr
) /* did the app forget to call defwindowproc ? */
1650 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1651 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1653 GetClientRect( hwnd
, &rc
);
1655 hrgn
= set_control_clipping( hDC
, &rc
);
1657 GetTextMetricsW (hDC
, &tm
);
1658 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1659 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1661 InflateRect(&rc
, -7, 1);
1662 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1664 if (dtFlags
!= (UINT
)-1L)
1666 /* Because buttons have CS_PARENTDC class style, there is a chance
1667 * that label will be drawn out of client rect.
1668 * But Windows doesn't clip label's rect, so do I.
1671 /* There is 1-pixel margin at the left, right, and bottom */
1672 rc
.left
--; rc
.right
++; rc
.bottom
++;
1673 FillRect(hDC
, &rc
, hbr
);
1674 rc
.left
++; rc
.right
--; rc
.bottom
--;
1676 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1678 SelectClipRgn( hDC
, hrgn
);
1679 if (hrgn
) DeleteObject( hrgn
);
1683 /**********************************************************************
1684 * User Button Functions
1687 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1692 LONG state
= get_button_state( hwnd
);
1695 GetClientRect( hwnd
, &rc
);
1697 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1699 parent
= GetParent(hwnd
);
1700 if (!parent
) parent
= hwnd
;
1701 #if defined(__REACTOS__) && defined(_USER32_)
1702 hBrush
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1704 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1705 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1706 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1707 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1710 FillRect( hDC
, &rc
, hBrush
);
1711 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1714 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1716 DrawFocusRect( hDC
, &rc
);
1724 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_FOCUS
) ? BN_SETFOCUS
: BN_KILLFOCUS
);
1728 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_PUSHED
) ? BN_HILITE
: BN_UNHILITE
);
1732 BUTTON_NOTIFY_PARENT( hwnd
, BN_PAINT
);
1738 /**********************************************************************
1739 * Ownerdrawn Button Functions
1742 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1744 LONG state
= get_button_state( hwnd
);
1746 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1748 HFONT hFont
, hPrevFont
= 0;
1751 dis
.CtlType
= ODT_BUTTON
;
1754 dis
.itemAction
= action
;
1755 dis
.itemState
= ((state
& BST_FOCUS
) ? ODS_FOCUS
: 0) |
1756 ((state
& BST_PUSHED
) ? ODS_SELECTED
: 0) |
1757 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1758 dis
.hwndItem
= hwnd
;
1761 GetClientRect( hwnd
, &dis
.rcItem
);
1763 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1764 parent
= GetParent(hwnd
);
1765 if (!parent
) parent
= hwnd
;
1766 #if defined(__REACTOS__) && defined(_USER32_)
1767 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1769 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1772 hrgn
= set_control_clipping( hDC
, &dis
.rcItem
);
1774 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1775 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1776 SelectClipRgn( hDC
, hrgn
);
1777 if (hrgn
) DeleteObject( hrgn
);
1781 void BUTTON_Register()
1785 ZeroMemory(&wndClass
, sizeof(WNDCLASSW
));
1786 wndClass
.style
= CS_GLOBALCLASS
| CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
;
1787 wndClass
.lpfnWndProc
= ButtonWndProcW
;
1788 wndClass
.cbClsExtra
= 0;
1789 wndClass
.cbWndExtra
= sizeof(PBUTTON_DATA
);
1790 wndClass
.hCursor
= LoadCursorW(0, (LPCWSTR
)IDC_ARROW
);
1791 wndClass
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
1792 wndClass
.lpszClassName
= WC_BUTTONW
;
1794 RegisterClassW(&wndClass
);
1797 void BUTTON_Unregister()
1799 UnregisterClassW(WC_BUTTONW
, NULL
);