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
14 static void PB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
);
15 static void PB_PaintGrayOnGray(HDC32 hDC
,HFONT32 hFont
,RECT32
*rc
,char *text
);
16 static void CB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
);
17 static void GB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
);
18 static void UB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
);
19 static void OB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
);
20 static void BUTTON_CheckAutoRadioButton( WND
*wndPtr
);
22 #define MAX_BTN_TYPE 12
24 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
26 BUTTON_UNCHECKED
, /* BS_PUSHBUTTON */
27 BUTTON_UNCHECKED
, /* BS_DEFPUSHBUTTON */
28 BUTTON_CHECKED
, /* BS_CHECKBOX */
29 BUTTON_CHECKED
, /* BS_AUTOCHECKBOX */
30 BUTTON_CHECKED
, /* BS_RADIOBUTTON */
31 BUTTON_3STATE
, /* BS_3STATE */
32 BUTTON_3STATE
, /* BS_AUTO3STATE */
33 BUTTON_UNCHECKED
, /* BS_GROUPBOX */
34 BUTTON_UNCHECKED
, /* BS_USERBUTTON */
35 BUTTON_CHECKED
, /* BS_AUTORADIOBUTTON */
36 BUTTON_UNCHECKED
, /* Not defined */
37 BUTTON_UNCHECKED
/* BS_OWNERDRAW */
40 typedef void (*pfPaint
)( WND
*wndPtr
, HDC32 hdc
, WORD action
);
42 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
44 PB_Paint
, /* BS_PUSHBUTTON */
45 PB_Paint
, /* BS_DEFPUSHBUTTON */
46 CB_Paint
, /* BS_CHECKBOX */
47 CB_Paint
, /* BS_AUTOCHECKBOX */
48 CB_Paint
, /* BS_RADIOBUTTON */
49 CB_Paint
, /* BS_3STATE */
50 CB_Paint
, /* BS_AUTO3STATE */
51 GB_Paint
, /* BS_GROUPBOX */
52 UB_Paint
, /* BS_USERBUTTON */
53 CB_Paint
, /* BS_AUTORADIOBUTTON */
54 NULL
, /* Not defined */
55 OB_Paint
/* BS_OWNERDRAW */
58 #define PAINT_BUTTON(wndPtr,style,action) \
59 if (btnPaintFunc[style]) { \
60 HDC32 hdc = GetDC32( (wndPtr)->hwndSelf ); \
61 (btnPaintFunc[style])(wndPtr,hdc,action); \
62 ReleaseDC32( (wndPtr)->hwndSelf, hdc ); }
64 #define BUTTON_SEND_CTLCOLOR(wndPtr,hdc) \
65 SendMessage32A( GetParent32((wndPtr)->hwndSelf), WM_CTLCOLORBTN, \
66 (hdc), (wndPtr)->hwndSelf )
68 static HBITMAP32 hbitmapCheckBoxes
= 0;
69 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
72 /***********************************************************************
75 LRESULT WINAPI
ButtonWndProc( HWND32 hWnd
, UINT32 uMsg
,
76 WPARAM32 wParam
, LPARAM lParam
)
79 POINT32 pt
= { LOWORD(lParam
), HIWORD(lParam
) };
80 WND
*wndPtr
= WIN_FindWndPtr(hWnd
);
81 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
82 LONG style
= wndPtr
->dwStyle
& 0x0f;
89 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
90 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
92 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
93 default: return DLGC_BUTTON
;
97 PAINT_BUTTON( wndPtr
, style
, ODA_DRAWENTIRE
);
101 if (!hbitmapCheckBoxes
)
104 hbitmapCheckBoxes
= LoadBitmap32A(0, MAKEINTRESOURCE32A(OBM_CHECKBOXES
));
105 GetObject32A( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
106 checkBoxWidth
= bmp
.bmWidth
/ 4;
107 checkBoxHeight
= bmp
.bmHeight
/ 3;
109 if (style
< 0L || style
>= MAX_BTN_TYPE
) return -1; /* abort */
110 infoPtr
->state
= BUTTON_UNCHECKED
;
118 if (btnPaintFunc
[style
])
121 HDC32 hdc
= wParam
? (HDC32
)wParam
: BeginPaint32( hWnd
, &ps
);
122 SetBkMode32( hdc
, OPAQUE
);
123 (btnPaintFunc
[style
])( wndPtr
, hdc
, ODA_DRAWENTIRE
);
124 if( !wParam
) EndPaint32( hWnd
, &ps
);
129 SendMessage32A( hWnd
, BM_SETSTATE32
, TRUE
, 0 );
131 SetCapture32( hWnd
);
136 if (!(infoPtr
->state
& BUTTON_HIGHLIGHTED
)) break;
137 SendMessage32A( hWnd
, BM_SETSTATE32
, FALSE
, 0 );
138 GetClientRect32( hWnd
, &rect
);
139 if (PtInRect32( &rect
, pt
))
143 case BS_AUTOCHECKBOX
:
144 SendMessage32A( hWnd
, BM_SETCHECK32
,
145 !(infoPtr
->state
& BUTTON_CHECKED
), 0 );
147 case BS_AUTORADIOBUTTON
:
148 SendMessage32A( hWnd
, BM_SETCHECK32
, TRUE
, 0 );
151 SendMessage32A( hWnd
, BM_SETCHECK32
,
152 (infoPtr
->state
& BUTTON_3STATE
) ? 0 :
153 ((infoPtr
->state
& 3) + 1), 0 );
156 SendMessage32A( GetParent32(hWnd
), WM_COMMAND
,
157 MAKEWPARAM( wndPtr
->wIDmenu
, BN_CLICKED
), hWnd
);
162 if (GetCapture32() == hWnd
)
164 GetClientRect32( hWnd
, &rect
);
165 SendMessage32A( hWnd
, BM_SETSTATE32
, PtInRect32(&rect
, pt
), 0 );
170 if(style
== BS_GROUPBOX
) return HTTRANSPARENT
;
171 return DefWindowProc32A( hWnd
, uMsg
, wParam
, lParam
);
174 DEFWND_SetText( wndPtr
, (LPCSTR
)lParam
);
175 if( wndPtr
->dwStyle
& WS_VISIBLE
)
176 PAINT_BUTTON( wndPtr
, style
, ODA_DRAWENTIRE
);
180 infoPtr
->hFont
= (HFONT16
)wParam
;
181 if (lParam
&& (wndPtr
->dwStyle
& WS_VISIBLE
))
182 PAINT_BUTTON( wndPtr
, style
, ODA_DRAWENTIRE
);
186 return infoPtr
->hFont
;
189 infoPtr
->state
|= BUTTON_HASFOCUS
;
190 PAINT_BUTTON( wndPtr
, style
, ODA_FOCUS
);
194 infoPtr
->state
&= ~BUTTON_HASFOCUS
;
195 PAINT_BUTTON( wndPtr
, style
, ODA_FOCUS
);
196 InvalidateRect32( hWnd
, NULL
, TRUE
);
199 case WM_SYSCOLORCHANGE
:
200 InvalidateRect32( hWnd
, NULL
, FALSE
);
205 if ((wParam
& 0x0f) >= MAX_BTN_TYPE
) break;
206 wndPtr
->dwStyle
= (wndPtr
->dwStyle
& 0xfffffff0)
207 | (wParam
& 0x0000000f);
208 style
= wndPtr
->dwStyle
& 0x0000000f;
209 PAINT_BUTTON( wndPtr
, style
, ODA_DRAWENTIRE
);
214 return infoPtr
->state
& 3;
218 if (wParam
> maxCheckState
[style
]) wParam
= maxCheckState
[style
];
219 if ((infoPtr
->state
& 3) != wParam
)
221 infoPtr
->state
= (infoPtr
->state
& ~3) | wParam
;
222 PAINT_BUTTON( wndPtr
, style
, ODA_SELECT
);
224 if ((style
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
))
225 BUTTON_CheckAutoRadioButton( wndPtr
);
230 return infoPtr
->state
;
236 if (infoPtr
->state
& BUTTON_HIGHLIGHTED
) break;
237 infoPtr
->state
|= BUTTON_HIGHLIGHTED
;
241 if (!(infoPtr
->state
& BUTTON_HIGHLIGHTED
)) break;
242 infoPtr
->state
&= ~BUTTON_HIGHLIGHTED
;
244 PAINT_BUTTON( wndPtr
, style
, ODA_SELECT
);
248 return DefWindowProc32A(hWnd
, uMsg
, wParam
, lParam
);
254 /**********************************************************************
255 * Push Button Functions
258 static void PB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
)
263 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
265 GetClientRect32( wndPtr
->hwndSelf
, &rc
);
267 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
268 if (infoPtr
->hFont
) SelectObject32( hDC
, infoPtr
->hFont
);
269 BUTTON_SEND_CTLCOLOR( wndPtr
, hDC
);
270 hOldPen
= (HPEN32
)SelectObject32(hDC
, GetSysColorPen32(COLOR_WINDOWFRAME
));
271 hOldBrush
=(HBRUSH32
)SelectObject32(hDC
,GetSysColorBrush32(COLOR_BTNFACE
));
272 SetBkMode32(hDC
, TRANSPARENT
);
273 Rectangle32(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
274 if (action
== ODA_DRAWENTIRE
)
276 SetPixel32( hDC
, rc
.left
, rc
.top
, GetSysColor32(COLOR_WINDOW
) );
277 SetPixel32( hDC
, rc
.left
, rc
.bottom
-1, GetSysColor32(COLOR_WINDOW
) );
278 SetPixel32( hDC
, rc
.right
-1, rc
.top
, GetSysColor32(COLOR_WINDOW
) );
279 SetPixel32( hDC
, rc
.right
-1, rc
.bottom
-1, GetSysColor32(COLOR_WINDOW
));
281 InflateRect32( &rc
, -1, -1 );
283 if ((wndPtr
->dwStyle
& 0x000f) == BS_DEFPUSHBUTTON
)
285 Rectangle32(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
286 InflateRect32( &rc
, -1, -1 );
289 if (infoPtr
->state
& BUTTON_HIGHLIGHTED
)
291 /* draw button shadow: */
292 SelectObject32(hDC
, GetSysColorBrush32(COLOR_BTNSHADOW
));
293 PatBlt32(hDC
, rc
.left
, rc
.top
, 1, rc
.bottom
-rc
.top
, PATCOPY
);
294 PatBlt32(hDC
, rc
.left
, rc
.top
, rc
.right
-rc
.left
, 1, PATCOPY
);
295 rc
.left
+= 2; /* To position the text down and right */
298 else GRAPH_DrawReliefRect( hDC
, &rc
, 2, 2, FALSE
);
300 /* draw button label, if any: */
301 if (wndPtr
->text
&& wndPtr
->text
[0])
304 GetObject32A( GetSysColorBrush32(COLOR_BTNFACE
), sizeof(lb
), &lb
);
305 if (wndPtr
->dwStyle
& WS_DISABLED
&&
306 GetSysColor32(COLOR_GRAYTEXT
)==lb
.lbColor
)
307 /* don't write gray text on gray bkg */
308 PB_PaintGrayOnGray(hDC
,infoPtr
->hFont
,&rc
,wndPtr
->text
);
311 SetTextColor32( hDC
, (wndPtr
->dwStyle
& WS_DISABLED
) ?
312 GetSysColor32(COLOR_GRAYTEXT
) :
313 GetSysColor32(COLOR_BTNTEXT
) );
314 DrawText32A( hDC
, wndPtr
->text
, -1, &rc
,
315 DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
316 /* do we have the focus? */
317 if (infoPtr
->state
& BUTTON_HASFOCUS
)
319 RECT32 r
= { 0, 0, 0, 0 };
320 INT32 xdelta
, ydelta
;
322 DrawText32A( hDC
, wndPtr
->text
, -1, &r
,
323 DT_SINGLELINE
| DT_CALCRECT
);
324 xdelta
= ((rc
.right
- rc
.left
) - (r
.right
- r
.left
) - 1) / 2;
325 ydelta
= ((rc
.bottom
- rc
.top
) - (r
.bottom
- r
.top
) - 1) / 2;
326 if (xdelta
< 0) xdelta
= 0;
327 if (ydelta
< 0) ydelta
= 0;
328 InflateRect32( &rc
, -xdelta
, -ydelta
);
329 DrawFocusRect32( hDC
, &rc
);
334 SelectObject32( hDC
, hOldPen
);
335 SelectObject32( hDC
, hOldBrush
);
339 /**********************************************************************
340 * Push Button sub function [internal]
341 * using a raster brush to avoid gray text on gray background
344 void PB_PaintGrayOnGray(HDC32 hDC
,HFONT32 hFont
,RECT32
*rc
,char *text
)
346 static const int Pattern
[] = {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55};
347 HBITMAP32 hbm
= CreateBitmap32( 8, 8, 1, 1, Pattern
);
348 HDC32 hdcMem
= CreateCompatibleDC32(hDC
);
354 DrawText32A( hDC
, text
, -1, &rect
, DT_SINGLELINE
| DT_CALCRECT
);
356 rect
.left
=(rc
->right
-rect
.right
)/2; /* for centering text bitmap */
357 rect
.top
=(rc
->bottom
-rect
.bottom
)/2;
358 hbmMem
= CreateCompatibleBitmap32( hDC
,rect
.right
,rect
.bottom
);
359 SelectObject32( hdcMem
, hbmMem
);
360 hBr
= SelectObject32( hdcMem
, CreatePatternBrush32(hbm
) );
361 DeleteObject32( hbm
);
362 PatBlt32( hdcMem
,0,0,rect
.right
,rect
.bottom
,WHITENESS
);
363 if (hFont
) SelectObject32( hdcMem
, hFont
);
364 DrawText32A( hdcMem
, text
, -1, &rc2
, DT_SINGLELINE
);
365 PatBlt32( hdcMem
,0,0,rect
.right
,rect
.bottom
,0xFA0089);
366 DeleteObject32( SelectObject32( hdcMem
,hBr
) );
367 BitBlt32(hDC
,rect
.left
,rect
.top
,rect
.right
,rect
.bottom
,hdcMem
,0,0,0x990000);
369 DeleteObject32( hbmMem
);
373 /**********************************************************************
374 * Check Box & Radio Button Functions
377 static void CB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
)
379 RECT32 rbox
, rtext
, client
;
382 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
385 GetClientRect32(wndPtr
->hwndSelf
, &client
);
386 rbox
= rtext
= client
;
388 if (infoPtr
->hFont
) SelectObject32( hDC
, infoPtr
->hFont
);
390 /* Something is still not right, checkboxes (and edit controls)
391 * in wsping32 have white backgrounds instead of dark grey.
392 * BUTTON_SEND_CTLCOLOR() is even worse since it returns 0 in this
393 * particular case and the background is not painted at all.
396 hBrush
= GetControlBrush( wndPtr
->hwndSelf
, hDC
, CTLCOLOR_BTN
);
398 if (wndPtr
->dwStyle
& BS_LEFTTEXT
)
400 /* magic +4 is what CTL3D expects */
402 rtext
.right
-= checkBoxWidth
+ 4;
403 rbox
.left
= rbox
.right
- checkBoxWidth
;
407 rtext
.left
+= checkBoxWidth
+ 4;
408 rbox
.right
= checkBoxWidth
;
411 /* Draw the check-box bitmap */
413 if (wndPtr
->text
) textlen
= strlen( wndPtr
->text
);
414 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
417 delta
= (rbox
.bottom
- rbox
.top
- checkBoxHeight
) >> 1;
419 if (action
== ODA_SELECT
) FillRect32( hDC
, &rbox
, hBrush
);
420 else FillRect32( hDC
, &client
, hBrush
);
422 if (infoPtr
->state
& BUTTON_HIGHLIGHTED
) x
+= 2 * checkBoxWidth
;
423 if (infoPtr
->state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) x
+= checkBoxWidth
;
424 if (((wndPtr
->dwStyle
& 0x0f) == BS_RADIOBUTTON
) ||
425 ((wndPtr
->dwStyle
& 0x0f) == BS_AUTORADIOBUTTON
)) y
+= checkBoxHeight
;
426 else if (infoPtr
->state
& BUTTON_3STATE
) y
+= 2 * checkBoxHeight
;
428 GRAPH_DrawBitmap( hDC
, hbitmapCheckBoxes
, rbox
.left
, rbox
.top
+ delta
,
429 x
, y
, checkBoxWidth
, checkBoxHeight
, FALSE
);
430 if( textlen
&& action
!= ODA_SELECT
)
432 if (wndPtr
->dwStyle
& WS_DISABLED
)
433 SetTextColor32( hDC
, GetSysColor32(COLOR_GRAYTEXT
) );
434 DrawText32A( hDC
, wndPtr
->text
, textlen
, &rtext
,
435 DT_SINGLELINE
| DT_VCENTER
);
436 textlen
= 0; /* skip DrawText() below */
440 if ((action
== ODA_FOCUS
) ||
441 ((action
== ODA_DRAWENTIRE
) && (infoPtr
->state
& BUTTON_HASFOCUS
)))
443 /* again, this is what CTL3D expects */
445 SetRectEmpty32(&rbox
);
447 DrawText32A( hDC
, wndPtr
->text
, textlen
, &rbox
,
448 DT_SINGLELINE
| DT_CALCRECT
);
449 textlen
= rbox
.bottom
- rbox
.top
;
450 delta
= ((rtext
.bottom
- rtext
.top
) - textlen
)/2;
451 rbox
.bottom
= (rbox
.top
= rtext
.top
+ delta
- 1) + textlen
+ 2;
452 textlen
= rbox
.right
- rbox
.left
;
453 rbox
.right
= (rbox
.left
+= --rtext
.left
) + textlen
+ 2;
454 IntersectRect32(&rbox
, &rbox
, &rtext
);
455 DrawFocusRect32( hDC
, &rbox
);
460 /**********************************************************************
461 * BUTTON_CheckAutoRadioButton
463 * wndPtr is checked, uncheck every other auto radio button in group
465 static void BUTTON_CheckAutoRadioButton( WND
*wndPtr
)
467 HWND32 parent
, sibling
, start
;
468 if (!(wndPtr
->dwStyle
& WS_CHILD
)) return;
469 parent
= wndPtr
->parent
->hwndSelf
;
470 /* assure that starting control is not disabled or invisible */
471 start
= sibling
= GetNextDlgGroupItem32( parent
, wndPtr
->hwndSelf
, TRUE
);
475 if ((wndPtr
->hwndSelf
!= sibling
) &&
476 ((WIN_FindWndPtr(sibling
)->dwStyle
& 0x0f) == BS_AUTORADIOBUTTON
))
477 SendMessage32A( sibling
, BM_SETCHECK32
, BUTTON_UNCHECKED
, 0 );
478 sibling
= GetNextDlgGroupItem32( parent
, sibling
, FALSE
);
479 } while (sibling
!= start
);
483 /**********************************************************************
484 * Group Box Functions
487 static void GB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
)
490 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
492 if (action
!= ODA_DRAWENTIRE
) return;
494 BUTTON_SEND_CTLCOLOR( wndPtr
, hDC
);
496 GetClientRect32( wndPtr
->hwndSelf
, &rc
);
497 if (TWEAK_WineLook
== WIN31_LOOK
)
498 GRAPH_DrawRectangle( hDC
, rc
.left
, rc
.top
+ 2, rc
.right
- 1, rc
.bottom
- 1,
499 GetSysColorPen32(COLOR_WINDOWFRAME
) );
505 SelectObject32 (hDC
, infoPtr
->hFont
);
506 GetTextMetrics32A (hDC
, &tm
);
507 rcFrame
.top
+= (tm
.tmHeight
/ 2) - 1;
508 DrawEdge32 (hDC
, &rcFrame
, EDGE_ETCHED
, BF_RECT
);
513 if (infoPtr
->hFont
) SelectObject32( hDC
, infoPtr
->hFont
);
514 if (wndPtr
->dwStyle
& WS_DISABLED
)
515 SetTextColor32( hDC
, GetSysColor32(COLOR_GRAYTEXT
) );
517 DrawText32A( hDC
, wndPtr
->text
, -1, &rc
, DT_SINGLELINE
| DT_NOCLIP
);
522 /**********************************************************************
523 * User Button Functions
526 static void UB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
)
530 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
532 if (action
== ODA_SELECT
) return;
534 GetClientRect32( wndPtr
->hwndSelf
, &rc
);
536 if (infoPtr
->hFont
) SelectObject32( hDC
, infoPtr
->hFont
);
537 hBrush
= GetControlBrush( wndPtr
->hwndSelf
, hDC
, CTLCOLOR_BTN
);
539 FillRect32( hDC
, &rc
, hBrush
);
540 if ((action
== ODA_FOCUS
) ||
541 ((action
== ODA_DRAWENTIRE
) && (infoPtr
->state
& BUTTON_HASFOCUS
)))
542 DrawFocusRect32( hDC
, &rc
);
546 /**********************************************************************
547 * Ownerdrawn Button Functions
550 static void OB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
)
552 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
553 DRAWITEMSTRUCT32 dis
;
555 dis
.CtlType
= ODT_BUTTON
;
556 dis
.CtlID
= wndPtr
->wIDmenu
;
558 dis
.itemAction
= action
;
559 dis
.itemState
= ((infoPtr
->state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
560 ((infoPtr
->state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
561 ((wndPtr
->dwStyle
& WS_DISABLED
) ? ODS_DISABLED
: 0);
562 dis
.hwndItem
= wndPtr
->hwndSelf
;
565 GetClientRect32( wndPtr
->hwndSelf
, &dis
.rcItem
);
566 SendMessage32A( GetParent32(wndPtr
->hwndSelf
), WM_DRAWITEM
,
567 wndPtr
->wIDmenu
, (LPARAM
)&dis
);