From 94716fd801a96eb689d56b4f09eeeba5a652ce05 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 22 Jan 2022 22:32:40 +0000 Subject: [PATCH] Add wxBitmap::CreateWithLogicalSize() The new function has a more clear name than CreateScaled() it replaces and uses a more useful parameter order, with the scale factor, which must always be specified when using it, coming before, and not after, the depth, which almost never needs to be specified and so can be left at its default value in 99% of cases. --- include/wx/bitmap.h | 18 +++++++++++++--- include/wx/gtk/bitmap.h | 5 ++++- include/wx/msw/bitmap.h | 13 ++++++++++-- include/wx/osx/bitmap.h | 5 ++--- interface/wx/bitmap.h | 44 +++++++++++++++++++++++++++++++++------ src/common/bmpbase.cpp | 4 ++-- src/common/dcbufcmn.cpp | 2 +- src/gtk/bitmap.cpp | 4 ++-- src/msw/bitmap.cpp | 4 ++-- src/osx/cocoa/toolbar.mm | 2 +- src/osx/cocoa/utils.mm | 4 ++-- src/osx/core/bitmap.cpp | 9 ++++---- src/propgrid/propgrid.cpp | 4 ++-- src/stc/PlatWX.cpp | 2 +- 14 files changed, 88 insertions(+), 32 deletions(-) diff --git a/include/wx/bitmap.h b/include/wx/bitmap.h index 1fa6d5717f..74c23a818c 100644 --- a/include/wx/bitmap.h +++ b/include/wx/bitmap.h @@ -177,7 +177,15 @@ public: virtual bool Create(int width, int height, int depth = wxBITMAP_SCREEN_DEPTH) = 0; virtual bool Create(const wxSize& sz, int depth = wxBITMAP_SCREEN_DEPTH) = 0; - virtual bool CreateScaled(int w, int h, int d, double logicalScale); + + bool CreateWithLogicalSize(const wxSize& sz, + double scale, + int depth = wxBITMAP_SCREEN_DEPTH) + { return DoCreate(sz, scale, depth); } + bool CreateWithLogicalSize(int width, int height, + double scale, + int depth = wxBITMAP_SCREEN_DEPTH) + { return DoCreate(wxSize(width, height), scale, depth); } virtual int GetHeight() const = 0; virtual int GetWidth() const = 0; @@ -203,8 +211,10 @@ public: double GetLogicalHeight() const; wxSize GetLogicalSize() const; - // Old synonyms for GetLogicalXXX() functions, prefer the new names in the - // new code. + // Old synonyms for CreateWithLogicalSize() and GetLogicalXXX() functions, + // prefer the new names in the new code. + bool CreateScaled(int w, int h, int d, double logicalScale) + { return CreateWithLogicalSize(w, h, logicalScale, d); } double GetScaledWidth() const { return GetLogicalWidth(); } double GetScaledHeight() const { return GetLogicalHeight(); } wxSize GetScaledSize() const { return GetLogicalSize(); } @@ -273,6 +283,8 @@ public: } protected: + virtual bool DoCreate(const wxSize& sz, double scale, int depth); + static wxList sm_handlers; wxDECLARE_ABSTRACT_CLASS(wxBitmapBase); diff --git a/include/wx/gtk/bitmap.h b/include/wx/gtk/bitmap.h index 975dff950f..63283c2554 100644 --- a/include/wx/gtk/bitmap.h +++ b/include/wx/gtk/bitmap.h @@ -86,7 +86,6 @@ public: bool Create(int width, int height, const wxDC& WXUNUSED(dc)) { return Create(width,height); } #ifdef __WXGTK3__ - virtual bool CreateScaled(int w, int h, int depth, double scale) wxOVERRIDE; virtual void SetScaleFactor(double scale) wxOVERRIDE; virtual double GetScaleFactor() const wxOVERRIDE; #endif @@ -155,6 +154,10 @@ protected: virtual wxGDIRefData* CreateGDIRefData() const wxOVERRIDE; virtual wxGDIRefData* CloneGDIRefData(const wxGDIRefData* data) const wxOVERRIDE; +#ifdef __WXGTK3__ + virtual bool DoCreate(const wxSize& sz, double scale, int depth) wxOVERRIDE; +#endif + private: #ifndef __WXGTK3__ void SetPixmap(GdkPixmap* pixmap); diff --git a/include/wx/msw/bitmap.h b/include/wx/msw/bitmap.h index f117f85842..4c0eb8e341 100644 --- a/include/wx/msw/bitmap.h +++ b/include/wx/msw/bitmap.h @@ -158,7 +158,14 @@ public: virtual bool Create(int width, int height, const wxDC& dc); virtual bool Create(const void* data, wxBitmapType type, int width, int height, int depth = 1); - virtual bool CreateScaled(int w, int h, int d, double logicalScale); + + bool CreateWithLogicalSize(const wxSize& sz, + double scale, + int depth = wxBITMAP_SCREEN_DEPTH); + bool CreateWithLogicalSize(int width, int height, + double scale, + int depth = wxBITMAP_SCREEN_DEPTH) + { return CreateWithLogicalSize(wxSize(width, height), scale, depth); } virtual bool LoadFile(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE); virtual bool SaveFile(const wxString& name, wxBitmapType type, const wxPalette *cmap = NULL) const; @@ -197,7 +204,9 @@ public: double GetLogicalHeight() const; wxSize GetLogicalSize() const; - // old synonyms for GetLogicalXXX() functions + // old synonyms for CreateWithLogicalSize() and GetLogicalXXX() functions + bool CreateScaled(int w, int h, int d, double logicalScale) + { return CreateWithLogicalSize(wxSize(w, h), logicalScale, d); } double GetScaledWidth() const { return GetLogicalWidth(); } double GetScaledHeight() const { return GetLogicalHeight(); } wxSize GetScaledSize() const { return GetLogicalSize(); } diff --git a/include/wx/osx/bitmap.h b/include/wx/osx/bitmap.h index c8173abc59..cef7860f1c 100644 --- a/include/wx/osx/bitmap.h +++ b/include/wx/osx/bitmap.h @@ -150,9 +150,6 @@ public: // Create a bitmap compatible with the given DC, inheriting its magnification factor bool Create(int width, int height, const wxDC& dc); - // Create a bitmap with a scale factor, width and height are multiplied with that factor - bool CreateScaled(int logwidth, int logheight, int depth, double logicalScale) wxOVERRIDE; - // virtual bool Create( WXHICON icon) ; virtual bool LoadFile(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE) wxOVERRIDE; virtual bool SaveFile(const wxString& name, wxBitmapType type, const wxPalette *cmap = NULL) const wxOVERRIDE; @@ -245,6 +242,8 @@ public: protected: virtual wxGDIRefData *CreateGDIRefData() const wxOVERRIDE; virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const wxOVERRIDE; + + virtual bool DoCreate(const wxSize& sz, double scale, int depth) wxOVERRIDE; }; #endif // _WX_BITMAP_H_ diff --git a/interface/wx/bitmap.h b/interface/wx/bitmap.h index 42cf69b343..e9ffc3a4db 100644 --- a/interface/wx/bitmap.h +++ b/interface/wx/bitmap.h @@ -460,7 +460,39 @@ public: bool Create(int width, int height, const wxDC& dc); /** - Create a bitmap with a scale factor, width and height are multiplied with that factor + Create a bitmap specifying its size in logical pixels and the scale + factor to use. + + The physical size of the bitmap is obtained by multiplying the given + size in logical pixels by @a scale and rounding it to the closest + integer. + + @param size + The size of the bitmap in logical pixels. Both width and height + must be strictly positive. + @param scale + Scale factor used by the bitmap, see SetScaleFactor(). + @param depth + The number of bits used to represent each bitmap pixel. + + @return @true if the creation was successful. + + @since 3.1.6 + */ + bool CreateWithLogicalSize(const wxSize& size, + double scale, + int depth = wxBITMAP_SCREEN_DEPTH); + + /// @overload + bool CreateWithLogicalSize(int width, int height, + double scale, + int depth = wxBITMAP_SCREEN_DEPTH); + + /** + Create a bitmap with a scale factor. + + This is an older synonym for CreateWithLogicalSize(), use the new + function in the new code. @param width The width of the bitmap in pixels, must be strictly positive. @@ -545,9 +577,9 @@ public: Returns the size of bitmap in DPI-independent units. This assumes that the bitmap was created using the value of scale - factor corresponding to the current DPI (see CreateScaled() and - SetScaleFactor()) and returns its physical size divided by this scale - factor. + factor corresponding to the current DPI (see CreateWithLogicalSize() + and SetScaleFactor()) and returns its physical size divided by this + scale factor. Unlike GetLogicalSize(), this function returns the same value under all platforms and so its result should @e not be used as window or device @@ -863,8 +895,8 @@ public: which logical and physical pixels differ (i.e. wxOSX and wxGTK3, but not wxMSW). - When creating a new bitmap, CreateScaled() can be used to specify the - correct scale factor from the beginning. + When creating a new bitmap, CreateWithLogicalSize() can be used to + specify the correct scale factor from the beginning. @since 3.1.6 */ diff --git a/src/common/bmpbase.cpp b/src/common/bmpbase.cpp index 99f04459a5..d511ee5a04 100644 --- a/src/common/bmpbase.cpp +++ b/src/common/bmpbase.cpp @@ -206,9 +206,9 @@ bool wxBitmapBase::CopyFromIcon(const wxIcon& icon) // Trivial implementations of scale-factor related functions // ---------------------------------------------------------------------------- -bool wxBitmapBase::CreateScaled(int w, int h, int d, double logicalScale) +bool wxBitmapBase::DoCreate(const wxSize& sz, double scale, int depth) { - return Create(wxRound(w*logicalScale), wxRound(h*logicalScale), d); + return Create(sz*scale, depth); } void wxBitmapBase::SetScaleFactor(double WXUNUSED(scale)) diff --git a/src/common/dcbufcmn.cpp b/src/common/dcbufcmn.cpp index 50194afaf8..4f6e83c527 100644 --- a/src/common/dcbufcmn.cpp +++ b/src/common/dcbufcmn.cpp @@ -84,7 +84,7 @@ private: // we must always return a valid bitmap but creating a bitmap of // size 0 would fail, so create a 1*1 bitmap in this case - buffer->CreateScaled(wxMax(w, 1), wxMax(h, 1), -1, scale); + buffer->CreateWithLogicalSize(wxMax(w, 1), wxMax(h, 1), scale); return buffer; } diff --git a/src/gtk/bitmap.cpp b/src/gtk/bitmap.cpp index f929e78dc2..1578a5269e 100644 --- a/src/gtk/bitmap.cpp +++ b/src/gtk/bitmap.cpp @@ -990,9 +990,9 @@ void wxBitmap::SetMask( wxMask *mask ) } #ifdef __WXGTK3__ -bool wxBitmap::CreateScaled(int w, int h, int depth, double scale) +bool wxBitmap::DoCreate(const wxSize& size, double scale, int depth) { - Create(wxRound(w * scale), wxRound(h * scale), depth); + Create(size*scale, depth); M_BMPDATA->m_scaleFactor = scale; return true; } diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index 8cdd009d5d..2f7b045450 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -747,9 +747,9 @@ bool wxBitmap::Create(int width, int height, const wxDC& dc) return false; } -bool wxBitmap::CreateScaled(int w, int h, int d, double logicalScale) +bool wxBitmap::CreateWithLogicalSize(const wxSize& size, double scale, int depth) { - return Create(wxRound(w*logicalScale), wxRound(h*logicalScale), d); + return Create(size*scale, depth); } bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc) diff --git a/src/osx/cocoa/toolbar.mm b/src/osx/cocoa/toolbar.mm index 4be4fa3f36..7d684e9395 100644 --- a/src/osx/cocoa/toolbar.mm +++ b/src/osx/cocoa/toolbar.mm @@ -599,7 +599,7 @@ void wxToolBarTool::UpdateImages() // TODO CS this should use the best current representation, or optionally iterate through all wxSize sz = m_bmpNormal.GetDefaultSize(); m_alternateBitmap = wxBitmap(); - m_alternateBitmap.Create(sz.x, sz.y, -1); // TODO CS m_alternateBitmap.CreateScaled(sz.x, sz.y, -1, m_bmpNormal.GetScaleFactor()); + m_alternateBitmap.Create(sz.x, sz.y, -1); // TODO CS m_alternateBitmap.CreateWithLogicalSize(sz, m_bmpNormal.GetScaleFactor()); m_alternateBitmap.UseAlpha(); wxMemoryDC dc; diff --git a/src/osx/cocoa/utils.mm b/src/osx/cocoa/utils.mm index 792971c816..e7de491337 100644 --- a/src/osx/cocoa/utils.mm +++ b/src/osx/cocoa/utils.mm @@ -559,7 +559,7 @@ wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const const wxSize bitmapSize(subrect ? subrect->GetSize() : m_window->GetSize()); wxBitmap bitmap; - bitmap.CreateScaled(bitmapSize.x, bitmapSize.y, -1, m_contentScaleFactor); + bitmap.CreateWithLogicalSize(bitmapSize, m_contentScaleFactor); NSView* view = (NSView*) m_window->GetHandle(); if ( [view isHiddenOrHasHiddenAncestor] == NO ) @@ -591,7 +591,7 @@ wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const CGRect r = CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef) , CGImageGetHeight(cgImageRef) ); - // The bitmap created by wxBitmap::CreateScaled() above is scaled, + // The bitmap created by wxBitmap::CreateWithLogicalSize() above is scaled, // so we need to adjust the coordinates for it. r.size.width /= m_contentScaleFactor; r.size.height /= m_contentScaleFactor; diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index ec597f3f13..3dc697c5c9 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -961,7 +961,7 @@ wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const wxBitmap ret; double scale = GetScaleFactor(); - ret.CreateScaled( rect.width, rect.height, GetDepth(), scale ); + ret.CreateWithLogicalSize( rect.GetSize(), scale, GetDepth() ); wxASSERT_MSG( ret.IsOk(), wxT("GetSubBitmap error") ); if ( HasAlpha() ) ret.UseAlpha() ; @@ -1039,17 +1039,18 @@ bool wxBitmap::Create(int w, int h, int d) bool wxBitmap::Create(int w, int h, const wxDC& dc) { double factor = dc.GetContentScaleFactor(); - return CreateScaled(w,h,wxBITMAP_SCREEN_DEPTH, factor); + return CreateWithLogicalSize(w, h, factor); } -bool wxBitmap::CreateScaled(int w, int h, int d, double logicalScale) +bool wxBitmap::DoCreate(const wxSize& size, double scale, int d) { UnRef(); if ( d < 0 ) d = wxDisplayDepth() ; - m_refData = new wxBitmapRefData( w*logicalScale , h*logicalScale , d, logicalScale ); + const wxSize sizePhys = size*scale; + m_refData = new wxBitmapRefData( sizePhys.x, sizePhys.y, d ); return GetBitmapData()->IsOk() ; } diff --git a/src/propgrid/propgrid.cpp b/src/propgrid/propgrid.cpp index c27ed64c16..b784410902 100644 --- a/src/propgrid/propgrid.cpp +++ b/src/propgrid/propgrid.cpp @@ -4621,7 +4621,7 @@ void wxPropertyGrid::OnResize( wxSizeEvent& event ) int w = wxMax(width, 250); int h = wxMax(height + dblh, 400); m_doubleBuffer = new wxBitmap; - m_doubleBuffer->CreateScaled( w, h, wxBITMAP_SCREEN_DEPTH, scaleFactor ); + m_doubleBuffer->CreateWithLogicalSize( w, h, scaleFactor ); } else { @@ -4635,7 +4635,7 @@ void wxPropertyGrid::OnResize( wxSizeEvent& event ) if ( h < (height+dblh) ) h = height + dblh; delete m_doubleBuffer; m_doubleBuffer = new wxBitmap; - m_doubleBuffer->CreateScaled( w, h, wxBITMAP_SCREEN_DEPTH, scaleFactor ); + m_doubleBuffer->CreateWithLogicalSize( w, h, scaleFactor ); } } } diff --git a/src/stc/PlatWX.cpp b/src/stc/PlatWX.cpp index 88bff830e1..44067dba6a 100644 --- a/src/stc/PlatWX.cpp +++ b/src/stc/PlatWX.cpp @@ -299,7 +299,7 @@ void SurfaceImpl::InitPixMap(int width, int height, Surface *surface, WindowID w if (width < 1) width = 1; if (height < 1) height = 1; bitmap = new wxBitmap(); - bitmap->CreateScaled(width, height,wxBITMAP_SCREEN_DEPTH,(GETWIN(winid))->GetContentScaleFactor()); + bitmap->CreateWithLogicalSize(width, height,(GETWIN(winid))->GetContentScaleFactor()); mdc->SelectObject(*bitmap); }