kernel32: Add a stub for GetCurrentProcessorNumberEx.
[wine.git] / dlls / user32 / button.c
blob9a10b86a65f8eef389f185a3da9935779a57cceb
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:
285 if (btnPaintFunc[btn_type])
287 PAINTSTRUCT ps;
288 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
289 int nOldMode = SetBkMode( hdc, OPAQUE );
290 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
291 SetBkMode(hdc, nOldMode); /* reset painting mode */
292 if( !wParam ) EndPaint( hWnd, &ps );
294 break;
296 case WM_KEYDOWN:
297 if (wParam == VK_SPACE)
299 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
300 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
301 SetCapture( hWnd );
303 break;
305 case WM_LBUTTONDBLCLK:
306 if(style & BS_NOTIFY ||
307 btn_type == BS_RADIOBUTTON ||
308 btn_type == BS_USERBUTTON ||
309 btn_type == BS_OWNERDRAW)
311 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
312 break;
314 /* fall through */
315 case WM_LBUTTONDOWN:
316 SetCapture( hWnd );
317 SetFocus( hWnd );
318 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
319 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
320 break;
322 case WM_KEYUP:
323 if (wParam != VK_SPACE)
324 break;
325 /* fall through */
326 case WM_LBUTTONUP:
327 state = get_button_state( hWnd );
328 if (!(state & BUTTON_BTNPRESSED)) break;
329 state &= BUTTON_NSTATES;
330 set_button_state( hWnd, state );
331 if (!(state & BST_PUSHED))
333 ReleaseCapture();
334 break;
336 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
337 GetClientRect( hWnd, &rect );
338 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
340 state = get_button_state( hWnd );
341 switch(btn_type)
343 case BS_AUTOCHECKBOX:
344 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
345 break;
346 case BS_AUTORADIOBUTTON:
347 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
348 break;
349 case BS_AUTO3STATE:
350 SendMessageW( hWnd, BM_SETCHECK,
351 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
352 break;
354 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
356 ReleaseCapture();
357 break;
359 case WM_CAPTURECHANGED:
360 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
361 state = get_button_state( hWnd );
362 if (state & BUTTON_BTNPRESSED)
364 state &= BUTTON_NSTATES;
365 set_button_state( hWnd, state );
366 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
368 break;
370 case WM_MOUSEMOVE:
371 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
373 GetClientRect( hWnd, &rect );
374 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
376 break;
378 case WM_SETTEXT:
380 /* Clear an old text here as Windows does */
381 HDC hdc = GetDC(hWnd);
382 HBRUSH hbrush;
383 RECT client, rc;
384 HWND parent = GetParent(hWnd);
386 if (!parent) parent = hWnd;
387 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
388 (WPARAM)hdc, (LPARAM)hWnd);
389 if (!hbrush) /* did the app forget to call DefWindowProc ? */
390 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
391 (WPARAM)hdc, (LPARAM)hWnd);
393 GetClientRect(hWnd, &client);
394 rc = client;
395 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
396 /* Clip by client rect bounds */
397 if (rc.right > client.right) rc.right = client.right;
398 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
399 FillRect(hdc, &rc, hbrush);
400 ReleaseDC(hWnd, hdc);
402 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
403 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
404 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
405 InvalidateRect( hWnd, NULL, TRUE );
406 else
407 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
408 return 1; /* success. FIXME: check text length */
411 case WM_SETFONT:
412 set_button_font( hWnd, (HFONT)wParam );
413 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
414 break;
416 case WM_GETFONT:
417 return (LRESULT)get_button_font( hWnd );
419 case WM_SETFOCUS:
420 TRACE("WM_SETFOCUS %p\n",hWnd);
421 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
422 paint_button( hWnd, btn_type, ODA_FOCUS );
423 if (style & BS_NOTIFY)
424 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
425 break;
427 case WM_KILLFOCUS:
428 TRACE("WM_KILLFOCUS %p\n",hWnd);
429 state = get_button_state( hWnd );
430 set_button_state( hWnd, state & ~BST_FOCUS );
431 paint_button( hWnd, btn_type, ODA_FOCUS );
433 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
434 ReleaseCapture();
435 if (style & BS_NOTIFY)
436 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
438 InvalidateRect( hWnd, NULL, FALSE );
439 break;
441 case WM_SYSCOLORCHANGE:
442 InvalidateRect( hWnd, NULL, FALSE );
443 break;
445 case BM_SETSTYLE:
446 btn_type = wParam & BS_TYPEMASK;
447 style = (style & ~BS_TYPEMASK) | btn_type;
448 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
450 /* Only redraw if lParam flag is set.*/
451 if (lParam)
452 InvalidateRect( hWnd, NULL, TRUE );
454 break;
456 case BM_CLICK:
457 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
458 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
459 break;
461 case BM_SETIMAGE:
462 /* Check that image format matches button style */
463 switch (style & (BS_BITMAP|BS_ICON))
465 case BS_BITMAP:
466 if (wParam != IMAGE_BITMAP) return 0;
467 break;
468 case BS_ICON:
469 if (wParam != IMAGE_ICON) return 0;
470 break;
471 default:
472 return 0;
474 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
475 InvalidateRect( hWnd, NULL, FALSE );
476 return (LRESULT)oldHbitmap;
478 case BM_GETIMAGE:
479 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
481 case BM_GETCHECK:
482 return get_button_state( hWnd ) & 3;
484 case BM_SETCHECK:
485 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
486 state = get_button_state( hWnd );
487 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
489 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
490 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
492 if ((state & 3) != wParam)
494 set_button_state( hWnd, (state & ~3) | wParam );
495 paint_button( hWnd, btn_type, ODA_SELECT );
497 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BST_CHECKED) && (style & WS_CHILD))
498 BUTTON_CheckAutoRadioButton( hWnd );
499 break;
501 case BM_GETSTATE:
502 return get_button_state( hWnd );
504 case BM_SETSTATE:
505 state = get_button_state( hWnd );
506 if (wParam)
507 set_button_state( hWnd, state | BST_PUSHED );
508 else
509 set_button_state( hWnd, state & ~BST_PUSHED );
511 paint_button( hWnd, btn_type, ODA_SELECT );
512 break;
514 case WM_NCHITTEST:
515 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
516 /* fall through */
517 default:
518 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
519 DefWindowProcA(hWnd, uMsg, wParam, lParam);
521 return 0;
524 /**********************************************************************
525 * Convert button styles to flags used by DrawText.
527 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
529 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
531 /* "Convert" pushlike buttons to pushbuttons */
532 if (style & BS_PUSHLIKE)
533 style &= ~BS_TYPEMASK;
535 if (!(style & BS_MULTILINE))
536 dtStyle |= DT_SINGLELINE;
537 else
538 dtStyle |= DT_WORDBREAK;
540 switch (style & BS_CENTER)
542 case BS_LEFT: /* DT_LEFT is 0 */ break;
543 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
544 case BS_CENTER: dtStyle |= DT_CENTER; break;
545 default:
546 /* Pushbutton's text is centered by default */
547 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
548 /* all other flavours have left aligned text */
551 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
553 /* DrawText ignores vertical alignment for multiline text,
554 * but we use these flags to align label manually.
556 if (get_button_type(style) != BS_GROUPBOX)
558 switch (style & BS_VCENTER)
560 case BS_TOP: /* DT_TOP is 0 */ break;
561 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
562 case BS_VCENTER: /* fall through */
563 default: dtStyle |= DT_VCENTER; break;
566 else
567 /* GroupBox's text is always single line and is top aligned. */
568 dtStyle |= DT_SINGLELINE;
570 return dtStyle;
573 /**********************************************************************
574 * BUTTON_CalcLabelRect
576 * Calculates label's rectangle depending on button style.
578 * Returns flags to be passed to DrawText.
579 * Calculated rectangle doesn't take into account button state
580 * (pushed, etc.). If there is nothing to draw (no text/image) output
581 * rectangle is empty, and return value is (UINT)-1.
583 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
585 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
586 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
587 WCHAR *text;
588 ICONINFO iconInfo;
589 BITMAP bm;
590 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
591 RECT r = *rc;
592 INT n;
594 /* Calculate label rectangle according to label type */
595 switch (style & (BS_ICON|BS_BITMAP))
597 case BS_TEXT:
598 if (!(text = get_button_text( hwnd ))) goto empty_rect;
599 if (!text[0])
601 HeapFree( GetProcessHeap(), 0, text );
602 goto empty_rect;
604 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
605 HeapFree( GetProcessHeap(), 0, text );
606 break;
608 case BS_ICON:
609 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
610 goto empty_rect;
612 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
614 r.right = r.left + bm.bmWidth;
615 r.bottom = r.top + bm.bmHeight;
617 DeleteObject(iconInfo.hbmColor);
618 DeleteObject(iconInfo.hbmMask);
619 break;
621 case BS_BITMAP:
622 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
623 goto empty_rect;
625 r.right = r.left + bm.bmWidth;
626 r.bottom = r.top + bm.bmHeight;
627 break;
629 default:
630 empty_rect:
631 rc->right = r.left;
632 rc->bottom = r.top;
633 return (UINT)-1;
636 /* Position label inside bounding rectangle according to
637 * alignment flags. (calculated rect is always left-top aligned).
638 * If label is aligned to any side - shift label in opposite
639 * direction to leave extra space for focus rectangle.
641 switch (dtStyle & (DT_CENTER|DT_RIGHT))
643 case DT_LEFT: r.left++; r.right++; break;
644 case DT_CENTER: n = r.right - r.left;
645 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
646 r.right = r.left + n; break;
647 case DT_RIGHT: n = r.right - r.left;
648 r.right = rc->right - 1;
649 r.left = r.right - n;
650 break;
653 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
655 case DT_TOP: r.top++; r.bottom++; break;
656 case DT_VCENTER: n = r.bottom - r.top;
657 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
658 r.bottom = r.top + n; break;
659 case DT_BOTTOM: n = r.bottom - r.top;
660 r.bottom = rc->bottom - 1;
661 r.top = r.bottom - n;
662 break;
665 *rc = r;
666 return dtStyle;
670 /**********************************************************************
671 * BUTTON_DrawTextCallback
673 * Callback function used by DrawStateW function.
675 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
677 RECT rc;
678 rc.left = 0;
679 rc.top = 0;
680 rc.right = cx;
681 rc.bottom = cy;
683 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
684 return TRUE;
688 /**********************************************************************
689 * BUTTON_DrawLabel
691 * Common function for drawing button label.
693 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
695 DRAWSTATEPROC lpOutputProc = NULL;
696 LPARAM lp;
697 WPARAM wp = 0;
698 HBRUSH hbr = 0;
699 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
700 LONG state = get_button_state( hwnd );
701 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
702 WCHAR *text = NULL;
704 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
705 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
706 * I don't have Win31 on hand to verify that, so I leave it as is.
709 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
711 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
712 flags |= DSS_MONO;
715 switch (style & (BS_ICON|BS_BITMAP))
717 case BS_TEXT:
718 /* DST_COMPLEX -- is 0 */
719 lpOutputProc = BUTTON_DrawTextCallback;
720 if (!(text = get_button_text( hwnd ))) return;
721 lp = (LPARAM)text;
722 wp = dtFlags;
723 break;
725 case BS_ICON:
726 flags |= DST_ICON;
727 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
728 break;
730 case BS_BITMAP:
731 flags |= DST_BITMAP;
732 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
733 break;
735 default:
736 return;
739 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
740 rc->right - rc->left, rc->bottom - rc->top, flags);
741 HeapFree( GetProcessHeap(), 0, text );
744 /**********************************************************************
745 * Push Button Functions
747 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
749 RECT rc, r;
750 UINT dtFlags, uState;
751 HPEN hOldPen;
752 HBRUSH hOldBrush;
753 INT oldBkMode;
754 COLORREF oldTxtColor;
755 HFONT hFont;
756 LONG state = get_button_state( hwnd );
757 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
758 BOOL pushedState = (state & BST_PUSHED);
759 HWND parent;
760 HRGN hrgn;
762 GetClientRect( hwnd, &rc );
764 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
765 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
766 parent = GetParent(hwnd);
767 if (!parent) parent = hwnd;
768 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
770 hrgn = set_control_clipping( hDC, &rc );
772 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
773 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
774 oldBkMode = SetBkMode(hDC, TRANSPARENT);
776 if (get_button_type(style) == BS_DEFPUSHBUTTON)
778 if (action != ODA_FOCUS)
779 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
780 InflateRect( &rc, -1, -1 );
783 /* completely skip the drawing if only focus has changed */
784 if (action == ODA_FOCUS) goto draw_focus;
786 uState = DFCS_BUTTONPUSH;
788 if (style & BS_FLAT)
789 uState |= DFCS_MONO;
790 else if (pushedState)
792 if (get_button_type(style) == BS_DEFPUSHBUTTON )
793 uState |= DFCS_FLAT;
794 else
795 uState |= DFCS_PUSHED;
798 if (state & (BST_CHECKED | BST_INDETERMINATE))
799 uState |= DFCS_CHECKED;
801 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
803 /* draw button label */
804 r = rc;
805 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
807 if (dtFlags == (UINT)-1L)
808 goto cleanup;
810 if (pushedState)
811 OffsetRect(&r, 1, 1);
813 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
815 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
817 SetTextColor( hDC, oldTxtColor );
819 draw_focus:
820 if (action == ODA_FOCUS || (state & BST_FOCUS))
822 InflateRect( &rc, -2, -2 );
823 DrawFocusRect( hDC, &rc );
826 cleanup:
827 SelectObject( hDC, hOldPen );
828 SelectObject( hDC, hOldBrush );
829 SetBkMode(hDC, oldBkMode);
830 SelectClipRgn( hDC, hrgn );
831 if (hrgn) DeleteObject( hrgn );
834 /**********************************************************************
835 * Check Box & Radio Button Functions
838 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
840 RECT rbox, rtext, client;
841 HBRUSH hBrush;
842 int delta;
843 UINT dtFlags;
844 HFONT hFont;
845 LONG state = get_button_state( hwnd );
846 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
847 HWND parent;
848 HRGN hrgn;
850 if (style & BS_PUSHLIKE)
852 PB_Paint( hwnd, hDC, action );
853 return;
856 GetClientRect(hwnd, &client);
857 rbox = rtext = client;
859 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
861 parent = GetParent(hwnd);
862 if (!parent) parent = hwnd;
863 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
864 (WPARAM)hDC, (LPARAM)hwnd);
865 if (!hBrush) /* did the app forget to call defwindowproc ? */
866 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
867 (WPARAM)hDC, (LPARAM)hwnd );
868 hrgn = set_control_clipping( hDC, &client );
870 if (style & BS_LEFTTEXT)
872 /* magic +4 is what CTL3D expects */
874 rtext.right -= checkBoxWidth + 4;
875 rbox.left = rbox.right - checkBoxWidth;
877 else
879 rtext.left += checkBoxWidth + 4;
880 rbox.right = checkBoxWidth;
883 /* Since WM_ERASEBKGND does nothing, first prepare background */
884 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
885 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
887 /* Draw label */
888 client = rtext;
889 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
891 /* Only adjust rbox when rtext is valid */
892 if (dtFlags != (UINT)-1L)
894 rbox.top = rtext.top;
895 rbox.bottom = rtext.bottom;
898 /* Draw the check-box bitmap */
899 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
901 UINT flags;
903 if ((get_button_type(style) == BS_RADIOBUTTON) ||
904 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
905 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
906 else flags = DFCS_BUTTONCHECK;
908 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
909 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
911 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
913 /* rbox must have the correct height */
914 delta = rbox.bottom - rbox.top - checkBoxHeight;
916 if (style & BS_TOP) {
917 if (delta > 0) {
918 rbox.bottom = rbox.top + checkBoxHeight;
919 } else {
920 rbox.top -= -delta/2 + 1;
921 rbox.bottom = rbox.top + checkBoxHeight;
923 } else if (style & BS_BOTTOM) {
924 if (delta > 0) {
925 rbox.top = rbox.bottom - checkBoxHeight;
926 } else {
927 rbox.bottom += -delta/2 + 1;
928 rbox.top = rbox.bottom - checkBoxHeight;
930 } else { /* Default */
931 if (delta > 0) {
932 int ofs = (delta / 2);
933 rbox.bottom -= ofs + 1;
934 rbox.top = rbox.bottom - checkBoxHeight;
935 } else if (delta < 0) {
936 int ofs = (-delta / 2);
937 rbox.top -= ofs + 1;
938 rbox.bottom = rbox.top + checkBoxHeight;
942 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
945 if (dtFlags == (UINT)-1L) /* Noting to draw */
946 return;
948 if (action == ODA_DRAWENTIRE)
949 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
951 /* ... and focus */
952 if (action == ODA_FOCUS || (state & BST_FOCUS))
954 rtext.left--;
955 rtext.right++;
956 IntersectRect(&rtext, &rtext, &client);
957 DrawFocusRect( hDC, &rtext );
959 SelectClipRgn( hDC, hrgn );
960 if (hrgn) DeleteObject( hrgn );
964 /**********************************************************************
965 * BUTTON_CheckAutoRadioButton
967 * hwnd is checked, uncheck every other auto radio button in group
969 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
971 HWND parent, sibling, start;
973 parent = GetParent(hwnd);
974 /* make sure that starting control is not disabled or invisible */
975 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
978 if (!sibling) break;
979 if ((hwnd != sibling) &&
980 ((GetWindowLongW( sibling, GWL_STYLE) & BS_TYPEMASK) == BS_AUTORADIOBUTTON))
981 SendMessageW( sibling, BM_SETCHECK, BST_UNCHECKED, 0 );
982 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
983 } while (sibling != start);
987 /**********************************************************************
988 * Group Box Functions
991 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
993 RECT rc, rcFrame;
994 HBRUSH hbr;
995 HFONT hFont;
996 UINT dtFlags;
997 TEXTMETRICW tm;
998 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
999 HWND parent;
1000 HRGN hrgn;
1002 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1003 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1004 parent = GetParent(hwnd);
1005 if (!parent) parent = hwnd;
1006 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1007 if (!hbr) /* did the app forget to call defwindowproc ? */
1008 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1009 (WPARAM)hDC, (LPARAM)hwnd);
1010 GetClientRect( hwnd, &rc);
1011 rcFrame = rc;
1012 hrgn = set_control_clipping( hDC, &rc );
1014 GetTextMetricsW (hDC, &tm);
1015 rcFrame.top += (tm.tmHeight / 2) - 1;
1016 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1018 InflateRect(&rc, -7, 1);
1019 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1021 if (dtFlags != (UINT)-1)
1023 /* Because buttons have CS_PARENTDC class style, there is a chance
1024 * that label will be drawn out of client rect.
1025 * But Windows doesn't clip label's rect, so do I.
1028 /* There is 1-pixel margin at the left, right, and bottom */
1029 rc.left--; rc.right++; rc.bottom++;
1030 FillRect(hDC, &rc, hbr);
1031 rc.left++; rc.right--; rc.bottom--;
1033 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1035 SelectClipRgn( hDC, hrgn );
1036 if (hrgn) DeleteObject( hrgn );
1040 /**********************************************************************
1041 * User Button Functions
1044 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1046 RECT rc;
1047 HBRUSH hBrush;
1048 HFONT hFont;
1049 LONG state = get_button_state( hwnd );
1050 HWND parent;
1052 GetClientRect( hwnd, &rc);
1054 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1056 parent = GetParent(hwnd);
1057 if (!parent) parent = hwnd;
1058 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1059 if (!hBrush) /* did the app forget to call defwindowproc ? */
1060 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1061 (WPARAM)hDC, (LPARAM)hwnd);
1063 FillRect( hDC, &rc, hBrush );
1064 if (action == ODA_FOCUS || (state & BST_FOCUS))
1065 DrawFocusRect( hDC, &rc );
1067 switch (action)
1069 case ODA_FOCUS:
1070 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1071 break;
1073 case ODA_SELECT:
1074 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1075 break;
1077 default:
1078 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1079 break;
1084 /**********************************************************************
1085 * Ownerdrawn Button Functions
1088 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1090 LONG state = get_button_state( hwnd );
1091 DRAWITEMSTRUCT dis;
1092 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1093 HWND parent;
1094 HFONT hFont, hPrevFont = 0;
1095 HRGN hrgn;
1097 dis.CtlType = ODT_BUTTON;
1098 dis.CtlID = id;
1099 dis.itemID = 0;
1100 dis.itemAction = action;
1101 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1102 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1103 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1104 dis.hwndItem = hwnd;
1105 dis.hDC = hDC;
1106 dis.itemData = 0;
1107 GetClientRect( hwnd, &dis.rcItem );
1109 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1110 parent = GetParent(hwnd);
1111 if (!parent) parent = hwnd;
1112 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1114 hrgn = set_control_clipping( hDC, &dis.rcItem );
1116 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1117 if (hPrevFont) SelectObject(hDC, hPrevFont);
1118 SelectClipRgn( hDC, hrgn );
1119 if (hrgn) DeleteObject( hrgn );