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