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 static void setup_clipping( HWND hwnd
, HDC hdc
)
224 GetClientRect( hwnd
, &rc
);
225 DPtoLP( hdc
, (POINT
*)&rc
, 2 );
226 IntersectClipRect( hdc
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
229 /***********************************************************************
230 * ButtonWndProc_common
232 static LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
233 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
237 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
238 UINT btn_type
= get_button_type( style
);
242 pt
.x
= (short)LOWORD(lParam
);
243 pt
.y
= (short)HIWORD(lParam
);
251 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
252 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
254 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
255 case BS_GROUPBOX
: return DLGC_STATIC
;
256 default: return DLGC_BUTTON
;
260 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
264 if (!hbitmapCheckBoxes
)
267 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
268 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
269 checkBoxWidth
= bmp
.bmWidth
/ 4;
270 checkBoxHeight
= bmp
.bmHeight
/ 3;
272 if (btn_type
>= MAX_BTN_TYPE
)
273 return -1; /* abort */
274 set_button_state( hWnd
, BUTTON_UNCHECKED
);
278 if (btn_type
== BS_OWNERDRAW
)
280 HDC hdc
= (HDC
)wParam
;
283 HWND parent
= GetParent(hWnd
);
284 if (!parent
) parent
= hWnd
;
285 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
286 if (!hBrush
) /* did the app forget to call defwindowproc ? */
287 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
288 (WPARAM
)hdc
, (LPARAM
)hWnd
);
289 GetClientRect(hWnd
, &rc
);
290 FillRect(hdc
, &rc
, hBrush
);
296 if (btnPaintFunc
[btn_type
])
299 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
300 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
301 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
302 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
303 if( !wParam
) EndPaint( hWnd
, &ps
);
308 if (wParam
== VK_SPACE
)
310 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
311 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
315 case WM_LBUTTONDBLCLK
:
316 if(style
& BS_NOTIFY
||
317 btn_type
== BS_RADIOBUTTON
||
318 btn_type
== BS_USERBUTTON
||
319 btn_type
== BS_OWNERDRAW
)
321 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
328 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
329 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
333 if (wParam
!= VK_SPACE
)
337 state
= get_button_state( hWnd
);
338 if (!(state
& BUTTON_BTNPRESSED
)) break;
339 state
&= BUTTON_NSTATES
;
340 set_button_state( hWnd
, state
);
341 if (!(state
& BUTTON_HIGHLIGHTED
))
346 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
348 GetClientRect( hWnd
, &rect
);
349 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
351 state
= get_button_state( hWnd
);
354 case BS_AUTOCHECKBOX
:
355 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BUTTON_CHECKED
), 0 );
357 case BS_AUTORADIOBUTTON
:
358 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
361 SendMessageW( hWnd
, BM_SETCHECK
,
362 (state
& BUTTON_3STATE
) ? 0 : ((state
& 3) + 1), 0 );
365 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
369 case WM_CAPTURECHANGED
:
370 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
371 state
= get_button_state( hWnd
);
372 if (state
& BUTTON_BTNPRESSED
)
374 state
&= BUTTON_NSTATES
;
375 set_button_state( hWnd
, state
);
376 if (state
& BUTTON_HIGHLIGHTED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
381 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
383 GetClientRect( hWnd
, &rect
);
384 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
390 /* Clear an old text here as Windows does */
391 HDC hdc
= GetDC(hWnd
);
394 HWND parent
= GetParent(hWnd
);
396 if (!parent
) parent
= hWnd
;
397 hbrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
398 (WPARAM
)hdc
, (LPARAM
)hWnd
);
399 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
400 hbrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
401 (WPARAM
)hdc
, (LPARAM
)hWnd
);
403 GetClientRect(hWnd
, &client
);
405 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
406 /* Clip by client rect bounds */
407 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
408 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
409 FillRect(hdc
, &rc
, hbrush
);
410 ReleaseDC(hWnd
, hdc
);
412 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
413 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
414 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
415 InvalidateRect( hWnd
, NULL
, TRUE
);
417 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
418 return 1; /* success. FIXME: check text length */
422 set_button_font( hWnd
, (HFONT
)wParam
);
423 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
427 return (LRESULT
)get_button_font( hWnd
);
430 TRACE("WM_SETFOCUS %p\n",hWnd
);
431 set_button_state( hWnd
, get_button_state(hWnd
) | BUTTON_HASFOCUS
);
432 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
433 if (style
& BS_NOTIFY
)
434 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
438 TRACE("WM_KILLFOCUS %p\n",hWnd
);
439 state
= get_button_state( hWnd
);
440 set_button_state( hWnd
, state
& ~BUTTON_HASFOCUS
);
441 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
443 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
445 if (style
& BS_NOTIFY
)
446 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
450 case WM_SYSCOLORCHANGE
:
451 InvalidateRect( hWnd
, NULL
, FALSE
);
456 if ((wParam
& 0x0f) >= MAX_BTN_TYPE
) break;
457 btn_type
= wParam
& 0x0f;
458 style
= (style
& ~0x0f) | btn_type
;
459 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
461 /* Only redraw if lParam flag is set.*/
463 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
468 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
469 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
473 /* Check that image format matches button style */
474 switch (style
& (BS_BITMAP
|BS_ICON
))
477 if (wParam
!= IMAGE_BITMAP
) return 0;
480 if (wParam
!= IMAGE_ICON
) return 0;
485 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
486 InvalidateRect( hWnd
, NULL
, FALSE
);
487 return (LRESULT
)oldHbitmap
;
490 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
494 return get_button_state( hWnd
) & 3;
498 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
499 state
= get_button_state( hWnd
);
500 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
502 if (wParam
) style
|= WS_TABSTOP
;
503 else style
&= ~WS_TABSTOP
;
504 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
506 if ((state
& 3) != wParam
)
508 set_button_state( hWnd
, (state
& ~3) | wParam
);
509 paint_button( hWnd
, btn_type
, ODA_SELECT
);
511 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
) && (style
& WS_CHILD
))
512 BUTTON_CheckAutoRadioButton( hWnd
);
517 return get_button_state( hWnd
);
521 state
= get_button_state( hWnd
);
524 if (state
& BUTTON_HIGHLIGHTED
) break;
525 set_button_state( hWnd
, state
| BUTTON_HIGHLIGHTED
);
529 if (!(state
& BUTTON_HIGHLIGHTED
)) break;
530 set_button_state( hWnd
, state
& ~BUTTON_HIGHLIGHTED
);
532 paint_button( hWnd
, btn_type
, ODA_SELECT
);
536 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
539 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
540 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
545 /***********************************************************************
547 * The button window procedure. This is just a wrapper which locks
548 * the passed HWND and calls the real window procedure (with a WND*
549 * pointer pointing to the locked windowstructure).
551 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
553 if (!IsWindow( hWnd
)) return 0;
554 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, TRUE
);
558 /***********************************************************************
561 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
563 if (!IsWindow( hWnd
)) return 0;
564 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, FALSE
);
568 /**********************************************************************
569 * Convert button styles to flags used by DrawText.
570 * TODO: handle WS_EX_RIGHT extended style.
572 static UINT
BUTTON_BStoDT(DWORD style
)
574 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
576 /* "Convert" pushlike buttons to pushbuttons */
577 if (style
& BS_PUSHLIKE
)
580 if (!(style
& BS_MULTILINE
))
581 dtStyle
|= DT_SINGLELINE
;
583 dtStyle
|= DT_WORDBREAK
;
585 switch (style
& BS_CENTER
)
587 case BS_LEFT
: /* DT_LEFT is 0 */ break;
588 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
589 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
591 /* Pushbutton's text is centered by default */
592 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
593 /* all other flavours have left aligned text */
596 /* DrawText ignores vertical alignment for multiline text,
597 * but we use these flags to align label manually.
599 if (get_button_type(style
) != BS_GROUPBOX
)
601 switch (style
& BS_VCENTER
)
603 case BS_TOP
: /* DT_TOP is 0 */ break;
604 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
605 case BS_VCENTER
: /* fall through */
606 default: dtStyle
|= DT_VCENTER
; break;
610 /* GroupBox's text is always single line and is top aligned. */
611 dtStyle
|= DT_SINGLELINE
;
616 /**********************************************************************
617 * BUTTON_CalcLabelRect
619 * Calculates label's rectangle depending on button style.
621 * Returns flags to be passed to DrawText.
622 * Calculated rectangle doesn't take into account button state
623 * (pushed, etc.). If there is nothing to draw (no text/image) output
624 * rectangle is empty, and return value is (UINT)-1.
626 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
628 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
632 UINT dtStyle
= BUTTON_BStoDT(style
);
636 /* Calculate label rectangle according to label type */
637 switch (style
& (BS_ICON
|BS_BITMAP
))
640 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
643 HeapFree( GetProcessHeap(), 0, text
);
646 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
647 HeapFree( GetProcessHeap(), 0, text
);
651 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
654 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
656 r
.right
= r
.left
+ bm
.bmWidth
;
657 r
.bottom
= r
.top
+ bm
.bmHeight
;
659 DeleteObject(iconInfo
.hbmColor
);
660 DeleteObject(iconInfo
.hbmMask
);
664 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
667 r
.right
= r
.left
+ bm
.bmWidth
;
668 r
.bottom
= r
.top
+ bm
.bmHeight
;
675 return (UINT
)(LONG
)-1;
678 /* Position label inside bounding rectangle according to
679 * alignment flags. (calculated rect is always left-top aligned).
680 * If label is aligned to any side - shift label in opposite
681 * direction to leave extra space for focus rectangle.
683 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
685 case DT_LEFT
: r
.left
++; r
.right
++; break;
686 case DT_CENTER
: n
= r
.right
- r
.left
;
687 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
688 r
.right
= r
.left
+ n
; break;
689 case DT_RIGHT
: n
= r
.right
- r
.left
;
690 r
.right
= rc
->right
- 1;
691 r
.left
= r
.right
- n
;
695 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
697 case DT_TOP
: r
.top
++; r
.bottom
++; break;
698 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
699 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
700 r
.bottom
= r
.top
+ n
; break;
701 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
702 r
.bottom
= rc
->bottom
- 1;
703 r
.top
= r
.bottom
- n
;
712 /**********************************************************************
713 * BUTTON_DrawTextCallback
715 * Callback function used by DrawStateW function.
717 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
725 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
730 /**********************************************************************
733 * Common function for drawing button label.
735 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
737 DRAWSTATEPROC lpOutputProc
= NULL
;
741 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
742 LONG state
= get_button_state( hwnd
);
743 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
746 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
747 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
748 * I don't have Win31 on hand to verify that, so I leave it as is.
751 if ((style
& BS_PUSHLIKE
) && (state
& BUTTON_3STATE
))
753 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
757 switch (style
& (BS_ICON
|BS_BITMAP
))
760 /* DST_COMPLEX -- is 0 */
761 lpOutputProc
= BUTTON_DrawTextCallback
;
762 if (!(text
= get_button_text( hwnd
))) return;
764 wp
= (WPARAM
)dtFlags
;
769 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
774 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
781 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
782 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
783 HeapFree( GetProcessHeap(), 0, text
);
786 /**********************************************************************
787 * Push Button Functions
789 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
791 RECT rc
, focus_rect
, r
;
792 UINT dtFlags
, uState
;
796 COLORREF oldTxtColor
;
798 LONG state
= get_button_state( hwnd
);
799 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
800 BOOL pushedState
= (state
& BUTTON_HIGHLIGHTED
);
803 GetClientRect( hwnd
, &rc
);
805 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
806 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
807 parent
= GetParent(hwnd
);
808 if (!parent
) parent
= hwnd
;
809 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
811 setup_clipping( hwnd
, hDC
);
813 hOldPen
= (HPEN
)SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
814 hOldBrush
=(HBRUSH
)SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
815 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
817 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
819 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
820 InflateRect( &rc
, -1, -1 );
823 uState
= DFCS_BUTTONPUSH
| DFCS_ADJUSTRECT
;
827 else if (pushedState
)
829 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
832 uState
|= DFCS_PUSHED
;
835 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
))
836 uState
|= DFCS_CHECKED
;
838 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
842 /* draw button label */
844 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
846 if (dtFlags
== (UINT
)-1L)
850 OffsetRect(&r
, 1, 1);
852 IntersectClipRect(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
854 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
856 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
858 SetTextColor( hDC
, oldTxtColor
);
860 if (state
& BUTTON_HASFOCUS
)
862 InflateRect( &focus_rect
, -1, -1 );
863 IntersectRect(&focus_rect
, &focus_rect
, &rc
);
864 DrawFocusRect( hDC
, &focus_rect
);
868 SelectObject( hDC
, hOldPen
);
869 SelectObject( hDC
, hOldBrush
);
870 SetBkMode(hDC
, oldBkMode
);
873 /**********************************************************************
874 * Check Box & Radio Button Functions
877 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
879 RECT rbox
, rtext
, client
;
884 LONG state
= get_button_state( hwnd
);
885 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
888 if (style
& BS_PUSHLIKE
)
890 PB_Paint( hwnd
, hDC
, action
);
894 GetClientRect(hwnd
, &client
);
895 rbox
= rtext
= client
;
897 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
899 parent
= GetParent(hwnd
);
900 if (!parent
) parent
= hwnd
;
901 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
902 (WPARAM
)hDC
, (LPARAM
)hwnd
);
903 if (!hBrush
) /* did the app forget to call defwindowproc ? */
904 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
905 (WPARAM
)hDC
, (LPARAM
)hwnd
);
907 if (style
& BS_LEFTTEXT
)
909 /* magic +4 is what CTL3D expects */
911 rtext
.right
-= checkBoxWidth
+ 4;
912 rbox
.left
= rbox
.right
- checkBoxWidth
;
916 rtext
.left
+= checkBoxWidth
+ 4;
917 rbox
.right
= checkBoxWidth
;
920 /* Since WM_ERASEBKGND does nothing, first prepare background */
921 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
922 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
926 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
928 /* Only adjust rbox when rtext is valid */
929 if (dtFlags
!= (UINT
)-1L)
931 rbox
.top
= rtext
.top
;
932 rbox
.bottom
= rtext
.bottom
;
935 /* Draw the check-box bitmap */
936 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
940 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
941 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
942 else if (state
& BUTTON_3STATE
) flags
= DFCS_BUTTON3STATE
;
943 else flags
= DFCS_BUTTONCHECK
;
945 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) flags
|= DFCS_CHECKED
;
946 if (state
& BUTTON_HIGHLIGHTED
) flags
|= DFCS_PUSHED
;
948 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
950 /* rbox must have the correct height */
951 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
953 if (style
& BS_TOP
) {
955 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
957 rbox
.top
-= -delta
/2 + 1;
958 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
960 } else if (style
& BS_BOTTOM
) {
962 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
964 rbox
.bottom
+= -delta
/2 + 1;
965 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
967 } else { /* Default */
969 int ofs
= (delta
/ 2);
970 rbox
.bottom
-= ofs
+ 1;
971 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
972 } else if (delta
< 0) {
973 int ofs
= (-delta
/ 2);
975 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
979 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
982 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
985 setup_clipping( hwnd
, hDC
);
987 if (action
== ODA_DRAWENTIRE
)
988 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
991 if ((action
== ODA_FOCUS
) ||
992 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
996 IntersectRect(&rtext
, &rtext
, &client
);
997 DrawFocusRect( hDC
, &rtext
);
1002 /**********************************************************************
1003 * BUTTON_CheckAutoRadioButton
1005 * hwnd is checked, uncheck every other auto radio button in group
1007 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
1009 HWND parent
, sibling
, start
;
1011 parent
= GetParent(hwnd
);
1012 /* make sure that starting control is not disabled or invisible */
1013 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
1016 if (!sibling
) break;
1017 if ((hwnd
!= sibling
) &&
1018 ((GetWindowLongW( sibling
, GWL_STYLE
) & 0x0f) == BS_AUTORADIOBUTTON
))
1019 SendMessageW( sibling
, BM_SETCHECK
, BUTTON_UNCHECKED
, 0 );
1020 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1021 } while (sibling
!= start
);
1025 /**********************************************************************
1026 * Group Box Functions
1029 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1036 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1039 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1040 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1041 parent
= GetParent(hwnd
);
1042 if (!parent
) parent
= hwnd
;
1043 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1044 if (!hbr
) /* did the app forget to call defwindowproc ? */
1045 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1046 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1048 GetClientRect( hwnd
, &rc
);
1051 GetTextMetricsW (hDC
, &tm
);
1052 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1053 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1055 InflateRect(&rc
, -7, 1);
1056 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1058 if (dtFlags
== (UINT
)-1L)
1061 setup_clipping( hwnd
, hDC
);
1063 /* Because buttons have CS_PARENTDC class style, there is a chance
1064 * that label will be drawn out of client rect.
1065 * But Windows doesn't clip label's rect, so do I.
1068 /* There is 1-pixel marging at the left, right, and bottom */
1069 rc
.left
--; rc
.right
++; rc
.bottom
++;
1070 FillRect(hDC
, &rc
, hbr
);
1071 rc
.left
++; rc
.right
--; rc
.bottom
--;
1073 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1077 /**********************************************************************
1078 * User Button Functions
1081 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1086 LONG state
= get_button_state( hwnd
);
1089 if (action
== ODA_SELECT
) return;
1091 GetClientRect( hwnd
, &rc
);
1093 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1095 parent
= GetParent(hwnd
);
1096 if (!parent
) parent
= hwnd
;
1097 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1098 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1099 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1100 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1102 FillRect( hDC
, &rc
, hBrush
);
1103 if ((action
== ODA_FOCUS
) ||
1104 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
1105 DrawFocusRect( hDC
, &rc
);
1109 /**********************************************************************
1110 * Ownerdrawn Button Functions
1113 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1115 LONG state
= get_button_state( hwnd
);
1117 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1119 HFONT hFont
, hPrevFont
= 0;
1121 dis
.CtlType
= ODT_BUTTON
;
1124 dis
.itemAction
= action
;
1125 dis
.itemState
= ((state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
1126 ((state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
1127 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1128 dis
.hwndItem
= hwnd
;
1131 GetClientRect( hwnd
, &dis
.rcItem
);
1133 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1134 parent
= GetParent(hwnd
);
1135 if (!parent
) parent
= hwnd
;
1136 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1138 setup_clipping( hwnd
, hDC
);
1140 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1141 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);