push ad05fa8ea86b4a1581adad6c24ad723042d385d2
[wine/hacks.git] / dlls / user32 / button.c
blob46940f00c897f126d37db862d554d02fa99b2a85
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
34 * - BS_TYPEMASK
36 * Messages
37 * - WM_CHAR: Checks a (manual or automatic) check box on '+' or '=', clears it on '-' key.
38 * - WM_SETFOCUS: For (manual or automatic) radio buttons, send the parent window BN_CLICKED
39 * - WM_NCCREATE: Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
40 * - WM_SYSKEYUP
41 * - BCM_GETIDEALSIZE
42 * - BCM_GETIMAGELIST
43 * - BCM_GETTEXTMARGIN
44 * - BCM_SETIMAGELIST
45 * - BCM_SETTEXTMARGIN
47 * Notifications
48 * - BCN_HOTITEMCHANGE
49 * - BN_DISABLE
50 * - BN_PUSHED/BN_HILITE
51 * + BN_KILLFOCUS: is it OK?
52 * - BN_PAINT
53 * + BN_SETFOCUS: is it OK?
54 * - BN_UNPUSHED/BN_UNHILITE
55 * - NM_CUSTOMDRAW
57 * Structures/Macros/Definitions
58 * - BUTTON_IMAGELIST
59 * - NMBCHOTITEM
60 * - Button_GetIdealSize
61 * - Button_GetImageList
62 * - Button_GetTextMargin
63 * - Button_SetImageList
64 * - Button_SetTextMargin
67 #include <stdarg.h>
68 #include <string.h>
69 #include <stdlib.h>
71 #define OEMRESOURCE
73 #include "windef.h"
74 #include "winbase.h"
75 #include "wingdi.h"
76 #include "wine/winuser16.h"
77 #include "controls.h"
78 #include "user_private.h"
79 #include "wine/debug.h"
81 WINE_DEFAULT_DEBUG_CHANNEL(button);
83 /* GetWindowLong offsets for window extra information */
84 #define STATE_GWL_OFFSET 0
85 #define HFONT_GWL_OFFSET (sizeof(LONG))
86 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
87 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
89 /* Button state values */
90 #define BUTTON_UNCHECKED 0x00
91 #define BUTTON_CHECKED 0x01
92 #define BUTTON_3STATE 0x02
93 #define BUTTON_HIGHLIGHTED 0x04
94 #define BUTTON_HASFOCUS 0x08
95 #define BUTTON_NSTATES 0x0F
96 /* undocumented flags */
97 #define BUTTON_BTNPRESSED 0x40
98 #define BUTTON_UNKNOWN2 0x20
99 #define BUTTON_UNKNOWN3 0x10
101 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
102 do { /* Notify parent which has created this button control */ \
103 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
104 SendMessageW(GetParent(hWnd), WM_COMMAND, \
105 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
106 (LPARAM)(hWnd)); \
107 } while(0)
109 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
110 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
111 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
112 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
113 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
114 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
115 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
116 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
117 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
119 #define MAX_BTN_TYPE 12
121 static const WORD maxCheckState[MAX_BTN_TYPE] =
123 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
124 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
125 BUTTON_CHECKED, /* BS_CHECKBOX */
126 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
127 BUTTON_CHECKED, /* BS_RADIOBUTTON */
128 BUTTON_3STATE, /* BS_3STATE */
129 BUTTON_3STATE, /* BS_AUTO3STATE */
130 BUTTON_UNCHECKED, /* BS_GROUPBOX */
131 BUTTON_UNCHECKED, /* BS_USERBUTTON */
132 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
133 BUTTON_UNCHECKED, /* Not defined */
134 BUTTON_UNCHECKED /* BS_OWNERDRAW */
137 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
139 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
141 PB_Paint, /* BS_PUSHBUTTON */
142 PB_Paint, /* BS_DEFPUSHBUTTON */
143 CB_Paint, /* BS_CHECKBOX */
144 CB_Paint, /* BS_AUTOCHECKBOX */
145 CB_Paint, /* BS_RADIOBUTTON */
146 CB_Paint, /* BS_3STATE */
147 CB_Paint, /* BS_AUTO3STATE */
148 GB_Paint, /* BS_GROUPBOX */
149 UB_Paint, /* BS_USERBUTTON */
150 CB_Paint, /* BS_AUTORADIOBUTTON */
151 NULL, /* Not defined */
152 OB_Paint /* BS_OWNERDRAW */
155 static HBITMAP hbitmapCheckBoxes = 0;
156 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
159 /*********************************************************************
160 * button class descriptor
162 static const WCHAR buttonW[] = {'B','u','t','t','o','n',0};
163 const struct builtin_class_descr BUTTON_builtin_class =
165 buttonW, /* name */
166 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
167 ButtonWndProcA, /* procA */
168 ButtonWndProcW, /* procW */
169 NB_EXTRA_BYTES, /* extra */
170 IDC_ARROW, /* cursor */
171 0 /* brush */
175 static inline LONG get_button_state( HWND hwnd )
177 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
180 static inline void set_button_state( HWND hwnd, LONG state )
182 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
185 static inline HFONT get_button_font( HWND hwnd )
187 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
190 static inline void set_button_font( HWND hwnd, HFONT font )
192 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
195 static inline UINT get_button_type( LONG window_style )
197 return (window_style & 0x0f);
200 /* paint a button of any type */
201 static inline void paint_button( HWND hwnd, LONG style, UINT action )
203 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
205 HDC hdc = GetDC( hwnd );
206 btnPaintFunc[style]( hwnd, hdc, action );
207 ReleaseDC( hwnd, hdc );
211 /* retrieve the button text; returned buffer must be freed by caller */
212 static inline WCHAR *get_button_text( HWND hwnd )
214 INT len = 512;
215 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
216 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
217 return buffer;
220 /***********************************************************************
221 * ButtonWndProc_common
223 static LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
224 WPARAM wParam, LPARAM lParam, BOOL unicode )
226 RECT rect;
227 POINT pt;
228 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
229 UINT btn_type = get_button_type( style );
230 LONG state;
231 HANDLE oldHbitmap;
233 pt.x = (short)LOWORD(lParam);
234 pt.y = (short)HIWORD(lParam);
236 switch (uMsg)
238 case WM_GETDLGCODE:
239 switch(btn_type)
241 case BS_USERBUTTON:
242 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
243 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
244 case BS_RADIOBUTTON:
245 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
246 case BS_GROUPBOX: return DLGC_STATIC;
247 default: return DLGC_BUTTON;
250 case WM_ENABLE:
251 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
252 break;
254 case WM_CREATE:
255 if (!hbitmapCheckBoxes)
257 BITMAP bmp;
258 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
259 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
260 checkBoxWidth = bmp.bmWidth / 4;
261 checkBoxHeight = bmp.bmHeight / 3;
263 if (btn_type >= MAX_BTN_TYPE)
264 return -1; /* abort */
265 set_button_state( hWnd, BUTTON_UNCHECKED );
266 return 0;
268 case WM_ERASEBKGND:
269 if (btn_type == BS_OWNERDRAW)
271 HDC hdc = (HDC)wParam;
272 RECT rc;
273 HBRUSH hBrush;
274 HWND parent = GetParent(hWnd);
275 if (!parent) parent = hWnd;
276 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
277 if (!hBrush) /* did the app forget to call defwindowproc ? */
278 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
279 (WPARAM)hdc, (LPARAM)hWnd);
280 GetClientRect(hWnd, &rc);
281 FillRect(hdc, &rc, hBrush);
283 return 1;
285 case WM_PRINTCLIENT:
286 case WM_PAINT:
287 if (btnPaintFunc[btn_type])
289 PAINTSTRUCT ps;
290 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
291 int nOldMode = SetBkMode( hdc, OPAQUE );
292 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
293 SetBkMode(hdc, nOldMode); /* reset painting mode */
294 if( !wParam ) EndPaint( hWnd, &ps );
296 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 );
304 break;
306 case WM_LBUTTONDBLCLK:
307 if(style & BS_NOTIFY ||
308 btn_type == BS_RADIOBUTTON ||
309 btn_type == BS_USERBUTTON ||
310 btn_type == BS_OWNERDRAW)
312 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
313 break;
315 /* fall through */
316 case WM_LBUTTONDOWN:
317 SetCapture( hWnd );
318 SetFocus( hWnd );
319 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
320 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
321 break;
323 case WM_KEYUP:
324 if (wParam != VK_SPACE)
325 break;
326 /* fall through */
327 case WM_LBUTTONUP:
328 state = get_button_state( hWnd );
329 if (!(state & BUTTON_BTNPRESSED)) break;
330 state &= BUTTON_NSTATES;
331 set_button_state( hWnd, state );
332 if (!(state & BUTTON_HIGHLIGHTED))
334 ReleaseCapture();
335 break;
337 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
338 ReleaseCapture();
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 & BUTTON_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 & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
354 break;
356 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
358 break;
360 case WM_CAPTURECHANGED:
361 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
362 state = get_button_state( hWnd );
363 if (state & BUTTON_BTNPRESSED)
365 state &= BUTTON_NSTATES;
366 set_button_state( hWnd, state );
367 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
369 break;
371 case WM_MOUSEMOVE:
372 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
374 GetClientRect( hWnd, &rect );
375 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
377 break;
379 case WM_SETTEXT:
381 /* Clear an old text here as Windows does */
382 HDC hdc = GetDC(hWnd);
383 HBRUSH hbrush;
384 RECT client, rc;
385 HWND parent = GetParent(hWnd);
387 if (!parent) parent = hWnd;
388 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
389 (WPARAM)hdc, (LPARAM)hWnd);
390 if (!hbrush) /* did the app forget to call DefWindowProc ? */
391 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
392 (WPARAM)hdc, (LPARAM)hWnd);
394 GetClientRect(hWnd, &client);
395 rc = client;
396 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
397 /* Clip by client rect bounds */
398 if (rc.right > client.right) rc.right = client.right;
399 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
400 FillRect(hdc, &rc, hbrush);
401 ReleaseDC(hWnd, hdc);
403 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
404 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
405 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
406 InvalidateRect( hWnd, NULL, TRUE );
407 else
408 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
409 return 1; /* success. FIXME: check text length */
412 case WM_SETFONT:
413 set_button_font( hWnd, (HFONT)wParam );
414 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
415 break;
417 case WM_GETFONT:
418 return (LRESULT)get_button_font( hWnd );
420 case WM_SETFOCUS:
421 TRACE("WM_SETFOCUS %p\n",hWnd);
422 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
423 paint_button( hWnd, btn_type, ODA_FOCUS );
424 if (style & BS_NOTIFY)
425 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
426 break;
428 case WM_KILLFOCUS:
429 TRACE("WM_KILLFOCUS %p\n",hWnd);
430 state = get_button_state( hWnd );
431 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
432 paint_button( hWnd, btn_type, ODA_FOCUS );
434 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
435 ReleaseCapture();
436 if (style & BS_NOTIFY)
437 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
439 break;
441 case WM_SYSCOLORCHANGE:
442 InvalidateRect( hWnd, NULL, FALSE );
443 break;
445 case BM_SETSTYLE16:
446 case BM_SETSTYLE:
447 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
448 btn_type = wParam & 0x0f;
449 style = (style & ~0x0f) | btn_type;
450 SetWindowLongW( hWnd, GWL_STYLE, style );
452 /* Only redraw if lParam flag is set.*/
453 if (lParam)
454 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
456 break;
458 case BM_CLICK:
459 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
460 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
461 break;
463 case BM_SETIMAGE:
464 /* Check that image format matches button style */
465 switch (style & (BS_BITMAP|BS_ICON))
467 case BS_BITMAP:
468 if (wParam != IMAGE_BITMAP) return 0;
469 break;
470 case BS_ICON:
471 if (wParam != IMAGE_ICON) return 0;
472 break;
473 default:
474 return 0;
476 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
477 InvalidateRect( hWnd, NULL, FALSE );
478 return (LRESULT)oldHbitmap;
480 case BM_GETIMAGE:
481 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
483 case BM_GETCHECK16:
484 case BM_GETCHECK:
485 return get_button_state( hWnd ) & 3;
487 case BM_SETCHECK16:
488 case BM_SETCHECK:
489 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
490 state = get_button_state( hWnd );
491 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
493 if (wParam) style |= WS_TABSTOP;
494 else style &= ~WS_TABSTOP;
495 SetWindowLongW( hWnd, GWL_STYLE, style );
497 if ((state & 3) != wParam)
499 set_button_state( hWnd, (state & ~3) | wParam );
500 paint_button( hWnd, btn_type, ODA_SELECT );
502 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
503 BUTTON_CheckAutoRadioButton( hWnd );
504 break;
506 case BM_GETSTATE16:
507 case BM_GETSTATE:
508 return get_button_state( hWnd );
510 case BM_SETSTATE16:
511 case BM_SETSTATE:
512 state = get_button_state( hWnd );
513 if (wParam)
515 if (state & BUTTON_HIGHLIGHTED) break;
516 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
518 else
520 if (!(state & BUTTON_HIGHLIGHTED)) break;
521 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
523 paint_button( hWnd, btn_type, ODA_SELECT );
524 break;
526 case WM_NCHITTEST:
527 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
528 /* fall through */
529 default:
530 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
531 DefWindowProcA(hWnd, uMsg, wParam, lParam);
533 return 0;
536 /***********************************************************************
537 * ButtonWndProcW
538 * The button window procedure. This is just a wrapper which locks
539 * the passed HWND and calls the real window procedure (with a WND*
540 * pointer pointing to the locked windowstructure).
542 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
544 if (!IsWindow( hWnd )) return 0;
545 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
549 /***********************************************************************
550 * ButtonWndProcA
552 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
554 if (!IsWindow( hWnd )) return 0;
555 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
559 /**********************************************************************
560 * Convert button styles to flags used by DrawText.
561 * TODO: handle WS_EX_RIGHT extended style.
563 static UINT BUTTON_BStoDT(DWORD style)
565 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
567 /* "Convert" pushlike buttons to pushbuttons */
568 if (style & BS_PUSHLIKE)
569 style &= ~0x0F;
571 if (!(style & BS_MULTILINE))
572 dtStyle |= DT_SINGLELINE;
573 else
574 dtStyle |= DT_WORDBREAK;
576 switch (style & BS_CENTER)
578 case BS_LEFT: /* DT_LEFT is 0 */ break;
579 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
580 case BS_CENTER: dtStyle |= DT_CENTER; break;
581 default:
582 /* Pushbutton's text is centered by default */
583 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
584 /* all other flavours have left aligned text */
587 /* DrawText ignores vertical alignment for multiline text,
588 * but we use these flags to align label manually.
590 if (get_button_type(style) != BS_GROUPBOX)
592 switch (style & BS_VCENTER)
594 case BS_TOP: /* DT_TOP is 0 */ break;
595 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
596 case BS_VCENTER: /* fall through */
597 default: dtStyle |= DT_VCENTER; break;
600 else
601 /* GroupBox's text is always single line and is top aligned. */
602 dtStyle |= DT_SINGLELINE;
604 return dtStyle;
607 /**********************************************************************
608 * BUTTON_CalcLabelRect
610 * Calculates label's rectangle depending on button style.
612 * Returns flags to be passed to DrawText.
613 * Calculated rectangle doesn't take into account button state
614 * (pushed, etc.). If there is nothing to draw (no text/image) output
615 * rectangle is empty, and return value is (UINT)-1.
617 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
619 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
620 WCHAR *text;
621 ICONINFO iconInfo;
622 BITMAP bm;
623 UINT dtStyle = BUTTON_BStoDT(style);
624 RECT r = *rc;
625 INT n;
627 /* Calculate label rectangle according to label type */
628 switch (style & (BS_ICON|BS_BITMAP))
630 case BS_TEXT:
631 if (!(text = get_button_text( hwnd ))) goto empty_rect;
632 if (!text[0])
634 HeapFree( GetProcessHeap(), 0, text );
635 goto empty_rect;
637 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
638 HeapFree( GetProcessHeap(), 0, text );
639 break;
641 case BS_ICON:
642 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
643 goto empty_rect;
645 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
647 r.right = r.left + bm.bmWidth;
648 r.bottom = r.top + bm.bmHeight;
650 DeleteObject(iconInfo.hbmColor);
651 DeleteObject(iconInfo.hbmMask);
652 break;
654 case BS_BITMAP:
655 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
656 goto empty_rect;
658 r.right = r.left + bm.bmWidth;
659 r.bottom = r.top + bm.bmHeight;
660 break;
662 default:
663 empty_rect:
664 rc->right = r.left;
665 rc->bottom = r.top;
666 return (UINT)(LONG)-1;
669 /* Position label inside bounding rectangle according to
670 * alignment flags. (calculated rect is always left-top aligned).
671 * If label is aligned to any side - shift label in opposite
672 * direction to leave extra space for focus rectangle.
674 switch (dtStyle & (DT_CENTER|DT_RIGHT))
676 case DT_LEFT: r.left++; r.right++; break;
677 case DT_CENTER: n = r.right - r.left;
678 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
679 r.right = r.left + n; break;
680 case DT_RIGHT: n = r.right - r.left;
681 r.right = rc->right - 1;
682 r.left = r.right - n;
683 break;
686 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
688 case DT_TOP: r.top++; r.bottom++; break;
689 case DT_VCENTER: n = r.bottom - r.top;
690 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
691 r.bottom = r.top + n; break;
692 case DT_BOTTOM: n = r.bottom - r.top;
693 r.bottom = rc->bottom - 1;
694 r.top = r.bottom - n;
695 break;
698 *rc = r;
699 return dtStyle;
703 /**********************************************************************
704 * BUTTON_DrawTextCallback
706 * Callback function used by DrawStateW function.
708 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
710 RECT rc;
711 rc.left = 0;
712 rc.top = 0;
713 rc.right = cx;
714 rc.bottom = cy;
716 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
717 return TRUE;
721 /**********************************************************************
722 * BUTTON_DrawLabel
724 * Common function for drawing button label.
726 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
728 DRAWSTATEPROC lpOutputProc = NULL;
729 LPARAM lp;
730 WPARAM wp = 0;
731 HBRUSH hbr = 0;
732 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
733 LONG state = get_button_state( hwnd );
734 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
735 WCHAR *text = NULL;
737 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
738 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
739 * I don't have Win31 on hand to verify that, so I leave it as is.
742 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
744 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
745 flags |= DSS_MONO;
748 switch (style & (BS_ICON|BS_BITMAP))
750 case BS_TEXT:
751 /* DST_COMPLEX -- is 0 */
752 lpOutputProc = BUTTON_DrawTextCallback;
753 if (!(text = get_button_text( hwnd ))) return;
754 lp = (LPARAM)text;
755 wp = (WPARAM)dtFlags;
756 break;
758 case BS_ICON:
759 flags |= DST_ICON;
760 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
761 break;
763 case BS_BITMAP:
764 flags |= DST_BITMAP;
765 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
766 break;
768 default:
769 return;
772 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
773 rc->right - rc->left, rc->bottom - rc->top, flags);
774 HeapFree( GetProcessHeap(), 0, text );
777 /**********************************************************************
778 * Push Button Functions
780 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
782 RECT rc, focus_rect, r;
783 UINT dtFlags, uState;
784 HPEN hOldPen;
785 HBRUSH hOldBrush;
786 INT oldBkMode;
787 COLORREF oldTxtColor;
788 HFONT hFont;
789 LONG state = get_button_state( hwnd );
790 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
791 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
792 HWND parent;
794 GetClientRect( hwnd, &rc );
796 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
797 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
798 parent = GetParent(hwnd);
799 if (!parent) parent = hwnd;
800 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
801 hOldPen = (HPEN)SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
802 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
803 oldBkMode = SetBkMode(hDC, TRANSPARENT);
805 if (get_button_type(style) == BS_DEFPUSHBUTTON)
807 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
808 InflateRect( &rc, -1, -1 );
811 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
813 if (style & BS_FLAT)
814 uState |= DFCS_MONO;
815 else if (pushedState)
817 if (get_button_type(style) == BS_DEFPUSHBUTTON )
818 uState |= DFCS_FLAT;
819 else
820 uState |= DFCS_PUSHED;
823 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
824 uState |= DFCS_CHECKED;
826 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
828 focus_rect = rc;
830 /* draw button label */
831 r = rc;
832 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
834 if (dtFlags == (UINT)-1L)
835 goto cleanup;
837 if (pushedState)
838 OffsetRect(&r, 1, 1);
840 IntersectClipRect(hDC, rc.left, rc.top, rc.right, rc.bottom);
842 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
844 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
846 SetTextColor( hDC, oldTxtColor );
848 if (state & BUTTON_HASFOCUS)
850 InflateRect( &focus_rect, -1, -1 );
851 IntersectRect(&focus_rect, &focus_rect, &rc);
852 DrawFocusRect( hDC, &focus_rect );
855 cleanup:
856 SelectObject( hDC, hOldPen );
857 SelectObject( hDC, hOldBrush );
858 SetBkMode(hDC, oldBkMode);
861 /**********************************************************************
862 * Check Box & Radio Button Functions
865 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
867 RECT rbox, rtext, client;
868 HBRUSH hBrush;
869 int delta;
870 UINT dtFlags;
871 HFONT hFont;
872 LONG state = get_button_state( hwnd );
873 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
874 HWND parent;
876 if (style & BS_PUSHLIKE)
878 PB_Paint( hwnd, hDC, action );
879 return;
882 GetClientRect(hwnd, &client);
883 rbox = rtext = client;
885 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
887 parent = GetParent(hwnd);
888 if (!parent) parent = hwnd;
889 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
890 (WPARAM)hDC, (LPARAM)hwnd);
891 if (!hBrush) /* did the app forget to call defwindowproc ? */
892 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
893 (WPARAM)hDC, (LPARAM)hwnd );
895 if (style & BS_LEFTTEXT)
897 /* magic +4 is what CTL3D expects */
899 rtext.right -= checkBoxWidth + 4;
900 rbox.left = rbox.right - checkBoxWidth;
902 else
904 rtext.left += checkBoxWidth + 4;
905 rbox.right = checkBoxWidth;
908 /* Since WM_ERASEBKGND does nothing, first prepare background */
909 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
910 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
912 /* Draw label */
913 client = rtext;
914 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
916 /* Only adjust rbox when rtext is valid */
917 if (dtFlags != (UINT)-1L)
919 rbox.top = rtext.top;
920 rbox.bottom = rtext.bottom;
923 /* Draw the check-box bitmap */
924 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
926 UINT flags;
928 if ((get_button_type(style) == BS_RADIOBUTTON) ||
929 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
930 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
931 else flags = DFCS_BUTTONCHECK;
933 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
934 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
936 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
938 /* rbox must have the correct height */
939 delta = rbox.bottom - rbox.top - checkBoxHeight;
941 if (style & BS_TOP) {
942 if (delta > 0) {
943 rbox.bottom = rbox.top + checkBoxHeight;
944 } else {
945 rbox.top -= -delta/2 + 1;
946 rbox.bottom = rbox.top + checkBoxHeight;
948 } else if (style & BS_BOTTOM) {
949 if (delta > 0) {
950 rbox.top = rbox.bottom - checkBoxHeight;
951 } else {
952 rbox.bottom += -delta/2 + 1;
953 rbox.top = rbox.bottom - checkBoxHeight;
955 } else { /* Default */
956 if (delta > 0) {
957 int ofs = (delta / 2);
958 rbox.bottom -= ofs + 1;
959 rbox.top = rbox.bottom - checkBoxHeight;
960 } else if (delta < 0) {
961 int ofs = (-delta / 2);
962 rbox.top -= ofs + 1;
963 rbox.bottom = rbox.top + checkBoxHeight;
967 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
970 if (dtFlags == (UINT)-1L) /* Noting to draw */
971 return;
973 IntersectClipRect(hDC, client.left, client.top, client.right, client.bottom);
975 if (action == ODA_DRAWENTIRE)
976 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
978 /* ... and focus */
979 if ((action == ODA_FOCUS) ||
980 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
982 rtext.left--;
983 rtext.right++;
984 IntersectRect(&rtext, &rtext, &client);
985 DrawFocusRect( hDC, &rtext );
990 /**********************************************************************
991 * BUTTON_CheckAutoRadioButton
993 * hwnd is checked, uncheck every other auto radio button in group
995 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
997 HWND parent, sibling, start;
999 parent = GetParent(hwnd);
1000 /* make sure that starting control is not disabled or invisible */
1001 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1004 if (!sibling) break;
1005 if ((hwnd != sibling) &&
1006 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1007 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1008 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1009 } while (sibling != start);
1013 /**********************************************************************
1014 * Group Box Functions
1017 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1019 RECT rc, rcFrame;
1020 HBRUSH hbr;
1021 HFONT hFont;
1022 UINT dtFlags;
1023 TEXTMETRICW tm;
1024 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1025 HWND parent;
1027 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1028 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1029 parent = GetParent(hwnd);
1030 if (!parent) parent = hwnd;
1031 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1032 if (!hbr) /* did the app forget to call defwindowproc ? */
1033 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1034 (WPARAM)hDC, (LPARAM)hwnd);
1036 GetClientRect( hwnd, &rc);
1037 rcFrame = rc;
1039 GetTextMetricsW (hDC, &tm);
1040 rcFrame.top += (tm.tmHeight / 2) - 1;
1041 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1043 InflateRect(&rc, -7, 1);
1044 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1046 if (dtFlags == (UINT)-1L)
1047 return;
1049 /* Because buttons have CS_PARENTDC class style, there is a chance
1050 * that label will be drawn out of client rect.
1051 * But Windows doesn't clip label's rect, so do I.
1054 /* There is 1-pixel marging at the left, right, and bottom */
1055 rc.left--; rc.right++; rc.bottom++;
1056 FillRect(hDC, &rc, hbr);
1057 rc.left++; rc.right--; rc.bottom--;
1059 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1063 /**********************************************************************
1064 * User Button Functions
1067 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1069 RECT rc;
1070 HBRUSH hBrush;
1071 HFONT hFont;
1072 LONG state = get_button_state( hwnd );
1073 HWND parent;
1075 if (action == ODA_SELECT) return;
1077 GetClientRect( hwnd, &rc);
1079 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1081 parent = GetParent(hwnd);
1082 if (!parent) parent = hwnd;
1083 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1084 if (!hBrush) /* did the app forget to call defwindowproc ? */
1085 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1086 (WPARAM)hDC, (LPARAM)hwnd);
1088 FillRect( hDC, &rc, hBrush );
1089 if ((action == ODA_FOCUS) ||
1090 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1091 DrawFocusRect( hDC, &rc );
1095 /**********************************************************************
1096 * Ownerdrawn Button Functions
1099 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1101 LONG state = get_button_state( hwnd );
1102 DRAWITEMSTRUCT dis;
1103 HRGN clipRegion;
1104 RECT clipRect;
1105 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1106 HWND parent;
1107 HFONT hFont, hPrevFont = 0;
1109 dis.CtlType = ODT_BUTTON;
1110 dis.CtlID = id;
1111 dis.itemID = 0;
1112 dis.itemAction = action;
1113 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1114 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1115 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1116 dis.hwndItem = hwnd;
1117 dis.hDC = hDC;
1118 dis.itemData = 0;
1119 GetClientRect( hwnd, &dis.rcItem );
1121 clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1122 if (GetClipRgn(hDC, clipRegion) != 1)
1124 DeleteObject(clipRegion);
1125 clipRegion=NULL;
1127 clipRect = dis.rcItem;
1128 DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1129 IntersectClipRect(hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
1131 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1132 parent = GetParent(hwnd);
1133 if (!parent) parent = hwnd;
1134 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1135 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1136 if (hPrevFont) SelectObject(hDC, hPrevFont);
1137 SelectClipRgn(hDC, clipRegion);