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
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
54 * - BN_UNPUSHED/BN_UNHILITE
57 * Structures/Macros/Definitions
60 * - Button_GetIdealSize
61 * - Button_GetImageList
62 * - Button_GetTextMargin
63 * - Button_SetImageList
64 * - Button_SetTextMargin
74 #include "wine/winuser16.h"
76 #include "user_private.h"
78 /* GetWindowLong offsets for window extra information */
79 #define STATE_GWL_OFFSET 0
80 #define HFONT_GWL_OFFSET (sizeof(LONG))
81 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
82 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
84 /* Button state values */
85 #define BUTTON_UNCHECKED 0x00
86 #define BUTTON_CHECKED 0x01
87 #define BUTTON_3STATE 0x02
88 #define BUTTON_HIGHLIGHTED 0x04
89 #define BUTTON_HASFOCUS 0x08
90 #define BUTTON_NSTATES 0x0F
91 /* undocumented flags */
92 #define BUTTON_BTNPRESSED 0x40
93 #define BUTTON_UNKNOWN2 0x20
94 #define BUTTON_UNKNOWN3 0x10
96 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
97 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
98 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
99 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
100 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
101 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
102 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
103 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
104 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
106 #define MAX_BTN_TYPE 12
108 static const WORD maxCheckState[MAX_BTN_TYPE] =
110 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
111 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
112 BUTTON_CHECKED, /* BS_CHECKBOX */
113 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
114 BUTTON_CHECKED, /* BS_RADIOBUTTON */
115 BUTTON_3STATE, /* BS_3STATE */
116 BUTTON_3STATE, /* BS_AUTO3STATE */
117 BUTTON_UNCHECKED, /* BS_GROUPBOX */
118 BUTTON_UNCHECKED, /* BS_USERBUTTON */
119 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
120 BUTTON_UNCHECKED, /* Not defined */
121 BUTTON_UNCHECKED /* BS_OWNERDRAW */
124 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
126 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
128 PB_Paint, /* BS_PUSHBUTTON */
129 PB_Paint, /* BS_DEFPUSHBUTTON */
130 CB_Paint, /* BS_CHECKBOX */
131 CB_Paint, /* BS_AUTOCHECKBOX */
132 CB_Paint, /* BS_RADIOBUTTON */
133 CB_Paint, /* BS_3STATE */
134 CB_Paint, /* BS_AUTO3STATE */
135 GB_Paint, /* BS_GROUPBOX */
136 UB_Paint, /* BS_USERBUTTON */
137 CB_Paint, /* BS_AUTORADIOBUTTON */
138 NULL, /* Not defined */
139 OB_Paint /* BS_OWNERDRAW */
142 static HBITMAP hbitmapCheckBoxes = 0;
143 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
146 /*********************************************************************
147 * button class descriptor
149 const struct builtin_class_descr BUTTON_builtin_class =
152 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
153 ButtonWndProcA, /* procA */
154 ButtonWndProcW, /* procW */
155 NB_EXTRA_BYTES, /* extra */
156 IDC_ARROW, /* cursor */
161 inline static LONG get_button_state( HWND hwnd )
163 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
166 inline static void set_button_state( HWND hwnd, LONG state )
168 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
171 inline static HFONT get_button_font( HWND hwnd )
173 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
176 inline static void set_button_font( HWND hwnd, HFONT font )
178 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
181 inline static UINT get_button_type( LONG window_style )
183 return (window_style & 0x0f);
186 /* paint a button of any type */
187 inline static void paint_button( HWND hwnd, LONG style, UINT action )
189 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
191 HDC hdc = GetDC( hwnd );
192 btnPaintFunc[style]( hwnd, hdc, action );
193 ReleaseDC( hwnd, hdc );
197 /* retrieve the button text; returned buffer must be freed by caller */
198 inline static WCHAR *get_button_text( HWND hwnd )
201 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
202 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
206 /***********************************************************************
207 * ButtonWndProc_common
209 static LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
210 WPARAM wParam, LPARAM lParam, BOOL unicode )
214 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
215 UINT btn_type = get_button_type( style );
219 pt.x = LOWORD(lParam);
220 pt.y = HIWORD(lParam);
228 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
229 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
231 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
232 case BS_GROUPBOX: return DLGC_STATIC;
233 default: return DLGC_BUTTON;
237 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
241 if (!hbitmapCheckBoxes)
244 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
245 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
246 checkBoxWidth = bmp.bmWidth / 4;
247 checkBoxHeight = bmp.bmHeight / 3;
249 if (btn_type >= MAX_BTN_TYPE)
250 return -1; /* abort */
251 set_button_state( hWnd, BUTTON_UNCHECKED );
255 if (btn_type == BS_OWNERDRAW)
257 HDC hdc = (HDC)wParam;
260 HWND parent = GetParent(hWnd);
261 if (!parent) parent = hWnd;
262 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
263 if (!hBrush) /* did the app forget to call defwindowproc ? */
264 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
265 (WPARAM)hdc, (LPARAM)hWnd);
266 GetClientRect(hWnd, &rc);
267 FillRect(hdc, &rc, hBrush);
272 if (btnPaintFunc[btn_type])
275 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
276 int nOldMode = SetBkMode( hdc, OPAQUE );
277 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
278 SetBkMode(hdc, nOldMode); /* reset painting mode */
279 if( !wParam ) EndPaint( hWnd, &ps );
284 if (wParam == VK_SPACE)
286 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
287 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
291 case WM_LBUTTONDBLCLK:
292 if(style & BS_NOTIFY ||
293 btn_type == BS_RADIOBUTTON ||
294 btn_type == BS_USERBUTTON ||
295 btn_type == BS_OWNERDRAW)
297 SendMessageW( GetParent(hWnd), WM_COMMAND,
298 MAKEWPARAM( GetWindowLongPtrW(hWnd,GWLP_ID), BN_DOUBLECLICKED ),
306 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
307 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
311 if (wParam != VK_SPACE)
315 state = get_button_state( hWnd );
316 if (!(state & BUTTON_BTNPRESSED)) break;
317 state &= BUTTON_NSTATES;
318 set_button_state( hWnd, state );
319 if (!(state & BUTTON_HIGHLIGHTED))
324 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
326 GetClientRect( hWnd, &rect );
327 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
329 state = get_button_state( hWnd );
332 case BS_AUTOCHECKBOX:
333 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
335 case BS_AUTORADIOBUTTON:
336 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
339 SendMessageW( hWnd, BM_SETCHECK,
340 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
343 SendMessageW( GetParent(hWnd), WM_COMMAND,
344 MAKEWPARAM( GetWindowLongPtrW(hWnd,GWLP_ID), BN_CLICKED ), (LPARAM)hWnd);
348 case WM_CAPTURECHANGED:
349 state = get_button_state( hWnd );
350 if (state & BUTTON_BTNPRESSED)
352 state &= BUTTON_NSTATES;
353 set_button_state( hWnd, state );
354 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
359 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
361 GetClientRect( hWnd, &rect );
362 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
368 /* Clear an old text here as Windows does */
369 HDC hdc = GetDC(hWnd);
372 HWND parent = GetParent(hWnd);
374 if (!parent) parent = hWnd;
375 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
376 (WPARAM)hdc, (LPARAM)hWnd);
377 if (!hbrush) /* did the app forget to call DefWindowProc ? */
378 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
379 (WPARAM)hdc, (LPARAM)hWnd);
381 GetClientRect(hWnd, &client);
383 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
384 /* Clip by client rect bounds */
385 if (rc.right > client.right) rc.right = client.right;
386 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
387 FillRect(hdc, &rc, hbrush);
388 ReleaseDC(hWnd, hdc);
390 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
391 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
392 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
393 InvalidateRect( hWnd, NULL, TRUE );
395 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
396 return 1; /* success. FIXME: check text length */
400 set_button_font( hWnd, (HFONT)wParam );
401 if (lParam) paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
405 return (LRESULT)get_button_font( hWnd );
408 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
409 paint_button( hWnd, btn_type, ODA_FOCUS );
413 state = get_button_state( hWnd );
414 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
415 paint_button( hWnd, btn_type, ODA_FOCUS );
417 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
421 case WM_SYSCOLORCHANGE:
422 InvalidateRect( hWnd, NULL, FALSE );
427 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
428 btn_type = wParam & 0x0f;
429 style = (style & ~0x0f) | btn_type;
430 SetWindowLongW( hWnd, GWL_STYLE, style );
432 /* Only redraw if lParam flag is set.*/
434 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
439 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
440 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
444 /* Check that image format matches button style */
445 switch (style & (BS_BITMAP|BS_ICON))
448 if (wParam != IMAGE_BITMAP) return 0;
451 if (wParam != IMAGE_ICON) return 0;
456 oldHbitmap = (HBITMAP)SetWindowLongW( hWnd, HIMAGE_GWL_OFFSET, lParam );
457 InvalidateRect( hWnd, NULL, FALSE );
458 return (LRESULT)oldHbitmap;
461 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
465 return get_button_state( hWnd ) & 3;
469 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
470 state = get_button_state( hWnd );
471 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
473 if (wParam) style |= WS_TABSTOP;
474 else style &= ~WS_TABSTOP;
475 SetWindowLongW( hWnd, GWL_STYLE, style );
477 if ((state & 3) != wParam)
479 set_button_state( hWnd, (state & ~3) | wParam );
480 paint_button( hWnd, btn_type, ODA_SELECT );
482 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
483 BUTTON_CheckAutoRadioButton( hWnd );
488 return get_button_state( hWnd );
492 state = get_button_state( hWnd );
495 if (state & BUTTON_HIGHLIGHTED) break;
496 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
500 if (!(state & BUTTON_HIGHLIGHTED)) break;
501 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
503 paint_button( hWnd, btn_type, ODA_SELECT );
507 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
510 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
511 DefWindowProcA(hWnd, uMsg, wParam, lParam);
516 /***********************************************************************
518 * The button window procedure. This is just a wrapper which locks
519 * the passed HWND and calls the real window procedure (with a WND*
520 * pointer pointing to the locked windowstructure).
522 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
524 if (!IsWindow( hWnd )) return 0;
525 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
529 /***********************************************************************
532 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
534 if (!IsWindow( hWnd )) return 0;
535 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
539 /**********************************************************************
540 * Convert button styles to flags used by DrawText.
541 * TODO: handle WS_EX_RIGHT extended style.
543 static UINT BUTTON_BStoDT(DWORD style)
545 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
547 /* "Convert" pushlike buttons to pushbuttons */
548 if (style & BS_PUSHLIKE)
551 if (!(style & BS_MULTILINE))
552 dtStyle |= DT_SINGLELINE;
554 dtStyle |= DT_WORDBREAK;
556 switch (style & BS_CENTER)
558 case BS_LEFT: /* DT_LEFT is 0 */ break;
559 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
560 case BS_CENTER: dtStyle |= DT_CENTER; break;
562 /* Pushbutton's text is centered by default */
563 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
564 /* all other flavours have left aligned text */
567 /* DrawText ignores vertical alignment for multiline text,
568 * but we use these flags to align label manualy.
570 if (get_button_type(style) != BS_GROUPBOX)
572 switch (style & BS_VCENTER)
574 case BS_TOP: /* DT_TOP is 0 */ break;
575 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
576 case BS_VCENTER: /* fall through */
577 default: dtStyle |= DT_VCENTER; break;
581 /* GroupBox's text is always single line and is top aligned. */
582 dtStyle |= DT_SINGLELINE;
587 /**********************************************************************
588 * BUTTON_CalcLabelRect
590 * Calculates label's rectangle depending on button style.
592 * Returns flags to be passed to DrawText.
593 * Calculated rectangle doesn't take into account button state
594 * (pushed, etc.). If there is nothing to draw (no text/image) output
595 * rectangle is empty, and return value is (UINT)-1.
597 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
599 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
603 UINT dtStyle = BUTTON_BStoDT(style);
607 /* Calculate label rectangle according to label type */
608 switch (style & (BS_ICON|BS_BITMAP))
611 if (!(text = get_button_text( hwnd ))) goto empty_rect;
614 HeapFree( GetProcessHeap(), 0, text );
617 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
618 HeapFree( GetProcessHeap(), 0, text );
622 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
625 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
627 r.right = r.left + bm.bmWidth;
628 r.bottom = r.top + bm.bmHeight;
630 DeleteObject(iconInfo.hbmColor);
631 DeleteObject(iconInfo.hbmMask);
635 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
638 r.right = r.left + bm.bmWidth;
639 r.bottom = r.top + bm.bmHeight;
646 return (UINT)(LONG)-1;
649 /* Position label inside bounding rectangle according to
650 * alignment flags. (calculated rect is always left-top aligned).
651 * If label is aligned to any side - shift label in opposite
652 * direction to leave extra space for focus rectangle.
654 switch (dtStyle & (DT_CENTER|DT_RIGHT))
656 case DT_LEFT: r.left++; r.right++; break;
657 case DT_CENTER: n = r.right - r.left;
658 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
659 r.right = r.left + n; break;
660 case DT_RIGHT: n = r.right - r.left;
661 r.right = rc->right - 1;
662 r.left = r.right - n;
666 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
668 case DT_TOP: r.top++; r.bottom++; break;
669 case DT_VCENTER: n = r.bottom - r.top;
670 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
671 r.bottom = r.top + n; break;
672 case DT_BOTTOM: n = r.bottom - r.top;
673 r.bottom = rc->bottom - 1;
674 r.top = r.bottom - n;
683 /**********************************************************************
684 * BUTTON_DrawTextCallback
686 * Callback function used by DrawStateW function.
688 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
696 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
701 /**********************************************************************
704 * Common function for drawing button label.
706 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
708 DRAWSTATEPROC lpOutputProc = NULL;
712 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
713 LONG state = get_button_state( hwnd );
714 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
717 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
718 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
719 * I don't have Win31 on hand to verify that, so I leave it as is.
722 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
724 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
728 switch (style & (BS_ICON|BS_BITMAP))
731 /* DST_COMPLEX -- is 0 */
732 lpOutputProc = BUTTON_DrawTextCallback;
733 if (!(text = get_button_text( hwnd ))) return;
735 wp = (WPARAM)dtFlags;
740 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
745 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
752 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
753 rc->right - rc->left, rc->bottom - rc->top, flags);
754 HeapFree( GetProcessHeap(), 0, text );
757 /**********************************************************************
758 * Push Button Functions
760 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
762 RECT rc, focus_rect, r;
763 UINT dtFlags, uState;
768 COLORREF oldTxtColor;
770 LONG state = get_button_state( hwnd );
771 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
772 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
775 GetClientRect( hwnd, &rc );
777 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
778 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
779 parent = GetParent(hwnd);
780 if (!parent) parent = hwnd;
781 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
782 hOldPen = (HPEN)SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
783 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
784 oldBkMode = SetBkMode(hDC, TRANSPARENT);
786 if (get_button_type(style) == BS_DEFPUSHBUTTON)
788 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
789 InflateRect( &rc, -1, -1 );
792 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
796 else if (pushedState)
798 if (get_button_type(style) == BS_DEFPUSHBUTTON )
801 uState |= DFCS_PUSHED;
804 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
805 uState |= DFCS_CHECKED;
807 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
811 /* draw button label */
813 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
815 if (dtFlags == (UINT)-1L)
819 OffsetRect(&r, 1, 1);
821 hRgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
822 SelectClipRgn(hDC, hRgn);
824 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
826 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
828 SetTextColor( hDC, oldTxtColor );
829 SelectClipRgn(hDC, 0);
832 if (state & BUTTON_HASFOCUS)
834 InflateRect( &focus_rect, -1, -1 );
835 IntersectRect(&focus_rect, &focus_rect, &rc);
836 DrawFocusRect( hDC, &focus_rect );
840 SelectObject( hDC, hOldPen );
841 SelectObject( hDC, hOldBrush );
842 SetBkMode(hDC, oldBkMode);
845 /**********************************************************************
846 * Check Box & Radio Button Functions
849 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
851 RECT rbox, rtext, client;
857 LONG state = get_button_state( hwnd );
858 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
861 if (style & BS_PUSHLIKE)
863 PB_Paint( hwnd, hDC, action );
867 GetClientRect(hwnd, &client);
868 rbox = rtext = client;
870 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
872 parent = GetParent(hwnd);
873 if (!parent) parent = hwnd;
874 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
875 (WPARAM)hDC, (LPARAM)hwnd);
876 if (!hBrush) /* did the app forget to call defwindowproc ? */
877 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
878 (WPARAM)hDC, (LPARAM)hwnd );
880 if (style & BS_LEFTTEXT)
882 /* magic +4 is what CTL3D expects */
884 rtext.right -= checkBoxWidth + 4;
885 rbox.left = rbox.right - checkBoxWidth;
889 rtext.left += checkBoxWidth + 4;
890 rbox.right = checkBoxWidth;
893 /* Since WM_ERASEBKGND does nothing, first prepare background */
894 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
895 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
899 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
901 rbox.top = rtext.top;
902 rbox.bottom = rtext.bottom;
903 /* Draw the check-box bitmap */
904 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
908 if ((get_button_type(style) == BS_RADIOBUTTON) ||
909 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
910 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
911 else flags = DFCS_BUTTONCHECK;
913 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
914 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
916 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
918 /* rbox must have the correct height */
919 delta = rbox.bottom - rbox.top - checkBoxHeight;
921 if (style & BS_TOP) {
923 rbox.bottom = rbox.top + checkBoxHeight;
925 rbox.top -= -delta/2 + 1;
926 rbox.bottom = rbox.top + checkBoxHeight;
928 } else if (style & BS_BOTTOM) {
930 rbox.top = rbox.bottom - checkBoxHeight;
932 rbox.bottom += -delta/2 + 1;
933 rbox.top = rbox.bottom -= checkBoxHeight;
935 } else { /* Default */
937 int ofs = (delta / 2);
938 rbox.bottom -= ofs + 1;
939 rbox.top = rbox.bottom - checkBoxHeight;
940 } else if (delta < 0) {
941 int ofs = (-delta / 2);
943 rbox.bottom = rbox.top + checkBoxHeight;
947 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
950 if (dtFlags == (UINT)-1L) /* Noting to draw */
952 hRgn = CreateRectRgn(client.left, client.top, client.right, client.bottom);
953 SelectClipRgn(hDC, hRgn);
956 if (action == ODA_DRAWENTIRE)
957 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
960 if ((action == ODA_FOCUS) ||
961 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
965 IntersectRect(&rtext, &rtext, &client);
966 DrawFocusRect( hDC, &rtext );
968 SelectClipRgn(hDC, 0);
972 /**********************************************************************
973 * BUTTON_CheckAutoRadioButton
975 * hwnd is checked, uncheck every other auto radio button in group
977 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
979 HWND parent, sibling, start;
981 parent = GetParent(hwnd);
982 /* make sure that starting control is not disabled or invisible */
983 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
987 if ((hwnd != sibling) &&
988 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
989 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
990 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
991 } while (sibling != start);
995 /**********************************************************************
996 * Group Box Functions
999 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1006 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1009 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1010 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1011 parent = GetParent(hwnd);
1012 if (!parent) parent = hwnd;
1013 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1014 if (!hbr) /* did the app forget to call defwindowproc ? */
1015 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1016 (WPARAM)hDC, (LPARAM)hwnd);
1018 GetClientRect( hwnd, &rc);
1021 GetTextMetricsW (hDC, &tm);
1022 rcFrame.top += (tm.tmHeight / 2) - 1;
1023 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1025 InflateRect(&rc, -7, 1);
1026 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1028 if (dtFlags == (UINT)-1L)
1031 /* Because buttons have CS_PARENTDC class style, there is a chance
1032 * that label will be drawn out of client rect.
1033 * But Windows doesn't clip label's rect, so do I.
1036 /* There is 1-pixel marging at the left, right, and bottom */
1037 rc.left--; rc.right++; rc.bottom++;
1038 FillRect(hDC, &rc, hbr);
1039 rc.left++; rc.right--; rc.bottom--;
1041 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1045 /**********************************************************************
1046 * User Button Functions
1049 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1054 LONG state = get_button_state( hwnd );
1057 if (action == ODA_SELECT) return;
1059 GetClientRect( hwnd, &rc);
1061 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1063 parent = GetParent(hwnd);
1064 if (!parent) parent = hwnd;
1065 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1066 if (!hBrush) /* did the app forget to call defwindowproc ? */
1067 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1068 (WPARAM)hDC, (LPARAM)hwnd);
1070 FillRect( hDC, &rc, hBrush );
1071 if ((action == ODA_FOCUS) ||
1072 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1073 DrawFocusRect( hDC, &rc );
1077 /**********************************************************************
1078 * Ownerdrawn Button Functions
1081 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1083 LONG state = get_button_state( hwnd );
1087 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1089 HFONT hFont, hPrevFont = 0;
1091 dis.CtlType = ODT_BUTTON;
1094 dis.itemAction = action;
1095 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1096 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1097 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1098 dis.hwndItem = hwnd;
1101 GetClientRect( hwnd, &dis.rcItem );
1103 clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1104 if (GetClipRgn(hDC, clipRegion) != 1)
1106 DeleteObject(clipRegion);
1109 clipRect = dis.rcItem;
1110 DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1111 IntersectClipRect(hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
1113 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1114 parent = GetParent(hwnd);
1115 if (!parent) parent = hwnd;
1116 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1117 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1118 if (hPrevFont) SelectObject(hDC, hPrevFont);
1119 SelectClipRgn(hDC, clipRegion);