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