Release 980822
[wine/multimedia.git] / controls / button.c
blob0466339c3f6f5354c875cb20fbeb1927e9e1794a
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 "win.h"
9 #include "graphics.h"
10 #include "button.h"
11 #include "windows.h"
13 static void PB_Paint( WND *wndPtr, HDC32 hDC, WORD action );
14 static void PB_PaintGrayOnGray(HDC32 hDC,HFONT32 hFont,RECT32 *rc,char *text);
15 static void CB_Paint( WND *wndPtr, HDC32 hDC, WORD action );
16 static void GB_Paint( WND *wndPtr, HDC32 hDC, WORD action );
17 static void UB_Paint( WND *wndPtr, HDC32 hDC, WORD action );
18 static void OB_Paint( WND *wndPtr, HDC32 hDC, WORD action );
19 static void BUTTON_CheckAutoRadioButton( WND *wndPtr );
21 #define MAX_BTN_TYPE 12
23 static const WORD maxCheckState[MAX_BTN_TYPE] =
25 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
26 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
27 BUTTON_CHECKED, /* BS_CHECKBOX */
28 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
29 BUTTON_CHECKED, /* BS_RADIOBUTTON */
30 BUTTON_3STATE, /* BS_3STATE */
31 BUTTON_3STATE, /* BS_AUTO3STATE */
32 BUTTON_UNCHECKED, /* BS_GROUPBOX */
33 BUTTON_UNCHECKED, /* BS_USERBUTTON */
34 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
35 BUTTON_UNCHECKED, /* Not defined */
36 BUTTON_UNCHECKED /* BS_OWNERDRAW */
39 typedef void (*pfPaint)( WND *wndPtr, HDC32 hdc, WORD action );
41 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
43 PB_Paint, /* BS_PUSHBUTTON */
44 PB_Paint, /* BS_DEFPUSHBUTTON */
45 CB_Paint, /* BS_CHECKBOX */
46 CB_Paint, /* BS_AUTOCHECKBOX */
47 CB_Paint, /* BS_RADIOBUTTON */
48 CB_Paint, /* BS_3STATE */
49 CB_Paint, /* BS_AUTO3STATE */
50 GB_Paint, /* BS_GROUPBOX */
51 UB_Paint, /* BS_USERBUTTON */
52 CB_Paint, /* BS_AUTORADIOBUTTON */
53 NULL, /* Not defined */
54 OB_Paint /* BS_OWNERDRAW */
57 #define PAINT_BUTTON(wndPtr,style,action) \
58 if (btnPaintFunc[style]) { \
59 HDC32 hdc = GetDC32( (wndPtr)->hwndSelf ); \
60 (btnPaintFunc[style])(wndPtr,hdc,action); \
61 ReleaseDC32( (wndPtr)->hwndSelf, hdc ); }
63 #define BUTTON_SEND_CTLCOLOR(wndPtr,hdc) \
64 SendMessage32A( GetParent32((wndPtr)->hwndSelf), WM_CTLCOLORBTN, \
65 (hdc), (wndPtr)->hwndSelf )
67 static HBITMAP32 hbitmapCheckBoxes = 0;
68 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
71 /***********************************************************************
72 * ButtonWndProc
74 LRESULT WINAPI ButtonWndProc( HWND32 hWnd, UINT32 uMsg,
75 WPARAM32 wParam, LPARAM lParam )
77 RECT32 rect;
78 POINT32 pt = { LOWORD(lParam), HIWORD(lParam) };
79 WND *wndPtr = WIN_FindWndPtr(hWnd);
80 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
81 LONG style = wndPtr->dwStyle & 0x0f;
83 switch (uMsg)
85 case WM_GETDLGCODE:
86 switch(style)
88 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
89 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
90 case BS_RADIOBUTTON:
91 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
92 default: return DLGC_BUTTON;
95 case WM_ENABLE:
96 PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
97 break;
99 case WM_CREATE:
100 if (!hbitmapCheckBoxes)
102 BITMAP32 bmp;
103 hbitmapCheckBoxes = LoadBitmap32A(0, MAKEINTRESOURCE32A(OBM_CHECKBOXES));
104 GetObject32A( hbitmapCheckBoxes, sizeof(bmp), &bmp );
105 checkBoxWidth = bmp.bmWidth / 4;
106 checkBoxHeight = bmp.bmHeight / 3;
108 if (style < 0L || style >= MAX_BTN_TYPE) return -1; /* abort */
109 infoPtr->state = BUTTON_UNCHECKED;
110 infoPtr->hFont = 0;
111 return 0;
113 case WM_ERASEBKGND:
114 return 1;
116 case WM_PAINT:
117 if (btnPaintFunc[style])
119 PAINTSTRUCT32 ps;
120 HDC32 hdc = wParam ? (HDC32)wParam : BeginPaint32( hWnd, &ps );
121 SetBkMode32( hdc, OPAQUE );
122 (btnPaintFunc[style])( wndPtr, hdc, ODA_DRAWENTIRE );
123 if( !wParam ) EndPaint32( hWnd, &ps );
125 break;
127 case WM_LBUTTONDOWN:
128 SendMessage32A( hWnd, BM_SETSTATE32, TRUE, 0 );
129 SetFocus32( hWnd );
130 SetCapture32( hWnd );
131 break;
133 case WM_LBUTTONUP:
134 ReleaseCapture();
135 if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) break;
136 SendMessage32A( hWnd, BM_SETSTATE32, FALSE, 0 );
137 GetClientRect32( hWnd, &rect );
138 if (PtInRect32( &rect, pt ))
140 switch(style)
142 case BS_AUTOCHECKBOX:
143 SendMessage32A( hWnd, BM_SETCHECK32,
144 !(infoPtr->state & BUTTON_CHECKED), 0 );
145 break;
146 case BS_AUTORADIOBUTTON:
147 SendMessage32A( hWnd, BM_SETCHECK32, TRUE, 0 );
148 break;
149 case BS_AUTO3STATE:
150 SendMessage32A( hWnd, BM_SETCHECK32,
151 (infoPtr->state & BUTTON_3STATE) ? 0 :
152 ((infoPtr->state & 3) + 1), 0 );
153 break;
155 SendMessage32A( GetParent32(hWnd), WM_COMMAND,
156 MAKEWPARAM( wndPtr->wIDmenu, BN_CLICKED ), hWnd);
158 break;
160 case WM_MOUSEMOVE:
161 if (GetCapture32() == hWnd)
163 GetClientRect32( hWnd, &rect );
164 SendMessage32A( hWnd, BM_SETSTATE32, PtInRect32(&rect, pt), 0 );
166 break;
168 case WM_NCHITTEST:
169 if(style == BS_GROUPBOX) return HTTRANSPARENT;
170 return DefWindowProc32A( hWnd, uMsg, wParam, lParam );
172 case WM_SETTEXT:
173 DEFWND_SetText( wndPtr, (LPCSTR)lParam );
174 if( wndPtr->dwStyle & WS_VISIBLE )
175 PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
176 return 0;
178 case WM_SETFONT:
179 infoPtr->hFont = (HFONT16)wParam;
180 if (lParam && (wndPtr->dwStyle & WS_VISIBLE))
181 PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
182 break;
184 case WM_GETFONT:
185 return infoPtr->hFont;
187 case WM_SETFOCUS:
188 infoPtr->state |= BUTTON_HASFOCUS;
189 PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
190 break;
192 case WM_KILLFOCUS:
193 infoPtr->state &= ~BUTTON_HASFOCUS;
194 PAINT_BUTTON( wndPtr, style, ODA_FOCUS );
195 InvalidateRect32( hWnd, NULL, TRUE );
196 break;
198 case WM_SYSCOLORCHANGE:
199 InvalidateRect32( hWnd, NULL, FALSE );
200 break;
202 case BM_SETSTYLE16:
203 case BM_SETSTYLE32:
204 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
205 wndPtr->dwStyle = (wndPtr->dwStyle & 0xfffffff0)
206 | (wParam & 0x0000000f);
207 style = wndPtr->dwStyle & 0x0000000f;
208 PAINT_BUTTON( wndPtr, style, ODA_DRAWENTIRE );
209 break;
211 case BM_GETCHECK16:
212 case BM_GETCHECK32:
213 return infoPtr->state & 3;
215 case BM_SETCHECK16:
216 case BM_SETCHECK32:
217 if (wParam > maxCheckState[style]) wParam = maxCheckState[style];
218 if ((infoPtr->state & 3) != wParam)
220 infoPtr->state = (infoPtr->state & ~3) | wParam;
221 PAINT_BUTTON( wndPtr, style, ODA_SELECT );
223 if ((style == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED))
224 BUTTON_CheckAutoRadioButton( wndPtr );
225 break;
227 case BM_GETSTATE16:
228 case BM_GETSTATE32:
229 return infoPtr->state;
231 case BM_SETSTATE16:
232 case BM_SETSTATE32:
233 if (wParam)
235 if (infoPtr->state & BUTTON_HIGHLIGHTED) break;
236 infoPtr->state |= BUTTON_HIGHLIGHTED;
238 else
240 if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) break;
241 infoPtr->state &= ~BUTTON_HIGHLIGHTED;
243 PAINT_BUTTON( wndPtr, style, ODA_SELECT );
244 break;
246 default:
247 return DefWindowProc32A(hWnd, uMsg, wParam, lParam);
249 return 0;
253 /**********************************************************************
254 * Push Button Functions
257 static void PB_Paint( WND *wndPtr, HDC32 hDC, WORD action )
259 RECT32 rc;
260 HPEN32 hOldPen;
261 HBRUSH32 hOldBrush;
262 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
264 GetClientRect32( wndPtr->hwndSelf, &rc );
266 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
267 if (infoPtr->hFont) SelectObject32( hDC, infoPtr->hFont );
268 BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
269 hOldPen = (HPEN32)SelectObject32(hDC, GetSysColorPen32(COLOR_WINDOWFRAME));
270 hOldBrush =(HBRUSH32)SelectObject32(hDC,GetSysColorBrush32(COLOR_BTNFACE));
271 SetBkMode32(hDC, TRANSPARENT);
272 Rectangle32(hDC, rc.left, rc.top, rc.right, rc.bottom);
273 if (action == ODA_DRAWENTIRE)
275 SetPixel32( hDC, rc.left, rc.top, GetSysColor32(COLOR_WINDOW) );
276 SetPixel32( hDC, rc.left, rc.bottom-1, GetSysColor32(COLOR_WINDOW) );
277 SetPixel32( hDC, rc.right-1, rc.top, GetSysColor32(COLOR_WINDOW) );
278 SetPixel32( hDC, rc.right-1, rc.bottom-1, GetSysColor32(COLOR_WINDOW));
280 InflateRect32( &rc, -1, -1 );
282 if ((wndPtr->dwStyle & 0x000f) == BS_DEFPUSHBUTTON)
284 Rectangle32(hDC, rc.left, rc.top, rc.right, rc.bottom);
285 InflateRect32( &rc, -1, -1 );
288 if (infoPtr->state & BUTTON_HIGHLIGHTED)
290 /* draw button shadow: */
291 SelectObject32(hDC, GetSysColorBrush32(COLOR_BTNSHADOW));
292 PatBlt32(hDC, rc.left, rc.top, 1, rc.bottom-rc.top, PATCOPY );
293 PatBlt32(hDC, rc.left, rc.top, rc.right-rc.left, 1, PATCOPY );
294 rc.left += 2; /* To position the text down and right */
295 rc.top += 2;
297 else GRAPH_DrawReliefRect( hDC, &rc, 2, 2, FALSE );
299 /* draw button label, if any: */
300 if (wndPtr->text && wndPtr->text[0])
302 LOGBRUSH32 lb;
303 GetObject32A( GetSysColorBrush32(COLOR_BTNFACE), sizeof(lb), &lb );
304 if (wndPtr->dwStyle & WS_DISABLED &&
305 GetSysColor32(COLOR_GRAYTEXT)==lb.lbColor)
306 /* don't write gray text on gray bkg */
307 PB_PaintGrayOnGray(hDC,infoPtr->hFont,&rc,wndPtr->text);
308 else
310 SetTextColor32( hDC, (wndPtr->dwStyle & WS_DISABLED) ?
311 GetSysColor32(COLOR_GRAYTEXT) :
312 GetSysColor32(COLOR_BTNTEXT) );
313 DrawText32A( hDC, wndPtr->text, -1, &rc,
314 DT_SINGLELINE | DT_CENTER | DT_VCENTER );
315 /* do we have the focus? */
316 if (infoPtr->state & BUTTON_HASFOCUS)
318 RECT32 r = { 0, 0, 0, 0 };
319 INT32 xdelta, ydelta;
321 DrawText32A( hDC, wndPtr->text, -1, &r,
322 DT_SINGLELINE | DT_CALCRECT );
323 xdelta = ((rc.right - rc.left) - (r.right - r.left) - 1) / 2;
324 ydelta = ((rc.bottom - rc.top) - (r.bottom - r.top) - 1) / 2;
325 if (xdelta < 0) xdelta = 0;
326 if (ydelta < 0) ydelta = 0;
327 InflateRect32( &rc, -xdelta, -ydelta );
328 DrawFocusRect32( hDC, &rc );
333 SelectObject32( hDC, hOldPen );
334 SelectObject32( hDC, hOldBrush );
338 /**********************************************************************
339 * Push Button sub function [internal]
340 * using a raster brush to avoid gray text on gray background
343 void PB_PaintGrayOnGray(HDC32 hDC,HFONT32 hFont,RECT32 *rc,char *text)
345 static const int Pattern[] = {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55};
346 HBITMAP32 hbm = CreateBitmap32( 8, 8, 1, 1, Pattern );
347 HDC32 hdcMem = CreateCompatibleDC32(hDC);
348 HBITMAP32 hbmMem;
349 HBRUSH32 hBr;
350 RECT32 rect,rc2;
352 rect=*rc;
353 DrawText32A( hDC, text, -1, &rect, DT_SINGLELINE | DT_CALCRECT);
354 rc2=rect;
355 rect.left=(rc->right-rect.right)/2; /* for centering text bitmap */
356 rect.top=(rc->bottom-rect.bottom)/2;
357 hbmMem = CreateCompatibleBitmap32( hDC,rect.right,rect.bottom );
358 SelectObject32( hdcMem, hbmMem);
359 hBr = SelectObject32( hdcMem, CreatePatternBrush32(hbm) );
360 DeleteObject32( hbm );
361 PatBlt32( hdcMem,0,0,rect.right,rect.bottom,WHITENESS);
362 if (hFont) SelectObject32( hdcMem, hFont);
363 DrawText32A( hdcMem, text, -1, &rc2, DT_SINGLELINE);
364 PatBlt32( hdcMem,0,0,rect.right,rect.bottom,0xFA0089);
365 DeleteObject32( SelectObject32( hdcMem,hBr) );
366 BitBlt32(hDC,rect.left,rect.top,rect.right,rect.bottom,hdcMem,0,0,0x990000);
367 DeleteDC32( hdcMem);
368 DeleteObject32( hbmMem );
372 /**********************************************************************
373 * Check Box & Radio Button Functions
376 static void CB_Paint( WND *wndPtr, HDC32 hDC, WORD action )
378 RECT32 rbox, rtext, client;
379 HBRUSH32 hBrush;
380 int textlen, delta;
381 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
383 textlen = 0;
384 GetClientRect32(wndPtr->hwndSelf, &client);
385 rbox = rtext = client;
387 if (infoPtr->hFont) SelectObject32( hDC, infoPtr->hFont );
389 /* Something is still not right, checkboxes (and edit controls)
390 * in wsping32 have white backgrounds instead of dark grey.
391 * BUTTON_SEND_CTLCOLOR() is even worse since it returns 0 in this
392 * particular case and the background is not painted at all.
395 hBrush = GetControlBrush( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
397 if (wndPtr->dwStyle & BS_LEFTTEXT)
399 /* magic +4 is what CTL3D expects */
401 rtext.right -= checkBoxWidth + 4;
402 rbox.left = rbox.right - checkBoxWidth;
404 else
406 rtext.left += checkBoxWidth + 4;
407 rbox.right = checkBoxWidth;
410 /* Draw the check-box bitmap */
412 if (wndPtr->text) textlen = strlen( wndPtr->text );
413 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
415 int x = 0, y = 0;
416 delta = (rbox.bottom - rbox.top - checkBoxHeight) >> 1;
418 if (action == ODA_SELECT) FillRect32( hDC, &rbox, hBrush );
419 else FillRect32( hDC, &client, hBrush );
421 if (infoPtr->state & BUTTON_HIGHLIGHTED) x += 2 * checkBoxWidth;
422 if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) x += checkBoxWidth;
423 if (((wndPtr->dwStyle & 0x0f) == BS_RADIOBUTTON) ||
424 ((wndPtr->dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) y += checkBoxHeight;
425 else if (infoPtr->state & BUTTON_3STATE) y += 2 * checkBoxHeight;
427 GRAPH_DrawBitmap( hDC, hbitmapCheckBoxes, rbox.left, rbox.top + delta,
428 x, y, checkBoxWidth, checkBoxHeight, FALSE );
429 if( textlen && action != ODA_SELECT )
431 if (wndPtr->dwStyle & WS_DISABLED)
432 SetTextColor32( hDC, GetSysColor32(COLOR_GRAYTEXT) );
433 DrawText32A( hDC, wndPtr->text, textlen, &rtext,
434 DT_SINGLELINE | DT_VCENTER );
435 textlen = 0; /* skip DrawText() below */
439 if ((action == ODA_FOCUS) ||
440 ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
442 /* again, this is what CTL3D expects */
444 SetRectEmpty32(&rbox);
445 if( textlen )
446 DrawText32A( hDC, wndPtr->text, textlen, &rbox,
447 DT_SINGLELINE | DT_CALCRECT );
448 textlen = rbox.bottom - rbox.top;
449 delta = ((rtext.bottom - rtext.top) - textlen)/2;
450 rbox.bottom = (rbox.top = rtext.top + delta - 1) + textlen + 2;
451 textlen = rbox.right - rbox.left;
452 rbox.right = (rbox.left += --rtext.left) + textlen + 2;
453 IntersectRect32(&rbox, &rbox, &rtext);
454 DrawFocusRect32( hDC, &rbox );
459 /**********************************************************************
460 * BUTTON_CheckAutoRadioButton
462 * wndPtr is checked, uncheck every other auto radio button in group
464 static void BUTTON_CheckAutoRadioButton( WND *wndPtr )
466 HWND32 parent, sibling, start;
467 if (!(wndPtr->dwStyle & WS_CHILD)) return;
468 parent = wndPtr->parent->hwndSelf;
469 /* assure that starting control is not disabled or invisible */
470 start = sibling = GetNextDlgGroupItem32( parent, wndPtr->hwndSelf, TRUE );
473 if (!sibling) break;
474 if ((wndPtr->hwndSelf != sibling) &&
475 ((WIN_FindWndPtr(sibling)->dwStyle & 0x0f) == BS_AUTORADIOBUTTON))
476 SendMessage32A( sibling, BM_SETCHECK32, BUTTON_UNCHECKED, 0 );
477 sibling = GetNextDlgGroupItem32( parent, sibling, FALSE );
478 } while (sibling != start);
482 /**********************************************************************
483 * Group Box Functions
486 static void GB_Paint( WND *wndPtr, HDC32 hDC, WORD action )
488 RECT32 rc;
489 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
491 if (action != ODA_DRAWENTIRE) return;
493 BUTTON_SEND_CTLCOLOR( wndPtr, hDC );
495 GetClientRect32( wndPtr->hwndSelf, &rc);
496 GRAPH_DrawRectangle( hDC, rc.left, rc.top + 2, rc.right - 1, rc.bottom - 1,
497 GetSysColorPen32(COLOR_WINDOWFRAME) );
498 if (wndPtr->text)
500 if (infoPtr->hFont) SelectObject32( hDC, infoPtr->hFont );
501 if (wndPtr->dwStyle & WS_DISABLED)
502 SetTextColor32( hDC, GetSysColor32(COLOR_GRAYTEXT) );
503 rc.left += 10;
504 DrawText32A( hDC, wndPtr->text, -1, &rc, DT_SINGLELINE | DT_NOCLIP );
509 /**********************************************************************
510 * User Button Functions
513 static void UB_Paint( WND *wndPtr, HDC32 hDC, WORD action )
515 RECT32 rc;
516 HBRUSH32 hBrush;
517 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
519 if (action == ODA_SELECT) return;
521 GetClientRect32( wndPtr->hwndSelf, &rc);
523 if (infoPtr->hFont) SelectObject32( hDC, infoPtr->hFont );
524 hBrush = GetControlBrush( wndPtr->hwndSelf, hDC, CTLCOLOR_BTN );
526 FillRect32( hDC, &rc, hBrush );
527 if ((action == ODA_FOCUS) ||
528 ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
529 DrawFocusRect32( hDC, &rc );
533 /**********************************************************************
534 * Ownerdrawn Button Functions
537 static void OB_Paint( WND *wndPtr, HDC32 hDC, WORD action )
539 BUTTONINFO *infoPtr = (BUTTONINFO *)wndPtr->wExtra;
540 DRAWITEMSTRUCT32 dis;
542 dis.CtlType = ODT_BUTTON;
543 dis.CtlID = wndPtr->wIDmenu;
544 dis.itemID = 0;
545 dis.itemAction = action;
546 dis.itemState = ((infoPtr->state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
547 ((infoPtr->state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
548 ((wndPtr->dwStyle & WS_DISABLED) ? ODS_DISABLED : 0);
549 dis.hwndItem = wndPtr->hwndSelf;
550 dis.hDC = hDC;
551 dis.itemData = 0;
552 GetClientRect32( wndPtr->hwndSelf, &dis.rcItem );
553 SendMessage32A( GetParent32(wndPtr->hwndSelf), WM_DRAWITEM,
554 wndPtr->wIDmenu, (LPARAM)&dis );