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