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
66 #define NDEBUG // turn off TRACEs
69 /* GetWindowLong offsets for window extra information */
70 #define STATE_GWL_OFFSET 0
71 #define HFONT_GWL_OFFSET (sizeof(LONG))
72 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
73 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
75 /* Button state values */
76 #define BUTTON_UNCHECKED 0x00
77 #define BUTTON_CHECKED 0x01
78 #define BUTTON_3STATE 0x02
79 #define BUTTON_HIGHLIGHTED 0x04
80 #define BUTTON_HASFOCUS 0x08
81 #define BUTTON_NSTATES 0x0F
82 /* undocumented flags */
83 #define BUTTON_BTNPRESSED 0x40
84 #define BUTTON_UNKNOWN2 0x20
85 #define BUTTON_UNKNOWN3 0x10
87 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
88 do { /* Notify parent which has created this button control */ \
89 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
90 SendMessageW(GetParent(hWnd), WM_COMMAND, \
91 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
95 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
96 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
97 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
98 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
99 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
100 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
101 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
102 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
103 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
105 #define MAX_BTN_TYPE 12
107 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
109 BUTTON_UNCHECKED
, /* BS_PUSHBUTTON */
110 BUTTON_UNCHECKED
, /* BS_DEFPUSHBUTTON */
111 BUTTON_CHECKED
, /* BS_CHECKBOX */
112 BUTTON_CHECKED
, /* BS_AUTOCHECKBOX */
113 BUTTON_CHECKED
, /* BS_RADIOBUTTON */
114 BUTTON_3STATE
, /* BS_3STATE */
115 BUTTON_3STATE
, /* BS_AUTO3STATE */
116 BUTTON_UNCHECKED
, /* BS_GROUPBOX */
117 BUTTON_UNCHECKED
, /* BS_USERBUTTON */
118 BUTTON_CHECKED
, /* BS_AUTORADIOBUTTON */
119 BUTTON_UNCHECKED
, /* Not defined */
120 BUTTON_UNCHECKED
/* BS_OWNERDRAW */
123 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
125 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
127 PB_Paint
, /* BS_PUSHBUTTON */
128 PB_Paint
, /* BS_DEFPUSHBUTTON */
129 CB_Paint
, /* BS_CHECKBOX */
130 CB_Paint
, /* BS_AUTOCHECKBOX */
131 CB_Paint
, /* BS_RADIOBUTTON */
132 CB_Paint
, /* BS_3STATE */
133 CB_Paint
, /* BS_AUTO3STATE */
134 GB_Paint
, /* BS_GROUPBOX */
135 UB_Paint
, /* BS_USERBUTTON */
136 CB_Paint
, /* BS_AUTORADIOBUTTON */
137 NULL
, /* Not defined */
138 OB_Paint
/* BS_OWNERDRAW */
141 static HBITMAP hbitmapCheckBoxes
= 0;
142 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
145 /*********************************************************************
146 * button class descriptor
148 const struct builtin_class_descr BUTTON_builtin_class
=
151 L
"Button", /* name */
152 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
153 ButtonWndProcW
, /* procW */
154 ButtonWndProcA
, /* procA */
155 NB_EXTRA_BYTES
, /* extra */
156 (LPWSTR
)IDC_ARROW
, /* cursor */
160 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
161 ButtonWndProcA
, /* procA */
162 ButtonWndProcW
, /* procW */
163 NB_EXTRA_BYTES
, /* extra */
164 IDC_ARROW
, /* cursor */
170 __inline
static LONG
get_button_state( HWND hwnd
)
172 return GetWindowLongW( hwnd
, STATE_GWL_OFFSET
);
175 __inline
static void set_button_state( HWND hwnd
, LONG state
)
177 SetWindowLongW( hwnd
, STATE_GWL_OFFSET
, state
);
180 __inline
static HFONT
get_button_font( HWND hwnd
)
182 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
185 __inline
static void set_button_font( HWND hwnd
, HFONT font
)
187 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
190 __inline
static UINT
get_button_type( LONG window_style
)
192 return (window_style
& 0x0f);
195 /* paint a button of any type */
196 __inline
static void paint_button( HWND hwnd
, LONG style
, UINT action
)
198 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
200 HDC hdc
= GetDC( hwnd
);
201 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
202 ReleaseDC( hwnd
, hdc
);
206 /* retrieve the button text; returned buffer must be freed by caller */
207 __inline
static WCHAR
*get_button_text( HWND hwnd
)
210 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
211 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
215 /***********************************************************************
216 * ButtonWndProc_common
218 static LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
219 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
223 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
224 UINT btn_type
= get_button_type( style
);
228 pt
.x
= LOWORD(lParam
);
229 pt
.y
= HIWORD(lParam
);
237 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
238 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
240 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
241 case BS_GROUPBOX
: return DLGC_STATIC
;
242 default: return DLGC_BUTTON
;
246 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
250 if (!hbitmapCheckBoxes
)
253 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
254 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
255 checkBoxWidth
= bmp
.bmWidth
/ 4;
256 checkBoxHeight
= bmp
.bmHeight
/ 3;
258 if (btn_type
>= MAX_BTN_TYPE
)
259 return -1; /* abort */
260 set_button_state( hWnd
, BUTTON_UNCHECKED
);
264 if (btn_type
== BS_OWNERDRAW
)
266 HDC hdc
= (HDC
)wParam
;
269 HWND parent
= GetParent(hWnd
);
270 if (!parent
) parent
= hWnd
;
271 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
272 if (!hBrush
) /* did the app forget to call defwindowproc ? */
273 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
274 (WPARAM
)hdc
, (LPARAM
)hWnd
);
275 GetClientRect(hWnd
, &rc
);
276 FillRect(hdc
, &rc
, hBrush
);
282 if (btnPaintFunc
[btn_type
])
285 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
286 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
287 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
288 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
289 if( !wParam
) EndPaint( hWnd
, &ps
);
294 if (wParam
== VK_SPACE
)
296 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
297 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
301 case WM_LBUTTONDBLCLK
:
302 if(style
& BS_NOTIFY
||
303 btn_type
== BS_RADIOBUTTON
||
304 btn_type
== BS_USERBUTTON
||
305 btn_type
== BS_OWNERDRAW
)
307 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
314 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
315 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
319 if (wParam
!= VK_SPACE
)
323 state
= get_button_state( hWnd
);
324 if (!(state
& BUTTON_BTNPRESSED
)) break;
325 state
&= BUTTON_NSTATES
;
326 set_button_state( hWnd
, state
);
327 if (!(state
& BUTTON_HIGHLIGHTED
))
332 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
334 GetClientRect( hWnd
, &rect
);
335 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
337 state
= get_button_state( hWnd
);
340 case BS_AUTOCHECKBOX
:
341 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BUTTON_CHECKED
), 0 );
343 case BS_AUTORADIOBUTTON
:
344 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
347 SendMessageW( hWnd
, BM_SETCHECK
,
348 (state
& BUTTON_3STATE
) ? 0 : ((state
& 3) + 1), 0 );
351 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
355 case WM_CAPTURECHANGED
:
356 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
357 state
= get_button_state( hWnd
);
358 if (state
& BUTTON_BTNPRESSED
)
360 state
&= BUTTON_NSTATES
;
361 set_button_state( hWnd
, state
);
362 if (state
& BUTTON_HIGHLIGHTED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
367 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
369 GetClientRect( hWnd
, &rect
);
370 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
376 /* Clear an old text here as Windows does */
377 HDC hdc
= GetDC(hWnd
);
380 HWND parent
= GetParent(hWnd
);
382 if (!parent
) parent
= hWnd
;
383 hbrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
384 (WPARAM
)hdc
, (LPARAM
)hWnd
);
385 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
386 hbrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
387 (WPARAM
)hdc
, (LPARAM
)hWnd
);
389 GetClientRect(hWnd
, &client
);
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
);
398 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
399 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
400 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
401 InvalidateRect( hWnd
, NULL
, TRUE
);
403 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
404 return 1; /* success. FIXME: check text length */
408 set_button_font( hWnd
, (HFONT
)wParam
);
409 if (lParam
) paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
413 return (LRESULT
)get_button_font( hWnd
);
416 TRACE("WM_SETFOCUS %p\n",hWnd
);
417 set_button_state( hWnd
, get_button_state(hWnd
) | BUTTON_HASFOCUS
);
418 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
419 if (style
& BS_NOTIFY
)
420 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
424 TRACE("WM_KILLFOCUS %p\n",hWnd
);
425 state
= get_button_state( hWnd
);
426 set_button_state( hWnd
, state
& ~BUTTON_HASFOCUS
);
427 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
429 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
431 if (style
& BS_NOTIFY
)
432 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
436 case WM_SYSCOLORCHANGE
:
437 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
);
484 return get_button_state( hWnd
) & 3;
490 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
491 state
= get_button_state( hWnd
);
492 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
494 if (wParam
) style
|= WS_TABSTOP
;
495 else style
&= ~WS_TABSTOP
;
496 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
498 if ((state
& 3) != wParam
)
500 set_button_state( hWnd
, (state
& ~3) | wParam
);
501 paint_button( hWnd
, btn_type
, ODA_SELECT
);
503 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
) && (style
& WS_CHILD
))
504 BUTTON_CheckAutoRadioButton( hWnd
);
511 return get_button_state( hWnd
);
517 state
= get_button_state( hWnd
);
520 if (state
& BUTTON_HIGHLIGHTED
) break;
521 set_button_state( hWnd
, state
| BUTTON_HIGHLIGHTED
);
525 if (!(state
& BUTTON_HIGHLIGHTED
)) break;
526 set_button_state( hWnd
, state
& ~BUTTON_HIGHLIGHTED
);
528 paint_button( hWnd
, btn_type
, ODA_SELECT
);
532 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
535 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
536 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
541 /***********************************************************************
543 * The button window procedure. This is just a wrapper which locks
544 * the passed HWND and calls the real window procedure (with a WND*
545 * pointer pointing to the locked windowstructure).
547 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
549 if (!IsWindow( hWnd
)) return 0;
550 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, TRUE
);
554 /***********************************************************************
557 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
559 if (!IsWindow( hWnd
)) return 0;
560 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, FALSE
);
564 /**********************************************************************
565 * Convert button styles to flags used by DrawText.
566 * TODO: handle WS_EX_RIGHT extended style.
568 static UINT
BUTTON_BStoDT(DWORD style
)
570 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
572 /* "Convert" pushlike buttons to pushbuttons */
573 if (style
& BS_PUSHLIKE
)
576 if (!(style
& BS_MULTILINE
))
577 dtStyle
|= DT_SINGLELINE
;
579 dtStyle
|= DT_WORDBREAK
;
581 switch (style
& BS_CENTER
)
583 case BS_LEFT
: /* DT_LEFT is 0 */ break;
584 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
585 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
587 /* Pushbutton's text is centered by default */
588 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
589 /* all other flavours have left aligned text */
592 /* DrawText ignores vertical alignment for multiline text,
593 * but we use these flags to align label manualy.
595 if (get_button_type(style
) != BS_GROUPBOX
)
597 switch (style
& BS_VCENTER
)
599 case BS_TOP
: /* DT_TOP is 0 */ break;
600 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
601 case BS_VCENTER
: /* fall through */
602 default: dtStyle
|= DT_VCENTER
; break;
606 /* GroupBox's text is always single line and is top aligned. */
607 dtStyle
|= DT_SINGLELINE
;
612 /**********************************************************************
613 * BUTTON_CalcLabelRect
615 * Calculates label's rectangle depending on button style.
617 * Returns flags to be passed to DrawText.
618 * Calculated rectangle doesn't take into account button state
619 * (pushed, etc.). If there is nothing to draw (no text/image) output
620 * rectangle is empty, and return value is (UINT)-1.
622 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
624 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
628 UINT dtStyle
= BUTTON_BStoDT(style
);
632 /* Calculate label rectangle according to label type */
633 switch (style
& (BS_ICON
|BS_BITMAP
))
636 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
639 HeapFree( GetProcessHeap(), 0, text
);
642 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
643 HeapFree( GetProcessHeap(), 0, text
);
647 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
650 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
652 r
.right
= r
.left
+ bm
.bmWidth
;
653 r
.bottom
= r
.top
+ bm
.bmHeight
;
655 DeleteObject(iconInfo
.hbmColor
);
656 DeleteObject(iconInfo
.hbmMask
);
660 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
663 r
.right
= r
.left
+ bm
.bmWidth
;
664 r
.bottom
= r
.top
+ bm
.bmHeight
;
671 return (UINT
)(LONG
)-1;
674 /* Position label inside bounding rectangle according to
675 * alignment flags. (calculated rect is always left-top aligned).
676 * If label is aligned to any side - shift label in opposite
677 * direction to leave extra space for focus rectangle.
679 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
681 case DT_LEFT
: r
.left
++; r
.right
++; break;
682 case DT_CENTER
: n
= r
.right
- r
.left
;
683 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
684 r
.right
= r
.left
+ n
; break;
685 case DT_RIGHT
: n
= r
.right
- r
.left
;
686 r
.right
= rc
->right
- 1;
687 r
.left
= r
.right
- n
;
691 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
693 case DT_TOP
: r
.top
++; r
.bottom
++; break;
694 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
695 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
696 r
.bottom
= r
.top
+ n
; break;
697 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
698 r
.bottom
= rc
->bottom
- 1;
699 r
.top
= r
.bottom
- n
;
708 /**********************************************************************
709 * BUTTON_DrawTextCallback
711 * Callback function used by DrawStateW function.
713 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
721 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
726 /**********************************************************************
729 * Common function for drawing button label.
731 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, RECT
*rc
)
733 DRAWSTATEPROC lpOutputProc
= NULL
;
737 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
738 LONG state
= get_button_state( hwnd
);
739 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
742 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
743 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
744 * I don't have Win31 on hand to verify that, so I leave it as is.
747 if ((style
& BS_PUSHLIKE
) && (state
& BUTTON_3STATE
))
749 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
753 switch (style
& (BS_ICON
|BS_BITMAP
))
756 /* DST_COMPLEX -- is 0 */
757 lpOutputProc
= BUTTON_DrawTextCallback
;
758 if (!(text
= get_button_text( hwnd
))) return;
760 wp
= (WPARAM
)dtFlags
;
765 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
770 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
777 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
778 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
779 HeapFree( GetProcessHeap(), 0, text
);
782 /**********************************************************************
783 * Push Button Functions
785 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
787 RECT rc
, focus_rect
, r
;
788 UINT dtFlags
, uState
;
792 COLORREF oldTxtColor
;
794 LONG state
= get_button_state( hwnd
);
795 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
796 BOOL pushedState
= (state
& BUTTON_HIGHLIGHTED
);
799 GetClientRect( hwnd
, &rc
);
801 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
802 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
803 parent
= GetParent(hwnd
);
804 if (!parent
) parent
= hwnd
;
805 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
807 hOldPen
= (HPEN
)SelectObject(hDC
, GetSysColorPen(COLOR_WINDOWFRAME
));
809 hOldPen
= (HPEN
)SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
811 hOldBrush
=(HBRUSH
)SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
812 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
814 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
816 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
817 InflateRect( &rc
, -1, -1 );
820 uState
= DFCS_BUTTONPUSH
| DFCS_ADJUSTRECT
;
824 else if (pushedState
)
826 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
829 uState
|= DFCS_PUSHED
;
832 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
))
833 uState
|= DFCS_CHECKED
;
835 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
839 /* draw button label */
841 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
843 if (dtFlags
== (UINT
)-1L)
847 OffsetRect(&r
, 1, 1);
849 IntersectClipRect(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
851 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
853 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
855 SetTextColor( hDC
, oldTxtColor
);
857 if (state
& BUTTON_HASFOCUS
)
859 InflateRect( &focus_rect
, -1, -1 );
860 IntersectRect(&focus_rect
, &focus_rect
, &rc
);
861 DrawFocusRect( hDC
, &focus_rect
);
865 SelectObject( hDC
, hOldPen
);
866 SelectObject( hDC
, hOldBrush
);
867 SetBkMode(hDC
, oldBkMode
);
870 /**********************************************************************
871 * Check Box & Radio Button Functions
874 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
876 RECT rbox
, rtext
, client
;
881 LONG state
= get_button_state( hwnd
);
882 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
885 if (style
& BS_PUSHLIKE
)
887 PB_Paint( hwnd
, hDC
, action
);
891 GetClientRect(hwnd
, &client
);
892 rbox
= rtext
= client
;
894 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
896 parent
= GetParent(hwnd
);
897 if (!parent
) parent
= hwnd
;
898 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
899 (WPARAM
)hDC
, (LPARAM
)hwnd
);
900 if (!hBrush
) /* did the app forget to call defwindowproc ? */
901 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
902 (WPARAM
)hDC
, (LPARAM
)hwnd
);
904 if (style
& BS_LEFTTEXT
)
906 /* magic +4 is what CTL3D expects */
908 rtext
.right
-= checkBoxWidth
+ 4;
909 rbox
.left
= rbox
.right
- checkBoxWidth
;
913 rtext
.left
+= checkBoxWidth
+ 4;
914 rbox
.right
= checkBoxWidth
;
917 /* Since WM_ERASEBKGND does nothing, first prepare background */
918 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
919 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
923 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
925 /* Only adjust rbox when rtext is valid */
926 if (dtFlags
!= (UINT
)-1L)
928 rbox
.top
= rtext
.top
;
929 rbox
.bottom
= rtext
.bottom
;
932 /* Draw the check-box bitmap */
933 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
937 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
938 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
939 else if (state
& BUTTON_3STATE
) flags
= DFCS_BUTTON3STATE
;
940 else flags
= DFCS_BUTTONCHECK
;
942 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) flags
|= DFCS_CHECKED
;
943 if (state
& BUTTON_HIGHLIGHTED
) flags
|= DFCS_PUSHED
;
945 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
947 /* rbox must have the correct height */
948 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
950 if (style
& BS_TOP
) {
952 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
954 rbox
.top
-= -delta
/2 + 1;
955 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
957 } else if (style
& BS_BOTTOM
) {
959 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
961 rbox
.bottom
+= -delta
/2 + 1;
962 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
964 } else { /* Default */
966 int ofs
= (delta
/ 2);
967 rbox
.bottom
-= ofs
+ 1;
968 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
969 } else if (delta
< 0) {
970 int ofs
= (-delta
/ 2);
972 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
976 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
979 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
982 IntersectClipRect(hDC
, client
.left
, client
.top
, client
.right
, client
.bottom
);
984 if (action
== ODA_DRAWENTIRE
)
985 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
988 if ((action
== ODA_FOCUS
) ||
989 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
993 IntersectRect(&rtext
, &rtext
, &client
);
994 DrawFocusRect( hDC
, &rtext
);
999 /**********************************************************************
1000 * BUTTON_CheckAutoRadioButton
1002 * hwnd is checked, uncheck every other auto radio button in group
1004 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1006 HWND parent
, sibling
, start
;
1008 parent
= GetParent(hwnd
);
1009 /* make sure that starting control is not disabled or invisible */
1010 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1013 if (!sibling
) break;
1014 if ((hwnd
!= sibling
) &&
1015 ((GetWindowLongW( sibling
, GWL_STYLE
) & 0x0f) == BS_AUTORADIOBUTTON
))
1016 SendMessageW( sibling
, BM_SETCHECK
, BUTTON_UNCHECKED
, 0 );
1017 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1018 } while (sibling
!= start
);
1022 /**********************************************************************
1023 * Group Box Functions
1026 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1033 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1036 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1037 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1038 parent
= GetParent(hwnd
);
1039 if (!parent
) parent
= hwnd
;
1040 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1041 if (!hbr
) /* did the app forget to call defwindowproc ? */
1042 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1043 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1045 GetClientRect( hwnd
, &rc
);
1048 GetTextMetricsW (hDC
, &tm
);
1049 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1050 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1052 InflateRect(&rc
, -7, 1);
1053 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1055 if (dtFlags
== (UINT
)-1L)
1058 /* Because buttons have CS_PARENTDC class style, there is a chance
1059 * that label will be drawn out of client rect.
1060 * But Windows doesn't clip label's rect, so do I.
1063 /* There is 1-pixel marging at the left, right, and bottom */
1064 rc
.left
--; rc
.right
++; rc
.bottom
++;
1065 FillRect(hDC
, &rc
, hbr
);
1066 rc
.left
++; rc
.right
--; rc
.bottom
--;
1068 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1072 /**********************************************************************
1073 * User Button Functions
1076 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1081 LONG state
= get_button_state( hwnd
);
1084 if (action
== ODA_SELECT
) return;
1086 GetClientRect( hwnd
, &rc
);
1088 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1090 parent
= GetParent(hwnd
);
1091 if (!parent
) parent
= hwnd
;
1092 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1093 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1094 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1095 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1097 FillRect( hDC
, &rc
, hBrush
);
1098 if ((action
== ODA_FOCUS
) ||
1099 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
1100 DrawFocusRect( hDC
, &rc
);
1104 /**********************************************************************
1105 * Ownerdrawn Button Functions
1108 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1110 LONG state
= get_button_state( hwnd
);
1114 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1116 HFONT hFont
, hPrevFont
= 0;
1118 dis
.CtlType
= ODT_BUTTON
;
1121 dis
.itemAction
= action
;
1122 dis
.itemState
= ((state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
1123 ((state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
1124 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1125 dis
.hwndItem
= hwnd
;
1128 GetClientRect( hwnd
, &dis
.rcItem
);
1130 clipRegion
= CreateRectRgnIndirect(&dis
.rcItem
);
1131 if (GetClipRgn(hDC
, clipRegion
) != 1)
1133 DeleteObject(clipRegion
);
1136 clipRect
= dis
.rcItem
;
1137 DPtoLP(hDC
, (LPPOINT
) &clipRect
, 2);
1138 IntersectClipRect(hDC
, clipRect
.left
, clipRect
.top
, clipRect
.right
, clipRect
.bottom
);
1140 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1141 parent
= GetParent(hwnd
);
1142 if (!parent
) parent
= hwnd
;
1143 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1144 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1145 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1146 SelectClipRgn(hDC
, clipRegion
);