Include wine/test.h before windows headers.
[wine/multimedia.git] / controls / button.c
blobc99512f855455962eb7425aee5d4e69a40fc8e58
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
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <string.h>
23 #include <stdlib.h>
25 #include "winbase.h"
26 #include "windef.h"
27 #include "wingdi.h"
28 #include "wine/winuser16.h"
29 #include "controls.h"
30 #include "user.h"
32 /* GetWindowLong offsets for window extra information */
33 #define STATE_GWL_OFFSET 0
34 #define HFONT_GWL_OFFSET (sizeof(LONG))
35 #define HIMAGE_GWL_OFFSET (2*sizeof(LONG))
36 #define NB_EXTRA_BYTES (3*sizeof(LONG))
38 /* Button state values */
39 #define BUTTON_UNCHECKED 0x00
40 #define BUTTON_CHECKED 0x01
41 #define BUTTON_3STATE 0x02
42 #define BUTTON_HIGHLIGHTED 0x04
43 #define BUTTON_HASFOCUS 0x08
44 #define BUTTON_NSTATES 0x0F
45 /* undocumented flags */
46 #define BUTTON_BTNPRESSED 0x40
47 #define BUTTON_UNKNOWN2 0x20
48 #define BUTTON_UNKNOWN3 0x10
50 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
51 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
52 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
53 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
54 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
55 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
56 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
57 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
59 #define MAX_BTN_TYPE 12
61 static const WORD maxCheckState[MAX_BTN_TYPE] =
63 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
64 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
65 BUTTON_CHECKED, /* BS_CHECKBOX */
66 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
67 BUTTON_CHECKED, /* BS_RADIOBUTTON */
68 BUTTON_3STATE, /* BS_3STATE */
69 BUTTON_3STATE, /* BS_AUTO3STATE */
70 BUTTON_UNCHECKED, /* BS_GROUPBOX */
71 BUTTON_UNCHECKED, /* BS_USERBUTTON */
72 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
73 BUTTON_UNCHECKED, /* Not defined */
74 BUTTON_UNCHECKED /* BS_OWNERDRAW */
77 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
79 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
81 PB_Paint, /* BS_PUSHBUTTON */
82 PB_Paint, /* BS_DEFPUSHBUTTON */
83 CB_Paint, /* BS_CHECKBOX */
84 CB_Paint, /* BS_AUTOCHECKBOX */
85 CB_Paint, /* BS_RADIOBUTTON */
86 CB_Paint, /* BS_3STATE */
87 CB_Paint, /* BS_AUTO3STATE */
88 GB_Paint, /* BS_GROUPBOX */
89 UB_Paint, /* BS_USERBUTTON */
90 CB_Paint, /* BS_AUTORADIOBUTTON */
91 NULL, /* Not defined */
92 OB_Paint /* BS_OWNERDRAW */
95 static HBITMAP hbitmapCheckBoxes = 0;
96 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
99 /*********************************************************************
100 * button class descriptor
102 const struct builtin_class_descr BUTTON_builtin_class =
104 "Button", /* name */
105 CS_GLOBALCLASS | CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
106 ButtonWndProcA, /* procA */
107 ButtonWndProcW, /* procW */
108 NB_EXTRA_BYTES, /* extra */
109 IDC_ARROWA, /* cursor */
110 0 /* brush */
114 inline static LONG get_button_state( HWND hwnd )
116 return GetWindowLongA( hwnd, STATE_GWL_OFFSET );
119 inline static void set_button_state( HWND hwnd, LONG state )
121 SetWindowLongA( hwnd, STATE_GWL_OFFSET, state );
124 inline static HFONT get_button_font( HWND hwnd )
126 return GetWindowLongA( hwnd, HFONT_GWL_OFFSET );
129 inline static void set_button_font( HWND hwnd, HFONT font )
131 SetWindowLongA( hwnd, HFONT_GWL_OFFSET, font );
134 inline static UINT get_button_type( LONG window_style )
136 return (window_style & 0x0f);
139 /* paint a button of any type */
140 inline static void paint_button( HWND hwnd, LONG style, UINT action )
142 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
144 HDC hdc = GetDC( hwnd );
145 btnPaintFunc[style]( hwnd, hdc, action );
146 ReleaseDC( hwnd, hdc );
150 /* retrieve the button text; returned buffer must be freed by caller */
151 inline static WCHAR *get_button_text( HWND hwnd )
153 INT len = GetWindowTextLengthW( hwnd );
154 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
155 if (buffer) GetWindowTextW( hwnd, buffer, len + 1 );
156 return buffer;
159 /***********************************************************************
160 * ButtonWndProc_common
162 static LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
163 WPARAM wParam, LPARAM lParam, BOOL unicode )
165 RECT rect;
166 POINT pt;
167 LONG style = GetWindowLongA( hWnd, GWL_STYLE );
168 UINT btn_type = get_button_type( style );
169 LONG state;
170 HANDLE oldHbitmap;
172 pt.x = LOWORD(lParam);
173 pt.y = HIWORD(lParam);
175 switch (uMsg)
177 case WM_GETDLGCODE:
178 switch(btn_type)
180 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
181 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
182 case BS_RADIOBUTTON:
183 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
184 default: return DLGC_BUTTON;
187 case WM_ENABLE:
188 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
189 break;
191 case WM_CREATE:
192 if (!hbitmapCheckBoxes)
194 BITMAP bmp;
195 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
196 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
197 checkBoxWidth = bmp.bmWidth / 4;
198 checkBoxHeight = bmp.bmHeight / 3;
200 if (btn_type < 0L || btn_type >= MAX_BTN_TYPE)
201 return -1; /* abort */
202 set_button_state( hWnd, BUTTON_UNCHECKED );
203 return 0;
205 case WM_ERASEBKGND:
206 return 1;
208 case WM_PAINT:
209 if (btnPaintFunc[btn_type])
211 PAINTSTRUCT ps;
212 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
213 int nOldMode = SetBkMode( hdc, OPAQUE );
214 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
215 SetBkMode(hdc, nOldMode); /* reset painting mode */
216 if( !wParam ) EndPaint( hWnd, &ps );
218 break;
220 case WM_KEYDOWN:
221 if (wParam == VK_SPACE)
223 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
224 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
226 break;
228 case WM_LBUTTONDBLCLK:
229 if(style & BS_NOTIFY ||
230 btn_type == BS_RADIOBUTTON ||
231 btn_type == BS_USERBUTTON ||
232 btn_type == BS_OWNERDRAW)
234 SendMessageW( GetParent(hWnd), WM_COMMAND,
235 MAKEWPARAM( GetWindowLongA(hWnd,GWL_ID), BN_DOUBLECLICKED ),
236 (LPARAM)hWnd);
237 break;
239 /* fall through */
240 case WM_LBUTTONDOWN:
241 SetCapture( hWnd );
242 SetFocus( hWnd );
243 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
244 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
245 break;
247 case WM_KEYUP:
248 if (wParam != VK_SPACE)
249 break;
250 /* fall through */
251 case WM_LBUTTONUP:
252 state = get_button_state( hWnd );
253 if (!(state & BUTTON_BTNPRESSED)) break;
254 state &= BUTTON_NSTATES;
255 set_button_state( hWnd, state );
256 if (!(state & BUTTON_HIGHLIGHTED))
258 ReleaseCapture();
259 break;
261 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
262 ReleaseCapture();
263 GetClientRect( hWnd, &rect );
264 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
266 state = get_button_state( hWnd );
267 switch(btn_type)
269 case BS_AUTOCHECKBOX:
270 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
271 break;
272 case BS_AUTORADIOBUTTON:
273 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
274 break;
275 case BS_AUTO3STATE:
276 SendMessageW( hWnd, BM_SETCHECK,
277 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
278 break;
280 SendMessageW( GetParent(hWnd), WM_COMMAND,
281 MAKEWPARAM( GetWindowLongA(hWnd,GWL_ID), BN_CLICKED ), (LPARAM)hWnd);
283 break;
285 case WM_CAPTURECHANGED:
286 state = get_button_state( hWnd );
287 if (state & BUTTON_BTNPRESSED)
289 state &= BUTTON_NSTATES;
290 set_button_state( hWnd, state );
291 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
293 break;
295 case WM_MOUSEMOVE:
296 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
298 GetClientRect( hWnd, &rect );
299 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
301 break;
303 case WM_SETTEXT:
304 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
305 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
306 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
307 return 1; /* success. FIXME: check text length */
309 case WM_SETFONT:
310 set_button_font( hWnd, wParam );
311 if (lParam) paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
312 break;
314 case WM_GETFONT:
315 return get_button_font( hWnd );
317 case WM_SETFOCUS:
318 if ((btn_type == BS_RADIOBUTTON || btn_type == BS_AUTORADIOBUTTON) && (GetCapture() != hWnd) &&
319 !(SendMessageW(hWnd, BM_GETCHECK, 0, 0) & BST_CHECKED))
321 /* The notification is sent when the button (BS_AUTORADIOBUTTON)
322 is unchecked and the focus was not given by a mouse click. */
323 if (btn_type == BS_AUTORADIOBUTTON)
324 SendMessageW( hWnd, BM_SETCHECK, BUTTON_CHECKED, 0 );
325 SendMessageW( GetParent(hWnd), WM_COMMAND,
326 MAKEWPARAM( GetWindowLongA(hWnd,GWL_ID), BN_CLICKED ), (LPARAM)hWnd);
328 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
329 paint_button( hWnd, btn_type, ODA_FOCUS );
330 break;
332 case WM_KILLFOCUS:
333 set_button_state( hWnd, get_button_state(hWnd) & ~BUTTON_HASFOCUS );
334 paint_button( hWnd, btn_type, ODA_FOCUS );
335 InvalidateRect( hWnd, NULL, TRUE );
336 break;
338 case WM_SYSCOLORCHANGE:
339 InvalidateRect( hWnd, NULL, FALSE );
340 break;
342 case BM_SETSTYLE16:
343 case BM_SETSTYLE:
344 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
345 btn_type = wParam & 0x0f;
346 style = (style & ~0x0f) | btn_type;
347 SetWindowLongA( hWnd, GWL_STYLE, style );
349 /* Only redraw if lParam flag is set.*/
350 if (lParam)
351 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
353 break;
355 case BM_CLICK:
356 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
357 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
358 break;
360 case BM_SETIMAGE:
361 /* Check that image format matches button style */
362 switch (style & (BS_BITMAP|BS_ICON))
364 case BS_BITMAP:
365 if (wParam != IMAGE_BITMAP) return 0;
366 break;
367 case BS_ICON:
368 if (wParam != IMAGE_ICON) return 0;
369 break;
370 default:
371 return 0;
373 oldHbitmap = SetWindowLongA( hWnd, HIMAGE_GWL_OFFSET, lParam );
374 InvalidateRect( hWnd, NULL, FALSE );
375 return oldHbitmap;
377 case BM_GETIMAGE:
378 return GetWindowLongA( hWnd, HIMAGE_GWL_OFFSET );
380 case BM_GETCHECK16:
381 case BM_GETCHECK:
382 return get_button_state( hWnd ) & 3;
384 case BM_SETCHECK16:
385 case BM_SETCHECK:
386 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
387 state = get_button_state( hWnd );
388 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
390 if (wParam) style |= WS_TABSTOP;
391 else style &= ~WS_TABSTOP;
392 SetWindowLongA( hWnd, GWL_STYLE, style );
394 if ((state & 3) != wParam)
396 set_button_state( hWnd, (state & ~3) | wParam );
397 paint_button( hWnd, btn_type, ODA_SELECT );
399 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
400 BUTTON_CheckAutoRadioButton( hWnd );
401 break;
403 case BM_GETSTATE16:
404 case BM_GETSTATE:
405 return get_button_state( hWnd );
407 case BM_SETSTATE16:
408 case BM_SETSTATE:
409 state = get_button_state( hWnd );
410 if (wParam)
412 if (state & BUTTON_HIGHLIGHTED) break;
413 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
415 else
417 if (!(state & BUTTON_HIGHLIGHTED)) break;
418 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
420 paint_button( hWnd, btn_type, ODA_SELECT );
421 break;
423 case WM_NCHITTEST:
424 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
425 /* fall through */
426 default:
427 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
428 DefWindowProcA(hWnd, uMsg, wParam, lParam);
430 return 0;
433 /***********************************************************************
434 * ButtonWndProcW
435 * The button window procedure. This is just a wrapper which locks
436 * the passed HWND and calls the real window procedure (with a WND*
437 * pointer pointing to the locked windowstructure).
439 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
441 if (!IsWindow( hWnd )) return 0;
442 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
446 /***********************************************************************
447 * ButtonWndProcA
449 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
451 if (!IsWindow( hWnd )) return 0;
452 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
456 /**********************************************************************
457 * Convert button styles to flags used by DrawText.
458 * TODO: handle WS_EX_RIGHT extended style.
460 static UINT BUTTON_BStoDT(DWORD style)
462 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
464 /* "Convert" pushlike buttons to pushbuttons */
465 if (style & BS_PUSHLIKE)
466 style &= ~0x0F;
468 if (!(style & BS_MULTILINE))
469 dtStyle |= DT_SINGLELINE;
470 else
471 dtStyle |= DT_WORDBREAK;
473 switch (style & BS_CENTER)
475 case BS_LEFT: /* DT_LEFT is 0 */ break;
476 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
477 case BS_CENTER: dtStyle |= DT_CENTER; break;
478 default:
479 /* Pushbutton's text is centered by default */
480 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
481 /* all other flavours have left aligned text */
484 /* DrawText ignores vertical alignment for multiline text,
485 * but we use these flags to align label manualy.
487 if (get_button_type(style) != BS_GROUPBOX)
489 switch (style & BS_VCENTER)
491 case BS_TOP: /* DT_TOP is 0 */ break;
492 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
493 case BS_VCENTER: /* fall through */
494 default: dtStyle |= DT_VCENTER; break;
497 else
498 /* GroupBox's text is always single line and is top aligned. */
499 dtStyle |= DT_SINGLELINE;
501 return dtStyle;
504 /**********************************************************************
505 * BUTTON_CalcLabelRect
507 * Calculates label's rectangle depending on button style.
509 * Returns flags to be passed to DrawText.
510 * Calculated rectangle doesn't take into account button state
511 * (pushed, etc.). If there is nothing to draw (no text/image) output
512 * rectangle is empty, and return value is (UINT)-1.
514 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
516 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
517 WCHAR *text;
518 ICONINFO iconInfo;
519 BITMAP bm;
520 UINT dtStyle = BUTTON_BStoDT(style);
521 RECT r = *rc;
522 INT n;
524 /* Calculate label rectangle according to label type */
525 switch (style & (BS_ICON|BS_BITMAP))
527 case BS_TEXT:
528 if (!(text = get_button_text( hwnd ))) goto empty_rect;
529 if (!text[0])
531 HeapFree( GetProcessHeap(), 0, text );
532 goto empty_rect;
534 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
535 HeapFree( GetProcessHeap(), 0, text );
536 break;
538 case BS_ICON:
539 if (!GetIconInfo((HICON)GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
540 goto empty_rect;
542 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
544 r.right = r.left + bm.bmWidth;
545 r.bottom = r.top + bm.bmHeight;
547 DeleteObject(iconInfo.hbmColor);
548 DeleteObject(iconInfo.hbmMask);
549 break;
551 case BS_BITMAP:
552 if (!GetObjectW( (HANDLE)GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
553 goto empty_rect;
555 r.right = r.left + bm.bmWidth;
556 r.bottom = r.top + bm.bmHeight;
557 break;
559 default:
560 empty_rect:
561 r.right = r.left;
562 r.bottom = r.top;
563 return (UINT)(LONG)-1;
566 /* Position label inside bounding rectangle according to
567 * alignment flags. (calculated rect is always left-top aligned).
568 * If label is aligned to any side - shift label in opposite
569 * direction to leave extra space for focus rectangle.
571 switch (dtStyle & (DT_CENTER|DT_RIGHT))
573 case DT_LEFT: r.left++; r.right++; break;
574 case DT_CENTER: n = r.right - r.left;
575 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
576 r.right = r.left + n; break;
577 case DT_RIGHT: n = r.right - r.left;
578 r.right = rc->right - 1;
579 r.left = r.right - n;
580 break;
583 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
585 case DT_TOP: r.top++; r.bottom++; break;
586 case DT_VCENTER: n = r.bottom - r.top;
587 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
588 r.bottom = r.top + n; break;
589 case DT_BOTTOM: n = r.bottom - r.top;
590 r.bottom = rc->bottom - 1;
591 r.top = r.bottom - n;
592 break;
595 *rc = r;
596 return dtStyle;
600 /**********************************************************************
601 * BUTTON_DrawTextCallback
603 * Callback function used by DrawStateW function.
605 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
607 RECT rc;
608 rc.left = 0;
609 rc.top = 0;
610 rc.right = cx;
611 rc.bottom = cy;
613 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
614 return TRUE;
618 /**********************************************************************
619 * BUTTON_DrawLabel
621 * Common function for drawing button label.
623 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
625 DRAWSTATEPROC lpOutputProc = NULL;
626 LPARAM lp;
627 WPARAM wp = 0;
628 HBRUSH hbr = 0;
629 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
630 LONG state = get_button_state( hwnd );
631 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
632 WCHAR *text = NULL;
634 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
635 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
636 * I don't have Win31 on hand to verify that, so I leave it as is.
639 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
641 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
642 flags |= DSS_MONO;
645 switch (style & (BS_ICON|BS_BITMAP))
647 case BS_TEXT:
648 /* DST_COMPLEX -- is 0 */
649 lpOutputProc = BUTTON_DrawTextCallback;
650 if (!(text = get_button_text( hwnd ))) return;
651 lp = (LPARAM)text;
652 wp = (WPARAM)dtFlags;
653 break;
655 case BS_ICON:
656 flags |= DST_ICON;
657 lp = GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET );
658 break;
660 case BS_BITMAP:
661 flags |= DST_BITMAP;
662 lp = GetWindowLongA( hwnd, HIMAGE_GWL_OFFSET );
663 break;
665 default:
666 return;
669 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
670 rc->right - rc->left, rc->bottom - rc->top, flags);
671 if (text) HeapFree( GetProcessHeap(), 0, text );
674 /**********************************************************************
675 * Push Button Functions
677 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
679 RECT rc, focus_rect, r;
680 UINT dtFlags;
681 HRGN hRgn;
682 HPEN hOldPen;
683 HBRUSH hOldBrush;
684 INT oldBkMode;
685 COLORREF oldTxtColor;
686 HFONT hFont;
687 LONG state = get_button_state( hwnd );
688 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
689 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
691 GetClientRect( hwnd, &rc );
693 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
694 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
695 SendMessageW( GetParent(hwnd), WM_CTLCOLORBTN, hDC, (LPARAM)hwnd );
696 hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
697 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
698 oldBkMode = SetBkMode(hDC, TRANSPARENT);
700 if ( TWEAK_WineLook == WIN31_LOOK)
702 COLORREF clr_wnd = GetSysColor(COLOR_WINDOW);
703 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
705 SetPixel( hDC, rc.left, rc.top, clr_wnd);
706 SetPixel( hDC, rc.left, rc.bottom-1, clr_wnd);
707 SetPixel( hDC, rc.right-1, rc.top, clr_wnd);
708 SetPixel( hDC, rc.right-1, rc.bottom-1, clr_wnd);
709 InflateRect( &rc, -1, -1 );
712 if (get_button_type(style) == BS_DEFPUSHBUTTON)
714 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
715 InflateRect( &rc, -1, -1 );
718 if (TWEAK_WineLook == WIN31_LOOK)
720 if (pushedState)
722 /* draw button shadow: */
723 SelectObject(hDC, GetSysColorBrush(COLOR_BTNSHADOW));
724 PatBlt(hDC, rc.left, rc.top, 1, rc.bottom-rc.top, PATCOPY );
725 PatBlt(hDC, rc.left, rc.top, rc.right-rc.left, 1, PATCOPY );
726 } else {
727 rc.right++, rc.bottom++;
728 DrawEdge( hDC, &rc, EDGE_RAISED, BF_RECT );
729 rc.right--, rc.bottom--;
732 else
734 UINT uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
736 if (style & BS_FLAT)
737 uState |= DFCS_MONO;
738 else if (pushedState)
740 if (get_button_type(style) == BS_DEFPUSHBUTTON )
741 uState |= DFCS_FLAT;
742 else
743 uState |= DFCS_PUSHED;
746 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
747 uState |= DFCS_CHECKED;
749 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
751 focus_rect = rc;
754 /* draw button label */
755 r = rc;
756 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
758 if (dtFlags == (UINT)-1L)
759 goto cleanup;
761 if (pushedState)
762 OffsetRect(&r, 1, 1);
764 if(TWEAK_WineLook == WIN31_LOOK)
766 focus_rect = r;
767 InflateRect(&focus_rect, 2, 0);
770 hRgn = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
771 SelectClipRgn(hDC, hRgn);
773 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
775 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
777 SetTextColor( hDC, oldTxtColor );
778 SelectClipRgn(hDC, 0);
779 DeleteObject(hRgn);
781 if (state & BUTTON_HASFOCUS)
783 InflateRect( &focus_rect, -1, -1 );
784 IntersectRect(&focus_rect, &focus_rect, &rc);
785 DrawFocusRect( hDC, &focus_rect );
788 cleanup:
789 SelectObject( hDC, hOldPen );
790 SelectObject( hDC, hOldBrush );
791 SetBkMode(hDC, oldBkMode);
794 /**********************************************************************
795 * Check Box & Radio Button Functions
798 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
800 RECT rbox, rtext, client;
801 HBRUSH hBrush;
802 int delta;
803 UINT dtFlags;
804 HRGN hRgn;
805 HFONT hFont;
806 LONG state = get_button_state( hwnd );
807 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
809 if (style & BS_PUSHLIKE)
811 PB_Paint( hwnd, hDC, action );
812 return;
815 GetClientRect(hwnd, &client);
816 rbox = rtext = client;
818 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
820 hBrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hDC, (LPARAM)hwnd );
821 if (!hBrush) /* did the app forget to call defwindowproc ? */
822 hBrush = DefWindowProcW( GetParent(hwnd), WM_CTLCOLORSTATIC, hDC, (LPARAM)hwnd );
824 if (style & BS_LEFTTEXT)
826 /* magic +4 is what CTL3D expects */
828 rtext.right -= checkBoxWidth + 4;
829 rbox.left = rbox.right - checkBoxWidth;
831 else
833 rtext.left += checkBoxWidth + 4;
834 rbox.right = checkBoxWidth;
837 /* Draw the check-box bitmap */
838 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
840 /* Since WM_ERASEBKGND does nothing, first prepare background */
841 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
842 else FillRect( hDC, &client, hBrush );
844 if( TWEAK_WineLook == WIN31_LOOK )
846 HDC hMemDC = CreateCompatibleDC( hDC );
847 int x = 0, y = 0;
848 delta = (rbox.bottom - rbox.top - checkBoxHeight) / 2;
850 /* Check in case the client area is smaller than the checkbox bitmap */
851 if (delta < 0) delta = 0;
853 if (state & BUTTON_HIGHLIGHTED) x += 2 * checkBoxWidth;
854 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) x += checkBoxWidth;
855 if ((get_button_type(style) == BS_RADIOBUTTON) ||
856 (get_button_type(style) == BS_AUTORADIOBUTTON)) y += checkBoxHeight;
857 else if (state & BUTTON_3STATE) y += 2 * checkBoxHeight;
859 /* The bitmap for the radio button is not aligned with the
860 * left of the window, it is 1 pixel off. */
861 if ((get_button_type(style) == BS_RADIOBUTTON) ||
862 (get_button_type(style) == BS_AUTORADIOBUTTON))
863 rbox.left += 1;
865 SelectObject( hMemDC, hbitmapCheckBoxes );
866 BitBlt( hDC, rbox.left, rbox.top + delta, checkBoxWidth,
867 checkBoxHeight, hMemDC, x, y, SRCCOPY );
868 DeleteDC( hMemDC );
870 else
872 UINT flags;
874 if ((get_button_type(style) == BS_RADIOBUTTON) ||
875 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
876 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
877 else flags = DFCS_BUTTONCHECK;
879 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
880 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
882 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
884 /* rbox must have the correct height */
885 delta = rbox.bottom - rbox.top - checkBoxHeight;
886 if (delta > 0)
888 int ofs = (abs(delta) / 2);
889 rbox.bottom -= ofs + 1;
890 rbox.top = rbox.bottom - checkBoxHeight;
892 else if (delta < 0)
894 int ofs = (abs(delta) / 2);
895 rbox.top -= ofs + 1;
896 rbox.bottom = rbox.top + checkBoxHeight;
899 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
903 /* Draw label */
904 client = rtext;
905 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
907 if (dtFlags == (UINT)-1L) /* Noting to draw */
908 return;
909 hRgn = CreateRectRgn(client.left, client.top, client.right, client.bottom);
910 SelectClipRgn(hDC, hRgn);
911 DeleteObject(hRgn);
913 if (action == ODA_DRAWENTIRE)
914 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
916 /* ... and focus */
917 if ((action == ODA_FOCUS) ||
918 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
920 rtext.left--;
921 rtext.right++;
922 IntersectRect(&rtext, &rtext, &client);
923 DrawFocusRect( hDC, &rtext );
925 SelectClipRgn(hDC, 0);
929 /**********************************************************************
930 * BUTTON_CheckAutoRadioButton
932 * hwnd is checked, uncheck every other auto radio button in group
934 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
936 HWND parent, sibling, start;
938 parent = GetParent(hwnd);
939 /* make sure that starting control is not disabled or invisible */
940 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
943 if (!sibling) break;
944 if ((hwnd != sibling) &&
945 ((GetWindowLongA( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
946 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
947 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
948 } while (sibling != start);
952 /**********************************************************************
953 * Group Box Functions
956 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
958 RECT rc, rcFrame;
959 HBRUSH hbr;
960 HFONT hFont;
961 UINT dtFlags;
962 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
964 if (action != ODA_DRAWENTIRE) return;
966 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
967 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
968 hbr = SendMessageW( GetParent(hwnd), WM_CTLCOLORSTATIC, hDC, (LPARAM)hwnd );
969 if (!hbr) /* did the app forget to call defwindowproc ? */
970 hbr = DefWindowProcW( GetParent(hwnd), WM_CTLCOLORSTATIC, hDC, (LPARAM)hwnd );
972 GetClientRect( hwnd, &rc);
973 if (TWEAK_WineLook == WIN31_LOOK) {
974 HPEN hPrevPen = SelectObject( hDC,
975 GetSysColorPen(COLOR_WINDOWFRAME));
976 HBRUSH hPrevBrush = SelectObject( hDC,
977 GetStockObject(NULL_BRUSH) );
979 Rectangle( hDC, rc.left, rc.top + 2, rc.right - 1, rc.bottom - 1 );
980 SelectObject( hDC, hPrevBrush );
981 SelectObject( hDC, hPrevPen );
982 } else {
983 TEXTMETRICW tm;
984 rcFrame = rc;
986 GetTextMetricsW (hDC, &tm);
987 rcFrame.top += (tm.tmHeight / 2) - 1;
988 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
991 InflateRect(&rc, -7, 1);
992 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
994 if (dtFlags == (UINT)-1L)
995 return;
997 /* Because buttons have CS_PARENTDC class style, there is a chance
998 * that label will be drawn out of client rect.
999 * But Windows doesn't clip label's rect, so do I.
1002 /* There is 1-pixel marging at the left, right, and bottom */
1003 rc.left--; rc.right++; rc.bottom++;
1004 FillRect(hDC, &rc, hbr);
1005 rc.left++; rc.right--; rc.bottom--;
1007 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1011 /**********************************************************************
1012 * User Button Functions
1015 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1017 RECT rc;
1018 HBRUSH hBrush;
1019 HFONT hFont;
1020 LONG state = get_button_state( hwnd );
1022 if (action == ODA_SELECT) return;
1024 GetClientRect( hwnd, &rc);
1026 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1028 hBrush = SendMessageW( GetParent(hwnd), WM_CTLCOLORBTN, hDC, (LPARAM)hwnd );
1029 if (!hBrush) /* did the app forget to call defwindowproc ? */
1030 hBrush = DefWindowProcW( GetParent(hwnd), WM_CTLCOLORBTN, hDC, (LPARAM)hwnd );
1032 FillRect( hDC, &rc, hBrush );
1033 if ((action == ODA_FOCUS) ||
1034 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1035 DrawFocusRect( hDC, &rc );
1039 /**********************************************************************
1040 * Ownerdrawn Button Functions
1043 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1045 LONG state = get_button_state( hwnd );
1046 DRAWITEMSTRUCT dis;
1047 HRGN clipRegion;
1048 RECT clipRect;
1049 UINT id = GetWindowLongA( hwnd, GWL_ID );
1051 dis.CtlType = ODT_BUTTON;
1052 dis.CtlID = id;
1053 dis.itemID = 0;
1054 dis.itemAction = action;
1055 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1056 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1057 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1058 dis.hwndItem = hwnd;
1059 dis.hDC = hDC;
1060 dis.itemData = 0;
1061 GetClientRect( hwnd, &dis.rcItem );
1063 clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1064 if (GetClipRgn(hDC, clipRegion) != 1)
1066 DeleteObject(clipRegion);
1067 clipRegion=(HRGN)NULL;
1069 clipRect = dis.rcItem;
1070 DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1071 IntersectClipRect(hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
1073 SetBkColor( hDC, GetSysColor( COLOR_BTNFACE ) );
1074 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1075 SelectClipRgn(hDC, clipRegion);