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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* GetWindowLong offsets for window extra information */
25 #define STATE_GWL_OFFSET 0
26 #define HFONT_GWL_OFFSET (sizeof(LONG))
27 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET*sizeof(LONG))
28 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET*sizeof(LONG))
30 /* Button state values */
31 #define BUTTON_UNCHECKED 0x00
32 #define BUTTON_CHECKED 0x01
33 #define BUTTON_3STATE 0x02
34 #define BUTTON_HIGHLIGHTED 0x04
35 #define BUTTON_HASFOCUS 0x08
36 #define BUTTON_NSTATES 0x0F
37 /* undocumented flags */
38 #define BUTTON_BTNPRESSED 0x40
39 #define BUTTON_UNKNOWN2 0x20
40 #define BUTTON_UNKNOWN3 0x10
42 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
43 do { /* Notify parent which has created this button control */ \
44 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
45 SendMessageW(GetParent(hWnd), WM_COMMAND, \
46 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
50 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
51 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
52 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
53 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
54 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
55 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
56 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
57 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
58 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
60 #define MAX_BTN_TYPE 12
62 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
64 BUTTON_UNCHECKED
, /* BS_PUSHBUTTON */
65 BUTTON_UNCHECKED
, /* BS_DEFPUSHBUTTON */
66 BUTTON_CHECKED
, /* BS_CHECKBOX */
67 BUTTON_CHECKED
, /* BS_AUTOCHECKBOX */
68 BUTTON_CHECKED
, /* BS_RADIOBUTTON */
69 BUTTON_3STATE
, /* BS_3STATE */
70 BUTTON_3STATE
, /* BS_AUTO3STATE */
71 BUTTON_UNCHECKED
, /* BS_GROUPBOX */
72 BUTTON_UNCHECKED
, /* BS_USERBUTTON */
73 BUTTON_CHECKED
, /* BS_AUTORADIOBUTTON */
74 BUTTON_UNCHECKED
, /* Not defined */
75 BUTTON_UNCHECKED
/* BS_OWNERDRAW */
78 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
80 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
82 PB_Paint
, /* BS_PUSHBUTTON */
83 PB_Paint
, /* BS_DEFPUSHBUTTON */
84 CB_Paint
, /* BS_CHECKBOX */
85 CB_Paint
, /* BS_AUTOCHECKBOX */
86 CB_Paint
, /* BS_RADIOBUTTON */
87 CB_Paint
, /* BS_3STATE */
88 CB_Paint
, /* BS_AUTO3STATE */
89 GB_Paint
, /* BS_GROUPBOX */
90 UB_Paint
, /* BS_USERBUTTON */
91 CB_Paint
, /* BS_AUTORADIOBUTTON */
92 NULL
, /* Not defined */
93 OB_Paint
/* BS_OWNERDRAW */
96 static HBITMAP hbitmapCheckBoxes
= 0;
97 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
100 /*********************************************************************
101 * button class descriptor
103 const struct builtin_class_descr BUTTON_builtin_class
=
106 L
"Button", /* name */
107 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
108 ButtonWndProcW
, /* procW */
109 ButtonWndProcA
, /* procA */
110 NB_EXTRA_BYTES
, /* extra */
111 (LPWSTR
)IDC_ARROW
, /* cursor */
115 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
116 ButtonWndProcA
, /* procA */
117 ButtonWndProcW
, /* procW */
118 NB_EXTRA_BYTES
, /* extra */
119 IDC_ARROW
, /* cursor */
125 __inline
static LONG
get_button_state( HWND hwnd
)
127 return GetWindowLongW( hwnd
, STATE_GWL_OFFSET
);
130 __inline
static void set_button_state( HWND hwnd
, LONG state
)
132 SetWindowLongW( hwnd
, STATE_GWL_OFFSET
, state
);
135 __inline
static HFONT
get_button_font( HWND hwnd
)
137 return (HFONT
)GetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
);
140 __inline
static void set_button_font( HWND hwnd
, HFONT font
)
142 SetWindowLongPtrW( hwnd
, HFONT_GWL_OFFSET
, (LONG
)font
);
145 __inline
static UINT
get_button_type( LONG window_style
)
147 return (window_style
& 0x0f);
150 /* paint a button of any type */
151 __inline
static void paint_button( HWND hwnd
, LONG style
, UINT action
)
153 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
155 HDC hdc
= GetDC( hwnd
);
156 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
157 ReleaseDC( hwnd
, hdc
);
161 /* retrieve the button text; returned buffer must be freed by caller */
162 __inline
static WCHAR
*get_button_text( HWND hwnd
)
165 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
166 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
170 /***********************************************************************
171 * ButtonWndProc_common
173 static LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
174 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
178 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
179 UINT btn_type
= get_button_type( style
);
183 pt
.x
= LOWORD(lParam
);
184 pt
.y
= HIWORD(lParam
);
192 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
193 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
195 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
196 case BS_GROUPBOX
: return DLGC_STATIC
;
197 default: return DLGC_BUTTON
;
201 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
205 if (!hbitmapCheckBoxes
)
208 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
209 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
210 checkBoxWidth
= bmp
.bmWidth
/ 4;
211 checkBoxHeight
= bmp
.bmHeight
/ 3;
213 if (btn_type
>= MAX_BTN_TYPE
)
214 return -1; /* abort */
215 set_button_state( hWnd
, BUTTON_UNCHECKED
);
219 if (btn_type
== BS_OWNERDRAW
)
221 HDC hdc
= (HDC
)wParam
;
224 HWND parent
= GetParent(hWnd
);
225 if (!parent
) parent
= hWnd
;
226 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
227 if (!hBrush
) /* did the app forget to call defwindowproc ? */
228 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
229 (WPARAM
)hdc
, (LPARAM
)hWnd
);
230 GetClientRect(hWnd
, &rc
);
231 FillRect(hdc
, &rc
, hBrush
);
237 if (btnPaintFunc
[btn_type
])
240 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
241 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
242 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
243 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
244 if( !wParam
) EndPaint( hWnd
, &ps
);
249 if (wParam
== VK_SPACE
)
251 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
252 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
256 case WM_LBUTTONDBLCLK
:
257 if(style
& BS_NOTIFY
||
258 btn_type
== BS_RADIOBUTTON
||
259 btn_type
== BS_USERBUTTON
||
260 btn_type
== BS_OWNERDRAW
)
262 BUTTON_NOTIFY_PARENT(hWnd
, BN_DOUBLECLICKED
);
269 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
270 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
274 if (wParam
!= VK_SPACE
)
278 state
= get_button_state( hWnd
);
279 if (!(state
& BUTTON_BTNPRESSED
)) break;
280 state
&= BUTTON_NSTATES
;
281 set_button_state( hWnd
, state
);
282 if (!(state
& BUTTON_HIGHLIGHTED
))
287 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
289 GetClientRect( hWnd
, &rect
);
290 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
292 state
= get_button_state( hWnd
);
295 case BS_AUTOCHECKBOX
:
296 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BUTTON_CHECKED
), 0 );
298 case BS_AUTORADIOBUTTON
:
299 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
302 SendMessageW( hWnd
, BM_SETCHECK
,
303 (state
& BUTTON_3STATE
) ? 0 : ((state
& 3) + 1), 0 );
306 BUTTON_NOTIFY_PARENT(hWnd
, BN_CLICKED
);
310 case WM_CAPTURECHANGED
:
311 state
= get_button_state( hWnd
);
312 if (state
& BUTTON_BTNPRESSED
)
314 state
&= BUTTON_NSTATES
;
315 set_button_state( hWnd
, state
);
316 if (state
& BUTTON_HIGHLIGHTED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
321 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
323 GetClientRect( hWnd
, &rect
);
324 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
330 /* Clear an old text here as Windows does */
331 HDC hdc
= GetDC(hWnd
);
334 HWND parent
= GetParent(hWnd
);
336 if (!parent
) parent
= hWnd
;
337 hbrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
338 (WPARAM
)hdc
, (LPARAM
)hWnd
);
339 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
340 hbrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
341 (WPARAM
)hdc
, (LPARAM
)hWnd
);
343 GetClientRect(hWnd
, &client
);
345 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
346 /* Clip by client rect bounds */
347 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
348 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
349 FillRect(hdc
, &rc
, hbrush
);
350 ReleaseDC(hWnd
, hdc
);
352 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
353 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
354 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
355 InvalidateRect( hWnd
, NULL
, TRUE
);
357 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
358 return 1; /* success. FIXME: check text length */
362 set_button_font( hWnd
, (HFONT
)wParam
);
363 if (lParam
) paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
367 return (LRESULT
)get_button_font( hWnd
);
370 set_button_state( hWnd
, get_button_state(hWnd
) | BUTTON_HASFOCUS
);
371 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
372 if (style
& BS_NOTIFY
)
373 BUTTON_NOTIFY_PARENT(hWnd
, BN_SETFOCUS
);
377 state
= get_button_state( hWnd
);
378 set_button_state( hWnd
, state
& ~BUTTON_HASFOCUS
);
379 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
381 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
383 if (style
& BS_NOTIFY
)
384 BUTTON_NOTIFY_PARENT(hWnd
, BN_KILLFOCUS
);
388 case WM_SYSCOLORCHANGE
:
389 InvalidateRect( hWnd
, NULL
, FALSE
);
396 if ((wParam
& 0x0f) >= MAX_BTN_TYPE
) break;
397 btn_type
= wParam
& 0x0f;
398 style
= (style
& ~0x0f) | btn_type
;
399 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
401 /* Only redraw if lParam flag is set.*/
403 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
408 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
409 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
413 /* Check that image format matches button style */
414 switch (style
& (BS_BITMAP
|BS_ICON
))
417 if (wParam
!= IMAGE_BITMAP
) return 0;
420 if (wParam
!= IMAGE_ICON
) return 0;
425 oldHbitmap
= (HBITMAP
)SetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
426 InvalidateRect( hWnd
, NULL
, FALSE
);
427 return (LRESULT
)oldHbitmap
;
430 return GetWindowLongPtrW( hWnd
, HIMAGE_GWL_OFFSET
);
436 return get_button_state( hWnd
) & 3;
442 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
443 state
= get_button_state( hWnd
);
444 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
446 if (wParam
) style
|= WS_TABSTOP
;
447 else style
&= ~WS_TABSTOP
;
448 SetWindowLongW( hWnd
, GWL_STYLE
, style
);
450 if ((state
& 3) != wParam
)
452 set_button_state( hWnd
, (state
& ~3) | wParam
);
453 paint_button( hWnd
, btn_type
, ODA_SELECT
);
455 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
) && (style
& WS_CHILD
))
456 BUTTON_CheckAutoRadioButton( hWnd
);
463 return get_button_state( hWnd
);
469 state
= get_button_state( hWnd
);
472 if (state
& BUTTON_HIGHLIGHTED
) break;
473 set_button_state( hWnd
, state
| BUTTON_HIGHLIGHTED
);
477 if (!(state
& BUTTON_HIGHLIGHTED
)) break;
478 set_button_state( hWnd
, state
& ~BUTTON_HIGHLIGHTED
);
480 paint_button( hWnd
, btn_type
, ODA_SELECT
);
484 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
487 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
488 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
493 /***********************************************************************
495 * The button window procedure. This is just a wrapper which locks
496 * the passed HWND and calls the real window procedure (with a WND*
497 * pointer pointing to the locked windowstructure).
499 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
501 if (!IsWindow( hWnd
)) return 0;
502 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, TRUE
);
506 /***********************************************************************
509 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
511 if (!IsWindow( hWnd
)) return 0;
512 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, FALSE
);
516 /**********************************************************************
517 * Convert button styles to flags used by DrawText.
518 * TODO: handle WS_EX_RIGHT extended style.
520 static UINT
BUTTON_BStoDT(DWORD style
)
522 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
524 /* "Convert" pushlike buttons to pushbuttons */
525 if (style
& BS_PUSHLIKE
)
528 if (!(style
& BS_MULTILINE
))
529 dtStyle
|= DT_SINGLELINE
;
531 dtStyle
|= DT_WORDBREAK
;
533 switch (style
& BS_CENTER
)
535 case BS_LEFT
: /* DT_LEFT is 0 */ break;
536 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
537 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
539 /* Pushbutton's text is centered by default */
540 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
541 /* all other flavours have left aligned text */
544 /* DrawText ignores vertical alignment for multiline text,
545 * but we use these flags to align label manualy.
547 if (get_button_type(style
) != BS_GROUPBOX
)
549 switch (style
& BS_VCENTER
)
551 case BS_TOP
: /* DT_TOP is 0 */ break;
552 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
553 case BS_VCENTER
: /* fall through */
554 default: dtStyle
|= DT_VCENTER
; break;
558 /* GroupBox's text is always single line and is top aligned. */
559 dtStyle
|= DT_SINGLELINE
;
564 /**********************************************************************
565 * BUTTON_CalcLabelRect
567 * Calculates label's rectangle depending on button style.
569 * Returns flags to be passed to DrawText.
570 * Calculated rectangle doesn't take into account button state
571 * (pushed, etc.). If there is nothing to draw (no text/image) output
572 * rectangle is empty, and return value is (UINT)-1.
574 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
576 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
580 UINT dtStyle
= BUTTON_BStoDT(style
);
584 /* Calculate label rectangle according to label type */
585 switch (style
& (BS_ICON
|BS_BITMAP
))
588 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
591 HeapFree( GetProcessHeap(), 0, text
);
594 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
595 HeapFree( GetProcessHeap(), 0, text
);
599 if (!GetIconInfo((HICON
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
602 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
604 r
.right
= r
.left
+ bm
.bmWidth
;
605 r
.bottom
= r
.top
+ bm
.bmHeight
;
607 DeleteObject(iconInfo
.hbmColor
);
608 DeleteObject(iconInfo
.hbmMask
);
612 if (!GetObjectW( (HANDLE
)GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
615 r
.right
= r
.left
+ bm
.bmWidth
;
616 r
.bottom
= r
.top
+ bm
.bmHeight
;
623 return (UINT
)(LONG
)-1;
626 /* Position label inside bounding rectangle according to
627 * alignment flags. (calculated rect is always left-top aligned).
628 * If label is aligned to any side - shift label in opposite
629 * direction to leave extra space for focus rectangle.
631 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
633 case DT_LEFT
: r
.left
++; r
.right
++; break;
634 case DT_CENTER
: n
= r
.right
- r
.left
;
635 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
636 r
.right
= r
.left
+ n
; break;
637 case DT_RIGHT
: n
= r
.right
- r
.left
;
638 r
.right
= rc
->right
- 1;
639 r
.left
= r
.right
- n
;
643 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
645 case DT_TOP
: r
.top
++; r
.bottom
++; break;
646 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
647 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
648 r
.bottom
= r
.top
+ n
; break;
649 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
650 r
.bottom
= rc
->bottom
- 1;
651 r
.top
= r
.bottom
- n
;
660 /**********************************************************************
661 * BUTTON_DrawTextCallback
663 * Callback function used by DrawStateW function.
665 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
673 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
678 /**********************************************************************
681 * Common function for drawing button label.
683 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, RECT
*rc
)
685 DRAWSTATEPROC lpOutputProc
= NULL
;
689 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
690 LONG state
= get_button_state( hwnd
);
691 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
694 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
695 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
696 * I don't have Win31 on hand to verify that, so I leave it as is.
699 if ((style
& BS_PUSHLIKE
) && (state
& BUTTON_3STATE
))
701 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
705 switch (style
& (BS_ICON
|BS_BITMAP
))
708 /* DST_COMPLEX -- is 0 */
709 lpOutputProc
= BUTTON_DrawTextCallback
;
710 if (!(text
= get_button_text( hwnd
))) return;
712 wp
= (WPARAM
)dtFlags
;
717 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
722 lp
= GetWindowLongPtrW( hwnd
, HIMAGE_GWL_OFFSET
);
729 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
730 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
731 HeapFree( GetProcessHeap(), 0, text
);
734 /**********************************************************************
735 * Push Button Functions
737 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
739 RECT rc
, focus_rect
, r
;
740 UINT dtFlags
, uState
;
744 COLORREF oldTxtColor
;
746 LONG state
= get_button_state( hwnd
);
747 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
748 BOOL pushedState
= (state
& BUTTON_HIGHLIGHTED
);
751 GetClientRect( hwnd
, &rc
);
753 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
754 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
755 parent
= GetParent(hwnd
);
756 if (!parent
) parent
= hwnd
;
757 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
759 hOldPen
= (HPEN
)SelectObject(hDC
, GetSysColorPen(COLOR_WINDOWFRAME
));
761 hOldPen
= (HPEN
)SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
763 hOldBrush
=(HBRUSH
)SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
764 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
766 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
768 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
769 InflateRect( &rc
, -1, -1 );
772 uState
= DFCS_BUTTONPUSH
| DFCS_ADJUSTRECT
;
776 else if (pushedState
)
778 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
781 uState
|= DFCS_PUSHED
;
784 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
))
785 uState
|= DFCS_CHECKED
;
787 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
791 /* draw button label */
793 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
795 if (dtFlags
== (UINT
)-1L)
799 OffsetRect(&r
, 1, 1);
801 IntersectClipRect(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
803 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
805 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
807 SetTextColor( hDC
, oldTxtColor
);
809 if (state
& BUTTON_HASFOCUS
)
811 InflateRect( &focus_rect
, -1, -1 );
812 IntersectRect(&focus_rect
, &focus_rect
, &rc
);
813 DrawFocusRect( hDC
, &focus_rect
);
817 SelectObject( hDC
, hOldPen
);
818 SelectObject( hDC
, hOldBrush
);
819 SetBkMode(hDC
, oldBkMode
);
823 /**********************************************************************
824 * Check Box & Radio Button Functions
827 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
829 RECT rbox
, rtext
, client
;
834 LONG state
= get_button_state( hwnd
);
835 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
838 if (style
& BS_PUSHLIKE
)
840 PB_Paint( hwnd
, hDC
, action
);
844 GetClientRect(hwnd
, &client
);
845 rbox
= rtext
= client
;
847 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
849 parent
= GetParent(hwnd
);
850 if (!parent
) parent
= hwnd
;
851 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
852 (WPARAM
)hDC
, (LPARAM
)hwnd
);
853 if (!hBrush
) /* did the app forget to call defwindowproc ? */
854 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
855 (WPARAM
)hDC
, (LPARAM
)hwnd
);
857 if (style
& BS_LEFTTEXT
)
859 /* magic +4 is what CTL3D expects */
861 rtext
.right
-= checkBoxWidth
+ 4;
862 rbox
.left
= rbox
.right
- checkBoxWidth
;
866 rtext
.left
+= checkBoxWidth
+ 4;
867 rbox
.right
= checkBoxWidth
;
870 /* Since WM_ERASEBKGND does nothing, first prepare background */
871 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
872 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
876 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
878 /* Only adjust rbox when rtext is valid */
879 if (dtFlags
!= (UINT
)-1L)
881 rbox
.top
= rtext
.top
;
882 rbox
.bottom
= rtext
.bottom
;
885 /* Draw the check-box bitmap */
886 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
890 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
891 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
892 else if (state
& BUTTON_3STATE
) flags
= DFCS_BUTTON3STATE
;
893 else flags
= DFCS_BUTTONCHECK
;
895 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) flags
|= DFCS_CHECKED
;
896 if (state
& BUTTON_HIGHLIGHTED
) flags
|= DFCS_PUSHED
;
898 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
900 /* rbox must have the correct height */
901 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
903 if (style
& BS_TOP
) {
905 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
907 rbox
.top
-= -delta
/2 + 1;
908 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
910 } else if (style
& BS_BOTTOM
) {
912 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
914 rbox
.bottom
+= -delta
/2 + 1;
915 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
917 } else { /* Default */
919 int ofs
= (delta
/ 2);
920 rbox
.bottom
-= ofs
+ 1;
921 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
922 } else if (delta
< 0) {
923 int ofs
= (-delta
/ 2);
925 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
929 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
932 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
935 IntersectClipRect(hDC
, client
.left
, client
.top
, client
.right
, client
.bottom
);
937 if (action
== ODA_DRAWENTIRE
)
938 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
941 if ((action
== ODA_FOCUS
) ||
942 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
946 IntersectRect(&rtext
, &rtext
, &client
);
947 DrawFocusRect( hDC
, &rtext
);
952 /**********************************************************************
953 * BUTTON_CheckAutoRadioButton
955 * hwnd is checked, uncheck every other auto radio button in group
957 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
959 HWND parent
, sibling
, start
;
961 parent
= GetParent(hwnd
);
962 /* make sure that starting control is not disabled or invisible */
963 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
967 if ((hwnd
!= sibling
) &&
968 ((GetWindowLongW( sibling
, GWL_STYLE
) & 0x0f) == BS_AUTORADIOBUTTON
))
969 SendMessageW( sibling
, BM_SETCHECK
, BUTTON_UNCHECKED
, 0 );
970 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
971 } while (sibling
!= start
);
975 /**********************************************************************
976 * Group Box Functions
979 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
986 LONG style
= GetWindowLongW( hwnd
, GWL_STYLE
);
989 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
990 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
991 parent
= GetParent(hwnd
);
992 if (!parent
) parent
= hwnd
;
993 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
994 if (!hbr
) /* did the app forget to call defwindowproc ? */
995 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
996 (WPARAM
)hDC
, (LPARAM
)hwnd
);
998 GetClientRect( hwnd
, &rc
);
1001 GetTextMetricsW (hDC
, &tm
);
1002 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1003 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1005 InflateRect(&rc
, -7, 1);
1006 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1008 if (dtFlags
== (UINT
)-1L)
1011 /* Because buttons have CS_PARENTDC class style, there is a chance
1012 * that label will be drawn out of client rect.
1013 * But Windows doesn't clip label's rect, so do I.
1016 /* There is 1-pixel marging at the left, right, and bottom */
1017 rc
.left
--; rc
.right
++; rc
.bottom
++;
1018 FillRect(hDC
, &rc
, hbr
);
1019 rc
.left
++; rc
.right
--; rc
.bottom
--;
1021 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1025 /**********************************************************************
1026 * User Button Functions
1029 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1034 LONG state
= get_button_state( hwnd
);
1037 if (action
== ODA_SELECT
) return;
1039 GetClientRect( hwnd
, &rc
);
1041 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1043 parent
= GetParent(hwnd
);
1044 if (!parent
) parent
= hwnd
;
1045 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1046 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1047 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1048 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1050 FillRect( hDC
, &rc
, hBrush
);
1051 if ((action
== ODA_FOCUS
) ||
1052 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
1053 DrawFocusRect( hDC
, &rc
);
1056 /**********************************************************************
1057 * Ownerdrawn Button Functions
1060 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1062 LONG state
= get_button_state( hwnd
);
1066 LONG_PTR id
= GetWindowLongPtrW( hwnd
, GWLP_ID
);
1068 HFONT hFont
, hPrevFont
= 0;
1070 dis
.CtlType
= ODT_BUTTON
;
1073 dis
.itemAction
= action
;
1074 dis
.itemState
= ((state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
1075 ((state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
1076 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1077 dis
.hwndItem
= hwnd
;
1080 GetClientRect( hwnd
, &dis
.rcItem
);
1082 clipRegion
= CreateRectRgnIndirect(&dis
.rcItem
);
1083 if (GetClipRgn(hDC
, clipRegion
) != 1)
1085 DeleteObject(clipRegion
);
1088 clipRect
= dis
.rcItem
;
1089 DPtoLP(hDC
, (LPPOINT
) &clipRect
, 2);
1090 IntersectClipRect(hDC
, clipRect
.left
, clipRect
.top
, clipRect
.right
, clipRect
.bottom
);
1092 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1093 parent
= GetParent(hwnd
);
1094 if (!parent
) parent
= hwnd
;
1095 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1096 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1097 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1098 SelectClipRgn(hDC
, clipRegion
);