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
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
74 #include "wine/winuser16.h"
76 #include "user_private.h"
77 #include "wine/debug.h"
79 WINE_DEFAULT_DEBUG_CHANNEL(button);
81 /* GetWindowLong offsets for window extra information */
82 #define STATE_GWL_OFFSET 0
83 #define HFONT_GWL_OFFSET (sizeof(LONG))
84 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
85 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
87 /* Button state values */
88 #define BUTTON_UNCHECKED 0x00
89 #define BUTTON_CHECKED 0x01
90 #define BUTTON_3STATE 0x02
91 #define BUTTON_HIGHLIGHTED 0x04
92 #define BUTTON_HASFOCUS 0x08
93 #define BUTTON_NSTATES 0x0F
94 /* undocumented flags */
95 #define BUTTON_BTNPRESSED 0x40
96 #define BUTTON_UNKNOWN2 0x20
97 #define BUTTON_UNKNOWN3 0x10
99 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
100 do { /* Notify parent which has created this button control */ \
101 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
102 SendMessageW(GetParent(hWnd), WM_COMMAND, \
103 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
107 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
108 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
109 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
110 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
111 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
112 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
113 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
114 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
115 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
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 const struct builtin_class_descr BUTTON_builtin_class =
163 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
164 ButtonWndProcA, /* procA */
165 ButtonWndProcW, /* procW */
166 NB_EXTRA_BYTES, /* extra */
167 IDC_ARROW, /* cursor */
172 inline static LONG get_button_state( HWND hwnd )
174 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
177 inline static void set_button_state( HWND hwnd, LONG state )
179 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
182 inline static HFONT get_button_font( HWND hwnd )
184 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
187 inline static void set_button_font( HWND hwnd, HFONT font )
189 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
192 inline static UINT get_button_type( LONG window_style )
194 return (window_style & 0x0f);
197 /* paint a button of any type */
198 inline static void paint_button( HWND hwnd, LONG style, UINT action )
200 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
202 HDC hdc = GetDC( hwnd );
203 btnPaintFunc[style]( hwnd, hdc, action );
204 ReleaseDC( hwnd, hdc );
208 /* retrieve the button text; returned buffer must be freed by caller */
209 inline static WCHAR *get_button_text( HWND hwnd )
212 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
213 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
217 /***********************************************************************
218 * ButtonWndProc_common
220 static LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
221 WPARAM wParam, LPARAM lParam, BOOL unicode )
225 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
226 UINT btn_type = get_button_type( style );
230 pt.x = LOWORD(lParam);
231 pt.y = HIWORD(lParam);
239 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
240 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
242 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
243 case BS_GROUPBOX: return DLGC_STATIC;
244 default: return DLGC_BUTTON;
248 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
252 if (!hbitmapCheckBoxes)
255 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
256 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
257 checkBoxWidth = bmp.bmWidth / 4;
258 checkBoxHeight = bmp.bmHeight / 3;
260 if (btn_type >= MAX_BTN_TYPE)
261 return -1; /* abort */
262 set_button_state( hWnd, BUTTON_UNCHECKED );
266 if (btn_type == BS_OWNERDRAW)
268 HDC hdc = (HDC)wParam;
271 HWND parent = GetParent(hWnd);
272 if (!parent) parent = hWnd;
273 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
274 if (!hBrush) /* did the app forget to call defwindowproc ? */
275 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
276 (WPARAM)hdc, (LPARAM)hWnd);
277 GetClientRect(hWnd, &rc);
278 FillRect(hdc, &rc, hBrush);
283 if (btnPaintFunc[btn_type])
286 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
287 int nOldMode = SetBkMode( hdc, OPAQUE );
288 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
289 SetBkMode(hdc, nOldMode); /* reset painting mode */
290 if( !wParam ) EndPaint( hWnd, &ps );
295 if (wParam == VK_SPACE)
297 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
298 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
302 case WM_LBUTTONDBLCLK:
303 if(style & BS_NOTIFY ||
304 btn_type == BS_RADIOBUTTON ||
305 btn_type == BS_USERBUTTON ||
306 btn_type == BS_OWNERDRAW)
308 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
315 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
316 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
320 if (wParam != VK_SPACE)
324 state = get_button_state( hWnd );
325 if (!(state & BUTTON_BTNPRESSED)) break;
326 state &= BUTTON_NSTATES;
327 set_button_state( hWnd, state );
328 if (!(state & BUTTON_HIGHLIGHTED))
333 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
335 GetClientRect( hWnd, &rect );
336 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
338 state = get_button_state( hWnd );
341 case BS_AUTOCHECKBOX:
342 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
344 case BS_AUTORADIOBUTTON:
345 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
348 SendMessageW( hWnd, BM_SETCHECK,
349 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
352 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
356 case WM_CAPTURECHANGED:
357 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
358 state = get_button_state( hWnd );
359 if (state & BUTTON_BTNPRESSED)
361 state &= BUTTON_NSTATES;
362 set_button_state( hWnd, state );
363 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
368 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
370 GetClientRect( hWnd, &rect );
371 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
377 /* Clear an old text here as Windows does */
378 HDC hdc = GetDC(hWnd);
381 HWND parent = GetParent(hWnd);
383 if (!parent) parent = hWnd;
384 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
385 (WPARAM)hdc, (LPARAM)hWnd);
386 if (!hbrush) /* did the app forget to call DefWindowProc ? */
387 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
388 (WPARAM)hdc, (LPARAM)hWnd);
390 GetClientRect(hWnd, &client);
392 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
393 /* Clip by client rect bounds */
394 if (rc.right > client.right) rc.right = client.right;
395 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
396 FillRect(hdc, &rc, hbrush);
397 ReleaseDC(hWnd, hdc);
399 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
400 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
401 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
402 InvalidateRect( hWnd, NULL, TRUE );
404 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
405 return 1; /* success. FIXME: check text length */
409 set_button_font( hWnd, (HFONT)wParam );
410 if (lParam) paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
414 return (LRESULT)get_button_font( hWnd );
417 TRACE("WM_SETFOCUS %p\n",hWnd);
418 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
419 paint_button( hWnd, btn_type, ODA_FOCUS );
420 if (style & BS_NOTIFY)
421 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
425 TRACE("WM_KILLFOCUS %p\n",hWnd);
426 state = get_button_state( hWnd );
427 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
428 paint_button( hWnd, btn_type, ODA_FOCUS );
430 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
432 if (style & BS_NOTIFY)
433 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
437 case WM_SYSCOLORCHANGE:
438 InvalidateRect( hWnd, NULL, FALSE );
443 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
444 btn_type = wParam & 0x0f;
445 style = (style & ~0x0f) | btn_type;
446 SetWindowLongW( hWnd, GWL_STYLE, style );
448 /* Only redraw if lParam flag is set.*/
450 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
455 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
456 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
460 /* Check that image format matches button style */
461 switch (style & (BS_BITMAP|BS_ICON))
464 if (wParam != IMAGE_BITMAP) return 0;
467 if (wParam != IMAGE_ICON) return 0;
472 oldHbitmap = (HBITMAP)SetWindowLongW( hWnd, HIMAGE_GWL_OFFSET, lParam );
473 InvalidateRect( hWnd, NULL, FALSE );
474 return (LRESULT)oldHbitmap;
477 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
481 return get_button_state( hWnd ) & 3;
485 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
486 state = get_button_state( hWnd );
487 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
489 if (wParam) style |= WS_TABSTOP;
490 else style &= ~WS_TABSTOP;
491 SetWindowLongW( hWnd, GWL_STYLE, style );
493 if ((state & 3) != wParam)
495 set_button_state( hWnd, (state & ~3) | wParam );
496 paint_button( hWnd, btn_type, ODA_SELECT );
498 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
499 BUTTON_CheckAutoRadioButton( hWnd );
504 return get_button_state( hWnd );
508 state = get_button_state( hWnd );
511 if (state & BUTTON_HIGHLIGHTED) break;
512 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
516 if (!(state & BUTTON_HIGHLIGHTED)) break;
517 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
519 paint_button( hWnd, btn_type, ODA_SELECT );
523 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
526 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
527 DefWindowProcA(hWnd, uMsg, wParam, lParam);
532 /***********************************************************************
534 * The button window procedure. This is just a wrapper which locks
535 * the passed HWND and calls the real window procedure (with a WND*
536 * pointer pointing to the locked windowstructure).
538 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
540 if (!IsWindow( hWnd )) return 0;
541 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
545 /***********************************************************************
548 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
550 if (!IsWindow( hWnd )) return 0;
551 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
555 /**********************************************************************
556 * Convert button styles to flags used by DrawText.
557 * TODO: handle WS_EX_RIGHT extended style.
559 static UINT BUTTON_BStoDT(DWORD style)
561 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
563 /* "Convert" pushlike buttons to pushbuttons */
564 if (style & BS_PUSHLIKE)
567 if (!(style & BS_MULTILINE))
568 dtStyle |= DT_SINGLELINE;
570 dtStyle |= DT_WORDBREAK;
572 switch (style & BS_CENTER)
574 case BS_LEFT: /* DT_LEFT is 0 */ break;
575 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
576 case BS_CENTER: dtStyle |= DT_CENTER; break;
578 /* Pushbutton's text is centered by default */
579 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
580 /* all other flavours have left aligned text */
583 /* DrawText ignores vertical alignment for multiline text,
584 * but we use these flags to align label manualy.
586 if (get_button_type(style) != BS_GROUPBOX)
588 switch (style & BS_VCENTER)
590 case BS_TOP: /* DT_TOP is 0 */ break;
591 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
592 case BS_VCENTER: /* fall through */
593 default: dtStyle |= DT_VCENTER; break;
597 /* GroupBox's text is always single line and is top aligned. */
598 dtStyle |= DT_SINGLELINE;
603 /**********************************************************************
604 * BUTTON_CalcLabelRect
606 * Calculates label's rectangle depending on button style.
608 * Returns flags to be passed to DrawText.
609 * Calculated rectangle doesn't take into account button state
610 * (pushed, etc.). If there is nothing to draw (no text/image) output
611 * rectangle is empty, and return value is (UINT)-1.
613 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
615 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
619 UINT dtStyle = BUTTON_BStoDT(style);
623 /* Calculate label rectangle according to label type */
624 switch (style & (BS_ICON|BS_BITMAP))
627 if (!(text = get_button_text( hwnd ))) goto empty_rect;
630 HeapFree( GetProcessHeap(), 0, text );
633 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
634 HeapFree( GetProcessHeap(), 0, text );
638 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
641 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
643 r.right = r.left + bm.bmWidth;
644 r.bottom = r.top + bm.bmHeight;
646 DeleteObject(iconInfo.hbmColor);
647 DeleteObject(iconInfo.hbmMask);
651 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
654 r.right = r.left + bm.bmWidth;
655 r.bottom = r.top + bm.bmHeight;
662 return (UINT)(LONG)-1;
665 /* Position label inside bounding rectangle according to
666 * alignment flags. (calculated rect is always left-top aligned).
667 * If label is aligned to any side - shift label in opposite
668 * direction to leave extra space for focus rectangle.
670 switch (dtStyle & (DT_CENTER|DT_RIGHT))
672 case DT_LEFT: r.left++; r.right++; break;
673 case DT_CENTER: n = r.right - r.left;
674 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
675 r.right = r.left + n; break;
676 case DT_RIGHT: n = r.right - r.left;
677 r.right = rc->right - 1;
678 r.left = r.right - n;
682 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
684 case DT_TOP: r.top++; r.bottom++; break;
685 case DT_VCENTER: n = r.bottom - r.top;
686 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
687 r.bottom = r.top + n; break;
688 case DT_BOTTOM: n = r.bottom - r.top;
689 r.bottom = rc->bottom - 1;
690 r.top = r.bottom - n;
699 /**********************************************************************
700 * BUTTON_DrawTextCallback
702 * Callback function used by DrawStateW function.
704 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
712 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
717 /**********************************************************************
720 * Common function for drawing button label.
722 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
724 DRAWSTATEPROC lpOutputProc = NULL;
728 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
729 LONG state = get_button_state( hwnd );
730 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
733 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
734 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
735 * I don't have Win31 on hand to verify that, so I leave it as is.
738 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
740 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
744 switch (style & (BS_ICON|BS_BITMAP))
747 /* DST_COMPLEX -- is 0 */
748 lpOutputProc = BUTTON_DrawTextCallback;
749 if (!(text = get_button_text( hwnd ))) return;
751 wp = (WPARAM)dtFlags;
756 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
761 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
768 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
769 rc->right - rc->left, rc->bottom - rc->top, flags);
770 HeapFree( GetProcessHeap(), 0, text );
773 /**********************************************************************
774 * Push Button Functions
776 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
778 RECT rc, focus_rect, r;
779 UINT dtFlags, uState;
784 COLORREF oldTxtColor;
786 LONG state = get_button_state( hwnd );
787 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
788 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
791 GetClientRect( hwnd, &rc );
793 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
794 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
795 parent = GetParent(hwnd);
796 if (!parent) parent = hwnd;
797 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
798 hOldPen = (HPEN)SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
799 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
800 oldBkMode = SetBkMode(hDC, TRANSPARENT);
802 if (get_button_type(style) == BS_DEFPUSHBUTTON)
804 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
805 InflateRect( &rc, -1, -1 );
808 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
812 else if (pushedState)
814 if (get_button_type(style) == BS_DEFPUSHBUTTON )
817 uState |= DFCS_PUSHED;
820 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
821 uState |= DFCS_CHECKED;
823 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
827 /* draw button label */
829 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
831 if (dtFlags == (UINT)-1L)
835 OffsetRect(&r, 1, 1);
837 hRgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
838 SelectClipRgn(hDC, hRgn);
840 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
842 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
844 SetTextColor( hDC, oldTxtColor );
845 SelectClipRgn(hDC, 0);
848 if (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;
873 LONG state = get_button_state( hwnd );
874 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
877 if (style & BS_PUSHLIKE)
879 PB_Paint( hwnd, hDC, action );
883 GetClientRect(hwnd, &client);
884 rbox = rtext = client;
886 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
888 parent = GetParent(hwnd);
889 if (!parent) parent = hwnd;
890 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
891 (WPARAM)hDC, (LPARAM)hwnd);
892 if (!hBrush) /* did the app forget to call defwindowproc ? */
893 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
894 (WPARAM)hDC, (LPARAM)hwnd );
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 rbox.top = rtext.top;
918 rbox.bottom = rtext.bottom;
919 /* Draw the check-box bitmap */
920 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
924 if ((get_button_type(style) == BS_RADIOBUTTON) ||
925 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
926 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
927 else flags = DFCS_BUTTONCHECK;
929 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
930 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
932 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
934 /* rbox must have the correct height */
935 delta = rbox.bottom - rbox.top - checkBoxHeight;
937 if (style & BS_TOP) {
939 rbox.bottom = rbox.top + checkBoxHeight;
941 rbox.top -= -delta/2 + 1;
942 rbox.bottom = rbox.top + checkBoxHeight;
944 } else if (style & BS_BOTTOM) {
946 rbox.top = rbox.bottom - checkBoxHeight;
948 rbox.bottom += -delta/2 + 1;
949 rbox.top = rbox.bottom -= checkBoxHeight;
951 } else { /* Default */
953 int ofs = (delta / 2);
954 rbox.bottom -= ofs + 1;
955 rbox.top = rbox.bottom - checkBoxHeight;
956 } else if (delta < 0) {
957 int ofs = (-delta / 2);
959 rbox.bottom = rbox.top + checkBoxHeight;
963 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
966 if (dtFlags == (UINT)-1L) /* Noting to draw */
968 hRgn = CreateRectRgn(client.left, client.top, client.right, client.bottom);
969 SelectClipRgn(hDC, hRgn);
972 if (action == ODA_DRAWENTIRE)
973 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
976 if ((action == ODA_FOCUS) ||
977 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
981 IntersectRect(&rtext, &rtext, &client);
982 DrawFocusRect( hDC, &rtext );
984 SelectClipRgn(hDC, 0);
988 /**********************************************************************
989 * BUTTON_CheckAutoRadioButton
991 * hwnd is checked, uncheck every other auto radio button in group
993 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
995 HWND parent, sibling, start;
997 parent = GetParent(hwnd);
998 /* make sure that starting control is not disabled or invisible */
999 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1002 if (!sibling) break;
1003 if ((hwnd != sibling) &&
1004 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1005 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1006 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1007 } while (sibling != start);
1011 /**********************************************************************
1012 * Group Box Functions
1015 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1022 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1025 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1026 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1027 parent = GetParent(hwnd);
1028 if (!parent) parent = hwnd;
1029 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1030 if (!hbr) /* did the app forget to call defwindowproc ? */
1031 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1032 (WPARAM)hDC, (LPARAM)hwnd);
1034 GetClientRect( hwnd, &rc);
1037 GetTextMetricsW (hDC, &tm);
1038 rcFrame.top += (tm.tmHeight / 2) - 1;
1039 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1041 InflateRect(&rc, -7, 1);
1042 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1044 if (dtFlags == (UINT)-1L)
1047 /* Because buttons have CS_PARENTDC class style, there is a chance
1048 * that label will be drawn out of client rect.
1049 * But Windows doesn't clip label's rect, so do I.
1052 /* There is 1-pixel marging at the left, right, and bottom */
1053 rc.left--; rc.right++; rc.bottom++;
1054 FillRect(hDC, &rc, hbr);
1055 rc.left++; rc.right--; rc.bottom--;
1057 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1061 /**********************************************************************
1062 * User Button Functions
1065 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1070 LONG state = get_button_state( hwnd );
1073 if (action == ODA_SELECT) return;
1075 GetClientRect( hwnd, &rc);
1077 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1079 parent = GetParent(hwnd);
1080 if (!parent) parent = hwnd;
1081 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1082 if (!hBrush) /* did the app forget to call defwindowproc ? */
1083 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1084 (WPARAM)hDC, (LPARAM)hwnd);
1086 FillRect( hDC, &rc, hBrush );
1087 if ((action == ODA_FOCUS) ||
1088 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1089 DrawFocusRect( hDC, &rc );
1093 /**********************************************************************
1094 * Ownerdrawn Button Functions
1097 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1099 LONG state = get_button_state( hwnd );
1103 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1105 HFONT hFont, hPrevFont = 0;
1107 dis.CtlType = ODT_BUTTON;
1110 dis.itemAction = action;
1111 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1112 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1113 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1114 dis.hwndItem = hwnd;
1117 GetClientRect( hwnd, &dis.rcItem );
1119 clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1120 if (GetClipRgn(hDC, clipRegion) != 1)
1122 DeleteObject(clipRegion);
1125 clipRect = dis.rcItem;
1126 DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1127 IntersectClipRect(hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
1129 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1130 parent = GetParent(hwnd);
1131 if (!parent) parent = hwnd;
1132 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1133 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1134 if (hPrevFont) SelectObject(hDC, hPrevFont);
1135 SelectClipRgn(hDC, clipRegion);