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