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
13 static void PB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
);
14 static void PB_PaintGrayOnGray(HDC32 hDC
,HFONT32 hFont
,RECT32
*rc
,char *text
);
15 static void CB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
);
16 static void GB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
);
17 static void UB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
);
18 static void OB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
);
19 static void BUTTON_CheckAutoRadioButton( WND
*wndPtr
);
21 #define MAX_BTN_TYPE 12
23 static const WORD maxCheckState
[MAX_BTN_TYPE
] =
25 BUTTON_UNCHECKED
, /* BS_PUSHBUTTON */
26 BUTTON_UNCHECKED
, /* BS_DEFPUSHBUTTON */
27 BUTTON_CHECKED
, /* BS_CHECKBOX */
28 BUTTON_CHECKED
, /* BS_AUTOCHECKBOX */
29 BUTTON_CHECKED
, /* BS_RADIOBUTTON */
30 BUTTON_3STATE
, /* BS_3STATE */
31 BUTTON_3STATE
, /* BS_AUTO3STATE */
32 BUTTON_UNCHECKED
, /* BS_GROUPBOX */
33 BUTTON_UNCHECKED
, /* BS_USERBUTTON */
34 BUTTON_CHECKED
, /* BS_AUTORADIOBUTTON */
35 BUTTON_UNCHECKED
, /* Not defined */
36 BUTTON_UNCHECKED
/* BS_OWNERDRAW */
39 typedef void (*pfPaint
)( WND
*wndPtr
, HDC32 hdc
, WORD action
);
41 static const pfPaint btnPaintFunc
[MAX_BTN_TYPE
] =
43 PB_Paint
, /* BS_PUSHBUTTON */
44 PB_Paint
, /* BS_DEFPUSHBUTTON */
45 CB_Paint
, /* BS_CHECKBOX */
46 CB_Paint
, /* BS_AUTOCHECKBOX */
47 CB_Paint
, /* BS_RADIOBUTTON */
48 CB_Paint
, /* BS_3STATE */
49 CB_Paint
, /* BS_AUTO3STATE */
50 GB_Paint
, /* BS_GROUPBOX */
51 UB_Paint
, /* BS_USERBUTTON */
52 CB_Paint
, /* BS_AUTORADIOBUTTON */
53 NULL
, /* Not defined */
54 OB_Paint
/* BS_OWNERDRAW */
57 #define PAINT_BUTTON(wndPtr,style,action) \
58 if (btnPaintFunc[style]) { \
59 HDC32 hdc = GetDC32( (wndPtr)->hwndSelf ); \
60 (btnPaintFunc[style])(wndPtr,hdc,action); \
61 ReleaseDC32( (wndPtr)->hwndSelf, hdc ); }
63 #define BUTTON_SEND_CTLCOLOR(wndPtr,hdc) \
64 SendMessage32A( GetParent32((wndPtr)->hwndSelf), WM_CTLCOLORBTN, \
65 (hdc), (wndPtr)->hwndSelf )
67 static HBITMAP16 hbitmapCheckBoxes
= 0;
68 static WORD checkBoxWidth
= 0, checkBoxHeight
= 0;
71 /***********************************************************************
74 LRESULT
ButtonWndProc(HWND32 hWnd
, UINT32 uMsg
, WPARAM32 wParam
, LPARAM lParam
)
77 WND
*wndPtr
= WIN_FindWndPtr(hWnd
);
78 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
79 LONG style
= wndPtr
->dwStyle
& 0x0f;
86 case BS_PUSHBUTTON
: return DLGC_BUTTON
| DLGC_UNDEFPUSHBUTTON
;
87 case BS_DEFPUSHBUTTON
: return DLGC_BUTTON
| DLGC_DEFPUSHBUTTON
;
89 case BS_AUTORADIOBUTTON
: return DLGC_BUTTON
| DLGC_RADIOBUTTON
;
90 default: return DLGC_BUTTON
;
94 PAINT_BUTTON( wndPtr
, style
, ODA_DRAWENTIRE
);
98 if (!hbitmapCheckBoxes
)
101 hbitmapCheckBoxes
= LoadBitmap16(0, MAKEINTRESOURCE(OBM_CHECKBOXES
));
102 GetObject16( hbitmapCheckBoxes
, sizeof(bmp
), &bmp
);
103 checkBoxWidth
= bmp
.bmWidth
/ 4;
104 checkBoxHeight
= bmp
.bmHeight
/ 3;
106 if (style
< 0L || style
>= MAX_BTN_TYPE
) return -1; /* abort */
107 infoPtr
->state
= BUTTON_UNCHECKED
;
115 if (btnPaintFunc
[style
])
118 HDC32 hdc
= BeginPaint32( hWnd
, &ps
);
119 (btnPaintFunc
[style
])( wndPtr
, hdc
, ODA_DRAWENTIRE
);
120 EndPaint32( hWnd
, &ps
);
125 SendMessage32A( hWnd
, BM_SETSTATE32
, TRUE
, 0 );
127 SetCapture32( hWnd
);
132 if (!(infoPtr
->state
& BUTTON_HIGHLIGHTED
)) break;
133 SendMessage32A( hWnd
, BM_SETSTATE32
, FALSE
, 0 );
134 GetClientRect16( hWnd
, &rect
);
135 if (PtInRect16( &rect
, MAKEPOINT16(lParam
) ))
139 case BS_AUTOCHECKBOX
:
140 SendMessage32A( hWnd
, BM_SETCHECK32
,
141 !(infoPtr
->state
& BUTTON_CHECKED
), 0 );
143 case BS_AUTORADIOBUTTON
:
144 SendMessage32A( hWnd
, BM_SETCHECK32
, TRUE
, 0 );
147 SendMessage32A( hWnd
, BM_SETCHECK32
,
148 (infoPtr
->state
& BUTTON_3STATE
) ? 0 :
149 ((infoPtr
->state
& 3) + 1), 0 );
152 SendMessage32A( GetParent32(hWnd
), WM_COMMAND
,
153 MAKEWPARAM( wndPtr
->wIDmenu
, BN_CLICKED
), hWnd
);
158 if (GetCapture32() == hWnd
)
160 GetClientRect16( hWnd
, &rect
);
161 SendMessage32A( hWnd
, BM_SETSTATE32
,
162 PtInRect16( &rect
,MAKEPOINT16(lParam
) ), 0 );
167 if(style
== BS_GROUPBOX
) return HTTRANSPARENT
;
168 return DefWindowProc32A( hWnd
, uMsg
, wParam
, lParam
);
171 DEFWND_SetText( wndPtr
, (LPSTR
)lParam
);
172 PAINT_BUTTON( wndPtr
, style
, ODA_DRAWENTIRE
);
176 infoPtr
->hFont
= (HFONT16
)wParam
;
177 if (lParam
) PAINT_BUTTON( wndPtr
, style
, ODA_DRAWENTIRE
);
181 return infoPtr
->hFont
;
184 infoPtr
->state
|= BUTTON_HASFOCUS
;
185 PAINT_BUTTON( wndPtr
, style
, ODA_FOCUS
);
189 infoPtr
->state
&= ~BUTTON_HASFOCUS
;
190 PAINT_BUTTON( wndPtr
, style
, ODA_FOCUS
);
191 InvalidateRect16( hWnd
, NULL
, TRUE
);
194 case WM_SYSCOLORCHANGE
:
195 InvalidateRect32( hWnd
, NULL
, FALSE
);
200 if ((wParam
& 0x0f) >= MAX_BTN_TYPE
) break;
201 wndPtr
->dwStyle
= (wndPtr
->dwStyle
& 0xfffffff0)
202 | (wParam
& 0x0000000f);
203 style
= wndPtr
->dwStyle
& 0x0000000f;
204 PAINT_BUTTON( wndPtr
, style
, ODA_DRAWENTIRE
);
209 return infoPtr
->state
& 3;
213 if (wParam
> maxCheckState
[style
]) wParam
= maxCheckState
[style
];
214 if ((infoPtr
->state
& 3) != wParam
)
216 infoPtr
->state
= (infoPtr
->state
& ~3) | wParam
;
217 PAINT_BUTTON( wndPtr
, style
, ODA_SELECT
);
219 if ((style
== BS_AUTORADIOBUTTON
) && (wParam
== BUTTON_CHECKED
))
220 BUTTON_CheckAutoRadioButton( wndPtr
);
225 return infoPtr
->state
;
231 if (infoPtr
->state
& BUTTON_HIGHLIGHTED
) break;
232 infoPtr
->state
|= BUTTON_HIGHLIGHTED
;
236 if (!(infoPtr
->state
& BUTTON_HIGHLIGHTED
)) break;
237 infoPtr
->state
&= ~BUTTON_HIGHLIGHTED
;
239 PAINT_BUTTON( wndPtr
, style
, ODA_SELECT
);
243 return DefWindowProc32A(hWnd
, uMsg
, wParam
, lParam
);
249 /**********************************************************************
250 * Push Button Functions
253 static void PB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
)
258 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
260 GetClientRect32( wndPtr
->hwndSelf
, &rc
);
262 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
263 if (infoPtr
->hFont
) SelectObject32( hDC
, infoPtr
->hFont
);
264 BUTTON_SEND_CTLCOLOR( wndPtr
, hDC
);
265 hOldPen
= (HPEN32
)SelectObject32(hDC
, sysColorObjects
.hpenWindowFrame
);
266 hOldBrush
= (HBRUSH32
)SelectObject32(hDC
, sysColorObjects
.hbrushBtnFace
);
267 SetBkMode32(hDC
, TRANSPARENT
);
268 Rectangle32(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
269 if (action
== ODA_DRAWENTIRE
)
271 SetPixel32( hDC
, rc
.left
, rc
.top
, GetSysColor32(COLOR_WINDOW
) );
272 SetPixel32( hDC
, rc
.left
, rc
.bottom
-1, GetSysColor32(COLOR_WINDOW
) );
273 SetPixel32( hDC
, rc
.right
-1, rc
.top
, GetSysColor32(COLOR_WINDOW
) );
274 SetPixel32( hDC
, rc
.right
-1, rc
.bottom
-1, GetSysColor32(COLOR_WINDOW
));
276 InflateRect32( &rc
, -1, -1 );
278 if ((wndPtr
->dwStyle
& 0x000f) == BS_DEFPUSHBUTTON
)
280 Rectangle32(hDC
, rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
281 InflateRect32( &rc
, -1, -1 );
284 if (infoPtr
->state
& BUTTON_HIGHLIGHTED
)
286 /* draw button shadow: */
287 SelectObject32(hDC
, sysColorObjects
.hbrushBtnShadow
);
288 PatBlt32(hDC
, rc
.left
, rc
.top
, 1, rc
.bottom
-rc
.top
, PATCOPY
);
289 PatBlt32(hDC
, rc
.left
, rc
.top
, rc
.right
-rc
.left
, 1, PATCOPY
);
290 rc
.left
+= 2; /* To position the text down and right */
293 else GRAPH_DrawReliefRect( hDC
, &rc
, 2, 2, FALSE
);
295 /* draw button label, if any: */
296 if (wndPtr
->text
&& wndPtr
->text
[0])
299 GetObject16( sysColorObjects
.hbrushBtnFace
, sizeof(lb
), &lb
);
300 if (wndPtr
->dwStyle
& WS_DISABLED
&&
301 GetSysColor32(COLOR_GRAYTEXT
)==lb
.lbColor
)
302 /* don't write gray text on gray bkg */
303 PB_PaintGrayOnGray(hDC
,infoPtr
->hFont
,&rc
,wndPtr
->text
);
306 SetTextColor32( hDC
, (wndPtr
->dwStyle
& WS_DISABLED
) ?
307 GetSysColor32(COLOR_GRAYTEXT
) :
308 GetSysColor32(COLOR_BTNTEXT
) );
309 DrawText32A( hDC
, wndPtr
->text
, -1, &rc
,
310 DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
311 /* do we have the focus? */
312 if (infoPtr
->state
& BUTTON_HASFOCUS
)
314 RECT32 r
= { 0, 0, 0, 0 };
315 INT32 xdelta
, ydelta
;
317 DrawText32A( hDC
, wndPtr
->text
, -1, &r
,
318 DT_SINGLELINE
| DT_CALCRECT
);
319 xdelta
= ((rc
.right
- rc
.left
) - (r
.right
- r
.left
) - 1) / 2;
320 ydelta
= ((rc
.bottom
- rc
.top
) - (r
.bottom
- r
.top
) - 1) / 2;
321 if (xdelta
< 0) xdelta
= 0;
322 if (ydelta
< 0) ydelta
= 0;
323 InflateRect32( &rc
, -xdelta
, -ydelta
);
324 DrawFocusRect32( hDC
, &rc
);
329 SelectObject32( hDC
, hOldPen
);
330 SelectObject32( hDC
, hOldBrush
);
334 /**********************************************************************
335 * Push Button sub function [internal]
336 * using a raster brush to avoid gray text on gray background
339 void PB_PaintGrayOnGray(HDC32 hDC
,HFONT32 hFont
,RECT32
*rc
,char *text
)
341 static const int Pattern
[] = {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55};
342 HBITMAP32 hbm
= CreateBitmap32( 8, 8, 1, 1, Pattern
);
343 HDC32 hdcMem
= CreateCompatibleDC32(hDC
);
349 DrawText32A( hDC
, text
, -1, &rect
, DT_SINGLELINE
| DT_CALCRECT
);
351 rect
.left
=(rc
->right
-rect
.right
)/2; /* for centering text bitmap */
352 rect
.top
=(rc
->bottom
-rect
.bottom
)/2;
353 hbmMem
= CreateCompatibleBitmap32( hDC
,rect
.right
,rect
.bottom
);
354 SelectObject32( hdcMem
, hbmMem
);
355 hBr
= SelectObject32( hdcMem
, CreatePatternBrush32(hbm
) );
356 DeleteObject32( hbm
);
357 PatBlt32( hdcMem
,0,0,rect
.right
,rect
.bottom
,WHITENESS
);
358 if (hFont
) SelectObject32( hdcMem
, hFont
);
359 DrawText32A( hdcMem
, text
, -1, &rc2
, DT_SINGLELINE
);
360 PatBlt32( hdcMem
,0,0,rect
.right
,rect
.bottom
,0xFA0089);
361 DeleteObject32( SelectObject32( hdcMem
,hBr
) );
362 BitBlt32(hDC
,rect
.left
,rect
.top
,rect
.right
,rect
.bottom
,hdcMem
,0,0,0x990000);
364 DeleteObject32( hbmMem
);
368 /**********************************************************************
369 * Check Box & Radio Button Functions
372 static void CB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
)
374 RECT16 rbox
, rtext
, client
;
377 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
380 GetClientRect16(wndPtr
->hwndSelf
, &client
);
381 rbox
= rtext
= client
;
383 if (infoPtr
->hFont
) SelectObject32( hDC
, infoPtr
->hFont
);
384 hBrush
= BUTTON_SEND_CTLCOLOR( wndPtr
, hDC
);
385 if (wndPtr
->dwStyle
& BS_LEFTTEXT
)
387 /* magic +4 is what CTL3D expects */
389 rtext
.right
-= checkBoxWidth
+ 4;
390 rbox
.left
= rbox
.right
- checkBoxWidth
;
394 rtext
.left
+= checkBoxWidth
+ 4;
395 rbox
.right
= checkBoxWidth
;
398 /* Draw the check-box bitmap */
400 if (wndPtr
->text
) textlen
= strlen( wndPtr
->text
);
401 if (action
== ODA_DRAWENTIRE
|| action
== ODA_SELECT
)
404 delta
= (rbox
.bottom
- rbox
.top
- checkBoxHeight
) >> 1;
406 if (action
== ODA_SELECT
) FillRect16( hDC
, &rbox
, hBrush
);
407 else FillRect16( hDC
, &client
, hBrush
);
409 if (infoPtr
->state
& BUTTON_HIGHLIGHTED
) x
+= 2 * checkBoxWidth
;
410 if (infoPtr
->state
& (BUTTON_CHECKED
| BUTTON_3STATE
)) x
+= checkBoxWidth
;
411 if (((wndPtr
->dwStyle
& 0x0f) == BS_RADIOBUTTON
) ||
412 ((wndPtr
->dwStyle
& 0x0f) == BS_AUTORADIOBUTTON
)) y
+= checkBoxHeight
;
413 else if (infoPtr
->state
& BUTTON_3STATE
) y
+= 2 * checkBoxHeight
;
415 GRAPH_DrawBitmap( hDC
, hbitmapCheckBoxes
, rbox
.left
, rbox
.top
+ delta
,
416 x
, y
, checkBoxWidth
, checkBoxHeight
);
417 if( textlen
&& action
!= ODA_SELECT
)
419 if (wndPtr
->dwStyle
& WS_DISABLED
)
420 SetTextColor32( hDC
, GetSysColor32(COLOR_GRAYTEXT
) );
421 DrawText16( hDC
, wndPtr
->text
, textlen
, &rtext
,
422 DT_SINGLELINE
| DT_VCENTER
);
426 if ((action
== ODA_FOCUS
) ||
427 ((action
== ODA_DRAWENTIRE
) && (infoPtr
->state
& BUTTON_HASFOCUS
)))
429 /* again, this is what CTL3D expects */
431 SetRectEmpty16(&rbox
);
433 DrawText16( hDC
, wndPtr
->text
, textlen
, &rbox
,
434 DT_SINGLELINE
| DT_CALCRECT
);
435 textlen
= rbox
.bottom
- rbox
.top
;
436 delta
= ((rtext
.bottom
- rtext
.top
) - textlen
)/2;
437 rbox
.bottom
= (rbox
.top
= rtext
.top
+ delta
- 1) + textlen
+ 2;
438 textlen
= rbox
.right
- rbox
.left
;
439 rbox
.right
= (rbox
.left
+= --rtext
.left
) + textlen
+ 2;
440 IntersectRect16(&rbox
, &rbox
, &rtext
);
441 DrawFocusRect16( hDC
, &rbox
);
446 /**********************************************************************
447 * BUTTON_CheckAutoRadioButton
449 * wndPtr is checked, uncheck every other auto radio button in group
451 static void BUTTON_CheckAutoRadioButton( WND
*wndPtr
)
453 HWND32 parent
, sibling
;
454 if (!(wndPtr
->dwStyle
& WS_CHILD
)) return;
455 parent
= wndPtr
->parent
->hwndSelf
;
456 for(sibling
= GetNextDlgGroupItem32( parent
, wndPtr
->hwndSelf
, FALSE
);
457 sibling
!= wndPtr
->hwndSelf
&& sibling
!= 0;
458 sibling
= GetNextDlgGroupItem32( parent
, sibling
, FALSE
))
459 if((WIN_FindWndPtr(sibling
)->dwStyle
& 0x0f) == BS_AUTORADIOBUTTON
)
460 SendMessage32A( sibling
, BM_SETCHECK32
, BUTTON_UNCHECKED
, 0 );
464 /**********************************************************************
465 * Group Box Functions
468 static void GB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
)
471 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
473 if (action
!= ODA_DRAWENTIRE
) return;
475 if (infoPtr
->hFont
) SelectObject32( hDC
, infoPtr
->hFont
);
476 BUTTON_SEND_CTLCOLOR( wndPtr
, hDC
);
477 SelectObject32( hDC
, sysColorObjects
.hpenWindowFrame
);
479 GetClientRect16( wndPtr
->hwndSelf
, &rc
);
481 MoveTo( hDC
, rc
.left
, rc
.top
+2 );
482 LineTo32( hDC
, rc
.right
-1, rc
.top
+2 );
483 LineTo32( hDC
, rc
.right
-1, rc
.bottom
-1 );
484 LineTo32( hDC
, rc
.left
, rc
.bottom
-1 );
485 LineTo32( hDC
, rc
.left
, rc
.top
+2 );
487 if (!wndPtr
->text
) return;
488 if (wndPtr
->dwStyle
& WS_DISABLED
)
489 SetTextColor32( hDC
, GetSysColor32(COLOR_GRAYTEXT
) );
491 DrawText16( hDC
, wndPtr
->text
, -1, &rc
, DT_SINGLELINE
| DT_NOCLIP
);
495 /**********************************************************************
496 * User Button Functions
499 static void UB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
)
503 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
505 if (action
== ODA_SELECT
) return;
507 GetClientRect16( wndPtr
->hwndSelf
, &rc
);
509 if (infoPtr
->hFont
) SelectObject32( hDC
, infoPtr
->hFont
);
510 hBrush
= BUTTON_SEND_CTLCOLOR( wndPtr
, hDC
);
511 FillRect16( hDC
, &rc
, hBrush
);
513 if ((action
== ODA_FOCUS
) ||
514 ((action
== ODA_DRAWENTIRE
) && (infoPtr
->state
& BUTTON_HASFOCUS
)))
515 DrawFocusRect16( hDC
, &rc
);
519 /**********************************************************************
520 * Ownerdrawn Button Functions
523 static void OB_Paint( WND
*wndPtr
, HDC32 hDC
, WORD action
)
525 BUTTONINFO
*infoPtr
= (BUTTONINFO
*)wndPtr
->wExtra
;
526 DRAWITEMSTRUCT32 dis
;
528 dis
.CtlType
= ODT_BUTTON
;
529 dis
.CtlID
= wndPtr
->wIDmenu
;
531 dis
.itemAction
= action
;
532 dis
.itemState
= ((infoPtr
->state
& BUTTON_HASFOCUS
) ? ODS_FOCUS
: 0) |
533 ((infoPtr
->state
& BUTTON_HIGHLIGHTED
) ? ODS_SELECTED
: 0) |
534 ((wndPtr
->dwStyle
& WS_DISABLED
) ? ODS_DISABLED
: 0);
535 dis
.hwndItem
= wndPtr
->hwndSelf
;
538 GetClientRect32( wndPtr
->hwndSelf
, &dis
.rcItem
);
539 SendMessage32A( GetParent32(wndPtr
->hwndSelf
), WM_DRAWITEM
,
540 wndPtr
->wIDmenu
, (LPARAM
)&dis
);