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_NOTIFY_PARENT(hWnd, code) \
84 do { /* Notify parent which has created this button control */ \
85 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
86 SendMessageW(GetParent(hWnd), WM_COMMAND, \
87 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
91 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
92 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
93 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
94 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
95 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
96 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
97 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
99 #define MAX_BTN_TYPE 16
101 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
103 BST_UNCHECKED
, /* BS_PUSHBUTTON */
104 BST_UNCHECKED
, /* BS_DEFPUSHBUTTON */
105 BST_CHECKED
, /* BS_CHECKBOX */
106 BST_CHECKED
, /* BS_AUTOCHECKBOX */
107 BST_CHECKED
, /* BS_RADIOBUTTON */
108 BST_INDETERMINATE
, /* BS_3STATE */
109 BST_INDETERMINATE
, /* BS_AUTO3STATE */
110 BST_UNCHECKED
, /* BS_GROUPBOX */
111 BST_UNCHECKED
, /* BS_USERBUTTON */
112 BST_CHECKED
, /* BS_AUTORADIOBUTTON */
113 BST_UNCHECKED
, /* BS_PUSHBOX */
114 BST_UNCHECKED
/* BS_OWNERDRAW */
117 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
119 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
121 PB_Paint
, /* BS_PUSHBUTTON */
122 PB_Paint
, /* BS_DEFPUSHBUTTON */
123 CB_Paint
, /* BS_CHECKBOX */
124 CB_Paint
, /* BS_AUTOCHECKBOX */
125 CB_Paint
, /* BS_RADIOBUTTON */
126 CB_Paint
, /* BS_3STATE */
127 CB_Paint
, /* BS_AUTO3STATE */
128 GB_Paint
, /* BS_GROUPBOX */
129 UB_Paint
, /* BS_USERBUTTON */
130 CB_Paint
, /* BS_AUTORADIOBUTTON */
131 NULL
, /* BS_PUSHBOX */
132 OB_Paint
/* BS_OWNERDRAW */
135 static HBITMAP hbitmapCheckBoxes
= 0;
136 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
139 /*********************************************************************
140 * button class descriptor
142 static const WCHAR buttonW
[] = {'B','u','t','t','o','n',0};
143 const struct builtin_class_descr BUTTON_builtin_class
=
146 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
147 ButtonWndProcA
, /* procA */
148 ButtonWndProcW
, /* procW */
149 NB_EXTRA_BYTES
, /* extra */
150 IDC_ARROW
, /* cursor */
155 static inline LONG
get_button_state( HWND hwnd
)
157 return GetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
);
160 static inline void set_button_state( HWND hwnd
, LONG state
)
162 SetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
, state
);
165 static __inline
void set_ui_state( HWND hwnd
, LONG flags
)
167 SetWindowLongPtrW( hwnd
, UISTATE_GWL_OFFSET
, flags
);
170 static __inline LONG
get_ui_state( HWND hwnd
)
172 return GetWindowLongPtrW( hwnd
, UISTATE_GWL_OFFSET
);
175 __inline
static HFONT
get_button_font( HWND hwnd
)
177 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
180 static inline void set_button_font( HWND hwnd
, HFONT font
)
182 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
185 static inline UINT
get_button_type( LONG window_style
)
187 return (window_style
& BS_TYPEMASK
);
190 /* paint a button of any type */
191 static inline void paint_button( HWND hwnd
, LONG style
, UINT action
)
193 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
195 HDC hdc
= GetDC( hwnd
);
196 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
197 ReleaseDC( hwnd
, hdc
);
201 /* retrieve the button text; returned buffer must be freed by caller */
202 static inline WCHAR
*get_button_text( HWND hwnd
)
205 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
206 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
210 /* Retrieve the UI state for the control */
211 static BOOL
button_update_uistate(HWND hwnd
, BOOL unicode
)
213 LONG flags
, prevflags
;
216 flags
= DefWindowProcW(hwnd
, WM_QUERYUISTATE
, 0, 0);
218 flags
= DefWindowProcA(hwnd
, WM_QUERYUISTATE
, 0, 0);
220 prevflags
= get_ui_state(hwnd
);
222 if (prevflags
!= flags
)
224 set_ui_state(hwnd
, flags
);
231 /***********************************************************************
232 * ButtonWndProc_common
234 LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
235 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
239 LONG style
= GetWindowLongPtrW( hWnd
, GWL_STYLE
);
240 UINT btn_type
= get_button_type( style
);
246 pWnd
= ValidateHwnd(hWnd
);
251 NtUserSetWindowFNID(hWnd
, FNID_BUTTON
);
255 if (pWnd
->fnid
!= FNID_BUTTON
)
257 ERR("Wrong window class for Button! fnId 0x%x\n",pWnd
->fnid
);
264 pt
.x
= (short)LOWORD(lParam
);
265 pt
.y
= (short)HIWORD(lParam
);
273 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
274 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
276 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
277 case BS_GROUPBOX
: return DLGC_STATIC
;
278 default: return DLGC_BUTTON
;
282 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
286 if (!hbitmapCheckBoxes
)
289 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
290 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
291 checkBoxWidth
= bmp
.bmWidth
/ 4;
292 checkBoxHeight
= bmp
.bmHeight
/ 3;
294 if (btn_type
>= MAX_BTN_TYPE
)
295 return -1; /* abort */
297 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
298 if (btn_type
== BS_USERBUTTON
)
301 style
= (style
& ~BS_TYPEMASK
) | BS_PUSHBUTTON
;
302 SetWindowLongPtrW( hWnd
, GWL_STYLE
, style
);
304 style
= (style
& ~BS_TYPEMASK
) | BS_PUSHBUTTON
;
305 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
308 set_button_state( hWnd
, BST_UNCHECKED
);
309 button_update_uistate( hWnd
, unicode
);
314 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
320 if (btn_type
== BS_OWNERDRAW
)
322 HDC hdc
= (HDC
)wParam
;
325 HWND parent
= GetParent(hWnd
);
326 if (!parent
) parent
= hWnd
;
327 hBrush
= GetControlColor( parent
, hWnd
, hdc
, WM_CTLCOLORBTN
);
328 GetClientRect(hWnd
, &rc
);
329 FillRect(hdc
, &rc
, hBrush
);
335 if (btnPaintFunc
[btn_type
])
338 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
339 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
340 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
341 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
342 if( !wParam
) EndPaint( hWnd
, &ps
);
347 if (wParam
== VK_SPACE
)
349 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
350 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
355 case WM_LBUTTONDBLCLK
:
356 if(style
& BS_NOTIFY
||
357 btn_type
== BS_RADIOBUTTON
||
358 btn_type
== BS_USERBUTTON
||
359 btn_type
== BS_OWNERDRAW
)
361 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
368 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
369 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
373 if (wParam
!= VK_SPACE
)
377 state
= get_button_state( hWnd
);
378 if (!(state
& BUTTON_BTNPRESSED
)) break;
379 state
&= BUTTON_NSTATES
;
380 set_button_state( hWnd
, state
);
381 if (!(state
& BST_PUSHED
))
386 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
388 GetClientRect( hWnd
, &rect
);
389 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
391 state
= get_button_state( hWnd
);
394 case BS_AUTOCHECKBOX
:
395 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BST_CHECKED
), 0 );
397 case BS_AUTORADIOBUTTON
:
398 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
401 SendMessageW( hWnd
, BM_SETCHECK
,
402 (state
& BST_INDETERMINATE
) ? 0 : ((state
& 3) + 1), 0 );
405 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
409 case WM_CAPTURECHANGED
:
410 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
411 state
= get_button_state( hWnd
);
412 if (state
& BUTTON_BTNPRESSED
)
414 state
&= BUTTON_NSTATES
;
415 set_button_state( hWnd
, state
);
416 if (state
& BST_PUSHED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
421 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
423 GetClientRect( hWnd
, &rect
);
424 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
430 /* Clear an old text here as Windows does */
432 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
433 // Patch: http://source.winehq.org/patches/data/70889
434 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
436 if (style
& WS_VISIBLE
)
438 HDC hdc
= GetDC(hWnd
);
441 HWND parent
= GetParent(hWnd
);
442 UINT ctlMessage
=(btn_type
== BS_PUSHBUTTON
||
443 btn_type
== BS_DEFPUSHBUTTON
||
444 btn_type
== BS_PUSHLIKE
||
445 btn_type
== BS_USERBUTTON
||
446 btn_type
== BS_OWNERDRAW
) ?
447 WM_CTLCOLORBTN
: WM_CTLCOLORSTATIC
;
449 if (!parent
) parent
= hWnd
;
451 hbrush
= GetControlColor( parent
, hWnd
, hdc
, ctlMessage
);
453 GetClientRect(hWnd
, &client
);
455 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
456 /* Clip by client rect bounds */
457 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
458 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
459 FillRect(hdc
, &rc
, hbrush
);
460 ReleaseDC(hWnd
, hdc
);
463 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
464 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
465 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
466 InvalidateRect( hWnd
, NULL
, TRUE
);
468 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
469 return 1; /* success. FIXME: check text length */
473 set_button_font( hWnd
, (HFONT
)wParam
);
474 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
478 return (LRESULT
)get_button_font( hWnd
);
481 TRACE("WM_SETFOCUS %p\n",hWnd
);
482 set_button_state( hWnd
, get_button_state(hWnd
) | BST_FOCUS
);
483 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
484 if (style
& BS_NOTIFY
)
485 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
489 TRACE("WM_KILLFOCUS %p\n",hWnd
);
490 state
= get_button_state( hWnd
);
491 set_button_state( hWnd
, state
& ~BST_FOCUS
);
492 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
494 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
496 if (style
& BS_NOTIFY
)
497 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
499 InvalidateRect( hWnd
, NULL
, FALSE
);
502 case WM_SYSCOLORCHANGE
:
503 InvalidateRect( hWnd
, NULL
, FALSE
);
507 if ((wParam
& BS_TYPEMASK
) >= MAX_BTN_TYPE
) break;
508 btn_type
= wParam
& BS_TYPEMASK
;
509 style
= (style
& ~BS_TYPEMASK
) | btn_type
;
510 SetWindowLongPtrW( hWnd
, GWL_STYLE
, style
);
511 //WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
513 /* Only redraw if lParam flag is set.*/
515 InvalidateRect( hWnd
, NULL
, TRUE
);
520 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
521 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
525 /* Check that image format matches button style */
526 switch (style
& (BS_BITMAP
|BS_ICON
))
529 if (wParam
!= IMAGE_BITMAP
) return 0;
532 if (wParam
!= IMAGE_ICON
) return 0;
537 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
538 InvalidateRect( hWnd
, NULL
, FALSE
);
539 return (LRESULT
)oldHbitmap
;
542 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
545 return get_button_state( hWnd
) & 3;
548 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
549 state
= get_button_state( hWnd
);
550 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
553 if (wParam
) style
|= WS_TABSTOP
;
554 else style
&= ~WS_TABSTOP
;
555 SetWindowLongPtrW( hWnd
, GWL_STYLE
, style
);
557 if (wParam
) WIN_SetStyle( hWnd
, WS_TABSTOP
, 0 );
558 else WIN_SetStyle( hWnd
, 0, WS_TABSTOP
);
561 if ((state
& 3) != wParam
)
563 set_button_state( hWnd
, (state
& ~3) | wParam
);
564 paint_button( hWnd
, btn_type
, ODA_SELECT
);
566 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BST_CHECKED
) && (style
& WS_CHILD
))
567 BUTTON_CheckAutoRadioButton( hWnd
);
571 return get_button_state( hWnd
);
574 state
= get_button_state( hWnd
);
576 set_button_state( hWnd
, state
| BST_PUSHED
);
578 set_button_state( hWnd
, state
& ~BST_PUSHED
);
580 paint_button( hWnd
, btn_type
, ODA_SELECT
);
583 case WM_UPDATEUISTATE
:
585 DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
587 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
589 if (button_update_uistate(hWnd
, unicode
))
590 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
594 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
597 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
598 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
603 /***********************************************************************
605 * The button window procedure. This is just a wrapper which locks
606 * the passed HWND and calls the real window procedure (with a WND*
607 * pointer pointing to the locked windowstructure).
609 LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
611 if (!IsWindow( hWnd
)) return 0;
612 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, TRUE
);
616 /***********************************************************************
619 LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
621 if (!IsWindow( hWnd
)) return 0;
622 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, FALSE
);
626 /**********************************************************************
627 * Convert button styles to flags used by DrawText.
629 static UINT
BUTTON_BStoDT( DWORD style
, DWORD ex_style
)
631 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
633 /* "Convert" pushlike buttons to pushbuttons */
634 if (style
& BS_PUSHLIKE
)
635 style
&= ~BS_TYPEMASK
;
637 if (!(style
& BS_MULTILINE
))
638 dtStyle
|= DT_SINGLELINE
;
640 dtStyle
|= DT_WORDBREAK
;
642 switch (style
& BS_CENTER
)
644 case BS_LEFT
: /* DT_LEFT is 0 */ break;
645 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
646 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
648 /* Pushbutton's text is centered by default */
649 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
650 /* all other flavours have left aligned text */
653 if (ex_style
& WS_EX_RIGHT
) dtStyle
= DT_RIGHT
| (dtStyle
& ~(DT_LEFT
| DT_CENTER
));
655 /* DrawText ignores vertical alignment for multiline text,
656 * but we use these flags to align label manually.
658 if (get_button_type(style
) != BS_GROUPBOX
)
660 switch (style
& BS_VCENTER
)
662 case BS_TOP
: /* DT_TOP is 0 */ break;
663 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
664 case BS_VCENTER
: /* fall through */
665 default: dtStyle
|= DT_VCENTER
; break;
669 /* GroupBox's text is always single line and is top aligned. */
670 dtStyle
|= DT_SINGLELINE
;
675 /**********************************************************************
676 * BUTTON_CalcLabelRect
678 * Calculates label's rectangle depending on button style.
680 * Returns flags to be passed to DrawText.
681 * Calculated rectangle doesn't take into account button state
682 * (pushed, etc.). If there is nothing to draw (no text/image) output
683 * rectangle is empty, and return value is (UINT)-1.
685 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
687 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
688 LONG ex_style
= GetWindowLongPtrW( hwnd
, GWL_EXSTYLE
);
692 UINT dtStyle
= BUTTON_BStoDT( style
, ex_style
);
696 /* Calculate label rectangle according to label type */
697 switch (style
& (BS_ICON
|BS_BITMAP
))
700 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
703 HeapFree( GetProcessHeap(), 0, text
);
706 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
707 HeapFree( GetProcessHeap(), 0, text
);
709 if (get_ui_state( hwnd
) & UISF_HIDEACCEL
)
710 dtStyle
|= DT_HIDEPREFIX
;
714 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
717 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
719 r
.right
= r
.left
+ bm
.bmWidth
;
720 r
.bottom
= r
.top
+ bm
.bmHeight
;
722 DeleteObject(iconInfo
.hbmColor
);
723 DeleteObject(iconInfo
.hbmMask
);
727 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
730 r
.right
= r
.left
+ bm
.bmWidth
;
731 r
.bottom
= r
.top
+ bm
.bmHeight
;
741 /* Position label inside bounding rectangle according to
742 * alignment flags. (calculated rect is always left-top aligned).
743 * If label is aligned to any side - shift label in opposite
744 * direction to leave extra space for focus rectangle.
746 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
748 case DT_LEFT
: r
.left
++; r
.right
++; break;
749 case DT_CENTER
: n
= r
.right
- r
.left
;
750 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
751 r
.right
= r
.left
+ n
; break;
752 case DT_RIGHT
: n
= r
.right
- r
.left
;
753 r
.right
= rc
->right
- 1;
754 r
.left
= r
.right
- n
;
758 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
760 case DT_TOP
: r
.top
++; r
.bottom
++; break;
761 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
762 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
763 r
.bottom
= r
.top
+ n
; break;
764 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
765 r
.bottom
= rc
->bottom
- 1;
766 r
.top
= r
.bottom
- n
;
775 /**********************************************************************
776 * BUTTON_DrawTextCallback
778 * Callback function used by DrawStateW function.
780 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
788 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
793 /**********************************************************************
796 * Common function for drawing button label.
798 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
800 DRAWSTATEPROC lpOutputProc
= NULL
;
804 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
805 LONG state
= get_button_state( hwnd
);
806 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
809 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
810 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
811 * I don't have Win31 on hand to verify that, so I leave it as is.
814 if ((style
& BS_PUSHLIKE
) && (state
& BST_INDETERMINATE
))
816 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
820 switch (style
& (BS_ICON
|BS_BITMAP
))
823 /* DST_COMPLEX -- is 0 */
824 lpOutputProc
= BUTTON_DrawTextCallback
;
825 if (!(text
= get_button_text( hwnd
))) return;
827 wp
= (WPARAM
)dtFlags
;
829 if (dtFlags
& DT_HIDEPREFIX
)
830 flags
|= DSS_HIDEPREFIX
;
835 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
840 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
847 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
848 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
849 HeapFree( GetProcessHeap(), 0, text
);
852 /**********************************************************************
853 * Push Button Functions
855 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
858 UINT dtFlags
, uState
;
862 COLORREF oldTxtColor
;
864 LONG state
= get_button_state( hwnd
);
865 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
866 BOOL pushedState
= (state
& BST_PUSHED
);
870 GetClientRect( hwnd
, &rc
);
872 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
873 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
874 parent
= GetParent(hwnd
);
875 if (!parent
) parent
= hwnd
;
876 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
878 hrgn
= set_control_clipping( hDC
, &rc
);
880 hOldPen
= SelectObject(hDC
, GetStockObject(DC_PEN
));
881 SetDCPenColor(hDC
, GetSysColor(COLOR_WINDOWFRAME
));
883 hOldPen
= SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
885 hOldBrush
= SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
886 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
888 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
890 if (action
!= ODA_FOCUS
)
891 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
892 InflateRect( &rc
, -1, -1 );
895 /* completely skip the drawing if only focus has changed */
896 if (action
== ODA_FOCUS
) goto draw_focus
;
898 uState
= DFCS_BUTTONPUSH
;
902 else if (pushedState
)
904 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
907 uState
|= DFCS_PUSHED
;
910 if (state
& (BST_CHECKED
| BST_INDETERMINATE
))
911 uState
|= DFCS_CHECKED
;
913 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
915 /* draw button label */
917 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
919 if (dtFlags
== (UINT
)-1L)
923 OffsetRect(&r
, 1, 1);
925 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
927 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
929 SetTextColor( hDC
, oldTxtColor
);
932 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
934 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
936 InflateRect( &rc
, -2, -2 );
937 DrawFocusRect( hDC
, &rc
);
942 SelectObject( hDC
, hOldPen
);
943 SelectObject( hDC
, hOldBrush
);
944 SetBkMode(hDC
, oldBkMode
);
945 SelectClipRgn( hDC
, hrgn
);
946 if (hrgn
) DeleteObject( hrgn
);
949 /**********************************************************************
950 * Check Box & Radio Button Functions
953 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
955 RECT rbox
, rtext
, client
;
960 LONG state
= get_button_state( hwnd
);
961 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
965 if (style
& BS_PUSHLIKE
)
967 PB_Paint( hwnd
, hDC
, action
);
971 GetClientRect(hwnd
, &client
);
972 rbox
= rtext
= client
;
974 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
976 parent
= GetParent(hwnd
);
977 if (!parent
) parent
= hwnd
;
978 hBrush
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
979 hrgn
= set_control_clipping( hDC
, &client
);
981 if (style
& BS_LEFTTEXT
)
983 /* magic +4 is what CTL3D expects */
985 rtext
.right
-= checkBoxWidth
+ 4;
986 rbox
.left
= rbox
.right
- checkBoxWidth
;
990 rtext
.left
+= checkBoxWidth
+ 4;
991 rbox
.right
= checkBoxWidth
;
994 /* Since WM_ERASEBKGND does nothing, first prepare background */
995 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
996 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
1000 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
1002 /* Only adjust rbox when rtext is valid */
1003 if (dtFlags
!= (UINT
)-1L)
1005 rbox
.top
= rtext
.top
;
1006 rbox
.bottom
= rtext
.bottom
;
1009 /* Draw the check-box bitmap */
1010 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
1014 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
1015 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
1016 else if (state
& BST_INDETERMINATE
) flags
= DFCS_BUTTON3STATE
;
1017 else flags
= DFCS_BUTTONCHECK
;
1019 if (state
& (BST_CHECKED
| BST_INDETERMINATE
)) flags
|= DFCS_CHECKED
;
1020 if (state
& BST_PUSHED
) flags
|= DFCS_PUSHED
;
1022 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
1024 /* rbox must have the correct height */
1025 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
1027 if (style
& BS_TOP
) {
1029 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1031 rbox
.top
-= -delta
/2 + 1;
1032 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1034 } else if (style
& BS_BOTTOM
) {
1036 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1038 rbox
.bottom
+= -delta
/2 + 1;
1039 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1041 } else { /* Default */
1043 int ofs
= (delta
/ 2);
1044 rbox
.bottom
-= ofs
+ 1;
1045 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1046 } else if (delta
< 0) {
1047 int ofs
= (-delta
/ 2);
1048 rbox
.top
-= ofs
+ 1;
1049 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1053 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
1056 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
1059 if (action
== ODA_DRAWENTIRE
)
1060 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
1063 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1065 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1069 IntersectRect(&rtext
, &rtext
, &client
);
1070 DrawFocusRect( hDC
, &rtext
);
1073 SelectClipRgn( hDC
, hrgn
);
1074 if (hrgn
) DeleteObject( hrgn
);
1078 /**********************************************************************
1079 * BUTTON_CheckAutoRadioButton
1081 * hwnd is checked, uncheck every other auto radio button in group
1083 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1085 HWND parent
, sibling
, start
;
1087 parent
= GetParent(hwnd
);
1088 /* make sure that starting control is not disabled or invisible */
1089 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1092 if (!sibling
) break;
1093 if ((hwnd
!= sibling
) &&
1094 ((GetWindowLongPtrW( sibling
, GWL_STYLE
) & BS_TYPEMASK
) == BS_AUTORADIOBUTTON
))
1095 SendMessageW( sibling
, BM_SETCHECK
, BST_UNCHECKED
, 0 );
1096 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1097 } while (sibling
!= start
);
1101 /**********************************************************************
1102 * Group Box Functions
1105 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1112 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1116 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1117 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1118 parent
= GetParent(hwnd
);
1119 if (!parent
) parent
= hwnd
;
1120 hbr
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1122 GetClientRect( hwnd
, &rc
);
1124 hrgn
= set_control_clipping( hDC
, &rc
);
1126 GetTextMetricsW (hDC
, &tm
);
1127 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1128 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1130 InflateRect(&rc
, -7, 1);
1131 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1133 if (dtFlags
!= (UINT
)-1L)
1135 /* Because buttons have CS_PARENTDC class style, there is a chance
1136 * that label will be drawn out of client rect.
1137 * But Windows doesn't clip label's rect, so do I.
1140 /* There is 1-pixel margin at the left, right, and bottom */
1141 rc
.left
--; rc
.right
++; rc
.bottom
++;
1142 FillRect(hDC
, &rc
, hbr
);
1143 rc
.left
++; rc
.right
--; rc
.bottom
--;
1145 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1147 SelectClipRgn( hDC
, hrgn
);
1148 if (hrgn
) DeleteObject( hrgn
);
1152 /**********************************************************************
1153 * User Button Functions
1156 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1161 LONG state
= get_button_state( hwnd
);
1164 GetClientRect( hwnd
, &rc
);
1166 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1168 parent
= GetParent(hwnd
);
1169 if (!parent
) parent
= hwnd
;
1170 hBrush
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1172 FillRect( hDC
, &rc
, hBrush
);
1173 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1175 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1176 DrawFocusRect( hDC
, &rc
);
1182 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_FOCUS
) ? BN_SETFOCUS
: BN_KILLFOCUS
);
1186 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_PUSHED
) ? BN_HILITE
: BN_UNHILITE
);
1190 BUTTON_NOTIFY_PARENT( hwnd
, BN_PAINT
);
1196 /**********************************************************************
1197 * Ownerdrawn Button Functions
1200 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1202 LONG state
= get_button_state( hwnd
);
1204 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1206 HFONT hFont
, hPrevFont
= 0;
1209 dis
.CtlType
= ODT_BUTTON
;
1212 dis
.itemAction
= action
;
1213 dis
.itemState
= ((state
& BST_FOCUS
) ? ODS_FOCUS
: 0) |
1214 ((state
& BST_PUSHED
) ? ODS_SELECTED
: 0) |
1215 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1216 dis
.hwndItem
= hwnd
;
1219 GetClientRect( hwnd
, &dis
.rcItem
);
1221 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1222 parent
= GetParent(hwnd
);
1223 if (!parent
) parent
= hwnd
;
1224 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1226 hrgn
= set_control_clipping( hDC
, &dis
.rcItem
);
1228 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1229 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1230 SelectClipRgn( hDC
, hrgn
);
1231 if (hrgn
) DeleteObject( hrgn
);