Add wxBitmap::SetScaleFactor()

We need to be able to change the scale factor of the bitmaps returned by
wxBitmapBundle::GetBitmap(), so add a function allowing to do this.

Also add wxHAS_BITMAP_SCALE_FACTOR allowing to check whether this
function actually does something non-trivial and explain in the docs
that GetScaleFactor() always returns 1 on the platforms where this
symbol is not defined.
This commit is contained in:
Vadim Zeitlin
2021-10-23 21:18:37 +02:00
parent c8f76dea8e
commit 2c1f4c002d
8 changed files with 66 additions and 4 deletions

View File

@@ -167,6 +167,9 @@ Currently the following symbols exist:
have an efficient (CPU-specific) implementation. Notice that the functions have an efficient (CPU-specific) implementation. Notice that the functions
themselves are always available but can be prohibitively slow to use when themselves are always available but can be prohibitively slow to use when
implemented in a generic way, using a critical section.} implemented in a generic way, using a critical section.}
@itemdef{wxHAS_BITMAP_SCALE_FACTOR, Defined in @c wx/bitmap.h if bitmaps
actually use scale factor under the current platform, see
wxBitmap::SetScaleFactor().}
@itemdef{wxHAS_BITMAPTOGGLEBUTTON, Defined in @c wx/tglbtn.h if @itemdef{wxHAS_BITMAPTOGGLEBUTTON, Defined in @c wx/tglbtn.h if
wxBitmapToggleButton class is available in addition to wxToggleButton.} wxBitmapToggleButton class is available in addition to wxToggleButton.}
@itemdef{wxHAS_CONFIG_TEMPLATE_RW, Defined if the currently used compiler @itemdef{wxHAS_CONFIG_TEMPLATE_RW, Defined if the currently used compiler

View File

@@ -188,6 +188,7 @@ public:
{ return wxSize(GetWidth(), GetHeight()); } { return wxSize(GetWidth(), GetHeight()); }
// support for scaled bitmaps // support for scaled bitmaps
virtual void SetScaleFactor(double WXUNUSED(scale)) { }
virtual double GetScaleFactor() const { return 1.0; } virtual double GetScaleFactor() const { return 1.0; }
virtual double GetScaledWidth() const { return GetWidth() / GetScaleFactor(); } virtual double GetScaledWidth() const { return GetWidth() / GetScaleFactor(); }
virtual double GetScaledHeight() const { return GetHeight() / GetScaleFactor(); } virtual double GetScaledHeight() const { return GetHeight() / GetScaleFactor(); }

View File

@@ -17,6 +17,10 @@ typedef struct _GdkPixbuf GdkPixbuf;
class WXDLLIMPEXP_FWD_CORE wxPixelDataBase; class WXDLLIMPEXP_FWD_CORE wxPixelDataBase;
class WXDLLIMPEXP_FWD_CORE wxCursor; class WXDLLIMPEXP_FWD_CORE wxCursor;
#ifdef __WXGTK3__
#define wxHAS_BITMAP_SCALE_FACTOR
#endif
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// wxMask // wxMask
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -85,8 +89,9 @@ public:
{ return Create(sz.GetWidth(), sz.GetHeight(), depth); } { return Create(sz.GetWidth(), sz.GetHeight(), depth); }
bool Create(int width, int height, const wxDC& WXUNUSED(dc)) bool Create(int width, int height, const wxDC& WXUNUSED(dc))
{ return Create(width,height); } { return Create(width,height); }
#ifdef __WXGTK3__ #ifdef wxHAS_BITMAP_SCALE_FACTOR
virtual bool CreateScaled(int w, int h, int depth, double scale) wxOVERRIDE; virtual bool CreateScaled(int w, int h, int depth, double scale) wxOVERRIDE;
virtual void SetScaleFactor(double scale) wxOVERRIDE;
virtual double GetScaleFactor() const wxOVERRIDE; virtual double GetScaleFactor() const wxOVERRIDE;
#endif #endif

View File

@@ -186,6 +186,7 @@ public:
void ResetAlpha() { UseAlpha(false); } void ResetAlpha() { UseAlpha(false); }
// support for scaled bitmaps // support for scaled bitmaps
virtual void SetScaleFactor(double WXUNUSED(scale)) { }
virtual double GetScaleFactor() const { return 1.0; } virtual double GetScaleFactor() const { return 1.0; }
virtual double GetScaledWidth() const { return GetWidth(); } virtual double GetScaledWidth() const { return GetWidth(); }
virtual double GetScaledHeight() const { return GetHeight(); } virtual double GetScaledHeight() const { return GetHeight(); }

View File

@@ -13,6 +13,8 @@
#include "wx/palette.h" #include "wx/palette.h"
#define wxHAS_BITMAP_SCALE_FACTOR
// Bitmap // Bitmap
class WXDLLIMPEXP_FWD_CORE wxBitmap; class WXDLLIMPEXP_FWD_CORE wxBitmap;
class wxBitmapRefData ; class wxBitmapRefData ;
@@ -239,6 +241,7 @@ public:
void EndRawAccess(); void EndRawAccess();
#endif #endif
void SetScaleFactor(double scale) wxOVERRIDE;
double GetScaleFactor() const wxOVERRIDE; double GetScaleFactor() const wxOVERRIDE;
void SetSelectedInto(wxDC *dc); void SetSelectedInto(wxDC *dc);

View File

@@ -469,7 +469,7 @@ public:
@param depth @param depth
The number of bits used to represent each bitmap pixel. The number of bits used to represent each bitmap pixel.
@param logicalScale @param logicalScale
Scale factor used by the bitmap Scale factor used by the bitmap, see SetScaleFactor().
@return @true if the creation was successful. @return @true if the creation was successful.
@@ -586,9 +586,10 @@ public:
1 must be used in high DPI to appear sharp on the screen. 1 must be used in high DPI to appear sharp on the screen.
Note that the scale factor is only used in the ports where logical Note that the scale factor is only used in the ports where logical
pixels are not the same as physical ones, such as wxOSX or wxGTK3. pixels are not the same as physical ones, such as wxOSX or wxGTK3, and
this function always returns 1 under the other platforms.
@see GetScaledWidth(), GetScaledHeight(), GetScaledSize() @see SetScaleFactor(), GetScaledWidth(), GetScaledHeight(), GetScaledSize()
@since 2.9.5 @since 2.9.5
*/ */
@@ -796,6 +797,38 @@ public:
*/ */
virtual void SetHeight(int height); virtual void SetHeight(int height);
/**
Sets the bitmap scale factor.
This doesn't change the bitmap actual size or its contents, but changes
its scale factor, so that it appears in a smaller size when it is drawn
on screen: e.g. setting @a scale to 2 means that the bitmap will be
twice smaller (in each direction) when drawn on screen in the ports in
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.
Note that this method exists in all ports, but simply does nothing in
those of them that don't use logical pixel scaling. The preprocessor
symbol @c wxHAS_BITMAP_SCALE_FACTOR can be tested to determine whether
the scale factor is really supported, e.g.
@code
bitmap.SetScaleFactor(2);
// In the other ports scale factor is always 1, so the assert would
// fail there.
#ifdef wxHAS_BITMAP_SCALE_FACTOR
wxASSERT( bitmap.GetScaleFactor() == 2 );
#endif
@endcode
@since 3.1.6
*/
virtual void SetScaleFactor(double scale);
/** /**
Sets the mask for this bitmap. Sets the mask for this bitmap.

View File

@@ -1002,6 +1002,13 @@ bool wxBitmap::CreateScaled(int w, int h, int depth, double scale)
return true; return true;
} }
void wxBitmap::SetScaleFactor(double scale)
{
wxCHECK_RET(m_refData, "invalid bitmap");
M_BMPDATA->m_scaleFactor = scale;
}
double wxBitmap::GetScaleFactor() const double wxBitmap::GetScaleFactor() const
{ {
wxCHECK_MSG(m_refData, -1, "invalid bitmap"); wxCHECK_MSG(m_refData, -1, "invalid bitmap");

View File

@@ -75,6 +75,8 @@ public:
int GetBytesPerRow() const; int GetBytesPerRow() const;
bool HasAlpha() const; bool HasAlpha() const;
WXImage GetImage() const; WXImage GetImage() const;
void SetScaleFactor(double scale) { m_scaleFactor = scale; }
double GetScaleFactor() const { return m_scaleFactor; } double GetScaleFactor() const { return m_scaleFactor; }
const void *GetRawAccess() const; const void *GetRawAccess() const;
@@ -1389,6 +1391,13 @@ int wxBitmap::GetWidth() const
return GetBitmapData()->GetWidth() ; return GetBitmapData()->GetWidth() ;
} }
void wxBitmap::SetScaleFactor(double scale)
{
wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
return GetBitmapData()->SetScaleFactor(scale) ;
}
double wxBitmap::GetScaleFactor() const double wxBitmap::GetScaleFactor() const
{ {
wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );