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
);
398 BOOL
BUTTON_DrawIml(HDC hDC
, BUTTON_IMAGELIST
*pimlData
, RECT
*prc
, BOOL bOnlyCalc
)
406 if (!ImageList_GetIconSize(pimlData
->himl
, &ImageSize
.cx
, &ImageSize
.cy
))
409 if (pimlData
->uAlign
== BUTTON_IMAGELIST_ALIGN_LEFT
)
411 left
= prc
->left
+ pimlData
->margin
.left
;
412 top
= prc
->top
+ (prc
->bottom
- prc
->top
- ImageSize
.cy
) / 2;
413 prc
->left
= left
+ pimlData
->margin
.right
+ ImageSize
.cx
;
415 else if (pimlData
->uAlign
== BUTTON_IMAGELIST_ALIGN_RIGHT
)
417 left
= prc
->right
- pimlData
->margin
.right
- ImageSize
.cx
;
418 top
= prc
->top
+ (prc
->bottom
- prc
->top
- ImageSize
.cy
) / 2;
419 prc
->right
= left
- pimlData
->margin
.left
;
421 else if (pimlData
->uAlign
== BUTTON_IMAGELIST_ALIGN_TOP
)
423 left
= prc
->left
+ (prc
->right
- prc
->left
- ImageSize
.cx
) / 2;
424 top
= prc
->top
+ pimlData
->margin
.top
;
425 prc
->top
= top
+ ImageSize
.cy
+ pimlData
->margin
.bottom
;
427 else if (pimlData
->uAlign
== BUTTON_IMAGELIST_ALIGN_BOTTOM
)
429 left
= prc
->left
+ (prc
->right
- prc
->left
- ImageSize
.cx
) / 2;
430 top
= prc
->bottom
- pimlData
->margin
.bottom
- ImageSize
.cy
;
431 prc
->bottom
= top
- pimlData
->margin
.top
;
433 else if (pimlData
->uAlign
== BUTTON_IMAGELIST_ALIGN_CENTER
)
435 left
= prc
->left
+ (prc
->right
- prc
->left
- ImageSize
.cx
) / 2;
436 top
= prc
->top
+ (prc
->bottom
- prc
->top
- ImageSize
.cy
) / 2;
440 ImageList_Draw(pimlData
->himl
, 0, hDC
, left
, top
, 0);
447 /* retrieve the button text; returned buffer must be freed by caller */
448 inline WCHAR
*get_button_text( HWND hwnd
)
451 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
452 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
457 /* Retrieve the UI state for the control */
458 static BOOL
button_update_uistate(HWND hwnd
, BOOL unicode
)
460 LONG flags
, prevflags
;
463 flags
= DefWindowProcW(hwnd
, WM_QUERYUISTATE
, 0, 0);
465 flags
= DefWindowProcA(hwnd
, WM_QUERYUISTATE
, 0, 0);
467 prevflags
= get_ui_state(hwnd
);
469 if (prevflags
!= flags
)
471 set_ui_state(hwnd
, flags
);
479 /***********************************************************************
480 * ButtonWndProc_common
482 LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
483 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
487 LONG style
= GetWindowLongPtrW( hWnd
, GWL_STYLE
);
488 UINT btn_type
= get_button_type( style
);
491 #if defined(__REACTOS__) && defined(_USER32_)
494 pWnd
= ValidateHwnd(hWnd
);
499 NtUserSetWindowFNID(hWnd
, FNID_BUTTON
);
503 if (pWnd
->fnid
!= FNID_BUTTON
)
505 ERR("Wrong window class for Button! fnId 0x%x\n",pWnd
->fnid
);
513 if (!IsWindow( hWnd
)) return 0;
516 pt
.x
= (short)LOWORD(lParam
);
517 pt
.y
= (short)HIWORD(lParam
);
524 PBUTTON_DATA data
= HeapAlloc( GetProcessHeap(), 0, sizeof(BUTTON_DATA
) );
527 ERR("Failed to alloc internal button data\n");
531 memset(data
, 0, sizeof(BUTTON_DATA
));
532 SetRect(&data
->rcTextMargin
, 1,1,1,1);
534 _SetButtonData(hWnd
, data
);
539 PBUTTON_DATA data
= _GetButtonData(hWnd
);
545 HeapFree( GetProcessHeap(), 0, data
);
546 _SetButtonData(hWnd
, NULL
);
549 OpenThemeData(hWnd
, WC_BUTTONW
);
552 CloseThemeData (GetWindowTheme(hWnd
));
554 case WM_THEMECHANGED
:
555 CloseThemeData (GetWindowTheme(hWnd
));
556 OpenThemeData(hWnd
, WC_BUTTONW
);
557 InvalidateRect(hWnd
, NULL
, TRUE
);
561 state
= get_button_state( hWnd
);
564 NMBCHOTITEM nmhotitem
;
567 set_button_state(hWnd
, state
);
569 nmhotitem
.hdr
.hwndFrom
= hWnd
;
570 nmhotitem
.hdr
.idFrom
= GetWindowLongPtrW (hWnd
, GWLP_ID
);
571 nmhotitem
.hdr
.code
= BCN_HOTITEMCHANGE
;
572 nmhotitem
.dwFlags
= HICF_LEAVING
;
573 SendMessageW(GetParent(hWnd
), WM_NOTIFY
, nmhotitem
.hdr
.idFrom
, (LPARAM
)&nmhotitem
);
575 InvalidateRect(hWnd
, NULL
, TRUE
);
581 TRACKMOUSEEVENT mouse_event
;
582 state
= get_button_state( hWnd
);
583 if ((state
& BST_HOT
) == 0)
585 NMBCHOTITEM nmhotitem
;
588 set_button_state(hWnd
, state
);
590 nmhotitem
.hdr
.hwndFrom
= hWnd
;
591 nmhotitem
.hdr
.idFrom
= GetWindowLongPtrW (hWnd
, GWLP_ID
);
592 nmhotitem
.hdr
.code
= BCN_HOTITEMCHANGE
;
593 nmhotitem
.dwFlags
= HICF_ENTERING
;
594 SendMessageW(GetParent(hWnd
), WM_NOTIFY
, nmhotitem
.hdr
.idFrom
, (LPARAM
)&nmhotitem
);
596 InvalidateRect(hWnd
, NULL
, TRUE
);
599 mouse_event
.cbSize
= sizeof(TRACKMOUSEEVENT
);
600 mouse_event
.dwFlags
= TME_QUERY
;
601 if(!TrackMouseEvent(&mouse_event
) || !(mouse_event
.dwFlags
&TME_LEAVE
))
603 mouse_event
.dwFlags
= TME_LEAVE
;
604 mouse_event
.hwndTrack
= hWnd
;
605 mouse_event
.dwHoverTime
= 1;
606 TrackMouseEvent(&mouse_event
);
610 case BCM_GETTEXTMARGIN
:
612 RECT
* prc
= (RECT
*)lParam
;
613 PBUTTON_DATA data
= _GetButtonData(hWnd
);
616 *prc
= data
->rcTextMargin
;
619 case BCM_SETTEXTMARGIN
:
621 RECT
* prc
= (RECT
*)lParam
;
622 PBUTTON_DATA data
= _GetButtonData(hWnd
);
625 data
->rcTextMargin
= *prc
;
628 case BCM_SETIMAGELIST
:
630 BUTTON_IMAGELIST
* pimldata
= (BUTTON_IMAGELIST
*)lParam
;
631 PBUTTON_DATA data
= _GetButtonData(hWnd
);
632 if (!data
|| !pimldata
|| !pimldata
->himl
)
634 data
->imlData
= *pimldata
;
637 case BCM_GETIMAGELIST
:
639 BUTTON_IMAGELIST
* pimldata
= (BUTTON_IMAGELIST
*)lParam
;
640 PBUTTON_DATA data
= _GetButtonData(hWnd
);
641 if (!data
|| !pimldata
)
643 *pimldata
= data
->imlData
;
646 case BCM_GETIDEALSIZE
:
648 HTHEME theme
= GetWindowTheme(hWnd
);
650 SIZE
* pSize
= (SIZE
*)lParam
;
652 if (btn_type
== BS_PUSHBUTTON
||
653 btn_type
== BS_DEFPUSHBUTTON
||
654 btn_type
== BS_USERBUTTON
)
656 ret
= BUTTON_GetIdealSize(theme
, hWnd
, pSize
);
661 GetClientRect(hWnd
, &rect
);
662 pSize
->cx
= rect
.right
;
663 pSize
->cy
= rect
.bottom
;
670 if (!_GetButtonData(hWnd
))
673 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
674 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
685 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
686 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
688 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
689 case BS_GROUPBOX
: return DLGC_STATIC
;
690 default: return DLGC_BUTTON
;
694 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
698 if (btn_type
>= MAX_BTN_TYPE
)
699 return -1; /* abort */
701 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
702 if (btn_type
== BS_USERBUTTON
)
704 style
= (style
& ~BS_TYPEMASK
) | BS_PUSHBUTTON
;
706 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
708 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
711 set_button_state( hWnd
, BST_UNCHECKED
);
713 button_update_uistate( hWnd
, unicode
);
717 #if defined(__REACTOS__) && defined(_USER32_)
719 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
724 if (btn_type
== BS_OWNERDRAW
)
726 HDC hdc
= (HDC
)wParam
;
729 HWND parent
= GetParent(hWnd
);
730 if (!parent
) parent
= hWnd
;
731 #if defined(__REACTOS__) && defined(_USER32_)
732 hBrush
= GetControlColor( parent
, hWnd
, hdc
, WM_CTLCOLORBTN
);
734 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
735 if (!hBrush
) /* did the app forget to call defwindowproc ? */
736 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
737 (WPARAM
)hdc
, (LPARAM
)hWnd
);
739 GetClientRect(hWnd
, &rc
);
740 FillRect(hdc
, &rc
, hBrush
);
748 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
750 HTHEME theme
= GetWindowTheme(hWnd
);
751 if (theme
&& BUTTON_PaintWithTheme(theme
, hWnd
, hdc
, uMsg
== WM_PRINTCLIENT
? lParam
: 0))
753 if ( !wParam
) EndPaint( hWnd
, &ps
);
757 if (btnPaintFunc
[btn_type
])
759 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
760 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
761 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
763 if ( !wParam
) EndPaint( hWnd
, &ps
);
768 if (wParam
== VK_SPACE
)
770 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
771 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
776 case WM_LBUTTONDBLCLK
:
777 if(style
& BS_NOTIFY
||
778 btn_type
== BS_RADIOBUTTON
||
779 btn_type
== BS_USERBUTTON
||
780 btn_type
== BS_OWNERDRAW
)
782 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
789 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
790 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
794 if (wParam
!= VK_SPACE
)
799 BOOL TellParent
= FALSE
; //// ReactOS see note below.
801 state
= get_button_state( hWnd
);
802 if (!(state
& BUTTON_BTNPRESSED
)) break;
803 state
&= BUTTON_NSTATES
;
804 set_button_state( hWnd
, state
);
805 if (!(state
& BST_PUSHED
))
810 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
811 GetClientRect( hWnd
, &rect
);
812 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
814 state
= get_button_state( hWnd
);
817 case BS_AUTOCHECKBOX
:
818 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BST_CHECKED
), 0 );
820 case BS_AUTORADIOBUTTON
:
821 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
824 SendMessageW( hWnd
, BM_SETCHECK
,
825 (state
& BST_INDETERMINATE
) ? 0 : ((state
& 3) + 1), 0 );
829 TellParent
= TRUE
; // <---- Fix CORE-10194, Notify parent after capture is released.
832 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
837 if (TellParent
) BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
846 case WM_CAPTURECHANGED
:
847 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
848 if (hWnd
== (HWND
)lParam
) break;
849 state
= get_button_state( hWnd
);
850 if (state
& BUTTON_BTNPRESSED
)
852 state
&= BUTTON_NSTATES
;
853 set_button_state( hWnd
, state
);
854 if (state
& BST_PUSHED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
859 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
861 GetClientRect( hWnd
, &rect
);
862 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
868 /* Clear an old text here as Windows does */
871 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
872 // Patch: http://source.winehq.org/patches/data/70889
873 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
876 if (style
& WS_VISIBLE
)
878 if (IsWindowVisible(hWnd
))
881 HDC hdc
= GetDC(hWnd
);
884 HWND parent
= GetParent(hWnd
);
885 UINT message
= (btn_type
== BS_PUSHBUTTON
||
886 btn_type
== BS_DEFPUSHBUTTON
||
887 btn_type
== BS_PUSHLIKE
||
888 btn_type
== BS_USERBUTTON
||
889 btn_type
== BS_OWNERDRAW
) ?
890 WM_CTLCOLORBTN
: WM_CTLCOLORSTATIC
;
892 if (!parent
) parent
= hWnd
;
893 #if defined(__REACTOS__) && defined(_USER32_)
894 hbrush
= GetControlColor(parent
, hWnd
, hdc
, message
);
896 hbrush
= (HBRUSH
)SendMessageW(parent
, message
,
897 (WPARAM
)hdc
, (LPARAM
)hWnd
);
898 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
899 hbrush
= (HBRUSH
)DefWindowProcW(parent
, message
,
900 (WPARAM
)hdc
, (LPARAM
)hWnd
);
903 GetClientRect(hWnd
, &client
);
905 /* FIXME: check other BS_* handlers */
906 if (btn_type
== BS_GROUPBOX
)
907 InflateRect(&rc
, -7, 1); /* GB_Paint does this */
908 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
909 /* Clip by client rect bounds */
910 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
911 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
912 FillRect(hdc
, &rc
, hbrush
);
913 ReleaseDC(hWnd
, hdc
);
916 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
917 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
918 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
919 InvalidateRect( hWnd
, NULL
, TRUE
);
921 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
922 return 1; /* success. FIXME: check text length */
926 set_button_font( hWnd
, (HFONT
)wParam
);
927 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
931 return (LRESULT
)get_button_font( hWnd
);
934 TRACE("WM_SETFOCUS %p\n",hWnd
);
935 set_button_state( hWnd
, get_button_state(hWnd
) | BST_FOCUS
);
937 if (btn_type
!= BS_OWNERDRAW
)
938 InvalidateRect(hWnd
, NULL
, FALSE
);
941 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
942 if (style
& BS_NOTIFY
)
943 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
947 TRACE("WM_KILLFOCUS %p\n",hWnd
);
948 state
= get_button_state( hWnd
);
949 set_button_state( hWnd
, state
& ~BST_FOCUS
);
951 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
954 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
956 if (style
& BS_NOTIFY
)
957 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
959 InvalidateRect( hWnd
, NULL
, FALSE
);
962 case WM_SYSCOLORCHANGE
:
963 InvalidateRect( hWnd
, NULL
, FALSE
);
967 btn_type
= wParam
& BS_TYPEMASK
;
968 style
= (style
& ~BS_TYPEMASK
) | btn_type
;
970 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
972 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
975 /* Only redraw if lParam flag is set.*/
977 InvalidateRect( hWnd
, NULL
, TRUE
);
983 state
= get_button_state(hWnd
);
984 if (state
& BUTTON_BMCLICK
)
986 set_button_state(hWnd
, state
| BUTTON_BMCLICK
); // Tracked in STATE_GWL_OFFSET.
988 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
989 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
991 state
= get_button_state(hWnd
);
992 if (!(state
& BUTTON_BMCLICK
)) break;
993 state
&= ~BUTTON_BMCLICK
;
994 set_button_state(hWnd
, state
);
999 /* Check that image format matches button style */
1000 switch (style
& (BS_BITMAP
|BS_ICON
))
1003 if (wParam
!= IMAGE_BITMAP
) return 0;
1006 if (wParam
!= IMAGE_ICON
) return 0;
1012 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
1014 oldHbitmap
= (HBITMAP
)set_button_image(hWnd
, lParam
);
1016 InvalidateRect( hWnd
, NULL
, FALSE
);
1017 return (LRESULT
)oldHbitmap
;
1021 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
1023 return get_button_image(hWnd
);
1027 return get_button_state( hWnd
) & 3;
1030 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
1031 state
= get_button_state( hWnd
);
1032 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
1035 if (wParam
) style
|= WS_TABSTOP
;
1036 else style
&= ~WS_TABSTOP
;
1037 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
1039 if (wParam
) WIN_SetStyle( hWnd
, WS_TABSTOP
, 0 );
1040 else WIN_SetStyle( hWnd
, 0, WS_TABSTOP
);
1043 if ((state
& 3) != wParam
)
1045 set_button_state( hWnd
, (state
& ~3) | wParam
);
1047 paint_button( hWnd
, btn_type
, ODA_SELECT
);
1049 InvalidateRect(hWnd
, NULL
, FALSE
);
1052 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BST_CHECKED
) && (style
& WS_CHILD
))
1053 BUTTON_CheckAutoRadioButton( hWnd
);
1057 return get_button_state( hWnd
);
1060 state
= get_button_state( hWnd
);
1062 set_button_state( hWnd
, state
| BST_PUSHED
);
1064 set_button_state( hWnd
, state
& ~BST_PUSHED
);
1067 paint_button( hWnd
, btn_type
, ODA_SELECT
);
1069 InvalidateRect(hWnd
, NULL
, FALSE
);
1074 case WM_UPDATEUISTATE
:
1076 DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
1078 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
1080 if (button_update_uistate(hWnd
, unicode
))
1081 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
1086 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
1089 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
1090 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
1097 /***********************************************************************
1099 * The button window procedure. This is just a wrapper which locks
1100 * the passed HWND and calls the real window procedure (with a WND*
1101 * pointer pointing to the locked windowstructure).
1103 LRESULT WINAPI
ButtonWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1105 if (!IsWindow(hWnd
)) return 0;
1106 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);
1109 /***********************************************************************
1112 LRESULT WINAPI
ButtonWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1114 if (!IsWindow(hWnd
)) return 0;
1115 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
1118 #endif /* __REACTOS__ */
1120 /**********************************************************************
1121 * Convert button styles to flags used by DrawText.
1123 static UINT
BUTTON_BStoDT( DWORD style
, DWORD ex_style
)
1125 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
1127 /* "Convert" pushlike buttons to pushbuttons */
1128 if (style
& BS_PUSHLIKE
)
1129 style
&= ~BS_TYPEMASK
;
1131 if (!(style
& BS_MULTILINE
))
1132 dtStyle
|= DT_SINGLELINE
;
1134 dtStyle
|= DT_WORDBREAK
;
1136 switch (style
& BS_CENTER
)
1138 case BS_LEFT
: /* DT_LEFT is 0 */ break;
1139 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
1140 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
1142 /* Pushbutton's text is centered by default */
1143 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
1144 /* all other flavours have left aligned text */
1147 if (ex_style
& WS_EX_RIGHT
) dtStyle
= DT_RIGHT
| (dtStyle
& ~(DT_LEFT
| DT_CENTER
));
1149 /* DrawText ignores vertical alignment for multiline text,
1150 * but we use these flags to align label manually.
1152 if (get_button_type(style
) != BS_GROUPBOX
)
1154 switch (style
& BS_VCENTER
)
1156 case BS_TOP
: /* DT_TOP is 0 */ break;
1157 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
1158 case BS_VCENTER
: /* fall through */
1159 default: dtStyle
|= DT_VCENTER
; break;
1163 /* GroupBox's text is always single line and is top aligned. */
1164 dtStyle
|= DT_SINGLELINE
;
1169 /**********************************************************************
1170 * BUTTON_CalcLabelRect
1172 * Calculates label's rectangle depending on button style.
1174 * Returns flags to be passed to DrawText.
1175 * Calculated rectangle doesn't take into account button state
1176 * (pushed, etc.). If there is nothing to draw (no text/image) output
1177 * rectangle is empty, and return value is (UINT)-1.
1179 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
1181 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1182 LONG ex_style
= GetWindowLongPtrW( hwnd
, GWL_EXSTYLE
);
1186 UINT dtStyle
= BUTTON_BStoDT( style
, ex_style
);
1190 PBUTTON_DATA pdata
= _GetButtonData(hwnd
);
1193 /* Calculate label rectangle according to label type */
1194 switch (style
& (BS_ICON
|BS_BITMAP
))
1198 HFONT hFont
, hPrevFont
= 0;
1202 bHasIml
= BUTTON_DrawIml(hdc
, &pdata
->imlData
, &r
, TRUE
);
1205 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
1208 HeapFree( GetProcessHeap(), 0, text
);
1212 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hdc
, hFont
);
1213 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
1214 if (hPrevFont
) SelectObject( hdc
, hPrevFont
);
1215 HeapFree( GetProcessHeap(), 0, text
);
1217 if (get_ui_state(hwnd
) & UISF_HIDEACCEL
)
1218 dtStyle
|= DT_HIDEPREFIX
;
1222 if (pdata
->imlData
.uAlign
== BUTTON_IMAGELIST_ALIGN_LEFT
)
1223 r
.left
= pdata
->imlData
.margin
.left
;
1224 else if (pdata
->imlData
.uAlign
== BUTTON_IMAGELIST_ALIGN_RIGHT
)
1225 r
.right
= pdata
->imlData
.margin
.right
;
1226 else if (pdata
->imlData
.uAlign
== BUTTON_IMAGELIST_ALIGN_TOP
)
1227 r
.top
= pdata
->imlData
.margin
.top
;
1228 else if (pdata
->imlData
.uAlign
== BUTTON_IMAGELIST_ALIGN_BOTTOM
)
1229 r
.bottom
= pdata
->imlData
.margin
.bottom
;
1237 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
1239 if (!GetIconInfo((HICON
)get_button_image(hwnd
), &iconInfo
))
1243 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
1245 r
.right
= r
.left
+ bm
.bmWidth
;
1246 r
.bottom
= r
.top
+ bm
.bmHeight
;
1248 DeleteObject(iconInfo
.hbmColor
);
1249 DeleteObject(iconInfo
.hbmMask
);
1254 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
1256 if (!GetObjectW( (HANDLE
)get_button_image(hwnd
), sizeof(BITMAP
), &bm
))
1260 r
.right
= r
.left
+ bm
.bmWidth
;
1261 r
.bottom
= r
.top
+ bm
.bmHeight
;
1271 /* Position label inside bounding rectangle according to
1272 * alignment flags. (calculated rect is always left-top aligned).
1273 * If label is aligned to any side - shift label in opposite
1274 * direction to leave extra space for focus rectangle.
1276 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
1278 case DT_LEFT
: r
.left
++; r
.right
++; break;
1279 case DT_CENTER
: n
= r
.right
- r
.left
;
1280 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
1281 r
.right
= r
.left
+ n
; break;
1282 case DT_RIGHT
: n
= r
.right
- r
.left
;
1283 r
.right
= rc
->right
- 1;
1284 r
.left
= r
.right
- n
;
1288 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
1290 case DT_TOP
: r
.top
++; r
.bottom
++; break;
1291 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
1292 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
1293 r
.bottom
= r
.top
+ n
; break;
1294 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
1295 r
.bottom
= rc
->bottom
- 1;
1296 r
.top
= r
.bottom
- n
;
1305 /**********************************************************************
1306 * BUTTON_DrawTextCallback
1308 * Callback function used by DrawStateW function.
1310 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
1315 SetRect(&rc
, 0, 0, cx
, cy
);
1316 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
1319 HWND hwnd
= (HWND
)lp
;
1321 PBUTTON_DATA pdata
= _GetButtonData(hwnd
);
1324 if (!(text
= get_button_text( hwnd
))) return TRUE
;
1326 SetRect(&rc
, 0, 0, cx
, cy
);
1328 BUTTON_DrawIml(hdc
, &pdata
->imlData
, &rc
, FALSE
);
1330 DrawTextW(hdc
, text
, -1, &rc
, (UINT
)wp
);
1331 HeapFree(GetProcessHeap(), 0, text
);
1337 /**********************************************************************
1340 * Common function for drawing button label.
1342 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
1344 DRAWSTATEPROC lpOutputProc
= NULL
;
1348 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
1349 LONG state
= get_button_state( hwnd
);
1350 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1353 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
1354 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
1355 * I don't have Win31 on hand to verify that, so I leave it as is.
1358 if ((style
& BS_PUSHLIKE
) && (state
& BST_INDETERMINATE
))
1360 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
1364 switch (style
& (BS_ICON
|BS_BITMAP
))
1367 /* DST_COMPLEX -- is 0 */
1368 lpOutputProc
= BUTTON_DrawTextCallback
;
1370 if (!(text
= get_button_text( hwnd
))) return;
1376 wp
= (WPARAM
)dtFlags
;
1379 if (dtFlags
& DT_HIDEPREFIX
)
1380 flags
|= DSS_HIDEPREFIX
;
1387 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
1389 lp
= get_button_image(hwnd
);
1394 flags
|= DST_BITMAP
;
1396 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
1398 lp
= get_button_image(hwnd
);
1406 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
1407 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
1408 HeapFree( GetProcessHeap(), 0, text
);
1411 /**********************************************************************
1412 * Push Button Functions
1414 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1417 UINT dtFlags
, uState
;
1421 COLORREF oldTxtColor
;
1423 LONG state
= get_button_state( hwnd
);
1424 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1425 BOOL pushedState
= (state
& BST_PUSHED
);
1429 GetClientRect( hwnd
, &rc
);
1431 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
1432 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1433 parent
= GetParent(hwnd
);
1434 if (!parent
) parent
= hwnd
;
1435 #if defined(__REACTOS__) && defined(_USER32_)
1436 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1438 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1441 hrgn
= set_control_clipping( hDC
, &rc
);
1443 hOldPen
= SelectObject(hDC
, GetStockObject(DC_PEN
));
1444 SetDCPenColor(hDC
, GetSysColor(COLOR_WINDOWFRAME
));
1446 hOldPen
= SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
1448 hOldBrush
= SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
1449 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
1451 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
1453 if (action
!= ODA_FOCUS
)
1454 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
1455 InflateRect( &rc
, -1, -1 );
1458 /* completely skip the drawing if only focus has changed */
1459 if (action
== ODA_FOCUS
) goto draw_focus
;
1461 uState
= DFCS_BUTTONPUSH
;
1463 if (style
& BS_FLAT
)
1464 uState
|= DFCS_MONO
;
1465 else if (pushedState
)
1467 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
1468 uState
|= DFCS_FLAT
;
1470 uState
|= DFCS_PUSHED
;
1473 if (state
& (BST_CHECKED
| BST_INDETERMINATE
))
1474 uState
|= DFCS_CHECKED
;
1476 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
1478 /* draw button label */
1480 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
1482 if (dtFlags
== (UINT
)-1L)
1486 OffsetRect(&r
, 1, 1);
1488 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
1490 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
1492 SetTextColor( hDC
, oldTxtColor
);
1495 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1498 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1501 InflateRect( &rc
, -2, -2 );
1502 DrawFocusRect( hDC
, &rc
);
1509 SelectObject( hDC
, hOldPen
);
1510 SelectObject( hDC
, hOldBrush
);
1511 SetBkMode(hDC
, oldBkMode
);
1512 SelectClipRgn( hDC
, hrgn
);
1513 if (hrgn
) DeleteObject( hrgn
);
1516 /**********************************************************************
1517 * Check Box & Radio Button Functions
1520 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1522 RECT rbox
, rtext
, client
;
1524 int delta
, text_offset
, checkBoxWidth
, checkBoxHeight
;
1527 LONG state
= get_button_state( hwnd
);
1528 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1529 LONG ex_style
= GetWindowLongW( hwnd
, GWL_EXSTYLE
);
1533 if (style
& BS_PUSHLIKE
)
1535 PB_Paint( hwnd
, hDC
, action
);
1539 GetClientRect(hwnd
, &client
);
1540 rbox
= rtext
= client
;
1542 checkBoxWidth
= 12 * GetDeviceCaps( hDC
, LOGPIXELSX
) / 96 + 1;
1543 checkBoxHeight
= 12 * GetDeviceCaps( hDC
, LOGPIXELSY
) / 96 + 1;
1545 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1546 GetCharWidthW( hDC
, '0', '0', &text_offset
);
1549 parent
= GetParent(hwnd
);
1550 if (!parent
) parent
= hwnd
;
1551 #if defined(__REACTOS__) && defined(_USER32_)
1552 hBrush
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1554 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
1555 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1556 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1557 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1558 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1560 hrgn
= set_control_clipping( hDC
, &client
);
1562 if (style
& BS_LEFTTEXT
|| ex_style
& WS_EX_RIGHT
)
1564 /* magic +4 is what CTL3D expects */
1566 rtext
.right
-= checkBoxWidth
+ text_offset
;;
1567 rbox
.left
= rbox
.right
- checkBoxWidth
;
1571 rtext
.left
+= checkBoxWidth
+ text_offset
;;
1572 rbox
.right
= checkBoxWidth
;
1575 /* Since WM_ERASEBKGND does nothing, first prepare background */
1576 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
1577 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
1581 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
1583 /* Only adjust rbox when rtext is valid */
1584 if (dtFlags
!= (UINT
)-1L)
1586 rbox
.top
= rtext
.top
;
1587 rbox
.bottom
= rtext
.bottom
;
1590 /* Draw the check-box bitmap */
1591 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
1595 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
1596 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
1597 else if (state
& BST_INDETERMINATE
) flags
= DFCS_BUTTON3STATE
;
1598 else flags
= DFCS_BUTTONCHECK
;
1600 if (state
& (BST_CHECKED
| BST_INDETERMINATE
)) flags
|= DFCS_CHECKED
;
1601 if (state
& BST_PUSHED
) flags
|= DFCS_PUSHED
;
1603 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
1605 /* rbox must have the correct height */
1606 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
1608 if (style
& BS_TOP
) {
1610 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1612 rbox
.top
-= -delta
/2 + 1;
1613 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1615 } else if (style
& BS_BOTTOM
) {
1617 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1619 rbox
.bottom
+= -delta
/2 + 1;
1620 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1622 } else { /* Default */
1624 int ofs
= (delta
/ 2);
1625 rbox
.bottom
-= ofs
+ 1;
1626 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1627 } else if (delta
< 0) {
1628 int ofs
= (-delta
/ 2);
1629 rbox
.top
-= ofs
+ 1;
1630 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1634 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
1637 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
1640 if (action
== ODA_DRAWENTIRE
)
1641 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
1644 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1647 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1652 IntersectRect(&rtext
, &rtext
, &client
);
1653 DrawFocusRect( hDC
, &rtext
);
1658 SelectClipRgn( hDC
, hrgn
);
1659 if (hrgn
) DeleteObject( hrgn
);
1663 /**********************************************************************
1664 * BUTTON_CheckAutoRadioButton
1666 * hwnd is checked, uncheck every other auto radio button in group
1668 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1670 HWND parent
, sibling
, start
;
1672 parent
= GetParent(hwnd
);
1673 /* make sure that starting control is not disabled or invisible */
1674 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1677 if (!sibling
) break;
1678 if ((hwnd
!= sibling
) &&
1679 ((GetWindowLongPtrW( sibling
, GWL_STYLE
) & BS_TYPEMASK
) == BS_AUTORADIOBUTTON
))
1680 SendMessageW( sibling
, BM_SETCHECK
, BST_UNCHECKED
, 0 );
1681 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1682 } while (sibling
!= start
);
1686 /**********************************************************************
1687 * Group Box Functions
1690 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1697 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1701 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1702 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1703 parent
= GetParent(hwnd
);
1704 if (!parent
) parent
= hwnd
;
1705 #if defined(__REACTOS__) && defined(_USER32_)
1706 hbr
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1708 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1709 if (!hbr
) /* did the app forget to call defwindowproc ? */
1710 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1711 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1713 GetClientRect( hwnd
, &rc
);
1715 hrgn
= set_control_clipping( hDC
, &rc
);
1717 GetTextMetricsW (hDC
, &tm
);
1718 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1719 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1721 InflateRect(&rc
, -7, 1);
1722 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1724 if (dtFlags
!= (UINT
)-1L)
1726 /* Because buttons have CS_PARENTDC class style, there is a chance
1727 * that label will be drawn out of client rect.
1728 * But Windows doesn't clip label's rect, so do I.
1731 /* There is 1-pixel margin at the left, right, and bottom */
1732 rc
.left
--; rc
.right
++; rc
.bottom
++;
1733 FillRect(hDC
, &rc
, hbr
);
1734 rc
.left
++; rc
.right
--; rc
.bottom
--;
1736 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1738 SelectClipRgn( hDC
, hrgn
);
1739 if (hrgn
) DeleteObject( hrgn
);
1743 /**********************************************************************
1744 * User Button Functions
1747 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1752 LONG state
= get_button_state( hwnd
);
1755 GetClientRect( hwnd
, &rc
);
1757 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1759 parent
= GetParent(hwnd
);
1760 if (!parent
) parent
= hwnd
;
1761 #if defined(__REACTOS__) && defined(_USER32_)
1762 hBrush
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1764 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1765 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1766 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1767 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1770 FillRect( hDC
, &rc
, hBrush
);
1771 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1774 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1776 DrawFocusRect( hDC
, &rc
);
1784 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_FOCUS
) ? BN_SETFOCUS
: BN_KILLFOCUS
);
1788 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_PUSHED
) ? BN_HILITE
: BN_UNHILITE
);
1792 BUTTON_NOTIFY_PARENT( hwnd
, BN_PAINT
);
1798 /**********************************************************************
1799 * Ownerdrawn Button Functions
1802 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1804 LONG state
= get_button_state( hwnd
);
1806 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1808 HFONT hFont
, hPrevFont
= 0;
1811 dis
.CtlType
= ODT_BUTTON
;
1814 dis
.itemAction
= action
;
1815 dis
.itemState
= ((state
& BST_FOCUS
) ? ODS_FOCUS
: 0) |
1816 ((state
& BST_PUSHED
) ? ODS_SELECTED
: 0) |
1817 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1818 dis
.hwndItem
= hwnd
;
1821 GetClientRect( hwnd
, &dis
.rcItem
);
1823 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1824 parent
= GetParent(hwnd
);
1825 if (!parent
) parent
= hwnd
;
1826 #if defined(__REACTOS__) && defined(_USER32_)
1827 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1829 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1832 hrgn
= set_control_clipping( hDC
, &dis
.rcItem
);
1834 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1835 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1836 SelectClipRgn( hDC
, hrgn
);
1837 if (hrgn
) DeleteObject( hrgn
);
1841 void BUTTON_Register()
1845 ZeroMemory(&wndClass
, sizeof(WNDCLASSW
));
1846 wndClass
.style
= CS_GLOBALCLASS
| CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
;
1847 wndClass
.lpfnWndProc
= ButtonWndProcW
;
1848 wndClass
.cbClsExtra
= 0;
1849 wndClass
.cbWndExtra
= sizeof(PBUTTON_DATA
);
1850 wndClass
.hCursor
= LoadCursorW(0, (LPCWSTR
)IDC_ARROW
);
1851 wndClass
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
1852 wndClass
.lpszClassName
= WC_BUTTONW
;
1854 RegisterClassW(&wndClass
);
1857 void BUTTON_Unregister()
1859 UnregisterClassW(WC_BUTTONW
, NULL
);