diff --git a/include/wx/bmpbndl.h b/include/wx/bmpbndl.h index 5a13937a01..6872a142bd 100644 --- a/include/wx/bmpbndl.h +++ b/include/wx/bmpbndl.h @@ -122,6 +122,16 @@ public: // Access implementation wxBitmapBundleImpl* GetImpl() const { return m_impl.get(); } + + // Implementation only from now on. + + // Get the bitmap size preferred by the majority of the elements of the + // bundles at the scale appropriate for the given scale. + static wxSize + GetConsensusSizeFor(wxWindow* win, + const wxVector& bundles, + const wxSize& sizeDefault); + private: typedef wxObjectDataPtr wxBitmapBundleImplPtr; diff --git a/src/common/bmpbndl.cpp b/src/common/bmpbndl.cpp index 37040f634b..7a98fde781 100644 --- a/src/common/bmpbndl.cpp +++ b/src/common/bmpbndl.cpp @@ -465,6 +465,97 @@ wxBitmap wxBitmapBundle::GetBitmapFor(const wxWindow* window) const return GetBitmap(GetPreferredSizeFor(window)); } +namespace +{ + +// Struct containing the number of tools preferring to use the given size. +struct SizePrefWithCount +{ + SizePrefWithCount() : count(0) { } + + wxSize size; + int count; +}; + +typedef wxVector SizePrefs; + +void RecordSizePref(SizePrefs& prefs, const wxSize& size) +{ + for ( size_t n = 0; n < prefs.size(); ++n ) + { + if ( prefs[n].size == size ) + { + prefs[n].count++; + return; + } + } + + SizePrefWithCount pref; + pref.size = size; + pref.count++; + prefs.push_back(pref); +} + +} // anonymous namespace + +/* static */ +wxSize +wxBitmapBundle::GetConsensusSizeFor(wxWindow* win, + const wxVector& bundles, + const wxSize& sizeDefault) +{ + const double scale = win->GetDPIScaleFactor(); + const wxSize sizeIdeal = sizeDefault*scale; + + // We want to use preferred bitmap size, but the preferred sizes can be + // different for different bitmap bundles, so record all their preferences + // first. + SizePrefs prefs; + for ( size_t n = 0; n < bundles.size(); ++n ) + { + RecordSizePref(prefs, bundles[n].GetPreferredSizeAtScale(scale)); + } + + // Now find the size preferred by most tools. + int countMax = 0; + wxSize sizePreferred; + for ( size_t n = 0; n < prefs.size(); ++n ) + { + const int countThis = prefs[n].count; + const wxSize sizeThis = prefs[n].size; + + if ( countThis > countMax ) + { + countMax = countThis; + sizePreferred = sizeThis; + } + else if ( countThis == countMax ) + { + // We have a tie between different sizes, choose the one + // corresponding to the current scale factor, if possible, as this + // is the ideal bitmap size that should be consistent with all the + // other bitmaps. + if ( sizePreferred != sizeIdeal ) + { + if ( sizeThis == sizeIdeal ) + { + sizePreferred = sizeThis; + } + else // Neither of the sizes is the ideal one. + { + // Choose the larger one as like this some bitmaps will be + // downscaled, which should look better than upscaling some + // (other) ones. + if ( sizeThis.y > sizePreferred.y ) + sizePreferred = sizeThis; + } + } + } + } + + return sizePreferred; +} + // ============================================================================ // wxBitmapBundleImpl implementation // ============================================================================ diff --git a/src/common/tbarbase.cpp b/src/common/tbarbase.cpp index cda354fe36..a73b79b954 100644 --- a/src/common/tbarbase.cpp +++ b/src/common/tbarbase.cpp @@ -433,39 +433,6 @@ void wxToolBarBase::ClearTools() } } -namespace -{ - -// Struct containing the number of tools preferring to use the given size. -struct SizePrefWithCount -{ - SizePrefWithCount() : count(0) { } - - wxSize size; - int count; -}; - -typedef wxVector SizePrefs; - -void RecordSizePref(SizePrefs& prefs, const wxSize& size) -{ - for ( size_t n = 0; n < prefs.size(); ++n ) - { - if ( prefs[n].size == size ) - { - prefs[n].count++; - return; - } - } - - SizePrefWithCount pref; - pref.size = size; - pref.count++; - prefs.push_back(pref); -} - -} // anonymous namespace - void wxToolBarBase::AdjustToolBitmapSize() { if ( HasFlag(wxTB_NOICONS) ) @@ -476,59 +443,31 @@ void wxToolBarBase::AdjustToolBitmapSize() const wxSize sizeOrig(m_defaultWidth, m_defaultHeight); - // We want to use preferred bitmap size, but the preferred sizes can be - // different for different bitmap bundles, so record all their preferences - // first. - SizePrefs prefs; - const double scale = GetDPIScaleFactor(); + // Check if we should be using a different size because we have bitmaps + // that shouldn't be scaled to the size we use right now. + + wxVector bundles; for ( wxToolBarToolsList::const_iterator i = m_tools.begin(); i != m_tools.end(); ++i ) { const wxBitmapBundle& bmp = (*i)->GetNormalBitmapBundle(); if ( bmp.IsOk() ) - RecordSizePref(prefs, bmp.GetPreferredSizeAtScale(scale)); + bundles.push_back(bmp); } - // Now find the size preferred by most tools. - int countMax = 0; - wxSize sizePreferred; - for ( size_t n = 0; n < prefs.size(); ++n ) + if ( !bundles.empty() ) { - const int countThis = prefs[n].count; - const wxSize sizeThis = prefs[n].size; + const wxSize sizePreferred = wxBitmapBundle::GetConsensusSizeFor + ( + this, + bundles, + sizeOrig + ); - if ( countThis > countMax ) - { - countMax = countThis; - sizePreferred = sizeThis; - } - else if ( countThis == countMax ) - { - // We have a tie between different sizes, choose the one - // corresponding to the current scale factor, if possible, as this - // is the ideal bitmap size that should be consistent with all the - // other bitmaps. - if ( sizePreferred != sizeOrig*scale ) - { - if ( sizeThis == sizeOrig*scale ) - { - sizePreferred = sizeThis; - } - else // Neither of the sizes is the ideal one. - { - // Choose the larger one as like this some bitmaps will be - // downscaled, which should look better than upscaling some - // (other) ones. - if ( sizeThis.y > sizePreferred.y ) - sizePreferred = sizeThis; - } - } - } + if ( sizePreferred != sizeOrig ) + SetToolBitmapSize(sizePreferred); } - - if ( sizePreferred != sizeOrig ) - SetToolBitmapSize(sizePreferred); } bool wxToolBarBase::Realize()