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
82 #define BUTTON_BMCLICK 0x100 // ReactOS Need to up to wine!
84 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
85 do { /* Notify parent which has created this button control */ \
86 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
87 SendMessageW(GetParent(hWnd), WM_COMMAND, \
88 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
92 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
93 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
94 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
95 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
96 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
97 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
98 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
100 #define MAX_BTN_TYPE 16
102 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
104 BST_UNCHECKED
, /* BS_PUSHBUTTON */
105 BST_UNCHECKED
, /* BS_DEFPUSHBUTTON */
106 BST_CHECKED
, /* BS_CHECKBOX */
107 BST_CHECKED
, /* BS_AUTOCHECKBOX */
108 BST_CHECKED
, /* BS_RADIOBUTTON */
109 BST_INDETERMINATE
, /* BS_3STATE */
110 BST_INDETERMINATE
, /* BS_AUTO3STATE */
111 BST_UNCHECKED
, /* BS_GROUPBOX */
112 BST_UNCHECKED
, /* BS_USERBUTTON */
113 BST_CHECKED
, /* BS_AUTORADIOBUTTON */
114 BST_UNCHECKED
, /* BS_PUSHBOX */
115 BST_UNCHECKED
/* BS_OWNERDRAW */
118 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
120 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
122 PB_Paint
, /* BS_PUSHBUTTON */
123 PB_Paint
, /* BS_DEFPUSHBUTTON */
124 CB_Paint
, /* BS_CHECKBOX */
125 CB_Paint
, /* BS_AUTOCHECKBOX */
126 CB_Paint
, /* BS_RADIOBUTTON */
127 CB_Paint
, /* BS_3STATE */
128 CB_Paint
, /* BS_AUTO3STATE */
129 GB_Paint
, /* BS_GROUPBOX */
130 UB_Paint
, /* BS_USERBUTTON */
131 CB_Paint
, /* BS_AUTORADIOBUTTON */
132 NULL
, /* BS_PUSHBOX */
133 OB_Paint
/* BS_OWNERDRAW */
136 static HBITMAP hbitmapCheckBoxes
= 0;
137 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
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 */
148 ButtonWndProcA
, /* procA */
149 ButtonWndProcW
, /* procW */
150 NB_EXTRA_BYTES
, /* extra */
151 IDC_ARROW
, /* cursor */
156 static inline LONG
get_button_state( HWND hwnd
)
158 return GetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
);
161 static inline void set_button_state( HWND hwnd
, LONG state
)
163 SetWindowLongPtrW( hwnd
, STATE_GWL_OFFSET
, state
);
166 static __inline
void set_ui_state( HWND hwnd
, LONG flags
)
168 SetWindowLongPtrW( hwnd
, UISTATE_GWL_OFFSET
, flags
);
171 static __inline LONG
get_ui_state( HWND hwnd
)
173 return GetWindowLongPtrW( hwnd
, UISTATE_GWL_OFFSET
);
176 __inline
static HFONT
get_button_font( HWND hwnd
)
178 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
181 static inline void set_button_font( HWND hwnd
, HFONT font
)
183 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
186 static inline UINT
get_button_type( LONG window_style
)
188 return (window_style
& BS_TYPEMASK
);
191 /* paint a button of any type */
192 static inline void paint_button( HWND hwnd
, LONG style
, UINT action
)
194 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
196 HDC hdc
= GetDC( hwnd
);
197 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
198 ReleaseDC( hwnd
, hdc
);
202 /* retrieve the button text; returned buffer must be freed by caller */
203 static inline WCHAR
*get_button_text( HWND hwnd
)
206 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
207 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
211 /* Retrieve the UI state for the control */
212 static BOOL
button_update_uistate(HWND hwnd
, BOOL unicode
)
214 LONG flags
, prevflags
;
217 flags
= DefWindowProcW(hwnd
, WM_QUERYUISTATE
, 0, 0);
219 flags
= DefWindowProcA(hwnd
, WM_QUERYUISTATE
, 0, 0);
221 prevflags
= get_ui_state(hwnd
);
223 if (prevflags
!= flags
)
225 set_ui_state(hwnd
, flags
);
232 /***********************************************************************
233 * ButtonWndProc_common
235 LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
236 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
240 LONG style
= GetWindowLongPtrW( hWnd
, GWL_STYLE
);
241 UINT btn_type
= get_button_type( style
);
247 pWnd
= ValidateHwnd(hWnd
);
252 NtUserSetWindowFNID(hWnd
, FNID_BUTTON
);
256 if (pWnd
->fnid
!= FNID_BUTTON
)
258 ERR("Wrong window class for Button! fnId 0x%x\n",pWnd
->fnid
);
265 pt
.x
= (short)LOWORD(lParam
);
266 pt
.y
= (short)HIWORD(lParam
);
274 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
275 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
277 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
278 case BS_GROUPBOX
: return DLGC_STATIC
;
279 default: return DLGC_BUTTON
;
283 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
287 if (!hbitmapCheckBoxes
)
290 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
291 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
292 checkBoxWidth
= bmp
.bmWidth
/ 4;
293 checkBoxHeight
= bmp
.bmHeight
/ 3;
295 if (btn_type
>= MAX_BTN_TYPE
)
296 return -1; /* abort */
298 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
299 if (btn_type
== BS_USERBUTTON
)
302 style
= (style
& ~BS_TYPEMASK
) | BS_PUSHBUTTON
;
303 SetWindowLongPtrW( hWnd
, GWL_STYLE
, style
);
305 style
= (style
& ~BS_TYPEMASK
) | BS_PUSHBUTTON
;
306 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
309 set_button_state( hWnd
, BST_UNCHECKED
);
310 button_update_uistate( hWnd
, unicode
);
315 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
321 if (btn_type
== BS_OWNERDRAW
)
323 HDC hdc
= (HDC
)wParam
;
326 HWND parent
= GetParent(hWnd
);
327 if (!parent
) parent
= hWnd
;
328 hBrush
= GetControlColor( parent
, hWnd
, hdc
, WM_CTLCOLORBTN
);
329 GetClientRect(hWnd
, &rc
);
330 FillRect(hdc
, &rc
, hBrush
);
336 if (btnPaintFunc
[btn_type
])
339 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
340 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
341 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
342 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
343 if( !wParam
) EndPaint( hWnd
, &ps
);
348 if (wParam
== VK_SPACE
)
350 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
351 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
356 case WM_LBUTTONDBLCLK
:
357 if(style
& BS_NOTIFY
||
358 btn_type
== BS_RADIOBUTTON
||
359 btn_type
== BS_USERBUTTON
||
360 btn_type
== BS_OWNERDRAW
)
362 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
369 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
370 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
374 if (wParam
!= VK_SPACE
)
378 state
= get_button_state( hWnd
);
379 if (!(state
& BUTTON_BTNPRESSED
)) break;
380 state
&= BUTTON_NSTATES
;
381 set_button_state( hWnd
, state
);
382 if (!(state
& BST_PUSHED
))
387 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
389 GetClientRect( hWnd
, &rect
);
390 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
392 state
= get_button_state( hWnd
);
395 case BS_AUTOCHECKBOX
:
396 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BST_CHECKED
), 0 );
398 case BS_AUTORADIOBUTTON
:
399 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
402 SendMessageW( hWnd
, BM_SETCHECK
,
403 (state
& BST_INDETERMINATE
) ? 0 : ((state
& 3) + 1), 0 );
406 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
410 case WM_CAPTURECHANGED
:
411 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
412 state
= get_button_state( hWnd
);
413 if (state
& BUTTON_BTNPRESSED
)
415 state
&= BUTTON_NSTATES
;
416 set_button_state( hWnd
, state
);
417 if (state
& BST_PUSHED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
422 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
424 GetClientRect( hWnd
, &rect
);
425 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
431 /* Clear an old text here as Windows does */
433 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
434 // Patch: http://source.winehq.org/patches/data/70889
435 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
437 if (style
& WS_VISIBLE
)
439 HDC hdc
= GetDC(hWnd
);
442 HWND parent
= GetParent(hWnd
);
443 UINT ctlMessage
=(btn_type
== BS_PUSHBUTTON
||
444 btn_type
== BS_DEFPUSHBUTTON
||
445 btn_type
== BS_PUSHLIKE
||
446 btn_type
== BS_USERBUTTON
||
447 btn_type
== BS_OWNERDRAW
) ?
448 WM_CTLCOLORBTN
: WM_CTLCOLORSTATIC
;
450 if (!parent
) parent
= hWnd
;
452 hbrush
= GetControlColor( parent
, hWnd
, hdc
, ctlMessage
);
454 GetClientRect(hWnd
, &client
);
456 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
457 /* Clip by client rect bounds */
458 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
459 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
460 FillRect(hdc
, &rc
, hbrush
);
461 ReleaseDC(hWnd
, hdc
);
464 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
465 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
466 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
467 InvalidateRect( hWnd
, NULL
, TRUE
);
469 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
470 return 1; /* success. FIXME: check text length */
474 set_button_font( hWnd
, (HFONT
)wParam
);
475 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
479 return (LRESULT
)get_button_font( hWnd
);
482 TRACE("WM_SETFOCUS %p\n",hWnd
);
483 set_button_state( hWnd
, get_button_state(hWnd
) | BST_FOCUS
);
484 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
485 if (style
& BS_NOTIFY
)
486 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
490 TRACE("WM_KILLFOCUS %p\n",hWnd
);
491 state
= get_button_state( hWnd
);
492 set_button_state( hWnd
, state
& ~BST_FOCUS
);
493 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
495 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
497 if (style
& BS_NOTIFY
)
498 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
500 InvalidateRect( hWnd
, NULL
, FALSE
);
503 case WM_SYSCOLORCHANGE
:
504 InvalidateRect( hWnd
, NULL
, FALSE
);
508 if ((wParam
& BS_TYPEMASK
) >= MAX_BTN_TYPE
) break;
509 btn_type
= wParam
& BS_TYPEMASK
;
510 style
= (style
& ~BS_TYPEMASK
) | btn_type
;
511 SetWindowLongPtrW( hWnd
, GWL_STYLE
, style
);
512 //WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
514 /* Only redraw if lParam flag is set.*/
516 InvalidateRect( hWnd
, NULL
, TRUE
);
522 state
= get_button_state( hWnd
);
523 if (state
& BUTTON_BMCLICK
)
525 set_button_state( hWnd
, state
| BUTTON_BMCLICK
); // Tracked in STATE_GWL_OFFSET.
527 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
528 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
530 state
= get_button_state( hWnd
);
531 if (!(state
& BUTTON_BMCLICK
)) break;
532 state
&= ~BUTTON_BMCLICK
;
533 set_button_state( hWnd
, state
);
538 /* Check that image format matches button style */
539 switch (style
& (BS_BITMAP
|BS_ICON
))
542 if (wParam
!= IMAGE_BITMAP
) return 0;
545 if (wParam
!= IMAGE_ICON
) return 0;
550 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
551 InvalidateRect( hWnd
, NULL
, FALSE
);
552 return (LRESULT
)oldHbitmap
;
555 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
558 return get_button_state( hWnd
) & 3;
561 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
562 state
= get_button_state( hWnd
);
563 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
566 if (wParam
) style
|= WS_TABSTOP
;
567 else style
&= ~WS_TABSTOP
;
568 SetWindowLongPtrW( hWnd
, GWL_STYLE
, style
);
570 if (wParam
) WIN_SetStyle( hWnd
, WS_TABSTOP
, 0 );
571 else WIN_SetStyle( hWnd
, 0, WS_TABSTOP
);
574 if ((state
& 3) != wParam
)
576 set_button_state( hWnd
, (state
& ~3) | wParam
);
577 paint_button( hWnd
, btn_type
, ODA_SELECT
);
579 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BST_CHECKED
) && (style
& WS_CHILD
))
580 BUTTON_CheckAutoRadioButton( hWnd
);
584 return get_button_state( hWnd
);
587 state
= get_button_state( hWnd
);
589 set_button_state( hWnd
, state
| BST_PUSHED
);
591 set_button_state( hWnd
, state
& ~BST_PUSHED
);
593 paint_button( hWnd
, btn_type
, ODA_SELECT
);
596 case WM_UPDATEUISTATE
:
598 DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
600 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
602 if (button_update_uistate(hWnd
, unicode
))
603 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
607 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
610 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
611 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
616 /***********************************************************************
618 * The button window procedure. This is just a wrapper which locks
619 * the passed HWND and calls the real window procedure (with a WND*
620 * pointer pointing to the locked windowstructure).
622 LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
624 if (!IsWindow( hWnd
)) return 0;
625 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, TRUE
);
629 /***********************************************************************
632 LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
634 if (!IsWindow( hWnd
)) return 0;
635 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, FALSE
);
639 /**********************************************************************
640 * Convert button styles to flags used by DrawText.
642 static UINT
BUTTON_BStoDT( DWORD style
, DWORD ex_style
)
644 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
646 /* "Convert" pushlike buttons to pushbuttons */
647 if (style
& BS_PUSHLIKE
)
648 style
&= ~BS_TYPEMASK
;
650 if (!(style
& BS_MULTILINE
))
651 dtStyle
|= DT_SINGLELINE
;
653 dtStyle
|= DT_WORDBREAK
;
655 switch (style
& BS_CENTER
)
657 case BS_LEFT
: /* DT_LEFT is 0 */ break;
658 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
659 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
661 /* Pushbutton's text is centered by default */
662 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
663 /* all other flavours have left aligned text */
666 if (ex_style
& WS_EX_RIGHT
) dtStyle
= DT_RIGHT
| (dtStyle
& ~(DT_LEFT
| DT_CENTER
));
668 /* DrawText ignores vertical alignment for multiline text,
669 * but we use these flags to align label manually.
671 if (get_button_type(style
) != BS_GROUPBOX
)
673 switch (style
& BS_VCENTER
)
675 case BS_TOP
: /* DT_TOP is 0 */ break;
676 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
677 case BS_VCENTER
: /* fall through */
678 default: dtStyle
|= DT_VCENTER
; break;
682 /* GroupBox's text is always single line and is top aligned. */
683 dtStyle
|= DT_SINGLELINE
;
688 /**********************************************************************
689 * BUTTON_CalcLabelRect
691 * Calculates label's rectangle depending on button style.
693 * Returns flags to be passed to DrawText.
694 * Calculated rectangle doesn't take into account button state
695 * (pushed, etc.). If there is nothing to draw (no text/image) output
696 * rectangle is empty, and return value is (UINT)-1.
698 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
700 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
701 LONG ex_style
= GetWindowLongPtrW( hwnd
, GWL_EXSTYLE
);
705 UINT dtStyle
= BUTTON_BStoDT( style
, ex_style
);
709 /* Calculate label rectangle according to label type */
710 switch (style
& (BS_ICON
|BS_BITMAP
))
713 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
716 HeapFree( GetProcessHeap(), 0, text
);
719 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
720 HeapFree( GetProcessHeap(), 0, text
);
722 if (get_ui_state( hwnd
) & UISF_HIDEACCEL
)
723 dtStyle
|= DT_HIDEPREFIX
;
727 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
730 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
732 r
.right
= r
.left
+ bm
.bmWidth
;
733 r
.bottom
= r
.top
+ bm
.bmHeight
;
735 DeleteObject(iconInfo
.hbmColor
);
736 DeleteObject(iconInfo
.hbmMask
);
740 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
743 r
.right
= r
.left
+ bm
.bmWidth
;
744 r
.bottom
= r
.top
+ bm
.bmHeight
;
754 /* Position label inside bounding rectangle according to
755 * alignment flags. (calculated rect is always left-top aligned).
756 * If label is aligned to any side - shift label in opposite
757 * direction to leave extra space for focus rectangle.
759 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
761 case DT_LEFT
: r
.left
++; r
.right
++; break;
762 case DT_CENTER
: n
= r
.right
- r
.left
;
763 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
764 r
.right
= r
.left
+ n
; break;
765 case DT_RIGHT
: n
= r
.right
- r
.left
;
766 r
.right
= rc
->right
- 1;
767 r
.left
= r
.right
- n
;
771 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
773 case DT_TOP
: r
.top
++; r
.bottom
++; break;
774 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
775 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
776 r
.bottom
= r
.top
+ n
; break;
777 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
778 r
.bottom
= rc
->bottom
- 1;
779 r
.top
= r
.bottom
- n
;
788 /**********************************************************************
789 * BUTTON_DrawTextCallback
791 * Callback function used by DrawStateW function.
793 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
801 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
806 /**********************************************************************
809 * Common function for drawing button label.
811 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
813 DRAWSTATEPROC lpOutputProc
= NULL
;
817 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
818 LONG state
= get_button_state( hwnd
);
819 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
822 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
823 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
824 * I don't have Win31 on hand to verify that, so I leave it as is.
827 if ((style
& BS_PUSHLIKE
) && (state
& BST_INDETERMINATE
))
829 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
833 switch (style
& (BS_ICON
|BS_BITMAP
))
836 /* DST_COMPLEX -- is 0 */
837 lpOutputProc
= BUTTON_DrawTextCallback
;
838 if (!(text
= get_button_text( hwnd
))) return;
840 wp
= (WPARAM
)dtFlags
;
842 if (dtFlags
& DT_HIDEPREFIX
)
843 flags
|= DSS_HIDEPREFIX
;
848 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
853 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
860 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
861 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
862 HeapFree( GetProcessHeap(), 0, text
);
865 /**********************************************************************
866 * Push Button Functions
868 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
871 UINT dtFlags
, uState
;
875 COLORREF oldTxtColor
;
877 LONG state
= get_button_state( hwnd
);
878 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
879 BOOL pushedState
= (state
& BST_PUSHED
);
883 GetClientRect( hwnd
, &rc
);
885 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
886 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
887 parent
= GetParent(hwnd
);
888 if (!parent
) parent
= hwnd
;
889 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
891 hrgn
= set_control_clipping( hDC
, &rc
);
893 hOldPen
= SelectObject(hDC
, GetStockObject(DC_PEN
));
894 SetDCPenColor(hDC
, GetSysColor(COLOR_WINDOWFRAME
));
896 hOldPen
= SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
898 hOldBrush
= SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
899 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
901 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
903 if (action
!= ODA_FOCUS
)
904 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
905 InflateRect( &rc
, -1, -1 );
908 /* completely skip the drawing if only focus has changed */
909 if (action
== ODA_FOCUS
) goto draw_focus
;
911 uState
= DFCS_BUTTONPUSH
;
915 else if (pushedState
)
917 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
920 uState
|= DFCS_PUSHED
;
923 if (state
& (BST_CHECKED
| BST_INDETERMINATE
))
924 uState
|= DFCS_CHECKED
;
926 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
928 /* draw button label */
930 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
932 if (dtFlags
== (UINT
)-1L)
936 OffsetRect(&r
, 1, 1);
938 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
940 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
942 SetTextColor( hDC
, oldTxtColor
);
945 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
947 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
949 InflateRect( &rc
, -2, -2 );
950 DrawFocusRect( hDC
, &rc
);
955 SelectObject( hDC
, hOldPen
);
956 SelectObject( hDC
, hOldBrush
);
957 SetBkMode(hDC
, oldBkMode
);
958 SelectClipRgn( hDC
, hrgn
);
959 if (hrgn
) DeleteObject( hrgn
);
962 /**********************************************************************
963 * Check Box & Radio Button Functions
966 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
968 RECT rbox
, rtext
, client
;
973 LONG state
= get_button_state( hwnd
);
974 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
978 if (style
& BS_PUSHLIKE
)
980 PB_Paint( hwnd
, hDC
, action
);
984 GetClientRect(hwnd
, &client
);
985 rbox
= rtext
= client
;
987 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
989 parent
= GetParent(hwnd
);
990 if (!parent
) parent
= hwnd
;
991 hBrush
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
992 hrgn
= set_control_clipping( hDC
, &client
);
994 if (style
& BS_LEFTTEXT
)
996 /* magic +4 is what CTL3D expects */
998 rtext
.right
-= checkBoxWidth
+ 4;
999 rbox
.left
= rbox
.right
- checkBoxWidth
;
1003 rtext
.left
+= checkBoxWidth
+ 4;
1004 rbox
.right
= checkBoxWidth
;
1007 /* Since WM_ERASEBKGND does nothing, first prepare background */
1008 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
1009 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
1013 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
1015 /* Only adjust rbox when rtext is valid */
1016 if (dtFlags
!= (UINT
)-1L)
1018 rbox
.top
= rtext
.top
;
1019 rbox
.bottom
= rtext
.bottom
;
1022 /* Draw the check-box bitmap */
1023 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
1027 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
1028 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
1029 else if (state
& BST_INDETERMINATE
) flags
= DFCS_BUTTON3STATE
;
1030 else flags
= DFCS_BUTTONCHECK
;
1032 if (state
& (BST_CHECKED
| BST_INDETERMINATE
)) flags
|= DFCS_CHECKED
;
1033 if (state
& BST_PUSHED
) flags
|= DFCS_PUSHED
;
1035 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
1037 /* rbox must have the correct height */
1038 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
1040 if (style
& BS_TOP
) {
1042 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1044 rbox
.top
-= -delta
/2 + 1;
1045 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1047 } else if (style
& BS_BOTTOM
) {
1049 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1051 rbox
.bottom
+= -delta
/2 + 1;
1052 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1054 } else { /* Default */
1056 int ofs
= (delta
/ 2);
1057 rbox
.bottom
-= ofs
+ 1;
1058 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1059 } else if (delta
< 0) {
1060 int ofs
= (-delta
/ 2);
1061 rbox
.top
-= ofs
+ 1;
1062 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1066 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
1069 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
1072 if (action
== ODA_DRAWENTIRE
)
1073 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
1076 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1078 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1082 IntersectRect(&rtext
, &rtext
, &client
);
1083 DrawFocusRect( hDC
, &rtext
);
1086 SelectClipRgn( hDC
, hrgn
);
1087 if (hrgn
) DeleteObject( hrgn
);
1091 /**********************************************************************
1092 * BUTTON_CheckAutoRadioButton
1094 * hwnd is checked, uncheck every other auto radio button in group
1096 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1098 HWND parent
, sibling
, start
;
1100 parent
= GetParent(hwnd
);
1101 /* make sure that starting control is not disabled or invisible */
1102 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1105 if (!sibling
) break;
1106 if ((hwnd
!= sibling
) &&
1107 ((GetWindowLongPtrW( sibling
, GWL_STYLE
) & BS_TYPEMASK
) == BS_AUTORADIOBUTTON
))
1108 SendMessageW( sibling
, BM_SETCHECK
, BST_UNCHECKED
, 0 );
1109 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1110 } while (sibling
!= start
);
1114 /**********************************************************************
1115 * Group Box Functions
1118 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1125 LONG style
= GetWindowLongPtrW( hwnd
, GWL_STYLE
);
1129 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1130 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1131 parent
= GetParent(hwnd
);
1132 if (!parent
) parent
= hwnd
;
1133 hbr
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORSTATIC
);
1135 GetClientRect( hwnd
, &rc
);
1137 hrgn
= set_control_clipping( hDC
, &rc
);
1139 GetTextMetricsW (hDC
, &tm
);
1140 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1141 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1143 InflateRect(&rc
, -7, 1);
1144 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1146 if (dtFlags
!= (UINT
)-1L)
1148 /* Because buttons have CS_PARENTDC class style, there is a chance
1149 * that label will be drawn out of client rect.
1150 * But Windows doesn't clip label's rect, so do I.
1153 /* There is 1-pixel margin at the left, right, and bottom */
1154 rc
.left
--; rc
.right
++; rc
.bottom
++;
1155 FillRect(hDC
, &rc
, hbr
);
1156 rc
.left
++; rc
.right
--; rc
.bottom
--;
1158 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1160 SelectClipRgn( hDC
, hrgn
);
1161 if (hrgn
) DeleteObject( hrgn
);
1165 /**********************************************************************
1166 * User Button Functions
1169 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1174 LONG state
= get_button_state( hwnd
);
1177 GetClientRect( hwnd
, &rc
);
1179 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1181 parent
= GetParent(hwnd
);
1182 if (!parent
) parent
= hwnd
;
1183 hBrush
= GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1185 FillRect( hDC
, &rc
, hBrush
);
1186 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1188 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1189 DrawFocusRect( hDC
, &rc
);
1195 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_FOCUS
) ? BN_SETFOCUS
: BN_KILLFOCUS
);
1199 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_PUSHED
) ? BN_HILITE
: BN_UNHILITE
);
1203 BUTTON_NOTIFY_PARENT( hwnd
, BN_PAINT
);
1209 /**********************************************************************
1210 * Ownerdrawn Button Functions
1213 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1215 LONG state
= get_button_state( hwnd
);
1217 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1219 HFONT hFont
, hPrevFont
= 0;
1222 dis
.CtlType
= ODT_BUTTON
;
1225 dis
.itemAction
= action
;
1226 dis
.itemState
= ((state
& BST_FOCUS
) ? ODS_FOCUS
: 0) |
1227 ((state
& BST_PUSHED
) ? ODS_SELECTED
: 0) |
1228 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1229 dis
.hwndItem
= hwnd
;
1232 GetClientRect( hwnd
, &dis
.rcItem
);
1234 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1235 parent
= GetParent(hwnd
);
1236 if (!parent
) parent
= hwnd
;
1237 GetControlColor( parent
, hwnd
, hDC
, WM_CTLCOLORBTN
);
1239 hrgn
= set_control_clipping( hDC
, &dis
.rcItem
);
1241 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1242 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1243 SelectClipRgn( hDC
, hrgn
);
1244 if (hrgn
) DeleteObject( hrgn
);