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