mshtml: Rename fire_event_obj and dispatch_event.
[wine.git] / dlls / user32 / button.c
blobe5cee120c1ed6eebac14c05722c6d81c4f1ee2ef
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 /*********************************************************************
147 * button class descriptor
149 static const WCHAR buttonW[] = {'B','u','t','t','o','n',0};
150 const struct builtin_class_descr BUTTON_builtin_class =
152 buttonW, /* name */
153 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
154 WINPROC_BUTTON, /* proc */
155 NB_EXTRA_BYTES, /* extra */
156 IDC_ARROW, /* cursor */
157 0 /* brush */
161 static inline LONG get_button_state( HWND hwnd )
163 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
166 static inline void set_button_state( HWND hwnd, LONG state )
168 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
171 static inline HFONT get_button_font( HWND hwnd )
173 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
176 static inline void set_button_font( HWND hwnd, HFONT font )
178 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
181 static inline UINT get_button_type( LONG window_style )
183 return (window_style & BS_TYPEMASK);
186 /* paint a button of any type */
187 static inline void paint_button( HWND hwnd, LONG style, UINT action )
189 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
191 HDC hdc = GetDC( hwnd );
192 btnPaintFunc[style]( hwnd, hdc, action );
193 ReleaseDC( hwnd, hdc );
197 /* retrieve the button text; returned buffer must be freed by caller */
198 static inline WCHAR *get_button_text( HWND hwnd )
200 static const INT len = 512;
201 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
202 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
203 return buffer;
206 /***********************************************************************
207 * ButtonWndProc_common
209 LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL unicode )
211 RECT rect;
212 POINT pt;
213 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
214 UINT btn_type = get_button_type( style );
215 LONG state;
216 HANDLE oldHbitmap;
218 if (!IsWindow( hWnd )) return 0;
220 pt.x = (short)LOWORD(lParam);
221 pt.y = (short)HIWORD(lParam);
223 switch (uMsg)
225 case WM_GETDLGCODE:
226 switch(btn_type)
228 case BS_USERBUTTON:
229 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
230 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
231 case BS_RADIOBUTTON:
232 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
233 case BS_GROUPBOX: return DLGC_STATIC;
234 default: return DLGC_BUTTON;
237 case WM_ENABLE:
238 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
239 break;
241 case WM_CREATE:
242 if (btn_type >= MAX_BTN_TYPE)
243 return -1; /* abort */
245 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
246 if (btn_type == BS_USERBUTTON )
248 style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
249 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
251 set_button_state( hWnd, BST_UNCHECKED );
252 return 0;
254 case WM_ERASEBKGND:
255 if (btn_type == BS_OWNERDRAW)
257 HDC hdc = (HDC)wParam;
258 RECT rc;
259 HBRUSH hBrush;
260 HWND parent = GetParent(hWnd);
261 if (!parent) parent = hWnd;
262 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
263 if (!hBrush) /* did the app forget to call defwindowproc ? */
264 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
265 (WPARAM)hdc, (LPARAM)hWnd);
266 GetClientRect(hWnd, &rc);
267 FillRect(hdc, &rc, hBrush);
269 return 1;
271 case WM_PRINTCLIENT:
272 case WM_PAINT:
274 PAINTSTRUCT ps;
275 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
276 if (btnPaintFunc[btn_type])
278 int nOldMode = SetBkMode( hdc, OPAQUE );
279 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
280 SetBkMode(hdc, nOldMode); /* reset painting mode */
282 if ( !wParam ) EndPaint( hWnd, &ps );
283 break;
286 case WM_KEYDOWN:
287 if (wParam == VK_SPACE)
289 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
290 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
291 SetCapture( hWnd );
293 break;
295 case WM_LBUTTONDBLCLK:
296 if(style & BS_NOTIFY ||
297 btn_type == BS_RADIOBUTTON ||
298 btn_type == BS_USERBUTTON ||
299 btn_type == BS_OWNERDRAW)
301 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
302 break;
304 /* fall through */
305 case WM_LBUTTONDOWN:
306 SetCapture( hWnd );
307 SetFocus( hWnd );
308 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
309 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
310 break;
312 case WM_KEYUP:
313 if (wParam != VK_SPACE)
314 break;
315 /* fall through */
316 case WM_LBUTTONUP:
317 state = get_button_state( hWnd );
318 if (!(state & BUTTON_BTNPRESSED)) break;
319 state &= BUTTON_NSTATES;
320 set_button_state( hWnd, state );
321 if (!(state & BST_PUSHED))
323 ReleaseCapture();
324 break;
326 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
327 GetClientRect( hWnd, &rect );
328 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
330 state = get_button_state( hWnd );
331 switch(btn_type)
333 case BS_AUTOCHECKBOX:
334 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
335 break;
336 case BS_AUTORADIOBUTTON:
337 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
338 break;
339 case BS_AUTO3STATE:
340 SendMessageW( hWnd, BM_SETCHECK,
341 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
342 break;
344 ReleaseCapture();
345 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
347 else
349 ReleaseCapture();
351 break;
353 case WM_CAPTURECHANGED:
354 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
355 if (hWnd == (HWND)lParam) break;
356 state = get_button_state( hWnd );
357 if (state & BUTTON_BTNPRESSED)
359 state &= BUTTON_NSTATES;
360 set_button_state( hWnd, state );
361 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
363 break;
365 case WM_MOUSEMOVE:
366 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
368 GetClientRect( hWnd, &rect );
369 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
371 break;
373 case WM_SETTEXT:
375 /* Clear an old text here as Windows does */
376 if (IsWindowVisible(hWnd))
378 HDC hdc = GetDC(hWnd);
379 HBRUSH hbrush;
380 RECT client, rc;
381 HWND parent = GetParent(hWnd);
382 UINT message = (btn_type == BS_PUSHBUTTON ||
383 btn_type == BS_DEFPUSHBUTTON ||
384 btn_type == BS_USERBUTTON ||
385 btn_type == BS_OWNERDRAW) ?
386 WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
388 if (!parent) parent = hWnd;
389 hbrush = (HBRUSH)SendMessageW(parent, message,
390 (WPARAM)hdc, (LPARAM)hWnd);
391 if (!hbrush) /* did the app forget to call DefWindowProc ? */
392 hbrush = (HBRUSH)DefWindowProcW(parent, message,
393 (WPARAM)hdc, (LPARAM)hWnd);
395 GetClientRect(hWnd, &client);
396 rc = client;
397 /* FIXME: check other BS_* handlers */
398 if (btn_type == BS_GROUPBOX)
399 InflateRect(&rc, -7, 1); /* GB_Paint does this */
400 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
401 /* Clip by client rect bounds */
402 if (rc.right > client.right) rc.right = client.right;
403 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
404 FillRect(hdc, &rc, hbrush);
405 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:
605 HFONT hFont, hPrevFont = 0;
607 if (!(text = get_button_text( hwnd ))) goto empty_rect;
608 if (!text[0])
610 HeapFree( GetProcessHeap(), 0, text );
611 goto empty_rect;
614 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hdc, hFont );
615 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
616 if (hPrevFont) SelectObject( hdc, hPrevFont );
617 HeapFree( GetProcessHeap(), 0, text );
618 break;
621 case BS_ICON:
622 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
623 goto empty_rect;
625 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
627 r.right = r.left + bm.bmWidth;
628 r.bottom = r.top + bm.bmHeight;
630 DeleteObject(iconInfo.hbmColor);
631 DeleteObject(iconInfo.hbmMask);
632 break;
634 case BS_BITMAP:
635 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
636 goto empty_rect;
638 r.right = r.left + bm.bmWidth;
639 r.bottom = r.top + bm.bmHeight;
640 break;
642 default:
643 empty_rect:
644 rc->right = r.left;
645 rc->bottom = r.top;
646 return (UINT)-1;
649 /* Position label inside bounding rectangle according to
650 * alignment flags. (calculated rect is always left-top aligned).
651 * If label is aligned to any side - shift label in opposite
652 * direction to leave extra space for focus rectangle.
654 switch (dtStyle & (DT_CENTER|DT_RIGHT))
656 case DT_LEFT: r.left++; r.right++; break;
657 case DT_CENTER: n = r.right - r.left;
658 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
659 r.right = r.left + n; break;
660 case DT_RIGHT: n = r.right - r.left;
661 r.right = rc->right - 1;
662 r.left = r.right - n;
663 break;
666 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
668 case DT_TOP: r.top++; r.bottom++; break;
669 case DT_VCENTER: n = r.bottom - r.top;
670 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
671 r.bottom = r.top + n; break;
672 case DT_BOTTOM: n = r.bottom - r.top;
673 r.bottom = rc->bottom - 1;
674 r.top = r.bottom - n;
675 break;
678 *rc = r;
679 return dtStyle;
683 /**********************************************************************
684 * BUTTON_DrawTextCallback
686 * Callback function used by DrawStateW function.
688 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
690 RECT rc;
692 SetRect(&rc, 0, 0, cx, cy);
693 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
694 return TRUE;
698 /**********************************************************************
699 * BUTTON_DrawLabel
701 * Common function for drawing button label.
703 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
705 DRAWSTATEPROC lpOutputProc = NULL;
706 LPARAM lp;
707 WPARAM wp = 0;
708 HBRUSH hbr = 0;
709 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
710 LONG state = get_button_state( hwnd );
711 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
712 WCHAR *text = NULL;
714 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
715 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
716 * I don't have Win31 on hand to verify that, so I leave it as is.
719 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
721 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
722 flags |= DSS_MONO;
725 switch (style & (BS_ICON|BS_BITMAP))
727 case BS_TEXT:
728 /* DST_COMPLEX -- is 0 */
729 lpOutputProc = BUTTON_DrawTextCallback;
730 if (!(text = get_button_text( hwnd ))) return;
731 lp = (LPARAM)text;
732 wp = dtFlags;
733 break;
735 case BS_ICON:
736 flags |= DST_ICON;
737 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
738 break;
740 case BS_BITMAP:
741 flags |= DST_BITMAP;
742 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
743 break;
745 default:
746 return;
749 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
750 rc->right - rc->left, rc->bottom - rc->top, flags);
751 HeapFree( GetProcessHeap(), 0, text );
754 /**********************************************************************
755 * Push Button Functions
757 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
759 RECT rc, r;
760 UINT dtFlags, uState;
761 HPEN hOldPen;
762 HBRUSH hOldBrush;
763 INT oldBkMode;
764 COLORREF oldTxtColor;
765 HFONT hFont;
766 LONG state = get_button_state( hwnd );
767 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
768 BOOL pushedState = (state & BST_PUSHED);
769 HWND parent;
770 HRGN hrgn;
772 GetClientRect( hwnd, &rc );
774 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
775 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
776 parent = GetParent(hwnd);
777 if (!parent) parent = hwnd;
778 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
780 hrgn = set_control_clipping( hDC, &rc );
782 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
783 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
784 oldBkMode = SetBkMode(hDC, TRANSPARENT);
786 if (get_button_type(style) == BS_DEFPUSHBUTTON)
788 if (action != ODA_FOCUS)
789 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
790 InflateRect( &rc, -1, -1 );
793 /* completely skip the drawing if only focus has changed */
794 if (action == ODA_FOCUS) goto draw_focus;
796 uState = DFCS_BUTTONPUSH;
798 if (style & BS_FLAT)
799 uState |= DFCS_MONO;
800 else if (pushedState)
802 if (get_button_type(style) == BS_DEFPUSHBUTTON )
803 uState |= DFCS_FLAT;
804 else
805 uState |= DFCS_PUSHED;
808 if (state & (BST_CHECKED | BST_INDETERMINATE))
809 uState |= DFCS_CHECKED;
811 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
813 /* draw button label */
814 r = rc;
815 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
817 if (dtFlags == (UINT)-1L)
818 goto cleanup;
820 if (pushedState)
821 OffsetRect(&r, 1, 1);
823 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
825 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
827 SetTextColor( hDC, oldTxtColor );
829 draw_focus:
830 if (action == ODA_FOCUS || (state & BST_FOCUS))
832 InflateRect( &rc, -2, -2 );
833 DrawFocusRect( hDC, &rc );
836 cleanup:
837 SelectObject( hDC, hOldPen );
838 SelectObject( hDC, hOldBrush );
839 SetBkMode(hDC, oldBkMode);
840 SelectClipRgn( hDC, hrgn );
841 if (hrgn) DeleteObject( hrgn );
844 /**********************************************************************
845 * Check Box & Radio Button Functions
848 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
850 RECT rbox, rtext, client;
851 HBRUSH hBrush;
852 int delta, text_offset, checkBoxWidth, checkBoxHeight;
853 UINT dtFlags;
854 HFONT hFont;
855 LONG state = get_button_state( hwnd );
856 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
857 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
858 HWND parent;
859 HRGN hrgn;
861 if (style & BS_PUSHLIKE)
863 PB_Paint( hwnd, hDC, action );
864 return;
867 GetClientRect(hwnd, &client);
868 rbox = rtext = client;
870 checkBoxWidth = 12 * GetDeviceCaps( hDC, LOGPIXELSX ) / 96 + 1;
871 checkBoxHeight = 12 * GetDeviceCaps( hDC, LOGPIXELSY ) / 96 + 1;
873 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
874 GetCharWidthW( hDC, '0', '0', &text_offset );
875 text_offset /= 2;
877 parent = GetParent(hwnd);
878 if (!parent) parent = hwnd;
879 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
880 (WPARAM)hDC, (LPARAM)hwnd);
881 if (!hBrush) /* did the app forget to call defwindowproc ? */
882 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
883 (WPARAM)hDC, (LPARAM)hwnd );
884 hrgn = set_control_clipping( hDC, &client );
886 if (style & BS_LEFTTEXT || ex_style & WS_EX_RIGHT)
888 rtext.right -= checkBoxWidth + text_offset;
889 rbox.left = rbox.right - checkBoxWidth;
891 else
893 rtext.left += checkBoxWidth + text_offset;
894 rbox.right = checkBoxWidth;
897 /* Since WM_ERASEBKGND does nothing, first prepare background */
898 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
899 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
901 /* Draw label */
902 client = rtext;
903 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
905 /* Only adjust rbox when rtext is valid */
906 if (dtFlags != (UINT)-1L)
908 rbox.top = rtext.top;
909 rbox.bottom = rtext.bottom;
912 /* Draw the check-box bitmap */
913 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
915 UINT flags;
917 if ((get_button_type(style) == BS_RADIOBUTTON) ||
918 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
919 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
920 else flags = DFCS_BUTTONCHECK;
922 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
923 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
925 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
927 /* rbox must have the correct height */
928 delta = rbox.bottom - rbox.top - checkBoxHeight;
930 if (style & BS_TOP) {
931 if (delta > 0) {
932 rbox.bottom = rbox.top + checkBoxHeight;
933 } else {
934 rbox.top -= -delta/2 + 1;
935 rbox.bottom = rbox.top + checkBoxHeight;
937 } else if (style & BS_BOTTOM) {
938 if (delta > 0) {
939 rbox.top = rbox.bottom - checkBoxHeight;
940 } else {
941 rbox.bottom += -delta/2 + 1;
942 rbox.top = rbox.bottom - checkBoxHeight;
944 } else { /* Default */
945 if (delta > 0) {
946 int ofs = (delta / 2);
947 rbox.bottom -= ofs + 1;
948 rbox.top = rbox.bottom - checkBoxHeight;
949 } else if (delta < 0) {
950 int ofs = (-delta / 2);
951 rbox.top -= ofs + 1;
952 rbox.bottom = rbox.top + checkBoxHeight;
956 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
959 if (dtFlags == (UINT)-1L) /* Noting to draw */
960 return;
962 if (action == ODA_DRAWENTIRE)
963 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
965 /* ... and focus */
966 if (action == ODA_FOCUS || (state & BST_FOCUS))
968 rtext.left--;
969 rtext.right++;
970 IntersectRect(&rtext, &rtext, &client);
971 DrawFocusRect( hDC, &rtext );
973 SelectClipRgn( hDC, hrgn );
974 if (hrgn) DeleteObject( hrgn );
978 /**********************************************************************
979 * BUTTON_CheckAutoRadioButton
981 * hwnd is checked, uncheck every other auto radio button in group
983 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
985 HWND parent, sibling, start;
987 parent = GetParent(hwnd);
988 /* make sure that starting control is not disabled or invisible */
989 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
992 if (!sibling) break;
993 if ((hwnd != sibling) &&
994 ((GetWindowLongW( sibling, GWL_STYLE) & BS_TYPEMASK) == BS_AUTORADIOBUTTON))
995 SendMessageW( sibling, BM_SETCHECK, BST_UNCHECKED, 0 );
996 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
997 } while (sibling != start);
1001 /**********************************************************************
1002 * Group Box Functions
1005 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1007 RECT rc, rcFrame;
1008 HBRUSH hbr;
1009 HFONT hFont;
1010 UINT dtFlags;
1011 TEXTMETRICW tm;
1012 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1013 HWND parent;
1014 HRGN hrgn;
1016 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1017 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1018 parent = GetParent(hwnd);
1019 if (!parent) parent = hwnd;
1020 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1021 if (!hbr) /* did the app forget to call defwindowproc ? */
1022 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1023 (WPARAM)hDC, (LPARAM)hwnd);
1024 GetClientRect( hwnd, &rc);
1025 rcFrame = rc;
1026 hrgn = set_control_clipping( hDC, &rc );
1028 GetTextMetricsW (hDC, &tm);
1029 rcFrame.top += (tm.tmHeight / 2) - 1;
1030 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1032 InflateRect(&rc, -7, 1);
1033 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1035 if (dtFlags != (UINT)-1)
1037 /* Because buttons have CS_PARENTDC class style, there is a chance
1038 * that label will be drawn out of client rect.
1039 * But Windows doesn't clip label's rect, so do I.
1042 /* There is 1-pixel margin at the left, right, and bottom */
1043 rc.left--; rc.right++; rc.bottom++;
1044 FillRect(hDC, &rc, hbr);
1045 rc.left++; rc.right--; rc.bottom--;
1047 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1049 SelectClipRgn( hDC, hrgn );
1050 if (hrgn) DeleteObject( hrgn );
1054 /**********************************************************************
1055 * User Button Functions
1058 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1060 RECT rc;
1061 HBRUSH hBrush;
1062 HFONT hFont;
1063 LONG state = get_button_state( hwnd );
1064 HWND parent;
1066 GetClientRect( hwnd, &rc);
1068 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1070 parent = GetParent(hwnd);
1071 if (!parent) parent = hwnd;
1072 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1073 if (!hBrush) /* did the app forget to call defwindowproc ? */
1074 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1075 (WPARAM)hDC, (LPARAM)hwnd);
1077 FillRect( hDC, &rc, hBrush );
1078 if (action == ODA_FOCUS || (state & BST_FOCUS))
1079 DrawFocusRect( hDC, &rc );
1081 switch (action)
1083 case ODA_FOCUS:
1084 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1085 break;
1087 case ODA_SELECT:
1088 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1089 break;
1091 default:
1092 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1093 break;
1098 /**********************************************************************
1099 * Ownerdrawn Button Functions
1102 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1104 LONG state = get_button_state( hwnd );
1105 DRAWITEMSTRUCT dis;
1106 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1107 HWND parent;
1108 HFONT hFont, hPrevFont = 0;
1109 HRGN hrgn;
1111 dis.CtlType = ODT_BUTTON;
1112 dis.CtlID = id;
1113 dis.itemID = 0;
1114 dis.itemAction = action;
1115 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1116 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1117 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1118 dis.hwndItem = hwnd;
1119 dis.hDC = hDC;
1120 dis.itemData = 0;
1121 GetClientRect( hwnd, &dis.rcItem );
1123 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1124 parent = GetParent(hwnd);
1125 if (!parent) parent = hwnd;
1126 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1128 hrgn = set_control_clipping( hDC, &dis.rcItem );
1130 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1131 if (hPrevFont) SelectObject(hDC, hPrevFont);
1132 SelectClipRgn( hDC, hrgn );
1133 if (hrgn) DeleteObject( hrgn );