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
) style
|= WS_TABSTOP
;
507 else style
&= ~WS_TABSTOP
;
508 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
510 if ((state
& 3) != wParam
)
512 set_button_state( hWnd
, (state
& ~3) | wParam
);
513 paint_button( hWnd
, btn_type
, ODA_SELECT
);
515 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
) && (style
& WS_CHILD
))
516 BUTTON_CheckAutoRadioButton( hWnd
);
520 return get_button_state( hWnd
);
523 state
= get_button_state( hWnd
);
526 if (state
& BUTTON_HIGHLIGHTED
) break;
527 set_button_state( hWnd
, state
| BUTTON_HIGHLIGHTED
);
531 if (!(state
& BUTTON_HIGHLIGHTED
)) break;
532 set_button_state( hWnd
, state
& ~BUTTON_HIGHLIGHTED
);
534 paint_button( hWnd
, btn_type
, ODA_SELECT
);
538 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
541 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
542 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
547 /**********************************************************************
548 * Convert button styles to flags used by DrawText.
550 static UINT
BUTTON_BStoDT( DWORD style
, DWORD ex_style
)
552 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
554 /* "Convert" pushlike buttons to pushbuttons */
555 if (style
& BS_PUSHLIKE
)
558 if (!(style
& BS_MULTILINE
))
559 dtStyle
|= DT_SINGLELINE
;
561 dtStyle
|= DT_WORDBREAK
;
563 switch (style
& BS_CENTER
)
565 case BS_LEFT
: /* DT_LEFT is 0 */ break;
566 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
567 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
569 /* Pushbutton's text is centered by default */
570 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
571 /* all other flavours have left aligned text */
574 if (ex_style
& WS_EX_RIGHT
) dtStyle
= DT_RIGHT
| (dtStyle
& ~(DT_LEFT
| DT_CENTER
));
576 /* DrawText ignores vertical alignment for multiline text,
577 * but we use these flags to align label manually.
579 if (get_button_type(style
) != BS_GROUPBOX
)
581 switch (style
& BS_VCENTER
)
583 case BS_TOP
: /* DT_TOP is 0 */ break;
584 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
585 case BS_VCENTER
: /* fall through */
586 default: dtStyle
|= DT_VCENTER
; break;
590 /* GroupBox's text is always single line and is top aligned. */
591 dtStyle
|= DT_SINGLELINE
;
596 /**********************************************************************
597 * BUTTON_CalcLabelRect
599 * Calculates label's rectangle depending on button style.
601 * Returns flags to be passed to DrawText.
602 * Calculated rectangle doesn't take into account button state
603 * (pushed, etc.). If there is nothing to draw (no text/image) output
604 * rectangle is empty, and return value is (UINT)-1.
606 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
608 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
609 LONG ex_style
= GetWindowLongW( hwnd
, GWL_EXSTYLE
);
613 UINT dtStyle
= BUTTON_BStoDT( style
, ex_style
);
617 /* Calculate label rectangle according to label type */
618 switch (style
& (BS_ICON
|BS_BITMAP
))
621 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
624 HeapFree( GetProcessHeap(), 0, text
);
627 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
628 HeapFree( GetProcessHeap(), 0, text
);
632 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
635 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
637 r
.right
= r
.left
+ bm
.bmWidth
;
638 r
.bottom
= r
.top
+ bm
.bmHeight
;
640 DeleteObject(iconInfo
.hbmColor
);
641 DeleteObject(iconInfo
.hbmMask
);
645 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
648 r
.right
= r
.left
+ bm
.bmWidth
;
649 r
.bottom
= r
.top
+ bm
.bmHeight
;
659 /* Position label inside bounding rectangle according to
660 * alignment flags. (calculated rect is always left-top aligned).
661 * If label is aligned to any side - shift label in opposite
662 * direction to leave extra space for focus rectangle.
664 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
666 case DT_LEFT
: r
.left
++; r
.right
++; break;
667 case DT_CENTER
: n
= r
.right
- r
.left
;
668 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
669 r
.right
= r
.left
+ n
; break;
670 case DT_RIGHT
: n
= r
.right
- r
.left
;
671 r
.right
= rc
->right
- 1;
672 r
.left
= r
.right
- n
;
676 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
678 case DT_TOP
: r
.top
++; r
.bottom
++; break;
679 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
680 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
681 r
.bottom
= r
.top
+ n
; break;
682 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
683 r
.bottom
= rc
->bottom
- 1;
684 r
.top
= r
.bottom
- n
;
693 /**********************************************************************
694 * BUTTON_DrawTextCallback
696 * Callback function used by DrawStateW function.
698 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
706 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
711 /**********************************************************************
714 * Common function for drawing button label.
716 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, const RECT
*rc
)
718 DRAWSTATEPROC lpOutputProc
= NULL
;
722 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
723 LONG state
= get_button_state( hwnd
);
724 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
727 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
728 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
729 * I don't have Win31 on hand to verify that, so I leave it as is.
732 if ((style
& BS_PUSHLIKE
) && (state
& BUTTON_3STATE
))
734 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
738 switch (style
& (BS_ICON
|BS_BITMAP
))
741 /* DST_COMPLEX -- is 0 */
742 lpOutputProc
= BUTTON_DrawTextCallback
;
743 if (!(text
= get_button_text( hwnd
))) return;
750 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
755 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
762 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
763 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
764 HeapFree( GetProcessHeap(), 0, text
);
767 /**********************************************************************
768 * Push Button Functions
770 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
773 UINT dtFlags
, uState
;
777 COLORREF oldTxtColor
;
779 LONG state
= get_button_state( hwnd
);
780 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
781 BOOL pushedState
= (state
& BUTTON_HIGHLIGHTED
);
784 GetClientRect( hwnd
, &rc
);
786 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
787 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
788 parent
= GetParent(hwnd
);
789 if (!parent
) parent
= hwnd
;
790 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
792 setup_clipping( hwnd
, hDC
);
794 hOldPen
= SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
795 hOldBrush
= SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
796 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
798 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
800 if (action
!= ODA_FOCUS
)
801 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
802 InflateRect( &rc
, -1, -1 );
805 /* completely skip the drawing if only focus has changed */
806 if (action
== ODA_FOCUS
) goto draw_focus
;
808 uState
= DFCS_BUTTONPUSH
;
812 else if (pushedState
)
814 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
817 uState
|= DFCS_PUSHED
;
820 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
))
821 uState
|= DFCS_CHECKED
;
823 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
825 /* draw button label */
827 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
829 if (dtFlags
== (UINT
)-1L)
833 OffsetRect(&r
, 1, 1);
835 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
837 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
839 SetTextColor( hDC
, oldTxtColor
);
842 if ((action
== ODA_FOCUS
) ||
843 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
845 InflateRect( &rc
, -2, -2 );
846 DrawFocusRect( hDC
, &rc
);
850 SelectObject( hDC
, hOldPen
);
851 SelectObject( hDC
, hOldBrush
);
852 SetBkMode(hDC
, oldBkMode
);
855 /**********************************************************************
856 * Check Box & Radio Button Functions
859 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
861 RECT rbox
, rtext
, client
;
866 LONG state
= get_button_state( hwnd
);
867 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
870 if (style
& BS_PUSHLIKE
)
872 PB_Paint( hwnd
, hDC
, action
);
876 GetClientRect(hwnd
, &client
);
877 rbox
= rtext
= client
;
879 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
881 parent
= GetParent(hwnd
);
882 if (!parent
) parent
= hwnd
;
883 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
884 (WPARAM
)hDC
, (LPARAM
)hwnd
);
885 if (!hBrush
) /* did the app forget to call defwindowproc ? */
886 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
887 (WPARAM
)hDC
, (LPARAM
)hwnd
);
888 setup_clipping( hwnd
, hDC
);
890 if (style
& BS_LEFTTEXT
)
892 /* magic +4 is what CTL3D expects */
894 rtext
.right
-= checkBoxWidth
+ 4;
895 rbox
.left
= rbox
.right
- checkBoxWidth
;
899 rtext
.left
+= checkBoxWidth
+ 4;
900 rbox
.right
= checkBoxWidth
;
903 /* Since WM_ERASEBKGND does nothing, first prepare background */
904 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
905 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
909 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
911 /* Only adjust rbox when rtext is valid */
912 if (dtFlags
!= (UINT
)-1L)
914 rbox
.top
= rtext
.top
;
915 rbox
.bottom
= rtext
.bottom
;
918 /* Draw the check-box bitmap */
919 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
923 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
924 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
925 else if (state
& BUTTON_3STATE
) flags
= DFCS_BUTTON3STATE
;
926 else flags
= DFCS_BUTTONCHECK
;
928 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) flags
|= DFCS_CHECKED
;
929 if (state
& BUTTON_HIGHLIGHTED
) flags
|= DFCS_PUSHED
;
931 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
933 /* rbox must have the correct height */
934 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
936 if (style
& BS_TOP
) {
938 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
940 rbox
.top
-= -delta
/2 + 1;
941 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
943 } else if (style
& BS_BOTTOM
) {
945 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
947 rbox
.bottom
+= -delta
/2 + 1;
948 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
950 } else { /* Default */
952 int ofs
= (delta
/ 2);
953 rbox
.bottom
-= ofs
+ 1;
954 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
955 } else if (delta
< 0) {
956 int ofs
= (-delta
/ 2);
958 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
962 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
965 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
968 if (action
== ODA_DRAWENTIRE
)
969 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
972 if ((action
== ODA_FOCUS
) ||
973 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
977 IntersectRect(&rtext
, &rtext
, &client
);
978 DrawFocusRect( hDC
, &rtext
);
983 /**********************************************************************
984 * BUTTON_CheckAutoRadioButton
986 * hwnd is checked, uncheck every other auto radio button in group
988 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
990 HWND parent
, sibling
, start
;
992 parent
= GetParent(hwnd
);
993 /* make sure that starting control is not disabled or invisible */
994 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
998 if ((hwnd
!= sibling
) &&
999 ((GetWindowLongW( sibling
, GWL_STYLE
) & 0x0f) == BS_AUTORADIOBUTTON
))
1000 SendMessageW( sibling
, BM_SETCHECK
, BUTTON_UNCHECKED
, 0 );
1001 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
1002 } while (sibling
!= start
);
1006 /**********************************************************************
1007 * Group Box Functions
1010 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1017 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1020 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1021 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1022 parent
= GetParent(hwnd
);
1023 if (!parent
) parent
= hwnd
;
1024 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1025 if (!hbr
) /* did the app forget to call defwindowproc ? */
1026 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
1027 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1028 setup_clipping( hwnd
, hDC
);
1030 GetClientRect( hwnd
, &rc
);
1033 GetTextMetricsW (hDC
, &tm
);
1034 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1035 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1037 InflateRect(&rc
, -7, 1);
1038 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1040 if (dtFlags
== (UINT
)-1L)
1043 /* Because buttons have CS_PARENTDC class style, there is a chance
1044 * that label will be drawn out of client rect.
1045 * But Windows doesn't clip label's rect, so do I.
1048 /* There is 1-pixel margin at the left, right, and bottom */
1049 rc
.left
--; rc
.right
++; rc
.bottom
++;
1050 FillRect(hDC
, &rc
, hbr
);
1051 rc
.left
++; rc
.right
--; rc
.bottom
--;
1053 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1057 /**********************************************************************
1058 * User Button Functions
1061 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1066 LONG state
= get_button_state( hwnd
);
1069 if (action
== ODA_SELECT
) return;
1071 GetClientRect( hwnd
, &rc
);
1073 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1075 parent
= GetParent(hwnd
);
1076 if (!parent
) parent
= hwnd
;
1077 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1078 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1079 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1080 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1082 FillRect( hDC
, &rc
, hBrush
);
1083 if ((action
== ODA_FOCUS
) ||
1084 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
1085 DrawFocusRect( hDC
, &rc
);
1087 BUTTON_NOTIFY_PARENT( hwnd
, BN_PAINT
);
1091 /**********************************************************************
1092 * Ownerdrawn Button Functions
1095 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1097 LONG state
= get_button_state( hwnd
);
1099 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1101 HFONT hFont
, hPrevFont
= 0;
1103 dis
.CtlType
= ODT_BUTTON
;
1106 dis
.itemAction
= action
;
1107 dis
.itemState
= ((state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
1108 ((state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
1109 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1110 dis
.hwndItem
= hwnd
;
1113 GetClientRect( hwnd
, &dis
.rcItem
);
1115 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1116 parent
= GetParent(hwnd
);
1117 if (!parent
) parent
= hwnd
;
1118 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1120 setup_clipping( hwnd
, hDC
);
1122 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1123 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);