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