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
74 #include "wine/winuser16.h"
76 #include "user_private.h"
77 #include "wine/debug.h"
79 WINE_DEFAULT_DEBUG_CHANNEL(button
);
81 /* GetWindowLong offsets for window extra information */
82 #define STATE_GWL_OFFSET 0
83 #define HFONT_GWL_OFFSET (sizeof(LONG))
84 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
85 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
87 /* Button state values */
88 #define BUTTON_UNCHECKED 0x00
89 #define BUTTON_CHECKED 0x01
90 #define BUTTON_3STATE 0x02
91 #define BUTTON_HIGHLIGHTED 0x04
92 #define BUTTON_HASFOCUS 0x08
93 #define BUTTON_NSTATES 0x0F
94 /* undocumented flags */
95 #define BUTTON_BTNPRESSED 0x40
96 #define BUTTON_UNKNOWN2 0x20
97 #define BUTTON_UNKNOWN3 0x10
99 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
100 do { /* Notify parent which has created this button control */ \
101 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
102 SendMessageW(GetParent(hWnd), WM_COMMAND, \
103 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
107 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
108 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
109 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
110 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
111 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
112 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
113 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
114 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
115 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
117 #define MAX_BTN_TYPE 12
119 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
121 BUTTON_UNCHECKED
, /* BS_PUSHBUTTON */
122 BUTTON_UNCHECKED
, /* BS_DEFPUSHBUTTON */
123 BUTTON_CHECKED
, /* BS_CHECKBOX */
124 BUTTON_CHECKED
, /* BS_AUTOCHECKBOX */
125 BUTTON_CHECKED
, /* BS_RADIOBUTTON */
126 BUTTON_3STATE
, /* BS_3STATE */
127 BUTTON_3STATE
, /* BS_AUTO3STATE */
128 BUTTON_UNCHECKED
, /* BS_GROUPBOX */
129 BUTTON_UNCHECKED
, /* BS_USERBUTTON */
130 BUTTON_CHECKED
, /* BS_AUTORADIOBUTTON */
131 BUTTON_UNCHECKED
, /* Not defined */
132 BUTTON_UNCHECKED
/* BS_OWNERDRAW */
135 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
137 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
139 PB_Paint
, /* BS_PUSHBUTTON */
140 PB_Paint
, /* BS_DEFPUSHBUTTON */
141 CB_Paint
, /* BS_CHECKBOX */
142 CB_Paint
, /* BS_AUTOCHECKBOX */
143 CB_Paint
, /* BS_RADIOBUTTON */
144 CB_Paint
, /* BS_3STATE */
145 CB_Paint
, /* BS_AUTO3STATE */
146 GB_Paint
, /* BS_GROUPBOX */
147 UB_Paint
, /* BS_USERBUTTON */
148 CB_Paint
, /* BS_AUTORADIOBUTTON */
149 NULL
, /* Not defined */
150 OB_Paint
/* BS_OWNERDRAW */
153 static HBITMAP hbitmapCheckBoxes
= 0;
154 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
157 /*********************************************************************
158 * button class descriptor
160 const struct builtin_class_descr BUTTON_builtin_class
=
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 */
172 inline static LONG
get_button_state( HWND hwnd
)
174 return GetWindowLongW( hwnd
, STATE_GWL_OFFSET
);
177 inline static void set_button_state( HWND hwnd
, LONG state
)
179 SetWindowLongW( hwnd
, STATE_GWL_OFFSET
, state
);
182 inline static HFONT
get_button_font( HWND hwnd
)
184 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
187 inline static void set_button_font( HWND hwnd
, HFONT font
)
189 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
192 inline static UINT
get_button_type( LONG window_style
)
194 return (window_style
& 0x0f);
197 /* paint a button of any type */
198 inline static void paint_button( HWND hwnd
, LONG style
, UINT action
)
200 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
202 HDC hdc
= GetDC( hwnd
);
203 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
204 ReleaseDC( hwnd
, hdc
);
208 /* retrieve the button text; returned buffer must be freed by caller */
209 inline static WCHAR
*get_button_text( HWND hwnd
)
212 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
213 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
217 /***********************************************************************
218 * ButtonWndProc_common
220 static LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
221 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
225 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
226 UINT btn_type
= get_button_type( style
);
230 pt
.x
= (short)LOWORD(lParam
);
231 pt
.y
= (short)HIWORD(lParam
);
239 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
240 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
242 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
243 case BS_GROUPBOX
: return DLGC_STATIC
;
244 default: return DLGC_BUTTON
;
248 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
252 if (!hbitmapCheckBoxes
)
255 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
256 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
257 checkBoxWidth
= bmp
.bmWidth
/ 4;
258 checkBoxHeight
= bmp
.bmHeight
/ 3;
260 if (btn_type
>= MAX_BTN_TYPE
)
261 return -1; /* abort */
262 set_button_state( hWnd
, BUTTON_UNCHECKED
);
266 if (btn_type
== BS_OWNERDRAW
)
268 HDC hdc
= (HDC
)wParam
;
271 HWND parent
= GetParent(hWnd
);
272 if (!parent
) parent
= hWnd
;
273 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
274 if (!hBrush
) /* did the app forget to call defwindowproc ? */
275 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
276 (WPARAM
)hdc
, (LPARAM
)hWnd
);
277 GetClientRect(hWnd
, &rc
);
278 FillRect(hdc
, &rc
, hBrush
);
284 if (btnPaintFunc
[btn_type
])
287 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
288 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
289 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
290 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
291 if( !wParam
) EndPaint( hWnd
, &ps
);
296 if (wParam
== VK_SPACE
)
298 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
299 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
303 case WM_LBUTTONDBLCLK
:
304 if(style
& BS_NOTIFY
||
305 btn_type
== BS_RADIOBUTTON
||
306 btn_type
== BS_USERBUTTON
||
307 btn_type
== BS_OWNERDRAW
)
309 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
316 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
317 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
321 if (wParam
!= VK_SPACE
)
325 state
= get_button_state( hWnd
);
326 if (!(state
& BUTTON_BTNPRESSED
)) break;
327 state
&= BUTTON_NSTATES
;
328 set_button_state( hWnd
, state
);
329 if (!(state
& BUTTON_HIGHLIGHTED
))
334 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
336 GetClientRect( hWnd
, &rect
);
337 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
339 state
= get_button_state( hWnd
);
342 case BS_AUTOCHECKBOX
:
343 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BUTTON_CHECKED
), 0 );
345 case BS_AUTORADIOBUTTON
:
346 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
349 SendMessageW( hWnd
, BM_SETCHECK
,
350 (state
& BUTTON_3STATE
) ? 0 : ((state
& 3) + 1), 0 );
353 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
357 case WM_CAPTURECHANGED
:
358 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
359 state
= get_button_state( hWnd
);
360 if (state
& BUTTON_BTNPRESSED
)
362 state
&= BUTTON_NSTATES
;
363 set_button_state( hWnd
, state
);
364 if (state
& BUTTON_HIGHLIGHTED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
369 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
371 GetClientRect( hWnd
, &rect
);
372 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
378 /* Clear an old text here as Windows does */
379 HDC hdc
= GetDC(hWnd
);
382 HWND parent
= GetParent(hWnd
);
384 if (!parent
) parent
= hWnd
;
385 hbrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
386 (WPARAM
)hdc
, (LPARAM
)hWnd
);
387 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
388 hbrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
389 (WPARAM
)hdc
, (LPARAM
)hWnd
);
391 GetClientRect(hWnd
, &client
);
393 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
394 /* Clip by client rect bounds */
395 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
396 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
397 FillRect(hdc
, &rc
, hbrush
);
398 ReleaseDC(hWnd
, hdc
);
400 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
401 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
402 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
403 InvalidateRect( hWnd
, NULL
, TRUE
);
405 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
406 return 1; /* success. FIXME: check text length */
410 set_button_font( hWnd
, (HFONT
)wParam
);
411 if (lParam
) paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
415 return (LRESULT
)get_button_font( hWnd
);
418 TRACE("WM_SETFOCUS %p\n",hWnd
);
419 set_button_state( hWnd
, get_button_state(hWnd
) | BUTTON_HASFOCUS
);
420 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
421 if (style
& BS_NOTIFY
)
422 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
426 TRACE("WM_KILLFOCUS %p\n",hWnd
);
427 state
= get_button_state( hWnd
);
428 set_button_state( hWnd
, state
& ~BUTTON_HASFOCUS
);
429 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
431 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
433 if (style
& BS_NOTIFY
)
434 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
438 case WM_SYSCOLORCHANGE
:
439 InvalidateRect( hWnd
, NULL
, FALSE
);
444 if ((wParam
& 0x0f) >= MAX_BTN_TYPE
) break;
445 btn_type
= wParam
& 0x0f;
446 style
= (style
& ~0x0f) | btn_type
;
447 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
449 /* Only redraw if lParam flag is set.*/
451 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
456 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
457 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
461 /* Check that image format matches button style */
462 switch (style
& (BS_BITMAP
|BS_ICON
))
465 if (wParam
!= IMAGE_BITMAP
) return 0;
468 if (wParam
!= IMAGE_ICON
) return 0;
473 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
474 InvalidateRect( hWnd
, NULL
, FALSE
);
475 return (LRESULT
)oldHbitmap
;
478 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
482 return get_button_state( hWnd
) & 3;
486 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
487 state
= get_button_state( hWnd
);
488 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
490 if (wParam
) style
|= WS_TABSTOP
;
491 else style
&= ~WS_TABSTOP
;
492 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
494 if ((state
& 3) != wParam
)
496 set_button_state( hWnd
, (state
& ~3) | wParam
);
497 paint_button( hWnd
, btn_type
, ODA_SELECT
);
499 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
) && (style
& WS_CHILD
))
500 BUTTON_CheckAutoRadioButton( hWnd
);
505 return get_button_state( hWnd
);
509 state
= get_button_state( hWnd
);
512 if (state
& BUTTON_HIGHLIGHTED
) break;
513 set_button_state( hWnd
, state
| BUTTON_HIGHLIGHTED
);
517 if (!(state
& BUTTON_HIGHLIGHTED
)) break;
518 set_button_state( hWnd
, state
& ~BUTTON_HIGHLIGHTED
);
520 paint_button( hWnd
, btn_type
, ODA_SELECT
);
524 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
527 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
528 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
533 /***********************************************************************
535 * The button window procedure. This is just a wrapper which locks
536 * the passed HWND and calls the real window procedure (with a WND*
537 * pointer pointing to the locked windowstructure).
539 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
541 if (!IsWindow( hWnd
)) return 0;
542 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, TRUE
);
546 /***********************************************************************
549 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
551 if (!IsWindow( hWnd
)) return 0;
552 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, FALSE
);
556 /**********************************************************************
557 * Convert button styles to flags used by DrawText.
558 * TODO: handle WS_EX_RIGHT extended style.
560 static UINT
BUTTON_BStoDT(DWORD style
)
562 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
564 /* "Convert" pushlike buttons to pushbuttons */
565 if (style
& BS_PUSHLIKE
)
568 if (!(style
& BS_MULTILINE
))
569 dtStyle
|= DT_SINGLELINE
;
571 dtStyle
|= DT_WORDBREAK
;
573 switch (style
& BS_CENTER
)
575 case BS_LEFT
: /* DT_LEFT is 0 */ break;
576 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
577 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
579 /* Pushbutton's text is centered by default */
580 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
581 /* all other flavours have left aligned text */
584 /* DrawText ignores vertical alignment for multiline text,
585 * but we use these flags to align label manualy.
587 if (get_button_type(style
) != BS_GROUPBOX
)
589 switch (style
& BS_VCENTER
)
591 case BS_TOP
: /* DT_TOP is 0 */ break;
592 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
593 case BS_VCENTER
: /* fall through */
594 default: dtStyle
|= DT_VCENTER
; break;
598 /* GroupBox's text is always single line and is top aligned. */
599 dtStyle
|= DT_SINGLELINE
;
604 /**********************************************************************
605 * BUTTON_CalcLabelRect
607 * Calculates label's rectangle depending on button style.
609 * Returns flags to be passed to DrawText.
610 * Calculated rectangle doesn't take into account button state
611 * (pushed, etc.). If there is nothing to draw (no text/image) output
612 * rectangle is empty, and return value is (UINT)-1.
614 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
616 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
620 UINT dtStyle
= BUTTON_BStoDT(style
);
624 /* Calculate label rectangle according to label type */
625 switch (style
& (BS_ICON
|BS_BITMAP
))
628 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
631 HeapFree( GetProcessHeap(), 0, text
);
634 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
635 HeapFree( GetProcessHeap(), 0, text
);
639 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
642 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
644 r
.right
= r
.left
+ bm
.bmWidth
;
645 r
.bottom
= r
.top
+ bm
.bmHeight
;
647 DeleteObject(iconInfo
.hbmColor
);
648 DeleteObject(iconInfo
.hbmMask
);
652 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
655 r
.right
= r
.left
+ bm
.bmWidth
;
656 r
.bottom
= r
.top
+ bm
.bmHeight
;
663 return (UINT
)(LONG
)-1;
666 /* Position label inside bounding rectangle according to
667 * alignment flags. (calculated rect is always left-top aligned).
668 * If label is aligned to any side - shift label in opposite
669 * direction to leave extra space for focus rectangle.
671 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
673 case DT_LEFT
: r
.left
++; r
.right
++; break;
674 case DT_CENTER
: n
= r
.right
- r
.left
;
675 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
676 r
.right
= r
.left
+ n
; break;
677 case DT_RIGHT
: n
= r
.right
- r
.left
;
678 r
.right
= rc
->right
- 1;
679 r
.left
= r
.right
- n
;
683 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
685 case DT_TOP
: r
.top
++; r
.bottom
++; break;
686 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
687 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
688 r
.bottom
= r
.top
+ n
; break;
689 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
690 r
.bottom
= rc
->bottom
- 1;
691 r
.top
= r
.bottom
- n
;
700 /**********************************************************************
701 * BUTTON_DrawTextCallback
703 * Callback function used by DrawStateW function.
705 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
713 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
718 /**********************************************************************
721 * Common function for drawing button label.
723 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, RECT
*rc
)
725 DRAWSTATEPROC lpOutputProc
= NULL
;
729 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
730 LONG state
= get_button_state( hwnd
);
731 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
734 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
735 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
736 * I don't have Win31 on hand to verify that, so I leave it as is.
739 if ((style
& BS_PUSHLIKE
) && (state
& BUTTON_3STATE
))
741 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
745 switch (style
& (BS_ICON
|BS_BITMAP
))
748 /* DST_COMPLEX -- is 0 */
749 lpOutputProc
= BUTTON_DrawTextCallback
;
750 if (!(text
= get_button_text( hwnd
))) return;
752 wp
= (WPARAM
)dtFlags
;
757 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
762 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
769 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
770 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
771 HeapFree( GetProcessHeap(), 0, text
);
774 /**********************************************************************
775 * Push Button Functions
777 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
779 RECT rc
, focus_rect
, r
;
780 UINT dtFlags
, uState
;
784 COLORREF oldTxtColor
;
786 LONG state
= get_button_state( hwnd
);
787 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
788 BOOL pushedState
= (state
& BUTTON_HIGHLIGHTED
);
791 GetClientRect( hwnd
, &rc
);
793 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
794 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
795 parent
= GetParent(hwnd
);
796 if (!parent
) parent
= hwnd
;
797 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
798 hOldPen
= (HPEN
)SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
799 hOldBrush
=(HBRUSH
)SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
800 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
802 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
804 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
805 InflateRect( &rc
, -1, -1 );
808 uState
= DFCS_BUTTONPUSH
| DFCS_ADJUSTRECT
;
812 else if (pushedState
)
814 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
817 uState
|= DFCS_PUSHED
;
820 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
))
821 uState
|= DFCS_CHECKED
;
823 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
827 /* draw button label */
829 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
831 if (dtFlags
== (UINT
)-1L)
835 OffsetRect(&r
, 1, 1);
837 IntersectClipRect(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
839 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
841 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
843 SetTextColor( hDC
, oldTxtColor
);
845 if (state
& BUTTON_HASFOCUS
)
847 InflateRect( &focus_rect
, -1, -1 );
848 IntersectRect(&focus_rect
, &focus_rect
, &rc
);
849 DrawFocusRect( hDC
, &focus_rect
);
853 SelectObject( hDC
, hOldPen
);
854 SelectObject( hDC
, hOldBrush
);
855 SetBkMode(hDC
, oldBkMode
);
858 /**********************************************************************
859 * Check Box & Radio Button Functions
862 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
864 RECT rbox
, rtext
, client
;
869 LONG state
= get_button_state( hwnd
);
870 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
873 if (style
& BS_PUSHLIKE
)
875 PB_Paint( hwnd
, hDC
, action
);
879 GetClientRect(hwnd
, &client
);
880 rbox
= rtext
= client
;
882 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
884 parent
= GetParent(hwnd
);
885 if (!parent
) parent
= hwnd
;
886 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
887 (WPARAM
)hDC
, (LPARAM
)hwnd
);
888 if (!hBrush
) /* did the app forget to call defwindowproc ? */
889 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
890 (WPARAM
)hDC
, (LPARAM
)hwnd
);
892 if (style
& BS_LEFTTEXT
)
894 /* magic +4 is what CTL3D expects */
896 rtext
.right
-= checkBoxWidth
+ 4;
897 rbox
.left
= rbox
.right
- checkBoxWidth
;
901 rtext
.left
+= checkBoxWidth
+ 4;
902 rbox
.right
= checkBoxWidth
;
905 /* Since WM_ERASEBKGND does nothing, first prepare background */
906 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
907 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
911 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
913 /* Only adjust rbox when rtext is valid */
914 if (dtFlags
!= (UINT
)-1L)
916 rbox
.top
= rtext
.top
;
917 rbox
.bottom
= rtext
.bottom
;
920 /* Draw the check-box bitmap */
921 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
925 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
926 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
927 else if (state
& BUTTON_3STATE
) flags
= DFCS_BUTTON3STATE
;
928 else flags
= DFCS_BUTTONCHECK
;
930 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) flags
|= DFCS_CHECKED
;
931 if (state
& BUTTON_HIGHLIGHTED
) flags
|= DFCS_PUSHED
;
933 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
935 /* rbox must have the correct height */
936 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
938 if (style
& BS_TOP
) {
940 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
942 rbox
.top
-= -delta
/2 + 1;
943 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
945 } else if (style
& BS_BOTTOM
) {
947 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
949 rbox
.bottom
+= -delta
/2 + 1;
950 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
952 } else { /* Default */
954 int ofs
= (delta
/ 2);
955 rbox
.bottom
-= ofs
+ 1;
956 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
957 } else if (delta
< 0) {
958 int ofs
= (-delta
/ 2);
960 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
964 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
967 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
970 IntersectClipRect(hDC
, client
.left
, client
.top
, client
.right
, client
.bottom
);
972 if (action
== ODA_DRAWENTIRE
)
973 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
976 if ((action
== ODA_FOCUS
) ||
977 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
981 IntersectRect(&rtext
, &rtext
, &client
);
982 DrawFocusRect( hDC
, &rtext
);
987 /**********************************************************************
988 * BUTTON_CheckAutoRadioButton
990 * hwnd is checked, uncheck every other auto radio button in group
992 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
994 HWND parent
, sibling
, start
;
996 parent
= GetParent(hwnd
);
997 /* make sure that starting control is not disabled or invisible */
998 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1001 if (!sibling
) break;
1002 if ((hwnd
!= sibling
) &&
1003 ((GetWindowLongW( sibling
, GWL_STYLE
) & 0x0f) == BS_AUTORADIOBUTTON
))
1004 SendMessageW( sibling
, BM_SETCHECK
, BUTTON_UNCHECKED
, 0 );
1005 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1006 } while (sibling
!= start
);
1010 /**********************************************************************
1011 * Group Box Functions
1014 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1021 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1024 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1025 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1026 parent
= GetParent(hwnd
);
1027 if (!parent
) parent
= hwnd
;
1028 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1029 if (!hbr
) /* did the app forget to call defwindowproc ? */
1030 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1031 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1033 GetClientRect( hwnd
, &rc
);
1036 GetTextMetricsW (hDC
, &tm
);
1037 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1038 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1040 InflateRect(&rc
, -7, 1);
1041 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1043 if (dtFlags
== (UINT
)-1L)
1046 /* Because buttons have CS_PARENTDC class style, there is a chance
1047 * that label will be drawn out of client rect.
1048 * But Windows doesn't clip label's rect, so do I.
1051 /* There is 1-pixel marging at the left, right, and bottom */
1052 rc
.left
--; rc
.right
++; rc
.bottom
++;
1053 FillRect(hDC
, &rc
, hbr
);
1054 rc
.left
++; rc
.right
--; rc
.bottom
--;
1056 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1060 /**********************************************************************
1061 * User Button Functions
1064 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1069 LONG state
= get_button_state( hwnd
);
1072 if (action
== ODA_SELECT
) return;
1074 GetClientRect( hwnd
, &rc
);
1076 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1078 parent
= GetParent(hwnd
);
1079 if (!parent
) parent
= hwnd
;
1080 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1081 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1082 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1083 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1085 FillRect( hDC
, &rc
, hBrush
);
1086 if ((action
== ODA_FOCUS
) ||
1087 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
1088 DrawFocusRect( hDC
, &rc
);
1092 /**********************************************************************
1093 * Ownerdrawn Button Functions
1096 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1098 LONG state
= get_button_state( hwnd
);
1102 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1104 HFONT hFont
, hPrevFont
= 0;
1106 dis
.CtlType
= ODT_BUTTON
;
1109 dis
.itemAction
= action
;
1110 dis
.itemState
= ((state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
1111 ((state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
1112 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1113 dis
.hwndItem
= hwnd
;
1116 GetClientRect( hwnd
, &dis
.rcItem
);
1118 clipRegion
= CreateRectRgnIndirect(&dis
.rcItem
);
1119 if (GetClipRgn(hDC
, clipRegion
) != 1)
1121 DeleteObject(clipRegion
);
1124 clipRect
= dis
.rcItem
;
1125 DPtoLP(hDC
, (LPPOINT
) &clipRect
, 2);
1126 IntersectClipRect(hDC
, clipRect
.left
, clipRect
.top
, clipRect
.right
, clipRect
.bottom
);
1128 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1129 parent
= GetParent(hwnd
);
1130 if (!parent
) parent
= hwnd
;
1131 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1132 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1133 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1134 SelectClipRgn(hDC
, clipRegion
);