bcrypt: Make format_gnutls_signature() static.
[wine.git] / dlls / user32 / button.c
blob9a011ba266cab4e4414e22e6ab3a08210ae5c034
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
32 * Notifications
33 * - BN_DISABLE
34 * - BN_PUSHED/BN_HILITE
35 * + BN_KILLFOCUS: is it OK?
36 * - BN_PAINT
37 * + BN_SETFOCUS: is it OK?
38 * - BN_UNPUSHED/BN_UNHILITE
41 #include <stdarg.h>
42 #include <string.h>
43 #include <stdlib.h>
45 #define OEMRESOURCE
47 #include "windef.h"
48 #include "winbase.h"
49 #include "wingdi.h"
50 #include "controls.h"
51 #include "win.h"
52 #include "user_private.h"
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(button);
57 /* GetWindowLong offsets for window extra information */
58 #define STATE_GWL_OFFSET 0
59 #define HFONT_GWL_OFFSET (sizeof(LONG))
60 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
61 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
63 /* undocumented flags */
64 #define BUTTON_NSTATES 0x0F
65 #define BUTTON_BTNPRESSED 0x40
66 #define BUTTON_UNKNOWN2 0x20
67 #define BUTTON_UNKNOWN3 0x10
69 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
70 do { /* Notify parent which has created this button control */ \
71 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
72 SendMessageW(GetParent(hWnd), WM_COMMAND, \
73 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
74 (LPARAM)(hWnd)); \
75 } while(0)
77 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
78 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
79 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
80 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
81 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
82 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
83 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
85 #define MAX_BTN_TYPE 16
87 static const WORD maxCheckState[MAX_BTN_TYPE] =
89 BST_UNCHECKED, /* BS_PUSHBUTTON */
90 BST_UNCHECKED, /* BS_DEFPUSHBUTTON */
91 BST_CHECKED, /* BS_CHECKBOX */
92 BST_CHECKED, /* BS_AUTOCHECKBOX */
93 BST_CHECKED, /* BS_RADIOBUTTON */
94 BST_INDETERMINATE, /* BS_3STATE */
95 BST_INDETERMINATE, /* BS_AUTO3STATE */
96 BST_UNCHECKED, /* BS_GROUPBOX */
97 BST_UNCHECKED, /* BS_USERBUTTON */
98 BST_CHECKED, /* BS_AUTORADIOBUTTON */
99 BST_UNCHECKED, /* BS_PUSHBOX */
100 BST_UNCHECKED /* BS_OWNERDRAW */
103 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
105 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
107 PB_Paint, /* BS_PUSHBUTTON */
108 PB_Paint, /* BS_DEFPUSHBUTTON */
109 CB_Paint, /* BS_CHECKBOX */
110 CB_Paint, /* BS_AUTOCHECKBOX */
111 CB_Paint, /* BS_RADIOBUTTON */
112 CB_Paint, /* BS_3STATE */
113 CB_Paint, /* BS_AUTO3STATE */
114 GB_Paint, /* BS_GROUPBOX */
115 UB_Paint, /* BS_USERBUTTON */
116 CB_Paint, /* BS_AUTORADIOBUTTON */
117 NULL, /* BS_PUSHBOX */
118 OB_Paint /* BS_OWNERDRAW */
121 /*********************************************************************
122 * button class descriptor
124 static const WCHAR buttonW[] = {'B','u','t','t','o','n',0};
125 const struct builtin_class_descr BUTTON_builtin_class =
127 buttonW, /* name */
128 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
129 WINPROC_BUTTON, /* proc */
130 NB_EXTRA_BYTES, /* extra */
131 IDC_ARROW, /* cursor */
132 0 /* brush */
136 static inline LONG get_button_state( HWND hwnd )
138 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
141 static inline void set_button_state( HWND hwnd, LONG state )
143 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
146 static inline HFONT get_button_font( HWND hwnd )
148 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
151 static inline void set_button_font( HWND hwnd, HFONT font )
153 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
156 static inline UINT get_button_type( LONG window_style )
158 return (window_style & BS_TYPEMASK);
161 /* paint a button of any type */
162 static inline void paint_button( HWND hwnd, LONG style, UINT action )
164 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
166 HDC hdc = GetDC( hwnd );
167 btnPaintFunc[style]( hwnd, hdc, action );
168 ReleaseDC( hwnd, hdc );
172 /* retrieve the button text; returned buffer must be freed by caller */
173 static inline WCHAR *get_button_text( HWND hwnd )
175 static const INT len = 512;
176 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
177 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
178 return buffer;
181 /***********************************************************************
182 * ButtonWndProc_common
184 LRESULT ButtonWndProc_common(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL unicode )
186 RECT rect;
187 POINT pt;
188 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
189 UINT btn_type = get_button_type( style );
190 LONG state;
191 HANDLE oldHbitmap;
193 if (!IsWindow( hWnd )) return 0;
195 pt.x = (short)LOWORD(lParam);
196 pt.y = (short)HIWORD(lParam);
198 switch (uMsg)
200 case WM_GETDLGCODE:
201 switch(btn_type)
203 case BS_USERBUTTON:
204 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
205 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
206 case BS_RADIOBUTTON:
207 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
208 case BS_GROUPBOX: return DLGC_STATIC;
209 default: return DLGC_BUTTON;
212 case WM_ENABLE:
213 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
214 break;
216 case WM_CREATE:
217 if (btn_type >= MAX_BTN_TYPE)
218 return -1; /* abort */
220 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
221 if (btn_type == BS_USERBUTTON )
223 style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
224 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
226 set_button_state( hWnd, BST_UNCHECKED );
227 return 0;
229 case WM_ERASEBKGND:
230 if (btn_type == BS_OWNERDRAW)
232 HDC hdc = (HDC)wParam;
233 RECT rc;
234 HBRUSH hBrush;
235 HWND parent = GetParent(hWnd);
236 if (!parent) parent = hWnd;
237 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
238 if (!hBrush) /* did the app forget to call defwindowproc ? */
239 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
240 (WPARAM)hdc, (LPARAM)hWnd);
241 GetClientRect(hWnd, &rc);
242 FillRect(hdc, &rc, hBrush);
244 return 1;
246 case WM_PRINTCLIENT:
247 case WM_PAINT:
249 PAINTSTRUCT ps;
250 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
251 if (btnPaintFunc[btn_type])
253 int nOldMode = SetBkMode( hdc, OPAQUE );
254 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
255 SetBkMode(hdc, nOldMode); /* reset painting mode */
257 if ( !wParam ) EndPaint( hWnd, &ps );
258 break;
261 case WM_KEYDOWN:
262 if (wParam == VK_SPACE)
264 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
265 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
266 SetCapture( hWnd );
268 break;
270 case WM_LBUTTONDBLCLK:
271 if(style & BS_NOTIFY ||
272 btn_type == BS_RADIOBUTTON ||
273 btn_type == BS_USERBUTTON ||
274 btn_type == BS_OWNERDRAW)
276 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
277 break;
279 /* fall through */
280 case WM_LBUTTONDOWN:
281 SetCapture( hWnd );
282 SetFocus( hWnd );
283 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
284 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
285 break;
287 case WM_KEYUP:
288 if (wParam != VK_SPACE)
289 break;
290 /* fall through */
291 case WM_LBUTTONUP:
292 state = get_button_state( hWnd );
293 if (!(state & BUTTON_BTNPRESSED)) break;
294 state &= BUTTON_NSTATES;
295 set_button_state( hWnd, state );
296 if (!(state & BST_PUSHED))
298 ReleaseCapture();
299 break;
301 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
302 GetClientRect( hWnd, &rect );
303 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
305 state = get_button_state( hWnd );
306 switch(btn_type)
308 case BS_AUTOCHECKBOX:
309 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
310 break;
311 case BS_AUTORADIOBUTTON:
312 BUTTON_CheckAutoRadioButton( hWnd );
313 break;
314 case BS_AUTO3STATE:
315 SendMessageW( hWnd, BM_SETCHECK,
316 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
317 break;
319 ReleaseCapture();
320 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
322 else
324 ReleaseCapture();
326 break;
328 case WM_CAPTURECHANGED:
329 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
330 if (hWnd == (HWND)lParam) break;
331 state = get_button_state( hWnd );
332 if (state & BUTTON_BTNPRESSED)
334 state &= BUTTON_NSTATES;
335 set_button_state( hWnd, state );
336 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
338 break;
340 case WM_MOUSEMOVE:
341 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
343 GetClientRect( hWnd, &rect );
344 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
346 break;
348 case WM_SETTEXT:
350 /* Clear an old text here as Windows does */
351 if (IsWindowVisible(hWnd))
353 HDC hdc = GetDC(hWnd);
354 HBRUSH hbrush;
355 RECT client, rc;
356 HWND parent = GetParent(hWnd);
357 UINT message = (btn_type == BS_PUSHBUTTON ||
358 btn_type == BS_DEFPUSHBUTTON ||
359 btn_type == BS_USERBUTTON ||
360 btn_type == BS_OWNERDRAW) ?
361 WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
363 if (!parent) parent = hWnd;
364 hbrush = (HBRUSH)SendMessageW(parent, message,
365 (WPARAM)hdc, (LPARAM)hWnd);
366 if (!hbrush) /* did the app forget to call DefWindowProc ? */
367 hbrush = (HBRUSH)DefWindowProcW(parent, message,
368 (WPARAM)hdc, (LPARAM)hWnd);
370 GetClientRect(hWnd, &client);
371 rc = client;
372 /* FIXME: check other BS_* handlers */
373 if (btn_type == BS_GROUPBOX)
374 InflateRect(&rc, -7, 1); /* GB_Paint does this */
375 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
376 /* Clip by client rect bounds */
377 if (rc.right > client.right) rc.right = client.right;
378 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
379 FillRect(hdc, &rc, hbrush);
380 ReleaseDC(hWnd, hdc);
383 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
384 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
385 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
386 InvalidateRect( hWnd, NULL, TRUE );
387 else
388 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
389 return 1; /* success. FIXME: check text length */
392 case WM_SETFONT:
393 set_button_font( hWnd, (HFONT)wParam );
394 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
395 break;
397 case WM_GETFONT:
398 return (LRESULT)get_button_font( hWnd );
400 case WM_SETFOCUS:
401 TRACE("WM_SETFOCUS %p\n",hWnd);
402 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
403 paint_button( hWnd, btn_type, ODA_FOCUS );
404 if (style & BS_NOTIFY)
405 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
406 break;
408 case WM_KILLFOCUS:
409 TRACE("WM_KILLFOCUS %p\n",hWnd);
410 state = get_button_state( hWnd );
411 set_button_state( hWnd, state & ~BST_FOCUS );
412 paint_button( hWnd, btn_type, ODA_FOCUS );
414 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
415 ReleaseCapture();
416 if (style & BS_NOTIFY)
417 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
419 InvalidateRect( hWnd, NULL, FALSE );
420 break;
422 case WM_SYSCOLORCHANGE:
423 InvalidateRect( hWnd, NULL, FALSE );
424 break;
426 case BM_SETSTYLE:
427 btn_type = wParam & BS_TYPEMASK;
428 style = (style & ~BS_TYPEMASK) | btn_type;
429 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
431 /* Only redraw if lParam flag is set.*/
432 if (lParam)
433 InvalidateRect( hWnd, NULL, TRUE );
435 break;
437 case BM_CLICK:
438 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
439 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
440 break;
442 case BM_SETIMAGE:
443 /* Check that image format matches button style */
444 switch (style & (BS_BITMAP|BS_ICON))
446 case BS_BITMAP:
447 if (wParam != IMAGE_BITMAP) return 0;
448 break;
449 case BS_ICON:
450 if (wParam != IMAGE_ICON) return 0;
451 break;
452 default:
453 return 0;
455 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
456 InvalidateRect( hWnd, NULL, FALSE );
457 return (LRESULT)oldHbitmap;
459 case BM_GETIMAGE:
460 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
462 case BM_GETCHECK:
463 return get_button_state( hWnd ) & 3;
465 case BM_SETCHECK:
466 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
467 state = get_button_state( hWnd );
468 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
470 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
471 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
473 if ((state & 3) != wParam)
475 set_button_state( hWnd, (state & ~3) | wParam );
476 paint_button( hWnd, btn_type, ODA_SELECT );
478 break;
480 case BM_GETSTATE:
481 return get_button_state( hWnd );
483 case BM_SETSTATE:
484 state = get_button_state( hWnd );
485 if (wParam)
486 set_button_state( hWnd, state | BST_PUSHED );
487 else
488 set_button_state( hWnd, state & ~BST_PUSHED );
490 paint_button( hWnd, btn_type, ODA_SELECT );
491 break;
493 case WM_NCHITTEST:
494 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
495 /* fall through */
496 default:
497 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
498 DefWindowProcA(hWnd, uMsg, wParam, lParam);
500 return 0;
503 /**********************************************************************
504 * Convert button styles to flags used by DrawText.
506 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
508 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
510 /* "Convert" pushlike buttons to pushbuttons */
511 if (style & BS_PUSHLIKE)
512 style &= ~BS_TYPEMASK;
514 if (!(style & BS_MULTILINE))
515 dtStyle |= DT_SINGLELINE;
516 else
517 dtStyle |= DT_WORDBREAK;
519 switch (style & BS_CENTER)
521 case BS_LEFT: /* DT_LEFT is 0 */ break;
522 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
523 case BS_CENTER: dtStyle |= DT_CENTER; break;
524 default:
525 /* Pushbutton's text is centered by default */
526 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
527 /* all other flavours have left aligned text */
530 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
532 /* DrawText ignores vertical alignment for multiline text,
533 * but we use these flags to align label manually.
535 if (get_button_type(style) != BS_GROUPBOX)
537 switch (style & BS_VCENTER)
539 case BS_TOP: /* DT_TOP is 0 */ break;
540 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
541 case BS_VCENTER: /* fall through */
542 default: dtStyle |= DT_VCENTER; break;
545 else
546 /* GroupBox's text is always single line and is top aligned. */
547 dtStyle |= DT_SINGLELINE;
549 return dtStyle;
552 /**********************************************************************
553 * BUTTON_CalcLabelRect
555 * Calculates label's rectangle depending on button style.
557 * Returns flags to be passed to DrawText.
558 * Calculated rectangle doesn't take into account button state
559 * (pushed, etc.). If there is nothing to draw (no text/image) output
560 * rectangle is empty, and return value is (UINT)-1.
562 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
564 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
565 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
566 WCHAR *text;
567 ICONINFO iconInfo;
568 BITMAP bm;
569 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
570 RECT r = *rc;
571 INT n;
573 /* Calculate label rectangle according to label type */
574 switch (style & (BS_ICON|BS_BITMAP))
576 case BS_TEXT:
578 HFONT hFont, hPrevFont = 0;
580 if (!(text = get_button_text( hwnd ))) goto empty_rect;
581 if (!text[0])
583 HeapFree( GetProcessHeap(), 0, text );
584 goto empty_rect;
587 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hdc, hFont );
588 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
589 if (hPrevFont) SelectObject( hdc, hPrevFont );
590 HeapFree( GetProcessHeap(), 0, text );
591 break;
594 case BS_ICON:
595 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
596 goto empty_rect;
598 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
600 r.right = r.left + bm.bmWidth;
601 r.bottom = r.top + bm.bmHeight;
603 DeleteObject(iconInfo.hbmColor);
604 DeleteObject(iconInfo.hbmMask);
605 break;
607 case BS_BITMAP:
608 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
609 goto empty_rect;
611 r.right = r.left + bm.bmWidth;
612 r.bottom = r.top + bm.bmHeight;
613 break;
615 default:
616 empty_rect:
617 rc->right = r.left;
618 rc->bottom = r.top;
619 return (UINT)-1;
622 /* Position label inside bounding rectangle according to
623 * alignment flags. (calculated rect is always left-top aligned).
624 * If label is aligned to any side - shift label in opposite
625 * direction to leave extra space for focus rectangle.
627 switch (dtStyle & (DT_CENTER|DT_RIGHT))
629 case DT_LEFT: r.left++; r.right++; break;
630 case DT_CENTER: n = r.right - r.left;
631 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
632 r.right = r.left + n; break;
633 case DT_RIGHT: n = r.right - r.left;
634 r.right = rc->right - 1;
635 r.left = r.right - n;
636 break;
639 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
641 case DT_TOP: r.top++; r.bottom++; break;
642 case DT_VCENTER: n = r.bottom - r.top;
643 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
644 r.bottom = r.top + n; break;
645 case DT_BOTTOM: n = r.bottom - r.top;
646 r.bottom = rc->bottom - 1;
647 r.top = r.bottom - n;
648 break;
651 *rc = r;
652 return dtStyle;
656 /**********************************************************************
657 * BUTTON_DrawTextCallback
659 * Callback function used by DrawStateW function.
661 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
663 RECT rc;
665 SetRect(&rc, 0, 0, cx, cy);
666 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
667 return TRUE;
671 /**********************************************************************
672 * BUTTON_DrawLabel
674 * Common function for drawing button label.
676 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
678 DRAWSTATEPROC lpOutputProc = NULL;
679 LPARAM lp;
680 WPARAM wp = 0;
681 HBRUSH hbr = 0;
682 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
683 LONG state = get_button_state( hwnd );
684 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
685 WCHAR *text = NULL;
687 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
688 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
689 * I don't have Win31 on hand to verify that, so I leave it as is.
692 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
694 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
695 flags |= DSS_MONO;
698 switch (style & (BS_ICON|BS_BITMAP))
700 case BS_TEXT:
701 /* DST_COMPLEX -- is 0 */
702 lpOutputProc = BUTTON_DrawTextCallback;
703 if (!(text = get_button_text( hwnd ))) return;
704 lp = (LPARAM)text;
705 wp = dtFlags;
706 break;
708 case BS_ICON:
709 flags |= DST_ICON;
710 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
711 break;
713 case BS_BITMAP:
714 flags |= DST_BITMAP;
715 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
716 break;
718 default:
719 return;
722 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
723 rc->right - rc->left, rc->bottom - rc->top, flags);
724 HeapFree( GetProcessHeap(), 0, text );
727 /**********************************************************************
728 * Push Button Functions
730 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
732 RECT rc, r;
733 UINT dtFlags, uState;
734 HPEN hOldPen;
735 HBRUSH hOldBrush;
736 INT oldBkMode;
737 COLORREF oldTxtColor;
738 HFONT hFont;
739 LONG state = get_button_state( hwnd );
740 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
741 BOOL pushedState = (state & BST_PUSHED);
742 HWND parent;
743 HRGN hrgn;
745 GetClientRect( hwnd, &rc );
747 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
748 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
749 parent = GetParent(hwnd);
750 if (!parent) parent = hwnd;
751 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
753 hrgn = set_control_clipping( hDC, &rc );
755 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
756 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
757 oldBkMode = SetBkMode(hDC, TRANSPARENT);
759 if (get_button_type(style) == BS_DEFPUSHBUTTON)
761 if (action != ODA_FOCUS)
762 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
763 InflateRect( &rc, -1, -1 );
766 /* completely skip the drawing if only focus has changed */
767 if (action == ODA_FOCUS) goto draw_focus;
769 uState = DFCS_BUTTONPUSH;
771 if (style & BS_FLAT)
772 uState |= DFCS_MONO;
773 else if (pushedState)
775 if (get_button_type(style) == BS_DEFPUSHBUTTON )
776 uState |= DFCS_FLAT;
777 else
778 uState |= DFCS_PUSHED;
781 if (state & (BST_CHECKED | BST_INDETERMINATE))
782 uState |= DFCS_CHECKED;
784 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
786 /* draw button label */
787 r = rc;
788 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
790 if (dtFlags == (UINT)-1L)
791 goto cleanup;
793 if (pushedState)
794 OffsetRect(&r, 1, 1);
796 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
798 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
800 SetTextColor( hDC, oldTxtColor );
802 draw_focus:
803 if (action == ODA_FOCUS || (state & BST_FOCUS))
805 InflateRect( &rc, -2, -2 );
806 DrawFocusRect( hDC, &rc );
809 cleanup:
810 SelectObject( hDC, hOldPen );
811 SelectObject( hDC, hOldBrush );
812 SetBkMode(hDC, oldBkMode);
813 SelectClipRgn( hDC, hrgn );
814 if (hrgn) DeleteObject( hrgn );
817 /**********************************************************************
818 * Check Box & Radio Button Functions
821 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
823 RECT rbox, rtext, client;
824 HBRUSH hBrush;
825 int delta, text_offset, checkBoxWidth, checkBoxHeight;
826 UINT dtFlags;
827 HFONT hFont;
828 LONG state = get_button_state( hwnd );
829 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
830 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
831 HWND parent;
832 HRGN hrgn;
834 if (style & BS_PUSHLIKE)
836 PB_Paint( hwnd, hDC, action );
837 return;
840 GetClientRect(hwnd, &client);
841 rbox = rtext = client;
843 checkBoxWidth = 12 * GetDpiForWindow( hwnd ) / 96 + 1;
844 checkBoxHeight = 12 * GetDpiForWindow( hwnd ) / 96 + 1;
846 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
847 GetCharWidthW( hDC, '0', '0', &text_offset );
848 text_offset /= 2;
850 parent = GetParent(hwnd);
851 if (!parent) parent = hwnd;
852 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
853 (WPARAM)hDC, (LPARAM)hwnd);
854 if (!hBrush) /* did the app forget to call defwindowproc ? */
855 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
856 (WPARAM)hDC, (LPARAM)hwnd );
857 hrgn = set_control_clipping( hDC, &client );
859 if (style & BS_LEFTTEXT || ex_style & WS_EX_RIGHT)
861 rtext.right -= checkBoxWidth + text_offset;
862 rbox.left = rbox.right - checkBoxWidth;
864 else
866 rtext.left += checkBoxWidth + text_offset;
867 rbox.right = checkBoxWidth;
870 /* Since WM_ERASEBKGND does nothing, first prepare background */
871 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
872 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
874 /* Draw label */
875 client = rtext;
876 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
878 /* Only adjust rbox when rtext is valid */
879 if (dtFlags != (UINT)-1L)
881 rbox.top = rtext.top;
882 rbox.bottom = rtext.bottom;
885 /* Draw the check-box bitmap */
886 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
888 UINT flags;
890 if ((get_button_type(style) == BS_RADIOBUTTON) ||
891 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
892 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
893 else flags = DFCS_BUTTONCHECK;
895 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
896 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
898 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
900 /* rbox must have the correct height */
901 delta = rbox.bottom - rbox.top - checkBoxHeight;
903 if ((style & BS_VCENTER) == BS_TOP) {
904 if (delta > 0) {
905 rbox.bottom = rbox.top + checkBoxHeight;
906 } else {
907 rbox.top -= -delta/2 + 1;
908 rbox.bottom = rbox.top + checkBoxHeight;
910 } else if ((style & BS_VCENTER) == BS_BOTTOM) {
911 if (delta > 0) {
912 rbox.top = rbox.bottom - checkBoxHeight;
913 } else {
914 rbox.bottom += -delta/2 + 1;
915 rbox.top = rbox.bottom - checkBoxHeight;
917 } else { /* Default */
918 if (delta > 0) {
919 int ofs = (delta / 2);
920 rbox.bottom -= ofs + 1;
921 rbox.top = rbox.bottom - checkBoxHeight;
922 } else if (delta < 0) {
923 int ofs = (-delta / 2);
924 rbox.top -= ofs + 1;
925 rbox.bottom = rbox.top + checkBoxHeight;
929 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
932 if (dtFlags == (UINT)-1L) /* Noting to draw */
933 return;
935 if (action == ODA_DRAWENTIRE)
936 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
938 /* ... and focus */
939 if (action == ODA_FOCUS || (state & BST_FOCUS))
941 rtext.left--;
942 rtext.right++;
943 IntersectRect(&rtext, &rtext, &client);
944 DrawFocusRect( hDC, &rtext );
946 SelectClipRgn( hDC, hrgn );
947 if (hrgn) DeleteObject( hrgn );
951 /**********************************************************************
952 * BUTTON_CheckAutoRadioButton
954 * hwnd is checked, uncheck every other auto radio button in group
956 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
958 HWND parent, sibling, start;
960 parent = GetParent(hwnd);
961 /* make sure that starting control is not disabled or invisible */
962 start = sibling = hwnd;
965 if (!sibling) break;
966 if (SendMessageW( sibling, WM_GETDLGCODE, 0, 0 ) == (DLGC_BUTTON | DLGC_RADIOBUTTON))
967 SendMessageW( sibling, BM_SETCHECK, sibling == hwnd ? BST_CHECKED : BST_UNCHECKED, 0 );
968 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
969 } while (sibling != start);
973 /**********************************************************************
974 * Group Box Functions
977 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
979 RECT rc, rcFrame;
980 HBRUSH hbr;
981 HFONT hFont;
982 UINT dtFlags;
983 TEXTMETRICW tm;
984 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
985 HWND parent;
986 HRGN hrgn;
988 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
989 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
990 parent = GetParent(hwnd);
991 if (!parent) parent = hwnd;
992 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
993 if (!hbr) /* did the app forget to call defwindowproc ? */
994 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
995 (WPARAM)hDC, (LPARAM)hwnd);
996 GetClientRect( hwnd, &rc);
997 rcFrame = rc;
998 hrgn = set_control_clipping( hDC, &rc );
1000 GetTextMetricsW (hDC, &tm);
1001 rcFrame.top += (tm.tmHeight / 2) - 1;
1002 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1004 InflateRect(&rc, -7, 1);
1005 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1007 if (dtFlags != (UINT)-1)
1009 /* Because buttons have CS_PARENTDC class style, there is a chance
1010 * that label will be drawn out of client rect.
1011 * But Windows doesn't clip label's rect, so do I.
1014 /* There is 1-pixel margin at the left, right, and bottom */
1015 rc.left--; rc.right++; rc.bottom++;
1016 FillRect(hDC, &rc, hbr);
1017 rc.left++; rc.right--; rc.bottom--;
1019 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1021 SelectClipRgn( hDC, hrgn );
1022 if (hrgn) DeleteObject( hrgn );
1026 /**********************************************************************
1027 * User Button Functions
1030 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1032 RECT rc;
1033 HBRUSH hBrush;
1034 HFONT hFont;
1035 LONG state = get_button_state( hwnd );
1036 HWND parent;
1038 GetClientRect( hwnd, &rc);
1040 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1042 parent = GetParent(hwnd);
1043 if (!parent) parent = hwnd;
1044 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1045 if (!hBrush) /* did the app forget to call defwindowproc ? */
1046 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1047 (WPARAM)hDC, (LPARAM)hwnd);
1049 FillRect( hDC, &rc, hBrush );
1050 if (action == ODA_FOCUS || (state & BST_FOCUS))
1051 DrawFocusRect( hDC, &rc );
1053 switch (action)
1055 case ODA_FOCUS:
1056 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1057 break;
1059 case ODA_SELECT:
1060 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1061 break;
1063 default:
1064 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1065 break;
1070 /**********************************************************************
1071 * Ownerdrawn Button Functions
1074 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1076 LONG state = get_button_state( hwnd );
1077 DRAWITEMSTRUCT dis;
1078 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1079 HWND parent;
1080 HFONT hFont;
1081 HRGN hrgn;
1083 dis.CtlType = ODT_BUTTON;
1084 dis.CtlID = id;
1085 dis.itemID = 0;
1086 dis.itemAction = action;
1087 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1088 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1089 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1090 dis.hwndItem = hwnd;
1091 dis.hDC = hDC;
1092 dis.itemData = 0;
1093 GetClientRect( hwnd, &dis.rcItem );
1095 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1096 parent = GetParent(hwnd);
1097 if (!parent) parent = hwnd;
1098 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1100 hrgn = set_control_clipping( hDC, &dis.rcItem );
1102 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1103 SelectClipRgn( hDC, hrgn );
1104 if (hrgn) DeleteObject( hrgn );