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
76 #include "wine/winuser16.h"
78 #include "user_private.h"
79 #include "wine/debug.h"
81 WINE_DEFAULT_DEBUG_CHANNEL(button
);
83 /* GetWindowLong offsets for window extra information */
84 #define STATE_GWL_OFFSET 0
85 #define HFONT_GWL_OFFSET (sizeof(LONG))
86 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
87 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
89 /* Button state values */
90 #define BUTTON_UNCHECKED 0x00
91 #define BUTTON_CHECKED 0x01
92 #define BUTTON_3STATE 0x02
93 #define BUTTON_HIGHLIGHTED 0x04
94 #define BUTTON_HASFOCUS 0x08
95 #define BUTTON_NSTATES 0x0F
96 /* undocumented flags */
97 #define BUTTON_BTNPRESSED 0x40
98 #define BUTTON_UNKNOWN2 0x20
99 #define BUTTON_UNKNOWN3 0x10
101 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
102 do { /* Notify parent which has created this button control */ \
103 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
104 SendMessageW(GetParent(hWnd), WM_COMMAND, \
105 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
109 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
110 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
111 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
112 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
113 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
114 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
115 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
116 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
117 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
119 #define MAX_BTN_TYPE 12
121 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
123 BUTTON_UNCHECKED
, /* BS_PUSHBUTTON */
124 BUTTON_UNCHECKED
, /* BS_DEFPUSHBUTTON */
125 BUTTON_CHECKED
, /* BS_CHECKBOX */
126 BUTTON_CHECKED
, /* BS_AUTOCHECKBOX */
127 BUTTON_CHECKED
, /* BS_RADIOBUTTON */
128 BUTTON_3STATE
, /* BS_3STATE */
129 BUTTON_3STATE
, /* BS_AUTO3STATE */
130 BUTTON_UNCHECKED
, /* BS_GROUPBOX */
131 BUTTON_UNCHECKED
, /* BS_USERBUTTON */
132 BUTTON_CHECKED
, /* BS_AUTORADIOBUTTON */
133 BUTTON_UNCHECKED
, /* Not defined */
134 BUTTON_UNCHECKED
/* BS_OWNERDRAW */
137 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
139 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
141 PB_Paint
, /* BS_PUSHBUTTON */
142 PB_Paint
, /* BS_DEFPUSHBUTTON */
143 CB_Paint
, /* BS_CHECKBOX */
144 CB_Paint
, /* BS_AUTOCHECKBOX */
145 CB_Paint
, /* BS_RADIOBUTTON */
146 CB_Paint
, /* BS_3STATE */
147 CB_Paint
, /* BS_AUTO3STATE */
148 GB_Paint
, /* BS_GROUPBOX */
149 UB_Paint
, /* BS_USERBUTTON */
150 CB_Paint
, /* BS_AUTORADIOBUTTON */
151 NULL
, /* Not defined */
152 OB_Paint
/* BS_OWNERDRAW */
155 static HBITMAP hbitmapCheckBoxes
= 0;
156 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
159 /*********************************************************************
160 * button class descriptor
162 static const WCHAR buttonW
[] = {'B','u','t','t','o','n',0};
163 const struct builtin_class_descr BUTTON_builtin_class
=
166 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
167 ButtonWndProcA
, /* procA */
168 ButtonWndProcW
, /* procW */
169 NB_EXTRA_BYTES
, /* extra */
170 IDC_ARROW
, /* cursor */
175 static inline LONG
get_button_state( HWND hwnd
)
177 return GetWindowLongW( hwnd
, STATE_GWL_OFFSET
);
180 static inline void set_button_state( HWND hwnd
, LONG state
)
182 SetWindowLongW( hwnd
, STATE_GWL_OFFSET
, state
);
185 static inline HFONT
get_button_font( HWND hwnd
)
187 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
190 static inline void set_button_font( HWND hwnd
, HFONT font
)
192 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
195 static inline UINT
get_button_type( LONG window_style
)
197 return (window_style
& 0x0f);
200 /* paint a button of any type */
201 static inline void paint_button( HWND hwnd
, LONG style
, UINT action
)
203 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
205 HDC hdc
= GetDC( hwnd
);
206 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
207 ReleaseDC( hwnd
, hdc
);
211 /* retrieve the button text; returned buffer must be freed by caller */
212 static inline WCHAR
*get_button_text( HWND hwnd
)
215 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
216 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
220 /***********************************************************************
221 * ButtonWndProc_common
223 static LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
224 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
228 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
229 UINT btn_type
= get_button_type( style
);
233 pt
.x
= (short)LOWORD(lParam
);
234 pt
.y
= (short)HIWORD(lParam
);
242 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
243 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
245 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
246 case BS_GROUPBOX
: return DLGC_STATIC
;
247 default: return DLGC_BUTTON
;
251 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
255 if (!hbitmapCheckBoxes
)
258 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
259 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
260 checkBoxWidth
= bmp
.bmWidth
/ 4;
261 checkBoxHeight
= bmp
.bmHeight
/ 3;
263 if (btn_type
>= MAX_BTN_TYPE
)
264 return -1; /* abort */
265 set_button_state( hWnd
, BUTTON_UNCHECKED
);
269 if (btn_type
== BS_OWNERDRAW
)
271 HDC hdc
= (HDC
)wParam
;
274 HWND parent
= GetParent(hWnd
);
275 if (!parent
) parent
= hWnd
;
276 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
277 if (!hBrush
) /* did the app forget to call defwindowproc ? */
278 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
279 (WPARAM
)hdc
, (LPARAM
)hWnd
);
280 GetClientRect(hWnd
, &rc
);
281 FillRect(hdc
, &rc
, hBrush
);
287 if (btnPaintFunc
[btn_type
])
290 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
291 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
292 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
293 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
294 if( !wParam
) EndPaint( hWnd
, &ps
);
299 if (wParam
== VK_SPACE
)
301 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
302 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
306 case WM_LBUTTONDBLCLK
:
307 if(style
& BS_NOTIFY
||
308 btn_type
== BS_RADIOBUTTON
||
309 btn_type
== BS_USERBUTTON
||
310 btn_type
== BS_OWNERDRAW
)
312 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
319 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
320 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
324 if (wParam
!= VK_SPACE
)
328 state
= get_button_state( hWnd
);
329 if (!(state
& BUTTON_BTNPRESSED
)) break;
330 state
&= BUTTON_NSTATES
;
331 set_button_state( hWnd
, state
);
332 if (!(state
& BUTTON_HIGHLIGHTED
))
337 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
339 GetClientRect( hWnd
, &rect
);
340 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
342 state
= get_button_state( hWnd
);
345 case BS_AUTOCHECKBOX
:
346 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BUTTON_CHECKED
), 0 );
348 case BS_AUTORADIOBUTTON
:
349 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
352 SendMessageW( hWnd
, BM_SETCHECK
,
353 (state
& BUTTON_3STATE
) ? 0 : ((state
& 3) + 1), 0 );
356 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
360 case WM_CAPTURECHANGED
:
361 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
362 state
= get_button_state( hWnd
);
363 if (state
& BUTTON_BTNPRESSED
)
365 state
&= BUTTON_NSTATES
;
366 set_button_state( hWnd
, state
);
367 if (state
& BUTTON_HIGHLIGHTED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
372 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
374 GetClientRect( hWnd
, &rect
);
375 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
381 /* Clear an old text here as Windows does */
382 HDC hdc
= GetDC(hWnd
);
385 HWND parent
= GetParent(hWnd
);
387 if (!parent
) parent
= hWnd
;
388 hbrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
389 (WPARAM
)hdc
, (LPARAM
)hWnd
);
390 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
391 hbrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
392 (WPARAM
)hdc
, (LPARAM
)hWnd
);
394 GetClientRect(hWnd
, &client
);
396 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
397 /* Clip by client rect bounds */
398 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
399 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
400 FillRect(hdc
, &rc
, hbrush
);
401 ReleaseDC(hWnd
, hdc
);
403 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
404 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
405 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
406 InvalidateRect( hWnd
, NULL
, TRUE
);
408 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
409 return 1; /* success. FIXME: check text length */
413 set_button_font( hWnd
, (HFONT
)wParam
);
414 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
418 return (LRESULT
)get_button_font( hWnd
);
421 TRACE("WM_SETFOCUS %p\n",hWnd
);
422 set_button_state( hWnd
, get_button_state(hWnd
) | BUTTON_HASFOCUS
);
423 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
424 if (style
& BS_NOTIFY
)
425 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
429 TRACE("WM_KILLFOCUS %p\n",hWnd
);
430 state
= get_button_state( hWnd
);
431 set_button_state( hWnd
, state
& ~BUTTON_HASFOCUS
);
432 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
434 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
436 if (style
& BS_NOTIFY
)
437 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
441 case WM_SYSCOLORCHANGE
:
442 InvalidateRect( hWnd
, NULL
, FALSE
);
447 if ((wParam
& 0x0f) >= MAX_BTN_TYPE
) break;
448 btn_type
= wParam
& 0x0f;
449 style
= (style
& ~0x0f) | btn_type
;
450 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
452 /* Only redraw if lParam flag is set.*/
454 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
459 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
460 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
464 /* Check that image format matches button style */
465 switch (style
& (BS_BITMAP
|BS_ICON
))
468 if (wParam
!= IMAGE_BITMAP
) return 0;
471 if (wParam
!= IMAGE_ICON
) return 0;
476 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
477 InvalidateRect( hWnd
, NULL
, FALSE
);
478 return (LRESULT
)oldHbitmap
;
481 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
485 return get_button_state( hWnd
) & 3;
489 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
490 state
= get_button_state( hWnd
);
491 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
493 if (wParam
) style
|= WS_TABSTOP
;
494 else style
&= ~WS_TABSTOP
;
495 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
497 if ((state
& 3) != wParam
)
499 set_button_state( hWnd
, (state
& ~3) | wParam
);
500 paint_button( hWnd
, btn_type
, ODA_SELECT
);
502 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
) && (style
& WS_CHILD
))
503 BUTTON_CheckAutoRadioButton( hWnd
);
508 return get_button_state( hWnd
);
512 state
= get_button_state( hWnd
);
515 if (state
& BUTTON_HIGHLIGHTED
) break;
516 set_button_state( hWnd
, state
| BUTTON_HIGHLIGHTED
);
520 if (!(state
& BUTTON_HIGHLIGHTED
)) break;
521 set_button_state( hWnd
, state
& ~BUTTON_HIGHLIGHTED
);
523 paint_button( hWnd
, btn_type
, ODA_SELECT
);
527 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
530 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
531 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
536 /***********************************************************************
538 * The button window procedure. This is just a wrapper which locks
539 * the passed HWND and calls the real window procedure (with a WND*
540 * pointer pointing to the locked windowstructure).
542 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
544 if (!IsWindow( hWnd
)) return 0;
545 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, TRUE
);
549 /***********************************************************************
552 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
554 if (!IsWindow( hWnd
)) return 0;
555 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, FALSE
);
559 /**********************************************************************
560 * Convert button styles to flags used by DrawText.
561 * TODO: handle WS_EX_RIGHT extended style.
563 static UINT
BUTTON_BStoDT(DWORD style
)
565 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
567 /* "Convert" pushlike buttons to pushbuttons */
568 if (style
& BS_PUSHLIKE
)
571 if (!(style
& BS_MULTILINE
))
572 dtStyle
|= DT_SINGLELINE
;
574 dtStyle
|= DT_WORDBREAK
;
576 switch (style
& BS_CENTER
)
578 case BS_LEFT
: /* DT_LEFT is 0 */ break;
579 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
580 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
582 /* Pushbutton's text is centered by default */
583 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
584 /* all other flavours have left aligned text */
587 /* DrawText ignores vertical alignment for multiline text,
588 * but we use these flags to align label manually.
590 if (get_button_type(style
) != BS_GROUPBOX
)
592 switch (style
& BS_VCENTER
)
594 case BS_TOP
: /* DT_TOP is 0 */ break;
595 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
596 case BS_VCENTER
: /* fall through */
597 default: dtStyle
|= DT_VCENTER
; break;
601 /* GroupBox's text is always single line and is top aligned. */
602 dtStyle
|= DT_SINGLELINE
;
607 /**********************************************************************
608 * BUTTON_CalcLabelRect
610 * Calculates label's rectangle depending on button style.
612 * Returns flags to be passed to DrawText.
613 * Calculated rectangle doesn't take into account button state
614 * (pushed, etc.). If there is nothing to draw (no text/image) output
615 * rectangle is empty, and return value is (UINT)-1.
617 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
619 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
623 UINT dtStyle
= BUTTON_BStoDT(style
);
627 /* Calculate label rectangle according to label type */
628 switch (style
& (BS_ICON
|BS_BITMAP
))
631 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
634 HeapFree( GetProcessHeap(), 0, text
);
637 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
638 HeapFree( GetProcessHeap(), 0, text
);
642 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
645 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
647 r
.right
= r
.left
+ bm
.bmWidth
;
648 r
.bottom
= r
.top
+ bm
.bmHeight
;
650 DeleteObject(iconInfo
.hbmColor
);
651 DeleteObject(iconInfo
.hbmMask
);
655 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
658 r
.right
= r
.left
+ bm
.bmWidth
;
659 r
.bottom
= r
.top
+ bm
.bmHeight
;
666 return (UINT
)(LONG
)-1;
669 /* Position label inside bounding rectangle according to
670 * alignment flags. (calculated rect is always left-top aligned).
671 * If label is aligned to any side - shift label in opposite
672 * direction to leave extra space for focus rectangle.
674 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
676 case DT_LEFT
: r
.left
++; r
.right
++; break;
677 case DT_CENTER
: n
= r
.right
- r
.left
;
678 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
679 r
.right
= r
.left
+ n
; break;
680 case DT_RIGHT
: n
= r
.right
- r
.left
;
681 r
.right
= rc
->right
- 1;
682 r
.left
= r
.right
- n
;
686 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
688 case DT_TOP
: r
.top
++; r
.bottom
++; break;
689 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
690 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
691 r
.bottom
= r
.top
+ n
; break;
692 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
693 r
.bottom
= rc
->bottom
- 1;
694 r
.top
= r
.bottom
- n
;
703 /**********************************************************************
704 * BUTTON_DrawTextCallback
706 * Callback function used by DrawStateW function.
708 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
716 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
721 /**********************************************************************
724 * Common function for drawing button label.
726 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, RECT
*rc
)
728 DRAWSTATEPROC lpOutputProc
= NULL
;
732 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
733 LONG state
= get_button_state( hwnd
);
734 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
737 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
738 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
739 * I don't have Win31 on hand to verify that, so I leave it as is.
742 if ((style
& BS_PUSHLIKE
) && (state
& BUTTON_3STATE
))
744 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
748 switch (style
& (BS_ICON
|BS_BITMAP
))
751 /* DST_COMPLEX -- is 0 */
752 lpOutputProc
= BUTTON_DrawTextCallback
;
753 if (!(text
= get_button_text( hwnd
))) return;
755 wp
= (WPARAM
)dtFlags
;
760 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
765 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
772 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
773 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
774 HeapFree( GetProcessHeap(), 0, text
);
777 /**********************************************************************
778 * Push Button Functions
780 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
782 RECT rc
, focus_rect
, r
;
783 UINT dtFlags
, uState
;
787 COLORREF oldTxtColor
;
789 LONG state
= get_button_state( hwnd
);
790 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
791 BOOL pushedState
= (state
& BUTTON_HIGHLIGHTED
);
794 GetClientRect( hwnd
, &rc
);
796 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
797 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
798 parent
= GetParent(hwnd
);
799 if (!parent
) parent
= hwnd
;
800 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
801 hOldPen
= (HPEN
)SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
802 hOldBrush
=(HBRUSH
)SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
803 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
805 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
807 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
808 InflateRect( &rc
, -1, -1 );
811 uState
= DFCS_BUTTONPUSH
| DFCS_ADJUSTRECT
;
815 else if (pushedState
)
817 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
820 uState
|= DFCS_PUSHED
;
823 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
))
824 uState
|= DFCS_CHECKED
;
826 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
830 /* draw button label */
832 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
834 if (dtFlags
== (UINT
)-1L)
838 OffsetRect(&r
, 1, 1);
840 IntersectClipRect(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
842 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
844 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
846 SetTextColor( hDC
, oldTxtColor
);
848 if (state
& BUTTON_HASFOCUS
)
850 InflateRect( &focus_rect
, -1, -1 );
851 IntersectRect(&focus_rect
, &focus_rect
, &rc
);
852 DrawFocusRect( hDC
, &focus_rect
);
856 SelectObject( hDC
, hOldPen
);
857 SelectObject( hDC
, hOldBrush
);
858 SetBkMode(hDC
, oldBkMode
);
861 /**********************************************************************
862 * Check Box & Radio Button Functions
865 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
867 RECT rbox
, rtext
, client
;
872 LONG state
= get_button_state( hwnd
);
873 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
876 if (style
& BS_PUSHLIKE
)
878 PB_Paint( hwnd
, hDC
, action
);
882 GetClientRect(hwnd
, &client
);
883 rbox
= rtext
= client
;
885 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
887 parent
= GetParent(hwnd
);
888 if (!parent
) parent
= hwnd
;
889 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
890 (WPARAM
)hDC
, (LPARAM
)hwnd
);
891 if (!hBrush
) /* did the app forget to call defwindowproc ? */
892 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
893 (WPARAM
)hDC
, (LPARAM
)hwnd
);
895 if (style
& BS_LEFTTEXT
)
897 /* magic +4 is what CTL3D expects */
899 rtext
.right
-= checkBoxWidth
+ 4;
900 rbox
.left
= rbox
.right
- checkBoxWidth
;
904 rtext
.left
+= checkBoxWidth
+ 4;
905 rbox
.right
= checkBoxWidth
;
908 /* Since WM_ERASEBKGND does nothing, first prepare background */
909 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
910 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
914 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
916 /* Only adjust rbox when rtext is valid */
917 if (dtFlags
!= (UINT
)-1L)
919 rbox
.top
= rtext
.top
;
920 rbox
.bottom
= rtext
.bottom
;
923 /* Draw the check-box bitmap */
924 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
928 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
929 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
930 else if (state
& BUTTON_3STATE
) flags
= DFCS_BUTTON3STATE
;
931 else flags
= DFCS_BUTTONCHECK
;
933 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) flags
|= DFCS_CHECKED
;
934 if (state
& BUTTON_HIGHLIGHTED
) flags
|= DFCS_PUSHED
;
936 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
938 /* rbox must have the correct height */
939 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
941 if (style
& BS_TOP
) {
943 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
945 rbox
.top
-= -delta
/2 + 1;
946 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
948 } else if (style
& BS_BOTTOM
) {
950 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
952 rbox
.bottom
+= -delta
/2 + 1;
953 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
955 } else { /* Default */
957 int ofs
= (delta
/ 2);
958 rbox
.bottom
-= ofs
+ 1;
959 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
960 } else if (delta
< 0) {
961 int ofs
= (-delta
/ 2);
963 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
967 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
970 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
973 IntersectClipRect(hDC
, client
.left
, client
.top
, client
.right
, client
.bottom
);
975 if (action
== ODA_DRAWENTIRE
)
976 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
979 if ((action
== ODA_FOCUS
) ||
980 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
984 IntersectRect(&rtext
, &rtext
, &client
);
985 DrawFocusRect( hDC
, &rtext
);
990 /**********************************************************************
991 * BUTTON_CheckAutoRadioButton
993 * hwnd is checked, uncheck every other auto radio button in group
995 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
997 HWND parent
, sibling
, start
;
999 parent
= GetParent(hwnd
);
1000 /* make sure that starting control is not disabled or invisible */
1001 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1004 if (!sibling
) break;
1005 if ((hwnd
!= sibling
) &&
1006 ((GetWindowLongW( sibling
, GWL_STYLE
) & 0x0f) == BS_AUTORADIOBUTTON
))
1007 SendMessageW( sibling
, BM_SETCHECK
, BUTTON_UNCHECKED
, 0 );
1008 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1009 } while (sibling
!= start
);
1013 /**********************************************************************
1014 * Group Box Functions
1017 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1024 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1027 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1028 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1029 parent
= GetParent(hwnd
);
1030 if (!parent
) parent
= hwnd
;
1031 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1032 if (!hbr
) /* did the app forget to call defwindowproc ? */
1033 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1034 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1036 GetClientRect( hwnd
, &rc
);
1039 GetTextMetricsW (hDC
, &tm
);
1040 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1041 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1043 InflateRect(&rc
, -7, 1);
1044 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1046 if (dtFlags
== (UINT
)-1L)
1049 /* Because buttons have CS_PARENTDC class style, there is a chance
1050 * that label will be drawn out of client rect.
1051 * But Windows doesn't clip label's rect, so do I.
1054 /* There is 1-pixel marging at the left, right, and bottom */
1055 rc
.left
--; rc
.right
++; rc
.bottom
++;
1056 FillRect(hDC
, &rc
, hbr
);
1057 rc
.left
++; rc
.right
--; rc
.bottom
--;
1059 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1063 /**********************************************************************
1064 * User Button Functions
1067 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1072 LONG state
= get_button_state( hwnd
);
1075 if (action
== ODA_SELECT
) return;
1077 GetClientRect( hwnd
, &rc
);
1079 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1081 parent
= GetParent(hwnd
);
1082 if (!parent
) parent
= hwnd
;
1083 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1084 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1085 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1086 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1088 FillRect( hDC
, &rc
, hBrush
);
1089 if ((action
== ODA_FOCUS
) ||
1090 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
1091 DrawFocusRect( hDC
, &rc
);
1095 /**********************************************************************
1096 * Ownerdrawn Button Functions
1099 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1101 LONG state
= get_button_state( hwnd
);
1105 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1107 HFONT hFont
, hPrevFont
= 0;
1109 dis
.CtlType
= ODT_BUTTON
;
1112 dis
.itemAction
= action
;
1113 dis
.itemState
= ((state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
1114 ((state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
1115 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1116 dis
.hwndItem
= hwnd
;
1119 GetClientRect( hwnd
, &dis
.rcItem
);
1121 clipRegion
= CreateRectRgnIndirect(&dis
.rcItem
);
1122 if (GetClipRgn(hDC
, clipRegion
) != 1)
1124 DeleteObject(clipRegion
);
1127 clipRect
= dis
.rcItem
;
1128 DPtoLP(hDC
, (LPPOINT
) &clipRect
, 2);
1129 IntersectClipRect(hDC
, clipRect
.left
, clipRect
.top
, clipRect
.right
, clipRect
.bottom
);
1131 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1132 parent
= GetParent(hwnd
);
1133 if (!parent
) parent
= hwnd
;
1134 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1135 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1136 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1137 SelectClipRgn(hDC
, clipRegion
);