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