dxgi: Add IDXGISwapChain2 stubs for D3D11.
[wine.git] / dlls / user32 / button.c
blob10a111562a72611d443e7ae2878e793141268a98
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
21 * TODO
22 * Styles
23 * - BS_NOTIFY: is it complete?
24 * - BS_RIGHTBUTTON: same as BS_LEFTTEXT
26 * Messages
27 * - WM_CHAR: Checks a (manual or automatic) check box on '+' or '=', clears it on '-' key.
28 * - WM_SETFOCUS: For (manual or automatic) radio buttons, send the parent window BN_CLICKED
29 * - WM_NCCREATE: Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
30 * - WM_SYSKEYUP
32 * Notifications
33 * - BN_DISABLE
34 * - BN_PUSHED/BN_HILITE
35 * + BN_KILLFOCUS: is it OK?
36 * - BN_PAINT
37 * + BN_SETFOCUS: is it OK?
38 * - BN_UNPUSHED/BN_UNHILITE
41 #define OEMRESOURCE
43 #include "user_private.h"
44 #include "controls.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(button);
49 /* GetWindowLong offsets for window extra information */
50 #define STATE_GWL_OFFSET 0
51 #define HFONT_GWL_OFFSET (sizeof(LONG))
52 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
53 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
55 /* undocumented flags */
56 #define BUTTON_NSTATES 0x0F
57 #define BUTTON_BTNPRESSED 0x40
58 #define BUTTON_UNKNOWN2 0x20
59 #define BUTTON_UNKNOWN3 0x10
61 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
62 do { /* Notify parent which has created this button control */ \
63 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
64 SendMessageW(GetParent(hWnd), WM_COMMAND, \
65 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
66 (LPARAM)(hWnd)); \
67 } while(0)
69 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
70 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
71 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
72 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
73 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
74 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
75 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
77 #define MAX_BTN_TYPE 16
79 static const WORD maxCheckState[MAX_BTN_TYPE] =
81 BST_UNCHECKED, /* BS_PUSHBUTTON */
82 BST_UNCHECKED, /* BS_DEFPUSHBUTTON */
83 BST_CHECKED, /* BS_CHECKBOX */
84 BST_CHECKED, /* BS_AUTOCHECKBOX */
85 BST_CHECKED, /* BS_RADIOBUTTON */
86 BST_INDETERMINATE, /* BS_3STATE */
87 BST_INDETERMINATE, /* BS_AUTO3STATE */
88 BST_UNCHECKED, /* BS_GROUPBOX */
89 BST_UNCHECKED, /* BS_USERBUTTON */
90 BST_CHECKED, /* BS_AUTORADIOBUTTON */
91 BST_UNCHECKED, /* BS_PUSHBOX */
92 BST_UNCHECKED /* BS_OWNERDRAW */
95 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
97 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
99 PB_Paint, /* BS_PUSHBUTTON */
100 PB_Paint, /* BS_DEFPUSHBUTTON */
101 CB_Paint, /* BS_CHECKBOX */
102 CB_Paint, /* BS_AUTOCHECKBOX */
103 CB_Paint, /* BS_RADIOBUTTON */
104 CB_Paint, /* BS_3STATE */
105 CB_Paint, /* BS_AUTO3STATE */
106 GB_Paint, /* BS_GROUPBOX */
107 UB_Paint, /* BS_USERBUTTON */
108 CB_Paint, /* BS_AUTORADIOBUTTON */
109 NULL, /* BS_PUSHBOX */
110 OB_Paint /* BS_OWNERDRAW */
114 static inline LONG get_button_state( HWND hwnd )
116 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
119 static inline void set_button_state( HWND hwnd, LONG state )
121 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
124 static inline HFONT get_button_font( HWND hwnd )
126 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
129 static inline void set_button_font( HWND hwnd, HFONT font )
131 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
134 static inline UINT get_button_type( LONG window_style )
136 return (window_style & BS_TYPEMASK);
139 /* paint a button of any type */
140 static inline void paint_button( HWND hwnd, LONG style, UINT action )
142 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
144 HDC hdc = NtUserGetDC( hwnd );
145 btnPaintFunc[style]( hwnd, hdc, action );
146 NtUserReleaseDC( hwnd, hdc );
150 /* retrieve the button text; returned buffer must be freed by caller */
151 static inline WCHAR *get_button_text( HWND hwnd )
153 static const INT len = 512;
154 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
155 if (buffer) NtUserInternalGetWindowText( hwnd, buffer, len + 1 );
156 return buffer;
159 /***********************************************************************
160 * ButtonWndProc_common
162 LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL unicode )
164 RECT rect;
165 POINT pt;
166 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
167 UINT btn_type = get_button_type( style );
168 LONG state;
169 HANDLE oldHbitmap;
171 if (!IsWindow( hWnd )) return 0;
173 pt.x = (short)LOWORD(lParam);
174 pt.y = (short)HIWORD(lParam);
176 switch (uMsg)
178 case WM_GETDLGCODE:
179 switch(btn_type)
181 case BS_USERBUTTON:
182 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
183 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
184 case BS_RADIOBUTTON:
185 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
186 case BS_GROUPBOX: return DLGC_STATIC;
187 default: return DLGC_BUTTON;
190 case WM_ENABLE:
191 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
192 break;
194 case WM_CREATE:
195 if (btn_type >= MAX_BTN_TYPE)
196 return -1; /* abort */
198 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
199 if (btn_type == BS_USERBUTTON )
201 style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
202 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
204 set_button_state( hWnd, BST_UNCHECKED );
205 return 0;
207 case WM_ERASEBKGND:
208 if (btn_type == BS_OWNERDRAW)
210 HDC hdc = (HDC)wParam;
211 RECT rc;
212 HBRUSH hBrush;
213 HWND parent = GetParent(hWnd);
214 if (!parent) parent = hWnd;
215 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
216 if (!hBrush) /* did the app forget to call defwindowproc ? */
217 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
218 (WPARAM)hdc, (LPARAM)hWnd);
219 GetClientRect(hWnd, &rc);
220 FillRect(hdc, &rc, hBrush);
222 return 1;
224 case WM_PRINTCLIENT:
225 case WM_PAINT:
227 PAINTSTRUCT ps;
228 HDC hdc = wParam ? (HDC)wParam : NtUserBeginPaint( hWnd, &ps );
229 if (btnPaintFunc[btn_type])
231 int nOldMode = SetBkMode( hdc, OPAQUE );
232 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
233 SetBkMode(hdc, nOldMode); /* reset painting mode */
235 if (!wParam) NtUserEndPaint( hWnd, &ps );
236 break;
239 case WM_KEYDOWN:
240 if (wParam == VK_SPACE)
242 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
243 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
244 NtUserSetCapture( hWnd );
246 break;
248 case WM_LBUTTONDBLCLK:
249 if(style & BS_NOTIFY ||
250 btn_type == BS_RADIOBUTTON ||
251 btn_type == BS_USERBUTTON ||
252 btn_type == BS_OWNERDRAW)
254 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
255 break;
257 /* fall through */
258 case WM_LBUTTONDOWN:
259 NtUserSetCapture( hWnd );
260 NtUserSetFocus( hWnd );
261 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
262 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
263 break;
265 case WM_KEYUP:
266 if (wParam != VK_SPACE)
267 break;
268 /* fall through */
269 case WM_LBUTTONUP:
270 state = get_button_state( hWnd );
271 if (!(state & BUTTON_BTNPRESSED)) break;
272 state &= BUTTON_NSTATES;
273 set_button_state( hWnd, state );
274 if (!(state & BST_PUSHED))
276 ReleaseCapture();
277 break;
279 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
280 GetClientRect( hWnd, &rect );
281 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
283 state = get_button_state( hWnd );
284 switch(btn_type)
286 case BS_AUTOCHECKBOX:
287 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
288 break;
289 case BS_AUTORADIOBUTTON:
290 BUTTON_CheckAutoRadioButton( hWnd );
291 break;
292 case BS_AUTO3STATE:
293 SendMessageW( hWnd, BM_SETCHECK,
294 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
295 break;
297 ReleaseCapture();
298 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
300 else
302 ReleaseCapture();
304 break;
306 case WM_CAPTURECHANGED:
307 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
308 if (hWnd == (HWND)lParam) break;
309 state = get_button_state( hWnd );
310 if (state & BUTTON_BTNPRESSED)
312 state &= BUTTON_NSTATES;
313 set_button_state( hWnd, state );
314 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
316 break;
318 case WM_MOUSEMOVE:
319 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
321 GetClientRect( hWnd, &rect );
322 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
324 break;
326 case WM_SETTEXT:
328 /* Clear an old text here as Windows does */
329 if (IsWindowVisible(hWnd))
331 HDC hdc = NtUserGetDC(hWnd);
332 HBRUSH hbrush;
333 RECT client, rc;
334 HWND parent = GetParent(hWnd);
335 UINT message = (btn_type == BS_PUSHBUTTON ||
336 btn_type == BS_DEFPUSHBUTTON ||
337 btn_type == BS_USERBUTTON ||
338 btn_type == BS_OWNERDRAW) ?
339 WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
341 if (!parent) parent = hWnd;
342 hbrush = (HBRUSH)SendMessageW(parent, message,
343 (WPARAM)hdc, (LPARAM)hWnd);
344 if (!hbrush) /* did the app forget to call DefWindowProc ? */
345 hbrush = (HBRUSH)DefWindowProcW(parent, message,
346 (WPARAM)hdc, (LPARAM)hWnd);
348 GetClientRect(hWnd, &client);
349 rc = client;
350 /* FIXME: check other BS_* handlers */
351 if (btn_type == BS_GROUPBOX)
352 InflateRect(&rc, -7, 1); /* GB_Paint does this */
353 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
354 /* Clip by client rect bounds */
355 if (rc.right > client.right) rc.right = client.right;
356 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
357 FillRect(hdc, &rc, hbrush);
358 NtUserReleaseDC( hWnd, hdc );
361 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
362 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
363 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
364 NtUserInvalidateRect( hWnd, NULL, TRUE );
365 else
366 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
367 return 1; /* success. FIXME: check text length */
370 case WM_SETFONT:
371 set_button_font( hWnd, (HFONT)wParam );
372 if (lParam) NtUserInvalidateRect(hWnd, NULL, TRUE);
373 break;
375 case WM_GETFONT:
376 return (LRESULT)get_button_font( hWnd );
378 case WM_SETFOCUS:
379 TRACE("WM_SETFOCUS %p\n",hWnd);
380 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
381 paint_button( hWnd, btn_type, ODA_FOCUS );
382 if (style & BS_NOTIFY)
383 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
384 break;
386 case WM_KILLFOCUS:
387 TRACE("WM_KILLFOCUS %p\n",hWnd);
388 state = get_button_state( hWnd );
389 set_button_state( hWnd, state & ~BST_FOCUS );
390 paint_button( hWnd, btn_type, ODA_FOCUS );
392 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
393 ReleaseCapture();
394 if (style & BS_NOTIFY)
395 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
397 NtUserInvalidateRect( hWnd, NULL, FALSE );
398 break;
400 case WM_SYSCOLORCHANGE:
401 NtUserInvalidateRect( hWnd, NULL, FALSE );
402 break;
404 case BM_SETSTYLE:
405 btn_type = wParam & BS_TYPEMASK;
406 style = (style & ~BS_TYPEMASK) | btn_type;
407 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
409 /* Only redraw if lParam flag is set.*/
410 if (lParam)
411 NtUserInvalidateRect( hWnd, NULL, TRUE );
413 break;
415 case BM_CLICK:
416 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
417 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
418 break;
420 case BM_SETIMAGE:
421 /* Check that image format matches button style */
422 switch (style & (BS_BITMAP|BS_ICON))
424 case BS_BITMAP:
425 if (wParam != IMAGE_BITMAP) return 0;
426 break;
427 case BS_ICON:
428 if (wParam != IMAGE_ICON) return 0;
429 break;
430 default:
431 return 0;
433 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
434 NtUserInvalidateRect( hWnd, NULL, FALSE );
435 return (LRESULT)oldHbitmap;
437 case BM_GETIMAGE:
438 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
440 case BM_GETCHECK:
441 return get_button_state( hWnd ) & 3;
443 case BM_SETCHECK:
444 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
445 state = get_button_state( hWnd );
446 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
448 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
449 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
451 if ((state & 3) != wParam)
453 set_button_state( hWnd, (state & ~3) | wParam );
454 paint_button( hWnd, btn_type, ODA_SELECT );
456 break;
458 case BM_GETSTATE:
459 return get_button_state( hWnd );
461 case BM_SETSTATE:
462 state = get_button_state( hWnd );
463 if (wParam)
464 set_button_state( hWnd, state | BST_PUSHED );
465 else
466 set_button_state( hWnd, state & ~BST_PUSHED );
468 paint_button( hWnd, btn_type, ODA_SELECT );
469 break;
471 case WM_NCHITTEST:
472 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
473 /* fall through */
474 default:
475 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
476 DefWindowProcA(hWnd, uMsg, wParam, lParam);
478 return 0;
481 /**********************************************************************
482 * Convert button styles to flags used by DrawText.
484 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
486 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
488 /* "Convert" pushlike buttons to pushbuttons */
489 if (style & BS_PUSHLIKE)
490 style &= ~BS_TYPEMASK;
492 if (!(style & BS_MULTILINE))
493 dtStyle |= DT_SINGLELINE;
494 else
495 dtStyle |= DT_WORDBREAK;
497 switch (style & BS_CENTER)
499 case BS_LEFT: /* DT_LEFT is 0 */ break;
500 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
501 case BS_CENTER: dtStyle |= DT_CENTER; break;
502 default:
503 /* Pushbutton's text is centered by default */
504 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
505 /* all other flavours have left aligned text */
508 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
510 /* DrawText ignores vertical alignment for multiline text,
511 * but we use these flags to align label manually.
513 if (get_button_type(style) != BS_GROUPBOX)
515 switch (style & BS_VCENTER)
517 case BS_TOP: /* DT_TOP is 0 */ break;
518 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
519 case BS_VCENTER: /* fall through */
520 default: dtStyle |= DT_VCENTER; break;
523 else
524 /* GroupBox's text is always single line and is top aligned. */
525 dtStyle |= DT_SINGLELINE;
527 return dtStyle;
530 /**********************************************************************
531 * BUTTON_CalcLabelRect
533 * Calculates label's rectangle depending on button style.
535 * Returns flags to be passed to DrawText.
536 * Calculated rectangle doesn't take into account button state
537 * (pushed, etc.). If there is nothing to draw (no text/image) output
538 * rectangle is empty, and return value is (UINT)-1.
540 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
542 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
543 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
544 WCHAR *text;
545 ICONINFO iconInfo;
546 BITMAP bm;
547 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
548 RECT r = *rc;
549 INT n;
551 /* Calculate label rectangle according to label type */
552 switch (style & (BS_ICON|BS_BITMAP))
554 case BS_TEXT:
556 HFONT hFont, hPrevFont = 0;
558 if (!(text = get_button_text( hwnd ))) goto empty_rect;
559 if (!text[0])
561 HeapFree( GetProcessHeap(), 0, text );
562 goto empty_rect;
565 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hdc, hFont );
566 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
567 if (hPrevFont) SelectObject( hdc, hPrevFont );
568 HeapFree( GetProcessHeap(), 0, text );
569 break;
572 case BS_ICON:
573 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
574 goto empty_rect;
576 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
578 r.right = r.left + bm.bmWidth;
579 r.bottom = r.top + bm.bmHeight;
581 DeleteObject(iconInfo.hbmColor);
582 DeleteObject(iconInfo.hbmMask);
583 break;
585 case BS_BITMAP:
586 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
587 goto empty_rect;
589 r.right = r.left + bm.bmWidth;
590 r.bottom = r.top + bm.bmHeight;
591 break;
593 default:
594 empty_rect:
595 rc->right = r.left;
596 rc->bottom = r.top;
597 return (UINT)-1;
600 /* Position label inside bounding rectangle according to
601 * alignment flags. (calculated rect is always left-top aligned).
602 * If label is aligned to any side - shift label in opposite
603 * direction to leave extra space for focus rectangle.
605 switch (dtStyle & (DT_CENTER|DT_RIGHT))
607 case DT_LEFT: r.left++; r.right++; break;
608 case DT_CENTER: n = r.right - r.left;
609 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
610 r.right = r.left + n; break;
611 case DT_RIGHT: n = r.right - r.left;
612 r.right = rc->right - 1;
613 r.left = r.right - n;
614 break;
617 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
619 case DT_TOP: r.top++; r.bottom++; break;
620 case DT_VCENTER: n = r.bottom - r.top;
621 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
622 r.bottom = r.top + n; break;
623 case DT_BOTTOM: n = r.bottom - r.top;
624 r.bottom = rc->bottom - 1;
625 r.top = r.bottom - n;
626 break;
629 *rc = r;
630 return dtStyle;
634 /**********************************************************************
635 * BUTTON_DrawTextCallback
637 * Callback function used by DrawStateW function.
639 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
641 RECT rc;
643 SetRect(&rc, 0, 0, cx, cy);
644 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
645 return TRUE;
649 /**********************************************************************
650 * BUTTON_DrawLabel
652 * Common function for drawing button label.
654 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
656 DRAWSTATEPROC lpOutputProc = NULL;
657 LPARAM lp;
658 WPARAM wp = 0;
659 HBRUSH hbr = 0;
660 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
661 LONG state = get_button_state( hwnd );
662 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
663 WCHAR *text = NULL;
665 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
666 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
667 * I don't have Win31 on hand to verify that, so I leave it as is.
670 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
672 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
673 flags |= DSS_MONO;
676 switch (style & (BS_ICON|BS_BITMAP))
678 case BS_TEXT:
679 /* DST_COMPLEX -- is 0 */
680 lpOutputProc = BUTTON_DrawTextCallback;
681 if (!(text = get_button_text( hwnd ))) return;
682 lp = (LPARAM)text;
683 wp = dtFlags;
684 break;
686 case BS_ICON:
687 flags |= DST_ICON;
688 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
689 break;
691 case BS_BITMAP:
692 flags |= DST_BITMAP;
693 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
694 break;
696 default:
697 return;
700 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
701 rc->right - rc->left, rc->bottom - rc->top, flags);
702 HeapFree( GetProcessHeap(), 0, text );
705 /**********************************************************************
706 * Push Button Functions
708 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
710 RECT rc, r;
711 UINT dtFlags, uState;
712 HPEN hOldPen;
713 HBRUSH hOldBrush;
714 INT oldBkMode;
715 COLORREF oldTxtColor;
716 HFONT hFont;
717 LONG state = get_button_state( hwnd );
718 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
719 BOOL pushedState = (state & BST_PUSHED);
720 HWND parent;
721 HRGN hrgn;
723 GetClientRect( hwnd, &rc );
725 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
726 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
727 parent = GetParent(hwnd);
728 if (!parent) parent = hwnd;
729 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
731 hrgn = set_control_clipping( hDC, &rc );
733 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
734 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
735 oldBkMode = SetBkMode(hDC, TRANSPARENT);
737 if (get_button_type(style) == BS_DEFPUSHBUTTON)
739 if (action != ODA_FOCUS)
740 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
741 InflateRect( &rc, -1, -1 );
744 /* completely skip the drawing if only focus has changed */
745 if (action == ODA_FOCUS) goto draw_focus;
747 uState = DFCS_BUTTONPUSH;
749 if (style & BS_FLAT)
750 uState |= DFCS_MONO;
751 else if (pushedState)
753 if (get_button_type(style) == BS_DEFPUSHBUTTON )
754 uState |= DFCS_FLAT;
755 else
756 uState |= DFCS_PUSHED;
759 if (state & (BST_CHECKED | BST_INDETERMINATE))
760 uState |= DFCS_CHECKED;
762 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
764 /* draw button label */
765 r = rc;
766 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
768 if (dtFlags == (UINT)-1L)
769 goto cleanup;
771 if (pushedState)
772 OffsetRect(&r, 1, 1);
774 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
776 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
778 SetTextColor( hDC, oldTxtColor );
780 draw_focus:
781 if (action == ODA_FOCUS || (state & BST_FOCUS))
783 InflateRect( &rc, -2, -2 );
784 DrawFocusRect( hDC, &rc );
787 cleanup:
788 SelectObject( hDC, hOldPen );
789 SelectObject( hDC, hOldBrush );
790 SetBkMode(hDC, oldBkMode);
791 SelectClipRgn( hDC, hrgn );
792 if (hrgn) DeleteObject( hrgn );
795 /**********************************************************************
796 * Check Box & Radio Button Functions
799 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
801 RECT rbox, rtext, client;
802 HBRUSH hBrush;
803 int delta, text_offset, checkBoxWidth, checkBoxHeight;
804 UINT dtFlags;
805 HFONT hFont;
806 LONG state = get_button_state( hwnd );
807 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
808 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
809 HWND parent;
810 HRGN hrgn;
812 if (style & BS_PUSHLIKE)
814 PB_Paint( hwnd, hDC, action );
815 return;
818 GetClientRect(hwnd, &client);
819 rbox = rtext = client;
821 checkBoxWidth = 12 * GetDpiForWindow( hwnd ) / 96 + 1;
822 checkBoxHeight = 12 * GetDpiForWindow( hwnd ) / 96 + 1;
824 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
825 GetCharWidthW( hDC, '0', '0', &text_offset );
826 text_offset /= 2;
828 parent = GetParent(hwnd);
829 if (!parent) parent = hwnd;
830 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
831 (WPARAM)hDC, (LPARAM)hwnd);
832 if (!hBrush) /* did the app forget to call defwindowproc ? */
833 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
834 (WPARAM)hDC, (LPARAM)hwnd );
835 hrgn = set_control_clipping( hDC, &client );
837 if (style & BS_LEFTTEXT || ex_style & WS_EX_RIGHT)
839 rtext.right -= checkBoxWidth + text_offset;
840 rbox.left = rbox.right - checkBoxWidth;
842 else
844 rtext.left += checkBoxWidth + text_offset;
845 rbox.right = checkBoxWidth;
848 /* Since WM_ERASEBKGND does nothing, first prepare background */
849 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
850 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
852 /* Draw label */
853 client = rtext;
854 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
856 /* Only adjust rbox when rtext is valid */
857 if (dtFlags != (UINT)-1L)
859 rbox.top = rtext.top;
860 rbox.bottom = rtext.bottom;
863 /* Draw the check-box bitmap */
864 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
866 UINT flags;
868 if ((get_button_type(style) == BS_RADIOBUTTON) ||
869 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
870 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
871 else flags = DFCS_BUTTONCHECK;
873 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
874 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
876 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
878 /* rbox must have the correct height */
879 delta = rbox.bottom - rbox.top - checkBoxHeight;
881 if ((style & BS_VCENTER) == BS_TOP) {
882 if (delta > 0) {
883 rbox.bottom = rbox.top + checkBoxHeight;
884 } else {
885 rbox.top -= -delta/2 + 1;
886 rbox.bottom = rbox.top + checkBoxHeight;
888 } else if ((style & BS_VCENTER) == BS_BOTTOM) {
889 if (delta > 0) {
890 rbox.top = rbox.bottom - checkBoxHeight;
891 } else {
892 rbox.bottom += -delta/2 + 1;
893 rbox.top = rbox.bottom - checkBoxHeight;
895 } else { /* Default */
896 if (delta > 0) {
897 int ofs = (delta / 2);
898 rbox.bottom -= ofs + 1;
899 rbox.top = rbox.bottom - checkBoxHeight;
900 } else if (delta < 0) {
901 int ofs = (-delta / 2);
902 rbox.top -= ofs + 1;
903 rbox.bottom = rbox.top + checkBoxHeight;
907 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
910 if (dtFlags == (UINT)-1L) /* Noting to draw */
911 return;
913 if (action == ODA_DRAWENTIRE)
914 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
916 /* ... and focus */
917 if (action == ODA_FOCUS || (state & BST_FOCUS))
919 rtext.left--;
920 rtext.right++;
921 IntersectRect(&rtext, &rtext, &client);
922 DrawFocusRect( hDC, &rtext );
924 SelectClipRgn( hDC, hrgn );
925 if (hrgn) DeleteObject( hrgn );
929 /**********************************************************************
930 * BUTTON_CheckAutoRadioButton
932 * hwnd is checked, uncheck every other auto radio button in group
934 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
936 HWND parent, sibling, start;
938 parent = GetParent(hwnd);
939 /* make sure that starting control is not disabled or invisible */
940 start = sibling = hwnd;
943 if (!sibling) break;
944 if (SendMessageW( sibling, WM_GETDLGCODE, 0, 0 ) == (DLGC_BUTTON | DLGC_RADIOBUTTON))
945 SendMessageW( sibling, BM_SETCHECK, sibling == hwnd ? BST_CHECKED : BST_UNCHECKED, 0 );
946 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
947 } while (sibling != start);
951 /**********************************************************************
952 * Group Box Functions
955 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
957 RECT rc, rcFrame;
958 HBRUSH hbr;
959 HFONT hFont;
960 UINT dtFlags;
961 TEXTMETRICW tm;
962 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
963 HWND parent;
964 HRGN hrgn;
966 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
967 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
968 parent = GetParent(hwnd);
969 if (!parent) parent = hwnd;
970 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
971 if (!hbr) /* did the app forget to call defwindowproc ? */
972 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
973 (WPARAM)hDC, (LPARAM)hwnd);
974 GetClientRect( hwnd, &rc);
975 rcFrame = rc;
976 hrgn = set_control_clipping( hDC, &rc );
978 GetTextMetricsW (hDC, &tm);
979 rcFrame.top += (tm.tmHeight / 2) - 1;
980 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
982 InflateRect(&rc, -7, 1);
983 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
985 if (dtFlags != (UINT)-1)
987 /* Because buttons have CS_PARENTDC class style, there is a chance
988 * that label will be drawn out of client rect.
989 * But Windows doesn't clip label's rect, so do I.
992 /* There is 1-pixel margin at the left, right, and bottom */
993 rc.left--; rc.right++; rc.bottom++;
994 FillRect(hDC, &rc, hbr);
995 rc.left++; rc.right--; rc.bottom--;
997 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
999 SelectClipRgn( hDC, hrgn );
1000 if (hrgn) DeleteObject( hrgn );
1004 /**********************************************************************
1005 * User Button Functions
1008 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1010 RECT rc;
1011 HBRUSH hBrush;
1012 HFONT hFont;
1013 LONG state = get_button_state( hwnd );
1014 HWND parent;
1016 GetClientRect( hwnd, &rc);
1018 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1020 parent = GetParent(hwnd);
1021 if (!parent) parent = hwnd;
1022 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1023 if (!hBrush) /* did the app forget to call defwindowproc ? */
1024 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1025 (WPARAM)hDC, (LPARAM)hwnd);
1027 FillRect( hDC, &rc, hBrush );
1028 if (action == ODA_FOCUS || (state & BST_FOCUS))
1029 DrawFocusRect( hDC, &rc );
1031 switch (action)
1033 case ODA_FOCUS:
1034 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1035 break;
1037 case ODA_SELECT:
1038 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1039 break;
1041 default:
1042 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1043 break;
1048 /**********************************************************************
1049 * Ownerdrawn Button Functions
1052 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1054 LONG state = get_button_state( hwnd );
1055 DRAWITEMSTRUCT dis;
1056 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1057 HWND parent;
1058 HFONT hFont;
1059 HRGN hrgn;
1061 dis.CtlType = ODT_BUTTON;
1062 dis.CtlID = id;
1063 dis.itemID = 0;
1064 dis.itemAction = action;
1065 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1066 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1067 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1068 dis.hwndItem = hwnd;
1069 dis.hDC = hDC;
1070 dis.itemData = 0;
1071 GetClientRect( hwnd, &dis.rcItem );
1073 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1074 parent = GetParent(hwnd);
1075 if (!parent) parent = hwnd;
1076 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1078 hrgn = set_control_clipping( hDC, &dis.rcItem );
1080 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1081 SelectClipRgn( hDC, hrgn );
1082 if (hrgn) DeleteObject( hrgn );