3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS User32
5 * PURPOSE: Button control
6 * FILE: lib/user32/controls/static.c
7 * PROGRAMER: Richard Campbell lib/user32/controls/button.c
8 * REVISION HISTORY: 2003/05/28 Created
9 * NOTES: Adapted from Wine
13 #include "user32/regcontrol.h"
16 #define STATE_GWL_OFFSET 0
17 #define HFONT_GWL_OFFSET (sizeof(LONG))
18 #define HIMAGE_GWL_OFFSET (2*sizeof(LONG))
19 #define NB_EXTRA_BYTES (3*sizeof(LONG))
21 #define BUTTON_UNCHECKED 0x00
22 #define BUTTON_CHECKED 0x01
23 #define BUTTON_3STATE 0x02
24 #define BUTTON_HIGHLIGHTED 0x04
25 #define BUTTON_HASFOCUS 0x08
26 #define BUTTON_NSTATES 0x0F
28 #define BUTTON_BTNPRESSED 0x40
29 #define BUTTON_UNKNOWN2 0x20
30 #define BUTTON_UNKNOWN3 0x10
32 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
33 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
34 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
35 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
36 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
37 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
38 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
39 // static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
40 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
42 #define MAX_BTN_TYPE 12
44 static const WORD maxCheckState[MAX_BTN_TYPE] =
46 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
47 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
48 BUTTON_CHECKED, /* BS_CHECKBOX */
49 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
50 BUTTON_CHECKED, /* BS_RADIOBUTTON */
51 BUTTON_3STATE, /* BS_3STATE */
52 BUTTON_3STATE, /* BS_AUTO3STATE */
53 BUTTON_UNCHECKED, /* BS_GROUPBOX */
54 BUTTON_UNCHECKED, /* BS_USERBUTTON */
55 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
56 BUTTON_UNCHECKED, /* Not defined */
57 BUTTON_UNCHECKED /* BS_OWNERDRAW */
60 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
62 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
64 PB_Paint, /* BS_PUSHBUTTON */
65 PB_Paint, /* BS_DEFPUSHBUTTON */
66 CB_Paint, /* BS_CHECKBOX */
67 CB_Paint, /* BS_AUTOCHECKBOX */
68 CB_Paint, /* BS_RADIOBUTTON */
69 CB_Paint, /* BS_3STATE */
70 CB_Paint, /* BS_AUTO3STATE */
71 GB_Paint, /* BS_GROUPBOX */
72 UB_Paint, /* BS_USERBUTTON */
73 CB_Paint, /* BS_AUTORADIOBUTTON */
74 NULL, /* Not defined */
75 OB_Paint /* BS_OWNERDRAW */
78 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
81 /*********************************************************************
82 * button class descriptor
84 const struct builtin_class_descr BUTTON_builtin_class =
87 CS_GLOBALCLASS | CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
88 (WNDPROC) ButtonWndProcW, /* procW */
89 NB_EXTRA_BYTES, /* extra */
90 (LPCWSTR) IDC_ARROW, /* cursor */
94 HPEN STDCALL GetSysColorPen (int nIndex);
96 inline static LONG get_button_state( HWND hwnd )
98 return GetWindowLongA( hwnd, STATE_GWL_OFFSET );
101 inline static void set_button_state( HWND hwnd, LONG state )
103 SetWindowLongA( hwnd, STATE_GWL_OFFSET, state );
106 inline static HFONT get_button_font( HWND hwnd )
108 return (HFONT)GetWindowLongA( hwnd, HFONT_GWL_OFFSET );
111 inline static void set_button_font( HWND hwnd, HFONT font )
113 SetWindowLongA( hwnd, HFONT_GWL_OFFSET, (LONG)font );
116 inline static UINT get_button_type( LONG window_style )
118 return (window_style & 0x0f);
121 /* paint a button of any type */
122 inline static void paint_button( HWND hwnd, LONG style, UINT action )
124 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
126 HDC hdc = GetDC( hwnd );
127 btnPaintFunc[style]( hwnd, hdc, action );
128 ReleaseDC( hwnd, hdc );
132 /* retrieve the button text; returned buffer must be freed by caller */
133 inline static WCHAR *get_button_text( HWND hwnd )
137 DbgPrint("[button] In get_button_text()\n");
138 len = GetWindowTextLengthW( hwnd );
139 buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
142 GetWindowTextW( hwnd, buffer, len );
145 DbgPrint("[button] TextLen %d Text = %s\n", len, buffer);
149 /***********************************************************************
150 * ButtonWndProc_common
152 static LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
153 WPARAM wParam, LPARAM lParam, BOOL unicode )
162 DbgPrint("[button] ButtonWndProc called : msg %d\n", uMsg);
164 style = GetWindowLongA( hWnd, GWL_STYLE );
165 btn_type = get_button_type( style );
167 pt.x = LOWORD(lParam);
168 pt.y = HIWORD(lParam);
175 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
176 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
178 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
179 default: return DLGC_BUTTON;
183 DbgPrint("[button] WM_ENABLE\n");
184 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
188 DbgPrint("[button] WM_CREATE\n");
193 if (btn_type >= MAX_BTN_TYPE)
194 return -1; /* abort */
195 set_button_state( hWnd, BUTTON_UNCHECKED );
196 DbgPrint("[button] Done creating\n");
200 DbgPrint("[button] WM_ERASEBKGND\n");
204 DbgPrint("[button] WM_PAINT\n");
205 if (btnPaintFunc[btn_type])
210 DbgPrint("[button] About to draw...\n");
211 hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
212 nOldMode = SetBkMode( hdc, OPAQUE );
213 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
214 SetBkMode(hdc, nOldMode); /* reset painting mode */
215 if( !wParam ) EndPaint( hWnd, &ps );
220 if (wParam == VK_SPACE)
222 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
223 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
227 case WM_LBUTTONDBLCLK:
228 if(style & BS_NOTIFY ||
229 btn_type == BS_RADIOBUTTON ||
230 btn_type == BS_USERBUTTON ||
231 btn_type == BS_OWNERDRAW)
233 SendMessageW( GetParent(hWnd), WM_COMMAND,
234 MAKEWPARAM( GetWindowLongA(hWnd,GWL_ID), BN_DOUBLECLICKED ),
242 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
243 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
247 if (wParam != VK_SPACE)
251 state = get_button_state( hWnd );
252 if (!(state & BUTTON_BTNPRESSED)) break;
253 state &= BUTTON_NSTATES;
254 set_button_state( hWnd, state );
255 if (!(state & BUTTON_HIGHLIGHTED))
260 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
262 GetClientRect( hWnd, &rect );
263 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
265 state = get_button_state( hWnd );
268 case BS_AUTOCHECKBOX:
269 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
271 case BS_AUTORADIOBUTTON:
272 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
275 SendMessageW( hWnd, BM_SETCHECK,
276 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
279 SendMessageW( GetParent(hWnd), WM_COMMAND,
280 MAKEWPARAM( GetWindowLongA(hWnd,GWL_ID), BN_CLICKED ), (LPARAM)hWnd);
284 case WM_CAPTURECHANGED:
285 state = get_button_state( hWnd );
286 if (state & BUTTON_BTNPRESSED)
288 state &= BUTTON_NSTATES;
289 set_button_state( hWnd, state );
290 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
295 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
297 GetClientRect( hWnd, &rect );
298 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
307 DbgPrint("[button] WM_SETTEXT");
308 /* Clear an old text here as Windows does */
311 hbrush = (HBRUSH)SendMessageW(GetParent(hWnd), WM_CTLCOLORSTATIC,
312 (WPARAM)hdc, (LPARAM)hWnd);
313 if (!hbrush) /* did the app forget to call DefWindowProc ? */
314 hbrush = (HBRUSH)DefWindowProcW(GetParent(hWnd), WM_CTLCOLORSTATIC,
315 (WPARAM)hdc, (LPARAM)hWnd);
317 GetClientRect(hWnd, &client);
319 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
320 /* Clip by client rect bounds */
321 if (rc.right > client.right) rc.right = client.right;
322 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
323 FillRect(hdc, &rc, hbrush);
324 ReleaseDC(hWnd, hdc);
326 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
327 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
328 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
329 InvalidateRect( hWnd, NULL, TRUE );
331 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
332 return 1; /* success. FIXME: check text length */
336 DbgPrint("[button] WM_SETFONT");
337 set_button_font( hWnd, (HFONT)wParam );
338 if (lParam) paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
342 DbgPrint("[button] WM_GETFONT");
343 return (LRESULT)get_button_font( hWnd );
346 DbgPrint("[button] WM_SETFOCUS");
347 if ((btn_type == BS_RADIOBUTTON || btn_type == BS_AUTORADIOBUTTON) && (GetCapture() != hWnd) &&
348 !(SendMessageW(hWnd, BM_GETCHECK, 0, 0) & BST_CHECKED))
350 /* The notification is sent when the button (BS_AUTORADIOBUTTON)
351 is unchecked and the focus was not given by a mouse click. */
352 if (btn_type == BS_AUTORADIOBUTTON)
353 SendMessageW( hWnd, BM_SETCHECK, BUTTON_CHECKED, 0 );
354 SendMessageW( GetParent(hWnd), WM_COMMAND,
355 MAKEWPARAM( GetWindowLongA(hWnd,GWL_ID), BN_CLICKED ), (LPARAM)hWnd);
357 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
358 paint_button( hWnd, btn_type, ODA_FOCUS );
362 DbgPrint("[button] WM_KILLFOCUS");
363 set_button_state( hWnd, get_button_state(hWnd) & ~BUTTON_HASFOCUS );
364 paint_button( hWnd, btn_type, ODA_FOCUS );
365 InvalidateRect( hWnd, NULL, TRUE );
368 case WM_SYSCOLORCHANGE:
369 DbgPrint("[button] WM_SYSCOLORCHANGE");
370 InvalidateRect( hWnd, NULL, FALSE );
374 DbgPrint("[button] BM_SETSTYLE");
375 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
376 btn_type = wParam & 0x0f;
377 style = (style & ~0x0f) | btn_type;
378 SetWindowLongA( hWnd, GWL_STYLE, style );
380 /* Only redraw if lParam flag is set.*/
382 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
387 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
388 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
392 /* Check that image format matches button style */
393 switch (style & (BS_BITMAP|BS_ICON))
396 if (wParam != IMAGE_BITMAP) return 0;
399 if (wParam != IMAGE_ICON) return 0;
404 oldHbitmap = (HBITMAP)SetWindowLongA( hWnd, HIMAGE_GWL_OFFSET, lParam );
405 InvalidateRect( hWnd, NULL, FALSE );
406 return (LRESULT)oldHbitmap;
409 return GetWindowLongA( hWnd, HIMAGE_GWL_OFFSET );
412 return get_button_state( hWnd ) & 3;
415 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
416 state = get_button_state( hWnd );
417 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
419 if (wParam) style |= WS_TABSTOP;
420 else style &= ~WS_TABSTOP;
421 SetWindowLongA( hWnd, GWL_STYLE, style );
423 if (((WPARAM) state & 3) != wParam)
425 set_button_state( hWnd, (state & ~3) | wParam );
426 paint_button( hWnd, btn_type, ODA_SELECT );
428 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
429 BUTTON_CheckAutoRadioButton( hWnd );
433 return get_button_state( hWnd );
436 state = get_button_state( hWnd );
439 if (state & BUTTON_HIGHLIGHTED) break;
440 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
444 if (!(state & BUTTON_HIGHLIGHTED)) break;
445 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
447 paint_button( hWnd, btn_type, ODA_SELECT );
451 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
454 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
455 DefWindowProcA(hWnd, uMsg, wParam, lParam);
460 /***********************************************************************
462 * The button window procedure. This is just a wrapper which locks
463 * the passed HWND and calls the real window procedure (with a WND*
464 * pointer pointing to the locked windowstructure).
466 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
468 if (!IsWindow( hWnd )) return 0;
469 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
474 /***********************************************************************
477 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
479 if (!IsWindow( hWnd )) return 0;
480 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
485 /**********************************************************************
486 * Convert button styles to flags used by DrawText.
487 * TODO: handle WS_EX_RIGHT extended style.
489 static UINT BUTTON_BStoDT(DWORD style)
491 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
493 /* "Convert" pushlike buttons to pushbuttons */
494 if (style & BS_PUSHLIKE)
497 if (!(style & BS_MULTILINE))
498 dtStyle |= DT_SINGLELINE;
500 dtStyle |= DT_WORDBREAK;
502 switch (style & BS_CENTER)
504 case BS_LEFT: /* DT_LEFT is 0 */ break;
505 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
506 case BS_CENTER: dtStyle |= DT_CENTER; break;
508 /* Pushbutton's text is centered by default */
509 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
510 /* all other flavours have left aligned text */
513 /* DrawText ignores vertical alignment for multiline text,
514 * but we use these flags to align label manualy.
516 if (get_button_type(style) != BS_GROUPBOX)
518 switch (style & BS_VCENTER)
520 case BS_TOP: /* DT_TOP is 0 */ break;
521 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
522 case BS_VCENTER: /* fall through */
523 default: dtStyle |= DT_VCENTER; break;
527 /* GroupBox's text is always single line and is top aligned. */
528 dtStyle |= DT_SINGLELINE;
533 /**********************************************************************
534 * BUTTON_CalcLabelRect
536 * Calculates label's rectangle depending on button style.
538 * Returns flags to be passed to DrawText.
539 * Calculated rectangle doesn't take into account button state
540 * (pushed, etc.). If there is nothing to draw (no text/image) output
541 * rectangle is empty, and return value is (UINT)-1.
543 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
553 DbgPrint("[button] In BUTTON_CalcLabelRect()\n");
554 style = GetWindowLongW( hwnd, GWL_STYLE );
556 dtStyle = BUTTON_BStoDT(style);
558 /* Calculate label rectangle according to label type */
559 switch (style & (BS_ICON|BS_BITMAP))
562 DbgPrint("[button] BS_TEXT\n");
563 if (!(text = get_button_text( hwnd ))) goto empty_rect;
566 HeapFree( GetProcessHeap(), 0, text );
569 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
570 HeapFree( GetProcessHeap(), 0, text );
574 DbgPrint("[button] BS_ICON\n");
575 if (!GetIconInfo((HICON)GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
578 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
580 r.right = r.left + bm.bmWidth;
581 r.bottom = r.top + bm.bmHeight;
583 DeleteObject(iconInfo.hbmColor);
584 DeleteObject(iconInfo.hbmMask);
588 DbgPrint("[button] BS_BITMAP\n");
589 if (!GetObjectW( (HANDLE)GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
592 r.right = r.left + bm.bmWidth;
593 r.bottom = r.top + bm.bmHeight;
598 DbgPrint("[button] EMPTY RECT!\n");
601 return (UINT)(LONG)-1;
604 /* Position label inside bounding rectangle according to
605 * alignment flags. (calculated rect is always left-top aligned).
606 * If label is aligned to any side - shift label in opposite
607 * direction to leave extra space for focus rectangle.
609 switch (dtStyle & (DT_CENTER|DT_RIGHT))
611 case DT_LEFT: r.left++; r.right++; break;
612 case DT_CENTER: n = r.right - r.left;
613 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
614 r.right = r.left + n; break;
615 case DT_RIGHT: n = r.right - r.left;
616 r.right = rc->right - 1;
617 r.left = r.right - n;
621 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
623 case DT_TOP: r.top++; r.bottom++; break;
624 case DT_VCENTER: n = r.bottom - r.top;
625 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
626 r.bottom = r.top + n; break;
627 case DT_BOTTOM: n = r.bottom - r.top;
628 r.bottom = rc->bottom - 1;
629 r.top = r.bottom - n;
633 DbgPrint("[button] Resulting rectangle = %dx%d to %dx%d\n", r.left, r.top, r.bottom, r.right);
640 /**********************************************************************
641 * BUTTON_DrawTextCallback
643 * Callback function used by DrawStateW function.
645 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
653 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
658 /**********************************************************************
661 * Common function for drawing button label.
663 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
665 DRAWSTATEPROC lpOutputProc = NULL;
674 DbgPrint("[button] In BUTTON_DrawLabel()\n");
675 flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
676 state = get_button_state( hwnd );
677 style = GetWindowLongA( hwnd, GWL_STYLE );
679 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
680 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
681 * I don't have Win31 on hand to verify that, so I leave it as is.
684 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
686 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
690 switch (style & (BS_ICON|BS_BITMAP))
693 /* DST_COMPLEX -- is 0 */
694 lpOutputProc = BUTTON_DrawTextCallback;
695 if (!(text = get_button_text( hwnd ))) return;
697 wp = (WPARAM)dtFlags;
702 lp = GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET );
707 lp = GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET );
714 DbgPrint("[button] DrawState\n");
715 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
716 rc->right - rc->left, rc->bottom - rc->top, flags);
717 if (text) HeapFree( GetProcessHeap(), 0, text );
720 /**********************************************************************
721 * Push Button Functions
723 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
725 RECT rc, focus_rect, r;
731 COLORREF oldTxtColor;
738 DbgPrint("[button] In PB_Paint()\n");
739 state = get_button_state( hwnd );
740 style = GetWindowLongA( hwnd, GWL_STYLE );
741 pushedState = (state & BUTTON_HIGHLIGHTED);
743 GetClientRect( hwnd, &rc );
745 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
746 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
747 SendMessageW( GetParent(hwnd), WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
748 hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
749 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
750 oldBkMode = SetBkMode(hDC, TRANSPARENT);
752 if (get_button_type(style) == BS_DEFPUSHBUTTON)
754 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
755 InflateRect( &rc, -1, -1 );
758 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
762 else if (pushedState)
764 if (get_button_type(style) == BS_DEFPUSHBUTTON )
767 uState |= DFCS_PUSHED;
770 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
771 uState |= DFCS_CHECKED;
773 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
777 /* draw button label */
778 DbgPrint("[button] About to calculate label rectangle\n");
780 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
782 if (dtFlags == (UINT)-1L)
784 DbgPrint("[button] JUMPING TO CLEANUP!\n");
789 OffsetRect(&r, 1, 1);
791 hRgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
792 SelectClipRgn(hDC, hRgn);
794 DbgPrint("[button] Setting text color\n");
795 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
797 DbgPrint("[button] Calling BUTTON_DrawLabel\n");
798 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
800 SetTextColor( hDC, oldTxtColor );
801 SelectClipRgn(hDC, 0);
804 if (state & BUTTON_HASFOCUS)
806 InflateRect( &focus_rect, -1, -1 );
807 IntersectRect(&focus_rect, &focus_rect, &rc);
808 DrawFocusRect( hDC, &focus_rect );
812 SelectObject( hDC, hOldPen );
813 SelectObject( hDC, hOldBrush );
814 SetBkMode(hDC, oldBkMode);
816 DbgPrint("[button] Quitting\n");
819 /**********************************************************************
820 * Check Box & Radio Button Functions
823 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
825 RECT rbox, rtext, client;
831 LONG state = get_button_state( hwnd );
832 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
834 if (style & BS_PUSHLIKE)
836 PB_Paint( hwnd, hDC, action );
840 GetClientRect(hwnd, &client);
841 rbox = rtext = client;
843 if ((hFont = get_button_font( hwnd )))
844 SelectObject( hDC, hFont );
846 hBrush = (HBRUSH)SendMessageW(GetParent(hwnd), WM_CTLCOLORSTATIC,
847 (WPARAM)hDC, (LPARAM)hwnd);
848 if (!hBrush) /* did the app forget to call defwindowproc ? */
849 hBrush = (HBRUSH)DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC,
850 (WPARAM)hDC, (LPARAM)hwnd );
852 if (style & BS_LEFTTEXT)
854 /* magic +4 is what CTL3D expects */
856 rtext.right -= checkBoxWidth + 4;
857 rbox.left = rbox.right - checkBoxWidth;
861 rtext.left += checkBoxWidth + 4;
862 rbox.right = checkBoxWidth;
865 /* Since WM_ERASEBKGND does nothing, first prepare background */
866 if (action == ODA_SELECT)
867 FillRect( hDC, &rbox, hBrush );
868 if (action == ODA_DRAWENTIRE)
869 FillRect( hDC, &client, hBrush );
873 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
875 rbox.top = rtext.top;
876 rbox.bottom = rtext.bottom;
877 /* Draw the check-box bitmap */
878 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
882 if ((get_button_type(style) == BS_RADIOBUTTON) ||
883 (get_button_type(style) == BS_AUTORADIOBUTTON))
884 flags = DFCS_BUTTONRADIO;
885 else if (state & BUTTON_3STATE)
886 flags = DFCS_BUTTON3STATE;
888 flags = DFCS_BUTTONCHECK;
890 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
891 flags |= DFCS_CHECKED;
892 if (state & BUTTON_HIGHLIGHTED)
893 flags |= DFCS_PUSHED;
895 if (style & WS_DISABLED)
896 flags |= DFCS_INACTIVE;
898 /* rbox must have the correct height */
899 delta = rbox.bottom - rbox.top - checkBoxHeight;
905 rbox.bottom = rbox.top + checkBoxHeight;
909 rbox.top -= -delta/2 + 1;
910 rbox.bottom += rbox.top + checkBoxHeight;
913 else if (style & BS_BOTTOM)
917 rbox.top = rbox.bottom - checkBoxHeight;
921 rbox.bottom += -delta/2 + 1;
922 rbox.top = rbox.bottom -= checkBoxHeight;
930 int ofs = (delta / 2);
931 rbox.bottom -= ofs + 1;
932 rbox.top = rbox.bottom - checkBoxHeight;
936 int ofs = (-delta / 2);
938 rbox.bottom = rbox.top + checkBoxHeight;
942 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
945 if (dtFlags == (UINT)-1L) /* Noting to draw */
948 hRgn = CreateRectRgn(client.left, client.top, client.right, client.bottom);
949 SelectClipRgn(hDC, hRgn);
952 if (action == ODA_DRAWENTIRE)
953 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
956 if ((action == ODA_FOCUS) ||
957 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
961 IntersectRect(&rtext, &rtext, &client);
962 DrawFocusRect( hDC, &rtext );
964 SelectClipRgn(hDC, 0);
968 /**********************************************************************
969 * BUTTON_CheckAutoRadioButton
971 * hwnd is checked, uncheck every other auto radio button in group
973 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
975 HWND parent, sibling, start;
977 parent = GetParent(hwnd);
978 /* make sure that starting control is not disabled or invisible */
979 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
983 if ((hwnd != sibling) &&
984 ((GetWindowLongA( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
985 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
986 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
987 } while (sibling != start);
991 /**********************************************************************
992 * Group Box Functions
995 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1002 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1004 if (action != ODA_DRAWENTIRE) return;
1006 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1007 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1008 hbr = (HBRUSH)SendMessageW(GetParent(hwnd), WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1009 if (!hbr) /* did the app forget to call defwindowproc ? */
1010 hbr = (HBRUSH)DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC,
1011 (WPARAM)hDC, (LPARAM)hwnd);
1013 GetClientRect( hwnd, &rc);
1017 GetTextMetricsW (hDC, &tm);
1018 rcFrame.top += (tm.tmHeight / 2) - 1;
1019 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1021 InflateRect(&rc, -7, 1);
1022 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1024 if (dtFlags == (UINT)-1L)
1027 /* Because buttons have CS_PARENTDC class style, there is a chance
1028 * that label will be drawn out of client rect.
1029 * But Windows doesn't clip label's rect, so do I.
1032 /* There is 1-pixel marging at the left, right, and bottom */
1033 rc.left--; rc.right++; rc.bottom++;
1034 FillRect(hDC, &rc, hbr);
1035 rc.left++; rc.right--; rc.bottom--;
1037 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1041 /**********************************************************************
1042 * User Button Functions
1045 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1050 LONG state = get_button_state( hwnd );
1052 if (action == ODA_SELECT) return;
1054 GetClientRect( hwnd, &rc);
1056 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1058 hBrush = (HBRUSH)SendMessageW(GetParent(hwnd), WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1059 if (!hBrush) /* did the app forget to call defwindowproc ? */
1060 hBrush = (HBRUSH)DefWindowProcW(GetParent(hwnd), WM_CTLCOLORBTN,
1061 (WPARAM)hDC, (LPARAM)hwnd);
1063 FillRect( hDC, &rc, hBrush );
1064 if ((action == ODA_FOCUS) ||
1065 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1066 DrawFocusRect( hDC, &rc );
1070 /**********************************************************************
1071 * Ownerdrawn Button Functions
1074 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1076 LONG state = get_button_state( hwnd );
1080 UINT id = GetWindowLongA( hwnd, GWL_ID );
1082 dis.CtlType = ODT_BUTTON;
1085 dis.itemAction = action;
1086 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1087 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1088 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1089 dis.hwndItem = hwnd;
1092 GetClientRect( hwnd, &dis.rcItem );
1094 clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1095 if (GetClipRgn(hDC, clipRegion) != 1)
1097 DeleteObject(clipRegion);
1100 clipRect = dis.rcItem;
1101 DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1102 IntersectClipRect(hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
1104 SetBkColor( hDC, GetSysColor( COLOR_BTNFACE ) );
1105 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1106 SelectClipRgn(hDC, clipRegion);