In win32 a WM_MENUSELECT message should contain the position when the
[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 static void BUTTON_DrawPushButton( WND *wndPtr, HDC hDC, WORD action, BOOL pushedState);
24
25 #define MAX_BTN_TYPE  12
26
27 static const WORD maxCheckState[MAX_BTN_TYPE] =
28 {
29     BUTTON_UNCHECKED,   /* BS_PUSHBUTTON */
30     BUTTON_UNCHECKED,   /* BS_DEFPUSHBUTTON */
31     BUTTON_CHECKED,     /* BS_CHECKBOX */
32     BUTTON_CHECKED,     /* BS_AUTOCHECKBOX */
33     BUTTON_CHECKED,     /* BS_RADIOBUTTON */
34     BUTTON_3STATE,      /* BS_3STATE */
35     BUTTON_3STATE,      /* BS_AUTO3STATE */
36     BUTTON_UNCHECKED,   /* BS_GROUPBOX */
37     BUTTON_UNCHECKED,   /* BS_USERBUTTON */
38     BUTTON_CHECKED,     /* BS_AUTORADIOBUTTON */
39     BUTTON_UNCHECKED,   /* Not defined */
40     BUTTON_UNCHECKED    /* BS_OWNERDRAW */
41 };
42
43 typedef void (*pfPaint)( WND *wndPtr, HDC hdc, WORD action );
44
45 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
46 {
47     PB_Paint,    /* BS_PUSHBUTTON */
48     PB_Paint,    /* BS_DEFPUSHBUTTON */
49     CB_Paint,    /* BS_CHECKBOX */
50     CB_Paint,    /* BS_AUTOCHECKBOX */
51     CB_Paint,    /* BS_RADIOBUTTON */
52     CB_Paint,    /* BS_3STATE */
53     CB_Paint,    /* BS_AUTO3STATE */
54     GB_Paint,    /* BS_GROUPBOX */
55     UB_Paint,    /* BS_USERBUTTON */
56     CB_Paint,    /* BS_AUTORADIOBUTTON */
57     NULL,        /* Not defined */
58     OB_Paint     /* BS_OWNERDRAW */
59 };
60
61 #define PAINT_BUTTON(wndPtr,style,action) \
62      if (btnPaintFunc[style]) { \
63          HDC hdc = GetDC( (wndPtr)->hwndSelf ); \
64          (btnPaintFunc[style])(wndPtr,hdc,action); \
65          ReleaseDC( (wndPtr)->hwndSelf, hdc ); }
66
67 #define BUTTON_SEND_CTLCOLOR(wndPtr,hdc) \
68     SendMessageA( GetParent((wndPtr)->hwndSelf), WM_CTLCOLORBTN, \
69                     (hdc), (wndPtr)->hwndSelf )
70
71 static HBITMAP hbitmapCheckBoxes = 0;
72 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
73
74
75 /***********************************************************************
76  *           ButtonWndProc_locked
77  * 
78  * Called with window lock held.
79  */
80 static inline LRESULT WINAPI ButtonWndProc_locked(WND* wndPtr, UINT uMsg,
81                                            WPARAM wParam, LPARAM lParam )
82 {
83     RECT rect;
84     HWND        hWnd = wndPtr->hwndSelf;
85     POINT pt;
86     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
87     LONG style = wndPtr->dwStyle & 0x0f;
88     HANDLE oldHbitmap;
89
90     pt.x = LOWORD(lParam);
91     pt.y = HIWORD(lParam);
92
93     switch (uMsg)
94     {
95     case WM_GETDLGCODE:
96         switch(style)
97         {
98         case BS_PUSHBUTTON:      return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
99         case BS_DEFPUSHBUTTON:   return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
100         case BS_RADIOBUTTON:
101         case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
102         default:                 return DLGC_BUTTON;
103         }
104
105     case WM_ENABLE:
106         PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
107         break;
108
109     case WM_CREATE:
110         if (!hbitmapCheckBoxes)
111         {
112             BITMAP bmp;
113             hbitmapCheckBoxes = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_CHECKBOXES));
114             GetObjectA( hbitmapCheckBoxes, sizeof(bmp), &bmp );
115             checkBoxWidth  = bmp.bmWidth / 4;
116             checkBoxHeight = bmp.bmHeight / 3;
117         }
118         if (style < 0L || style >= MAX_BTN_TYPE)
119             return -1; /* abort */
120         infoPtr->state = BUTTON_UNCHECKED;
121         infoPtr->hFont = 0;
122         infoPtr->hImage = 0;
123         return 0;
124
125     case WM_ERASEBKGND:
126         return 1;
127
128     case WM_PAINT:
129         if (btnPaintFunc[style])
130         {
131             PAINTSTRUCT ps;
132             HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
133             SetBkMode( hdc, OPAQUE );
134             (btnPaintFunc[style])( wndPtr, hdc, ODA_DRAWENTIRE );
135             if( !wParam ) EndPaint( hWnd, &ps );
136         }
137         break;
138
139     case WM_KEYDOWN:
140         if (wParam == VK_SPACE)
141         {
142             SendMessageA( hWnd, BM_SETSTATE, TRUE, 0 );
143             infoPtr->state |= BUTTON_BTNPRESSED;
144         }
145         break;
146         
147     case WM_LBUTTONDBLCLK:
148         if(wndPtr->dwStyle & BS_NOTIFY || 
149                 style==BS_RADIOBUTTON ||
150                 style==BS_USERBUTTON ||
151                 style==BS_OWNERDRAW) {
152             SendMessageA( GetParent(hWnd), WM_COMMAND,
153                     MAKEWPARAM( wndPtr->wIDmenu, BN_DOUBLECLICKED ), hWnd);
154             break;
155         }
156         /* fall through */
157     case WM_LBUTTONDOWN:
158         SetCapture( hWnd );
159         SetFocus( hWnd );
160         SendMessageA( hWnd, BM_SETSTATE, TRUE, 0 );
161         infoPtr->state |= BUTTON_BTNPRESSED;
162         break;
163
164     case WM_KEYUP:
165         if (wParam != VK_SPACE)
166             break;
167         /* fall through */
168     case WM_LBUTTONUP:
169         if (!(infoPtr->state & BUTTON_BTNPRESSED)) break;
170         infoPtr->state &= BUTTON_NSTATES;
171         if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) {
172             ReleaseCapture();
173             break;
174         }
175         SendMessageA( hWnd, BM_SETSTATE, FALSE, 0 );
176         ReleaseCapture();
177         GetClientRect( hWnd, &rect );
178         if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
179         {
180             switch(style)
181             {
182             case BS_AUTOCHECKBOX:
183                 SendMessageA( hWnd, BM_SETCHECK,
184                                 !(infoPtr->state & BUTTON_CHECKED), 0 );
185                 break;
186             case BS_AUTORADIOBUTTON:
187                 SendMessageA( hWnd, BM_SETCHECK, TRUE, 0 );
188                 break;
189             case BS_AUTO3STATE:
190                 SendMessageA( hWnd, BM_SETCHECK,
191                                 (infoPtr->state & BUTTON_3STATE) ? 0 :
192                                 ((infoPtr->state & 3) + 1), 0 );
193                 break;
194             }
195             SendMessageA( GetParent(hWnd), WM_COMMAND,
196                             MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
197         }
198         break;
199
200     case WM_CAPTURECHANGED:
201         if (infoPtr->state & BUTTON_BTNPRESSED) {
202             infoPtr->state &= BUTTON_NSTATES;
203             if (infoPtr->state & BUTTON_HIGHLIGHTED) 
204                 SendMessageA( hWnd, BM_SETSTATE, FALSE, 0 );
205         }
206         break;
207
208     case WM_MOUSEMOVE:
209         if (GetCapture() == hWnd)
210         {
211             GetClientRect( hWnd, &rect );
212             SendMessageA( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
213         }
214         break;
215
216     case WM_NCHITTEST:
217         if(style == BS_GROUPBOX) return HTTRANSPARENT;
218         return DefWindowProcA( hWnd, uMsg, wParam, lParam );
219
220     case WM_SETTEXT:
221         DEFWND_SetText( wndPtr, (LPCSTR)lParam );
222         if( wndPtr->dwStyle & WS_VISIBLE )
223             PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
224         return 0;
225
226     case WM_SETFONT:
227         infoPtr->hFont = (HFONT16)wParam;
228         if (lParam && (wndPtr->dwStyle & WS_VISIBLE)) 
229             PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
230         break;
231
232     case WM_GETFONT:
233         return infoPtr->hFont;
234
235     case WM_SETFOCUS:
236         if ((style == BS_AUTORADIOBUTTON) && (GetCapture() != hWnd) &&
237             !(SendMessageA(hWnd, BM_GETCHECK, 0, 0) & BST_CHECKED))
238         {
239             /* The notification is sent when the button (BS_AUTORADIOBUTTON) 
240                is unckecked and the focus was not given by a mouse click. */
241             SendMessageA( hWnd, BM_SETCHECK, TRUE, 0 );
242             SendMessageA( GetParent(hWnd), WM_COMMAND,
243                           MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
244         }
245         infoPtr->state |= BUTTON_HASFOCUS;
246         PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
247         break;
248
249     case WM_KILLFOCUS:
250         infoPtr->state &= ~BUTTON_HASFOCUS;
251         PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
252         InvalidateRect( hWnd, NULL, TRUE );
253         break;
254
255     case WM_SYSCOLORCHANGE:
256         InvalidateRect( hWnd, NULL, FALSE );
257         break;
258
259     case BM_SETSTYLE16:
260     case BM_SETSTYLE:
261         if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
262         wndPtr->dwStyle = (wndPtr->dwStyle & 0xfffffff0) 
263                            | (wParam & 0x0000000f);
264         style = wndPtr->dwStyle & 0x0000000f;
265         PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
266         break;
267
268     case BM_SETIMAGE:
269         oldHbitmap = infoPtr->hImage;
270         if ((wndPtr->dwStyle & BS_BITMAP) || (wndPtr->dwStyle & BS_ICON))
271             infoPtr->hImage = (HANDLE) lParam;
272         return oldHbitmap;
273
274     case BM_GETIMAGE:
275         if (wParam == IMAGE_BITMAP)
276             return (HBITMAP)infoPtr->hImage;
277         else if (wParam == IMAGE_ICON)
278             return (HICON)infoPtr->hImage;
279         else
280             return (HICON)0;
281
282     case BM_GETCHECK16:
283     case BM_GETCHECK:
284         return infoPtr->state & 3;
285
286     case BM_SETCHECK16:
287     case BM_SETCHECK:
288         if (wParam > maxCheckState[style]) wParam = maxCheckState[style];
289         if ((infoPtr->state & 3) != wParam)
290         {
291             if ((style == BS_RADIOBUTTON) || (style == BS_AUTORADIOBUTTON))
292             {
293                 if (wParam)
294                     wndPtr->dwStyle |= WS_TABSTOP;
295                 else
296                     wndPtr->dwStyle &= ~WS_TABSTOP;
297             }
298             infoPtr->state = (infoPtr->state & ~3) | wParam;
299             PAINT_BUTTON( wndPtr, style, ODA_SELECT );
300         }
301         if ((style == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED))
302             BUTTON_CheckAutoRadioButton( wndPtr );
303         break;
304
305     case BM_GETSTATE16:
306     case BM_GETSTATE:
307         return infoPtr->state;
308
309     case BM_SETSTATE16:
310     case BM_SETSTATE:
311         if (wParam)
312         {
313             if (infoPtr->state & BUTTON_HIGHLIGHTED) break;
314             infoPtr->state |= BUTTON_HIGHLIGHTED;
315         }
316         else
317         {
318             if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) break;
319             infoPtr->state &= ~BUTTON_HIGHLIGHTED;
320         }
321         PAINT_BUTTON( wndPtr, style, ODA_SELECT );
322         break;
323
324     default:
325         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
326     }
327     return 0;
328 }
329
330 /***********************************************************************
331  *           ButtonWndProc
332  * The button window procedure. This is just a wrapper which locks
333  * the passed HWND and calls the real window procedure (with a WND*
334  * pointer pointing to the locked windowstructure).
335  */
336 LRESULT WINAPI ButtonWndProc( HWND hWnd, UINT uMsg,
337                               WPARAM wParam, LPARAM lParam )
338 {
339     LRESULT res;
340     WND *wndPtr = WIN_FindWndPtr(hWnd);
341
342     res = ButtonWndProc_locked(wndPtr,uMsg,wParam,lParam);
343
344     WIN_ReleaseWndPtr(wndPtr);
345     return res;
346 }
347
348 /**********************************************************************
349  *       Push Button Functions
350  */
351 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action )
352 {
353     BUTTONINFO *infoPtr      = (BUTTONINFO *)wndPtr->wExtra;
354     BOOL        bHighLighted = (infoPtr->state & BUTTON_HIGHLIGHTED);
355
356     /* 
357      * Delegate this to the more generic pushbutton painting
358      * method.
359      */
360     BUTTON_DrawPushButton(wndPtr,
361                           hDC,
362                           action,
363                           bHighLighted);
364 }
365
366 /**********************************************************************
367  * This method will actually do the drawing of the pushbutton 
368  * depending on it's state and the pushedState parameter.
369  */
370 static void BUTTON_DrawPushButton(
371   WND* wndPtr,
372   HDC  hDC, 
373   WORD action, 
374   BOOL pushedState )
375 {
376     RECT rc, focus_rect;
377     HPEN hOldPen;
378     HBRUSH hOldBrush;
379     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
380     int xBorderOffset, yBorderOffset;
381     xBorderOffset = yBorderOffset = 0;
382
383     GetClientRect( wndPtr->hwndSelf, &rc );
384
385       /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
386     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
387     BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
388     hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
389     hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
390     SetBkMode(hDC, TRANSPARENT);
391
392     if ( TWEAK_WineLook == WIN31_LOOK)
393     {
394         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
395
396         SetPixel( hDC, rc.left, rc.top, GetSysColor(COLOR_WINDOW) );
397         SetPixel( hDC, rc.left, rc.bottom-1, GetSysColor(COLOR_WINDOW) );
398         SetPixel( hDC, rc.right-1, rc.top, GetSysColor(COLOR_WINDOW) );
399         SetPixel( hDC, rc.right-1, rc.bottom-1, GetSysColor(COLOR_WINDOW));
400         InflateRect( &rc, -1, -1 );
401     }
402     
403     if ((wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON)
404     {
405         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
406         InflateRect( &rc, -1, -1 );
407     }
408
409     if (TWEAK_WineLook == WIN31_LOOK)
410     {
411         if (pushedState)
412         {
413             /* draw button shadow: */
414             SelectObject(hDC, GetSysColorBrush(COLOR_BTNSHADOW));
415             PatBlt(hDC, rc.left, rc.top, 1, rc.bottom-rc.top, PATCOPY );
416             PatBlt(hDC, rc.left, rc.top, rc.right-rc.left, 1, PATCOPY );
417             rc.left += 2;  /* To position the text down and right */
418             rc.top  += 2;
419         } else {
420            rc.right++, rc.bottom++;
421            DrawEdge( hDC, &rc, EDGE_RAISED, BF_RECT );
422
423            /* To place de bitmap correctly */
424            xBorderOffset += GetSystemMetrics(SM_CXEDGE);
425            yBorderOffset += GetSystemMetrics(SM_CYEDGE);
426
427            rc.right--, rc.bottom--;
428         }
429     }
430     else
431     {
432         UINT uState = DFCS_BUTTONPUSH;
433
434         if (pushedState)
435         {
436             if ( (wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON )
437                 uState |= DFCS_FLAT;
438             else
439                 uState |= DFCS_PUSHED;
440         }
441
442         DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
443         InflateRect( &rc, -2, -2 );
444
445         focus_rect = rc;
446
447         if (pushedState)
448         {
449             rc.left += 2;  /* To position the text down and right */
450             rc.top  += 2;
451         }
452     }
453
454     /* draw button label, if any:
455      *
456      * In win9x we don't show text if there is a bitmap or icon.
457      * I don't know about win31 so I leave it as it was for win31.
458      * Dennis Björklund 12 Jul, 99
459      */
460     if ( wndPtr->text && wndPtr->text[0]
461          && (TWEAK_WineLook == WIN31_LOOK || !(wndPtr->dwStyle & (BS_ICON|BS_BITMAP))) )
462     {
463         LOGBRUSH lb;
464         GetObjectA( GetSysColorBrush(COLOR_BTNFACE), sizeof(lb), &lb );
465         if (wndPtr->dwStyle & WS_DISABLED &&
466             GetSysColor(COLOR_GRAYTEXT)==lb.lbColor)
467             /* don't write gray text on gray background */
468             PaintGrayOnGray( hDC,infoPtr->hFont,&rc,wndPtr->text,
469                                DT_CENTER | DT_VCENTER );
470         else
471         {
472             SetTextColor( hDC, (wndPtr->dwStyle & WS_DISABLED) ?
473                                  GetSysColor(COLOR_GRAYTEXT) :
474                                  GetSysColor(COLOR_BTNTEXT) );
475             DrawTextA( hDC, wndPtr->text, -1, &rc,
476                          DT_SINGLELINE | DT_CENTER | DT_VCENTER );
477             /* do we have the focus?
478              * Win9x draws focus last with a size prop. to the button
479              */
480             if (TWEAK_WineLook == WIN31_LOOK
481                 && infoPtr->state & BUTTON_HASFOCUS)
482             {
483                 RECT r = { 0, 0, 0, 0 };
484                 INT xdelta, ydelta;
485
486                 DrawTextA( hDC, wndPtr->text, -1, &r,
487                              DT_SINGLELINE | DT_CALCRECT );
488                 xdelta = ((rc.right - rc.left) - (r.right - r.left) - 1) / 2;
489                 ydelta = ((rc.bottom - rc.top) - (r.bottom - r.top) - 1) / 2;
490                 if (xdelta < 0) xdelta = 0;
491                 if (ydelta < 0) ydelta = 0;
492                 InflateRect( &rc, -xdelta, -ydelta );
493                 DrawFocusRect( hDC, &rc );
494             }
495         }   
496     }
497     if ( ((wndPtr->dwStyle & BS_ICON) || (wndPtr->dwStyle & BS_BITMAP) ) &&
498          (infoPtr->hImage != 0) )
499     {
500         int yOffset, xOffset;
501         int imageWidth, imageHeight;
502
503         /*
504          * We extract the size of the image from the handle.
505          */
506         if (wndPtr->dwStyle & BS_ICON)
507         {
508             ICONINFO iconInfo;
509             BITMAP   bm;
510
511             GetIconInfo((HICON)infoPtr->hImage, &iconInfo);
512             GetObjectA (iconInfo.hbmColor, sizeof(BITMAP), &bm);
513             
514             imageWidth  = bm.bmWidth;
515             imageHeight = bm.bmHeight;
516
517             DeleteObject(iconInfo.hbmColor);
518             DeleteObject(iconInfo.hbmMask);
519
520         }
521         else
522         {
523             BITMAP   bm;
524
525             GetObjectA (infoPtr->hImage, sizeof(BITMAP), &bm);
526         
527             imageWidth  = bm.bmWidth;
528             imageHeight = bm.bmHeight;
529         }
530
531         /* Center the bitmap */
532         xOffset = (((rc.right - rc.left) - 2*xBorderOffset) - imageWidth ) / 2;
533         yOffset = (((rc.bottom - rc.top) - 2*yBorderOffset) - imageHeight) / 2;
534
535         /* If the image is too big for the button then create a region*/
536         if(xOffset < 0 || yOffset < 0)
537         {
538             HRGN hBitmapRgn = 0;
539             hBitmapRgn = CreateRectRgn(
540                 rc.left + xBorderOffset, rc.top +yBorderOffset, 
541                 rc.right - xBorderOffset, rc.bottom - yBorderOffset);
542             SelectClipRgn(hDC, hBitmapRgn);
543             DeleteObject(hBitmapRgn);
544         }
545
546         /* Let minimum 1 space from border */
547         xOffset++, yOffset++;
548
549         /*
550          * Draw the image now.
551          */
552         if (wndPtr->dwStyle & BS_ICON)
553         {
554             DrawIcon(hDC,
555                 rc.left + xOffset, rc.top + yOffset,
556                      (HICON)infoPtr->hImage);
557         }
558         else
559         {
560             HDC hdcMem;
561
562             hdcMem = CreateCompatibleDC (hDC);
563             SelectObject (hdcMem, (HBITMAP)infoPtr->hImage);
564             BitBlt(hDC, 
565                    rc.left + xOffset, 
566                    rc.top + yOffset, 
567                    imageWidth, imageHeight,
568                    hdcMem, 0, 0, SRCCOPY);
569             
570             DeleteDC (hdcMem);
571         }
572
573         if(xOffset < 0 || yOffset < 0)
574         {
575             SelectClipRgn(hDC, 0);
576         }
577     }
578
579     if (TWEAK_WineLook != WIN31_LOOK
580         && infoPtr->state & BUTTON_HASFOCUS)
581     {
582         InflateRect( &focus_rect, -1, -1 );
583         DrawFocusRect( hDC, &focus_rect );
584     }
585
586     
587     SelectObject( hDC, hOldPen );
588     SelectObject( hDC, hOldBrush );
589 }
590
591
592 /**********************************************************************
593  *   PB_Paint & CB_Paint sub function                        [internal]
594  *   Paint text using a raster brush to avoid gray text on gray 
595  *   background. 'format' can be a combination of DT_CENTER and 
596  *   DT_VCENTER to use this function in both PB_PAINT and 
597  *   CB_PAINT.   - Dirk Thierbach
598  *
599  *   FIXME: This and TEXT_GrayString should be eventually combined,
600  *   so calling one common function from PB_Paint, CB_Paint and
601  *   TEXT_GrayString will be enough. Also note that this
602  *   function ignores the CACHE_GetPattern funcs.
603  */
604
605 void PaintGrayOnGray(HDC hDC,HFONT hFont,RECT *rc,char *text,
606                         UINT format)
607 {
608 /*  This is the standard gray on gray pattern:
609     static const WORD Pattern[] = {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55}; 
610 */
611 /*  This pattern gives better readability with X Fonts.
612     FIXME: Maybe the user should be allowed to decide which he wants. */
613     static const WORD Pattern[] = {0x55,0xFF,0xAA,0xFF,0x55,0xFF,0xAA,0xFF}; 
614
615     HBITMAP hbm  = CreateBitmap( 8, 8, 1, 1, Pattern );
616     HDC hdcMem = CreateCompatibleDC(hDC);
617     HBITMAP hbmMem;
618     HBRUSH hBr;
619     RECT rect,rc2;
620
621     rect=*rc;
622     DrawTextA( hDC, text, -1, &rect, DT_SINGLELINE | DT_CALCRECT);
623     /* now text width and height are in rect.right and rect.bottom */
624     rc2=rect;
625     rect.left = rect.top = 0; /* drawing pos in hdcMem */
626     if (format & DT_CENTER) rect.left=(rc->right-rect.right)/2;
627     if (format & DT_VCENTER) rect.top=(rc->bottom-rect.bottom)/2;
628     hbmMem = CreateCompatibleBitmap( hDC,rect.right,rect.bottom );
629     SelectObject( hdcMem, hbmMem);
630     PatBlt( hdcMem,0,0,rect.right,rect.bottom,WHITENESS);
631       /* will be overwritten by DrawText, but just in case */
632     if (hFont) SelectObject( hdcMem, hFont);
633     DrawTextA( hdcMem, text, -1, &rc2, DT_SINGLELINE);  
634       /* After draw: foreground = 0 bits, background = 1 bits */
635     hBr = SelectObject( hdcMem, CreatePatternBrush(hbm) );
636     DeleteObject( hbm );
637     PatBlt( hdcMem,0,0,rect.right,rect.bottom,0xAF0229); 
638       /* only keep the foreground bits where pattern is 1 */
639     DeleteObject( SelectObject( hdcMem,hBr) );
640     BitBlt(hDC,rect.left,rect.top,rect.right,rect.bottom,hdcMem,0,0,SRCAND);
641       /* keep the background of the dest */
642     DeleteDC( hdcMem);
643     DeleteObject( hbmMem );
644 }
645
646
647 /**********************************************************************
648  *       Check Box & Radio Button Functions
649  */
650
651 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action )
652 {
653     RECT rbox, rtext, client;
654     HBRUSH hBrush;
655     int textlen, delta;
656     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
657
658     /* 
659      * if the button has a bitmap/icon, draw a normal pushbutton
660      * instead of a radion button.
661      */
662     if (infoPtr->hImage != 0)
663     {
664         BOOL bHighLighted = ((infoPtr->state & BUTTON_HIGHLIGHTED) ||
665                              (infoPtr->state & BUTTON_CHECKED));
666
667         BUTTON_DrawPushButton(wndPtr,
668                               hDC,
669                               action,
670                               bHighLighted);
671         return;
672     }
673
674     textlen = 0;
675     GetClientRect(wndPtr->hwndSelf, &client);
676     rbox = rtext = client;
677
678     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
679
680     /* Something is still not right, checkboxes (and edit controls)
681      * in wsping32 have white backgrounds instead of dark grey.
682      * BUTTON_SEND_CTLCOLOR() is even worse since it returns 0 in this
683      * particular case and the background is not painted at all.
684      */
685
686     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
687
688     if (wndPtr->dwStyle & BS_LEFTTEXT) 
689     {
690         /* magic +4 is what CTL3D expects */
691
692         rtext.right -= checkBoxWidth + 4;
693         rbox.left = rbox.right - checkBoxWidth;
694     }
695     else 
696     {
697         rtext.left += checkBoxWidth + 4;
698         rbox.right = checkBoxWidth;
699     }
700
701       /* Draw the check-box bitmap */
702
703     if (wndPtr->text) textlen = strlen( wndPtr->text );
704     if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
705     { 
706         if( TWEAK_WineLook == WIN31_LOOK )
707         {
708         HDC hMemDC = CreateCompatibleDC( hDC );
709         int x = 0, y = 0;
710         delta = (rbox.bottom - rbox.top - checkBoxHeight) / 2;
711
712         /* Check in case the client area is smaller than the checkbox bitmap */
713         if (delta < 0) delta = 0;
714
715         if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
716         else FillRect( hDC, &client, hBrush );
717
718         if (infoPtr->state & BUTTON_HIGHLIGHTED) x += 2 * checkBoxWidth;
719         if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) x += checkBoxWidth;
720         if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
721             ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) y += checkBoxHeight;
722         else if (infoPtr->state & BUTTON_3STATE) y += 2 * checkBoxHeight;
723
724         /* The bitmap for the radio button is not aligned with the
725          * left of the window, it is 1 pixel off. */
726         if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
727             ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
728           rbox.left += 1;
729
730         SelectObject( hMemDC, hbitmapCheckBoxes );
731         BitBlt( hDC, rbox.left, rbox.top + delta, checkBoxWidth,
732                   checkBoxHeight, hMemDC, x, y, SRCCOPY );
733         DeleteDC( hMemDC );
734         }
735         else
736         {
737             UINT state;
738
739             if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
740                 ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) state = DFCS_BUTTONRADIO;
741             else if (infoPtr->state & BUTTON_3STATE) state = DFCS_BUTTON3STATE;
742             else state = DFCS_BUTTONCHECK;
743
744             if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) state |= DFCS_CHECKED;
745             
746             if (infoPtr->state & BUTTON_HIGHLIGHTED) state |= DFCS_PUSHED;
747
748             if (wndPtr->dwStyle & WS_DISABLED) state |= DFCS_INACTIVE;
749
750             DrawFrameControl( hDC, &rbox, DFC_BUTTON, state );
751         }
752
753
754         if( textlen && action != ODA_SELECT )
755         {
756           if (wndPtr->dwStyle & WS_DISABLED &&
757               GetSysColor(COLOR_GRAYTEXT)==GetBkColor(hDC)) {
758             /* don't write gray text on gray background */
759             PaintGrayOnGray( hDC, infoPtr->hFont, &rtext, wndPtr->text,
760                              DT_VCENTER);
761           } else {
762             if (wndPtr->dwStyle & WS_DISABLED)
763                 SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
764             DrawTextA( hDC, wndPtr->text, textlen, &rtext,
765                          DT_SINGLELINE | DT_VCENTER );
766           }
767         }
768     }
769
770     if ((action == ODA_FOCUS) ||
771         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
772     {
773         /* again, this is what CTL3D expects */
774
775         SetRectEmpty(&rbox);
776         if( textlen )
777             DrawTextA( hDC, wndPtr->text, textlen, &rbox,
778                          DT_SINGLELINE | DT_CALCRECT );
779         textlen = rbox.bottom - rbox.top;
780         delta = ((rtext.bottom - rtext.top) - textlen)/2;
781         rbox.bottom = (rbox.top = rtext.top + delta - 1) + textlen + 2;
782         textlen = rbox.right - rbox.left;
783         rbox.right = (rbox.left += --rtext.left) + textlen + 2;
784         IntersectRect(&rbox, &rbox, &rtext);
785         DrawFocusRect( hDC, &rbox );
786     }
787 }
788
789
790 /**********************************************************************
791  *       BUTTON_CheckAutoRadioButton
792  *
793  * wndPtr is checked, uncheck every other auto radio button in group
794  */
795 static void BUTTON_CheckAutoRadioButton( WND *wndPtr )
796 {
797     HWND parent, sibling, start;
798     if (!(wndPtr->dwStyle & WS_CHILD)) return;
799     parent = wndPtr->parent->hwndSelf;
800     /* assure that starting control is not disabled or invisible */
801     start = sibling = GetNextDlgGroupItem( parent, wndPtr->hwndSelf, TRUE );
802     do
803     {
804         WND *tmpWnd;
805         if (!sibling) break;
806         tmpWnd = WIN_FindWndPtr(sibling);
807         if ((wndPtr->hwndSelf != sibling) &&
808             ((tmpWnd->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
809             SendMessageA( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
810         sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
811         WIN_ReleaseWndPtr(tmpWnd);
812     } while (sibling != start);
813 }
814
815
816 /**********************************************************************
817  *       Group Box Functions
818  */
819
820 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action )
821 {
822     RECT rc, rcFrame;
823     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
824
825     if (action != ODA_DRAWENTIRE) return;
826
827     BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
828
829     GetClientRect( wndPtr->hwndSelf, &rc);
830     if (TWEAK_WineLook == WIN31_LOOK) {
831         HPEN hPrevPen = SelectObject( hDC,
832                                           GetSysColorPen(COLOR_WINDOWFRAME));
833         HBRUSH hPrevBrush = SelectObject( hDC,
834                                               GetStockObject(NULL_BRUSH) );
835
836         Rectangle( hDC, rc.left, rc.top + 2, rc.right - 1, rc.bottom - 1 );
837         SelectObject( hDC, hPrevBrush );
838         SelectObject( hDC, hPrevPen );
839     } else {
840         TEXTMETRICA tm;
841         rcFrame = rc;
842
843         if (infoPtr->hFont)
844             SelectObject (hDC, infoPtr->hFont);
845         GetTextMetricsA (hDC, &tm);
846         rcFrame.top += (tm.tmHeight / 2) - 1;
847         DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT);
848     }
849
850     if (wndPtr->text)
851     {
852         if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
853         if (wndPtr->dwStyle & WS_DISABLED)
854             SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
855         rc.left += 10;
856         DrawTextA( hDC, wndPtr->text, -1, &rc, DT_SINGLELINE | DT_NOCLIP );
857     }
858 }
859
860
861 /**********************************************************************
862  *       User Button Functions
863  */
864
865 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action )
866 {
867     RECT rc;
868     HBRUSH hBrush;
869     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
870
871     if (action == ODA_SELECT) return;
872
873     GetClientRect( wndPtr->hwndSelf, &rc);
874
875     if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
876     hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
877
878     FillRect( hDC, &rc, hBrush );
879     if ((action == ODA_FOCUS) ||
880         ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
881         DrawFocusRect( hDC, &rc );
882 }
883
884
885 /**********************************************************************
886  *       Ownerdrawn Button Functions
887  */
888
889 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action )
890 {
891     BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
892     DRAWITEMSTRUCT dis;
893
894     dis.CtlType    = ODT_BUTTON;
895     dis.CtlID      = wndPtr->wIDmenu;
896     dis.itemID     = 0;
897     dis.itemAction = action;
898     dis.itemState  = ((infoPtr->state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
899                      ((infoPtr->state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
900                      ((wndPtr->dwStyle & WS_DISABLED) ? ODS_DISABLED : 0);
901     dis.hwndItem   = wndPtr->hwndSelf;
902     dis.hDC        = hDC;
903     dis.itemData   = 0;
904     GetClientRect( wndPtr->hwndSelf, &dis.rcItem );
905
906     SetBkColor( hDC, GetSysColor( COLOR_BTNFACE ) );
907
908     SendMessageA( GetParent(wndPtr->hwndSelf), WM_DRAWITEM,
909                     wndPtr->wIDmenu, (LPARAM)&dis );
910 }
911