wined3d: Use the FBO cache in stretch_rect_fbo().
[wine/hramrach.git] / dlls / user32 / button.c
blob0aefe1b51815549f30778eb925fa8a320d7a748b
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 "controls.h"
77 #include "win.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 );
117 #define MAX_BTN_TYPE 12
119 static const WORD maxCheckState[MAX_BTN_TYPE] =
121 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
122 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
123 BUTTON_CHECKED, /* BS_CHECKBOX */
124 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
125 BUTTON_CHECKED, /* BS_RADIOBUTTON */
126 BUTTON_3STATE, /* BS_3STATE */
127 BUTTON_3STATE, /* BS_AUTO3STATE */
128 BUTTON_UNCHECKED, /* BS_GROUPBOX */
129 BUTTON_UNCHECKED, /* BS_USERBUTTON */
130 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
131 BUTTON_UNCHECKED, /* Not defined */
132 BUTTON_UNCHECKED /* BS_OWNERDRAW */
135 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
137 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
139 PB_Paint, /* BS_PUSHBUTTON */
140 PB_Paint, /* BS_DEFPUSHBUTTON */
141 CB_Paint, /* BS_CHECKBOX */
142 CB_Paint, /* BS_AUTOCHECKBOX */
143 CB_Paint, /* BS_RADIOBUTTON */
144 CB_Paint, /* BS_3STATE */
145 CB_Paint, /* BS_AUTO3STATE */
146 GB_Paint, /* BS_GROUPBOX */
147 UB_Paint, /* BS_USERBUTTON */
148 CB_Paint, /* BS_AUTORADIOBUTTON */
149 NULL, /* Not defined */
150 OB_Paint /* BS_OWNERDRAW */
153 static HBITMAP hbitmapCheckBoxes = 0;
154 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
157 /*********************************************************************
158 * button class descriptor
160 static const WCHAR buttonW[] = {'B','u','t','t','o','n',0};
161 const struct builtin_class_descr BUTTON_builtin_class =
163 buttonW, /* name */
164 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
165 WINPROC_BUTTON, /* proc */
166 NB_EXTRA_BYTES, /* extra */
167 IDC_ARROW, /* cursor */
168 0 /* brush */
172 static inline LONG get_button_state( HWND hwnd )
174 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
177 static inline void set_button_state( HWND hwnd, LONG state )
179 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
182 static inline HFONT get_button_font( HWND hwnd )
184 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
187 static inline void set_button_font( HWND hwnd, HFONT font )
189 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
192 static inline UINT get_button_type( LONG window_style )
194 return (window_style & 0x0f);
197 /* paint a button of any type */
198 static inline void paint_button( HWND hwnd, LONG style, UINT action )
200 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
202 HDC hdc = GetDC( hwnd );
203 btnPaintFunc[style]( hwnd, hdc, action );
204 ReleaseDC( hwnd, hdc );
208 /* retrieve the button text; returned buffer must be freed by caller */
209 static inline WCHAR *get_button_text( HWND hwnd )
211 INT len = 512;
212 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
213 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
214 return buffer;
217 static void setup_clipping( HWND hwnd, HDC hdc )
219 RECT rc;
221 GetClientRect( hwnd, &rc );
222 DPtoLP( hdc, (POINT *)&rc, 2 );
223 IntersectClipRect( hdc, rc.left, rc.top, rc.right, rc.bottom );
226 /***********************************************************************
227 * ButtonWndProc_common
229 LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL unicode )
231 RECT rect;
232 POINT pt;
233 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
234 UINT btn_type = get_button_type( style );
235 LONG state;
236 HANDLE oldHbitmap;
238 if (!IsWindow( hWnd )) return 0;
240 pt.x = (short)LOWORD(lParam);
241 pt.y = (short)HIWORD(lParam);
243 switch (uMsg)
245 case WM_GETDLGCODE:
246 switch(btn_type)
248 case BS_USERBUTTON:
249 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
250 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
251 case BS_RADIOBUTTON:
252 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
253 case BS_GROUPBOX: return DLGC_STATIC;
254 default: return DLGC_BUTTON;
257 case WM_ENABLE:
258 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
259 break;
261 case WM_CREATE:
262 if (!hbitmapCheckBoxes)
264 BITMAP bmp;
265 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
266 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
267 checkBoxWidth = bmp.bmWidth / 4;
268 checkBoxHeight = bmp.bmHeight / 3;
270 if (btn_type >= MAX_BTN_TYPE)
271 return -1; /* abort */
273 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
274 if (btn_type == BS_USERBUTTON )
276 style = (style & ~0x0f) | BS_PUSHBUTTON;
277 WIN_SetStyle( hWnd, style, 0x0f & ~style );
279 set_button_state( hWnd, BUTTON_UNCHECKED );
280 return 0;
282 case WM_ERASEBKGND:
283 if (btn_type == BS_OWNERDRAW)
285 HDC hdc = (HDC)wParam;
286 RECT rc;
287 HBRUSH hBrush;
288 HWND parent = GetParent(hWnd);
289 if (!parent) parent = hWnd;
290 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
291 if (!hBrush) /* did the app forget to call defwindowproc ? */
292 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
293 (WPARAM)hdc, (LPARAM)hWnd);
294 GetClientRect(hWnd, &rc);
295 FillRect(hdc, &rc, hBrush);
297 return 1;
299 case WM_PRINTCLIENT:
300 case WM_PAINT:
301 if (btnPaintFunc[btn_type])
303 PAINTSTRUCT ps;
304 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
305 int nOldMode = SetBkMode( hdc, OPAQUE );
306 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
307 SetBkMode(hdc, nOldMode); /* reset painting mode */
308 if( !wParam ) EndPaint( hWnd, &ps );
310 break;
312 case WM_KEYDOWN:
313 if (wParam == VK_SPACE)
315 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
316 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
317 SetCapture( hWnd );
319 break;
321 case WM_LBUTTONDBLCLK:
322 if(style & BS_NOTIFY ||
323 btn_type == BS_RADIOBUTTON ||
324 btn_type == BS_USERBUTTON ||
325 btn_type == BS_OWNERDRAW)
327 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
328 break;
330 /* fall through */
331 case WM_LBUTTONDOWN:
332 SetCapture( hWnd );
333 SetFocus( hWnd );
334 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
335 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
336 break;
338 case WM_KEYUP:
339 if (wParam != VK_SPACE)
340 break;
341 /* fall through */
342 case WM_LBUTTONUP:
343 state = get_button_state( hWnd );
344 if (!(state & BUTTON_BTNPRESSED)) break;
345 state &= BUTTON_NSTATES;
346 set_button_state( hWnd, state );
347 if (!(state & BUTTON_HIGHLIGHTED))
349 ReleaseCapture();
350 break;
352 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
353 ReleaseCapture();
354 GetClientRect( hWnd, &rect );
355 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
357 state = get_button_state( hWnd );
358 switch(btn_type)
360 case BS_AUTOCHECKBOX:
361 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
362 break;
363 case BS_AUTORADIOBUTTON:
364 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
365 break;
366 case BS_AUTO3STATE:
367 SendMessageW( hWnd, BM_SETCHECK,
368 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
369 break;
371 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
373 break;
375 case WM_CAPTURECHANGED:
376 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
377 state = get_button_state( hWnd );
378 if (state & BUTTON_BTNPRESSED)
380 state &= BUTTON_NSTATES;
381 set_button_state( hWnd, state );
382 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
384 break;
386 case WM_MOUSEMOVE:
387 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
389 GetClientRect( hWnd, &rect );
390 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
392 break;
394 case WM_SETTEXT:
396 /* Clear an old text here as Windows does */
397 HDC hdc = GetDC(hWnd);
398 HBRUSH hbrush;
399 RECT client, rc;
400 HWND parent = GetParent(hWnd);
402 if (!parent) parent = hWnd;
403 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
404 (WPARAM)hdc, (LPARAM)hWnd);
405 if (!hbrush) /* did the app forget to call DefWindowProc ? */
406 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
407 (WPARAM)hdc, (LPARAM)hWnd);
409 GetClientRect(hWnd, &client);
410 rc = client;
411 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
412 /* Clip by client rect bounds */
413 if (rc.right > client.right) rc.right = client.right;
414 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
415 FillRect(hdc, &rc, hbrush);
416 ReleaseDC(hWnd, hdc);
418 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
419 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
420 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
421 InvalidateRect( hWnd, NULL, TRUE );
422 else
423 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
424 return 1; /* success. FIXME: check text length */
427 case WM_SETFONT:
428 set_button_font( hWnd, (HFONT)wParam );
429 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
430 break;
432 case WM_GETFONT:
433 return (LRESULT)get_button_font( hWnd );
435 case WM_SETFOCUS:
436 TRACE("WM_SETFOCUS %p\n",hWnd);
437 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
438 paint_button( hWnd, btn_type, ODA_FOCUS );
439 if (style & BS_NOTIFY)
440 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
441 break;
443 case WM_KILLFOCUS:
444 TRACE("WM_KILLFOCUS %p\n",hWnd);
445 state = get_button_state( hWnd );
446 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
447 paint_button( hWnd, btn_type, ODA_FOCUS );
449 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
450 ReleaseCapture();
451 if (style & BS_NOTIFY)
452 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
454 InvalidateRect( hWnd, NULL, FALSE );
455 break;
457 case WM_SYSCOLORCHANGE:
458 InvalidateRect( hWnd, NULL, FALSE );
459 break;
461 case BM_SETSTYLE:
462 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
463 btn_type = wParam & 0x0f;
464 style = (style & ~0x0f) | btn_type;
465 WIN_SetStyle( hWnd, style, 0x0f & ~style );
467 /* Only redraw if lParam flag is set.*/
468 if (lParam)
469 InvalidateRect( hWnd, NULL, TRUE );
471 break;
473 case BM_CLICK:
474 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
475 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
476 break;
478 case BM_SETIMAGE:
479 /* Check that image format matches button style */
480 switch (style & (BS_BITMAP|BS_ICON))
482 case BS_BITMAP:
483 if (wParam != IMAGE_BITMAP) return 0;
484 break;
485 case BS_ICON:
486 if (wParam != IMAGE_ICON) return 0;
487 break;
488 default:
489 return 0;
491 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
492 InvalidateRect( hWnd, NULL, FALSE );
493 return (LRESULT)oldHbitmap;
495 case BM_GETIMAGE:
496 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
498 case BM_GETCHECK:
499 return get_button_state( hWnd ) & 3;
501 case BM_SETCHECK:
502 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
503 state = get_button_state( hWnd );
504 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
506 if (wParam) style |= WS_TABSTOP;
507 else style &= ~WS_TABSTOP;
508 SetWindowLongW( hWnd, GWL_STYLE, style );
510 if ((state & 3) != wParam)
512 set_button_state( hWnd, (state & ~3) | wParam );
513 paint_button( hWnd, btn_type, ODA_SELECT );
515 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
516 BUTTON_CheckAutoRadioButton( hWnd );
517 break;
519 case BM_GETSTATE:
520 return get_button_state( hWnd );
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 * Convert button styles to flags used by DrawText.
550 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
552 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
554 /* "Convert" pushlike buttons to pushbuttons */
555 if (style & BS_PUSHLIKE)
556 style &= ~0x0F;
558 if (!(style & BS_MULTILINE))
559 dtStyle |= DT_SINGLELINE;
560 else
561 dtStyle |= DT_WORDBREAK;
563 switch (style & BS_CENTER)
565 case BS_LEFT: /* DT_LEFT is 0 */ break;
566 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
567 case BS_CENTER: dtStyle |= DT_CENTER; break;
568 default:
569 /* Pushbutton's text is centered by default */
570 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
571 /* all other flavours have left aligned text */
574 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
576 /* DrawText ignores vertical alignment for multiline text,
577 * but we use these flags to align label manually.
579 if (get_button_type(style) != BS_GROUPBOX)
581 switch (style & BS_VCENTER)
583 case BS_TOP: /* DT_TOP is 0 */ break;
584 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
585 case BS_VCENTER: /* fall through */
586 default: dtStyle |= DT_VCENTER; break;
589 else
590 /* GroupBox's text is always single line and is top aligned. */
591 dtStyle |= DT_SINGLELINE;
593 return dtStyle;
596 /**********************************************************************
597 * BUTTON_CalcLabelRect
599 * Calculates label's rectangle depending on button style.
601 * Returns flags to be passed to DrawText.
602 * Calculated rectangle doesn't take into account button state
603 * (pushed, etc.). If there is nothing to draw (no text/image) output
604 * rectangle is empty, and return value is (UINT)-1.
606 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
608 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
609 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
610 WCHAR *text;
611 ICONINFO iconInfo;
612 BITMAP bm;
613 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
614 RECT r = *rc;
615 INT n;
617 /* Calculate label rectangle according to label type */
618 switch (style & (BS_ICON|BS_BITMAP))
620 case BS_TEXT:
621 if (!(text = get_button_text( hwnd ))) goto empty_rect;
622 if (!text[0])
624 HeapFree( GetProcessHeap(), 0, text );
625 goto empty_rect;
627 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
628 HeapFree( GetProcessHeap(), 0, text );
629 break;
631 case BS_ICON:
632 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
633 goto empty_rect;
635 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
637 r.right = r.left + bm.bmWidth;
638 r.bottom = r.top + bm.bmHeight;
640 DeleteObject(iconInfo.hbmColor);
641 DeleteObject(iconInfo.hbmMask);
642 break;
644 case BS_BITMAP:
645 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
646 goto empty_rect;
648 r.right = r.left + bm.bmWidth;
649 r.bottom = r.top + bm.bmHeight;
650 break;
652 default:
653 empty_rect:
654 rc->right = r.left;
655 rc->bottom = r.top;
656 return (UINT)-1;
659 /* Position label inside bounding rectangle according to
660 * alignment flags. (calculated rect is always left-top aligned).
661 * If label is aligned to any side - shift label in opposite
662 * direction to leave extra space for focus rectangle.
664 switch (dtStyle & (DT_CENTER|DT_RIGHT))
666 case DT_LEFT: r.left++; r.right++; break;
667 case DT_CENTER: n = r.right - r.left;
668 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
669 r.right = r.left + n; break;
670 case DT_RIGHT: n = r.right - r.left;
671 r.right = rc->right - 1;
672 r.left = r.right - n;
673 break;
676 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
678 case DT_TOP: r.top++; r.bottom++; break;
679 case DT_VCENTER: n = r.bottom - r.top;
680 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
681 r.bottom = r.top + n; break;
682 case DT_BOTTOM: n = r.bottom - r.top;
683 r.bottom = rc->bottom - 1;
684 r.top = r.bottom - n;
685 break;
688 *rc = r;
689 return dtStyle;
693 /**********************************************************************
694 * BUTTON_DrawTextCallback
696 * Callback function used by DrawStateW function.
698 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
700 RECT rc;
701 rc.left = 0;
702 rc.top = 0;
703 rc.right = cx;
704 rc.bottom = cy;
706 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
707 return TRUE;
711 /**********************************************************************
712 * BUTTON_DrawLabel
714 * Common function for drawing button label.
716 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
718 DRAWSTATEPROC lpOutputProc = NULL;
719 LPARAM lp;
720 WPARAM wp = 0;
721 HBRUSH hbr = 0;
722 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
723 LONG state = get_button_state( hwnd );
724 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
725 WCHAR *text = NULL;
727 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
728 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
729 * I don't have Win31 on hand to verify that, so I leave it as is.
732 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
734 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
735 flags |= DSS_MONO;
738 switch (style & (BS_ICON|BS_BITMAP))
740 case BS_TEXT:
741 /* DST_COMPLEX -- is 0 */
742 lpOutputProc = BUTTON_DrawTextCallback;
743 if (!(text = get_button_text( hwnd ))) return;
744 lp = (LPARAM)text;
745 wp = dtFlags;
746 break;
748 case BS_ICON:
749 flags |= DST_ICON;
750 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
751 break;
753 case BS_BITMAP:
754 flags |= DST_BITMAP;
755 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
756 break;
758 default:
759 return;
762 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
763 rc->right - rc->left, rc->bottom - rc->top, flags);
764 HeapFree( GetProcessHeap(), 0, text );
767 /**********************************************************************
768 * Push Button Functions
770 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
772 RECT rc, r;
773 UINT dtFlags, uState;
774 HPEN hOldPen;
775 HBRUSH hOldBrush;
776 INT oldBkMode;
777 COLORREF oldTxtColor;
778 HFONT hFont;
779 LONG state = get_button_state( hwnd );
780 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
781 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
782 HWND parent;
784 GetClientRect( hwnd, &rc );
786 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
787 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
788 parent = GetParent(hwnd);
789 if (!parent) parent = hwnd;
790 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
792 setup_clipping( hwnd, hDC );
794 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
795 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
796 oldBkMode = SetBkMode(hDC, TRANSPARENT);
798 if (get_button_type(style) == BS_DEFPUSHBUTTON)
800 if (action != ODA_FOCUS)
801 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
802 InflateRect( &rc, -1, -1 );
805 /* completely skip the drawing if only focus has changed */
806 if (action == ODA_FOCUS) goto draw_focus;
808 uState = DFCS_BUTTONPUSH;
810 if (style & BS_FLAT)
811 uState |= DFCS_MONO;
812 else if (pushedState)
814 if (get_button_type(style) == BS_DEFPUSHBUTTON )
815 uState |= DFCS_FLAT;
816 else
817 uState |= DFCS_PUSHED;
820 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
821 uState |= DFCS_CHECKED;
823 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
825 /* draw button label */
826 r = rc;
827 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
829 if (dtFlags == (UINT)-1L)
830 goto cleanup;
832 if (pushedState)
833 OffsetRect(&r, 1, 1);
835 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
837 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
839 SetTextColor( hDC, oldTxtColor );
841 draw_focus:
842 if ((action == ODA_FOCUS) ||
843 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
845 InflateRect( &rc, -2, -2 );
846 DrawFocusRect( hDC, &rc );
849 cleanup:
850 SelectObject( hDC, hOldPen );
851 SelectObject( hDC, hOldBrush );
852 SetBkMode(hDC, oldBkMode);
855 /**********************************************************************
856 * Check Box & Radio Button Functions
859 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
861 RECT rbox, rtext, client;
862 HBRUSH hBrush;
863 int delta;
864 UINT dtFlags;
865 HFONT hFont;
866 LONG state = get_button_state( hwnd );
867 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
868 HWND parent;
870 if (style & BS_PUSHLIKE)
872 PB_Paint( hwnd, hDC, action );
873 return;
876 GetClientRect(hwnd, &client);
877 rbox = rtext = client;
879 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
881 parent = GetParent(hwnd);
882 if (!parent) parent = hwnd;
883 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
884 (WPARAM)hDC, (LPARAM)hwnd);
885 if (!hBrush) /* did the app forget to call defwindowproc ? */
886 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
887 (WPARAM)hDC, (LPARAM)hwnd );
888 setup_clipping( hwnd, hDC );
890 if (style & BS_LEFTTEXT)
892 /* magic +4 is what CTL3D expects */
894 rtext.right -= checkBoxWidth + 4;
895 rbox.left = rbox.right - checkBoxWidth;
897 else
899 rtext.left += checkBoxWidth + 4;
900 rbox.right = checkBoxWidth;
903 /* Since WM_ERASEBKGND does nothing, first prepare background */
904 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
905 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
907 /* Draw label */
908 client = rtext;
909 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
911 /* Only adjust rbox when rtext is valid */
912 if (dtFlags != (UINT)-1L)
914 rbox.top = rtext.top;
915 rbox.bottom = rtext.bottom;
918 /* Draw the check-box bitmap */
919 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
921 UINT flags;
923 if ((get_button_type(style) == BS_RADIOBUTTON) ||
924 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
925 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
926 else flags = DFCS_BUTTONCHECK;
928 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
929 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
931 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
933 /* rbox must have the correct height */
934 delta = rbox.bottom - rbox.top - checkBoxHeight;
936 if (style & BS_TOP) {
937 if (delta > 0) {
938 rbox.bottom = rbox.top + checkBoxHeight;
939 } else {
940 rbox.top -= -delta/2 + 1;
941 rbox.bottom = rbox.top + checkBoxHeight;
943 } else if (style & BS_BOTTOM) {
944 if (delta > 0) {
945 rbox.top = rbox.bottom - checkBoxHeight;
946 } else {
947 rbox.bottom += -delta/2 + 1;
948 rbox.top = rbox.bottom - checkBoxHeight;
950 } else { /* Default */
951 if (delta > 0) {
952 int ofs = (delta / 2);
953 rbox.bottom -= ofs + 1;
954 rbox.top = rbox.bottom - checkBoxHeight;
955 } else if (delta < 0) {
956 int ofs = (-delta / 2);
957 rbox.top -= ofs + 1;
958 rbox.bottom = rbox.top + checkBoxHeight;
962 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
965 if (dtFlags == (UINT)-1L) /* Noting to draw */
966 return;
968 if (action == ODA_DRAWENTIRE)
969 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
971 /* ... and focus */
972 if ((action == ODA_FOCUS) ||
973 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
975 rtext.left--;
976 rtext.right++;
977 IntersectRect(&rtext, &rtext, &client);
978 DrawFocusRect( hDC, &rtext );
983 /**********************************************************************
984 * BUTTON_CheckAutoRadioButton
986 * hwnd is checked, uncheck every other auto radio button in group
988 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
990 HWND parent, sibling, start;
992 parent = GetParent(hwnd);
993 /* make sure that starting control is not disabled or invisible */
994 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
997 if (!sibling) break;
998 if ((hwnd != sibling) &&
999 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1000 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1001 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1002 } while (sibling != start);
1006 /**********************************************************************
1007 * Group Box Functions
1010 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1012 RECT rc, rcFrame;
1013 HBRUSH hbr;
1014 HFONT hFont;
1015 UINT dtFlags;
1016 TEXTMETRICW tm;
1017 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1018 HWND parent;
1020 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1021 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1022 parent = GetParent(hwnd);
1023 if (!parent) parent = hwnd;
1024 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1025 if (!hbr) /* did the app forget to call defwindowproc ? */
1026 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1027 (WPARAM)hDC, (LPARAM)hwnd);
1028 setup_clipping( hwnd, hDC );
1030 GetClientRect( hwnd, &rc);
1031 rcFrame = rc;
1033 GetTextMetricsW (hDC, &tm);
1034 rcFrame.top += (tm.tmHeight / 2) - 1;
1035 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1037 InflateRect(&rc, -7, 1);
1038 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1040 if (dtFlags == (UINT)-1L)
1041 return;
1043 /* Because buttons have CS_PARENTDC class style, there is a chance
1044 * that label will be drawn out of client rect.
1045 * But Windows doesn't clip label's rect, so do I.
1048 /* There is 1-pixel margin at the left, right, and bottom */
1049 rc.left--; rc.right++; rc.bottom++;
1050 FillRect(hDC, &rc, hbr);
1051 rc.left++; rc.right--; rc.bottom--;
1053 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1057 /**********************************************************************
1058 * User Button Functions
1061 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1063 RECT rc;
1064 HBRUSH hBrush;
1065 HFONT hFont;
1066 LONG state = get_button_state( hwnd );
1067 HWND parent;
1069 if (action == ODA_SELECT) return;
1071 GetClientRect( hwnd, &rc);
1073 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1075 parent = GetParent(hwnd);
1076 if (!parent) parent = hwnd;
1077 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1078 if (!hBrush) /* did the app forget to call defwindowproc ? */
1079 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1080 (WPARAM)hDC, (LPARAM)hwnd);
1082 FillRect( hDC, &rc, hBrush );
1083 if ((action == ODA_FOCUS) ||
1084 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1085 DrawFocusRect( hDC, &rc );
1087 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1091 /**********************************************************************
1092 * Ownerdrawn Button Functions
1095 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1097 LONG state = get_button_state( hwnd );
1098 DRAWITEMSTRUCT dis;
1099 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1100 HWND parent;
1101 HFONT hFont, hPrevFont = 0;
1103 dis.CtlType = ODT_BUTTON;
1104 dis.CtlID = id;
1105 dis.itemID = 0;
1106 dis.itemAction = action;
1107 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1108 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1109 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1110 dis.hwndItem = hwnd;
1111 dis.hDC = hDC;
1112 dis.itemData = 0;
1113 GetClientRect( hwnd, &dis.rcItem );
1115 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1116 parent = GetParent(hwnd);
1117 if (!parent) parent = hwnd;
1118 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1120 setup_clipping( hwnd, hDC );
1122 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1123 if (hPrevFont) SelectObject(hDC, hPrevFont);