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