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
29 #include "wine/winuser16.h"
33 /* GetWindowLong offsets for window extra information */
34 #define STATE_GWL_OFFSET 0
35 #define HFONT_GWL_OFFSET (sizeof(LONG))
36 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
37 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
39 /* Button state values */
40 #define BUTTON_UNCHECKED 0x00
41 #define BUTTON_CHECKED 0x01
42 #define BUTTON_3STATE 0x02
43 #define BUTTON_HIGHLIGHTED 0x04
44 #define BUTTON_HASFOCUS 0x08
45 #define BUTTON_NSTATES 0x0F
46 /* undocumented flags */
47 #define BUTTON_BTNPRESSED 0x40
48 #define BUTTON_UNKNOWN2 0x20
49 #define BUTTON_UNKNOWN3 0x10
51 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
52 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
53 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
54 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
55 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
56 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
57 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
58 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
59 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
61 #define MAX_BTN_TYPE 12
63 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
65 BUTTON_UNCHECKED
, /* BS_PUSHBUTTON */
66 BUTTON_UNCHECKED
, /* BS_DEFPUSHBUTTON */
67 BUTTON_CHECKED
, /* BS_CHECKBOX */
68 BUTTON_CHECKED
, /* BS_AUTOCHECKBOX */
69 BUTTON_CHECKED
, /* BS_RADIOBUTTON */
70 BUTTON_3STATE
, /* BS_3STATE */
71 BUTTON_3STATE
, /* BS_AUTO3STATE */
72 BUTTON_UNCHECKED
, /* BS_GROUPBOX */
73 BUTTON_UNCHECKED
, /* BS_USERBUTTON */
74 BUTTON_CHECKED
, /* BS_AUTORADIOBUTTON */
75 BUTTON_UNCHECKED
, /* Not defined */
76 BUTTON_UNCHECKED
/* BS_OWNERDRAW */
79 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
81 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
83 PB_Paint
, /* BS_PUSHBUTTON */
84 PB_Paint
, /* BS_DEFPUSHBUTTON */
85 CB_Paint
, /* BS_CHECKBOX */
86 CB_Paint
, /* BS_AUTOCHECKBOX */
87 CB_Paint
, /* BS_RADIOBUTTON */
88 CB_Paint
, /* BS_3STATE */
89 CB_Paint
, /* BS_AUTO3STATE */
90 GB_Paint
, /* BS_GROUPBOX */
91 UB_Paint
, /* BS_USERBUTTON */
92 CB_Paint
, /* BS_AUTORADIOBUTTON */
93 NULL
, /* Not defined */
94 OB_Paint
/* BS_OWNERDRAW */
97 static HBITMAP hbitmapCheckBoxes
= 0;
98 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
101 /*********************************************************************
102 * button class descriptor
104 const struct builtin_class_descr BUTTON_builtin_class
=
107 CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
108 ButtonWndProcA
, /* procA */
109 ButtonWndProcW
, /* procW */
110 NB_EXTRA_BYTES
, /* extra */
111 IDC_ARROW
, /* cursor */
116 inline static LONG
get_button_state( HWND hwnd
)
118 return GetWindowLongA( hwnd
, STATE_GWL_OFFSET
);
121 inline static void set_button_state( HWND hwnd
, LONG state
)
123 SetWindowLongA( hwnd
, STATE_GWL_OFFSET
, state
);
126 inline static HFONT
get_button_font( HWND hwnd
)
128 return (HFONT
)GetWindowLongPtrA( hwnd
, HFONT_GWL_OFFSET
);
131 inline static void set_button_font( HWND hwnd
, HFONT font
)
133 SetWindowLongPtrA( hwnd
, HFONT_GWL_OFFSET
, (LONG_PTR
)font
);
136 inline static UINT
get_button_type( LONG window_style
)
138 return (window_style
& 0x0f);
141 /* paint a button of any type */
142 inline static void paint_button( HWND hwnd
, LONG style
, UINT action
)
144 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
146 HDC hdc
= GetDC( hwnd
);
147 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
148 ReleaseDC( hwnd
, hdc
);
152 /* retrieve the button text; returned buffer must be freed by caller */
153 inline static WCHAR
*get_button_text( HWND hwnd
)
156 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
157 if (buffer
) InternalGetWindowText( hwnd
, buffer
, len
+ 1 );
161 /***********************************************************************
162 * ButtonWndProc_common
164 static LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
165 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
169 LONG style
= GetWindowLongA( hWnd
, GWL_STYLE
);
170 UINT btn_type
= get_button_type( style
);
174 pt
.x
= LOWORD(lParam
);
175 pt
.y
= HIWORD(lParam
);
182 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
183 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
185 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
186 default: return DLGC_BUTTON
;
190 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
194 if (!hbitmapCheckBoxes
)
197 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
198 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
199 checkBoxWidth
= bmp
.bmWidth
/ 4;
200 checkBoxHeight
= bmp
.bmHeight
/ 3;
202 if (btn_type
>= MAX_BTN_TYPE
)
203 return -1; /* abort */
204 set_button_state( hWnd
, BUTTON_UNCHECKED
);
208 if (btn_type
== BS_OWNERDRAW
)
210 HDC hdc
= (HDC
)wParam
;
213 HWND parent
= GetParent(hWnd
);
214 if (!parent
) parent
= hWnd
;
215 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hdc
, (LPARAM
)hWnd
);
216 if (!hBrush
) /* did the app forget to call defwindowproc ? */
217 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
218 (WPARAM
)hdc
, (LPARAM
)hWnd
);
219 GetClientRect(hWnd
, &rc
);
220 FillRect(hdc
, &rc
, hBrush
);
225 if (btnPaintFunc
[btn_type
])
228 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
229 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
230 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
231 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
232 if( !wParam
) EndPaint( hWnd
, &ps
);
237 if (wParam
== VK_SPACE
)
239 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
240 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
244 case WM_LBUTTONDBLCLK
:
245 if(style
& BS_NOTIFY
||
246 btn_type
== BS_RADIOBUTTON
||
247 btn_type
== BS_USERBUTTON
||
248 btn_type
== BS_OWNERDRAW
)
250 SendMessageW( GetParent(hWnd
), WM_COMMAND
,
251 MAKEWPARAM( GetWindowLongPtrA(hWnd
,GWLP_ID
), BN_DOUBLECLICKED
),
259 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
260 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
264 if (wParam
!= VK_SPACE
)
268 state
= get_button_state( hWnd
);
269 if (!(state
& BUTTON_BTNPRESSED
)) break;
270 state
&= BUTTON_NSTATES
;
271 set_button_state( hWnd
, state
);
272 if (!(state
& BUTTON_HIGHLIGHTED
))
277 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
279 GetClientRect( hWnd
, &rect
);
280 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
282 state
= get_button_state( hWnd
);
285 case BS_AUTOCHECKBOX
:
286 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BUTTON_CHECKED
), 0 );
288 case BS_AUTORADIOBUTTON
:
289 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
292 SendMessageW( hWnd
, BM_SETCHECK
,
293 (state
& BUTTON_3STATE
) ? 0 : ((state
& 3) + 1), 0 );
296 SendMessageW( GetParent(hWnd
), WM_COMMAND
,
297 MAKEWPARAM( GetWindowLongPtrA(hWnd
,GWLP_ID
), BN_CLICKED
), (LPARAM
)hWnd
);
301 case WM_CAPTURECHANGED
:
302 state
= get_button_state( hWnd
);
303 if (state
& BUTTON_BTNPRESSED
)
305 state
&= BUTTON_NSTATES
;
306 set_button_state( hWnd
, state
);
307 if (state
& BUTTON_HIGHLIGHTED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
312 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
314 GetClientRect( hWnd
, &rect
);
315 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
321 /* Clear an old text here as Windows does */
322 HDC hdc
= GetDC(hWnd
);
325 HWND parent
= GetParent(hWnd
);
327 if (!parent
) parent
= hWnd
;
328 hbrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
329 (WPARAM
)hdc
, (LPARAM
)hWnd
);
330 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
331 hbrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
332 (WPARAM
)hdc
, (LPARAM
)hWnd
);
334 GetClientRect(hWnd
, &client
);
336 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
337 /* Clip by client rect bounds */
338 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
339 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
340 FillRect(hdc
, &rc
, hbrush
);
341 ReleaseDC(hWnd
, hdc
);
343 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
344 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
345 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
346 InvalidateRect( hWnd
, NULL
, TRUE
);
348 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
349 return 1; /* success. FIXME: check text length */
353 set_button_font( hWnd
, (HFONT
)wParam
);
354 if (lParam
) paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
358 return (LRESULT
)get_button_font( hWnd
);
361 set_button_state( hWnd
, get_button_state(hWnd
) | BUTTON_HASFOCUS
);
362 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
366 state
= get_button_state( hWnd
);
367 set_button_state( hWnd
, state
& ~BUTTON_HASFOCUS
);
368 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
370 if ((state
& BUTTON_BTNPRESSED
) && GetCapture() == hWnd
)
374 case WM_SYSCOLORCHANGE
:
375 InvalidateRect( hWnd
, NULL
, FALSE
);
380 if ((wParam
& 0x0f) >= MAX_BTN_TYPE
) break;
381 btn_type
= wParam
& 0x0f;
382 style
= (style
& ~0x0f) | btn_type
;
383 SetWindowLongA( hWnd
, GWL_STYLE
, style
);
385 /* Only redraw if lParam flag is set.*/
387 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
392 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
393 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
397 /* Check that image format matches button style */
398 switch (style
& (BS_BITMAP
|BS_ICON
))
401 if (wParam
!= IMAGE_BITMAP
) return 0;
404 if (wParam
!= IMAGE_ICON
) return 0;
409 oldHbitmap
= (HBITMAP
)SetWindowLongA( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
410 InvalidateRect( hWnd
, NULL
, FALSE
);
411 return (LRESULT
)oldHbitmap
;
414 return GetWindowLongPtrA( hWnd
, HIMAGE_GWL_OFFSET
);
418 return get_button_state( hWnd
) & 3;
422 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
423 state
= get_button_state( hWnd
);
424 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
426 if (wParam
) style
|= WS_TABSTOP
;
427 else style
&= ~WS_TABSTOP
;
428 SetWindowLongA( hWnd
, GWL_STYLE
, style
);
430 if ((state
& 3) != wParam
)
432 set_button_state( hWnd
, (state
& ~3) | wParam
);
433 paint_button( hWnd
, btn_type
, ODA_SELECT
);
435 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
) && (style
& WS_CHILD
))
436 BUTTON_CheckAutoRadioButton( hWnd
);
441 return get_button_state( hWnd
);
445 state
= get_button_state( hWnd
);
448 if (state
& BUTTON_HIGHLIGHTED
) break;
449 set_button_state( hWnd
, state
| BUTTON_HIGHLIGHTED
);
453 if (!(state
& BUTTON_HIGHLIGHTED
)) break;
454 set_button_state( hWnd
, state
& ~BUTTON_HIGHLIGHTED
);
456 paint_button( hWnd
, btn_type
, ODA_SELECT
);
460 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
463 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
464 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
469 /***********************************************************************
471 * The button window procedure. This is just a wrapper which locks
472 * the passed HWND and calls the real window procedure (with a WND*
473 * pointer pointing to the locked windowstructure).
475 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
477 if (!IsWindow( hWnd
)) return 0;
478 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, TRUE
);
482 /***********************************************************************
485 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
487 if (!IsWindow( hWnd
)) return 0;
488 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, FALSE
);
492 /**********************************************************************
493 * Convert button styles to flags used by DrawText.
494 * TODO: handle WS_EX_RIGHT extended style.
496 static UINT
BUTTON_BStoDT(DWORD style
)
498 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
500 /* "Convert" pushlike buttons to pushbuttons */
501 if (style
& BS_PUSHLIKE
)
504 if (!(style
& BS_MULTILINE
))
505 dtStyle
|= DT_SINGLELINE
;
507 dtStyle
|= DT_WORDBREAK
;
509 switch (style
& BS_CENTER
)
511 case BS_LEFT
: /* DT_LEFT is 0 */ break;
512 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
513 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
515 /* Pushbutton's text is centered by default */
516 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
517 /* all other flavours have left aligned text */
520 /* DrawText ignores vertical alignment for multiline text,
521 * but we use these flags to align label manualy.
523 if (get_button_type(style
) != BS_GROUPBOX
)
525 switch (style
& BS_VCENTER
)
527 case BS_TOP
: /* DT_TOP is 0 */ break;
528 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
529 case BS_VCENTER
: /* fall through */
530 default: dtStyle
|= DT_VCENTER
; break;
534 /* GroupBox's text is always single line and is top aligned. */
535 dtStyle
|= DT_SINGLELINE
;
540 /**********************************************************************
541 * BUTTON_CalcLabelRect
543 * Calculates label's rectangle depending on button style.
545 * Returns flags to be passed to DrawText.
546 * Calculated rectangle doesn't take into account button state
547 * (pushed, etc.). If there is nothing to draw (no text/image) output
548 * rectangle is empty, and return value is (UINT)-1.
550 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
552 LONG style
= GetWindowLongA( hwnd
, GWL_STYLE
);
556 UINT dtStyle
= BUTTON_BStoDT(style
);
560 /* Calculate label rectangle according to label type */
561 switch (style
& (BS_ICON
|BS_BITMAP
))
564 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
567 HeapFree( GetProcessHeap(), 0, text
);
570 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
571 HeapFree( GetProcessHeap(), 0, text
);
575 if (!GetIconInfo((HICON
)GetWindowLongPtrA( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
578 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
580 r
.right
= r
.left
+ bm
.bmWidth
;
581 r
.bottom
= r
.top
+ bm
.bmHeight
;
583 DeleteObject(iconInfo
.hbmColor
);
584 DeleteObject(iconInfo
.hbmMask
);
588 if (!GetObjectW( (HANDLE
)GetWindowLongPtrA( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
591 r
.right
= r
.left
+ bm
.bmWidth
;
592 r
.bottom
= r
.top
+ bm
.bmHeight
;
599 return (UINT
)(LONG
)-1;
602 /* Position label inside bounding rectangle according to
603 * alignment flags. (calculated rect is always left-top aligned).
604 * If label is aligned to any side - shift label in opposite
605 * direction to leave extra space for focus rectangle.
607 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
609 case DT_LEFT
: r
.left
++; r
.right
++; break;
610 case DT_CENTER
: n
= r
.right
- r
.left
;
611 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
612 r
.right
= r
.left
+ n
; break;
613 case DT_RIGHT
: n
= r
.right
- r
.left
;
614 r
.right
= rc
->right
- 1;
615 r
.left
= r
.right
- n
;
619 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
621 case DT_TOP
: r
.top
++; r
.bottom
++; break;
622 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
623 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
624 r
.bottom
= r
.top
+ n
; break;
625 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
626 r
.bottom
= rc
->bottom
- 1;
627 r
.top
= r
.bottom
- n
;
636 /**********************************************************************
637 * BUTTON_DrawTextCallback
639 * Callback function used by DrawStateW function.
641 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
649 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
654 /**********************************************************************
657 * Common function for drawing button label.
659 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, RECT
*rc
)
661 DRAWSTATEPROC lpOutputProc
= NULL
;
665 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
666 LONG state
= get_button_state( hwnd
);
667 LONG style
= GetWindowLongA( hwnd
, GWL_STYLE
);
670 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
671 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
672 * I don't have Win31 on hand to verify that, so I leave it as is.
675 if ((style
& BS_PUSHLIKE
) && (state
& BUTTON_3STATE
))
677 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
681 switch (style
& (BS_ICON
|BS_BITMAP
))
684 /* DST_COMPLEX -- is 0 */
685 lpOutputProc
= BUTTON_DrawTextCallback
;
686 if (!(text
= get_button_text( hwnd
))) return;
688 wp
= (WPARAM
)dtFlags
;
693 lp
= GetWindowLongPtrA( hwnd
, HIMAGE_GWL_OFFSET
);
698 lp
= GetWindowLongPtrA( hwnd
, HIMAGE_GWL_OFFSET
);
705 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
706 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
707 if (text
) HeapFree( GetProcessHeap(), 0, text
);
710 /**********************************************************************
711 * Push Button Functions
713 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
715 RECT rc
, focus_rect
, r
;
716 UINT dtFlags
, uState
;
721 COLORREF oldTxtColor
;
723 LONG state
= get_button_state( hwnd
);
724 LONG style
= GetWindowLongA( hwnd
, GWL_STYLE
);
725 BOOL pushedState
= (state
& BUTTON_HIGHLIGHTED
);
728 GetClientRect( hwnd
, &rc
);
730 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
731 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
732 parent
= GetParent(hwnd
);
733 if (!parent
) parent
= hwnd
;
734 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
735 hOldPen
= (HPEN
)SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
736 hOldBrush
=(HBRUSH
)SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
737 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
739 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
741 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
742 InflateRect( &rc
, -1, -1 );
745 uState
= DFCS_BUTTONPUSH
| DFCS_ADJUSTRECT
;
749 else if (pushedState
)
751 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
754 uState
|= DFCS_PUSHED
;
757 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
))
758 uState
|= DFCS_CHECKED
;
760 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
764 /* draw button label */
766 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
768 if (dtFlags
== (UINT
)-1L)
772 OffsetRect(&r
, 1, 1);
774 hRgn
= CreateRectRgn(rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
775 SelectClipRgn(hDC
, hRgn
);
777 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
779 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
781 SetTextColor( hDC
, oldTxtColor
);
782 SelectClipRgn(hDC
, 0);
785 if (state
& BUTTON_HASFOCUS
)
787 InflateRect( &focus_rect
, -1, -1 );
788 IntersectRect(&focus_rect
, &focus_rect
, &rc
);
789 DrawFocusRect( hDC
, &focus_rect
);
793 SelectObject( hDC
, hOldPen
);
794 SelectObject( hDC
, hOldBrush
);
795 SetBkMode(hDC
, oldBkMode
);
798 /**********************************************************************
799 * Check Box & Radio Button Functions
802 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
804 RECT rbox
, rtext
, client
;
810 LONG state
= get_button_state( hwnd
);
811 LONG style
= GetWindowLongA( hwnd
, GWL_STYLE
);
814 if (style
& BS_PUSHLIKE
)
816 PB_Paint( hwnd
, hDC
, action
);
820 GetClientRect(hwnd
, &client
);
821 rbox
= rtext
= client
;
823 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
825 parent
= GetParent(hwnd
);
826 if (!parent
) parent
= hwnd
;
827 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
,
828 (WPARAM
)hDC
, (LPARAM
)hwnd
);
829 if (!hBrush
) /* did the app forget to call defwindowproc ? */
830 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
831 (WPARAM
)hDC
, (LPARAM
)hwnd
);
833 if (style
& BS_LEFTTEXT
)
835 /* magic +4 is what CTL3D expects */
837 rtext
.right
-= checkBoxWidth
+ 4;
838 rbox
.left
= rbox
.right
- checkBoxWidth
;
842 rtext
.left
+= checkBoxWidth
+ 4;
843 rbox
.right
= checkBoxWidth
;
846 /* Since WM_ERASEBKGND does nothing, first prepare background */
847 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
848 if (action
== ODA_DRAWENTIRE
) FillRect( hDC
, &client
, hBrush
);
852 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
854 rbox
.top
= rtext
.top
;
855 rbox
.bottom
= rtext
.bottom
;
856 /* Draw the check-box bitmap */
857 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
861 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
862 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
863 else if (state
& BUTTON_3STATE
) flags
= DFCS_BUTTON3STATE
;
864 else flags
= DFCS_BUTTONCHECK
;
866 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) flags
|= DFCS_CHECKED
;
867 if (state
& BUTTON_HIGHLIGHTED
) flags
|= DFCS_PUSHED
;
869 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
871 /* rbox must have the correct height */
872 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
874 if (style
& BS_TOP
) {
876 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
878 rbox
.top
-= -delta
/2 + 1;
879 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
881 } else if (style
& BS_BOTTOM
) {
883 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
885 rbox
.bottom
+= -delta
/2 + 1;
886 rbox
.top
= rbox
.bottom
-= checkBoxHeight
;
888 } else { /* Default */
890 int ofs
= (delta
/ 2);
891 rbox
.bottom
-= ofs
+ 1;
892 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
893 } else if (delta
< 0) {
894 int ofs
= (-delta
/ 2);
896 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
900 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
903 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
905 hRgn
= CreateRectRgn(client
.left
, client
.top
, client
.right
, client
.bottom
);
906 SelectClipRgn(hDC
, hRgn
);
909 if (action
== ODA_DRAWENTIRE
)
910 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
913 if ((action
== ODA_FOCUS
) ||
914 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
918 IntersectRect(&rtext
, &rtext
, &client
);
919 DrawFocusRect( hDC
, &rtext
);
921 SelectClipRgn(hDC
, 0);
925 /**********************************************************************
926 * BUTTON_CheckAutoRadioButton
928 * hwnd is checked, uncheck every other auto radio button in group
930 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
932 HWND parent
, sibling
, start
;
934 parent
= GetParent(hwnd
);
935 /* make sure that starting control is not disabled or invisible */
936 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
940 if ((hwnd
!= sibling
) &&
941 ((GetWindowLongA( sibling
, GWL_STYLE
) & 0x0f) == BS_AUTORADIOBUTTON
))
942 SendMessageW( sibling
, BM_SETCHECK
, BUTTON_UNCHECKED
, 0 );
943 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
944 } while (sibling
!= start
);
948 /**********************************************************************
949 * Group Box Functions
952 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
959 LONG style
= GetWindowLongA( hwnd
, GWL_STYLE
);
962 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
963 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
964 parent
= GetParent(hwnd
);
965 if (!parent
) parent
= hwnd
;
966 hbr
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORSTATIC
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
967 if (!hbr
) /* did the app forget to call defwindowproc ? */
968 hbr
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORSTATIC
,
969 (WPARAM
)hDC
, (LPARAM
)hwnd
);
971 GetClientRect( hwnd
, &rc
);
974 GetTextMetricsW (hDC
, &tm
);
975 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
976 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
978 InflateRect(&rc
, -7, 1);
979 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
981 if (dtFlags
== (UINT
)-1L)
984 /* Because buttons have CS_PARENTDC class style, there is a chance
985 * that label will be drawn out of client rect.
986 * But Windows doesn't clip label's rect, so do I.
989 /* There is 1-pixel marging at the left, right, and bottom */
990 rc
.left
--; rc
.right
++; rc
.bottom
++;
991 FillRect(hDC
, &rc
, hbr
);
992 rc
.left
++; rc
.right
--; rc
.bottom
--;
994 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
998 /**********************************************************************
999 * User Button Functions
1002 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1007 LONG state
= get_button_state( hwnd
);
1010 if (action
== ODA_SELECT
) return;
1012 GetClientRect( hwnd
, &rc
);
1014 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1016 parent
= GetParent(hwnd
);
1017 if (!parent
) parent
= hwnd
;
1018 hBrush
= (HBRUSH
)SendMessageW(parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1019 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1020 hBrush
= (HBRUSH
)DefWindowProcW(parent
, WM_CTLCOLORBTN
,
1021 (WPARAM
)hDC
, (LPARAM
)hwnd
);
1023 FillRect( hDC
, &rc
, hBrush
);
1024 if ((action
== ODA_FOCUS
) ||
1025 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
1026 DrawFocusRect( hDC
, &rc
);
1030 /**********************************************************************
1031 * Ownerdrawn Button Functions
1034 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1036 LONG state
= get_button_state( hwnd
);
1040 LONG_PTR id
= GetWindowLongPtrA( hwnd
, GWLP_ID
);
1042 HFONT hFont
, hPrevFont
= 0;
1044 dis
.CtlType
= ODT_BUTTON
;
1047 dis
.itemAction
= action
;
1048 dis
.itemState
= ((state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
1049 ((state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
1050 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1051 dis
.hwndItem
= hwnd
;
1054 GetClientRect( hwnd
, &dis
.rcItem
);
1056 clipRegion
= CreateRectRgnIndirect(&dis
.rcItem
);
1057 if (GetClipRgn(hDC
, clipRegion
) != 1)
1059 DeleteObject(clipRegion
);
1062 clipRect
= dis
.rcItem
;
1063 DPtoLP(hDC
, (LPPOINT
) &clipRect
, 2);
1064 IntersectClipRect(hDC
, clipRect
.left
, clipRect
.top
, clipRect
.right
, clipRect
.bottom
);
1066 if ((hFont
= get_button_font( hwnd
))) hPrevFont
= SelectObject( hDC
, hFont
);
1067 parent
= GetParent(hwnd
);
1068 if (!parent
) parent
= hwnd
;
1069 SendMessageW( parent
, WM_CTLCOLORBTN
, (WPARAM
)hDC
, (LPARAM
)hwnd
);
1070 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1071 if (hPrevFont
) SelectObject(hDC
, hPrevFont
);
1072 SelectClipRgn(hDC
, clipRegion
);