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