Register Shell Drag and Drop Helper.
[wine/wine64.git] / dlls / user / button.c
blobd486ef05ce5db00473c44ffec93764e8a34bd1d5
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
52 * - BN_PAINT
53 * - BN_SETFOCUS
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"
78 /* GetWindowLong offsets for window extra information */
79 #define STATE_GWL_OFFSET 0
80 #define HFONT_GWL_OFFSET (sizeof(LONG))
81 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
82 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET+sizeof(HANDLE))
84 /* Button state values */
85 #define BUTTON_UNCHECKED 0x00
86 #define BUTTON_CHECKED 0x01
87 #define BUTTON_3STATE 0x02
88 #define BUTTON_HIGHLIGHTED 0x04
89 #define BUTTON_HASFOCUS 0x08
90 #define BUTTON_NSTATES 0x0F
91 /* undocumented flags */
92 #define BUTTON_BTNPRESSED 0x40
93 #define BUTTON_UNKNOWN2 0x20
94 #define BUTTON_UNKNOWN3 0x10
96 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
97 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
98 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
99 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
100 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
101 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
102 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
103 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
104 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
106 #define MAX_BTN_TYPE 12
108 static const WORD maxCheckState[MAX_BTN_TYPE] =
110 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
111 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
112 BUTTON_CHECKED, /* BS_CHECKBOX */
113 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
114 BUTTON_CHECKED, /* BS_RADIOBUTTON */
115 BUTTON_3STATE, /* BS_3STATE */
116 BUTTON_3STATE, /* BS_AUTO3STATE */
117 BUTTON_UNCHECKED, /* BS_GROUPBOX */
118 BUTTON_UNCHECKED, /* BS_USERBUTTON */
119 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
120 BUTTON_UNCHECKED, /* Not defined */
121 BUTTON_UNCHECKED /* BS_OWNERDRAW */
124 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
126 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
128 PB_Paint, /* BS_PUSHBUTTON */
129 PB_Paint, /* BS_DEFPUSHBUTTON */
130 CB_Paint, /* BS_CHECKBOX */
131 CB_Paint, /* BS_AUTOCHECKBOX */
132 CB_Paint, /* BS_RADIOBUTTON */
133 CB_Paint, /* BS_3STATE */
134 CB_Paint, /* BS_AUTO3STATE */
135 GB_Paint, /* BS_GROUPBOX */
136 UB_Paint, /* BS_USERBUTTON */
137 CB_Paint, /* BS_AUTORADIOBUTTON */
138 NULL, /* Not defined */
139 OB_Paint /* BS_OWNERDRAW */
142 static HBITMAP hbitmapCheckBoxes = 0;
143 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
146 /*********************************************************************
147 * button class descriptor
149 const struct builtin_class_descr BUTTON_builtin_class =
151 "Button", /* name */
152 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
153 ButtonWndProcA, /* procA */
154 ButtonWndProcW, /* procW */
155 NB_EXTRA_BYTES, /* extra */
156 IDC_ARROW, /* cursor */
157 0 /* brush */
161 inline static LONG get_button_state( HWND hwnd )
163 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
166 inline static void set_button_state( HWND hwnd, LONG state )
168 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
171 inline static HFONT get_button_font( HWND hwnd )
173 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
176 inline static void set_button_font( HWND hwnd, HFONT font )
178 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
181 inline static UINT get_button_type( LONG window_style )
183 return (window_style & 0x0f);
186 /* paint a button of any type */
187 inline static void paint_button( HWND hwnd, LONG style, UINT action )
189 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
191 HDC hdc = GetDC( hwnd );
192 btnPaintFunc[style]( hwnd, hdc, action );
193 ReleaseDC( hwnd, hdc );
197 /* retrieve the button text; returned buffer must be freed by caller */
198 inline static WCHAR *get_button_text( HWND hwnd )
200 INT len = 512;
201 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
202 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
203 return buffer;
206 /***********************************************************************
207 * ButtonWndProc_common
209 static LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
210 WPARAM wParam, LPARAM lParam, BOOL unicode )
212 RECT rect;
213 POINT pt;
214 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
215 UINT btn_type = get_button_type( style );
216 LONG state;
217 HANDLE oldHbitmap;
219 pt.x = LOWORD(lParam);
220 pt.y = HIWORD(lParam);
222 switch (uMsg)
224 case WM_GETDLGCODE:
225 switch(btn_type)
227 case BS_USERBUTTON:
228 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
229 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
230 case BS_RADIOBUTTON:
231 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
232 case BS_GROUPBOX: return DLGC_STATIC;
233 default: return DLGC_BUTTON;
236 case WM_ENABLE:
237 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
238 break;
240 case WM_CREATE:
241 if (!hbitmapCheckBoxes)
243 BITMAP bmp;
244 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
245 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
246 checkBoxWidth = bmp.bmWidth / 4;
247 checkBoxHeight = bmp.bmHeight / 3;
249 if (btn_type >= MAX_BTN_TYPE)
250 return -1; /* abort */
251 set_button_state( hWnd, BUTTON_UNCHECKED );
252 return 0;
254 case WM_ERASEBKGND:
255 if (btn_type == BS_OWNERDRAW)
257 HDC hdc = (HDC)wParam;
258 RECT rc;
259 HBRUSH hBrush;
260 HWND parent = GetParent(hWnd);
261 if (!parent) parent = hWnd;
262 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
263 if (!hBrush) /* did the app forget to call defwindowproc ? */
264 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
265 (WPARAM)hdc, (LPARAM)hWnd);
266 GetClientRect(hWnd, &rc);
267 FillRect(hdc, &rc, hBrush);
269 return 1;
271 case WM_PAINT:
272 if (btnPaintFunc[btn_type])
274 PAINTSTRUCT ps;
275 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
276 int nOldMode = SetBkMode( hdc, OPAQUE );
277 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
278 SetBkMode(hdc, nOldMode); /* reset painting mode */
279 if( !wParam ) EndPaint( hWnd, &ps );
281 break;
283 case WM_KEYDOWN:
284 if (wParam == VK_SPACE)
286 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
287 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
289 break;
291 case WM_LBUTTONDBLCLK:
292 if(style & BS_NOTIFY ||
293 btn_type == BS_RADIOBUTTON ||
294 btn_type == BS_USERBUTTON ||
295 btn_type == BS_OWNERDRAW)
297 SendMessageW( GetParent(hWnd), WM_COMMAND,
298 MAKEWPARAM( GetWindowLongPtrW(hWnd,GWLP_ID), BN_DOUBLECLICKED ),
299 (LPARAM)hWnd);
300 break;
302 /* fall through */
303 case WM_LBUTTONDOWN:
304 SetCapture( hWnd );
305 SetFocus( hWnd );
306 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
307 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
308 break;
310 case WM_KEYUP:
311 if (wParam != VK_SPACE)
312 break;
313 /* fall through */
314 case WM_LBUTTONUP:
315 state = get_button_state( hWnd );
316 if (!(state & BUTTON_BTNPRESSED)) break;
317 state &= BUTTON_NSTATES;
318 set_button_state( hWnd, state );
319 if (!(state & BUTTON_HIGHLIGHTED))
321 ReleaseCapture();
322 break;
324 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
325 ReleaseCapture();
326 GetClientRect( hWnd, &rect );
327 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
329 state = get_button_state( hWnd );
330 switch(btn_type)
332 case BS_AUTOCHECKBOX:
333 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
334 break;
335 case BS_AUTORADIOBUTTON:
336 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
337 break;
338 case BS_AUTO3STATE:
339 SendMessageW( hWnd, BM_SETCHECK,
340 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
341 break;
343 SendMessageW( GetParent(hWnd), WM_COMMAND,
344 MAKEWPARAM( GetWindowLongPtrW(hWnd,GWLP_ID), BN_CLICKED ), (LPARAM)hWnd);
346 break;
348 case WM_CAPTURECHANGED:
349 state = get_button_state( hWnd );
350 if (state & BUTTON_BTNPRESSED)
352 state &= BUTTON_NSTATES;
353 set_button_state( hWnd, state );
354 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
356 break;
358 case WM_MOUSEMOVE:
359 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
361 GetClientRect( hWnd, &rect );
362 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
364 break;
366 case WM_SETTEXT:
368 /* Clear an old text here as Windows does */
369 HDC hdc = GetDC(hWnd);
370 HBRUSH hbrush;
371 RECT client, rc;
372 HWND parent = GetParent(hWnd);
374 if (!parent) parent = hWnd;
375 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
376 (WPARAM)hdc, (LPARAM)hWnd);
377 if (!hbrush) /* did the app forget to call DefWindowProc ? */
378 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
379 (WPARAM)hdc, (LPARAM)hWnd);
381 GetClientRect(hWnd, &client);
382 rc = client;
383 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
384 /* Clip by client rect bounds */
385 if (rc.right > client.right) rc.right = client.right;
386 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
387 FillRect(hdc, &rc, hbrush);
388 ReleaseDC(hWnd, hdc);
390 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
391 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
392 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
393 InvalidateRect( hWnd, NULL, TRUE );
394 else
395 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
396 return 1; /* success. FIXME: check text length */
399 case WM_SETFONT:
400 set_button_font( hWnd, (HFONT)wParam );
401 if (lParam) paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
402 break;
404 case WM_GETFONT:
405 return (LRESULT)get_button_font( hWnd );
407 case WM_SETFOCUS:
408 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
409 paint_button( hWnd, btn_type, ODA_FOCUS );
410 break;
412 case WM_KILLFOCUS:
413 state = get_button_state( hWnd );
414 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
415 paint_button( hWnd, btn_type, ODA_FOCUS );
417 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
418 ReleaseCapture();
419 break;
421 case WM_SYSCOLORCHANGE:
422 InvalidateRect( hWnd, NULL, FALSE );
423 break;
425 case BM_SETSTYLE16:
426 case BM_SETSTYLE:
427 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
428 btn_type = wParam & 0x0f;
429 style = (style & ~0x0f) | btn_type;
430 SetWindowLongW( hWnd, GWL_STYLE, style );
432 /* Only redraw if lParam flag is set.*/
433 if (lParam)
434 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
436 break;
438 case BM_CLICK:
439 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
440 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
441 break;
443 case BM_SETIMAGE:
444 /* Check that image format matches button style */
445 switch (style & (BS_BITMAP|BS_ICON))
447 case BS_BITMAP:
448 if (wParam != IMAGE_BITMAP) return 0;
449 break;
450 case BS_ICON:
451 if (wParam != IMAGE_ICON) return 0;
452 break;
453 default:
454 return 0;
456 oldHbitmap = (HBITMAP)SetWindowLongW( hWnd, HIMAGE_GWL_OFFSET, lParam );
457 InvalidateRect( hWnd, NULL, FALSE );
458 return (LRESULT)oldHbitmap;
460 case BM_GETIMAGE:
461 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
463 case BM_GETCHECK16:
464 case BM_GETCHECK:
465 return get_button_state( hWnd ) & 3;
467 case BM_SETCHECK16:
468 case BM_SETCHECK:
469 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
470 state = get_button_state( hWnd );
471 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
473 if (wParam) style |= WS_TABSTOP;
474 else style &= ~WS_TABSTOP;
475 SetWindowLongW( hWnd, GWL_STYLE, style );
477 if ((state & 3) != wParam)
479 set_button_state( hWnd, (state & ~3) | wParam );
480 paint_button( hWnd, btn_type, ODA_SELECT );
482 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
483 BUTTON_CheckAutoRadioButton( hWnd );
484 break;
486 case BM_GETSTATE16:
487 case BM_GETSTATE:
488 return get_button_state( hWnd );
490 case BM_SETSTATE16:
491 case BM_SETSTATE:
492 state = get_button_state( hWnd );
493 if (wParam)
495 if (state & BUTTON_HIGHLIGHTED) break;
496 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
498 else
500 if (!(state & BUTTON_HIGHLIGHTED)) break;
501 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
503 paint_button( hWnd, btn_type, ODA_SELECT );
504 break;
506 case WM_NCHITTEST:
507 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
508 /* fall through */
509 default:
510 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
511 DefWindowProcA(hWnd, uMsg, wParam, lParam);
513 return 0;
516 /***********************************************************************
517 * ButtonWndProcW
518 * The button window procedure. This is just a wrapper which locks
519 * the passed HWND and calls the real window procedure (with a WND*
520 * pointer pointing to the locked windowstructure).
522 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
524 if (!IsWindow( hWnd )) return 0;
525 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
529 /***********************************************************************
530 * ButtonWndProcA
532 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
534 if (!IsWindow( hWnd )) return 0;
535 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
539 /**********************************************************************
540 * Convert button styles to flags used by DrawText.
541 * TODO: handle WS_EX_RIGHT extended style.
543 static UINT BUTTON_BStoDT(DWORD style)
545 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
547 /* "Convert" pushlike buttons to pushbuttons */
548 if (style & BS_PUSHLIKE)
549 style &= ~0x0F;
551 if (!(style & BS_MULTILINE))
552 dtStyle |= DT_SINGLELINE;
553 else
554 dtStyle |= DT_WORDBREAK;
556 switch (style & BS_CENTER)
558 case BS_LEFT: /* DT_LEFT is 0 */ break;
559 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
560 case BS_CENTER: dtStyle |= DT_CENTER; break;
561 default:
562 /* Pushbutton's text is centered by default */
563 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
564 /* all other flavours have left aligned text */
567 /* DrawText ignores vertical alignment for multiline text,
568 * but we use these flags to align label manualy.
570 if (get_button_type(style) != BS_GROUPBOX)
572 switch (style & BS_VCENTER)
574 case BS_TOP: /* DT_TOP is 0 */ break;
575 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
576 case BS_VCENTER: /* fall through */
577 default: dtStyle |= DT_VCENTER; break;
580 else
581 /* GroupBox's text is always single line and is top aligned. */
582 dtStyle |= DT_SINGLELINE;
584 return dtStyle;
587 /**********************************************************************
588 * BUTTON_CalcLabelRect
590 * Calculates label's rectangle depending on button style.
592 * Returns flags to be passed to DrawText.
593 * Calculated rectangle doesn't take into account button state
594 * (pushed, etc.). If there is nothing to draw (no text/image) output
595 * rectangle is empty, and return value is (UINT)-1.
597 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
599 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
600 WCHAR *text;
601 ICONINFO iconInfo;
602 BITMAP bm;
603 UINT dtStyle = BUTTON_BStoDT(style);
604 RECT r = *rc;
605 INT n;
607 /* Calculate label rectangle according to label type */
608 switch (style & (BS_ICON|BS_BITMAP))
610 case BS_TEXT:
611 if (!(text = get_button_text( hwnd ))) goto empty_rect;
612 if (!text[0])
614 HeapFree( GetProcessHeap(), 0, text );
615 goto empty_rect;
617 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
618 HeapFree( GetProcessHeap(), 0, text );
619 break;
621 case BS_ICON:
622 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
623 goto empty_rect;
625 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
627 r.right = r.left + bm.bmWidth;
628 r.bottom = r.top + bm.bmHeight;
630 DeleteObject(iconInfo.hbmColor);
631 DeleteObject(iconInfo.hbmMask);
632 break;
634 case BS_BITMAP:
635 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
636 goto empty_rect;
638 r.right = r.left + bm.bmWidth;
639 r.bottom = r.top + bm.bmHeight;
640 break;
642 default:
643 empty_rect:
644 r.right = r.left;
645 r.bottom = r.top;
646 return (UINT)(LONG)-1;
649 /* Position label inside bounding rectangle according to
650 * alignment flags. (calculated rect is always left-top aligned).
651 * If label is aligned to any side - shift label in opposite
652 * direction to leave extra space for focus rectangle.
654 switch (dtStyle & (DT_CENTER|DT_RIGHT))
656 case DT_LEFT: r.left++; r.right++; break;
657 case DT_CENTER: n = r.right - r.left;
658 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
659 r.right = r.left + n; break;
660 case DT_RIGHT: n = r.right - r.left;
661 r.right = rc->right - 1;
662 r.left = r.right - n;
663 break;
666 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
668 case DT_TOP: r.top++; r.bottom++; break;
669 case DT_VCENTER: n = r.bottom - r.top;
670 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
671 r.bottom = r.top + n; break;
672 case DT_BOTTOM: n = r.bottom - r.top;
673 r.bottom = rc->bottom - 1;
674 r.top = r.bottom - n;
675 break;
678 *rc = r;
679 return dtStyle;
683 /**********************************************************************
684 * BUTTON_DrawTextCallback
686 * Callback function used by DrawStateW function.
688 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
690 RECT rc;
691 rc.left = 0;
692 rc.top = 0;
693 rc.right = cx;
694 rc.bottom = cy;
696 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
697 return TRUE;
701 /**********************************************************************
702 * BUTTON_DrawLabel
704 * Common function for drawing button label.
706 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
708 DRAWSTATEPROC lpOutputProc = NULL;
709 LPARAM lp;
710 WPARAM wp = 0;
711 HBRUSH hbr = 0;
712 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
713 LONG state = get_button_state( hwnd );
714 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
715 WCHAR *text = NULL;
717 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
718 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
719 * I don't have Win31 on hand to verify that, so I leave it as is.
722 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
724 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
725 flags |= DSS_MONO;
728 switch (style & (BS_ICON|BS_BITMAP))
730 case BS_TEXT:
731 /* DST_COMPLEX -- is 0 */
732 lpOutputProc = BUTTON_DrawTextCallback;
733 if (!(text = get_button_text( hwnd ))) return;
734 lp = (LPARAM)text;
735 wp = (WPARAM)dtFlags;
736 break;
738 case BS_ICON:
739 flags |= DST_ICON;
740 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
741 break;
743 case BS_BITMAP:
744 flags |= DST_BITMAP;
745 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
746 break;
748 default:
749 return;
752 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
753 rc->right - rc->left, rc->bottom - rc->top, flags);
754 HeapFree( GetProcessHeap(), 0, text );
757 /**********************************************************************
758 * Push Button Functions
760 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
762 RECT rc, focus_rect, r;
763 UINT dtFlags, uState;
764 HRGN hRgn;
765 HPEN hOldPen;
766 HBRUSH hOldBrush;
767 INT oldBkMode;
768 COLORREF oldTxtColor;
769 HFONT hFont;
770 LONG state = get_button_state( hwnd );
771 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
772 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
773 HWND parent;
775 GetClientRect( hwnd, &rc );
777 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
778 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
779 parent = GetParent(hwnd);
780 if (!parent) parent = hwnd;
781 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
782 hOldPen = (HPEN)SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
783 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
784 oldBkMode = SetBkMode(hDC, TRANSPARENT);
786 if (get_button_type(style) == BS_DEFPUSHBUTTON)
788 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
789 InflateRect( &rc, -1, -1 );
792 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
794 if (style & BS_FLAT)
795 uState |= DFCS_MONO;
796 else if (pushedState)
798 if (get_button_type(style) == BS_DEFPUSHBUTTON )
799 uState |= DFCS_FLAT;
800 else
801 uState |= DFCS_PUSHED;
804 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
805 uState |= DFCS_CHECKED;
807 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
809 focus_rect = rc;
811 /* draw button label */
812 r = rc;
813 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
815 if (dtFlags == (UINT)-1L)
816 goto cleanup;
818 if (pushedState)
819 OffsetRect(&r, 1, 1);
821 hRgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
822 SelectClipRgn(hDC, hRgn);
824 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
826 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
828 SetTextColor( hDC, oldTxtColor );
829 SelectClipRgn(hDC, 0);
830 DeleteObject(hRgn);
832 if (state & BUTTON_HASFOCUS)
834 InflateRect( &focus_rect, -1, -1 );
835 IntersectRect(&focus_rect, &focus_rect, &rc);
836 DrawFocusRect( hDC, &focus_rect );
839 cleanup:
840 SelectObject( hDC, hOldPen );
841 SelectObject( hDC, hOldBrush );
842 SetBkMode(hDC, oldBkMode);
845 /**********************************************************************
846 * Check Box & Radio Button Functions
849 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
851 RECT rbox, rtext, client;
852 HBRUSH hBrush;
853 int delta;
854 UINT dtFlags;
855 HRGN hRgn;
856 HFONT hFont;
857 LONG state = get_button_state( hwnd );
858 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
859 HWND parent;
861 if (style & BS_PUSHLIKE)
863 PB_Paint( hwnd, hDC, action );
864 return;
867 GetClientRect(hwnd, &client);
868 rbox = rtext = client;
870 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
872 parent = GetParent(hwnd);
873 if (!parent) parent = hwnd;
874 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
875 (WPARAM)hDC, (LPARAM)hwnd);
876 if (!hBrush) /* did the app forget to call defwindowproc ? */
877 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
878 (WPARAM)hDC, (LPARAM)hwnd );
880 if (style & BS_LEFTTEXT)
882 /* magic +4 is what CTL3D expects */
884 rtext.right -= checkBoxWidth + 4;
885 rbox.left = rbox.right - checkBoxWidth;
887 else
889 rtext.left += checkBoxWidth + 4;
890 rbox.right = checkBoxWidth;
893 /* Since WM_ERASEBKGND does nothing, first prepare background */
894 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
895 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
897 /* Draw label */
898 client = rtext;
899 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
901 rbox.top = rtext.top;
902 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 & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
911 else flags = DFCS_BUTTONCHECK;
913 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
914 if (state & BUTTON_HIGHLIGHTED) 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;
952 hRgn = CreateRectRgn(client.left, client.top, client.right, client.bottom);
953 SelectClipRgn(hDC, hRgn);
954 DeleteObject(hRgn);
956 if (action == ODA_DRAWENTIRE)
957 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
959 /* ... and focus */
960 if ((action == ODA_FOCUS) ||
961 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
963 rtext.left--;
964 rtext.right++;
965 IntersectRect(&rtext, &rtext, &client);
966 DrawFocusRect( hDC, &rtext );
968 SelectClipRgn(hDC, 0);
972 /**********************************************************************
973 * BUTTON_CheckAutoRadioButton
975 * hwnd is checked, uncheck every other auto radio button in group
977 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
979 HWND parent, sibling, start;
981 parent = GetParent(hwnd);
982 /* make sure that starting control is not disabled or invisible */
983 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
986 if (!sibling) break;
987 if ((hwnd != sibling) &&
988 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
989 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
990 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
991 } while (sibling != start);
995 /**********************************************************************
996 * Group Box Functions
999 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1001 RECT rc, rcFrame;
1002 HBRUSH hbr;
1003 HFONT hFont;
1004 UINT dtFlags;
1005 TEXTMETRICW tm;
1006 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1007 HWND parent;
1009 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1010 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1011 parent = GetParent(hwnd);
1012 if (!parent) parent = hwnd;
1013 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1014 if (!hbr) /* did the app forget to call defwindowproc ? */
1015 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1016 (WPARAM)hDC, (LPARAM)hwnd);
1018 GetClientRect( hwnd, &rc);
1019 rcFrame = rc;
1021 GetTextMetricsW (hDC, &tm);
1022 rcFrame.top += (tm.tmHeight / 2) - 1;
1023 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1025 InflateRect(&rc, -7, 1);
1026 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1028 if (dtFlags == (UINT)-1L)
1029 return;
1031 /* Because buttons have CS_PARENTDC class style, there is a chance
1032 * that label will be drawn out of client rect.
1033 * But Windows doesn't clip label's rect, so do I.
1036 /* There is 1-pixel marging at the left, right, and bottom */
1037 rc.left--; rc.right++; rc.bottom++;
1038 FillRect(hDC, &rc, hbr);
1039 rc.left++; rc.right--; rc.bottom--;
1041 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
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 if (action == ODA_SELECT) return;
1059 GetClientRect( hwnd, &rc);
1061 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1063 parent = GetParent(hwnd);
1064 if (!parent) parent = hwnd;
1065 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1066 if (!hBrush) /* did the app forget to call defwindowproc ? */
1067 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1068 (WPARAM)hDC, (LPARAM)hwnd);
1070 FillRect( hDC, &rc, hBrush );
1071 if ((action == ODA_FOCUS) ||
1072 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1073 DrawFocusRect( hDC, &rc );
1077 /**********************************************************************
1078 * Ownerdrawn Button Functions
1081 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1083 LONG state = get_button_state( hwnd );
1084 DRAWITEMSTRUCT dis;
1085 HRGN clipRegion;
1086 RECT clipRect;
1087 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1088 HWND parent;
1089 HFONT hFont, hPrevFont = 0;
1091 dis.CtlType = ODT_BUTTON;
1092 dis.CtlID = id;
1093 dis.itemID = 0;
1094 dis.itemAction = action;
1095 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1096 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1097 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1098 dis.hwndItem = hwnd;
1099 dis.hDC = hDC;
1100 dis.itemData = 0;
1101 GetClientRect( hwnd, &dis.rcItem );
1103 clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1104 if (GetClipRgn(hDC, clipRegion) != 1)
1106 DeleteObject(clipRegion);
1107 clipRegion=NULL;
1109 clipRect = dis.rcItem;
1110 DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1111 IntersectClipRect(hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
1113 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1114 parent = GetParent(hwnd);
1115 if (!parent) parent = hwnd;
1116 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1117 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1118 if (hPrevFont) SelectObject(hDC, hPrevFont);
1119 SelectClipRgn(hDC, clipRegion);