Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / user32 / button.c
blob8a255435027079e0552f37bad4761b2d7922c000
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 RECT rc;
203 GetClientRect( hwnd, &rc );
204 if (btnPaintFunc[style] && IsWindowVisible(hwnd) && !IsRectEmpty(&rc))
206 HDC hdc = GetDC( hwnd );
207 btnPaintFunc[style]( hwnd, hdc, action );
208 ReleaseDC( hwnd, hdc );
212 /* retrieve the button text; returned buffer must be freed by caller */
213 static inline WCHAR *get_button_text( HWND hwnd )
215 INT len = 512;
216 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
217 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
218 return buffer;
221 /***********************************************************************
222 * ButtonWndProc_common
224 static LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
225 WPARAM wParam, LPARAM lParam, BOOL unicode )
227 RECT rect;
228 POINT pt;
229 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
230 UINT btn_type = get_button_type( style );
231 LONG state;
232 HANDLE oldHbitmap;
234 pt.x = (short)LOWORD(lParam);
235 pt.y = (short)HIWORD(lParam);
237 switch (uMsg)
239 case WM_GETDLGCODE:
240 switch(btn_type)
242 case BS_USERBUTTON:
243 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
244 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
245 case BS_RADIOBUTTON:
246 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
247 case BS_GROUPBOX: return DLGC_STATIC;
248 default: return DLGC_BUTTON;
251 case WM_ENABLE:
252 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
253 break;
255 case WM_CREATE:
256 if (!hbitmapCheckBoxes)
258 BITMAP bmp;
259 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
260 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
261 checkBoxWidth = bmp.bmWidth / 4;
262 checkBoxHeight = bmp.bmHeight / 3;
264 if (btn_type >= MAX_BTN_TYPE)
265 return -1; /* abort */
266 set_button_state( hWnd, BUTTON_UNCHECKED );
267 return 0;
269 case WM_ERASEBKGND:
270 if (btn_type == BS_OWNERDRAW)
272 HDC hdc = (HDC)wParam;
273 RECT rc;
274 HBRUSH hBrush;
275 HWND parent = GetParent(hWnd);
276 if (!parent) parent = hWnd;
277 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
278 if (!hBrush) /* did the app forget to call defwindowproc ? */
279 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
280 (WPARAM)hdc, (LPARAM)hWnd);
281 GetClientRect(hWnd, &rc);
282 FillRect(hdc, &rc, hBrush);
284 return 1;
286 case WM_PRINTCLIENT:
287 case WM_PAINT:
288 if (btnPaintFunc[btn_type])
290 PAINTSTRUCT ps;
291 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
292 int nOldMode = SetBkMode( hdc, OPAQUE );
293 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
294 SetBkMode(hdc, nOldMode); /* reset painting mode */
295 if( !wParam ) EndPaint( hWnd, &ps );
297 break;
299 case WM_KEYDOWN:
300 if (wParam == VK_SPACE)
302 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
303 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
305 break;
307 case WM_LBUTTONDBLCLK:
308 if(style & BS_NOTIFY ||
309 btn_type == BS_RADIOBUTTON ||
310 btn_type == BS_USERBUTTON ||
311 btn_type == BS_OWNERDRAW)
313 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
314 break;
316 /* fall through */
317 case WM_LBUTTONDOWN:
318 SetCapture( hWnd );
319 SetFocus( hWnd );
320 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
321 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
322 break;
324 case WM_KEYUP:
325 if (wParam != VK_SPACE)
326 break;
327 /* fall through */
328 case WM_LBUTTONUP:
329 state = get_button_state( hWnd );
330 if (!(state & BUTTON_BTNPRESSED)) break;
331 state &= BUTTON_NSTATES;
332 set_button_state( hWnd, state );
333 if (!(state & BUTTON_HIGHLIGHTED))
335 ReleaseCapture();
336 break;
338 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
339 ReleaseCapture();
340 GetClientRect( hWnd, &rect );
341 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
343 state = get_button_state( hWnd );
344 switch(btn_type)
346 case BS_AUTOCHECKBOX:
347 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
348 break;
349 case BS_AUTORADIOBUTTON:
350 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
351 break;
352 case BS_AUTO3STATE:
353 SendMessageW( hWnd, BM_SETCHECK,
354 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
355 break;
357 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
359 break;
361 case WM_CAPTURECHANGED:
362 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
363 state = get_button_state( hWnd );
364 if (state & BUTTON_BTNPRESSED)
366 state &= BUTTON_NSTATES;
367 set_button_state( hWnd, state );
368 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
370 break;
372 case WM_MOUSEMOVE:
373 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
375 GetClientRect( hWnd, &rect );
376 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
378 break;
380 case WM_SETTEXT:
382 /* Clear an old text here as Windows does */
383 HDC hdc = GetDC(hWnd);
384 HBRUSH hbrush;
385 RECT client, rc;
386 HWND parent = GetParent(hWnd);
388 if (!parent) parent = hWnd;
389 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
390 (WPARAM)hdc, (LPARAM)hWnd);
391 if (!hbrush) /* did the app forget to call DefWindowProc ? */
392 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
393 (WPARAM)hdc, (LPARAM)hWnd);
395 GetClientRect(hWnd, &client);
396 rc = client;
397 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
398 /* Clip by client rect bounds */
399 if (rc.right > client.right) rc.right = client.right;
400 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
401 FillRect(hdc, &rc, hbrush);
402 ReleaseDC(hWnd, hdc);
404 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
405 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
406 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
407 InvalidateRect( hWnd, NULL, TRUE );
408 else
409 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
410 return 1; /* success. FIXME: check text length */
413 case WM_SETFONT:
414 set_button_font( hWnd, (HFONT)wParam );
415 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
416 break;
418 case WM_GETFONT:
419 return (LRESULT)get_button_font( hWnd );
421 case WM_SETFOCUS:
422 TRACE("WM_SETFOCUS %p\n",hWnd);
423 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
424 paint_button( hWnd, btn_type, ODA_FOCUS );
425 if (style & BS_NOTIFY)
426 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
427 break;
429 case WM_KILLFOCUS:
430 TRACE("WM_KILLFOCUS %p\n",hWnd);
431 state = get_button_state( hWnd );
432 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
433 paint_button( hWnd, btn_type, ODA_FOCUS );
435 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
436 ReleaseCapture();
437 if (style & BS_NOTIFY)
438 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
440 break;
442 case WM_SYSCOLORCHANGE:
443 InvalidateRect( hWnd, NULL, FALSE );
444 break;
446 case BM_SETSTYLE16:
447 case BM_SETSTYLE:
448 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
449 btn_type = wParam & 0x0f;
450 style = (style & ~0x0f) | btn_type;
451 SetWindowLongW( hWnd, GWL_STYLE, style );
453 /* Only redraw if lParam flag is set.*/
454 if (lParam)
455 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
457 break;
459 case BM_CLICK:
460 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
461 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
462 break;
464 case BM_SETIMAGE:
465 /* Check that image format matches button style */
466 switch (style & (BS_BITMAP|BS_ICON))
468 case BS_BITMAP:
469 if (wParam != IMAGE_BITMAP) return 0;
470 break;
471 case BS_ICON:
472 if (wParam != IMAGE_ICON) return 0;
473 break;
474 default:
475 return 0;
477 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
478 InvalidateRect( hWnd, NULL, FALSE );
479 return (LRESULT)oldHbitmap;
481 case BM_GETIMAGE:
482 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
484 case BM_GETCHECK16:
485 case BM_GETCHECK:
486 return get_button_state( hWnd ) & 3;
488 case BM_SETCHECK16:
489 case BM_SETCHECK:
490 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
491 state = get_button_state( hWnd );
492 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
494 if (wParam) style |= WS_TABSTOP;
495 else style &= ~WS_TABSTOP;
496 SetWindowLongW( hWnd, GWL_STYLE, style );
498 if ((state & 3) != wParam)
500 set_button_state( hWnd, (state & ~3) | wParam );
501 paint_button( hWnd, btn_type, ODA_SELECT );
503 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
504 BUTTON_CheckAutoRadioButton( hWnd );
505 break;
507 case BM_GETSTATE16:
508 case BM_GETSTATE:
509 return get_button_state( hWnd );
511 case BM_SETSTATE16:
512 case BM_SETSTATE:
513 state = get_button_state( hWnd );
514 if (wParam)
516 if (state & BUTTON_HIGHLIGHTED) break;
517 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
519 else
521 if (!(state & BUTTON_HIGHLIGHTED)) break;
522 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
524 paint_button( hWnd, btn_type, ODA_SELECT );
525 break;
527 case WM_NCHITTEST:
528 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
529 /* fall through */
530 default:
531 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
532 DefWindowProcA(hWnd, uMsg, wParam, lParam);
534 return 0;
537 /***********************************************************************
538 * ButtonWndProcW
539 * The button window procedure. This is just a wrapper which locks
540 * the passed HWND and calls the real window procedure (with a WND*
541 * pointer pointing to the locked windowstructure).
543 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
545 if (!IsWindow( hWnd )) return 0;
546 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
550 /***********************************************************************
551 * ButtonWndProcA
553 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
555 if (!IsWindow( hWnd )) return 0;
556 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
560 /**********************************************************************
561 * Convert button styles to flags used by DrawText.
562 * TODO: handle WS_EX_RIGHT extended style.
564 static UINT BUTTON_BStoDT(DWORD style)
566 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
568 /* "Convert" pushlike buttons to pushbuttons */
569 if (style & BS_PUSHLIKE)
570 style &= ~0x0F;
572 if (!(style & BS_MULTILINE))
573 dtStyle |= DT_SINGLELINE;
574 else
575 dtStyle |= DT_WORDBREAK;
577 switch (style & BS_CENTER)
579 case BS_LEFT: /* DT_LEFT is 0 */ break;
580 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
581 case BS_CENTER: dtStyle |= DT_CENTER; break;
582 default:
583 /* Pushbutton's text is centered by default */
584 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
585 /* all other flavours have left aligned text */
588 /* DrawText ignores vertical alignment for multiline text,
589 * but we use these flags to align label manualy.
591 if (get_button_type(style) != BS_GROUPBOX)
593 switch (style & BS_VCENTER)
595 case BS_TOP: /* DT_TOP is 0 */ break;
596 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
597 case BS_VCENTER: /* fall through */
598 default: dtStyle |= DT_VCENTER; break;
601 else
602 /* GroupBox's text is always single line and is top aligned. */
603 dtStyle |= DT_SINGLELINE;
605 return dtStyle;
608 /**********************************************************************
609 * BUTTON_CalcLabelRect
611 * Calculates label's rectangle depending on button style.
613 * Returns flags to be passed to DrawText.
614 * Calculated rectangle doesn't take into account button state
615 * (pushed, etc.). If there is nothing to draw (no text/image) output
616 * rectangle is empty, and return value is (UINT)-1.
618 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
620 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
621 WCHAR *text;
622 ICONINFO iconInfo;
623 BITMAP bm;
624 UINT dtStyle = BUTTON_BStoDT(style);
625 RECT r = *rc;
626 INT n;
628 /* Calculate label rectangle according to label type */
629 switch (style & (BS_ICON|BS_BITMAP))
631 case BS_TEXT:
632 if (!(text = get_button_text( hwnd ))) goto empty_rect;
633 if (!text[0])
635 HeapFree( GetProcessHeap(), 0, text );
636 goto empty_rect;
638 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
639 HeapFree( GetProcessHeap(), 0, text );
640 break;
642 case BS_ICON:
643 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
644 goto empty_rect;
646 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
648 r.right = r.left + bm.bmWidth;
649 r.bottom = r.top + bm.bmHeight;
651 DeleteObject(iconInfo.hbmColor);
652 DeleteObject(iconInfo.hbmMask);
653 break;
655 case BS_BITMAP:
656 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
657 goto empty_rect;
659 r.right = r.left + bm.bmWidth;
660 r.bottom = r.top + bm.bmHeight;
661 break;
663 default:
664 empty_rect:
665 rc->right = r.left;
666 rc->bottom = r.top;
667 return (UINT)(LONG)-1;
670 /* Position label inside bounding rectangle according to
671 * alignment flags. (calculated rect is always left-top aligned).
672 * If label is aligned to any side - shift label in opposite
673 * direction to leave extra space for focus rectangle.
675 switch (dtStyle & (DT_CENTER|DT_RIGHT))
677 case DT_LEFT: r.left++; r.right++; break;
678 case DT_CENTER: n = r.right - r.left;
679 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
680 r.right = r.left + n; break;
681 case DT_RIGHT: n = r.right - r.left;
682 r.right = rc->right - 1;
683 r.left = r.right - n;
684 break;
687 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
689 case DT_TOP: r.top++; r.bottom++; break;
690 case DT_VCENTER: n = r.bottom - r.top;
691 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
692 r.bottom = r.top + n; break;
693 case DT_BOTTOM: n = r.bottom - r.top;
694 r.bottom = rc->bottom - 1;
695 r.top = r.bottom - n;
696 break;
699 *rc = r;
700 return dtStyle;
704 /**********************************************************************
705 * BUTTON_DrawTextCallback
707 * Callback function used by DrawStateW function.
709 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
711 RECT rc;
712 rc.left = 0;
713 rc.top = 0;
714 rc.right = cx;
715 rc.bottom = cy;
717 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
718 return TRUE;
722 /**********************************************************************
723 * BUTTON_DrawLabel
725 * Common function for drawing button label.
727 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
729 DRAWSTATEPROC lpOutputProc = NULL;
730 LPARAM lp;
731 WPARAM wp = 0;
732 HBRUSH hbr = 0;
733 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
734 LONG state = get_button_state( hwnd );
735 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
736 WCHAR *text = NULL;
738 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
739 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
740 * I don't have Win31 on hand to verify that, so I leave it as is.
743 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
745 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
746 flags |= DSS_MONO;
749 switch (style & (BS_ICON|BS_BITMAP))
751 case BS_TEXT:
752 /* DST_COMPLEX -- is 0 */
753 lpOutputProc = BUTTON_DrawTextCallback;
754 if (!(text = get_button_text( hwnd ))) return;
755 lp = (LPARAM)text;
756 wp = (WPARAM)dtFlags;
757 break;
759 case BS_ICON:
760 flags |= DST_ICON;
761 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
762 break;
764 case BS_BITMAP:
765 flags |= DST_BITMAP;
766 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
767 break;
769 default:
770 return;
773 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
774 rc->right - rc->left, rc->bottom - rc->top, flags);
775 HeapFree( GetProcessHeap(), 0, text );
778 /**********************************************************************
779 * Push Button Functions
781 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
783 RECT rc, focus_rect, r;
784 UINT dtFlags, uState;
785 HPEN hOldPen;
786 HBRUSH hOldBrush;
787 INT oldBkMode;
788 COLORREF oldTxtColor;
789 HFONT hFont;
790 LONG state = get_button_state( hwnd );
791 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
792 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
793 HWND parent;
795 GetClientRect( hwnd, &rc );
797 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
798 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
799 parent = GetParent(hwnd);
800 if (!parent) parent = hwnd;
801 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
802 hOldPen = (HPEN)SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
803 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
804 oldBkMode = SetBkMode(hDC, TRANSPARENT);
806 if (get_button_type(style) == BS_DEFPUSHBUTTON)
808 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
809 InflateRect( &rc, -1, -1 );
812 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
814 if (style & BS_FLAT)
815 uState |= DFCS_MONO;
816 else if (pushedState)
818 if (get_button_type(style) == BS_DEFPUSHBUTTON )
819 uState |= DFCS_FLAT;
820 else
821 uState |= DFCS_PUSHED;
824 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
825 uState |= DFCS_CHECKED;
827 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
829 focus_rect = rc;
831 /* draw button label */
832 r = rc;
833 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
835 if (dtFlags == (UINT)-1L)
836 goto cleanup;
838 if (pushedState)
839 OffsetRect(&r, 1, 1);
841 IntersectClipRect(hDC, rc.left, rc.top, rc.right, rc.bottom);
843 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
845 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
847 SetTextColor( hDC, oldTxtColor );
849 if (state & BUTTON_HASFOCUS)
851 InflateRect( &focus_rect, -1, -1 );
852 IntersectRect(&focus_rect, &focus_rect, &rc);
853 DrawFocusRect( hDC, &focus_rect );
856 cleanup:
857 SelectObject( hDC, hOldPen );
858 SelectObject( hDC, hOldBrush );
859 SetBkMode(hDC, oldBkMode);
862 /**********************************************************************
863 * Check Box & Radio Button Functions
866 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
868 RECT rbox, rtext, client;
869 HBRUSH hBrush;
870 int delta;
871 UINT dtFlags;
872 HFONT hFont;
873 LONG state = get_button_state( hwnd );
874 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
875 HWND parent;
877 if (style & BS_PUSHLIKE)
879 PB_Paint( hwnd, hDC, action );
880 return;
883 GetClientRect(hwnd, &client);
884 rbox = rtext = client;
886 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
888 parent = GetParent(hwnd);
889 if (!parent) parent = hwnd;
890 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
891 (WPARAM)hDC, (LPARAM)hwnd);
892 if (!hBrush) /* did the app forget to call defwindowproc ? */
893 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
894 (WPARAM)hDC, (LPARAM)hwnd );
896 if (style & BS_LEFTTEXT)
898 /* magic +4 is what CTL3D expects */
900 rtext.right -= checkBoxWidth + 4;
901 rbox.left = rbox.right - checkBoxWidth;
903 else
905 rtext.left += checkBoxWidth + 4;
906 rbox.right = checkBoxWidth;
909 /* Since WM_ERASEBKGND does nothing, first prepare background */
910 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
911 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
913 /* Draw label */
914 client = rtext;
915 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
917 /* Only adjust rbox when rtext is valid */
918 if (dtFlags != (UINT)-1L)
920 rbox.top = rtext.top;
921 rbox.bottom = rtext.bottom;
924 /* Draw the check-box bitmap */
925 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
927 UINT flags;
929 if ((get_button_type(style) == BS_RADIOBUTTON) ||
930 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
931 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
932 else flags = DFCS_BUTTONCHECK;
934 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
935 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
937 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
939 /* rbox must have the correct height */
940 delta = rbox.bottom - rbox.top - checkBoxHeight;
942 if (style & BS_TOP) {
943 if (delta > 0) {
944 rbox.bottom = rbox.top + checkBoxHeight;
945 } else {
946 rbox.top -= -delta/2 + 1;
947 rbox.bottom = rbox.top + checkBoxHeight;
949 } else if (style & BS_BOTTOM) {
950 if (delta > 0) {
951 rbox.top = rbox.bottom - checkBoxHeight;
952 } else {
953 rbox.bottom += -delta/2 + 1;
954 rbox.top = rbox.bottom - checkBoxHeight;
956 } else { /* Default */
957 if (delta > 0) {
958 int ofs = (delta / 2);
959 rbox.bottom -= ofs + 1;
960 rbox.top = rbox.bottom - checkBoxHeight;
961 } else if (delta < 0) {
962 int ofs = (-delta / 2);
963 rbox.top -= ofs + 1;
964 rbox.bottom = rbox.top + checkBoxHeight;
968 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
971 if (dtFlags == (UINT)-1L) /* Noting to draw */
972 return;
974 IntersectClipRect(hDC, client.left, client.top, client.right, client.bottom);
976 if (action == ODA_DRAWENTIRE)
977 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
979 /* ... and focus */
980 if ((action == ODA_FOCUS) ||
981 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
983 rtext.left--;
984 rtext.right++;
985 IntersectRect(&rtext, &rtext, &client);
986 DrawFocusRect( hDC, &rtext );
991 /**********************************************************************
992 * BUTTON_CheckAutoRadioButton
994 * hwnd is checked, uncheck every other auto radio button in group
996 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
998 HWND parent, sibling, start;
1000 parent = GetParent(hwnd);
1001 /* make sure that starting control is not disabled or invisible */
1002 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1005 if (!sibling) break;
1006 if ((hwnd != sibling) &&
1007 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1008 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1009 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1010 } while (sibling != start);
1014 /**********************************************************************
1015 * Group Box Functions
1018 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1020 RECT rc, rcFrame;
1021 HBRUSH hbr;
1022 HFONT hFont;
1023 UINT dtFlags;
1024 TEXTMETRICW tm;
1025 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1026 HWND parent;
1028 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1029 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1030 parent = GetParent(hwnd);
1031 if (!parent) parent = hwnd;
1032 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1033 if (!hbr) /* did the app forget to call defwindowproc ? */
1034 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1035 (WPARAM)hDC, (LPARAM)hwnd);
1037 GetClientRect( hwnd, &rc);
1038 rcFrame = rc;
1040 GetTextMetricsW (hDC, &tm);
1041 rcFrame.top += (tm.tmHeight / 2) - 1;
1042 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1044 InflateRect(&rc, -7, 1);
1045 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1047 if (dtFlags == (UINT)-1L)
1048 return;
1050 /* Because buttons have CS_PARENTDC class style, there is a chance
1051 * that label will be drawn out of client rect.
1052 * But Windows doesn't clip label's rect, so do I.
1055 /* There is 1-pixel marging at the left, right, and bottom */
1056 rc.left--; rc.right++; rc.bottom++;
1057 FillRect(hDC, &rc, hbr);
1058 rc.left++; rc.right--; rc.bottom--;
1060 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1064 /**********************************************************************
1065 * User Button Functions
1068 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1070 RECT rc;
1071 HBRUSH hBrush;
1072 HFONT hFont;
1073 LONG state = get_button_state( hwnd );
1074 HWND parent;
1076 if (action == ODA_SELECT) return;
1078 GetClientRect( hwnd, &rc);
1080 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1082 parent = GetParent(hwnd);
1083 if (!parent) parent = hwnd;
1084 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1085 if (!hBrush) /* did the app forget to call defwindowproc ? */
1086 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1087 (WPARAM)hDC, (LPARAM)hwnd);
1089 FillRect( hDC, &rc, hBrush );
1090 if ((action == ODA_FOCUS) ||
1091 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1092 DrawFocusRect( hDC, &rc );
1096 /**********************************************************************
1097 * Ownerdrawn Button Functions
1100 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1102 LONG state = get_button_state( hwnd );
1103 DRAWITEMSTRUCT dis;
1104 HRGN clipRegion;
1105 RECT clipRect;
1106 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1107 HWND parent;
1108 HFONT hFont, hPrevFont = 0;
1110 dis.CtlType = ODT_BUTTON;
1111 dis.CtlID = id;
1112 dis.itemID = 0;
1113 dis.itemAction = action;
1114 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1115 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1116 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1117 dis.hwndItem = hwnd;
1118 dis.hDC = hDC;
1119 dis.itemData = 0;
1120 GetClientRect( hwnd, &dis.rcItem );
1122 clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1123 if (GetClipRgn(hDC, clipRegion) != 1)
1125 DeleteObject(clipRegion);
1126 clipRegion=NULL;
1128 clipRect = dis.rcItem;
1129 DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1130 IntersectClipRect(hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
1132 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1133 parent = GetParent(hwnd);
1134 if (!parent) parent = hwnd;
1135 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1136 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1137 if (hPrevFont) SelectObject(hDC, hPrevFont);
1138 SelectClipRgn(hDC, clipRegion);