From 024c231624ae7b2a1717b46ddeae5d6b39acac8d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 23 Sep 2021 22:46:53 +0100 Subject: [PATCH] Replace multiple RescaleBitmap() with wxBitmap::Rescale() Define a single function and use it in both wxArtProvider and (the generic implementation of) wxSearchCtrl instead of repeating the same code elsewhere. Note that another, but slightly different, version of RescaleBitmap() still remains in wxPropertyGrid. Deprecate undocumented wxArtProvider::RescaleBitmap() which is completely useless now. No real changes, this is just a refactoring. --- include/wx/artprov.h | 4 +++- include/wx/bitmap.h | 3 +++ interface/wx/bitmap.h | 10 ++++++++++ src/common/artprov.cpp | 27 ++++----------------------- src/common/bmpbase.cpp | 27 +++++++++++++++++++++++++++ src/generic/srchctlg.cpp | 32 +++----------------------------- src/msw/artmsw.cpp | 4 ++-- 7 files changed, 52 insertions(+), 55 deletions(-) diff --git a/include/wx/artprov.h b/include/wx/artprov.h index 6dd2850a82..85fb9f6565 100644 --- a/include/wx/artprov.h +++ b/include/wx/artprov.h @@ -189,8 +189,10 @@ public: // the topmost provider if platform_dependent = false static wxSize GetSizeHint(const wxArtClient& client, bool platform_dependent = false); - // Rescale bitmap (used internally if requested size is other than the available). +#if WXWIN_COMPATIBILITY_3_0 + wxDEPRECATED_MSG("use wxBitmap::Rescale() instead.") static void RescaleBitmap(wxBitmap& bmp, const wxSize& sizeNeeded); +#endif // WXWIN_COMPATIBILITY_3_0 protected: friend class wxArtProviderModule; diff --git a/include/wx/bitmap.h b/include/wx/bitmap.h index adb95b9c50..e4cb25302f 100644 --- a/include/wx/bitmap.h +++ b/include/wx/bitmap.h @@ -97,6 +97,9 @@ class WXDLLIMPEXP_CORE wxBitmapHelpers public: // Create a new wxBitmap from the PNG data in the given buffer. static wxBitmap NewFromPNGData(const void* data, size_t size); + + // Rescale the given bitmap to the requested size. + static void Rescale(wxBitmap& bmp, const wxSize& sizeNeeded); }; diff --git a/interface/wx/bitmap.h b/interface/wx/bitmap.h index f119444e05..68c92102ff 100644 --- a/interface/wx/bitmap.h +++ b/interface/wx/bitmap.h @@ -682,6 +682,16 @@ public: */ static bool RemoveHandler(const wxString& name); + /** + Rescale the given bitmap to the requested size. + + This function uses wxImage::Rescale() to resize the given @a bmp to the + requested size. Both the bitmap itself and size must be valid. + + @since 3.1.6 + */ + static void Rescale(wxBitmap& bmp, const wxSize& sizeNeeded); + /** Saves a bitmap in the named file. diff --git a/src/common/artprov.cpp b/src/common/artprov.cpp index 53faae0cbc..07d662dacc 100644 --- a/src/common/artprov.cpp +++ b/src/common/artprov.cpp @@ -209,31 +209,12 @@ wxArtProvider::~wxArtProvider() // wxArtProvider: retrieving bitmaps/icons // ---------------------------------------------------------------------------- +#if WXWIN_COMPATIBILITY_3_0 void wxArtProvider::RescaleBitmap(wxBitmap& bmp, const wxSize& sizeNeeded) { - wxCHECK_RET( sizeNeeded.IsFullySpecified(), wxS("New size must be given") ); - -#if wxUSE_IMAGE - wxImage img = bmp.ConvertToImage(); - img.Rescale(sizeNeeded.x, sizeNeeded.y); - bmp = wxBitmap(img); -#else // !wxUSE_IMAGE - // Fallback method of scaling the bitmap - wxBitmap newBmp(sizeNeeded, bmp.GetDepth()); -#if defined(__WXMSW__) || defined(__WXOSX__) - // wxBitmap::UseAlpha() is used only on wxMSW and wxOSX. - newBmp.UseAlpha(bmp.HasAlpha()); -#endif // __WXMSW__ || __WXOSX__ - { - wxMemoryDC dc(newBmp); - double scX = (double)sizeNeeded.GetWidth() / bmp.GetWidth(); - double scY = (double)sizeNeeded.GetHeight() / bmp.GetHeight(); - dc.SetUserScale(scX, scY); - dc.DrawBitmap(bmp, 0, 0); - } - bmp = newBmp; -#endif // wxUSE_IMAGE/!wxUSE_IMAGE + return wxBitmap::Rescale(bmp, sizeNeeded); } +#endif // WXWIN_COMPATIBILITY_3_0 /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id, const wxArtClient& client, @@ -283,7 +264,7 @@ void wxArtProvider::RescaleBitmap(wxBitmap& bmp, const wxSize& sizeNeeded) { if ( bmp.GetSize() != sizeNeeded ) { - RescaleBitmap(bmp, sizeNeeded); + wxBitmap::Rescale(bmp, sizeNeeded); } } diff --git a/src/common/bmpbase.cpp b/src/common/bmpbase.cpp index af0c415411..786f9c5e26 100644 --- a/src/common/bmpbase.cpp +++ b/src/common/bmpbase.cpp @@ -64,6 +64,33 @@ wxBitmap wxBitmapHelpers::NewFromPNGData(const void* data, size_t size) #endif // !__WXOSX__ +/* static */ +void wxBitmapHelpers::Rescale(wxBitmap& bmp, const wxSize& sizeNeeded) +{ + wxCHECK_RET( sizeNeeded.IsFullySpecified(), wxS("New size must be given") ); + +#if wxUSE_IMAGE + wxImage img = bmp.ConvertToImage(); + img.Rescale(sizeNeeded.x, sizeNeeded.y); + bmp = wxBitmap(img); +#else // !wxUSE_IMAGE + // Fallback method of scaling the bitmap + wxBitmap newBmp(sizeNeeded, bmp.GetDepth()); +#if defined(__WXMSW__) || defined(__WXOSX__) + // wxBitmap::UseAlpha() is used only on wxMSW and wxOSX. + newBmp.UseAlpha(bmp.HasAlpha()); +#endif // __WXMSW__ || __WXOSX__ + { + wxMemoryDC dc(newBmp); + double scX = (double)sizeNeeded.GetWidth() / bmp.GetWidth(); + double scY = (double)sizeNeeded.GetHeight() / bmp.GetHeight(); + dc.SetUserScale(scX, scY); + dc.DrawBitmap(bmp, 0, 0); + } + bmp = newBmp; +#endif // wxUSE_IMAGE/!wxUSE_IMAGE +} + // ---------------------------------------------------------------------------- // wxBitmapBase // ---------------------------------------------------------------------------- diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index 2f7e722b39..4aec60d510 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -937,32 +937,6 @@ static int GetMultiplier() return 6; } -static void RescaleBitmap(wxBitmap& bmp, const wxSize& sizeNeeded) -{ - wxCHECK_RET( sizeNeeded.IsFullySpecified(), wxS("New size must be given") ); - -#if wxUSE_IMAGE - wxImage img = bmp.ConvertToImage(); - img.Rescale(sizeNeeded.x, sizeNeeded.y); - bmp = wxBitmap(img); -#else // !wxUSE_IMAGE - // Fallback method of scaling the bitmap - wxBitmap newBmp(sizeNeeded, bmp.GetDepth()); -#if defined(__WXMSW__) || defined(__WXOSX__) - // wxBitmap::UseAlpha() is used only on wxMSW and wxOSX. - newBmp.UseAlpha(bmp.HasAlpha()); -#endif // __WXMSW__ || __WXOSX__ - { - wxMemoryDC dc(newBmp); - double scX = (double)sizeNeeded.GetWidth() / bmp.GetWidth(); - double scY = (double)sizeNeeded.GetHeight() / bmp.GetHeight(); - dc.SetUserScale(scX, scY); - dc.DrawBitmap(bmp, 0, 0); - } - bmp = newBmp; -#endif // wxUSE_IMAGE/!wxUSE_IMAGE -} - wxBitmap wxSearchCtrl::RenderSearchBitmap( int x, int y, bool renderDrop ) { wxColour bg = GetBackgroundColour(); @@ -1055,7 +1029,7 @@ wxBitmap wxSearchCtrl::RenderSearchBitmap( int x, int y, bool renderDrop ) if ( multiplier != 1 ) { - RescaleBitmap(bitmap, wxSize(x, y)); + wxBitmap::Rescale(bitmap, wxSize(x, y)); } if ( !renderDrop ) { @@ -1137,7 +1111,7 @@ wxBitmap wxSearchCtrl::RenderCancelBitmap( int x, int y ) }; mem.DrawPolygon(WXSIZEOF(handlePolygon2),handlePolygon2,multiplier*lineStartBase,multiplier*(x-lineStartBase)); - // Stop drawing on the bitmap before possibly calling RescaleBitmap() + // Stop drawing on the bitmap before possibly calling wxBitmap::Rescale() // below. mem.SelectObject(wxNullBitmap); @@ -1147,7 +1121,7 @@ wxBitmap wxSearchCtrl::RenderCancelBitmap( int x, int y ) if ( multiplier != 1 ) { - RescaleBitmap(bitmap, wxSize(x, y)); + wxBitmap::Rescale(bitmap, wxSize(x, y)); } return bitmap; diff --git a/src/msw/artmsw.cpp b/src/msw/artmsw.cpp index 4e08e4abd1..eb95c5e221 100644 --- a/src/msw/artmsw.cpp +++ b/src/msw/artmsw.cpp @@ -218,7 +218,7 @@ static wxBitmap CreateFromStdIcon(const char *iconName, const wxSize size = wxArtProvider::GetNativeSizeHint(client); if ( size != wxDefaultSize ) { - wxArtProvider::RescaleBitmap(bmp, size); + wxBitmap::Rescale(bmp, size); } } @@ -254,7 +254,7 @@ wxBitmap wxWindowsArtProvider::CreateBitmap(const wxArtID& id, { if ( bitmap.GetSize() != sizeNeeded ) { - wxArtProvider::RescaleBitmap(bitmap, sizeNeeded); + wxBitmap::Rescale(bitmap, sizeNeeded); } return bitmap;