user32: Release capture before sending WM_COMMAND.
[wine.git] / dlls / user32 / button.c
blobdb479a82d501b2aa38c657f4156507c361a1e6fd
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 * NOTES
23 * This code was audited for completeness against the documented features
24 * of Comctl32.dll version 6.0 on Oct. 3, 2004, by Dimitrie O. Paun.
26 * Unless otherwise noted, we believe this code to be complete, as per
27 * the specification mentioned above.
28 * If you discover missing features, or bugs, please note them below.
30 * TODO
31 * Styles
32 * - BS_NOTIFY: is it complete?
33 * - BS_RIGHTBUTTON: same as BS_LEFTTEXT
35 * Messages
36 * - WM_CHAR: Checks a (manual or automatic) check box on '+' or '=', clears it on '-' key.
37 * - WM_SETFOCUS: For (manual or automatic) radio buttons, send the parent window BN_CLICKED
38 * - WM_NCCREATE: Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
39 * - WM_SYSKEYUP
40 * - BCM_GETIDEALSIZE
41 * - BCM_GETIMAGELIST
42 * - BCM_GETTEXTMARGIN
43 * - BCM_SETIMAGELIST
44 * - BCM_SETTEXTMARGIN
46 * Notifications
47 * - BCN_HOTITEMCHANGE
48 * - BN_DISABLE
49 * - BN_PUSHED/BN_HILITE
50 * + BN_KILLFOCUS: is it OK?
51 * - BN_PAINT
52 * + BN_SETFOCUS: is it OK?
53 * - BN_UNPUSHED/BN_UNHILITE
54 * - NM_CUSTOMDRAW
56 * Structures/Macros/Definitions
57 * - BUTTON_IMAGELIST
58 * - NMBCHOTITEM
59 * - Button_GetIdealSize
60 * - Button_GetImageList
61 * - Button_GetTextMargin
62 * - Button_SetImageList
63 * - Button_SetTextMargin
66 #include <stdarg.h>
67 #include <string.h>
68 #include <stdlib.h>
70 #define OEMRESOURCE
72 #include "windef.h"
73 #include "winbase.h"
74 #include "wingdi.h"
75 #include "controls.h"
76 #include "win.h"
77 #include "user_private.h"
78 #include "wine/debug.h"
80 WINE_DEFAULT_DEBUG_CHANNEL(button);
82 /* GetWindowLong offsets for window extra information */
83 #define STATE_GWL_OFFSET 0
84 #define HFONT_GWL_OFFSET (sizeof(LONG))
85 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
86 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
88 /* undocumented flags */
89 #define BUTTON_NSTATES 0x0F
90 #define BUTTON_BTNPRESSED 0x40
91 #define BUTTON_UNKNOWN2 0x20
92 #define BUTTON_UNKNOWN3 0x10
94 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
95 do { /* Notify parent which has created this button control */ \
96 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
97 SendMessageW(GetParent(hWnd), WM_COMMAND, \
98 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
99 (LPARAM)(hWnd)); \
100 } while(0)
102 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
103 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
104 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
105 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
106 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
107 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
108 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
110 #define MAX_BTN_TYPE 16
112 static const WORD maxCheckState[MAX_BTN_TYPE] =
114 BST_UNCHECKED, /* BS_PUSHBUTTON */
115 BST_UNCHECKED, /* BS_DEFPUSHBUTTON */
116 BST_CHECKED, /* BS_CHECKBOX */
117 BST_CHECKED, /* BS_AUTOCHECKBOX */
118 BST_CHECKED, /* BS_RADIOBUTTON */
119 BST_INDETERMINATE, /* BS_3STATE */
120 BST_INDETERMINATE, /* BS_AUTO3STATE */
121 BST_UNCHECKED, /* BS_GROUPBOX */
122 BST_UNCHECKED, /* BS_USERBUTTON */
123 BST_CHECKED, /* BS_AUTORADIOBUTTON */
124 BST_UNCHECKED, /* BS_PUSHBOX */
125 BST_UNCHECKED /* BS_OWNERDRAW */
128 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
130 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
132 PB_Paint, /* BS_PUSHBUTTON */
133 PB_Paint, /* BS_DEFPUSHBUTTON */
134 CB_Paint, /* BS_CHECKBOX */
135 CB_Paint, /* BS_AUTOCHECKBOX */
136 CB_Paint, /* BS_RADIOBUTTON */
137 CB_Paint, /* BS_3STATE */
138 CB_Paint, /* BS_AUTO3STATE */
139 GB_Paint, /* BS_GROUPBOX */
140 UB_Paint, /* BS_USERBUTTON */
141 CB_Paint, /* BS_AUTORADIOBUTTON */
142 NULL, /* BS_PUSHBOX */
143 OB_Paint /* BS_OWNERDRAW */
146 static HBITMAP hbitmapCheckBoxes = 0;
147 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
150 /*********************************************************************
151 * button class descriptor
153 static const WCHAR buttonW[] = {'B','u','t','t','o','n',0};
154 const struct builtin_class_descr BUTTON_builtin_class =
156 buttonW, /* name */
157 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
158 WINPROC_BUTTON, /* proc */
159 NB_EXTRA_BYTES, /* extra */
160 IDC_ARROW, /* cursor */
161 0 /* brush */
165 static inline LONG get_button_state( HWND hwnd )
167 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
170 static inline void set_button_state( HWND hwnd, LONG state )
172 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
175 static inline HFONT get_button_font( HWND hwnd )
177 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
180 static inline void set_button_font( HWND hwnd, HFONT font )
182 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
185 static inline UINT get_button_type( LONG window_style )
187 return (window_style & BS_TYPEMASK);
190 /* paint a button of any type */
191 static inline void paint_button( HWND hwnd, LONG style, UINT action )
193 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
195 HDC hdc = GetDC( hwnd );
196 btnPaintFunc[style]( hwnd, hdc, action );
197 ReleaseDC( hwnd, hdc );
201 /* retrieve the button text; returned buffer must be freed by caller */
202 static inline WCHAR *get_button_text( HWND hwnd )
204 INT len = 512;
205 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
206 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
207 return buffer;
210 /***********************************************************************
211 * ButtonWndProc_common
213 LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL unicode )
215 RECT rect;
216 POINT pt;
217 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
218 UINT btn_type = get_button_type( style );
219 LONG state;
220 HANDLE oldHbitmap;
222 if (!IsWindow( hWnd )) return 0;
224 pt.x = (short)LOWORD(lParam);
225 pt.y = (short)HIWORD(lParam);
227 switch (uMsg)
229 case WM_GETDLGCODE:
230 switch(btn_type)
232 case BS_USERBUTTON:
233 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
234 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
235 case BS_RADIOBUTTON:
236 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
237 case BS_GROUPBOX: return DLGC_STATIC;
238 default: return DLGC_BUTTON;
241 case WM_ENABLE:
242 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
243 break;
245 case WM_CREATE:
246 if (!hbitmapCheckBoxes)
248 BITMAP bmp;
249 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
250 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
251 checkBoxWidth = bmp.bmWidth / 4;
252 checkBoxHeight = bmp.bmHeight / 3;
254 if (btn_type >= MAX_BTN_TYPE)
255 return -1; /* abort */
257 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
258 if (btn_type == BS_USERBUTTON )
260 style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
261 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
263 set_button_state( hWnd, BST_UNCHECKED );
264 return 0;
266 case WM_ERASEBKGND:
267 if (btn_type == BS_OWNERDRAW)
269 HDC hdc = (HDC)wParam;
270 RECT rc;
271 HBRUSH hBrush;
272 HWND parent = GetParent(hWnd);
273 if (!parent) parent = hWnd;
274 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
275 if (!hBrush) /* did the app forget to call defwindowproc ? */
276 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
277 (WPARAM)hdc, (LPARAM)hWnd);
278 GetClientRect(hWnd, &rc);
279 FillRect(hdc, &rc, hBrush);
281 return 1;
283 case WM_PRINTCLIENT:
284 case WM_PAINT:
286 PAINTSTRUCT ps;
287 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
288 if (btnPaintFunc[btn_type])
290 int nOldMode = SetBkMode( hdc, OPAQUE );
291 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
292 SetBkMode(hdc, nOldMode); /* reset painting mode */
294 if ( !wParam ) EndPaint( hWnd, &ps );
295 break;
298 case WM_KEYDOWN:
299 if (wParam == VK_SPACE)
301 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
302 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
303 SetCapture( hWnd );
305 break;
307 case WM_LBUTTONDBLCLK:
308 if(style & BS_NOTIFY ||
309 btn_type == BS_RADIOBUTTON ||
310 btn_type == BS_USERBUTTON ||
311 btn_type == BS_OWNERDRAW)
313 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
314 break;
316 /* fall through */
317 case WM_LBUTTONDOWN:
318 SetCapture( hWnd );
319 SetFocus( hWnd );
320 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
321 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
322 break;
324 case WM_KEYUP:
325 if (wParam != VK_SPACE)
326 break;
327 /* fall through */
328 case WM_LBUTTONUP:
329 state = get_button_state( hWnd );
330 if (!(state & BUTTON_BTNPRESSED)) break;
331 state &= BUTTON_NSTATES;
332 set_button_state( hWnd, state );
333 if (!(state & BST_PUSHED))
335 ReleaseCapture();
336 break;
338 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
339 GetClientRect( hWnd, &rect );
340 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
342 state = get_button_state( hWnd );
343 switch(btn_type)
345 case BS_AUTOCHECKBOX:
346 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
347 break;
348 case BS_AUTORADIOBUTTON:
349 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
350 break;
351 case BS_AUTO3STATE:
352 SendMessageW( hWnd, BM_SETCHECK,
353 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
354 break;
356 ReleaseCapture();
357 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
359 else
361 ReleaseCapture();
363 break;
365 case WM_CAPTURECHANGED:
366 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
367 state = get_button_state( hWnd );
368 if (state & BUTTON_BTNPRESSED)
370 state &= BUTTON_NSTATES;
371 set_button_state( hWnd, state );
372 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
374 break;
376 case WM_MOUSEMOVE:
377 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
379 GetClientRect( hWnd, &rect );
380 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
382 break;
384 case WM_SETTEXT:
386 /* Clear an old text here as Windows does */
387 HDC hdc = GetDC(hWnd);
388 HBRUSH hbrush;
389 RECT client, rc;
390 HWND parent = GetParent(hWnd);
392 if (!parent) parent = hWnd;
393 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
394 (WPARAM)hdc, (LPARAM)hWnd);
395 if (!hbrush) /* did the app forget to call DefWindowProc ? */
396 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
397 (WPARAM)hdc, (LPARAM)hWnd);
399 GetClientRect(hWnd, &client);
400 rc = client;
401 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
402 /* Clip by client rect bounds */
403 if (rc.right > client.right) rc.right = client.right;
404 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
405 FillRect(hdc, &rc, hbrush);
406 ReleaseDC(hWnd, hdc);
408 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
409 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
410 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
411 InvalidateRect( hWnd, NULL, TRUE );
412 else
413 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
414 return 1; /* success. FIXME: check text length */
417 case WM_SETFONT:
418 set_button_font( hWnd, (HFONT)wParam );
419 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
420 break;
422 case WM_GETFONT:
423 return (LRESULT)get_button_font( hWnd );
425 case WM_SETFOCUS:
426 TRACE("WM_SETFOCUS %p\n",hWnd);
427 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
428 paint_button( hWnd, btn_type, ODA_FOCUS );
429 if (style & BS_NOTIFY)
430 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
431 break;
433 case WM_KILLFOCUS:
434 TRACE("WM_KILLFOCUS %p\n",hWnd);
435 state = get_button_state( hWnd );
436 set_button_state( hWnd, state & ~BST_FOCUS );
437 paint_button( hWnd, btn_type, ODA_FOCUS );
439 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
440 ReleaseCapture();
441 if (style & BS_NOTIFY)
442 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
444 InvalidateRect( hWnd, NULL, FALSE );
445 break;
447 case WM_SYSCOLORCHANGE:
448 InvalidateRect( hWnd, NULL, FALSE );
449 break;
451 case BM_SETSTYLE:
452 btn_type = wParam & BS_TYPEMASK;
453 style = (style & ~BS_TYPEMASK) | btn_type;
454 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
456 /* Only redraw if lParam flag is set.*/
457 if (lParam)
458 InvalidateRect( hWnd, NULL, TRUE );
460 break;
462 case BM_CLICK:
463 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
464 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
465 break;
467 case BM_SETIMAGE:
468 /* Check that image format matches button style */
469 switch (style & (BS_BITMAP|BS_ICON))
471 case BS_BITMAP:
472 if (wParam != IMAGE_BITMAP) return 0;
473 break;
474 case BS_ICON:
475 if (wParam != IMAGE_ICON) return 0;
476 break;
477 default:
478 return 0;
480 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
481 InvalidateRect( hWnd, NULL, FALSE );
482 return (LRESULT)oldHbitmap;
484 case BM_GETIMAGE:
485 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
487 case BM_GETCHECK:
488 return get_button_state( hWnd ) & 3;
490 case BM_SETCHECK:
491 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
492 state = get_button_state( hWnd );
493 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
495 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
496 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
498 if ((state & 3) != wParam)
500 set_button_state( hWnd, (state & ~3) | wParam );
501 paint_button( hWnd, btn_type, ODA_SELECT );
503 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BST_CHECKED) && (style & WS_CHILD))
504 BUTTON_CheckAutoRadioButton( hWnd );
505 break;
507 case BM_GETSTATE:
508 return get_button_state( hWnd );
510 case BM_SETSTATE:
511 state = get_button_state( hWnd );
512 if (wParam)
513 set_button_state( hWnd, state | BST_PUSHED );
514 else
515 set_button_state( hWnd, state & ~BST_PUSHED );
517 paint_button( hWnd, btn_type, ODA_SELECT );
518 break;
520 case WM_NCHITTEST:
521 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
522 /* fall through */
523 default:
524 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
525 DefWindowProcA(hWnd, uMsg, wParam, lParam);
527 return 0;
530 /**********************************************************************
531 * Convert button styles to flags used by DrawText.
533 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
535 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
537 /* "Convert" pushlike buttons to pushbuttons */
538 if (style & BS_PUSHLIKE)
539 style &= ~BS_TYPEMASK;
541 if (!(style & BS_MULTILINE))
542 dtStyle |= DT_SINGLELINE;
543 else
544 dtStyle |= DT_WORDBREAK;
546 switch (style & BS_CENTER)
548 case BS_LEFT: /* DT_LEFT is 0 */ break;
549 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
550 case BS_CENTER: dtStyle |= DT_CENTER; break;
551 default:
552 /* Pushbutton's text is centered by default */
553 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
554 /* all other flavours have left aligned text */
557 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
559 /* DrawText ignores vertical alignment for multiline text,
560 * but we use these flags to align label manually.
562 if (get_button_type(style) != BS_GROUPBOX)
564 switch (style & BS_VCENTER)
566 case BS_TOP: /* DT_TOP is 0 */ break;
567 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
568 case BS_VCENTER: /* fall through */
569 default: dtStyle |= DT_VCENTER; break;
572 else
573 /* GroupBox's text is always single line and is top aligned. */
574 dtStyle |= DT_SINGLELINE;
576 return dtStyle;
579 /**********************************************************************
580 * BUTTON_CalcLabelRect
582 * Calculates label's rectangle depending on button style.
584 * Returns flags to be passed to DrawText.
585 * Calculated rectangle doesn't take into account button state
586 * (pushed, etc.). If there is nothing to draw (no text/image) output
587 * rectangle is empty, and return value is (UINT)-1.
589 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
591 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
592 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
593 WCHAR *text;
594 ICONINFO iconInfo;
595 BITMAP bm;
596 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
597 RECT r = *rc;
598 INT n;
600 /* Calculate label rectangle according to label type */
601 switch (style & (BS_ICON|BS_BITMAP))
603 case BS_TEXT:
604 if (!(text = get_button_text( hwnd ))) goto empty_rect;
605 if (!text[0])
607 HeapFree( GetProcessHeap(), 0, text );
608 goto empty_rect;
610 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
611 HeapFree( GetProcessHeap(), 0, text );
612 break;
614 case BS_ICON:
615 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
616 goto empty_rect;
618 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
620 r.right = r.left + bm.bmWidth;
621 r.bottom = r.top + bm.bmHeight;
623 DeleteObject(iconInfo.hbmColor);
624 DeleteObject(iconInfo.hbmMask);
625 break;
627 case BS_BITMAP:
628 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
629 goto empty_rect;
631 r.right = r.left + bm.bmWidth;
632 r.bottom = r.top + bm.bmHeight;
633 break;
635 default:
636 empty_rect:
637 rc->right = r.left;
638 rc->bottom = r.top;
639 return (UINT)-1;
642 /* Position label inside bounding rectangle according to
643 * alignment flags. (calculated rect is always left-top aligned).
644 * If label is aligned to any side - shift label in opposite
645 * direction to leave extra space for focus rectangle.
647 switch (dtStyle & (DT_CENTER|DT_RIGHT))
649 case DT_LEFT: r.left++; r.right++; break;
650 case DT_CENTER: n = r.right - r.left;
651 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
652 r.right = r.left + n; break;
653 case DT_RIGHT: n = r.right - r.left;
654 r.right = rc->right - 1;
655 r.left = r.right - n;
656 break;
659 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
661 case DT_TOP: r.top++; r.bottom++; break;
662 case DT_VCENTER: n = r.bottom - r.top;
663 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
664 r.bottom = r.top + n; break;
665 case DT_BOTTOM: n = r.bottom - r.top;
666 r.bottom = rc->bottom - 1;
667 r.top = r.bottom - n;
668 break;
671 *rc = r;
672 return dtStyle;
676 /**********************************************************************
677 * BUTTON_DrawTextCallback
679 * Callback function used by DrawStateW function.
681 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
683 RECT rc;
684 rc.left = 0;
685 rc.top = 0;
686 rc.right = cx;
687 rc.bottom = cy;
689 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
690 return TRUE;
694 /**********************************************************************
695 * BUTTON_DrawLabel
697 * Common function for drawing button label.
699 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
701 DRAWSTATEPROC lpOutputProc = NULL;
702 LPARAM lp;
703 WPARAM wp = 0;
704 HBRUSH hbr = 0;
705 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
706 LONG state = get_button_state( hwnd );
707 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
708 WCHAR *text = NULL;
710 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
711 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
712 * I don't have Win31 on hand to verify that, so I leave it as is.
715 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
717 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
718 flags |= DSS_MONO;
721 switch (style & (BS_ICON|BS_BITMAP))
723 case BS_TEXT:
724 /* DST_COMPLEX -- is 0 */
725 lpOutputProc = BUTTON_DrawTextCallback;
726 if (!(text = get_button_text( hwnd ))) return;
727 lp = (LPARAM)text;
728 wp = dtFlags;
729 break;
731 case BS_ICON:
732 flags |= DST_ICON;
733 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
734 break;
736 case BS_BITMAP:
737 flags |= DST_BITMAP;
738 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
739 break;
741 default:
742 return;
745 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
746 rc->right - rc->left, rc->bottom - rc->top, flags);
747 HeapFree( GetProcessHeap(), 0, text );
750 /**********************************************************************
751 * Push Button Functions
753 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
755 RECT rc, r;
756 UINT dtFlags, uState;
757 HPEN hOldPen;
758 HBRUSH hOldBrush;
759 INT oldBkMode;
760 COLORREF oldTxtColor;
761 HFONT hFont;
762 LONG state = get_button_state( hwnd );
763 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
764 BOOL pushedState = (state & BST_PUSHED);
765 HWND parent;
766 HRGN hrgn;
768 GetClientRect( hwnd, &rc );
770 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
771 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
772 parent = GetParent(hwnd);
773 if (!parent) parent = hwnd;
774 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
776 hrgn = set_control_clipping( hDC, &rc );
778 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
779 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
780 oldBkMode = SetBkMode(hDC, TRANSPARENT);
782 if (get_button_type(style) == BS_DEFPUSHBUTTON)
784 if (action != ODA_FOCUS)
785 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
786 InflateRect( &rc, -1, -1 );
789 /* completely skip the drawing if only focus has changed */
790 if (action == ODA_FOCUS) goto draw_focus;
792 uState = DFCS_BUTTONPUSH;
794 if (style & BS_FLAT)
795 uState |= DFCS_MONO;
796 else if (pushedState)
798 if (get_button_type(style) == BS_DEFPUSHBUTTON )
799 uState |= DFCS_FLAT;
800 else
801 uState |= DFCS_PUSHED;
804 if (state & (BST_CHECKED | BST_INDETERMINATE))
805 uState |= DFCS_CHECKED;
807 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
809 /* draw button label */
810 r = rc;
811 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
813 if (dtFlags == (UINT)-1L)
814 goto cleanup;
816 if (pushedState)
817 OffsetRect(&r, 1, 1);
819 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
821 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
823 SetTextColor( hDC, oldTxtColor );
825 draw_focus:
826 if (action == ODA_FOCUS || (state & BST_FOCUS))
828 InflateRect( &rc, -2, -2 );
829 DrawFocusRect( hDC, &rc );
832 cleanup:
833 SelectObject( hDC, hOldPen );
834 SelectObject( hDC, hOldBrush );
835 SetBkMode(hDC, oldBkMode);
836 SelectClipRgn( hDC, hrgn );
837 if (hrgn) DeleteObject( hrgn );
840 /**********************************************************************
841 * Check Box & Radio Button Functions
844 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
846 RECT rbox, rtext, client;
847 HBRUSH hBrush;
848 int delta;
849 UINT dtFlags;
850 HFONT hFont;
851 LONG state = get_button_state( hwnd );
852 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
853 HWND parent;
854 HRGN hrgn;
856 if (style & BS_PUSHLIKE)
858 PB_Paint( hwnd, hDC, action );
859 return;
862 GetClientRect(hwnd, &client);
863 rbox = rtext = client;
865 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
867 parent = GetParent(hwnd);
868 if (!parent) parent = hwnd;
869 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
870 (WPARAM)hDC, (LPARAM)hwnd);
871 if (!hBrush) /* did the app forget to call defwindowproc ? */
872 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
873 (WPARAM)hDC, (LPARAM)hwnd );
874 hrgn = set_control_clipping( hDC, &client );
876 if (style & BS_LEFTTEXT)
878 /* magic +4 is what CTL3D expects */
880 rtext.right -= checkBoxWidth + 4;
881 rbox.left = rbox.right - checkBoxWidth;
883 else
885 rtext.left += checkBoxWidth + 4;
886 rbox.right = checkBoxWidth;
889 /* Since WM_ERASEBKGND does nothing, first prepare background */
890 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
891 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
893 /* Draw label */
894 client = rtext;
895 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
897 /* Only adjust rbox when rtext is valid */
898 if (dtFlags != (UINT)-1L)
900 rbox.top = rtext.top;
901 rbox.bottom = rtext.bottom;
904 /* Draw the check-box bitmap */
905 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
907 UINT flags;
909 if ((get_button_type(style) == BS_RADIOBUTTON) ||
910 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
911 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
912 else flags = DFCS_BUTTONCHECK;
914 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
915 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
917 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
919 /* rbox must have the correct height */
920 delta = rbox.bottom - rbox.top - checkBoxHeight;
922 if (style & BS_TOP) {
923 if (delta > 0) {
924 rbox.bottom = rbox.top + checkBoxHeight;
925 } else {
926 rbox.top -= -delta/2 + 1;
927 rbox.bottom = rbox.top + checkBoxHeight;
929 } else if (style & BS_BOTTOM) {
930 if (delta > 0) {
931 rbox.top = rbox.bottom - checkBoxHeight;
932 } else {
933 rbox.bottom += -delta/2 + 1;
934 rbox.top = rbox.bottom - checkBoxHeight;
936 } else { /* Default */
937 if (delta > 0) {
938 int ofs = (delta / 2);
939 rbox.bottom -= ofs + 1;
940 rbox.top = rbox.bottom - checkBoxHeight;
941 } else if (delta < 0) {
942 int ofs = (-delta / 2);
943 rbox.top -= ofs + 1;
944 rbox.bottom = rbox.top + checkBoxHeight;
948 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
951 if (dtFlags == (UINT)-1L) /* Noting to draw */
952 return;
954 if (action == ODA_DRAWENTIRE)
955 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
957 /* ... and focus */
958 if (action == ODA_FOCUS || (state & BST_FOCUS))
960 rtext.left--;
961 rtext.right++;
962 IntersectRect(&rtext, &rtext, &client);
963 DrawFocusRect( hDC, &rtext );
965 SelectClipRgn( hDC, hrgn );
966 if (hrgn) DeleteObject( hrgn );
970 /**********************************************************************
971 * BUTTON_CheckAutoRadioButton
973 * hwnd is checked, uncheck every other auto radio button in group
975 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
977 HWND parent, sibling, start;
979 parent = GetParent(hwnd);
980 /* make sure that starting control is not disabled or invisible */
981 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
984 if (!sibling) break;
985 if ((hwnd != sibling) &&
986 ((GetWindowLongW( sibling, GWL_STYLE) & BS_TYPEMASK) == BS_AUTORADIOBUTTON))
987 SendMessageW( sibling, BM_SETCHECK, BST_UNCHECKED, 0 );
988 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
989 } while (sibling != start);
993 /**********************************************************************
994 * Group Box Functions
997 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
999 RECT rc, rcFrame;
1000 HBRUSH hbr;
1001 HFONT hFont;
1002 UINT dtFlags;
1003 TEXTMETRICW tm;
1004 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1005 HWND parent;
1006 HRGN hrgn;
1008 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1009 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1010 parent = GetParent(hwnd);
1011 if (!parent) parent = hwnd;
1012 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1013 if (!hbr) /* did the app forget to call defwindowproc ? */
1014 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1015 (WPARAM)hDC, (LPARAM)hwnd);
1016 GetClientRect( hwnd, &rc);
1017 rcFrame = rc;
1018 hrgn = set_control_clipping( hDC, &rc );
1020 GetTextMetricsW (hDC, &tm);
1021 rcFrame.top += (tm.tmHeight / 2) - 1;
1022 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1024 InflateRect(&rc, -7, 1);
1025 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1027 if (dtFlags != (UINT)-1)
1029 /* Because buttons have CS_PARENTDC class style, there is a chance
1030 * that label will be drawn out of client rect.
1031 * But Windows doesn't clip label's rect, so do I.
1034 /* There is 1-pixel margin at the left, right, and bottom */
1035 rc.left--; rc.right++; rc.bottom++;
1036 FillRect(hDC, &rc, hbr);
1037 rc.left++; rc.right--; rc.bottom--;
1039 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1041 SelectClipRgn( hDC, hrgn );
1042 if (hrgn) DeleteObject( hrgn );
1046 /**********************************************************************
1047 * User Button Functions
1050 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1052 RECT rc;
1053 HBRUSH hBrush;
1054 HFONT hFont;
1055 LONG state = get_button_state( hwnd );
1056 HWND parent;
1058 GetClientRect( hwnd, &rc);
1060 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1062 parent = GetParent(hwnd);
1063 if (!parent) parent = hwnd;
1064 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1065 if (!hBrush) /* did the app forget to call defwindowproc ? */
1066 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1067 (WPARAM)hDC, (LPARAM)hwnd);
1069 FillRect( hDC, &rc, hBrush );
1070 if (action == ODA_FOCUS || (state & BST_FOCUS))
1071 DrawFocusRect( hDC, &rc );
1073 switch (action)
1075 case ODA_FOCUS:
1076 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1077 break;
1079 case ODA_SELECT:
1080 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1081 break;
1083 default:
1084 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1085 break;
1090 /**********************************************************************
1091 * Ownerdrawn Button Functions
1094 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1096 LONG state = get_button_state( hwnd );
1097 DRAWITEMSTRUCT dis;
1098 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1099 HWND parent;
1100 HFONT hFont, hPrevFont = 0;
1101 HRGN hrgn;
1103 dis.CtlType = ODT_BUTTON;
1104 dis.CtlID = id;
1105 dis.itemID = 0;
1106 dis.itemAction = action;
1107 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1108 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1109 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1110 dis.hwndItem = hwnd;
1111 dis.hDC = hDC;
1112 dis.itemData = 0;
1113 GetClientRect( hwnd, &dis.rcItem );
1115 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1116 parent = GetParent(hwnd);
1117 if (!parent) parent = hwnd;
1118 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1120 hrgn = set_control_clipping( hDC, &dis.rcItem );
1122 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1123 if (hPrevFont) SelectObject(hDC, hPrevFont);
1124 SelectClipRgn( hDC, hrgn );
1125 if (hrgn) DeleteObject( hrgn );