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