Add wxBitmapBundle::GetPreferredLogicalSizeFor()

We often need the logical bitmap size when using it in size computations
involving window size, so add a function returning it directly to
wxBitmapBundle, similarly to wxBitmap::GetLogicalSize(), to avoid using
FromPhys() everywhere.

Also rename the existing wxBitmapBundle::GetPreferredSizeFor() to
GetPreferredBitmapSizeFor() to make it more clear that this is similar
to wxBitmap::GetSize() and so returns the size in physical units.

Closes #22056.
This commit is contained in:
Alexander Koshelev
2022-01-25 12:10:07 +03:00
committed by Vadim Zeitlin
parent dd185a0b27
commit b2629a97e5
20 changed files with 82 additions and 57 deletions

View File

@@ -176,6 +176,9 @@ use "Logical", "Physical" or "DIP" in their names, the only exceptions are:
wxBitmap::GetLogicalHeight() and wxBitmap::GetLogicalSize() to use or get the
size in logical pixels which must be used in any computations involving the
sizes expressed in logical units.
- Similarly, the size returned by wxBitmapBundle::GetPreferredBitmapSizeFor()
is in physical pixels, but wxBitmapBundle::GetPreferredLogicalSizeFor() can
be used to retrieve the same value in logical units.
- The default size of wxBitmapBundle, taken by wxBitmapBundle::FromSVG() and
returned by wxBitmapBundle::GetDefaultSize() is in DIPs.

View File

@@ -99,11 +99,14 @@ public:
// default DPI, i.e. 100% scaling. Returns invalid size for empty bundle.
wxSize GetDefaultSize() const;
// Get the physical size of the preferred bitmap at the given scale.
wxSize GetPreferredBitmapSizeAtScale(double scale) const;
// Get preferred size, i.e. usually the closest size in which a bitmap is
// available to the ideal size determined from the default size and the DPI
// scaling, for the given window.
wxSize GetPreferredSizeFor(const wxWindow* window) const;
wxSize GetPreferredSizeAtScale(double scale) const;
// scaling, for the given window, in physical/logical pixels respectively.
wxSize GetPreferredBitmapSizeFor(const wxWindow* window) const;
wxSize GetPreferredLogicalSizeFor(const wxWindow* window) const;
// Get bitmap of the specified size, creating a new bitmap from the closest
// available size by rescaling it if necessary.
@@ -115,9 +118,9 @@ public:
// GetBitmap() converting the returned bitmap to the icon.
wxIcon GetIcon(const wxSize& size) const;
// Helpers combining GetBitmap() or GetIcon() and GetPreferredSizeFor():
// return the bitmap or icon of the size appropriate for the current DPI
// scaling of the given window.
// Helpers combining GetPreferredBitmapSizeFor() and GetBitmap() or
// GetIcon(): return the bitmap or icon of the size appropriate for the
// current DPI scaling of the given window.
wxBitmap GetBitmapFor(const wxWindow* window) const;
wxIcon GetIconFor(const wxWindow* window) const;
@@ -217,7 +220,7 @@ public:
// Return the preferred size that should be used at the given scale.
//
// Must always return a valid size.
virtual wxSize GetPreferredSizeAtScale(double scale) const = 0;
virtual wxSize GetPreferredBitmapSizeAtScale(double scale) const = 0;
// Retrieve the bitmap of exactly the given size.
//

View File

@@ -293,18 +293,30 @@ public:
bitmap doesn't need to be rescaled, which typically significantly
lowers its quality.
*/
wxSize GetPreferredSizeAtScale(double scale) const;
wxSize GetPreferredBitmapSizeAtScale(double scale) const;
/**
Get the size that would be best to use for this bundle at the DPI
scaling factor used by the given window.
This is just a convenient wrapper for GetPreferredSizeAtScale() calling
This is just a convenient wrapper for GetPreferredBitmapSizeAtScale() calling
that function with the result of wxWindow::GetDPIScaleFactor().
@param window Non-null and fully created window.
*/
wxSize GetPreferredSizeFor(const wxWindow* window) const;
wxSize GetPreferredBitmapSizeFor(const wxWindow* window) const;
/**
Get the size that would be best to use for this bundle at the DPI
scaling factor used by the given window in logical size.
This is just call GetPreferredBitmapSizeAtScale() with the result of
wxWindow::GetDPIScaleFactor() and convert returned value with
wxWindow::FromPhys().
@param window Non-null and fully created window.
*/
wxSize GetPreferredLogicalSizeFor(const wxWindow* window) const;
/**
Get bitmap of the specified size, creating a new bitmap from the closest
@@ -325,8 +337,8 @@ public:
Get bitmap of the size appropriate for the DPI scaling used by the
given window.
This helper function simply combines GetBitmap() and
GetPreferredSizeFor(), i.e. it returns a (normally unscaled) bitmap
This helper function simply combines GetPreferredBitmapSizeFor() and
GetBitmap(), i.e. it returns a (normally unscaled) bitmap
from the bundle of the closest size to the size that should be used at
the DPI scaling of the provided window.
@@ -384,7 +396,7 @@ public:
... determine the minimum/default size for bitmap to use ...
}
wxSize GetPreferredSizeAtScale(double scale) const wxOVERRIDE
wxSize GetPreferredBitmapSizeAtScale(double scale) const wxOVERRIDE
{
// If it's ok to scale the bitmap, just use the standard size
// at the given scale:
@@ -428,7 +440,7 @@ public:
Must always return a valid size.
*/
virtual wxSize GetPreferredSizeAtScale(double scale) const = 0;
virtual wxSize GetPreferredBitmapSizeAtScale(double scale) const = 0;
/**
Retrieve the bitmap of exactly the given size.

View File

@@ -520,7 +520,7 @@ void MyFrame::PopulateToolbar(wxToolBarBase* toolBar)
return m_sizeDef;
}
wxSize GetPreferredSizeAtScale(double scale) const wxOVERRIDE
wxSize GetPreferredBitmapSizeAtScale(double scale) const wxOVERRIDE
{
// We just scale the bitmap to fit the requested size, so
// we don't really have any preferences.

View File

@@ -1015,7 +1015,7 @@ void MyTreeCtrl::CreateImages(int size)
return m_sizeDef;
}
wxSize GetPreferredSizeAtScale(double scale) const wxOVERRIDE
wxSize GetPreferredBitmapSizeAtScale(double scale) const wxOVERRIDE
{
return m_sizeDef*scale;
}

View File

@@ -410,7 +410,7 @@ void wxAuiGenericToolBarArt::DrawDropDownButton(
const wxSize sizeDropDown = m_buttonDropDownBmp.GetPreferredSizeFor(wnd);
const wxSize sizeDropDown = m_buttonDropDownBmp.GetPreferredLogicalSizeFor(wnd);
dropBmpX = dropDownRect.x +
(dropDownRect.width/2) -
(sizeDropDown.x/2);

View File

@@ -685,7 +685,7 @@ wxAuiDefaultDockArt::DrawIcon(wxDC& dc, wxWindow *window, const wxRect& rect, wx
}
// Ensure the icon fits into the title bar.
wxSize iconSize = pane.icon.GetPreferredSizeFor(window);
wxSize iconSize = pane.icon.GetPreferredLogicalSizeFor(window);
if (iconSize.y > rect.height)
{
iconSize *= static_cast<double>(rect.height) / iconSize.y;
@@ -693,7 +693,7 @@ wxAuiDefaultDockArt::DrawIcon(wxDC& dc, wxWindow *window, const wxRect& rect, wx
// Draw the icon centered vertically
int xOffset = window->FromDIP(2);
const wxBitmap& icon = pane.icon.GetBitmap(iconSize);
const wxBitmap& icon = pane.icon.GetBitmap(window->ToPhys(iconSize));
dc.DrawBitmap(icon,
rect.x+xOffset, rect.y+(rect.height-icon.GetLogicalHeight())/2,
true);

View File

@@ -248,9 +248,9 @@ void wxAuiGenericTabArt::SetSizingInfo(const wxSize& tab_ctrl_size,
int tot_width = (int)tab_ctrl_size.x - GetIndentSize() - wnd->FromDIP(4);
if (m_flags & wxAUI_NB_CLOSE_BUTTON)
tot_width -= wnd->FromPhys(m_activeCloseBmp.GetPreferredSizeFor(wnd).x);
tot_width -= m_activeCloseBmp.GetPreferredLogicalSizeFor(wnd).x;
if (m_flags & wxAUI_NB_WINDOWLIST_BUTTON)
tot_width -= wnd->FromPhys(m_activeWindowListBmp.GetPreferredSizeFor(wnd).x);
tot_width -= m_activeWindowListBmp.GetPreferredLogicalSizeFor(wnd).x;
if (tab_count > 0)
{
@@ -705,7 +705,7 @@ wxSize wxAuiGenericTabArt::GetTabSize(wxDC& dc,
{
// we need the correct size of the bitmap to be used on this window in
// logical dimensions for drawing
const wxSize bitmapSize = wnd->FromPhys(bitmap.GetPreferredSizeFor(wnd));
const wxSize bitmapSize = bitmap.GetPreferredLogicalSizeFor(wnd);
// increase by bitmap plus right side bitmap padding
tab_width += bitmapSize.x + wnd->FromDIP(3);

View File

@@ -306,7 +306,7 @@ wxSize wxAuiMSWTabArt::GetTabSize(wxDC& dc,
// if there's a bitmap, add space for it
if ( bitmap.IsOk() )
{
const wxSize bitmapSize = wnd->FromPhys(bitmap.GetPreferredSizeFor(wnd));
const wxSize bitmapSize = bitmap.GetPreferredLogicalSizeFor(wnd);
tabWidth += bitmapSize.x + wnd->FromDIP(3); // bitmap padding
tabHeight = wxMax(tabHeight, bitmapSize.y + wnd->FromDIP(2));

View File

@@ -162,7 +162,7 @@ public:
return m_sizeDefault;
}
virtual wxSize GetPreferredSizeAtScale(double scale) const wxOVERRIDE
virtual wxSize GetPreferredBitmapSizeAtScale(double scale) const wxOVERRIDE
{
// We have no preferred sizes.
return m_sizeDefault*scale;

View File

@@ -63,7 +63,7 @@ public:
}
virtual wxSize GetDefaultSize() const wxOVERRIDE;
virtual wxSize GetPreferredSizeAtScale(double scale) const wxOVERRIDE;
virtual wxSize GetPreferredBitmapSizeAtScale(double scale) const wxOVERRIDE;
virtual wxBitmap GetBitmap(const wxSize& size) wxOVERRIDE;
private:
@@ -169,7 +169,7 @@ wxSize wxBitmapBundleImplSet::GetDefaultSize() const
return m_sizeDefault;
}
wxSize wxBitmapBundleImplSet::GetPreferredSizeAtScale(double scale) const
wxSize wxBitmapBundleImplSet::GetPreferredBitmapSizeAtScale(double scale) const
{
// Target size is the ideal size we'd like the bitmap to have at this scale.
const wxSize sizeTarget = GetDefaultSize()*scale;
@@ -429,19 +429,26 @@ wxSize wxBitmapBundle::GetDefaultSize() const
return m_impl->GetDefaultSize();
}
wxSize wxBitmapBundle::GetPreferredSizeFor(const wxWindow* window) const
wxSize wxBitmapBundle::GetPreferredBitmapSizeFor(const wxWindow* window) const
{
wxCHECK_MSG( window, wxDefaultSize, "window must be valid" );
return GetPreferredSizeAtScale(window->GetDPIScaleFactor());
return GetPreferredBitmapSizeAtScale(window->GetDPIScaleFactor());
}
wxSize wxBitmapBundle::GetPreferredSizeAtScale(double scale) const
wxSize wxBitmapBundle::GetPreferredLogicalSizeFor(const wxWindow* window) const
{
wxCHECK_MSG( window, wxDefaultSize, "window must be valid" );
return window->FromPhys(GetPreferredBitmapSizeAtScale(window->GetDPIScaleFactor()));
}
wxSize wxBitmapBundle::GetPreferredBitmapSizeAtScale(double scale) const
{
if ( !m_impl )
return wxDefaultSize;
return m_impl->GetPreferredSizeAtScale(scale);
return m_impl->GetPreferredBitmapSizeAtScale(scale);
}
wxBitmap wxBitmapBundle::GetBitmap(const wxSize& size) const
@@ -474,12 +481,12 @@ wxIcon wxBitmapBundle::GetIcon(const wxSize& size) const
wxBitmap wxBitmapBundle::GetBitmapFor(const wxWindow* window) const
{
return GetBitmap(GetPreferredSizeFor(window));
return GetBitmap(GetPreferredBitmapSizeFor(window));
}
wxIcon wxBitmapBundle::GetIconFor(const wxWindow* window) const
{
return GetIcon(GetPreferredSizeFor(window));
return GetIcon(GetPreferredBitmapSizeFor(window));
}
namespace
@@ -530,7 +537,7 @@ wxBitmapBundle::GetConsensusSizeFor(wxWindow* win,
SizePrefs prefs;
for ( size_t n = 0; n < bundles.size(); ++n )
{
RecordSizePref(prefs, bundles[n].GetPreferredSizeAtScale(scale));
RecordSizePref(prefs, bundles[n].GetPreferredBitmapSizeAtScale(scale));
}
// Now find the size preferred by most tools.

View File

@@ -2221,7 +2221,7 @@ bool wxDataViewCheckIconTextRenderer::Render(wxRect cell, wxDC* dc, int state)
const bool drawIcon = bb.IsOk();
if ( drawIcon )
{
const wxSize sizeIcon = bb.GetPreferredSizeFor(GetView());
const wxSize sizeIcon = bb.GetPreferredLogicalSizeFor(GetView());
rectIcon = wxRect(cell.GetPosition(), sizeIcon);
rectIcon.x += xoffset;
rectIcon = rectIcon.CentreIn(cell, wxVERTICAL);

View File

@@ -92,7 +92,7 @@ wxStaticBitmapBase::~wxStaticBitmapBase()
wxSize wxStaticBitmapBase::DoGetBestSize() const
{
if ( m_bitmapBundle.IsOk() )
return FromPhys(m_bitmapBundle.GetPreferredSizeFor(this));
return m_bitmapBundle.GetPreferredLogicalSizeFor(this);
// the fall back size is completely arbitrary
return wxSize(16, 16);

View File

@@ -108,7 +108,7 @@ public:
}
virtual wxSize GetDefaultSize() const wxOVERRIDE;
virtual wxSize GetPreferredSizeAtScale(double scale) const wxOVERRIDE;
virtual wxSize GetPreferredBitmapSizeAtScale(double scale) const wxOVERRIDE;
virtual wxBitmap GetBitmap(const wxSize& size) wxOVERRIDE;
private:
@@ -142,7 +142,7 @@ wxSize wxBitmapBundleImplSVG::GetDefaultSize() const
return m_sizeDef;
}
wxSize wxBitmapBundleImplSVG::GetPreferredSizeAtScale(double scale) const
wxSize wxBitmapBundleImplSVG::GetPreferredBitmapSizeAtScale(double scale) const
{
// We consider that we can render at any scale.
return m_sizeDef*scale;

View File

@@ -1577,7 +1577,7 @@ wxSize wxDataViewIconTextRenderer::GetSize() const
const wxBitmapBundle& bb = m_value.GetBitmapBundle();
if (bb.IsOk())
size.x += bb.GetPreferredSizeFor(dvc).x + dvc->FromDIP(4);
size.x += bb.GetPreferredLogicalSizeFor(dvc).x + dvc->FromDIP(4);
return size;
}
return dvc->FromDIP(wxSize(80,20));
@@ -1596,7 +1596,7 @@ wxWindow* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow *parent, wxRect
{
wxWindow* const dvc = GetView();
int w = bb.GetPreferredSizeFor(dvc).x + dvc->FromDIP(4);
int w = bb.GetPreferredLogicalSizeFor(dvc).x + dvc->FromDIP(4);
labelRect.x += w;
labelRect.width -= w;
}

View File

@@ -108,7 +108,7 @@ public:
wxButtonImageData(wxWindow* btn, const wxBitmapBundle& normalBundle)
: m_btn(btn)
{
m_bitmapSize = normalBundle.GetPreferredSizeFor(btn);
m_bitmapSize = normalBundle.GetPreferredBitmapSizeFor(btn);
m_bitmapBundles[wxAnyButton::State_Normal] = normalBundle;
@@ -125,7 +125,7 @@ public:
{
event.Skip();
m_bitmapSize = m_bitmapBundles[wxAnyButton::State_Normal].GetPreferredSizeFor(m_btn);
m_bitmapSize = m_bitmapBundles[wxAnyButton::State_Normal].GetPreferredBitmapSizeFor(m_btn);
}
// Bitmap can be set either explicitly, when the bitmap for the given state

View File

@@ -138,7 +138,7 @@ public:
const wxBitmap& bitmap);
virtual wxSize GetDefaultSize() const wxOVERRIDE;
virtual wxSize GetPreferredSizeAtScale(double scale) const wxOVERRIDE;
virtual wxSize GetPreferredBitmapSizeAtScale(double scale) const wxOVERRIDE;
virtual wxBitmap GetBitmap(const wxSize& size) wxOVERRIDE;
private:
@@ -186,7 +186,7 @@ wxSize wxBitmapBundleImplRC::GetDefaultSize() const
return m_bitmaps[0].GetSize();
}
wxSize wxBitmapBundleImplRC::GetPreferredSizeAtScale(double scale) const
wxSize wxBitmapBundleImplRC::GetPreferredBitmapSizeAtScale(double scale) const
{
// Optimistically assume we're going to use this exact scale by default.
double scalePreferred = scale;

View File

@@ -109,7 +109,7 @@ WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const
wxSize wxStaticBitmap::GetImageSize() const
{
return m_icon.IsOk() ? m_icon.GetSize()
: m_bitmapBundle.GetPreferredSizeFor(this);
: m_bitmapBundle.GetPreferredBitmapSizeFor(this);
}
void wxStaticBitmap::SetIcon(const wxIcon& icon)

View File

@@ -113,7 +113,7 @@ public:
~wxOSXImageBundleImpl();
virtual wxSize GetDefaultSize() const wxOVERRIDE;
virtual wxSize GetPreferredSizeAtScale(double scale) const wxOVERRIDE;
virtual wxSize GetPreferredBitmapSizeAtScale(double scale) const wxOVERRIDE;
virtual wxBitmap GetBitmap(const wxSize& size) wxOVERRIDE;
};
@@ -138,7 +138,7 @@ wxSize wxOSXImageBundleImpl::GetDefaultSize() const
return wxSize(sz.width, sz.height);
}
wxSize wxOSXImageBundleImpl::GetPreferredSizeAtScale(double scale) const
wxSize wxOSXImageBundleImpl::GetPreferredBitmapSizeAtScale(double scale) const
{
// The system always performs scaling, as the scaling factor is integer and
// so it doesn't make sense to round it up or down, hence we should use the
@@ -253,7 +253,7 @@ WXImage wxOSXGetImageFromBundle(const wxBitmapBundle& bundle)
image = wxOSXImageFromBitmap(bmp);
// unconditionally try to add a 2x version, if there really is a different one
wxSize doublesz = impl->GetPreferredSizeAtScale(2.0);
wxSize doublesz = impl->GetPreferredBitmapSizeAtScale(2.0);
if ( doublesz != sz )
{
bmp = const_cast<wxBitmapBundleImpl*>(impl)->GetBitmap(doublesz);

View File

@@ -49,7 +49,7 @@ TEST_CASE("BitmapBundle::FromBitmaps", "[bmpbundle]")
TEST_CASE("BitmapBundle::GetPreferredSize", "[bmpbundle]")
{
CHECK( wxBitmapBundle().GetPreferredSizeAtScale(1) == wxDefaultSize );
CHECK( wxBitmapBundle().GetPreferredBitmapSizeAtScale(1) == wxDefaultSize );
const wxSize normal(32, 32);
const wxSize bigger(64, 64);
@@ -59,18 +59,18 @@ TEST_CASE("BitmapBundle::GetPreferredSize", "[bmpbundle]")
// Check that the existing bitmaps are used without scaling for most of the
// typical scaling values.
CHECK( b.GetPreferredSizeAtScale(0 ) == normal );
CHECK( b.GetPreferredSizeAtScale(1 ) == normal );
CHECK( b.GetPreferredSizeAtScale(1.25) == normal );
CHECK( b.GetPreferredSizeAtScale(1.4 ) == normal );
CHECK( b.GetPreferredSizeAtScale(1.5 ) == bigger );
CHECK( b.GetPreferredSizeAtScale(1.75) == bigger );
CHECK( b.GetPreferredSizeAtScale(2 ) == bigger );
CHECK( b.GetPreferredSizeAtScale(2.5 ) == bigger );
CHECK( b.GetPreferredBitmapSizeAtScale(0 ) == normal );
CHECK( b.GetPreferredBitmapSizeAtScale(1 ) == normal );
CHECK( b.GetPreferredBitmapSizeAtScale(1.25) == normal );
CHECK( b.GetPreferredBitmapSizeAtScale(1.4 ) == normal );
CHECK( b.GetPreferredBitmapSizeAtScale(1.5 ) == bigger );
CHECK( b.GetPreferredBitmapSizeAtScale(1.75) == bigger );
CHECK( b.GetPreferredBitmapSizeAtScale(2 ) == bigger );
CHECK( b.GetPreferredBitmapSizeAtScale(2.5 ) == bigger );
// This scale is too big to use any of the existing bitmaps, so they will
// be scaled.
CHECK( b.GetPreferredSizeAtScale(3 ) == 3*normal );
CHECK( b.GetPreferredBitmapSizeAtScale(3 ) == 3*normal );
}
#ifdef wxHAS_DPI_INDEPENDENT_PIXELS