wineboot: Update Portuguese (Brazilian) translation.
[wine/multimedia.git] / dlls / user32 / button.c
blob54d54dc6810573ff715b1642a9312d545208bafd
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) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
507 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
509 if ((state & 3) != wParam)
511 set_button_state( hWnd, (state & ~3) | wParam );
512 paint_button( hWnd, btn_type, ODA_SELECT );
514 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
515 BUTTON_CheckAutoRadioButton( hWnd );
516 break;
518 case BM_GETSTATE:
519 return get_button_state( hWnd );
521 case BM_SETSTATE:
522 state = get_button_state( hWnd );
523 if (wParam)
524 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
525 else
526 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
528 paint_button( hWnd, btn_type, ODA_SELECT );
529 break;
531 case WM_NCHITTEST:
532 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
533 /* fall through */
534 default:
535 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
536 DefWindowProcA(hWnd, uMsg, wParam, lParam);
538 return 0;
541 /**********************************************************************
542 * Convert button styles to flags used by DrawText.
544 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
546 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
548 /* "Convert" pushlike buttons to pushbuttons */
549 if (style & BS_PUSHLIKE)
550 style &= ~0x0F;
552 if (!(style & BS_MULTILINE))
553 dtStyle |= DT_SINGLELINE;
554 else
555 dtStyle |= DT_WORDBREAK;
557 switch (style & BS_CENTER)
559 case BS_LEFT: /* DT_LEFT is 0 */ break;
560 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
561 case BS_CENTER: dtStyle |= DT_CENTER; break;
562 default:
563 /* Pushbutton's text is centered by default */
564 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
565 /* all other flavours have left aligned text */
568 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
570 /* DrawText ignores vertical alignment for multiline text,
571 * but we use these flags to align label manually.
573 if (get_button_type(style) != BS_GROUPBOX)
575 switch (style & BS_VCENTER)
577 case BS_TOP: /* DT_TOP is 0 */ break;
578 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
579 case BS_VCENTER: /* fall through */
580 default: dtStyle |= DT_VCENTER; break;
583 else
584 /* GroupBox's text is always single line and is top aligned. */
585 dtStyle |= DT_SINGLELINE;
587 return dtStyle;
590 /**********************************************************************
591 * BUTTON_CalcLabelRect
593 * Calculates label's rectangle depending on button style.
595 * Returns flags to be passed to DrawText.
596 * Calculated rectangle doesn't take into account button state
597 * (pushed, etc.). If there is nothing to draw (no text/image) output
598 * rectangle is empty, and return value is (UINT)-1.
600 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
602 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
603 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
604 WCHAR *text;
605 ICONINFO iconInfo;
606 BITMAP bm;
607 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
608 RECT r = *rc;
609 INT n;
611 /* Calculate label rectangle according to label type */
612 switch (style & (BS_ICON|BS_BITMAP))
614 case BS_TEXT:
615 if (!(text = get_button_text( hwnd ))) goto empty_rect;
616 if (!text[0])
618 HeapFree( GetProcessHeap(), 0, text );
619 goto empty_rect;
621 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
622 HeapFree( GetProcessHeap(), 0, text );
623 break;
625 case BS_ICON:
626 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
627 goto empty_rect;
629 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
631 r.right = r.left + bm.bmWidth;
632 r.bottom = r.top + bm.bmHeight;
634 DeleteObject(iconInfo.hbmColor);
635 DeleteObject(iconInfo.hbmMask);
636 break;
638 case BS_BITMAP:
639 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
640 goto empty_rect;
642 r.right = r.left + bm.bmWidth;
643 r.bottom = r.top + bm.bmHeight;
644 break;
646 default:
647 empty_rect:
648 rc->right = r.left;
649 rc->bottom = r.top;
650 return (UINT)-1;
653 /* Position label inside bounding rectangle according to
654 * alignment flags. (calculated rect is always left-top aligned).
655 * If label is aligned to any side - shift label in opposite
656 * direction to leave extra space for focus rectangle.
658 switch (dtStyle & (DT_CENTER|DT_RIGHT))
660 case DT_LEFT: r.left++; r.right++; break;
661 case DT_CENTER: n = r.right - r.left;
662 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
663 r.right = r.left + n; break;
664 case DT_RIGHT: n = r.right - r.left;
665 r.right = rc->right - 1;
666 r.left = r.right - n;
667 break;
670 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
672 case DT_TOP: r.top++; r.bottom++; break;
673 case DT_VCENTER: n = r.bottom - r.top;
674 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
675 r.bottom = r.top + n; break;
676 case DT_BOTTOM: n = r.bottom - r.top;
677 r.bottom = rc->bottom - 1;
678 r.top = r.bottom - n;
679 break;
682 *rc = r;
683 return dtStyle;
687 /**********************************************************************
688 * BUTTON_DrawTextCallback
690 * Callback function used by DrawStateW function.
692 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
694 RECT rc;
695 rc.left = 0;
696 rc.top = 0;
697 rc.right = cx;
698 rc.bottom = cy;
700 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
701 return TRUE;
705 /**********************************************************************
706 * BUTTON_DrawLabel
708 * Common function for drawing button label.
710 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
712 DRAWSTATEPROC lpOutputProc = NULL;
713 LPARAM lp;
714 WPARAM wp = 0;
715 HBRUSH hbr = 0;
716 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
717 LONG state = get_button_state( hwnd );
718 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
719 WCHAR *text = NULL;
721 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
722 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
723 * I don't have Win31 on hand to verify that, so I leave it as is.
726 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
728 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
729 flags |= DSS_MONO;
732 switch (style & (BS_ICON|BS_BITMAP))
734 case BS_TEXT:
735 /* DST_COMPLEX -- is 0 */
736 lpOutputProc = BUTTON_DrawTextCallback;
737 if (!(text = get_button_text( hwnd ))) return;
738 lp = (LPARAM)text;
739 wp = dtFlags;
740 break;
742 case BS_ICON:
743 flags |= DST_ICON;
744 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
745 break;
747 case BS_BITMAP:
748 flags |= DST_BITMAP;
749 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
750 break;
752 default:
753 return;
756 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
757 rc->right - rc->left, rc->bottom - rc->top, flags);
758 HeapFree( GetProcessHeap(), 0, text );
761 /**********************************************************************
762 * Push Button Functions
764 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
766 RECT rc, r;
767 UINT dtFlags, uState;
768 HPEN hOldPen;
769 HBRUSH hOldBrush;
770 INT oldBkMode;
771 COLORREF oldTxtColor;
772 HFONT hFont;
773 LONG state = get_button_state( hwnd );
774 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
775 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
776 HWND parent;
778 GetClientRect( hwnd, &rc );
780 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
781 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
782 parent = GetParent(hwnd);
783 if (!parent) parent = hwnd;
784 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
786 setup_clipping( hwnd, hDC );
788 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
789 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
790 oldBkMode = SetBkMode(hDC, TRANSPARENT);
792 if (get_button_type(style) == BS_DEFPUSHBUTTON)
794 if (action != ODA_FOCUS)
795 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
796 InflateRect( &rc, -1, -1 );
799 /* completely skip the drawing if only focus has changed */
800 if (action == ODA_FOCUS) goto draw_focus;
802 uState = DFCS_BUTTONPUSH;
804 if (style & BS_FLAT)
805 uState |= DFCS_MONO;
806 else if (pushedState)
808 if (get_button_type(style) == BS_DEFPUSHBUTTON )
809 uState |= DFCS_FLAT;
810 else
811 uState |= DFCS_PUSHED;
814 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
815 uState |= DFCS_CHECKED;
817 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
819 /* draw button label */
820 r = rc;
821 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
823 if (dtFlags == (UINT)-1L)
824 goto cleanup;
826 if (pushedState)
827 OffsetRect(&r, 1, 1);
829 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
831 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
833 SetTextColor( hDC, oldTxtColor );
835 draw_focus:
836 if (action == ODA_FOCUS || (state & BUTTON_HASFOCUS))
838 InflateRect( &rc, -2, -2 );
839 DrawFocusRect( hDC, &rc );
842 cleanup:
843 SelectObject( hDC, hOldPen );
844 SelectObject( hDC, hOldBrush );
845 SetBkMode(hDC, oldBkMode);
848 /**********************************************************************
849 * Check Box & Radio Button Functions
852 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
854 RECT rbox, rtext, client;
855 HBRUSH hBrush;
856 int delta;
857 UINT dtFlags;
858 HFONT hFont;
859 LONG state = get_button_state( hwnd );
860 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
861 HWND parent;
863 if (style & BS_PUSHLIKE)
865 PB_Paint( hwnd, hDC, action );
866 return;
869 GetClientRect(hwnd, &client);
870 rbox = rtext = client;
872 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
874 parent = GetParent(hwnd);
875 if (!parent) parent = hwnd;
876 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
877 (WPARAM)hDC, (LPARAM)hwnd);
878 if (!hBrush) /* did the app forget to call defwindowproc ? */
879 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
880 (WPARAM)hDC, (LPARAM)hwnd );
881 setup_clipping( hwnd, hDC );
883 if (style & BS_LEFTTEXT)
885 /* magic +4 is what CTL3D expects */
887 rtext.right -= checkBoxWidth + 4;
888 rbox.left = rbox.right - checkBoxWidth;
890 else
892 rtext.left += checkBoxWidth + 4;
893 rbox.right = checkBoxWidth;
896 /* Since WM_ERASEBKGND does nothing, first prepare background */
897 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
898 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
900 /* Draw label */
901 client = rtext;
902 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
904 /* Only adjust rbox when rtext is valid */
905 if (dtFlags != (UINT)-1L)
907 rbox.top = rtext.top;
908 rbox.bottom = rtext.bottom;
911 /* Draw the check-box bitmap */
912 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
914 UINT flags;
916 if ((get_button_type(style) == BS_RADIOBUTTON) ||
917 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
918 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
919 else flags = DFCS_BUTTONCHECK;
921 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
922 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
924 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
926 /* rbox must have the correct height */
927 delta = rbox.bottom - rbox.top - checkBoxHeight;
929 if (style & BS_TOP) {
930 if (delta > 0) {
931 rbox.bottom = rbox.top + checkBoxHeight;
932 } else {
933 rbox.top -= -delta/2 + 1;
934 rbox.bottom = rbox.top + checkBoxHeight;
936 } else if (style & BS_BOTTOM) {
937 if (delta > 0) {
938 rbox.top = rbox.bottom - checkBoxHeight;
939 } else {
940 rbox.bottom += -delta/2 + 1;
941 rbox.top = rbox.bottom - checkBoxHeight;
943 } else { /* Default */
944 if (delta > 0) {
945 int ofs = (delta / 2);
946 rbox.bottom -= ofs + 1;
947 rbox.top = rbox.bottom - checkBoxHeight;
948 } else if (delta < 0) {
949 int ofs = (-delta / 2);
950 rbox.top -= ofs + 1;
951 rbox.bottom = rbox.top + checkBoxHeight;
955 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
958 if (dtFlags == (UINT)-1L) /* Noting to draw */
959 return;
961 if (action == ODA_DRAWENTIRE)
962 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
964 /* ... and focus */
965 if (action == ODA_FOCUS || (state & BUTTON_HASFOCUS))
967 rtext.left--;
968 rtext.right++;
969 IntersectRect(&rtext, &rtext, &client);
970 DrawFocusRect( hDC, &rtext );
975 /**********************************************************************
976 * BUTTON_CheckAutoRadioButton
978 * hwnd is checked, uncheck every other auto radio button in group
980 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
982 HWND parent, sibling, start;
984 parent = GetParent(hwnd);
985 /* make sure that starting control is not disabled or invisible */
986 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
989 if (!sibling) break;
990 if ((hwnd != sibling) &&
991 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
992 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
993 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
994 } while (sibling != start);
998 /**********************************************************************
999 * Group Box Functions
1002 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1004 RECT rc, rcFrame;
1005 HBRUSH hbr;
1006 HFONT hFont;
1007 UINT dtFlags;
1008 TEXTMETRICW tm;
1009 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1010 HWND parent;
1012 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1013 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1014 parent = GetParent(hwnd);
1015 if (!parent) parent = hwnd;
1016 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1017 if (!hbr) /* did the app forget to call defwindowproc ? */
1018 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1019 (WPARAM)hDC, (LPARAM)hwnd);
1020 setup_clipping( hwnd, hDC );
1022 GetClientRect( hwnd, &rc);
1023 rcFrame = rc;
1025 GetTextMetricsW (hDC, &tm);
1026 rcFrame.top += (tm.tmHeight / 2) - 1;
1027 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1029 InflateRect(&rc, -7, 1);
1030 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1032 if (dtFlags == (UINT)-1L)
1033 return;
1035 /* Because buttons have CS_PARENTDC class style, there is a chance
1036 * that label will be drawn out of client rect.
1037 * But Windows doesn't clip label's rect, so do I.
1040 /* There is 1-pixel margin at the left, right, and bottom */
1041 rc.left--; rc.right++; rc.bottom++;
1042 FillRect(hDC, &rc, hbr);
1043 rc.left++; rc.right--; rc.bottom--;
1045 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1049 /**********************************************************************
1050 * User Button Functions
1053 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1055 RECT rc;
1056 HBRUSH hBrush;
1057 HFONT hFont;
1058 LONG state = get_button_state( hwnd );
1059 HWND parent;
1061 GetClientRect( hwnd, &rc);
1063 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1065 parent = GetParent(hwnd);
1066 if (!parent) parent = hwnd;
1067 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1068 if (!hBrush) /* did the app forget to call defwindowproc ? */
1069 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1070 (WPARAM)hDC, (LPARAM)hwnd);
1072 FillRect( hDC, &rc, hBrush );
1073 if (action == ODA_FOCUS || (state & BUTTON_HASFOCUS))
1074 DrawFocusRect( hDC, &rc );
1076 switch (action)
1078 case ODA_FOCUS:
1079 BUTTON_NOTIFY_PARENT( hwnd, (state & BUTTON_HASFOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1080 break;
1082 case ODA_SELECT:
1083 BUTTON_NOTIFY_PARENT( hwnd, (state & BUTTON_HIGHLIGHTED) ? BN_HILITE : BN_UNHILITE );
1084 break;
1086 default:
1087 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1088 break;
1093 /**********************************************************************
1094 * Ownerdrawn Button Functions
1097 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1099 LONG state = get_button_state( hwnd );
1100 DRAWITEMSTRUCT dis;
1101 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1102 HWND parent;
1103 HFONT hFont, hPrevFont = 0;
1105 dis.CtlType = ODT_BUTTON;
1106 dis.CtlID = id;
1107 dis.itemID = 0;
1108 dis.itemAction = action;
1109 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1110 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1111 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1112 dis.hwndItem = hwnd;
1113 dis.hDC = hDC;
1114 dis.itemData = 0;
1115 GetClientRect( hwnd, &dis.rcItem );
1117 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1118 parent = GetParent(hwnd);
1119 if (!parent) parent = hwnd;
1120 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1122 setup_clipping( hwnd, hDC );
1124 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1125 if (hPrevFont) SelectObject(hDC, hPrevFont);