- Small fixes/updates
[wine.git] / controls / button.c
blob747b73238f2dee0991909c6e5ca65d24829f01c5
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_RADIOBUTTON || 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 if (style == BS_AUTORADIOBUTTON)
245 SendMessageA( hWnd, BM_SETCHECK, BUTTON_CHECKED, 0 );
246 SendMessageA( GetParent(hWnd), WM_COMMAND,
247 MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
249 infoPtr->state |= BUTTON_HASFOCUS;
250 PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
251 break;
253 case WM_KILLFOCUS:
254 infoPtr->state &= ~BUTTON_HASFOCUS;
255 PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
256 InvalidateRect( hWnd, NULL, TRUE );
257 break;
259 case WM_SYSCOLORCHANGE:
260 InvalidateRect( hWnd, NULL, FALSE );
261 break;
263 case BM_SETSTYLE16:
264 case BM_SETSTYLE:
265 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
266 wndPtr->dwStyle = (wndPtr->dwStyle & 0xfffffff0)
267 | (wParam & 0x0000000f);
268 style = wndPtr->dwStyle & 0x0000000f;
269 PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
270 break;
272 case BM_CLICK:
273 SendMessageA( hWnd, WM_LBUTTONDOWN, 0, 0 );
274 SendMessageA( hWnd, WM_LBUTTONUP, 0, 0 );
275 break;
277 case BM_SETIMAGE:
278 oldHbitmap = infoPtr->hImage;
279 if ((wndPtr->dwStyle & BS_BITMAP) || (wndPtr->dwStyle & BS_ICON))
281 infoPtr->hImage = (HANDLE) lParam;
282 InvalidateRect( hWnd, NULL, FALSE );
284 return oldHbitmap;
286 case BM_GETIMAGE:
287 if (wParam == IMAGE_BITMAP)
288 return (HBITMAP)infoPtr->hImage;
289 else if (wParam == IMAGE_ICON)
290 return (HICON)infoPtr->hImage;
291 else
292 return (HICON)0;
294 case BM_GETCHECK16:
295 case BM_GETCHECK:
296 return infoPtr->state & 3;
298 case BM_SETCHECK16:
299 case BM_SETCHECK:
300 if (wParam > maxCheckState[style]) wParam = maxCheckState[style];
301 if ((infoPtr->state & 3) != wParam)
303 if ((style == BS_RADIOBUTTON) || (style == BS_AUTORADIOBUTTON))
305 if (wParam)
306 wndPtr->dwStyle |= WS_TABSTOP;
307 else
308 wndPtr->dwStyle &= ~WS_TABSTOP;
310 infoPtr->state = (infoPtr->state & ~3) | wParam;
311 PAINT_BUTTON( wndPtr, style, ODA_SELECT );
313 if ((style == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED))
314 BUTTON_CheckAutoRadioButton( wndPtr );
315 break;
317 case BM_GETSTATE16:
318 case BM_GETSTATE:
319 return infoPtr->state;
321 case BM_SETSTATE16:
322 case BM_SETSTATE:
323 if (wParam)
325 if (infoPtr->state & BUTTON_HIGHLIGHTED) break;
326 infoPtr->state |= BUTTON_HIGHLIGHTED;
328 else
330 if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) break;
331 infoPtr->state &= ~BUTTON_HIGHLIGHTED;
333 PAINT_BUTTON( wndPtr, style, ODA_SELECT );
334 break;
336 default:
337 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
339 return 0;
342 /***********************************************************************
343 * ButtonWndProc
344 * The button window procedure. This is just a wrapper which locks
345 * the passed HWND and calls the real window procedure (with a WND*
346 * pointer pointing to the locked windowstructure).
348 LRESULT WINAPI ButtonWndProc( HWND hWnd, UINT uMsg,
349 WPARAM wParam, LPARAM lParam )
351 LRESULT res;
352 WND *wndPtr = WIN_FindWndPtr(hWnd);
354 res = ButtonWndProc_locked(wndPtr,uMsg,wParam,lParam);
356 WIN_ReleaseWndPtr(wndPtr);
357 return res;
360 /**********************************************************************
361 * Push Button Functions
363 static void PB_Paint( WND *wndPtr, HDC hDC, WORD action )
365 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
366 BOOL bHighLighted = (infoPtr->state & BUTTON_HIGHLIGHTED);
369 * Delegate this to the more generic pushbutton painting
370 * method.
372 BUTTON_DrawPushButton(wndPtr,
373 hDC,
374 action,
375 bHighLighted);
378 /**********************************************************************
379 * This method will actually do the drawing of the pushbutton
380 * depending on it's state and the pushedState parameter.
382 static void BUTTON_DrawPushButton(
383 WND* wndPtr,
384 HDC hDC,
385 WORD action,
386 BOOL pushedState )
388 RECT rc, focus_rect;
389 HPEN hOldPen;
390 HBRUSH hOldBrush;
391 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
392 int xBorderOffset, yBorderOffset;
393 xBorderOffset = yBorderOffset = 0;
395 GetClientRect( wndPtr->hwndSelf, &rc );
397 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
398 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
399 BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
400 hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
401 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
402 SetBkMode(hDC, TRANSPARENT);
404 if ( TWEAK_WineLook == WIN31_LOOK)
406 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
408 SetPixel( hDC, rc.left, rc.top, GetSysColor(COLOR_WINDOW) );
409 SetPixel( hDC, rc.left, rc.bottom-1, GetSysColor(COLOR_WINDOW) );
410 SetPixel( hDC, rc.right-1, rc.top, GetSysColor(COLOR_WINDOW) );
411 SetPixel( hDC, rc.right-1, rc.bottom-1, GetSysColor(COLOR_WINDOW));
412 InflateRect( &rc, -1, -1 );
415 if ((wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON)
417 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
418 InflateRect( &rc, -1, -1 );
421 if (TWEAK_WineLook == WIN31_LOOK)
423 if (pushedState)
425 /* draw button shadow: */
426 SelectObject(hDC, GetSysColorBrush(COLOR_BTNSHADOW));
427 PatBlt(hDC, rc.left, rc.top, 1, rc.bottom-rc.top, PATCOPY );
428 PatBlt(hDC, rc.left, rc.top, rc.right-rc.left, 1, PATCOPY );
429 rc.left += 2; /* To position the text down and right */
430 rc.top += 2;
431 } else {
432 rc.right++, rc.bottom++;
433 DrawEdge( hDC, &rc, EDGE_RAISED, BF_RECT );
435 /* To place de bitmap correctly */
436 xBorderOffset += GetSystemMetrics(SM_CXEDGE);
437 yBorderOffset += GetSystemMetrics(SM_CYEDGE);
439 rc.right--, rc.bottom--;
442 else
444 UINT uState = DFCS_BUTTONPUSH;
446 if (pushedState)
448 if ( (wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON )
449 uState |= DFCS_FLAT;
450 else
451 uState |= DFCS_PUSHED;
454 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
455 InflateRect( &rc, -2, -2 );
457 focus_rect = rc;
459 if (pushedState)
461 rc.left += 2; /* To position the text down and right */
462 rc.top += 2;
466 /* draw button label, if any:
468 * In win9x we don't show text if there is a bitmap or icon.
469 * I don't know about win31 so I leave it as it was for win31.
470 * Dennis Björklund 12 Jul, 99
472 if ( wndPtr->text && wndPtr->text[0]
473 && (TWEAK_WineLook == WIN31_LOOK || !(wndPtr->dwStyle & (BS_ICON|BS_BITMAP))) )
475 LOGBRUSH lb;
476 GetObjectA( GetSysColorBrush(COLOR_BTNFACE), sizeof(lb), &lb );
477 if (wndPtr->dwStyle & WS_DISABLED &&
478 GetSysColor(COLOR_GRAYTEXT)==lb.lbColor)
479 /* don't write gray text on gray background */
480 PaintGrayOnGray( hDC,infoPtr->hFont,&rc,wndPtr->text,
481 DT_CENTER | DT_VCENTER );
482 else
484 SetTextColor( hDC, (wndPtr->dwStyle & WS_DISABLED) ?
485 GetSysColor(COLOR_GRAYTEXT) :
486 GetSysColor(COLOR_BTNTEXT) );
487 DrawTextA( hDC, wndPtr->text, -1, &rc,
488 DT_SINGLELINE | DT_CENTER | DT_VCENTER );
489 /* do we have the focus?
490 * Win9x draws focus last with a size prop. to the button
492 if (TWEAK_WineLook == WIN31_LOOK
493 && infoPtr->state & BUTTON_HASFOCUS)
495 RECT r = { 0, 0, 0, 0 };
496 INT xdelta, ydelta;
498 DrawTextA( hDC, wndPtr->text, -1, &r,
499 DT_SINGLELINE | DT_CALCRECT );
500 xdelta = ((rc.right - rc.left) - (r.right - r.left) - 1) / 2;
501 ydelta = ((rc.bottom - rc.top) - (r.bottom - r.top) - 1) / 2;
502 if (xdelta < 0) xdelta = 0;
503 if (ydelta < 0) ydelta = 0;
504 InflateRect( &rc, -xdelta, -ydelta );
505 DrawFocusRect( hDC, &rc );
509 if ( ((wndPtr->dwStyle & BS_ICON) || (wndPtr->dwStyle & BS_BITMAP) ) &&
510 (infoPtr->hImage != 0) )
512 int yOffset, xOffset;
513 int imageWidth, imageHeight;
516 * We extract the size of the image from the handle.
518 if (wndPtr->dwStyle & BS_ICON)
520 ICONINFO iconInfo;
521 BITMAP bm;
523 GetIconInfo((HICON)infoPtr->hImage, &iconInfo);
524 GetObjectA (iconInfo.hbmColor, sizeof(BITMAP), &bm);
526 imageWidth = bm.bmWidth;
527 imageHeight = bm.bmHeight;
529 DeleteObject(iconInfo.hbmColor);
530 DeleteObject(iconInfo.hbmMask);
533 else
535 BITMAP bm;
537 GetObjectA (infoPtr->hImage, sizeof(BITMAP), &bm);
539 imageWidth = bm.bmWidth;
540 imageHeight = bm.bmHeight;
543 /* Center the bitmap */
544 xOffset = (((rc.right - rc.left) - 2*xBorderOffset) - imageWidth ) / 2;
545 yOffset = (((rc.bottom - rc.top) - 2*yBorderOffset) - imageHeight) / 2;
547 /* If the image is too big for the button then create a region*/
548 if(xOffset < 0 || yOffset < 0)
550 HRGN hBitmapRgn = 0;
551 hBitmapRgn = CreateRectRgn(
552 rc.left + xBorderOffset, rc.top +yBorderOffset,
553 rc.right - xBorderOffset, rc.bottom - yBorderOffset);
554 SelectClipRgn(hDC, hBitmapRgn);
555 DeleteObject(hBitmapRgn);
558 /* Let minimum 1 space from border */
559 xOffset++, yOffset++;
562 * Draw the image now.
564 if (wndPtr->dwStyle & BS_ICON)
566 DrawIcon(hDC,
567 rc.left + xOffset, rc.top + yOffset,
568 (HICON)infoPtr->hImage);
570 else
572 HDC hdcMem;
574 hdcMem = CreateCompatibleDC (hDC);
575 SelectObject (hdcMem, (HBITMAP)infoPtr->hImage);
576 BitBlt(hDC,
577 rc.left + xOffset,
578 rc.top + yOffset,
579 imageWidth, imageHeight,
580 hdcMem, 0, 0, SRCCOPY);
582 DeleteDC (hdcMem);
585 if(xOffset < 0 || yOffset < 0)
587 SelectClipRgn(hDC, 0);
591 if (TWEAK_WineLook != WIN31_LOOK
592 && infoPtr->state & BUTTON_HASFOCUS)
594 InflateRect( &focus_rect, -1, -1 );
595 DrawFocusRect( hDC, &focus_rect );
599 SelectObject( hDC, hOldPen );
600 SelectObject( hDC, hOldBrush );
604 /**********************************************************************
605 * PB_Paint & CB_Paint sub function [internal]
606 * Paint text using a raster brush to avoid gray text on gray
607 * background. 'format' can be a combination of DT_CENTER and
608 * DT_VCENTER to use this function in both PB_PAINT and
609 * CB_PAINT. - Dirk Thierbach
611 * FIXME: This and TEXT_GrayString should be eventually combined,
612 * so calling one common function from PB_Paint, CB_Paint and
613 * TEXT_GrayString will be enough. Also note that this
614 * function ignores the CACHE_GetPattern funcs.
617 void PaintGrayOnGray(HDC hDC,HFONT hFont,RECT *rc,char *text,
618 UINT format)
620 /* This is the standard gray on gray pattern:
621 static const WORD Pattern[] = {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55};
623 /* This pattern gives better readability with X Fonts.
624 FIXME: Maybe the user should be allowed to decide which he wants. */
625 static const WORD Pattern[] = {0x55,0xFF,0xAA,0xFF,0x55,0xFF,0xAA,0xFF};
627 HBITMAP hbm = CreateBitmap( 8, 8, 1, 1, Pattern );
628 HDC hdcMem = CreateCompatibleDC(hDC);
629 HBITMAP hbmMem;
630 HBRUSH hBr;
631 RECT rect,rc2;
633 rect=*rc;
634 DrawTextA( hDC, text, -1, &rect, DT_SINGLELINE | DT_CALCRECT);
635 /* now text width and height are in rect.right and rect.bottom */
636 rc2=rect;
637 rect.left = rect.top = 0; /* drawing pos in hdcMem */
638 if (format & DT_CENTER) rect.left=(rc->right-rect.right)/2;
639 if (format & DT_VCENTER) rect.top=(rc->bottom-rect.bottom)/2;
640 hbmMem = CreateCompatibleBitmap( hDC,rect.right,rect.bottom );
641 SelectObject( hdcMem, hbmMem);
642 PatBlt( hdcMem,0,0,rect.right,rect.bottom,WHITENESS);
643 /* will be overwritten by DrawText, but just in case */
644 if (hFont) SelectObject( hdcMem, hFont);
645 DrawTextA( hdcMem, text, -1, &rc2, DT_SINGLELINE);
646 /* After draw: foreground = 0 bits, background = 1 bits */
647 hBr = SelectObject( hdcMem, CreatePatternBrush(hbm) );
648 DeleteObject( hbm );
649 PatBlt( hdcMem,0,0,rect.right,rect.bottom,0xAF0229);
650 /* only keep the foreground bits where pattern is 1 */
651 DeleteObject( SelectObject( hdcMem,hBr) );
652 BitBlt(hDC,rect.left,rect.top,rect.right,rect.bottom,hdcMem,0,0,SRCAND);
653 /* keep the background of the dest */
654 DeleteDC( hdcMem);
655 DeleteObject( hbmMem );
659 /**********************************************************************
660 * Check Box & Radio Button Functions
663 static void CB_Paint( WND *wndPtr, HDC hDC, WORD action )
665 RECT rbox, rtext, client;
666 HBRUSH hBrush;
667 int textlen, delta;
668 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
671 * if the button has a bitmap/icon, draw a normal pushbutton
672 * instead of a radion button.
674 if (infoPtr->hImage != 0)
676 BOOL bHighLighted = ((infoPtr->state & BUTTON_HIGHLIGHTED) ||
677 (infoPtr->state & BUTTON_CHECKED));
679 BUTTON_DrawPushButton(wndPtr,
680 hDC,
681 action,
682 bHighLighted);
683 return;
686 textlen = 0;
687 GetClientRect(wndPtr->hwndSelf, &client);
688 rbox = rtext = client;
690 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
692 /* Something is still not right, checkboxes (and edit controls)
693 * in wsping32 have white backgrounds instead of dark grey.
694 * BUTTON_SEND_CTLCOLOR() is even worse since it returns 0 in this
695 * particular case and the background is not painted at all.
698 hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
700 if (wndPtr->dwStyle & BS_LEFTTEXT)
702 /* magic +4 is what CTL3D expects */
704 rtext.right -= checkBoxWidth + 4;
705 rbox.left = rbox.right - checkBoxWidth;
707 else
709 rtext.left += checkBoxWidth + 4;
710 rbox.right = checkBoxWidth;
713 /* Draw the check-box bitmap */
715 if (wndPtr->text) textlen = strlen( wndPtr->text );
716 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
718 if( TWEAK_WineLook == WIN31_LOOK )
720 HDC hMemDC = CreateCompatibleDC( hDC );
721 int x = 0, y = 0;
722 delta = (rbox.bottom - rbox.top - checkBoxHeight) / 2;
724 /* Check in case the client area is smaller than the checkbox bitmap */
725 if (delta < 0) delta = 0;
727 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
728 else FillRect( hDC, &client, hBrush );
730 if (infoPtr->state & BUTTON_HIGHLIGHTED) x += 2 * checkBoxWidth;
731 if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) x += checkBoxWidth;
732 if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
733 ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) y += checkBoxHeight;
734 else if (infoPtr->state & BUTTON_3STATE) y += 2 * checkBoxHeight;
736 /* The bitmap for the radio button is not aligned with the
737 * left of the window, it is 1 pixel off. */
738 if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
739 ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
740 rbox.left += 1;
742 SelectObject( hMemDC, hbitmapCheckBoxes );
743 BitBlt( hDC, rbox.left, rbox.top + delta, checkBoxWidth,
744 checkBoxHeight, hMemDC, x, y, SRCCOPY );
745 DeleteDC( hMemDC );
747 else
749 UINT state;
751 if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
752 ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) state = DFCS_BUTTONRADIO;
753 else if (infoPtr->state & BUTTON_3STATE) state = DFCS_BUTTON3STATE;
754 else state = DFCS_BUTTONCHECK;
756 if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) state |= DFCS_CHECKED;
758 if (infoPtr->state & BUTTON_HIGHLIGHTED) state |= DFCS_PUSHED;
760 if (wndPtr->dwStyle & WS_DISABLED) state |= DFCS_INACTIVE;
762 DrawFrameControl( hDC, &rbox, DFC_BUTTON, state );
766 if( textlen && action != ODA_SELECT )
768 if (wndPtr->dwStyle & WS_DISABLED &&
769 GetSysColor(COLOR_GRAYTEXT)==GetBkColor(hDC)) {
770 /* don't write gray text on gray background */
771 PaintGrayOnGray( hDC, infoPtr->hFont, &rtext, wndPtr->text,
772 DT_VCENTER);
773 } else {
774 if (wndPtr->dwStyle & WS_DISABLED)
775 SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
776 DrawTextA( hDC, wndPtr->text, textlen, &rtext,
777 DT_SINGLELINE | DT_VCENTER );
782 if ((action == ODA_FOCUS) ||
783 ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
785 /* again, this is what CTL3D expects */
787 SetRectEmpty(&rbox);
788 if( textlen )
789 DrawTextA( hDC, wndPtr->text, textlen, &rbox,
790 DT_SINGLELINE | DT_CALCRECT );
791 textlen = rbox.bottom - rbox.top;
792 delta = ((rtext.bottom - rtext.top) - textlen)/2;
793 rbox.bottom = (rbox.top = rtext.top + delta - 1) + textlen + 2;
794 textlen = rbox.right - rbox.left;
795 rbox.right = (rbox.left += --rtext.left) + textlen + 2;
796 IntersectRect(&rbox, &rbox, &rtext);
797 DrawFocusRect( hDC, &rbox );
802 /**********************************************************************
803 * BUTTON_CheckAutoRadioButton
805 * wndPtr is checked, uncheck every other auto radio button in group
807 static void BUTTON_CheckAutoRadioButton( WND *wndPtr )
809 HWND parent, sibling, start;
810 if (!(wndPtr->dwStyle & WS_CHILD)) return;
811 parent = wndPtr->parent->hwndSelf;
812 /* assure that starting control is not disabled or invisible */
813 start = sibling = GetNextDlgGroupItem( parent, wndPtr->hwndSelf, TRUE );
816 WND *tmpWnd;
817 if (!sibling) break;
818 tmpWnd = WIN_FindWndPtr(sibling);
819 if ((wndPtr->hwndSelf != sibling) &&
820 ((tmpWnd->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
821 SendMessageA( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
822 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
823 WIN_ReleaseWndPtr(tmpWnd);
824 } while (sibling != start);
828 /**********************************************************************
829 * Group Box Functions
832 static void GB_Paint( WND *wndPtr, HDC hDC, WORD action )
834 RECT rc, rcFrame;
835 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
837 if (action != ODA_DRAWENTIRE) return;
839 BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
841 GetClientRect( wndPtr->hwndSelf, &rc);
842 if (TWEAK_WineLook == WIN31_LOOK) {
843 HPEN hPrevPen = SelectObject( hDC,
844 GetSysColorPen(COLOR_WINDOWFRAME));
845 HBRUSH hPrevBrush = SelectObject( hDC,
846 GetStockObject(NULL_BRUSH) );
848 Rectangle( hDC, rc.left, rc.top + 2, rc.right - 1, rc.bottom - 1 );
849 SelectObject( hDC, hPrevBrush );
850 SelectObject( hDC, hPrevPen );
851 } else {
852 TEXTMETRICA tm;
853 rcFrame = rc;
855 if (infoPtr->hFont)
856 SelectObject (hDC, infoPtr->hFont);
857 GetTextMetricsA (hDC, &tm);
858 rcFrame.top += (tm.tmHeight / 2) - 1;
859 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT);
862 if (wndPtr->text)
864 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
865 if (wndPtr->dwStyle & WS_DISABLED)
866 SetTextColor( hDC, GetSysColor(COLOR_GRAYTEXT) );
867 rc.left += 10;
868 DrawTextA( hDC, wndPtr->text, -1, &rc, DT_SINGLELINE | DT_NOCLIP );
873 /**********************************************************************
874 * User Button Functions
877 static void UB_Paint( WND *wndPtr, HDC hDC, WORD action )
879 RECT rc;
880 HBRUSH hBrush;
881 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
883 if (action == ODA_SELECT) return;
885 GetClientRect( wndPtr->hwndSelf, &rc);
887 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
888 hBrush = GetControlBrush16( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
890 FillRect( hDC, &rc, hBrush );
891 if ((action == ODA_FOCUS) ||
892 ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
893 DrawFocusRect( hDC, &rc );
897 /**********************************************************************
898 * Ownerdrawn Button Functions
901 static void OB_Paint( WND *wndPtr, HDC hDC, WORD action )
903 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
904 DRAWITEMSTRUCT dis;
906 dis.CtlType = ODT_BUTTON;
907 dis.CtlID = wndPtr->wIDmenu;
908 dis.itemID = 0;
909 dis.itemAction = action;
910 dis.itemState = ((infoPtr->state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
911 ((infoPtr->state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
912 ((wndPtr->dwStyle & WS_DISABLED) ? ODS_DISABLED : 0);
913 dis.hwndItem = wndPtr->hwndSelf;
914 dis.hDC = hDC;
915 dis.itemData = 0;
916 GetClientRect( wndPtr->hwndSelf, &dis.rcItem );
918 SetBkColor( hDC, GetSysColor( COLOR_BTNFACE ) );
920 SendMessageA( GetParent(wndPtr->hwndSelf), WM_DRAWITEM,
921 wndPtr->wIDmenu, (LPARAM)&dis );