crypt32/tests: Fix a test failure on older crypt32.
[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_SETSTYLE16:
465     case BM_SETSTYLE:
466         if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
467         btn_type = wParam & 0x0f;
468         style = (style & ~0x0f) | btn_type;
469         WIN_SetStyle( hWnd, style, 0x0f & ~style );
470
471         /* Only redraw if lParam flag is set.*/
472         if (lParam)
473             InvalidateRect( hWnd, NULL, TRUE );
474
475         break;
476
477     case BM_CLICK:
478         SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
479         SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
480         break;
481
482     case BM_SETIMAGE:
483         /* Check that image format matches button style */
484         switch (style & (BS_BITMAP|BS_ICON))
485         {
486         case BS_BITMAP:
487             if (wParam != IMAGE_BITMAP) return 0;
488             break;
489         case BS_ICON:
490             if (wParam != IMAGE_ICON) return 0;
491             break;
492         default:
493             return 0;
494         }
495         oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
496         InvalidateRect( hWnd, NULL, FALSE );
497         return (LRESULT)oldHbitmap;
498
499     case BM_GETIMAGE:
500         return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
501
502     case BM_GETCHECK16:
503     case BM_GETCHECK:
504         return get_button_state( hWnd ) & 3;
505
506     case BM_SETCHECK16:
507     case BM_SETCHECK:
508         if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
509         state = get_button_state( hWnd );
510         if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
511         {
512             if (wParam) style |= WS_TABSTOP;
513             else style &= ~WS_TABSTOP;
514             SetWindowLongW( hWnd, GWL_STYLE, style );
515         }
516         if ((state & 3) != wParam)
517         {
518             set_button_state( hWnd, (state & ~3) | wParam );
519             paint_button( hWnd, btn_type, ODA_SELECT );
520         }
521         if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
522             BUTTON_CheckAutoRadioButton( hWnd );
523         break;
524
525     case BM_GETSTATE16:
526     case BM_GETSTATE:
527         return get_button_state( hWnd );
528
529     case BM_SETSTATE16:
530     case BM_SETSTATE:
531         state = get_button_state( hWnd );
532         if (wParam)
533         {
534             if (state & BUTTON_HIGHLIGHTED) break;
535             set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
536         }
537         else
538         {
539             if (!(state & BUTTON_HIGHLIGHTED)) break;
540             set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
541         }
542         paint_button( hWnd, btn_type, ODA_SELECT );
543         break;
544
545     case WM_NCHITTEST:
546         if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
547         /* fall through */
548     default:
549         return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
550                          DefWindowProcA(hWnd, uMsg, wParam, lParam);
551     }
552     return 0;
553 }
554
555 /***********************************************************************
556  *           ButtonWndProcW
557  * The button window procedure. This is just a wrapper which locks
558  * the passed HWND and calls the real window procedure (with a WND*
559  * pointer pointing to the locked windowstructure).
560  */
561 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
562 {
563     if (!IsWindow( hWnd )) return 0;
564     return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
565 }
566
567
568 /***********************************************************************
569  *           ButtonWndProcA
570  */
571 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
572 {
573     if (!IsWindow( hWnd )) return 0;
574     return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
575 }
576
577
578 /**********************************************************************
579  * Convert button styles to flags used by DrawText.
580  */
581 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
582 {
583    UINT dtStyle = DT_NOCLIP;  /* We use SelectClipRgn to limit output */
584
585    /* "Convert" pushlike buttons to pushbuttons */
586    if (style & BS_PUSHLIKE)
587       style &= ~0x0F;
588
589    if (!(style & BS_MULTILINE))
590       dtStyle |= DT_SINGLELINE;
591    else
592       dtStyle |= DT_WORDBREAK;
593
594    switch (style & BS_CENTER)
595    {
596       case BS_LEFT:   /* DT_LEFT is 0 */    break;
597       case BS_RIGHT:  dtStyle |= DT_RIGHT;  break;
598       case BS_CENTER: dtStyle |= DT_CENTER; break;
599       default:
600          /* Pushbutton's text is centered by default */
601          if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
602          /* all other flavours have left aligned text */
603    }
604
605    if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
606
607    /* DrawText ignores vertical alignment for multiline text,
608     * but we use these flags to align label manually.
609     */
610    if (get_button_type(style) != BS_GROUPBOX)
611    {
612       switch (style & BS_VCENTER)
613       {
614          case BS_TOP:     /* DT_TOP is 0 */      break;
615          case BS_BOTTOM:  dtStyle |= DT_BOTTOM;  break;
616          case BS_VCENTER: /* fall through */
617          default:         dtStyle |= DT_VCENTER; break;
618       }
619    }
620    else
621       /* GroupBox's text is always single line and is top aligned. */
622       dtStyle |= DT_SINGLELINE;
623
624    return dtStyle;
625 }
626
627 /**********************************************************************
628  *       BUTTON_CalcLabelRect
629  *
630  *   Calculates label's rectangle depending on button style.
631  *
632  * Returns flags to be passed to DrawText.
633  * Calculated rectangle doesn't take into account button state
634  * (pushed, etc.). If there is nothing to draw (no text/image) output
635  * rectangle is empty, and return value is (UINT)-1.
636  */
637 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
638 {
639    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
640    LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
641    WCHAR *text;
642    ICONINFO    iconInfo;
643    BITMAP      bm;
644    UINT        dtStyle = BUTTON_BStoDT( style, ex_style );
645    RECT        r = *rc;
646    INT         n;
647
648    /* Calculate label rectangle according to label type */
649    switch (style & (BS_ICON|BS_BITMAP))
650    {
651       case BS_TEXT:
652           if (!(text = get_button_text( hwnd ))) goto empty_rect;
653           if (!text[0])
654           {
655               HeapFree( GetProcessHeap(), 0, text );
656               goto empty_rect;
657           }
658           DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
659           HeapFree( GetProcessHeap(), 0, text );
660           break;
661
662       case BS_ICON:
663          if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
664             goto empty_rect;
665
666          GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
667
668          r.right  = r.left + bm.bmWidth;
669          r.bottom = r.top  + bm.bmHeight;
670
671          DeleteObject(iconInfo.hbmColor);
672          DeleteObject(iconInfo.hbmMask);
673          break;
674
675       case BS_BITMAP:
676          if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
677             goto empty_rect;
678
679          r.right  = r.left + bm.bmWidth;
680          r.bottom = r.top  + bm.bmHeight;
681          break;
682
683       default:
684       empty_rect:
685          rc->right = r.left;
686          rc->bottom = r.top;
687          return (UINT)-1;
688    }
689
690    /* Position label inside bounding rectangle according to
691     * alignment flags. (calculated rect is always left-top aligned).
692     * If label is aligned to any side - shift label in opposite
693     * direction to leave extra space for focus rectangle.
694     */
695    switch (dtStyle & (DT_CENTER|DT_RIGHT))
696    {
697       case DT_LEFT:    r.left++;  r.right++;  break;
698       case DT_CENTER:  n = r.right - r.left;
699                        r.left   = rc->left + ((rc->right - rc->left) - n) / 2;
700                        r.right  = r.left + n; break;
701       case DT_RIGHT:   n = r.right - r.left;
702                        r.right  = rc->right - 1;
703                        r.left   = r.right - n;
704                        break;
705    }
706
707    switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
708    {
709       case DT_TOP:     r.top++;  r.bottom++;  break;
710       case DT_VCENTER: n = r.bottom - r.top;
711                        r.top    = rc->top + ((rc->bottom - rc->top) - n) / 2;
712                        r.bottom = r.top + n;  break;
713       case DT_BOTTOM:  n = r.bottom - r.top;
714                        r.bottom = rc->bottom - 1;
715                        r.top    = r.bottom - n;
716                        break;
717    }
718
719    *rc = r;
720    return dtStyle;
721 }
722
723
724 /**********************************************************************
725  *       BUTTON_DrawTextCallback
726  *
727  *   Callback function used by DrawStateW function.
728  */
729 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
730 {
731    RECT rc;
732    rc.left = 0;
733    rc.top = 0;
734    rc.right = cx;
735    rc.bottom = cy;
736
737    DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
738    return TRUE;
739 }
740
741
742 /**********************************************************************
743  *       BUTTON_DrawLabel
744  *
745  *   Common function for drawing button label.
746  */
747 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
748 {
749    DRAWSTATEPROC lpOutputProc = NULL;
750    LPARAM lp;
751    WPARAM wp = 0;
752    HBRUSH hbr = 0;
753    UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
754    LONG state = get_button_state( hwnd );
755    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
756    WCHAR *text = NULL;
757
758    /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
759     * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
760     * I don't have Win31 on hand to verify that, so I leave it as is.
761     */
762
763    if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
764    {
765       hbr = GetSysColorBrush(COLOR_GRAYTEXT);
766       flags |= DSS_MONO;
767    }
768
769    switch (style & (BS_ICON|BS_BITMAP))
770    {
771       case BS_TEXT:
772          /* DST_COMPLEX -- is 0 */
773          lpOutputProc = BUTTON_DrawTextCallback;
774          if (!(text = get_button_text( hwnd ))) return;
775          lp = (LPARAM)text;
776          wp = (WPARAM)dtFlags;
777          break;
778
779       case BS_ICON:
780          flags |= DST_ICON;
781          lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
782          break;
783
784       case BS_BITMAP:
785          flags |= DST_BITMAP;
786          lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
787          break;
788
789       default:
790          return;
791    }
792
793    DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
794               rc->right - rc->left, rc->bottom - rc->top, flags);
795    HeapFree( GetProcessHeap(), 0, text );
796 }
797
798 /**********************************************************************
799  *       Push Button Functions
800  */
801 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
802 {
803     RECT     rc, focus_rect, r;
804     UINT     dtFlags, uState;
805     HPEN     hOldPen;
806     HBRUSH   hOldBrush;
807     INT      oldBkMode;
808     COLORREF oldTxtColor;
809     HFONT hFont;
810     LONG state = get_button_state( hwnd );
811     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
812     BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
813     HWND parent;
814
815     GetClientRect( hwnd, &rc );
816
817     /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
818     if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
819     parent = GetParent(hwnd);
820     if (!parent) parent = hwnd;
821     SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
822
823     setup_clipping( hwnd, hDC );
824
825     hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
826     hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
827     oldBkMode = SetBkMode(hDC, TRANSPARENT);
828
829     if (get_button_type(style) == BS_DEFPUSHBUTTON)
830     {
831         if (action != ODA_FOCUS)
832             Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
833         InflateRect( &rc, -1, -1 );
834     }
835
836     focus_rect = rc;
837
838     /* completely skip the drawing if only focus has changed */
839     if (action == ODA_FOCUS) goto draw_focus;
840
841     uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
842
843     if (style & BS_FLAT)
844         uState |= DFCS_MONO;
845     else if (pushedState)
846     {
847         if (get_button_type(style) == BS_DEFPUSHBUTTON )
848             uState |= DFCS_FLAT;
849         else
850             uState |= DFCS_PUSHED;
851     }
852
853     if (state & (BUTTON_CHECKED | BUTTON_3STATE))
854         uState |= DFCS_CHECKED;
855
856     DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
857
858     /* draw button label */
859     r = rc;
860     dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
861
862     if (dtFlags == (UINT)-1L)
863        goto cleanup;
864
865     if (pushedState)
866        OffsetRect(&r, 1, 1);
867
868     IntersectClipRect(hDC, rc.left, rc.top, rc.right, rc.bottom);
869
870     oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
871
872     BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
873
874     SetTextColor( hDC, oldTxtColor );
875
876 draw_focus:
877     if ((action == ODA_FOCUS) ||
878         ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
879     {
880         InflateRect( &focus_rect, -1, -1 );
881         IntersectRect(&focus_rect, &focus_rect, &rc);
882         DrawFocusRect( hDC, &focus_rect );
883     }
884
885  cleanup:
886     SelectObject( hDC, hOldPen );
887     SelectObject( hDC, hOldBrush );
888     SetBkMode(hDC, oldBkMode);
889 }
890
891 /**********************************************************************
892  *       Check Box & Radio Button Functions
893  */
894
895 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
896 {
897     RECT rbox, rtext, client;
898     HBRUSH hBrush;
899     int delta;
900     UINT dtFlags;
901     HFONT hFont;
902     LONG state = get_button_state( hwnd );
903     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
904     HWND parent;
905
906     if (style & BS_PUSHLIKE)
907     {
908         PB_Paint( hwnd, hDC, action );
909         return;
910     }
911
912     GetClientRect(hwnd, &client);
913     rbox = rtext = client;
914
915     if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
916
917     parent = GetParent(hwnd);
918     if (!parent) parent = hwnd;
919     hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
920                                   (WPARAM)hDC, (LPARAM)hwnd);
921     if (!hBrush) /* did the app forget to call defwindowproc ? */
922         hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
923                                         (WPARAM)hDC, (LPARAM)hwnd );
924     setup_clipping( hwnd, hDC );
925
926     if (style & BS_LEFTTEXT)
927     {
928         /* magic +4 is what CTL3D expects */
929
930         rtext.right -= checkBoxWidth + 4;
931         rbox.left = rbox.right - checkBoxWidth;
932     }
933     else
934     {
935         rtext.left += checkBoxWidth + 4;
936         rbox.right = checkBoxWidth;
937     }
938
939     /* Since WM_ERASEBKGND does nothing, first prepare background */
940     if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
941     if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
942
943     /* Draw label */
944     client = rtext;
945     dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
946     
947     /* Only adjust rbox when rtext is valid */
948     if (dtFlags != (UINT)-1L)
949     {
950         rbox.top = rtext.top;
951         rbox.bottom = rtext.bottom;
952     }
953
954     /* Draw the check-box bitmap */
955     if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
956     {
957         UINT flags;
958
959         if ((get_button_type(style) == BS_RADIOBUTTON) ||
960             (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
961         else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
962         else flags = DFCS_BUTTONCHECK;
963
964         if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
965         if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
966
967         if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
968
969         /* rbox must have the correct height */
970         delta = rbox.bottom - rbox.top - checkBoxHeight;
971         
972         if (style & BS_TOP) {
973             if (delta > 0) {
974                 rbox.bottom = rbox.top + checkBoxHeight;
975             } else { 
976                 rbox.top -= -delta/2 + 1;
977                 rbox.bottom = rbox.top + checkBoxHeight;
978             }
979         } else if (style & BS_BOTTOM) {
980             if (delta > 0) {
981                 rbox.top = rbox.bottom - checkBoxHeight;
982             } else {
983                 rbox.bottom += -delta/2 + 1;
984                 rbox.top = rbox.bottom - checkBoxHeight;
985             }
986         } else { /* Default */
987             if (delta > 0) {
988                 int ofs = (delta / 2);
989                 rbox.bottom -= ofs + 1;
990                 rbox.top = rbox.bottom - checkBoxHeight;
991             } else if (delta < 0) {
992                 int ofs = (-delta / 2);
993                 rbox.top -= ofs + 1;
994                 rbox.bottom = rbox.top + checkBoxHeight;
995             }
996         }
997
998         DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
999     }
1000
1001     if (dtFlags == (UINT)-1L) /* Noting to draw */
1002         return;
1003
1004     if (action == ODA_DRAWENTIRE)
1005         BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
1006
1007     /* ... and focus */
1008     if ((action == ODA_FOCUS) ||
1009         ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1010     {
1011         rtext.left--;
1012         rtext.right++;
1013         IntersectRect(&rtext, &rtext, &client);
1014         DrawFocusRect( hDC, &rtext );
1015     }
1016 }
1017
1018
1019 /**********************************************************************
1020  *       BUTTON_CheckAutoRadioButton
1021  *
1022  * hwnd is checked, uncheck every other auto radio button in group
1023  */
1024 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
1025 {
1026     HWND parent, sibling, start;
1027
1028     parent = GetParent(hwnd);
1029     /* make sure that starting control is not disabled or invisible */
1030     start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1031     do
1032     {
1033         if (!sibling) break;
1034         if ((hwnd != sibling) &&
1035             ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
1036             SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
1037         sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1038     } while (sibling != start);
1039 }
1040
1041
1042 /**********************************************************************
1043  *       Group Box Functions
1044  */
1045
1046 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1047 {
1048     RECT rc, rcFrame;
1049     HBRUSH hbr;
1050     HFONT hFont;
1051     UINT dtFlags;
1052     TEXTMETRICW tm;
1053     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1054     HWND parent;
1055
1056     if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1057     /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1058     parent = GetParent(hwnd);
1059     if (!parent) parent = hwnd;
1060     hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1061     if (!hbr) /* did the app forget to call defwindowproc ? */
1062         hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1063                                      (WPARAM)hDC, (LPARAM)hwnd);
1064     setup_clipping( hwnd, hDC );
1065
1066     GetClientRect( hwnd, &rc);
1067     rcFrame = rc;
1068
1069     GetTextMetricsW (hDC, &tm);
1070     rcFrame.top += (tm.tmHeight / 2) - 1;
1071     DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1072
1073     InflateRect(&rc, -7, 1);
1074     dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1075
1076     if (dtFlags == (UINT)-1L)
1077        return;
1078
1079     /* Because buttons have CS_PARENTDC class style, there is a chance
1080      * that label will be drawn out of client rect.
1081      * But Windows doesn't clip label's rect, so do I.
1082      */
1083
1084     /* There is 1-pixel margin at the left, right, and bottom */
1085     rc.left--; rc.right++; rc.bottom++;
1086     FillRect(hDC, &rc, hbr);
1087     rc.left++; rc.right--; rc.bottom--;
1088
1089     BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1090 }
1091
1092
1093 /**********************************************************************
1094  *       User Button Functions
1095  */
1096
1097 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1098 {
1099     RECT rc;
1100     HBRUSH hBrush;
1101     HFONT hFont;
1102     LONG state = get_button_state( hwnd );
1103     HWND parent;
1104
1105     if (action == ODA_SELECT) return;
1106
1107     GetClientRect( hwnd, &rc);
1108
1109     if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1110
1111     parent = GetParent(hwnd);
1112     if (!parent) parent = hwnd;
1113     hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1114     if (!hBrush) /* did the app forget to call defwindowproc ? */
1115         hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1116                                         (WPARAM)hDC, (LPARAM)hwnd);
1117
1118     FillRect( hDC, &rc, hBrush );
1119     if ((action == ODA_FOCUS) ||
1120         ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1121         DrawFocusRect( hDC, &rc );
1122
1123     BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1124 }
1125
1126
1127 /**********************************************************************
1128  *       Ownerdrawn Button Functions
1129  */
1130
1131 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1132 {
1133     LONG state = get_button_state( hwnd );
1134     DRAWITEMSTRUCT dis;
1135     LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1136     HWND parent;
1137     HFONT hFont, hPrevFont = 0;
1138
1139     dis.CtlType    = ODT_BUTTON;
1140     dis.CtlID      = id;
1141     dis.itemID     = 0;
1142     dis.itemAction = action;
1143     dis.itemState  = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1144                      ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1145                      (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1146     dis.hwndItem   = hwnd;
1147     dis.hDC        = hDC;
1148     dis.itemData   = 0;
1149     GetClientRect( hwnd, &dis.rcItem );
1150
1151     if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1152     parent = GetParent(hwnd);
1153     if (!parent) parent = hwnd;
1154     SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1155
1156     setup_clipping( hwnd, hDC );
1157
1158     SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1159     if (hPrevFont) SelectObject(hDC, hPrevFont);
1160 }