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