From 24970061faee1fec2f11f1d8895ccc3ea9a7563b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 12 Apr 2022 17:44:59 +0100 Subject: [PATCH] Add wxBitmap(wxImage, wxDC) ctor to all ports This ctor was previously present only in wxMSW, make it available in all ports to allow the same code to compile everywhere. In most of them wxDC argument is simply ignored, but in wxGTK and wxOSX it is used to assign the appropriate scale factor for the new bitmap. Enable previously wxMSW-only unit test checking for this. --- include/wx/dfb/bitmap.h | 7 ++++++- include/wx/gtk/bitmap.h | 6 ++++-- include/wx/gtk1/bitmap.h | 1 + include/wx/osx/bitmap.h | 4 ++++ include/wx/qt/bitmap.h | 4 ++++ include/wx/x11/bitmap.h | 1 + interface/wx/bitmap.h | 17 +++++++++++++++++ src/dfb/bitmap.cpp | 2 +- src/gtk/bitmap.cpp | 19 +++++++++++++------ src/osx/core/bitmap.cpp | 12 +++++++++++- src/qt/bitmap.cpp | 13 ++++++++++++- tests/graphics/bitmap.cpp | 5 ----- 12 files changed, 74 insertions(+), 17 deletions(-) diff --git a/include/wx/dfb/bitmap.h b/include/wx/dfb/bitmap.h index e2d44768b2..c3967b168a 100644 --- a/include/wx/dfb/bitmap.h +++ b/include/wx/dfb/bitmap.h @@ -32,7 +32,10 @@ public: wxBitmap(const wxString &filename, wxBitmapType type = wxBITMAP_DEFAULT_TYPE); wxBitmap(const char* const* bits); #if wxUSE_IMAGE - wxBitmap(const wxImage& image, int depth = -1, double WXUNUSED(scale) = 1.0); + wxBitmap(const wxImage& image, int depth = -1, double scale = 1.0) + { InitFromImage(image, depth, scale); } + wxBitmap(const wxImage& image, const wxDC& WXUNUSED(dc)) + { InitFromImage(image, -1, 1.0); } #endif bool Create(const wxIDirectFBSurfacePtr& surface); @@ -86,6 +89,8 @@ protected: virtual wxGDIRefData *CreateGDIRefData() const; virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const; + void InitFromImage(const wxImage& image, int depth, double scale); + bool CreateWithFormat(int width, int height, int dfbFormat); wxDECLARE_DYNAMIC_CLASS(wxBitmap); diff --git a/include/wx/gtk/bitmap.h b/include/wx/gtk/bitmap.h index cb9e65e66c..e53186dc16 100644 --- a/include/wx/gtk/bitmap.h +++ b/include/wx/gtk/bitmap.h @@ -75,6 +75,7 @@ public: wxBitmap( const wxString &filename, wxBitmapType type = wxBITMAP_DEFAULT_TYPE ); #if wxUSE_IMAGE wxBitmap(const wxImage& image, int depth = wxBITMAP_SCREEN_DEPTH, double scale = 1.0); + wxBitmap(const wxImage& image, const wxDC& dc); #endif // wxUSE_IMAGE wxBitmap(GdkPixbuf* pixbuf, int depth = 0); explicit wxBitmap(const wxCursor& cursor); @@ -147,11 +148,12 @@ public: bool HasAlpha() const; protected: -#ifndef __WXGTK3__ #if wxUSE_IMAGE + void InitFromImage(const wxImage& image, int depth, double scale); +#ifndef __WXGTK3__ bool CreateFromImage(const wxImage& image, int depth); -#endif // wxUSE_IMAGE #endif +#endif // wxUSE_IMAGE virtual wxGDIRefData* CreateGDIRefData() const wxOVERRIDE; virtual wxGDIRefData* CloneGDIRefData(const wxGDIRefData* data) const wxOVERRIDE; diff --git a/include/wx/gtk1/bitmap.h b/include/wx/gtk1/bitmap.h index 313e68c012..8c3111d588 100644 --- a/include/wx/gtk1/bitmap.h +++ b/include/wx/gtk1/bitmap.h @@ -69,6 +69,7 @@ public: wxBitmap( const char* const* bits ); wxBitmap( const wxString &filename, wxBitmapType type = wxBITMAP_DEFAULT_TYPE ); wxBitmap( const wxImage& image, int depth = -1, double WXUNUSED(scale) = 1.0 ) { (void)CreateFromImage(image, depth); } + wxBitmap( const wxImage& image, const wxDC& WXUNUSED(dc) ) { (void)CreateFromImage(image); } explicit wxBitmap(const wxCursor& cursor); virtual ~wxBitmap(); diff --git a/include/wx/osx/bitmap.h b/include/wx/osx/bitmap.h index 7db0b97bc3..59008b119f 100644 --- a/include/wx/osx/bitmap.h +++ b/include/wx/osx/bitmap.h @@ -114,6 +114,7 @@ public: // Convert from wxImage: wxBitmap(const wxImage& image, int depth = -1, double scale = 1.0); + wxBitmap(const wxImage& image, const wxDC& dc); // Convert from wxIcon wxBitmap(const wxIcon& icon) { CopyFromIcon(icon); } @@ -236,6 +237,9 @@ protected: virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const wxOVERRIDE; virtual bool DoCreate(const wxSize& sz, double scale, int depth) wxOVERRIDE; + +private: + void InitFromImage(const wxImage& image, int depth, double scale); }; #endif // _WX_BITMAP_H_ diff --git a/include/wx/qt/bitmap.h b/include/wx/qt/bitmap.h index 2e1315a8fe..11c4ec81e9 100644 --- a/include/wx/qt/bitmap.h +++ b/include/wx/qt/bitmap.h @@ -24,6 +24,7 @@ public: wxBitmap(const char* const* bits); wxBitmap(const wxString &filename, wxBitmapType type = wxBITMAP_TYPE_XPM); wxBitmap(const wxImage& image, int depth = wxBITMAP_SCREEN_DEPTH, double scale = 1.0); + wxBitmap(const wxImage& image, const wxDC& dc); // Convert from wxIcon / wxCursor wxBitmap(const wxIcon& icon) { CopyFromIcon(icon); } @@ -77,6 +78,9 @@ protected: virtual wxGDIRefData *CreateGDIRefData() const wxOVERRIDE; virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const wxOVERRIDE; +private: + void InitFromImage(const wxImage& image, int depth, double WXUNUSED(scale)); + wxDECLARE_DYNAMIC_CLASS(wxBitmap); }; diff --git a/include/wx/x11/bitmap.h b/include/wx/x11/bitmap.h index 9f46c9cbd4..c7cf0616ff 100644 --- a/include/wx/x11/bitmap.h +++ b/include/wx/x11/bitmap.h @@ -84,6 +84,7 @@ public: #if wxUSE_IMAGE wxBitmap( const wxImage& image, int depth = -1, double WXUNUSED(scale) = 1.0 ) { (void)CreateFromImage(image, depth); } + wxBitmap( const wxImage& image, const wxDC& WXUNUSED(dc) ) { (void)CreateFromImage(image); } wxImage ConvertToImage() const; bool CreateFromImage(const wxImage& image, int depth = -1); #endif // wxUSE_IMAGE diff --git a/interface/wx/bitmap.h b/interface/wx/bitmap.h index 9f38ccff5d..0faae1d891 100644 --- a/interface/wx/bitmap.h +++ b/interface/wx/bitmap.h @@ -367,6 +367,23 @@ public: */ wxBitmap(const wxImage& img, int depth = wxBITMAP_SCREEN_DEPTH); + /** + Creates a bitmap compatible with the given DC from the given image. + + This constructor initializes the bitmap with the data of the given + image, which must be valid, but inherits the scaling factor from the + given device context instead of simply using the default factor of 1. + + @param img + Platform-independent wxImage object. + @param dc + DC from which the scaling factor is inherited + + @since 3.1.7 (previously this constructor overload was only available + in wxMSW port) + */ + wxBitmap(const wxImage& img, const wxDC& dc); + /** Creates bitmap corresponding to the given cursor. diff --git a/src/dfb/bitmap.cpp b/src/dfb/bitmap.cpp index 98a0167eb4..47ed640950 100644 --- a/src/dfb/bitmap.cpp +++ b/src/dfb/bitmap.cpp @@ -417,7 +417,7 @@ bool wxBitmap::CreateWithFormat(int width, int height, int dfbFormat) } #if wxUSE_IMAGE -wxBitmap::wxBitmap(const wxImage& imageOrig, int depth, double WXUNUSED(scale)) +void wxBitmap::InitFromImage(const wxImage& imageOrig, int depth, double WXUNUSED(scale)) { wxCHECK_RET( imageOrig.IsOk(), wxT("invalid image") ); diff --git a/src/gtk/bitmap.cpp b/src/gtk/bitmap.cpp index 095b09b78c..7cb3e7bb63 100644 --- a/src/gtk/bitmap.cpp +++ b/src/gtk/bitmap.cpp @@ -16,15 +16,12 @@ #include "wx/image.h" #include "wx/colour.h" #include "wx/cursor.h" + #include "wx/dc.h" #endif #include "wx/math.h" #include "wx/rawbmp.h" -#ifdef __WXGTK3__ -#include "wx/dc.h" -#endif - #include "wx/gtk/private/object.h" #include "wx/gtk/private.h" @@ -593,7 +590,7 @@ static void CopyImageData( #if wxUSE_IMAGE #ifdef __WXGTK3__ -wxBitmap::wxBitmap(const wxImage& image, int depth, double scale) +void wxBitmap::InitFromImage(const wxImage& image, int depth, double scale) { wxCHECK_RET(image.IsOk(), "invalid image"); @@ -640,7 +637,7 @@ wxBitmap::wxBitmap(const wxImage& image, int depth, double scale) } } #else -wxBitmap::wxBitmap(const wxImage& image, int depth, double WXUNUSED(scale)) +void wxBitmap::InitFromImage(const wxImage& image, int depth, double WXUNUSED(scale)) { wxCHECK_RET(image.IsOk(), "invalid image"); @@ -786,6 +783,16 @@ bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image) } #endif +wxBitmap::wxBitmap(const wxImage& image, int depth, double scale) +{ + InitFromImage(image, depth, scale); +} + +wxBitmap::wxBitmap(const wxImage& image, const wxDC& dc) +{ + InitFromImage(image, -1, dc.GetContentScaleFactor()); +} + wxImage wxBitmap::ConvertToImage() const { #ifdef __WXGTK3__ diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index 9ffe066f9c..0f8824d81a 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -1134,7 +1134,7 @@ bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height #if wxUSE_IMAGE -wxBitmap::wxBitmap(const wxImage& image, int depth, double scale) +void wxBitmap::InitFromImage(const wxImage& image, int depth, double scale) { wxCHECK_RET( image.IsOk(), wxT("invalid image") ); @@ -1235,6 +1235,16 @@ wxBitmap::wxBitmap(const wxImage& image, int depth, double scale) } } +wxBitmap::wxBitmap(const wxImage& image, int depth, double scale) +{ + InitFromImage(image, depth, scale); +} + +wxBitmap::wxBitmap(const wxImage& image, const wxDC& dc) +{ + InitFromImage(image, -1, dc.GetContentScaleFactor()); +} + wxImage wxBitmap::ConvertToImage() const { wxImage image; diff --git a/src/qt/bitmap.cpp b/src/qt/bitmap.cpp index 770c20c148..46e7707036 100644 --- a/src/qt/bitmap.cpp +++ b/src/qt/bitmap.cpp @@ -14,6 +14,7 @@ #include #ifndef WX_PRECOMP + #include "wx/dc.h" #include "wx/icon.h" #include "wx/image.h" #endif // WX_PRECOMP @@ -209,7 +210,7 @@ wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type ) LoadFile(filename, type); } -wxBitmap::wxBitmap(const wxImage& image, int depth, double WXUNUSED(scale) ) +void wxBitmap::InitFromImage(const wxImage& image, int depth, double WXUNUSED(scale) ) { Qt::ImageConversionFlags flags = 0; if (depth == 1) @@ -217,6 +218,16 @@ wxBitmap::wxBitmap(const wxImage& image, int depth, double WXUNUSED(scale) ) m_refData = new wxBitmapRefData(QPixmap::fromImage(ConvertImage(image), flags)); } +wxBitmap::wxBitmap(const wxImage& image, int depth, double scale) +{ + InitFromImage(image, depth, scale); +} + +wxBitmap::wxBitmap(const wxImage& image, const wxDC& dc) +{ + InitFromImage(image, -1, dc.GetContentScaleFactor()); +} + wxBitmap::wxBitmap(const wxCursor& cursor) { // note that pixmap could be invalid if is not a pixmap cursor diff --git a/tests/graphics/bitmap.cpp b/tests/graphics/bitmap.cpp index e2a50281b6..dde38ad8c4 100644 --- a/tests/graphics/bitmap.cpp +++ b/tests/graphics/bitmap.cpp @@ -1699,12 +1699,9 @@ TEST_CASE("Bitmap::DC", "[bitmap][dc]") wxBitmap bmp(10, 10, dc); CHECK( bmp.IsOk() ); - // wxBitmap ctor from wxImage and wxDC is not available in the other ports. -#ifdef __WXMSW__ wxImage image(10, 10); wxBitmap bmpFromImage(image, dc); CHECK( bmpFromImage.IsOk() ); -#endif // __WXMSW__ #endif // wxUSE_SVG } @@ -1727,7 +1724,6 @@ TEST_CASE("Bitmap::ScaleFactor", "[bitmap][dc][scale]") CHECK( bmp2.GetScaleFactor() == 2 ); CHECK( bmp2.GetSize() == wxSize(8, 8) ); -#ifdef __WXMSW__ // A compatible bitmap created from wxImage and this DC should also inherit // the same scale factor, but its size should be still the same as that of // the image. @@ -1735,7 +1731,6 @@ TEST_CASE("Bitmap::ScaleFactor", "[bitmap][dc][scale]") wxBitmap bmp3(img, dc); CHECK( bmp3.GetScaleFactor() == 2 ); CHECK( bmp3.GetSize() == wxSize(16, 16) ); -#endif // __WXMSW__ } #endif // ports with scaled bitmaps support