From 244ec264af625d992e09e13dcb93b91147fccf74 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Fri, 7 Aug 2015 23:40:40 +0200 Subject: [PATCH] Factor out duplicated code for bitmap scaling in wxArtProvider. Move duplicated code responsible for rescaling bitmaps in wxArtProvider to the shared wxArtPrvider::RescaleBitmap() method. Additionally, make this new method work even in wxUSE_IMAGE==0 case. See #17071. See https://github.com/wxWidgets/wxWidgets/pull/64. --- include/wx/artprov.h | 3 +++ src/common/artprov.cpp | 29 ++++++++++++++++++++++++----- src/generic/datavgen.cpp | 2 +- src/msw/artmsw.cpp | 6 +----- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/include/wx/artprov.h b/include/wx/artprov.h index 35707e5494..14ba530417 100644 --- a/include/wx/artprov.h +++ b/include/wx/artprov.h @@ -188,6 +188,9 @@ 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). + static void RescaleBitmap(wxBitmap& bmp, const wxSize& sizeNeeded); + protected: friend class wxArtProviderModule; #if wxUSE_ARTPROVIDER_STD diff --git a/src/common/artprov.cpp b/src/common/artprov.cpp index e6d3736417..7ff0a2785e 100644 --- a/src/common/artprov.cpp +++ b/src/common/artprov.cpp @@ -212,6 +212,29 @@ wxArtProvider::~wxArtProvider() // wxArtProvider: retrieving bitmaps/icons // ---------------------------------------------------------------------------- +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); + newBmp.UseAlpha(bmp.HasAlpha()); + { + 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 +} + /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id, const wxArtClient& client, const wxSize& size) @@ -256,17 +279,13 @@ wxArtProvider::~wxArtProvider() } // if we didn't get the correct size, resize the bitmap -#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB) if ( bmp.IsOk() && sizeNeeded != wxDefaultSize ) { if ( bmp.GetSize() != sizeNeeded ) { - wxImage img = bmp.ConvertToImage(); - img.Rescale(sizeNeeded.x, sizeNeeded.y); - bmp = wxBitmap(img); + RescaleBitmap(bmp, sizeNeeded); } } -#endif // wxUSE_IMAGE sm_cache->PutBitmap(hashId, bmp); } diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index f1d58bdc65..c5bb710690 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -570,7 +570,7 @@ public: void Resort(); private: - wxDataViewMainWindow *m_window; + wxDataViewMainWindow * const m_window; wxDataViewTreeNode *m_parent; // Corresponding model item. diff --git a/src/msw/artmsw.cpp b/src/msw/artmsw.cpp index 404648effd..9d8935f49c 100644 --- a/src/msw/artmsw.cpp +++ b/src/msw/artmsw.cpp @@ -150,7 +150,6 @@ static wxBitmap CreateFromStdIcon(const char *iconName, wxBitmap bmp; bmp.CopyFromIcon(icon); -#if wxUSE_IMAGE // The standard native message box icons are in message box size (32x32). // If they are requested in any size other than the default or message // box size, they must be rescaled first. @@ -159,12 +158,9 @@ static wxBitmap CreateFromStdIcon(const char *iconName, const wxSize size = wxArtProvider::GetNativeSizeHint(client); if ( size != wxDefaultSize ) { - wxImage img = bmp.ConvertToImage(); - img.Rescale(size.x, size.y); - bmp = wxBitmap(img); + wxArtProvider::RescaleBitmap(bmp, size); } } -#endif // wxUSE_IMAGE return bmp; }