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.
This commit is contained in:
Vadim Zeitlin 2022-01-22 22:32:40 +00:00
parent dad828da38
commit 94716fd801
14 changed files with 88 additions and 32 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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(); }

View File

@ -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_

View File

@ -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
*/

View File

@ -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))

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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() ;
}

View File

@ -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 );
}
}
}

View File

@ -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);
}