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