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