user32: Don't wait for other threads to process WM_NCDESTROY.
[wine.git] / dlls / user32 / button.c
blob7684e7f2fa690c1d049e1a14fcc555f5f048198d
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 static const WCHAR buttonW[] = {'B','u','t','t','o','n',0};
125 const struct builtin_class_descr BUTTON_builtin_class =
127 buttonW, /* name */
128 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
129 WINPROC_BUTTON, /* proc */
130 NB_EXTRA_BYTES, /* extra */
131 IDC_ARROW, /* cursor */
132 0 /* brush */
136 static inline LONG get_button_state( HWND hwnd )
138 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
141 static inline void set_button_state( HWND hwnd, LONG state )
143 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
146 static inline HFONT get_button_font( HWND hwnd )
148 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
151 static inline void set_button_font( HWND hwnd, HFONT font )
153 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
156 static inline UINT get_button_type( LONG window_style )
158 return (window_style & BS_TYPEMASK);
161 /* paint a button of any type */
162 static inline void paint_button( HWND hwnd, LONG style, UINT action )
164 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
166 HDC hdc = GetDC( hwnd );
167 btnPaintFunc[style]( hwnd, hdc, action );
168 ReleaseDC( hwnd, hdc );
172 /* retrieve the button text; returned buffer must be freed by caller */
173 static inline WCHAR *get_button_text( HWND hwnd )
175 static const INT len = 512;
176 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
177 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
178 return buffer;
181 /***********************************************************************
182 * ButtonWndProc_common
184 LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL unicode )
186 RECT rect;
187 POINT pt;
188 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
189 UINT btn_type = get_button_type( style );
190 LONG state;
191 HANDLE oldHbitmap;
193 if (!IsWindow( hWnd )) return 0;
195 pt.x = (short)LOWORD(lParam);
196 pt.y = (short)HIWORD(lParam);
198 switch (uMsg)
200 case WM_GETDLGCODE:
201 switch(btn_type)
203 case BS_USERBUTTON:
204 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
205 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
206 case BS_RADIOBUTTON:
207 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
208 case BS_GROUPBOX: return DLGC_STATIC;
209 default: return DLGC_BUTTON;
212 case WM_ENABLE:
213 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
214 break;
216 case WM_CREATE:
217 if (btn_type >= MAX_BTN_TYPE)
218 return -1; /* abort */
220 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
221 if (btn_type == BS_USERBUTTON )
223 style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
224 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
226 set_button_state( hWnd, BST_UNCHECKED );
227 return 0;
229 case WM_ERASEBKGND:
230 if (btn_type == BS_OWNERDRAW)
232 HDC hdc = (HDC)wParam;
233 RECT rc;
234 HBRUSH hBrush;
235 HWND parent = GetParent(hWnd);
236 if (!parent) parent = hWnd;
237 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
238 if (!hBrush) /* did the app forget to call defwindowproc ? */
239 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
240 (WPARAM)hdc, (LPARAM)hWnd);
241 GetClientRect(hWnd, &rc);
242 FillRect(hdc, &rc, hBrush);
244 return 1;
246 case WM_PRINTCLIENT:
247 case WM_PAINT:
249 PAINTSTRUCT ps;
250 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
251 if (btnPaintFunc[btn_type])
253 int nOldMode = SetBkMode( hdc, OPAQUE );
254 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
255 SetBkMode(hdc, nOldMode); /* reset painting mode */
257 if ( !wParam ) EndPaint( hWnd, &ps );
258 break;
261 case WM_KEYDOWN:
262 if (wParam == VK_SPACE)
264 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
265 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
266 SetCapture( hWnd );
268 break;
270 case WM_LBUTTONDBLCLK:
271 if(style & BS_NOTIFY ||
272 btn_type == BS_RADIOBUTTON ||
273 btn_type == BS_USERBUTTON ||
274 btn_type == BS_OWNERDRAW)
276 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
277 break;
279 /* fall through */
280 case WM_LBUTTONDOWN:
281 SetCapture( hWnd );
282 SetFocus( hWnd );
283 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
284 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
285 break;
287 case WM_KEYUP:
288 if (wParam != VK_SPACE)
289 break;
290 /* fall through */
291 case WM_LBUTTONUP:
292 state = get_button_state( hWnd );
293 if (!(state & BUTTON_BTNPRESSED)) break;
294 state &= BUTTON_NSTATES;
295 set_button_state( hWnd, state );
296 if (!(state & BST_PUSHED))
298 ReleaseCapture();
299 break;
301 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
302 GetClientRect( hWnd, &rect );
303 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
305 state = get_button_state( hWnd );
306 switch(btn_type)
308 case BS_AUTOCHECKBOX:
309 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
310 break;
311 case BS_AUTORADIOBUTTON:
312 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
313 break;
314 case BS_AUTO3STATE:
315 SendMessageW( hWnd, BM_SETCHECK,
316 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
317 break;
319 ReleaseCapture();
320 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
322 else
324 ReleaseCapture();
326 break;
328 case WM_CAPTURECHANGED:
329 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
330 if (hWnd == (HWND)lParam) break;
331 state = get_button_state( hWnd );
332 if (state & BUTTON_BTNPRESSED)
334 state &= BUTTON_NSTATES;
335 set_button_state( hWnd, state );
336 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
338 break;
340 case WM_MOUSEMOVE:
341 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
343 GetClientRect( hWnd, &rect );
344 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
346 break;
348 case WM_SETTEXT:
350 /* Clear an old text here as Windows does */
351 if (IsWindowVisible(hWnd))
353 HDC hdc = GetDC(hWnd);
354 HBRUSH hbrush;
355 RECT client, rc;
356 HWND parent = GetParent(hWnd);
357 UINT message = (btn_type == BS_PUSHBUTTON ||
358 btn_type == BS_DEFPUSHBUTTON ||
359 btn_type == BS_USERBUTTON ||
360 btn_type == BS_OWNERDRAW) ?
361 WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
363 if (!parent) parent = hWnd;
364 hbrush = (HBRUSH)SendMessageW(parent, message,
365 (WPARAM)hdc, (LPARAM)hWnd);
366 if (!hbrush) /* did the app forget to call DefWindowProc ? */
367 hbrush = (HBRUSH)DefWindowProcW(parent, message,
368 (WPARAM)hdc, (LPARAM)hWnd);
370 GetClientRect(hWnd, &client);
371 rc = client;
372 /* FIXME: check other BS_* handlers */
373 if (btn_type == BS_GROUPBOX)
374 InflateRect(&rc, -7, 1); /* GB_Paint does this */
375 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
376 /* Clip by client rect bounds */
377 if (rc.right > client.right) rc.right = client.right;
378 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
379 FillRect(hdc, &rc, hbrush);
380 ReleaseDC(hWnd, hdc);
383 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
384 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
385 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
386 InvalidateRect( hWnd, NULL, TRUE );
387 else
388 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
389 return 1; /* success. FIXME: check text length */
392 case WM_SETFONT:
393 set_button_font( hWnd, (HFONT)wParam );
394 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
395 break;
397 case WM_GETFONT:
398 return (LRESULT)get_button_font( hWnd );
400 case WM_SETFOCUS:
401 TRACE("WM_SETFOCUS %p\n",hWnd);
402 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
403 paint_button( hWnd, btn_type, ODA_FOCUS );
404 if (style & BS_NOTIFY)
405 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
406 break;
408 case WM_KILLFOCUS:
409 TRACE("WM_KILLFOCUS %p\n",hWnd);
410 state = get_button_state( hWnd );
411 set_button_state( hWnd, state & ~BST_FOCUS );
412 paint_button( hWnd, btn_type, ODA_FOCUS );
414 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
415 ReleaseCapture();
416 if (style & BS_NOTIFY)
417 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
419 InvalidateRect( hWnd, NULL, FALSE );
420 break;
422 case WM_SYSCOLORCHANGE:
423 InvalidateRect( hWnd, NULL, FALSE );
424 break;
426 case BM_SETSTYLE:
427 btn_type = wParam & BS_TYPEMASK;
428 style = (style & ~BS_TYPEMASK) | btn_type;
429 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
431 /* Only redraw if lParam flag is set.*/
432 if (lParam)
433 InvalidateRect( hWnd, NULL, TRUE );
435 break;
437 case BM_CLICK:
438 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
439 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
440 break;
442 case BM_SETIMAGE:
443 /* Check that image format matches button style */
444 switch (style & (BS_BITMAP|BS_ICON))
446 case BS_BITMAP:
447 if (wParam != IMAGE_BITMAP) return 0;
448 break;
449 case BS_ICON:
450 if (wParam != IMAGE_ICON) return 0;
451 break;
452 default:
453 return 0;
455 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
456 InvalidateRect( hWnd, NULL, FALSE );
457 return (LRESULT)oldHbitmap;
459 case BM_GETIMAGE:
460 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
462 case BM_GETCHECK:
463 return get_button_state( hWnd ) & 3;
465 case BM_SETCHECK:
466 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
467 state = get_button_state( hWnd );
468 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
470 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
471 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
473 if ((state & 3) != wParam)
475 set_button_state( hWnd, (state & ~3) | wParam );
476 paint_button( hWnd, btn_type, ODA_SELECT );
478 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BST_CHECKED) && (style & WS_CHILD))
479 BUTTON_CheckAutoRadioButton( hWnd );
480 break;
482 case BM_GETSTATE:
483 return get_button_state( hWnd );
485 case BM_SETSTATE:
486 state = get_button_state( hWnd );
487 if (wParam)
488 set_button_state( hWnd, state | BST_PUSHED );
489 else
490 set_button_state( hWnd, state & ~BST_PUSHED );
492 paint_button( hWnd, btn_type, ODA_SELECT );
493 break;
495 case WM_NCHITTEST:
496 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
497 /* fall through */
498 default:
499 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
500 DefWindowProcA(hWnd, uMsg, wParam, lParam);
502 return 0;
505 /**********************************************************************
506 * Convert button styles to flags used by DrawText.
508 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
510 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
512 /* "Convert" pushlike buttons to pushbuttons */
513 if (style & BS_PUSHLIKE)
514 style &= ~BS_TYPEMASK;
516 if (!(style & BS_MULTILINE))
517 dtStyle |= DT_SINGLELINE;
518 else
519 dtStyle |= DT_WORDBREAK;
521 switch (style & BS_CENTER)
523 case BS_LEFT: /* DT_LEFT is 0 */ break;
524 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
525 case BS_CENTER: dtStyle |= DT_CENTER; break;
526 default:
527 /* Pushbutton's text is centered by default */
528 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
529 /* all other flavours have left aligned text */
532 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
534 /* DrawText ignores vertical alignment for multiline text,
535 * but we use these flags to align label manually.
537 if (get_button_type(style) != BS_GROUPBOX)
539 switch (style & BS_VCENTER)
541 case BS_TOP: /* DT_TOP is 0 */ break;
542 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
543 case BS_VCENTER: /* fall through */
544 default: dtStyle |= DT_VCENTER; break;
547 else
548 /* GroupBox's text is always single line and is top aligned. */
549 dtStyle |= DT_SINGLELINE;
551 return dtStyle;
554 /**********************************************************************
555 * BUTTON_CalcLabelRect
557 * Calculates label's rectangle depending on button style.
559 * Returns flags to be passed to DrawText.
560 * Calculated rectangle doesn't take into account button state
561 * (pushed, etc.). If there is nothing to draw (no text/image) output
562 * rectangle is empty, and return value is (UINT)-1.
564 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
566 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
567 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
568 WCHAR *text;
569 ICONINFO iconInfo;
570 BITMAP bm;
571 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
572 RECT r = *rc;
573 INT n;
575 /* Calculate label rectangle according to label type */
576 switch (style & (BS_ICON|BS_BITMAP))
578 case BS_TEXT:
580 HFONT hFont, hPrevFont = 0;
582 if (!(text = get_button_text( hwnd ))) goto empty_rect;
583 if (!text[0])
585 HeapFree( GetProcessHeap(), 0, text );
586 goto empty_rect;
589 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hdc, hFont );
590 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
591 if (hPrevFont) SelectObject( hdc, hPrevFont );
592 HeapFree( GetProcessHeap(), 0, text );
593 break;
596 case BS_ICON:
597 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
598 goto empty_rect;
600 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
602 r.right = r.left + bm.bmWidth;
603 r.bottom = r.top + bm.bmHeight;
605 DeleteObject(iconInfo.hbmColor);
606 DeleteObject(iconInfo.hbmMask);
607 break;
609 case BS_BITMAP:
610 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
611 goto empty_rect;
613 r.right = r.left + bm.bmWidth;
614 r.bottom = r.top + bm.bmHeight;
615 break;
617 default:
618 empty_rect:
619 rc->right = r.left;
620 rc->bottom = r.top;
621 return (UINT)-1;
624 /* Position label inside bounding rectangle according to
625 * alignment flags. (calculated rect is always left-top aligned).
626 * If label is aligned to any side - shift label in opposite
627 * direction to leave extra space for focus rectangle.
629 switch (dtStyle & (DT_CENTER|DT_RIGHT))
631 case DT_LEFT: r.left++; r.right++; break;
632 case DT_CENTER: n = r.right - r.left;
633 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
634 r.right = r.left + n; break;
635 case DT_RIGHT: n = r.right - r.left;
636 r.right = rc->right - 1;
637 r.left = r.right - n;
638 break;
641 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
643 case DT_TOP: r.top++; r.bottom++; break;
644 case DT_VCENTER: n = r.bottom - r.top;
645 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
646 r.bottom = r.top + n; break;
647 case DT_BOTTOM: n = r.bottom - r.top;
648 r.bottom = rc->bottom - 1;
649 r.top = r.bottom - n;
650 break;
653 *rc = r;
654 return dtStyle;
658 /**********************************************************************
659 * BUTTON_DrawTextCallback
661 * Callback function used by DrawStateW function.
663 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
665 RECT rc;
667 SetRect(&rc, 0, 0, cx, cy);
668 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
669 return TRUE;
673 /**********************************************************************
674 * BUTTON_DrawLabel
676 * Common function for drawing button label.
678 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
680 DRAWSTATEPROC lpOutputProc = NULL;
681 LPARAM lp;
682 WPARAM wp = 0;
683 HBRUSH hbr = 0;
684 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
685 LONG state = get_button_state( hwnd );
686 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
687 WCHAR *text = NULL;
689 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
690 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
691 * I don't have Win31 on hand to verify that, so I leave it as is.
694 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
696 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
697 flags |= DSS_MONO;
700 switch (style & (BS_ICON|BS_BITMAP))
702 case BS_TEXT:
703 /* DST_COMPLEX -- is 0 */
704 lpOutputProc = BUTTON_DrawTextCallback;
705 if (!(text = get_button_text( hwnd ))) return;
706 lp = (LPARAM)text;
707 wp = dtFlags;
708 break;
710 case BS_ICON:
711 flags |= DST_ICON;
712 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
713 break;
715 case BS_BITMAP:
716 flags |= DST_BITMAP;
717 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
718 break;
720 default:
721 return;
724 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
725 rc->right - rc->left, rc->bottom - rc->top, flags);
726 HeapFree( GetProcessHeap(), 0, text );
729 /**********************************************************************
730 * Push Button Functions
732 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
734 RECT rc, r;
735 UINT dtFlags, uState;
736 HPEN hOldPen;
737 HBRUSH hOldBrush;
738 INT oldBkMode;
739 COLORREF oldTxtColor;
740 HFONT hFont;
741 LONG state = get_button_state( hwnd );
742 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
743 BOOL pushedState = (state & BST_PUSHED);
744 HWND parent;
745 HRGN hrgn;
747 GetClientRect( hwnd, &rc );
749 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
750 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
751 parent = GetParent(hwnd);
752 if (!parent) parent = hwnd;
753 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
755 hrgn = set_control_clipping( hDC, &rc );
757 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
758 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
759 oldBkMode = SetBkMode(hDC, TRANSPARENT);
761 if (get_button_type(style) == BS_DEFPUSHBUTTON)
763 if (action != ODA_FOCUS)
764 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
765 InflateRect( &rc, -1, -1 );
768 /* completely skip the drawing if only focus has changed */
769 if (action == ODA_FOCUS) goto draw_focus;
771 uState = DFCS_BUTTONPUSH;
773 if (style & BS_FLAT)
774 uState |= DFCS_MONO;
775 else if (pushedState)
777 if (get_button_type(style) == BS_DEFPUSHBUTTON )
778 uState |= DFCS_FLAT;
779 else
780 uState |= DFCS_PUSHED;
783 if (state & (BST_CHECKED | BST_INDETERMINATE))
784 uState |= DFCS_CHECKED;
786 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
788 /* draw button label */
789 r = rc;
790 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
792 if (dtFlags == (UINT)-1L)
793 goto cleanup;
795 if (pushedState)
796 OffsetRect(&r, 1, 1);
798 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
800 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
802 SetTextColor( hDC, oldTxtColor );
804 draw_focus:
805 if (action == ODA_FOCUS || (state & BST_FOCUS))
807 InflateRect( &rc, -2, -2 );
808 DrawFocusRect( hDC, &rc );
811 cleanup:
812 SelectObject( hDC, hOldPen );
813 SelectObject( hDC, hOldBrush );
814 SetBkMode(hDC, oldBkMode);
815 SelectClipRgn( hDC, hrgn );
816 if (hrgn) DeleteObject( hrgn );
819 /**********************************************************************
820 * Check Box & Radio Button Functions
823 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
825 RECT rbox, rtext, client;
826 HBRUSH hBrush;
827 int delta, text_offset, checkBoxWidth, checkBoxHeight;
828 UINT dtFlags;
829 HFONT hFont;
830 LONG state = get_button_state( hwnd );
831 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
832 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
833 HWND parent;
834 HRGN hrgn;
836 if (style & BS_PUSHLIKE)
838 PB_Paint( hwnd, hDC, action );
839 return;
842 GetClientRect(hwnd, &client);
843 rbox = rtext = client;
845 checkBoxWidth = 12 * GetDeviceCaps( hDC, LOGPIXELSX ) / 96 + 1;
846 checkBoxHeight = 12 * GetDeviceCaps( hDC, LOGPIXELSY ) / 96 + 1;
848 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
849 GetCharWidthW( hDC, '0', '0', &text_offset );
850 text_offset /= 2;
852 parent = GetParent(hwnd);
853 if (!parent) parent = hwnd;
854 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
855 (WPARAM)hDC, (LPARAM)hwnd);
856 if (!hBrush) /* did the app forget to call defwindowproc ? */
857 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
858 (WPARAM)hDC, (LPARAM)hwnd );
859 hrgn = set_control_clipping( hDC, &client );
861 if (style & BS_LEFTTEXT || ex_style & WS_EX_RIGHT)
863 rtext.right -= checkBoxWidth + text_offset;
864 rbox.left = rbox.right - checkBoxWidth;
866 else
868 rtext.left += checkBoxWidth + text_offset;
869 rbox.right = checkBoxWidth;
872 /* Since WM_ERASEBKGND does nothing, first prepare background */
873 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
874 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
876 /* Draw label */
877 client = rtext;
878 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
880 /* Only adjust rbox when rtext is valid */
881 if (dtFlags != (UINT)-1L)
883 rbox.top = rtext.top;
884 rbox.bottom = rtext.bottom;
887 /* Draw the check-box bitmap */
888 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
890 UINT flags;
892 if ((get_button_type(style) == BS_RADIOBUTTON) ||
893 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
894 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
895 else flags = DFCS_BUTTONCHECK;
897 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
898 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
900 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
902 /* rbox must have the correct height */
903 delta = rbox.bottom - rbox.top - checkBoxHeight;
905 if ((style & BS_VCENTER) == BS_TOP) {
906 if (delta > 0) {
907 rbox.bottom = rbox.top + checkBoxHeight;
908 } else {
909 rbox.top -= -delta/2 + 1;
910 rbox.bottom = rbox.top + checkBoxHeight;
912 } else if ((style & BS_VCENTER) == BS_BOTTOM) {
913 if (delta > 0) {
914 rbox.top = rbox.bottom - checkBoxHeight;
915 } else {
916 rbox.bottom += -delta/2 + 1;
917 rbox.top = rbox.bottom - checkBoxHeight;
919 } else { /* Default */
920 if (delta > 0) {
921 int ofs = (delta / 2);
922 rbox.bottom -= ofs + 1;
923 rbox.top = rbox.bottom - checkBoxHeight;
924 } else if (delta < 0) {
925 int ofs = (-delta / 2);
926 rbox.top -= ofs + 1;
927 rbox.bottom = rbox.top + checkBoxHeight;
931 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
934 if (dtFlags == (UINT)-1L) /* Noting to draw */
935 return;
937 if (action == ODA_DRAWENTIRE)
938 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
940 /* ... and focus */
941 if (action == ODA_FOCUS || (state & BST_FOCUS))
943 rtext.left--;
944 rtext.right++;
945 IntersectRect(&rtext, &rtext, &client);
946 DrawFocusRect( hDC, &rtext );
948 SelectClipRgn( hDC, hrgn );
949 if (hrgn) DeleteObject( hrgn );
953 /**********************************************************************
954 * BUTTON_CheckAutoRadioButton
956 * hwnd is checked, uncheck every other auto radio button in group
958 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
960 HWND parent, sibling, start;
962 parent = GetParent(hwnd);
963 /* make sure that starting control is not disabled or invisible */
964 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
967 if (!sibling) break;
968 if ((hwnd != sibling) &&
969 ((GetWindowLongW( sibling, GWL_STYLE) & BS_TYPEMASK) == BS_AUTORADIOBUTTON))
970 SendMessageW( sibling, BM_SETCHECK, BST_UNCHECKED, 0 );
971 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
972 } while (sibling != start);
976 /**********************************************************************
977 * Group Box Functions
980 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
982 RECT rc, rcFrame;
983 HBRUSH hbr;
984 HFONT hFont;
985 UINT dtFlags;
986 TEXTMETRICW tm;
987 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
988 HWND parent;
989 HRGN hrgn;
991 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
992 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
993 parent = GetParent(hwnd);
994 if (!parent) parent = hwnd;
995 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
996 if (!hbr) /* did the app forget to call defwindowproc ? */
997 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
998 (WPARAM)hDC, (LPARAM)hwnd);
999 GetClientRect( hwnd, &rc);
1000 rcFrame = rc;
1001 hrgn = set_control_clipping( hDC, &rc );
1003 GetTextMetricsW (hDC, &tm);
1004 rcFrame.top += (tm.tmHeight / 2) - 1;
1005 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1007 InflateRect(&rc, -7, 1);
1008 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1010 if (dtFlags != (UINT)-1)
1012 /* Because buttons have CS_PARENTDC class style, there is a chance
1013 * that label will be drawn out of client rect.
1014 * But Windows doesn't clip label's rect, so do I.
1017 /* There is 1-pixel margin at the left, right, and bottom */
1018 rc.left--; rc.right++; rc.bottom++;
1019 FillRect(hDC, &rc, hbr);
1020 rc.left++; rc.right--; rc.bottom--;
1022 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1024 SelectClipRgn( hDC, hrgn );
1025 if (hrgn) DeleteObject( hrgn );
1029 /**********************************************************************
1030 * User Button Functions
1033 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1035 RECT rc;
1036 HBRUSH hBrush;
1037 HFONT hFont;
1038 LONG state = get_button_state( hwnd );
1039 HWND parent;
1041 GetClientRect( hwnd, &rc);
1043 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1045 parent = GetParent(hwnd);
1046 if (!parent) parent = hwnd;
1047 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1048 if (!hBrush) /* did the app forget to call defwindowproc ? */
1049 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1050 (WPARAM)hDC, (LPARAM)hwnd);
1052 FillRect( hDC, &rc, hBrush );
1053 if (action == ODA_FOCUS || (state & BST_FOCUS))
1054 DrawFocusRect( hDC, &rc );
1056 switch (action)
1058 case ODA_FOCUS:
1059 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1060 break;
1062 case ODA_SELECT:
1063 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1064 break;
1066 default:
1067 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1068 break;
1073 /**********************************************************************
1074 * Ownerdrawn Button Functions
1077 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1079 LONG state = get_button_state( hwnd );
1080 DRAWITEMSTRUCT dis;
1081 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1082 HWND parent;
1083 HFONT hFont;
1084 HRGN hrgn;
1086 dis.CtlType = ODT_BUTTON;
1087 dis.CtlID = id;
1088 dis.itemID = 0;
1089 dis.itemAction = action;
1090 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1091 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1092 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1093 dis.hwndItem = hwnd;
1094 dis.hDC = hDC;
1095 dis.itemData = 0;
1096 GetClientRect( hwnd, &dis.rcItem );
1098 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1099 parent = GetParent(hwnd);
1100 if (!parent) parent = hwnd;
1101 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1103 hrgn = set_control_clipping( hDC, &dis.rcItem );
1105 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1106 SelectClipRgn( hDC, hrgn );
1107 if (hrgn) DeleteObject( hrgn );