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