[COMCTL32] -Fix several tests for the v6 button when themes are enabled.
[reactos.git] / reactos / dll / win32 / comctl32 / button.c
1 /* File: button.c -- Button type widgets
2 *
3 * Copyright (C) 1993 Johannes Ruscheinski
4 * Copyright (C) 1993 David Metcalfe
5 * Copyright (C) 1994 Alexandre Julliard
6 *
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.
11 *
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.
16 *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 * NOTES
22 *
23 * This code was audited for completeness against the documented features
24 * of Comctl32.dll version 6.0 on Oct. 3, 2004, by Dimitrie O. Paun.
25 *
26 * Unless otherwise noted, we believe this code to be complete, as per
27 * the specification mentioned above.
28 * If you discover missing features, or bugs, please note them below.
29 *
30 * TODO
31 * Styles
32 * - BS_NOTIFY: is it complete?
33 * - BS_RIGHTBUTTON: same as BS_LEFTTEXT
34 *
35 * Messages
36 * - WM_CHAR: Checks a (manual or automatic) check box on '+' or '=', clears it on '-' key.
37 * - WM_SETFOCUS: For (manual or automatic) radio buttons, send the parent window BN_CLICKED
38 * - WM_NCCREATE: Turns any BS_OWNERDRAW button into a BS_PUSHBUTTON button.
39 * - WM_SYSKEYUP
40 * - BCM_GETIDEALSIZE
41 * - BCM_GETIMAGELIST
42 * - BCM_GETTEXTMARGIN
43 * - BCM_SETIMAGELIST
44 * - BCM_SETTEXTMARGIN
45 *
46 * Notifications
47 * - BCN_HOTITEMCHANGE
48 * - BN_DISABLE
49 * - BN_PUSHED/BN_HILITE
50 * + BN_KILLFOCUS: is it OK?
51 * - BN_PAINT
52 * + BN_SETFOCUS: is it OK?
53 * - BN_UNPUSHED/BN_UNHILITE
54 * - NM_CUSTOMDRAW
55 *
56 * Structures/Macros/Definitions
57 * - BUTTON_IMAGELIST
58 * - NMBCHOTITEM
59 * - Button_GetIdealSize
60 * - Button_GetImageList
61 * - Button_GetTextMargin
62 * - Button_SetImageList
63 * - Button_SetTextMargin
64 */
65 #include "comctl32.h"
66
67 #include <wine/debug.h>
68 WINE_DEFAULT_DEBUG_CHANNEL(button);
69
70 /* GetWindowLong offsets for window extra information */
71 #define STATE_GWL_OFFSET 0
72 #define HFONT_GWL_OFFSET (sizeof(LONG))
73 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
74 #define UISTATE_GWL_OFFSET (HIMAGE_GWL_OFFSET+sizeof(HFONT))
75 #define NB_EXTRA_BYTES (UISTATE_GWL_OFFSET+sizeof(LONG))
76
77 /* undocumented flags */
78 #define BUTTON_NSTATES 0x0F
79 #define BUTTON_BTNPRESSED 0x40
80 #define BUTTON_UNKNOWN2 0x20
81 #define BUTTON_UNKNOWN3 0x10
82 #ifdef __REACTOS__
83 #define BUTTON_BMCLICK 0x100 // ReactOS Need to up to wine!
84 #endif
85
86 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
87 do { /* Notify parent which has created this button control */ \
88 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
89 SendMessageW(GetParent(hWnd), WM_COMMAND, \
90 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
91 (LPARAM)(hWnd)); \
92 } while(0)
93
94 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
95 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
96 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
97 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
98 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
99 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
100 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
101
102 #define MAX_BTN_TYPE 16
103
104 static const WORD maxCheckState[MAX_BTN_TYPE] =
105 {
106 BST_UNCHECKED, /* BS_PUSHBUTTON */
107 BST_UNCHECKED, /* BS_DEFPUSHBUTTON */
108 BST_CHECKED, /* BS_CHECKBOX */
109 BST_CHECKED, /* BS_AUTOCHECKBOX */
110 BST_CHECKED, /* BS_RADIOBUTTON */
111 BST_INDETERMINATE, /* BS_3STATE */
112 BST_INDETERMINATE, /* BS_AUTO3STATE */
113 BST_UNCHECKED, /* BS_GROUPBOX */
114 BST_UNCHECKED, /* BS_USERBUTTON */
115 BST_CHECKED, /* BS_AUTORADIOBUTTON */
116 BST_UNCHECKED, /* BS_PUSHBOX */
117 BST_UNCHECKED /* BS_OWNERDRAW */
118 };
119
120 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
121
122 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
123 {
124 PB_Paint, /* BS_PUSHBUTTON */
125 PB_Paint, /* BS_DEFPUSHBUTTON */
126 CB_Paint, /* BS_CHECKBOX */
127 CB_Paint, /* BS_AUTOCHECKBOX */
128 CB_Paint, /* BS_RADIOBUTTON */
129 CB_Paint, /* BS_3STATE */
130 CB_Paint, /* BS_AUTO3STATE */
131 GB_Paint, /* BS_GROUPBOX */
132 UB_Paint, /* BS_USERBUTTON */
133 CB_Paint, /* BS_AUTORADIOBUTTON */
134 NULL, /* BS_PUSHBOX */
135 OB_Paint /* BS_OWNERDRAW */
136 };
137
138 /* The original code from user32 was kept in order to make it easier to bring changes from user32 */
139 #ifdef _USER32_
140 /*********************************************************************
141 * button class descriptor
142 */
143 static const WCHAR buttonW[] = {'B','u','t','t','o','n',0};
144 const struct builtin_class_descr BUTTON_builtin_class =
145 {
146 buttonW, /* name */
147 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
148 #ifdef __REACTOS__
149 ButtonWndProcA, /* procA */
150 ButtonWndProcW, /* procW */
151 #else
152 WINPROC_BUTTON, /* proc */
153 #endif
154 NB_EXTRA_BYTES, /* extra */
155 IDC_ARROW, /* cursor */
156 0 /* brush */
157 };
158 #endif
159
160
161 static inline LONG get_button_state( HWND hwnd )
162 {
163 return GetWindowLongPtrW( hwnd, STATE_GWL_OFFSET );
164 }
165
166 static inline void set_button_state( HWND hwnd, LONG state )
167 {
168 SetWindowLongPtrW( hwnd, STATE_GWL_OFFSET, state );
169 }
170
171 #ifdef __REACTOS__
172
173 static __inline void set_ui_state( HWND hwnd, LONG flags )
174 {
175 SetWindowLongPtrW( hwnd, UISTATE_GWL_OFFSET, flags );
176 }
177
178 static __inline LONG get_ui_state( HWND hwnd )
179 {
180 return GetWindowLongPtrW( hwnd, UISTATE_GWL_OFFSET );
181 }
182
183 #endif /* __REACTOS__ */
184
185 #ifndef _USER32_
186 #define NtUserAlterWindowStyle SetWindowLongPtrW
187
188 HRGN set_control_clipping( HDC hdc, const RECT *rect )
189 {
190 RECT rc = *rect;
191 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
192
193 if (GetClipRgn( hdc, hrgn ) != 1)
194 {
195 DeleteObject( hrgn );
196 hrgn = 0;
197 }
198 DPtoLP( hdc, (POINT *)&rc, 2 );
199 if (GetLayout( hdc ) & LAYOUT_RTL) /* compensate for the shifting done by IntersectClipRect */
200 {
201 rc.left++;
202 rc.right++;
203 }
204 IntersectClipRect( hdc, rc.left, rc.top, rc.right, rc.bottom );
205 return hrgn;
206 }
207
208 BOOL BUTTON_PaintWithTheme(HTHEME theme, HWND hwnd, HDC hParamDC, LPARAM prfFlag);
209
210 #endif
211
212 static inline HFONT get_button_font( HWND hwnd )
213 {
214 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
215 }
216
217 static inline void set_button_font( HWND hwnd, HFONT font )
218 {
219 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
220 }
221
222 static inline UINT get_button_type( LONG window_style )
223 {
224 return (window_style & BS_TYPEMASK);
225 }
226
227 /* paint a button of any type */
228 static inline void paint_button( HWND hwnd, LONG style, UINT action )
229 {
230 #ifndef _USER32_
231 HTHEME theme = GetWindowTheme(hwnd);
232 RECT rc;
233 HDC hdc = GetDC( hwnd );
234 /* GetDC appears to give a dc with a clip rect that includes the whoe parent, not sure if it is correct or not. */
235 GetClientRect(hwnd, &rc);
236 IntersectClipRect (hdc, rc.left, rc. top, rc.right, rc.bottom);
237 if (theme && BUTTON_PaintWithTheme(theme, hwnd, hdc, 0))
238 {
239 ReleaseDC( hwnd, hdc );
240 return;
241 }
242 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
243 {
244 btnPaintFunc[style]( hwnd, hdc, action );
245 }
246 ReleaseDC( hwnd, hdc );
247 #else
248 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
249 {
250 HDC hdc = GetDC( hwnd );
251 btnPaintFunc[style]( hwnd, hdc, action );
252 ReleaseDC( hwnd, hdc );
253 }
254 #endif
255 }
256
257 /* retrieve the button text; returned buffer must be freed by caller */
258 static inline WCHAR *get_button_text( HWND hwnd )
259 {
260 INT len = 512;
261 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
262 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
263 return buffer;
264 }
265
266 #ifdef __REACTOS__
267 /* Retrieve the UI state for the control */
268 static BOOL button_update_uistate(HWND hwnd, BOOL unicode)
269 {
270 LONG flags, prevflags;
271
272 if (unicode)
273 flags = DefWindowProcW(hwnd, WM_QUERYUISTATE, 0, 0);
274 else
275 flags = DefWindowProcA(hwnd, WM_QUERYUISTATE, 0, 0);
276
277 prevflags = get_ui_state(hwnd);
278
279 if (prevflags != flags)
280 {
281 set_ui_state(hwnd, flags);
282 return TRUE;
283 }
284
285 return FALSE;
286 }
287 #endif
288
289 /***********************************************************************
290 * ButtonWndProc_common
291 */
292 LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
293 WPARAM wParam, LPARAM lParam, BOOL unicode )
294 {
295 RECT rect;
296 POINT pt;
297 LONG style = GetWindowLongPtrW( hWnd, GWL_STYLE );
298 UINT btn_type = get_button_type( style );
299 LONG state;
300 HANDLE oldHbitmap;
301 #if defined(__REACTOS__) && defined(_USER32_)
302 PWND pWnd;
303
304 pWnd = ValidateHwnd(hWnd);
305 if (pWnd)
306 {
307 if (!pWnd->fnid)
308 {
309 NtUserSetWindowFNID(hWnd, FNID_BUTTON);
310 }
311 else
312 {
313 if (pWnd->fnid != FNID_BUTTON)
314 {
315 ERR("Wrong window class for Button! fnId 0x%x\n",pWnd->fnid);
316 return 0;
317 }
318 }
319 }
320 else
321 return 0;
322 #else
323 if (!IsWindow( hWnd )) return 0;
324 #endif
325
326 pt.x = (short)LOWORD(lParam);
327 pt.y = (short)HIWORD(lParam);
328
329 switch (uMsg)
330 {
331 case WM_GETDLGCODE:
332 switch(btn_type)
333 {
334 case BS_USERBUTTON:
335 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
336 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
337 case BS_RADIOBUTTON:
338 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
339 case BS_GROUPBOX: return DLGC_STATIC;
340 default: return DLGC_BUTTON;
341 }
342
343 case WM_ENABLE:
344 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
345 break;
346
347 case WM_CREATE:
348 if (btn_type >= MAX_BTN_TYPE)
349 return -1; /* abort */
350
351 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
352 if (btn_type == BS_USERBUTTON )
353 {
354 style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
355 #ifdef __REACTOS__
356 NtUserAlterWindowStyle(hWnd, GWL_STYLE, style );
357 #else
358 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
359 #endif
360 }
361 set_button_state( hWnd, BST_UNCHECKED );
362 #ifdef __REACTOS__
363 button_update_uistate( hWnd, unicode );
364 #endif
365 #ifndef _USER32_
366 OpenThemeData(hWnd, WC_BUTTONW);
367 #endif
368 return 0;
369
370 #if defined(__REACTOS__) && defined(_USER32_)
371 case WM_NCDESTROY:
372 NtUserSetWindowFNID(hWnd, FNID_DESTROY);
373 case WM_DESTROY:
374 break;
375 #endif
376 #ifndef _USER32_
377 case WM_DESTROY:
378 CloseThemeData (GetWindowTheme(hWnd));
379 break;
380 case WM_THEMECHANGED:
381 CloseThemeData (GetWindowTheme(hWnd));
382 OpenThemeData(hWnd, WC_BUTTONW);
383 break;
384
385 case WM_MOUSEHOVER:
386 {
387 int state = (int)SendMessageW(hWnd, BM_GETSTATE, 0, 0);
388 SetWindowLongW(hWnd, 0, state|BST_HOT);
389 InvalidateRect(hWnd, NULL, FALSE);
390 break;
391 }
392
393 case WM_MOUSELEAVE:
394 {
395 int state = (int)SendMessageW(hWnd, BM_GETSTATE, 0, 0);
396 SetWindowLongW(hWnd, 0, state&(~BST_HOT));
397 InvalidateRect(hWnd, NULL, FALSE);
398 break;
399 }
400 #endif
401 case WM_ERASEBKGND:
402 if (btn_type == BS_OWNERDRAW)
403 {
404 HDC hdc = (HDC)wParam;
405 RECT rc;
406 HBRUSH hBrush;
407 HWND parent = GetParent(hWnd);
408 if (!parent) parent = hWnd;
409 #if defined(__REACTOS__) && defined(_USER32_)
410 hBrush = GetControlColor( parent, hWnd, hdc, WM_CTLCOLORBTN);
411 #else
412 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
413 if (!hBrush) /* did the app forget to call defwindowproc ? */
414 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
415 (WPARAM)hdc, (LPARAM)hWnd);
416 #endif
417 GetClientRect(hWnd, &rc);
418 FillRect(hdc, &rc, hBrush);
419 }
420 return 1;
421
422 case WM_PRINTCLIENT:
423 case WM_PAINT:
424 {
425 PAINTSTRUCT ps;
426 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
427 #ifndef _USER32_
428 HTHEME theme = GetWindowTheme(hWnd);
429 if (theme && BUTTON_PaintWithTheme(theme, hWnd, hdc, uMsg == WM_PRINTCLIENT ? lParam : 0))
430 {
431 if ( !wParam ) EndPaint( hWnd, &ps );
432 return 0;
433 }
434 #endif
435 if (btnPaintFunc[btn_type])
436 {
437 int nOldMode = SetBkMode( hdc, OPAQUE );
438 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
439 SetBkMode(hdc, nOldMode); /* reset painting mode */
440 }
441 if ( !wParam ) EndPaint( hWnd, &ps );
442 break;
443 }
444
445 case WM_KEYDOWN:
446 if (wParam == VK_SPACE)
447 {
448 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
449 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
450 SetCapture( hWnd );
451 }
452 break;
453
454 case WM_LBUTTONDBLCLK:
455 if(style & BS_NOTIFY ||
456 btn_type == BS_RADIOBUTTON ||
457 btn_type == BS_USERBUTTON ||
458 btn_type == BS_OWNERDRAW)
459 {
460 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
461 break;
462 }
463 /* fall through */
464 case WM_LBUTTONDOWN:
465 SetCapture( hWnd );
466 SetFocus( hWnd );
467 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
468 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
469 break;
470
471 case WM_KEYUP:
472 if (wParam != VK_SPACE)
473 break;
474 /* fall through */
475 case WM_LBUTTONUP:
476 #ifdef _REACTOS_
477 BOOL TellParent = FALSE; //// ReactOS see note below.
478 #endif
479 state = get_button_state( hWnd );
480 if (!(state & BUTTON_BTNPRESSED)) break;
481 state &= BUTTON_NSTATES;
482 set_button_state( hWnd, state );
483 if (!(state & BST_PUSHED))
484 {
485 ReleaseCapture();
486 break;
487 }
488 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
489 GetClientRect( hWnd, &rect );
490 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
491 {
492 state = get_button_state( hWnd );
493 switch(btn_type)
494 {
495 case BS_AUTOCHECKBOX:
496 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
497 break;
498 case BS_AUTORADIOBUTTON:
499 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
500 break;
501 case BS_AUTO3STATE:
502 SendMessageW( hWnd, BM_SETCHECK,
503 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
504 break;
505 }
506 #ifdef _REACTOS_
507 TellParent = TRUE; // <---- Fix CORE-10194, Notify parent after capture is released.
508 #else
509 ReleaseCapture();
510 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
511 #endif
512 }
513 #ifdef _REACTOS_
514 ReleaseCapture();
515 if (TellParent) BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
516 #else
517 else
518 {
519 ReleaseCapture();
520 }
521 #endif
522 break;
523
524 case WM_CAPTURECHANGED:
525 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
526 if (hWnd == (HWND)lParam) break;
527 state = get_button_state( hWnd );
528 if (state & BUTTON_BTNPRESSED)
529 {
530 state &= BUTTON_NSTATES;
531 set_button_state( hWnd, state );
532 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
533 }
534 break;
535
536 case WM_MOUSEMOVE:
537 #ifndef _USER32_
538 {
539 TRACKMOUSEEVENT mouse_event;
540 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
541 mouse_event.dwFlags = TME_QUERY;
542 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
543 {
544 mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
545 mouse_event.hwndTrack = hWnd;
546 mouse_event.dwHoverTime = 1;
547 TrackMouseEvent(&mouse_event);
548 }
549 }
550 #endif
551
552 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
553 {
554 GetClientRect( hWnd, &rect );
555 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
556 }
557 break;
558
559 case WM_SETTEXT:
560 {
561 /* Clear an old text here as Windows does */
562 //
563 // ReactOS Note :
564 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
565 // Patch: http://source.winehq.org/patches/data/70889
566 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
567 //
568 #ifdef __REACTOS__
569 if (style & WS_VISIBLE)
570 #else
571 if (IsWindowVisible(hWnd))
572 #endif
573 {
574 HDC hdc = GetDC(hWnd);
575 HBRUSH hbrush;
576 RECT client, rc;
577 HWND parent = GetParent(hWnd);
578 UINT message = (btn_type == BS_PUSHBUTTON ||
579 btn_type == BS_DEFPUSHBUTTON ||
580 btn_type == BS_PUSHLIKE ||
581 btn_type == BS_USERBUTTON ||
582 btn_type == BS_OWNERDRAW) ?
583 WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
584
585 if (!parent) parent = hWnd;
586 #if defined(__REACTOS__) && defined(_USER32_)
587 hbrush = GetControlColor(parent, hWnd, hdc, message);
588 #else
589 hbrush = (HBRUSH)SendMessageW(parent, message,
590 (WPARAM)hdc, (LPARAM)hWnd);
591 if (!hbrush) /* did the app forget to call DefWindowProc ? */
592 hbrush = (HBRUSH)DefWindowProcW(parent, message,
593 (WPARAM)hdc, (LPARAM)hWnd);
594 #endif
595
596 GetClientRect(hWnd, &client);
597 rc = client;
598 /* FIXME: check other BS_* handlers */
599 if (btn_type == BS_GROUPBOX)
600 InflateRect(&rc, -7, 1); /* GB_Paint does this */
601 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
602 /* Clip by client rect bounds */
603 if (rc.right > client.right) rc.right = client.right;
604 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
605 FillRect(hdc, &rc, hbrush);
606 ReleaseDC(hWnd, hdc);
607 }
608
609 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
610 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
611 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
612 InvalidateRect( hWnd, NULL, TRUE );
613 else
614 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
615 return 1; /* success. FIXME: check text length */
616 }
617
618 case WM_SETFONT:
619 set_button_font( hWnd, (HFONT)wParam );
620 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
621 break;
622
623 case WM_GETFONT:
624 return (LRESULT)get_button_font( hWnd );
625
626 case WM_SETFOCUS:
627 TRACE("WM_SETFOCUS %p\n",hWnd);
628 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
629 if (btn_type == BS_OWNERDRAW)
630 paint_button( hWnd, btn_type, ODA_FOCUS );
631 else
632 InvalidateRect(hWnd, NULL, FALSE);
633 if (style & BS_NOTIFY)
634 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
635 break;
636
637 case WM_KILLFOCUS:
638 TRACE("WM_KILLFOCUS %p\n",hWnd);
639 state = get_button_state( hWnd );
640 set_button_state( hWnd, state & ~BST_FOCUS );
641
642 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
643 ReleaseCapture();
644 if (style & BS_NOTIFY)
645 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
646
647 InvalidateRect( hWnd, NULL, FALSE );
648 break;
649
650 case WM_SYSCOLORCHANGE:
651 InvalidateRect( hWnd, NULL, FALSE );
652 break;
653
654 case BM_SETSTYLE:
655 btn_type = wParam & BS_TYPEMASK;
656 style = (style & ~BS_TYPEMASK) | btn_type;
657 #ifdef __REACTOS__
658 NtUserAlterWindowStyle(hWnd, GWL_STYLE, style);
659 #else
660 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
661 #endif
662
663 /* Only redraw if lParam flag is set.*/
664 if (lParam)
665 InvalidateRect( hWnd, NULL, TRUE );
666
667 break;
668
669 case BM_CLICK:
670 #ifdef __REACTOS__
671 state = get_button_state(hWnd);
672 if (state & BUTTON_BMCLICK)
673 break;
674 set_button_state(hWnd, state | BUTTON_BMCLICK); // Tracked in STATE_GWL_OFFSET.
675 #endif
676 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
677 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
678 #ifdef __REACTOS__
679 state = get_button_state(hWnd);
680 if (!(state & BUTTON_BMCLICK)) break;
681 state &= ~BUTTON_BMCLICK;
682 set_button_state(hWnd, state);
683 #endif
684 break;
685
686 case BM_SETIMAGE:
687 /* Check that image format matches button style */
688 switch (style & (BS_BITMAP|BS_ICON))
689 {
690 case BS_BITMAP:
691 if (wParam != IMAGE_BITMAP) return 0;
692 break;
693 case BS_ICON:
694 if (wParam != IMAGE_ICON) return 0;
695 break;
696 default:
697 return 0;
698 }
699 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
700 InvalidateRect( hWnd, NULL, FALSE );
701 return (LRESULT)oldHbitmap;
702
703 case BM_GETIMAGE:
704 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
705
706 case BM_GETCHECK:
707 return get_button_state( hWnd ) & 3;
708
709 case BM_SETCHECK:
710 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
711 state = get_button_state( hWnd );
712 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
713 {
714 #ifdef __REACTOS__
715 if (wParam) style |= WS_TABSTOP;
716 else style &= ~WS_TABSTOP;
717 NtUserAlterWindowStyle(hWnd, GWL_STYLE, style);
718 #else
719 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
720 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
721 #endif
722 }
723 if ((state & 3) != wParam)
724 {
725 set_button_state( hWnd, (state & ~3) | wParam );
726 InvalidateRect(hWnd, NULL, FALSE);
727 }
728 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BST_CHECKED) && (style & WS_CHILD))
729 BUTTON_CheckAutoRadioButton( hWnd );
730 break;
731
732 case BM_GETSTATE:
733 return get_button_state( hWnd );
734
735 case BM_SETSTATE:
736 state = get_button_state( hWnd );
737 if (wParam)
738 set_button_state( hWnd, state | BST_PUSHED );
739 else
740 set_button_state( hWnd, state & ~BST_PUSHED );
741
742 InvalidateRect(hWnd, NULL, FALSE);
743 break;
744
745 #ifdef __REACTOS__
746 case WM_UPDATEUISTATE:
747 if (unicode)
748 DefWindowProcW(hWnd, uMsg, wParam, lParam);
749 else
750 DefWindowProcA(hWnd, uMsg, wParam, lParam);
751
752 if (button_update_uistate(hWnd, unicode))
753 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
754 break;
755 #endif
756
757 case WM_NCHITTEST:
758 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
759 /* fall through */
760 default:
761 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
762 DefWindowProcA(hWnd, uMsg, wParam, lParam);
763 }
764 return 0;
765 }
766
767 #ifdef __REACTOS__
768
769 /***********************************************************************
770 * ButtonWndProcW
771 * The button window procedure. This is just a wrapper which locks
772 * the passed HWND and calls the real window procedure (with a WND*
773 * pointer pointing to the locked windowstructure).
774 */
775 LRESULT WINAPI ButtonWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
776 {
777 if (!IsWindow(hWnd)) return 0;
778 return ButtonWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
779 }
780
781 /***********************************************************************
782 * ButtonWndProcA
783 */
784 LRESULT WINAPI ButtonWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
785 {
786 if (!IsWindow(hWnd)) return 0;
787 return ButtonWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
788 }
789
790 #endif /* __REACTOS__ */
791
792 /**********************************************************************
793 * Convert button styles to flags used by DrawText.
794 */
795 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
796 {
797 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
798
799 /* "Convert" pushlike buttons to pushbuttons */
800 if (style & BS_PUSHLIKE)
801 style &= ~BS_TYPEMASK;
802
803 if (!(style & BS_MULTILINE))
804 dtStyle |= DT_SINGLELINE;
805 else
806 dtStyle |= DT_WORDBREAK;
807
808 switch (style & BS_CENTER)
809 {
810 case BS_LEFT: /* DT_LEFT is 0 */ break;
811 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
812 case BS_CENTER: dtStyle |= DT_CENTER; break;
813 default:
814 /* Pushbutton's text is centered by default */
815 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
816 /* all other flavours have left aligned text */
817 }
818
819 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
820
821 /* DrawText ignores vertical alignment for multiline text,
822 * but we use these flags to align label manually.
823 */
824 if (get_button_type(style) != BS_GROUPBOX)
825 {
826 switch (style & BS_VCENTER)
827 {
828 case BS_TOP: /* DT_TOP is 0 */ break;
829 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
830 case BS_VCENTER: /* fall through */
831 default: dtStyle |= DT_VCENTER; break;
832 }
833 }
834 else
835 /* GroupBox's text is always single line and is top aligned. */
836 dtStyle |= DT_SINGLELINE;
837
838 return dtStyle;
839 }
840
841 /**********************************************************************
842 * BUTTON_CalcLabelRect
843 *
844 * Calculates label's rectangle depending on button style.
845 *
846 * Returns flags to be passed to DrawText.
847 * Calculated rectangle doesn't take into account button state
848 * (pushed, etc.). If there is nothing to draw (no text/image) output
849 * rectangle is empty, and return value is (UINT)-1.
850 */
851 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
852 {
853 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
854 LONG ex_style = GetWindowLongPtrW( hwnd, GWL_EXSTYLE );
855 WCHAR *text;
856 ICONINFO iconInfo;
857 BITMAP bm;
858 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
859 RECT r = *rc;
860 INT n;
861
862 /* Calculate label rectangle according to label type */
863 switch (style & (BS_ICON|BS_BITMAP))
864 {
865 case BS_TEXT:
866 {
867 HFONT hFont, hPrevFont = 0;
868
869 if (!(text = get_button_text( hwnd ))) goto empty_rect;
870 if (!text[0])
871 {
872 HeapFree( GetProcessHeap(), 0, text );
873 goto empty_rect;
874 }
875
876 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hdc, hFont );
877 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
878 if (hPrevFont) SelectObject( hdc, hPrevFont );
879 HeapFree( GetProcessHeap(), 0, text );
880 #ifdef __REACTOS__
881 if (get_ui_state(hwnd) & UISF_HIDEACCEL)
882 dtStyle |= DT_HIDEPREFIX;
883 #endif
884 break;
885 }
886
887 case BS_ICON:
888 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
889 goto empty_rect;
890
891 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
892
893 r.right = r.left + bm.bmWidth;
894 r.bottom = r.top + bm.bmHeight;
895
896 DeleteObject(iconInfo.hbmColor);
897 DeleteObject(iconInfo.hbmMask);
898 break;
899
900 case BS_BITMAP:
901 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
902 goto empty_rect;
903
904 r.right = r.left + bm.bmWidth;
905 r.bottom = r.top + bm.bmHeight;
906 break;
907
908 default:
909 empty_rect:
910 rc->right = r.left;
911 rc->bottom = r.top;
912 return (UINT)-1;
913 }
914
915 /* Position label inside bounding rectangle according to
916 * alignment flags. (calculated rect is always left-top aligned).
917 * If label is aligned to any side - shift label in opposite
918 * direction to leave extra space for focus rectangle.
919 */
920 switch (dtStyle & (DT_CENTER|DT_RIGHT))
921 {
922 case DT_LEFT: r.left++; r.right++; break;
923 case DT_CENTER: n = r.right - r.left;
924 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
925 r.right = r.left + n; break;
926 case DT_RIGHT: n = r.right - r.left;
927 r.right = rc->right - 1;
928 r.left = r.right - n;
929 break;
930 }
931
932 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
933 {
934 case DT_TOP: r.top++; r.bottom++; break;
935 case DT_VCENTER: n = r.bottom - r.top;
936 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
937 r.bottom = r.top + n; break;
938 case DT_BOTTOM: n = r.bottom - r.top;
939 r.bottom = rc->bottom - 1;
940 r.top = r.bottom - n;
941 break;
942 }
943
944 *rc = r;
945 return dtStyle;
946 }
947
948
949 /**********************************************************************
950 * BUTTON_DrawTextCallback
951 *
952 * Callback function used by DrawStateW function.
953 */
954 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
955 {
956 RECT rc;
957
958 SetRect(&rc, 0, 0, cx, cy);
959 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
960 return TRUE;
961 }
962
963
964 /**********************************************************************
965 * BUTTON_DrawLabel
966 *
967 * Common function for drawing button label.
968 */
969 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
970 {
971 DRAWSTATEPROC lpOutputProc = NULL;
972 LPARAM lp;
973 WPARAM wp = 0;
974 HBRUSH hbr = 0;
975 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
976 LONG state = get_button_state( hwnd );
977 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
978 WCHAR *text = NULL;
979
980 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
981 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
982 * I don't have Win31 on hand to verify that, so I leave it as is.
983 */
984
985 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
986 {
987 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
988 flags |= DSS_MONO;
989 }
990
991 switch (style & (BS_ICON|BS_BITMAP))
992 {
993 case BS_TEXT:
994 /* DST_COMPLEX -- is 0 */
995 lpOutputProc = BUTTON_DrawTextCallback;
996 if (!(text = get_button_text( hwnd ))) return;
997 lp = (LPARAM)text;
998 wp = (WPARAM)dtFlags;
999
1000 #ifdef __REACTOS__
1001 if (dtFlags & DT_HIDEPREFIX)
1002 flags |= DSS_HIDEPREFIX;
1003 #endif
1004 break;
1005
1006 case BS_ICON:
1007 flags |= DST_ICON;
1008 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
1009 break;
1010
1011 case BS_BITMAP:
1012 flags |= DST_BITMAP;
1013 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
1014 break;
1015
1016 default:
1017 return;
1018 }
1019
1020 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
1021 rc->right - rc->left, rc->bottom - rc->top, flags);
1022 HeapFree( GetProcessHeap(), 0, text );
1023 }
1024
1025 /**********************************************************************
1026 * Push Button Functions
1027 */
1028 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
1029 {
1030 RECT rc, r;
1031 UINT dtFlags, uState;
1032 HPEN hOldPen;
1033 HBRUSH hOldBrush;
1034 INT oldBkMode;
1035 COLORREF oldTxtColor;
1036 HFONT hFont;
1037 LONG state = get_button_state( hwnd );
1038 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1039 BOOL pushedState = (state & BST_PUSHED);
1040 HWND parent;
1041 HRGN hrgn;
1042
1043 GetClientRect( hwnd, &rc );
1044
1045 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
1046 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1047 parent = GetParent(hwnd);
1048 if (!parent) parent = hwnd;
1049 #if defined(__REACTOS__) && defined(_USER32_)
1050 GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
1051 #else
1052 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1053 #endif
1054
1055 hrgn = set_control_clipping( hDC, &rc );
1056 #ifdef __REACTOS__
1057 hOldPen = SelectObject(hDC, GetStockObject(DC_PEN));
1058 SetDCPenColor(hDC, GetSysColor(COLOR_WINDOWFRAME));
1059 #else
1060 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
1061 #endif
1062 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
1063 oldBkMode = SetBkMode(hDC, TRANSPARENT);
1064
1065 if (get_button_type(style) == BS_DEFPUSHBUTTON)
1066 {
1067 if (action != ODA_FOCUS)
1068 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
1069 InflateRect( &rc, -1, -1 );
1070 }
1071
1072 /* completely skip the drawing if only focus has changed */
1073 if (action == ODA_FOCUS) goto draw_focus;
1074
1075 uState = DFCS_BUTTONPUSH;
1076
1077 if (style & BS_FLAT)
1078 uState |= DFCS_MONO;
1079 else if (pushedState)
1080 {
1081 if (get_button_type(style) == BS_DEFPUSHBUTTON )
1082 uState |= DFCS_FLAT;
1083 else
1084 uState |= DFCS_PUSHED;
1085 }
1086
1087 if (state & (BST_CHECKED | BST_INDETERMINATE))
1088 uState |= DFCS_CHECKED;
1089
1090 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
1091
1092 /* draw button label */
1093 r = rc;
1094 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
1095
1096 if (dtFlags == (UINT)-1L)
1097 goto cleanup;
1098
1099 if (pushedState)
1100 OffsetRect(&r, 1, 1);
1101
1102 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
1103
1104 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
1105
1106 SetTextColor( hDC, oldTxtColor );
1107
1108 draw_focus:
1109 if (action == ODA_FOCUS || (state & BST_FOCUS))
1110 {
1111 #ifdef __REACTOS__
1112 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1113 {
1114 #endif
1115 InflateRect( &rc, -2, -2 );
1116 DrawFocusRect( hDC, &rc );
1117 #ifdef __REACTOS__
1118 }
1119 #endif
1120 }
1121
1122 cleanup:
1123 SelectObject( hDC, hOldPen );
1124 SelectObject( hDC, hOldBrush );
1125 SetBkMode(hDC, oldBkMode);
1126 SelectClipRgn( hDC, hrgn );
1127 if (hrgn) DeleteObject( hrgn );
1128 }
1129
1130 /**********************************************************************
1131 * Check Box & Radio Button Functions
1132 */
1133
1134 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
1135 {
1136 RECT rbox, rtext, client;
1137 HBRUSH hBrush;
1138 int delta, text_offset, checkBoxWidth, checkBoxHeight;
1139 UINT dtFlags;
1140 HFONT hFont;
1141 LONG state = get_button_state( hwnd );
1142 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1143 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
1144 HWND parent;
1145 HRGN hrgn;
1146
1147 if (style & BS_PUSHLIKE)
1148 {
1149 PB_Paint( hwnd, hDC, action );
1150 return;
1151 }
1152
1153 GetClientRect(hwnd, &client);
1154 rbox = rtext = client;
1155
1156 checkBoxWidth = 12 * GetDeviceCaps( hDC, LOGPIXELSX ) / 96 + 1;
1157 checkBoxHeight = 12 * GetDeviceCaps( hDC, LOGPIXELSY ) / 96 + 1;
1158
1159 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1160 GetCharWidthW( hDC, '0', '0', &text_offset );
1161 text_offset /= 2;
1162
1163 parent = GetParent(hwnd);
1164 if (!parent) parent = hwnd;
1165 #if defined(__REACTOS__) && defined(_USER32_)
1166 hBrush = GetControlColor(parent, hwnd, hDC, WM_CTLCOLORSTATIC);
1167 #else
1168 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
1169 (WPARAM)hDC, (LPARAM)hwnd);
1170 if (!hBrush) /* did the app forget to call defwindowproc ? */
1171 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1172 (WPARAM)hDC, (LPARAM)hwnd );
1173 #endif
1174 hrgn = set_control_clipping( hDC, &client );
1175
1176 if (style & BS_LEFTTEXT || ex_style & WS_EX_RIGHT)
1177 {
1178 /* magic +4 is what CTL3D expects */
1179
1180 rtext.right -= checkBoxWidth + text_offset;;
1181 rbox.left = rbox.right - checkBoxWidth;
1182 }
1183 else
1184 {
1185 rtext.left += checkBoxWidth + text_offset;;
1186 rbox.right = checkBoxWidth;
1187 }
1188
1189 /* Since WM_ERASEBKGND does nothing, first prepare background */
1190 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
1191 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
1192
1193 /* Draw label */
1194 client = rtext;
1195 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
1196
1197 /* Only adjust rbox when rtext is valid */
1198 if (dtFlags != (UINT)-1L)
1199 {
1200 rbox.top = rtext.top;
1201 rbox.bottom = rtext.bottom;
1202 }
1203
1204 /* Draw the check-box bitmap */
1205 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
1206 {
1207 UINT flags;
1208
1209 if ((get_button_type(style) == BS_RADIOBUTTON) ||
1210 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
1211 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
1212 else flags = DFCS_BUTTONCHECK;
1213
1214 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
1215 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
1216
1217 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
1218
1219 /* rbox must have the correct height */
1220 delta = rbox.bottom - rbox.top - checkBoxHeight;
1221
1222 if (style & BS_TOP) {
1223 if (delta > 0) {
1224 rbox.bottom = rbox.top + checkBoxHeight;
1225 } else {
1226 rbox.top -= -delta/2 + 1;
1227 rbox.bottom = rbox.top + checkBoxHeight;
1228 }
1229 } else if (style & BS_BOTTOM) {
1230 if (delta > 0) {
1231 rbox.top = rbox.bottom - checkBoxHeight;
1232 } else {
1233 rbox.bottom += -delta/2 + 1;
1234 rbox.top = rbox.bottom - checkBoxHeight;
1235 }
1236 } else { /* Default */
1237 if (delta > 0) {
1238 int ofs = (delta / 2);
1239 rbox.bottom -= ofs + 1;
1240 rbox.top = rbox.bottom - checkBoxHeight;
1241 } else if (delta < 0) {
1242 int ofs = (-delta / 2);
1243 rbox.top -= ofs + 1;
1244 rbox.bottom = rbox.top + checkBoxHeight;
1245 }
1246 }
1247
1248 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
1249 }
1250
1251 if (dtFlags == (UINT)-1L) /* Noting to draw */
1252 return;
1253
1254 if (action == ODA_DRAWENTIRE)
1255 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
1256
1257 /* ... and focus */
1258 if (action == ODA_FOCUS || (state & BST_FOCUS))
1259 {
1260 #ifdef __REACTOS__
1261 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1262 {
1263 #endif
1264 rtext.left--;
1265 rtext.right++;
1266 IntersectRect(&rtext, &rtext, &client);
1267 DrawFocusRect( hDC, &rtext );
1268 #ifdef __REACTOS__
1269 }
1270 #endif
1271 }
1272 SelectClipRgn( hDC, hrgn );
1273 if (hrgn) DeleteObject( hrgn );
1274 }
1275
1276
1277 /**********************************************************************
1278 * BUTTON_CheckAutoRadioButton
1279 *
1280 * hwnd is checked, uncheck every other auto radio button in group
1281 */
1282 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
1283 {
1284 HWND parent, sibling, start;
1285
1286 parent = GetParent(hwnd);
1287 /* make sure that starting control is not disabled or invisible */
1288 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1289 do
1290 {
1291 if (!sibling) break;
1292 if ((hwnd != sibling) &&
1293 ((GetWindowLongPtrW( sibling, GWL_STYLE) & BS_TYPEMASK) == BS_AUTORADIOBUTTON))
1294 SendMessageW( sibling, BM_SETCHECK, BST_UNCHECKED, 0 );
1295 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1296 } while (sibling != start);
1297 }
1298
1299
1300 /**********************************************************************
1301 * Group Box Functions
1302 */
1303
1304 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1305 {
1306 RECT rc, rcFrame;
1307 HBRUSH hbr;
1308 HFONT hFont;
1309 UINT dtFlags;
1310 TEXTMETRICW tm;
1311 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1312 HWND parent;
1313 HRGN hrgn;
1314
1315 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1316 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1317 parent = GetParent(hwnd);
1318 if (!parent) parent = hwnd;
1319 #if defined(__REACTOS__) && defined(_USER32_)
1320 hbr = GetControlColor(parent, hwnd, hDC, WM_CTLCOLORSTATIC);
1321 #else
1322 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1323 if (!hbr) /* did the app forget to call defwindowproc ? */
1324 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1325 (WPARAM)hDC, (LPARAM)hwnd);
1326 #endif
1327 GetClientRect( hwnd, &rc);
1328 rcFrame = rc;
1329 hrgn = set_control_clipping( hDC, &rc );
1330
1331 GetTextMetricsW (hDC, &tm);
1332 rcFrame.top += (tm.tmHeight / 2) - 1;
1333 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1334
1335 InflateRect(&rc, -7, 1);
1336 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1337
1338 if (dtFlags != (UINT)-1L)
1339 {
1340 /* Because buttons have CS_PARENTDC class style, there is a chance
1341 * that label will be drawn out of client rect.
1342 * But Windows doesn't clip label's rect, so do I.
1343 */
1344
1345 /* There is 1-pixel margin at the left, right, and bottom */
1346 rc.left--; rc.right++; rc.bottom++;
1347 FillRect(hDC, &rc, hbr);
1348 rc.left++; rc.right--; rc.bottom--;
1349
1350 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1351 }
1352 SelectClipRgn( hDC, hrgn );
1353 if (hrgn) DeleteObject( hrgn );
1354 }
1355
1356
1357 /**********************************************************************
1358 * User Button Functions
1359 */
1360
1361 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1362 {
1363 RECT rc;
1364 HBRUSH hBrush;
1365 HFONT hFont;
1366 LONG state = get_button_state( hwnd );
1367 HWND parent;
1368
1369 GetClientRect( hwnd, &rc);
1370
1371 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1372
1373 parent = GetParent(hwnd);
1374 if (!parent) parent = hwnd;
1375 #if defined(__REACTOS__) && defined(_USER32_)
1376 hBrush = GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
1377 #else
1378 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1379 if (!hBrush) /* did the app forget to call defwindowproc ? */
1380 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1381 (WPARAM)hDC, (LPARAM)hwnd);
1382 #endif
1383
1384 FillRect( hDC, &rc, hBrush );
1385 if (action == ODA_FOCUS || (state & BST_FOCUS))
1386 #ifdef __REACTOS__
1387 {
1388 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1389 #endif
1390 DrawFocusRect( hDC, &rc );
1391 #ifdef __REACTOS__
1392 }
1393 #endif
1394
1395 switch (action)
1396 {
1397 case ODA_FOCUS:
1398 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1399 break;
1400
1401 case ODA_SELECT:
1402 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1403 break;
1404
1405 default:
1406 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1407 break;
1408 }
1409 }
1410
1411
1412 /**********************************************************************
1413 * Ownerdrawn Button Functions
1414 */
1415
1416 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1417 {
1418 LONG state = get_button_state( hwnd );
1419 DRAWITEMSTRUCT dis;
1420 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1421 HWND parent;
1422 HFONT hFont, hPrevFont = 0;
1423 HRGN hrgn;
1424
1425 dis.CtlType = ODT_BUTTON;
1426 dis.CtlID = id;
1427 dis.itemID = 0;
1428 dis.itemAction = action;
1429 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1430 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1431 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1432 dis.hwndItem = hwnd;
1433 dis.hDC = hDC;
1434 dis.itemData = 0;
1435 GetClientRect( hwnd, &dis.rcItem );
1436
1437 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1438 parent = GetParent(hwnd);
1439 if (!parent) parent = hwnd;
1440 #if defined(__REACTOS__) && defined(_USER32_)
1441 GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
1442 #else
1443 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1444 #endif
1445
1446 hrgn = set_control_clipping( hDC, &dis.rcItem );
1447
1448 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1449 if (hPrevFont) SelectObject(hDC, hPrevFont);
1450 SelectClipRgn( hDC, hrgn );
1451 if (hrgn) DeleteObject( hrgn );
1452 }
1453
1454 #ifndef _USER32_
1455 void BUTTON_Register()
1456 {
1457 WNDCLASSW wndClass;
1458
1459 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
1460 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC;
1461 wndClass.lpfnWndProc = ButtonWndProcW;
1462 wndClass.cbClsExtra = 0;
1463 wndClass.cbWndExtra = NB_EXTRA_BYTES;
1464 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
1465 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1466 wndClass.lpszClassName = WC_BUTTONW;
1467
1468 RegisterClassW(&wndClass);
1469 }
1470
1471 void BUTTON_Unregister()
1472 {
1473 UnregisterClassW(WC_BUTTONW, NULL);
1474 }
1475 #endif