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