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
37 * - WM_CHAR: Checks a (manual or automatic) check box on '+' or '=', clears it on '-' key.
38 * - WM_SETFOCUS: For (manual or automatic) radio buttons, send the parent window BN_CLICKED
39 * - WM_NCCREATE: Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
50 * - BN_PUSHED/BN_HILITE
51 * + BN_KILLFOCUS: is it OK?
53 * + BN_SETFOCUS: is it OK?
54 * - BN_UNPUSHED/BN_UNHILITE
57 * Structures/Macros/Definitions
60 * - Button_GetIdealSize
61 * - Button_GetImageList
62 * - Button_GetTextMargin
63 * - Button_SetImageList
64 * - Button_SetTextMargin
68 #include <wine/debug.h>
69 WINE_DEFAULT_DEBUG_CHANNEL(button
);
71 /* GetWindowLong offsets for window extra information */
72 #define STATE_GWL_OFFSET 0
73 #define HFONT_GWL_OFFSET (sizeof(LONG))
74 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
75 #define UISTATE_GWL_OFFSET (HIMAGE_GWL_OFFSET+sizeof(HFONT))
76 #define NB_EXTRA_BYTES (UISTATE_GWL_OFFSET+sizeof(LONG))
78 /* Button state values */
79 #define BUTTON_UNCHECKED 0x00
80 #define BUTTON_CHECKED 0x01
81 #define BUTTON_3STATE 0x02
82 #define BUTTON_HIGHLIGHTED 0x04
83 #define BUTTON_HASFOCUS 0x08
84 #define BUTTON_NSTATES 0x0F
85 /* undocumented flags */
86 #define BUTTON_BTNPRESSED 0x40
87 #define BUTTON_UNKNOWN2 0x20
88 #define BUTTON_UNKNOWN3 0x10
90 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
91 do { /* Notify parent which has created this button control */ \
92 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
93 SendMessageW(GetParent(hWnd), WM_COMMAND, \
94 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
98 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
99 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
100 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
101 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
102 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
103 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
104 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
105 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
106 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
108 #define MAX_BTN_TYPE 12
110 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
112 BUTTON_UNCHECKED
, /* BS_PUSHBUTTON */
113 BUTTON_UNCHECKED
, /* BS_DEFPUSHBUTTON */
114 BUTTON_CHECKED
, /* BS_CHECKBOX */
115 BUTTON_CHECKED
, /* BS_AUTOCHECKBOX */
116 BUTTON_CHECKED
, /* BS_RADIOBUTTON */
117 BUTTON_3STATE
, /* BS_3STATE */
118 BUTTON_3STATE
, /* BS_AUTO3STATE */
119 BUTTON_UNCHECKED
, /* BS_GROUPBOX */
120 BUTTON_UNCHECKED
, /* BS_USERBUTTON */
121 BUTTON_CHECKED
, /* BS_AUTORADIOBUTTON */
122 BUTTON_UNCHECKED
, /* Not defined */
123 BUTTON_UNCHECKED
/* BS_OWNERDRAW */
126 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
128 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
130 PB_Paint
, /* BS_PUSHBUTTON */
131 PB_Paint
, /* BS_DEFPUSHBUTTON */
132 CB_Paint
, /* BS_CHECKBOX */
133 CB_Paint
, /* BS_AUTOCHECKBOX */
134 CB_Paint
, /* BS_RADIOBUTTON */
135 CB_Paint
, /* BS_3STATE */
136 CB_Paint
, /* BS_AUTO3STATE */
137 GB_Paint
, /* BS_GROUPBOX */
138 UB_Paint
, /* BS_USERBUTTON */
139 CB_Paint
, /* BS_AUTORADIOBUTTON */
140 NULL
, /* Not defined */
141 OB_Paint
/* BS_OWNERDRAW */
144 static HBITMAP hbitmapCheckBoxes
= 0;
145 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
148 /*********************************************************************
149 * button class descriptor
151 const struct builtin_class_descr BUTTON_builtin_class
=
154 L
"Button", /* name */
155 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
156 ButtonWndProcW
, /* procW */
157 ButtonWndProcA
, /* procA */
158 NB_EXTRA_BYTES
, /* extra */
159 (LPWSTR
)IDC_ARROW
, /* cursor */
163 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
164 ButtonWndProcA
, /* procA */
165 ButtonWndProcW
, /* procW */
166 NB_EXTRA_BYTES
, /* extra */
167 IDC_ARROW
, /* cursor */
173 __inline
static LONG
get_button_state( HWND hwnd
)
175 return GetWindowLongW( hwnd
, STATE_GWL_OFFSET
);
178 __inline
static void set_button_state( HWND hwnd
, LONG state
)
180 SetWindowLongW( hwnd
, STATE_GWL_OFFSET
, state
);
183 static __inline
void set_ui_state( HWND hwnd
, LONG flags
)
185 SetWindowLongW( hwnd
, UISTATE_GWL_OFFSET
, flags
);
188 static __inline LONG
get_ui_state( HWND hwnd
)
190 return GetWindowLongPtrW( hwnd
, UISTATE_GWL_OFFSET
);
193 __inline
static HFONT
get_button_font( HWND hwnd
)
195 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
198 __inline
static void set_button_font( HWND hwnd
, HFONT font
)
200 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
203 __inline
static UINT
get_button_type( LONG window_style
)
205 return (window_style
& 0x0f);
208 /* paint a button of any type */
209 __inline
static void paint_button( HWND hwnd
, LONG style
, UINT action
)
211 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
213 HDC hdc
= GetDC( hwnd
);
214 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
215 ReleaseDC( hwnd
, hdc
);
219 /* retrieve the button text; returned buffer must be freed by caller */
220 __inline
static WCHAR
*get_button_text( HWND hwnd
)
223 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
224 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
228 /* Retrieve the UI state for the control */
229 static BOOL
button_update_uistate(HWND hwnd
, BOOL unicode
)
231 LONG flags
, prevflags
;
234 flags
= DefWindowProcW(hwnd
, WM_QUERYUISTATE
, 0, 0);
236 flags
= DefWindowProcA(hwnd
, WM_QUERYUISTATE
, 0, 0);
238 prevflags
= get_ui_state(hwnd
);
240 if (prevflags
!= flags
)
242 set_ui_state(hwnd
, flags
);
249 /***********************************************************************
250 * ButtonWndProc_common
252 static LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
253 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
257 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
258 UINT btn_type
= get_button_type( style
);
262 pt
.x
= (short)LOWORD(lParam
);
263 pt
.y
= (short)HIWORD(lParam
);
271 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
272 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
274 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
275 case BS_GROUPBOX
: return DLGC_STATIC
;
276 default: return DLGC_BUTTON
;
280 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
284 if (!hbitmapCheckBoxes
)
287 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
288 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
289 checkBoxWidth
= bmp
.bmWidth
/ 4;
290 checkBoxHeight
= bmp
.bmHeight
/ 3;
292 if (btn_type
>= MAX_BTN_TYPE
)
293 return -1; /* abort */
294 set_button_state( hWnd
, BUTTON_UNCHECKED
);
295 button_update_uistate( hWnd
, unicode
);
299 if (btn_type
== BS_OWNERDRAW
)
301 HDC hdc
= (HDC
)wParam
;
304 HWND parent
= GetParent(hWnd
);
305 if (!parent
) parent
= hWnd
;
306 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
307 if (!hBrush
) /* did the app forget to call defwindowproc ? */
308 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
309 (WPARAM
)hdc
, (LPARAM
)hWnd
);
310 GetClientRect(hWnd
, &rc
);
311 FillRect(hdc
, &rc
, hBrush
);
317 if (btnPaintFunc
[btn_type
])
320 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
321 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
322 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
323 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
324 if( !wParam
) EndPaint( hWnd
, &ps
);
329 if (wParam
== VK_SPACE
)
331 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
332 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
336 case WM_LBUTTONDBLCLK
:
337 if(style
& BS_NOTIFY
||
338 btn_type
== BS_RADIOBUTTON
||
339 btn_type
== BS_USERBUTTON
||
340 btn_type
== BS_OWNERDRAW
)
342 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
349 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
350 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
354 if (wParam
!= VK_SPACE
)
358 state
= get_button_state( hWnd
);
359 if (!(state
& BUTTON_BTNPRESSED
)) break;
360 state
&= BUTTON_NSTATES
;
361 set_button_state( hWnd
, state
);
362 if (!(state
& BUTTON_HIGHLIGHTED
))
367 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
369 GetClientRect( hWnd
, &rect
);
370 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
372 state
= get_button_state( hWnd
);
375 case BS_AUTOCHECKBOX
:
376 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BUTTON_CHECKED
), 0 );
378 case BS_AUTORADIOBUTTON
:
379 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
382 SendMessageW( hWnd
, BM_SETCHECK
,
383 (state
& BUTTON_3STATE
) ? 0 : ((state
& 3) + 1), 0 );
386 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
390 case WM_CAPTURECHANGED
:
391 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
392 state
= get_button_state( hWnd
);
393 if (state
& BUTTON_BTNPRESSED
)
395 state
&= BUTTON_NSTATES
;
396 set_button_state( hWnd
, state
);
397 if (state
& BUTTON_HIGHLIGHTED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
402 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
404 GetClientRect( hWnd
, &rect
);
405 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
411 /* Clear an old text here as Windows does */
412 HDC hdc
= GetDC(hWnd
);
415 HWND parent
= GetParent(hWnd
);
417 if (!parent
) parent
= hWnd
;
418 hbrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
419 (WPARAM
)hdc
, (LPARAM
)hWnd
);
420 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
421 hbrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
422 (WPARAM
)hdc
, (LPARAM
)hWnd
);
424 GetClientRect(hWnd
, &client
);
426 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
427 /* Clip by client rect bounds */
428 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
429 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
430 FillRect(hdc
, &rc
, hbrush
);
431 ReleaseDC(hWnd
, hdc
);
433 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
434 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
435 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
436 InvalidateRect( hWnd
, NULL
, TRUE
);
438 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
439 return 1; /* success. FIXME: check text length */
443 set_button_font( hWnd
, (HFONT
)wParam
);
444 if (lParam
) paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
448 return (LRESULT
)get_button_font( hWnd
);
451 TRACE("WM_SETFOCUS %p\n",hWnd
);
452 set_button_state( hWnd
, get_button_state(hWnd
) | BUTTON_HASFOCUS
);
453 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
454 if (style
& BS_NOTIFY
)
455 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
459 TRACE("WM_KILLFOCUS %p\n",hWnd
);
460 state
= get_button_state( hWnd
);
461 set_button_state( hWnd
, state
& ~BUTTON_HASFOCUS
);
462 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
464 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
466 if (style
& BS_NOTIFY
)
467 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
471 case WM_SYSCOLORCHANGE
:
472 InvalidateRect( hWnd
, NULL
, FALSE
);
479 if ((wParam
& 0x0f) >= MAX_BTN_TYPE
) break;
480 btn_type
= wParam
& 0x0f;
481 style
= (style
& ~0x0f) | btn_type
;
482 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
484 /* Only redraw if lParam flag is set.*/
486 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
491 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
492 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
496 /* Check that image format matches button style */
497 switch (style
& (BS_BITMAP
|BS_ICON
))
500 if (wParam
!= IMAGE_BITMAP
) return 0;
503 if (wParam
!= IMAGE_ICON
) return 0;
508 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
509 InvalidateRect( hWnd
, NULL
, FALSE
);
510 return (LRESULT
)oldHbitmap
;
513 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
519 return get_button_state( hWnd
) & 3;
525 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
526 state
= get_button_state( hWnd
);
527 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
529 if (wParam
) style
|= WS_TABSTOP
;
530 else style
&= ~WS_TABSTOP
;
531 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
533 if ((state
& 3) != wParam
)
535 set_button_state( hWnd
, (state
& ~3) | wParam
);
536 paint_button( hWnd
, btn_type
, ODA_SELECT
);
538 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
) && (style
& WS_CHILD
))
539 BUTTON_CheckAutoRadioButton( hWnd
);
546 return get_button_state( hWnd
);
552 state
= get_button_state( hWnd
);
555 if (state
& BUTTON_HIGHLIGHTED
) break;
556 set_button_state( hWnd
, state
| BUTTON_HIGHLIGHTED
);
560 if (!(state
& BUTTON_HIGHLIGHTED
)) break;
561 set_button_state( hWnd
, state
& ~BUTTON_HIGHLIGHTED
);
563 paint_button( hWnd
, btn_type
, ODA_SELECT
);
566 case WM_UPDATEUISTATE
:
568 DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
570 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
572 if (button_update_uistate(hWnd
, unicode
))
573 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
577 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
580 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
581 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
586 /***********************************************************************
588 * The button window procedure. This is just a wrapper which locks
589 * the passed HWND and calls the real window procedure (with a WND*
590 * pointer pointing to the locked windowstructure).
592 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
594 if (!IsWindow( hWnd
)) return 0;
595 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, TRUE
);
599 /***********************************************************************
602 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
604 if (!IsWindow( hWnd
)) return 0;
605 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, FALSE
);
609 /**********************************************************************
610 * Convert button styles to flags used by DrawText.
611 * TODO: handle WS_EX_RIGHT extended style.
613 static UINT
BUTTON_BStoDT(DWORD style
)
615 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
617 /* "Convert" pushlike buttons to pushbuttons */
618 if (style
& BS_PUSHLIKE
)
621 if (!(style
& BS_MULTILINE
))
622 dtStyle
|= DT_SINGLELINE
;
624 dtStyle
|= DT_WORDBREAK
;
626 switch (style
& BS_CENTER
)
628 case BS_LEFT
: /* DT_LEFT is 0 */ break;
629 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
630 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
632 /* Pushbutton's text is centered by default */
633 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
634 /* all other flavours have left aligned text */
637 /* DrawText ignores vertical alignment for multiline text,
638 * but we use these flags to align label manualy.
640 if (get_button_type(style
) != BS_GROUPBOX
)
642 switch (style
& BS_VCENTER
)
644 case BS_TOP
: /* DT_TOP is 0 */ break;
645 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
646 case BS_VCENTER
: /* fall through */
647 default: dtStyle
|= DT_VCENTER
; break;
651 /* GroupBox's text is always single line and is top aligned. */
652 dtStyle
|= DT_SINGLELINE
;
657 /**********************************************************************
658 * BUTTON_CalcLabelRect
660 * Calculates label's rectangle depending on button style.
662 * Returns flags to be passed to DrawText.
663 * Calculated rectangle doesn't take into account button state
664 * (pushed, etc.). If there is nothing to draw (no text/image) output
665 * rectangle is empty, and return value is (UINT)-1.
667 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
669 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
673 UINT dtStyle
= BUTTON_BStoDT(style
);
677 /* Calculate label rectangle according to label type */
678 switch (style
& (BS_ICON
|BS_BITMAP
))
681 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
684 HeapFree( GetProcessHeap(), 0, text
);
687 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
688 HeapFree( GetProcessHeap(), 0, text
);
690 if (get_ui_state( hwnd
) & UISF_HIDEACCEL
)
691 dtStyle
|= DT_HIDEPREFIX
;
695 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
698 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
700 r
.right
= r
.left
+ bm
.bmWidth
;
701 r
.bottom
= r
.top
+ bm
.bmHeight
;
703 DeleteObject(iconInfo
.hbmColor
);
704 DeleteObject(iconInfo
.hbmMask
);
708 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
711 r
.right
= r
.left
+ bm
.bmWidth
;
712 r
.bottom
= r
.top
+ bm
.bmHeight
;
719 return (UINT
)(LONG
)-1;
722 /* Position label inside bounding rectangle according to
723 * alignment flags. (calculated rect is always left-top aligned).
724 * If label is aligned to any side - shift label in opposite
725 * direction to leave extra space for focus rectangle.
727 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
729 case DT_LEFT
: r
.left
++; r
.right
++; break;
730 case DT_CENTER
: n
= r
.right
- r
.left
;
731 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
732 r
.right
= r
.left
+ n
; break;
733 case DT_RIGHT
: n
= r
.right
- r
.left
;
734 r
.right
= rc
->right
- 1;
735 r
.left
= r
.right
- n
;
739 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
741 case DT_TOP
: r
.top
++; r
.bottom
++; break;
742 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
743 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
744 r
.bottom
= r
.top
+ n
; break;
745 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
746 r
.bottom
= rc
->bottom
- 1;
747 r
.top
= r
.bottom
- n
;
756 /**********************************************************************
757 * BUTTON_DrawTextCallback
759 * Callback function used by DrawStateW function.
761 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
769 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
774 /**********************************************************************
777 * Common function for drawing button label.
779 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, RECT
*rc
)
781 DRAWSTATEPROC lpOutputProc
= NULL
;
785 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
786 LONG state
= get_button_state( hwnd
);
787 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
790 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
791 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
792 * I don't have Win31 on hand to verify that, so I leave it as is.
795 if ((style
& BS_PUSHLIKE
) && (state
& BUTTON_3STATE
))
797 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
801 switch (style
& (BS_ICON
|BS_BITMAP
))
804 /* DST_COMPLEX -- is 0 */
805 lpOutputProc
= BUTTON_DrawTextCallback
;
806 if (!(text
= get_button_text( hwnd
))) return;
808 wp
= (WPARAM
)dtFlags
;
810 if (dtFlags
& DT_HIDEPREFIX
)
811 flags
|= DSS_HIDEPREFIX
;
816 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
821 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
828 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
829 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
830 HeapFree( GetProcessHeap(), 0, text
);
833 /**********************************************************************
834 * Push Button Functions
836 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
838 RECT rc
, focus_rect
, r
;
839 UINT dtFlags
, uState
;
843 COLORREF oldTxtColor
;
845 LONG state
= get_button_state( hwnd
);
846 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
847 BOOL pushedState
= (state
& BUTTON_HIGHLIGHTED
);
850 GetClientRect( hwnd
, &rc
);
852 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
853 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
854 parent
= GetParent(hwnd
);
855 if (!parent
) parent
= hwnd
;
856 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
857 hOldPen
= (HPEN
)SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
858 hOldBrush
=(HBRUSH
)SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
859 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
861 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
863 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
864 InflateRect( &rc
, -1, -1 );
867 uState
= DFCS_BUTTONPUSH
| DFCS_ADJUSTRECT
;
871 else if (pushedState
)
873 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
876 uState
|= DFCS_PUSHED
;
879 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
))
880 uState
|= DFCS_CHECKED
;
882 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
886 /* draw button label */
888 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
890 if (dtFlags
== (UINT
)-1L)
894 OffsetRect(&r
, 1, 1);
896 IntersectClipRect(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
898 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
900 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
902 SetTextColor( hDC
, oldTxtColor
);
904 if (state
& BUTTON_HASFOCUS
)
906 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
908 InflateRect( &focus_rect
, -1, -1 );
909 IntersectRect(&focus_rect
, &focus_rect
, &rc
);
910 DrawFocusRect( hDC
, &focus_rect
);
915 SelectObject( hDC
, hOldPen
);
916 SelectObject( hDC
, hOldBrush
);
917 SetBkMode(hDC
, oldBkMode
);
920 /**********************************************************************
921 * Check Box & Radio Button Functions
924 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
926 RECT rbox
, rtext
, client
;
931 LONG state
= get_button_state( hwnd
);
932 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
935 if (style
& BS_PUSHLIKE
)
937 PB_Paint( hwnd
, hDC
, action
);
941 GetClientRect(hwnd
, &client
);
942 rbox
= rtext
= client
;
944 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
946 parent
= GetParent(hwnd
);
947 if (!parent
) parent
= hwnd
;
948 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
949 (WPARAM
)hDC
, (LPARAM
)hwnd
);
950 if (!hBrush
) /* did the app forget to call defwindowproc ? */
951 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
952 (WPARAM
)hDC
, (LPARAM
)hwnd
);
954 if (style
& BS_LEFTTEXT
)
956 /* magic +4 is what CTL3D expects */
958 rtext
.right
-= checkBoxWidth
+ 4;
959 rbox
.left
= rbox
.right
- checkBoxWidth
;
963 rtext
.left
+= checkBoxWidth
+ 4;
964 rbox
.right
= checkBoxWidth
;
967 /* Since WM_ERASEBKGND does nothing, first prepare background */
968 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
969 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
973 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
975 /* Only adjust rbox when rtext is valid */
976 if (dtFlags
!= (UINT
)-1L)
978 rbox
.top
= rtext
.top
;
979 rbox
.bottom
= rtext
.bottom
;
982 /* Draw the check-box bitmap */
983 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
987 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
988 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
989 else if (state
& BUTTON_3STATE
) flags
= DFCS_BUTTON3STATE
;
990 else flags
= DFCS_BUTTONCHECK
;
992 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) flags
|= DFCS_CHECKED
;
993 if (state
& BUTTON_HIGHLIGHTED
) flags
|= DFCS_PUSHED
;
995 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
997 /* rbox must have the correct height */
998 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
1000 if (style
& BS_TOP
) {
1002 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1004 rbox
.top
-= -delta
/2 + 1;
1005 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1007 } else if (style
& BS_BOTTOM
) {
1009 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1011 rbox
.bottom
+= -delta
/2 + 1;
1012 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1014 } else { /* Default */
1016 int ofs
= (delta
/ 2);
1017 rbox
.bottom
-= ofs
+ 1;
1018 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
1019 } else if (delta
< 0) {
1020 int ofs
= (-delta
/ 2);
1021 rbox
.top
-= ofs
+ 1;
1022 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
1026 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
1029 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
1032 IntersectClipRect(hDC
, client
.left
, client
.top
, client
.right
, client
.bottom
);
1034 if (action
== ODA_DRAWENTIRE
)
1035 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
1038 if ((action
== ODA_FOCUS
) ||
1039 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
1041 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1045 IntersectRect(&rtext
, &rtext
, &client
);
1046 DrawFocusRect( hDC
, &rtext
);
1052 /**********************************************************************
1053 * BUTTON_CheckAutoRadioButton
1055 * hwnd is checked, uncheck every other auto radio button in group
1057 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1059 HWND parent
, sibling
, start
;
1061 parent
= GetParent(hwnd
);
1062 /* make sure that starting control is not disabled or invisible */
1063 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1066 if (!sibling
) break;
1067 if ((hwnd
!= sibling
) &&
1068 ((GetWindowLongW( sibling
, GWL_STYLE
) & 0x0f) == BS_AUTORADIOBUTTON
))
1069 SendMessageW( sibling
, BM_SETCHECK
, BUTTON_UNCHECKED
, 0 );
1070 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1071 } while (sibling
!= start
);
1075 /**********************************************************************
1076 * Group Box Functions
1079 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1086 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1089 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1090 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1091 parent
= GetParent(hwnd
);
1092 if (!parent
) parent
= hwnd
;
1093 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1094 if (!hbr
) /* did the app forget to call defwindowproc ? */
1095 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1096 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1098 GetClientRect( hwnd
, &rc
);
1101 GetTextMetricsW (hDC
, &tm
);
1102 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1103 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1105 InflateRect(&rc
, -7, 1);
1106 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1108 if (dtFlags
== (UINT
)-1L)
1111 /* Because buttons have CS_PARENTDC class style, there is a chance
1112 * that label will be drawn out of client rect.
1113 * But Windows doesn't clip label's rect, so do I.
1116 /* There is 1-pixel marging at the left, right, and bottom */
1117 rc
.left
--; rc
.right
++; rc
.bottom
++;
1118 FillRect(hDC
, &rc
, hbr
);
1119 rc
.left
++; rc
.right
--; rc
.bottom
--;
1121 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1125 /**********************************************************************
1126 * User Button Functions
1129 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1134 LONG state
= get_button_state( hwnd
);
1137 if (action
== ODA_SELECT
) return;
1139 GetClientRect( hwnd
, &rc
);
1141 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1143 parent
= GetParent(hwnd
);
1144 if (!parent
) parent
= hwnd
;
1145 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1146 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1147 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1148 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1150 FillRect( hDC
, &rc
, hBrush
);
1151 if ((action
== ODA_FOCUS
) ||
1152 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
1154 if (!(get_ui_state(hwnd
) & UISF_HIDEFOCUS
))
1155 DrawFocusRect( hDC
, &rc
);
1160 /**********************************************************************
1161 * Ownerdrawn Button Functions
1164 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1166 LONG state
= get_button_state( hwnd
);
1170 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1172 HFONT hFont
, hPrevFont
= 0;
1174 dis
.CtlType
= ODT_BUTTON
;
1177 dis
.itemAction
= action
;
1178 dis
.itemState
= ((state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
1179 ((state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
1180 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1181 dis
.hwndItem
= hwnd
;
1184 GetClientRect( hwnd
, &dis
.rcItem
);
1186 clipRegion
= CreateRectRgnIndirect(&dis
.rcItem
);
1187 if (GetClipRgn(hDC
, clipRegion
) != 1)
1189 DeleteObject(clipRegion
);
1192 clipRect
= dis
.rcItem
;
1193 DPtoLP(hDC
, (LPPOINT
) &clipRect
, 2);
1194 IntersectClipRect(hDC
, clipRect
.left
, clipRect
.top
, clipRect
.right
, clipRect
.bottom
);
1196 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1197 parent
= GetParent(hwnd
);
1198 if (!parent
) parent
= hwnd
;
1199 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1200 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1201 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1202 SelectClipRgn(hDC
, clipRegion
);