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