wined3d: Merge wined3d_surface_upload_data() into texture2d_upload_data().
[wine.git] / dlls / user32 / button.c
blob0f56e4e51d47fb9b77e7faec3793b2a85583729d
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 * TODO
22 * Styles
23 * - BS_NOTIFY: is it complete?
24 * - BS_RIGHTBUTTON: same as BS_LEFTTEXT
26 * Messages
27 * - WM_CHAR: Checks a (manual or automatic) check box on '+' or '=', clears it on '-' key.
28 * - WM_SETFOCUS: For (manual or automatic) radio buttons, send the parent window BN_CLICKED
29 * - WM_NCCREATE: Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
30 * - WM_SYSKEYUP
31 * - BCM_GETIDEALSIZE
32 * - BCM_GETIMAGELIST
33 * - BCM_GETTEXTMARGIN
34 * - BCM_SETIMAGELIST
35 * - BCM_SETTEXTMARGIN
37 * Notifications
38 * - BCN_HOTITEMCHANGE
39 * - BN_DISABLE
40 * - BN_PUSHED/BN_HILITE
41 * + BN_KILLFOCUS: is it OK?
42 * - BN_PAINT
43 * + BN_SETFOCUS: is it OK?
44 * - BN_UNPUSHED/BN_UNHILITE
45 * - NM_CUSTOMDRAW
47 * Structures/Macros/Definitions
48 * - BUTTON_IMAGELIST
49 * - NMBCHOTITEM
50 * - Button_GetIdealSize
51 * - Button_GetImageList
52 * - Button_GetTextMargin
53 * - Button_SetImageList
54 * - Button_SetTextMargin
57 #include <stdarg.h>
58 #include <string.h>
59 #include <stdlib.h>
61 #define OEMRESOURCE
63 #include "windef.h"
64 #include "winbase.h"
65 #include "wingdi.h"
66 #include "controls.h"
67 #include "win.h"
68 #include "user_private.h"
69 #include "wine/debug.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(button);
73 /* GetWindowLong offsets for window extra information */
74 #define STATE_GWL_OFFSET 0
75 #define HFONT_GWL_OFFSET (sizeof(LONG))
76 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
77 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
79 /* undocumented flags */
80 #define BUTTON_NSTATES 0x0F
81 #define BUTTON_BTNPRESSED 0x40
82 #define BUTTON_UNKNOWN2 0x20
83 #define BUTTON_UNKNOWN3 0x10
85 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
86 do { /* Notify parent which has created this button control */ \
87 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
88 SendMessageW(GetParent(hWnd), WM_COMMAND, \
89 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
90 (LPARAM)(hWnd)); \
91 } while(0)
93 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
94 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
95 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
96 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
97 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
98 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
99 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
101 #define MAX_BTN_TYPE 16
103 static const WORD maxCheckState[MAX_BTN_TYPE] =
105 BST_UNCHECKED, /* BS_PUSHBUTTON */
106 BST_UNCHECKED, /* BS_DEFPUSHBUTTON */
107 BST_CHECKED, /* BS_CHECKBOX */
108 BST_CHECKED, /* BS_AUTOCHECKBOX */
109 BST_CHECKED, /* BS_RADIOBUTTON */
110 BST_INDETERMINATE, /* BS_3STATE */
111 BST_INDETERMINATE, /* BS_AUTO3STATE */
112 BST_UNCHECKED, /* BS_GROUPBOX */
113 BST_UNCHECKED, /* BS_USERBUTTON */
114 BST_CHECKED, /* BS_AUTORADIOBUTTON */
115 BST_UNCHECKED, /* BS_PUSHBOX */
116 BST_UNCHECKED /* BS_OWNERDRAW */
119 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
121 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
123 PB_Paint, /* BS_PUSHBUTTON */
124 PB_Paint, /* BS_DEFPUSHBUTTON */
125 CB_Paint, /* BS_CHECKBOX */
126 CB_Paint, /* BS_AUTOCHECKBOX */
127 CB_Paint, /* BS_RADIOBUTTON */
128 CB_Paint, /* BS_3STATE */
129 CB_Paint, /* BS_AUTO3STATE */
130 GB_Paint, /* BS_GROUPBOX */
131 UB_Paint, /* BS_USERBUTTON */
132 CB_Paint, /* BS_AUTORADIOBUTTON */
133 NULL, /* BS_PUSHBOX */
134 OB_Paint /* BS_OWNERDRAW */
137 /*********************************************************************
138 * button class descriptor
140 static const WCHAR buttonW[] = {'B','u','t','t','o','n',0};
141 const struct builtin_class_descr BUTTON_builtin_class =
143 buttonW, /* name */
144 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
145 WINPROC_BUTTON, /* proc */
146 NB_EXTRA_BYTES, /* extra */
147 IDC_ARROW, /* cursor */
148 0 /* brush */
152 static inline LONG get_button_state( HWND hwnd )
154 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
157 static inline void set_button_state( HWND hwnd, LONG state )
159 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
162 static inline HFONT get_button_font( HWND hwnd )
164 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
167 static inline void set_button_font( HWND hwnd, HFONT font )
169 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
172 static inline UINT get_button_type( LONG window_style )
174 return (window_style & BS_TYPEMASK);
177 /* paint a button of any type */
178 static inline void paint_button( HWND hwnd, LONG style, UINT action )
180 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
182 HDC hdc = GetDC( hwnd );
183 btnPaintFunc[style]( hwnd, hdc, action );
184 ReleaseDC( hwnd, hdc );
188 /* retrieve the button text; returned buffer must be freed by caller */
189 static inline WCHAR *get_button_text( HWND hwnd )
191 static const INT len = 512;
192 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
193 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
194 return buffer;
197 /***********************************************************************
198 * ButtonWndProc_common
200 LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL unicode )
202 RECT rect;
203 POINT pt;
204 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
205 UINT btn_type = get_button_type( style );
206 LONG state;
207 HANDLE oldHbitmap;
209 if (!IsWindow( hWnd )) return 0;
211 pt.x = (short)LOWORD(lParam);
212 pt.y = (short)HIWORD(lParam);
214 switch (uMsg)
216 case WM_GETDLGCODE:
217 switch(btn_type)
219 case BS_USERBUTTON:
220 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
221 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
222 case BS_RADIOBUTTON:
223 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
224 case BS_GROUPBOX: return DLGC_STATIC;
225 default: return DLGC_BUTTON;
228 case WM_ENABLE:
229 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
230 break;
232 case WM_CREATE:
233 if (btn_type >= MAX_BTN_TYPE)
234 return -1; /* abort */
236 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
237 if (btn_type == BS_USERBUTTON )
239 style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
240 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
242 set_button_state( hWnd, BST_UNCHECKED );
243 return 0;
245 case WM_ERASEBKGND:
246 if (btn_type == BS_OWNERDRAW)
248 HDC hdc = (HDC)wParam;
249 RECT rc;
250 HBRUSH hBrush;
251 HWND parent = GetParent(hWnd);
252 if (!parent) parent = hWnd;
253 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
254 if (!hBrush) /* did the app forget to call defwindowproc ? */
255 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
256 (WPARAM)hdc, (LPARAM)hWnd);
257 GetClientRect(hWnd, &rc);
258 FillRect(hdc, &rc, hBrush);
260 return 1;
262 case WM_PRINTCLIENT:
263 case WM_PAINT:
265 PAINTSTRUCT ps;
266 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
267 if (btnPaintFunc[btn_type])
269 int nOldMode = SetBkMode( hdc, OPAQUE );
270 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
271 SetBkMode(hdc, nOldMode); /* reset painting mode */
273 if ( !wParam ) EndPaint( hWnd, &ps );
274 break;
277 case WM_KEYDOWN:
278 if (wParam == VK_SPACE)
280 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
281 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
282 SetCapture( hWnd );
284 break;
286 case WM_LBUTTONDBLCLK:
287 if(style & BS_NOTIFY ||
288 btn_type == BS_RADIOBUTTON ||
289 btn_type == BS_USERBUTTON ||
290 btn_type == BS_OWNERDRAW)
292 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
293 break;
295 /* fall through */
296 case WM_LBUTTONDOWN:
297 SetCapture( hWnd );
298 SetFocus( hWnd );
299 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
300 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
301 break;
303 case WM_KEYUP:
304 if (wParam != VK_SPACE)
305 break;
306 /* fall through */
307 case WM_LBUTTONUP:
308 state = get_button_state( hWnd );
309 if (!(state & BUTTON_BTNPRESSED)) break;
310 state &= BUTTON_NSTATES;
311 set_button_state( hWnd, state );
312 if (!(state & BST_PUSHED))
314 ReleaseCapture();
315 break;
317 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
318 GetClientRect( hWnd, &rect );
319 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
321 state = get_button_state( hWnd );
322 switch(btn_type)
324 case BS_AUTOCHECKBOX:
325 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
326 break;
327 case BS_AUTORADIOBUTTON:
328 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
329 break;
330 case BS_AUTO3STATE:
331 SendMessageW( hWnd, BM_SETCHECK,
332 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
333 break;
335 ReleaseCapture();
336 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
338 else
340 ReleaseCapture();
342 break;
344 case WM_CAPTURECHANGED:
345 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
346 if (hWnd == (HWND)lParam) break;
347 state = get_button_state( hWnd );
348 if (state & BUTTON_BTNPRESSED)
350 state &= BUTTON_NSTATES;
351 set_button_state( hWnd, state );
352 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
354 break;
356 case WM_MOUSEMOVE:
357 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
359 GetClientRect( hWnd, &rect );
360 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
362 break;
364 case WM_SETTEXT:
366 /* Clear an old text here as Windows does */
367 if (IsWindowVisible(hWnd))
369 HDC hdc = GetDC(hWnd);
370 HBRUSH hbrush;
371 RECT client, rc;
372 HWND parent = GetParent(hWnd);
373 UINT message = (btn_type == BS_PUSHBUTTON ||
374 btn_type == BS_DEFPUSHBUTTON ||
375 btn_type == BS_USERBUTTON ||
376 btn_type == BS_OWNERDRAW) ?
377 WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
379 if (!parent) parent = hWnd;
380 hbrush = (HBRUSH)SendMessageW(parent, message,
381 (WPARAM)hdc, (LPARAM)hWnd);
382 if (!hbrush) /* did the app forget to call DefWindowProc ? */
383 hbrush = (HBRUSH)DefWindowProcW(parent, message,
384 (WPARAM)hdc, (LPARAM)hWnd);
386 GetClientRect(hWnd, &client);
387 rc = client;
388 /* FIXME: check other BS_* handlers */
389 if (btn_type == BS_GROUPBOX)
390 InflateRect(&rc, -7, 1); /* GB_Paint does this */
391 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
392 /* Clip by client rect bounds */
393 if (rc.right > client.right) rc.right = client.right;
394 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
395 FillRect(hdc, &rc, hbrush);
396 ReleaseDC(hWnd, hdc);
399 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
400 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
401 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
402 InvalidateRect( hWnd, NULL, TRUE );
403 else
404 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
405 return 1; /* success. FIXME: check text length */
408 case WM_SETFONT:
409 set_button_font( hWnd, (HFONT)wParam );
410 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
411 break;
413 case WM_GETFONT:
414 return (LRESULT)get_button_font( hWnd );
416 case WM_SETFOCUS:
417 TRACE("WM_SETFOCUS %p\n",hWnd);
418 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
419 paint_button( hWnd, btn_type, ODA_FOCUS );
420 if (style & BS_NOTIFY)
421 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
422 break;
424 case WM_KILLFOCUS:
425 TRACE("WM_KILLFOCUS %p\n",hWnd);
426 state = get_button_state( hWnd );
427 set_button_state( hWnd, state & ~BST_FOCUS );
428 paint_button( hWnd, btn_type, ODA_FOCUS );
430 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
431 ReleaseCapture();
432 if (style & BS_NOTIFY)
433 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
435 InvalidateRect( hWnd, NULL, FALSE );
436 break;
438 case WM_SYSCOLORCHANGE:
439 InvalidateRect( hWnd, NULL, FALSE );
440 break;
442 case BM_SETSTYLE:
443 btn_type = wParam & BS_TYPEMASK;
444 style = (style & ~BS_TYPEMASK) | btn_type;
445 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
447 /* Only redraw if lParam flag is set.*/
448 if (lParam)
449 InvalidateRect( hWnd, NULL, TRUE );
451 break;
453 case BM_CLICK:
454 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
455 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
456 break;
458 case BM_SETIMAGE:
459 /* Check that image format matches button style */
460 switch (style & (BS_BITMAP|BS_ICON))
462 case BS_BITMAP:
463 if (wParam != IMAGE_BITMAP) return 0;
464 break;
465 case BS_ICON:
466 if (wParam != IMAGE_ICON) return 0;
467 break;
468 default:
469 return 0;
471 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
472 InvalidateRect( hWnd, NULL, FALSE );
473 return (LRESULT)oldHbitmap;
475 case BM_GETIMAGE:
476 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
478 case BM_GETCHECK:
479 return get_button_state( hWnd ) & 3;
481 case BM_SETCHECK:
482 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
483 state = get_button_state( hWnd );
484 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
486 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
487 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
489 if ((state & 3) != wParam)
491 set_button_state( hWnd, (state & ~3) | wParam );
492 paint_button( hWnd, btn_type, ODA_SELECT );
494 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BST_CHECKED) && (style & WS_CHILD))
495 BUTTON_CheckAutoRadioButton( hWnd );
496 break;
498 case BM_GETSTATE:
499 return get_button_state( hWnd );
501 case BM_SETSTATE:
502 state = get_button_state( hWnd );
503 if (wParam)
504 set_button_state( hWnd, state | BST_PUSHED );
505 else
506 set_button_state( hWnd, state & ~BST_PUSHED );
508 paint_button( hWnd, btn_type, ODA_SELECT );
509 break;
511 case WM_NCHITTEST:
512 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
513 /* fall through */
514 default:
515 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
516 DefWindowProcA(hWnd, uMsg, wParam, lParam);
518 return 0;
521 /**********************************************************************
522 * Convert button styles to flags used by DrawText.
524 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
526 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
528 /* "Convert" pushlike buttons to pushbuttons */
529 if (style & BS_PUSHLIKE)
530 style &= ~BS_TYPEMASK;
532 if (!(style & BS_MULTILINE))
533 dtStyle |= DT_SINGLELINE;
534 else
535 dtStyle |= DT_WORDBREAK;
537 switch (style & BS_CENTER)
539 case BS_LEFT: /* DT_LEFT is 0 */ break;
540 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
541 case BS_CENTER: dtStyle |= DT_CENTER; break;
542 default:
543 /* Pushbutton's text is centered by default */
544 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
545 /* all other flavours have left aligned text */
548 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
550 /* DrawText ignores vertical alignment for multiline text,
551 * but we use these flags to align label manually.
553 if (get_button_type(style) != BS_GROUPBOX)
555 switch (style & BS_VCENTER)
557 case BS_TOP: /* DT_TOP is 0 */ break;
558 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
559 case BS_VCENTER: /* fall through */
560 default: dtStyle |= DT_VCENTER; break;
563 else
564 /* GroupBox's text is always single line and is top aligned. */
565 dtStyle |= DT_SINGLELINE;
567 return dtStyle;
570 /**********************************************************************
571 * BUTTON_CalcLabelRect
573 * Calculates label's rectangle depending on button style.
575 * Returns flags to be passed to DrawText.
576 * Calculated rectangle doesn't take into account button state
577 * (pushed, etc.). If there is nothing to draw (no text/image) output
578 * rectangle is empty, and return value is (UINT)-1.
580 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
582 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
583 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
584 WCHAR *text;
585 ICONINFO iconInfo;
586 BITMAP bm;
587 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
588 RECT r = *rc;
589 INT n;
591 /* Calculate label rectangle according to label type */
592 switch (style & (BS_ICON|BS_BITMAP))
594 case BS_TEXT:
596 HFONT hFont, hPrevFont = 0;
598 if (!(text = get_button_text( hwnd ))) goto empty_rect;
599 if (!text[0])
601 HeapFree( GetProcessHeap(), 0, text );
602 goto empty_rect;
605 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hdc, hFont );
606 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
607 if (hPrevFont) SelectObject( hdc, hPrevFont );
608 HeapFree( GetProcessHeap(), 0, text );
609 break;
612 case BS_ICON:
613 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
614 goto empty_rect;
616 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
618 r.right = r.left + bm.bmWidth;
619 r.bottom = r.top + bm.bmHeight;
621 DeleteObject(iconInfo.hbmColor);
622 DeleteObject(iconInfo.hbmMask);
623 break;
625 case BS_BITMAP:
626 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
627 goto empty_rect;
629 r.right = r.left + bm.bmWidth;
630 r.bottom = r.top + bm.bmHeight;
631 break;
633 default:
634 empty_rect:
635 rc->right = r.left;
636 rc->bottom = r.top;
637 return (UINT)-1;
640 /* Position label inside bounding rectangle according to
641 * alignment flags. (calculated rect is always left-top aligned).
642 * If label is aligned to any side - shift label in opposite
643 * direction to leave extra space for focus rectangle.
645 switch (dtStyle & (DT_CENTER|DT_RIGHT))
647 case DT_LEFT: r.left++; r.right++; break;
648 case DT_CENTER: n = r.right - r.left;
649 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
650 r.right = r.left + n; break;
651 case DT_RIGHT: n = r.right - r.left;
652 r.right = rc->right - 1;
653 r.left = r.right - n;
654 break;
657 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
659 case DT_TOP: r.top++; r.bottom++; break;
660 case DT_VCENTER: n = r.bottom - r.top;
661 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
662 r.bottom = r.top + n; break;
663 case DT_BOTTOM: n = r.bottom - r.top;
664 r.bottom = rc->bottom - 1;
665 r.top = r.bottom - n;
666 break;
669 *rc = r;
670 return dtStyle;
674 /**********************************************************************
675 * BUTTON_DrawTextCallback
677 * Callback function used by DrawStateW function.
679 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
681 RECT rc;
683 SetRect(&rc, 0, 0, cx, cy);
684 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
685 return TRUE;
689 /**********************************************************************
690 * BUTTON_DrawLabel
692 * Common function for drawing button label.
694 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
696 DRAWSTATEPROC lpOutputProc = NULL;
697 LPARAM lp;
698 WPARAM wp = 0;
699 HBRUSH hbr = 0;
700 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
701 LONG state = get_button_state( hwnd );
702 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
703 WCHAR *text = NULL;
705 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
706 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
707 * I don't have Win31 on hand to verify that, so I leave it as is.
710 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
712 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
713 flags |= DSS_MONO;
716 switch (style & (BS_ICON|BS_BITMAP))
718 case BS_TEXT:
719 /* DST_COMPLEX -- is 0 */
720 lpOutputProc = BUTTON_DrawTextCallback;
721 if (!(text = get_button_text( hwnd ))) return;
722 lp = (LPARAM)text;
723 wp = dtFlags;
724 break;
726 case BS_ICON:
727 flags |= DST_ICON;
728 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
729 break;
731 case BS_BITMAP:
732 flags |= DST_BITMAP;
733 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
734 break;
736 default:
737 return;
740 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
741 rc->right - rc->left, rc->bottom - rc->top, flags);
742 HeapFree( GetProcessHeap(), 0, text );
745 /**********************************************************************
746 * Push Button Functions
748 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
750 RECT rc, r;
751 UINT dtFlags, uState;
752 HPEN hOldPen;
753 HBRUSH hOldBrush;
754 INT oldBkMode;
755 COLORREF oldTxtColor;
756 HFONT hFont;
757 LONG state = get_button_state( hwnd );
758 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
759 BOOL pushedState = (state & BST_PUSHED);
760 HWND parent;
761 HRGN hrgn;
763 GetClientRect( hwnd, &rc );
765 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
766 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
767 parent = GetParent(hwnd);
768 if (!parent) parent = hwnd;
769 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
771 hrgn = set_control_clipping( hDC, &rc );
773 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
774 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
775 oldBkMode = SetBkMode(hDC, TRANSPARENT);
777 if (get_button_type(style) == BS_DEFPUSHBUTTON)
779 if (action != ODA_FOCUS)
780 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
781 InflateRect( &rc, -1, -1 );
784 /* completely skip the drawing if only focus has changed */
785 if (action == ODA_FOCUS) goto draw_focus;
787 uState = DFCS_BUTTONPUSH;
789 if (style & BS_FLAT)
790 uState |= DFCS_MONO;
791 else if (pushedState)
793 if (get_button_type(style) == BS_DEFPUSHBUTTON )
794 uState |= DFCS_FLAT;
795 else
796 uState |= DFCS_PUSHED;
799 if (state & (BST_CHECKED | BST_INDETERMINATE))
800 uState |= DFCS_CHECKED;
802 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
804 /* draw button label */
805 r = rc;
806 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
808 if (dtFlags == (UINT)-1L)
809 goto cleanup;
811 if (pushedState)
812 OffsetRect(&r, 1, 1);
814 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
816 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
818 SetTextColor( hDC, oldTxtColor );
820 draw_focus:
821 if (action == ODA_FOCUS || (state & BST_FOCUS))
823 InflateRect( &rc, -2, -2 );
824 DrawFocusRect( hDC, &rc );
827 cleanup:
828 SelectObject( hDC, hOldPen );
829 SelectObject( hDC, hOldBrush );
830 SetBkMode(hDC, oldBkMode);
831 SelectClipRgn( hDC, hrgn );
832 if (hrgn) DeleteObject( hrgn );
835 /**********************************************************************
836 * Check Box & Radio Button Functions
839 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
841 RECT rbox, rtext, client;
842 HBRUSH hBrush;
843 int delta, text_offset, checkBoxWidth, checkBoxHeight;
844 UINT dtFlags;
845 HFONT hFont;
846 LONG state = get_button_state( hwnd );
847 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
848 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
849 HWND parent;
850 HRGN hrgn;
852 if (style & BS_PUSHLIKE)
854 PB_Paint( hwnd, hDC, action );
855 return;
858 GetClientRect(hwnd, &client);
859 rbox = rtext = client;
861 checkBoxWidth = 12 * GetDeviceCaps( hDC, LOGPIXELSX ) / 96 + 1;
862 checkBoxHeight = 12 * GetDeviceCaps( hDC, LOGPIXELSY ) / 96 + 1;
864 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
865 GetCharWidthW( hDC, '0', '0', &text_offset );
866 text_offset /= 2;
868 parent = GetParent(hwnd);
869 if (!parent) parent = hwnd;
870 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
871 (WPARAM)hDC, (LPARAM)hwnd);
872 if (!hBrush) /* did the app forget to call defwindowproc ? */
873 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
874 (WPARAM)hDC, (LPARAM)hwnd );
875 hrgn = set_control_clipping( hDC, &client );
877 if (style & BS_LEFTTEXT || ex_style & WS_EX_RIGHT)
879 rtext.right -= checkBoxWidth + text_offset;
880 rbox.left = rbox.right - checkBoxWidth;
882 else
884 rtext.left += checkBoxWidth + text_offset;
885 rbox.right = checkBoxWidth;
888 /* Since WM_ERASEBKGND does nothing, first prepare background */
889 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
890 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
892 /* Draw label */
893 client = rtext;
894 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
896 /* Only adjust rbox when rtext is valid */
897 if (dtFlags != (UINT)-1L)
899 rbox.top = rtext.top;
900 rbox.bottom = rtext.bottom;
903 /* Draw the check-box bitmap */
904 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
906 UINT flags;
908 if ((get_button_type(style) == BS_RADIOBUTTON) ||
909 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
910 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
911 else flags = DFCS_BUTTONCHECK;
913 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
914 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
916 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
918 /* rbox must have the correct height */
919 delta = rbox.bottom - rbox.top - checkBoxHeight;
921 if (style & BS_TOP) {
922 if (delta > 0) {
923 rbox.bottom = rbox.top + checkBoxHeight;
924 } else {
925 rbox.top -= -delta/2 + 1;
926 rbox.bottom = rbox.top + checkBoxHeight;
928 } else if (style & BS_BOTTOM) {
929 if (delta > 0) {
930 rbox.top = rbox.bottom - checkBoxHeight;
931 } else {
932 rbox.bottom += -delta/2 + 1;
933 rbox.top = rbox.bottom - checkBoxHeight;
935 } else { /* Default */
936 if (delta > 0) {
937 int ofs = (delta / 2);
938 rbox.bottom -= ofs + 1;
939 rbox.top = rbox.bottom - checkBoxHeight;
940 } else if (delta < 0) {
941 int ofs = (-delta / 2);
942 rbox.top -= ofs + 1;
943 rbox.bottom = rbox.top + checkBoxHeight;
947 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
950 if (dtFlags == (UINT)-1L) /* Noting to draw */
951 return;
953 if (action == ODA_DRAWENTIRE)
954 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
956 /* ... and focus */
957 if (action == ODA_FOCUS || (state & BST_FOCUS))
959 rtext.left--;
960 rtext.right++;
961 IntersectRect(&rtext, &rtext, &client);
962 DrawFocusRect( hDC, &rtext );
964 SelectClipRgn( hDC, hrgn );
965 if (hrgn) DeleteObject( hrgn );
969 /**********************************************************************
970 * BUTTON_CheckAutoRadioButton
972 * hwnd is checked, uncheck every other auto radio button in group
974 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
976 HWND parent, sibling, start;
978 parent = GetParent(hwnd);
979 /* make sure that starting control is not disabled or invisible */
980 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
983 if (!sibling) break;
984 if ((hwnd != sibling) &&
985 ((GetWindowLongW( sibling, GWL_STYLE) & BS_TYPEMASK) == BS_AUTORADIOBUTTON))
986 SendMessageW( sibling, BM_SETCHECK, BST_UNCHECKED, 0 );
987 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
988 } while (sibling != start);
992 /**********************************************************************
993 * Group Box Functions
996 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
998 RECT rc, rcFrame;
999 HBRUSH hbr;
1000 HFONT hFont;
1001 UINT dtFlags;
1002 TEXTMETRICW tm;
1003 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1004 HWND parent;
1005 HRGN hrgn;
1007 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1008 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1009 parent = GetParent(hwnd);
1010 if (!parent) parent = hwnd;
1011 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1012 if (!hbr) /* did the app forget to call defwindowproc ? */
1013 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1014 (WPARAM)hDC, (LPARAM)hwnd);
1015 GetClientRect( hwnd, &rc);
1016 rcFrame = rc;
1017 hrgn = set_control_clipping( hDC, &rc );
1019 GetTextMetricsW (hDC, &tm);
1020 rcFrame.top += (tm.tmHeight / 2) - 1;
1021 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1023 InflateRect(&rc, -7, 1);
1024 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1026 if (dtFlags != (UINT)-1)
1028 /* Because buttons have CS_PARENTDC class style, there is a chance
1029 * that label will be drawn out of client rect.
1030 * But Windows doesn't clip label's rect, so do I.
1033 /* There is 1-pixel margin at the left, right, and bottom */
1034 rc.left--; rc.right++; rc.bottom++;
1035 FillRect(hDC, &rc, hbr);
1036 rc.left++; rc.right--; rc.bottom--;
1038 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1040 SelectClipRgn( hDC, hrgn );
1041 if (hrgn) DeleteObject( hrgn );
1045 /**********************************************************************
1046 * User Button Functions
1049 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1051 RECT rc;
1052 HBRUSH hBrush;
1053 HFONT hFont;
1054 LONG state = get_button_state( hwnd );
1055 HWND parent;
1057 GetClientRect( hwnd, &rc);
1059 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1061 parent = GetParent(hwnd);
1062 if (!parent) parent = hwnd;
1063 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1064 if (!hBrush) /* did the app forget to call defwindowproc ? */
1065 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1066 (WPARAM)hDC, (LPARAM)hwnd);
1068 FillRect( hDC, &rc, hBrush );
1069 if (action == ODA_FOCUS || (state & BST_FOCUS))
1070 DrawFocusRect( hDC, &rc );
1072 switch (action)
1074 case ODA_FOCUS:
1075 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1076 break;
1078 case ODA_SELECT:
1079 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1080 break;
1082 default:
1083 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1084 break;
1089 /**********************************************************************
1090 * Ownerdrawn Button Functions
1093 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1095 LONG state = get_button_state( hwnd );
1096 DRAWITEMSTRUCT dis;
1097 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1098 HWND parent;
1099 HFONT hFont;
1100 HRGN hrgn;
1102 dis.CtlType = ODT_BUTTON;
1103 dis.CtlID = id;
1104 dis.itemID = 0;
1105 dis.itemAction = action;
1106 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1107 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1108 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1109 dis.hwndItem = hwnd;
1110 dis.hDC = hDC;
1111 dis.itemData = 0;
1112 GetClientRect( hwnd, &dis.rcItem );
1114 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1115 parent = GetParent(hwnd);
1116 if (!parent) parent = hwnd;
1117 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1119 hrgn = set_control_clipping( hDC, &dis.rcItem );
1121 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1122 SelectClipRgn( hDC, hrgn );
1123 if (hrgn) DeleteObject( hrgn );