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