push 955563f995be8b0942dbb757ceb5b3a4c8ccafbf
[wine/hacks.git] / dlls / user32 / button.c
blob926563c98f22ce8660ca065a5607ea76fd8ae96b
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 static void setup_clipping( HWND hwnd, HDC hdc )
222 RECT rc;
224 GetClientRect( hwnd, &rc );
225 DPtoLP( hdc, (POINT *)&rc, 2 );
226 IntersectClipRect( hdc, rc.left, rc.top, rc.right, rc.bottom );
229 /***********************************************************************
230 * ButtonWndProc_common
232 static LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg,
233 WPARAM wParam, LPARAM lParam, BOOL unicode )
235 RECT rect;
236 POINT pt;
237 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
238 UINT btn_type = get_button_type( style );
239 LONG state;
240 HANDLE oldHbitmap;
242 pt.x = (short)LOWORD(lParam);
243 pt.y = (short)HIWORD(lParam);
245 switch (uMsg)
247 case WM_GETDLGCODE:
248 switch(btn_type)
250 case BS_USERBUTTON:
251 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
252 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
253 case BS_RADIOBUTTON:
254 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
255 case BS_GROUPBOX: return DLGC_STATIC;
256 default: return DLGC_BUTTON;
259 case WM_ENABLE:
260 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
261 break;
263 case WM_CREATE:
264 if (!hbitmapCheckBoxes)
266 BITMAP bmp;
267 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
268 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
269 checkBoxWidth = bmp.bmWidth / 4;
270 checkBoxHeight = bmp.bmHeight / 3;
272 if (btn_type >= MAX_BTN_TYPE)
273 return -1; /* abort */
274 set_button_state( hWnd, BUTTON_UNCHECKED );
275 return 0;
277 case WM_ERASEBKGND:
278 if (btn_type == BS_OWNERDRAW)
280 HDC hdc = (HDC)wParam;
281 RECT rc;
282 HBRUSH hBrush;
283 HWND parent = GetParent(hWnd);
284 if (!parent) parent = hWnd;
285 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
286 if (!hBrush) /* did the app forget to call defwindowproc ? */
287 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
288 (WPARAM)hdc, (LPARAM)hWnd);
289 GetClientRect(hWnd, &rc);
290 FillRect(hdc, &rc, hBrush);
292 return 1;
294 case WM_PRINTCLIENT:
295 case WM_PAINT:
296 if (btnPaintFunc[btn_type])
298 PAINTSTRUCT ps;
299 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
300 int nOldMode = SetBkMode( hdc, OPAQUE );
301 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
302 SetBkMode(hdc, nOldMode); /* reset painting mode */
303 if( !wParam ) EndPaint( hWnd, &ps );
305 break;
307 case WM_KEYDOWN:
308 if (wParam == VK_SPACE)
310 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
311 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
312 SetCapture( hWnd );
314 break;
316 case WM_LBUTTONDBLCLK:
317 if(style & BS_NOTIFY ||
318 btn_type == BS_RADIOBUTTON ||
319 btn_type == BS_USERBUTTON ||
320 btn_type == BS_OWNERDRAW)
322 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
323 break;
325 /* fall through */
326 case WM_LBUTTONDOWN:
327 SetCapture( hWnd );
328 SetFocus( hWnd );
329 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
330 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
331 break;
333 case WM_KEYUP:
334 if (wParam != VK_SPACE)
335 break;
336 /* fall through */
337 case WM_LBUTTONUP:
338 state = get_button_state( hWnd );
339 if (!(state & BUTTON_BTNPRESSED)) break;
340 state &= BUTTON_NSTATES;
341 set_button_state( hWnd, state );
342 if (!(state & BUTTON_HIGHLIGHTED))
344 ReleaseCapture();
345 break;
347 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
348 ReleaseCapture();
349 GetClientRect( hWnd, &rect );
350 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
352 state = get_button_state( hWnd );
353 switch(btn_type)
355 case BS_AUTOCHECKBOX:
356 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
357 break;
358 case BS_AUTORADIOBUTTON:
359 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
360 break;
361 case BS_AUTO3STATE:
362 SendMessageW( hWnd, BM_SETCHECK,
363 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
364 break;
366 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
368 break;
370 case WM_CAPTURECHANGED:
371 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
372 state = get_button_state( hWnd );
373 if (state & BUTTON_BTNPRESSED)
375 state &= BUTTON_NSTATES;
376 set_button_state( hWnd, state );
377 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
379 break;
381 case WM_MOUSEMOVE:
382 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
384 GetClientRect( hWnd, &rect );
385 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
387 break;
389 case WM_SETTEXT:
391 /* Clear an old text here as Windows does */
392 HDC hdc = GetDC(hWnd);
393 HBRUSH hbrush;
394 RECT client, rc;
395 HWND parent = GetParent(hWnd);
397 if (!parent) parent = hWnd;
398 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
399 (WPARAM)hdc, (LPARAM)hWnd);
400 if (!hbrush) /* did the app forget to call DefWindowProc ? */
401 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
402 (WPARAM)hdc, (LPARAM)hWnd);
404 GetClientRect(hWnd, &client);
405 rc = client;
406 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
407 /* Clip by client rect bounds */
408 if (rc.right > client.right) rc.right = client.right;
409 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
410 FillRect(hdc, &rc, hbrush);
411 ReleaseDC(hWnd, hdc);
413 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
414 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
415 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
416 InvalidateRect( hWnd, NULL, TRUE );
417 else
418 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
419 return 1; /* success. FIXME: check text length */
422 case WM_SETFONT:
423 set_button_font( hWnd, (HFONT)wParam );
424 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
425 break;
427 case WM_GETFONT:
428 return (LRESULT)get_button_font( hWnd );
430 case WM_SETFOCUS:
431 TRACE("WM_SETFOCUS %p\n",hWnd);
432 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
433 paint_button( hWnd, btn_type, ODA_FOCUS );
434 if (style & BS_NOTIFY)
435 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
436 break;
438 case WM_KILLFOCUS:
439 TRACE("WM_KILLFOCUS %p\n",hWnd);
440 state = get_button_state( hWnd );
441 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
442 paint_button( hWnd, btn_type, ODA_FOCUS );
444 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
445 ReleaseCapture();
446 if (style & BS_NOTIFY)
447 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
449 InvalidateRect( hWnd, NULL, FALSE );
450 break;
452 case WM_SYSCOLORCHANGE:
453 InvalidateRect( hWnd, NULL, FALSE );
454 break;
456 case BM_SETSTYLE16:
457 case BM_SETSTYLE:
458 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
459 btn_type = wParam & 0x0f;
460 style = (style & ~0x0f) | btn_type;
461 SetWindowLongW( hWnd, GWL_STYLE, style );
463 /* Only redraw if lParam flag is set.*/
464 if (lParam)
465 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
467 break;
469 case BM_CLICK:
470 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
471 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
472 break;
474 case BM_SETIMAGE:
475 /* Check that image format matches button style */
476 switch (style & (BS_BITMAP|BS_ICON))
478 case BS_BITMAP:
479 if (wParam != IMAGE_BITMAP) return 0;
480 break;
481 case BS_ICON:
482 if (wParam != IMAGE_ICON) return 0;
483 break;
484 default:
485 return 0;
487 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
488 InvalidateRect( hWnd, NULL, FALSE );
489 return (LRESULT)oldHbitmap;
491 case BM_GETIMAGE:
492 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
494 case BM_GETCHECK16:
495 case BM_GETCHECK:
496 return get_button_state( hWnd ) & 3;
498 case BM_SETCHECK16:
499 case BM_SETCHECK:
500 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
501 state = get_button_state( hWnd );
502 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
504 if (wParam) style |= WS_TABSTOP;
505 else style &= ~WS_TABSTOP;
506 SetWindowLongW( hWnd, GWL_STYLE, style );
508 if ((state & 3) != wParam)
510 set_button_state( hWnd, (state & ~3) | wParam );
511 paint_button( hWnd, btn_type, ODA_SELECT );
513 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
514 BUTTON_CheckAutoRadioButton( hWnd );
515 break;
517 case BM_GETSTATE16:
518 case BM_GETSTATE:
519 return get_button_state( hWnd );
521 case BM_SETSTATE16:
522 case BM_SETSTATE:
523 state = get_button_state( hWnd );
524 if (wParam)
526 if (state & BUTTON_HIGHLIGHTED) break;
527 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
529 else
531 if (!(state & BUTTON_HIGHLIGHTED)) break;
532 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
534 paint_button( hWnd, btn_type, ODA_SELECT );
535 break;
537 case WM_NCHITTEST:
538 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
539 /* fall through */
540 default:
541 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
542 DefWindowProcA(hWnd, uMsg, wParam, lParam);
544 return 0;
547 /***********************************************************************
548 * ButtonWndProcW
549 * The button window procedure. This is just a wrapper which locks
550 * the passed HWND and calls the real window procedure (with a WND*
551 * pointer pointing to the locked windowstructure).
553 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
555 if (!IsWindow( hWnd )) return 0;
556 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
560 /***********************************************************************
561 * ButtonWndProcA
563 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
565 if (!IsWindow( hWnd )) return 0;
566 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
570 /**********************************************************************
571 * Convert button styles to flags used by DrawText.
573 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
575 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
577 /* "Convert" pushlike buttons to pushbuttons */
578 if (style & BS_PUSHLIKE)
579 style &= ~0x0F;
581 if (!(style & BS_MULTILINE))
582 dtStyle |= DT_SINGLELINE;
583 else
584 dtStyle |= DT_WORDBREAK;
586 switch (style & BS_CENTER)
588 case BS_LEFT: /* DT_LEFT is 0 */ break;
589 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
590 case BS_CENTER: dtStyle |= DT_CENTER; break;
591 default:
592 /* Pushbutton's text is centered by default */
593 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
594 /* all other flavours have left aligned text */
597 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
599 /* DrawText ignores vertical alignment for multiline text,
600 * but we use these flags to align label manually.
602 if (get_button_type(style) != BS_GROUPBOX)
604 switch (style & BS_VCENTER)
606 case BS_TOP: /* DT_TOP is 0 */ break;
607 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
608 case BS_VCENTER: /* fall through */
609 default: dtStyle |= DT_VCENTER; break;
612 else
613 /* GroupBox's text is always single line and is top aligned. */
614 dtStyle |= DT_SINGLELINE;
616 return dtStyle;
619 /**********************************************************************
620 * BUTTON_CalcLabelRect
622 * Calculates label's rectangle depending on button style.
624 * Returns flags to be passed to DrawText.
625 * Calculated rectangle doesn't take into account button state
626 * (pushed, etc.). If there is nothing to draw (no text/image) output
627 * rectangle is empty, and return value is (UINT)-1.
629 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
631 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
632 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
633 WCHAR *text;
634 ICONINFO iconInfo;
635 BITMAP bm;
636 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
637 RECT r = *rc;
638 INT n;
640 /* Calculate label rectangle according to label type */
641 switch (style & (BS_ICON|BS_BITMAP))
643 case BS_TEXT:
644 if (!(text = get_button_text( hwnd ))) goto empty_rect;
645 if (!text[0])
647 HeapFree( GetProcessHeap(), 0, text );
648 goto empty_rect;
650 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
651 HeapFree( GetProcessHeap(), 0, text );
652 break;
654 case BS_ICON:
655 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
656 goto empty_rect;
658 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
660 r.right = r.left + bm.bmWidth;
661 r.bottom = r.top + bm.bmHeight;
663 DeleteObject(iconInfo.hbmColor);
664 DeleteObject(iconInfo.hbmMask);
665 break;
667 case BS_BITMAP:
668 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
669 goto empty_rect;
671 r.right = r.left + bm.bmWidth;
672 r.bottom = r.top + bm.bmHeight;
673 break;
675 default:
676 empty_rect:
677 rc->right = r.left;
678 rc->bottom = r.top;
679 return (UINT)-1;
682 /* Position label inside bounding rectangle according to
683 * alignment flags. (calculated rect is always left-top aligned).
684 * If label is aligned to any side - shift label in opposite
685 * direction to leave extra space for focus rectangle.
687 switch (dtStyle & (DT_CENTER|DT_RIGHT))
689 case DT_LEFT: r.left++; r.right++; break;
690 case DT_CENTER: n = r.right - r.left;
691 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
692 r.right = r.left + n; break;
693 case DT_RIGHT: n = r.right - r.left;
694 r.right = rc->right - 1;
695 r.left = r.right - n;
696 break;
699 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
701 case DT_TOP: r.top++; r.bottom++; break;
702 case DT_VCENTER: n = r.bottom - r.top;
703 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
704 r.bottom = r.top + n; break;
705 case DT_BOTTOM: n = r.bottom - r.top;
706 r.bottom = rc->bottom - 1;
707 r.top = r.bottom - n;
708 break;
711 *rc = r;
712 return dtStyle;
716 /**********************************************************************
717 * BUTTON_DrawTextCallback
719 * Callback function used by DrawStateW function.
721 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
723 RECT rc;
724 rc.left = 0;
725 rc.top = 0;
726 rc.right = cx;
727 rc.bottom = cy;
729 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
730 return TRUE;
734 /**********************************************************************
735 * BUTTON_DrawLabel
737 * Common function for drawing button label.
739 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
741 DRAWSTATEPROC lpOutputProc = NULL;
742 LPARAM lp;
743 WPARAM wp = 0;
744 HBRUSH hbr = 0;
745 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
746 LONG state = get_button_state( hwnd );
747 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
748 WCHAR *text = NULL;
750 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
751 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
752 * I don't have Win31 on hand to verify that, so I leave it as is.
755 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
757 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
758 flags |= DSS_MONO;
761 switch (style & (BS_ICON|BS_BITMAP))
763 case BS_TEXT:
764 /* DST_COMPLEX -- is 0 */
765 lpOutputProc = BUTTON_DrawTextCallback;
766 if (!(text = get_button_text( hwnd ))) return;
767 lp = (LPARAM)text;
768 wp = (WPARAM)dtFlags;
769 break;
771 case BS_ICON:
772 flags |= DST_ICON;
773 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
774 break;
776 case BS_BITMAP:
777 flags |= DST_BITMAP;
778 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
779 break;
781 default:
782 return;
785 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
786 rc->right - rc->left, rc->bottom - rc->top, flags);
787 HeapFree( GetProcessHeap(), 0, text );
790 /**********************************************************************
791 * Push Button Functions
793 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
795 RECT rc, focus_rect, r;
796 UINT dtFlags, uState;
797 HPEN hOldPen;
798 HBRUSH hOldBrush;
799 INT oldBkMode;
800 COLORREF oldTxtColor;
801 HFONT hFont;
802 LONG state = get_button_state( hwnd );
803 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
804 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
805 HWND parent;
807 GetClientRect( hwnd, &rc );
809 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
810 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
811 parent = GetParent(hwnd);
812 if (!parent) parent = hwnd;
813 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
815 setup_clipping( hwnd, hDC );
817 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
818 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
819 oldBkMode = SetBkMode(hDC, TRANSPARENT);
821 if (get_button_type(style) == BS_DEFPUSHBUTTON)
823 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
824 InflateRect( &rc, -1, -1 );
827 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
829 if (style & BS_FLAT)
830 uState |= DFCS_MONO;
831 else if (pushedState)
833 if (get_button_type(style) == BS_DEFPUSHBUTTON )
834 uState |= DFCS_FLAT;
835 else
836 uState |= DFCS_PUSHED;
839 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
840 uState |= DFCS_CHECKED;
842 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
844 focus_rect = rc;
846 /* draw button label */
847 r = rc;
848 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
850 if (dtFlags == (UINT)-1L)
851 goto cleanup;
853 if (pushedState)
854 OffsetRect(&r, 1, 1);
856 IntersectClipRect(hDC, rc.left, rc.top, rc.right, rc.bottom);
858 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
860 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
862 SetTextColor( hDC, oldTxtColor );
864 if (state & BUTTON_HASFOCUS)
866 InflateRect( &focus_rect, -1, -1 );
867 IntersectRect(&focus_rect, &focus_rect, &rc);
868 DrawFocusRect( hDC, &focus_rect );
871 cleanup:
872 SelectObject( hDC, hOldPen );
873 SelectObject( hDC, hOldBrush );
874 SetBkMode(hDC, oldBkMode);
877 /**********************************************************************
878 * Check Box & Radio Button Functions
881 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
883 RECT rbox, rtext, client;
884 HBRUSH hBrush;
885 int delta;
886 UINT dtFlags;
887 HFONT hFont;
888 LONG state = get_button_state( hwnd );
889 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
890 HWND parent;
892 if (style & BS_PUSHLIKE)
894 PB_Paint( hwnd, hDC, action );
895 return;
898 GetClientRect(hwnd, &client);
899 rbox = rtext = client;
901 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
903 parent = GetParent(hwnd);
904 if (!parent) parent = hwnd;
905 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
906 (WPARAM)hDC, (LPARAM)hwnd);
907 if (!hBrush) /* did the app forget to call defwindowproc ? */
908 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
909 (WPARAM)hDC, (LPARAM)hwnd );
910 setup_clipping( hwnd, hDC );
912 if (style & BS_LEFTTEXT)
914 /* magic +4 is what CTL3D expects */
916 rtext.right -= checkBoxWidth + 4;
917 rbox.left = rbox.right - checkBoxWidth;
919 else
921 rtext.left += checkBoxWidth + 4;
922 rbox.right = checkBoxWidth;
925 /* Since WM_ERASEBKGND does nothing, first prepare background */
926 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
927 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
929 /* Draw label */
930 client = rtext;
931 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
933 /* Only adjust rbox when rtext is valid */
934 if (dtFlags != (UINT)-1L)
936 rbox.top = rtext.top;
937 rbox.bottom = rtext.bottom;
940 /* Draw the check-box bitmap */
941 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
943 UINT flags;
945 if ((get_button_type(style) == BS_RADIOBUTTON) ||
946 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
947 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
948 else flags = DFCS_BUTTONCHECK;
950 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
951 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
953 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
955 /* rbox must have the correct height */
956 delta = rbox.bottom - rbox.top - checkBoxHeight;
958 if (style & BS_TOP) {
959 if (delta > 0) {
960 rbox.bottom = rbox.top + checkBoxHeight;
961 } else {
962 rbox.top -= -delta/2 + 1;
963 rbox.bottom = rbox.top + checkBoxHeight;
965 } else if (style & BS_BOTTOM) {
966 if (delta > 0) {
967 rbox.top = rbox.bottom - checkBoxHeight;
968 } else {
969 rbox.bottom += -delta/2 + 1;
970 rbox.top = rbox.bottom - checkBoxHeight;
972 } else { /* Default */
973 if (delta > 0) {
974 int ofs = (delta / 2);
975 rbox.bottom -= ofs + 1;
976 rbox.top = rbox.bottom - checkBoxHeight;
977 } else if (delta < 0) {
978 int ofs = (-delta / 2);
979 rbox.top -= ofs + 1;
980 rbox.bottom = rbox.top + checkBoxHeight;
984 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
987 if (dtFlags == (UINT)-1L) /* Noting to draw */
988 return;
990 if (action == ODA_DRAWENTIRE)
991 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
993 /* ... and focus */
994 if ((action == ODA_FOCUS) ||
995 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
997 rtext.left--;
998 rtext.right++;
999 IntersectRect(&rtext, &rtext, &client);
1000 DrawFocusRect( hDC, &rtext );
1005 /**********************************************************************
1006 * BUTTON_CheckAutoRadioButton
1008 * hwnd is checked, uncheck every other auto radio button in group
1010 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
1012 HWND parent, sibling, start;
1014 parent = GetParent(hwnd);
1015 /* make sure that starting control is not disabled or invisible */
1016 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1019 if (!sibling) break;
1020 if ((hwnd != sibling) &&
1021 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1022 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1023 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1024 } while (sibling != start);
1028 /**********************************************************************
1029 * Group Box Functions
1032 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1034 RECT rc, rcFrame;
1035 HBRUSH hbr;
1036 HFONT hFont;
1037 UINT dtFlags;
1038 TEXTMETRICW tm;
1039 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1040 HWND parent;
1042 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1043 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1044 parent = GetParent(hwnd);
1045 if (!parent) parent = hwnd;
1046 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1047 if (!hbr) /* did the app forget to call defwindowproc ? */
1048 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1049 (WPARAM)hDC, (LPARAM)hwnd);
1050 setup_clipping( hwnd, hDC );
1052 GetClientRect( hwnd, &rc);
1053 rcFrame = rc;
1055 GetTextMetricsW (hDC, &tm);
1056 rcFrame.top += (tm.tmHeight / 2) - 1;
1057 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1059 InflateRect(&rc, -7, 1);
1060 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1062 if (dtFlags == (UINT)-1L)
1063 return;
1065 /* Because buttons have CS_PARENTDC class style, there is a chance
1066 * that label will be drawn out of client rect.
1067 * But Windows doesn't clip label's rect, so do I.
1070 /* There is 1-pixel margin at the left, right, and bottom */
1071 rc.left--; rc.right++; rc.bottom++;
1072 FillRect(hDC, &rc, hbr);
1073 rc.left++; rc.right--; rc.bottom--;
1075 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1079 /**********************************************************************
1080 * User Button Functions
1083 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1085 RECT rc;
1086 HBRUSH hBrush;
1087 HFONT hFont;
1088 LONG state = get_button_state( hwnd );
1089 HWND parent;
1091 if (action == ODA_SELECT) return;
1093 GetClientRect( hwnd, &rc);
1095 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1097 parent = GetParent(hwnd);
1098 if (!parent) parent = hwnd;
1099 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1100 if (!hBrush) /* did the app forget to call defwindowproc ? */
1101 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1102 (WPARAM)hDC, (LPARAM)hwnd);
1104 FillRect( hDC, &rc, hBrush );
1105 if ((action == ODA_FOCUS) ||
1106 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1107 DrawFocusRect( hDC, &rc );
1111 /**********************************************************************
1112 * Ownerdrawn Button Functions
1115 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1117 LONG state = get_button_state( hwnd );
1118 DRAWITEMSTRUCT dis;
1119 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1120 HWND parent;
1121 HFONT hFont, hPrevFont = 0;
1123 dis.CtlType = ODT_BUTTON;
1124 dis.CtlID = id;
1125 dis.itemID = 0;
1126 dis.itemAction = action;
1127 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1128 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1129 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1130 dis.hwndItem = hwnd;
1131 dis.hDC = hDC;
1132 dis.itemData = 0;
1133 GetClientRect( hwnd, &dis.rcItem );
1135 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1136 parent = GetParent(hwnd);
1137 if (!parent) parent = hwnd;
1138 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1140 setup_clipping( hwnd, hDC );
1142 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1143 if (hPrevFont) SelectObject(hDC, hPrevFont);