d2d1: Create feature level 10.0 device context state objects.
[wine.git] / dlls / user32 / button.c
blobe3af68e0e543745de9efd403757df166fc1ea0a7
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 #include <stdarg.h>
42 #include <string.h>
43 #include <stdlib.h>
45 #define OEMRESOURCE
47 #include "windef.h"
48 #include "winbase.h"
49 #include "wingdi.h"
50 #include "controls.h"
51 #include "win.h"
52 #include "user_private.h"
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(button);
57 /* GetWindowLong offsets for window extra information */
58 #define STATE_GWL_OFFSET 0
59 #define HFONT_GWL_OFFSET (sizeof(LONG))
60 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
61 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
63 /* undocumented flags */
64 #define BUTTON_NSTATES 0x0F
65 #define BUTTON_BTNPRESSED 0x40
66 #define BUTTON_UNKNOWN2 0x20
67 #define BUTTON_UNKNOWN3 0x10
69 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
70 do { /* Notify parent which has created this button control */ \
71 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
72 SendMessageW(GetParent(hWnd), WM_COMMAND, \
73 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
74 (LPARAM)(hWnd)); \
75 } while(0)
77 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
78 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
79 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
80 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
81 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
82 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
83 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
85 #define MAX_BTN_TYPE 16
87 static const WORD maxCheckState[MAX_BTN_TYPE] =
89 BST_UNCHECKED, /* BS_PUSHBUTTON */
90 BST_UNCHECKED, /* BS_DEFPUSHBUTTON */
91 BST_CHECKED, /* BS_CHECKBOX */
92 BST_CHECKED, /* BS_AUTOCHECKBOX */
93 BST_CHECKED, /* BS_RADIOBUTTON */
94 BST_INDETERMINATE, /* BS_3STATE */
95 BST_INDETERMINATE, /* BS_AUTO3STATE */
96 BST_UNCHECKED, /* BS_GROUPBOX */
97 BST_UNCHECKED, /* BS_USERBUTTON */
98 BST_CHECKED, /* BS_AUTORADIOBUTTON */
99 BST_UNCHECKED, /* BS_PUSHBOX */
100 BST_UNCHECKED /* BS_OWNERDRAW */
103 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
105 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
107 PB_Paint, /* BS_PUSHBUTTON */
108 PB_Paint, /* BS_DEFPUSHBUTTON */
109 CB_Paint, /* BS_CHECKBOX */
110 CB_Paint, /* BS_AUTOCHECKBOX */
111 CB_Paint, /* BS_RADIOBUTTON */
112 CB_Paint, /* BS_3STATE */
113 CB_Paint, /* BS_AUTO3STATE */
114 GB_Paint, /* BS_GROUPBOX */
115 UB_Paint, /* BS_USERBUTTON */
116 CB_Paint, /* BS_AUTORADIOBUTTON */
117 NULL, /* BS_PUSHBOX */
118 OB_Paint /* BS_OWNERDRAW */
121 /*********************************************************************
122 * button class descriptor
124 const struct builtin_class_descr BUTTON_builtin_class =
126 L"Button", /* name */
127 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
128 WINPROC_BUTTON, /* proc */
129 NB_EXTRA_BYTES, /* extra */
130 IDC_ARROW, /* cursor */
131 0 /* brush */
135 static inline LONG get_button_state( HWND hwnd )
137 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
140 static inline void set_button_state( HWND hwnd, LONG state )
142 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
145 static inline HFONT get_button_font( HWND hwnd )
147 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
150 static inline void set_button_font( HWND hwnd, HFONT font )
152 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
155 static inline UINT get_button_type( LONG window_style )
157 return (window_style & BS_TYPEMASK);
160 /* paint a button of any type */
161 static inline void paint_button( HWND hwnd, LONG style, UINT action )
163 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
165 HDC hdc = GetDC( hwnd );
166 btnPaintFunc[style]( hwnd, hdc, action );
167 ReleaseDC( hwnd, hdc );
171 /* retrieve the button text; returned buffer must be freed by caller */
172 static inline WCHAR *get_button_text( HWND hwnd )
174 static const INT len = 512;
175 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
176 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
177 return buffer;
180 /***********************************************************************
181 * ButtonWndProc_common
183 LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL unicode )
185 RECT rect;
186 POINT pt;
187 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
188 UINT btn_type = get_button_type( style );
189 LONG state;
190 HANDLE oldHbitmap;
192 if (!IsWindow( hWnd )) return 0;
194 pt.x = (short)LOWORD(lParam);
195 pt.y = (short)HIWORD(lParam);
197 switch (uMsg)
199 case WM_GETDLGCODE:
200 switch(btn_type)
202 case BS_USERBUTTON:
203 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
204 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
205 case BS_RADIOBUTTON:
206 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
207 case BS_GROUPBOX: return DLGC_STATIC;
208 default: return DLGC_BUTTON;
211 case WM_ENABLE:
212 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
213 break;
215 case WM_CREATE:
216 if (btn_type >= MAX_BTN_TYPE)
217 return -1; /* abort */
219 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
220 if (btn_type == BS_USERBUTTON )
222 style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
223 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
225 set_button_state( hWnd, BST_UNCHECKED );
226 return 0;
228 case WM_ERASEBKGND:
229 if (btn_type == BS_OWNERDRAW)
231 HDC hdc = (HDC)wParam;
232 RECT rc;
233 HBRUSH hBrush;
234 HWND parent = GetParent(hWnd);
235 if (!parent) parent = hWnd;
236 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
237 if (!hBrush) /* did the app forget to call defwindowproc ? */
238 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
239 (WPARAM)hdc, (LPARAM)hWnd);
240 GetClientRect(hWnd, &rc);
241 FillRect(hdc, &rc, hBrush);
243 return 1;
245 case WM_PRINTCLIENT:
246 case WM_PAINT:
248 PAINTSTRUCT ps;
249 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
250 if (btnPaintFunc[btn_type])
252 int nOldMode = SetBkMode( hdc, OPAQUE );
253 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
254 SetBkMode(hdc, nOldMode); /* reset painting mode */
256 if ( !wParam ) EndPaint( hWnd, &ps );
257 break;
260 case WM_KEYDOWN:
261 if (wParam == VK_SPACE)
263 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
264 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
265 SetCapture( hWnd );
267 break;
269 case WM_LBUTTONDBLCLK:
270 if(style & BS_NOTIFY ||
271 btn_type == BS_RADIOBUTTON ||
272 btn_type == BS_USERBUTTON ||
273 btn_type == BS_OWNERDRAW)
275 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
276 break;
278 /* fall through */
279 case WM_LBUTTONDOWN:
280 SetCapture( hWnd );
281 SetFocus( hWnd );
282 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
283 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
284 break;
286 case WM_KEYUP:
287 if (wParam != VK_SPACE)
288 break;
289 /* fall through */
290 case WM_LBUTTONUP:
291 state = get_button_state( hWnd );
292 if (!(state & BUTTON_BTNPRESSED)) break;
293 state &= BUTTON_NSTATES;
294 set_button_state( hWnd, state );
295 if (!(state & BST_PUSHED))
297 ReleaseCapture();
298 break;
300 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
301 GetClientRect( hWnd, &rect );
302 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
304 state = get_button_state( hWnd );
305 switch(btn_type)
307 case BS_AUTOCHECKBOX:
308 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
309 break;
310 case BS_AUTORADIOBUTTON:
311 BUTTON_CheckAutoRadioButton( hWnd );
312 break;
313 case BS_AUTO3STATE:
314 SendMessageW( hWnd, BM_SETCHECK,
315 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
316 break;
318 ReleaseCapture();
319 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
321 else
323 ReleaseCapture();
325 break;
327 case WM_CAPTURECHANGED:
328 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
329 if (hWnd == (HWND)lParam) break;
330 state = get_button_state( hWnd );
331 if (state & BUTTON_BTNPRESSED)
333 state &= BUTTON_NSTATES;
334 set_button_state( hWnd, state );
335 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
337 break;
339 case WM_MOUSEMOVE:
340 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
342 GetClientRect( hWnd, &rect );
343 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
345 break;
347 case WM_SETTEXT:
349 /* Clear an old text here as Windows does */
350 if (IsWindowVisible(hWnd))
352 HDC hdc = GetDC(hWnd);
353 HBRUSH hbrush;
354 RECT client, rc;
355 HWND parent = GetParent(hWnd);
356 UINT message = (btn_type == BS_PUSHBUTTON ||
357 btn_type == BS_DEFPUSHBUTTON ||
358 btn_type == BS_USERBUTTON ||
359 btn_type == BS_OWNERDRAW) ?
360 WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
362 if (!parent) parent = hWnd;
363 hbrush = (HBRUSH)SendMessageW(parent, message,
364 (WPARAM)hdc, (LPARAM)hWnd);
365 if (!hbrush) /* did the app forget to call DefWindowProc ? */
366 hbrush = (HBRUSH)DefWindowProcW(parent, message,
367 (WPARAM)hdc, (LPARAM)hWnd);
369 GetClientRect(hWnd, &client);
370 rc = client;
371 /* FIXME: check other BS_* handlers */
372 if (btn_type == BS_GROUPBOX)
373 InflateRect(&rc, -7, 1); /* GB_Paint does this */
374 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
375 /* Clip by client rect bounds */
376 if (rc.right > client.right) rc.right = client.right;
377 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
378 FillRect(hdc, &rc, hbrush);
379 ReleaseDC(hWnd, hdc);
382 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
383 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
384 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
385 InvalidateRect( hWnd, NULL, TRUE );
386 else
387 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
388 return 1; /* success. FIXME: check text length */
391 case WM_SETFONT:
392 set_button_font( hWnd, (HFONT)wParam );
393 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
394 break;
396 case WM_GETFONT:
397 return (LRESULT)get_button_font( hWnd );
399 case WM_SETFOCUS:
400 TRACE("WM_SETFOCUS %p\n",hWnd);
401 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
402 paint_button( hWnd, btn_type, ODA_FOCUS );
403 if (style & BS_NOTIFY)
404 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
405 break;
407 case WM_KILLFOCUS:
408 TRACE("WM_KILLFOCUS %p\n",hWnd);
409 state = get_button_state( hWnd );
410 set_button_state( hWnd, state & ~BST_FOCUS );
411 paint_button( hWnd, btn_type, ODA_FOCUS );
413 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
414 ReleaseCapture();
415 if (style & BS_NOTIFY)
416 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
418 InvalidateRect( hWnd, NULL, FALSE );
419 break;
421 case WM_SYSCOLORCHANGE:
422 InvalidateRect( hWnd, NULL, FALSE );
423 break;
425 case BM_SETSTYLE:
426 btn_type = wParam & BS_TYPEMASK;
427 style = (style & ~BS_TYPEMASK) | btn_type;
428 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
430 /* Only redraw if lParam flag is set.*/
431 if (lParam)
432 InvalidateRect( hWnd, NULL, TRUE );
434 break;
436 case BM_CLICK:
437 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
438 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
439 break;
441 case BM_SETIMAGE:
442 /* Check that image format matches button style */
443 switch (style & (BS_BITMAP|BS_ICON))
445 case BS_BITMAP:
446 if (wParam != IMAGE_BITMAP) return 0;
447 break;
448 case BS_ICON:
449 if (wParam != IMAGE_ICON) return 0;
450 break;
451 default:
452 return 0;
454 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
455 InvalidateRect( hWnd, NULL, FALSE );
456 return (LRESULT)oldHbitmap;
458 case BM_GETIMAGE:
459 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
461 case BM_GETCHECK:
462 return get_button_state( hWnd ) & 3;
464 case BM_SETCHECK:
465 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
466 state = get_button_state( hWnd );
467 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
469 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
470 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
472 if ((state & 3) != wParam)
474 set_button_state( hWnd, (state & ~3) | wParam );
475 paint_button( hWnd, btn_type, ODA_SELECT );
477 break;
479 case BM_GETSTATE:
480 return get_button_state( hWnd );
482 case BM_SETSTATE:
483 state = get_button_state( hWnd );
484 if (wParam)
485 set_button_state( hWnd, state | BST_PUSHED );
486 else
487 set_button_state( hWnd, state & ~BST_PUSHED );
489 paint_button( hWnd, btn_type, ODA_SELECT );
490 break;
492 case WM_NCHITTEST:
493 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
494 /* fall through */
495 default:
496 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
497 DefWindowProcA(hWnd, uMsg, wParam, lParam);
499 return 0;
502 /**********************************************************************
503 * Convert button styles to flags used by DrawText.
505 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
507 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
509 /* "Convert" pushlike buttons to pushbuttons */
510 if (style & BS_PUSHLIKE)
511 style &= ~BS_TYPEMASK;
513 if (!(style & BS_MULTILINE))
514 dtStyle |= DT_SINGLELINE;
515 else
516 dtStyle |= DT_WORDBREAK;
518 switch (style & BS_CENTER)
520 case BS_LEFT: /* DT_LEFT is 0 */ break;
521 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
522 case BS_CENTER: dtStyle |= DT_CENTER; break;
523 default:
524 /* Pushbutton's text is centered by default */
525 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
526 /* all other flavours have left aligned text */
529 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
531 /* DrawText ignores vertical alignment for multiline text,
532 * but we use these flags to align label manually.
534 if (get_button_type(style) != BS_GROUPBOX)
536 switch (style & BS_VCENTER)
538 case BS_TOP: /* DT_TOP is 0 */ break;
539 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
540 case BS_VCENTER: /* fall through */
541 default: dtStyle |= DT_VCENTER; break;
544 else
545 /* GroupBox's text is always single line and is top aligned. */
546 dtStyle |= DT_SINGLELINE;
548 return dtStyle;
551 /**********************************************************************
552 * BUTTON_CalcLabelRect
554 * Calculates label's rectangle depending on button style.
556 * Returns flags to be passed to DrawText.
557 * Calculated rectangle doesn't take into account button state
558 * (pushed, etc.). If there is nothing to draw (no text/image) output
559 * rectangle is empty, and return value is (UINT)-1.
561 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
563 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
564 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
565 WCHAR *text;
566 ICONINFO iconInfo;
567 BITMAP bm;
568 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
569 RECT r = *rc;
570 INT n;
572 /* Calculate label rectangle according to label type */
573 switch (style & (BS_ICON|BS_BITMAP))
575 case BS_TEXT:
577 HFONT hFont, hPrevFont = 0;
579 if (!(text = get_button_text( hwnd ))) goto empty_rect;
580 if (!text[0])
582 HeapFree( GetProcessHeap(), 0, text );
583 goto empty_rect;
586 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hdc, hFont );
587 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
588 if (hPrevFont) SelectObject( hdc, hPrevFont );
589 HeapFree( GetProcessHeap(), 0, text );
590 break;
593 case BS_ICON:
594 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
595 goto empty_rect;
597 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
599 r.right = r.left + bm.bmWidth;
600 r.bottom = r.top + bm.bmHeight;
602 DeleteObject(iconInfo.hbmColor);
603 DeleteObject(iconInfo.hbmMask);
604 break;
606 case BS_BITMAP:
607 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
608 goto empty_rect;
610 r.right = r.left + bm.bmWidth;
611 r.bottom = r.top + bm.bmHeight;
612 break;
614 default:
615 empty_rect:
616 rc->right = r.left;
617 rc->bottom = r.top;
618 return (UINT)-1;
621 /* Position label inside bounding rectangle according to
622 * alignment flags. (calculated rect is always left-top aligned).
623 * If label is aligned to any side - shift label in opposite
624 * direction to leave extra space for focus rectangle.
626 switch (dtStyle & (DT_CENTER|DT_RIGHT))
628 case DT_LEFT: r.left++; r.right++; break;
629 case DT_CENTER: n = r.right - r.left;
630 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
631 r.right = r.left + n; break;
632 case DT_RIGHT: n = r.right - r.left;
633 r.right = rc->right - 1;
634 r.left = r.right - n;
635 break;
638 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
640 case DT_TOP: r.top++; r.bottom++; break;
641 case DT_VCENTER: n = r.bottom - r.top;
642 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
643 r.bottom = r.top + n; break;
644 case DT_BOTTOM: n = r.bottom - r.top;
645 r.bottom = rc->bottom - 1;
646 r.top = r.bottom - n;
647 break;
650 *rc = r;
651 return dtStyle;
655 /**********************************************************************
656 * BUTTON_DrawTextCallback
658 * Callback function used by DrawStateW function.
660 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
662 RECT rc;
664 SetRect(&rc, 0, 0, cx, cy);
665 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
666 return TRUE;
670 /**********************************************************************
671 * BUTTON_DrawLabel
673 * Common function for drawing button label.
675 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
677 DRAWSTATEPROC lpOutputProc = NULL;
678 LPARAM lp;
679 WPARAM wp = 0;
680 HBRUSH hbr = 0;
681 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
682 LONG state = get_button_state( hwnd );
683 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
684 WCHAR *text = NULL;
686 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
687 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
688 * I don't have Win31 on hand to verify that, so I leave it as is.
691 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
693 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
694 flags |= DSS_MONO;
697 switch (style & (BS_ICON|BS_BITMAP))
699 case BS_TEXT:
700 /* DST_COMPLEX -- is 0 */
701 lpOutputProc = BUTTON_DrawTextCallback;
702 if (!(text = get_button_text( hwnd ))) return;
703 lp = (LPARAM)text;
704 wp = dtFlags;
705 break;
707 case BS_ICON:
708 flags |= DST_ICON;
709 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
710 break;
712 case BS_BITMAP:
713 flags |= DST_BITMAP;
714 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
715 break;
717 default:
718 return;
721 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
722 rc->right - rc->left, rc->bottom - rc->top, flags);
723 HeapFree( GetProcessHeap(), 0, text );
726 /**********************************************************************
727 * Push Button Functions
729 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
731 RECT rc, r;
732 UINT dtFlags, uState;
733 HPEN hOldPen;
734 HBRUSH hOldBrush;
735 INT oldBkMode;
736 COLORREF oldTxtColor;
737 HFONT hFont;
738 LONG state = get_button_state( hwnd );
739 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
740 BOOL pushedState = (state & BST_PUSHED);
741 HWND parent;
742 HRGN hrgn;
744 GetClientRect( hwnd, &rc );
746 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
747 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
748 parent = GetParent(hwnd);
749 if (!parent) parent = hwnd;
750 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
752 hrgn = set_control_clipping( hDC, &rc );
754 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
755 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
756 oldBkMode = SetBkMode(hDC, TRANSPARENT);
758 if (get_button_type(style) == BS_DEFPUSHBUTTON)
760 if (action != ODA_FOCUS)
761 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
762 InflateRect( &rc, -1, -1 );
765 /* completely skip the drawing if only focus has changed */
766 if (action == ODA_FOCUS) goto draw_focus;
768 uState = DFCS_BUTTONPUSH;
770 if (style & BS_FLAT)
771 uState |= DFCS_MONO;
772 else if (pushedState)
774 if (get_button_type(style) == BS_DEFPUSHBUTTON )
775 uState |= DFCS_FLAT;
776 else
777 uState |= DFCS_PUSHED;
780 if (state & (BST_CHECKED | BST_INDETERMINATE))
781 uState |= DFCS_CHECKED;
783 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
785 /* draw button label */
786 r = rc;
787 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
789 if (dtFlags == (UINT)-1L)
790 goto cleanup;
792 if (pushedState)
793 OffsetRect(&r, 1, 1);
795 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
797 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
799 SetTextColor( hDC, oldTxtColor );
801 draw_focus:
802 if (action == ODA_FOCUS || (state & BST_FOCUS))
804 InflateRect( &rc, -2, -2 );
805 DrawFocusRect( hDC, &rc );
808 cleanup:
809 SelectObject( hDC, hOldPen );
810 SelectObject( hDC, hOldBrush );
811 SetBkMode(hDC, oldBkMode);
812 SelectClipRgn( hDC, hrgn );
813 if (hrgn) DeleteObject( hrgn );
816 /**********************************************************************
817 * Check Box & Radio Button Functions
820 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
822 RECT rbox, rtext, client;
823 HBRUSH hBrush;
824 int delta, text_offset, checkBoxWidth, checkBoxHeight;
825 UINT dtFlags;
826 HFONT hFont;
827 LONG state = get_button_state( hwnd );
828 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
829 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
830 HWND parent;
831 HRGN hrgn;
833 if (style & BS_PUSHLIKE)
835 PB_Paint( hwnd, hDC, action );
836 return;
839 GetClientRect(hwnd, &client);
840 rbox = rtext = client;
842 checkBoxWidth = 12 * GetDpiForWindow( hwnd ) / 96 + 1;
843 checkBoxHeight = 12 * GetDpiForWindow( hwnd ) / 96 + 1;
845 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
846 GetCharWidthW( hDC, '0', '0', &text_offset );
847 text_offset /= 2;
849 parent = GetParent(hwnd);
850 if (!parent) parent = hwnd;
851 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
852 (WPARAM)hDC, (LPARAM)hwnd);
853 if (!hBrush) /* did the app forget to call defwindowproc ? */
854 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
855 (WPARAM)hDC, (LPARAM)hwnd );
856 hrgn = set_control_clipping( hDC, &client );
858 if (style & BS_LEFTTEXT || ex_style & WS_EX_RIGHT)
860 rtext.right -= checkBoxWidth + text_offset;
861 rbox.left = rbox.right - checkBoxWidth;
863 else
865 rtext.left += checkBoxWidth + text_offset;
866 rbox.right = checkBoxWidth;
869 /* Since WM_ERASEBKGND does nothing, first prepare background */
870 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
871 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
873 /* Draw label */
874 client = rtext;
875 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
877 /* Only adjust rbox when rtext is valid */
878 if (dtFlags != (UINT)-1L)
880 rbox.top = rtext.top;
881 rbox.bottom = rtext.bottom;
884 /* Draw the check-box bitmap */
885 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
887 UINT flags;
889 if ((get_button_type(style) == BS_RADIOBUTTON) ||
890 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
891 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
892 else flags = DFCS_BUTTONCHECK;
894 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
895 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
897 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
899 /* rbox must have the correct height */
900 delta = rbox.bottom - rbox.top - checkBoxHeight;
902 if ((style & BS_VCENTER) == BS_TOP) {
903 if (delta > 0) {
904 rbox.bottom = rbox.top + checkBoxHeight;
905 } else {
906 rbox.top -= -delta/2 + 1;
907 rbox.bottom = rbox.top + checkBoxHeight;
909 } else if ((style & BS_VCENTER) == BS_BOTTOM) {
910 if (delta > 0) {
911 rbox.top = rbox.bottom - checkBoxHeight;
912 } else {
913 rbox.bottom += -delta/2 + 1;
914 rbox.top = rbox.bottom - checkBoxHeight;
916 } else { /* Default */
917 if (delta > 0) {
918 int ofs = (delta / 2);
919 rbox.bottom -= ofs + 1;
920 rbox.top = rbox.bottom - checkBoxHeight;
921 } else if (delta < 0) {
922 int ofs = (-delta / 2);
923 rbox.top -= ofs + 1;
924 rbox.bottom = rbox.top + checkBoxHeight;
928 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
931 if (dtFlags == (UINT)-1L) /* Noting to draw */
932 return;
934 if (action == ODA_DRAWENTIRE)
935 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
937 /* ... and focus */
938 if (action == ODA_FOCUS || (state & BST_FOCUS))
940 rtext.left--;
941 rtext.right++;
942 IntersectRect(&rtext, &rtext, &client);
943 DrawFocusRect( hDC, &rtext );
945 SelectClipRgn( hDC, hrgn );
946 if (hrgn) DeleteObject( hrgn );
950 /**********************************************************************
951 * BUTTON_CheckAutoRadioButton
953 * hwnd is checked, uncheck every other auto radio button in group
955 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
957 HWND parent, sibling, start;
959 parent = GetParent(hwnd);
960 /* make sure that starting control is not disabled or invisible */
961 start = sibling = hwnd;
964 if (!sibling) break;
965 if (SendMessageW( sibling, WM_GETDLGCODE, 0, 0 ) == (DLGC_BUTTON | DLGC_RADIOBUTTON))
966 SendMessageW( sibling, BM_SETCHECK, sibling == hwnd ? BST_CHECKED : BST_UNCHECKED, 0 );
967 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
968 } while (sibling != start);
972 /**********************************************************************
973 * Group Box Functions
976 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
978 RECT rc, rcFrame;
979 HBRUSH hbr;
980 HFONT hFont;
981 UINT dtFlags;
982 TEXTMETRICW tm;
983 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
984 HWND parent;
985 HRGN hrgn;
987 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
988 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
989 parent = GetParent(hwnd);
990 if (!parent) parent = hwnd;
991 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
992 if (!hbr) /* did the app forget to call defwindowproc ? */
993 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
994 (WPARAM)hDC, (LPARAM)hwnd);
995 GetClientRect( hwnd, &rc);
996 rcFrame = rc;
997 hrgn = set_control_clipping( hDC, &rc );
999 GetTextMetricsW (hDC, &tm);
1000 rcFrame.top += (tm.tmHeight / 2) - 1;
1001 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1003 InflateRect(&rc, -7, 1);
1004 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1006 if (dtFlags != (UINT)-1)
1008 /* Because buttons have CS_PARENTDC class style, there is a chance
1009 * that label will be drawn out of client rect.
1010 * But Windows doesn't clip label's rect, so do I.
1013 /* There is 1-pixel margin at the left, right, and bottom */
1014 rc.left--; rc.right++; rc.bottom++;
1015 FillRect(hDC, &rc, hbr);
1016 rc.left++; rc.right--; rc.bottom--;
1018 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1020 SelectClipRgn( hDC, hrgn );
1021 if (hrgn) DeleteObject( hrgn );
1025 /**********************************************************************
1026 * User Button Functions
1029 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1031 RECT rc;
1032 HBRUSH hBrush;
1033 HFONT hFont;
1034 LONG state = get_button_state( hwnd );
1035 HWND parent;
1037 GetClientRect( hwnd, &rc);
1039 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1041 parent = GetParent(hwnd);
1042 if (!parent) parent = hwnd;
1043 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1044 if (!hBrush) /* did the app forget to call defwindowproc ? */
1045 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1046 (WPARAM)hDC, (LPARAM)hwnd);
1048 FillRect( hDC, &rc, hBrush );
1049 if (action == ODA_FOCUS || (state & BST_FOCUS))
1050 DrawFocusRect( hDC, &rc );
1052 switch (action)
1054 case ODA_FOCUS:
1055 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1056 break;
1058 case ODA_SELECT:
1059 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1060 break;
1062 default:
1063 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1064 break;
1069 /**********************************************************************
1070 * Ownerdrawn Button Functions
1073 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1075 LONG state = get_button_state( hwnd );
1076 DRAWITEMSTRUCT dis;
1077 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1078 HWND parent;
1079 HFONT hFont;
1080 HRGN hrgn;
1082 dis.CtlType = ODT_BUTTON;
1083 dis.CtlID = id;
1084 dis.itemID = 0;
1085 dis.itemAction = action;
1086 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1087 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1088 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1089 dis.hwndItem = hwnd;
1090 dis.hDC = hDC;
1091 dis.itemData = 0;
1092 GetClientRect( hwnd, &dis.rcItem );
1094 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1095 parent = GetParent(hwnd);
1096 if (!parent) parent = hwnd;
1097 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1099 hrgn = set_control_clipping( hDC, &dis.rcItem );
1101 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1102 SelectClipRgn( hDC, hrgn );
1103 if (hrgn) DeleteObject( hrgn );