Like the AUTORADIOBUTTON, the parent of a RADIOBUTTON style button
[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 "winbase.h"
12 #include "windef.h"
13 #include "wingdi.h"
14 #include "wine/winuser16.h"
15 #include "tweak.h"
16
17 static void PaintGrayOnGray( HDC hDC,HFONT hFont,RECT *rc,
18                              char *text, UINT format );
19
20 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action );
21 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action );
22 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action );
23 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action );
24 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action );
25 static void BUTTON_CheckAutoRadioButton( WND *wndPtr );
26 static void BUTTON_DrawPushButton( WND *wndPtr, HDC hDC, WORD action, BOOL pushedState);
27
28 #define MAX_BTN_TYPE  12
29
30 static const WORD maxCheckState[MAX_BTN_TYPE] =
31 {
32     BUTTON_UNCHECKED,   /* BS_PUSHBUTTON */
33     BUTTON_UNCHECKED,   /* BS_DEFPUSHBUTTON */
34     BUTTON_CHECKED,     /* BS_CHECKBOX */
35     BUTTON_CHECKED,     /* BS_AUTOCHECKBOX */
36     BUTTON_CHECKED,     /* BS_RADIOBUTTON */
37     BUTTON_3STATE,      /* BS_3STATE */
38     BUTTON_3STATE,      /* BS_AUTO3STATE */
39     BUTTON_UNCHECKED,   /* BS_GROUPBOX */
40     BUTTON_UNCHECKED,   /* BS_USERBUTTON */
41     BUTTON_CHECKED,     /* BS_AUTORADIOBUTTON */
42     BUTTON_UNCHECKED,   /* Not defined */
43     BUTTON_UNCHECKED    /* BS_OWNERDRAW */
44 };
45
46 typedef void (*pfPaint)( WND *wndPtr, HDC hdc, WORD action );
47
48 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
49 {
50     PB_Paint,    /* BS_PUSHBUTTON */
51     PB_Paint,    /* BS_DEFPUSHBUTTON */
52     CB_Paint,    /* BS_CHECKBOX */
53     CB_Paint,    /* BS_AUTOCHECKBOX */
54     CB_Paint,    /* BS_RADIOBUTTON */
55     CB_Paint,    /* BS_3STATE */
56     CB_Paint,    /* BS_AUTO3STATE */
57     GB_Paint,    /* BS_GROUPBOX */
58     UB_Paint,    /* BS_USERBUTTON */
59     CB_Paint,    /* BS_AUTORADIOBUTTON */
60     NULL,        /* Not defined */
61     OB_Paint     /* BS_OWNERDRAW */
62 };
63
64 #define PAINT_BUTTON(wndPtr,style,action) \
65      if (btnPaintFunc[style]) { \
66          HDC hdc = GetDC( (wndPtr)->hwndSelf ); \
67          (btnPaintFunc[style])(wndPtr,hdc,action); \
68          ReleaseDC( (wndPtr)->hwndSelf, hdc ); }
69
70 #define BUTTON_SEND_CTLCOLOR(wndPtr,hdc) \
71     SendMessageA( GetParent((wndPtr)->hwndSelf), WM_CTLCOLORBTN, \
72                     (hdc), (wndPtr)->hwndSelf )
73
74 static HBITMAP hbitmapCheckBoxes = 0;
75 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
76
77
78 /***********************************************************************
79  *           ButtonWndProc_locked
80  * 
81  * Called with window lock held.
82  */
83 static inline LRESULT WINAPI ButtonWndProc_locked(WND* wndPtr, UINT uMsg,
84                                            WPARAM wParam, LPARAM lParam )
85 {
86     RECT rect;
87     HWND        hWnd = wndPtr->hwndSelf;
88     POINT pt;
89     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
90     LONG style = wndPtr->dwStyle & 0x0f;
91     HANDLE oldHbitmap;
92
93     pt.x = LOWORD(lParam);
94     pt.y = HIWORD(lParam);
95
96     switch (uMsg)
97     {
98     case WM_GETDLGCODE:
99         switch(style)
100         {
101         case BS_PUSHBUTTON:      return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
102         case BS_DEFPUSHBUTTON:   return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
103         case BS_RADIOBUTTON:
104         case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
105         default:                 return DLGC_BUTTON;
106         }
107
108     case WM_ENABLE:
109         PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
110         break;
111
112     case WM_CREATE:
113         if (!hbitmapCheckBoxes)
114         {
115             BITMAP bmp;
116             hbitmapCheckBoxes = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_CHECKBOXES));
117             GetObjectA( hbitmapCheckBoxes, sizeof(bmp), &bmp );
118             checkBoxWidth  = bmp.bmWidth / 4;
119             checkBoxHeight = bmp.bmHeight / 3;
120         }
121         if (style < 0L || style >= MAX_BTN_TYPE)
122             return -1; /* abort */
123         infoPtr->state = BUTTON_UNCHECKED;
124         infoPtr->hFont = 0;
125         infoPtr->hImage = 0;
126         return 0;
127
128     case WM_ERASEBKGND:
129         return 1;
130
131     case WM_PAINT:
132         if (btnPaintFunc[style])
133         {
134             PAINTSTRUCT ps;
135             HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
136             SetBkMode( hdc, OPAQUE );
137             (btnPaintFunc[style])( wndPtr, hdc, ODA_DRAWENTIRE );
138             if( !wParam ) EndPaint( hWnd, &ps );
139         }
140         break;
141
142     case WM_KEYDOWN:
143         if (wParam == VK_SPACE)
144         {
145             SendMessageA( hWnd, BM_SETSTATE, TRUE, 0 );
146             infoPtr->state |= BUTTON_BTNPRESSED;
147         }
148         break;
149         
150     case WM_LBUTTONDBLCLK:
151         if(wndPtr->dwStyle & BS_NOTIFY || 
152                 style==BS_RADIOBUTTON ||
153                 style==BS_USERBUTTON ||
154                 style==BS_OWNERDRAW) {
155             SendMessageA( GetParent(hWnd), WM_COMMAND,
156                     MAKEWPARAM( wndPtr->wIDmenu, BN_DOUBLECLICKED ), hWnd);
157             break;
158         }
159         /* fall through */
160     case WM_LBUTTONDOWN:
161         SetCapture( hWnd );
162         SetFocus( hWnd );
163         SendMessageA( hWnd, BM_SETSTATE, TRUE, 0 );
164         infoPtr->state |= BUTTON_BTNPRESSED;
165         break;
166
167     case WM_KEYUP:
168         if (wParam != VK_SPACE)
169             break;
170         /* fall through */
171     case WM_LBUTTONUP:
172         if (!(infoPtr->state & BUTTON_BTNPRESSED)) break;
173         infoPtr->state &= BUTTON_NSTATES;
174         if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) {
175             ReleaseCapture();
176             break;
177         }
178         SendMessageA( hWnd, BM_SETSTATE, FALSE, 0 );
179         ReleaseCapture();
180         GetClientRect( hWnd, &rect );
181         if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
182         {
183             switch(style)
184             {
185             case BS_AUTOCHECKBOX:
186                 SendMessageA( hWnd, BM_SETCHECK,
187                                 !(infoPtr->state & BUTTON_CHECKED), 0 );
188                 break;
189             case BS_AUTORADIOBUTTON:
190                 SendMessageA( hWnd, BM_SETCHECK, TRUE, 0 );
191                 break;
192             case BS_AUTO3STATE:
193                 SendMessageA( hWnd, BM_SETCHECK,
194                                 (infoPtr->state & BUTTON_3STATE) ? 0 :
195                                 ((infoPtr->state & 3) + 1), 0 );
196                 break;
197             }
198             SendMessageA( GetParent(hWnd), WM_COMMAND,
199                             MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
200         }
201         break;
202
203     case WM_CAPTURECHANGED:
204         if (infoPtr->state & BUTTON_BTNPRESSED) {
205             infoPtr->state &= BUTTON_NSTATES;
206             if (infoPtr->state & BUTTON_HIGHLIGHTED) 
207                 SendMessageA( hWnd, BM_SETSTATE, FALSE, 0 );
208         }
209         break;
210
211     case WM_MOUSEMOVE:
212         if (GetCapture() == hWnd)
213         {
214             GetClientRect( hWnd, &rect );
215             SendMessageA( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
216         }
217         break;
218
219     case WM_NCHITTEST:
220         if(style == BS_GROUPBOX) return HTTRANSPARENT;
221         return DefWindowProcA( hWnd, uMsg, wParam, lParam );
222
223     case WM_SETTEXT:
224         DEFWND_SetText( wndPtr, (LPCSTR)lParam );
225         if( wndPtr->dwStyle & WS_VISIBLE )
226             PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
227         return 0;
228
229     case WM_SETFONT:
230         infoPtr->hFont = (HFONT16)wParam;
231         if (lParam && (wndPtr->dwStyle & WS_VISIBLE)) 
232             PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
233         break;
234
235     case WM_GETFONT:
236         return infoPtr->hFont;
237
238     case WM_SETFOCUS:
239         if ((style == BS_RADIOBUTTON || style == BS_AUTORADIOBUTTON) && (GetCapture() != hWnd) &&
240             !(SendMessageA(hWnd, BM_GETCHECK, 0, 0) & BST_CHECKED))
241         {
242             /* The notification is sent when the button (BS_AUTORADIOBUTTON) 
243                is unckecked and the focus was not given by a mouse click. */
244             if (style == BS_AUTORADIOBUTTON)
245                     SendMessageA( hWnd, BM_SETCHECK, BUTTON_CHECKED, 0 );
246             SendMessageA( GetParent(hWnd), WM_COMMAND,
247                           MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
248         }
249         infoPtr->state |= BUTTON_HASFOCUS;
250         PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
251         break;
252
253     case WM_KILLFOCUS:
254         infoPtr->state &= ~BUTTON_HASFOCUS;
255         PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
256         InvalidateRect( hWnd, NULL, TRUE );
257         break;
258
259     case WM_SYSCOLORCHANGE:
260         InvalidateRect( hWnd, NULL, FALSE );
261         break;
262
263     case BM_SETSTYLE16:
264     case BM_SETSTYLE:
265         if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
266         wndPtr->dwStyle = (wndPtr->dwStyle & 0xfffffff0) 
267                            | (wParam & 0x0000000f);
268         style = wndPtr->dwStyle & 0x0000000f;
269         PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
270         break;
271
272     case BM_CLICK:
273         SendMessageA( hWnd, WM_LBUTTONDOWN, 0, 0 );
274         SendMessageA( hWnd, WM_LBUTTONUP, 0, 0 );
275         break;
276
277     case BM_SETIMAGE:
278         oldHbitmap = infoPtr->hImage;
279         if ((wndPtr->dwStyle & BS_BITMAP) || (wndPtr->dwStyle & BS_ICON))
280         {
281             infoPtr->hImage = (HANDLE) lParam;
282             InvalidateRect( hWnd, NULL, FALSE );
283         }
284         return oldHbitmap;
285
286     case BM_GETIMAGE:
287         if (wParam == IMAGE_BITMAP)
288             return (HBITMAP)infoPtr->hImage;
289         else if (wParam == IMAGE_ICON)
290             return (HICON)infoPtr->hImage;
291         else
292             return (HICON)0;
293
294     case BM_GETCHECK16:
295     case BM_GETCHECK:
296         return infoPtr->state & 3;
297
298     case BM_SETCHECK16:
299     case BM_SETCHECK:
300         if (wParam > maxCheckState[style]) wParam = maxCheckState[style];
301         if ((infoPtr->state & 3) != wParam)
302         {
303             if ((style == BS_RADIOBUTTON) || (style == BS_AUTORADIOBUTTON))
304             {
305                 if (wParam)
306                     wndPtr->dwStyle |= WS_TABSTOP;
307                 else
308                     wndPtr->dwStyle &= ~WS_TABSTOP;
309             }
310             infoPtr->state = (infoPtr->state & ~3) | wParam;
311             PAINT_BUTTON( wndPtr, style, ODA_SELECT );
312         }
313         if ((style == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED))
314             BUTTON_CheckAutoRadioButton( wndPtr );
315         break;
316
317     case BM_GETSTATE16:
318     case BM_GETSTATE:
319         return infoPtr->state;
320
321     case BM_SETSTATE16:
322     case BM_SETSTATE:
323         if (wParam)
324         {
325             if (infoPtr->state & BUTTON_HIGHLIGHTED) break;
326             infoPtr->state |= BUTTON_HIGHLIGHTED;
327         }
328         else
329         {
330             if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) break;
331             infoPtr->state &= ~BUTTON_HIGHLIGHTED;
332         }
333         PAINT_BUTTON( wndPtr, style, ODA_SELECT );
334         break;
335
336     default:
337         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
338     }
339     return 0;
340 }
341
342 /***********************************************************************
343  *           ButtonWndProc
344  * The button window procedure. This is just a wrapper which locks
345  * the passed HWND and calls the real window procedure (with a WND*
346  * pointer pointing to the locked windowstructure).
347  */
348 LRESULT WINAPI ButtonWndProc( HWND hWnd, UINT uMsg,
349                               WPARAM wParam, LPARAM lParam )
350 {
351     LRESULT res;
352     WND *wndPtr = WIN_FindWndPtr(hWnd);
353
354     res = ButtonWndProc_locked(wndPtr,uMsg,wParam,lParam);
355
356     WIN_ReleaseWndPtr(wndPtr);
357     return res;
358 }
359
360 /**********************************************************************
361  *       Push Button Functions
362  */
363 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action )
364 {
365     BUTTONINFO *infoPtr      = (BUTTONINFO *)wndPtr->wExtra;
366     BOOL        bHighLighted = (infoPtr->state & BUTTON_HIGHLIGHTED);
367
368     /* 
369      * Delegate this to the more generic pushbutton painting
370      * method.
371      */
372     BUTTON_DrawPushButton(wndPtr,
373                           hDC,
374                           action,
375                           bHighLighted);
376 }
377
378 /**********************************************************************
379  * This method will actually do the drawing of the pushbutton 
380  * depending on it's state and the pushedState parameter.
381  */
382 static void BUTTON_DrawPushButton(
383   WND* wndPtr,
384   HDC  hDC, 
385   WORD action, 
386   BOOL pushedState )
387 {
388     RECT rc, focus_rect;
389     HPEN hOldPen;
390     HBRUSH hOldBrush;
391     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
392     int xBorderOffset, yBorderOffset;
393     xBorderOffset = yBorderOffset = 0;
394
395     GetClientRect( wndPtr->hwndSelf, &rc );
396
397       /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
398     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
399     BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
400     hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
401     hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
402     SetBkMode(hDC, TRANSPARENT);
403
404     if ( TWEAK_WineLook == WIN31_LOOK)
405     {
406         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
407
408         SetPixel( hDC, rc.left, rc.top, GetSysColor(COLOR_WINDOW) );
409         SetPixel( hDC, rc.left, rc.bottom-1, GetSysColor(COLOR_WINDOW) );
410         SetPixel( hDC, rc.right-1, rc.top, GetSysColor(COLOR_WINDOW) );
411         SetPixel( hDC, rc.right-1, rc.bottom-1, GetSysColor(COLOR_WINDOW));
412         InflateRect( &rc, -1, -1 );
413     }
414     
415     if ((wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON)
416     {
417         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
418         InflateRect( &rc, -1, -1 );
419     }
420
421     if (TWEAK_WineLook == WIN31_LOOK)
422     {
423         if (pushedState)
424         {
425             /* draw button shadow: */
426             SelectObject(hDC, GetSysColorBrush(COLOR_BTNSHADOW));
427             PatBlt(hDC, rc.left, rc.top, 1, rc.bottom-rc.top, PATCOPY );
428             PatBlt(hDC, rc.left, rc.top, rc.right-rc.left, 1, PATCOPY );
429             rc.left += 2;  /* To position the text down and right */
430             rc.top  += 2;
431         } else {
432            rc.right++, rc.bottom++;
433            DrawEdge( hDC, &rc, EDGE_RAISED, BF_RECT );
434
435            /* To place de bitmap correctly */
436            xBorderOffset += GetSystemMetrics(SM_CXEDGE);
437            yBorderOffset += GetSystemMetrics(SM_CYEDGE);
438
439            rc.right--, rc.bottom--;
440         }
441     }
442     else
443     {
444         UINT uState = DFCS_BUTTONPUSH;
445
446         if (pushedState)
447         {
448             if ( (wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON )
449                 uState |= DFCS_FLAT;
450             else
451                 uState |= DFCS_PUSHED;
452         }
453
454         DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
455         InflateRect( &rc, -2, -2 );
456
457         focus_rect = rc;
458
459         if (pushedState)
460         {
461             rc.left += 2;  /* To position the text down and right */
462             rc.top  += 2;
463         }
464     }
465
466     /* draw button label, if any:
467      *
468      * In win9x we don't show text if there is a bitmap or icon.
469      * I don't know about win31 so I leave it as it was for win31.
470      * Dennis Björklund 12 Jul, 99
471      */
472     if ( wndPtr->text && wndPtr->text[0]
473          && (TWEAK_WineLook == WIN31_LOOK || !(wndPtr->dwStyle & (BS_ICON|BS_BITMAP))) )
474     {
475         LOGBRUSH lb;
476         GetObjectA( GetSysColorBrush(COLOR_BTNFACE), sizeof(lb), &lb );
477         if (wndPtr->dwStyle & WS_DISABLED &&
478             GetSysColor(COLOR_GRAYTEXT)==lb.lbColor)
479             /* don't write gray text on gray background */
480             PaintGrayOnGray( hDC,infoPtr->hFont,&rc,wndPtr->text,
481                                DT_CENTER | DT_VCENTER );
482         else
483         {
484             SetTextColor( hDC, (wndPtr->dwStyle & WS_DISABLED) ?
485                                  GetSysColor(COLOR_GRAYTEXT) :
486                                  GetSysColor(COLOR_BTNTEXT) );
487             DrawTextA( hDC, wndPtr->text, -1, &rc,
488                          DT_SINGLELINE | DT_CENTER | DT_VCENTER );
489             /* do we have the focus?
490              * Win9x draws focus last with a size prop. to the button
491              */
492             if (TWEAK_WineLook == WIN31_LOOK
493                 && infoPtr->state & BUTTON_HASFOCUS)
494             {
495                 RECT r = { 0, 0, 0, 0 };
496                 INT xdelta, ydelta;
497
498                 DrawTextA( hDC, wndPtr->text, -1, &r,
499                              DT_SINGLELINE | DT_CALCRECT );
500                 xdelta = ((rc.right - rc.left) - (r.right - r.left) - 1) / 2;
501                 ydelta = ((rc.bottom - rc.top) - (r.bottom - r.top) - 1) / 2;
502                 if (xdelta < 0) xdelta = 0;
503                 if (ydelta < 0) ydelta = 0;
504                 InflateRect( &rc, -xdelta, -ydelta );
505                 DrawFocusRect( hDC, &rc );
506             }
507         }   
508     }
509     if ( ((wndPtr->dwStyle & BS_ICON) || (wndPtr->dwStyle & BS_BITMAP) ) &&
510          (infoPtr->hImage != 0) )
511     {
512         int yOffset, xOffset;
513         int imageWidth, imageHeight;
514
515         /*
516          * We extract the size of the image from the handle.
517          */
518         if (wndPtr->dwStyle & BS_ICON)
519         {
520             ICONINFO iconInfo;
521             BITMAP   bm;
522
523             GetIconInfo((HICON)infoPtr->hImage, &iconInfo);
524             GetObjectA (iconInfo.hbmColor, sizeof(BITMAP), &bm);
525             
526             imageWidth  = bm.bmWidth;
527             imageHeight = bm.bmHeight;
528
529             DeleteObject(iconInfo.hbmColor);
530             DeleteObject(iconInfo.hbmMask);
531
532         }
533         else
534         {
535             BITMAP   bm;
536
537             GetObjectA (infoPtr->hImage, sizeof(BITMAP), &bm);
538         
539             imageWidth  = bm.bmWidth;
540             imageHeight = bm.bmHeight;
541         }
542
543         /* Center the bitmap */
544         xOffset = (((rc.right - rc.left) - 2*xBorderOffset) - imageWidth ) / 2;
545         yOffset = (((rc.bottom - rc.top) - 2*yBorderOffset) - imageHeight) / 2;
546
547         /* If the image is too big for the button then create a region*/
548         if(xOffset < 0 || yOffset < 0)
549         {
550             HRGN hBitmapRgn = 0;
551             hBitmapRgn = CreateRectRgn(
552                 rc.left + xBorderOffset, rc.top +yBorderOffset, 
553                 rc.right - xBorderOffset, rc.bottom - yBorderOffset);
554             SelectClipRgn(hDC, hBitmapRgn);
555             DeleteObject(hBitmapRgn);
556         }
557
558         /* Let minimum 1 space from border */
559         xOffset++, yOffset++;
560
561         /*
562          * Draw the image now.
563          */
564         if (wndPtr->dwStyle & BS_ICON)
565         {
566             DrawIcon(hDC,
567                 rc.left + xOffset, rc.top + yOffset,
568                      (HICON)infoPtr->hImage);
569         }
570         else
571         {
572             HDC hdcMem;
573
574             hdcMem = CreateCompatibleDC (hDC);
575             SelectObject (hdcMem, (HBITMAP)infoPtr->hImage);
576             BitBlt(hDC, 
577                    rc.left + xOffset, 
578                    rc.top + yOffset, 
579                    imageWidth, imageHeight,
580                    hdcMem, 0, 0, SRCCOPY);
581             
582             DeleteDC (hdcMem);
583         }
584
585         if(xOffset < 0 || yOffset < 0)
586         {
587             SelectClipRgn(hDC, 0);
588         }
589     }
590
591     if (TWEAK_WineLook != WIN31_LOOK
592         && infoPtr->state & BUTTON_HASFOCUS)
593     {
594         InflateRect( &focus_rect, -1, -1 );
595         DrawFocusRect( hDC, &focus_rect );
596     }
597
598     
599     SelectObject( hDC, hOldPen );
600     SelectObject( hDC, hOldBrush );
601 }
602
603
604 /**********************************************************************
605  *   PB_Paint & CB_Paint sub function                        [internal]
606  *   Paint text using a raster brush to avoid gray text on gray 
607  *   background. 'format' can be a combination of DT_CENTER and 
608  *   DT_VCENTER to use this function in both PB_PAINT and 
609  *   CB_PAINT.   - Dirk Thierbach
610  *
611  *   FIXME: This and TEXT_GrayString should be eventually combined,
612  *   so calling one common function from PB_Paint, CB_Paint and
613  *   TEXT_GrayString will be enough. Also note that this
614  *   function ignores the CACHE_GetPattern funcs.
615  */
616
617 void PaintGrayOnGray(HDC hDC,HFONT hFont,RECT *rc,char *text,
618                         UINT format)
619 {
620 /*  This is the standard gray on gray pattern:
621     static const WORD Pattern[] = {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55}; 
622 */
623 /*  This pattern gives better readability with X Fonts.
624     FIXME: Maybe the user should be allowed to decide which he wants. */
625     static const WORD Pattern[] = {0x55,0xFF,0xAA,0xFF,0x55,0xFF,0xAA,0xFF}; 
626
627     HBITMAP hbm  = CreateBitmap( 8, 8, 1, 1, Pattern );
628     HDC hdcMem = CreateCompatibleDC(hDC);
629     HBITMAP hbmMem;
630     HBRUSH hBr;
631     RECT rect,rc2;
632
633     rect=*rc;
634     DrawTextA( hDC, text, -1, &rect, DT_SINGLELINE | DT_CALCRECT);
635     /* now text width and height are in rect.right and rect.bottom */
636     rc2=rect;
637     rect.left = rect.top = 0; /* drawing pos in hdcMem */
638     if (format & DT_CENTER) rect.left=(rc->right-rect.right)/2;
639     if (format & DT_VCENTER) rect.top=(rc->bottom-rect.bottom)/2;
640     hbmMem = CreateCompatibleBitmap( hDC,rect.right,rect.bottom );
641     SelectObject( hdcMem, hbmMem);
642     PatBlt( hdcMem,0,0,rect.right,rect.bottom,WHITENESS);
643       /* will be overwritten by DrawText, but just in case */
644     if (hFont) SelectObject( hdcMem, hFont);
645     DrawTextA( hdcMem, text, -1, &rc2, DT_SINGLELINE);  
646       /* After draw: foreground = 0 bits, background = 1 bits */
647     hBr = SelectObject( hdcMem, CreatePatternBrush(hbm) );
648     DeleteObject( hbm );
649     PatBlt( hdcMem,0,0,rect.right,rect.bottom,0xAF0229); 
650       /* only keep the foreground bits where pattern is 1 */
651     DeleteObject( SelectObject( hdcMem,hBr) );
652     BitBlt(hDC,rect.left,rect.top,rect.right,rect.bottom,hdcMem,0,0,SRCAND);
653       /* keep the background of the dest */
654     DeleteDC( hdcMem);
655     DeleteObject( hbmMem );
656 }
657
658
659 /**********************************************************************
660  *       Check Box & Radio Button Functions
661  */
662
663 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action )
664 {
665     RECT rbox, rtext, client;
666     HBRUSH hBrush;
667     int textlen, delta;
668     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
669
670     /* 
671      * if the button has a bitmap/icon, draw a normal pushbutton
672      * instead of a radion button.
673      */
674     if (infoPtr->hImage != 0)
675     {
676         BOOL bHighLighted = ((infoPtr->state & BUTTON_HIGHLIGHTED) ||
677                              (infoPtr->state & BUTTON_CHECKED));
678
679         BUTTON_DrawPushButton(wndPtr,
680                               hDC,
681                               action,
682                               bHighLighted);
683         return;
684     }
685
686     textlen = 0;
687     GetClientRect(wndPtr->hwndSelf, &client);
688     rbox = rtext = client;
689
690     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
691
692     /* Something is still not right, checkboxes (and edit controls)
693      * in wsping32 have white backgrounds instead of dark grey.
694      * BUTTON_SEND_CTLCOLOR() is even worse since it returns 0 in this
695      * particular case and the background is not painted at all.
696      */
697
698     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
699
700     if (wndPtr->dwStyle & BS_LEFTTEXT) 
701     {
702         /* magic +4 is what CTL3D expects */
703
704         rtext.right -= checkBoxWidth + 4;
705         rbox.left = rbox.right - checkBoxWidth;
706     }
707     else 
708     {
709         rtext.left += checkBoxWidth + 4;
710         rbox.right = checkBoxWidth;
711     }
712
713       /* Draw the check-box bitmap */
714
715     if (wndPtr->text) textlen = strlen( wndPtr->text );
716     if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
717     { 
718         if( TWEAK_WineLook == WIN31_LOOK )
719         {
720         HDC hMemDC = CreateCompatibleDC( hDC );
721         int x = 0, y = 0;
722         delta = (rbox.bottom - rbox.top - checkBoxHeight) / 2;
723
724         /* Check in case the client area is smaller than the checkbox bitmap */
725         if (delta < 0) delta = 0;
726
727         if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
728         else FillRect( hDC, &client, hBrush );
729
730         if (infoPtr->state & BUTTON_HIGHLIGHTED) x += 2 * checkBoxWidth;
731         if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) x += checkBoxWidth;
732         if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
733             ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) y += checkBoxHeight;
734         else if (infoPtr->state & BUTTON_3STATE) y += 2 * checkBoxHeight;
735
736         /* The bitmap for the radio button is not aligned with the
737          * left of the window, it is 1 pixel off. */
738         if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
739             ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
740           rbox.left += 1;
741
742         SelectObject( hMemDC, hbitmapCheckBoxes );
743         BitBlt( hDC, rbox.left, rbox.top + delta, checkBoxWidth,
744                   checkBoxHeight, hMemDC, x, y, SRCCOPY );
745         DeleteDC( hMemDC );
746         }
747         else
748         {
749             UINT state;
750
751             if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
752                 ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) state = DFCS_BUTTONRADIO;
753             else if (infoPtr->state & BUTTON_3STATE) state = DFCS_BUTTON3STATE;
754             else state = DFCS_BUTTONCHECK;
755
756             if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) state |= DFCS_CHECKED;
757             
758             if (infoPtr->state & BUTTON_HIGHLIGHTED) state |= DFCS_PUSHED;
759
760             if (wndPtr->dwStyle & WS_DISABLED) state |= DFCS_INACTIVE;
761
762             DrawFrameControl( hDC, &rbox, DFC_BUTTON, state );
763         }
764
765
766         if( textlen && action != ODA_SELECT )
767         {
768           if (wndPtr->dwStyle & WS_DISABLED &&
769               GetSysColor(COLOR_GRAYTEXT)==GetBkColor(hDC)) {
770             /* don't write gray text on gray background */
771             PaintGrayOnGray( hDC, infoPtr->hFont, &rtext, wndPtr->text,
772                              DT_VCENTER);
773           } else {
774             if (wndPtr->dwStyle & WS_DISABLED)
775                 SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
776             DrawTextA( hDC, wndPtr->text, textlen, &rtext,
777                          DT_SINGLELINE | DT_VCENTER );
778           }
779         }
780     }
781
782     if ((action == ODA_FOCUS) ||
783         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
784     {
785         /* again, this is what CTL3D expects */
786
787         SetRectEmpty(&rbox);
788         if( textlen )
789             DrawTextA( hDC, wndPtr->text, textlen, &rbox,
790                          DT_SINGLELINE | DT_CALCRECT );
791         textlen = rbox.bottom - rbox.top;
792         delta = ((rtext.bottom - rtext.top) - textlen)/2;
793         rbox.bottom = (rbox.top = rtext.top + delta - 1) + textlen + 2;
794         textlen = rbox.right - rbox.left;
795         rbox.right = (rbox.left += --rtext.left) + textlen + 2;
796         IntersectRect(&rbox, &rbox, &rtext);
797         DrawFocusRect( hDC, &rbox );
798     }
799 }
800
801
802 /**********************************************************************
803  *       BUTTON_CheckAutoRadioButton
804  *
805  * wndPtr is checked, uncheck every other auto radio button in group
806  */
807 static void BUTTON_CheckAutoRadioButton( WND *wndPtr )
808 {
809     HWND parent, sibling, start;
810     if (!(wndPtr->dwStyle & WS_CHILD)) return;
811     parent = wndPtr->parent->hwndSelf;
812     /* assure that starting control is not disabled or invisible */
813     start = sibling = GetNextDlgGroupItem( parent, wndPtr->hwndSelf, TRUE );
814     do
815     {
816         WND *tmpWnd;
817         if (!sibling) break;
818         tmpWnd = WIN_FindWndPtr(sibling);
819         if ((wndPtr->hwndSelf != sibling) &&
820             ((tmpWnd->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
821             SendMessageA( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
822         sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
823         WIN_ReleaseWndPtr(tmpWnd);
824     } while (sibling != start);
825 }
826
827
828 /**********************************************************************
829  *       Group Box Functions
830  */
831
832 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action )
833 {
834     RECT rc, rcFrame;
835     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
836
837     if (action != ODA_DRAWENTIRE) return;
838
839     BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
840
841     GetClientRect( wndPtr->hwndSelf, &rc);
842     if (TWEAK_WineLook == WIN31_LOOK) {
843         HPEN hPrevPen = SelectObject( hDC,
844                                           GetSysColorPen(COLOR_WINDOWFRAME));
845         HBRUSH hPrevBrush = SelectObject( hDC,
846                                               GetStockObject(NULL_BRUSH) );
847
848         Rectangle( hDC, rc.left, rc.top + 2, rc.right - 1, rc.bottom - 1 );
849         SelectObject( hDC, hPrevBrush );
850         SelectObject( hDC, hPrevPen );
851     } else {
852         TEXTMETRICA tm;
853         rcFrame = rc;
854
855         if (infoPtr->hFont)
856             SelectObject (hDC, infoPtr->hFont);
857         GetTextMetricsA (hDC, &tm);
858         rcFrame.top += (tm.tmHeight / 2) - 1;
859         DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT);
860     }
861
862     if (wndPtr->text)
863     {
864         if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
865         if (wndPtr->dwStyle & WS_DISABLED)
866             SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
867         rc.left += 10;
868         DrawTextA( hDC, wndPtr->text, -1, &rc, DT_SINGLELINE | DT_NOCLIP );
869     }
870 }
871
872
873 /**********************************************************************
874  *       User Button Functions
875  */
876
877 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action )
878 {
879     RECT rc;
880     HBRUSH hBrush;
881     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
882
883     if (action == ODA_SELECT) return;
884
885     GetClientRect( wndPtr->hwndSelf, &rc);
886
887     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
888     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
889
890     FillRect( hDC, &rc, hBrush );
891     if ((action == ODA_FOCUS) ||
892         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
893         DrawFocusRect( hDC, &rc );
894 }
895
896
897 /**********************************************************************
898  *       Ownerdrawn Button Functions
899  */
900
901 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action )
902 {
903     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
904     DRAWITEMSTRUCT dis;
905
906     dis.CtlType    = ODT_BUTTON;
907     dis.CtlID      = wndPtr->wIDmenu;
908     dis.itemID     = 0;
909     dis.itemAction = action;
910     dis.itemState  = ((infoPtr->state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
911                      ((infoPtr->state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
912                      ((wndPtr->dwStyle & WS_DISABLED) ? ODS_DISABLED : 0);
913     dis.hwndItem   = wndPtr->hwndSelf;
914     dis.hDC        = hDC;
915     dis.itemData   = 0;
916     GetClientRect( wndPtr->hwndSelf, &dis.rcItem );
917
918     SetBkColor( hDC, GetSysColor( COLOR_BTNFACE ) );
919
920     SendMessageA( GetParent(wndPtr->hwndSelf), WM_DRAWITEM,
921                     wndPtr->wIDmenu, (LPARAM)&dis );
922 }
923