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 * - BS_NOTIFY: is it complete?
24 * - BS_RIGHTBUTTON: same as BS_LEFTTEXT
27 * - WM_CHAR: Checks a (manual or automatic) check box on '+' or '=', clears it on '-' key.
28 * - WM_SETFOCUS: For (manual or automatic) radio buttons, send the parent window BN_CLICKED
29 * - WM_NCCREATE: Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
40 * - BN_PUSHED/BN_HILITE
41 * + BN_KILLFOCUS: is it OK?
43 * + BN_SETFOCUS: is it OK?
44 * - BN_UNPUSHED/BN_UNHILITE
47 * Structures/Macros/Definitions
50 * - Button_GetIdealSize
51 * - Button_GetImageList
52 * - Button_GetTextMargin
53 * - Button_SetImageList
54 * - Button_SetTextMargin
68 #include "user_private.h"
69 #include "wine/debug.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(button
);
73 /* GetWindowLong offsets for window extra information */
74 #define STATE_GWL_OFFSET 0
75 #define HFONT_GWL_OFFSET (sizeof(LONG))
76 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
77 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
79 /* undocumented flags */
80 #define BUTTON_NSTATES 0x0F
81 #define BUTTON_BTNPRESSED 0x40
82 #define BUTTON_UNKNOWN2 0x20
83 #define BUTTON_UNKNOWN3 0x10
85 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
86 do { /* Notify parent which has created this button control */ \
87 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
88 SendMessageW(GetParent(hWnd), WM_COMMAND, \
89 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
93 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
94 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
95 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
96 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
97 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
98 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
99 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
101 #define MAX_BTN_TYPE 16
103 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
105 BST_UNCHECKED
, /* BS_PUSHBUTTON */
106 BST_UNCHECKED
, /* BS_DEFPUSHBUTTON */
107 BST_CHECKED
, /* BS_CHECKBOX */
108 BST_CHECKED
, /* BS_AUTOCHECKBOX */
109 BST_CHECKED
, /* BS_RADIOBUTTON */
110 BST_INDETERMINATE
, /* BS_3STATE */
111 BST_INDETERMINATE
, /* BS_AUTO3STATE */
112 BST_UNCHECKED
, /* BS_GROUPBOX */
113 BST_UNCHECKED
, /* BS_USERBUTTON */
114 BST_CHECKED
, /* BS_AUTORADIOBUTTON */
115 BST_UNCHECKED
, /* BS_PUSHBOX */
116 BST_UNCHECKED
/* BS_OWNERDRAW */
119 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
121 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
123 PB_Paint
, /* BS_PUSHBUTTON */
124 PB_Paint
, /* BS_DEFPUSHBUTTON */
125 CB_Paint
, /* BS_CHECKBOX */
126 CB_Paint
, /* BS_AUTOCHECKBOX */
127 CB_Paint
, /* BS_RADIOBUTTON */
128 CB_Paint
, /* BS_3STATE */
129 CB_Paint
, /* BS_AUTO3STATE */
130 GB_Paint
, /* BS_GROUPBOX */
131 UB_Paint
, /* BS_USERBUTTON */
132 CB_Paint
, /* BS_AUTORADIOBUTTON */
133 NULL
, /* BS_PUSHBOX */
134 OB_Paint
/* BS_OWNERDRAW */
137 /*********************************************************************
138 * button class descriptor
140 static const WCHAR buttonW
[] = {'B','u','t','t','o','n',0};
141 const struct builtin_class_descr BUTTON_builtin_class
=
144 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
145 WINPROC_BUTTON
, /* proc */
146 NB_EXTRA_BYTES
, /* extra */
147 IDC_ARROW
, /* cursor */
152 static inline LONG
get_button_state( HWND hwnd
)
154 return GetWindowLongW( hwnd
, STATE_GWL_OFFSET
);
157 static inline void set_button_state( HWND hwnd
, LONG state
)
159 SetWindowLongW( hwnd
, STATE_GWL_OFFSET
, state
);
162 static inline HFONT
get_button_font( HWND hwnd
)
164 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
167 static inline void set_button_font( HWND hwnd
, HFONT font
)
169 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
172 static inline UINT
get_button_type( LONG window_style
)
174 return (window_style
& BS_TYPEMASK
);
177 /* paint a button of any type */
178 static inline void paint_button( HWND hwnd
, LONG style
, UINT action
)
180 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
182 HDC hdc
= GetDC( hwnd
);
183 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
184 ReleaseDC( hwnd
, hdc
);
188 /* retrieve the button text; returned buffer must be freed by caller */
189 static inline WCHAR
*get_button_text( HWND hwnd
)
191 static const INT len
= 512;
192 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
193 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
197 /***********************************************************************
198 * ButtonWndProc_common
200 LRESULT
ButtonWndProc_common(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
204 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
205 UINT btn_type
= get_button_type( style
);
209 if (!IsWindow( hWnd
)) return 0;
211 pt
.x
= (short)LOWORD(lParam
);
212 pt
.y
= (short)HIWORD(lParam
);
220 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
221 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
223 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
224 case BS_GROUPBOX
: return DLGC_STATIC
;
225 default: return DLGC_BUTTON
;
229 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
233 if (btn_type
>= MAX_BTN_TYPE
)
234 return -1; /* abort */
236 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
237 if (btn_type
== BS_USERBUTTON
)
239 style
= (style
& ~BS_TYPEMASK
) | BS_PUSHBUTTON
;
240 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
242 set_button_state( hWnd
, BST_UNCHECKED
);
246 if (btn_type
== BS_OWNERDRAW
)
248 HDC hdc
= (HDC
)wParam
;
251 HWND parent
= GetParent(hWnd
);
252 if (!parent
) parent
= hWnd
;
253 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
254 if (!hBrush
) /* did the app forget to call defwindowproc ? */
255 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
256 (WPARAM
)hdc
, (LPARAM
)hWnd
);
257 GetClientRect(hWnd
, &rc
);
258 FillRect(hdc
, &rc
, hBrush
);
266 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
267 if (btnPaintFunc
[btn_type
])
269 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
270 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
271 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
273 if ( !wParam
) EndPaint( hWnd
, &ps
);
278 if (wParam
== VK_SPACE
)
280 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
281 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
286 case WM_LBUTTONDBLCLK
:
287 if(style
& BS_NOTIFY
||
288 btn_type
== BS_RADIOBUTTON
||
289 btn_type
== BS_USERBUTTON
||
290 btn_type
== BS_OWNERDRAW
)
292 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
299 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
300 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
304 if (wParam
!= VK_SPACE
)
308 state
= get_button_state( hWnd
);
309 if (!(state
& BUTTON_BTNPRESSED
)) break;
310 state
&= BUTTON_NSTATES
;
311 set_button_state( hWnd
, state
);
312 if (!(state
& BST_PUSHED
))
317 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
318 GetClientRect( hWnd
, &rect
);
319 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
321 state
= get_button_state( hWnd
);
324 case BS_AUTOCHECKBOX
:
325 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BST_CHECKED
), 0 );
327 case BS_AUTORADIOBUTTON
:
328 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
331 SendMessageW( hWnd
, BM_SETCHECK
,
332 (state
& BST_INDETERMINATE
) ? 0 : ((state
& 3) + 1), 0 );
336 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
344 case WM_CAPTURECHANGED
:
345 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
346 if (hWnd
== (HWND
)lParam
) break;
347 state
= get_button_state( hWnd
);
348 if (state
& BUTTON_BTNPRESSED
)
350 state
&= BUTTON_NSTATES
;
351 set_button_state( hWnd
, state
);
352 if (state
& BST_PUSHED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
357 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
359 GetClientRect( hWnd
, &rect
);
360 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
366 /* Clear an old text here as Windows does */
367 if (IsWindowVisible(hWnd
))
369 HDC hdc
= GetDC(hWnd
);
372 HWND parent
= GetParent(hWnd
);
373 UINT message
= (btn_type
== BS_PUSHBUTTON
||
374 btn_type
== BS_DEFPUSHBUTTON
||
375 btn_type
== BS_USERBUTTON
||
376 btn_type
== BS_OWNERDRAW
) ?
377 WM_CTLCOLORBTN
: WM_CTLCOLORSTATIC
;
379 if (!parent
) parent
= hWnd
;
380 hbrush
= (HBRUSH
)SendMessageW(parent
, message
,
381 (WPARAM
)hdc
, (LPARAM
)hWnd
);
382 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
383 hbrush
= (HBRUSH
)DefWindowProcW(parent
, message
,
384 (WPARAM
)hdc
, (LPARAM
)hWnd
);
386 GetClientRect(hWnd
, &client
);
388 /* FIXME: check other BS_* handlers */
389 if (btn_type
== BS_GROUPBOX
)
390 InflateRect(&rc
, -7, 1); /* GB_Paint does this */
391 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
392 /* Clip by client rect bounds */
393 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
394 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
395 FillRect(hdc
, &rc
, hbrush
);
396 ReleaseDC(hWnd
, hdc
);
399 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
400 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
401 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
402 InvalidateRect( hWnd
, NULL
, TRUE
);
404 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
405 return 1; /* success. FIXME: check text length */
409 set_button_font( hWnd
, (HFONT
)wParam
);
410 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
414 return (LRESULT
)get_button_font( hWnd
);
417 TRACE("WM_SETFOCUS %p\n",hWnd
);
418 set_button_state( hWnd
, get_button_state(hWnd
) | BST_FOCUS
);
419 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
420 if (style
& BS_NOTIFY
)
421 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
425 TRACE("WM_KILLFOCUS %p\n",hWnd
);
426 state
= get_button_state( hWnd
);
427 set_button_state( hWnd
, state
& ~BST_FOCUS
);
428 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
430 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
432 if (style
& BS_NOTIFY
)
433 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
435 InvalidateRect( hWnd
, NULL
, FALSE
);
438 case WM_SYSCOLORCHANGE
:
439 InvalidateRect( hWnd
, NULL
, FALSE
);
443 btn_type
= wParam
& BS_TYPEMASK
;
444 style
= (style
& ~BS_TYPEMASK
) | btn_type
;
445 WIN_SetStyle( hWnd
, style
, BS_TYPEMASK
& ~style
);
447 /* Only redraw if lParam flag is set.*/
449 InvalidateRect( hWnd
, NULL
, TRUE
);
454 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
455 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
459 /* Check that image format matches button style */
460 switch (style
& (BS_BITMAP
|BS_ICON
))
463 if (wParam
!= IMAGE_BITMAP
) return 0;
466 if (wParam
!= IMAGE_ICON
) return 0;
471 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
472 InvalidateRect( hWnd
, NULL
, FALSE
);
473 return (LRESULT
)oldHbitmap
;
476 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
479 return get_button_state( hWnd
) & 3;
482 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
483 state
= get_button_state( hWnd
);
484 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
486 if (wParam
) WIN_SetStyle( hWnd
, WS_TABSTOP
, 0 );
487 else WIN_SetStyle( hWnd
, 0, WS_TABSTOP
);
489 if ((state
& 3) != wParam
)
491 set_button_state( hWnd
, (state
& ~3) | wParam
);
492 paint_button( hWnd
, btn_type
, ODA_SELECT
);
494 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BST_CHECKED
) && (style
& WS_CHILD
))
495 BUTTON_CheckAutoRadioButton( hWnd
);
499 return get_button_state( hWnd
);
502 state
= get_button_state( hWnd
);
504 set_button_state( hWnd
, state
| BST_PUSHED
);
506 set_button_state( hWnd
, state
& ~BST_PUSHED
);
508 paint_button( hWnd
, btn_type
, ODA_SELECT
);
512 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
515 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
516 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
521 /**********************************************************************
522 * Convert button styles to flags used by DrawText.
524 static UINT
BUTTON_BStoDT( DWORD style
, DWORD ex_style
)
526 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
528 /* "Convert" pushlike buttons to pushbuttons */
529 if (style
& BS_PUSHLIKE
)
530 style
&= ~BS_TYPEMASK
;
532 if (!(style
& BS_MULTILINE
))
533 dtStyle
|= DT_SINGLELINE
;
535 dtStyle
|= DT_WORDBREAK
;
537 switch (style
& BS_CENTER
)
539 case BS_LEFT
: /* DT_LEFT is 0 */ break;
540 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
541 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
543 /* Pushbutton's text is centered by default */
544 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
545 /* all other flavours have left aligned text */
548 if (ex_style
& WS_EX_RIGHT
) dtStyle
= DT_RIGHT
| (dtStyle
& ~(DT_LEFT
| DT_CENTER
));
550 /* DrawText ignores vertical alignment for multiline text,
551 * but we use these flags to align label manually.
553 if (get_button_type(style
) != BS_GROUPBOX
)
555 switch (style
& BS_VCENTER
)
557 case BS_TOP
: /* DT_TOP is 0 */ break;
558 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
559 case BS_VCENTER
: /* fall through */
560 default: dtStyle
|= DT_VCENTER
; break;
564 /* GroupBox's text is always single line and is top aligned. */
565 dtStyle
|= DT_SINGLELINE
;
570 /**********************************************************************
571 * BUTTON_CalcLabelRect
573 * Calculates label's rectangle depending on button style.
575 * Returns flags to be passed to DrawText.
576 * Calculated rectangle doesn't take into account button state
577 * (pushed, etc.). If there is nothing to draw (no text/image) output
578 * rectangle is empty, and return value is (UINT)-1.
580 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
582 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
583 LONG ex_style
= GetWindowLongW( hwnd
, GWL_EXSTYLE
);
587 UINT dtStyle
= BUTTON_BStoDT( style
, ex_style
);
591 /* Calculate label rectangle according to label type */
592 switch (style
& (BS_ICON
|BS_BITMAP
))
596 HFONT hFont
, hPrevFont
= 0;
598 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
601 HeapFree( GetProcessHeap(), 0, text
);
605 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hdc
, hFont
);
606 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
607 if (hPrevFont
) SelectObject( hdc
, hPrevFont
);
608 HeapFree( GetProcessHeap(), 0, text
);
613 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
616 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
618 r
.right
= r
.left
+ bm
.bmWidth
;
619 r
.bottom
= r
.top
+ bm
.bmHeight
;
621 DeleteObject(iconInfo
.hbmColor
);
622 DeleteObject(iconInfo
.hbmMask
);
626 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
629 r
.right
= r
.left
+ bm
.bmWidth
;
630 r
.bottom
= r
.top
+ bm
.bmHeight
;
640 /* Position label inside bounding rectangle according to
641 * alignment flags. (calculated rect is always left-top aligned).
642 * If label is aligned to any side - shift label in opposite
643 * direction to leave extra space for focus rectangle.
645 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
647 case DT_LEFT
: r
.left
++; r
.right
++; break;
648 case DT_CENTER
: n
= r
.right
- r
.left
;
649 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
650 r
.right
= r
.left
+ n
; break;
651 case DT_RIGHT
: n
= r
.right
- r
.left
;
652 r
.right
= rc
->right
- 1;
653 r
.left
= r
.right
- n
;
657 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
659 case DT_TOP
: r
.top
++; r
.bottom
++; break;
660 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
661 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
662 r
.bottom
= r
.top
+ n
; break;
663 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
664 r
.bottom
= rc
->bottom
- 1;
665 r
.top
= r
.bottom
- n
;
674 /**********************************************************************
675 * BUTTON_DrawTextCallback
677 * Callback function used by DrawStateW function.
679 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
683 SetRect(&rc
, 0, 0, cx
, cy
);
684 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
689 /**********************************************************************
692 * Common function for drawing button label.
694 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
696 DRAWSTATEPROC lpOutputProc
= NULL
;
700 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
701 LONG state
= get_button_state( hwnd
);
702 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
705 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
706 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
707 * I don't have Win31 on hand to verify that, so I leave it as is.
710 if ((style
& BS_PUSHLIKE
) && (state
& BST_INDETERMINATE
))
712 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
716 switch (style
& (BS_ICON
|BS_BITMAP
))
719 /* DST_COMPLEX -- is 0 */
720 lpOutputProc
= BUTTON_DrawTextCallback
;
721 if (!(text
= get_button_text( hwnd
))) return;
728 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
733 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
740 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
741 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
742 HeapFree( GetProcessHeap(), 0, text
);
745 /**********************************************************************
746 * Push Button Functions
748 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
751 UINT dtFlags
, uState
;
755 COLORREF oldTxtColor
;
757 LONG state
= get_button_state( hwnd
);
758 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
759 BOOL pushedState
= (state
& BST_PUSHED
);
763 GetClientRect( hwnd
, &rc
);
765 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
766 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
767 parent
= GetParent(hwnd
);
768 if (!parent
) parent
= hwnd
;
769 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
771 hrgn
= set_control_clipping( hDC
, &rc
);
773 hOldPen
= SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
774 hOldBrush
= SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
775 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
777 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
779 if (action
!= ODA_FOCUS
)
780 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
781 InflateRect( &rc
, -1, -1 );
784 /* completely skip the drawing if only focus has changed */
785 if (action
== ODA_FOCUS
) goto draw_focus
;
787 uState
= DFCS_BUTTONPUSH
;
791 else if (pushedState
)
793 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
796 uState
|= DFCS_PUSHED
;
799 if (state
& (BST_CHECKED
| BST_INDETERMINATE
))
800 uState
|= DFCS_CHECKED
;
802 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
804 /* draw button label */
806 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
808 if (dtFlags
== (UINT
)-1L)
812 OffsetRect(&r
, 1, 1);
814 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
816 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
818 SetTextColor( hDC
, oldTxtColor
);
821 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
823 InflateRect( &rc
, -2, -2 );
824 DrawFocusRect( hDC
, &rc
);
828 SelectObject( hDC
, hOldPen
);
829 SelectObject( hDC
, hOldBrush
);
830 SetBkMode(hDC
, oldBkMode
);
831 SelectClipRgn( hDC
, hrgn
);
832 if (hrgn
) DeleteObject( hrgn
);
835 /**********************************************************************
836 * Check Box & Radio Button Functions
839 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
841 RECT rbox
, rtext
, client
;
843 int delta
, text_offset
, checkBoxWidth
, checkBoxHeight
;
846 LONG state
= get_button_state( hwnd
);
847 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
848 LONG ex_style
= GetWindowLongW( hwnd
, GWL_EXSTYLE
);
852 if (style
& BS_PUSHLIKE
)
854 PB_Paint( hwnd
, hDC
, action
);
858 GetClientRect(hwnd
, &client
);
859 rbox
= rtext
= client
;
861 checkBoxWidth
= 12 * GetDeviceCaps( hDC
, LOGPIXELSX
) / 96 + 1;
862 checkBoxHeight
= 12 * GetDeviceCaps( hDC
, LOGPIXELSY
) / 96 + 1;
864 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
865 GetCharWidthW( hDC
, '0', '0', &text_offset
);
868 parent
= GetParent(hwnd
);
869 if (!parent
) parent
= hwnd
;
870 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
871 (WPARAM
)hDC
, (LPARAM
)hwnd
);
872 if (!hBrush
) /* did the app forget to call defwindowproc ? */
873 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
874 (WPARAM
)hDC
, (LPARAM
)hwnd
);
875 hrgn
= set_control_clipping( hDC
, &client
);
877 if (style
& BS_LEFTTEXT
|| ex_style
& WS_EX_RIGHT
)
879 rtext
.right
-= checkBoxWidth
+ text_offset
;
880 rbox
.left
= rbox
.right
- checkBoxWidth
;
884 rtext
.left
+= checkBoxWidth
+ text_offset
;
885 rbox
.right
= checkBoxWidth
;
888 /* Since WM_ERASEBKGND does nothing, first prepare background */
889 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
890 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
894 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
896 /* Only adjust rbox when rtext is valid */
897 if (dtFlags
!= (UINT
)-1L)
899 rbox
.top
= rtext
.top
;
900 rbox
.bottom
= rtext
.bottom
;
903 /* Draw the check-box bitmap */
904 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
908 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
909 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
910 else if (state
& BST_INDETERMINATE
) flags
= DFCS_BUTTON3STATE
;
911 else flags
= DFCS_BUTTONCHECK
;
913 if (state
& (BST_CHECKED
| BST_INDETERMINATE
)) flags
|= DFCS_CHECKED
;
914 if (state
& BST_PUSHED
) flags
|= DFCS_PUSHED
;
916 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
918 /* rbox must have the correct height */
919 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
921 if (style
& BS_TOP
) {
923 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
925 rbox
.top
-= -delta
/2 + 1;
926 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
928 } else if (style
& BS_BOTTOM
) {
930 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
932 rbox
.bottom
+= -delta
/2 + 1;
933 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
935 } else { /* Default */
937 int ofs
= (delta
/ 2);
938 rbox
.bottom
-= ofs
+ 1;
939 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
940 } else if (delta
< 0) {
941 int ofs
= (-delta
/ 2);
943 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
947 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
950 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
953 if (action
== ODA_DRAWENTIRE
)
954 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
957 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
961 IntersectRect(&rtext
, &rtext
, &client
);
962 DrawFocusRect( hDC
, &rtext
);
964 SelectClipRgn( hDC
, hrgn
);
965 if (hrgn
) DeleteObject( hrgn
);
969 /**********************************************************************
970 * BUTTON_CheckAutoRadioButton
972 * hwnd is checked, uncheck every other auto radio button in group
974 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
976 HWND parent
, sibling
, start
;
978 parent
= GetParent(hwnd
);
979 /* make sure that starting control is not disabled or invisible */
980 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
984 if ((hwnd
!= sibling
) &&
985 ((GetWindowLongW( sibling
, GWL_STYLE
) & BS_TYPEMASK
) == BS_AUTORADIOBUTTON
))
986 SendMessageW( sibling
, BM_SETCHECK
, BST_UNCHECKED
, 0 );
987 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
988 } while (sibling
!= start
);
992 /**********************************************************************
993 * Group Box Functions
996 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1003 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1007 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1008 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1009 parent
= GetParent(hwnd
);
1010 if (!parent
) parent
= hwnd
;
1011 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1012 if (!hbr
) /* did the app forget to call defwindowproc ? */
1013 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1014 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1015 GetClientRect( hwnd
, &rc
);
1017 hrgn
= set_control_clipping( hDC
, &rc
);
1019 GetTextMetricsW (hDC
, &tm
);
1020 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1021 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1023 InflateRect(&rc
, -7, 1);
1024 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1026 if (dtFlags
!= (UINT
)-1)
1028 /* Because buttons have CS_PARENTDC class style, there is a chance
1029 * that label will be drawn out of client rect.
1030 * But Windows doesn't clip label's rect, so do I.
1033 /* There is 1-pixel margin at the left, right, and bottom */
1034 rc
.left
--; rc
.right
++; rc
.bottom
++;
1035 FillRect(hDC
, &rc
, hbr
);
1036 rc
.left
++; rc
.right
--; rc
.bottom
--;
1038 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1040 SelectClipRgn( hDC
, hrgn
);
1041 if (hrgn
) DeleteObject( hrgn
);
1045 /**********************************************************************
1046 * User Button Functions
1049 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1054 LONG state
= get_button_state( hwnd
);
1057 GetClientRect( hwnd
, &rc
);
1059 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1061 parent
= GetParent(hwnd
);
1062 if (!parent
) parent
= hwnd
;
1063 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1064 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1065 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1066 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1068 FillRect( hDC
, &rc
, hBrush
);
1069 if (action
== ODA_FOCUS
|| (state
& BST_FOCUS
))
1070 DrawFocusRect( hDC
, &rc
);
1075 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_FOCUS
) ? BN_SETFOCUS
: BN_KILLFOCUS
);
1079 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BST_PUSHED
) ? BN_HILITE
: BN_UNHILITE
);
1083 BUTTON_NOTIFY_PARENT( hwnd
, BN_PAINT
);
1089 /**********************************************************************
1090 * Ownerdrawn Button Functions
1093 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1095 LONG state
= get_button_state( hwnd
);
1097 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1102 dis
.CtlType
= ODT_BUTTON
;
1105 dis
.itemAction
= action
;
1106 dis
.itemState
= ((state
& BST_FOCUS
) ? ODS_FOCUS
: 0) |
1107 ((state
& BST_PUSHED
) ? ODS_SELECTED
: 0) |
1108 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1109 dis
.hwndItem
= hwnd
;
1112 GetClientRect( hwnd
, &dis
.rcItem
);
1114 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1115 parent
= GetParent(hwnd
);
1116 if (!parent
) parent
= hwnd
;
1117 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1119 hrgn
= set_control_clipping( hDC
, &dis
.rcItem
);
1121 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1122 SelectClipRgn( hDC
, hrgn
);
1123 if (hrgn
) DeleteObject( hrgn
);