Separate the requested and effective size of toolbar bitmaps

Add wxToolBarBase::DoSetToolBitmapSize() which does everything that
SetToolBitmapSize() used to do and make SetToolBitmapSize() itself also
remember the size passed to it in a new field, so that we could check
that we don't decrease the bitmap size below the size requested by the
application, while still being able to decrease it if necessary due to a
DPI change.

This allows SetToolBitmapSize() to continue working as before, i.e.
scale the bitmaps up if necessary without ever scaling them down, but
also allows them to resize in both directions when DPI changes.

Closes #22105.
This commit is contained in:
Vadim Zeitlin
2022-02-09 23:58:24 +00:00
parent cde6f96ea8
commit 6ec36a900c
7 changed files with 42 additions and 17 deletions

View File

@@ -48,7 +48,6 @@ public:
virtual bool Realize() wxOVERRIDE;
virtual void SetToolBitmapSize(const wxSize& size) wxOVERRIDE;
virtual wxSize GetToolSize() const wxOVERRIDE;
virtual void SetRows(int nRows) wxOVERRIDE;
@@ -116,6 +115,8 @@ protected:
virtual void DoToggleTool(wxToolBarToolBase *tool, bool toggle) wxOVERRIDE;
virtual void DoSetToggle(wxToolBarToolBase *tool, bool toggle) wxOVERRIDE;
virtual void DoSetToolBitmapSize(const wxSize& size) wxOVERRIDE;
// return the appropriate size and flags for the toolbar control
virtual wxSize DoGetBestSize() const wxOVERRIDE;

View File

@@ -53,7 +53,6 @@ public:
#endif
virtual bool Realize() wxOVERRIDE;
virtual void SetToolBitmapSize(const wxSize& size) wxOVERRIDE;
virtual wxSize GetToolSize() const wxOVERRIDE;
virtual void SetRows(int nRows) wxOVERRIDE;
@@ -115,6 +114,8 @@ protected:
virtual void DoToggleTool(wxToolBarToolBase *tool, bool toggle) wxOVERRIDE;
virtual void DoSetToggle(wxToolBarToolBase *tool, bool toggle) wxOVERRIDE;
virtual void DoSetToolBitmapSize(const wxSize& size) wxOVERRIDE;
wxDECLARE_EVENT_TABLE();
#if wxOSX_USE_NATIVE_TOOLBAR
bool m_macUsesNativeToolbar ;

View File

@@ -685,6 +685,10 @@ protected:
return tool;
}
// set the tool bitmap size without changing m_requestedBitmapSize
virtual void DoSetToolBitmapSize(const wxSize& size);
// the list of all our tools
wxToolBarToolsList m_tools;
@@ -700,10 +704,15 @@ protected:
int m_toolPacking,
m_toolSeparation;
// the size of the toolbar bitmaps
// the currently used size of the toolbar bitmaps: the name is unfortunate
// but keep it for compatibility
wxCoord m_defaultWidth, m_defaultHeight;
private:
// the size of the bitmaps requested by the application by calling
// SetToolBitmapSize()
wxSize m_requestedBitmapSize;
wxDECLARE_EVENT_TABLE();
wxDECLARE_NO_COPY_CLASS(wxToolBarBase);
};

View File

@@ -433,12 +433,19 @@ void wxToolBarBase::ClearTools()
}
}
void wxToolBarBase::SetToolBitmapSize(const wxSize& size)
void wxToolBarBase::DoSetToolBitmapSize(const wxSize& size)
{
m_defaultWidth = size.x;
m_defaultHeight = size.y;
}
void wxToolBarBase::SetToolBitmapSize(const wxSize& size)
{
m_requestedBitmapSize = size;
DoSetToolBitmapSize(size);
}
wxSize wxToolBarBase::GetToolBitmapSize() const
{
return wxSize(m_defaultWidth, m_defaultHeight);
@@ -448,7 +455,7 @@ void wxToolBarBase::AdjustToolBitmapSize()
{
if ( HasFlag(wxTB_NOICONS) )
{
SetToolBitmapSize(wxSize(0, 0));
DoSetToolBitmapSize(wxSize(0, 0));
return;
}
@@ -476,6 +483,11 @@ void wxToolBarBase::AdjustToolBitmapSize()
sizeOrig
);
// Don't do anything if it doesn't change, our current size is supposed
// to satisfy any constraints we might have anyhow.
if ( sizePreferred == sizeOrig )
return;
// This size is supposed to be in logical units for the platforms where
// they differ from physical ones, so convert it.
//
@@ -486,14 +498,16 @@ void wxToolBarBase::AdjustToolBitmapSize()
// use the size computed here, this would need to be revisited.
sizePreferred /= GetContentScaleFactor();
// Increase the bitmap size to the preferred one, as the default bitmap
// size is small and using larger bitmaps shouldn't shrink them to it.
// However intentionally setting a size larger than the actual bitmap
// size should scale them up, as people sometimes want to use bigger
// buttons and this is how it used to behave before wxBitmapBundle
// introduction.
if ( sizePreferred.x > sizeOrig.x || sizePreferred.y > sizeOrig.y )
SetToolBitmapSize(sizePreferred);
// Don't decrease the bitmap below the size requested by the application
// as using larger bitmaps shouldn't shrink them to the small default
// size.
sizePreferred.IncTo(m_requestedBitmapSize);
// Call DoSetToolBitmapSize() and not SetToolBitmapSize() to avoid
// changing the requested bitmap size: if we set our own adjusted size
// as the preferred one, we wouldn't decrease it later even if we ought
// to, as when moving from a monitor with higher DPI to a lower-DPI one.
DoSetToolBitmapSize(sizePreferred);
}
}

View File

@@ -1669,9 +1669,9 @@ bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
// toolbar geometry
// ----------------------------------------------------------------------------
void wxToolBar::SetToolBitmapSize(const wxSize& size)
void wxToolBar::DoSetToolBitmapSize(const wxSize& size)
{
wxToolBarBase::SetToolBitmapSize(size);
wxToolBarBase::DoSetToolBitmapSize(size);
::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
}

View File

@@ -1332,7 +1332,7 @@ void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
DoLayout();
}
void wxToolBar::SetToolBitmapSize(const wxSize& size)
void wxToolBar::DoSetToolBitmapSize(const wxSize& size)
{
m_defaultWidth = size.x + kwxMacToolBorder;
m_defaultHeight = size.y + kwxMacToolBorder;

View File

@@ -280,7 +280,7 @@ void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
DoLayout();
}
void wxToolBar::SetToolBitmapSize(const wxSize& size)
void wxToolBar::DoSetToolBitmapSize(const wxSize& size)
{
m_defaultWidth = size.x;
m_defaultHeight = size.y;