configure: Merge the libcurses existence check with the soname check.
[wine.git] / dlls / user32 / button.c
blob14416e2b7bc6725d5571d185d59814847328ea04
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 const struct builtin_class_descr BUTTON_builtin_class =
164 "Button", /* name */
165 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
166 ButtonWndProcA, /* procA */
167 ButtonWndProcW, /* procW */
168 NB_EXTRA_BYTES, /* extra */
169 IDC_ARROW, /* cursor */
170 0 /* brush */
174 static inline LONG get_button_state( HWND hwnd )
176 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
179 static inline void set_button_state( HWND hwnd, LONG state )
181 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
184 static inline HFONT get_button_font( HWND hwnd )
186 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
189 static inline void set_button_font( HWND hwnd, HFONT font )
191 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
194 static inline UINT get_button_type( LONG window_style )
196 return (window_style & 0x0f);
199 /* paint a button of any type */
200 static inline void paint_button( HWND hwnd, LONG style, UINT action )
202 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
204 HDC hdc = GetDC( hwnd );
205 btnPaintFunc[style]( hwnd, hdc, action );
206 ReleaseDC( hwnd, hdc );
210 /* retrieve the button text; returned buffer must be freed by caller */
211 static inline WCHAR *get_button_text( HWND hwnd )
213 INT len = 512;
214 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
215 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
216 return buffer;
219 /***********************************************************************
220 * ButtonWndProc_common
222 static LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
223 WPARAM wParam, LPARAM lParam, BOOL unicode )
225 RECT rect;
226 POINT pt;
227 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
228 UINT btn_type = get_button_type( style );
229 LONG state;
230 HANDLE oldHbitmap;
232 pt.x = (short)LOWORD(lParam);
233 pt.y = (short)HIWORD(lParam);
235 switch (uMsg)
237 case WM_GETDLGCODE:
238 switch(btn_type)
240 case BS_USERBUTTON:
241 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
242 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
243 case BS_RADIOBUTTON:
244 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
245 case BS_GROUPBOX: return DLGC_STATIC;
246 default: return DLGC_BUTTON;
249 case WM_ENABLE:
250 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
251 break;
253 case WM_CREATE:
254 if (!hbitmapCheckBoxes)
256 BITMAP bmp;
257 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
258 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
259 checkBoxWidth = bmp.bmWidth / 4;
260 checkBoxHeight = bmp.bmHeight / 3;
262 if (btn_type >= MAX_BTN_TYPE)
263 return -1; /* abort */
264 set_button_state( hWnd, BUTTON_UNCHECKED );
265 return 0;
267 case WM_ERASEBKGND:
268 if (btn_type == BS_OWNERDRAW)
270 HDC hdc = (HDC)wParam;
271 RECT rc;
272 HBRUSH hBrush;
273 HWND parent = GetParent(hWnd);
274 if (!parent) parent = hWnd;
275 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
276 if (!hBrush) /* did the app forget to call defwindowproc ? */
277 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
278 (WPARAM)hdc, (LPARAM)hWnd);
279 GetClientRect(hWnd, &rc);
280 FillRect(hdc, &rc, hBrush);
282 return 1;
284 case WM_PRINTCLIENT:
285 case WM_PAINT:
286 if (btnPaintFunc[btn_type])
288 PAINTSTRUCT ps;
289 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
290 int nOldMode = SetBkMode( hdc, OPAQUE );
291 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
292 SetBkMode(hdc, nOldMode); /* reset painting mode */
293 if( !wParam ) EndPaint( hWnd, &ps );
295 break;
297 case WM_KEYDOWN:
298 if (wParam == VK_SPACE)
300 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
301 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
303 break;
305 case WM_LBUTTONDBLCLK:
306 if(style & BS_NOTIFY ||
307 btn_type == BS_RADIOBUTTON ||
308 btn_type == BS_USERBUTTON ||
309 btn_type == BS_OWNERDRAW)
311 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
312 break;
314 /* fall through */
315 case WM_LBUTTONDOWN:
316 SetCapture( hWnd );
317 SetFocus( hWnd );
318 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
319 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
320 break;
322 case WM_KEYUP:
323 if (wParam != VK_SPACE)
324 break;
325 /* fall through */
326 case WM_LBUTTONUP:
327 state = get_button_state( hWnd );
328 if (!(state & BUTTON_BTNPRESSED)) break;
329 state &= BUTTON_NSTATES;
330 set_button_state( hWnd, state );
331 if (!(state & BUTTON_HIGHLIGHTED))
333 ReleaseCapture();
334 break;
336 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
337 ReleaseCapture();
338 GetClientRect( hWnd, &rect );
339 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
341 state = get_button_state( hWnd );
342 switch(btn_type)
344 case BS_AUTOCHECKBOX:
345 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
346 break;
347 case BS_AUTORADIOBUTTON:
348 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
349 break;
350 case BS_AUTO3STATE:
351 SendMessageW( hWnd, BM_SETCHECK,
352 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
353 break;
355 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
357 break;
359 case WM_CAPTURECHANGED:
360 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
361 state = get_button_state( hWnd );
362 if (state & BUTTON_BTNPRESSED)
364 state &= BUTTON_NSTATES;
365 set_button_state( hWnd, state );
366 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
368 break;
370 case WM_MOUSEMOVE:
371 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
373 GetClientRect( hWnd, &rect );
374 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
376 break;
378 case WM_SETTEXT:
380 /* Clear an old text here as Windows does */
381 HDC hdc = GetDC(hWnd);
382 HBRUSH hbrush;
383 RECT client, rc;
384 HWND parent = GetParent(hWnd);
386 if (!parent) parent = hWnd;
387 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
388 (WPARAM)hdc, (LPARAM)hWnd);
389 if (!hbrush) /* did the app forget to call DefWindowProc ? */
390 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
391 (WPARAM)hdc, (LPARAM)hWnd);
393 GetClientRect(hWnd, &client);
394 rc = client;
395 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
396 /* Clip by client rect bounds */
397 if (rc.right > client.right) rc.right = client.right;
398 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
399 FillRect(hdc, &rc, hbrush);
400 ReleaseDC(hWnd, hdc);
402 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
403 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
404 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
405 InvalidateRect( hWnd, NULL, TRUE );
406 else
407 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
408 return 1; /* success. FIXME: check text length */
411 case WM_SETFONT:
412 set_button_font( hWnd, (HFONT)wParam );
413 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
414 break;
416 case WM_GETFONT:
417 return (LRESULT)get_button_font( hWnd );
419 case WM_SETFOCUS:
420 TRACE("WM_SETFOCUS %p\n",hWnd);
421 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
422 paint_button( hWnd, btn_type, ODA_FOCUS );
423 if (style & BS_NOTIFY)
424 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
425 break;
427 case WM_KILLFOCUS:
428 TRACE("WM_KILLFOCUS %p\n",hWnd);
429 state = get_button_state( hWnd );
430 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
431 paint_button( hWnd, btn_type, ODA_FOCUS );
433 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
434 ReleaseCapture();
435 if (style & BS_NOTIFY)
436 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
438 break;
440 case WM_SYSCOLORCHANGE:
441 InvalidateRect( hWnd, NULL, FALSE );
442 break;
444 case BM_SETSTYLE16:
445 case BM_SETSTYLE:
446 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
447 btn_type = wParam & 0x0f;
448 style = (style & ~0x0f) | btn_type;
449 SetWindowLongW( hWnd, GWL_STYLE, style );
451 /* Only redraw if lParam flag is set.*/
452 if (lParam)
453 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
455 break;
457 case BM_CLICK:
458 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
459 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
460 break;
462 case BM_SETIMAGE:
463 /* Check that image format matches button style */
464 switch (style & (BS_BITMAP|BS_ICON))
466 case BS_BITMAP:
467 if (wParam != IMAGE_BITMAP) return 0;
468 break;
469 case BS_ICON:
470 if (wParam != IMAGE_ICON) return 0;
471 break;
472 default:
473 return 0;
475 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
476 InvalidateRect( hWnd, NULL, FALSE );
477 return (LRESULT)oldHbitmap;
479 case BM_GETIMAGE:
480 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
482 case BM_GETCHECK16:
483 case BM_GETCHECK:
484 return get_button_state( hWnd ) & 3;
486 case BM_SETCHECK16:
487 case BM_SETCHECK:
488 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
489 state = get_button_state( hWnd );
490 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
492 if (wParam) style |= WS_TABSTOP;
493 else style &= ~WS_TABSTOP;
494 SetWindowLongW( hWnd, GWL_STYLE, style );
496 if ((state & 3) != wParam)
498 set_button_state( hWnd, (state & ~3) | wParam );
499 paint_button( hWnd, btn_type, ODA_SELECT );
501 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
502 BUTTON_CheckAutoRadioButton( hWnd );
503 break;
505 case BM_GETSTATE16:
506 case BM_GETSTATE:
507 return get_button_state( hWnd );
509 case BM_SETSTATE16:
510 case BM_SETSTATE:
511 state = get_button_state( hWnd );
512 if (wParam)
514 if (state & BUTTON_HIGHLIGHTED) break;
515 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
517 else
519 if (!(state & BUTTON_HIGHLIGHTED)) break;
520 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
522 paint_button( hWnd, btn_type, ODA_SELECT );
523 break;
525 case WM_NCHITTEST:
526 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
527 /* fall through */
528 default:
529 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
530 DefWindowProcA(hWnd, uMsg, wParam, lParam);
532 return 0;
535 /***********************************************************************
536 * ButtonWndProcW
537 * The button window procedure. This is just a wrapper which locks
538 * the passed HWND and calls the real window procedure (with a WND*
539 * pointer pointing to the locked windowstructure).
541 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
543 if (!IsWindow( hWnd )) return 0;
544 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
548 /***********************************************************************
549 * ButtonWndProcA
551 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
553 if (!IsWindow( hWnd )) return 0;
554 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
558 /**********************************************************************
559 * Convert button styles to flags used by DrawText.
560 * TODO: handle WS_EX_RIGHT extended style.
562 static UINT BUTTON_BStoDT(DWORD style)
564 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
566 /* "Convert" pushlike buttons to pushbuttons */
567 if (style & BS_PUSHLIKE)
568 style &= ~0x0F;
570 if (!(style & BS_MULTILINE))
571 dtStyle |= DT_SINGLELINE;
572 else
573 dtStyle |= DT_WORDBREAK;
575 switch (style & BS_CENTER)
577 case BS_LEFT: /* DT_LEFT is 0 */ break;
578 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
579 case BS_CENTER: dtStyle |= DT_CENTER; break;
580 default:
581 /* Pushbutton's text is centered by default */
582 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
583 /* all other flavours have left aligned text */
586 /* DrawText ignores vertical alignment for multiline text,
587 * but we use these flags to align label manualy.
589 if (get_button_type(style) != BS_GROUPBOX)
591 switch (style & BS_VCENTER)
593 case BS_TOP: /* DT_TOP is 0 */ break;
594 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
595 case BS_VCENTER: /* fall through */
596 default: dtStyle |= DT_VCENTER; break;
599 else
600 /* GroupBox's text is always single line and is top aligned. */
601 dtStyle |= DT_SINGLELINE;
603 return dtStyle;
606 /**********************************************************************
607 * BUTTON_CalcLabelRect
609 * Calculates label's rectangle depending on button style.
611 * Returns flags to be passed to DrawText.
612 * Calculated rectangle doesn't take into account button state
613 * (pushed, etc.). If there is nothing to draw (no text/image) output
614 * rectangle is empty, and return value is (UINT)-1.
616 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
618 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
619 WCHAR *text;
620 ICONINFO iconInfo;
621 BITMAP bm;
622 UINT dtStyle = BUTTON_BStoDT(style);
623 RECT r = *rc;
624 INT n;
626 /* Calculate label rectangle according to label type */
627 switch (style & (BS_ICON|BS_BITMAP))
629 case BS_TEXT:
630 if (!(text = get_button_text( hwnd ))) goto empty_rect;
631 if (!text[0])
633 HeapFree( GetProcessHeap(), 0, text );
634 goto empty_rect;
636 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
637 HeapFree( GetProcessHeap(), 0, text );
638 break;
640 case BS_ICON:
641 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
642 goto empty_rect;
644 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
646 r.right = r.left + bm.bmWidth;
647 r.bottom = r.top + bm.bmHeight;
649 DeleteObject(iconInfo.hbmColor);
650 DeleteObject(iconInfo.hbmMask);
651 break;
653 case BS_BITMAP:
654 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
655 goto empty_rect;
657 r.right = r.left + bm.bmWidth;
658 r.bottom = r.top + bm.bmHeight;
659 break;
661 default:
662 empty_rect:
663 rc->right = r.left;
664 rc->bottom = r.top;
665 return (UINT)(LONG)-1;
668 /* Position label inside bounding rectangle according to
669 * alignment flags. (calculated rect is always left-top aligned).
670 * If label is aligned to any side - shift label in opposite
671 * direction to leave extra space for focus rectangle.
673 switch (dtStyle & (DT_CENTER|DT_RIGHT))
675 case DT_LEFT: r.left++; r.right++; break;
676 case DT_CENTER: n = r.right - r.left;
677 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
678 r.right = r.left + n; break;
679 case DT_RIGHT: n = r.right - r.left;
680 r.right = rc->right - 1;
681 r.left = r.right - n;
682 break;
685 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
687 case DT_TOP: r.top++; r.bottom++; break;
688 case DT_VCENTER: n = r.bottom - r.top;
689 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
690 r.bottom = r.top + n; break;
691 case DT_BOTTOM: n = r.bottom - r.top;
692 r.bottom = rc->bottom - 1;
693 r.top = r.bottom - n;
694 break;
697 *rc = r;
698 return dtStyle;
702 /**********************************************************************
703 * BUTTON_DrawTextCallback
705 * Callback function used by DrawStateW function.
707 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
709 RECT rc;
710 rc.left = 0;
711 rc.top = 0;
712 rc.right = cx;
713 rc.bottom = cy;
715 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
716 return TRUE;
720 /**********************************************************************
721 * BUTTON_DrawLabel
723 * Common function for drawing button label.
725 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
727 DRAWSTATEPROC lpOutputProc = NULL;
728 LPARAM lp;
729 WPARAM wp = 0;
730 HBRUSH hbr = 0;
731 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
732 LONG state = get_button_state( hwnd );
733 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
734 WCHAR *text = NULL;
736 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
737 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
738 * I don't have Win31 on hand to verify that, so I leave it as is.
741 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
743 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
744 flags |= DSS_MONO;
747 switch (style & (BS_ICON|BS_BITMAP))
749 case BS_TEXT:
750 /* DST_COMPLEX -- is 0 */
751 lpOutputProc = BUTTON_DrawTextCallback;
752 if (!(text = get_button_text( hwnd ))) return;
753 lp = (LPARAM)text;
754 wp = (WPARAM)dtFlags;
755 break;
757 case BS_ICON:
758 flags |= DST_ICON;
759 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
760 break;
762 case BS_BITMAP:
763 flags |= DST_BITMAP;
764 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
765 break;
767 default:
768 return;
771 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
772 rc->right - rc->left, rc->bottom - rc->top, flags);
773 HeapFree( GetProcessHeap(), 0, text );
776 /**********************************************************************
777 * Push Button Functions
779 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
781 RECT rc, focus_rect, r;
782 UINT dtFlags, uState;
783 HPEN hOldPen;
784 HBRUSH hOldBrush;
785 INT oldBkMode;
786 COLORREF oldTxtColor;
787 HFONT hFont;
788 LONG state = get_button_state( hwnd );
789 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
790 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
791 HWND parent;
793 GetClientRect( hwnd, &rc );
795 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
796 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
797 parent = GetParent(hwnd);
798 if (!parent) parent = hwnd;
799 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
800 hOldPen = (HPEN)SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
801 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
802 oldBkMode = SetBkMode(hDC, TRANSPARENT);
804 if (get_button_type(style) == BS_DEFPUSHBUTTON)
806 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
807 InflateRect( &rc, -1, -1 );
810 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
812 if (style & BS_FLAT)
813 uState |= DFCS_MONO;
814 else if (pushedState)
816 if (get_button_type(style) == BS_DEFPUSHBUTTON )
817 uState |= DFCS_FLAT;
818 else
819 uState |= DFCS_PUSHED;
822 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
823 uState |= DFCS_CHECKED;
825 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
827 focus_rect = rc;
829 /* draw button label */
830 r = rc;
831 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
833 if (dtFlags == (UINT)-1L)
834 goto cleanup;
836 if (pushedState)
837 OffsetRect(&r, 1, 1);
839 IntersectClipRect(hDC, rc.left, rc.top, rc.right, rc.bottom);
841 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
843 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
845 SetTextColor( hDC, oldTxtColor );
847 if (state & BUTTON_HASFOCUS)
849 InflateRect( &focus_rect, -1, -1 );
850 IntersectRect(&focus_rect, &focus_rect, &rc);
851 DrawFocusRect( hDC, &focus_rect );
854 cleanup:
855 SelectObject( hDC, hOldPen );
856 SelectObject( hDC, hOldBrush );
857 SetBkMode(hDC, oldBkMode);
860 /**********************************************************************
861 * Check Box & Radio Button Functions
864 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
866 RECT rbox, rtext, client;
867 HBRUSH hBrush;
868 int delta;
869 UINT dtFlags;
870 HFONT hFont;
871 LONG state = get_button_state( hwnd );
872 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
873 HWND parent;
875 if (style & BS_PUSHLIKE)
877 PB_Paint( hwnd, hDC, action );
878 return;
881 GetClientRect(hwnd, &client);
882 rbox = rtext = client;
884 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
886 parent = GetParent(hwnd);
887 if (!parent) parent = hwnd;
888 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
889 (WPARAM)hDC, (LPARAM)hwnd);
890 if (!hBrush) /* did the app forget to call defwindowproc ? */
891 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
892 (WPARAM)hDC, (LPARAM)hwnd );
894 if (style & BS_LEFTTEXT)
896 /* magic +4 is what CTL3D expects */
898 rtext.right -= checkBoxWidth + 4;
899 rbox.left = rbox.right - checkBoxWidth;
901 else
903 rtext.left += checkBoxWidth + 4;
904 rbox.right = checkBoxWidth;
907 /* Since WM_ERASEBKGND does nothing, first prepare background */
908 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
909 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
911 /* Draw label */
912 client = rtext;
913 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
915 /* Only adjust rbox when rtext is valid */
916 if (dtFlags != (UINT)-1L)
918 rbox.top = rtext.top;
919 rbox.bottom = rtext.bottom;
922 /* Draw the check-box bitmap */
923 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
925 UINT flags;
927 if ((get_button_type(style) == BS_RADIOBUTTON) ||
928 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
929 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
930 else flags = DFCS_BUTTONCHECK;
932 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
933 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
935 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
937 /* rbox must have the correct height */
938 delta = rbox.bottom - rbox.top - checkBoxHeight;
940 if (style & BS_TOP) {
941 if (delta > 0) {
942 rbox.bottom = rbox.top + checkBoxHeight;
943 } else {
944 rbox.top -= -delta/2 + 1;
945 rbox.bottom = rbox.top + checkBoxHeight;
947 } else if (style & BS_BOTTOM) {
948 if (delta > 0) {
949 rbox.top = rbox.bottom - checkBoxHeight;
950 } else {
951 rbox.bottom += -delta/2 + 1;
952 rbox.top = rbox.bottom - checkBoxHeight;
954 } else { /* Default */
955 if (delta > 0) {
956 int ofs = (delta / 2);
957 rbox.bottom -= ofs + 1;
958 rbox.top = rbox.bottom - checkBoxHeight;
959 } else if (delta < 0) {
960 int ofs = (-delta / 2);
961 rbox.top -= ofs + 1;
962 rbox.bottom = rbox.top + checkBoxHeight;
966 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
969 if (dtFlags == (UINT)-1L) /* Noting to draw */
970 return;
972 IntersectClipRect(hDC, client.left, client.top, client.right, client.bottom);
974 if (action == ODA_DRAWENTIRE)
975 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
977 /* ... and focus */
978 if ((action == ODA_FOCUS) ||
979 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
981 rtext.left--;
982 rtext.right++;
983 IntersectRect(&rtext, &rtext, &client);
984 DrawFocusRect( hDC, &rtext );
989 /**********************************************************************
990 * BUTTON_CheckAutoRadioButton
992 * hwnd is checked, uncheck every other auto radio button in group
994 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
996 HWND parent, sibling, start;
998 parent = GetParent(hwnd);
999 /* make sure that starting control is not disabled or invisible */
1000 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1003 if (!sibling) break;
1004 if ((hwnd != sibling) &&
1005 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1006 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1007 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1008 } while (sibling != start);
1012 /**********************************************************************
1013 * Group Box Functions
1016 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1018 RECT rc, rcFrame;
1019 HBRUSH hbr;
1020 HFONT hFont;
1021 UINT dtFlags;
1022 TEXTMETRICW tm;
1023 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1024 HWND parent;
1026 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1027 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1028 parent = GetParent(hwnd);
1029 if (!parent) parent = hwnd;
1030 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1031 if (!hbr) /* did the app forget to call defwindowproc ? */
1032 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1033 (WPARAM)hDC, (LPARAM)hwnd);
1035 GetClientRect( hwnd, &rc);
1036 rcFrame = rc;
1038 GetTextMetricsW (hDC, &tm);
1039 rcFrame.top += (tm.tmHeight / 2) - 1;
1040 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1042 InflateRect(&rc, -7, 1);
1043 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1045 if (dtFlags == (UINT)-1L)
1046 return;
1048 /* Because buttons have CS_PARENTDC class style, there is a chance
1049 * that label will be drawn out of client rect.
1050 * But Windows doesn't clip label's rect, so do I.
1053 /* There is 1-pixel marging at the left, right, and bottom */
1054 rc.left--; rc.right++; rc.bottom++;
1055 FillRect(hDC, &rc, hbr);
1056 rc.left++; rc.right--; rc.bottom--;
1058 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1062 /**********************************************************************
1063 * User Button Functions
1066 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1068 RECT rc;
1069 HBRUSH hBrush;
1070 HFONT hFont;
1071 LONG state = get_button_state( hwnd );
1072 HWND parent;
1074 if (action == ODA_SELECT) return;
1076 GetClientRect( hwnd, &rc);
1078 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1080 parent = GetParent(hwnd);
1081 if (!parent) parent = hwnd;
1082 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1083 if (!hBrush) /* did the app forget to call defwindowproc ? */
1084 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1085 (WPARAM)hDC, (LPARAM)hwnd);
1087 FillRect( hDC, &rc, hBrush );
1088 if ((action == ODA_FOCUS) ||
1089 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1090 DrawFocusRect( hDC, &rc );
1094 /**********************************************************************
1095 * Ownerdrawn Button Functions
1098 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1100 LONG state = get_button_state( hwnd );
1101 DRAWITEMSTRUCT dis;
1102 HRGN clipRegion;
1103 RECT clipRect;
1104 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1105 HWND parent;
1106 HFONT hFont, hPrevFont = 0;
1108 dis.CtlType = ODT_BUTTON;
1109 dis.CtlID = id;
1110 dis.itemID = 0;
1111 dis.itemAction = action;
1112 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1113 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1114 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1115 dis.hwndItem = hwnd;
1116 dis.hDC = hDC;
1117 dis.itemData = 0;
1118 GetClientRect( hwnd, &dis.rcItem );
1120 clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1121 if (GetClipRgn(hDC, clipRegion) != 1)
1123 DeleteObject(clipRegion);
1124 clipRegion=NULL;
1126 clipRect = dis.rcItem;
1127 DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1128 IntersectClipRect(hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
1130 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1131 parent = GetParent(hwnd);
1132 if (!parent) parent = hwnd;
1133 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1134 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1135 if (hPrevFont) SelectObject(hDC, hPrevFont);
1136 SelectClipRgn(hDC, clipRegion);