SNOOP_PrintArg: replaced IsBad* functions by exception handler.
[wine/wine-kai.git] / controls / button.c
blobde52ff86394f3d3fad9fe79a74e5e461c52cae23
1 /* File: button.c -- Button type widgets
3 * Copyright (C) 1993 Johannes Ruscheinski
4 * Copyright (C) 1993 David Metcalfe
5 * Copyright (C) 1994 Alexandre Julliard
6 */
8 #include <string.h>
9 #include "win.h"
10 #include "button.h"
11 #include "winbase.h"
12 #include "windef.h"
13 #include "wingdi.h"
14 #include "wine/winuser16.h"
15 #include "tweak.h"
17 static void PaintGrayOnGray( HDC hDC,HFONT hFont,RECT *rc,
18 char *text, UINT format );
20 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action );
21 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action );
22 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action );
23 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action );
24 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action );
25 static void BUTTON_CheckAutoRadioButton( WND *wndPtr );
26 static void BUTTON_DrawPushButton( WND *wndPtr, HDC hDC, WORD action, BOOL pushedState);
28 #define MAX_BTN_TYPE 12
30 static const WORD maxCheckState[MAX_BTN_TYPE] =
32 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
33 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
34 BUTTON_CHECKED, /* BS_CHECKBOX */
35 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
36 BUTTON_CHECKED, /* BS_RADIOBUTTON */
37 BUTTON_3STATE, /* BS_3STATE */
38 BUTTON_3STATE, /* BS_AUTO3STATE */
39 BUTTON_UNCHECKED, /* BS_GROUPBOX */
40 BUTTON_UNCHECKED, /* BS_USERBUTTON */
41 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
42 BUTTON_UNCHECKED, /* Not defined */
43 BUTTON_UNCHECKED /* BS_OWNERDRAW */
46 typedef void (*pfPaint)( WND *wndPtr, HDC hdc, WORD action );
48 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
50 PB_Paint, /* BS_PUSHBUTTON */
51 PB_Paint, /* BS_DEFPUSHBUTTON */
52 CB_Paint, /* BS_CHECKBOX */
53 CB_Paint, /* BS_AUTOCHECKBOX */
54 CB_Paint, /* BS_RADIOBUTTON */
55 CB_Paint, /* BS_3STATE */
56 CB_Paint, /* BS_AUTO3STATE */
57 GB_Paint, /* BS_GROUPBOX */
58 UB_Paint, /* BS_USERBUTTON */
59 CB_Paint, /* BS_AUTORADIOBUTTON */
60 NULL, /* Not defined */
61 OB_Paint /* BS_OWNERDRAW */
64 #define PAINT_BUTTON(wndPtr,style,action) \
65 if (btnPaintFunc[style]) { \
66 HDC hdc = GetDC( (wndPtr)->hwndSelf ); \
67 (btnPaintFunc[style])(wndPtr,hdc,action); \
68 ReleaseDC( (wndPtr)->hwndSelf, hdc ); }
70 #define BUTTON_SEND_CTLCOLOR(wndPtr,hdc) \
71 SendMessageA( GetParent((wndPtr)->hwndSelf), WM_CTLCOLORBTN, \
72 (hdc), (wndPtr)->hwndSelf )
74 static HBITMAP hbitmapCheckBoxes = 0;
75 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
78 /***********************************************************************
79 * ButtonWndProc_locked
81 * Called with window lock held.
83 static inline LRESULT WINAPI ButtonWndProc_locked(WND* wndPtr, UINT uMsg,
84 WPARAM wParam, LPARAM lParam )
86 RECT rect;
87 HWND hWnd = wndPtr->hwndSelf;
88 POINT pt;
89 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
90 LONG style = wndPtr->dwStyle & 0x0f;
91 HANDLE oldHbitmap;
93 pt.x = LOWORD(lParam);
94 pt.y = HIWORD(lParam);
96 switch (uMsg)
98 case WM_GETDLGCODE:
99 switch(style)
101 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
102 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
103 case BS_RADIOBUTTON:
104 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
105 default: return DLGC_BUTTON;
108 case WM_ENABLE:
109 PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
110 break;
112 case WM_CREATE:
113 if (!hbitmapCheckBoxes)
115 BITMAP bmp;
116 hbitmapCheckBoxes = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_CHECKBOXES));
117 GetObjectA( hbitmapCheckBoxes, sizeof(bmp), &bmp );
118 checkBoxWidth = bmp.bmWidth / 4;
119 checkBoxHeight = bmp.bmHeight / 3;
121 if (style < 0L || style >= MAX_BTN_TYPE)
122 return -1; /* abort */
123 infoPtr->state = BUTTON_UNCHECKED;
124 infoPtr->hFont = 0;
125 infoPtr->hImage = 0;
126 return 0;
128 case WM_ERASEBKGND:
129 return 1;
131 case WM_PAINT:
132 if (btnPaintFunc[style])
134 PAINTSTRUCT ps;
135 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
136 SetBkMode( hdc, OPAQUE );
137 (btnPaintFunc[style])( wndPtr, hdc, ODA_DRAWENTIRE );
138 if( !wParam ) EndPaint( hWnd, &ps );
140 break;
142 case WM_KEYDOWN:
143 if (wParam == VK_SPACE)
145 SendMessageA( hWnd, BM_SETSTATE, TRUE, 0 );
146 infoPtr->state |= BUTTON_BTNPRESSED;
148 break;
150 case WM_LBUTTONDBLCLK:
151 if(wndPtr->dwStyle & BS_NOTIFY ||
152 style==BS_RADIOBUTTON ||
153 style==BS_USERBUTTON ||
154 style==BS_OWNERDRAW) {
155 SendMessageA( GetParent(hWnd), WM_COMMAND,
156 MAKEWPARAM( wndPtr->wIDmenu, BN_DOUBLECLICKED ), hWnd);
157 break;
159 /* fall through */
160 case WM_LBUTTONDOWN:
161 SetCapture( hWnd );
162 SetFocus( hWnd );
163 SendMessageA( hWnd, BM_SETSTATE, TRUE, 0 );
164 infoPtr->state |= BUTTON_BTNPRESSED;
165 break;
167 case WM_KEYUP:
168 if (wParam != VK_SPACE)
169 break;
170 /* fall through */
171 case WM_LBUTTONUP:
172 if (!(infoPtr->state & BUTTON_BTNPRESSED)) break;
173 infoPtr->state &= BUTTON_NSTATES;
174 if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) {
175 ReleaseCapture();
176 break;
178 SendMessageA( hWnd, BM_SETSTATE, FALSE, 0 );
179 ReleaseCapture();
180 GetClientRect( hWnd, &rect );
181 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
183 switch(style)
185 case BS_AUTOCHECKBOX:
186 SendMessageA( hWnd, BM_SETCHECK,
187 !(infoPtr->state & BUTTON_CHECKED), 0 );
188 break;
189 case BS_AUTORADIOBUTTON:
190 SendMessageA( hWnd, BM_SETCHECK, TRUE, 0 );
191 break;
192 case BS_AUTO3STATE:
193 SendMessageA( hWnd, BM_SETCHECK,
194 (infoPtr->state & BUTTON_3STATE) ? 0 :
195 ((infoPtr->state & 3) + 1), 0 );
196 break;
198 SendMessageA( GetParent(hWnd), WM_COMMAND,
199 MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
201 break;
203 case WM_CAPTURECHANGED:
204 if (infoPtr->state & BUTTON_BTNPRESSED) {
205 infoPtr->state &= BUTTON_NSTATES;
206 if (infoPtr->state & BUTTON_HIGHLIGHTED)
207 SendMessageA( hWnd, BM_SETSTATE, FALSE, 0 );
209 break;
211 case WM_MOUSEMOVE:
212 if (GetCapture() == hWnd)
214 GetClientRect( hWnd, &rect );
215 SendMessageA( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
217 break;
219 case WM_NCHITTEST:
220 if(style == BS_GROUPBOX) return HTTRANSPARENT;
221 return DefWindowProcA( hWnd, uMsg, wParam, lParam );
223 case WM_SETTEXT:
224 DEFWND_SetText( wndPtr, (LPCSTR)lParam );
225 if( wndPtr->dwStyle & WS_VISIBLE )
226 PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
227 return 0;
229 case WM_SETFONT:
230 infoPtr->hFont = (HFONT16)wParam;
231 if (lParam && (wndPtr->dwStyle & WS_VISIBLE))
232 PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
233 break;
235 case WM_GETFONT:
236 return infoPtr->hFont;
238 case WM_SETFOCUS:
239 if ((style == BS_AUTORADIOBUTTON) && (GetCapture() != hWnd) &&
240 !(SendMessageA(hWnd, BM_GETCHECK, 0, 0) & BST_CHECKED))
242 /* The notification is sent when the button (BS_AUTORADIOBUTTON)
243 is unckecked and the focus was not given by a mouse click. */
244 SendMessageA( hWnd, BM_SETCHECK, TRUE, 0 );
245 SendMessageA( GetParent(hWnd), WM_COMMAND,
246 MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
248 infoPtr->state |= BUTTON_HASFOCUS;
249 PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
250 break;
252 case WM_KILLFOCUS:
253 infoPtr->state &= ~BUTTON_HASFOCUS;
254 PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
255 InvalidateRect( hWnd, NULL, TRUE );
256 break;
258 case WM_SYSCOLORCHANGE:
259 InvalidateRect( hWnd, NULL, FALSE );
260 break;
262 case BM_SETSTYLE16:
263 case BM_SETSTYLE:
264 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
265 wndPtr->dwStyle = (wndPtr->dwStyle & 0xfffffff0)
266 | (wParam & 0x0000000f);
267 style = wndPtr->dwStyle & 0x0000000f;
268 PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
269 break;
271 case BM_CLICK:
272 SendMessageA( hWnd, WM_LBUTTONDOWN, 0, 0 );
273 SendMessageA( hWnd, WM_LBUTTONUP, 0, 0 );
274 break;
276 case BM_SETIMAGE:
277 oldHbitmap = infoPtr->hImage;
278 if ((wndPtr->dwStyle & BS_BITMAP) || (wndPtr->dwStyle & BS_ICON))
280 infoPtr->hImage = (HANDLE) lParam;
281 InvalidateRect( hWnd, NULL, FALSE );
283 return oldHbitmap;
285 case BM_GETIMAGE:
286 if (wParam == IMAGE_BITMAP)
287 return (HBITMAP)infoPtr->hImage;
288 else if (wParam == IMAGE_ICON)
289 return (HICON)infoPtr->hImage;
290 else
291 return (HICON)0;
293 case BM_GETCHECK16:
294 case BM_GETCHECK:
295 return infoPtr->state & 3;
297 case BM_SETCHECK16:
298 case BM_SETCHECK:
299 if (wParam > maxCheckState[style]) wParam = maxCheckState[style];
300 if ((infoPtr->state & 3) != wParam)
302 if ((style == BS_RADIOBUTTON) || (style == BS_AUTORADIOBUTTON))
304 if (wParam)
305 wndPtr->dwStyle |= WS_TABSTOP;
306 else
307 wndPtr->dwStyle &= ~WS_TABSTOP;
309 infoPtr->state = (infoPtr->state & ~3) | wParam;
310 PAINT_BUTTON( wndPtr, style, ODA_SELECT );
312 if ((style == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED))
313 BUTTON_CheckAutoRadioButton( wndPtr );
314 break;
316 case BM_GETSTATE16:
317 case BM_GETSTATE:
318 return infoPtr->state;
320 case BM_SETSTATE16:
321 case BM_SETSTATE:
322 if (wParam)
324 if (infoPtr->state & BUTTON_HIGHLIGHTED) break;
325 infoPtr->state |= BUTTON_HIGHLIGHTED;
327 else
329 if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) break;
330 infoPtr->state &= ~BUTTON_HIGHLIGHTED;
332 PAINT_BUTTON( wndPtr, style, ODA_SELECT );
333 break;
335 default:
336 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
338 return 0;
341 /***********************************************************************
342 * ButtonWndProc
343 * The button window procedure. This is just a wrapper which locks
344 * the passed HWND and calls the real window procedure (with a WND*
345 * pointer pointing to the locked windowstructure).
347 LRESULT WINAPI ButtonWndProc( HWND hWnd, UINT uMsg,
348 WPARAM wParam, LPARAM lParam )
350 LRESULT res;
351 WND *wndPtr = WIN_FindWndPtr(hWnd);
353 res = ButtonWndProc_locked(wndPtr,uMsg,wParam,lParam);
355 WIN_ReleaseWndPtr(wndPtr);
356 return res;
359 /**********************************************************************
360 * Push Button Functions
362 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action )
364 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
365 BOOL bHighLighted = (infoPtr->state & BUTTON_HIGHLIGHTED);
368 * Delegate this to the more generic pushbutton painting
369 * method.
371 BUTTON_DrawPushButton(wndPtr,
372 hDC,
373 action,
374 bHighLighted);
377 /**********************************************************************
378 * This method will actually do the drawing of the pushbutton
379 * depending on it's state and the pushedState parameter.
381 static void BUTTON_DrawPushButton(
382 WND* wndPtr,
383 HDC hDC,
384 WORD action,
385 BOOL pushedState )
387 RECT rc, focus_rect;
388 HPEN hOldPen;
389 HBRUSH hOldBrush;
390 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
391 int xBorderOffset, yBorderOffset;
392 xBorderOffset = yBorderOffset = 0;
394 GetClientRect( wndPtr->hwndSelf, &rc );
396 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
397 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
398 BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
399 hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
400 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
401 SetBkMode(hDC, TRANSPARENT);
403 if ( TWEAK_WineLook == WIN31_LOOK)
405 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
407 SetPixel( hDC, rc.left, rc.top, GetSysColor(COLOR_WINDOW) );
408 SetPixel( hDC, rc.left, rc.bottom-1, GetSysColor(COLOR_WINDOW) );
409 SetPixel( hDC, rc.right-1, rc.top, GetSysColor(COLOR_WINDOW) );
410 SetPixel( hDC, rc.right-1, rc.bottom-1, GetSysColor(COLOR_WINDOW));
411 InflateRect( &rc, -1, -1 );
414 if ((wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON)
416 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
417 InflateRect( &rc, -1, -1 );
420 if (TWEAK_WineLook == WIN31_LOOK)
422 if (pushedState)
424 /* draw button shadow: */
425 SelectObject(hDC, GetSysColorBrush(COLOR_BTNSHADOW));
426 PatBlt(hDC, rc.left, rc.top, 1, rc.bottom-rc.top, PATCOPY );
427 PatBlt(hDC, rc.left, rc.top, rc.right-rc.left, 1, PATCOPY );
428 rc.left += 2; /* To position the text down and right */
429 rc.top += 2;
430 } else {
431 rc.right++, rc.bottom++;
432 DrawEdge( hDC, &rc, EDGE_RAISED, BF_RECT );
434 /* To place de bitmap correctly */
435 xBorderOffset += GetSystemMetrics(SM_CXEDGE);
436 yBorderOffset += GetSystemMetrics(SM_CYEDGE);
438 rc.right--, rc.bottom--;
441 else
443 UINT uState = DFCS_BUTTONPUSH;
445 if (pushedState)
447 if ( (wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON )
448 uState |= DFCS_FLAT;
449 else
450 uState |= DFCS_PUSHED;
453 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
454 InflateRect( &rc, -2, -2 );
456 focus_rect = rc;
458 if (pushedState)
460 rc.left += 2; /* To position the text down and right */
461 rc.top += 2;
465 /* draw button label, if any:
467 * In win9x we don't show text if there is a bitmap or icon.
468 * I don't know about win31 so I leave it as it was for win31.
469 * Dennis Björklund 12 Jul, 99
471 if ( wndPtr->text && wndPtr->text[0]
472 && (TWEAK_WineLook == WIN31_LOOK || !(wndPtr->dwStyle & (BS_ICON|BS_BITMAP))) )
474 LOGBRUSH lb;
475 GetObjectA( GetSysColorBrush(COLOR_BTNFACE), sizeof(lb), &lb );
476 if (wndPtr->dwStyle & WS_DISABLED &&
477 GetSysColor(COLOR_GRAYTEXT)==lb.lbColor)
478 /* don't write gray text on gray background */
479 PaintGrayOnGray( hDC,infoPtr->hFont,&rc,wndPtr->text,
480 DT_CENTER | DT_VCENTER );
481 else
483 SetTextColor( hDC, (wndPtr->dwStyle & WS_DISABLED) ?
484 GetSysColor(COLOR_GRAYTEXT) :
485 GetSysColor(COLOR_BTNTEXT) );
486 DrawTextA( hDC, wndPtr->text, -1, &rc,
487 DT_SINGLELINE | DT_CENTER | DT_VCENTER );
488 /* do we have the focus?
489 * Win9x draws focus last with a size prop. to the button
491 if (TWEAK_WineLook == WIN31_LOOK
492 && infoPtr->state & BUTTON_HASFOCUS)
494 RECT r = { 0, 0, 0, 0 };
495 INT xdelta, ydelta;
497 DrawTextA( hDC, wndPtr->text, -1, &r,
498 DT_SINGLELINE | DT_CALCRECT );
499 xdelta = ((rc.right - rc.left) - (r.right - r.left) - 1) / 2;
500 ydelta = ((rc.bottom - rc.top) - (r.bottom - r.top) - 1) / 2;
501 if (xdelta < 0) xdelta = 0;
502 if (ydelta < 0) ydelta = 0;
503 InflateRect( &rc, -xdelta, -ydelta );
504 DrawFocusRect( hDC, &rc );
508 if ( ((wndPtr->dwStyle & BS_ICON) || (wndPtr->dwStyle & BS_BITMAP) ) &&
509 (infoPtr->hImage != 0) )
511 int yOffset, xOffset;
512 int imageWidth, imageHeight;
515 * We extract the size of the image from the handle.
517 if (wndPtr->dwStyle & BS_ICON)
519 ICONINFO iconInfo;
520 BITMAP bm;
522 GetIconInfo((HICON)infoPtr->hImage, &iconInfo);
523 GetObjectA (iconInfo.hbmColor, sizeof(BITMAP), &bm);
525 imageWidth = bm.bmWidth;
526 imageHeight = bm.bmHeight;
528 DeleteObject(iconInfo.hbmColor);
529 DeleteObject(iconInfo.hbmMask);
532 else
534 BITMAP bm;
536 GetObjectA (infoPtr->hImage, sizeof(BITMAP), &bm);
538 imageWidth = bm.bmWidth;
539 imageHeight = bm.bmHeight;
542 /* Center the bitmap */
543 xOffset = (((rc.right - rc.left) - 2*xBorderOffset) - imageWidth ) / 2;
544 yOffset = (((rc.bottom - rc.top) - 2*yBorderOffset) - imageHeight) / 2;
546 /* If the image is too big for the button then create a region*/
547 if(xOffset < 0 || yOffset < 0)
549 HRGN hBitmapRgn = 0;
550 hBitmapRgn = CreateRectRgn(
551 rc.left + xBorderOffset, rc.top +yBorderOffset,
552 rc.right - xBorderOffset, rc.bottom - yBorderOffset);
553 SelectClipRgn(hDC, hBitmapRgn);
554 DeleteObject(hBitmapRgn);
557 /* Let minimum 1 space from border */
558 xOffset++, yOffset++;
561 * Draw the image now.
563 if (wndPtr->dwStyle & BS_ICON)
565 DrawIcon(hDC,
566 rc.left + xOffset, rc.top + yOffset,
567 (HICON)infoPtr->hImage);
569 else
571 HDC hdcMem;
573 hdcMem = CreateCompatibleDC (hDC);
574 SelectObject (hdcMem, (HBITMAP)infoPtr->hImage);
575 BitBlt(hDC,
576 rc.left + xOffset,
577 rc.top + yOffset,
578 imageWidth, imageHeight,
579 hdcMem, 0, 0, SRCCOPY);
581 DeleteDC (hdcMem);
584 if(xOffset < 0 || yOffset < 0)
586 SelectClipRgn(hDC, 0);
590 if (TWEAK_WineLook != WIN31_LOOK
591 && infoPtr->state & BUTTON_HASFOCUS)
593 InflateRect( &focus_rect, -1, -1 );
594 DrawFocusRect( hDC, &focus_rect );
598 SelectObject( hDC, hOldPen );
599 SelectObject( hDC, hOldBrush );
603 /**********************************************************************
604 * PB_Paint & CB_Paint sub function [internal]
605 * Paint text using a raster brush to avoid gray text on gray
606 * background. 'format' can be a combination of DT_CENTER and
607 * DT_VCENTER to use this function in both PB_PAINT and
608 * CB_PAINT. - Dirk Thierbach
610 * FIXME: This and TEXT_GrayString should be eventually combined,
611 * so calling one common function from PB_Paint, CB_Paint and
612 * TEXT_GrayString will be enough. Also note that this
613 * function ignores the CACHE_GetPattern funcs.
616 void PaintGrayOnGray(HDC hDC,HFONT hFont,RECT *rc,char *text,
617 UINT format)
619 /* This is the standard gray on gray pattern:
620 static const WORD Pattern[] = {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55};
622 /* This pattern gives better readability with X Fonts.
623 FIXME: Maybe the user should be allowed to decide which he wants. */
624 static const WORD Pattern[] = {0x55,0xFF,0xAA,0xFF,0x55,0xFF,0xAA,0xFF};
626 HBITMAP hbm = CreateBitmap( 8, 8, 1, 1, Pattern );
627 HDC hdcMem = CreateCompatibleDC(hDC);
628 HBITMAP hbmMem;
629 HBRUSH hBr;
630 RECT rect,rc2;
632 rect=*rc;
633 DrawTextA( hDC, text, -1, &rect, DT_SINGLELINE | DT_CALCRECT);
634 /* now text width and height are in rect.right and rect.bottom */
635 rc2=rect;
636 rect.left = rect.top = 0; /* drawing pos in hdcMem */
637 if (format & DT_CENTER) rect.left=(rc->right-rect.right)/2;
638 if (format & DT_VCENTER) rect.top=(rc->bottom-rect.bottom)/2;
639 hbmMem = CreateCompatibleBitmap( hDC,rect.right,rect.bottom );
640 SelectObject( hdcMem, hbmMem);
641 PatBlt( hdcMem,0,0,rect.right,rect.bottom,WHITENESS);
642 /* will be overwritten by DrawText, but just in case */
643 if (hFont) SelectObject( hdcMem, hFont);
644 DrawTextA( hdcMem, text, -1, &rc2, DT_SINGLELINE);
645 /* After draw: foreground = 0 bits, background = 1 bits */
646 hBr = SelectObject( hdcMem, CreatePatternBrush(hbm) );
647 DeleteObject( hbm );
648 PatBlt( hdcMem,0,0,rect.right,rect.bottom,0xAF0229);
649 /* only keep the foreground bits where pattern is 1 */
650 DeleteObject( SelectObject( hdcMem,hBr) );
651 BitBlt(hDC,rect.left,rect.top,rect.right,rect.bottom,hdcMem,0,0,SRCAND);
652 /* keep the background of the dest */
653 DeleteDC( hdcMem);
654 DeleteObject( hbmMem );
658 /**********************************************************************
659 * Check Box & Radio Button Functions
662 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action )
664 RECT rbox, rtext, client;
665 HBRUSH hBrush;
666 int textlen, delta;
667 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
670 * if the button has a bitmap/icon, draw a normal pushbutton
671 * instead of a radion button.
673 if (infoPtr->hImage != 0)
675 BOOL bHighLighted = ((infoPtr->state & BUTTON_HIGHLIGHTED) ||
676 (infoPtr->state & BUTTON_CHECKED));
678 BUTTON_DrawPushButton(wndPtr,
679 hDC,
680 action,
681 bHighLighted);
682 return;
685 textlen = 0;
686 GetClientRect(wndPtr->hwndSelf, &client);
687 rbox = rtext = client;
689 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
691 /* Something is still not right, checkboxes (and edit controls)
692 * in wsping32 have white backgrounds instead of dark grey.
693 * BUTTON_SEND_CTLCOLOR() is even worse since it returns 0 in this
694 * particular case and the background is not painted at all.
697 hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
699 if (wndPtr->dwStyle & BS_LEFTTEXT)
701 /* magic +4 is what CTL3D expects */
703 rtext.right -= checkBoxWidth + 4;
704 rbox.left = rbox.right - checkBoxWidth;
706 else
708 rtext.left += checkBoxWidth + 4;
709 rbox.right = checkBoxWidth;
712 /* Draw the check-box bitmap */
714 if (wndPtr->text) textlen = strlen( wndPtr->text );
715 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
717 if( TWEAK_WineLook == WIN31_LOOK )
719 HDC hMemDC = CreateCompatibleDC( hDC );
720 int x = 0, y = 0;
721 delta = (rbox.bottom - rbox.top - checkBoxHeight) / 2;
723 /* Check in case the client area is smaller than the checkbox bitmap */
724 if (delta < 0) delta = 0;
726 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
727 else FillRect( hDC, &client, hBrush );
729 if (infoPtr->state & BUTTON_HIGHLIGHTED) x += 2 * checkBoxWidth;
730 if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) x += checkBoxWidth;
731 if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
732 ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) y += checkBoxHeight;
733 else if (infoPtr->state & BUTTON_3STATE) y += 2 * checkBoxHeight;
735 /* The bitmap for the radio button is not aligned with the
736 * left of the window, it is 1 pixel off. */
737 if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
738 ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
739 rbox.left += 1;
741 SelectObject( hMemDC, hbitmapCheckBoxes );
742 BitBlt( hDC, rbox.left, rbox.top + delta, checkBoxWidth,
743 checkBoxHeight, hMemDC, x, y, SRCCOPY );
744 DeleteDC( hMemDC );
746 else
748 UINT state;
750 if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
751 ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) state = DFCS_BUTTONRADIO;
752 else if (infoPtr->state & BUTTON_3STATE) state = DFCS_BUTTON3STATE;
753 else state = DFCS_BUTTONCHECK;
755 if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) state |= DFCS_CHECKED;
757 if (infoPtr->state & BUTTON_HIGHLIGHTED) state |= DFCS_PUSHED;
759 if (wndPtr->dwStyle & WS_DISABLED) state |= DFCS_INACTIVE;
761 DrawFrameControl( hDC, &rbox, DFC_BUTTON, state );
765 if( textlen && action != ODA_SELECT )
767 if (wndPtr->dwStyle & WS_DISABLED &&
768 GetSysColor(COLOR_GRAYTEXT)==GetBkColor(hDC)) {
769 /* don't write gray text on gray background */
770 PaintGrayOnGray( hDC, infoPtr->hFont, &rtext, wndPtr->text,
771 DT_VCENTER);
772 } else {
773 if (wndPtr->dwStyle & WS_DISABLED)
774 SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
775 DrawTextA( hDC, wndPtr->text, textlen, &rtext,
776 DT_SINGLELINE | DT_VCENTER );
781 if ((action == ODA_FOCUS) ||
782 ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
784 /* again, this is what CTL3D expects */
786 SetRectEmpty(&rbox);
787 if( textlen )
788 DrawTextA( hDC, wndPtr->text, textlen, &rbox,
789 DT_SINGLELINE | DT_CALCRECT );
790 textlen = rbox.bottom - rbox.top;
791 delta = ((rtext.bottom - rtext.top) - textlen)/2;
792 rbox.bottom = (rbox.top = rtext.top + delta - 1) + textlen + 2;
793 textlen = rbox.right - rbox.left;
794 rbox.right = (rbox.left += --rtext.left) + textlen + 2;
795 IntersectRect(&rbox, &rbox, &rtext);
796 DrawFocusRect( hDC, &rbox );
801 /**********************************************************************
802 * BUTTON_CheckAutoRadioButton
804 * wndPtr is checked, uncheck every other auto radio button in group
806 static void BUTTON_CheckAutoRadioButton( WND *wndPtr )
808 HWND parent, sibling, start;
809 if (!(wndPtr->dwStyle & WS_CHILD)) return;
810 parent = wndPtr->parent->hwndSelf;
811 /* assure that starting control is not disabled or invisible */
812 start = sibling = GetNextDlgGroupItem( parent, wndPtr->hwndSelf, TRUE );
815 WND *tmpWnd;
816 if (!sibling) break;
817 tmpWnd = WIN_FindWndPtr(sibling);
818 if ((wndPtr->hwndSelf != sibling) &&
819 ((tmpWnd->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
820 SendMessageA( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
821 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
822 WIN_ReleaseWndPtr(tmpWnd);
823 } while (sibling != start);
827 /**********************************************************************
828 * Group Box Functions
831 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action )
833 RECT rc, rcFrame;
834 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
836 if (action != ODA_DRAWENTIRE) return;
838 BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
840 GetClientRect( wndPtr->hwndSelf, &rc);
841 if (TWEAK_WineLook == WIN31_LOOK) {
842 HPEN hPrevPen = SelectObject( hDC,
843 GetSysColorPen(COLOR_WINDOWFRAME));
844 HBRUSH hPrevBrush = SelectObject( hDC,
845 GetStockObject(NULL_BRUSH) );
847 Rectangle( hDC, rc.left, rc.top + 2, rc.right - 1, rc.bottom - 1 );
848 SelectObject( hDC, hPrevBrush );
849 SelectObject( hDC, hPrevPen );
850 } else {
851 TEXTMETRICA tm;
852 rcFrame = rc;
854 if (infoPtr->hFont)
855 SelectObject (hDC, infoPtr->hFont);
856 GetTextMetricsA (hDC, &tm);
857 rcFrame.top += (tm.tmHeight / 2) - 1;
858 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT);
861 if (wndPtr->text)
863 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
864 if (wndPtr->dwStyle & WS_DISABLED)
865 SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
866 rc.left += 10;
867 DrawTextA( hDC, wndPtr->text, -1, &rc, DT_SINGLELINE | DT_NOCLIP );
872 /**********************************************************************
873 * User Button Functions
876 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action )
878 RECT rc;
879 HBRUSH hBrush;
880 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
882 if (action == ODA_SELECT) return;
884 GetClientRect( wndPtr->hwndSelf, &rc);
886 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
887 hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
889 FillRect( hDC, &rc, hBrush );
890 if ((action == ODA_FOCUS) ||
891 ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
892 DrawFocusRect( hDC, &rc );
896 /**********************************************************************
897 * Ownerdrawn Button Functions
900 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action )
902 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
903 DRAWITEMSTRUCT dis;
905 dis.CtlType = ODT_BUTTON;
906 dis.CtlID = wndPtr->wIDmenu;
907 dis.itemID = 0;
908 dis.itemAction = action;
909 dis.itemState = ((infoPtr->state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
910 ((infoPtr->state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
911 ((wndPtr->dwStyle & WS_DISABLED) ? ODS_DISABLED : 0);
912 dis.hwndItem = wndPtr->hwndSelf;
913 dis.hDC = hDC;
914 dis.itemData = 0;
915 GetClientRect( wndPtr->hwndSelf, &dis.rcItem );
917 SetBkColor( hDC, GetSysColor( COLOR_BTNFACE ) );
919 SendMessageA( GetParent(wndPtr->hwndSelf), WM_DRAWITEM,
920 wndPtr->wIDmenu, (LPARAM)&dis );