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