Added OLEIVERB_INPLACEACTIVATE implementation in IOleObject::DoVerb.
[wine.git] / dlls / user / button.c
blob5d56db7c06b05161ecc3537dba72fd198336dda4
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 #include "windef.h"
72 #include "winbase.h"
73 #include "wingdi.h"
74 #include "wine/winuser16.h"
75 #include "controls.h"
76 #include "user_private.h"
77 #include "wine/debug.h"
79 WINE_DEFAULT_DEBUG_CHANNEL(button);
81 /* GetWindowLong offsets for window extra information */
82 #define STATE_GWL_OFFSET 0
83 #define HFONT_GWL_OFFSET (sizeof(LONG))
84 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
85 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
87 /* Button state values */
88 #define BUTTON_UNCHECKED 0x00
89 #define BUTTON_CHECKED 0x01
90 #define BUTTON_3STATE 0x02
91 #define BUTTON_HIGHLIGHTED 0x04
92 #define BUTTON_HASFOCUS 0x08
93 #define BUTTON_NSTATES 0x0F
94 /* undocumented flags */
95 #define BUTTON_BTNPRESSED 0x40
96 #define BUTTON_UNKNOWN2 0x20
97 #define BUTTON_UNKNOWN3 0x10
99 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
100 do { /* Notify parent which has created this button control */ \
101 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
102 SendMessageW(GetParent(hWnd), WM_COMMAND, \
103 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
104 (LPARAM)(hWnd)); \
105 } while(0)
107 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
108 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
109 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
110 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
111 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
112 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
113 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
114 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
115 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
117 #define MAX_BTN_TYPE 12
119 static const WORD maxCheckState[MAX_BTN_TYPE] =
121 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
122 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
123 BUTTON_CHECKED, /* BS_CHECKBOX */
124 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
125 BUTTON_CHECKED, /* BS_RADIOBUTTON */
126 BUTTON_3STATE, /* BS_3STATE */
127 BUTTON_3STATE, /* BS_AUTO3STATE */
128 BUTTON_UNCHECKED, /* BS_GROUPBOX */
129 BUTTON_UNCHECKED, /* BS_USERBUTTON */
130 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
131 BUTTON_UNCHECKED, /* Not defined */
132 BUTTON_UNCHECKED /* BS_OWNERDRAW */
135 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
137 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
139 PB_Paint, /* BS_PUSHBUTTON */
140 PB_Paint, /* BS_DEFPUSHBUTTON */
141 CB_Paint, /* BS_CHECKBOX */
142 CB_Paint, /* BS_AUTOCHECKBOX */
143 CB_Paint, /* BS_RADIOBUTTON */
144 CB_Paint, /* BS_3STATE */
145 CB_Paint, /* BS_AUTO3STATE */
146 GB_Paint, /* BS_GROUPBOX */
147 UB_Paint, /* BS_USERBUTTON */
148 CB_Paint, /* BS_AUTORADIOBUTTON */
149 NULL, /* Not defined */
150 OB_Paint /* BS_OWNERDRAW */
153 static HBITMAP hbitmapCheckBoxes = 0;
154 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
157 /*********************************************************************
158 * button class descriptor
160 const struct builtin_class_descr BUTTON_builtin_class =
162 "Button", /* name */
163 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
164 ButtonWndProcA, /* procA */
165 ButtonWndProcW, /* procW */
166 NB_EXTRA_BYTES, /* extra */
167 IDC_ARROW, /* cursor */
168 0 /* brush */
172 inline static LONG get_button_state( HWND hwnd )
174 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
177 inline static void set_button_state( HWND hwnd, LONG state )
179 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
182 inline static HFONT get_button_font( HWND hwnd )
184 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
187 inline static void set_button_font( HWND hwnd, HFONT font )
189 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
192 inline static UINT get_button_type( LONG window_style )
194 return (window_style & 0x0f);
197 /* paint a button of any type */
198 inline static void paint_button( HWND hwnd, LONG style, UINT action )
200 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
202 HDC hdc = GetDC( hwnd );
203 btnPaintFunc[style]( hwnd, hdc, action );
204 ReleaseDC( hwnd, hdc );
208 /* retrieve the button text; returned buffer must be freed by caller */
209 inline static WCHAR *get_button_text( HWND hwnd )
211 INT len = 512;
212 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
213 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
214 return buffer;
217 /***********************************************************************
218 * ButtonWndProc_common
220 static LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
221 WPARAM wParam, LPARAM lParam, BOOL unicode )
223 RECT rect;
224 POINT pt;
225 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
226 UINT btn_type = get_button_type( style );
227 LONG state;
228 HANDLE oldHbitmap;
230 pt.x = LOWORD(lParam);
231 pt.y = HIWORD(lParam);
233 switch (uMsg)
235 case WM_GETDLGCODE:
236 switch(btn_type)
238 case BS_USERBUTTON:
239 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
240 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
241 case BS_RADIOBUTTON:
242 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
243 case BS_GROUPBOX: return DLGC_STATIC;
244 default: return DLGC_BUTTON;
247 case WM_ENABLE:
248 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
249 break;
251 case WM_CREATE:
252 if (!hbitmapCheckBoxes)
254 BITMAP bmp;
255 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
256 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
257 checkBoxWidth = bmp.bmWidth / 4;
258 checkBoxHeight = bmp.bmHeight / 3;
260 if (btn_type >= MAX_BTN_TYPE)
261 return -1; /* abort */
262 set_button_state( hWnd, BUTTON_UNCHECKED );
263 return 0;
265 case WM_ERASEBKGND:
266 if (btn_type == BS_OWNERDRAW)
268 HDC hdc = (HDC)wParam;
269 RECT rc;
270 HBRUSH hBrush;
271 HWND parent = GetParent(hWnd);
272 if (!parent) parent = hWnd;
273 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
274 if (!hBrush) /* did the app forget to call defwindowproc ? */
275 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
276 (WPARAM)hdc, (LPARAM)hWnd);
277 GetClientRect(hWnd, &rc);
278 FillRect(hdc, &rc, hBrush);
280 return 1;
282 case WM_PRINTCLIENT:
283 case WM_PAINT:
284 if (btnPaintFunc[btn_type])
286 PAINTSTRUCT ps;
287 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
288 int nOldMode = SetBkMode( hdc, OPAQUE );
289 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
290 SetBkMode(hdc, nOldMode); /* reset painting mode */
291 if( !wParam ) EndPaint( hWnd, &ps );
293 break;
295 case WM_KEYDOWN:
296 if (wParam == VK_SPACE)
298 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
299 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
301 break;
303 case WM_LBUTTONDBLCLK:
304 if(style & BS_NOTIFY ||
305 btn_type == BS_RADIOBUTTON ||
306 btn_type == BS_USERBUTTON ||
307 btn_type == BS_OWNERDRAW)
309 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
310 break;
312 /* fall through */
313 case WM_LBUTTONDOWN:
314 SetCapture( hWnd );
315 SetFocus( hWnd );
316 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
317 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
318 break;
320 case WM_KEYUP:
321 if (wParam != VK_SPACE)
322 break;
323 /* fall through */
324 case WM_LBUTTONUP:
325 state = get_button_state( hWnd );
326 if (!(state & BUTTON_BTNPRESSED)) break;
327 state &= BUTTON_NSTATES;
328 set_button_state( hWnd, state );
329 if (!(state & BUTTON_HIGHLIGHTED))
331 ReleaseCapture();
332 break;
334 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
335 ReleaseCapture();
336 GetClientRect( hWnd, &rect );
337 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
339 state = get_button_state( hWnd );
340 switch(btn_type)
342 case BS_AUTOCHECKBOX:
343 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
344 break;
345 case BS_AUTORADIOBUTTON:
346 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
347 break;
348 case BS_AUTO3STATE:
349 SendMessageW( hWnd, BM_SETCHECK,
350 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
351 break;
353 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
355 break;
357 case WM_CAPTURECHANGED:
358 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
359 state = get_button_state( hWnd );
360 if (state & BUTTON_BTNPRESSED)
362 state &= BUTTON_NSTATES;
363 set_button_state( hWnd, state );
364 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
366 break;
368 case WM_MOUSEMOVE:
369 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
371 GetClientRect( hWnd, &rect );
372 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
374 break;
376 case WM_SETTEXT:
378 /* Clear an old text here as Windows does */
379 HDC hdc = GetDC(hWnd);
380 HBRUSH hbrush;
381 RECT client, rc;
382 HWND parent = GetParent(hWnd);
384 if (!parent) parent = hWnd;
385 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
386 (WPARAM)hdc, (LPARAM)hWnd);
387 if (!hbrush) /* did the app forget to call DefWindowProc ? */
388 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
389 (WPARAM)hdc, (LPARAM)hWnd);
391 GetClientRect(hWnd, &client);
392 rc = client;
393 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
394 /* Clip by client rect bounds */
395 if (rc.right > client.right) rc.right = client.right;
396 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
397 FillRect(hdc, &rc, hbrush);
398 ReleaseDC(hWnd, hdc);
400 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
401 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
402 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
403 InvalidateRect( hWnd, NULL, TRUE );
404 else
405 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
406 return 1; /* success. FIXME: check text length */
409 case WM_SETFONT:
410 set_button_font( hWnd, (HFONT)wParam );
411 if (lParam) paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
412 break;
414 case WM_GETFONT:
415 return (LRESULT)get_button_font( hWnd );
417 case WM_SETFOCUS:
418 TRACE("WM_SETFOCUS %p\n",hWnd);
419 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
420 paint_button( hWnd, btn_type, ODA_FOCUS );
421 if (style & BS_NOTIFY)
422 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
423 break;
425 case WM_KILLFOCUS:
426 TRACE("WM_KILLFOCUS %p\n",hWnd);
427 state = get_button_state( hWnd );
428 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
429 paint_button( hWnd, btn_type, ODA_FOCUS );
431 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
432 ReleaseCapture();
433 if (style & BS_NOTIFY)
434 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
436 break;
438 case WM_SYSCOLORCHANGE:
439 InvalidateRect( hWnd, NULL, FALSE );
440 break;
442 case BM_SETSTYLE16:
443 case BM_SETSTYLE:
444 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
445 btn_type = wParam & 0x0f;
446 style = (style & ~0x0f) | btn_type;
447 SetWindowLongW( hWnd, GWL_STYLE, style );
449 /* Only redraw if lParam flag is set.*/
450 if (lParam)
451 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
453 break;
455 case BM_CLICK:
456 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
457 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
458 break;
460 case BM_SETIMAGE:
461 /* Check that image format matches button style */
462 switch (style & (BS_BITMAP|BS_ICON))
464 case BS_BITMAP:
465 if (wParam != IMAGE_BITMAP) return 0;
466 break;
467 case BS_ICON:
468 if (wParam != IMAGE_ICON) return 0;
469 break;
470 default:
471 return 0;
473 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
474 InvalidateRect( hWnd, NULL, FALSE );
475 return (LRESULT)oldHbitmap;
477 case BM_GETIMAGE:
478 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
480 case BM_GETCHECK16:
481 case BM_GETCHECK:
482 return get_button_state( hWnd ) & 3;
484 case BM_SETCHECK16:
485 case BM_SETCHECK:
486 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
487 state = get_button_state( hWnd );
488 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
490 if (wParam) style |= WS_TABSTOP;
491 else style &= ~WS_TABSTOP;
492 SetWindowLongW( hWnd, GWL_STYLE, style );
494 if ((state & 3) != wParam)
496 set_button_state( hWnd, (state & ~3) | wParam );
497 paint_button( hWnd, btn_type, ODA_SELECT );
499 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
500 BUTTON_CheckAutoRadioButton( hWnd );
501 break;
503 case BM_GETSTATE16:
504 case BM_GETSTATE:
505 return get_button_state( hWnd );
507 case BM_SETSTATE16:
508 case BM_SETSTATE:
509 state = get_button_state( hWnd );
510 if (wParam)
512 if (state & BUTTON_HIGHLIGHTED) break;
513 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
515 else
517 if (!(state & BUTTON_HIGHLIGHTED)) break;
518 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
520 paint_button( hWnd, btn_type, ODA_SELECT );
521 break;
523 case WM_NCHITTEST:
524 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
525 /* fall through */
526 default:
527 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
528 DefWindowProcA(hWnd, uMsg, wParam, lParam);
530 return 0;
533 /***********************************************************************
534 * ButtonWndProcW
535 * The button window procedure. This is just a wrapper which locks
536 * the passed HWND and calls the real window procedure (with a WND*
537 * pointer pointing to the locked windowstructure).
539 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
541 if (!IsWindow( hWnd )) return 0;
542 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
546 /***********************************************************************
547 * ButtonWndProcA
549 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
551 if (!IsWindow( hWnd )) return 0;
552 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
556 /**********************************************************************
557 * Convert button styles to flags used by DrawText.
558 * TODO: handle WS_EX_RIGHT extended style.
560 static UINT BUTTON_BStoDT(DWORD style)
562 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
564 /* "Convert" pushlike buttons to pushbuttons */
565 if (style & BS_PUSHLIKE)
566 style &= ~0x0F;
568 if (!(style & BS_MULTILINE))
569 dtStyle |= DT_SINGLELINE;
570 else
571 dtStyle |= DT_WORDBREAK;
573 switch (style & BS_CENTER)
575 case BS_LEFT: /* DT_LEFT is 0 */ break;
576 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
577 case BS_CENTER: dtStyle |= DT_CENTER; break;
578 default:
579 /* Pushbutton's text is centered by default */
580 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
581 /* all other flavours have left aligned text */
584 /* DrawText ignores vertical alignment for multiline text,
585 * but we use these flags to align label manualy.
587 if (get_button_type(style) != BS_GROUPBOX)
589 switch (style & BS_VCENTER)
591 case BS_TOP: /* DT_TOP is 0 */ break;
592 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
593 case BS_VCENTER: /* fall through */
594 default: dtStyle |= DT_VCENTER; break;
597 else
598 /* GroupBox's text is always single line and is top aligned. */
599 dtStyle |= DT_SINGLELINE;
601 return dtStyle;
604 /**********************************************************************
605 * BUTTON_CalcLabelRect
607 * Calculates label's rectangle depending on button style.
609 * Returns flags to be passed to DrawText.
610 * Calculated rectangle doesn't take into account button state
611 * (pushed, etc.). If there is nothing to draw (no text/image) output
612 * rectangle is empty, and return value is (UINT)-1.
614 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
616 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
617 WCHAR *text;
618 ICONINFO iconInfo;
619 BITMAP bm;
620 UINT dtStyle = BUTTON_BStoDT(style);
621 RECT r = *rc;
622 INT n;
624 /* Calculate label rectangle according to label type */
625 switch (style & (BS_ICON|BS_BITMAP))
627 case BS_TEXT:
628 if (!(text = get_button_text( hwnd ))) goto empty_rect;
629 if (!text[0])
631 HeapFree( GetProcessHeap(), 0, text );
632 goto empty_rect;
634 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
635 HeapFree( GetProcessHeap(), 0, text );
636 break;
638 case BS_ICON:
639 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
640 goto empty_rect;
642 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
644 r.right = r.left + bm.bmWidth;
645 r.bottom = r.top + bm.bmHeight;
647 DeleteObject(iconInfo.hbmColor);
648 DeleteObject(iconInfo.hbmMask);
649 break;
651 case BS_BITMAP:
652 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
653 goto empty_rect;
655 r.right = r.left + bm.bmWidth;
656 r.bottom = r.top + bm.bmHeight;
657 break;
659 default:
660 empty_rect:
661 rc->right = r.left;
662 rc->bottom = r.top;
663 return (UINT)(LONG)-1;
666 /* Position label inside bounding rectangle according to
667 * alignment flags. (calculated rect is always left-top aligned).
668 * If label is aligned to any side - shift label in opposite
669 * direction to leave extra space for focus rectangle.
671 switch (dtStyle & (DT_CENTER|DT_RIGHT))
673 case DT_LEFT: r.left++; r.right++; break;
674 case DT_CENTER: n = r.right - r.left;
675 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
676 r.right = r.left + n; break;
677 case DT_RIGHT: n = r.right - r.left;
678 r.right = rc->right - 1;
679 r.left = r.right - n;
680 break;
683 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
685 case DT_TOP: r.top++; r.bottom++; break;
686 case DT_VCENTER: n = r.bottom - r.top;
687 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
688 r.bottom = r.top + n; break;
689 case DT_BOTTOM: n = r.bottom - r.top;
690 r.bottom = rc->bottom - 1;
691 r.top = r.bottom - n;
692 break;
695 *rc = r;
696 return dtStyle;
700 /**********************************************************************
701 * BUTTON_DrawTextCallback
703 * Callback function used by DrawStateW function.
705 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
707 RECT rc;
708 rc.left = 0;
709 rc.top = 0;
710 rc.right = cx;
711 rc.bottom = cy;
713 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
714 return TRUE;
718 /**********************************************************************
719 * BUTTON_DrawLabel
721 * Common function for drawing button label.
723 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
725 DRAWSTATEPROC lpOutputProc = NULL;
726 LPARAM lp;
727 WPARAM wp = 0;
728 HBRUSH hbr = 0;
729 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
730 LONG state = get_button_state( hwnd );
731 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
732 WCHAR *text = NULL;
734 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
735 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
736 * I don't have Win31 on hand to verify that, so I leave it as is.
739 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
741 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
742 flags |= DSS_MONO;
745 switch (style & (BS_ICON|BS_BITMAP))
747 case BS_TEXT:
748 /* DST_COMPLEX -- is 0 */
749 lpOutputProc = BUTTON_DrawTextCallback;
750 if (!(text = get_button_text( hwnd ))) return;
751 lp = (LPARAM)text;
752 wp = (WPARAM)dtFlags;
753 break;
755 case BS_ICON:
756 flags |= DST_ICON;
757 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
758 break;
760 case BS_BITMAP:
761 flags |= DST_BITMAP;
762 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
763 break;
765 default:
766 return;
769 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
770 rc->right - rc->left, rc->bottom - rc->top, flags);
771 HeapFree( GetProcessHeap(), 0, text );
774 /**********************************************************************
775 * Push Button Functions
777 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
779 RECT rc, focus_rect, r;
780 UINT dtFlags, uState;
781 HPEN hOldPen;
782 HBRUSH hOldBrush;
783 INT oldBkMode;
784 COLORREF oldTxtColor;
785 HFONT hFont;
786 LONG state = get_button_state( hwnd );
787 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
788 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
789 HWND parent;
791 GetClientRect( hwnd, &rc );
793 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
794 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
795 parent = GetParent(hwnd);
796 if (!parent) parent = hwnd;
797 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
798 hOldPen = (HPEN)SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
799 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
800 oldBkMode = SetBkMode(hDC, TRANSPARENT);
802 if (get_button_type(style) == BS_DEFPUSHBUTTON)
804 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
805 InflateRect( &rc, -1, -1 );
808 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
810 if (style & BS_FLAT)
811 uState |= DFCS_MONO;
812 else if (pushedState)
814 if (get_button_type(style) == BS_DEFPUSHBUTTON )
815 uState |= DFCS_FLAT;
816 else
817 uState |= DFCS_PUSHED;
820 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
821 uState |= DFCS_CHECKED;
823 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
825 focus_rect = rc;
827 /* draw button label */
828 r = rc;
829 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
831 if (dtFlags == (UINT)-1L)
832 goto cleanup;
834 if (pushedState)
835 OffsetRect(&r, 1, 1);
837 IntersectClipRect(hDC, rc.left, rc.top, rc.right, rc.bottom);
839 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
841 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
843 SetTextColor( hDC, oldTxtColor );
845 if (state & BUTTON_HASFOCUS)
847 InflateRect( &focus_rect, -1, -1 );
848 IntersectRect(&focus_rect, &focus_rect, &rc);
849 DrawFocusRect( hDC, &focus_rect );
852 cleanup:
853 SelectObject( hDC, hOldPen );
854 SelectObject( hDC, hOldBrush );
855 SetBkMode(hDC, oldBkMode);
858 /**********************************************************************
859 * Check Box & Radio Button Functions
862 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
864 RECT rbox, rtext, client;
865 HBRUSH hBrush;
866 int delta;
867 UINT dtFlags;
868 HFONT hFont;
869 LONG state = get_button_state( hwnd );
870 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
871 HWND parent;
873 if (style & BS_PUSHLIKE)
875 PB_Paint( hwnd, hDC, action );
876 return;
879 GetClientRect(hwnd, &client);
880 rbox = rtext = client;
882 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
884 parent = GetParent(hwnd);
885 if (!parent) parent = hwnd;
886 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
887 (WPARAM)hDC, (LPARAM)hwnd);
888 if (!hBrush) /* did the app forget to call defwindowproc ? */
889 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
890 (WPARAM)hDC, (LPARAM)hwnd );
892 if (style & BS_LEFTTEXT)
894 /* magic +4 is what CTL3D expects */
896 rtext.right -= checkBoxWidth + 4;
897 rbox.left = rbox.right - checkBoxWidth;
899 else
901 rtext.left += checkBoxWidth + 4;
902 rbox.right = checkBoxWidth;
905 /* Since WM_ERASEBKGND does nothing, first prepare background */
906 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
907 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
909 /* Draw label */
910 client = rtext;
911 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
913 /* Only adjust rbox when rtext is valid */
914 if (dtFlags != (UINT)-1L)
916 rbox.top = rtext.top;
917 rbox.bottom = rtext.bottom;
920 /* Draw the check-box bitmap */
921 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
923 UINT flags;
925 if ((get_button_type(style) == BS_RADIOBUTTON) ||
926 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
927 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
928 else flags = DFCS_BUTTONCHECK;
930 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
931 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
933 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
935 /* rbox must have the correct height */
936 delta = rbox.bottom - rbox.top - checkBoxHeight;
938 if (style & BS_TOP) {
939 if (delta > 0) {
940 rbox.bottom = rbox.top + checkBoxHeight;
941 } else {
942 rbox.top -= -delta/2 + 1;
943 rbox.bottom = rbox.top + checkBoxHeight;
945 } else if (style & BS_BOTTOM) {
946 if (delta > 0) {
947 rbox.top = rbox.bottom - checkBoxHeight;
948 } else {
949 rbox.bottom += -delta/2 + 1;
950 rbox.top = rbox.bottom - checkBoxHeight;
952 } else { /* Default */
953 if (delta > 0) {
954 int ofs = (delta / 2);
955 rbox.bottom -= ofs + 1;
956 rbox.top = rbox.bottom - checkBoxHeight;
957 } else if (delta < 0) {
958 int ofs = (-delta / 2);
959 rbox.top -= ofs + 1;
960 rbox.bottom = rbox.top + checkBoxHeight;
964 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
967 if (dtFlags == (UINT)-1L) /* Noting to draw */
968 return;
970 IntersectClipRect(hDC, client.left, client.top, client.right, client.bottom);
972 if (action == ODA_DRAWENTIRE)
973 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
975 /* ... and focus */
976 if ((action == ODA_FOCUS) ||
977 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
979 rtext.left--;
980 rtext.right++;
981 IntersectRect(&rtext, &rtext, &client);
982 DrawFocusRect( hDC, &rtext );
987 /**********************************************************************
988 * BUTTON_CheckAutoRadioButton
990 * hwnd is checked, uncheck every other auto radio button in group
992 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
994 HWND parent, sibling, start;
996 parent = GetParent(hwnd);
997 /* make sure that starting control is not disabled or invisible */
998 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1001 if (!sibling) break;
1002 if ((hwnd != sibling) &&
1003 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1004 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1005 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1006 } while (sibling != start);
1010 /**********************************************************************
1011 * Group Box Functions
1014 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1016 RECT rc, rcFrame;
1017 HBRUSH hbr;
1018 HFONT hFont;
1019 UINT dtFlags;
1020 TEXTMETRICW tm;
1021 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1022 HWND parent;
1024 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1025 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1026 parent = GetParent(hwnd);
1027 if (!parent) parent = hwnd;
1028 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1029 if (!hbr) /* did the app forget to call defwindowproc ? */
1030 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1031 (WPARAM)hDC, (LPARAM)hwnd);
1033 GetClientRect( hwnd, &rc);
1034 rcFrame = rc;
1036 GetTextMetricsW (hDC, &tm);
1037 rcFrame.top += (tm.tmHeight / 2) - 1;
1038 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1040 InflateRect(&rc, -7, 1);
1041 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1043 if (dtFlags == (UINT)-1L)
1044 return;
1046 /* Because buttons have CS_PARENTDC class style, there is a chance
1047 * that label will be drawn out of client rect.
1048 * But Windows doesn't clip label's rect, so do I.
1051 /* There is 1-pixel marging at the left, right, and bottom */
1052 rc.left--; rc.right++; rc.bottom++;
1053 FillRect(hDC, &rc, hbr);
1054 rc.left++; rc.right--; rc.bottom--;
1056 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1060 /**********************************************************************
1061 * User Button Functions
1064 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1066 RECT rc;
1067 HBRUSH hBrush;
1068 HFONT hFont;
1069 LONG state = get_button_state( hwnd );
1070 HWND parent;
1072 if (action == ODA_SELECT) return;
1074 GetClientRect( hwnd, &rc);
1076 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1078 parent = GetParent(hwnd);
1079 if (!parent) parent = hwnd;
1080 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1081 if (!hBrush) /* did the app forget to call defwindowproc ? */
1082 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1083 (WPARAM)hDC, (LPARAM)hwnd);
1085 FillRect( hDC, &rc, hBrush );
1086 if ((action == ODA_FOCUS) ||
1087 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1088 DrawFocusRect( hDC, &rc );
1092 /**********************************************************************
1093 * Ownerdrawn Button Functions
1096 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1098 LONG state = get_button_state( hwnd );
1099 DRAWITEMSTRUCT dis;
1100 HRGN clipRegion;
1101 RECT clipRect;
1102 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1103 HWND parent;
1104 HFONT hFont, hPrevFont = 0;
1106 dis.CtlType = ODT_BUTTON;
1107 dis.CtlID = id;
1108 dis.itemID = 0;
1109 dis.itemAction = action;
1110 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1111 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1112 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1113 dis.hwndItem = hwnd;
1114 dis.hDC = hDC;
1115 dis.itemData = 0;
1116 GetClientRect( hwnd, &dis.rcItem );
1118 clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1119 if (GetClipRgn(hDC, clipRegion) != 1)
1121 DeleteObject(clipRegion);
1122 clipRegion=NULL;
1124 clipRect = dis.rcItem;
1125 DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1126 IntersectClipRect(hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
1128 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1129 parent = GetParent(hwnd);
1130 if (!parent) parent = hwnd;
1131 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1132 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1133 if (hPrevFont) SelectObject(hDC, hPrevFont);
1134 SelectClipRgn(hDC, clipRegion);