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>
70 /* GetWindowLong offsets for window extra information */
71 #define STATE_GWL_OFFSET 0
72 #define HFONT_GWL_OFFSET (sizeof(LONG))
73 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
74 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
76 /* Button state values */
77 #define BUTTON_UNCHECKED 0x00
78 #define BUTTON_CHECKED 0x01
79 #define BUTTON_3STATE 0x02
80 #define BUTTON_HIGHLIGHTED 0x04
81 #define BUTTON_HASFOCUS 0x08
82 #define BUTTON_NSTATES 0x0F
83 /* undocumented flags */
84 #define BUTTON_BTNPRESSED 0x40
85 #define BUTTON_UNKNOWN2 0x20
86 #define BUTTON_UNKNOWN3 0x10
88 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
89 do { /* Notify parent which has created this button control */ \
90 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
91 SendMessageW(GetParent(hWnd), WM_COMMAND, \
92 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
96 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
97 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
98 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
99 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
100 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
101 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
102 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
103 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
104 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
106 #define MAX_BTN_TYPE 12
108 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
110 BUTTON_UNCHECKED
, /* BS_PUSHBUTTON */
111 BUTTON_UNCHECKED
, /* BS_DEFPUSHBUTTON */
112 BUTTON_CHECKED
, /* BS_CHECKBOX */
113 BUTTON_CHECKED
, /* BS_AUTOCHECKBOX */
114 BUTTON_CHECKED
, /* BS_RADIOBUTTON */
115 BUTTON_3STATE
, /* BS_3STATE */
116 BUTTON_3STATE
, /* BS_AUTO3STATE */
117 BUTTON_UNCHECKED
, /* BS_GROUPBOX */
118 BUTTON_UNCHECKED
, /* BS_USERBUTTON */
119 BUTTON_CHECKED
, /* BS_AUTORADIOBUTTON */
120 BUTTON_UNCHECKED
, /* Not defined */
121 BUTTON_UNCHECKED
/* BS_OWNERDRAW */
124 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
126 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
128 PB_Paint
, /* BS_PUSHBUTTON */
129 PB_Paint
, /* BS_DEFPUSHBUTTON */
130 CB_Paint
, /* BS_CHECKBOX */
131 CB_Paint
, /* BS_AUTOCHECKBOX */
132 CB_Paint
, /* BS_RADIOBUTTON */
133 CB_Paint
, /* BS_3STATE */
134 CB_Paint
, /* BS_AUTO3STATE */
135 GB_Paint
, /* BS_GROUPBOX */
136 UB_Paint
, /* BS_USERBUTTON */
137 CB_Paint
, /* BS_AUTORADIOBUTTON */
138 NULL
, /* Not defined */
139 OB_Paint
/* BS_OWNERDRAW */
142 static HBITMAP hbitmapCheckBoxes
= 0;
143 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
146 /*********************************************************************
147 * button class descriptor
149 const struct builtin_class_descr BUTTON_builtin_class
=
152 L
"Button", /* name */
153 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
154 ButtonWndProcW
, /* procW */
155 ButtonWndProcA
, /* procA */
156 NB_EXTRA_BYTES
, /* extra */
157 (LPWSTR
)IDC_ARROW
, /* cursor */
161 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
162 ButtonWndProcA
, /* procA */
163 ButtonWndProcW
, /* procW */
164 NB_EXTRA_BYTES
, /* extra */
165 IDC_ARROW
, /* cursor */
171 __inline
static LONG
get_button_state( HWND hwnd
)
173 return GetWindowLongW( hwnd
, STATE_GWL_OFFSET
);
176 __inline
static void set_button_state( HWND hwnd
, LONG state
)
178 SetWindowLongW( hwnd
, STATE_GWL_OFFSET
, state
);
181 __inline
static HFONT
get_button_font( HWND hwnd
)
183 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
186 __inline
static void set_button_font( HWND hwnd
, HFONT font
)
188 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
191 __inline
static UINT
get_button_type( LONG window_style
)
193 return (window_style
& 0x0f);
196 /* paint a button of any type */
197 __inline
static void paint_button( HWND hwnd
, LONG style
, UINT action
)
199 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
201 HDC hdc
= GetDC( hwnd
);
202 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
203 ReleaseDC( hwnd
, hdc
);
207 /* retrieve the button text; returned buffer must be freed by caller */
208 __inline
static WCHAR
*get_button_text( HWND hwnd
)
211 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
212 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
216 /***********************************************************************
217 * ButtonWndProc_common
219 static LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
220 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
224 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
225 UINT btn_type
= get_button_type( style
);
229 pt
.x
= LOWORD(lParam
);
230 pt
.y
= HIWORD(lParam
);
238 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
239 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
241 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
242 case BS_GROUPBOX
: return DLGC_STATIC
;
243 default: return DLGC_BUTTON
;
247 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
251 if (!hbitmapCheckBoxes
)
254 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
255 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
256 checkBoxWidth
= bmp
.bmWidth
/ 4;
257 checkBoxHeight
= bmp
.bmHeight
/ 3;
259 if (btn_type
>= MAX_BTN_TYPE
)
260 return -1; /* abort */
261 set_button_state( hWnd
, BUTTON_UNCHECKED
);
265 if (btn_type
== BS_OWNERDRAW
)
267 HDC hdc
= (HDC
)wParam
;
270 HWND parent
= GetParent(hWnd
);
271 if (!parent
) parent
= hWnd
;
272 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
273 if (!hBrush
) /* did the app forget to call defwindowproc ? */
274 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
275 (WPARAM
)hdc
, (LPARAM
)hWnd
);
276 GetClientRect(hWnd
, &rc
);
277 FillRect(hdc
, &rc
, hBrush
);
283 if (btnPaintFunc
[btn_type
])
286 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
287 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
288 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
289 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
290 if( !wParam
) EndPaint( hWnd
, &ps
);
295 if (wParam
== VK_SPACE
)
297 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
298 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
302 case WM_LBUTTONDBLCLK
:
303 if(style
& BS_NOTIFY
||
304 btn_type
== BS_RADIOBUTTON
||
305 btn_type
== BS_USERBUTTON
||
306 btn_type
== BS_OWNERDRAW
)
308 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
315 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
316 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
320 if (wParam
!= VK_SPACE
)
324 state
= get_button_state( hWnd
);
325 if (!(state
& BUTTON_BTNPRESSED
)) break;
326 state
&= BUTTON_NSTATES
;
327 set_button_state( hWnd
, state
);
328 if (!(state
& BUTTON_HIGHLIGHTED
))
333 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
335 GetClientRect( hWnd
, &rect
);
336 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
338 state
= get_button_state( hWnd
);
341 case BS_AUTOCHECKBOX
:
342 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BUTTON_CHECKED
), 0 );
344 case BS_AUTORADIOBUTTON
:
345 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
348 SendMessageW( hWnd
, BM_SETCHECK
,
349 (state
& BUTTON_3STATE
) ? 0 : ((state
& 3) + 1), 0 );
352 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
356 case WM_CAPTURECHANGED
:
357 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
358 state
= get_button_state( hWnd
);
359 if (state
& BUTTON_BTNPRESSED
)
361 state
&= BUTTON_NSTATES
;
362 set_button_state( hWnd
, state
);
363 if (state
& BUTTON_HIGHLIGHTED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
368 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
370 GetClientRect( hWnd
, &rect
);
371 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
377 /* Clear an old text here as Windows does */
378 HDC hdc
= GetDC(hWnd
);
381 HWND parent
= GetParent(hWnd
);
383 if (!parent
) parent
= hWnd
;
384 hbrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
385 (WPARAM
)hdc
, (LPARAM
)hWnd
);
386 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
387 hbrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
388 (WPARAM
)hdc
, (LPARAM
)hWnd
);
390 GetClientRect(hWnd
, &client
);
392 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
393 /* Clip by client rect bounds */
394 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
395 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
396 FillRect(hdc
, &rc
, hbrush
);
397 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
) paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
414 return (LRESULT
)get_button_font( hWnd
);
417 TRACE("WM_SETFOCUS %p\n",hWnd
);
418 set_button_state( hWnd
, get_button_state(hWnd
) | BUTTON_HASFOCUS
);
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
& ~BUTTON_HASFOCUS
);
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
);
437 case WM_SYSCOLORCHANGE
:
438 InvalidateRect( hWnd
, NULL
, FALSE
);
445 if ((wParam
& 0x0f) >= MAX_BTN_TYPE
) break;
446 btn_type
= wParam
& 0x0f;
447 style
= (style
& ~0x0f) | btn_type
;
448 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
450 /* Only redraw if lParam flag is set.*/
452 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
457 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
458 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
462 /* Check that image format matches button style */
463 switch (style
& (BS_BITMAP
|BS_ICON
))
466 if (wParam
!= IMAGE_BITMAP
) return 0;
469 if (wParam
!= IMAGE_ICON
) return 0;
474 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
475 InvalidateRect( hWnd
, NULL
, FALSE
);
476 return (LRESULT
)oldHbitmap
;
479 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
485 return get_button_state( hWnd
) & 3;
491 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
492 state
= get_button_state( hWnd
);
493 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
495 if (wParam
) style
|= WS_TABSTOP
;
496 else style
&= ~WS_TABSTOP
;
497 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
499 if ((state
& 3) != wParam
)
501 set_button_state( hWnd
, (state
& ~3) | wParam
);
502 paint_button( hWnd
, btn_type
, ODA_SELECT
);
504 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
) && (style
& WS_CHILD
))
505 BUTTON_CheckAutoRadioButton( hWnd
);
512 return get_button_state( hWnd
);
518 state
= get_button_state( hWnd
);
521 if (state
& BUTTON_HIGHLIGHTED
) break;
522 set_button_state( hWnd
, state
| BUTTON_HIGHLIGHTED
);
526 if (!(state
& BUTTON_HIGHLIGHTED
)) break;
527 set_button_state( hWnd
, state
& ~BUTTON_HIGHLIGHTED
);
529 paint_button( hWnd
, btn_type
, ODA_SELECT
);
533 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
536 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
537 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
542 /***********************************************************************
544 * The button window procedure. This is just a wrapper which locks
545 * the passed HWND and calls the real window procedure (with a WND*
546 * pointer pointing to the locked windowstructure).
548 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
550 if (!IsWindow( hWnd
)) return 0;
551 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, TRUE
);
555 /***********************************************************************
558 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
560 if (!IsWindow( hWnd
)) return 0;
561 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, FALSE
);
565 /**********************************************************************
566 * Convert button styles to flags used by DrawText.
567 * TODO: handle WS_EX_RIGHT extended style.
569 static UINT
BUTTON_BStoDT(DWORD style
)
571 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
573 /* "Convert" pushlike buttons to pushbuttons */
574 if (style
& BS_PUSHLIKE
)
577 if (!(style
& BS_MULTILINE
))
578 dtStyle
|= DT_SINGLELINE
;
580 dtStyle
|= DT_WORDBREAK
;
582 switch (style
& BS_CENTER
)
584 case BS_LEFT
: /* DT_LEFT is 0 */ break;
585 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
586 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
588 /* Pushbutton's text is centered by default */
589 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
590 /* all other flavours have left aligned text */
593 /* DrawText ignores vertical alignment for multiline text,
594 * but we use these flags to align label manualy.
596 if (get_button_type(style
) != BS_GROUPBOX
)
598 switch (style
& BS_VCENTER
)
600 case BS_TOP
: /* DT_TOP is 0 */ break;
601 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
602 case BS_VCENTER
: /* fall through */
603 default: dtStyle
|= DT_VCENTER
; break;
607 /* GroupBox's text is always single line and is top aligned. */
608 dtStyle
|= DT_SINGLELINE
;
613 /**********************************************************************
614 * BUTTON_CalcLabelRect
616 * Calculates label's rectangle depending on button style.
618 * Returns flags to be passed to DrawText.
619 * Calculated rectangle doesn't take into account button state
620 * (pushed, etc.). If there is nothing to draw (no text/image) output
621 * rectangle is empty, and return value is (UINT)-1.
623 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
625 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
629 UINT dtStyle
= BUTTON_BStoDT(style
);
633 /* Calculate label rectangle according to label type */
634 switch (style
& (BS_ICON
|BS_BITMAP
))
637 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
640 HeapFree( GetProcessHeap(), 0, text
);
643 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
644 HeapFree( GetProcessHeap(), 0, text
);
648 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
651 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
653 r
.right
= r
.left
+ bm
.bmWidth
;
654 r
.bottom
= r
.top
+ bm
.bmHeight
;
656 DeleteObject(iconInfo
.hbmColor
);
657 DeleteObject(iconInfo
.hbmMask
);
661 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
664 r
.right
= r
.left
+ bm
.bmWidth
;
665 r
.bottom
= r
.top
+ bm
.bmHeight
;
672 return (UINT
)(LONG
)-1;
675 /* Position label inside bounding rectangle according to
676 * alignment flags. (calculated rect is always left-top aligned).
677 * If label is aligned to any side - shift label in opposite
678 * direction to leave extra space for focus rectangle.
680 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
682 case DT_LEFT
: r
.left
++; r
.right
++; break;
683 case DT_CENTER
: n
= r
.right
- r
.left
;
684 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
685 r
.right
= r
.left
+ n
; break;
686 case DT_RIGHT
: n
= r
.right
- r
.left
;
687 r
.right
= rc
->right
- 1;
688 r
.left
= r
.right
- n
;
692 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
694 case DT_TOP
: r
.top
++; r
.bottom
++; break;
695 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
696 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
697 r
.bottom
= r
.top
+ n
; break;
698 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
699 r
.bottom
= rc
->bottom
- 1;
700 r
.top
= r
.bottom
- n
;
709 /**********************************************************************
710 * BUTTON_DrawTextCallback
712 * Callback function used by DrawStateW function.
714 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
722 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
727 /**********************************************************************
730 * Common function for drawing button label.
732 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, RECT
*rc
)
734 DRAWSTATEPROC lpOutputProc
= NULL
;
738 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
739 LONG state
= get_button_state( hwnd
);
740 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
743 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
744 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
745 * I don't have Win31 on hand to verify that, so I leave it as is.
748 if ((style
& BS_PUSHLIKE
) && (state
& BUTTON_3STATE
))
750 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
754 switch (style
& (BS_ICON
|BS_BITMAP
))
757 /* DST_COMPLEX -- is 0 */
758 lpOutputProc
= BUTTON_DrawTextCallback
;
759 if (!(text
= get_button_text( hwnd
))) return;
761 wp
= (WPARAM
)dtFlags
;
766 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
771 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
778 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
779 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
780 HeapFree( GetProcessHeap(), 0, text
);
783 /**********************************************************************
784 * Push Button Functions
786 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
788 RECT rc
, focus_rect
, r
;
789 UINT dtFlags
, uState
;
793 COLORREF oldTxtColor
;
795 LONG state
= get_button_state( hwnd
);
796 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
797 BOOL pushedState
= (state
& BUTTON_HIGHLIGHTED
);
800 GetClientRect( hwnd
, &rc
);
802 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
803 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
804 parent
= GetParent(hwnd
);
805 if (!parent
) parent
= hwnd
;
806 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
808 hOldPen
= (HPEN
)SelectObject(hDC
, GetSysColorPen(COLOR_WINDOWFRAME
));
810 hOldPen
= (HPEN
)SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
812 hOldBrush
=(HBRUSH
)SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
813 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
815 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
817 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
818 InflateRect( &rc
, -1, -1 );
821 uState
= DFCS_BUTTONPUSH
| DFCS_ADJUSTRECT
;
825 else if (pushedState
)
827 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
830 uState
|= DFCS_PUSHED
;
833 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
))
834 uState
|= DFCS_CHECKED
;
836 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
840 /* draw button label */
842 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
844 if (dtFlags
== (UINT
)-1L)
848 OffsetRect(&r
, 1, 1);
850 IntersectClipRect(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
852 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
854 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
856 SetTextColor( hDC
, oldTxtColor
);
858 if (state
& BUTTON_HASFOCUS
)
860 InflateRect( &focus_rect
, -1, -1 );
861 IntersectRect(&focus_rect
, &focus_rect
, &rc
);
862 DrawFocusRect( hDC
, &focus_rect
);
866 SelectObject( hDC
, hOldPen
);
867 SelectObject( hDC
, hOldBrush
);
868 SetBkMode(hDC
, oldBkMode
);
871 /**********************************************************************
872 * Check Box & Radio Button Functions
875 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
877 RECT rbox
, rtext
, client
;
882 LONG state
= get_button_state( hwnd
);
883 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
886 if (style
& BS_PUSHLIKE
)
888 PB_Paint( hwnd
, hDC
, action
);
892 GetClientRect(hwnd
, &client
);
893 rbox
= rtext
= client
;
895 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
897 parent
= GetParent(hwnd
);
898 if (!parent
) parent
= hwnd
;
899 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
900 (WPARAM
)hDC
, (LPARAM
)hwnd
);
901 if (!hBrush
) /* did the app forget to call defwindowproc ? */
902 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
903 (WPARAM
)hDC
, (LPARAM
)hwnd
);
905 if (style
& BS_LEFTTEXT
)
907 /* magic +4 is what CTL3D expects */
909 rtext
.right
-= checkBoxWidth
+ 4;
910 rbox
.left
= rbox
.right
- checkBoxWidth
;
914 rtext
.left
+= checkBoxWidth
+ 4;
915 rbox
.right
= checkBoxWidth
;
918 /* Since WM_ERASEBKGND does nothing, first prepare background */
919 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
920 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
924 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
926 /* Only adjust rbox when rtext is valid */
927 if (dtFlags
!= (UINT
)-1L)
929 rbox
.top
= rtext
.top
;
930 rbox
.bottom
= rtext
.bottom
;
933 /* Draw the check-box bitmap */
934 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
938 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
939 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
940 else if (state
& BUTTON_3STATE
) flags
= DFCS_BUTTON3STATE
;
941 else flags
= DFCS_BUTTONCHECK
;
943 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) flags
|= DFCS_CHECKED
;
944 if (state
& BUTTON_HIGHLIGHTED
) flags
|= DFCS_PUSHED
;
946 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
948 /* rbox must have the correct height */
949 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
951 if (style
& BS_TOP
) {
953 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
955 rbox
.top
-= -delta
/2 + 1;
956 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
958 } else if (style
& BS_BOTTOM
) {
960 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
962 rbox
.bottom
+= -delta
/2 + 1;
963 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
965 } else { /* Default */
967 int ofs
= (delta
/ 2);
968 rbox
.bottom
-= ofs
+ 1;
969 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
970 } else if (delta
< 0) {
971 int ofs
= (-delta
/ 2);
973 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
977 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
980 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
983 IntersectClipRect(hDC
, client
.left
, client
.top
, client
.right
, client
.bottom
);
985 if (action
== ODA_DRAWENTIRE
)
986 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
989 if ((action
== ODA_FOCUS
) ||
990 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
994 IntersectRect(&rtext
, &rtext
, &client
);
995 DrawFocusRect( hDC
, &rtext
);
1000 /**********************************************************************
1001 * BUTTON_CheckAutoRadioButton
1003 * hwnd is checked, uncheck every other auto radio button in group
1005 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1007 HWND parent
, sibling
, start
;
1009 parent
= GetParent(hwnd
);
1010 /* make sure that starting control is not disabled or invisible */
1011 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1014 if (!sibling
) break;
1015 if ((hwnd
!= sibling
) &&
1016 ((GetWindowLongW( sibling
, GWL_STYLE
) & 0x0f) == BS_AUTORADIOBUTTON
))
1017 SendMessageW( sibling
, BM_SETCHECK
, BUTTON_UNCHECKED
, 0 );
1018 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1019 } while (sibling
!= start
);
1023 /**********************************************************************
1024 * Group Box Functions
1027 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1034 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1037 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1038 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1039 parent
= GetParent(hwnd
);
1040 if (!parent
) parent
= hwnd
;
1041 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1042 if (!hbr
) /* did the app forget to call defwindowproc ? */
1043 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1044 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1046 GetClientRect( hwnd
, &rc
);
1049 GetTextMetricsW (hDC
, &tm
);
1050 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1051 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1053 InflateRect(&rc
, -7, 1);
1054 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1056 if (dtFlags
== (UINT
)-1L)
1059 /* Because buttons have CS_PARENTDC class style, there is a chance
1060 * that label will be drawn out of client rect.
1061 * But Windows doesn't clip label's rect, so do I.
1064 /* There is 1-pixel marging at the left, right, and bottom */
1065 rc
.left
--; rc
.right
++; rc
.bottom
++;
1066 FillRect(hDC
, &rc
, hbr
);
1067 rc
.left
++; rc
.right
--; rc
.bottom
--;
1069 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1073 /**********************************************************************
1074 * User Button Functions
1077 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1082 LONG state
= get_button_state( hwnd
);
1085 if (action
== ODA_SELECT
) return;
1087 GetClientRect( hwnd
, &rc
);
1089 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1091 parent
= GetParent(hwnd
);
1092 if (!parent
) parent
= hwnd
;
1093 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1094 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1095 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1096 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1098 FillRect( hDC
, &rc
, hBrush
);
1099 if ((action
== ODA_FOCUS
) ||
1100 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
1101 DrawFocusRect( hDC
, &rc
);
1105 /**********************************************************************
1106 * Ownerdrawn Button Functions
1109 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1111 LONG state
= get_button_state( hwnd
);
1115 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1117 HFONT hFont
, hPrevFont
= 0;
1119 dis
.CtlType
= ODT_BUTTON
;
1122 dis
.itemAction
= action
;
1123 dis
.itemState
= ((state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
1124 ((state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
1125 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1126 dis
.hwndItem
= hwnd
;
1129 GetClientRect( hwnd
, &dis
.rcItem
);
1131 clipRegion
= CreateRectRgnIndirect(&dis
.rcItem
);
1132 if (GetClipRgn(hDC
, clipRegion
) != 1)
1134 DeleteObject(clipRegion
);
1137 clipRect
= dis
.rcItem
;
1138 DPtoLP(hDC
, (LPPOINT
) &clipRect
, 2);
1139 IntersectClipRect(hDC
, clipRect
.left
, clipRect
.top
, clipRect
.right
, clipRect
.bottom
);
1141 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1142 parent
= GetParent(hwnd
);
1143 if (!parent
) parent
= hwnd
;
1144 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1145 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1146 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1147 SelectClipRgn(hDC
, clipRegion
);