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