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
28 #include "wine/winuser16.h"
32 /* GetWindowLong offsets for window extra information */
33 #define STATE_GWL_OFFSET 0
34 #define HFONT_GWL_OFFSET (sizeof(LONG))
35 #define HIMAGE_GWL_OFFSET (2*sizeof(LONG))
36 #define NB_EXTRA_BYTES (3*sizeof(LONG))
38 /* Button state values */
39 #define BUTTON_UNCHECKED 0x00
40 #define BUTTON_CHECKED 0x01
41 #define BUTTON_3STATE 0x02
42 #define BUTTON_HIGHLIGHTED 0x04
43 #define BUTTON_HASFOCUS 0x08
44 #define BUTTON_NSTATES 0x0F
45 /* undocumented flags */
46 #define BUTTON_BTNPRESSED 0x40
47 #define BUTTON_UNKNOWN2 0x20
48 #define BUTTON_UNKNOWN3 0x10
50 static UINT
BUTTON_CalcLabelRect( HWND hwnd
, HDC hdc
, RECT
*rc
);
51 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
52 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
53 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
54 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
55 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
);
56 static void BUTTON_CheckAutoRadioButton( HWND hwnd
);
57 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
58 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
60 #define MAX_BTN_TYPE 12
62 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
64 BUTTON_UNCHECKED
, /* BS_PUSHBUTTON */
65 BUTTON_UNCHECKED
, /* BS_DEFPUSHBUTTON */
66 BUTTON_CHECKED
, /* BS_CHECKBOX */
67 BUTTON_CHECKED
, /* BS_AUTOCHECKBOX */
68 BUTTON_CHECKED
, /* BS_RADIOBUTTON */
69 BUTTON_3STATE
, /* BS_3STATE */
70 BUTTON_3STATE
, /* BS_AUTO3STATE */
71 BUTTON_UNCHECKED
, /* BS_GROUPBOX */
72 BUTTON_UNCHECKED
, /* BS_USERBUTTON */
73 BUTTON_CHECKED
, /* BS_AUTORADIOBUTTON */
74 BUTTON_UNCHECKED
, /* Not defined */
75 BUTTON_UNCHECKED
/* BS_OWNERDRAW */
78 typedef void (*pfPaint
)( HWND hwnd
, HDC hdc
, UINT action
);
80 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
82 PB_Paint
, /* BS_PUSHBUTTON */
83 PB_Paint
, /* BS_DEFPUSHBUTTON */
84 CB_Paint
, /* BS_CHECKBOX */
85 CB_Paint
, /* BS_AUTOCHECKBOX */
86 CB_Paint
, /* BS_RADIOBUTTON */
87 CB_Paint
, /* BS_3STATE */
88 CB_Paint
, /* BS_AUTO3STATE */
89 GB_Paint
, /* BS_GROUPBOX */
90 UB_Paint
, /* BS_USERBUTTON */
91 CB_Paint
, /* BS_AUTORADIOBUTTON */
92 NULL
, /* Not defined */
93 OB_Paint
/* BS_OWNERDRAW */
96 static HBITMAP hbitmapCheckBoxes
= 0;
97 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
100 /*********************************************************************
101 * button class descriptor
103 const struct builtin_class_descr BUTTON_builtin_class
=
106 CS_GLOBALCLASS
| CS_DBLCLKS
| CS_VREDRAW
| CS_HREDRAW
| CS_PARENTDC
, /* style */
107 ButtonWndProcA
, /* procA */
108 ButtonWndProcW
, /* procW */
109 NB_EXTRA_BYTES
, /* extra */
110 IDC_ARROWA
, /* cursor */
115 inline static LONG
get_button_state( HWND hwnd
)
117 return GetWindowLongA( hwnd
, STATE_GWL_OFFSET
);
120 inline static void set_button_state( HWND hwnd
, LONG state
)
122 SetWindowLongA( hwnd
, STATE_GWL_OFFSET
, state
);
125 inline static HFONT
get_button_font( HWND hwnd
)
127 return GetWindowLongA( hwnd
, HFONT_GWL_OFFSET
);
130 inline static void set_button_font( HWND hwnd
, HFONT font
)
132 SetWindowLongA( hwnd
, HFONT_GWL_OFFSET
, font
);
135 inline static UINT
get_button_type( LONG window_style
)
137 return (window_style
& 0x0f);
140 /* paint a button of any type */
141 inline static void paint_button( HWND hwnd
, LONG style
, UINT action
)
143 if (btnPaintFunc
[style
] && IsWindowVisible(hwnd
))
145 HDC hdc
= GetDC( hwnd
);
146 btnPaintFunc
[style
]( hwnd
, hdc
, action
);
147 ReleaseDC( hwnd
, hdc
);
151 /* retrieve the button text; returned buffer must be freed by caller */
152 inline static WCHAR
*get_button_text( HWND hwnd
)
154 INT len
= GetWindowTextLengthW( hwnd
);
155 WCHAR
*buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
156 if (buffer
) GetWindowTextW( hwnd
, buffer
, len
+ 1 );
160 /***********************************************************************
161 * ButtonWndProc_common
163 static LRESULT WINAPI
ButtonWndProc_common(HWND hWnd
, UINT uMsg
,
164 WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
168 LONG style
= GetWindowLongA( hWnd
, GWL_STYLE
);
169 UINT btn_type
= get_button_type( style
);
173 pt
.x
= LOWORD(lParam
);
174 pt
.y
= HIWORD(lParam
);
181 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
182 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
184 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
185 default: return DLGC_BUTTON
;
189 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
193 if (!hbitmapCheckBoxes
)
196 hbitmapCheckBoxes
= LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES
));
197 GetObjectW( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
198 checkBoxWidth
= bmp
.bmWidth
/ 4;
199 checkBoxHeight
= bmp
.bmHeight
/ 3;
201 if (btn_type
< 0L || btn_type
>= MAX_BTN_TYPE
)
202 return -1; /* abort */
203 set_button_state( hWnd
, BUTTON_UNCHECKED
);
210 if (btnPaintFunc
[btn_type
])
213 HDC hdc
= wParam
? (HDC
)wParam
: BeginPaint( hWnd
, &ps
);
214 int nOldMode
= SetBkMode( hdc
, OPAQUE
);
215 (btnPaintFunc
[btn_type
])( hWnd
, hdc
, ODA_DRAWENTIRE
);
216 SetBkMode(hdc
, nOldMode
); /* reset painting mode */
217 if( !wParam
) EndPaint( hWnd
, &ps
);
222 if (wParam
== VK_SPACE
)
224 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
225 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
229 case WM_LBUTTONDBLCLK
:
230 if(style
& BS_NOTIFY
||
231 btn_type
== BS_RADIOBUTTON
||
232 btn_type
== BS_USERBUTTON
||
233 btn_type
== BS_OWNERDRAW
)
235 SendMessageW( GetParent(hWnd
), WM_COMMAND
,
236 MAKEWPARAM( GetWindowLongA(hWnd
,GWL_ID
), BN_DOUBLECLICKED
),
244 SendMessageW( hWnd
, BM_SETSTATE
, TRUE
, 0 );
245 set_button_state( hWnd
, get_button_state( hWnd
) | BUTTON_BTNPRESSED
);
249 if (wParam
!= VK_SPACE
)
253 state
= get_button_state( hWnd
);
254 if (!(state
& BUTTON_BTNPRESSED
)) break;
255 state
&= BUTTON_NSTATES
;
256 set_button_state( hWnd
, state
);
257 if (!(state
& BUTTON_HIGHLIGHTED
))
262 SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
264 GetClientRect( hWnd
, &rect
);
265 if (uMsg
== WM_KEYUP
|| PtInRect( &rect
, pt
))
267 state
= get_button_state( hWnd
);
270 case BS_AUTOCHECKBOX
:
271 SendMessageW( hWnd
, BM_SETCHECK
, !(state
& BUTTON_CHECKED
), 0 );
273 case BS_AUTORADIOBUTTON
:
274 SendMessageW( hWnd
, BM_SETCHECK
, TRUE
, 0 );
277 SendMessageW( hWnd
, BM_SETCHECK
,
278 (state
& BUTTON_3STATE
) ? 0 : ((state
& 3) + 1), 0 );
281 SendMessageW( GetParent(hWnd
), WM_COMMAND
,
282 MAKEWPARAM( GetWindowLongA(hWnd
,GWL_ID
), BN_CLICKED
), (LPARAM
)hWnd
);
286 case WM_CAPTURECHANGED
:
287 state
= get_button_state( hWnd
);
288 if (state
& BUTTON_BTNPRESSED
)
290 state
&= BUTTON_NSTATES
;
291 set_button_state( hWnd
, state
);
292 if (state
& BUTTON_HIGHLIGHTED
) SendMessageW( hWnd
, BM_SETSTATE
, FALSE
, 0 );
297 if ((wParam
& MK_LBUTTON
) && GetCapture() == hWnd
)
299 GetClientRect( hWnd
, &rect
);
300 SendMessageW( hWnd
, BM_SETSTATE
, PtInRect(&rect
, pt
), 0 );
306 /* Clear an old text here as Windows does */
307 HDC hdc
= GetDC(hWnd
);
311 hbrush
= SendMessageW(GetParent(hWnd
), WM_CTLCOLORSTATIC
, hdc
, (LPARAM
)hWnd
);
312 if (!hbrush
) /* did the app forget to call DefWindowProc ? */
313 hbrush
= DefWindowProcW(GetParent(hWnd
), WM_CTLCOLORSTATIC
, hdc
, (LPARAM
)hWnd
);
315 GetClientRect(hWnd
, &client
);
317 BUTTON_CalcLabelRect(hWnd
, hdc
, &rc
);
318 /* Clip by client rect bounds */
319 if (rc
.right
> client
.right
) rc
.right
= client
.right
;
320 if (rc
.bottom
> client
.bottom
) rc
.bottom
= client
.bottom
;
321 FillRect(hdc
, &rc
, hbrush
);
322 ReleaseDC(hWnd
, hdc
);
324 if (unicode
) DefWindowProcW( hWnd
, WM_SETTEXT
, wParam
, lParam
);
325 else DefWindowProcA( hWnd
, WM_SETTEXT
, wParam
, lParam
);
326 if (btn_type
== BS_GROUPBOX
) /* Yes, only for BS_GROUPBOX */
327 InvalidateRect( hWnd
, NULL
, TRUE
);
329 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
330 return 1; /* success. FIXME: check text length */
334 set_button_font( hWnd
, wParam
);
335 if (lParam
) paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
339 return get_button_font( hWnd
);
342 if ((btn_type
== BS_RADIOBUTTON
|| btn_type
== BS_AUTORADIOBUTTON
) && (GetCapture() != hWnd
) &&
343 !(SendMessageW(hWnd
, BM_GETCHECK
, 0, 0) & BST_CHECKED
))
345 /* The notification is sent when the button (BS_AUTORADIOBUTTON)
346 is unchecked and the focus was not given by a mouse click. */
347 if (btn_type
== BS_AUTORADIOBUTTON
)
348 SendMessageW( hWnd
, BM_SETCHECK
, BUTTON_CHECKED
, 0 );
349 SendMessageW( GetParent(hWnd
), WM_COMMAND
,
350 MAKEWPARAM( GetWindowLongA(hWnd
,GWL_ID
), BN_CLICKED
), (LPARAM
)hWnd
);
352 set_button_state( hWnd
, get_button_state(hWnd
) | BUTTON_HASFOCUS
);
353 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
357 set_button_state( hWnd
, get_button_state(hWnd
) & ~BUTTON_HASFOCUS
);
358 paint_button( hWnd
, btn_type
, ODA_FOCUS
);
359 InvalidateRect( hWnd
, NULL
, TRUE
);
362 case WM_SYSCOLORCHANGE
:
363 InvalidateRect( hWnd
, NULL
, FALSE
);
368 if ((wParam
& 0x0f) >= MAX_BTN_TYPE
) break;
369 btn_type
= wParam
& 0x0f;
370 style
= (style
& ~0x0f) | btn_type
;
371 SetWindowLongA( hWnd
, GWL_STYLE
, style
);
373 /* Only redraw if lParam flag is set.*/
375 paint_button( hWnd
, btn_type
, ODA_DRAWENTIRE
);
380 SendMessageW( hWnd
, WM_LBUTTONDOWN
, 0, 0 );
381 SendMessageW( hWnd
, WM_LBUTTONUP
, 0, 0 );
385 /* Check that image format matches button style */
386 switch (style
& (BS_BITMAP
|BS_ICON
))
389 if (wParam
!= IMAGE_BITMAP
) return 0;
392 if (wParam
!= IMAGE_ICON
) return 0;
397 oldHbitmap
= SetWindowLongA( hWnd
, HIMAGE_GWL_OFFSET
, lParam
);
398 InvalidateRect( hWnd
, NULL
, FALSE
);
402 return GetWindowLongA( hWnd
, HIMAGE_GWL_OFFSET
);
406 return get_button_state( hWnd
) & 3;
410 if (wParam
> maxCheckState
[btn_type
]) wParam
= maxCheckState
[btn_type
];
411 state
= get_button_state( hWnd
);
412 if ((btn_type
== BS_RADIOBUTTON
) || (btn_type
== BS_AUTORADIOBUTTON
))
414 if (wParam
) style
|= WS_TABSTOP
;
415 else style
&= ~WS_TABSTOP
;
416 SetWindowLongA( hWnd
, GWL_STYLE
, style
);
418 if ((state
& 3) != wParam
)
420 set_button_state( hWnd
, (state
& ~3) | wParam
);
421 paint_button( hWnd
, btn_type
, ODA_SELECT
);
423 if ((btn_type
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
) && (style
& WS_CHILD
))
424 BUTTON_CheckAutoRadioButton( hWnd
);
429 return get_button_state( hWnd
);
433 state
= get_button_state( hWnd
);
436 if (state
& BUTTON_HIGHLIGHTED
) break;
437 set_button_state( hWnd
, state
| BUTTON_HIGHLIGHTED
);
441 if (!(state
& BUTTON_HIGHLIGHTED
)) break;
442 set_button_state( hWnd
, state
& ~BUTTON_HIGHLIGHTED
);
444 paint_button( hWnd
, btn_type
, ODA_SELECT
);
448 if(btn_type
== BS_GROUPBOX
) return HTTRANSPARENT
;
451 return unicode
? DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
) :
452 DefWindowProcA(hWnd
, uMsg
, wParam
, lParam
);
457 /***********************************************************************
459 * The button window procedure. This is just a wrapper which locks
460 * the passed HWND and calls the real window procedure (with a WND*
461 * pointer pointing to the locked windowstructure).
463 static LRESULT WINAPI
ButtonWndProcW( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
465 if (!IsWindow( hWnd
)) return 0;
466 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, TRUE
);
470 /***********************************************************************
473 static LRESULT WINAPI
ButtonWndProcA( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
475 if (!IsWindow( hWnd
)) return 0;
476 return ButtonWndProc_common( hWnd
, uMsg
, wParam
, lParam
, FALSE
);
480 /**********************************************************************
481 * Convert button styles to flags used by DrawText.
482 * TODO: handle WS_EX_RIGHT extended style.
484 static UINT
BUTTON_BStoDT(DWORD style
)
486 UINT dtStyle
= DT_NOCLIP
; /* We use SelectClipRgn to limit output */
488 /* "Convert" pushlike buttons to pushbuttons */
489 if (style
& BS_PUSHLIKE
)
492 if (!(style
& BS_MULTILINE
))
493 dtStyle
|= DT_SINGLELINE
;
495 dtStyle
|= DT_WORDBREAK
;
497 switch (style
& BS_CENTER
)
499 case BS_LEFT
: /* DT_LEFT is 0 */ break;
500 case BS_RIGHT
: dtStyle
|= DT_RIGHT
; break;
501 case BS_CENTER
: dtStyle
|= DT_CENTER
; break;
503 /* Pushbutton's text is centered by default */
504 if (get_button_type(style
) <= BS_DEFPUSHBUTTON
) dtStyle
|= DT_CENTER
;
505 /* all other flavours have left aligned text */
508 /* DrawText ignores vertical alignment for multiline text,
509 * but we use these flags to align label manualy.
511 if (get_button_type(style
) != BS_GROUPBOX
)
513 switch (style
& BS_VCENTER
)
515 case BS_TOP
: /* DT_TOP is 0 */ break;
516 case BS_BOTTOM
: dtStyle
|= DT_BOTTOM
; break;
517 case BS_VCENTER
: /* fall through */
518 default: dtStyle
|= DT_VCENTER
; break;
522 /* GroupBox's text is always single line and is top aligned. */
523 dtStyle
|= DT_SINGLELINE
;
528 /**********************************************************************
529 * BUTTON_CalcLabelRect
531 * Calculates label's rectangle depending on button style.
533 * Returns flags to be passed to DrawText.
534 * Calculated rectangle doesn't take into account button state
535 * (pushed, etc.). If there is nothing to draw (no text/image) output
536 * rectangle is empty, and return value is (UINT)-1.
538 static UINT
BUTTON_CalcLabelRect(HWND hwnd
, HDC hdc
, RECT
*rc
)
540 LONG style
= GetWindowLongA( hwnd
, GWL_STYLE
);
544 UINT dtStyle
= BUTTON_BStoDT(style
);
548 /* Calculate label rectangle according to label type */
549 switch (style
& (BS_ICON
|BS_BITMAP
))
552 if (!(text
= get_button_text( hwnd
))) goto empty_rect
;
555 HeapFree( GetProcessHeap(), 0, text
);
558 DrawTextW(hdc
, text
, -1, &r
, dtStyle
| DT_CALCRECT
);
559 HeapFree( GetProcessHeap(), 0, text
);
563 if (!GetIconInfo((HICON
)GetWindowLongA( hwnd
, HIMAGE_GWL_OFFSET
), &iconInfo
))
566 GetObjectW (iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
);
568 r
.right
= r
.left
+ bm
.bmWidth
;
569 r
.bottom
= r
.top
+ bm
.bmHeight
;
571 DeleteObject(iconInfo
.hbmColor
);
572 DeleteObject(iconInfo
.hbmMask
);
576 if (!GetObjectW( (HANDLE
)GetWindowLongA( hwnd
, HIMAGE_GWL_OFFSET
), sizeof(BITMAP
), &bm
))
579 r
.right
= r
.left
+ bm
.bmWidth
;
580 r
.bottom
= r
.top
+ bm
.bmHeight
;
587 return (UINT
)(LONG
)-1;
590 /* Position label inside bounding rectangle according to
591 * alignment flags. (calculated rect is always left-top aligned).
592 * If label is aligned to any side - shift label in opposite
593 * direction to leave extra space for focus rectangle.
595 switch (dtStyle
& (DT_CENTER
|DT_RIGHT
))
597 case DT_LEFT
: r
.left
++; r
.right
++; break;
598 case DT_CENTER
: n
= r
.right
- r
.left
;
599 r
.left
= rc
->left
+ ((rc
->right
- rc
->left
) - n
) / 2;
600 r
.right
= r
.left
+ n
; break;
601 case DT_RIGHT
: n
= r
.right
- r
.left
;
602 r
.right
= rc
->right
- 1;
603 r
.left
= r
.right
- n
;
607 switch (dtStyle
& (DT_VCENTER
|DT_BOTTOM
))
609 case DT_TOP
: r
.top
++; r
.bottom
++; break;
610 case DT_VCENTER
: n
= r
.bottom
- r
.top
;
611 r
.top
= rc
->top
+ ((rc
->bottom
- rc
->top
) - n
) / 2;
612 r
.bottom
= r
.top
+ n
; break;
613 case DT_BOTTOM
: n
= r
.bottom
- r
.top
;
614 r
.bottom
= rc
->bottom
- 1;
615 r
.top
= r
.bottom
- n
;
624 /**********************************************************************
625 * BUTTON_DrawTextCallback
627 * Callback function used by DrawStateW function.
629 static BOOL CALLBACK
BUTTON_DrawTextCallback(HDC hdc
, LPARAM lp
, WPARAM wp
, int cx
, int cy
)
637 DrawTextW(hdc
, (LPCWSTR
)lp
, -1, &rc
, (UINT
)wp
);
642 /**********************************************************************
645 * Common function for drawing button label.
647 static void BUTTON_DrawLabel(HWND hwnd
, HDC hdc
, UINT dtFlags
, RECT
*rc
)
649 DRAWSTATEPROC lpOutputProc
= NULL
;
653 UINT flags
= IsWindowEnabled(hwnd
) ? DSS_NORMAL
: DSS_DISABLED
;
654 LONG state
= get_button_state( hwnd
);
655 LONG style
= GetWindowLongA( hwnd
, GWL_STYLE
);
658 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
659 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
660 * I don't have Win31 on hand to verify that, so I leave it as is.
663 if ((style
& BS_PUSHLIKE
) && (state
& BUTTON_3STATE
))
665 hbr
= GetSysColorBrush(COLOR_GRAYTEXT
);
669 switch (style
& (BS_ICON
|BS_BITMAP
))
672 /* DST_COMPLEX -- is 0 */
673 lpOutputProc
= BUTTON_DrawTextCallback
;
674 if (!(text
= get_button_text( hwnd
))) return;
676 wp
= (WPARAM
)dtFlags
;
681 lp
= GetWindowLongA( hwnd
, HIMAGE_GWL_OFFSET
);
686 lp
= GetWindowLongA( hwnd
, HIMAGE_GWL_OFFSET
);
693 DrawStateW(hdc
, hbr
, lpOutputProc
, lp
, wp
, rc
->left
, rc
->top
,
694 rc
->right
- rc
->left
, rc
->bottom
- rc
->top
, flags
);
695 if (text
) HeapFree( GetProcessHeap(), 0, text
);
698 /**********************************************************************
699 * Push Button Functions
701 static void PB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
703 RECT rc
, focus_rect
, r
;
709 COLORREF oldTxtColor
;
711 LONG state
= get_button_state( hwnd
);
712 LONG style
= GetWindowLongA( hwnd
, GWL_STYLE
);
713 BOOL pushedState
= (state
& BUTTON_HIGHLIGHTED
);
715 GetClientRect( hwnd
, &rc
);
717 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
718 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
719 SendMessageW( GetParent(hwnd
), WM_CTLCOLORBTN
, hDC
, (LPARAM
)hwnd
);
720 hOldPen
= (HPEN
)SelectObject(hDC
, SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
721 hOldBrush
=(HBRUSH
)SelectObject(hDC
,GetSysColorBrush(COLOR_BTNFACE
));
722 oldBkMode
= SetBkMode(hDC
, TRANSPARENT
);
724 if ( TWEAK_WineLook
== WIN31_LOOK
)
726 COLORREF clr_wnd
= GetSysColor(COLOR_WINDOW
);
727 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
729 SetPixel( hDC
, rc
.left
, rc
.top
, clr_wnd
);
730 SetPixel( hDC
, rc
.left
, rc
.bottom
-1, clr_wnd
);
731 SetPixel( hDC
, rc
.right
-1, rc
.top
, clr_wnd
);
732 SetPixel( hDC
, rc
.right
-1, rc
.bottom
-1, clr_wnd
);
733 InflateRect( &rc
, -1, -1 );
736 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
738 Rectangle(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
739 InflateRect( &rc
, -1, -1 );
742 if (TWEAK_WineLook
== WIN31_LOOK
)
746 /* draw button shadow: */
747 SelectObject(hDC
, GetSysColorBrush(COLOR_BTNSHADOW
));
748 PatBlt(hDC
, rc
.left
, rc
.top
, 1, rc
.bottom
-rc
.top
, PATCOPY
);
749 PatBlt(hDC
, rc
.left
, rc
.top
, rc
.right
-rc
.left
, 1, PATCOPY
);
751 rc
.right
++, rc
.bottom
++;
752 DrawEdge( hDC
, &rc
, EDGE_RAISED
, BF_RECT
);
753 rc
.right
--, rc
.bottom
--;
758 UINT uState
= DFCS_BUTTONPUSH
| DFCS_ADJUSTRECT
;
762 else if (pushedState
)
764 if (get_button_type(style
) == BS_DEFPUSHBUTTON
)
767 uState
|= DFCS_PUSHED
;
770 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
))
771 uState
|= DFCS_CHECKED
;
773 DrawFrameControl( hDC
, &rc
, DFC_BUTTON
, uState
);
778 /* draw button label */
780 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &r
);
782 if (dtFlags
== (UINT
)-1L)
786 OffsetRect(&r
, 1, 1);
788 if(TWEAK_WineLook
== WIN31_LOOK
)
791 InflateRect(&focus_rect
, 2, 0);
794 hRgn
= CreateRectRgn(rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
795 SelectClipRgn(hDC
, hRgn
);
797 oldTxtColor
= SetTextColor( hDC
, GetSysColor(COLOR_BTNTEXT
) );
799 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &r
);
801 SetTextColor( hDC
, oldTxtColor
);
802 SelectClipRgn(hDC
, 0);
805 if (state
& BUTTON_HASFOCUS
)
807 InflateRect( &focus_rect
, -1, -1 );
808 IntersectRect(&focus_rect
, &focus_rect
, &rc
);
809 DrawFocusRect( hDC
, &focus_rect
);
813 SelectObject( hDC
, hOldPen
);
814 SelectObject( hDC
, hOldBrush
);
815 SetBkMode(hDC
, oldBkMode
);
818 /**********************************************************************
819 * Check Box & Radio Button Functions
822 static void CB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
824 RECT rbox
, rtext
, client
;
830 LONG state
= get_button_state( hwnd
);
831 LONG style
= GetWindowLongA( hwnd
, GWL_STYLE
);
833 if (style
& BS_PUSHLIKE
)
835 PB_Paint( hwnd
, hDC
, action
);
839 GetClientRect(hwnd
, &client
);
840 rbox
= rtext
= client
;
842 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
844 hBrush
= SendMessageW( GetParent(hwnd
), WM_CTLCOLORSTATIC
, hDC
, (LPARAM
)hwnd
);
845 if (!hBrush
) /* did the app forget to call defwindowproc ? */
846 hBrush
= DefWindowProcW( GetParent(hwnd
), WM_CTLCOLORSTATIC
, hDC
, (LPARAM
)hwnd
);
848 if (style
& BS_LEFTTEXT
)
850 /* magic +4 is what CTL3D expects */
852 rtext
.right
-= checkBoxWidth
+ 4;
853 rbox
.left
= rbox
.right
- checkBoxWidth
;
857 rtext
.left
+= checkBoxWidth
+ 4;
858 rbox
.right
= checkBoxWidth
;
861 /* Draw the check-box bitmap */
862 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
864 /* Since WM_ERASEBKGND does nothing, first prepare background */
865 if (action
== ODA_SELECT
) FillRect( hDC
, &rbox
, hBrush
);
866 else FillRect( hDC
, &client
, hBrush
);
868 if( TWEAK_WineLook
== WIN31_LOOK
)
870 HDC hMemDC
= CreateCompatibleDC( hDC
);
872 delta
= (rbox
.bottom
- rbox
.top
- checkBoxHeight
) / 2;
874 /* Check in case the client area is smaller than the checkbox bitmap */
875 if (delta
< 0) delta
= 0;
877 if (state
& BUTTON_HIGHLIGHTED
) x
+= 2 * checkBoxWidth
;
878 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) x
+= checkBoxWidth
;
879 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
880 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) y
+= checkBoxHeight
;
881 else if (state
& BUTTON_3STATE
) y
+= 2 * checkBoxHeight
;
883 /* The bitmap for the radio button is not aligned with the
884 * left of the window, it is 1 pixel off. */
885 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
886 (get_button_type(style
) == BS_AUTORADIOBUTTON
))
889 SelectObject( hMemDC
, hbitmapCheckBoxes
);
890 BitBlt( hDC
, rbox
.left
, rbox
.top
+ delta
, checkBoxWidth
,
891 checkBoxHeight
, hMemDC
, x
, y
, SRCCOPY
);
898 if ((get_button_type(style
) == BS_RADIOBUTTON
) ||
899 (get_button_type(style
) == BS_AUTORADIOBUTTON
)) flags
= DFCS_BUTTONRADIO
;
900 else if (state
& BUTTON_3STATE
) flags
= DFCS_BUTTON3STATE
;
901 else flags
= DFCS_BUTTONCHECK
;
903 if (state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) flags
|= DFCS_CHECKED
;
904 if (state
& BUTTON_HIGHLIGHTED
) flags
|= DFCS_PUSHED
;
906 if (style
& WS_DISABLED
) flags
|= DFCS_INACTIVE
;
908 /* rbox must have the correct height */
909 delta
= rbox
.bottom
- rbox
.top
- checkBoxHeight
;
912 int ofs
= (abs(delta
) / 2);
913 rbox
.bottom
-= ofs
+ 1;
914 rbox
.top
= rbox
.bottom
- checkBoxHeight
;
918 int ofs
= (abs(delta
) / 2);
920 rbox
.bottom
= rbox
.top
+ checkBoxHeight
;
923 DrawFrameControl( hDC
, &rbox
, DFC_BUTTON
, flags
);
929 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rtext
);
931 if (dtFlags
== (UINT
)-1L) /* Noting to draw */
933 hRgn
= CreateRectRgn(client
.left
, client
.top
, client
.right
, client
.bottom
);
934 SelectClipRgn(hDC
, hRgn
);
937 if (action
== ODA_DRAWENTIRE
)
938 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rtext
);
941 if ((action
== ODA_FOCUS
) ||
942 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
946 IntersectRect(&rtext
, &rtext
, &client
);
947 DrawFocusRect( hDC
, &rtext
);
949 SelectClipRgn(hDC
, 0);
953 /**********************************************************************
954 * BUTTON_CheckAutoRadioButton
956 * hwnd is checked, uncheck every other auto radio button in group
958 static void BUTTON_CheckAutoRadioButton( HWND hwnd
)
960 HWND parent
, sibling
, start
;
962 parent
= GetParent(hwnd
);
963 /* make sure that starting control is not disabled or invisible */
964 start
= sibling
= GetNextDlgGroupItem( parent
, hwnd
, TRUE
);
968 if ((hwnd
!= sibling
) &&
969 ((GetWindowLongA( sibling
, GWL_STYLE
) & 0x0f) == BS_AUTORADIOBUTTON
))
970 SendMessageW( sibling
, BM_SETCHECK
, BUTTON_UNCHECKED
, 0 );
971 sibling
= GetNextDlgGroupItem( parent
, sibling
, FALSE
);
972 } while (sibling
!= start
);
976 /**********************************************************************
977 * Group Box Functions
980 static void GB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
986 LONG style
= GetWindowLongA( hwnd
, GWL_STYLE
);
988 if (action
!= ODA_DRAWENTIRE
) return;
990 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
991 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
992 hbr
= SendMessageW( GetParent(hwnd
), WM_CTLCOLORSTATIC
, hDC
, (LPARAM
)hwnd
);
993 if (!hbr
) /* did the app forget to call defwindowproc ? */
994 hbr
= DefWindowProcW( GetParent(hwnd
), WM_CTLCOLORSTATIC
, hDC
, (LPARAM
)hwnd
);
996 GetClientRect( hwnd
, &rc
);
997 if (TWEAK_WineLook
== WIN31_LOOK
) {
998 HPEN hPrevPen
= SelectObject( hDC
,
999 SYSCOLOR_GetPen(COLOR_WINDOWFRAME
));
1000 HBRUSH hPrevBrush
= SelectObject( hDC
,
1001 GetStockObject(NULL_BRUSH
) );
1003 Rectangle( hDC
, rc
.left
, rc
.top
+ 2, rc
.right
- 1, rc
.bottom
- 1 );
1004 SelectObject( hDC
, hPrevBrush
);
1005 SelectObject( hDC
, hPrevPen
);
1010 GetTextMetricsW (hDC
, &tm
);
1011 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
1012 DrawEdge (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
| ((style
& BS_FLAT
) ? BF_FLAT
: 0));
1015 InflateRect(&rc
, -7, 1);
1016 dtFlags
= BUTTON_CalcLabelRect(hwnd
, hDC
, &rc
);
1018 if (dtFlags
== (UINT
)-1L)
1021 /* Because buttons have CS_PARENTDC class style, there is a chance
1022 * that label will be drawn out of client rect.
1023 * But Windows doesn't clip label's rect, so do I.
1026 /* There is 1-pixel marging at the left, right, and bottom */
1027 rc
.left
--; rc
.right
++; rc
.bottom
++;
1028 FillRect(hDC
, &rc
, hbr
);
1029 rc
.left
++; rc
.right
--; rc
.bottom
--;
1031 BUTTON_DrawLabel(hwnd
, hDC
, dtFlags
, &rc
);
1035 /**********************************************************************
1036 * User Button Functions
1039 static void UB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1044 LONG state
= get_button_state( hwnd
);
1046 if (action
== ODA_SELECT
) return;
1048 GetClientRect( hwnd
, &rc
);
1050 if ((hFont
= get_button_font( hwnd
))) SelectObject( hDC
, hFont
);
1052 hBrush
= SendMessageW( GetParent(hwnd
), WM_CTLCOLORBTN
, hDC
, (LPARAM
)hwnd
);
1053 if (!hBrush
) /* did the app forget to call defwindowproc ? */
1054 hBrush
= DefWindowProcW( GetParent(hwnd
), WM_CTLCOLORBTN
, hDC
, (LPARAM
)hwnd
);
1056 FillRect( hDC
, &rc
, hBrush
);
1057 if ((action
== ODA_FOCUS
) ||
1058 ((action
== ODA_DRAWENTIRE
) && (state
& BUTTON_HASFOCUS
)))
1059 DrawFocusRect( hDC
, &rc
);
1063 /**********************************************************************
1064 * Ownerdrawn Button Functions
1067 static void OB_Paint( HWND hwnd
, HDC hDC
, UINT action
)
1069 LONG state
= get_button_state( hwnd
);
1073 UINT id
= GetWindowLongA( hwnd
, GWL_ID
);
1075 dis
.CtlType
= ODT_BUTTON
;
1078 dis
.itemAction
= action
;
1079 dis
.itemState
= ((state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
1080 ((state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
1081 (IsWindowEnabled(hwnd
) ? 0: ODS_DISABLED
);
1082 dis
.hwndItem
= hwnd
;
1085 GetClientRect( hwnd
, &dis
.rcItem
);
1087 clipRegion
= CreateRectRgnIndirect(&dis
.rcItem
);
1088 if (GetClipRgn(hDC
, clipRegion
) != 1)
1090 DeleteObject(clipRegion
);
1091 clipRegion
=(HRGN
)NULL
;
1093 clipRect
= dis
.rcItem
;
1094 DPtoLP(hDC
, (LPPOINT
) &clipRect
, 2);
1095 IntersectClipRect(hDC
, clipRect
.left
, clipRect
.top
, clipRect
.right
, clipRect
.bottom
);
1097 SetBkColor( hDC
, GetSysColor( COLOR_BTNFACE
) );
1098 SendMessageW( GetParent(hwnd
), WM_DRAWITEM
, id
, (LPARAM
)&dis
);
1099 SelectClipRgn(hDC
, clipRegion
);