b6f2947c5a4e5616ecbeeb4bb5a60e3f19414212
[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
159
160 static inline LONG get_button_state( HWND hwnd )
161 {
162 return GetWindowLongPtrW( hwnd, STATE_GWL_OFFSET );
163 }
164
165 static inline void set_button_state( HWND hwnd, LONG state )
166 {
167 SetWindowLongPtrW( hwnd, STATE_GWL_OFFSET, state );
168 }
169
170 #ifdef __REACTOS__
171
172 static __inline void set_ui_state( HWND hwnd, LONG flags )
173 {
174 SetWindowLongPtrW( hwnd, UISTATE_GWL_OFFSET, flags );
175 }
176
177 static __inline LONG get_ui_state( HWND hwnd )
178 {
179 return GetWindowLongPtrW( hwnd, UISTATE_GWL_OFFSET );
180 }
181
182 #endif /* __REACTOS__ */
183
184 static inline HFONT get_button_font( HWND hwnd )
185 {
186 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
187 }
188
189 static inline void set_button_font( HWND hwnd, HFONT font )
190 {
191 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
192 }
193
194 static inline UINT get_button_type( LONG window_style )
195 {
196 return (window_style & BS_TYPEMASK);
197 }
198
199 /* paint a button of any type */
200 static inline void paint_button( HWND hwnd, LONG style, UINT action )
201 {
202 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
203 {
204 HDC hdc = GetDC( hwnd );
205 btnPaintFunc[style]( hwnd, hdc, action );
206 ReleaseDC( hwnd, hdc );
207 }
208 }
209
210 #else
211
212 #define NtUserAlterWindowStyle SetWindowLongPtrW
213
214 static inline void _SetButtonData(HWND hwnd, PBUTTON_DATA data)
215 {
216 SetWindowLongPtrW( hwnd, 0, (LONG)data );
217 }
218
219 HRGN set_control_clipping( HDC hdc, const RECT *rect )
220 {
221 RECT rc = *rect;
222 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
223
224 if (GetClipRgn( hdc, hrgn ) != 1)
225 {
226 DeleteObject( hrgn );
227 hrgn = 0;
228 }
229 DPtoLP( hdc, (POINT *)&rc, 2 );
230 if (GetLayout( hdc ) & LAYOUT_RTL) /* compensate for the shifting done by IntersectClipRect */
231 {
232 rc.left++;
233 rc.right++;
234 }
235 IntersectClipRect( hdc, rc.left, rc.top, rc.right, rc.bottom );
236 return hrgn;
237 }
238
239 BOOL BUTTON_PaintWithTheme(HTHEME theme, HWND hwnd, HDC hParamDC, LPARAM prfFlag);
240 WCHAR *get_button_text( HWND hwnd );
241
242 static inline LONG_PTR get_button_image(HWND hwnd)
243 {
244 return _GetButtonData(hwnd)->image;
245 }
246
247 static inline LONG_PTR set_button_image(HWND hwnd, LONG_PTR image)
248 {
249 PBUTTON_DATA data = _GetButtonData(hwnd);
250 LONG_PTR ret = data->image;
251 data->image = image;
252 return ret;
253 }
254
255 static inline LONG get_button_state( HWND hwnd )
256 {
257 return _GetButtonData(hwnd)->state;
258 }
259
260 static inline void set_button_state( HWND hwnd, LONG state )
261 {
262 _GetButtonData(hwnd)->state = state;
263 }
264
265 static __inline void set_ui_state( HWND hwnd, LONG flags )
266 {
267 _GetButtonData(hwnd)->ui_state = flags;
268 }
269
270 static __inline LONG get_ui_state( HWND hwnd )
271 {
272 return _GetButtonData(hwnd)->ui_state;
273 }
274
275 static inline HFONT get_button_font( HWND hwnd )
276 {
277 return (HFONT)_GetButtonData(hwnd)->font;
278 }
279
280 static inline void set_button_font( HWND hwnd, HFONT font )
281 {
282 _GetButtonData(hwnd)->font = font;
283 }
284
285 static inline UINT get_button_type( LONG window_style )
286 {
287 return (window_style & BS_TYPEMASK);
288 }
289
290 /* paint a button of any type */
291 static inline void paint_button( HWND hwnd, LONG style, UINT action )
292 {
293 HTHEME theme = GetWindowTheme(hwnd);
294 RECT rc;
295 HDC hdc = GetDC( hwnd );
296 /* GetDC appears to give a dc with a clip rect that includes the whoe parent, not sure if it is correct or not. */
297 GetClientRect(hwnd, &rc);
298 IntersectClipRect (hdc, rc.left, rc. top, rc.right, rc.bottom);
299 if (theme && BUTTON_PaintWithTheme(theme, hwnd, hdc, 0))
300 {
301 ReleaseDC( hwnd, hdc );
302 return;
303 }
304 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
305 {
306 btnPaintFunc[style]( hwnd, hdc, action );
307 }
308 ReleaseDC( hwnd, hdc );
309 }
310
311 BOOL BUTTON_GetIdealSize(HTHEME theme, HWND hwnd, SIZE* psize)
312 {
313 PBUTTON_DATA pdata;
314 HDC hdc;
315 WCHAR *text;
316 HFONT hFont = 0, hPrevFont = 0;
317 SIZE TextSize, ImageSize, ButtonSize;
318 BOOL ret = FALSE;
319 LOGFONTW logfont = {0};
320
321 pdata = _GetButtonData(hwnd);
322 text = get_button_text( hwnd );
323 hdc = GetDC(hwnd);
324 if (!pdata || !text || !hdc || !text[0])
325 goto cleanup;
326
327 /* FIXME : Should use GetThemeTextExtent but unfortunately uses DrawTextW which is broken */
328 if (theme)
329 {
330 HRESULT hr = GetThemeFont(theme, hdc, BP_PUSHBUTTON, PBS_NORMAL, TMT_FONT, &logfont);
331 if(SUCCEEDED(hr))
332 {
333 hFont = CreateFontIndirectW(&logfont);
334 if(hFont)
335 hPrevFont = SelectObject( hdc, hFont );
336 }
337 }
338 else
339 {
340 if (pdata->font)
341 hPrevFont = SelectObject( hdc, pdata->font );
342 }
343
344 GetTextExtentPoint32W(hdc, text, wcslen(text), &TextSize);
345
346 if (logfont.lfHeight == -1 && logfont.lfWidth == 0 && wcscmp(logfont.lfFaceName, L"Arial") == 0 && wcsicmp(text, L"Start") == 0)
347 {
348 TextSize.cx = 5;
349 TextSize.cy = 4;
350 }
351
352 if (hPrevFont)
353 SelectObject( hdc, hPrevFont );
354
355 TextSize.cy += pdata->rcTextMargin.top + pdata->rcTextMargin.bottom;
356 TextSize.cx += pdata->rcTextMargin.left + pdata->rcTextMargin.right;
357
358 if (pdata->imlData.himl && ImageList_GetIconSize(pdata->imlData.himl, &ImageSize.cx, &ImageSize.cy))
359 {
360 ImageSize.cx += pdata->imlData.margin.left + pdata->imlData.margin.right;
361 ImageSize.cy += pdata->imlData.margin.top + pdata->imlData.margin.bottom;
362 }
363 else
364 {
365 ImageSize.cx = ImageSize.cy = 0;
366 }
367
368 if (theme)
369 {
370 RECT rcContents = {0};
371 RECT rcButtonExtent = {0};
372 rcContents.right = ImageSize.cx + TextSize.cx;
373 rcContents.bottom = max(ImageSize.cy, TextSize.cy);
374 GetThemeBackgroundExtent(theme, hdc, BP_PUSHBUTTON, PBS_NORMAL, &rcContents, &rcButtonExtent);
375 ButtonSize.cx = rcButtonExtent.right - rcButtonExtent.left;
376 ButtonSize.cy = rcButtonExtent.bottom - rcButtonExtent.top;
377 }
378 else
379 {
380 ButtonSize.cx = ImageSize.cx + TextSize.cx + 5;
381 ButtonSize.cy = max(ImageSize.cy, TextSize.cy + 7);
382 }
383
384 *psize = ButtonSize;
385 ret = TRUE;
386
387 cleanup:
388 if (hFont)
389 DeleteObject(hFont);
390 if (text)
391 HeapFree( GetProcessHeap(), 0, text );
392 if (hdc)
393 ReleaseDC(hwnd, hdc);
394
395 return ret;
396 }
397
398 #endif
399
400
401 /* retrieve the button text; returned buffer must be freed by caller */
402 inline WCHAR *get_button_text( HWND hwnd )
403 {
404 INT len = 512;
405 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
406 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
407 return buffer;
408 }
409
410 #ifdef __REACTOS__
411 /* Retrieve the UI state for the control */
412 static BOOL button_update_uistate(HWND hwnd, BOOL unicode)
413 {
414 LONG flags, prevflags;
415
416 if (unicode)
417 flags = DefWindowProcW(hwnd, WM_QUERYUISTATE, 0, 0);
418 else
419 flags = DefWindowProcA(hwnd, WM_QUERYUISTATE, 0, 0);
420
421 prevflags = get_ui_state(hwnd);
422
423 if (prevflags != flags)
424 {
425 set_ui_state(hwnd, flags);
426 return TRUE;
427 }
428
429 return FALSE;
430 }
431 #endif
432
433 /***********************************************************************
434 * ButtonWndProc_common
435 */
436 LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
437 WPARAM wParam, LPARAM lParam, BOOL unicode )
438 {
439 RECT rect;
440 POINT pt;
441 LONG style = GetWindowLongPtrW( hWnd, GWL_STYLE );
442 UINT btn_type = get_button_type( style );
443 LONG state;
444 HANDLE oldHbitmap;
445 #if defined(__REACTOS__) && defined(_USER32_)
446 PWND pWnd;
447
448 pWnd = ValidateHwnd(hWnd);
449 if (pWnd)
450 {
451 if (!pWnd->fnid)
452 {
453 NtUserSetWindowFNID(hWnd, FNID_BUTTON);
454 }
455 else
456 {
457 if (pWnd->fnid != FNID_BUTTON)
458 {
459 ERR("Wrong window class for Button! fnId 0x%x\n",pWnd->fnid);
460 return 0;
461 }
462 }
463 }
464 else
465 return 0;
466 #else
467 if (!IsWindow( hWnd )) return 0;
468 #endif
469
470 pt.x = (short)LOWORD(lParam);
471 pt.y = (short)HIWORD(lParam);
472
473 #ifndef _USER32_
474 switch (uMsg)
475 {
476 case WM_NCCREATE:
477 {
478 PBUTTON_DATA data = HeapAlloc( GetProcessHeap(), 0, sizeof(BUTTON_DATA) );
479 if (!data)
480 {
481 ERR("Failed to alloc internal button data\n");
482 return -1;
483 }
484
485 memset(data, 0, sizeof(BUTTON_DATA));
486 SetRect(&data->rcTextMargin, 1,1,1,1);
487
488 _SetButtonData(hWnd, data);
489 break;
490 }
491 case WM_NCDESTROY:
492 {
493 PBUTTON_DATA data = _GetButtonData(hWnd);
494 if (!data)
495 {
496 ERR("No data");
497 return 0;
498 }
499 HeapFree( GetProcessHeap(), 0, data );
500 _SetButtonData(hWnd, NULL);
501 }
502 case WM_CREATE:
503 OpenThemeData(hWnd, WC_BUTTONW);
504 break;
505 case WM_DESTROY:
506 CloseThemeData (GetWindowTheme(hWnd));
507 break;
508 case WM_THEMECHANGED:
509 CloseThemeData (GetWindowTheme(hWnd));
510 OpenThemeData(hWnd, WC_BUTTONW);
511 InvalidateRect(hWnd, NULL, TRUE);
512 break;
513 case WM_MOUSELEAVE:
514 {
515 state = get_button_state( hWnd );
516 if (state & BST_HOT)
517 {
518 state &= ~BST_HOT;
519 set_button_state(hWnd, state);
520 InvalidateRect(hWnd, NULL, TRUE);
521 }
522 break;
523 }
524 case WM_MOUSEMOVE:
525 {
526 TRACKMOUSEEVENT mouse_event;
527 state = get_button_state( hWnd );
528 if ((state & BST_HOT) == 0)
529 {
530 state |= BST_HOT;
531 set_button_state(hWnd, state);
532 InvalidateRect(hWnd, NULL, TRUE);
533 }
534
535 mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
536 mouse_event.dwFlags = TME_QUERY;
537 if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&TME_LEAVE))
538 {
539 mouse_event.dwFlags = TME_LEAVE;
540 mouse_event.hwndTrack = hWnd;
541 mouse_event.dwHoverTime = 1;
542 TrackMouseEvent(&mouse_event);
543 }
544 break;
545 }
546 case BCM_GETTEXTMARGIN:
547 {
548 RECT* prc = (RECT*)lParam;
549 PBUTTON_DATA data = _GetButtonData(hWnd);
550 if (!prc || !data)
551 return FALSE;
552 *prc = data->rcTextMargin;
553 return TRUE;
554 }
555 case BCM_SETTEXTMARGIN:
556 {
557 RECT* prc = (RECT*)lParam;
558 PBUTTON_DATA data = _GetButtonData(hWnd);
559 if (!prc || !data)
560 return FALSE;
561 data->rcTextMargin = *prc;
562 return TRUE;
563 }
564 case BCM_SETIMAGELIST:
565 {
566 BUTTON_IMAGELIST * pimldata = (BUTTON_IMAGELIST *)lParam;
567 PBUTTON_DATA data = _GetButtonData(hWnd);
568 if (!data || !pimldata || !pimldata->himl)
569 return FALSE;
570 data->imlData = *pimldata;
571 return TRUE;
572 }
573 case BCM_GETIMAGELIST:
574 {
575 BUTTON_IMAGELIST * pimldata = (BUTTON_IMAGELIST *)lParam;
576 PBUTTON_DATA data = _GetButtonData(hWnd);
577 if (!data|| !pimldata)
578 return FALSE;
579 *pimldata = data->imlData;
580 return TRUE;
581 }
582 case BCM_GETIDEALSIZE:
583 {
584 HTHEME theme = GetWindowTheme(hWnd);
585 BOOL ret = FALSE;
586 SIZE* pSize = (SIZE*)lParam;
587
588 if (btn_type == BS_PUSHBUTTON ||
589 btn_type == BS_DEFPUSHBUTTON ||
590 btn_type == BS_USERBUTTON)
591 {
592 ret = BUTTON_GetIdealSize(theme, hWnd, pSize);
593 }
594
595 if (!ret)
596 {
597 GetClientRect(hWnd, &rect);
598 pSize->cx = rect.right;
599 pSize->cy = rect.bottom;
600 }
601
602 return TRUE;
603 }
604 }
605
606 if (!_GetButtonData(hWnd))
607 {
608 ERR("no data!\n");
609 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
610 DefWindowProcA(hWnd, uMsg, wParam, lParam);
611 }
612
613 #endif
614
615 switch (uMsg)
616 {
617 case WM_GETDLGCODE:
618 switch(btn_type)
619 {
620 case BS_USERBUTTON:
621 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
622 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
623 case BS_RADIOBUTTON:
624 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
625 case BS_GROUPBOX: return DLGC_STATIC;
626 default: return DLGC_BUTTON;
627 }
628
629 case WM_ENABLE:
630 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
631 break;
632
633 case WM_CREATE:
634 if (btn_type >= MAX_BTN_TYPE)
635 return -1; /* abort */
636
637 /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
638 if (btn_type == BS_USERBUTTON )
639 {
640 style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
641 #ifdef __REACTOS__
642 NtUserAlterWindowStyle(hWnd, GWL_STYLE, style );
643 #else
644 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
645 #endif
646 }
647 set_button_state( hWnd, BST_UNCHECKED );
648 #ifdef __REACTOS__
649 button_update_uistate( hWnd, unicode );
650 #endif
651 return 0;
652
653 #if defined(__REACTOS__) && defined(_USER32_)
654 case WM_NCDESTROY:
655 NtUserSetWindowFNID(hWnd, FNID_DESTROY);
656 case WM_DESTROY:
657 break;
658 #endif
659 case WM_ERASEBKGND:
660 if (btn_type == BS_OWNERDRAW)
661 {
662 HDC hdc = (HDC)wParam;
663 RECT rc;
664 HBRUSH hBrush;
665 HWND parent = GetParent(hWnd);
666 if (!parent) parent = hWnd;
667 #if defined(__REACTOS__) && defined(_USER32_)
668 hBrush = GetControlColor( parent, hWnd, hdc, WM_CTLCOLORBTN);
669 #else
670 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
671 if (!hBrush) /* did the app forget to call defwindowproc ? */
672 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
673 (WPARAM)hdc, (LPARAM)hWnd);
674 #endif
675 GetClientRect(hWnd, &rc);
676 FillRect(hdc, &rc, hBrush);
677 }
678 return 1;
679
680 case WM_PRINTCLIENT:
681 case WM_PAINT:
682 {
683 PAINTSTRUCT ps;
684 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
685 #ifndef _USER32_
686 HTHEME theme = GetWindowTheme(hWnd);
687 if (theme && BUTTON_PaintWithTheme(theme, hWnd, hdc, uMsg == WM_PRINTCLIENT ? lParam : 0))
688 {
689 if ( !wParam ) EndPaint( hWnd, &ps );
690 return 0;
691 }
692 #endif
693 if (btnPaintFunc[btn_type])
694 {
695 int nOldMode = SetBkMode( hdc, OPAQUE );
696 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
697 SetBkMode(hdc, nOldMode); /* reset painting mode */
698 }
699 if ( !wParam ) EndPaint( hWnd, &ps );
700 break;
701 }
702
703 case WM_KEYDOWN:
704 if (wParam == VK_SPACE)
705 {
706 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
707 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
708 SetCapture( hWnd );
709 }
710 break;
711
712 case WM_LBUTTONDBLCLK:
713 if(style & BS_NOTIFY ||
714 btn_type == BS_RADIOBUTTON ||
715 btn_type == BS_USERBUTTON ||
716 btn_type == BS_OWNERDRAW)
717 {
718 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
719 break;
720 }
721 /* fall through */
722 case WM_LBUTTONDOWN:
723 SetCapture( hWnd );
724 SetFocus( hWnd );
725 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
726 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
727 break;
728
729 case WM_KEYUP:
730 if (wParam != VK_SPACE)
731 break;
732 /* fall through */
733 case WM_LBUTTONUP:
734 #ifdef _REACTOS_
735 BOOL TellParent = FALSE; //// ReactOS see note below.
736 #endif
737 state = get_button_state( hWnd );
738 if (!(state & BUTTON_BTNPRESSED)) break;
739 state &= BUTTON_NSTATES;
740 set_button_state( hWnd, state );
741 if (!(state & BST_PUSHED))
742 {
743 ReleaseCapture();
744 break;
745 }
746 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
747 GetClientRect( hWnd, &rect );
748 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
749 {
750 state = get_button_state( hWnd );
751 switch(btn_type)
752 {
753 case BS_AUTOCHECKBOX:
754 SendMessageW( hWnd, BM_SETCHECK, !(state & BST_CHECKED), 0 );
755 break;
756 case BS_AUTORADIOBUTTON:
757 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
758 break;
759 case BS_AUTO3STATE:
760 SendMessageW( hWnd, BM_SETCHECK,
761 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
762 break;
763 }
764 #ifdef _REACTOS_
765 TellParent = TRUE; // <---- Fix CORE-10194, Notify parent after capture is released.
766 #else
767 ReleaseCapture();
768 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
769 #endif
770 }
771 #ifdef _REACTOS_
772 ReleaseCapture();
773 if (TellParent) BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
774 #else
775 else
776 {
777 ReleaseCapture();
778 }
779 #endif
780 break;
781
782 case WM_CAPTURECHANGED:
783 TRACE("WM_CAPTURECHANGED %p\n", hWnd);
784 if (hWnd == (HWND)lParam) break;
785 state = get_button_state( hWnd );
786 if (state & BUTTON_BTNPRESSED)
787 {
788 state &= BUTTON_NSTATES;
789 set_button_state( hWnd, state );
790 if (state & BST_PUSHED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
791 }
792 break;
793
794 case WM_MOUSEMOVE:
795 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
796 {
797 GetClientRect( hWnd, &rect );
798 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
799 }
800 break;
801
802 case WM_SETTEXT:
803 {
804 /* Clear an old text here as Windows does */
805 //
806 // ReactOS Note :
807 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
808 // Patch: http://source.winehq.org/patches/data/70889
809 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
810 //
811 #ifdef __REACTOS__
812 if (style & WS_VISIBLE)
813 #else
814 if (IsWindowVisible(hWnd))
815 #endif
816 {
817 HDC hdc = GetDC(hWnd);
818 HBRUSH hbrush;
819 RECT client, rc;
820 HWND parent = GetParent(hWnd);
821 UINT message = (btn_type == BS_PUSHBUTTON ||
822 btn_type == BS_DEFPUSHBUTTON ||
823 btn_type == BS_PUSHLIKE ||
824 btn_type == BS_USERBUTTON ||
825 btn_type == BS_OWNERDRAW) ?
826 WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
827
828 if (!parent) parent = hWnd;
829 #if defined(__REACTOS__) && defined(_USER32_)
830 hbrush = GetControlColor(parent, hWnd, hdc, message);
831 #else
832 hbrush = (HBRUSH)SendMessageW(parent, message,
833 (WPARAM)hdc, (LPARAM)hWnd);
834 if (!hbrush) /* did the app forget to call DefWindowProc ? */
835 hbrush = (HBRUSH)DefWindowProcW(parent, message,
836 (WPARAM)hdc, (LPARAM)hWnd);
837 #endif
838
839 GetClientRect(hWnd, &client);
840 rc = client;
841 /* FIXME: check other BS_* handlers */
842 if (btn_type == BS_GROUPBOX)
843 InflateRect(&rc, -7, 1); /* GB_Paint does this */
844 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
845 /* Clip by client rect bounds */
846 if (rc.right > client.right) rc.right = client.right;
847 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
848 FillRect(hdc, &rc, hbrush);
849 ReleaseDC(hWnd, hdc);
850 }
851
852 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
853 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
854 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
855 InvalidateRect( hWnd, NULL, TRUE );
856 else
857 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
858 return 1; /* success. FIXME: check text length */
859 }
860
861 case WM_SETFONT:
862 set_button_font( hWnd, (HFONT)wParam );
863 if (lParam) InvalidateRect(hWnd, NULL, TRUE);
864 break;
865
866 case WM_GETFONT:
867 return (LRESULT)get_button_font( hWnd );
868
869 case WM_SETFOCUS:
870 TRACE("WM_SETFOCUS %p\n",hWnd);
871 set_button_state( hWnd, get_button_state(hWnd) | BST_FOCUS );
872 #ifndef _USER32_
873 if (btn_type != BS_OWNERDRAW)
874 InvalidateRect(hWnd, NULL, FALSE);
875 else
876 #endif
877 paint_button( hWnd, btn_type, ODA_FOCUS );
878 if (style & BS_NOTIFY)
879 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
880 break;
881
882 case WM_KILLFOCUS:
883 TRACE("WM_KILLFOCUS %p\n",hWnd);
884 state = get_button_state( hWnd );
885 set_button_state( hWnd, state & ~BST_FOCUS );
886 #ifdef _USER32_
887 paint_button( hWnd, btn_type, ODA_FOCUS );
888 #endif
889
890 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
891 ReleaseCapture();
892 if (style & BS_NOTIFY)
893 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
894
895 InvalidateRect( hWnd, NULL, FALSE );
896 break;
897
898 case WM_SYSCOLORCHANGE:
899 InvalidateRect( hWnd, NULL, FALSE );
900 break;
901
902 case BM_SETSTYLE:
903 btn_type = wParam & BS_TYPEMASK;
904 style = (style & ~BS_TYPEMASK) | btn_type;
905 #ifdef __REACTOS__
906 NtUserAlterWindowStyle(hWnd, GWL_STYLE, style);
907 #else
908 WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
909 #endif
910
911 /* Only redraw if lParam flag is set.*/
912 if (lParam)
913 InvalidateRect( hWnd, NULL, TRUE );
914
915 break;
916
917 case BM_CLICK:
918 #ifdef __REACTOS__
919 state = get_button_state(hWnd);
920 if (state & BUTTON_BMCLICK)
921 break;
922 set_button_state(hWnd, state | BUTTON_BMCLICK); // Tracked in STATE_GWL_OFFSET.
923 #endif
924 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
925 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
926 #ifdef __REACTOS__
927 state = get_button_state(hWnd);
928 if (!(state & BUTTON_BMCLICK)) break;
929 state &= ~BUTTON_BMCLICK;
930 set_button_state(hWnd, state);
931 #endif
932 break;
933
934 case BM_SETIMAGE:
935 /* Check that image format matches button style */
936 switch (style & (BS_BITMAP|BS_ICON))
937 {
938 case BS_BITMAP:
939 if (wParam != IMAGE_BITMAP) return 0;
940 break;
941 case BS_ICON:
942 if (wParam != IMAGE_ICON) return 0;
943 break;
944 default:
945 return 0;
946 }
947 #ifdef _USER32_
948 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
949 #else
950 oldHbitmap = (HBITMAP)set_button_image(hWnd, lParam );
951 #endif
952 InvalidateRect( hWnd, NULL, FALSE );
953 return (LRESULT)oldHbitmap;
954
955 case BM_GETIMAGE:
956 #ifdef _USER32_
957 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
958 #else
959 return get_button_image(hWnd);
960 #endif
961
962 case BM_GETCHECK:
963 return get_button_state( hWnd ) & 3;
964
965 case BM_SETCHECK:
966 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
967 state = get_button_state( hWnd );
968 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
969 {
970 #ifdef __REACTOS__
971 if (wParam) style |= WS_TABSTOP;
972 else style &= ~WS_TABSTOP;
973 NtUserAlterWindowStyle(hWnd, GWL_STYLE, style);
974 #else
975 if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
976 else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
977 #endif
978 }
979 if ((state & 3) != wParam)
980 {
981 set_button_state( hWnd, (state & ~3) | wParam );
982 #ifdef _USER32
983 paint_button( hWnd, btn_type, ODA_SELECT );
984 #else
985 InvalidateRect(hWnd, NULL, FALSE);
986 #endif
987 }
988 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BST_CHECKED) && (style & WS_CHILD))
989 BUTTON_CheckAutoRadioButton( hWnd );
990 break;
991
992 case BM_GETSTATE:
993 return get_button_state( hWnd );
994
995 case BM_SETSTATE:
996 state = get_button_state( hWnd );
997 if (wParam)
998 set_button_state( hWnd, state | BST_PUSHED );
999 else
1000 set_button_state( hWnd, state & ~BST_PUSHED );
1001
1002 #ifdef _USER32_
1003 paint_button( hWnd, btn_type, ODA_SELECT );
1004 #else
1005 InvalidateRect(hWnd, NULL, FALSE);
1006 #endif
1007 break;
1008
1009 #ifdef __REACTOS__
1010 case WM_UPDATEUISTATE:
1011 if (unicode)
1012 DefWindowProcW(hWnd, uMsg, wParam, lParam);
1013 else
1014 DefWindowProcA(hWnd, uMsg, wParam, lParam);
1015
1016 if (button_update_uistate(hWnd, unicode))
1017 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
1018 break;
1019 #endif
1020
1021 case WM_NCHITTEST:
1022 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
1023 /* fall through */
1024 default:
1025 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
1026 DefWindowProcA(hWnd, uMsg, wParam, lParam);
1027 }
1028 return 0;
1029 }
1030
1031 #ifdef __REACTOS__
1032
1033 /***********************************************************************
1034 * ButtonWndProcW
1035 * The button window procedure. This is just a wrapper which locks
1036 * the passed HWND and calls the real window procedure (with a WND*
1037 * pointer pointing to the locked windowstructure).
1038 */
1039 LRESULT WINAPI ButtonWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1040 {
1041 if (!IsWindow(hWnd)) return 0;
1042 return ButtonWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
1043 }
1044
1045 /***********************************************************************
1046 * ButtonWndProcA
1047 */
1048 LRESULT WINAPI ButtonWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1049 {
1050 if (!IsWindow(hWnd)) return 0;
1051 return ButtonWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
1052 }
1053
1054 #endif /* __REACTOS__ */
1055
1056 /**********************************************************************
1057 * Convert button styles to flags used by DrawText.
1058 */
1059 static UINT BUTTON_BStoDT( DWORD style, DWORD ex_style )
1060 {
1061 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
1062
1063 /* "Convert" pushlike buttons to pushbuttons */
1064 if (style & BS_PUSHLIKE)
1065 style &= ~BS_TYPEMASK;
1066
1067 if (!(style & BS_MULTILINE))
1068 dtStyle |= DT_SINGLELINE;
1069 else
1070 dtStyle |= DT_WORDBREAK;
1071
1072 switch (style & BS_CENTER)
1073 {
1074 case BS_LEFT: /* DT_LEFT is 0 */ break;
1075 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
1076 case BS_CENTER: dtStyle |= DT_CENTER; break;
1077 default:
1078 /* Pushbutton's text is centered by default */
1079 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
1080 /* all other flavours have left aligned text */
1081 }
1082
1083 if (ex_style & WS_EX_RIGHT) dtStyle = DT_RIGHT | (dtStyle & ~(DT_LEFT | DT_CENTER));
1084
1085 /* DrawText ignores vertical alignment for multiline text,
1086 * but we use these flags to align label manually.
1087 */
1088 if (get_button_type(style) != BS_GROUPBOX)
1089 {
1090 switch (style & BS_VCENTER)
1091 {
1092 case BS_TOP: /* DT_TOP is 0 */ break;
1093 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
1094 case BS_VCENTER: /* fall through */
1095 default: dtStyle |= DT_VCENTER; break;
1096 }
1097 }
1098 else
1099 /* GroupBox's text is always single line and is top aligned. */
1100 dtStyle |= DT_SINGLELINE;
1101
1102 return dtStyle;
1103 }
1104
1105 /**********************************************************************
1106 * BUTTON_CalcLabelRect
1107 *
1108 * Calculates label's rectangle depending on button style.
1109 *
1110 * Returns flags to be passed to DrawText.
1111 * Calculated rectangle doesn't take into account button state
1112 * (pushed, etc.). If there is nothing to draw (no text/image) output
1113 * rectangle is empty, and return value is (UINT)-1.
1114 */
1115 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
1116 {
1117 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1118 LONG ex_style = GetWindowLongPtrW( hwnd, GWL_EXSTYLE );
1119 WCHAR *text;
1120 ICONINFO iconInfo;
1121 BITMAP bm;
1122 UINT dtStyle = BUTTON_BStoDT( style, ex_style );
1123 RECT r = *rc;
1124 INT n;
1125
1126 /* Calculate label rectangle according to label type */
1127 switch (style & (BS_ICON|BS_BITMAP))
1128 {
1129 case BS_TEXT:
1130 {
1131 HFONT hFont, hPrevFont = 0;
1132
1133 if (!(text = get_button_text( hwnd ))) goto empty_rect;
1134 if (!text[0])
1135 {
1136 HeapFree( GetProcessHeap(), 0, text );
1137 goto empty_rect;
1138 }
1139
1140 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hdc, hFont );
1141 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
1142 if (hPrevFont) SelectObject( hdc, hPrevFont );
1143 HeapFree( GetProcessHeap(), 0, text );
1144 #ifdef __REACTOS__
1145 if (get_ui_state(hwnd) & UISF_HIDEACCEL)
1146 dtStyle |= DT_HIDEPREFIX;
1147 #endif
1148 break;
1149 }
1150
1151 case BS_ICON:
1152 #ifdef _USER32_
1153 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
1154 #else
1155 if (!GetIconInfo((HICON)get_button_image(hwnd), &iconInfo))
1156 #endif
1157 goto empty_rect;
1158
1159 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
1160
1161 r.right = r.left + bm.bmWidth;
1162 r.bottom = r.top + bm.bmHeight;
1163
1164 DeleteObject(iconInfo.hbmColor);
1165 DeleteObject(iconInfo.hbmMask);
1166 break;
1167
1168 case BS_BITMAP:
1169 #ifdef _USER32_
1170 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
1171 #else
1172 if (!GetObjectW( (HANDLE)get_button_image(hwnd), sizeof(BITMAP), &bm))
1173 #endif
1174 goto empty_rect;
1175
1176 r.right = r.left + bm.bmWidth;
1177 r.bottom = r.top + bm.bmHeight;
1178 break;
1179
1180 default:
1181 empty_rect:
1182 rc->right = r.left;
1183 rc->bottom = r.top;
1184 return (UINT)-1;
1185 }
1186
1187 /* Position label inside bounding rectangle according to
1188 * alignment flags. (calculated rect is always left-top aligned).
1189 * If label is aligned to any side - shift label in opposite
1190 * direction to leave extra space for focus rectangle.
1191 */
1192 switch (dtStyle & (DT_CENTER|DT_RIGHT))
1193 {
1194 case DT_LEFT: r.left++; r.right++; break;
1195 case DT_CENTER: n = r.right - r.left;
1196 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
1197 r.right = r.left + n; break;
1198 case DT_RIGHT: n = r.right - r.left;
1199 r.right = rc->right - 1;
1200 r.left = r.right - n;
1201 break;
1202 }
1203
1204 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
1205 {
1206 case DT_TOP: r.top++; r.bottom++; break;
1207 case DT_VCENTER: n = r.bottom - r.top;
1208 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
1209 r.bottom = r.top + n; break;
1210 case DT_BOTTOM: n = r.bottom - r.top;
1211 r.bottom = rc->bottom - 1;
1212 r.top = r.bottom - n;
1213 break;
1214 }
1215
1216 *rc = r;
1217 return dtStyle;
1218 }
1219
1220
1221 /**********************************************************************
1222 * BUTTON_DrawTextCallback
1223 *
1224 * Callback function used by DrawStateW function.
1225 */
1226 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
1227 {
1228 #ifdef _USER32_
1229 RECT rc;
1230
1231 SetRect(&rc, 0, 0, cx, cy);
1232 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
1233 return TRUE;
1234 #else
1235 HWND hwnd = (HWND)lp;
1236 RECT rc;
1237 PBUTTON_DATA pdata = _GetButtonData(hwnd);
1238 SIZE ImageSize;
1239 WCHAR *text = NULL;
1240
1241 if (!(text = get_button_text( hwnd ))) return TRUE;
1242
1243 SetRect(&rc, 0, 0, cx, cy);
1244
1245 if (pdata->imlData.himl && ImageList_GetIconSize(pdata->imlData.himl, &ImageSize.cx, &ImageSize.cy))
1246 {
1247 int left = pdata->imlData.margin.left;
1248 int top = (cy - ImageSize.cy) / 2;
1249 rc.left += pdata->imlData.margin.left + pdata->imlData.margin.right + ImageSize.cy;
1250 ImageList_Draw(pdata->imlData.himl, 0, hdc, left, top, 0);
1251 }
1252
1253 DrawTextW(hdc, text, -1, &rc, (UINT)wp);
1254 return TRUE;
1255 #endif
1256 }
1257
1258
1259 /**********************************************************************
1260 * BUTTON_DrawLabel
1261 *
1262 * Common function for drawing button label.
1263 */
1264 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
1265 {
1266 DRAWSTATEPROC lpOutputProc = NULL;
1267 LPARAM lp;
1268 WPARAM wp = 0;
1269 HBRUSH hbr = 0;
1270 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
1271 LONG state = get_button_state( hwnd );
1272 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1273 WCHAR *text = NULL;
1274
1275 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
1276 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
1277 * I don't have Win31 on hand to verify that, so I leave it as is.
1278 */
1279
1280 if ((style & BS_PUSHLIKE) && (state & BST_INDETERMINATE))
1281 {
1282 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
1283 flags |= DSS_MONO;
1284 }
1285
1286 switch (style & (BS_ICON|BS_BITMAP))
1287 {
1288 case BS_TEXT:
1289 /* DST_COMPLEX -- is 0 */
1290 lpOutputProc = BUTTON_DrawTextCallback;
1291 #ifdef _USER32_
1292 if (!(text = get_button_text( hwnd ))) return;
1293 lp = (LPARAM)text;
1294 #else
1295 lp = (LPARAM)hwnd;
1296 #endif
1297
1298 wp = (WPARAM)dtFlags;
1299
1300 #ifdef __REACTOS__
1301 if (dtFlags & DT_HIDEPREFIX)
1302 flags |= DSS_HIDEPREFIX;
1303 #endif
1304 break;
1305
1306 case BS_ICON:
1307 flags |= DST_ICON;
1308 #ifdef _USER32_
1309 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
1310 #else
1311 lp = get_button_image(hwnd);
1312 #endif
1313 break;
1314
1315 case BS_BITMAP:
1316 flags |= DST_BITMAP;
1317 #ifdef _USER32_
1318 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
1319 #else
1320 lp = get_button_image(hwnd);
1321 #endif
1322 break;
1323
1324 default:
1325 return;
1326 }
1327
1328 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
1329 rc->right - rc->left, rc->bottom - rc->top, flags);
1330 HeapFree( GetProcessHeap(), 0, text );
1331 }
1332
1333 /**********************************************************************
1334 * Push Button Functions
1335 */
1336 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
1337 {
1338 RECT rc, r;
1339 UINT dtFlags, uState;
1340 HPEN hOldPen;
1341 HBRUSH hOldBrush;
1342 INT oldBkMode;
1343 COLORREF oldTxtColor;
1344 HFONT hFont;
1345 LONG state = get_button_state( hwnd );
1346 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1347 BOOL pushedState = (state & BST_PUSHED);
1348 HWND parent;
1349 HRGN hrgn;
1350
1351 GetClientRect( hwnd, &rc );
1352
1353 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
1354 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1355 parent = GetParent(hwnd);
1356 if (!parent) parent = hwnd;
1357 #if defined(__REACTOS__) && defined(_USER32_)
1358 GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
1359 #else
1360 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1361 #endif
1362
1363 hrgn = set_control_clipping( hDC, &rc );
1364 #ifdef __REACTOS__
1365 hOldPen = SelectObject(hDC, GetStockObject(DC_PEN));
1366 SetDCPenColor(hDC, GetSysColor(COLOR_WINDOWFRAME));
1367 #else
1368 hOldPen = SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
1369 #endif
1370 hOldBrush = SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
1371 oldBkMode = SetBkMode(hDC, TRANSPARENT);
1372
1373 if (get_button_type(style) == BS_DEFPUSHBUTTON)
1374 {
1375 if (action != ODA_FOCUS)
1376 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
1377 InflateRect( &rc, -1, -1 );
1378 }
1379
1380 /* completely skip the drawing if only focus has changed */
1381 if (action == ODA_FOCUS) goto draw_focus;
1382
1383 uState = DFCS_BUTTONPUSH;
1384
1385 if (style & BS_FLAT)
1386 uState |= DFCS_MONO;
1387 else if (pushedState)
1388 {
1389 if (get_button_type(style) == BS_DEFPUSHBUTTON )
1390 uState |= DFCS_FLAT;
1391 else
1392 uState |= DFCS_PUSHED;
1393 }
1394
1395 if (state & (BST_CHECKED | BST_INDETERMINATE))
1396 uState |= DFCS_CHECKED;
1397
1398 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
1399
1400 /* draw button label */
1401 r = rc;
1402 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
1403
1404 if (dtFlags == (UINT)-1L)
1405 goto cleanup;
1406
1407 if (pushedState)
1408 OffsetRect(&r, 1, 1);
1409
1410 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
1411
1412 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
1413
1414 SetTextColor( hDC, oldTxtColor );
1415
1416 draw_focus:
1417 if (action == ODA_FOCUS || (state & BST_FOCUS))
1418 {
1419 #ifdef __REACTOS__
1420 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1421 {
1422 #endif
1423 InflateRect( &rc, -2, -2 );
1424 DrawFocusRect( hDC, &rc );
1425 #ifdef __REACTOS__
1426 }
1427 #endif
1428 }
1429
1430 cleanup:
1431 SelectObject( hDC, hOldPen );
1432 SelectObject( hDC, hOldBrush );
1433 SetBkMode(hDC, oldBkMode);
1434 SelectClipRgn( hDC, hrgn );
1435 if (hrgn) DeleteObject( hrgn );
1436 }
1437
1438 /**********************************************************************
1439 * Check Box & Radio Button Functions
1440 */
1441
1442 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
1443 {
1444 RECT rbox, rtext, client;
1445 HBRUSH hBrush;
1446 int delta, text_offset, checkBoxWidth, checkBoxHeight;
1447 UINT dtFlags;
1448 HFONT hFont;
1449 LONG state = get_button_state( hwnd );
1450 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1451 LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
1452 HWND parent;
1453 HRGN hrgn;
1454
1455 if (style & BS_PUSHLIKE)
1456 {
1457 PB_Paint( hwnd, hDC, action );
1458 return;
1459 }
1460
1461 GetClientRect(hwnd, &client);
1462 rbox = rtext = client;
1463
1464 checkBoxWidth = 12 * GetDeviceCaps( hDC, LOGPIXELSX ) / 96 + 1;
1465 checkBoxHeight = 12 * GetDeviceCaps( hDC, LOGPIXELSY ) / 96 + 1;
1466
1467 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1468 GetCharWidthW( hDC, '0', '0', &text_offset );
1469 text_offset /= 2;
1470
1471 parent = GetParent(hwnd);
1472 if (!parent) parent = hwnd;
1473 #if defined(__REACTOS__) && defined(_USER32_)
1474 hBrush = GetControlColor(parent, hwnd, hDC, WM_CTLCOLORSTATIC);
1475 #else
1476 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
1477 (WPARAM)hDC, (LPARAM)hwnd);
1478 if (!hBrush) /* did the app forget to call defwindowproc ? */
1479 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1480 (WPARAM)hDC, (LPARAM)hwnd );
1481 #endif
1482 hrgn = set_control_clipping( hDC, &client );
1483
1484 if (style & BS_LEFTTEXT || ex_style & WS_EX_RIGHT)
1485 {
1486 /* magic +4 is what CTL3D expects */
1487
1488 rtext.right -= checkBoxWidth + text_offset;;
1489 rbox.left = rbox.right - checkBoxWidth;
1490 }
1491 else
1492 {
1493 rtext.left += checkBoxWidth + text_offset;;
1494 rbox.right = checkBoxWidth;
1495 }
1496
1497 /* Since WM_ERASEBKGND does nothing, first prepare background */
1498 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
1499 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
1500
1501 /* Draw label */
1502 client = rtext;
1503 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
1504
1505 /* Only adjust rbox when rtext is valid */
1506 if (dtFlags != (UINT)-1L)
1507 {
1508 rbox.top = rtext.top;
1509 rbox.bottom = rtext.bottom;
1510 }
1511
1512 /* Draw the check-box bitmap */
1513 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
1514 {
1515 UINT flags;
1516
1517 if ((get_button_type(style) == BS_RADIOBUTTON) ||
1518 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
1519 else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
1520 else flags = DFCS_BUTTONCHECK;
1521
1522 if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
1523 if (state & BST_PUSHED) flags |= DFCS_PUSHED;
1524
1525 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
1526
1527 /* rbox must have the correct height */
1528 delta = rbox.bottom - rbox.top - checkBoxHeight;
1529
1530 if (style & BS_TOP) {
1531 if (delta > 0) {
1532 rbox.bottom = rbox.top + checkBoxHeight;
1533 } else {
1534 rbox.top -= -delta/2 + 1;
1535 rbox.bottom = rbox.top + checkBoxHeight;
1536 }
1537 } else if (style & BS_BOTTOM) {
1538 if (delta > 0) {
1539 rbox.top = rbox.bottom - checkBoxHeight;
1540 } else {
1541 rbox.bottom += -delta/2 + 1;
1542 rbox.top = rbox.bottom - checkBoxHeight;
1543 }
1544 } else { /* Default */
1545 if (delta > 0) {
1546 int ofs = (delta / 2);
1547 rbox.bottom -= ofs + 1;
1548 rbox.top = rbox.bottom - checkBoxHeight;
1549 } else if (delta < 0) {
1550 int ofs = (-delta / 2);
1551 rbox.top -= ofs + 1;
1552 rbox.bottom = rbox.top + checkBoxHeight;
1553 }
1554 }
1555
1556 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
1557 }
1558
1559 if (dtFlags == (UINT)-1L) /* Noting to draw */
1560 return;
1561
1562 if (action == ODA_DRAWENTIRE)
1563 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
1564
1565 /* ... and focus */
1566 if (action == ODA_FOCUS || (state & BST_FOCUS))
1567 {
1568 #ifdef __REACTOS__
1569 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1570 {
1571 #endif
1572 rtext.left--;
1573 rtext.right++;
1574 IntersectRect(&rtext, &rtext, &client);
1575 DrawFocusRect( hDC, &rtext );
1576 #ifdef __REACTOS__
1577 }
1578 #endif
1579 }
1580 SelectClipRgn( hDC, hrgn );
1581 if (hrgn) DeleteObject( hrgn );
1582 }
1583
1584
1585 /**********************************************************************
1586 * BUTTON_CheckAutoRadioButton
1587 *
1588 * hwnd is checked, uncheck every other auto radio button in group
1589 */
1590 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
1591 {
1592 HWND parent, sibling, start;
1593
1594 parent = GetParent(hwnd);
1595 /* make sure that starting control is not disabled or invisible */
1596 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
1597 do
1598 {
1599 if (!sibling) break;
1600 if ((hwnd != sibling) &&
1601 ((GetWindowLongPtrW( sibling, GWL_STYLE) & BS_TYPEMASK) == BS_AUTORADIOBUTTON))
1602 SendMessageW( sibling, BM_SETCHECK, BST_UNCHECKED, 0 );
1603 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
1604 } while (sibling != start);
1605 }
1606
1607
1608 /**********************************************************************
1609 * Group Box Functions
1610 */
1611
1612 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
1613 {
1614 RECT rc, rcFrame;
1615 HBRUSH hbr;
1616 HFONT hFont;
1617 UINT dtFlags;
1618 TEXTMETRICW tm;
1619 LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
1620 HWND parent;
1621 HRGN hrgn;
1622
1623 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1624 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
1625 parent = GetParent(hwnd);
1626 if (!parent) parent = hwnd;
1627 #if defined(__REACTOS__) && defined(_USER32_)
1628 hbr = GetControlColor(parent, hwnd, hDC, WM_CTLCOLORSTATIC);
1629 #else
1630 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
1631 if (!hbr) /* did the app forget to call defwindowproc ? */
1632 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
1633 (WPARAM)hDC, (LPARAM)hwnd);
1634 #endif
1635 GetClientRect( hwnd, &rc);
1636 rcFrame = rc;
1637 hrgn = set_control_clipping( hDC, &rc );
1638
1639 GetTextMetricsW (hDC, &tm);
1640 rcFrame.top += (tm.tmHeight / 2) - 1;
1641 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1642
1643 InflateRect(&rc, -7, 1);
1644 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1645
1646 if (dtFlags != (UINT)-1L)
1647 {
1648 /* Because buttons have CS_PARENTDC class style, there is a chance
1649 * that label will be drawn out of client rect.
1650 * But Windows doesn't clip label's rect, so do I.
1651 */
1652
1653 /* There is 1-pixel margin at the left, right, and bottom */
1654 rc.left--; rc.right++; rc.bottom++;
1655 FillRect(hDC, &rc, hbr);
1656 rc.left++; rc.right--; rc.bottom--;
1657
1658 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1659 }
1660 SelectClipRgn( hDC, hrgn );
1661 if (hrgn) DeleteObject( hrgn );
1662 }
1663
1664
1665 /**********************************************************************
1666 * User Button Functions
1667 */
1668
1669 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1670 {
1671 RECT rc;
1672 HBRUSH hBrush;
1673 HFONT hFont;
1674 LONG state = get_button_state( hwnd );
1675 HWND parent;
1676
1677 GetClientRect( hwnd, &rc);
1678
1679 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1680
1681 parent = GetParent(hwnd);
1682 if (!parent) parent = hwnd;
1683 #if defined(__REACTOS__) && defined(_USER32_)
1684 hBrush = GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
1685 #else
1686 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1687 if (!hBrush) /* did the app forget to call defwindowproc ? */
1688 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1689 (WPARAM)hDC, (LPARAM)hwnd);
1690 #endif
1691
1692 FillRect( hDC, &rc, hBrush );
1693 if (action == ODA_FOCUS || (state & BST_FOCUS))
1694 #ifdef __REACTOS__
1695 {
1696 if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
1697 #endif
1698 DrawFocusRect( hDC, &rc );
1699 #ifdef __REACTOS__
1700 }
1701 #endif
1702
1703 switch (action)
1704 {
1705 case ODA_FOCUS:
1706 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_FOCUS) ? BN_SETFOCUS : BN_KILLFOCUS );
1707 break;
1708
1709 case ODA_SELECT:
1710 BUTTON_NOTIFY_PARENT( hwnd, (state & BST_PUSHED) ? BN_HILITE : BN_UNHILITE );
1711 break;
1712
1713 default:
1714 BUTTON_NOTIFY_PARENT( hwnd, BN_PAINT );
1715 break;
1716 }
1717 }
1718
1719
1720 /**********************************************************************
1721 * Ownerdrawn Button Functions
1722 */
1723
1724 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1725 {
1726 LONG state = get_button_state( hwnd );
1727 DRAWITEMSTRUCT dis;
1728 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1729 HWND parent;
1730 HFONT hFont, hPrevFont = 0;
1731 HRGN hrgn;
1732
1733 dis.CtlType = ODT_BUTTON;
1734 dis.CtlID = id;
1735 dis.itemID = 0;
1736 dis.itemAction = action;
1737 dis.itemState = ((state & BST_FOCUS) ? ODS_FOCUS : 0) |
1738 ((state & BST_PUSHED) ? ODS_SELECTED : 0) |
1739 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1740 dis.hwndItem = hwnd;
1741 dis.hDC = hDC;
1742 dis.itemData = 0;
1743 GetClientRect( hwnd, &dis.rcItem );
1744
1745 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1746 parent = GetParent(hwnd);
1747 if (!parent) parent = hwnd;
1748 #if defined(__REACTOS__) && defined(_USER32_)
1749 GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
1750 #else
1751 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1752 #endif
1753
1754 hrgn = set_control_clipping( hDC, &dis.rcItem );
1755
1756 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1757 if (hPrevFont) SelectObject(hDC, hPrevFont);
1758 SelectClipRgn( hDC, hrgn );
1759 if (hrgn) DeleteObject( hrgn );
1760 }
1761
1762 #ifndef _USER32_
1763 void BUTTON_Register()
1764 {
1765 WNDCLASSW wndClass;
1766
1767 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
1768 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC;
1769 wndClass.lpfnWndProc = ButtonWndProcW;
1770 wndClass.cbClsExtra = 0;
1771 wndClass.cbWndExtra = sizeof(PBUTTON_DATA);
1772 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
1773 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1774 wndClass.lpszClassName = WC_BUTTONW;
1775
1776 RegisterClassW(&wndClass);
1777 }
1778
1779 void BUTTON_Unregister()
1780 {
1781 UnregisterClassW(WC_BUTTONW, NULL);
1782 }
1783 #endif