Win9x look & feel on buttons and a fix for ownerdrawn buttons
[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, focus_rect;
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
331     if ( TWEAK_WineLook == WIN31_LOOK)
332     {
333         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
334
335         SetPixel( hDC, rc.left, rc.top, GetSysColor(COLOR_WINDOW) );
336         SetPixel( hDC, rc.left, rc.bottom-1, GetSysColor(COLOR_WINDOW) );
337         SetPixel( hDC, rc.right-1, rc.top, GetSysColor(COLOR_WINDOW) );
338         SetPixel( hDC, rc.right-1, rc.bottom-1, GetSysColor(COLOR_WINDOW));
339         InflateRect( &rc, -1, -1 );
340     }
341     
342     if ((wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON)
343     {
344         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
345         InflateRect( &rc, -1, -1 );
346     }
347
348     if (TWEAK_WineLook == WIN31_LOOK)
349     {
350         if (infoPtr->state & BUTTON_HIGHLIGHTED)
351         {
352             /* draw button shadow: */
353             SelectObject(hDC, GetSysColorBrush(COLOR_BTNSHADOW));
354             PatBlt(hDC, rc.left, rc.top, 1, rc.bottom-rc.top, PATCOPY );
355             PatBlt(hDC, rc.left, rc.top, rc.right-rc.left, 1, PATCOPY );
356             rc.left += 2;  /* To position the text down and right */
357             rc.top  += 2;
358         } else {
359            rc.right++, rc.bottom++;
360            DrawEdge( hDC, &rc, EDGE_RAISED, BF_RECT );
361
362            /* To place de bitmap correctly */
363            xBorderOffset += GetSystemMetrics(SM_CXEDGE);
364            yBorderOffset += GetSystemMetrics(SM_CYEDGE);
365
366            rc.right--, rc.bottom--;
367         }
368     }
369     else
370     {
371         UINT uState = DFCS_BUTTONPUSH;
372
373         if (infoPtr->state & BUTTON_HIGHLIGHTED)
374         {
375             if ( (wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON )
376                 uState |= DFCS_FLAT;
377             else
378                 uState |= DFCS_PUSHED;
379         }
380
381         DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
382         InflateRect( &rc, -2, -2 );
383
384         focus_rect = rc;
385
386         if (infoPtr->state & BUTTON_HIGHLIGHTED)
387         {
388             rc.left += 2;  /* To position the text down and right */
389             rc.top  += 2;
390         }
391     }
392
393     /* draw button label, if any:
394      *
395      * In win9x we don't show text if there is a bitmap or icon.
396      * I don't know about win31 so I leave it as it was for win31.
397      * Dennis Björklund 12 Jul, 99
398      */
399     if ( wndPtr->text && wndPtr->text[0]
400          && (TWEAK_WineLook == WIN31_LOOK || !(wndPtr->dwStyle & (BS_ICON|BS_BITMAP))) )
401     {
402         LOGBRUSH lb;
403         GetObjectA( GetSysColorBrush(COLOR_BTNFACE), sizeof(lb), &lb );
404         if (wndPtr->dwStyle & WS_DISABLED &&
405             GetSysColor(COLOR_GRAYTEXT)==lb.lbColor)
406             /* don't write gray text on gray background */
407             PaintGrayOnGray( hDC,infoPtr->hFont,&rc,wndPtr->text,
408                                DT_CENTER | DT_VCENTER );
409         else
410         {
411             SetTextColor( hDC, (wndPtr->dwStyle & WS_DISABLED) ?
412                                  GetSysColor(COLOR_GRAYTEXT) :
413                                  GetSysColor(COLOR_BTNTEXT) );
414             DrawTextA( hDC, wndPtr->text, -1, &rc,
415                          DT_SINGLELINE | DT_CENTER | DT_VCENTER );
416             /* do we have the focus?
417              * Win9x draws focus last with a size prop. to the button
418              */
419             if (TWEAK_WineLook == WIN31_LOOK
420                 && infoPtr->state & BUTTON_HASFOCUS)
421             {
422                 RECT r = { 0, 0, 0, 0 };
423                 INT xdelta, ydelta;
424
425                 DrawTextA( hDC, wndPtr->text, -1, &r,
426                              DT_SINGLELINE | DT_CALCRECT );
427                 xdelta = ((rc.right - rc.left) - (r.right - r.left) - 1) / 2;
428                 ydelta = ((rc.bottom - rc.top) - (r.bottom - r.top) - 1) / 2;
429                 if (xdelta < 0) xdelta = 0;
430                 if (ydelta < 0) ydelta = 0;
431                 InflateRect( &rc, -xdelta, -ydelta );
432                 DrawFocusRect( hDC, &rc );
433             }
434         }   
435     }
436
437     if((wndPtr->dwStyle & BS_BITMAP) && (infoPtr->hImage != NULL))
438     {
439         BITMAP bm;
440         HDC hdcMem;
441         int yOffset, xOffset, imageWidth, imageHeight;
442
443         GetObjectA (infoPtr->hImage, sizeof(BITMAP), &bm);
444         
445         /* Center the bitmap */
446         xOffset = (((rc.right - rc.left) - 2*xBorderOffset) - bm.bmWidth ) / 2;
447         yOffset = (((rc.bottom - rc.top) - 2*yBorderOffset) - bm.bmHeight ) / 2;
448
449         imageWidth = bm.bmWidth;
450         imageHeight = bm.bmHeight;
451
452         /* If the image is to big for the button */
453         if (xOffset < 0)
454         {
455             imageWidth = rc.right - rc.left - 2*xBorderOffset -1;
456             xOffset = xBorderOffset;
457         }
458
459         if (yOffset < 0)
460         {
461             imageHeight = rc.bottom - rc.top - 2*yBorderOffset -1;
462             yOffset = yBorderOffset;
463         }
464
465         /* Let minimum 1 space from border */
466         xOffset++, yOffset++;
467
468         hdcMem = CreateCompatibleDC (hDC);
469         SelectObject (hdcMem, (HBITMAP)infoPtr->hImage);
470         BitBlt(hDC, rc.left + xOffset, 
471                rc.top + yOffset, 
472                imageWidth, imageHeight,
473                hdcMem, 0, 0, SRCCOPY);
474
475         DeleteDC (hdcMem);
476     }
477
478     if (TWEAK_WineLook != WIN31_LOOK
479         && infoPtr->state & BUTTON_HASFOCUS)
480     {
481         InflateRect( &focus_rect, -1, -1 );
482         DrawFocusRect( hDC, &focus_rect );
483     }
484
485     
486     SelectObject( hDC, hOldPen );
487     SelectObject( hDC, hOldBrush );
488 }
489
490
491 /**********************************************************************
492  *   PB_Paint & CB_Paint sub function                        [internal]
493  *   Paint text using a raster brush to avoid gray text on gray 
494  *   background. 'format' can be a combination of DT_CENTER and 
495  *   DT_VCENTER to use this function in both PB_PAINT and 
496  *   CB_PAINT.   - Dirk Thierbach
497  *
498  *   FIXME: This and TEXT_GrayString should be eventually combined,
499  *   so calling one common function from PB_Paint, CB_Paint and
500  *   TEXT_GrayString will be enough. Also note that this
501  *   function ignores the CACHE_GetPattern funcs.
502  */
503
504 void PaintGrayOnGray(HDC hDC,HFONT hFont,RECT *rc,char *text,
505                         UINT format)
506 {
507 /*  This is the standard gray on gray pattern:
508     static const WORD Pattern[] = {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55}; 
509 */
510 /*  This pattern gives better readability with X Fonts.
511     FIXME: Maybe the user should be allowed to decide which he wants. */
512     static const WORD Pattern[] = {0x55,0xFF,0xAA,0xFF,0x55,0xFF,0xAA,0xFF}; 
513
514     HBITMAP hbm  = CreateBitmap( 8, 8, 1, 1, Pattern );
515     HDC hdcMem = CreateCompatibleDC(hDC);
516     HBITMAP hbmMem;
517     HBRUSH hBr;
518     RECT rect,rc2;
519
520     rect=*rc;
521     DrawTextA( hDC, text, -1, &rect, DT_SINGLELINE | DT_CALCRECT);
522     /* now text width and height are in rect.right and rect.bottom */
523     rc2=rect;
524     rect.left = rect.top = 0; /* drawing pos in hdcMem */
525     if (format & DT_CENTER) rect.left=(rc->right-rect.right)/2;
526     if (format & DT_VCENTER) rect.top=(rc->bottom-rect.bottom)/2;
527     hbmMem = CreateCompatibleBitmap( hDC,rect.right,rect.bottom );
528     SelectObject( hdcMem, hbmMem);
529     PatBlt( hdcMem,0,0,rect.right,rect.bottom,WHITENESS);
530       /* will be overwritten by DrawText, but just in case */
531     if (hFont) SelectObject( hdcMem, hFont);
532     DrawTextA( hdcMem, text, -1, &rc2, DT_SINGLELINE);  
533       /* After draw: foreground = 0 bits, background = 1 bits */
534     hBr = SelectObject( hdcMem, CreatePatternBrush(hbm) );
535     DeleteObject( hbm );
536     PatBlt( hdcMem,0,0,rect.right,rect.bottom,0xAF0229); 
537       /* only keep the foreground bits where pattern is 1 */
538     DeleteObject( SelectObject( hdcMem,hBr) );
539     BitBlt(hDC,rect.left,rect.top,rect.right,rect.bottom,hdcMem,0,0,SRCAND);
540       /* keep the background of the dest */
541     DeleteDC( hdcMem);
542     DeleteObject( hbmMem );
543 }
544
545
546 /**********************************************************************
547  *       Check Box & Radio Button Functions
548  */
549
550 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action )
551 {
552     RECT rbox, rtext, client;
553     HBRUSH hBrush;
554     int textlen, delta;
555     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
556
557     textlen = 0;
558     GetClientRect(wndPtr->hwndSelf, &client);
559     rbox = rtext = client;
560
561     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
562
563     /* Something is still not right, checkboxes (and edit controls)
564      * in wsping32 have white backgrounds instead of dark grey.
565      * BUTTON_SEND_CTLCOLOR() is even worse since it returns 0 in this
566      * particular case and the background is not painted at all.
567      */
568
569     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
570
571     if (wndPtr->dwStyle & BS_LEFTTEXT) 
572     {
573         /* magic +4 is what CTL3D expects */
574
575         rtext.right -= checkBoxWidth + 4;
576         rbox.left = rbox.right - checkBoxWidth;
577     }
578     else 
579     {
580         rtext.left += checkBoxWidth + 4;
581         rbox.right = checkBoxWidth;
582     }
583
584       /* Draw the check-box bitmap */
585
586     if (wndPtr->text) textlen = strlen( wndPtr->text );
587     if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
588     { 
589         HDC hMemDC = CreateCompatibleDC( hDC );
590         int x = 0, y = 0;
591         delta = (rbox.bottom - rbox.top - checkBoxHeight) >> 1;
592
593         if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
594         else FillRect( hDC, &client, hBrush );
595
596         if (infoPtr->state & BUTTON_HIGHLIGHTED) x += 2 * checkBoxWidth;
597         if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) x += checkBoxWidth;
598         if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
599             ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) y += checkBoxHeight;
600         else if (infoPtr->state & BUTTON_3STATE) y += 2 * checkBoxHeight;
601
602         SelectObject( hMemDC, hbitmapCheckBoxes );
603         BitBlt( hDC, rbox.left, rbox.top + delta, checkBoxWidth,
604                   checkBoxHeight, hMemDC, x, y, SRCCOPY );
605         DeleteDC( hMemDC );
606
607         if( textlen && action != ODA_SELECT )
608         {
609           if (wndPtr->dwStyle & WS_DISABLED &&
610               GetSysColor(COLOR_GRAYTEXT)==GetBkColor(hDC)) {
611             /* don't write gray text on gray background */
612             PaintGrayOnGray( hDC, infoPtr->hFont, &rtext, wndPtr->text,
613                              DT_VCENTER);
614           } else {
615             if (wndPtr->dwStyle & WS_DISABLED)
616                 SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
617             DrawTextA( hDC, wndPtr->text, textlen, &rtext,
618                          DT_SINGLELINE | DT_VCENTER );
619             textlen = 0; /* skip DrawText() below */
620           }
621         }
622     }
623
624     if ((action == ODA_FOCUS) ||
625         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
626     {
627         /* again, this is what CTL3D expects */
628
629         SetRectEmpty(&rbox);
630         if( textlen )
631             DrawTextA( hDC, wndPtr->text, textlen, &rbox,
632                          DT_SINGLELINE | DT_CALCRECT );
633         textlen = rbox.bottom - rbox.top;
634         delta = ((rtext.bottom - rtext.top) - textlen)/2;
635         rbox.bottom = (rbox.top = rtext.top + delta - 1) + textlen + 2;
636         textlen = rbox.right - rbox.left;
637         rbox.right = (rbox.left += --rtext.left) + textlen + 2;
638         IntersectRect(&rbox, &rbox, &rtext);
639         DrawFocusRect( hDC, &rbox );
640     }
641 }
642
643
644 /**********************************************************************
645  *       BUTTON_CheckAutoRadioButton
646  *
647  * wndPtr is checked, uncheck every other auto radio button in group
648  */
649 static void BUTTON_CheckAutoRadioButton( WND *wndPtr )
650 {
651     HWND parent, sibling, start;
652     if (!(wndPtr->dwStyle & WS_CHILD)) return;
653     parent = wndPtr->parent->hwndSelf;
654     /* assure that starting control is not disabled or invisible */
655     start = sibling = GetNextDlgGroupItem( parent, wndPtr->hwndSelf, TRUE );
656     do
657     {
658         WND *tmpWnd;
659         if (!sibling) break;
660         tmpWnd = WIN_FindWndPtr(sibling);
661         if ((wndPtr->hwndSelf != sibling) &&
662             ((tmpWnd->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
663             SendMessageA( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
664         sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
665         WIN_ReleaseWndPtr(tmpWnd);
666     } while (sibling != start);
667 }
668
669
670 /**********************************************************************
671  *       Group Box Functions
672  */
673
674 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action )
675 {
676     RECT rc, rcFrame;
677     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
678
679     if (action != ODA_DRAWENTIRE) return;
680
681     BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
682
683     GetClientRect( wndPtr->hwndSelf, &rc);
684     if (TWEAK_WineLook == WIN31_LOOK) {
685         HPEN hPrevPen = SelectObject( hDC,
686                                           GetSysColorPen(COLOR_WINDOWFRAME));
687         HBRUSH hPrevBrush = SelectObject( hDC,
688                                               GetStockObject(NULL_BRUSH) );
689
690         Rectangle( hDC, rc.left, rc.top + 2, rc.right - 1, rc.bottom - 1 );
691         SelectObject( hDC, hPrevBrush );
692         SelectObject( hDC, hPrevPen );
693     } else {
694         TEXTMETRICA tm;
695         rcFrame = rc;
696
697         if (infoPtr->hFont)
698             SelectObject (hDC, infoPtr->hFont);
699         GetTextMetricsA (hDC, &tm);
700         rcFrame.top += (tm.tmHeight / 2) - 1;
701         DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT);
702     }
703
704     if (wndPtr->text)
705     {
706         if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
707         if (wndPtr->dwStyle & WS_DISABLED)
708             SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
709         rc.left += 10;
710         DrawTextA( hDC, wndPtr->text, -1, &rc, DT_SINGLELINE | DT_NOCLIP );
711     }
712 }
713
714
715 /**********************************************************************
716  *       User Button Functions
717  */
718
719 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action )
720 {
721     RECT rc;
722     HBRUSH hBrush;
723     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
724
725     if (action == ODA_SELECT) return;
726
727     GetClientRect( wndPtr->hwndSelf, &rc);
728
729     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
730     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
731
732     FillRect( hDC, &rc, hBrush );
733     if ((action == ODA_FOCUS) ||
734         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
735         DrawFocusRect( hDC, &rc );
736 }
737
738
739 /**********************************************************************
740  *       Ownerdrawn Button Functions
741  */
742
743 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action )
744 {
745     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
746     DRAWITEMSTRUCT dis;
747
748     dis.CtlType    = ODT_BUTTON;
749     dis.CtlID      = wndPtr->wIDmenu;
750     dis.itemID     = 0;
751     dis.itemAction = action;
752     dis.itemState  = ((infoPtr->state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
753                      ((infoPtr->state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
754                      ((wndPtr->dwStyle & WS_DISABLED) ? ODS_DISABLED : 0);
755     dis.hwndItem   = wndPtr->hwndSelf;
756     dis.hDC        = hDC;
757     dis.itemData   = 0;
758     GetClientRect( wndPtr->hwndSelf, &dis.rcItem );
759
760     SetBkColor( hDC, GetSysColor( COLOR_BTNFACE ) );
761     FillRect( hDC,  &dis.rcItem, GetSysColorBrush( COLOR_BTNFACE ) );
762
763     SendMessageA( GetParent(wndPtr->hwndSelf), WM_DRAWITEM,
764                     wndPtr->wIDmenu, (LPARAM)&dis );
765 }
766