[COMCTL32] -Redraw the button when we get a WM_THEMECHANGED.
[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 InvalidateRect(hWnd, NULL, FALSE);
384 break;
385
386 case WM_MOUSEHOVER:
387 {
388 int state = (int)SendMessageW(hWnd, BM_GETSTATE, 0, 0);
389 SetWindowLongW(hWnd, 0, state|BST_HOT);
390 InvalidateRect(hWnd, NULL, FALSE);
391 break;
392 }
393
394 case WM_MOUSELEAVE:
395 {
396 int state = (int)SendMessageW(hWnd, BM_GETSTATE, 0, 0);
397 SetWindowLongW(hWnd, 0, state&(~BST_HOT));
398 InvalidateRect(hWnd, NULL, FALSE);
399 break;
400 }
401 #endif
402 case WM_ERASEBKGND:
403 if (btn_type == BS_OWNERDRAW)
404 {
405 HDC hdc = (HDC)wParam;
406 RECT rc;
407 HBRUSH hBrush;
408 HWND parent = GetParent(hWnd);
409 if (!parent) parent = hWnd;
410 #if defined(__REACTOS__) && defined(_USER32_)
411 hBrush = GetControlColor( parent, hWnd, hdc, WM_CTLCOLORBTN);
412 #else
413 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
414 if (!hBrush) /* did the app forget to call defwindowproc ? */
415 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
416 (WPARAM)hdc, (LPARAM)hWnd);
417 #endif
418 GetClientRect(hWnd, &rc);
419 FillRect(hdc, &rc, hBrush);
420 }
421 return 1;
422
423 case WM_PRINTCLIENT:
424 case WM_PAINT:
425 {
426 PAINTSTRUCT ps;
427 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
428 #ifndef _USER32_
429 HTHEME theme = GetWindowTheme(hWnd);
430 if (theme && BUTTON_PaintWithTheme(theme, hWnd, hdc, uMsg == WM_PRINTCLIENT ? lParam : 0))
431 {
432 if ( !wParam ) EndPaint( hWnd, &ps );
433 return 0;
434 }
435 #endif
436 if (btnPaintFunc[btn_type])
437 {
438 int nOldMode = SetBkMode( hdc, OPAQUE );
439 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
440 SetBkMode(hdc, nOldMode); /* reset painting mode */
441 }
442 if ( !wParam ) EndPaint( hWnd, &ps );
443 break;
444 }
445
446 case WM_KEYDOWN:
447 if (wParam == VK_SPACE)
448 {
449 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
450 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
451 SetCapture( hWnd );
452 }
453 break;
454
455 case WM_LBUTTONDBLCLK:
456 if(style & BS_NOTIFY ||
457 btn_type == BS_RADIOBUTTON ||
458 btn_type == BS_USERBUTTON ||
459 btn_type == BS_OWNERDRAW)
460 {
461 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
462 break;
463 }
464 /* fall through */
465 case WM_LBUTTONDOWN:
466 SetCapture( hWnd );
467 SetFocus( hWnd );
468 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
469 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
470 break;
471
472 case WM_KEYUP:
473 if (wParam != VK_SPACE)
474 break;
475 /* fall through */
476 case WM_LBUTTONUP:
477 #ifdef _REACTOS_
478 BOOL TellParent = FALSE; //// ReactOS see note below.
479 #endif
480 state = get_button_state( hWnd );
481 if (!(state & BUTTON_BTNPRESSED)) break;
482 state &= BUTTON_NSTATES;
483 set_button_state( hWnd, state );
484 if (!(state & BST_PUSHED))
485 {
486 ReleaseCapture();
487 break;
488 }
489 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
490 GetClientRect( hWnd, &rect );
491 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
492 {
493 state = get_button_state( hWnd );
494 switch(btn_type)
495 {
496 case BS_AUTOCHECKBOX:
497 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
498 break;
499 case BS_AUTORADIOBUTTON:
500 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
501 break;
502 case BS_AUTO3STATE:
503 SendMessageW( hWnd, BM_SETCHECK,
504 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
505 break;
506 }
507 #ifdef _REACTOS_
508 TellParent = TRUE; // <---- Fix CORE-10194, Notify parent after capture is released.
509 #else
510 ReleaseCapture();
511 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
512 #endif
513 }
514 #ifdef _REACTOS_
515 ReleaseCapture();
516 if (TellParent) BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
517 #else
518 else
519 {
520 ReleaseCapture();
521 }
522 #endif
523 break;
524
525 case WM_CAPTURECHANGED:
526 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
527 if (hWnd == (HWND)lParam) break;
528 state = get_button_state( hWnd );
529 if (state & BUTTON_BTNPRESSED)
530 {
531 state &= BUTTON_NSTATES;
532 set_button_state( hWnd, state );
533 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
534 }
535 break;
536
537 case WM_MOUSEMOVE:
538 #ifndef _USER32_
539 {
540 TRACKMOUSEEVENT mouse_event;
541 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
542 mouse_event.dwFlags = TME_QUERY;
543 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
544 {
545 mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
546 mouse_event.hwndTrack = hWnd;
547 mouse_event.dwHoverTime = 1;
548 TrackMouseEvent(&mouse_event);
549 }
550 }
551 #endif
552
553 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
554 {
555 GetClientRect( hWnd, &rect );
556 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
557 }
558 break;
559
560 case WM_SETTEXT:
561 {
562 /* Clear an old text here as Windows does */
563 //
564 // ReactOS Note :
565 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
566 // Patch: http://source.winehq.org/patches/data/70889
567 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
568 //
569 #ifdef __REACTOS__
570 if (style & WS_VISIBLE)
571 #else
572 if (IsWindowVisible(hWnd))
573 #endif
574 {
575 HDC hdc = GetDC(hWnd);
576 HBRUSH hbrush;
577 RECT client, rc;
578 HWND parent = GetParent(hWnd);
579 UINT message = (btn_type == BS_PUSHBUTTON ||
580 btn_type == BS_DEFPUSHBUTTON ||
581 btn_type == BS_PUSHLIKE ||
582 btn_type == BS_USERBUTTON ||
583 btn_type == BS_OWNERDRAW) ?
584 WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
585
586 if (!parent) parent = hWnd;
587 #if defined(__REACTOS__) && defined(_USER32_)
588 hbrush = GetControlColor(parent, hWnd, hdc, message);
589 #else
590 hbrush = (HBRUSH)SendMessageW(parent, message,
591 (WPARAM)hdc, (LPARAM)hWnd);
592 if (!hbrush) /* did the app forget to call DefWindowProc ? */
593 hbrush = (HBRUSH)DefWindowProcW(parent, message,
594 (WPARAM)hdc, (LPARAM)hWnd);
595 #endif
596
597 GetClientRect(hWnd, &client);
598 rc = client;
599 /* FIXME: check other BS_* handlers */
600 if (btn_type == BS_GROUPBOX)
601 InflateRect(&rc, -7, 1); /* GB_Paint does this */
602 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
603 /* Clip by client rect bounds */
604 if (rc.right > client.right) rc.right = client.right;
605 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
606 FillRect(hdc, &rc, hbrush);
607 ReleaseDC(hWnd, hdc);
608 }
609
610 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
611 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
612 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
613 InvalidateRect( hWnd, NULL, TRUE );
614 else
615 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
616 return 1; /* success. FIXME: check text length */
617 }
618
619 case WM_SETFONT:
620 set_button_font( hWnd, (HFONT)wParam );
621 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
622 break;
623
624 case WM_GETFONT:
625 return (LRESULT)get_button_font( hWnd );
626
627 case WM_SETFOCUS:
628 TRACE("WM_SETFOCUS %p\n",hWnd);
629 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
630 if (btn_type == BS_OWNERDRAW)
631 paint_button( hWnd, btn_type, ODA_FOCUS );
632 else
633 InvalidateRect(hWnd, NULL, FALSE);
634 if (style & BS_NOTIFY)
635 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
636 break;
637
638 case WM_KILLFOCUS:
639 TRACE("WM_KILLFOCUS %p\n",hWnd);
640 state = get_button_state( hWnd );
641 set_button_state( hWnd, state & ~BST_FOCUS );
642
643 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
644 ReleaseCapture();
645 if (style & BS_NOTIFY)
646 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
647
648 InvalidateRect( hWnd, NULL, FALSE );
649 break;
650
651 case WM_SYSCOLORCHANGE:
652 InvalidateRect( hWnd, NULL, FALSE );
653 break;
654
655 case BM_SETSTYLE:
656 btn_type = wParam & BS_TYPEMASK;
657 style = (style & ~BS_TYPEMASK) | btn_type;
658 #ifdef __REACTOS__
659 NtUserAlterWindowStyle(hWnd, GWL_STYLE, style);
660 #else
661 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
662 #endif
663
664 /* Only redraw if lParam flag is set.*/
665 if (lParam)
666 InvalidateRect( hWnd, NULL, TRUE );
667
668 break;
669
670 case BM_CLICK:
671 #ifdef __REACTOS__
672 state = get_button_state(hWnd);
673 if (state & BUTTON_BMCLICK)
674 break;
675 set_button_state(hWnd, state | BUTTON_BMCLICK); // Tracked in STATE_GWL_OFFSET.
676 #endif
677 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
678 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
679 #ifdef __REACTOS__
680 state = get_button_state(hWnd);
681 if (!(state & BUTTON_BMCLICK)) break;
682 state &= ~BUTTON_BMCLICK;
683 set_button_state(hWnd, state);
684 #endif
685 break;
686
687 case BM_SETIMAGE:
688 /* Check that image format matches button style */
689 switch (style & (BS_BITMAP|BS_ICON))
690 {
691 case BS_BITMAP:
692 if (wParam != IMAGE_BITMAP) return 0;
693 break;
694 case BS_ICON:
695 if (wParam != IMAGE_ICON) return 0;
696 break;
697 default:
698 return 0;
699 }
700 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
701 InvalidateRect( hWnd, NULL, FALSE );
702 return (LRESULT)oldHbitmap;
703
704 case BM_GETIMAGE:
705 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
706
707 case BM_GETCHECK:
708 return get_button_state( hWnd ) & 3;
709
710 case BM_SETCHECK:
711 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
712 state = get_button_state( hWnd );
713 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
714 {
715 #ifdef __REACTOS__
716 if (wParam) style |= WS_TABSTOP;
717 else style &= ~WS_TABSTOP;
718 NtUserAlterWindowStyle(hWnd, GWL_STYLE, style);
719 #else
720 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
721 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
722 #endif
723 }
724 if ((state & 3) != wParam)
725 {
726 set_button_state( hWnd, (state & ~3) | wParam );
727 InvalidateRect(hWnd, NULL, FALSE);
728 }
729 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BST_CHECKED) && (style & WS_CHILD))
730 BUTTON_CheckAutoRadioButton( hWnd );
731 break;
732
733 case BM_GETSTATE:
734 return get_button_state( hWnd );
735
736 case BM_SETSTATE:
737 state = get_button_state( hWnd );
738 if (wParam)
739 set_button_state( hWnd, state | BST_PUSHED );
740 else
741 set_button_state( hWnd, state & ~BST_PUSHED );
742
743 InvalidateRect(hWnd, NULL, FALSE);
744 break;
745
746 #ifdef __REACTOS__
747 case WM_UPDATEUISTATE:
748 if (unicode)
749 DefWindowProcW(hWnd, uMsg, wParam, lParam);
750 else
751 DefWindowProcA(hWnd, uMsg, wParam, lParam);
752
753 if (button_update_uistate(hWnd, unicode))
754 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
755 break;
756 #endif
757
758 case WM_NCHITTEST:
759 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
760 /* fall through */
761 default:
762 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
763 DefWindowProcA(hWnd, uMsg, wParam, lParam);
764 }
765 return 0;
766 }
767
768 #ifdef __REACTOS__
769
770 /***********************************************************************
771 * ButtonWndProcW
772 * The button window procedure. This is just a wrapper which locks
773 * the passed HWND and calls the real window procedure (with a WND*
774 * pointer pointing to the locked windowstructure).
775 */
776 LRESULT WINAPI ButtonWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
777 {
778 if (!IsWindow(hWnd)) return 0;
779 return ButtonWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
780 }
781
782 /***********************************************************************
783 * ButtonWndProcA
784 */
785 LRESULT WINAPI ButtonWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
786 {
787 if (!IsWindow(hWnd)) return 0;
788 return ButtonWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
789 }
790
791 #endif /* __REACTOS__ */
792
793 /**********************************************************************
794 * Convert button styles to flags used by DrawText.
795 */
796 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
797 {
798 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
799
800 /* "Convert" pushlike buttons to pushbuttons */
801 if (style & BS_PUSHLIKE)
802 style &= ~BS_TYPEMASK;
803
804 if (!(style & BS_MULTILINE))
805 dtStyle |= DT_SINGLELINE;
806 else
807 dtStyle |= DT_WORDBREAK;
808
809 switch (style & BS_CENTER)
810 {
811 case BS_LEFT: /* DT_LEFT is 0 */ break;
812 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
813 case BS_CENTER: dtStyle |= DT_CENTER; break;
814 default:
815 /* Pushbutton's text is centered by default */
816 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
817 /* all other flavours have left aligned text */
818 }
819
820 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
821
822 /* DrawText ignores vertical alignment for multiline text,
823 * but we use these flags to align label manually.
824 */
825 if (get_button_type(style) != BS_GROUPBOX)
826 {
827 switch (style & BS_VCENTER)
828 {
829 case BS_TOP: /* DT_TOP is 0 */ break;
830 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
831 case BS_VCENTER: /* fall through */
832 default: dtStyle |= DT_VCENTER; break;
833 }
834 }
835 else
836 /* GroupBox's text is always single line and is top aligned. */
837 dtStyle |= DT_SINGLELINE;
838
839 return dtStyle;
840 }
841
842 /**********************************************************************
843 * BUTTON_CalcLabelRect
844 *
845 * Calculates label's rectangle depending on button style.
846 *
847 * Returns flags to be passed to DrawText.
848 * Calculated rectangle doesn't take into account button state
849 * (pushed, etc.). If there is nothing to draw (no text/image) output
850 * rectangle is empty, and return value is (UINT)-1.
851 */
852 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
853 {
854 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
855 LONG ex_style = GetWindowLongPtrW( hwnd, GWL_EXSTYLE );
856 WCHAR *text;
857 ICONINFO iconInfo;
858 BITMAP bm;
859 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
860 RECT r = *rc;
861 INT n;
862
863 /* Calculate label rectangle according to label type */
864 switch (style & (BS_ICON|BS_BITMAP))
865 {
866 case BS_TEXT:
867 {
868 HFONT hFont, hPrevFont = 0;
869
870 if (!(text = get_button_text( hwnd ))) goto empty_rect;
871 if (!text[0])
872 {
873 HeapFree( GetProcessHeap(), 0, text );
874 goto empty_rect;
875 }
876
877 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hdc, hFont );
878 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
879 if (hPrevFont) SelectObject( hdc, hPrevFont );
880 HeapFree( GetProcessHeap(), 0, text );
881 #ifdef __REACTOS__
882 if (get_ui_state(hwnd) & UISF_HIDEACCEL)
883 dtStyle |= DT_HIDEPREFIX;
884 #endif
885 break;
886 }
887
888 case BS_ICON:
889 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
890 goto empty_rect;
891
892 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
893
894 r.right = r.left + bm.bmWidth;
895 r.bottom = r.top + bm.bmHeight;
896
897 DeleteObject(iconInfo.hbmColor);
898 DeleteObject(iconInfo.hbmMask);
899 break;
900
901 case BS_BITMAP:
902 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
903 goto empty_rect;
904
905 r.right = r.left + bm.bmWidth;
906 r.bottom = r.top + bm.bmHeight;
907 break;
908
909 default:
910 empty_rect:
911 rc->right = r.left;
912 rc->bottom = r.top;
913 return (UINT)-1;
914 }
915
916 /* Position label inside bounding rectangle according to
917 * alignment flags. (calculated rect is always left-top aligned).
918 * If label is aligned to any side - shift label in opposite
919 * direction to leave extra space for focus rectangle.
920 */
921 switch (dtStyle & (DT_CENTER|DT_RIGHT))
922 {
923 case DT_LEFT: r.left++; r.right++; break;
924 case DT_CENTER: n = r.right - r.left;
925 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
926 r.right = r.left + n; break;
927 case DT_RIGHT: n = r.right - r.left;
928 r.right = rc->right - 1;
929 r.left = r.right - n;
930 break;
931 }
932
933 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
934 {
935 case DT_TOP: r.top++; r.bottom++; break;
936 case DT_VCENTER: n = r.bottom - r.top;
937 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
938 r.bottom = r.top + n; break;
939 case DT_BOTTOM: n = r.bottom - r.top;
940 r.bottom = rc->bottom - 1;
941 r.top = r.bottom - n;
942 break;
943 }
944
945 *rc = r;
946 return dtStyle;
947 }
948
949
950 /**********************************************************************
951 * BUTTON_DrawTextCallback
952 *
953 * Callback function used by DrawStateW function.
954 */
955 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
956 {
957 RECT rc;
958
959 SetRect(&rc, 0, 0, cx, cy);
960 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
961 return TRUE;
962 }
963
964
965 /**********************************************************************
966 * BUTTON_DrawLabel
967 *
968 * Common function for drawing button label.
969 */
970 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
971 {
972 DRAWSTATEPROC lpOutputProc = NULL;
973 LPARAM lp;
974 WPARAM wp = 0;
975 HBRUSH hbr = 0;
976 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
977 LONG state = get_button_state( hwnd );
978 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
979 WCHAR *text = NULL;
980
981 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
982 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
983 * I don't have Win31 on hand to verify that, so I leave it as is.
984 */
985
986 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
987 {
988 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
989 flags |= DSS_MONO;
990 }
991
992 switch (style & (BS_ICON|BS_BITMAP))
993 {
994 case BS_TEXT:
995 /* DST_COMPLEX -- is 0 */
996 lpOutputProc = BUTTON_DrawTextCallback;
997 if (!(text = get_button_text( hwnd ))) return;
998 lp = (LPARAM)text;
999 wp = (WPARAM)dtFlags;
1000
1001 #ifdef __REACTOS__
1002 if (dtFlags & DT_HIDEPREFIX)
1003 flags |= DSS_HIDEPREFIX;
1004 #endif
1005 break;
1006
1007 case BS_ICON:
1008 flags |= DST_ICON;
1009 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
1010 break;
1011
1012 case BS_BITMAP:
1013 flags |= DST_BITMAP;
1014 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
1015 break;
1016
1017 default:
1018 return;
1019 }
1020
1021 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
1022 rc->right - rc->left, rc->bottom - rc->top, flags);
1023 HeapFree( GetProcessHeap(), 0, text );
1024 }
1025
1026 /**********************************************************************
1027 * Push Button Functions
1028 */
1029 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
1030 {
1031 RECT rc, r;
1032 UINT dtFlags, uState;
1033 HPEN hOldPen;
1034 HBRUSH hOldBrush;
1035 INT oldBkMode;
1036 COLORREF oldTxtColor;
1037 HFONT hFont;
1038 LONG state = get_button_state( hwnd );
1039 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1040 BOOL pushedState = (state & BST_PUSHED);
1041 HWND parent;
1042 HRGN hrgn;
1043
1044 GetClientRect( hwnd, &rc );
1045
1046 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
1047 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1048 parent = GetParent(hwnd);
1049 if (!parent) parent = hwnd;
1050 #if defined(__REACTOS__) && defined(_USER32_)
1051 GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
1052 #else
1053 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1054 #endif
1055
1056 hrgn = set_control_clipping( hDC, &rc );
1057 #ifdef __REACTOS__
1058 hOldPen = SelectObject(hDC, GetStockObject(DC_PEN));
1059 SetDCPenColor(hDC, GetSysColor(COLOR_WINDOWFRAME));
1060 #else
1061 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
1062 #endif
1063 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
1064 oldBkMode = SetBkMode(hDC, TRANSPARENT);
1065
1066 if (get_button_type(style) == BS_DEFPUSHBUTTON)
1067 {
1068 if (action != ODA_FOCUS)
1069 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
1070 InflateRect( &rc, -1, -1 );
1071 }
1072
1073 /* completely skip the drawing if only focus has changed */
1074 if (action == ODA_FOCUS) goto draw_focus;
1075
1076 uState = DFCS_BUTTONPUSH;
1077
1078 if (style & BS_FLAT)
1079 uState |= DFCS_MONO;
1080 else if (pushedState)
1081 {
1082 if (get_button_type(style) == BS_DEFPUSHBUTTON )
1083 uState |= DFCS_FLAT;
1084 else
1085 uState |= DFCS_PUSHED;
1086 }
1087
1088 if (state & (BST_CHECKED | BST_INDETERMINATE))
1089 uState |= DFCS_CHECKED;
1090
1091 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
1092
1093 /* draw button label */
1094 r = rc;
1095 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
1096
1097 if (dtFlags == (UINT)-1L)
1098 goto cleanup;
1099
1100 if (pushedState)
1101 OffsetRect(&r, 1, 1);
1102
1103 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
1104
1105 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
1106
1107 SetTextColor( hDC, oldTxtColor );
1108
1109 draw_focus:
1110 if (action == ODA_FOCUS || (state & BST_FOCUS))
1111 {
1112 #ifdef __REACTOS__
1113 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1114 {
1115 #endif
1116 InflateRect( &rc, -2, -2 );
1117 DrawFocusRect( hDC, &rc );
1118 #ifdef __REACTOS__
1119 }
1120 #endif
1121 }
1122
1123 cleanup:
1124 SelectObject( hDC, hOldPen );
1125 SelectObject( hDC, hOldBrush );
1126 SetBkMode(hDC, oldBkMode);
1127 SelectClipRgn( hDC, hrgn );
1128 if (hrgn) DeleteObject( hrgn );
1129 }
1130
1131 /**********************************************************************
1132 * Check Box & Radio Button Functions
1133 */
1134
1135 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
1136 {
1137 RECT rbox, rtext, client;
1138 HBRUSH hBrush;
1139 int delta, text_offset, checkBoxWidth, checkBoxHeight;
1140 UINT dtFlags;
1141 HFONT hFont;
1142 LONG state = get_button_state( hwnd );
1143 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1144 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
1145 HWND parent;
1146 HRGN hrgn;
1147
1148 if (style & BS_PUSHLIKE)
1149 {
1150 PB_Paint( hwnd, hDC, action );
1151 return;
1152 }
1153
1154 GetClientRect(hwnd, &client);
1155 rbox = rtext = client;
1156
1157 checkBoxWidth = 12 * GetDeviceCaps( hDC, LOGPIXELSX ) / 96 + 1;
1158 checkBoxHeight = 12 * GetDeviceCaps( hDC, LOGPIXELSY ) / 96 + 1;
1159
1160 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1161 GetCharWidthW( hDC, '0', '0', &text_offset );
1162 text_offset /= 2;
1163
1164 parent = GetParent(hwnd);
1165 if (!parent) parent = hwnd;
1166 #if defined(__REACTOS__) && defined(_USER32_)
1167 hBrush = GetControlColor(parent, hwnd, hDC, WM_CTLCOLORSTATIC);
1168 #else
1169 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
1170 (WPARAM)hDC, (LPARAM)hwnd);
1171 if (!hBrush) /* did the app forget to call defwindowproc ? */
1172 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1173 (WPARAM)hDC, (LPARAM)hwnd );
1174 #endif
1175 hrgn = set_control_clipping( hDC, &client );
1176
1177 if (style & BS_LEFTTEXT || ex_style & WS_EX_RIGHT)
1178 {
1179 /* magic +4 is what CTL3D expects */
1180
1181 rtext.right -= checkBoxWidth + text_offset;;
1182 rbox.left = rbox.right - checkBoxWidth;
1183 }
1184 else
1185 {
1186 rtext.left += checkBoxWidth + text_offset;;
1187 rbox.right = checkBoxWidth;
1188 }
1189
1190 /* Since WM_ERASEBKGND does nothing, first prepare background */
1191 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
1192 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
1193
1194 /* Draw label */
1195 client = rtext;
1196 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
1197
1198 /* Only adjust rbox when rtext is valid */
1199 if (dtFlags != (UINT)-1L)
1200 {
1201 rbox.top = rtext.top;
1202 rbox.bottom = rtext.bottom;
1203 }
1204
1205 /* Draw the check-box bitmap */
1206 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
1207 {
1208 UINT flags;
1209
1210 if ((get_button_type(style) == BS_RADIOBUTTON) ||
1211 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
1212 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
1213 else flags = DFCS_BUTTONCHECK;
1214
1215 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
1216 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
1217
1218 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
1219
1220 /* rbox must have the correct height */
1221 delta = rbox.bottom - rbox.top - checkBoxHeight;
1222
1223 if (style & BS_TOP) {
1224 if (delta > 0) {
1225 rbox.bottom = rbox.top + checkBoxHeight;
1226 } else {
1227 rbox.top -= -delta/2 + 1;
1228 rbox.bottom = rbox.top + checkBoxHeight;
1229 }
1230 } else if (style & BS_BOTTOM) {
1231 if (delta > 0) {
1232 rbox.top = rbox.bottom - checkBoxHeight;
1233 } else {
1234 rbox.bottom += -delta/2 + 1;
1235 rbox.top = rbox.bottom - checkBoxHeight;
1236 }
1237 } else { /* Default */
1238 if (delta > 0) {
1239 int ofs = (delta / 2);
1240 rbox.bottom -= ofs + 1;
1241 rbox.top = rbox.bottom - checkBoxHeight;
1242 } else if (delta < 0) {
1243 int ofs = (-delta / 2);
1244 rbox.top -= ofs + 1;
1245 rbox.bottom = rbox.top + checkBoxHeight;
1246 }
1247 }
1248
1249 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
1250 }
1251
1252 if (dtFlags == (UINT)-1L) /* Noting to draw */
1253 return;
1254
1255 if (action == ODA_DRAWENTIRE)
1256 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
1257
1258 /* ... and focus */
1259 if (action == ODA_FOCUS || (state & BST_FOCUS))
1260 {
1261 #ifdef __REACTOS__
1262 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1263 {
1264 #endif
1265 rtext.left--;
1266 rtext.right++;
1267 IntersectRect(&rtext, &rtext, &client);
1268 DrawFocusRect( hDC, &rtext );
1269 #ifdef __REACTOS__
1270 }
1271 #endif
1272 }
1273 SelectClipRgn( hDC, hrgn );
1274 if (hrgn) DeleteObject( hrgn );
1275 }
1276
1277
1278 /**********************************************************************
1279 * BUTTON_CheckAutoRadioButton
1280 *
1281 * hwnd is checked, uncheck every other auto radio button in group
1282 */
1283 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
1284 {
1285 HWND parent, sibling, start;
1286
1287 parent = GetParent(hwnd);
1288 /* make sure that starting control is not disabled or invisible */
1289 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1290 do
1291 {
1292 if (!sibling) break;
1293 if ((hwnd != sibling) &&
1294 ((GetWindowLongPtrW( sibling, GWL_STYLE) & BS_TYPEMASK) == BS_AUTORADIOBUTTON))
1295 SendMessageW( sibling, BM_SETCHECK, BST_UNCHECKED, 0 );
1296 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1297 } while (sibling != start);
1298 }
1299
1300
1301 /**********************************************************************
1302 * Group Box Functions
1303 */
1304
1305 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1306 {
1307 RECT rc, rcFrame;
1308 HBRUSH hbr;
1309 HFONT hFont;
1310 UINT dtFlags;
1311 TEXTMETRICW tm;
1312 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1313 HWND parent;
1314 HRGN hrgn;
1315
1316 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1317 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1318 parent = GetParent(hwnd);
1319 if (!parent) parent = hwnd;
1320 #if defined(__REACTOS__) && defined(_USER32_)
1321 hbr = GetControlColor(parent, hwnd, hDC, WM_CTLCOLORSTATIC);
1322 #else
1323 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1324 if (!hbr) /* did the app forget to call defwindowproc ? */
1325 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1326 (WPARAM)hDC, (LPARAM)hwnd);
1327 #endif
1328 GetClientRect( hwnd, &rc);
1329 rcFrame = rc;
1330 hrgn = set_control_clipping( hDC, &rc );
1331
1332 GetTextMetricsW (hDC, &tm);
1333 rcFrame.top += (tm.tmHeight / 2) - 1;
1334 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1335
1336 InflateRect(&rc, -7, 1);
1337 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1338
1339 if (dtFlags != (UINT)-1L)
1340 {
1341 /* Because buttons have CS_PARENTDC class style, there is a chance
1342 * that label will be drawn out of client rect.
1343 * But Windows doesn't clip label's rect, so do I.
1344 */
1345
1346 /* There is 1-pixel margin at the left, right, and bottom */
1347 rc.left--; rc.right++; rc.bottom++;
1348 FillRect(hDC, &rc, hbr);
1349 rc.left++; rc.right--; rc.bottom--;
1350
1351 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1352 }
1353 SelectClipRgn( hDC, hrgn );
1354 if (hrgn) DeleteObject( hrgn );
1355 }
1356
1357
1358 /**********************************************************************
1359 * User Button Functions
1360 */
1361
1362 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1363 {
1364 RECT rc;
1365 HBRUSH hBrush;
1366 HFONT hFont;
1367 LONG state = get_button_state( hwnd );
1368 HWND parent;
1369
1370 GetClientRect( hwnd, &rc);
1371
1372 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1373
1374 parent = GetParent(hwnd);
1375 if (!parent) parent = hwnd;
1376 #if defined(__REACTOS__) && defined(_USER32_)
1377 hBrush = GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
1378 #else
1379 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1380 if (!hBrush) /* did the app forget to call defwindowproc ? */
1381 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1382 (WPARAM)hDC, (LPARAM)hwnd);
1383 #endif
1384
1385 FillRect( hDC, &rc, hBrush );
1386 if (action == ODA_FOCUS || (state & BST_FOCUS))
1387 #ifdef __REACTOS__
1388 {
1389 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1390 #endif
1391 DrawFocusRect( hDC, &rc );
1392 #ifdef __REACTOS__
1393 }
1394 #endif
1395
1396 switch (action)
1397 {
1398 case ODA_FOCUS:
1399 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1400 break;
1401
1402 case ODA_SELECT:
1403 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1404 break;
1405
1406 default:
1407 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1408 break;
1409 }
1410 }
1411
1412
1413 /**********************************************************************
1414 * Ownerdrawn Button Functions
1415 */
1416
1417 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1418 {
1419 LONG state = get_button_state( hwnd );
1420 DRAWITEMSTRUCT dis;
1421 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1422 HWND parent;
1423 HFONT hFont, hPrevFont = 0;
1424 HRGN hrgn;
1425
1426 dis.CtlType = ODT_BUTTON;
1427 dis.CtlID = id;
1428 dis.itemID = 0;
1429 dis.itemAction = action;
1430 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1431 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1432 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1433 dis.hwndItem = hwnd;
1434 dis.hDC = hDC;
1435 dis.itemData = 0;
1436 GetClientRect( hwnd, &dis.rcItem );
1437
1438 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1439 parent = GetParent(hwnd);
1440 if (!parent) parent = hwnd;
1441 #if defined(__REACTOS__) && defined(_USER32_)
1442 GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
1443 #else
1444 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1445 #endif
1446
1447 hrgn = set_control_clipping( hDC, &dis.rcItem );
1448
1449 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1450 if (hPrevFont) SelectObject(hDC, hPrevFont);
1451 SelectClipRgn( hDC, hrgn );
1452 if (hrgn) DeleteObject( hrgn );
1453 }
1454
1455 #ifndef _USER32_
1456 void BUTTON_Register()
1457 {
1458 WNDCLASSW wndClass;
1459
1460 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
1461 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC;
1462 wndClass.lpfnWndProc = ButtonWndProcW;
1463 wndClass.cbClsExtra = 0;
1464 wndClass.cbWndExtra = NB_EXTRA_BYTES;
1465 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
1466 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1467 wndClass.lpszClassName = WC_BUTTONW;
1468
1469 RegisterClassW(&wndClass);
1470 }
1471
1472 void BUTTON_Unregister()
1473 {
1474 UnregisterClassW(WC_BUTTONW, NULL);
1475 }
1476 #endif