Fixed silly EINTR bug with starting dosmod.
[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 "win.h"
10 #include "button.h"
11 #include "wine/winuser16.h"
12 #include "tweak.h"
13
14 static void PaintGrayOnGray( HDC hDC,HFONT hFont,RECT *rc,
15                              char *text, UINT format );
16
17 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action );
18 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action );
19 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action );
20 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action );
21 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action );
22 static void BUTTON_CheckAutoRadioButton( WND *wndPtr );
23
24 #define MAX_BTN_TYPE  12
25
26 static const WORD maxCheckState[MAX_BTN_TYPE] =
27 {
28     BUTTON_UNCHECKED,   /* BS_PUSHBUTTON */
29     BUTTON_UNCHECKED,   /* BS_DEFPUSHBUTTON */
30     BUTTON_CHECKED,     /* BS_CHECKBOX */
31     BUTTON_CHECKED,     /* BS_AUTOCHECKBOX */
32     BUTTON_CHECKED,     /* BS_RADIOBUTTON */
33     BUTTON_3STATE,      /* BS_3STATE */
34     BUTTON_3STATE,      /* BS_AUTO3STATE */
35     BUTTON_UNCHECKED,   /* BS_GROUPBOX */
36     BUTTON_UNCHECKED,   /* BS_USERBUTTON */
37     BUTTON_CHECKED,     /* BS_AUTORADIOBUTTON */
38     BUTTON_UNCHECKED,   /* Not defined */
39     BUTTON_UNCHECKED    /* BS_OWNERDRAW */
40 };
41
42 typedef void (*pfPaint)( WND *wndPtr, HDC hdc, WORD action );
43
44 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
45 {
46     PB_Paint,    /* BS_PUSHBUTTON */
47     PB_Paint,    /* BS_DEFPUSHBUTTON */
48     CB_Paint,    /* BS_CHECKBOX */
49     CB_Paint,    /* BS_AUTOCHECKBOX */
50     CB_Paint,    /* BS_RADIOBUTTON */
51     CB_Paint,    /* BS_3STATE */
52     CB_Paint,    /* BS_AUTO3STATE */
53     GB_Paint,    /* BS_GROUPBOX */
54     UB_Paint,    /* BS_USERBUTTON */
55     CB_Paint,    /* BS_AUTORADIOBUTTON */
56     NULL,        /* Not defined */
57     OB_Paint     /* BS_OWNERDRAW */
58 };
59
60 #define PAINT_BUTTON(wndPtr,style,action) \
61      if (btnPaintFunc[style]) { \
62          HDC hdc = GetDC( (wndPtr)->hwndSelf ); \
63          (btnPaintFunc[style])(wndPtr,hdc,action); \
64          ReleaseDC( (wndPtr)->hwndSelf, hdc ); }
65
66 #define BUTTON_SEND_CTLCOLOR(wndPtr,hdc) \
67     SendMessageA( GetParent((wndPtr)->hwndSelf), WM_CTLCOLORBTN, \
68                     (hdc), (wndPtr)->hwndSelf )
69
70 static HBITMAP hbitmapCheckBoxes = 0;
71 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
72
73
74 /***********************************************************************
75  *           ButtonWndProc_locked
76  * 
77  * Called with window lock held.
78  */
79 static inline LRESULT WINAPI ButtonWndProc_locked(WND* wndPtr, UINT uMsg,
80                                            WPARAM wParam, LPARAM lParam )
81 {
82     RECT rect;
83     HWND        hWnd = wndPtr->hwndSelf;
84     POINT pt;
85     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
86     LONG style = wndPtr->dwStyle & 0x0f;
87     HANDLE oldHbitmap;
88
89     pt.x = LOWORD(lParam);
90     pt.y = HIWORD(lParam);
91
92     switch (uMsg)
93     {
94     case WM_GETDLGCODE:
95         switch(style)
96         {
97         case BS_PUSHBUTTON:      return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
98         case BS_DEFPUSHBUTTON:   return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
99         case BS_RADIOBUTTON:
100         case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
101         default:                 return DLGC_BUTTON;
102         }
103
104     case WM_ENABLE:
105         PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
106         break;
107
108     case WM_CREATE:
109         if (!hbitmapCheckBoxes)
110         {
111             BITMAP bmp;
112             hbitmapCheckBoxes = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_CHECKBOXES));
113             GetObjectA( hbitmapCheckBoxes, sizeof(bmp), &bmp );
114             checkBoxWidth  = bmp.bmWidth / 4;
115             checkBoxHeight = bmp.bmHeight / 3;
116         }
117         if (style < 0L || style >= MAX_BTN_TYPE)
118             return -1; /* abort */
119         infoPtr->state = BUTTON_UNCHECKED;
120         infoPtr->hFont = 0;
121         infoPtr->hImage = NULL;
122         return 0;
123
124     case WM_ERASEBKGND:
125         return 1;
126
127     case WM_PAINT:
128         if (btnPaintFunc[style])
129         {
130             PAINTSTRUCT ps;
131             HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
132             SetBkMode( hdc, OPAQUE );
133             (btnPaintFunc[style])( wndPtr, hdc, ODA_DRAWENTIRE );
134             if( !wParam ) EndPaint( hWnd, &ps );
135         }
136         break;
137
138     case WM_LBUTTONDOWN:
139     case WM_LBUTTONDBLCLK:
140         SendMessageA( hWnd, BM_SETSTATE, TRUE, 0 );
141         SetFocus( hWnd );
142         SetCapture( hWnd );
143         break;
144
145     case WM_LBUTTONUP:
146         ReleaseCapture();
147         if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) break;
148         SendMessageA( hWnd, BM_SETSTATE, FALSE, 0 );
149         GetClientRect( hWnd, &rect );
150         if (PtInRect( &rect, pt ))
151         {
152             switch(style)
153             {
154             case BS_AUTOCHECKBOX:
155                 SendMessageA( hWnd, BM_SETCHECK,
156                                 !(infoPtr->state & BUTTON_CHECKED), 0 );
157                 break;
158             case BS_AUTORADIOBUTTON:
159                 SendMessageA( hWnd, BM_SETCHECK, TRUE, 0 );
160                 break;
161             case BS_AUTO3STATE:
162                 SendMessageA( hWnd, BM_SETCHECK,
163                                 (infoPtr->state & BUTTON_3STATE) ? 0 :
164                                 ((infoPtr->state & 3) + 1), 0 );
165                 break;
166             }
167             SendMessageA( GetParent(hWnd), WM_COMMAND,
168                             MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
169         }
170         break;
171
172     case WM_MOUSEMOVE:
173         if (GetCapture() == hWnd)
174         {
175             GetClientRect( hWnd, &rect );
176             SendMessageA( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
177         }
178         break;
179
180     case WM_NCHITTEST:
181         if(style == BS_GROUPBOX) return HTTRANSPARENT;
182         return DefWindowProcA( hWnd, uMsg, wParam, lParam );
183
184     case WM_SETTEXT:
185         DEFWND_SetText( wndPtr, (LPCSTR)lParam );
186         if( wndPtr->dwStyle & WS_VISIBLE )
187             PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
188         return 0;
189
190     case WM_SETFONT:
191         infoPtr->hFont = (HFONT16)wParam;
192         if (lParam && (wndPtr->dwStyle & WS_VISIBLE)) 
193             PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
194         break;
195
196     case WM_GETFONT:
197         return infoPtr->hFont;
198
199     case WM_SETFOCUS:
200         infoPtr->state |= BUTTON_HASFOCUS;
201         if (style == BS_AUTORADIOBUTTON)
202             SendMessageA( hWnd, BM_SETCHECK, 1, 0 );
203         PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
204         break;
205
206     case WM_KILLFOCUS:
207         infoPtr->state &= ~BUTTON_HASFOCUS;
208         PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
209         InvalidateRect( hWnd, NULL, TRUE );
210         break;
211
212     case WM_SYSCOLORCHANGE:
213         InvalidateRect( hWnd, NULL, FALSE );
214         break;
215
216     case BM_SETSTYLE16:
217     case BM_SETSTYLE:
218         if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
219         wndPtr->dwStyle = (wndPtr->dwStyle & 0xfffffff0) 
220                            | (wParam & 0x0000000f);
221         style = wndPtr->dwStyle & 0x0000000f;
222         PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
223         break;
224
225     case BM_SETIMAGE:
226         oldHbitmap = infoPtr->hImage;
227         if(wndPtr->dwStyle & BS_BITMAP)
228             infoPtr->hImage = (HANDLE) lParam;
229         return oldHbitmap;
230
231     case BM_GETIMAGE:
232         return infoPtr->hImage;
233
234     case BM_GETCHECK16:
235     case BM_GETCHECK:
236         return infoPtr->state & 3;
237
238     case BM_SETCHECK16:
239     case BM_SETCHECK:
240         if (wParam > maxCheckState[style]) wParam = maxCheckState[style];
241         if ((infoPtr->state & 3) != wParam)
242         {
243             if ((style == BS_RADIOBUTTON) || (style == BS_AUTORADIOBUTTON))
244             {
245                 if (wParam)
246                     wndPtr->dwStyle |= WS_TABSTOP;
247                 else
248                     wndPtr->dwStyle &= ~WS_TABSTOP;
249             }
250             infoPtr->state = (infoPtr->state & ~3) | wParam;
251             PAINT_BUTTON( wndPtr, style, ODA_SELECT );
252         }
253         if ((style == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED))
254             BUTTON_CheckAutoRadioButton( wndPtr );
255         break;
256
257     case BM_GETSTATE16:
258     case BM_GETSTATE:
259         return infoPtr->state;
260
261     case BM_SETSTATE16:
262     case BM_SETSTATE:
263         if (wParam)
264         {
265             if (infoPtr->state & BUTTON_HIGHLIGHTED) break;
266             infoPtr->state |= BUTTON_HIGHLIGHTED;
267         }
268         else
269         {
270             if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) break;
271             infoPtr->state &= ~BUTTON_HIGHLIGHTED;
272         }
273         PAINT_BUTTON( wndPtr, style, ODA_SELECT );
274         break;
275
276     default:
277         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
278     }
279     return 0;
280 }
281
282 /***********************************************************************
283  *           ButtonWndProc
284  * The button window procedure. This is just a wrapper which locks
285  * the passed HWND and calls the real window procedure (with a WND*
286  * pointer pointing to the locked windowstructure).
287  */
288 LRESULT WINAPI ButtonWndProc( HWND hWnd, UINT uMsg,
289                               WPARAM wParam, LPARAM lParam )
290 {
291     LRESULT res;
292     WND *wndPtr = WIN_FindWndPtr(hWnd);
293
294     res = ButtonWndProc_locked(wndPtr,uMsg,wParam,lParam);
295
296     WIN_ReleaseWndPtr(wndPtr);
297     return res;
298 }
299
300 /**********************************************************************
301  *       Push Button Functions
302  */
303
304 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action )
305 {
306     RECT rc;
307     HPEN hOldPen;
308     HBRUSH hOldBrush;
309     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
310     int xBorderOffset, yBorderOffset;
311     xBorderOffset = yBorderOffset = 0;
312
313     GetClientRect( wndPtr->hwndSelf, &rc );
314
315       /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
316     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
317     BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
318     hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
319     hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
320     SetBkMode(hDC, TRANSPARENT);
321     Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
322     if (TWEAK_WineLook == WIN31_LOOK)
323     {
324         SetPixel( hDC, rc.left, rc.top, GetSysColor(COLOR_WINDOW) );
325         SetPixel( hDC, rc.left, rc.bottom-1, GetSysColor(COLOR_WINDOW) );
326         SetPixel( hDC, rc.right-1, rc.top, GetSysColor(COLOR_WINDOW) );
327         SetPixel( hDC, rc.right-1, rc.bottom-1, GetSysColor(COLOR_WINDOW));
328     }
329     InflateRect( &rc, -1, -1 );
330
331     if ((wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON)
332     {
333         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
334         InflateRect( &rc, -1, -1 );
335     }
336
337     if (infoPtr->state & BUTTON_HIGHLIGHTED)
338     {
339         /* draw button shadow: */
340         SelectObject(hDC, GetSysColorBrush(COLOR_BTNSHADOW));
341         PatBlt(hDC, rc.left, rc.top, 1, rc.bottom-rc.top, PATCOPY );
342         PatBlt(hDC, rc.left, rc.top, rc.right-rc.left, 1, PATCOPY );
343         rc.left += 2;  /* To position the text down and right */
344         rc.top  += 2;
345     } else {
346         rc.right++, rc.bottom++;
347         DrawEdge( hDC, &rc, EDGE_RAISED, BF_RECT );
348
349         /* To place de bitmap correctly */
350         xBorderOffset += GetSystemMetrics(SM_CXEDGE);
351         yBorderOffset += GetSystemMetrics(SM_CYEDGE);
352
353         rc.right--, rc.bottom--;
354     }
355         
356     /* draw button label, if any: */
357     if (wndPtr->text && wndPtr->text[0])
358     {
359         LOGBRUSH lb;
360         GetObjectA( GetSysColorBrush(COLOR_BTNFACE), sizeof(lb), &lb );
361         if (wndPtr->dwStyle & WS_DISABLED &&
362             GetSysColor(COLOR_GRAYTEXT)==lb.lbColor)
363             /* don't write gray text on gray background */
364             PaintGrayOnGray( hDC,infoPtr->hFont,&rc,wndPtr->text,
365                                DT_CENTER | DT_VCENTER );
366         else
367         {
368             SetTextColor( hDC, (wndPtr->dwStyle & WS_DISABLED) ?
369                                  GetSysColor(COLOR_GRAYTEXT) :
370                                  GetSysColor(COLOR_BTNTEXT) );
371             DrawTextA( hDC, wndPtr->text, -1, &rc,
372                          DT_SINGLELINE | DT_CENTER | DT_VCENTER );
373             /* do we have the focus? */
374             if (infoPtr->state & BUTTON_HASFOCUS)
375             {
376                 RECT r = { 0, 0, 0, 0 };
377                 INT xdelta, ydelta;
378
379                 DrawTextA( hDC, wndPtr->text, -1, &r,
380                              DT_SINGLELINE | DT_CALCRECT );
381                 xdelta = ((rc.right - rc.left) - (r.right - r.left) - 1) / 2;
382                 ydelta = ((rc.bottom - rc.top) - (r.bottom - r.top) - 1) / 2;
383                 if (xdelta < 0) xdelta = 0;
384                 if (ydelta < 0) ydelta = 0;
385                 InflateRect( &rc, -xdelta, -ydelta );
386                 DrawFocusRect( hDC, &rc );
387             }
388         }   
389     }
390
391     if((wndPtr->dwStyle & BS_BITMAP) && (infoPtr->hImage != NULL))
392     {
393         BITMAP bm;
394         HDC hdcMem;
395         int yOffset, xOffset, imageWidth, imageHeight;
396
397         GetObjectA (infoPtr->hImage, sizeof(BITMAP), &bm);
398         
399         /* Center the bitmap */
400         xOffset = (((rc.right - rc.left) - 2*xBorderOffset) - bm.bmWidth ) / 2;
401         yOffset = (((rc.bottom - rc.top) - 2*yBorderOffset) - bm.bmHeight ) / 2;
402
403         imageWidth = bm.bmWidth;
404         imageHeight = bm.bmHeight;
405
406         /* If the image is to big for the button */
407         if (xOffset < 0)
408         {
409             imageWidth = rc.right - rc.left - 2*xBorderOffset -1;
410             xOffset = xBorderOffset;
411         }
412
413         if (yOffset < 0)
414         {
415             imageHeight = rc.bottom - rc.top - 2*yBorderOffset -1;
416             yOffset = yBorderOffset;
417         }
418
419         /* Let minimum 1 space from border */
420         xOffset++, yOffset++;
421
422         hdcMem = CreateCompatibleDC (hDC);
423         SelectObject (hdcMem, (HBITMAP)infoPtr->hImage);
424         BitBlt(hDC, rc.left + xOffset, 
425                rc.top + yOffset, 
426                imageWidth, imageHeight,
427                hdcMem, 0, 0, SRCCOPY);
428
429         DeleteDC (hdcMem);
430     }
431     
432     SelectObject( hDC, hOldPen );
433     SelectObject( hDC, hOldBrush );
434 }
435
436
437 /**********************************************************************
438  *   PB_Paint & CB_Paint sub function                        [internal]
439  *   Paint text using a raster brush to avoid gray text on gray 
440  *   background. 'format' can be a combination of DT_CENTER and 
441  *   DT_VCENTER to use this function in both PB_PAINT and 
442  *   CB_PAINT.   - Dirk Thierbach
443  *
444  *   FIXME: This and TEXT_GrayString should be eventually combined,
445  *   so calling one common function from PB_Paint, CB_Paint and
446  *   TEXT_GrayString will be enough. Also note that this
447  *   function ignores the CACHE_GetPattern funcs.
448  */
449
450 void PaintGrayOnGray(HDC hDC,HFONT hFont,RECT *rc,char *text,
451                         UINT format)
452 {
453 /*  This is the standard gray on gray pattern:
454     static const WORD Pattern[] = {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55}; 
455 */
456 /*  This pattern gives better readability with X Fonts.
457     FIXME: Maybe the user should be allowed to decide which he wants. */
458     static const WORD Pattern[] = {0x55,0xFF,0xAA,0xFF,0x55,0xFF,0xAA,0xFF}; 
459
460     HBITMAP hbm  = CreateBitmap( 8, 8, 1, 1, Pattern );
461     HDC hdcMem = CreateCompatibleDC(hDC);
462     HBITMAP hbmMem;
463     HBRUSH hBr;
464     RECT rect,rc2;
465
466     rect=*rc;
467     DrawTextA( hDC, text, -1, &rect, DT_SINGLELINE | DT_CALCRECT);
468     /* now text width and height are in rect.right and rect.bottom */
469     rc2=rect;
470     rect.left = rect.top = 0; /* drawing pos in hdcMem */
471     if (format & DT_CENTER) rect.left=(rc->right-rect.right)/2;
472     if (format & DT_VCENTER) rect.top=(rc->bottom-rect.bottom)/2;
473     hbmMem = CreateCompatibleBitmap( hDC,rect.right,rect.bottom );
474     SelectObject( hdcMem, hbmMem);
475     PatBlt( hdcMem,0,0,rect.right,rect.bottom,WHITENESS);
476       /* will be overwritten by DrawText, but just in case */
477     if (hFont) SelectObject( hdcMem, hFont);
478     DrawTextA( hdcMem, text, -1, &rc2, DT_SINGLELINE);  
479       /* After draw: foreground = 0 bits, background = 1 bits */
480     hBr = SelectObject( hdcMem, CreatePatternBrush(hbm) );
481     DeleteObject( hbm );
482     PatBlt( hdcMem,0,0,rect.right,rect.bottom,0xAF0229); 
483       /* only keep the foreground bits where pattern is 1 */
484     DeleteObject( SelectObject( hdcMem,hBr) );
485     BitBlt(hDC,rect.left,rect.top,rect.right,rect.bottom,hdcMem,0,0,SRCAND);
486       /* keep the background of the dest */
487     DeleteDC( hdcMem);
488     DeleteObject( hbmMem );
489 }
490
491
492 /**********************************************************************
493  *       Check Box & Radio Button Functions
494  */
495
496 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action )
497 {
498     RECT rbox, rtext, client;
499     HBRUSH hBrush;
500     int textlen, delta;
501     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
502
503     textlen = 0;
504     GetClientRect(wndPtr->hwndSelf, &client);
505     rbox = rtext = client;
506
507     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
508
509     /* Something is still not right, checkboxes (and edit controls)
510      * in wsping32 have white backgrounds instead of dark grey.
511      * BUTTON_SEND_CTLCOLOR() is even worse since it returns 0 in this
512      * particular case and the background is not painted at all.
513      */
514
515     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
516
517     if (wndPtr->dwStyle & BS_LEFTTEXT) 
518     {
519         /* magic +4 is what CTL3D expects */
520
521         rtext.right -= checkBoxWidth + 4;
522         rbox.left = rbox.right - checkBoxWidth;
523     }
524     else 
525     {
526         rtext.left += checkBoxWidth + 4;
527         rbox.right = checkBoxWidth;
528     }
529
530       /* Draw the check-box bitmap */
531
532     if (wndPtr->text) textlen = strlen( wndPtr->text );
533     if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
534     { 
535         HDC hMemDC = CreateCompatibleDC( hDC );
536         int x = 0, y = 0;
537         delta = (rbox.bottom - rbox.top - checkBoxHeight) >> 1;
538
539         if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
540         else FillRect( hDC, &client, hBrush );
541
542         if (infoPtr->state & BUTTON_HIGHLIGHTED) x += 2 * checkBoxWidth;
543         if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) x += checkBoxWidth;
544         if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
545             ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) y += checkBoxHeight;
546         else if (infoPtr->state & BUTTON_3STATE) y += 2 * checkBoxHeight;
547
548         SelectObject( hMemDC, hbitmapCheckBoxes );
549         BitBlt( hDC, rbox.left, rbox.top + delta, checkBoxWidth,
550                   checkBoxHeight, hMemDC, x, y, SRCCOPY );
551         DeleteDC( hMemDC );
552
553         if( textlen && action != ODA_SELECT )
554         {
555           if (wndPtr->dwStyle & WS_DISABLED &&
556               GetSysColor(COLOR_GRAYTEXT)==GetBkColor(hDC)) {
557             /* don't write gray text on gray background */
558             PaintGrayOnGray( hDC, infoPtr->hFont, &rtext, wndPtr->text,
559                              DT_VCENTER);
560           } else {
561             if (wndPtr->dwStyle & WS_DISABLED)
562                 SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
563             DrawTextA( hDC, wndPtr->text, textlen, &rtext,
564                          DT_SINGLELINE | DT_VCENTER );
565             textlen = 0; /* skip DrawText() below */
566           }
567         }
568     }
569
570     if ((action == ODA_FOCUS) ||
571         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
572     {
573         /* again, this is what CTL3D expects */
574
575         SetRectEmpty(&rbox);
576         if( textlen )
577             DrawTextA( hDC, wndPtr->text, textlen, &rbox,
578                          DT_SINGLELINE | DT_CALCRECT );
579         textlen = rbox.bottom - rbox.top;
580         delta = ((rtext.bottom - rtext.top) - textlen)/2;
581         rbox.bottom = (rbox.top = rtext.top + delta - 1) + textlen + 2;
582         textlen = rbox.right - rbox.left;
583         rbox.right = (rbox.left += --rtext.left) + textlen + 2;
584         IntersectRect(&rbox, &rbox, &rtext);
585         DrawFocusRect( hDC, &rbox );
586     }
587 }
588
589
590 /**********************************************************************
591  *       BUTTON_CheckAutoRadioButton
592  *
593  * wndPtr is checked, uncheck every other auto radio button in group
594  */
595 static void BUTTON_CheckAutoRadioButton( WND *wndPtr )
596 {
597     HWND parent, sibling, start;
598     if (!(wndPtr->dwStyle & WS_CHILD)) return;
599     parent = wndPtr->parent->hwndSelf;
600     /* assure that starting control is not disabled or invisible */
601     start = sibling = GetNextDlgGroupItem( parent, wndPtr->hwndSelf, TRUE );
602     do
603     {
604         WND *tmpWnd;
605         if (!sibling) break;
606         tmpWnd = WIN_FindWndPtr(sibling);
607         if ((wndPtr->hwndSelf != sibling) &&
608             ((tmpWnd->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
609             SendMessageA( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
610         sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
611         WIN_ReleaseWndPtr(tmpWnd);
612     } while (sibling != start);
613 }
614
615
616 /**********************************************************************
617  *       Group Box Functions
618  */
619
620 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action )
621 {
622     RECT rc, rcFrame;
623     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
624
625     if (action != ODA_DRAWENTIRE) return;
626
627     BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
628
629     GetClientRect( wndPtr->hwndSelf, &rc);
630     if (TWEAK_WineLook == WIN31_LOOK) {
631         HPEN hPrevPen = SelectObject( hDC,
632                                           GetSysColorPen(COLOR_WINDOWFRAME));
633         HBRUSH hPrevBrush = SelectObject( hDC,
634                                               GetStockObject(NULL_BRUSH) );
635
636         Rectangle( hDC, rc.left, rc.top + 2, rc.right - 1, rc.bottom - 1 );
637         SelectObject( hDC, hPrevBrush );
638         SelectObject( hDC, hPrevPen );
639     } else {
640         TEXTMETRICA tm;
641         rcFrame = rc;
642
643         if (infoPtr->hFont)
644             SelectObject (hDC, infoPtr->hFont);
645         GetTextMetricsA (hDC, &tm);
646         rcFrame.top += (tm.tmHeight / 2) - 1;
647         DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT);
648     }
649
650     if (wndPtr->text)
651     {
652         if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
653         if (wndPtr->dwStyle & WS_DISABLED)
654             SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
655         rc.left += 10;
656         DrawTextA( hDC, wndPtr->text, -1, &rc, DT_SINGLELINE | DT_NOCLIP );
657     }
658 }
659
660
661 /**********************************************************************
662  *       User Button Functions
663  */
664
665 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action )
666 {
667     RECT rc;
668     HBRUSH hBrush;
669     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
670
671     if (action == ODA_SELECT) return;
672
673     GetClientRect( wndPtr->hwndSelf, &rc);
674
675     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
676     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
677
678     FillRect( hDC, &rc, hBrush );
679     if ((action == ODA_FOCUS) ||
680         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
681         DrawFocusRect( hDC, &rc );
682 }
683
684
685 /**********************************************************************
686  *       Ownerdrawn Button Functions
687  */
688
689 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action )
690 {
691     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
692     DRAWITEMSTRUCT dis;
693
694     dis.CtlType    = ODT_BUTTON;
695     dis.CtlID      = wndPtr->wIDmenu;
696     dis.itemID     = 0;
697     dis.itemAction = action;
698     dis.itemState  = ((infoPtr->state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
699                      ((infoPtr->state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
700                      ((wndPtr->dwStyle & WS_DISABLED) ? ODS_DISABLED : 0);
701     dis.hwndItem   = wndPtr->hwndSelf;
702     dis.hDC        = hDC;
703     dis.itemData   = 0;
704     GetClientRect( wndPtr->hwndSelf, &dis.rcItem );
705     SendMessageA( GetParent(wndPtr->hwndSelf), WM_DRAWITEM,
706                     wndPtr->wIDmenu, (LPARAM)&dis );
707 }
708