From 0e3f010eec6038462c0e811c85a20ef86f21355d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 3 Jun 2023 18:56:20 +0100 Subject: [PATCH] Add wxUxThemeHandle::GetTrueSize() and GetDrawSize() helpers These functions are more convenient to use and, arguably, more clear to read than calls to ::GetThemePartSize(), so add these wrappers and replace most of the uses of the Win32 function with them. This shouldn't result in any real changes to behaviour. --- include/wx/msw/uxtheme.h | 18 ++++++++++++++++++ src/aui/barartmsw.cpp | 20 ++++---------------- src/aui/tabartmsw.cpp | 13 +++---------- src/msw/renderer.cpp | 29 ++++++----------------------- src/msw/uxtheme.cpp | 17 +++++++++++++++++ 5 files changed, 48 insertions(+), 49 deletions(-) diff --git a/include/wx/msw/uxtheme.h b/include/wx/msw/uxtheme.h index b8d5f9612f..c90c384da2 100644 --- a/include/wx/msw/uxtheme.h +++ b/include/wx/msw/uxtheme.h @@ -218,6 +218,21 @@ public: // GetThemeColor() because we want to default the state. wxColour GetColour(int part, int prop, int state = 0) const; + // Return the size of a theme element, either "as is" (TS_TRUE size) or as + // it would be used for drawing (TS_DRAW size). + // + // For now we don't allow specifying the HDC or rectangle as they don't + // seem to be useful. + wxSize GetTrueSize(int part, int state = 0) const + { + return DoGetSize(part, state, TS_TRUE); + } + + wxSize GetDrawSize(int part, int state = 0) const + { + return DoGetSize(part, state, TS_DRAW); + } + // Draw theme background: if the caller already has a RECT, it can be // provided directly, otherwise wxRect is converted to it. void DrawBackground(HDC hdc, const RECT& rc, int part, int state = 0); @@ -233,6 +248,9 @@ private: { } + wxSize DoGetSize(int part, int state, THEMESIZE ts) const; + + // This is almost, but not quite, const: it's only reset in move ctor. HTHEME m_hTheme; diff --git a/src/aui/barartmsw.cpp b/src/aui/barartmsw.cpp index ef5029b813..797ac9a00f 100644 --- a/src/aui/barartmsw.cpp +++ b/src/aui/barartmsw.cpp @@ -35,31 +35,19 @@ wxAuiMSWToolBarArt::wxAuiMSWToolBarArt() wxWindow* window = static_cast(wxApp::GetInstance())->GetTopWindow(); wxUxThemeHandle hTheme(window, L"Rebar"); - SIZE overflowSize; - ::GetThemePartSize(hTheme, nullptr, RP_CHEVRON, 0, - nullptr, TS_TRUE, &overflowSize); - m_overflowSize = overflowSize.cx; + m_overflowSize = hTheme.GetTrueSize(RP_CHEVRON).x; - SIZE gripperSize; - ::GetThemePartSize(hTheme, nullptr, RP_GRIPPER, 0, - nullptr, TS_TRUE, &gripperSize); - m_gripperSize = gripperSize.cx; + m_gripperSize = hTheme.GetTrueSize(RP_GRIPPER).x; wxUxThemeHandle hThemeToolbar(window, L"Toolbar"); - SIZE seperatorSize; - ::GetThemePartSize(hThemeToolbar, nullptr, TP_SEPARATOR, 0, - nullptr, TS_TRUE, &seperatorSize); - m_separatorSize = seperatorSize.cx; + m_separatorSize = hThemeToolbar.GetTrueSize(TP_SEPARATOR).x; // TP_DROPDOWNBUTTON is only 7px, too small to fit the dropdown arrow, // use 14px instead. m_dropdownSize = window->FromDIP(14); - SIZE buttonSize; - ::GetThemePartSize(hThemeToolbar, nullptr, TP_BUTTON, 0, - nullptr, TS_TRUE, &buttonSize); - m_buttonSize.Set(buttonSize.cx, buttonSize.cy); + m_buttonSize = hThemeToolbar.GetTrueSize(TP_BUTTON); } else m_themed = false; diff --git a/src/aui/tabartmsw.cpp b/src/aui/tabartmsw.cpp index 0ecc39aa08..5ea75a9c9c 100644 --- a/src/aui/tabartmsw.cpp +++ b/src/aui/tabartmsw.cpp @@ -428,21 +428,14 @@ int wxAuiMSWTabArt::GetBestTabCtrlSize(wxWindow* wnd, return wxAuiGenericTabArt::GetBestTabCtrlSize(wnd, pages, requiredBmp_size); } -void wxAuiMSWTabArt::InitSizes(wxWindow* wnd, wxDC& dc) +void wxAuiMSWTabArt::InitSizes(wxWindow* wnd, wxDC& WXUNUSED(dc)) { - SIZE uxSize; - // Borrow close button from tooltip (best fit on various backgrounds) wxUxThemeHandle hTooltipTheme(wnd, L"Tooltip"); - - ::GetThemePartSize(hTooltipTheme, GetHdcOf(dc.GetTempHDC()), - TTP_CLOSE, 0, nullptr, TS_TRUE, &uxSize); - m_closeBtnSize.Set(uxSize.cx, uxSize.cy); + m_closeBtnSize = hTooltipTheme.GetTrueSize(TTP_CLOSE); wxUxThemeHandle hTabTheme(wnd, L"Tab"); - ::GetThemePartSize(hTabTheme, GetHdcOf(dc.GetTempHDC()), - TABP_TABITEM, 0, nullptr, TS_TRUE, &uxSize); - m_tabSize.Set(uxSize.cx, uxSize.cy); + m_tabSize = hTabTheme.GetTrueSize(TABP_TABITEM); } bool wxAuiMSWTabArt::IsThemed() const diff --git a/src/msw/renderer.cpp b/src/msw/renderer.cpp index cf84d9f0cd..e964453677 100644 --- a/src/msw/renderer.cpp +++ b/src/msw/renderer.cpp @@ -846,9 +846,7 @@ wxSize wxRendererXP::GetCheckBoxSize(wxWindow* win, int flags) { if (::IsThemePartDefined(hTheme, BP_CHECKBOX, 0)) { - SIZE checkSize; - if (::GetThemePartSize(hTheme, nullptr, BP_CHECKBOX, CBS_UNCHECKEDNORMAL, nullptr, TS_DRAW, &checkSize) == S_OK) - return wxSize(checkSize.cx, checkSize.cy); + return hTheme.GetDrawSize(BP_CHECKBOX, CBS_UNCHECKEDNORMAL); } } return m_rendererNative.GetCheckBoxSize(win, flags); @@ -863,9 +861,7 @@ wxSize wxRendererXP::GetCheckMarkSize(wxWindow* win) { if (::IsThemePartDefined(hTheme, MENU_POPUPCHECK, 0)) { - SIZE checkSize; - if (::GetThemePartSize(hTheme, nullptr, MENU_POPUPCHECK, MC_CHECKMARKNORMAL, nullptr, TS_DRAW, &checkSize) == S_OK) - return wxSize(checkSize.cx, checkSize.cy); + return hTheme.GetDrawSize(MENU_POPUPCHECK, MC_CHECKMARKNORMAL); } } return m_rendererNative.GetCheckMarkSize(win); @@ -880,11 +876,7 @@ wxSize wxRendererXP::GetExpanderSize(wxWindow* win) { if ( ::IsThemePartDefined(hTheme, TVP_GLYPH, 0) ) { - SIZE expSize; - if (::GetThemePartSize(hTheme, nullptr, TVP_GLYPH, GLPS_CLOSED, nullptr, - TS_DRAW, &expSize) == S_OK) - return wxSize(expSize.cx, expSize.cy); - + return hTheme.GetDrawSize(TVP_GLYPH, GLPS_CLOSED); } } @@ -968,19 +960,10 @@ wxSize wxRendererXP::GetCollapseButtonSize(wxWindow *win, wxDC& dc) if ( ::IsThemePartDefined(hTheme, TDLG_EXPANDOBUTTON, 0) ) { - SIZE s; - ::GetThemePartSize(hTheme, - GetHdcOf(dc.GetTempHDC()), - TDLG_EXPANDOBUTTON, - TDLGEBS_NORMAL, - nullptr, - TS_TRUE, - &s); - - return wxSize(s.cx, s.cy); + return hTheme.GetTrueSize(TDLG_EXPANDOBUTTON, TDLGEBS_NORMAL); } - else - return m_rendererNative.GetCollapseButtonSize(win, dc); + + return m_rendererNative.GetCollapseButtonSize(win, dc); } void diff --git a/src/msw/uxtheme.cpp b/src/msw/uxtheme.cpp index 7dc36f4e5d..e0cbb9a506 100644 --- a/src/msw/uxtheme.cpp +++ b/src/msw/uxtheme.cpp @@ -83,6 +83,23 @@ wxColour wxUxThemeHandle::GetColour(int part, int prop, int state) const return wxRGBToColour(col); } +wxSize wxUxThemeHandle::DoGetSize(int part, int state, THEMESIZE ts) const +{ + SIZE size; + HRESULT hr = ::GetThemePartSize(m_hTheme, nullptr, part, state, nullptr, ts, + &size); + if ( FAILED(hr) ) + { + wxLogApiError( + wxString::Format("GetThemePartSize(%i, %i)", part, state), + hr + ); + return {}; + } + + return wxSize{size.cx, size.cy}; +} + void wxUxThemeHandle::DrawBackground(HDC hdc, const RECT& rc, int part, int state) {