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 && wcscmp(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
, FALSE
);
515 int state
= (int)SendMessageW(hWnd
, BM_GETSTATE
, 0, 0);
516 set_button_state(hWnd
, state
|BST_HOT
);
517 InvalidateRect(hWnd
, NULL
, FALSE
);
522 int state
= (int)SendMessageW(hWnd
, BM_GETSTATE
, 0, 0);
523 set_button_state(hWnd
, state
&(~BST_HOT
));
524 InvalidateRect(hWnd
, NULL
, FALSE
);
529 TRACKMOUSEEVENT mouse_event
;
530 mouse_event
.cbSize
= sizeof(TRACKMOUSEEVENT
);
531 mouse_event
.dwFlags
= TME_QUERY
;
532 if(!TrackMouseEvent(&mouse_event
) || !(mouse_event
.dwFlags
&(TME_HOVER
|TME_LEAVE
)))
534 mouse_event
.dwFlags
= TME_HOVER
|TME_LEAVE
;
535 mouse_event
.hwndTrack
= hWnd
;
536 mouse_event
.dwHoverTime
= 1;
537 TrackMouseEvent(&mouse_event
);
541 case BCM_GETTEXTMARGIN
:
543 RECT
* prc
= (RECT
*)lParam
;
544 PBUTTON_DATA data
= _GetButtonData(hWnd
);
547 *prc
= data
->rcTextMargin
;
550 case BCM_SETTEXTMARGIN
:
552 RECT
* prc
= (RECT
*)lParam
;
553 PBUTTON_DATA data
= _GetButtonData(hWnd
);
556 data
->rcTextMargin
= *prc
;
559 case BCM_SETIMAGELIST
:
561 BUTTON_IMAGELIST
* pimldata
= (BUTTON_IMAGELIST
*)lParam
;
562 PBUTTON_DATA data
= _GetButtonData(hWnd
);
563 if (!data
|| !pimldata
|| !pimldata
->himl
)
565 data
->imlData
= *pimldata
;
568 case BCM_GETIMAGELIST
:
570 BUTTON_IMAGELIST
* pimldata
= (BUTTON_IMAGELIST
*)lParam
;
571 PBUTTON_DATA data
= _GetButtonData(hWnd
);
572 if (!data
|| !pimldata
)
574 *pimldata
= data
->imlData
;
577 case BCM_GETIDEALSIZE
:
579 HTHEME theme
= GetWindowTheme(hWnd
);
581 SIZE
* pSize
= (SIZE
*)lParam
;
583 if (btn_type
== BS_PUSHBUTTON
||
584 btn_type
== BS_DEFPUSHBUTTON
||
585 btn_type
== BS_USERBUTTON
)
587 ret
= BUTTON_GetIdealSize(theme
, hWnd
, pSize
);
592 GetClientRect(hWnd
, &rect
);
593 pSize
->cx
= rect
.right
;
594 pSize
->cy
= rect
.bottom
;
601 if (!_GetButtonData(hWnd
))
604 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
605 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
616 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
617 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
619 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
620 case BS_GROUPBOX
: return DLGC_STATIC
;
621 default: return DLGC_BUTTON
;
625 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
629 if (btn_type
>= MAX_BTN_TYPE
)
630 return -1; /* abort */
632 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
633 if (btn_type
== BS_USERBUTTON
)
635 style
= (style
& ~BS_TYPEMASK
) | BS_PUSHBUTTON
;
637 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
639 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
642 set_button_state( hWnd
, BST_UNCHECKED
);
644 button_update_uistate( hWnd
, unicode
);
648 #if defined(__REACTOS__) && defined(_USER32_)
650 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
655 if (btn_type
== BS_OWNERDRAW
)
657 HDC hdc
= (HDC
)wParam
;
660 HWND parent
= GetParent(hWnd
);
661 if (!parent
) parent
= hWnd
;
662 #if defined(__REACTOS__) && defined(_USER32_)
663 hBrush
= GetControlColor( parent
, hWnd
, hdc
, WM_CTLCOLORBTN
);
665 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
666 if (!hBrush
) /* did the app forget to call defwindowproc ? */
667 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
668 (WPARAM
)hdc
, (LPARAM
)hWnd
);
670 GetClientRect(hWnd
, &rc
);
671 FillRect(hdc
, &rc
, hBrush
);
679 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
681 HTHEME theme
= GetWindowTheme(hWnd
);
682 if (theme
&& BUTTON_PaintWithTheme(theme
, hWnd
, hdc
, uMsg
== WM_PRINTCLIENT
? lParam
: 0))
684 if ( !wParam
) EndPaint( hWnd
, &ps
);
688 if (btnPaintFunc
[btn_type
])
690 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
691 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
692 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
694 if ( !wParam
) EndPaint( hWnd
, &ps
);
699 if (wParam
== VK_SPACE
)
701 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
702 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
707 case WM_LBUTTONDBLCLK
:
708 if(style
& BS_NOTIFY
||
709 btn_type
== BS_RADIOBUTTON
||
710 btn_type
== BS_USERBUTTON
||
711 btn_type
== BS_OWNERDRAW
)
713 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
720 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
721 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
725 if (wParam
!= VK_SPACE
)
730 BOOL TellParent
= FALSE
; //// ReactOS see note below.
732 state
= get_button_state( hWnd
);
733 if (!(state
& BUTTON_BTNPRESSED
)) break;
734 state
&= BUTTON_NSTATES
;
735 set_button_state( hWnd
, state
);
736 if (!(state
& BST_PUSHED
))
741 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
742 GetClientRect( hWnd
, &rect
);
743 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
745 state
= get_button_state( hWnd
);
748 case BS_AUTOCHECKBOX
:
749 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BST_CHECKED
), 0 );
751 case BS_AUTORADIOBUTTON
:
752 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
755 SendMessageW( hWnd
, BM_SETCHECK
,
756 (state
& BST_INDETERMINATE
) ? 0 : ((state
& 3) + 1), 0 );
760 TellParent
= TRUE
; // <---- Fix CORE-10194, Notify parent after capture is released.
763 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
768 if (TellParent
) BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
777 case WM_CAPTURECHANGED
:
778 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
779 if (hWnd
== (HWND
)lParam
) break;
780 state
= get_button_state( hWnd
);
781 if (state
& BUTTON_BTNPRESSED
)
783 state
&= BUTTON_NSTATES
;
784 set_button_state( hWnd
, state
);
785 if (state
& BST_PUSHED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
790 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
792 GetClientRect( hWnd
, &rect
);
793 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
799 /* Clear an old text here as Windows does */
802 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
803 // Patch: http://source.winehq.org/patches/data/70889
804 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
807 if (style
& WS_VISIBLE
)
809 if (IsWindowVisible(hWnd
))
812 HDC hdc
= GetDC(hWnd
);
815 HWND parent
= GetParent(hWnd
);
816 UINT message
= (btn_type
== BS_PUSHBUTTON
||
817 btn_type
== BS_DEFPUSHBUTTON
||
818 btn_type
== BS_PUSHLIKE
||
819 btn_type
== BS_USERBUTTON
||
820 btn_type
== BS_OWNERDRAW
) ?
821 WM_CTLCOLORBTN
: WM_CTLCOLORSTATIC
;
823 if (!parent
) parent
= hWnd
;
824 #if defined(__REACTOS__) && defined(_USER32_)
825 hbrush
= GetControlColor(parent
, hWnd
, hdc
, message
);
827 hbrush
= (HBRUSH
)SendMessageW(parent
, message
,
828 (WPARAM
)hdc
, (LPARAM
)hWnd
);
829 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
830 hbrush
= (HBRUSH
)DefWindowProcW(parent
, message
,
831 (WPARAM
)hdc
, (LPARAM
)hWnd
);
834 GetClientRect(hWnd
, &client
);
836 /* FIXME: check other BS_* handlers */
837 if (btn_type
== BS_GROUPBOX
)
838 InflateRect(&rc
, -7, 1); /* GB_Paint does this */
839 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
840 /* Clip by client rect bounds */
841 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
842 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
843 FillRect(hdc
, &rc
, hbrush
);
844 ReleaseDC(hWnd
, hdc
);
847 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
848 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
849 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
850 InvalidateRect( hWnd
, NULL
, TRUE
);
852 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
853 return 1; /* success. FIXME: check text length */
857 set_button_font( hWnd
, (HFONT
)wParam
);
858 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
862 return (LRESULT
)get_button_font( hWnd
);
865 TRACE("WM_SETFOCUS %p\n",hWnd
);
866 set_button_state( hWnd
, get_button_state(hWnd
) | BST_FOCUS
);
868 if (btn_type
!= BS_OWNERDRAW
)
869 InvalidateRect(hWnd
, NULL
, FALSE
);
872 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
873 if (style
& BS_NOTIFY
)
874 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
878 TRACE("WM_KILLFOCUS %p\n",hWnd
);
879 state
= get_button_state( hWnd
);
880 set_button_state( hWnd
, state
& ~BST_FOCUS
);
882 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
885 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
887 if (style
& BS_NOTIFY
)
888 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
890 InvalidateRect( hWnd
, NULL
, FALSE
);
893 case WM_SYSCOLORCHANGE
:
894 InvalidateRect( hWnd
, NULL
, FALSE
);
898 btn_type
= wParam
& BS_TYPEMASK
;
899 style
= (style
& ~BS_TYPEMASK
) | btn_type
;
901 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
903 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
906 /* Only redraw if lParam flag is set.*/
908 InvalidateRect( hWnd
, NULL
, TRUE
);
914 state
= get_button_state(hWnd
);
915 if (state
& BUTTON_BMCLICK
)
917 set_button_state(hWnd
, state
| BUTTON_BMCLICK
); // Tracked in STATE_GWL_OFFSET.
919 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
920 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
922 state
= get_button_state(hWnd
);
923 if (!(state
& BUTTON_BMCLICK
)) break;
924 state
&= ~BUTTON_BMCLICK
;
925 set_button_state(hWnd
, state
);
930 /* Check that image format matches button style */
931 switch (style
& (BS_BITMAP
|BS_ICON
))
934 if (wParam
!= IMAGE_BITMAP
) return 0;
937 if (wParam
!= IMAGE_ICON
) return 0;
943 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
945 oldHbitmap
= (HBITMAP
)set_button_image(hWnd
, lParam
);
947 InvalidateRect( hWnd
, NULL
, FALSE
);
948 return (LRESULT
)oldHbitmap
;
952 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
954 return get_button_image(hWnd
);
958 return get_button_state( hWnd
) & 3;
961 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
962 state
= get_button_state( hWnd
);
963 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
966 if (wParam
) style
|= WS_TABSTOP
;
967 else style
&= ~WS_TABSTOP
;
968 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
970 if (wParam
) WIN_SetStyle( hWnd
, WS_TABSTOP
, 0 );
971 else WIN_SetStyle( hWnd
, 0, WS_TABSTOP
);
974 if ((state
& 3) != wParam
)
976 set_button_state( hWnd
, (state
& ~3) | wParam
);
978 paint_button( hWnd
, btn_type
, ODA_SELECT
);
980 InvalidateRect(hWnd
, NULL
, FALSE
);
983 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BST_CHECKED
) && (style
& WS_CHILD
))
984 BUTTON_CheckAutoRadioButton( hWnd
);
988 return get_button_state( hWnd
);
991 state
= get_button_state( hWnd
);
993 set_button_state( hWnd
, state
| BST_PUSHED
);
995 set_button_state( hWnd
, state
& ~BST_PUSHED
);
998 paint_button( hWnd
, btn_type
, ODA_SELECT
);
1000 InvalidateRect(hWnd
, NULL
, FALSE
);
1005 case WM_UPDATEUISTATE
:
1007 DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
1009 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
1011 if (button_update_uistate(hWnd
, unicode
))
1012 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
1017 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
1020 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
1021 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
1028 /***********************************************************************
1030 * The button window procedure. This is just a wrapper which locks
1031 * the passed HWND and calls the real window procedure (with a WND*
1032 * pointer pointing to the locked windowstructure).
1034 LRESULT WINAPI
ButtonWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1036 if (!IsWindow(hWnd
)) return 0;
1037 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);
1040 /***********************************************************************
1043 LRESULT WINAPI
ButtonWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1045 if (!IsWindow(hWnd
)) return 0;
1046 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
1049 #endif /* __REACTOS__ */
1051 /**********************************************************************
1052 * Convert button styles to flags used by DrawText.
1054 static UINT
BUTTON_BStoDT( DWORD style
, DWORD ex_style
)
1056 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
1058 /* "Convert" pushlike buttons to pushbuttons */
1059 if (style
& BS_PUSHLIKE
)
1060 style
&= ~BS_TYPEMASK
;
1062 if (!(style
& BS_MULTILINE
))
1063 dtStyle
|= DT_SINGLELINE
;
1065 dtStyle
|= DT_WORDBREAK
;
1067 switch (style
& BS_CENTER
)
1069 case BS_LEFT
: /* DT_LEFT is 0 */ break;
1070 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
1071 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
1073 /* Pushbutton's text is centered by default */
1074 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
1075 /* all other flavours have left aligned text */
1078 if (ex_style
& WS_EX_RIGHT
) dtStyle
= DT_RIGHT
| (dtStyle
& ~(DT_LEFT
| DT_CENTER
));
1080 /* DrawText ignores vertical alignment for multiline text,
1081 * but we use these flags to align label manually.
1083 if (get_button_type(style
) != BS_GROUPBOX
)
1085 switch (style
& BS_VCENTER
)
1087 case BS_TOP
: /* DT_TOP is 0 */ break;
1088 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
1089 case BS_VCENTER
: /* fall through */
1090 default: dtStyle
|= DT_VCENTER
; break;
1094 /* GroupBox's text is always single line and is top aligned. */
1095 dtStyle
|= DT_SINGLELINE
;
1100 /**********************************************************************
1101 * BUTTON_CalcLabelRect
1103 * Calculates label's rectangle depending on button style.
1105 * Returns flags to be passed to DrawText.
1106 * Calculated rectangle doesn't take into account button state
1107 * (pushed, etc.). If there is nothing to draw (no text/image) output
1108 * rectangle is empty, and return value is (UINT)-1.
1110 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
1112 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1113 LONG ex_style
= GetWindowLongPtrW( hwnd
, GWL_EXSTYLE
);
1117 UINT dtStyle
= BUTTON_BStoDT( style
, ex_style
);
1121 /* Calculate label rectangle according to label type */
1122 switch (style
& (BS_ICON
|BS_BITMAP
))
1126 HFONT hFont
, hPrevFont
= 0;
1128 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
1131 HeapFree( GetProcessHeap(), 0, text
);
1135 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hdc
, hFont
);
1136 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
1137 if (hPrevFont
) SelectObject( hdc
, hPrevFont
);
1138 HeapFree( GetProcessHeap(), 0, text
);
1140 if (get_ui_state(hwnd
) & UISF_HIDEACCEL
)
1141 dtStyle
|= DT_HIDEPREFIX
;
1148 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
1150 if (!GetIconInfo((HICON
)get_button_image(hwnd
), &iconInfo
))
1154 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
1156 r
.right
= r
.left
+ bm
.bmWidth
;
1157 r
.bottom
= r
.top
+ bm
.bmHeight
;
1159 DeleteObject(iconInfo
.hbmColor
);
1160 DeleteObject(iconInfo
.hbmMask
);
1165 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
1167 if (!GetObjectW( (HANDLE
)get_button_image(hwnd
), sizeof(BITMAP
), &bm
))
1171 r
.right
= r
.left
+ bm
.bmWidth
;
1172 r
.bottom
= r
.top
+ bm
.bmHeight
;
1182 /* Position label inside bounding rectangle according to
1183 * alignment flags. (calculated rect is always left-top aligned).
1184 * If label is aligned to any side - shift label in opposite
1185 * direction to leave extra space for focus rectangle.
1187 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
1189 case DT_LEFT
: r
.left
++; r
.right
++; break;
1190 case DT_CENTER
: n
= r
.right
- r
.left
;
1191 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
1192 r
.right
= r
.left
+ n
; break;
1193 case DT_RIGHT
: n
= r
.right
- r
.left
;
1194 r
.right
= rc
->right
- 1;
1195 r
.left
= r
.right
- n
;
1199 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
1201 case DT_TOP
: r
.top
++; r
.bottom
++; break;
1202 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
1203 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
1204 r
.bottom
= r
.top
+ n
; break;
1205 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
1206 r
.bottom
= rc
->bottom
- 1;
1207 r
.top
= r
.bottom
- n
;
1216 /**********************************************************************
1217 * BUTTON_DrawTextCallback
1219 * Callback function used by DrawStateW function.
1221 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
1226 SetRect(&rc
, 0, 0, cx
, cy
);
1227 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
1230 HWND hwnd
= (HWND
)lp
;
1232 PBUTTON_DATA pdata
= _GetButtonData(hwnd
);
1236 if (!(text
= get_button_text( hwnd
))) return TRUE
;
1238 SetRect(&rc
, 0, 0, cx
, cy
);
1240 if (pdata
->imlData
.himl
&& ImageList_GetIconSize(pdata
->imlData
.himl
, &ImageSize
.cx
, &ImageSize
.cy
))
1242 int left
= pdata
->imlData
.margin
.left
;
1243 int top
= (cy
- ImageSize
.cy
) / 2;
1244 rc
.left
+= pdata
->imlData
.margin
.left
+ pdata
->imlData
.margin
.right
+ ImageSize
.cy
;
1245 ImageList_Draw(pdata
->imlData
.himl
, 0, hdc
, left
, top
, 0);
1248 DrawTextW(hdc
, text
, -1, &rc
, (UINT
)wp
);
1254 /**********************************************************************
1257 * Common function for drawing button label.
1259 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
1261 DRAWSTATEPROC lpOutputProc
= NULL
;
1265 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
1266 LONG state
= get_button_state( hwnd
);
1267 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1270 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
1271 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
1272 * I don't have Win31 on hand to verify that, so I leave it as is.
1275 if ((style
& BS_PUSHLIKE
) && (state
& BST_INDETERMINATE
))
1277 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
1281 switch (style
& (BS_ICON
|BS_BITMAP
))
1284 /* DST_COMPLEX -- is 0 */
1285 lpOutputProc
= BUTTON_DrawTextCallback
;
1287 if (!(text
= get_button_text( hwnd
))) return;
1293 wp
= (WPARAM
)dtFlags
;
1296 if (dtFlags
& DT_HIDEPREFIX
)
1297 flags
|= DSS_HIDEPREFIX
;
1304 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
1306 lp
= get_button_image(hwnd
);
1311 flags
|= DST_BITMAP
;
1313 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
1315 lp
= get_button_image(hwnd
);
1323 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
1324 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
1325 HeapFree( GetProcessHeap(), 0, text
);
1328 /**********************************************************************
1329 * Push Button Functions
1331 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1334 UINT dtFlags
, uState
;
1338 COLORREF oldTxtColor
;
1340 LONG state
= get_button_state( hwnd
);
1341 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1342 BOOL pushedState
= (state
& BST_PUSHED
);
1346 GetClientRect( hwnd
, &rc
);
1348 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
1349 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1350 parent
= GetParent(hwnd
);
1351 if (!parent
) parent
= hwnd
;
1352 #if defined(__REACTOS__) && defined(_USER32_)
1353 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1355 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1358 hrgn
= set_control_clipping( hDC
, &rc
);
1360 hOldPen
= SelectObject(hDC
, GetStockObject(DC_PEN
));
1361 SetDCPenColor(hDC
, GetSysColor(COLOR_WINDOWFRAME
));
1363 hOldPen
= SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
1365 hOldBrush
= SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
1366 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
1368 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
1370 if (action
!= ODA_FOCUS
)
1371 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
1372 InflateRect( &rc
, -1, -1 );
1375 /* completely skip the drawing if only focus has changed */
1376 if (action
== ODA_FOCUS
) goto draw_focus
;
1378 uState
= DFCS_BUTTONPUSH
;
1380 if (style
& BS_FLAT
)
1381 uState
|= DFCS_MONO
;
1382 else if (pushedState
)
1384 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
1385 uState
|= DFCS_FLAT
;
1387 uState
|= DFCS_PUSHED
;
1390 if (state
& (BST_CHECKED
| BST_INDETERMINATE
))
1391 uState
|= DFCS_CHECKED
;
1393 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
1395 /* draw button label */
1397 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
1399 if (dtFlags
== (UINT
)-1L)
1403 OffsetRect(&r
, 1, 1);
1405 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
1407 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
1409 SetTextColor( hDC
, oldTxtColor
);
1412 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1415 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1418 InflateRect( &rc
, -2, -2 );
1419 DrawFocusRect( hDC
, &rc
);
1426 SelectObject( hDC
, hOldPen
);
1427 SelectObject( hDC
, hOldBrush
);
1428 SetBkMode(hDC
, oldBkMode
);
1429 SelectClipRgn( hDC
, hrgn
);
1430 if (hrgn
) DeleteObject( hrgn
);
1433 /**********************************************************************
1434 * Check Box & Radio Button Functions
1437 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1439 RECT rbox
, rtext
, client
;
1441 int delta
, text_offset
, checkBoxWidth
, checkBoxHeight
;
1444 LONG state
= get_button_state( hwnd
);
1445 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1446 LONG ex_style
= GetWindowLongW( hwnd
, GWL_EXSTYLE
);
1450 if (style
& BS_PUSHLIKE
)
1452 PB_Paint( hwnd
, hDC
, action
);
1456 GetClientRect(hwnd
, &client
);
1457 rbox
= rtext
= client
;
1459 checkBoxWidth
= 12 * GetDeviceCaps( hDC
, LOGPIXELSX
) / 96 + 1;
1460 checkBoxHeight
= 12 * GetDeviceCaps( hDC
, LOGPIXELSY
) / 96 + 1;
1462 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1463 GetCharWidthW( hDC
, '0', '0', &text_offset
);
1466 parent
= GetParent(hwnd
);
1467 if (!parent
) parent
= hwnd
;
1468 #if defined(__REACTOS__) && defined(_USER32_)
1469 hBrush
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1471 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
1472 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1473 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1474 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1475 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1477 hrgn
= set_control_clipping( hDC
, &client
);
1479 if (style
& BS_LEFTTEXT
|| ex_style
& WS_EX_RIGHT
)
1481 /* magic +4 is what CTL3D expects */
1483 rtext
.right
-= checkBoxWidth
+ text_offset
;;
1484 rbox
.left
= rbox
.right
- checkBoxWidth
;
1488 rtext
.left
+= checkBoxWidth
+ text_offset
;;
1489 rbox
.right
= checkBoxWidth
;
1492 /* Since WM_ERASEBKGND does nothing, first prepare background */
1493 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
1494 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
1498 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
1500 /* Only adjust rbox when rtext is valid */
1501 if (dtFlags
!= (UINT
)-1L)
1503 rbox
.top
= rtext
.top
;
1504 rbox
.bottom
= rtext
.bottom
;
1507 /* Draw the check-box bitmap */
1508 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
1512 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
1513 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
1514 else if (state
& BST_INDETERMINATE
) flags
= DFCS_BUTTON3STATE
;
1515 else flags
= DFCS_BUTTONCHECK
;
1517 if (state
& (BST_CHECKED
| BST_INDETERMINATE
)) flags
|= DFCS_CHECKED
;
1518 if (state
& BST_PUSHED
) flags
|= DFCS_PUSHED
;
1520 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
1522 /* rbox must have the correct height */
1523 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
1525 if (style
& BS_TOP
) {
1527 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1529 rbox
.top
-= -delta
/2 + 1;
1530 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1532 } else if (style
& BS_BOTTOM
) {
1534 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1536 rbox
.bottom
+= -delta
/2 + 1;
1537 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1539 } else { /* Default */
1541 int ofs
= (delta
/ 2);
1542 rbox
.bottom
-= ofs
+ 1;
1543 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1544 } else if (delta
< 0) {
1545 int ofs
= (-delta
/ 2);
1546 rbox
.top
-= ofs
+ 1;
1547 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1551 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
1554 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
1557 if (action
== ODA_DRAWENTIRE
)
1558 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
1561 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1564 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1569 IntersectRect(&rtext
, &rtext
, &client
);
1570 DrawFocusRect( hDC
, &rtext
);
1575 SelectClipRgn( hDC
, hrgn
);
1576 if (hrgn
) DeleteObject( hrgn
);
1580 /**********************************************************************
1581 * BUTTON_CheckAutoRadioButton
1583 * hwnd is checked, uncheck every other auto radio button in group
1585 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1587 HWND parent
, sibling
, start
;
1589 parent
= GetParent(hwnd
);
1590 /* make sure that starting control is not disabled or invisible */
1591 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1594 if (!sibling
) break;
1595 if ((hwnd
!= sibling
) &&
1596 ((GetWindowLongPtrW( sibling
, GWL_STYLE
) & BS_TYPEMASK
) == BS_AUTORADIOBUTTON
))
1597 SendMessageW( sibling
, BM_SETCHECK
, BST_UNCHECKED
, 0 );
1598 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1599 } while (sibling
!= start
);
1603 /**********************************************************************
1604 * Group Box Functions
1607 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1614 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1618 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1619 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1620 parent
= GetParent(hwnd
);
1621 if (!parent
) parent
= hwnd
;
1622 #if defined(__REACTOS__) && defined(_USER32_)
1623 hbr
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1625 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1626 if (!hbr
) /* did the app forget to call defwindowproc ? */
1627 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1628 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1630 GetClientRect( hwnd
, &rc
);
1632 hrgn
= set_control_clipping( hDC
, &rc
);
1634 GetTextMetricsW (hDC
, &tm
);
1635 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1636 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1638 InflateRect(&rc
, -7, 1);
1639 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1641 if (dtFlags
!= (UINT
)-1L)
1643 /* Because buttons have CS_PARENTDC class style, there is a chance
1644 * that label will be drawn out of client rect.
1645 * But Windows doesn't clip label's rect, so do I.
1648 /* There is 1-pixel margin at the left, right, and bottom */
1649 rc
.left
--; rc
.right
++; rc
.bottom
++;
1650 FillRect(hDC
, &rc
, hbr
);
1651 rc
.left
++; rc
.right
--; rc
.bottom
--;
1653 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1655 SelectClipRgn( hDC
, hrgn
);
1656 if (hrgn
) DeleteObject( hrgn
);
1660 /**********************************************************************
1661 * User Button Functions
1664 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1669 LONG state
= get_button_state( hwnd
);
1672 GetClientRect( hwnd
, &rc
);
1674 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1676 parent
= GetParent(hwnd
);
1677 if (!parent
) parent
= hwnd
;
1678 #if defined(__REACTOS__) && defined(_USER32_)
1679 hBrush
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1681 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1682 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1683 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1684 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1687 FillRect( hDC
, &rc
, hBrush
);
1688 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1691 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1693 DrawFocusRect( hDC
, &rc
);
1701 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_FOCUS
) ? BN_SETFOCUS
: BN_KILLFOCUS
);
1705 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_PUSHED
) ? BN_HILITE
: BN_UNHILITE
);
1709 BUTTON_NOTIFY_PARENT( hwnd
, BN_PAINT
);
1715 /**********************************************************************
1716 * Ownerdrawn Button Functions
1719 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1721 LONG state
= get_button_state( hwnd
);
1723 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1725 HFONT hFont
, hPrevFont
= 0;
1728 dis
.CtlType
= ODT_BUTTON
;
1731 dis
.itemAction
= action
;
1732 dis
.itemState
= ((state
& BST_FOCUS
) ? ODS_FOCUS
: 0) |
1733 ((state
& BST_PUSHED
) ? ODS_SELECTED
: 0) |
1734 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1735 dis
.hwndItem
= hwnd
;
1738 GetClientRect( hwnd
, &dis
.rcItem
);
1740 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1741 parent
= GetParent(hwnd
);
1742 if (!parent
) parent
= hwnd
;
1743 #if defined(__REACTOS__) && defined(_USER32_)
1744 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1746 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1749 hrgn
= set_control_clipping( hDC
, &dis
.rcItem
);
1751 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1752 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1753 SelectClipRgn( hDC
, hrgn
);
1754 if (hrgn
) DeleteObject( hrgn
);
1758 void BUTTON_Register()
1762 ZeroMemory(&wndClass
, sizeof(WNDCLASSW
));
1763 wndClass
.style
= CS_GLOBALCLASS
| CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
;
1764 wndClass
.lpfnWndProc
= ButtonWndProcW
;
1765 wndClass
.cbClsExtra
= 0;
1766 wndClass
.cbWndExtra
= sizeof(PBUTTON_DATA
);
1767 wndClass
.hCursor
= LoadCursorW(0, (LPCWSTR
)IDC_ARROW
);
1768 wndClass
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
1769 wndClass
.lpszClassName
= WC_BUTTONW
;
1771 RegisterClassW(&wndClass
);
1774 void BUTTON_Unregister()
1776 UnregisterClassW(WC_BUTTONW
, NULL
);