[COMCTL32] - A button with the style BS_DEFPUSHBUTTON is drawn as defaulted instead...
[reactos.git] / reactos / dll / win32 / comctl32 / theme_button.c
index ffbee17..4fd38d6 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include "comctl32.h"
+WINE_DEFAULT_DEBUG_CHANNEL(theme_button);
 
 #define BUTTON_TYPE 0x0f /* bit mask for the available button types */
 
@@ -33,7 +34,22 @@ typedef enum
        STATE_DEFAULTED
 } ButtonState;
 
-typedef void (*pfThemedPaint)(HTHEME theme, HWND hwnd, HDC hdc, ButtonState drawState, UINT dtFlags);
+typedef void (*pfThemedPaint)(HTHEME theme, HWND hwnd, HDC hdc, ButtonState drawState, UINT dtFlags, BOOL focused, LPARAM prfFlag);
+
+static inline LONG get_button_state( HWND hwnd )
+{
+    return _GetButtonData(hwnd)->state;
+}
+
+static inline HFONT get_button_font( HWND hwnd )
+{
+    return (HFONT)_GetButtonData(hwnd)->font;
+}
+
+static inline LONG_PTR get_button_image(HWND hwnd)
+{
+    return _GetButtonData(hwnd)->image;
+}
 
 static UINT get_drawtext_flags(DWORD style, DWORD ex_style)
 {
@@ -86,32 +102,62 @@ static inline WCHAR *get_button_text(HWND hwnd)
     return text;
 }
 
-static void PB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
+static void PB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused, LPARAM prfFlag)
 {
     static const int states[] = { PBS_NORMAL, PBS_DISABLED, PBS_HOT, PBS_PRESSED, PBS_DEFAULTED };
 
     RECT bgRect, textRect;
-    HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
+    HFONT font = get_button_font(hwnd);
     HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
     int state = states[ drawState ];
     WCHAR *text = get_button_text(hwnd);
+    PBUTTON_DATA pdata = _GetButtonData(hwnd);
+    SIZE ImageSize;
 
     GetClientRect(hwnd, &bgRect);
     GetThemeBackgroundContentRect(theme, hDC, BP_PUSHBUTTON, state, &bgRect, &textRect);
 
-    if (IsThemeBackgroundPartiallyTransparent(theme, BP_PUSHBUTTON, state))
-        DrawThemeParentBackground(hwnd, hDC, NULL);
+    if (prfFlag == 0 || (prfFlag & PRF_ERASEBKGND))
+    {
+        if (IsThemeBackgroundPartiallyTransparent(theme, BP_PUSHBUTTON, state))
+            DrawThemeParentBackground(hwnd, hDC, NULL);
+    }
+
     DrawThemeBackground(theme, hDC, BP_PUSHBUTTON, state, &bgRect, NULL);
+
+    if (pdata->imlData.himl && ImageList_GetIconSize(pdata->imlData.himl, &ImageSize.cx, &ImageSize.cy))
+    {
+        int left = textRect.left + pdata->imlData.margin.left;
+        int top = textRect.top + (textRect.bottom - textRect.top - ImageSize.cy) / 2;
+        textRect.left += pdata->imlData.margin.left + pdata->imlData.margin.right + ImageSize.cy;
+        ImageList_Draw(pdata->imlData.himl, 0, hDC, left, top, 0);
+    }
+
     if (text)
     {
         DrawThemeText(theme, hDC, BP_PUSHBUTTON, state, text, lstrlenW(text), dtFlags, 0, &textRect);
         HeapFree(GetProcessHeap(), 0, text);
     }
 
+    if (focused)
+    {
+        MARGINS margins;
+        RECT focusRect = bgRect;
+
+        GetThemeMargins(theme, hDC, BP_PUSHBUTTON, state, TMT_CONTENTMARGINS, NULL, &margins);
+
+        focusRect.left += margins.cxLeftWidth;
+        focusRect.top += margins.cyTopHeight;
+        focusRect.right -= margins.cxRightWidth;
+        focusRect.bottom -= margins.cyBottomHeight;
+
+        DrawFocusRect( hDC, &focusRect );
+    }
+
     if (hPrevFont) SelectObject(hDC, hPrevFont);
 }
 
-static void CB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
+static void CB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused, LPARAM prfFlag)
 {
     static const int cb_states[3][5] =
     {
@@ -126,12 +172,10 @@ static void CB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UIN
         { RBS_CHECKEDNORMAL, RBS_CHECKEDDISABLED, RBS_CHECKEDHOT, RBS_CHECKEDPRESSED, RBS_CHECKEDNORMAL }
     };
 
-    static const int cb_size = 13;
-
+    SIZE sz;
     RECT bgRect, textRect;
-    HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
-    HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
-    LRESULT checkState = SendMessageW(hwnd, BM_GETCHECK, 0, 0);
+    HFONT font, hPrevFont = NULL;
+    LRESULT checkState = get_button_state(hwnd) & 3;
     DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
     int part = ((dwStyle & BUTTON_TYPE) == BS_RADIOBUTTON) || ((dwStyle & BUTTON_TYPE) == BS_AUTORADIOBUTTON)
              ? BP_RADIOBUTTON
@@ -140,39 +184,93 @@ static void CB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UIN
               ? cb_states[ checkState ][ drawState ]
               : rb_states[ checkState ][ drawState ];
     WCHAR *text = get_button_text(hwnd);
+    LOGFONTW lf;
+    BOOL created_font = FALSE;
+
+    HRESULT hr = GetThemeFont(theme, hDC, part, state, TMT_FONT, &lf);
+    if (SUCCEEDED(hr)) {
+        font = CreateFontIndirectW(&lf);
+        if (!font)
+            TRACE("Failed to create font\n");
+        else {
+            TRACE("font = %s\n", debugstr_w(lf.lfFaceName));
+            hPrevFont = SelectObject(hDC, font);
+            created_font = TRUE;
+        }
+    } else {
+        font = get_button_font(hwnd);
+        hPrevFont = SelectObject(hDC, font);
+    }
+
+    if (FAILED(GetThemePartSize(theme, hDC, part, state, NULL, TS_DRAW, &sz)))
+        sz.cx = sz.cy = 13;
 
     GetClientRect(hwnd, &bgRect);
     GetThemeBackgroundContentRect(theme, hDC, part, state, &bgRect, &textRect);
 
     if (dtFlags & DT_SINGLELINE) /* Center the checkbox / radio button to the text. */
-        bgRect.top = bgRect.top + (textRect.bottom - textRect.top - cb_size) / 2;
+        bgRect.top = bgRect.top + (textRect.bottom - textRect.top - sz.cy) / 2;
 
     /* adjust for the check/radio marker */
-    bgRect.bottom = bgRect.top + cb_size;
-    bgRect.right = bgRect.left + cb_size;
+    bgRect.bottom = bgRect.top + sz.cy;
+    bgRect.right = bgRect.left + sz.cx;
     textRect.left = bgRect.right + 6;
 
-    if (IsThemeBackgroundPartiallyTransparent(theme, part, state))
+    if (prfFlag == 0 || (prfFlag & PRF_ERASEBKGND))
+    {
         DrawThemeParentBackground(hwnd, hDC, NULL);
+    }
+
     DrawThemeBackground(theme, hDC, part, state, &bgRect, NULL);
     if (text)
     {
         DrawThemeText(theme, hDC, part, state, text, lstrlenW(text), dtFlags, 0, &textRect);
+
+        if (focused)
+        {
+            RECT focusRect;
+
+            focusRect = textRect;
+
+            DrawTextW(hDC, text, lstrlenW(text), &focusRect, dtFlags | DT_CALCRECT);
+
+            if (focusRect.right < textRect.right) focusRect.right++;
+            focusRect.bottom = textRect.bottom;
+
+            DrawFocusRect( hDC, &focusRect );
+        }
+
         HeapFree(GetProcessHeap(), 0, text);
     }
 
+    if (created_font) DeleteObject(font);
     if (hPrevFont) SelectObject(hDC, hPrevFont);
 }
 
-static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags)
+static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UINT dtFlags, BOOL focused, LPARAM prfFlag)
 {
     static const int states[] = { GBS_NORMAL, GBS_DISABLED, GBS_NORMAL, GBS_NORMAL, GBS_NORMAL };
 
     RECT bgRect, textRect, contentRect;
-    HFONT font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0);
-    HFONT hPrevFont = font ? SelectObject(hDC, font) : NULL;
     int state = states[ drawState ];
     WCHAR *text = get_button_text(hwnd);
+    LOGFONTW lf;
+    HFONT font, hPrevFont = NULL;
+    BOOL created_font = FALSE;
+
+    HRESULT hr = GetThemeFont(theme, hDC, BP_GROUPBOX, state, TMT_FONT, &lf);
+    if (SUCCEEDED(hr)) {
+        font = CreateFontIndirectW(&lf);
+        if (!font)
+            TRACE("Failed to create font\n");
+        else {
+            hPrevFont = SelectObject(hDC, font);
+            created_font = TRUE;
+        }
+    } else {
+        font = get_button_font(hwnd);
+        hPrevFont = SelectObject(hDC, font);
+    }
 
     GetClientRect(hwnd, &bgRect);
     textRect = bgRect;
@@ -192,20 +290,24 @@ static void GB_draw(HTHEME theme, HWND hwnd, HDC hDC, ButtonState drawState, UIN
     GetThemeBackgroundContentRect(theme, hDC, BP_GROUPBOX, state, &bgRect, &contentRect);
     ExcludeClipRect(hDC, contentRect.left, contentRect.top, contentRect.right, contentRect.bottom);
 
-    if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
-        DrawThemeParentBackground(hwnd, hDC, NULL);
+    if (prfFlag == 0 || (prfFlag & PRF_ERASEBKGND))
+    {
+        if (IsThemeBackgroundPartiallyTransparent(theme, BP_GROUPBOX, state))
+            DrawThemeParentBackground(hwnd, hDC, NULL);
+    }
+
     DrawThemeBackground(theme, hDC, BP_GROUPBOX, state, &bgRect, NULL);
 
     SelectClipRgn(hDC, NULL);
 
     if (text)
     {
-        textRect.left += 2;
-        textRect.right -= 2;
+        InflateRect(&textRect, -2, 0);
         DrawThemeText(theme, hDC, BP_GROUPBOX, state, text, lstrlenW(text), 0, 0, &textRect);
         HeapFree(GetProcessHeap(), 0, text);
     }
 
+    if (created_font) DeleteObject(font);
     if (hPrevFont) SelectObject(hDC, hPrevFont);
 }
 
@@ -229,20 +331,27 @@ static const pfThemedPaint btnThemedPaintFunc[BUTTON_TYPE + 1] =
     NULL, /* Not defined */
 };
 
-static BOOL BUTTON_Paint(HTHEME theme, HWND hwnd, HDC hParamDC)
+BOOL BUTTON_PaintWithTheme(HTHEME theme, HWND hwnd, HDC hParamDC, LPARAM prfFlag)
 {
-    PAINTSTRUCT ps;
-    HDC hDC;
-    DWORD dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
-    DWORD dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
-    UINT dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
-    int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
+    DWORD dwStyle;
+    DWORD dwStyleEx;
+    UINT dtFlags;
+    int state;
     ButtonState drawState;
-    pfThemedPaint paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
+    pfThemedPaint paint;
 
-    if(!paint)
+    dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
+    paint = btnThemedPaintFunc[ dwStyle & BUTTON_TYPE ];
+    if (!paint)
         return FALSE;
 
+    if (get_button_image(hwnd) != 0)
+        return FALSE;
+
+    dwStyleEx = GetWindowLongW(hwnd, GWL_EXSTYLE);
+    dtFlags = get_drawtext_flags(dwStyle, dwStyleEx);
+    state = get_button_state(hwnd);
+
     if(IsWindowEnabled(hwnd))
     {
         if(state & BST_PUSHED) drawState = STATE_PRESSED;
@@ -252,95 +361,11 @@ static BOOL BUTTON_Paint(HTHEME theme, HWND hwnd, HDC hParamDC)
     }
     else drawState = STATE_DISABLED;
 
-    hDC = hParamDC ? hParamDC : BeginPaint(hwnd, &ps);
-    paint(theme, hwnd, hDC, drawState, dtFlags);
-    if (!hParamDC) EndPaint(hwnd, &ps);
-    return TRUE;
-}
-
-/**********************************************************************
- * The button control subclass window proc.
- */
-LRESULT CALLBACK THEMING_ButtonSubclassProc(HWND hwnd, UINT msg,
-                                            WPARAM wParam, LPARAM lParam,
-                                            ULONG_PTR dwRefData)
-{
-    const WCHAR* themeClass = WC_BUTTONW;
-    HTHEME theme;
-    LRESULT result;
-
-    switch (msg)
+    if (drawState == STATE_NORMAL && (dwStyle & BUTTON_TYPE) == BS_DEFPUSHBUTTON)
     {
-    case WM_CREATE:
-        result = THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
-        OpenThemeData(hwnd, themeClass);
-        return result;
-
-    case WM_DESTROY:
-        theme = GetWindowTheme(hwnd);
-        CloseThemeData (theme);
-        return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
-
-    case WM_THEMECHANGED:
-        theme = GetWindowTheme(hwnd);
-        CloseThemeData (theme);
-        OpenThemeData(hwnd, themeClass);
-        break;
-
-    case WM_SYSCOLORCHANGE:
-        theme = GetWindowTheme(hwnd);
-       if (!theme) return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
-        /* Do nothing. When themed, a WM_THEMECHANGED will be received, too,
-        * which will do the repaint. */
-        break;
-
-    case WM_PAINT:
-        theme = GetWindowTheme(hwnd);
-        if (theme && BUTTON_Paint(theme, hwnd, (HDC)wParam))
-            return 0;
-        else
-            return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
-
-    case WM_ENABLE:
-        theme = GetWindowTheme(hwnd);
-        if (theme) RedrawWindow(hwnd, NULL, NULL,
-                                RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
-        return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
-
-    case WM_MOUSEMOVE:
-    {
-        TRACKMOUSEEVENT mouse_event;
-        mouse_event.cbSize = sizeof(TRACKMOUSEEVENT);
-        mouse_event.dwFlags = TME_QUERY;
-        if(!TrackMouseEvent(&mouse_event) || !(mouse_event.dwFlags&(TME_HOVER|TME_LEAVE)))
-        {
-            mouse_event.dwFlags = TME_HOVER|TME_LEAVE;
-            mouse_event.hwndTrack = hwnd;
-            mouse_event.dwHoverTime = 1;
-            TrackMouseEvent(&mouse_event);
-        }
-        break;
+        drawState = STATE_DEFAULTED;
     }
 
-    case WM_MOUSEHOVER:
-    {
-        int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
-        SetWindowLongW(hwnd, 0, state|BST_HOT);
-        InvalidateRect(hwnd, NULL, FALSE);
-        break;
-    }
-
-    case WM_MOUSELEAVE:
-    {
-        int state = (int)SendMessageW(hwnd, BM_GETSTATE, 0, 0);
-        SetWindowLongW(hwnd, 0, state&(~BST_HOT));
-        InvalidateRect(hwnd, NULL, FALSE);
-        break;
-    }
-
-    default:
-       /* Call old proc */
-       return THEMING_CallOriginalClass(hwnd, msg, wParam, lParam);
-    }
-    return 0;
+    paint(theme, hwnd, hParamDC, drawState, dtFlags, state & BST_FOCUS, prfFlag);
+    return TRUE;
 }