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
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
);
117 #define MAX_BTN_TYPE 12
119 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
121 BUTTON_UNCHECKED
, /* BS_PUSHBUTTON */
122 BUTTON_UNCHECKED
, /* BS_DEFPUSHBUTTON */
123 BUTTON_CHECKED
, /* BS_CHECKBOX */
124 BUTTON_CHECKED
, /* BS_AUTOCHECKBOX */
125 BUTTON_CHECKED
, /* BS_RADIOBUTTON */
126 BUTTON_3STATE
, /* BS_3STATE */
127 BUTTON_3STATE
, /* BS_AUTO3STATE */
128 BUTTON_UNCHECKED
, /* BS_GROUPBOX */
129 BUTTON_UNCHECKED
, /* BS_USERBUTTON */
130 BUTTON_CHECKED
, /* BS_AUTORADIOBUTTON */
131 BUTTON_UNCHECKED
, /* Not defined */
132 BUTTON_UNCHECKED
/* BS_OWNERDRAW */
135 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
137 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
139 PB_Paint
, /* BS_PUSHBUTTON */
140 PB_Paint
, /* BS_DEFPUSHBUTTON */
141 CB_Paint
, /* BS_CHECKBOX */
142 CB_Paint
, /* BS_AUTOCHECKBOX */
143 CB_Paint
, /* BS_RADIOBUTTON */
144 CB_Paint
, /* BS_3STATE */
145 CB_Paint
, /* BS_AUTO3STATE */
146 GB_Paint
, /* BS_GROUPBOX */
147 UB_Paint
, /* BS_USERBUTTON */
148 CB_Paint
, /* BS_AUTORADIOBUTTON */
149 NULL
, /* Not defined */
150 OB_Paint
/* BS_OWNERDRAW */
153 static HBITMAP hbitmapCheckBoxes
= 0;
154 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
157 /*********************************************************************
158 * button class descriptor
160 static const WCHAR buttonW
[] = {'B','u','t','t','o','n',0};
161 const struct builtin_class_descr BUTTON_builtin_class
=
164 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
165 WINPROC_BUTTON
, /* proc */
166 NB_EXTRA_BYTES
, /* extra */
167 IDC_ARROW
, /* cursor */
172 static inline LONG
get_button_state( HWND hwnd
)
174 return GetWindowLongW( hwnd
, STATE_GWL_OFFSET
);
177 static inline void set_button_state( HWND hwnd
, LONG state
)
179 SetWindowLongW( hwnd
, STATE_GWL_OFFSET
, state
);
182 static inline HFONT
get_button_font( HWND hwnd
)
184 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
187 static inline void set_button_font( HWND hwnd
, HFONT font
)
189 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
192 static inline UINT
get_button_type( LONG window_style
)
194 return (window_style
& 0x0f);
197 /* paint a button of any type */
198 static inline void paint_button( HWND hwnd
, LONG style
, UINT action
)
200 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
202 HDC hdc
= GetDC( hwnd
);
203 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
204 ReleaseDC( hwnd
, hdc
);
208 /* retrieve the button text; returned buffer must be freed by caller */
209 static inline WCHAR
*get_button_text( HWND hwnd
)
212 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
213 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
217 static void setup_clipping( HWND hwnd
, HDC hdc
)
221 GetClientRect( hwnd
, &rc
);
222 DPtoLP( hdc
, (POINT
*)&rc
, 2 );
223 IntersectClipRect( hdc
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
226 /***********************************************************************
227 * ButtonWndProc_common
229 LRESULT
ButtonWndProc_common(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
233 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
234 UINT btn_type
= get_button_type( style
);
238 if (!IsWindow( hWnd
)) return 0;
240 pt
.x
= (short)LOWORD(lParam
);
241 pt
.y
= (short)HIWORD(lParam
);
249 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
250 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
252 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
253 case BS_GROUPBOX
: return DLGC_STATIC
;
254 default: return DLGC_BUTTON
;
258 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
262 if (!hbitmapCheckBoxes
)
265 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
266 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
267 checkBoxWidth
= bmp
.bmWidth
/ 4;
268 checkBoxHeight
= bmp
.bmHeight
/ 3;
270 if (btn_type
>= MAX_BTN_TYPE
)
271 return -1; /* abort */
273 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
274 if (btn_type
== BS_USERBUTTON
)
276 style
= (style
& ~0x0f) | BS_PUSHBUTTON
;
277 WIN_SetStyle( hWnd
, style
, 0x0f & ~style
);
279 set_button_state( hWnd
, BUTTON_UNCHECKED
);
283 if (btn_type
== BS_OWNERDRAW
)
285 HDC hdc
= (HDC
)wParam
;
288 HWND parent
= GetParent(hWnd
);
289 if (!parent
) parent
= hWnd
;
290 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
291 if (!hBrush
) /* did the app forget to call defwindowproc ? */
292 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
293 (WPARAM
)hdc
, (LPARAM
)hWnd
);
294 GetClientRect(hWnd
, &rc
);
295 FillRect(hdc
, &rc
, hBrush
);
301 if (btnPaintFunc
[btn_type
])
304 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
305 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
306 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
307 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
308 if( !wParam
) EndPaint( hWnd
, &ps
);
313 if (wParam
== VK_SPACE
)
315 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
316 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
321 case WM_LBUTTONDBLCLK
:
322 if(style
& BS_NOTIFY
||
323 btn_type
== BS_RADIOBUTTON
||
324 btn_type
== BS_USERBUTTON
||
325 btn_type
== BS_OWNERDRAW
)
327 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
334 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
335 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
339 if (wParam
!= VK_SPACE
)
343 state
= get_button_state( hWnd
);
344 if (!(state
& BUTTON_BTNPRESSED
)) break;
345 state
&= BUTTON_NSTATES
;
346 set_button_state( hWnd
, state
);
347 if (!(state
& BUTTON_HIGHLIGHTED
))
352 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
354 GetClientRect( hWnd
, &rect
);
355 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
357 state
= get_button_state( hWnd
);
360 case BS_AUTOCHECKBOX
:
361 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BUTTON_CHECKED
), 0 );
363 case BS_AUTORADIOBUTTON
:
364 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
367 SendMessageW( hWnd
, BM_SETCHECK
,
368 (state
& BUTTON_3STATE
) ? 0 : ((state
& 3) + 1), 0 );
371 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
375 case WM_CAPTURECHANGED
:
376 TRACE("WM_CAPTURECHANGED %p\n", hWnd
);
377 state
= get_button_state( hWnd
);
378 if (state
& BUTTON_BTNPRESSED
)
380 state
&= BUTTON_NSTATES
;
381 set_button_state( hWnd
, state
);
382 if (state
& BUTTON_HIGHLIGHTED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
387 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
389 GetClientRect( hWnd
, &rect
);
390 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
396 /* Clear an old text here as Windows does */
397 HDC hdc
= GetDC(hWnd
);
400 HWND parent
= GetParent(hWnd
);
402 if (!parent
) parent
= hWnd
;
403 hbrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
404 (WPARAM
)hdc
, (LPARAM
)hWnd
);
405 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
406 hbrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
407 (WPARAM
)hdc
, (LPARAM
)hWnd
);
409 GetClientRect(hWnd
, &client
);
411 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
412 /* Clip by client rect bounds */
413 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
414 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
415 FillRect(hdc
, &rc
, hbrush
);
416 ReleaseDC(hWnd
, hdc
);
418 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
419 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
420 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
421 InvalidateRect( hWnd
, NULL
, TRUE
);
423 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
424 return 1; /* success. FIXME: check text length */
428 set_button_font( hWnd
, (HFONT
)wParam
);
429 if (lParam
) InvalidateRect(hWnd
, NULL
, TRUE
);
433 return (LRESULT
)get_button_font( hWnd
);
436 TRACE("WM_SETFOCUS %p\n",hWnd
);
437 set_button_state( hWnd
, get_button_state(hWnd
) | BUTTON_HASFOCUS
);
438 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
439 if (style
& BS_NOTIFY
)
440 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
444 TRACE("WM_KILLFOCUS %p\n",hWnd
);
445 state
= get_button_state( hWnd
);
446 set_button_state( hWnd
, state
& ~BUTTON_HASFOCUS
);
447 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
449 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
451 if (style
& BS_NOTIFY
)
452 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
454 InvalidateRect( hWnd
, NULL
, FALSE
);
457 case WM_SYSCOLORCHANGE
:
458 InvalidateRect( hWnd
, NULL
, FALSE
);
462 if ((wParam
& 0x0f) >= MAX_BTN_TYPE
) break;
463 btn_type
= wParam
& 0x0f;
464 style
= (style
& ~0x0f) | btn_type
;
465 WIN_SetStyle( hWnd
, style
, 0x0f & ~style
);
467 /* Only redraw if lParam flag is set.*/
469 InvalidateRect( hWnd
, NULL
, TRUE
);
474 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
475 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
479 /* Check that image format matches button style */
480 switch (style
& (BS_BITMAP
|BS_ICON
))
483 if (wParam
!= IMAGE_BITMAP
) return 0;
486 if (wParam
!= IMAGE_ICON
) return 0;
491 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
492 InvalidateRect( hWnd
, NULL
, FALSE
);
493 return (LRESULT
)oldHbitmap
;
496 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
499 return get_button_state( hWnd
) & 3;
502 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
503 state
= get_button_state( hWnd
);
504 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
506 if (wParam
) WIN_SetStyle( hWnd
, WS_TABSTOP
, 0 );
507 else WIN_SetStyle( hWnd
, 0, WS_TABSTOP
);
509 if ((state
& 3) != wParam
)
511 set_button_state( hWnd
, (state
& ~3) | wParam
);
512 paint_button( hWnd
, btn_type
, ODA_SELECT
);
514 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
) && (style
& WS_CHILD
))
515 BUTTON_CheckAutoRadioButton( hWnd
);
519 return get_button_state( hWnd
);
522 state
= get_button_state( hWnd
);
524 set_button_state( hWnd
, state
| BUTTON_HIGHLIGHTED
);
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 /**********************************************************************
542 * Convert button styles to flags used by DrawText.
544 static UINT
BUTTON_BStoDT( DWORD style
, DWORD ex_style
)
546 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
548 /* "Convert" pushlike buttons to pushbuttons */
549 if (style
& BS_PUSHLIKE
)
552 if (!(style
& BS_MULTILINE
))
553 dtStyle
|= DT_SINGLELINE
;
555 dtStyle
|= DT_WORDBREAK
;
557 switch (style
& BS_CENTER
)
559 case BS_LEFT
: /* DT_LEFT is 0 */ break;
560 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
561 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
563 /* Pushbutton's text is centered by default */
564 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
565 /* all other flavours have left aligned text */
568 if (ex_style
& WS_EX_RIGHT
) dtStyle
= DT_RIGHT
| (dtStyle
& ~(DT_LEFT
| DT_CENTER
));
570 /* DrawText ignores vertical alignment for multiline text,
571 * but we use these flags to align label manually.
573 if (get_button_type(style
) != BS_GROUPBOX
)
575 switch (style
& BS_VCENTER
)
577 case BS_TOP
: /* DT_TOP is 0 */ break;
578 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
579 case BS_VCENTER
: /* fall through */
580 default: dtStyle
|= DT_VCENTER
; break;
584 /* GroupBox's text is always single line and is top aligned. */
585 dtStyle
|= DT_SINGLELINE
;
590 /**********************************************************************
591 * BUTTON_CalcLabelRect
593 * Calculates label's rectangle depending on button style.
595 * Returns flags to be passed to DrawText.
596 * Calculated rectangle doesn't take into account button state
597 * (pushed, etc.). If there is nothing to draw (no text/image) output
598 * rectangle is empty, and return value is (UINT)-1.
600 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
602 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
603 LONG ex_style
= GetWindowLongW( hwnd
, GWL_EXSTYLE
);
607 UINT dtStyle
= BUTTON_BStoDT( style
, ex_style
);
611 /* Calculate label rectangle according to label type */
612 switch (style
& (BS_ICON
|BS_BITMAP
))
615 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
618 HeapFree( GetProcessHeap(), 0, text
);
621 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
622 HeapFree( GetProcessHeap(), 0, text
);
626 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
629 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
631 r
.right
= r
.left
+ bm
.bmWidth
;
632 r
.bottom
= r
.top
+ bm
.bmHeight
;
634 DeleteObject(iconInfo
.hbmColor
);
635 DeleteObject(iconInfo
.hbmMask
);
639 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
642 r
.right
= r
.left
+ bm
.bmWidth
;
643 r
.bottom
= r
.top
+ bm
.bmHeight
;
653 /* Position label inside bounding rectangle according to
654 * alignment flags. (calculated rect is always left-top aligned).
655 * If label is aligned to any side - shift label in opposite
656 * direction to leave extra space for focus rectangle.
658 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
660 case DT_LEFT
: r
.left
++; r
.right
++; break;
661 case DT_CENTER
: n
= r
.right
- r
.left
;
662 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
663 r
.right
= r
.left
+ n
; break;
664 case DT_RIGHT
: n
= r
.right
- r
.left
;
665 r
.right
= rc
->right
- 1;
666 r
.left
= r
.right
- n
;
670 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
672 case DT_TOP
: r
.top
++; r
.bottom
++; break;
673 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
674 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
675 r
.bottom
= r
.top
+ n
; break;
676 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
677 r
.bottom
= rc
->bottom
- 1;
678 r
.top
= r
.bottom
- n
;
687 /**********************************************************************
688 * BUTTON_DrawTextCallback
690 * Callback function used by DrawStateW function.
692 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
700 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
705 /**********************************************************************
708 * Common function for drawing button label.
710 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
712 DRAWSTATEPROC lpOutputProc
= NULL
;
716 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
717 LONG state
= get_button_state( hwnd
);
718 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
721 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
722 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
723 * I don't have Win31 on hand to verify that, so I leave it as is.
726 if ((style
& BS_PUSHLIKE
) && (state
& BUTTON_3STATE
))
728 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
732 switch (style
& (BS_ICON
|BS_BITMAP
))
735 /* DST_COMPLEX -- is 0 */
736 lpOutputProc
= BUTTON_DrawTextCallback
;
737 if (!(text
= get_button_text( hwnd
))) return;
744 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
749 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
756 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
757 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
758 HeapFree( GetProcessHeap(), 0, text
);
761 /**********************************************************************
762 * Push Button Functions
764 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
767 UINT dtFlags
, uState
;
771 COLORREF oldTxtColor
;
773 LONG state
= get_button_state( hwnd
);
774 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
775 BOOL pushedState
= (state
& BUTTON_HIGHLIGHTED
);
778 GetClientRect( hwnd
, &rc
);
780 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
781 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
782 parent
= GetParent(hwnd
);
783 if (!parent
) parent
= hwnd
;
784 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
786 setup_clipping( hwnd
, hDC
);
788 hOldPen
= SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
789 hOldBrush
= SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
790 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
792 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
794 if (action
!= ODA_FOCUS
)
795 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
796 InflateRect( &rc
, -1, -1 );
799 /* completely skip the drawing if only focus has changed */
800 if (action
== ODA_FOCUS
) goto draw_focus
;
802 uState
= DFCS_BUTTONPUSH
;
806 else if (pushedState
)
808 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
811 uState
|= DFCS_PUSHED
;
814 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
))
815 uState
|= DFCS_CHECKED
;
817 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
819 /* draw button label */
821 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
823 if (dtFlags
== (UINT
)-1L)
827 OffsetRect(&r
, 1, 1);
829 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
831 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
833 SetTextColor( hDC
, oldTxtColor
);
836 if (action
== ODA_FOCUS
|| (state
& BUTTON_HASFOCUS
))
838 InflateRect( &rc
, -2, -2 );
839 DrawFocusRect( hDC
, &rc
);
843 SelectObject( hDC
, hOldPen
);
844 SelectObject( hDC
, hOldBrush
);
845 SetBkMode(hDC
, oldBkMode
);
848 /**********************************************************************
849 * Check Box & Radio Button Functions
852 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
854 RECT rbox
, rtext
, client
;
859 LONG state
= get_button_state( hwnd
);
860 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
863 if (style
& BS_PUSHLIKE
)
865 PB_Paint( hwnd
, hDC
, action
);
869 GetClientRect(hwnd
, &client
);
870 rbox
= rtext
= client
;
872 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
874 parent
= GetParent(hwnd
);
875 if (!parent
) parent
= hwnd
;
876 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
877 (WPARAM
)hDC
, (LPARAM
)hwnd
);
878 if (!hBrush
) /* did the app forget to call defwindowproc ? */
879 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
880 (WPARAM
)hDC
, (LPARAM
)hwnd
);
881 setup_clipping( hwnd
, hDC
);
883 if (style
& BS_LEFTTEXT
)
885 /* magic +4 is what CTL3D expects */
887 rtext
.right
-= checkBoxWidth
+ 4;
888 rbox
.left
= rbox
.right
- checkBoxWidth
;
892 rtext
.left
+= checkBoxWidth
+ 4;
893 rbox
.right
= checkBoxWidth
;
896 /* Since WM_ERASEBKGND does nothing, first prepare background */
897 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
898 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
902 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
904 /* Only adjust rbox when rtext is valid */
905 if (dtFlags
!= (UINT
)-1L)
907 rbox
.top
= rtext
.top
;
908 rbox
.bottom
= rtext
.bottom
;
911 /* Draw the check-box bitmap */
912 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
916 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
917 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
918 else if (state
& BUTTON_3STATE
) flags
= DFCS_BUTTON3STATE
;
919 else flags
= DFCS_BUTTONCHECK
;
921 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) flags
|= DFCS_CHECKED
;
922 if (state
& BUTTON_HIGHLIGHTED
) flags
|= DFCS_PUSHED
;
924 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
926 /* rbox must have the correct height */
927 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
929 if (style
& BS_TOP
) {
931 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
933 rbox
.top
-= -delta
/2 + 1;
934 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
936 } else if (style
& BS_BOTTOM
) {
938 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
940 rbox
.bottom
+= -delta
/2 + 1;
941 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
943 } else { /* Default */
945 int ofs
= (delta
/ 2);
946 rbox
.bottom
-= ofs
+ 1;
947 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
948 } else if (delta
< 0) {
949 int ofs
= (-delta
/ 2);
951 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
955 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
958 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
961 if (action
== ODA_DRAWENTIRE
)
962 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
965 if (action
== ODA_FOCUS
|| (state
& BUTTON_HASFOCUS
))
969 IntersectRect(&rtext
, &rtext
, &client
);
970 DrawFocusRect( hDC
, &rtext
);
975 /**********************************************************************
976 * BUTTON_CheckAutoRadioButton
978 * hwnd is checked, uncheck every other auto radio button in group
980 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
982 HWND parent
, sibling
, start
;
984 parent
= GetParent(hwnd
);
985 /* make sure that starting control is not disabled or invisible */
986 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
990 if ((hwnd
!= sibling
) &&
991 ((GetWindowLongW( sibling
, GWL_STYLE
) & 0x0f) == BS_AUTORADIOBUTTON
))
992 SendMessageW( sibling
, BM_SETCHECK
, BUTTON_UNCHECKED
, 0 );
993 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
994 } while (sibling
!= start
);
998 /**********************************************************************
999 * Group Box Functions
1002 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1009 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1012 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1013 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1014 parent
= GetParent(hwnd
);
1015 if (!parent
) parent
= hwnd
;
1016 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1017 if (!hbr
) /* did the app forget to call defwindowproc ? */
1018 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1019 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1020 setup_clipping( hwnd
, hDC
);
1022 GetClientRect( hwnd
, &rc
);
1025 GetTextMetricsW (hDC
, &tm
);
1026 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1027 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1029 InflateRect(&rc
, -7, 1);
1030 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1032 if (dtFlags
== (UINT
)-1L)
1035 /* Because buttons have CS_PARENTDC class style, there is a chance
1036 * that label will be drawn out of client rect.
1037 * But Windows doesn't clip label's rect, so do I.
1040 /* There is 1-pixel margin at the left, right, and bottom */
1041 rc
.left
--; rc
.right
++; rc
.bottom
++;
1042 FillRect(hDC
, &rc
, hbr
);
1043 rc
.left
++; rc
.right
--; rc
.bottom
--;
1045 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1049 /**********************************************************************
1050 * User Button Functions
1053 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1058 LONG state
= get_button_state( hwnd
);
1061 GetClientRect( hwnd
, &rc
);
1063 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1065 parent
= GetParent(hwnd
);
1066 if (!parent
) parent
= hwnd
;
1067 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1068 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1069 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1070 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1072 FillRect( hDC
, &rc
, hBrush
);
1073 if (action
== ODA_FOCUS
|| (state
& BUTTON_HASFOCUS
))
1074 DrawFocusRect( hDC
, &rc
);
1079 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BUTTON_HASFOCUS
) ? BN_SETFOCUS
: BN_KILLFOCUS
);
1083 BUTTON_NOTIFY_PARENT( hwnd
, (state
& BUTTON_HIGHLIGHTED
) ? BN_HILITE
: BN_UNHILITE
);
1087 BUTTON_NOTIFY_PARENT( hwnd
, BN_PAINT
);
1093 /**********************************************************************
1094 * Ownerdrawn Button Functions
1097 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1099 LONG state
= get_button_state( hwnd
);
1101 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1103 HFONT hFont
, hPrevFont
= 0;
1105 dis
.CtlType
= ODT_BUTTON
;
1108 dis
.itemAction
= action
;
1109 dis
.itemState
= ((state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
1110 ((state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
1111 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1112 dis
.hwndItem
= hwnd
;
1115 GetClientRect( hwnd
, &dis
.rcItem
);
1117 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1118 parent
= GetParent(hwnd
);
1119 if (!parent
) parent
= hwnd
;
1120 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1122 setup_clipping( hwnd
, hDC
);
1124 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1125 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);