Added BN_CLICKED notification in WM_SETFOCUS.
[wine] / controls / button.c
1 /* File: button.c -- Button type widgets
2  *
3  * Copyright (C) 1993 Johannes Ruscheinski
4  * Copyright (C) 1993 David Metcalfe
5  * Copyright (C) 1994 Alexandre Julliard
6  */
7
8 #include <string.h>
9 #include "win.h"
10 #include "button.h"
11 #include "wine/winuser16.h"
12 #include "tweak.h"
13
14 static void PaintGrayOnGray( HDC hDC,HFONT hFont,RECT *rc,
15                              char *text, UINT format );
16
17 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action );
18 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action );
19 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action );
20 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action );
21 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action );
22 static void BUTTON_CheckAutoRadioButton( WND *wndPtr );
23
24 #define MAX_BTN_TYPE  12
25
26 static const WORD maxCheckState[MAX_BTN_TYPE] =
27 {
28     BUTTON_UNCHECKED,   /* BS_PUSHBUTTON */
29     BUTTON_UNCHECKED,   /* BS_DEFPUSHBUTTON */
30     BUTTON_CHECKED,     /* BS_CHECKBOX */
31     BUTTON_CHECKED,     /* BS_AUTOCHECKBOX */
32     BUTTON_CHECKED,     /* BS_RADIOBUTTON */
33     BUTTON_3STATE,      /* BS_3STATE */
34     BUTTON_3STATE,      /* BS_AUTO3STATE */
35     BUTTON_UNCHECKED,   /* BS_GROUPBOX */
36     BUTTON_UNCHECKED,   /* BS_USERBUTTON */
37     BUTTON_CHECKED,     /* BS_AUTORADIOBUTTON */
38     BUTTON_UNCHECKED,   /* Not defined */
39     BUTTON_UNCHECKED    /* BS_OWNERDRAW */
40 };
41
42 typedef void (*pfPaint)( WND *wndPtr, HDC hdc, WORD action );
43
44 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
45 {
46     PB_Paint,    /* BS_PUSHBUTTON */
47     PB_Paint,    /* BS_DEFPUSHBUTTON */
48     CB_Paint,    /* BS_CHECKBOX */
49     CB_Paint,    /* BS_AUTOCHECKBOX */
50     CB_Paint,    /* BS_RADIOBUTTON */
51     CB_Paint,    /* BS_3STATE */
52     CB_Paint,    /* BS_AUTO3STATE */
53     GB_Paint,    /* BS_GROUPBOX */
54     UB_Paint,    /* BS_USERBUTTON */
55     CB_Paint,    /* BS_AUTORADIOBUTTON */
56     NULL,        /* Not defined */
57     OB_Paint     /* BS_OWNERDRAW */
58 };
59
60 #define PAINT_BUTTON(wndPtr,style,action) \
61      if (btnPaintFunc[style]) { \
62          HDC hdc = GetDC( (wndPtr)->hwndSelf ); \
63          (btnPaintFunc[style])(wndPtr,hdc,action); \
64          ReleaseDC( (wndPtr)->hwndSelf, hdc ); }
65
66 #define BUTTON_SEND_CTLCOLOR(wndPtr,hdc) \
67     SendMessageA( GetParent((wndPtr)->hwndSelf), WM_CTLCOLORBTN, \
68                     (hdc), (wndPtr)->hwndSelf )
69
70 static HBITMAP hbitmapCheckBoxes = 0;
71 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
72
73
74 /***********************************************************************
75  *           ButtonWndProc_locked
76  * 
77  * Called with window lock held.
78  */
79 static inline LRESULT WINAPI ButtonWndProc_locked(WND* wndPtr, UINT uMsg,
80                                            WPARAM wParam, LPARAM lParam )
81 {
82     RECT rect;
83     HWND        hWnd = wndPtr->hwndSelf;
84     POINT pt;
85     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
86     LONG style = wndPtr->dwStyle & 0x0f;
87     HANDLE oldHbitmap;
88
89     pt.x = LOWORD(lParam);
90     pt.y = HIWORD(lParam);
91
92     switch (uMsg)
93     {
94     case WM_GETDLGCODE:
95         switch(style)
96         {
97         case BS_PUSHBUTTON:      return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
98         case BS_DEFPUSHBUTTON:   return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
99         case BS_RADIOBUTTON:
100         case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
101         default:                 return DLGC_BUTTON;
102         }
103
104     case WM_ENABLE:
105         PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
106         break;
107
108     case WM_CREATE:
109         if (!hbitmapCheckBoxes)
110         {
111             BITMAP bmp;
112             hbitmapCheckBoxes = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_CHECKBOXES));
113             GetObjectA( hbitmapCheckBoxes, sizeof(bmp), &bmp );
114             checkBoxWidth  = bmp.bmWidth / 4;
115             checkBoxHeight = bmp.bmHeight / 3;
116         }
117         if (style < 0L || style >= MAX_BTN_TYPE)
118             return -1; /* abort */
119         infoPtr->state = BUTTON_UNCHECKED;
120         infoPtr->hFont = 0;
121         infoPtr->hImage = NULL;
122         return 0;
123
124     case WM_ERASEBKGND:
125         return 1;
126
127     case WM_PAINT:
128         if (btnPaintFunc[style])
129         {
130             PAINTSTRUCT ps;
131             HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
132             SetBkMode( hdc, OPAQUE );
133             (btnPaintFunc[style])( wndPtr, hdc, ODA_DRAWENTIRE );
134             if( !wParam ) EndPaint( hWnd, &ps );
135         }
136         break;
137
138     case WM_LBUTTONDOWN:
139     case WM_LBUTTONDBLCLK:
140         SendMessageA( hWnd, BM_SETSTATE, TRUE, 0 );
141         SetCapture( hWnd );
142         SetFocus( hWnd );
143         break;
144
145     case WM_LBUTTONUP:
146         /* FIXME: real windows uses extra flags in the status for this */
147         if (GetCapture() != hWnd) break;
148         ReleaseCapture();
149         if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) break;
150         SendMessageA( hWnd, BM_SETSTATE, FALSE, 0 );
151         GetClientRect( hWnd, &rect );
152         if (PtInRect( &rect, pt ))
153         {
154             switch(style)
155             {
156             case BS_AUTOCHECKBOX:
157                 SendMessageA( hWnd, BM_SETCHECK,
158                                 !(infoPtr->state & BUTTON_CHECKED), 0 );
159                 break;
160             case BS_AUTORADIOBUTTON:
161                 SendMessageA( hWnd, BM_SETCHECK, TRUE, 0 );
162                 break;
163             case BS_AUTO3STATE:
164                 SendMessageA( hWnd, BM_SETCHECK,
165                                 (infoPtr->state & BUTTON_3STATE) ? 0 :
166                                 ((infoPtr->state & 3) + 1), 0 );
167                 break;
168             }
169             SendMessageA( GetParent(hWnd), WM_COMMAND,
170                             MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
171         }
172         break;
173
174     case WM_MOUSEMOVE:
175         if (GetCapture() == hWnd)
176         {
177             GetClientRect( hWnd, &rect );
178             SendMessageA( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
179         }
180         break;
181
182     case WM_NCHITTEST:
183         if(style == BS_GROUPBOX) return HTTRANSPARENT;
184         return DefWindowProcA( hWnd, uMsg, wParam, lParam );
185
186     case WM_SETTEXT:
187         DEFWND_SetText( wndPtr, (LPCSTR)lParam );
188         if( wndPtr->dwStyle & WS_VISIBLE )
189             PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
190         return 0;
191
192     case WM_SETFONT:
193         infoPtr->hFont = (HFONT16)wParam;
194         if (lParam && (wndPtr->dwStyle & WS_VISIBLE)) 
195             PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
196         break;
197
198     case WM_GETFONT:
199         return infoPtr->hFont;
200
201     case WM_SETFOCUS:
202         if ((style == BS_AUTORADIOBUTTON) && (GetCapture() != hWnd) &&
203             !(SendMessageA(hWnd, BM_GETCHECK, 0, 0) & BST_CHECKED))
204         {
205             /* The notification is sent when the button (BS_AUTORADIOBUTTON) 
206                is unckecked and the focus was not given by a mouse click. */
207             SendMessageA( hWnd, BM_SETCHECK, TRUE, 0 );
208             SendMessageA( GetParent(hWnd), WM_COMMAND,
209                           MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
210         }
211         infoPtr->state |= BUTTON_HASFOCUS;
212         PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
213         break;
214
215     case WM_KILLFOCUS:
216         infoPtr->state &= ~BUTTON_HASFOCUS;
217         PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
218         InvalidateRect( hWnd, NULL, TRUE );
219         break;
220
221     case WM_SYSCOLORCHANGE:
222         InvalidateRect( hWnd, NULL, FALSE );
223         break;
224
225     case BM_SETSTYLE16:
226     case BM_SETSTYLE:
227         if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
228         wndPtr->dwStyle = (wndPtr->dwStyle & 0xfffffff0) 
229                            | (wParam & 0x0000000f);
230         style = wndPtr->dwStyle & 0x0000000f;
231         PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
232         break;
233
234     case BM_SETIMAGE:
235         oldHbitmap = infoPtr->hImage;
236         if(wndPtr->dwStyle & BS_BITMAP)
237             infoPtr->hImage = (HANDLE) lParam;
238         return oldHbitmap;
239
240     case BM_GETIMAGE:
241         return infoPtr->hImage;
242
243     case BM_GETCHECK16:
244     case BM_GETCHECK:
245         return infoPtr->state & 3;
246
247     case BM_SETCHECK16:
248     case BM_SETCHECK:
249         if (wParam > maxCheckState[style]) wParam = maxCheckState[style];
250         if ((infoPtr->state & 3) != wParam)
251         {
252             if ((style == BS_RADIOBUTTON) || (style == BS_AUTORADIOBUTTON))
253             {
254                 if (wParam)
255                     wndPtr->dwStyle |= WS_TABSTOP;
256                 else
257                     wndPtr->dwStyle &= ~WS_TABSTOP;
258             }
259             infoPtr->state = (infoPtr->state & ~3) | wParam;
260             PAINT_BUTTON( wndPtr, style, ODA_SELECT );
261         }
262         if ((style == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED))
263             BUTTON_CheckAutoRadioButton( wndPtr );
264         break;
265
266     case BM_GETSTATE16:
267     case BM_GETSTATE:
268         return infoPtr->state;
269
270     case BM_SETSTATE16:
271     case BM_SETSTATE:
272         if (wParam)
273         {
274             if (infoPtr->state & BUTTON_HIGHLIGHTED) break;
275             infoPtr->state |= BUTTON_HIGHLIGHTED;
276         }
277         else
278         {
279             if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) break;
280             infoPtr->state &= ~BUTTON_HIGHLIGHTED;
281         }
282         PAINT_BUTTON( wndPtr, style, ODA_SELECT );
283         break;
284
285     default:
286         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
287     }
288     return 0;
289 }
290
291 /***********************************************************************
292  *           ButtonWndProc
293  * The button window procedure. This is just a wrapper which locks
294  * the passed HWND and calls the real window procedure (with a WND*
295  * pointer pointing to the locked windowstructure).
296  */
297 LRESULT WINAPI ButtonWndProc( HWND hWnd, UINT uMsg,
298                               WPARAM wParam, LPARAM lParam )
299 {
300     LRESULT res;
301     WND *wndPtr = WIN_FindWndPtr(hWnd);
302
303     res = ButtonWndProc_locked(wndPtr,uMsg,wParam,lParam);
304
305     WIN_ReleaseWndPtr(wndPtr);
306     return res;
307 }
308
309 /**********************************************************************
310  *       Push Button Functions
311  */
312
313 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action )
314 {
315     RECT rc;
316     HPEN hOldPen;
317     HBRUSH hOldBrush;
318     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
319     int xBorderOffset, yBorderOffset;
320     xBorderOffset = yBorderOffset = 0;
321
322     GetClientRect( wndPtr->hwndSelf, &rc );
323
324       /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
325     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
326     BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
327     hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
328     hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
329     SetBkMode(hDC, TRANSPARENT);
330     Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
331     if (TWEAK_WineLook == WIN31_LOOK)
332     {
333         SetPixel( hDC, rc.left, rc.top, GetSysColor(COLOR_WINDOW) );
334         SetPixel( hDC, rc.left, rc.bottom-1, GetSysColor(COLOR_WINDOW) );
335         SetPixel( hDC, rc.right-1, rc.top, GetSysColor(COLOR_WINDOW) );
336         SetPixel( hDC, rc.right-1, rc.bottom-1, GetSysColor(COLOR_WINDOW));
337     }
338     InflateRect( &rc, -1, -1 );
339
340     if ((wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON)
341     {
342         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
343         InflateRect( &rc, -1, -1 );
344     }
345
346     if (infoPtr->state & BUTTON_HIGHLIGHTED)
347     {
348         /* draw button shadow: */
349         SelectObject(hDC, GetSysColorBrush(COLOR_BTNSHADOW));
350         PatBlt(hDC, rc.left, rc.top, 1, rc.bottom-rc.top, PATCOPY );
351         PatBlt(hDC, rc.left, rc.top, rc.right-rc.left, 1, PATCOPY );
352         rc.left += 2;  /* To position the text down and right */
353         rc.top  += 2;
354     } else {
355         rc.right++, rc.bottom++;
356         DrawEdge( hDC, &rc, EDGE_RAISED, BF_RECT );
357
358         /* To place de bitmap correctly */
359         xBorderOffset += GetSystemMetrics(SM_CXEDGE);
360         yBorderOffset += GetSystemMetrics(SM_CYEDGE);
361
362         rc.right--, rc.bottom--;
363     }
364         
365     /* draw button label, if any: */
366     if (wndPtr->text && wndPtr->text[0])
367     {
368         LOGBRUSH lb;
369         GetObjectA( GetSysColorBrush(COLOR_BTNFACE), sizeof(lb), &lb );
370         if (wndPtr->dwStyle & WS_DISABLED &&
371             GetSysColor(COLOR_GRAYTEXT)==lb.lbColor)
372             /* don't write gray text on gray background */
373             PaintGrayOnGray( hDC,infoPtr->hFont,&rc,wndPtr->text,
374                                DT_CENTER | DT_VCENTER );
375         else
376         {
377             SetTextColor( hDC, (wndPtr->dwStyle & WS_DISABLED) ?
378                                  GetSysColor(COLOR_GRAYTEXT) :
379                                  GetSysColor(COLOR_BTNTEXT) );
380             DrawTextA( hDC, wndPtr->text, -1, &rc,
381                          DT_SINGLELINE | DT_CENTER | DT_VCENTER );
382             /* do we have the focus? */
383             if (infoPtr->state & BUTTON_HASFOCUS)
384             {
385                 RECT r = { 0, 0, 0, 0 };
386                 INT xdelta, ydelta;
387
388                 DrawTextA( hDC, wndPtr->text, -1, &r,
389                              DT_SINGLELINE | DT_CALCRECT );
390                 xdelta = ((rc.right - rc.left) - (r.right - r.left) - 1) / 2;
391                 ydelta = ((rc.bottom - rc.top) - (r.bottom - r.top) - 1) / 2;
392                 if (xdelta < 0) xdelta = 0;
393                 if (ydelta < 0) ydelta = 0;
394                 InflateRect( &rc, -xdelta, -ydelta );
395                 DrawFocusRect( hDC, &rc );
396             }
397         }   
398     }
399
400     if((wndPtr->dwStyle & BS_BITMAP) && (infoPtr->hImage != NULL))
401     {
402         BITMAP bm;
403         HDC hdcMem;
404         int yOffset, xOffset, imageWidth, imageHeight;
405
406         GetObjectA (infoPtr->hImage, sizeof(BITMAP), &bm);
407         
408         /* Center the bitmap */
409         xOffset = (((rc.right - rc.left) - 2*xBorderOffset) - bm.bmWidth ) / 2;
410         yOffset = (((rc.bottom - rc.top) - 2*yBorderOffset) - bm.bmHeight ) / 2;
411
412         imageWidth = bm.bmWidth;
413         imageHeight = bm.bmHeight;
414
415         /* If the image is to big for the button */
416         if (xOffset < 0)
417         {
418             imageWidth = rc.right - rc.left - 2*xBorderOffset -1;
419             xOffset = xBorderOffset;
420         }
421
422         if (yOffset < 0)
423         {
424             imageHeight = rc.bottom - rc.top - 2*yBorderOffset -1;
425             yOffset = yBorderOffset;
426         }
427
428         /* Let minimum 1 space from border */
429         xOffset++, yOffset++;
430
431         hdcMem = CreateCompatibleDC (hDC);
432         SelectObject (hdcMem, (HBITMAP)infoPtr->hImage);
433         BitBlt(hDC, rc.left + xOffset, 
434                rc.top + yOffset, 
435                imageWidth, imageHeight,
436                hdcMem, 0, 0, SRCCOPY);
437
438         DeleteDC (hdcMem);
439     }
440     
441     SelectObject( hDC, hOldPen );
442     SelectObject( hDC, hOldBrush );
443 }
444
445
446 /**********************************************************************
447  *   PB_Paint & CB_Paint sub function                        [internal]
448  *   Paint text using a raster brush to avoid gray text on gray 
449  *   background. 'format' can be a combination of DT_CENTER and 
450  *   DT_VCENTER to use this function in both PB_PAINT and 
451  *   CB_PAINT.   - Dirk Thierbach
452  *
453  *   FIXME: This and TEXT_GrayString should be eventually combined,
454  *   so calling one common function from PB_Paint, CB_Paint and
455  *   TEXT_GrayString will be enough. Also note that this
456  *   function ignores the CACHE_GetPattern funcs.
457  */
458
459 void PaintGrayOnGray(HDC hDC,HFONT hFont,RECT *rc,char *text,
460                         UINT format)
461 {
462 /*  This is the standard gray on gray pattern:
463     static const WORD Pattern[] = {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55}; 
464 */
465 /*  This pattern gives better readability with X Fonts.
466     FIXME: Maybe the user should be allowed to decide which he wants. */
467     static const WORD Pattern[] = {0x55,0xFF,0xAA,0xFF,0x55,0xFF,0xAA,0xFF}; 
468
469     HBITMAP hbm  = CreateBitmap( 8, 8, 1, 1, Pattern );
470     HDC hdcMem = CreateCompatibleDC(hDC);
471     HBITMAP hbmMem;
472     HBRUSH hBr;
473     RECT rect,rc2;
474
475     rect=*rc;
476     DrawTextA( hDC, text, -1, &rect, DT_SINGLELINE | DT_CALCRECT);
477     /* now text width and height are in rect.right and rect.bottom */
478     rc2=rect;
479     rect.left = rect.top = 0; /* drawing pos in hdcMem */
480     if (format & DT_CENTER) rect.left=(rc->right-rect.right)/2;
481     if (format & DT_VCENTER) rect.top=(rc->bottom-rect.bottom)/2;
482     hbmMem = CreateCompatibleBitmap( hDC,rect.right,rect.bottom );
483     SelectObject( hdcMem, hbmMem);
484     PatBlt( hdcMem,0,0,rect.right,rect.bottom,WHITENESS);
485       /* will be overwritten by DrawText, but just in case */
486     if (hFont) SelectObject( hdcMem, hFont);
487     DrawTextA( hdcMem, text, -1, &rc2, DT_SINGLELINE);  
488       /* After draw: foreground = 0 bits, background = 1 bits */
489     hBr = SelectObject( hdcMem, CreatePatternBrush(hbm) );
490     DeleteObject( hbm );
491     PatBlt( hdcMem,0,0,rect.right,rect.bottom,0xAF0229); 
492       /* only keep the foreground bits where pattern is 1 */
493     DeleteObject( SelectObject( hdcMem,hBr) );
494     BitBlt(hDC,rect.left,rect.top,rect.right,rect.bottom,hdcMem,0,0,SRCAND);
495       /* keep the background of the dest */
496     DeleteDC( hdcMem);
497     DeleteObject( hbmMem );
498 }
499
500
501 /**********************************************************************
502  *       Check Box & Radio Button Functions
503  */
504
505 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action )
506 {
507     RECT rbox, rtext, client;
508     HBRUSH hBrush;
509     int textlen, delta;
510     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
511
512     textlen = 0;
513     GetClientRect(wndPtr->hwndSelf, &client);
514     rbox = rtext = client;
515
516     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
517
518     /* Something is still not right, checkboxes (and edit controls)
519      * in wsping32 have white backgrounds instead of dark grey.
520      * BUTTON_SEND_CTLCOLOR() is even worse since it returns 0 in this
521      * particular case and the background is not painted at all.
522      */
523
524     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
525
526     if (wndPtr->dwStyle & BS_LEFTTEXT) 
527     {
528         /* magic +4 is what CTL3D expects */
529
530         rtext.right -= checkBoxWidth + 4;
531         rbox.left = rbox.right - checkBoxWidth;
532     }
533     else 
534     {
535         rtext.left += checkBoxWidth + 4;
536         rbox.right = checkBoxWidth;
537     }
538
539       /* Draw the check-box bitmap */
540
541     if (wndPtr->text) textlen = strlen( wndPtr->text );
542     if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
543     { 
544         HDC hMemDC = CreateCompatibleDC( hDC );
545         int x = 0, y = 0;
546         delta = (rbox.bottom - rbox.top - checkBoxHeight) >> 1;
547
548         if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
549         else FillRect( hDC, &client, hBrush );
550
551         if (infoPtr->state & BUTTON_HIGHLIGHTED) x += 2 * checkBoxWidth;
552         if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) x += checkBoxWidth;
553         if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
554             ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) y += checkBoxHeight;
555         else if (infoPtr->state & BUTTON_3STATE) y += 2 * checkBoxHeight;
556
557         SelectObject( hMemDC, hbitmapCheckBoxes );
558         BitBlt( hDC, rbox.left, rbox.top + delta, checkBoxWidth,
559                   checkBoxHeight, hMemDC, x, y, SRCCOPY );
560         DeleteDC( hMemDC );
561
562         if( textlen && action != ODA_SELECT )
563         {
564           if (wndPtr->dwStyle & WS_DISABLED &&
565               GetSysColor(COLOR_GRAYTEXT)==GetBkColor(hDC)) {
566             /* don't write gray text on gray background */
567             PaintGrayOnGray( hDC, infoPtr->hFont, &rtext, wndPtr->text,
568                              DT_VCENTER);
569           } else {
570             if (wndPtr->dwStyle & WS_DISABLED)
571                 SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
572             DrawTextA( hDC, wndPtr->text, textlen, &rtext,
573                          DT_SINGLELINE | DT_VCENTER );
574             textlen = 0; /* skip DrawText() below */
575           }
576         }
577     }
578
579     if ((action == ODA_FOCUS) ||
580         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
581     {
582         /* again, this is what CTL3D expects */
583
584         SetRectEmpty(&rbox);
585         if( textlen )
586             DrawTextA( hDC, wndPtr->text, textlen, &rbox,
587                          DT_SINGLELINE | DT_CALCRECT );
588         textlen = rbox.bottom - rbox.top;
589         delta = ((rtext.bottom - rtext.top) - textlen)/2;
590         rbox.bottom = (rbox.top = rtext.top + delta - 1) + textlen + 2;
591         textlen = rbox.right - rbox.left;
592         rbox.right = (rbox.left += --rtext.left) + textlen + 2;
593         IntersectRect(&rbox, &rbox, &rtext);
594         DrawFocusRect( hDC, &rbox );
595     }
596 }
597
598
599 /**********************************************************************
600  *       BUTTON_CheckAutoRadioButton
601  *
602  * wndPtr is checked, uncheck every other auto radio button in group
603  */
604 static void BUTTON_CheckAutoRadioButton( WND *wndPtr )
605 {
606     HWND parent, sibling, start;
607     if (!(wndPtr->dwStyle & WS_CHILD)) return;
608     parent = wndPtr->parent->hwndSelf;
609     /* assure that starting control is not disabled or invisible */
610     start = sibling = GetNextDlgGroupItem( parent, wndPtr->hwndSelf, TRUE );
611     do
612     {
613         WND *tmpWnd;
614         if (!sibling) break;
615         tmpWnd = WIN_FindWndPtr(sibling);
616         if ((wndPtr->hwndSelf != sibling) &&
617             ((tmpWnd->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
618             SendMessageA( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
619         sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
620         WIN_ReleaseWndPtr(tmpWnd);
621     } while (sibling != start);
622 }
623
624
625 /**********************************************************************
626  *       Group Box Functions
627  */
628
629 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action )
630 {
631     RECT rc, rcFrame;
632     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
633
634     if (action != ODA_DRAWENTIRE) return;
635
636     BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
637
638     GetClientRect( wndPtr->hwndSelf, &rc);
639     if (TWEAK_WineLook == WIN31_LOOK) {
640         HPEN hPrevPen = SelectObject( hDC,
641                                           GetSysColorPen(COLOR_WINDOWFRAME));
642         HBRUSH hPrevBrush = SelectObject( hDC,
643                                               GetStockObject(NULL_BRUSH) );
644
645         Rectangle( hDC, rc.left, rc.top + 2, rc.right - 1, rc.bottom - 1 );
646         SelectObject( hDC, hPrevBrush );
647         SelectObject( hDC, hPrevPen );
648     } else {
649         TEXTMETRICA tm;
650         rcFrame = rc;
651
652         if (infoPtr->hFont)
653             SelectObject (hDC, infoPtr->hFont);
654         GetTextMetricsA (hDC, &tm);
655         rcFrame.top += (tm.tmHeight / 2) - 1;
656         DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT);
657     }
658
659     if (wndPtr->text)
660     {
661         if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
662         if (wndPtr->dwStyle & WS_DISABLED)
663             SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
664         rc.left += 10;
665         DrawTextA( hDC, wndPtr->text, -1, &rc, DT_SINGLELINE | DT_NOCLIP );
666     }
667 }
668
669
670 /**********************************************************************
671  *       User Button Functions
672  */
673
674 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action )
675 {
676     RECT rc;
677     HBRUSH hBrush;
678     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
679
680     if (action == ODA_SELECT) return;
681
682     GetClientRect( wndPtr->hwndSelf, &rc);
683
684     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
685     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
686
687     FillRect( hDC, &rc, hBrush );
688     if ((action == ODA_FOCUS) ||
689         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
690         DrawFocusRect( hDC, &rc );
691 }
692
693
694 /**********************************************************************
695  *       Ownerdrawn Button Functions
696  */
697
698 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action )
699 {
700     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
701     DRAWITEMSTRUCT dis;
702
703     dis.CtlType    = ODT_BUTTON;
704     dis.CtlID      = wndPtr->wIDmenu;
705     dis.itemID     = 0;
706     dis.itemAction = action;
707     dis.itemState  = ((infoPtr->state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
708                      ((infoPtr->state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
709                      ((wndPtr->dwStyle & WS_DISABLED) ? ODS_DISABLED : 0);
710     dis.hwndItem   = wndPtr->hwndSelf;
711     dis.hDC        = hDC;
712     dis.itemData   = 0;
713     GetClientRect( wndPtr->hwndSelf, &dis.rcItem );
714     SendMessageA( GetParent(wndPtr->hwndSelf), WM_DRAWITEM,
715                     wndPtr->wIDmenu, (LPARAM)&dis );
716 }
717