1 /* File: button.c -- Button type widgets
3 * Copyright (C) 1993 Johannes Ruscheinski
4 * Copyright (C) 1993 David Metcalfe
5 * Copyright (C) 1994 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * This code was audited for completeness against the documented features
24 * of Comctl32.dll version 6.0 on Oct. 3, 2004, by Dimitrie O. Paun.
26 * Unless otherwise noted, we believe this code to be complete, as per
27 * the specification mentioned above.
28 * If you discover missing features, or bugs, please note them below.
32 * - BS_NOTIFY: is it complete?
33 * - BS_RIGHTBUTTON: same as BS_LEFTTEXT
36 * - WM_CHAR: Checks a (manual or automatic) check box on '+' or '=', clears it on '-' key.
37 * - WM_SETFOCUS: For (manual or automatic) radio buttons, send the parent window BN_CLICKED
38 * - WM_NCCREATE: Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
49 * - BN_PUSHED/BN_HILITE
50 * + BN_KILLFOCUS: is it OK?
52 * + BN_SETFOCUS: is it OK?
53 * - BN_UNPUSHED/BN_UNHILITE
56 * Structures/Macros/Definitions
59 * - Button_GetIdealSize
60 * - Button_GetImageList
61 * - Button_GetTextMargin
62 * - Button_SetImageList
63 * - Button_SetTextMargin
67 #include <wine/debug.h>
68 WINE_DEFAULT_DEBUG_CHANNEL(button
);
70 /* GetWindowLong offsets for window extra information */
71 #define STATE_GWL_OFFSET 0
72 #define HFONT_GWL_OFFSET (sizeof(LONG))
73 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
74 #define UISTATE_GWL_OFFSET (HIMAGE_GWL_OFFSET+sizeof(HFONT))
75 #define NB_EXTRA_BYTES (UISTATE_GWL_OFFSET+sizeof(LONG))
77 /* undocumented flags */
78 #define BUTTON_NSTATES 0x0F
79 #define BUTTON_BTNPRESSED 0x40
80 #define BUTTON_UNKNOWN2 0x20
81 #define BUTTON_UNKNOWN3 0x10
83 #define BUTTON_BMCLICK 0x100 // ReactOS Need to up to wine!
86 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
87 do { /* Notify parent which has created this button control */ \
88 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
89 SendMessageW(GetParent(hWnd), WM_COMMAND, \
90 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
94 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
95 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
96 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
97 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
98 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
99 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
100 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
102 #define MAX_BTN_TYPE 16
104 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
106 BST_UNCHECKED
, /* BS_PUSHBUTTON */
107 BST_UNCHECKED
, /* BS_DEFPUSHBUTTON */
108 BST_CHECKED
, /* BS_CHECKBOX */
109 BST_CHECKED
, /* BS_AUTOCHECKBOX */
110 BST_CHECKED
, /* BS_RADIOBUTTON */
111 BST_INDETERMINATE
, /* BS_3STATE */
112 BST_INDETERMINATE
, /* BS_AUTO3STATE */
113 BST_UNCHECKED
, /* BS_GROUPBOX */
114 BST_UNCHECKED
, /* BS_USERBUTTON */
115 BST_CHECKED
, /* BS_AUTORADIOBUTTON */
116 BST_UNCHECKED
, /* BS_PUSHBOX */
117 BST_UNCHECKED
/* BS_OWNERDRAW */
120 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
122 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
124 PB_Paint
, /* BS_PUSHBUTTON */
125 PB_Paint
, /* BS_DEFPUSHBUTTON */
126 CB_Paint
, /* BS_CHECKBOX */
127 CB_Paint
, /* BS_AUTOCHECKBOX */
128 CB_Paint
, /* BS_RADIOBUTTON */
129 CB_Paint
, /* BS_3STATE */
130 CB_Paint
, /* BS_AUTO3STATE */
131 GB_Paint
, /* BS_GROUPBOX */
132 UB_Paint
, /* BS_USERBUTTON */
133 CB_Paint
, /* BS_AUTORADIOBUTTON */
134 NULL
, /* BS_PUSHBOX */
135 OB_Paint
/* BS_OWNERDRAW */
138 static HBITMAP hbitmapCheckBoxes
= 0;
139 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
142 /*********************************************************************
143 * button class descriptor
145 static const WCHAR buttonW
[] = {'B','u','t','t','o','n',0};
146 const struct builtin_class_descr BUTTON_builtin_class
=
149 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
151 ButtonWndProcA
, /* procA */
152 ButtonWndProcW
, /* procW */
154 WINPROC_BUTTON
, /* proc */
156 NB_EXTRA_BYTES
, /* extra */
157 IDC_ARROW
, /* cursor */
162 static inline LONG
get_button_state( HWND hwnd
)
164 return GetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
);
167 static inline void set_button_state( HWND hwnd
, LONG state
)
169 SetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
, state
);
174 static __inline
void set_ui_state( HWND hwnd
, LONG flags
)
176 SetWindowLongPtrW( hwnd
, UISTATE_GWL_OFFSET
, flags
);
179 static __inline LONG
get_ui_state( HWND hwnd
)
181 return GetWindowLongPtrW( hwnd
, UISTATE_GWL_OFFSET
);
184 #endif /* __REACTOS__ */
186 static inline HFONT
get_button_font( HWND hwnd
)
188 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
191 static inline void set_button_font( HWND hwnd
, HFONT font
)
193 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
196 static inline UINT
get_button_type( LONG window_style
)
198 return (window_style
& BS_TYPEMASK
);
201 /* paint a button of any type */
202 static inline void paint_button( HWND hwnd
, LONG style
, UINT action
)
204 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
206 HDC hdc
= GetDC( hwnd
);
207 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
208 ReleaseDC( hwnd
, hdc
);
212 /* retrieve the button text; returned buffer must be freed by caller */
213 static inline WCHAR
*get_button_text( HWND hwnd
)
216 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
217 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
222 /* Retrieve the UI state for the control */
223 static BOOL
button_update_uistate(HWND hwnd
, BOOL unicode
)
225 LONG flags
, prevflags
;
228 flags
= DefWindowProcW(hwnd
, WM_QUERYUISTATE
, 0, 0);
230 flags
= DefWindowProcA(hwnd
, WM_QUERYUISTATE
, 0, 0);
232 prevflags
= get_ui_state(hwnd
);
234 if (prevflags
!= flags
)
236 set_ui_state(hwnd
, flags
);
244 /***********************************************************************
245 * ButtonWndProc_common
247 LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
248 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
252 LONG style
= GetWindowLongPtrW( hWnd
, GWL_STYLE
);
253 UINT btn_type
= get_button_type( style
);
259 pWnd
= ValidateHwnd(hWnd
);
264 NtUserSetWindowFNID(hWnd
, FNID_BUTTON
);
268 if (pWnd
->fnid
!= FNID_BUTTON
)
270 ERR("Wrong window class for Button! fnId 0x%x\n",pWnd
->fnid
);
277 if (!IsWindow( hWnd
)) return 0;
280 pt
.x
= (short)LOWORD(lParam
);
281 pt
.y
= (short)HIWORD(lParam
);
289 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
290 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
292 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
293 case BS_GROUPBOX
: return DLGC_STATIC
;
294 default: return DLGC_BUTTON
;
298 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
302 if (!hbitmapCheckBoxes
)
305 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
306 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
307 checkBoxWidth
= bmp
.bmWidth
/ 4;
308 checkBoxHeight
= bmp
.bmHeight
/ 3;
310 if (btn_type
>= MAX_BTN_TYPE
)
311 return -1; /* abort */
313 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
314 if (btn_type
== BS_USERBUTTON
)
316 style
= (style
& ~BS_TYPEMASK
) | BS_PUSHBUTTON
;
318 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
320 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
323 set_button_state( hWnd
, BST_UNCHECKED
);
325 button_update_uistate( hWnd
, unicode
);
331 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
337 if (btn_type
== BS_OWNERDRAW
)
339 HDC hdc
= (HDC
)wParam
;
342 HWND parent
= GetParent(hWnd
);
343 if (!parent
) parent
= hWnd
;
345 hBrush
= GetControlColor( parent
, hWnd
, hdc
, WM_CTLCOLORBTN
);
347 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
348 if (!hBrush
) /* did the app forget to call defwindowproc ? */
349 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
350 (WPARAM
)hdc
, (LPARAM
)hWnd
);
352 GetClientRect(hWnd
, &rc
);
353 FillRect(hdc
, &rc
, hBrush
);
361 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
362 if (btnPaintFunc
[btn_type
])
364 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
365 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
366 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
368 if ( !wParam
) EndPaint( hWnd
, &ps
);
373 if (wParam
== VK_SPACE
)
375 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
376 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
381 case WM_LBUTTONDBLCLK
:
382 if(style
& BS_NOTIFY
||
383 btn_type
== BS_RADIOBUTTON
||
384 btn_type
== BS_USERBUTTON
||
385 btn_type
== BS_OWNERDRAW
)
387 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
394 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
395 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
399 if (wParam
!= VK_SPACE
)
404 BOOL TellParent
= FALSE
; //// ReactOS see note below.
405 state
= get_button_state( hWnd
);
406 if (!(state
& BUTTON_BTNPRESSED
)) break;
407 state
&= BUTTON_NSTATES
;
408 set_button_state( hWnd
, state
);
409 if (!(state
& BST_PUSHED
))
414 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
415 GetClientRect( hWnd
, &rect
);
416 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
418 state
= get_button_state( hWnd
);
421 case BS_AUTOCHECKBOX
:
422 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BST_CHECKED
), 0 );
424 case BS_AUTORADIOBUTTON
:
425 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
428 SendMessageW( hWnd
, BM_SETCHECK
,
429 (state
& BST_INDETERMINATE
) ? 0 : ((state
& 3) + 1), 0 );
432 TellParent
= TRUE
; // <---- Fix CORE-10194, Notify parent after capture is released.
435 if (TellParent
) BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
439 case WM_CAPTURECHANGED
:
440 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
441 state
= get_button_state( hWnd
);
442 if (state
& BUTTON_BTNPRESSED
)
444 state
&= BUTTON_NSTATES
;
445 set_button_state( hWnd
, state
);
446 if (state
& BST_PUSHED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
451 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
453 GetClientRect( hWnd
, &rect
);
454 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
460 /* Clear an old text here as Windows does */
462 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
463 // Patch: http://source.winehq.org/patches/data/70889
464 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
467 if (style
& WS_VISIBLE
)
470 HDC hdc
= GetDC(hWnd
);
473 HWND parent
= GetParent(hWnd
);
475 UINT ctlMessage
= (btn_type
== BS_PUSHBUTTON
||
476 btn_type
== BS_DEFPUSHBUTTON
||
477 btn_type
== BS_PUSHLIKE
||
478 btn_type
== BS_USERBUTTON
||
479 btn_type
== BS_OWNERDRAW
) ?
480 WM_CTLCOLORBTN
: WM_CTLCOLORSTATIC
;
483 if (!parent
) parent
= hWnd
;
485 hbrush
= GetControlColor(parent
, hWnd
, hdc
, ctlMessage
);
487 hbrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
488 (WPARAM
)hdc
, (LPARAM
)hWnd
);
489 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
490 hbrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
491 (WPARAM
)hdc
, (LPARAM
)hWnd
);
494 GetClientRect(hWnd
, &client
);
496 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
497 /* Clip by client rect bounds */
498 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
499 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
500 FillRect(hdc
, &rc
, hbrush
);
501 ReleaseDC(hWnd
, hdc
);
506 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
507 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
508 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
509 InvalidateRect( hWnd
, NULL
, TRUE
);
511 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
512 return 1; /* success. FIXME: check text length */
516 set_button_font( hWnd
, (HFONT
)wParam
);
517 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
521 return (LRESULT
)get_button_font( hWnd
);
524 TRACE("WM_SETFOCUS %p\n",hWnd
);
525 set_button_state( hWnd
, get_button_state(hWnd
) | BST_FOCUS
);
526 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
527 if (style
& BS_NOTIFY
)
528 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
532 TRACE("WM_KILLFOCUS %p\n",hWnd
);
533 state
= get_button_state( hWnd
);
534 set_button_state( hWnd
, state
& ~BST_FOCUS
);
535 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
537 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
539 if (style
& BS_NOTIFY
)
540 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
542 InvalidateRect( hWnd
, NULL
, FALSE
);
545 case WM_SYSCOLORCHANGE
:
546 InvalidateRect( hWnd
, NULL
, FALSE
);
550 btn_type
= wParam
& BS_TYPEMASK
;
551 style
= (style
& ~BS_TYPEMASK
) | btn_type
;
553 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
555 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
558 /* Only redraw if lParam flag is set.*/
560 InvalidateRect( hWnd
, NULL
, TRUE
);
566 state
= get_button_state(hWnd
);
567 if (state
& BUTTON_BMCLICK
)
569 set_button_state(hWnd
, state
| BUTTON_BMCLICK
); // Tracked in STATE_GWL_OFFSET.
571 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
572 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
574 state
= get_button_state(hWnd
);
575 if (!(state
& BUTTON_BMCLICK
)) break;
576 state
&= ~BUTTON_BMCLICK
;
577 set_button_state(hWnd
, state
);
582 /* Check that image format matches button style */
583 switch (style
& (BS_BITMAP
|BS_ICON
))
586 if (wParam
!= IMAGE_BITMAP
) return 0;
589 if (wParam
!= IMAGE_ICON
) return 0;
594 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
595 InvalidateRect( hWnd
, NULL
, FALSE
);
596 return (LRESULT
)oldHbitmap
;
599 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
602 return get_button_state( hWnd
) & 3;
605 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
606 state
= get_button_state( hWnd
);
607 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
610 if (wParam
) style
|= WS_TABSTOP
;
611 else style
&= ~WS_TABSTOP
;
612 NtUserAlterWindowStyle(hWnd
, GWL_STYLE
, style
);
614 if (wParam
) WIN_SetStyle( hWnd
, WS_TABSTOP
, 0 );
615 else WIN_SetStyle( hWnd
, 0, WS_TABSTOP
);
618 if ((state
& 3) != wParam
)
620 set_button_state( hWnd
, (state
& ~3) | wParam
);
621 paint_button( hWnd
, btn_type
, ODA_SELECT
);
623 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BST_CHECKED
) && (style
& WS_CHILD
))
624 BUTTON_CheckAutoRadioButton( hWnd
);
628 return get_button_state( hWnd
);
631 state
= get_button_state( hWnd
);
633 set_button_state( hWnd
, state
| BST_PUSHED
);
635 set_button_state( hWnd
, state
& ~BST_PUSHED
);
637 paint_button( hWnd
, btn_type
, ODA_SELECT
);
641 case WM_UPDATEUISTATE
:
643 DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
645 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
647 if (button_update_uistate(hWnd
, unicode
))
648 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
653 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
656 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
657 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
664 /***********************************************************************
666 * The button window procedure. This is just a wrapper which locks
667 * the passed HWND and calls the real window procedure (with a WND*
668 * pointer pointing to the locked windowstructure).
670 LRESULT WINAPI
ButtonWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
672 if (!IsWindow(hWnd
)) return 0;
673 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);
676 /***********************************************************************
679 LRESULT WINAPI
ButtonWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
681 if (!IsWindow(hWnd
)) return 0;
682 return ButtonWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
685 #endif /* __REACTOS__ */
687 /**********************************************************************
688 * Convert button styles to flags used by DrawText.
690 static UINT
BUTTON_BStoDT( DWORD style
, DWORD ex_style
)
692 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
694 /* "Convert" pushlike buttons to pushbuttons */
695 if (style
& BS_PUSHLIKE
)
696 style
&= ~BS_TYPEMASK
;
698 if (!(style
& BS_MULTILINE
))
699 dtStyle
|= DT_SINGLELINE
;
701 dtStyle
|= DT_WORDBREAK
;
703 switch (style
& BS_CENTER
)
705 case BS_LEFT
: /* DT_LEFT is 0 */ break;
706 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
707 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
709 /* Pushbutton's text is centered by default */
710 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
711 /* all other flavours have left aligned text */
714 if (ex_style
& WS_EX_RIGHT
) dtStyle
= DT_RIGHT
| (dtStyle
& ~(DT_LEFT
| DT_CENTER
));
716 /* DrawText ignores vertical alignment for multiline text,
717 * but we use these flags to align label manually.
719 if (get_button_type(style
) != BS_GROUPBOX
)
721 switch (style
& BS_VCENTER
)
723 case BS_TOP
: /* DT_TOP is 0 */ break;
724 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
725 case BS_VCENTER
: /* fall through */
726 default: dtStyle
|= DT_VCENTER
; break;
730 /* GroupBox's text is always single line and is top aligned. */
731 dtStyle
|= DT_SINGLELINE
;
736 /**********************************************************************
737 * BUTTON_CalcLabelRect
739 * Calculates label's rectangle depending on button style.
741 * Returns flags to be passed to DrawText.
742 * Calculated rectangle doesn't take into account button state
743 * (pushed, etc.). If there is nothing to draw (no text/image) output
744 * rectangle is empty, and return value is (UINT)-1.
746 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
748 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
749 LONG ex_style
= GetWindowLongPtrW( hwnd
, GWL_EXSTYLE
);
753 UINT dtStyle
= BUTTON_BStoDT( style
, ex_style
);
757 /* Calculate label rectangle according to label type */
758 switch (style
& (BS_ICON
|BS_BITMAP
))
761 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
764 HeapFree( GetProcessHeap(), 0, text
);
767 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
768 HeapFree( GetProcessHeap(), 0, text
);
770 if (get_ui_state(hwnd
) & UISF_HIDEACCEL
)
771 dtStyle
|= DT_HIDEPREFIX
;
776 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
779 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
781 r
.right
= r
.left
+ bm
.bmWidth
;
782 r
.bottom
= r
.top
+ bm
.bmHeight
;
784 DeleteObject(iconInfo
.hbmColor
);
785 DeleteObject(iconInfo
.hbmMask
);
789 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
792 r
.right
= r
.left
+ bm
.bmWidth
;
793 r
.bottom
= r
.top
+ bm
.bmHeight
;
803 /* Position label inside bounding rectangle according to
804 * alignment flags. (calculated rect is always left-top aligned).
805 * If label is aligned to any side - shift label in opposite
806 * direction to leave extra space for focus rectangle.
808 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
810 case DT_LEFT
: r
.left
++; r
.right
++; break;
811 case DT_CENTER
: n
= r
.right
- r
.left
;
812 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
813 r
.right
= r
.left
+ n
; break;
814 case DT_RIGHT
: n
= r
.right
- r
.left
;
815 r
.right
= rc
->right
- 1;
816 r
.left
= r
.right
- n
;
820 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
822 case DT_TOP
: r
.top
++; r
.bottom
++; break;
823 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
824 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
825 r
.bottom
= r
.top
+ n
; break;
826 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
827 r
.bottom
= rc
->bottom
- 1;
828 r
.top
= r
.bottom
- n
;
837 /**********************************************************************
838 * BUTTON_DrawTextCallback
840 * Callback function used by DrawStateW function.
842 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
850 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
855 /**********************************************************************
858 * Common function for drawing button label.
860 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
862 DRAWSTATEPROC lpOutputProc
= NULL
;
866 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
867 LONG state
= get_button_state( hwnd
);
868 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
871 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
872 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
873 * I don't have Win31 on hand to verify that, so I leave it as is.
876 if ((style
& BS_PUSHLIKE
) && (state
& BST_INDETERMINATE
))
878 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
882 switch (style
& (BS_ICON
|BS_BITMAP
))
885 /* DST_COMPLEX -- is 0 */
886 lpOutputProc
= BUTTON_DrawTextCallback
;
887 if (!(text
= get_button_text( hwnd
))) return;
889 wp
= (WPARAM
)dtFlags
;
892 if (dtFlags
& DT_HIDEPREFIX
)
893 flags
|= DSS_HIDEPREFIX
;
899 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
904 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
911 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
912 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
913 HeapFree( GetProcessHeap(), 0, text
);
916 /**********************************************************************
917 * Push Button Functions
919 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
922 UINT dtFlags
, uState
;
926 COLORREF oldTxtColor
;
928 LONG state
= get_button_state( hwnd
);
929 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
930 BOOL pushedState
= (state
& BST_PUSHED
);
934 GetClientRect( hwnd
, &rc
);
936 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
937 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
938 parent
= GetParent(hwnd
);
939 if (!parent
) parent
= hwnd
;
941 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
943 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
946 hrgn
= set_control_clipping( hDC
, &rc
);
948 hOldPen
= SelectObject(hDC
, GetStockObject(DC_PEN
));
949 SetDCPenColor(hDC
, GetSysColor(COLOR_WINDOWFRAME
));
951 hOldPen
= SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
953 hOldBrush
= SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
954 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
956 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
958 if (action
!= ODA_FOCUS
)
959 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
960 InflateRect( &rc
, -1, -1 );
963 /* completely skip the drawing if only focus has changed */
964 if (action
== ODA_FOCUS
) goto draw_focus
;
966 uState
= DFCS_BUTTONPUSH
;
970 else if (pushedState
)
972 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
975 uState
|= DFCS_PUSHED
;
978 if (state
& (BST_CHECKED
| BST_INDETERMINATE
))
979 uState
|= DFCS_CHECKED
;
981 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
983 /* draw button label */
985 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
987 if (dtFlags
== (UINT
)-1L)
991 OffsetRect(&r
, 1, 1);
993 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
995 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
997 SetTextColor( hDC
, oldTxtColor
);
1000 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1003 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1006 InflateRect( &rc
, -2, -2 );
1007 DrawFocusRect( hDC
, &rc
);
1014 SelectObject( hDC
, hOldPen
);
1015 SelectObject( hDC
, hOldBrush
);
1016 SetBkMode(hDC
, oldBkMode
);
1017 SelectClipRgn( hDC
, hrgn
);
1018 if (hrgn
) DeleteObject( hrgn
);
1021 /**********************************************************************
1022 * Check Box & Radio Button Functions
1025 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1027 RECT rbox
, rtext
, client
;
1032 LONG state
= get_button_state( hwnd
);
1033 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1037 if (style
& BS_PUSHLIKE
)
1039 PB_Paint( hwnd
, hDC
, action
);
1043 GetClientRect(hwnd
, &client
);
1044 rbox
= rtext
= client
;
1046 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1048 parent
= GetParent(hwnd
);
1049 if (!parent
) parent
= hwnd
;
1051 hBrush
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1053 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
1054 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1055 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1056 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1057 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1059 hrgn
= set_control_clipping( hDC
, &client
);
1061 if (style
& BS_LEFTTEXT
)
1063 /* magic +4 is what CTL3D expects */
1065 rtext
.right
-= checkBoxWidth
+ 4;
1066 rbox
.left
= rbox
.right
- checkBoxWidth
;
1070 rtext
.left
+= checkBoxWidth
+ 4;
1071 rbox
.right
= checkBoxWidth
;
1074 /* Since WM_ERASEBKGND does nothing, first prepare background */
1075 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
1076 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
1080 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
1082 /* Only adjust rbox when rtext is valid */
1083 if (dtFlags
!= (UINT
)-1L)
1085 rbox
.top
= rtext
.top
;
1086 rbox
.bottom
= rtext
.bottom
;
1089 /* Draw the check-box bitmap */
1090 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
1094 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
1095 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
1096 else if (state
& BST_INDETERMINATE
) flags
= DFCS_BUTTON3STATE
;
1097 else flags
= DFCS_BUTTONCHECK
;
1099 if (state
& (BST_CHECKED
| BST_INDETERMINATE
)) flags
|= DFCS_CHECKED
;
1100 if (state
& BST_PUSHED
) flags
|= DFCS_PUSHED
;
1102 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
1104 /* rbox must have the correct height */
1105 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
1107 if (style
& BS_TOP
) {
1109 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1111 rbox
.top
-= -delta
/2 + 1;
1112 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1114 } else if (style
& BS_BOTTOM
) {
1116 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1118 rbox
.bottom
+= -delta
/2 + 1;
1119 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1121 } else { /* Default */
1123 int ofs
= (delta
/ 2);
1124 rbox
.bottom
-= ofs
+ 1;
1125 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1126 } else if (delta
< 0) {
1127 int ofs
= (-delta
/ 2);
1128 rbox
.top
-= ofs
+ 1;
1129 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1133 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
1136 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
1139 if (action
== ODA_DRAWENTIRE
)
1140 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
1143 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1146 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1151 IntersectRect(&rtext
, &rtext
, &client
);
1152 DrawFocusRect( hDC
, &rtext
);
1157 SelectClipRgn( hDC
, hrgn
);
1158 if (hrgn
) DeleteObject( hrgn
);
1162 /**********************************************************************
1163 * BUTTON_CheckAutoRadioButton
1165 * hwnd is checked, uncheck every other auto radio button in group
1167 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1169 HWND parent
, sibling
, start
;
1171 parent
= GetParent(hwnd
);
1172 /* make sure that starting control is not disabled or invisible */
1173 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1176 if (!sibling
) break;
1177 if ((hwnd
!= sibling
) &&
1178 ((GetWindowLongPtrW( sibling
, GWL_STYLE
) & BS_TYPEMASK
) == BS_AUTORADIOBUTTON
))
1179 SendMessageW( sibling
, BM_SETCHECK
, BST_UNCHECKED
, 0 );
1180 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1181 } while (sibling
!= start
);
1185 /**********************************************************************
1186 * Group Box Functions
1189 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1196 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1200 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1201 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1202 parent
= GetParent(hwnd
);
1203 if (!parent
) parent
= hwnd
;
1205 hbr
= GetControlColor(parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1207 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1208 if (!hbr
) /* did the app forget to call defwindowproc ? */
1209 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1210 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1212 GetClientRect( hwnd
, &rc
);
1214 hrgn
= set_control_clipping( hDC
, &rc
);
1216 GetTextMetricsW (hDC
, &tm
);
1217 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1218 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1220 InflateRect(&rc
, -7, 1);
1221 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1223 if (dtFlags
!= (UINT
)-1L)
1225 /* Because buttons have CS_PARENTDC class style, there is a chance
1226 * that label will be drawn out of client rect.
1227 * But Windows doesn't clip label's rect, so do I.
1230 /* There is 1-pixel margin at the left, right, and bottom */
1231 rc
.left
--; rc
.right
++; rc
.bottom
++;
1232 FillRect(hDC
, &rc
, hbr
);
1233 rc
.left
++; rc
.right
--; rc
.bottom
--;
1235 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1237 SelectClipRgn( hDC
, hrgn
);
1238 if (hrgn
) DeleteObject( hrgn
);
1242 /**********************************************************************
1243 * User Button Functions
1246 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1251 LONG state
= get_button_state( hwnd
);
1254 GetClientRect( hwnd
, &rc
);
1256 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1258 parent
= GetParent(hwnd
);
1259 if (!parent
) parent
= hwnd
;
1261 hBrush
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1263 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1264 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1265 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1266 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1269 FillRect( hDC
, &rc
, hBrush
);
1270 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1273 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1275 DrawFocusRect( hDC
, &rc
);
1283 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_FOCUS
) ? BN_SETFOCUS
: BN_KILLFOCUS
);
1287 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_PUSHED
) ? BN_HILITE
: BN_UNHILITE
);
1291 BUTTON_NOTIFY_PARENT( hwnd
, BN_PAINT
);
1297 /**********************************************************************
1298 * Ownerdrawn Button Functions
1301 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1303 LONG state
= get_button_state( hwnd
);
1305 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1307 HFONT hFont
, hPrevFont
= 0;
1310 dis
.CtlType
= ODT_BUTTON
;
1313 dis
.itemAction
= action
;
1314 dis
.itemState
= ((state
& BST_FOCUS
) ? ODS_FOCUS
: 0) |
1315 ((state
& BST_PUSHED
) ? ODS_SELECTED
: 0) |
1316 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1317 dis
.hwndItem
= hwnd
;
1320 GetClientRect( hwnd
, &dis
.rcItem
);
1322 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1323 parent
= GetParent(hwnd
);
1324 if (!parent
) parent
= hwnd
;
1326 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1328 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1331 hrgn
= set_control_clipping( hDC
, &dis
.rcItem
);
1333 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1334 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1335 SelectClipRgn( hDC
, hrgn
);
1336 if (hrgn
) DeleteObject( hrgn
);