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