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 */
166 BUILTIN_WINPROC(WINPROC_BUTTON), /* procW */
167 NB_EXTRA_BYTES, /* extra */
168 IDC_ARROW, /* cursor */
173 static inline LONG get_button_state( HWND hwnd )
175 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
178 static inline void set_button_state( HWND hwnd, LONG state )
180 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
183 static inline HFONT get_button_font( HWND hwnd )
185 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
188 static inline void set_button_font( HWND hwnd, HFONT font )
190 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
193 static inline UINT get_button_type( LONG window_style )
195 return (window_style & 0x0f);
198 /* paint a button of any type */
199 static inline void paint_button( HWND hwnd, LONG style, UINT action )
201 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
203 HDC hdc = GetDC( hwnd );
204 btnPaintFunc[style]( hwnd, hdc, action );
205 ReleaseDC( hwnd, hdc );
209 /* retrieve the button text; returned buffer must be freed by caller */
210 static inline WCHAR *get_button_text( HWND hwnd )
213 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
214 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
218 static void setup_clipping( HWND hwnd, HDC hdc )
222 GetClientRect( hwnd, &rc );
223 DPtoLP( hdc, (POINT *)&rc, 2 );
224 IntersectClipRect( hdc, rc.left, rc.top, rc.right, rc.bottom );
227 /***********************************************************************
228 * ButtonWndProc_common
230 LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL unicode )
234 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
235 UINT btn_type = get_button_type( style );
239 if (!IsWindow( hWnd )) return 0;
241 pt.x = (short)LOWORD(lParam);
242 pt.y = (short)HIWORD(lParam);
250 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
251 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
253 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
254 case BS_GROUPBOX: return DLGC_STATIC;
255 default: return DLGC_BUTTON;
259 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
263 if (!hbitmapCheckBoxes)
266 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
267 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
268 checkBoxWidth = bmp.bmWidth / 4;
269 checkBoxHeight = bmp.bmHeight / 3;
271 if (btn_type >= MAX_BTN_TYPE)
272 return -1; /* abort */
274 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
275 if (btn_type == BS_USERBUTTON )
277 style = (style & ~0x0f) | BS_PUSHBUTTON;
278 WIN_SetStyle( hWnd, style, 0x0f & ~style );
280 set_button_state( hWnd, BUTTON_UNCHECKED );
284 if (btn_type == BS_OWNERDRAW)
286 HDC hdc = (HDC)wParam;
289 HWND parent = GetParent(hWnd);
290 if (!parent) parent = hWnd;
291 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
292 if (!hBrush) /* did the app forget to call defwindowproc ? */
293 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
294 (WPARAM)hdc, (LPARAM)hWnd);
295 GetClientRect(hWnd, &rc);
296 FillRect(hdc, &rc, hBrush);
302 if (btnPaintFunc[btn_type])
305 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
306 int nOldMode = SetBkMode( hdc, OPAQUE );
307 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
308 SetBkMode(hdc, nOldMode); /* reset painting mode */
309 if( !wParam ) EndPaint( hWnd, &ps );
314 if (wParam == VK_SPACE)
316 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
317 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
322 case WM_LBUTTONDBLCLK:
323 if(style & BS_NOTIFY ||
324 btn_type == BS_RADIOBUTTON ||
325 btn_type == BS_USERBUTTON ||
326 btn_type == BS_OWNERDRAW)
328 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
335 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
336 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
340 if (wParam != VK_SPACE)
344 state = get_button_state( hWnd );
345 if (!(state & BUTTON_BTNPRESSED)) break;
346 state &= BUTTON_NSTATES;
347 set_button_state( hWnd, state );
348 if (!(state & BUTTON_HIGHLIGHTED))
353 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
355 GetClientRect( hWnd, &rect );
356 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
358 state = get_button_state( hWnd );
361 case BS_AUTOCHECKBOX:
362 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
364 case BS_AUTORADIOBUTTON:
365 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
368 SendMessageW( hWnd, BM_SETCHECK,
369 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
372 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
376 case WM_CAPTURECHANGED:
377 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
378 state = get_button_state( hWnd );
379 if (state & BUTTON_BTNPRESSED)
381 state &= BUTTON_NSTATES;
382 set_button_state( hWnd, state );
383 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
388 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
390 GetClientRect( hWnd, &rect );
391 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
397 /* Clear an old text here as Windows does */
398 HDC hdc = GetDC(hWnd);
401 HWND parent = GetParent(hWnd);
403 if (!parent) parent = hWnd;
404 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
405 (WPARAM)hdc, (LPARAM)hWnd);
406 if (!hbrush) /* did the app forget to call DefWindowProc ? */
407 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
408 (WPARAM)hdc, (LPARAM)hWnd);
410 GetClientRect(hWnd, &client);
412 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
413 /* Clip by client rect bounds */
414 if (rc.right > client.right) rc.right = client.right;
415 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
416 FillRect(hdc, &rc, hbrush);
417 ReleaseDC(hWnd, hdc);
419 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
420 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
421 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
422 InvalidateRect( hWnd, NULL, TRUE );
424 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
425 return 1; /* success. FIXME: check text length */
429 set_button_font( hWnd, (HFONT)wParam );
430 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
434 return (LRESULT)get_button_font( hWnd );
437 TRACE("WM_SETFOCUS %p\n",hWnd);
438 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
439 paint_button( hWnd, btn_type, ODA_FOCUS );
440 if (style & BS_NOTIFY)
441 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
445 TRACE("WM_KILLFOCUS %p\n",hWnd);
446 state = get_button_state( hWnd );
447 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
448 paint_button( hWnd, btn_type, ODA_FOCUS );
450 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
452 if (style & BS_NOTIFY)
453 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
455 InvalidateRect( hWnd, NULL, FALSE );
458 case WM_SYSCOLORCHANGE:
459 InvalidateRect( hWnd, NULL, FALSE );
463 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
464 btn_type = wParam & 0x0f;
465 style = (style & ~0x0f) | btn_type;
466 WIN_SetStyle( hWnd, style, 0x0f & ~style );
468 /* Only redraw if lParam flag is set.*/
470 InvalidateRect( hWnd, NULL, TRUE );
475 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
476 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
480 /* Check that image format matches button style */
481 switch (style & (BS_BITMAP|BS_ICON))
484 if (wParam != IMAGE_BITMAP) return 0;
487 if (wParam != IMAGE_ICON) return 0;
492 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
493 InvalidateRect( hWnd, NULL, FALSE );
494 return (LRESULT)oldHbitmap;
497 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
500 return get_button_state( hWnd ) & 3;
503 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
504 state = get_button_state( hWnd );
505 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
507 if (wParam) style |= WS_TABSTOP;
508 else style &= ~WS_TABSTOP;
509 SetWindowLongW( hWnd, GWL_STYLE, style );
511 if ((state & 3) != wParam)
513 set_button_state( hWnd, (state & ~3) | wParam );
514 paint_button( hWnd, btn_type, ODA_SELECT );
516 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
517 BUTTON_CheckAutoRadioButton( hWnd );
521 return get_button_state( hWnd );
524 state = get_button_state( hWnd );
527 if (state & BUTTON_HIGHLIGHTED) break;
528 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
532 if (!(state & BUTTON_HIGHLIGHTED)) break;
533 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
535 paint_button( hWnd, btn_type, ODA_SELECT );
539 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
542 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
543 DefWindowProcA(hWnd, uMsg, wParam, lParam);
548 /**********************************************************************
549 * Convert button styles to flags used by DrawText.
551 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
553 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
555 /* "Convert" pushlike buttons to pushbuttons */
556 if (style & BS_PUSHLIKE)
559 if (!(style & BS_MULTILINE))
560 dtStyle |= DT_SINGLELINE;
562 dtStyle |= DT_WORDBREAK;
564 switch (style & BS_CENTER)
566 case BS_LEFT: /* DT_LEFT is 0 */ break;
567 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
568 case BS_CENTER: dtStyle |= DT_CENTER; break;
570 /* Pushbutton's text is centered by default */
571 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
572 /* all other flavours have left aligned text */
575 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
577 /* DrawText ignores vertical alignment for multiline text,
578 * but we use these flags to align label manually.
580 if (get_button_type(style) != BS_GROUPBOX)
582 switch (style & BS_VCENTER)
584 case BS_TOP: /* DT_TOP is 0 */ break;
585 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
586 case BS_VCENTER: /* fall through */
587 default: dtStyle |= DT_VCENTER; break;
591 /* GroupBox's text is always single line and is top aligned. */
592 dtStyle |= DT_SINGLELINE;
597 /**********************************************************************
598 * BUTTON_CalcLabelRect
600 * Calculates label's rectangle depending on button style.
602 * Returns flags to be passed to DrawText.
603 * Calculated rectangle doesn't take into account button state
604 * (pushed, etc.). If there is nothing to draw (no text/image) output
605 * rectangle is empty, and return value is (UINT)-1.
607 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
609 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
610 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
614 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
618 /* Calculate label rectangle according to label type */
619 switch (style & (BS_ICON|BS_BITMAP))
622 if (!(text = get_button_text( hwnd ))) goto empty_rect;
625 HeapFree( GetProcessHeap(), 0, text );
628 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
629 HeapFree( GetProcessHeap(), 0, text );
633 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
636 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
638 r.right = r.left + bm.bmWidth;
639 r.bottom = r.top + bm.bmHeight;
641 DeleteObject(iconInfo.hbmColor);
642 DeleteObject(iconInfo.hbmMask);
646 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
649 r.right = r.left + bm.bmWidth;
650 r.bottom = r.top + bm.bmHeight;
660 /* Position label inside bounding rectangle according to
661 * alignment flags. (calculated rect is always left-top aligned).
662 * If label is aligned to any side - shift label in opposite
663 * direction to leave extra space for focus rectangle.
665 switch (dtStyle & (DT_CENTER|DT_RIGHT))
667 case DT_LEFT: r.left++; r.right++; break;
668 case DT_CENTER: n = r.right - r.left;
669 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
670 r.right = r.left + n; break;
671 case DT_RIGHT: n = r.right - r.left;
672 r.right = rc->right - 1;
673 r.left = r.right - n;
677 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
679 case DT_TOP: r.top++; r.bottom++; break;
680 case DT_VCENTER: n = r.bottom - r.top;
681 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
682 r.bottom = r.top + n; break;
683 case DT_BOTTOM: n = r.bottom - r.top;
684 r.bottom = rc->bottom - 1;
685 r.top = r.bottom - n;
694 /**********************************************************************
695 * BUTTON_DrawTextCallback
697 * Callback function used by DrawStateW function.
699 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
707 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
712 /**********************************************************************
715 * Common function for drawing button label.
717 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
719 DRAWSTATEPROC lpOutputProc = NULL;
723 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
724 LONG state = get_button_state( hwnd );
725 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
728 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
729 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
730 * I don't have Win31 on hand to verify that, so I leave it as is.
733 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
735 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
739 switch (style & (BS_ICON|BS_BITMAP))
742 /* DST_COMPLEX -- is 0 */
743 lpOutputProc = BUTTON_DrawTextCallback;
744 if (!(text = get_button_text( hwnd ))) return;
746 wp = (WPARAM)dtFlags;
751 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
756 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
763 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
764 rc->right - rc->left, rc->bottom - rc->top, flags);
765 HeapFree( GetProcessHeap(), 0, text );
768 /**********************************************************************
769 * Push Button Functions
771 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
773 RECT rc, focus_rect, r;
774 UINT dtFlags, uState;
778 COLORREF oldTxtColor;
780 LONG state = get_button_state( hwnd );
781 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
782 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
785 GetClientRect( hwnd, &rc );
787 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
788 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
789 parent = GetParent(hwnd);
790 if (!parent) parent = hwnd;
791 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
793 setup_clipping( hwnd, hDC );
795 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
796 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
797 oldBkMode = SetBkMode(hDC, TRANSPARENT);
799 if (get_button_type(style) == BS_DEFPUSHBUTTON)
801 if (action != ODA_FOCUS)
802 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
803 InflateRect( &rc, -1, -1 );
808 /* completely skip the drawing if only focus has changed */
809 if (action == ODA_FOCUS) goto draw_focus;
811 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
815 else if (pushedState)
817 if (get_button_type(style) == BS_DEFPUSHBUTTON )
820 uState |= DFCS_PUSHED;
823 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
824 uState |= DFCS_CHECKED;
826 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
828 /* draw button label */
830 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
832 if (dtFlags == (UINT)-1L)
836 OffsetRect(&r, 1, 1);
838 IntersectClipRect(hDC, rc.left, rc.top, rc.right, rc.bottom);
840 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
842 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
844 SetTextColor( hDC, oldTxtColor );
847 if ((action == ODA_FOCUS) ||
848 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
850 InflateRect( &focus_rect, -1, -1 );
851 IntersectRect(&focus_rect, &focus_rect, &rc);
852 DrawFocusRect( hDC, &focus_rect );
856 SelectObject( hDC, hOldPen );
857 SelectObject( hDC, hOldBrush );
858 SetBkMode(hDC, oldBkMode);
861 /**********************************************************************
862 * Check Box & Radio Button Functions
865 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
867 RECT rbox, rtext, client;
872 LONG state = get_button_state( hwnd );
873 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
876 if (style & BS_PUSHLIKE)
878 PB_Paint( hwnd, hDC, action );
882 GetClientRect(hwnd, &client);
883 rbox = rtext = client;
885 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
887 parent = GetParent(hwnd);
888 if (!parent) parent = hwnd;
889 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
890 (WPARAM)hDC, (LPARAM)hwnd);
891 if (!hBrush) /* did the app forget to call defwindowproc ? */
892 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
893 (WPARAM)hDC, (LPARAM)hwnd );
894 setup_clipping( hwnd, hDC );
896 if (style & BS_LEFTTEXT)
898 /* magic +4 is what CTL3D expects */
900 rtext.right -= checkBoxWidth + 4;
901 rbox.left = rbox.right - checkBoxWidth;
905 rtext.left += checkBoxWidth + 4;
906 rbox.right = checkBoxWidth;
909 /* Since WM_ERASEBKGND does nothing, first prepare background */
910 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
911 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
915 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
917 /* Only adjust rbox when rtext is valid */
918 if (dtFlags != (UINT)-1L)
920 rbox.top = rtext.top;
921 rbox.bottom = rtext.bottom;
924 /* Draw the check-box bitmap */
925 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
929 if ((get_button_type(style) == BS_RADIOBUTTON) ||
930 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
931 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
932 else flags = DFCS_BUTTONCHECK;
934 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
935 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
937 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
939 /* rbox must have the correct height */
940 delta = rbox.bottom - rbox.top - checkBoxHeight;
942 if (style & BS_TOP) {
944 rbox.bottom = rbox.top + checkBoxHeight;
946 rbox.top -= -delta/2 + 1;
947 rbox.bottom = rbox.top + checkBoxHeight;
949 } else if (style & BS_BOTTOM) {
951 rbox.top = rbox.bottom - checkBoxHeight;
953 rbox.bottom += -delta/2 + 1;
954 rbox.top = rbox.bottom - checkBoxHeight;
956 } else { /* Default */
958 int ofs = (delta / 2);
959 rbox.bottom -= ofs + 1;
960 rbox.top = rbox.bottom - checkBoxHeight;
961 } else if (delta < 0) {
962 int ofs = (-delta / 2);
964 rbox.bottom = rbox.top + checkBoxHeight;
968 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
971 if (dtFlags == (UINT)-1L) /* Noting to draw */
974 if (action == ODA_DRAWENTIRE)
975 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
978 if ((action == ODA_FOCUS) ||
979 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
983 IntersectRect(&rtext, &rtext, &client);
984 DrawFocusRect( hDC, &rtext );
989 /**********************************************************************
990 * BUTTON_CheckAutoRadioButton
992 * hwnd is checked, uncheck every other auto radio button in group
994 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
996 HWND parent, sibling, start;
998 parent = GetParent(hwnd);
999 /* make sure that starting control is not disabled or invisible */
1000 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1003 if (!sibling) break;
1004 if ((hwnd != sibling) &&
1005 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1006 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1007 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1008 } while (sibling != start);
1012 /**********************************************************************
1013 * Group Box Functions
1016 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1023 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1026 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1027 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1028 parent = GetParent(hwnd);
1029 if (!parent) parent = hwnd;
1030 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1031 if (!hbr) /* did the app forget to call defwindowproc ? */
1032 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1033 (WPARAM)hDC, (LPARAM)hwnd);
1034 setup_clipping( hwnd, hDC );
1036 GetClientRect( hwnd, &rc);
1039 GetTextMetricsW (hDC, &tm);
1040 rcFrame.top += (tm.tmHeight / 2) - 1;
1041 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1043 InflateRect(&rc, -7, 1);
1044 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1046 if (dtFlags == (UINT)-1L)
1049 /* Because buttons have CS_PARENTDC class style, there is a chance
1050 * that label will be drawn out of client rect.
1051 * But Windows doesn't clip label's rect, so do I.
1054 /* There is 1-pixel margin at the left, right, and bottom */
1055 rc.left--; rc.right++; rc.bottom++;
1056 FillRect(hDC, &rc, hbr);
1057 rc.left++; rc.right--; rc.bottom--;
1059 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1063 /**********************************************************************
1064 * User Button Functions
1067 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1072 LONG state = get_button_state( hwnd );
1075 if (action == ODA_SELECT) return;
1077 GetClientRect( hwnd, &rc);
1079 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1081 parent = GetParent(hwnd);
1082 if (!parent) parent = hwnd;
1083 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1084 if (!hBrush) /* did the app forget to call defwindowproc ? */
1085 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1086 (WPARAM)hDC, (LPARAM)hwnd);
1088 FillRect( hDC, &rc, hBrush );
1089 if ((action == ODA_FOCUS) ||
1090 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1091 DrawFocusRect( hDC, &rc );
1093 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1097 /**********************************************************************
1098 * Ownerdrawn Button Functions
1101 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1103 LONG state = get_button_state( hwnd );
1105 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1107 HFONT hFont, hPrevFont = 0;
1109 dis.CtlType = ODT_BUTTON;
1112 dis.itemAction = action;
1113 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1114 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1115 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1116 dis.hwndItem = hwnd;
1119 GetClientRect( hwnd, &dis.rcItem );
1121 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1122 parent = GetParent(hwnd);
1123 if (!parent) parent = hwnd;
1124 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1126 setup_clipping( hwnd, hDC );
1128 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1129 if (hPrevFont) SelectObject(hDC, hPrevFont);