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
76 #include "wine/winuser16.h"
79 #include "user_private.h"
80 #include "wine/debug.h"
82 WINE_DEFAULT_DEBUG_CHANNEL(button);
84 /* GetWindowLong offsets for window extra information */
85 #define STATE_GWL_OFFSET 0
86 #define HFONT_GWL_OFFSET (sizeof(LONG))
87 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
88 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
90 /* Button state values */
91 #define BUTTON_UNCHECKED 0x00
92 #define BUTTON_CHECKED 0x01
93 #define BUTTON_3STATE 0x02
94 #define BUTTON_HIGHLIGHTED 0x04
95 #define BUTTON_HASFOCUS 0x08
96 #define BUTTON_NSTATES 0x0F
97 /* undocumented flags */
98 #define BUTTON_BTNPRESSED 0x40
99 #define BUTTON_UNKNOWN2 0x20
100 #define BUTTON_UNKNOWN3 0x10
102 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
103 do { /* Notify parent which has created this button control */ \
104 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
105 SendMessageW(GetParent(hWnd), WM_COMMAND, \
106 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
110 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
111 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
112 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
113 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
114 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
115 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
116 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
117 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
118 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
120 #define MAX_BTN_TYPE 12
122 static const WORD maxCheckState[MAX_BTN_TYPE] =
124 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
125 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
126 BUTTON_CHECKED, /* BS_CHECKBOX */
127 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
128 BUTTON_CHECKED, /* BS_RADIOBUTTON */
129 BUTTON_3STATE, /* BS_3STATE */
130 BUTTON_3STATE, /* BS_AUTO3STATE */
131 BUTTON_UNCHECKED, /* BS_GROUPBOX */
132 BUTTON_UNCHECKED, /* BS_USERBUTTON */
133 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
134 BUTTON_UNCHECKED, /* Not defined */
135 BUTTON_UNCHECKED /* BS_OWNERDRAW */
138 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
140 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
142 PB_Paint, /* BS_PUSHBUTTON */
143 PB_Paint, /* BS_DEFPUSHBUTTON */
144 CB_Paint, /* BS_CHECKBOX */
145 CB_Paint, /* BS_AUTOCHECKBOX */
146 CB_Paint, /* BS_RADIOBUTTON */
147 CB_Paint, /* BS_3STATE */
148 CB_Paint, /* BS_AUTO3STATE */
149 GB_Paint, /* BS_GROUPBOX */
150 UB_Paint, /* BS_USERBUTTON */
151 CB_Paint, /* BS_AUTORADIOBUTTON */
152 NULL, /* Not defined */
153 OB_Paint /* BS_OWNERDRAW */
156 static HBITMAP hbitmapCheckBoxes = 0;
157 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
160 /*********************************************************************
161 * button class descriptor
163 static const WCHAR buttonW[] = {'B','u','t','t','o','n',0};
164 const struct builtin_class_descr BUTTON_builtin_class =
167 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
168 ButtonWndProcA, /* procA */
169 ButtonWndProcW, /* procW */
170 NB_EXTRA_BYTES, /* extra */
171 IDC_ARROW, /* cursor */
176 static inline LONG get_button_state( HWND hwnd )
178 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
181 static inline void set_button_state( HWND hwnd, LONG state )
183 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
186 static inline HFONT get_button_font( HWND hwnd )
188 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
191 static inline void set_button_font( HWND hwnd, HFONT font )
193 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
196 static inline UINT get_button_type( LONG window_style )
198 return (window_style & 0x0f);
201 /* paint a button of any type */
202 static inline void paint_button( HWND hwnd, LONG style, UINT action )
204 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
206 HDC hdc = GetDC( hwnd );
207 btnPaintFunc[style]( hwnd, hdc, action );
208 ReleaseDC( hwnd, hdc );
212 /* retrieve the button text; returned buffer must be freed by caller */
213 static inline WCHAR *get_button_text( HWND hwnd )
216 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
217 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
221 static void setup_clipping( HWND hwnd, HDC hdc )
225 GetClientRect( hwnd, &rc );
226 DPtoLP( hdc, (POINT *)&rc, 2 );
227 IntersectClipRect( hdc, rc.left, rc.top, rc.right, rc.bottom );
230 /***********************************************************************
231 * ButtonWndProc_common
233 static LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg,
234 WPARAM wParam, LPARAM lParam, BOOL unicode )
238 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
239 UINT btn_type = get_button_type( style );
243 pt.x = (short)LOWORD(lParam);
244 pt.y = (short)HIWORD(lParam);
252 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
253 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
255 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
256 case BS_GROUPBOX: return DLGC_STATIC;
257 default: return DLGC_BUTTON;
261 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
265 if (!hbitmapCheckBoxes)
268 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
269 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
270 checkBoxWidth = bmp.bmWidth / 4;
271 checkBoxHeight = bmp.bmHeight / 3;
273 if (btn_type >= MAX_BTN_TYPE)
274 return -1; /* abort */
276 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
277 if (btn_type == BS_USERBUTTON )
279 style = (style & ~0x0f) | BS_PUSHBUTTON;
280 WIN_SetStyle( hWnd, style, 0x0f & ~style );
282 set_button_state( hWnd, BUTTON_UNCHECKED );
286 if (btn_type == BS_OWNERDRAW)
288 HDC hdc = (HDC)wParam;
291 HWND parent = GetParent(hWnd);
292 if (!parent) parent = hWnd;
293 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
294 if (!hBrush) /* did the app forget to call defwindowproc ? */
295 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
296 (WPARAM)hdc, (LPARAM)hWnd);
297 GetClientRect(hWnd, &rc);
298 FillRect(hdc, &rc, hBrush);
304 if (btnPaintFunc[btn_type])
307 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
308 int nOldMode = SetBkMode( hdc, OPAQUE );
309 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
310 SetBkMode(hdc, nOldMode); /* reset painting mode */
311 if( !wParam ) EndPaint( hWnd, &ps );
316 if (wParam == VK_SPACE)
318 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
319 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
324 case WM_LBUTTONDBLCLK:
325 if(style & BS_NOTIFY ||
326 btn_type == BS_RADIOBUTTON ||
327 btn_type == BS_USERBUTTON ||
328 btn_type == BS_OWNERDRAW)
330 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
337 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
338 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
342 if (wParam != VK_SPACE)
346 state = get_button_state( hWnd );
347 if (!(state & BUTTON_BTNPRESSED)) break;
348 state &= BUTTON_NSTATES;
349 set_button_state( hWnd, state );
350 if (!(state & BUTTON_HIGHLIGHTED))
355 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
357 GetClientRect( hWnd, &rect );
358 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
360 state = get_button_state( hWnd );
363 case BS_AUTOCHECKBOX:
364 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
366 case BS_AUTORADIOBUTTON:
367 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
370 SendMessageW( hWnd, BM_SETCHECK,
371 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
374 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
378 case WM_CAPTURECHANGED:
379 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
380 state = get_button_state( hWnd );
381 if (state & BUTTON_BTNPRESSED)
383 state &= BUTTON_NSTATES;
384 set_button_state( hWnd, state );
385 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
390 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
392 GetClientRect( hWnd, &rect );
393 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
399 /* Clear an old text here as Windows does */
400 HDC hdc = GetDC(hWnd);
403 HWND parent = GetParent(hWnd);
405 if (!parent) parent = hWnd;
406 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
407 (WPARAM)hdc, (LPARAM)hWnd);
408 if (!hbrush) /* did the app forget to call DefWindowProc ? */
409 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
410 (WPARAM)hdc, (LPARAM)hWnd);
412 GetClientRect(hWnd, &client);
414 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
415 /* Clip by client rect bounds */
416 if (rc.right > client.right) rc.right = client.right;
417 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
418 FillRect(hdc, &rc, hbrush);
419 ReleaseDC(hWnd, hdc);
421 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
422 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
423 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
424 InvalidateRect( hWnd, NULL, TRUE );
426 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
427 return 1; /* success. FIXME: check text length */
431 set_button_font( hWnd, (HFONT)wParam );
432 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
436 return (LRESULT)get_button_font( hWnd );
439 TRACE("WM_SETFOCUS %p\n",hWnd);
440 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
441 paint_button( hWnd, btn_type, ODA_FOCUS );
442 if (style & BS_NOTIFY)
443 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
447 TRACE("WM_KILLFOCUS %p\n",hWnd);
448 state = get_button_state( hWnd );
449 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
450 paint_button( hWnd, btn_type, ODA_FOCUS );
452 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
454 if (style & BS_NOTIFY)
455 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
457 InvalidateRect( hWnd, NULL, FALSE );
460 case WM_SYSCOLORCHANGE:
461 InvalidateRect( hWnd, NULL, FALSE );
465 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
466 btn_type = wParam & 0x0f;
467 style = (style & ~0x0f) | btn_type;
468 WIN_SetStyle( hWnd, style, 0x0f & ~style );
470 /* Only redraw if lParam flag is set.*/
472 InvalidateRect( hWnd, NULL, TRUE );
477 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
478 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
482 /* Check that image format matches button style */
483 switch (style & (BS_BITMAP|BS_ICON))
486 if (wParam != IMAGE_BITMAP) return 0;
489 if (wParam != IMAGE_ICON) return 0;
494 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
495 InvalidateRect( hWnd, NULL, FALSE );
496 return (LRESULT)oldHbitmap;
499 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
502 return get_button_state( hWnd ) & 3;
505 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
506 state = get_button_state( hWnd );
507 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
509 if (wParam) style |= WS_TABSTOP;
510 else style &= ~WS_TABSTOP;
511 SetWindowLongW( hWnd, GWL_STYLE, style );
513 if ((state & 3) != wParam)
515 set_button_state( hWnd, (state & ~3) | wParam );
516 paint_button( hWnd, btn_type, ODA_SELECT );
518 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
519 BUTTON_CheckAutoRadioButton( hWnd );
523 return get_button_state( hWnd );
526 state = get_button_state( hWnd );
529 if (state & BUTTON_HIGHLIGHTED) break;
530 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
534 if (!(state & BUTTON_HIGHLIGHTED)) break;
535 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
537 paint_button( hWnd, btn_type, ODA_SELECT );
541 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
544 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
545 DefWindowProcA(hWnd, uMsg, wParam, lParam);
550 /***********************************************************************
551 * ButtonWndProc_wrapper16
553 static LRESULT ButtonWndProc_wrapper16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL unicode )
555 static const UINT msg16_offset = BM_GETCHECK16 - BM_GETCHECK;
564 return ButtonWndProc_common( hwnd, msg - msg16_offset, wParam, lParam, FALSE );
566 return ButtonWndProc_common( hwnd, msg, wParam, lParam, unicode );
570 /***********************************************************************
573 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
575 if (!IsWindow( hWnd )) return 0;
576 return ButtonWndProc_wrapper16( hWnd, uMsg, wParam, lParam, TRUE );
580 /***********************************************************************
583 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
585 if (!IsWindow( hWnd )) return 0;
586 return ButtonWndProc_wrapper16( hWnd, uMsg, wParam, lParam, FALSE );
590 /**********************************************************************
591 * Convert button styles to flags used by DrawText.
593 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
595 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
597 /* "Convert" pushlike buttons to pushbuttons */
598 if (style & BS_PUSHLIKE)
601 if (!(style & BS_MULTILINE))
602 dtStyle |= DT_SINGLELINE;
604 dtStyle |= DT_WORDBREAK;
606 switch (style & BS_CENTER)
608 case BS_LEFT: /* DT_LEFT is 0 */ break;
609 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
610 case BS_CENTER: dtStyle |= DT_CENTER; break;
612 /* Pushbutton's text is centered by default */
613 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
614 /* all other flavours have left aligned text */
617 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
619 /* DrawText ignores vertical alignment for multiline text,
620 * but we use these flags to align label manually.
622 if (get_button_type(style) != BS_GROUPBOX)
624 switch (style & BS_VCENTER)
626 case BS_TOP: /* DT_TOP is 0 */ break;
627 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
628 case BS_VCENTER: /* fall through */
629 default: dtStyle |= DT_VCENTER; break;
633 /* GroupBox's text is always single line and is top aligned. */
634 dtStyle |= DT_SINGLELINE;
639 /**********************************************************************
640 * BUTTON_CalcLabelRect
642 * Calculates label's rectangle depending on button style.
644 * Returns flags to be passed to DrawText.
645 * Calculated rectangle doesn't take into account button state
646 * (pushed, etc.). If there is nothing to draw (no text/image) output
647 * rectangle is empty, and return value is (UINT)-1.
649 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
651 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
652 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
656 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
660 /* Calculate label rectangle according to label type */
661 switch (style & (BS_ICON|BS_BITMAP))
664 if (!(text = get_button_text( hwnd ))) goto empty_rect;
667 HeapFree( GetProcessHeap(), 0, text );
670 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
671 HeapFree( GetProcessHeap(), 0, text );
675 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
678 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
680 r.right = r.left + bm.bmWidth;
681 r.bottom = r.top + bm.bmHeight;
683 DeleteObject(iconInfo.hbmColor);
684 DeleteObject(iconInfo.hbmMask);
688 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
691 r.right = r.left + bm.bmWidth;
692 r.bottom = r.top + bm.bmHeight;
702 /* Position label inside bounding rectangle according to
703 * alignment flags. (calculated rect is always left-top aligned).
704 * If label is aligned to any side - shift label in opposite
705 * direction to leave extra space for focus rectangle.
707 switch (dtStyle & (DT_CENTER|DT_RIGHT))
709 case DT_LEFT: r.left++; r.right++; break;
710 case DT_CENTER: n = r.right - r.left;
711 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
712 r.right = r.left + n; break;
713 case DT_RIGHT: n = r.right - r.left;
714 r.right = rc->right - 1;
715 r.left = r.right - n;
719 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
721 case DT_TOP: r.top++; r.bottom++; break;
722 case DT_VCENTER: n = r.bottom - r.top;
723 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
724 r.bottom = r.top + n; break;
725 case DT_BOTTOM: n = r.bottom - r.top;
726 r.bottom = rc->bottom - 1;
727 r.top = r.bottom - n;
736 /**********************************************************************
737 * BUTTON_DrawTextCallback
739 * Callback function used by DrawStateW function.
741 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
749 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
754 /**********************************************************************
757 * Common function for drawing button label.
759 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
761 DRAWSTATEPROC lpOutputProc = NULL;
765 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
766 LONG state = get_button_state( hwnd );
767 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
770 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
771 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
772 * I don't have Win31 on hand to verify that, so I leave it as is.
775 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
777 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
781 switch (style & (BS_ICON|BS_BITMAP))
784 /* DST_COMPLEX -- is 0 */
785 lpOutputProc = BUTTON_DrawTextCallback;
786 if (!(text = get_button_text( hwnd ))) return;
788 wp = (WPARAM)dtFlags;
793 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
798 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
805 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
806 rc->right - rc->left, rc->bottom - rc->top, flags);
807 HeapFree( GetProcessHeap(), 0, text );
810 /**********************************************************************
811 * Push Button Functions
813 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
815 RECT rc, focus_rect, r;
816 UINT dtFlags, uState;
820 COLORREF oldTxtColor;
822 LONG state = get_button_state( hwnd );
823 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
824 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
827 GetClientRect( hwnd, &rc );
829 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
830 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
831 parent = GetParent(hwnd);
832 if (!parent) parent = hwnd;
833 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
835 setup_clipping( hwnd, hDC );
837 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
838 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
839 oldBkMode = SetBkMode(hDC, TRANSPARENT);
841 if (get_button_type(style) == BS_DEFPUSHBUTTON)
843 if (action != ODA_FOCUS)
844 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
845 InflateRect( &rc, -1, -1 );
850 /* completely skip the drawing if only focus has changed */
851 if (action == ODA_FOCUS) goto draw_focus;
853 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
857 else if (pushedState)
859 if (get_button_type(style) == BS_DEFPUSHBUTTON )
862 uState |= DFCS_PUSHED;
865 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
866 uState |= DFCS_CHECKED;
868 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
870 /* draw button label */
872 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
874 if (dtFlags == (UINT)-1L)
878 OffsetRect(&r, 1, 1);
880 IntersectClipRect(hDC, rc.left, rc.top, rc.right, rc.bottom);
882 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
884 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
886 SetTextColor( hDC, oldTxtColor );
889 if ((action == ODA_FOCUS) ||
890 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
892 InflateRect( &focus_rect, -1, -1 );
893 IntersectRect(&focus_rect, &focus_rect, &rc);
894 DrawFocusRect( hDC, &focus_rect );
898 SelectObject( hDC, hOldPen );
899 SelectObject( hDC, hOldBrush );
900 SetBkMode(hDC, oldBkMode);
903 /**********************************************************************
904 * Check Box & Radio Button Functions
907 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
909 RECT rbox, rtext, client;
914 LONG state = get_button_state( hwnd );
915 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
918 if (style & BS_PUSHLIKE)
920 PB_Paint( hwnd, hDC, action );
924 GetClientRect(hwnd, &client);
925 rbox = rtext = client;
927 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
929 parent = GetParent(hwnd);
930 if (!parent) parent = hwnd;
931 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
932 (WPARAM)hDC, (LPARAM)hwnd);
933 if (!hBrush) /* did the app forget to call defwindowproc ? */
934 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
935 (WPARAM)hDC, (LPARAM)hwnd );
936 setup_clipping( hwnd, hDC );
938 if (style & BS_LEFTTEXT)
940 /* magic +4 is what CTL3D expects */
942 rtext.right -= checkBoxWidth + 4;
943 rbox.left = rbox.right - checkBoxWidth;
947 rtext.left += checkBoxWidth + 4;
948 rbox.right = checkBoxWidth;
951 /* Since WM_ERASEBKGND does nothing, first prepare background */
952 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
953 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
957 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
959 /* Only adjust rbox when rtext is valid */
960 if (dtFlags != (UINT)-1L)
962 rbox.top = rtext.top;
963 rbox.bottom = rtext.bottom;
966 /* Draw the check-box bitmap */
967 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
971 if ((get_button_type(style) == BS_RADIOBUTTON) ||
972 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
973 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
974 else flags = DFCS_BUTTONCHECK;
976 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
977 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
979 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
981 /* rbox must have the correct height */
982 delta = rbox.bottom - rbox.top - checkBoxHeight;
984 if (style & BS_TOP) {
986 rbox.bottom = rbox.top + checkBoxHeight;
988 rbox.top -= -delta/2 + 1;
989 rbox.bottom = rbox.top + checkBoxHeight;
991 } else if (style & BS_BOTTOM) {
993 rbox.top = rbox.bottom - checkBoxHeight;
995 rbox.bottom += -delta/2 + 1;
996 rbox.top = rbox.bottom - checkBoxHeight;
998 } else { /* Default */
1000 int ofs = (delta / 2);
1001 rbox.bottom -= ofs + 1;
1002 rbox.top = rbox.bottom - checkBoxHeight;
1003 } else if (delta < 0) {
1004 int ofs = (-delta / 2);
1005 rbox.top -= ofs + 1;
1006 rbox.bottom = rbox.top + checkBoxHeight;
1010 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
1013 if (dtFlags == (UINT)-1L) /* Noting to draw */
1016 if (action == ODA_DRAWENTIRE)
1017 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
1020 if ((action == ODA_FOCUS) ||
1021 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1025 IntersectRect(&rtext, &rtext, &client);
1026 DrawFocusRect( hDC, &rtext );
1031 /**********************************************************************
1032 * BUTTON_CheckAutoRadioButton
1034 * hwnd is checked, uncheck every other auto radio button in group
1036 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
1038 HWND parent, sibling, start;
1040 parent = GetParent(hwnd);
1041 /* make sure that starting control is not disabled or invisible */
1042 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1045 if (!sibling) break;
1046 if ((hwnd != sibling) &&
1047 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1048 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1049 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1050 } while (sibling != start);
1054 /**********************************************************************
1055 * Group Box Functions
1058 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1065 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1068 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1069 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1070 parent = GetParent(hwnd);
1071 if (!parent) parent = hwnd;
1072 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1073 if (!hbr) /* did the app forget to call defwindowproc ? */
1074 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1075 (WPARAM)hDC, (LPARAM)hwnd);
1076 setup_clipping( hwnd, hDC );
1078 GetClientRect( hwnd, &rc);
1081 GetTextMetricsW (hDC, &tm);
1082 rcFrame.top += (tm.tmHeight / 2) - 1;
1083 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1085 InflateRect(&rc, -7, 1);
1086 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1088 if (dtFlags == (UINT)-1L)
1091 /* Because buttons have CS_PARENTDC class style, there is a chance
1092 * that label will be drawn out of client rect.
1093 * But Windows doesn't clip label's rect, so do I.
1096 /* There is 1-pixel margin at the left, right, and bottom */
1097 rc.left--; rc.right++; rc.bottom++;
1098 FillRect(hDC, &rc, hbr);
1099 rc.left++; rc.right--; rc.bottom--;
1101 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1105 /**********************************************************************
1106 * User Button Functions
1109 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1114 LONG state = get_button_state( hwnd );
1117 if (action == ODA_SELECT) return;
1119 GetClientRect( hwnd, &rc);
1121 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1123 parent = GetParent(hwnd);
1124 if (!parent) parent = hwnd;
1125 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1126 if (!hBrush) /* did the app forget to call defwindowproc ? */
1127 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1128 (WPARAM)hDC, (LPARAM)hwnd);
1130 FillRect( hDC, &rc, hBrush );
1131 if ((action == ODA_FOCUS) ||
1132 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1133 DrawFocusRect( hDC, &rc );
1135 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1139 /**********************************************************************
1140 * Ownerdrawn Button Functions
1143 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1145 LONG state = get_button_state( hwnd );
1147 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1149 HFONT hFont, hPrevFont = 0;
1151 dis.CtlType = ODT_BUTTON;
1154 dis.itemAction = action;
1155 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1156 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1157 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1158 dis.hwndItem = hwnd;
1161 GetClientRect( hwnd, &dis.rcItem );
1163 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1164 parent = GetParent(hwnd);
1165 if (!parent) parent = hwnd;
1166 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1168 setup_clipping( hwnd, hDC );
1170 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1171 if (hPrevFont) SelectObject(hDC, hPrevFont);