Cast time_t to long for printing.
[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_GLOBALCLASS | 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 = GetWindowTextLengthW( hwnd );
156     WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
157     if (buffer) GetWindowTextW( 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         return 1;
209
210     case WM_PAINT:
211         if (btnPaintFunc[btn_type])
212         {
213             PAINTSTRUCT ps;
214             HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
215             int nOldMode = SetBkMode( hdc, OPAQUE );
216             (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
217             SetBkMode(hdc, nOldMode); /*  reset painting mode */
218             if( !wParam ) EndPaint( hWnd, &ps );
219         }
220         break;
221
222     case WM_KEYDOWN:
223         if (wParam == VK_SPACE)
224         {
225             SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
226             set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
227         }
228         break;
229
230     case WM_LBUTTONDBLCLK:
231         if(style & BS_NOTIFY ||
232            btn_type == BS_RADIOBUTTON ||
233            btn_type == BS_USERBUTTON ||
234            btn_type == BS_OWNERDRAW)
235         {
236             SendMessageW( GetParent(hWnd), WM_COMMAND,
237                           MAKEWPARAM( GetWindowLongA(hWnd,GWL_ID), BN_DOUBLECLICKED ),
238                           (LPARAM)hWnd);
239             break;
240         }
241         /* fall through */
242     case WM_LBUTTONDOWN:
243         SetCapture( hWnd );
244         SetFocus( hWnd );
245         SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
246         set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
247         break;
248
249     case WM_KEYUP:
250         if (wParam != VK_SPACE)
251             break;
252         /* fall through */
253     case WM_LBUTTONUP:
254         state = get_button_state( hWnd );
255         if (!(state & BUTTON_BTNPRESSED)) break;
256         state &= BUTTON_NSTATES;
257         set_button_state( hWnd, state );
258         if (!(state & BUTTON_HIGHLIGHTED))
259         {
260             ReleaseCapture();
261             break;
262         }
263         SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
264         ReleaseCapture();
265         GetClientRect( hWnd, &rect );
266         if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
267         {
268             state = get_button_state( hWnd );
269             switch(btn_type)
270             {
271             case BS_AUTOCHECKBOX:
272                 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
273                 break;
274             case BS_AUTORADIOBUTTON:
275                 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
276                 break;
277             case BS_AUTO3STATE:
278                 SendMessageW( hWnd, BM_SETCHECK,
279                                 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
280                 break;
281             }
282             SendMessageW( GetParent(hWnd), WM_COMMAND,
283                           MAKEWPARAM( GetWindowLongA(hWnd,GWL_ID), BN_CLICKED ), (LPARAM)hWnd);
284         }
285         break;
286
287     case WM_CAPTURECHANGED:
288         state = get_button_state( hWnd );
289         if (state & BUTTON_BTNPRESSED)
290         {
291             state &= BUTTON_NSTATES;
292             set_button_state( hWnd, state );
293             if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
294         }
295         break;
296
297     case WM_MOUSEMOVE:
298         if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
299         {
300             GetClientRect( hWnd, &rect );
301             SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
302         }
303         break;
304
305     case WM_SETTEXT:
306     {
307         /* Clear an old text here as Windows does */
308         HDC hdc = GetDC(hWnd);
309         HBRUSH hbrush;
310         RECT client, rc;
311
312         hbrush = (HBRUSH)SendMessageW(GetParent(hWnd), WM_CTLCOLORSTATIC,
313                                       (WPARAM)hdc, (LPARAM)hWnd);
314         if (!hbrush) /* did the app forget to call DefWindowProc ? */
315             hbrush = (HBRUSH)DefWindowProcW(GetParent(hWnd), WM_CTLCOLORSTATIC,
316                                             (WPARAM)hdc, (LPARAM)hWnd);
317
318         GetClientRect(hWnd, &client);
319         rc = client;
320         BUTTON_CalcLabelRect(hWnd, hdc, &rc);
321         /* Clip by client rect bounds */
322         if (rc.right > client.right) rc.right = client.right;
323         if (rc.bottom > client.bottom) rc.bottom = client.bottom;
324         FillRect(hdc, &rc, hbrush);
325         ReleaseDC(hWnd, hdc);
326
327         if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
328         else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
329         if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
330             InvalidateRect( hWnd, NULL, TRUE );
331         else
332             paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
333         return 1; /* success. FIXME: check text length */
334     }
335
336     case WM_SETFONT:
337         set_button_font( hWnd, (HFONT)wParam );
338         if (lParam) paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
339         break;
340
341     case WM_GETFONT:
342         return (LRESULT)get_button_font( hWnd );
343
344     case WM_SETFOCUS:
345         if ((btn_type == BS_RADIOBUTTON || btn_type == BS_AUTORADIOBUTTON) && (GetCapture() != hWnd) &&
346             !(SendMessageW(hWnd, BM_GETCHECK, 0, 0) & BST_CHECKED))
347         {
348             /* The notification is sent when the button (BS_AUTORADIOBUTTON)
349                is unchecked and the focus was not given by a mouse click. */
350             if (btn_type == BS_AUTORADIOBUTTON)
351                 SendMessageW( hWnd, BM_SETCHECK, BUTTON_CHECKED, 0 );
352             SendMessageW( GetParent(hWnd), WM_COMMAND,
353                           MAKEWPARAM( GetWindowLongA(hWnd,GWL_ID), BN_CLICKED ), (LPARAM)hWnd);
354         }
355         set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
356         paint_button( hWnd, btn_type, ODA_FOCUS );
357         break;
358
359     case WM_KILLFOCUS:
360         set_button_state( hWnd, get_button_state(hWnd) & ~BUTTON_HASFOCUS );
361         paint_button( hWnd, btn_type, ODA_FOCUS );
362         InvalidateRect( hWnd, NULL, TRUE );
363         break;
364
365     case WM_SYSCOLORCHANGE:
366         InvalidateRect( hWnd, NULL, FALSE );
367         break;
368
369     case BM_SETSTYLE16:
370     case BM_SETSTYLE:
371         if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
372         btn_type = wParam & 0x0f;
373         style = (style & ~0x0f) | btn_type;
374         SetWindowLongA( hWnd, GWL_STYLE, style );
375
376         /* Only redraw if lParam flag is set.*/
377         if (lParam)
378            paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
379
380         break;
381
382     case BM_CLICK:
383         SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
384         SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
385         break;
386
387     case BM_SETIMAGE:
388         /* Check that image format matches button style */
389         switch (style & (BS_BITMAP|BS_ICON))
390         {
391         case BS_BITMAP:
392             if (wParam != IMAGE_BITMAP) return 0;
393             break;
394         case BS_ICON:
395             if (wParam != IMAGE_ICON) return 0;
396             break;
397         default:
398             return 0;
399         }
400         oldHbitmap = (HBITMAP)SetWindowLongA( hWnd, HIMAGE_GWL_OFFSET, lParam );
401         InvalidateRect( hWnd, NULL, FALSE );
402         return (LRESULT)oldHbitmap;
403
404     case BM_GETIMAGE:
405         return GetWindowLongA( hWnd, HIMAGE_GWL_OFFSET );
406
407     case BM_GETCHECK16:
408     case BM_GETCHECK:
409         return get_button_state( hWnd ) & 3;
410
411     case BM_SETCHECK16:
412     case BM_SETCHECK:
413         if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
414         state = get_button_state( hWnd );
415         if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
416         {
417             if (wParam) style |= WS_TABSTOP;
418             else style &= ~WS_TABSTOP;
419             SetWindowLongA( hWnd, GWL_STYLE, style );
420         }
421         if ((state & 3) != wParam)
422         {
423             set_button_state( hWnd, (state & ~3) | wParam );
424             paint_button( hWnd, btn_type, ODA_SELECT );
425         }
426         if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
427             BUTTON_CheckAutoRadioButton( hWnd );
428         break;
429
430     case BM_GETSTATE16:
431     case BM_GETSTATE:
432         return get_button_state( hWnd );
433
434     case BM_SETSTATE16:
435     case BM_SETSTATE:
436         state = get_button_state( hWnd );
437         if (wParam)
438         {
439             if (state & BUTTON_HIGHLIGHTED) break;
440             set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
441         }
442         else
443         {
444             if (!(state & BUTTON_HIGHLIGHTED)) break;
445             set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
446         }
447         paint_button( hWnd, btn_type, ODA_SELECT );
448         break;
449
450     case WM_NCHITTEST:
451         if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
452         /* fall through */
453     default:
454         return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
455                          DefWindowProcA(hWnd, uMsg, wParam, lParam);
456     }
457     return 0;
458 }
459
460 /***********************************************************************
461  *           ButtonWndProcW
462  * The button window procedure. This is just a wrapper which locks
463  * the passed HWND and calls the real window procedure (with a WND*
464  * pointer pointing to the locked windowstructure).
465  */
466 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
467 {
468     if (!IsWindow( hWnd )) return 0;
469     return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
470 }
471
472
473 /***********************************************************************
474  *           ButtonWndProcA
475  */
476 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
477 {
478     if (!IsWindow( hWnd )) return 0;
479     return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
480 }
481
482
483 /**********************************************************************
484  * Convert button styles to flags used by DrawText.
485  * TODO: handle WS_EX_RIGHT extended style.
486  */
487 static UINT BUTTON_BStoDT(DWORD style)
488 {
489    UINT dtStyle = DT_NOCLIP;  /* We use SelectClipRgn to limit output */
490
491    /* "Convert" pushlike buttons to pushbuttons */
492    if (style & BS_PUSHLIKE)
493       style &= ~0x0F;
494
495    if (!(style & BS_MULTILINE))
496       dtStyle |= DT_SINGLELINE;
497    else
498       dtStyle |= DT_WORDBREAK;
499
500    switch (style & BS_CENTER)
501    {
502       case BS_LEFT:   /* DT_LEFT is 0 */    break;
503       case BS_RIGHT:  dtStyle |= DT_RIGHT;  break;
504       case BS_CENTER: dtStyle |= DT_CENTER; break;
505       default:
506          /* Pushbutton's text is centered by default */
507          if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
508          /* all other flavours have left aligned text */
509    }
510
511    /* DrawText ignores vertical alignment for multiline text,
512     * but we use these flags to align label manualy.
513     */
514    if (get_button_type(style) != BS_GROUPBOX)
515    {
516       switch (style & BS_VCENTER)
517       {
518          case BS_TOP:     /* DT_TOP is 0 */      break;
519          case BS_BOTTOM:  dtStyle |= DT_BOTTOM;  break;
520          case BS_VCENTER: /* fall through */
521          default:         dtStyle |= DT_VCENTER; break;
522       }
523    }
524    else
525       /* GroupBox's text is always single line and is top aligned. */
526       dtStyle |= DT_SINGLELINE;
527
528    return dtStyle;
529 }
530
531 /**********************************************************************
532  *       BUTTON_CalcLabelRect
533  *
534  *   Calculates label's rectangle depending on button style.
535  *
536  * Returns flags to be passed to DrawText.
537  * Calculated rectangle doesn't take into account button state
538  * (pushed, etc.). If there is nothing to draw (no text/image) output
539  * rectangle is empty, and return value is (UINT)-1.
540  */
541 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
542 {
543    LONG style = GetWindowLongA( hwnd, GWL_STYLE );
544    WCHAR *text;
545    ICONINFO    iconInfo;
546    BITMAP      bm;
547    UINT        dtStyle = BUTTON_BStoDT(style);
548    RECT        r = *rc;
549    INT         n;
550
551    /* Calculate label rectangle according to label type */
552    switch (style & (BS_ICON|BS_BITMAP))
553    {
554       case BS_TEXT:
555           if (!(text = get_button_text( hwnd ))) goto empty_rect;
556           if (!text[0])
557           {
558               HeapFree( GetProcessHeap(), 0, text );
559               goto empty_rect;
560           }
561           DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
562           HeapFree( GetProcessHeap(), 0, text );
563           break;
564
565       case BS_ICON:
566          if (!GetIconInfo((HICON)GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
567             goto empty_rect;
568
569          GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
570
571          r.right  = r.left + bm.bmWidth;
572          r.bottom = r.top  + bm.bmHeight;
573
574          DeleteObject(iconInfo.hbmColor);
575          DeleteObject(iconInfo.hbmMask);
576          break;
577
578       case BS_BITMAP:
579          if (!GetObjectW( (HANDLE)GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
580             goto empty_rect;
581
582          r.right  = r.left + bm.bmWidth;
583          r.bottom = r.top  + bm.bmHeight;
584          break;
585
586       default:
587       empty_rect:
588          r.right = r.left;
589          r.bottom = r.top;
590          return (UINT)(LONG)-1;
591    }
592
593    /* Position label inside bounding rectangle according to
594     * alignment flags. (calculated rect is always left-top aligned).
595     * If label is aligned to any side - shift label in opposite
596     * direction to leave extra space for focus rectangle.
597     */
598    switch (dtStyle & (DT_CENTER|DT_RIGHT))
599    {
600       case DT_LEFT:    r.left++;  r.right++;  break;
601       case DT_CENTER:  n = r.right - r.left;
602                        r.left   = rc->left + ((rc->right - rc->left) - n) / 2;
603                        r.right  = r.left + n; break;
604       case DT_RIGHT:   n = r.right - r.left;
605                        r.right  = rc->right - 1;
606                        r.left   = r.right - n;
607                        break;
608    }
609
610    switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
611    {
612       case DT_TOP:     r.top++;  r.bottom++;  break;
613       case DT_VCENTER: n = r.bottom - r.top;
614                        r.top    = rc->top + ((rc->bottom - rc->top) - n) / 2;
615                        r.bottom = r.top + n;  break;
616       case DT_BOTTOM:  n = r.bottom - r.top;
617                        r.bottom = rc->bottom - 1;
618                        r.top    = r.bottom - n;
619                        break;
620    }
621
622    *rc = r;
623    return dtStyle;
624 }
625
626
627 /**********************************************************************
628  *       BUTTON_DrawTextCallback
629  *
630  *   Callback function used by DrawStateW function.
631  */
632 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
633 {
634    RECT rc;
635    rc.left = 0;
636    rc.top = 0;
637    rc.right = cx;
638    rc.bottom = cy;
639
640    DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
641    return TRUE;
642 }
643
644
645 /**********************************************************************
646  *       BUTTON_DrawLabel
647  *
648  *   Common function for drawing button label.
649  */
650 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
651 {
652    DRAWSTATEPROC lpOutputProc = NULL;
653    LPARAM lp;
654    WPARAM wp = 0;
655    HBRUSH hbr = 0;
656    UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
657    LONG state = get_button_state( hwnd );
658    LONG style = GetWindowLongA( hwnd, GWL_STYLE );
659    WCHAR *text = NULL;
660
661    /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
662     * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
663     * I don't have Win31 on hand to verify that, so I leave it as is.
664     */
665
666    if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
667    {
668       hbr = GetSysColorBrush(COLOR_GRAYTEXT);
669       flags |= DSS_MONO;
670    }
671
672    switch (style & (BS_ICON|BS_BITMAP))
673    {
674       case BS_TEXT:
675          /* DST_COMPLEX -- is 0 */
676          lpOutputProc = BUTTON_DrawTextCallback;
677          if (!(text = get_button_text( hwnd ))) return;
678          lp = (LPARAM)text;
679          wp = (WPARAM)dtFlags;
680          break;
681
682       case BS_ICON:
683          flags |= DST_ICON;
684          lp = GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET );
685          break;
686
687       case BS_BITMAP:
688          flags |= DST_BITMAP;
689          lp = GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET );
690          break;
691
692       default:
693          return;
694    }
695
696    DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
697               rc->right - rc->left, rc->bottom - rc->top, flags);
698    if (text) HeapFree( GetProcessHeap(), 0, text );
699 }
700
701 /**********************************************************************
702  *       Push Button Functions
703  */
704 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
705 {
706     RECT     rc, focus_rect, r;
707     UINT     dtFlags;
708     HRGN     hRgn;
709     HPEN     hOldPen;
710     HBRUSH   hOldBrush;
711     INT      oldBkMode;
712     COLORREF oldTxtColor;
713     HFONT hFont;
714     LONG state = get_button_state( hwnd );
715     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
716     BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
717
718     GetClientRect( hwnd, &rc );
719
720     /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
721     if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
722     SendMessageW( GetParent(hwnd), WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
723     hOldPen = (HPEN)SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
724     hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
725     oldBkMode = SetBkMode(hDC, TRANSPARENT);
726
727     if ( TWEAK_WineLook == WIN31_LOOK)
728     {
729         COLORREF clr_wnd = GetSysColor(COLOR_WINDOW);
730         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
731
732         SetPixel( hDC, rc.left, rc.top, clr_wnd);
733         SetPixel( hDC, rc.left, rc.bottom-1, clr_wnd);
734         SetPixel( hDC, rc.right-1, rc.top, clr_wnd);
735         SetPixel( hDC, rc.right-1, rc.bottom-1, clr_wnd);
736         InflateRect( &rc, -1, -1 );
737     }
738
739     if (get_button_type(style) == BS_DEFPUSHBUTTON)
740     {
741         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
742         InflateRect( &rc, -1, -1 );
743     }
744
745     if (TWEAK_WineLook == WIN31_LOOK)
746     {
747         if (pushedState)
748         {
749             /* draw button shadow: */
750             SelectObject(hDC, GetSysColorBrush(COLOR_BTNSHADOW));
751             PatBlt(hDC, rc.left, rc.top, 1, rc.bottom-rc.top, PATCOPY );
752             PatBlt(hDC, rc.left, rc.top, rc.right-rc.left, 1, PATCOPY );
753         } else {
754            rc.right++, rc.bottom++;
755            DrawEdge( hDC, &rc, EDGE_RAISED, BF_RECT );
756            rc.right--, rc.bottom--;
757         }
758     }
759     else
760     {
761         UINT uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
762
763         if (style & BS_FLAT)
764             uState |= DFCS_MONO;
765         else if (pushedState)
766         {
767             if (get_button_type(style) == BS_DEFPUSHBUTTON )
768                 uState |= DFCS_FLAT;
769             else
770                 uState |= DFCS_PUSHED;
771         }
772
773         if (state & (BUTTON_CHECKED | BUTTON_3STATE))
774             uState |= DFCS_CHECKED;
775
776         DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
777
778         focus_rect = rc;
779     }
780
781     /* draw button label */
782     r = rc;
783     dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
784
785     if (dtFlags == (UINT)-1L)
786        goto cleanup;
787
788     if (pushedState)
789        OffsetRect(&r, 1, 1);
790
791     if(TWEAK_WineLook == WIN31_LOOK)
792     {
793        focus_rect = r;
794        InflateRect(&focus_rect, 2, 0);
795     }
796
797     hRgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
798     SelectClipRgn(hDC, hRgn);
799
800     oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
801
802     BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
803
804     SetTextColor( hDC, oldTxtColor );
805     SelectClipRgn(hDC, 0);
806     DeleteObject(hRgn);
807
808     if (state & BUTTON_HASFOCUS)
809     {
810         InflateRect( &focus_rect, -1, -1 );
811         IntersectRect(&focus_rect, &focus_rect, &rc);
812         DrawFocusRect( hDC, &focus_rect );
813     }
814
815  cleanup:
816     SelectObject( hDC, hOldPen );
817     SelectObject( hDC, hOldBrush );
818     SetBkMode(hDC, oldBkMode);
819 }
820
821 /**********************************************************************
822  *       Check Box & Radio Button Functions
823  */
824
825 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
826 {
827     RECT rbox, rtext, client;
828     HBRUSH hBrush;
829     int delta;
830     UINT dtFlags;
831     HRGN hRgn;
832     HFONT hFont;
833     LONG state = get_button_state( hwnd );
834     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
835
836     if (style & BS_PUSHLIKE)
837     {
838         PB_Paint( hwnd, hDC, action );
839         return;
840     }
841
842     GetClientRect(hwnd, &client);
843     rbox = rtext = client;
844
845     if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
846
847     hBrush = (HBRUSH)SendMessageW(GetParent(hwnd), WM_CTLCOLORSTATIC,
848                                   (WPARAM)hDC, (LPARAM)hwnd);
849     if (!hBrush) /* did the app forget to call defwindowproc ? */
850         hBrush = (HBRUSH)DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC,
851                                         (WPARAM)hDC, (LPARAM)hwnd );
852
853     if (style & BS_LEFTTEXT)
854     {
855         /* magic +4 is what CTL3D expects */
856
857         rtext.right -= checkBoxWidth + 4;
858         rbox.left = rbox.right - checkBoxWidth;
859     }
860     else
861     {
862         rtext.left += checkBoxWidth + 4;
863         rbox.right = checkBoxWidth;
864     }
865  
866     /* Since WM_ERASEBKGND does nothing, first prepare background */
867     if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
868     if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
869
870     /* Draw label */
871     client = rtext;
872     dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
873     
874     rbox.top = rtext.top;
875     rbox.bottom = rtext.bottom;
876     /* Draw the check-box bitmap */
877     if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
878     {
879         if( TWEAK_WineLook == WIN31_LOOK )
880         {
881             HDC hMemDC = CreateCompatibleDC( hDC );
882             int x = 0, y = 0;
883             delta = (rbox.bottom - rbox.top - checkBoxHeight) / 2;
884             
885             /* Check in case the client area is smaller than the checkbox bitmap */
886             if (delta < 0) delta = 0;
887             
888             if (state & BUTTON_HIGHLIGHTED) x += 2 * checkBoxWidth;
889             if (state & (BUTTON_CHECKED | BUTTON_3STATE)) x += checkBoxWidth;
890             if ((get_button_type(style) == BS_RADIOBUTTON) ||
891                 (get_button_type(style) == BS_AUTORADIOBUTTON)) y += checkBoxHeight;
892             else if (state & BUTTON_3STATE) y += 2 * checkBoxHeight;
893             
894             /* The bitmap for the radio button is not aligned with the
895              * left of the window, it is 1 pixel off. */
896             if ((get_button_type(style) == BS_RADIOBUTTON) ||
897                 (get_button_type(style) == BS_AUTORADIOBUTTON))
898               rbox.left += 1;
899             
900             SelectObject( hMemDC, hbitmapCheckBoxes );
901             BitBlt( hDC, rbox.left, rbox.top + delta, checkBoxWidth,
902                     checkBoxHeight, hMemDC, x, y, SRCCOPY );
903             DeleteDC( hMemDC );
904         }
905         else
906         {
907             UINT flags;
908
909             if ((get_button_type(style) == BS_RADIOBUTTON) ||
910                 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
911             else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
912             else flags = DFCS_BUTTONCHECK;
913
914             if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
915             if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
916
917             if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
918
919             /* rbox must have the correct height */
920             delta = rbox.bottom - rbox.top - checkBoxHeight;
921             
922             if (style & BS_TOP) {
923               if (delta > 0) {
924                 rbox.bottom = rbox.top + checkBoxHeight;
925               } else {
926                 rbox.top -= -delta/2 + 1;
927                 rbox.bottom += rbox.top + checkBoxHeight;
928               }
929             } else if (style & BS_BOTTOM) {
930               if (delta > 0) {
931                 rbox.top = rbox.bottom - checkBoxHeight;
932               } else {
933                 rbox.bottom += -delta/2 + 1;
934                 rbox.top = rbox.bottom -= checkBoxHeight;
935               }
936             } else { /* Default */
937               if (delta > 0)
938                 {
939                   int ofs = (delta / 2);
940                   rbox.bottom -= ofs + 1;
941                   rbox.top = rbox.bottom - checkBoxHeight;
942                 }
943               else if (delta < 0)
944                 {
945                   int ofs = (-delta / 2);
946                   rbox.top -= ofs + 1;
947                   rbox.bottom = rbox.top + checkBoxHeight;
948                 }
949             }
950
951             DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
952         }
953     }
954
955     if (dtFlags == (UINT)-1L) /* Noting to draw */
956         return;
957     hRgn = CreateRectRgn(client.left, client.top, client.right, client.bottom);
958     SelectClipRgn(hDC, hRgn);
959     DeleteObject(hRgn);
960
961     if (action == ODA_DRAWENTIRE)
962         BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
963
964     /* ... and focus */
965     if ((action == ODA_FOCUS) ||
966         ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
967     {
968         rtext.left--;
969         rtext.right++;
970         IntersectRect(&rtext, &rtext, &client);
971         DrawFocusRect( hDC, &rtext );
972     }
973     SelectClipRgn(hDC, 0);
974 }
975
976
977 /**********************************************************************
978  *       BUTTON_CheckAutoRadioButton
979  *
980  * hwnd is checked, uncheck every other auto radio button in group
981  */
982 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
983 {
984     HWND parent, sibling, start;
985
986     parent = GetParent(hwnd);
987     /* make sure that starting control is not disabled or invisible */
988     start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
989     do
990     {
991         if (!sibling) break;
992         if ((hwnd != sibling) &&
993             ((GetWindowLongA( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
994             SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
995         sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
996     } while (sibling != start);
997 }
998
999
1000 /**********************************************************************
1001  *       Group Box Functions
1002  */
1003
1004 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1005 {
1006     RECT rc, rcFrame;
1007     HBRUSH hbr;
1008     HFONT hFont;
1009     UINT dtFlags;
1010     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1011
1012     if (action != ODA_DRAWENTIRE) return;
1013
1014     if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1015     /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1016     hbr = (HBRUSH)SendMessageW(GetParent(hwnd), WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1017     if (!hbr) /* did the app forget to call defwindowproc ? */
1018         hbr = (HBRUSH)DefWindowProcW(GetParent(hwnd), WM_CTLCOLORSTATIC,
1019                                      (WPARAM)hDC, (LPARAM)hwnd);
1020
1021     GetClientRect( hwnd, &rc);
1022     if (TWEAK_WineLook == WIN31_LOOK) {
1023         HPEN hPrevPen = SelectObject( hDC,
1024                                           SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
1025         HBRUSH hPrevBrush = SelectObject( hDC,
1026                                               GetStockObject(NULL_BRUSH) );
1027
1028         Rectangle( hDC, rc.left, rc.top + 2, rc.right - 1, rc.bottom - 1 );
1029         SelectObject( hDC, hPrevBrush );
1030         SelectObject( hDC, hPrevPen );
1031     } else {
1032         TEXTMETRICW tm;
1033         rcFrame = rc;
1034
1035         GetTextMetricsW (hDC, &tm);
1036         rcFrame.top += (tm.tmHeight / 2) - 1;
1037         DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1038     }
1039
1040     InflateRect(&rc, -7, 1);
1041     dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1042
1043     if (dtFlags == (UINT)-1L)
1044        return;
1045
1046     /* Because buttons have CS_PARENTDC class style, there is a chance
1047      * that label will be drawn out of client rect.
1048      * But Windows doesn't clip label's rect, so do I.
1049      */
1050
1051     /* There is 1-pixel marging at the left, right, and bottom */
1052     rc.left--; rc.right++; rc.bottom++;
1053     FillRect(hDC, &rc, hbr);
1054     rc.left++; rc.right--; rc.bottom--;
1055
1056     BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1057 }
1058
1059
1060 /**********************************************************************
1061  *       User Button Functions
1062  */
1063
1064 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1065 {
1066     RECT rc;
1067     HBRUSH hBrush;
1068     HFONT hFont;
1069     LONG state = get_button_state( hwnd );
1070
1071     if (action == ODA_SELECT) return;
1072
1073     GetClientRect( hwnd, &rc);
1074
1075     if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1076
1077     hBrush = (HBRUSH)SendMessageW(GetParent(hwnd), WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1078     if (!hBrush) /* did the app forget to call defwindowproc ? */
1079         hBrush = (HBRUSH)DefWindowProcW(GetParent(hwnd), WM_CTLCOLORBTN,
1080                                         (WPARAM)hDC, (LPARAM)hwnd);
1081
1082     FillRect( hDC, &rc, hBrush );
1083     if ((action == ODA_FOCUS) ||
1084         ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1085         DrawFocusRect( hDC, &rc );
1086 }
1087
1088
1089 /**********************************************************************
1090  *       Ownerdrawn Button Functions
1091  */
1092
1093 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1094 {
1095     LONG state = get_button_state( hwnd );
1096     DRAWITEMSTRUCT dis;
1097     HRGN clipRegion;
1098     RECT clipRect;
1099     UINT id = GetWindowLongA( hwnd, GWL_ID );
1100
1101     dis.CtlType    = ODT_BUTTON;
1102     dis.CtlID      = id;
1103     dis.itemID     = 0;
1104     dis.itemAction = action;
1105     dis.itemState  = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1106                      ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1107                      (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1108     dis.hwndItem   = hwnd;
1109     dis.hDC        = hDC;
1110     dis.itemData   = 0;
1111     GetClientRect( hwnd, &dis.rcItem );
1112
1113     clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1114     if (GetClipRgn(hDC, clipRegion) != 1)
1115     {
1116         DeleteObject(clipRegion);
1117         clipRegion=NULL;
1118     }
1119     clipRect = dis.rcItem;
1120     DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1121     IntersectClipRect(hDC, clipRect.left,  clipRect.top, clipRect.right, clipRect.bottom);
1122
1123     SetBkColor( hDC, GetSysColor( COLOR_BTNFACE ) );
1124     SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1125     SelectClipRgn(hDC, clipRegion);
1126 }