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