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.
This commit is contained in:
Vadim Zeitlin 2023-06-03 18:56:20 +01:00
parent ef93da3160
commit 0e3f010eec
5 changed files with 48 additions and 49 deletions

View file

@ -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;

View file

@ -35,31 +35,19 @@ wxAuiMSWToolBarArt::wxAuiMSWToolBarArt()
wxWindow* window = static_cast<wxApp*>(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;

View file

@ -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

View file

@ -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

View file

@ -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)
{