Added a first-cut version of MapVirtualKeyExW() that has the same
[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 <stdlib.h>     /* for abs() */
10 #include "win.h"
11 #include "button.h"
12 #include "winbase.h"
13 #include "windef.h"
14 #include "wingdi.h"
15 #include "wine/winuser16.h"
16 #include "tweak.h"
17
18 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action );
19 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action );
20 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action );
21 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action );
22 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action );
23 static void BUTTON_CheckAutoRadioButton( WND *wndPtr );
24 static void BUTTON_DrawPushButton( WND *wndPtr, HDC hDC, WORD action, BOOL pushedState);
25
26 #define MAX_BTN_TYPE  12
27
28 static const WORD maxCheckState[MAX_BTN_TYPE] =
29 {
30     BUTTON_UNCHECKED,   /* BS_PUSHBUTTON */
31     BUTTON_UNCHECKED,   /* BS_DEFPUSHBUTTON */
32     BUTTON_CHECKED,     /* BS_CHECKBOX */
33     BUTTON_CHECKED,     /* BS_AUTOCHECKBOX */
34     BUTTON_CHECKED,     /* BS_RADIOBUTTON */
35     BUTTON_3STATE,      /* BS_3STATE */
36     BUTTON_3STATE,      /* BS_AUTO3STATE */
37     BUTTON_UNCHECKED,   /* BS_GROUPBOX */
38     BUTTON_UNCHECKED,   /* BS_USERBUTTON */
39     BUTTON_CHECKED,     /* BS_AUTORADIOBUTTON */
40     BUTTON_UNCHECKED,   /* Not defined */
41     BUTTON_UNCHECKED    /* BS_OWNERDRAW */
42 };
43
44 typedef void (*pfPaint)( WND *wndPtr, HDC hdc, WORD action );
45
46 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
47 {
48     PB_Paint,    /* BS_PUSHBUTTON */
49     PB_Paint,    /* BS_DEFPUSHBUTTON */
50     CB_Paint,    /* BS_CHECKBOX */
51     CB_Paint,    /* BS_AUTOCHECKBOX */
52     CB_Paint,    /* BS_RADIOBUTTON */
53     CB_Paint,    /* BS_3STATE */
54     CB_Paint,    /* BS_AUTO3STATE */
55     GB_Paint,    /* BS_GROUPBOX */
56     UB_Paint,    /* BS_USERBUTTON */
57     CB_Paint,    /* BS_AUTORADIOBUTTON */
58     NULL,        /* Not defined */
59     OB_Paint     /* BS_OWNERDRAW */
60 };
61
62 #define PAINT_BUTTON(wndPtr,style,action) \
63      if (btnPaintFunc[style]) { \
64          HDC hdc = GetDC( (wndPtr)->hwndSelf ); \
65          (btnPaintFunc[style])(wndPtr,hdc,action); \
66          ReleaseDC( (wndPtr)->hwndSelf, hdc ); }
67
68 #define BUTTON_SEND_CTLCOLOR(wndPtr,hdc) \
69     SendMessageW( GetParent((wndPtr)->hwndSelf), WM_CTLCOLORBTN, \
70                     (hdc), (wndPtr)->hwndSelf )
71
72 static HBITMAP hbitmapCheckBoxes = 0;
73 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
74
75
76 /***********************************************************************
77  *           ButtonWndProc_locked
78  * 
79  * Called with window lock held.
80  */
81 static inline LRESULT WINAPI ButtonWndProc_locked(WND* wndPtr, UINT uMsg,
82                                            WPARAM wParam, LPARAM lParam )
83 {
84     RECT rect;
85     HWND        hWnd = wndPtr->hwndSelf;
86     POINT pt;
87     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
88     LONG style = wndPtr->dwStyle & 0x0f;
89     HANDLE oldHbitmap;
90
91     pt.x = LOWORD(lParam);
92     pt.y = HIWORD(lParam);
93
94     switch (uMsg)
95     {
96     case WM_GETDLGCODE:
97         switch(style)
98         {
99         case BS_PUSHBUTTON:      return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
100         case BS_DEFPUSHBUTTON:   return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
101         case BS_RADIOBUTTON:
102         case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
103         default:                 return DLGC_BUTTON;
104         }
105
106     case WM_ENABLE:
107         PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
108         break;
109
110     case WM_CREATE:
111         if (!hbitmapCheckBoxes)
112         {
113             BITMAP bmp;
114             hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
115             GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
116             checkBoxWidth  = bmp.bmWidth / 4;
117             checkBoxHeight = bmp.bmHeight / 3;
118         }
119         if (style < 0L || style >= MAX_BTN_TYPE)
120             return -1; /* abort */
121         infoPtr->state = BUTTON_UNCHECKED;
122         infoPtr->hFont = 0;
123         infoPtr->hImage = 0;
124         return 0;
125
126     case WM_ERASEBKGND:
127         return 1;
128
129     case WM_PAINT:
130         if (btnPaintFunc[style])
131         {
132             PAINTSTRUCT ps;
133             HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
134             int nOldMode = SetBkMode( hdc, OPAQUE );
135             (btnPaintFunc[style])( wndPtr, hdc, ODA_DRAWENTIRE );
136             SetBkMode(hdc, nOldMode); /*  reset painting mode */
137             if( !wParam ) EndPaint( hWnd, &ps );
138         }
139         break;
140
141     case WM_KEYDOWN:
142         if (wParam == VK_SPACE)
143         {
144             SendMessageW( 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             SendMessageW( 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         SendMessageW( 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         SendMessageW( 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                 SendMessageW( hWnd, BM_SETCHECK,
186                                 !(infoPtr->state & BUTTON_CHECKED), 0 );
187                 break;
188             case BS_AUTORADIOBUTTON:
189                 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
190                 break;
191             case BS_AUTO3STATE:
192                 SendMessageW( hWnd, BM_SETCHECK,
193                                 (infoPtr->state & BUTTON_3STATE) ? 0 :
194                                 ((infoPtr->state & 3) + 1), 0 );
195                 break;
196             }
197             SendMessageW( 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                 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
207         }
208         break;
209
210     case WM_MOUSEMOVE:
211         if (GetCapture() == hWnd)
212         {
213             GetClientRect( hWnd, &rect );
214             SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
215         }
216         break;
217
218     case WM_NCHITTEST:
219         if(style == BS_GROUPBOX) return HTTRANSPARENT;
220         return DefWindowProcW( hWnd, uMsg, wParam, lParam );
221
222     case WM_SETTEXT:
223         DEFWND_SetTextW( wndPtr, (LPCWSTR)lParam );
224         if( wndPtr->dwStyle & WS_VISIBLE )
225             PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
226         return 1; /* success. FIXME: check text length */
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_RADIOBUTTON || style == BS_AUTORADIOBUTTON) && (GetCapture() != hWnd) &&
239             !(SendMessageW(hWnd, BM_GETCHECK, 0, 0) & BST_CHECKED))
240         {
241             /* The notification is sent when the button (BS_AUTORADIOBUTTON) 
242                is unchecked and the focus was not given by a mouse click. */
243             if (style == BS_AUTORADIOBUTTON)
244                     SendMessageW( hWnd, BM_SETCHECK, BUTTON_CHECKED, 0 );
245                     SendMessageW( GetParent(hWnd), WM_COMMAND,
246                           MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
247         }
248         infoPtr->state |= BUTTON_HASFOCUS;
249         PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
250         break;
251
252     case WM_KILLFOCUS:
253         infoPtr->state &= ~BUTTON_HASFOCUS;
254         PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
255         InvalidateRect( hWnd, NULL, TRUE );
256         break;
257
258     case WM_SYSCOLORCHANGE:
259         InvalidateRect( hWnd, NULL, FALSE );
260         break;
261
262     case BM_SETSTYLE16:
263     case BM_SETSTYLE:
264         if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
265         wndPtr->dwStyle = (wndPtr->dwStyle & 0xfffffff0) 
266                            | (wParam & 0x0000000f);
267         style = wndPtr->dwStyle & 0x0000000f;
268
269         /* Only redraw if lParam flag is set.*/
270         if (lParam)
271            PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
272
273         break;
274
275     case BM_CLICK:
276         SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
277         SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
278         break;
279
280     case BM_SETIMAGE:
281         /* Check that image format confirm button style */
282         if ((wndPtr->dwStyle & (BS_BITMAP|BS_ICON)) == BS_BITMAP)
283         {
284             if (wParam != (WPARAM) IMAGE_BITMAP)
285                 return (HICON)0;
286         }
287         else if ((wndPtr->dwStyle & (BS_BITMAP|BS_ICON)) == BS_ICON)
288         {
289             if (wParam != (WPARAM) IMAGE_ICON)
290                 return (HICON)0;
291         } else
292             return (HICON)0;
293
294         oldHbitmap = infoPtr->hImage;
295         infoPtr->hImage = (HANDLE) lParam;
296         InvalidateRect( hWnd, NULL, FALSE );
297         return oldHbitmap;
298
299     case BM_GETIMAGE:
300         if ((wndPtr->dwStyle & (BS_BITMAP|BS_ICON)) == BS_BITMAP)
301         {
302             if (wParam != (WPARAM) IMAGE_BITMAP)
303                 return (HICON)0;
304         }
305         else if ((wndPtr->dwStyle & (BS_BITMAP|BS_ICON)) == BS_ICON)
306         {
307             if (wParam != (WPARAM) IMAGE_ICON)
308                 return (HICON)0;
309         } else
310             return (HICON)0;
311         return infoPtr->hImage;
312
313     case BM_GETCHECK16:
314     case BM_GETCHECK:
315         return infoPtr->state & 3;
316
317     case BM_SETCHECK16:
318     case BM_SETCHECK:
319         if (wParam > maxCheckState[style]) wParam = maxCheckState[style];
320         if ((infoPtr->state & 3) != wParam)
321         {
322             if ((style == BS_RADIOBUTTON) || (style == BS_AUTORADIOBUTTON))
323             {
324                 if (wParam)
325                     wndPtr->dwStyle |= WS_TABSTOP;
326                 else
327                     wndPtr->dwStyle &= ~WS_TABSTOP;
328             }
329             infoPtr->state = (infoPtr->state & ~3) | wParam;
330             PAINT_BUTTON( wndPtr, style, ODA_SELECT );
331         }
332         if ((style == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED))
333             BUTTON_CheckAutoRadioButton( wndPtr );
334         break;
335
336     case BM_GETSTATE16:
337     case BM_GETSTATE:
338         return infoPtr->state;
339
340     case BM_SETSTATE16:
341     case BM_SETSTATE:
342         if (wParam)
343         {
344             if (infoPtr->state & BUTTON_HIGHLIGHTED) break;
345             infoPtr->state |= BUTTON_HIGHLIGHTED;
346         }
347         else
348         {
349             if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) break;
350             infoPtr->state &= ~BUTTON_HIGHLIGHTED;
351         }
352         PAINT_BUTTON( wndPtr, style, ODA_SELECT );
353         break;
354
355     default:
356         return DefWindowProcW(hWnd, uMsg, wParam, lParam);
357     }
358     return 0;
359 }
360
361 /***********************************************************************
362  *           ButtonWndProc
363  * The button window procedure. This is just a wrapper which locks
364  * the passed HWND and calls the real window procedure (with a WND*
365  * pointer pointing to the locked windowstructure).
366  */
367 LRESULT WINAPI ButtonWndProc( HWND hWnd, UINT uMsg,
368                               WPARAM wParam, LPARAM lParam )
369 {
370     LRESULT res;
371     WND *wndPtr = WIN_FindWndPtr(hWnd);
372
373     res = ButtonWndProc_locked(wndPtr,uMsg,wParam,lParam);
374
375     WIN_ReleaseWndPtr(wndPtr);
376     return res;
377 }
378
379 /**********************************************************************
380  *       Push Button Functions
381  */
382 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action )
383 {
384     BUTTONINFO *infoPtr      = (BUTTONINFO *)wndPtr->wExtra;
385     BOOL        bHighLighted = (infoPtr->state & BUTTON_HIGHLIGHTED);
386
387     /* 
388      * Delegate this to the more generic pushbutton painting
389      * method.
390      */
391     BUTTON_DrawPushButton(wndPtr,
392                           hDC,
393                           action,
394                           bHighLighted);
395 }
396
397 /**********************************************************************
398  * Convert button styles to flags used by DrawText.
399  * TODO: handle WS_EX_RIGHT extended style.
400  */
401 static UINT BUTTON_BStoDT(DWORD style)
402 {
403    UINT dtStyle = DT_NOCLIP;  /* We use SelectClipRgn to limit output */
404
405    /* "Convert" pushlike buttons to pushbuttons */
406    if (style & BS_PUSHLIKE)
407       style &= ~0x0F;
408
409    if (!(style & BS_MULTILINE))
410       dtStyle |= DT_SINGLELINE;
411    else
412       dtStyle |= DT_WORDBREAK;
413
414    switch (style & BS_CENTER)
415    {
416       case BS_LEFT:   /* DT_LEFT is 0 */    break;
417       case BS_RIGHT:  dtStyle |= DT_RIGHT;  break;
418       case BS_CENTER: dtStyle |= DT_CENTER; break;
419       default:
420          /* Pushbutton's text is centered by default */
421          if ((style & 0x0F) <= BS_DEFPUSHBUTTON)
422             dtStyle |= DT_CENTER;
423          /* all other flavours have left aligned text */
424    }
425
426    /* DrawText ignores vertical alignment for multiline text,
427     * but we use these flags to align label manualy.
428     */
429    if ((style & 0x0F) != BS_GROUPBOX)
430    {
431       switch (style & BS_VCENTER)
432       {
433          case BS_TOP:     /* DT_TOP is 0 */      break;
434          case BS_BOTTOM:  dtStyle |= DT_BOTTOM;  break;
435          case BS_VCENTER: /* fall through */
436          default:         dtStyle |= DT_VCENTER; break;
437       }
438    }
439    else
440       /* GroupBox's text is always single line and is top aligned. */
441       dtStyle |= DT_SINGLELINE;
442
443    return dtStyle;
444 }
445
446 /**********************************************************************
447  *       BUTTON_CalcLabelRect
448  *
449  *   Calculates label's rectangle depending on button style.
450  *
451  * Returns flags to be passed to DrawText.
452  * Calculated rectangle doesn't take into account button state
453  * (pushed, etc.). If there is nothing to draw (no text/image) output
454  * rectangle is empty, and return value is (UINT)-1.
455  */
456 static UINT BUTTON_CalcLabelRect(WND *wndPtr, HDC hdc, RECT *rc)
457 {
458    BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
459    ICONINFO    iconInfo;
460    BITMAP      bm;
461    UINT        dtStyle = BUTTON_BStoDT(wndPtr->dwStyle);
462    RECT        r = *rc;
463    INT         n;
464
465    /* Calculate label rectangle according to label type */
466    switch (wndPtr->dwStyle & (BS_ICON|BS_BITMAP))
467    {
468       case BS_TEXT:
469          if (wndPtr->text && wndPtr->text[0])
470             DrawTextW(hdc, wndPtr->text, -1, &r, dtStyle | DT_CALCRECT);
471          else
472             goto empty_rect;
473          break;
474
475       case BS_ICON:
476          if (!GetIconInfo((HICON)infoPtr->hImage, &iconInfo))
477             goto empty_rect;
478
479          GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
480
481          r.right  = r.left + bm.bmWidth;
482          r.bottom = r.top  + bm.bmHeight;
483
484          DeleteObject(iconInfo.hbmColor);
485          DeleteObject(iconInfo.hbmMask);
486          break;
487
488       case BS_BITMAP:
489          if (!GetObjectW (infoPtr->hImage, sizeof(BITMAP), &bm))
490             goto empty_rect;
491
492          r.right  = r.left + bm.bmWidth;
493          r.bottom = r.top  + bm.bmHeight;
494          break;
495
496       default:
497       empty_rect:   
498          r.right = r.left;
499          r.bottom = r.top;
500          return (UINT)(LONG)-1;
501    }
502
503    /* Position label inside bounding rectangle according to
504     * alignment flags. (calculated rect is always left-top aligned).
505     * If label is aligned to any side - shift label in opposite
506     * direction to leave extra space for focus rectangle.
507     */
508    switch (dtStyle & (DT_CENTER|DT_RIGHT))
509    {
510       case DT_LEFT:    r.left++;  r.right++;  break;
511       case DT_CENTER:  n = r.right - r.left;
512                        r.left   = rc->left + ((rc->right - rc->left) - n) / 2;
513                        r.right  = r.left + n; break;
514       case DT_RIGHT:   n = r.right - r.left;
515                        r.right  = rc->right - 1;
516                        r.left   = r.right - n;
517                        break;
518    }
519
520    switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
521    {
522       case DT_TOP:     r.top++;  r.bottom++;  break;
523       case DT_VCENTER: n = r.bottom - r.top;
524                        r.top    = rc->top + ((rc->bottom - rc->top) - n) / 2;
525                        r.bottom = r.top + n;  break;
526       case DT_BOTTOM:  n = r.bottom - r.top;
527                        r.bottom = rc->bottom - 1;
528                        r.top    = r.bottom - n;
529                        break;
530    }
531
532    *rc = r;
533    return dtStyle;
534 }
535
536
537 /**********************************************************************
538  *       BUTTON_DrawTextCallback
539  *
540  *   Callback function used by DrawStateW function.
541  */
542 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
543 {
544    RECT rc;
545    rc.left = 0;
546    rc.top = 0;
547    rc.right = cx;
548    rc.bottom = cy;
549
550    DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
551    return TRUE;
552 }
553
554
555 /**********************************************************************
556  *       BUTTON_DrawLabel
557  *
558  *   Common function for drawing button label.
559  */
560 static void BUTTON_DrawLabel(WND *wndPtr, HDC hdc, UINT dtFlags, RECT *rc)
561 {
562    BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
563    DRAWSTATEPROC lpOutputProc = NULL;
564    LPARAM lp;
565    WPARAM wp = 0;
566    HBRUSH hbr = 0;
567    UINT flags = IsWindowEnabled(wndPtr->hwndSelf) ? DSS_NORMAL : DSS_DISABLED;
568
569    /* Fixme: To draw disabled label in Win31 look-and-feel, we probably
570     * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
571     * I don't have Win31 on hand to verify that, so I leave it as is.
572     */
573
574    if ((wndPtr->dwStyle & BS_PUSHLIKE) && (infoPtr->state & BUTTON_3STATE))
575    {
576       hbr = GetSysColorBrush(COLOR_GRAYTEXT);
577       flags |= DSS_MONO;
578    }
579
580    switch (wndPtr->dwStyle & (BS_ICON|BS_BITMAP))
581    {
582       case BS_TEXT:
583          /* DST_COMPLEX -- is 0 */
584          lpOutputProc = BUTTON_DrawTextCallback;
585          lp = (LPARAM)wndPtr->text;
586          wp = (WPARAM)dtFlags;
587          break;
588
589       case BS_ICON:
590          flags |= DST_ICON;
591          lp = (LPARAM)infoPtr->hImage;
592          break;
593
594       case BS_BITMAP:
595          flags |= DST_BITMAP;
596          lp = (LPARAM)infoPtr->hImage;
597          break;
598
599       default:
600          return;
601    }
602
603    DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
604               rc->right - rc->left, rc->bottom - rc->top, flags);
605 }
606
607 /**********************************************************************
608  * This method will actually do the drawing of the pushbutton 
609  * depending on it's state and the pushedState parameter.
610  */
611 static void BUTTON_DrawPushButton(
612   WND* wndPtr,
613   HDC  hDC, 
614   WORD action, 
615   BOOL pushedState )
616 {
617     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
618     RECT     rc, focus_rect, r;
619     UINT     dtFlags;
620     HRGN     hRgn;
621     HPEN     hOldPen;
622     HBRUSH   hOldBrush;
623     INT      oldBkMode;
624     COLORREF oldTxtColor;
625
626     GetClientRect( wndPtr->hwndSelf, &rc );
627
628     /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
629     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
630     BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
631     hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
632     hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
633     oldBkMode = SetBkMode(hDC, TRANSPARENT);
634
635     if ( TWEAK_WineLook == WIN31_LOOK)
636     {
637         COLORREF clr_wnd = GetSysColor(COLOR_WINDOW);
638         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
639
640         SetPixel( hDC, rc.left, rc.top, clr_wnd);
641         SetPixel( hDC, rc.left, rc.bottom-1, clr_wnd);
642         SetPixel( hDC, rc.right-1, rc.top, clr_wnd);
643         SetPixel( hDC, rc.right-1, rc.bottom-1, clr_wnd);
644         InflateRect( &rc, -1, -1 );
645     }
646     
647     if ((wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON)
648     {
649         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
650         InflateRect( &rc, -1, -1 );
651     }
652
653     if (TWEAK_WineLook == WIN31_LOOK)
654     {
655         if (pushedState)
656         {
657             /* draw button shadow: */
658             SelectObject(hDC, GetSysColorBrush(COLOR_BTNSHADOW));
659             PatBlt(hDC, rc.left, rc.top, 1, rc.bottom-rc.top, PATCOPY );
660             PatBlt(hDC, rc.left, rc.top, rc.right-rc.left, 1, PATCOPY );
661         } else {
662            rc.right++, rc.bottom++;
663            DrawEdge( hDC, &rc, EDGE_RAISED, BF_RECT );
664            rc.right--, rc.bottom--;
665         }
666     }
667     else
668     {
669         UINT uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
670
671         if (wndPtr->dwStyle & BS_FLAT)
672             uState |= DFCS_MONO;
673         else if (pushedState)
674         {
675             if ( (wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON )
676                 uState |= DFCS_FLAT;
677             else
678                 uState |= DFCS_PUSHED;
679         }
680
681         if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE))
682             uState |= DFCS_CHECKED;
683
684         DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
685
686         focus_rect = rc;
687     }
688
689     /* draw button label */
690     r = rc;
691     dtFlags = BUTTON_CalcLabelRect(wndPtr, hDC, &r);
692
693     if (dtFlags == (UINT)-1L)
694        goto cleanup;
695
696     if (pushedState)
697        OffsetRect(&r, 1, 1);
698
699     if(TWEAK_WineLook == WIN31_LOOK)
700     {
701        focus_rect = r;
702        InflateRect(&focus_rect, 2, 0);
703     }
704
705     hRgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
706     SelectClipRgn(hDC, hRgn);
707
708     oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
709
710     BUTTON_DrawLabel(wndPtr, hDC, dtFlags, &r);
711
712     SetTextColor( hDC, oldTxtColor );
713     SelectClipRgn(hDC, 0);
714     DeleteObject(hRgn);
715
716     if (infoPtr->state & BUTTON_HASFOCUS)
717     {
718         InflateRect( &focus_rect, -1, -1 );
719         IntersectRect(&focus_rect, &focus_rect, &rc);
720         DrawFocusRect( hDC, &focus_rect );
721     }
722
723  cleanup:
724     SelectObject( hDC, hOldPen );
725     SelectObject( hDC, hOldBrush );
726     SetBkMode(hDC, oldBkMode);
727 }
728
729 /**********************************************************************
730  *       Check Box & Radio Button Functions
731  */
732
733 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action )
734 {
735     RECT rbox, rtext, client;
736     HBRUSH hBrush;
737     int delta;
738     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
739     UINT dtFlags;
740     HRGN hRgn;
741
742     if (wndPtr->dwStyle & BS_PUSHLIKE)
743     {
744         BOOL bHighLighted = (infoPtr->state & BUTTON_HIGHLIGHTED);
745
746         BUTTON_DrawPushButton(wndPtr,
747                               hDC,
748                               action,
749                               bHighLighted);
750         return;
751     }
752
753     GetClientRect(wndPtr->hwndSelf, &client);
754     rbox = rtext = client;
755
756     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
757
758     /* GetControlBrush16 sends WM_CTLCOLORBTN, plus it returns default brush
759      * if parent didn't return valid one. So we kill two hares at once
760      */
761     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
762
763     if (wndPtr->dwStyle & BS_LEFTTEXT) 
764     {
765         /* magic +4 is what CTL3D expects */
766
767         rtext.right -= checkBoxWidth + 4;
768         rbox.left = rbox.right - checkBoxWidth;
769     }
770     else 
771     {
772         rtext.left += checkBoxWidth + 4;
773         rbox.right = checkBoxWidth;
774     }
775
776     /* Draw the check-box bitmap */
777     if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
778     { 
779         if( TWEAK_WineLook == WIN31_LOOK )
780         {
781         HDC hMemDC = CreateCompatibleDC( hDC );
782         int x = 0, y = 0;
783         delta = (rbox.bottom - rbox.top - checkBoxHeight) / 2;
784
785         /* Check in case the client area is smaller than the checkbox bitmap */
786         if (delta < 0) delta = 0;
787
788         if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
789         else FillRect( hDC, &client, hBrush );
790
791         if (infoPtr->state & BUTTON_HIGHLIGHTED) x += 2 * checkBoxWidth;
792         if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) x += checkBoxWidth;
793         if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
794             ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) y += checkBoxHeight;
795         else if (infoPtr->state & BUTTON_3STATE) y += 2 * checkBoxHeight;
796
797         /* The bitmap for the radio button is not aligned with the
798          * left of the window, it is 1 pixel off. */
799         if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
800             ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
801           rbox.left += 1;
802
803         SelectObject( hMemDC, hbitmapCheckBoxes );
804         BitBlt( hDC, rbox.left, rbox.top + delta, checkBoxWidth,
805                   checkBoxHeight, hMemDC, x, y, SRCCOPY );
806         DeleteDC( hMemDC );
807         }
808         else
809         {
810             UINT state;
811
812             if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
813                 ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) state = DFCS_BUTTONRADIO;
814             else if (infoPtr->state & BUTTON_3STATE) state = DFCS_BUTTON3STATE;
815             else state = DFCS_BUTTONCHECK;
816
817             if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) state |= DFCS_CHECKED;
818             
819             if (infoPtr->state & BUTTON_HIGHLIGHTED) state |= DFCS_PUSHED;
820
821             if (wndPtr->dwStyle & WS_DISABLED) state |= DFCS_INACTIVE;
822
823             /* rbox must have the correct height */ 
824             delta = rbox.bottom - rbox.top - checkBoxHeight;
825             if (delta > 0) 
826             {  
827                 int ofs = (abs(delta) / 2);
828                 rbox.bottom -= ofs + 1;
829                 rbox.top = rbox.bottom - checkBoxHeight;
830             }
831             else if (delta < 0)
832             {
833                 int ofs = (abs(delta) / 2);
834                 rbox.top -= ofs + 1;
835                 rbox.bottom = rbox.top + checkBoxHeight;
836             }
837
838             DrawFrameControl( hDC, &rbox, DFC_BUTTON, state );
839         }
840     }
841
842     /* Draw label */
843     client = rtext;
844     dtFlags = BUTTON_CalcLabelRect(wndPtr, hDC, &rtext);
845
846     if (dtFlags == (UINT)-1L) /* Noting to draw */
847         return;
848     hRgn = CreateRectRgn(client.left, client.top, client.right, client.bottom);
849     SelectClipRgn(hDC, hRgn);
850     DeleteObject(hRgn);
851
852     if (action == ODA_DRAWENTIRE)
853         BUTTON_DrawLabel(wndPtr, hDC, dtFlags, &rtext);
854
855     /* ... and focus */
856     if ((action == ODA_FOCUS) ||
857         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
858     {
859         rtext.left--;
860         rtext.right++;
861         IntersectRect(&rtext, &rtext, &client);
862         DrawFocusRect( hDC, &rtext );
863     }
864     SelectClipRgn(hDC, 0);
865 }
866
867
868 /**********************************************************************
869  *       BUTTON_CheckAutoRadioButton
870  *
871  * wndPtr is checked, uncheck every other auto radio button in group
872  */
873 static void BUTTON_CheckAutoRadioButton( WND *wndPtr )
874 {
875     HWND parent, sibling, start;
876     if (!(wndPtr->dwStyle & WS_CHILD)) return;
877     parent = wndPtr->parent->hwndSelf;
878     /* assure that starting control is not disabled or invisible */
879     start = sibling = GetNextDlgGroupItem( parent, wndPtr->hwndSelf, TRUE );
880     do
881     {
882         WND *tmpWnd;
883         if (!sibling) break;
884         tmpWnd = WIN_FindWndPtr(sibling);
885         if ((wndPtr->hwndSelf != sibling) &&
886             ((tmpWnd->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
887             SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
888         sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
889         WIN_ReleaseWndPtr(tmpWnd);
890     } while (sibling != start);
891 }
892
893
894 /**********************************************************************
895  *       Group Box Functions
896  */
897
898 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action )
899 {
900     RECT rc, rcFrame;
901     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
902     HBRUSH hbr;
903     UINT dtFlags;
904
905     if (action != ODA_DRAWENTIRE) return;
906
907     if (infoPtr->hFont)
908         SelectObject (hDC, infoPtr->hFont);
909     /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
910     hbr = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_STATIC );
911
912
913     GetClientRect( wndPtr->hwndSelf, &rc);
914     if (TWEAK_WineLook == WIN31_LOOK) {
915         HPEN hPrevPen = SelectObject( hDC,
916                                           GetSysColorPen(COLOR_WINDOWFRAME));
917         HBRUSH hPrevBrush = SelectObject( hDC,
918                                               GetStockObject(NULL_BRUSH) );
919
920         Rectangle( hDC, rc.left, rc.top + 2, rc.right - 1, rc.bottom - 1 );
921         SelectObject( hDC, hPrevBrush );
922         SelectObject( hDC, hPrevPen );
923     } else {
924         TEXTMETRICW tm;
925         rcFrame = rc;
926
927         GetTextMetricsW (hDC, &tm);
928         rcFrame.top += (tm.tmHeight / 2) - 1;
929         DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT |
930                            ((wndPtr->dwStyle & BS_FLAT) ? BF_FLAT : 0));
931     }
932
933     InflateRect(&rc, -7, 1);
934     dtFlags = BUTTON_CalcLabelRect(wndPtr, hDC, &rc);
935
936     if (dtFlags == (UINT)-1L)
937        return;
938
939     /* Because buttons have CS_PARENTDC class style, there is a chance
940      * that label will be drawn out of client rect.
941      * But Windows doesn't clip label's rect, so do I.
942      */
943
944     /* There is 1-pixel marging at the left, right, and bottom */
945     rc.left--; rc.right++; rc.bottom++;
946     FillRect(hDC, &rc, hbr);
947     rc.left++; rc.right--; rc.bottom--;
948
949     BUTTON_DrawLabel(wndPtr, hDC, dtFlags, &rc);   
950 }
951
952
953 /**********************************************************************
954  *       User Button Functions
955  */
956
957 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action )
958 {
959     RECT rc;
960     HBRUSH hBrush;
961     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
962
963     if (action == ODA_SELECT) return;
964
965     GetClientRect( wndPtr->hwndSelf, &rc);
966
967     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
968     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
969
970     FillRect( hDC, &rc, hBrush );
971     if ((action == ODA_FOCUS) ||
972         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
973         DrawFocusRect( hDC, &rc );
974 }
975
976
977 /**********************************************************************
978  *       Ownerdrawn Button Functions
979  */
980
981 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action )
982 {
983     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
984     DRAWITEMSTRUCT dis;
985     HRGN clipRegion;
986     RECT clipRect;
987
988     dis.CtlType    = ODT_BUTTON;
989     dis.CtlID      = wndPtr->wIDmenu;
990     dis.itemID     = 0;
991     dis.itemAction = action;
992     dis.itemState  = ((infoPtr->state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
993                      ((infoPtr->state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
994                      (IsWindowEnabled(wndPtr->hwndSelf) ? 0: ODS_DISABLED);
995     dis.hwndItem   = wndPtr->hwndSelf;
996     dis.hDC        = hDC;
997     dis.itemData   = 0;
998     GetClientRect( wndPtr->hwndSelf, &dis.rcItem );
999
1000     clipRegion = CreateRectRgnIndirect(&dis.rcItem);   
1001     if (GetClipRgn(hDC, clipRegion) != 1)
1002     {
1003         DeleteObject(clipRegion);
1004         clipRegion=(HRGN)NULL;
1005     }
1006     clipRect = dis.rcItem;
1007     DPtoLP(hDC, (LPPOINT) &clipRect, 2);    
1008     IntersectClipRect(hDC, clipRect.left,  clipRect.top, clipRect.right, clipRect.bottom);
1009
1010     SetBkColor( hDC, GetSysColor( COLOR_BTNFACE ) );
1011
1012     SendMessageW( GetParent(wndPtr->hwndSelf), WM_DRAWITEM,
1013                     wndPtr->wIDmenu, (LPARAM)&dis );
1014
1015     SelectClipRgn(hDC, clipRegion);             
1016 }
1017