Add GetDPIScaleFactor to wxDC and wxGraphicsContext

It can be used to scale coordinates and sizes to make them DPI aware.

In the common headers, use wxDisplay::GetStdPPIValue() as default DPI value,
so the correct values are used on Windows (96) and macOS (72).

In wxMemoryDC use m_contentScaleFactor as the DPIScaleFactor, see e4c2298e5e
(Use window scale factor for all MSW wxDCs associated with windows, 2022-04-04).
This commit is contained in:
Maarten Bent
2022-04-16 00:31:10 +02:00
parent d72a3f7cec
commit ee2b02614e
9 changed files with 39 additions and 6 deletions

View File

@@ -539,6 +539,8 @@ public:
virtual double GetContentScaleFactor() const { return m_contentScaleFactor; } virtual double GetContentScaleFactor() const { return m_contentScaleFactor; }
virtual double GetDPIScaleFactor() const { return 1.0; }
#ifdef __WXMSW__ #ifdef __WXMSW__
// Native Windows functions using the underlying HDC don't honour GDI+ // Native Windows functions using the underlying HDC don't honour GDI+
// transformations which may be applied to it. Using this function we can // transformations which may be applied to it. Using this function we can
@@ -827,6 +829,9 @@ public:
double GetContentScaleFactor() const double GetContentScaleFactor() const
{ return m_pimpl->GetContentScaleFactor(); } { return m_pimpl->GetContentScaleFactor(); }
double GetDPIScaleFactor() const
{ return m_pimpl->GetDPIScaleFactor(); }
// Right-To-Left (RTL) modes // Right-To-Left (RTL) modes
void SetLayoutDirection(wxLayoutDirection dir) void SetLayoutDirection(wxLayoutDirection dir)

View File

@@ -101,6 +101,7 @@ public:
virtual bool CanGetTextExtent() const wxOVERRIDE; virtual bool CanGetTextExtent() const wxOVERRIDE;
virtual int GetDepth() const wxOVERRIDE; virtual int GetDepth() const wxOVERRIDE;
virtual wxSize GetPPI() const wxOVERRIDE; virtual wxSize GetPPI() const wxOVERRIDE;
virtual double GetDPIScaleFactor() const wxOVERRIDE;
virtual void SetLogicalFunction(wxRasterOperationMode function) wxOVERRIDE; virtual void SetLogicalFunction(wxRasterOperationMode function) wxOVERRIDE;

View File

@@ -880,6 +880,7 @@ public:
void SetContentScaleFactor(double contentScaleFactor); void SetContentScaleFactor(double contentScaleFactor);
double GetContentScaleFactor() const { return m_contentScaleFactor; } double GetContentScaleFactor() const { return m_contentScaleFactor; }
double GetDPIScaleFactor() const;
#ifdef __WXMSW__ #ifdef __WXMSW__
virtual WXHDC GetNativeHDC() = 0; virtual WXHDC GetNativeHDC() = 0;

View File

@@ -77,7 +77,7 @@ public:
virtual bool CanGetTextExtent() const wxOVERRIDE; virtual bool CanGetTextExtent() const wxOVERRIDE;
virtual int GetDepth() const wxOVERRIDE; virtual int GetDepth() const wxOVERRIDE;
virtual wxSize GetPPI() const wxOVERRIDE; virtual wxSize GetPPI() const wxOVERRIDE;
virtual double GetDPIScaleFactor() const wxOVERRIDE;
virtual void SetMapMode(wxMappingMode mode) wxOVERRIDE; virtual void SetMapMode(wxMappingMode mode) wxOVERRIDE;
virtual void SetUserScale(double x, double y) wxOVERRIDE; virtual void SetUserScale(double x, double y) wxOVERRIDE;

View File

@@ -27,6 +27,7 @@ public:
virtual void DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) wxOVERRIDE; virtual void DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) wxOVERRIDE;
virtual void DoGetSize(int* width, int* height) const wxOVERRIDE; virtual void DoGetSize(int* width, int* height) const wxOVERRIDE;
virtual void DoSelect(const wxBitmap& bitmap) wxOVERRIDE; virtual void DoSelect(const wxBitmap& bitmap) wxOVERRIDE;
virtual double GetDPIScaleFactor() const wxOVERRIDE;
virtual wxBitmap DoGetAsBitmap(const wxRect* subrect) const wxOVERRIDE virtual wxBitmap DoGetAsBitmap(const wxRect* subrect) const wxOVERRIDE
{ return subrect == NULL ? GetSelectedBitmap() : GetSelectedBitmap().GetSubBitmapOfHDC(*subrect, GetHDC() );} { return subrect == NULL ? GetSelectedBitmap() : GetSelectedBitmap().GetSubBitmapOfHDC(*subrect, GetHDC() );}

View File

@@ -24,6 +24,8 @@
#include "wx/geometry.h" #include "wx/geometry.h"
#endif #endif
#include "wx/display.h"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Local functions // Local functions
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -485,7 +487,12 @@ wxSize wxGCDCImpl::GetPPI() const
// This is the same value that wxGraphicsContext::GetDPI() returns by // This is the same value that wxGraphicsContext::GetDPI() returns by
// default. // default.
return wxSize(72, 72); return wxDisplay::GetStdPPI();
}
double wxGCDCImpl::GetDPIScaleFactor() const
{
return m_graphicContext ? m_graphicContext->GetDPIScaleFactor() : 1.0;
} }
int wxGCDCImpl::GetDepth() const int wxGCDCImpl::GetDepth() const

View File

@@ -34,6 +34,7 @@
#endif #endif
#include "wx/private/graphics.h" #include "wx/private/graphics.h"
#include "wx/display.h"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -612,6 +613,13 @@ void wxGraphicsContext::SetContentScaleFactor(double contentScaleFactor)
m_contentScaleFactor = contentScaleFactor; m_contentScaleFactor = contentScaleFactor;
} }
double wxGraphicsContext::GetDPIScaleFactor() const
{
wxDouble x, y;
GetDPI(&x, &y);
return x / (double)wxDisplay::GetStdPPIValue();
}
#if 0 #if 0
void wxGraphicsContext::SetAlpha( wxDouble WXUNUSED(alpha) ) void wxGraphicsContext::SetAlpha( wxDouble WXUNUSED(alpha) )
{ {
@@ -635,8 +643,8 @@ void wxGraphicsContext::GetDPI( wxDouble* dpiX, wxDouble* dpiY) const
{ {
// Use some standard DPI value, it doesn't make much sense for the // Use some standard DPI value, it doesn't make much sense for the
// contexts not using any pixels anyhow. // contexts not using any pixels anyhow.
*dpiX = 72.0; *dpiX = wxDisplay::GetStdPPIValue();
*dpiY = 72.0; *dpiY = wxDisplay::GetStdPPIValue();
} }
} }
@@ -908,8 +916,8 @@ wxGraphicsContext::CreateLinearGradientBrush(
return GetRenderer()->CreateLinearGradientBrush return GetRenderer()->CreateLinearGradientBrush
( (
x1, y1, x1, y1,
x2, y2, x2, y2,
gradientStops, gradientStops,
matrix matrix
); );
} }

View File

@@ -2661,6 +2661,11 @@ wxSize wxMSWDCImpl::GetPPI() const
return ppi; return ppi;
} }
double wxMSWDCImpl::GetDPIScaleFactor() const
{
return GetPPI().x / 96.0;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// DC caching // DC caching
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -143,6 +143,11 @@ void wxMemoryDCImpl::DoSelect( const wxBitmap& bitmap )
SetFont(GetFont()); SetFont(GetFont());
} }
double wxMemoryDCImpl::GetDPIScaleFactor() const
{
return m_contentScaleFactor;
}
void wxMemoryDCImpl::SetFont(const wxFont& font) void wxMemoryDCImpl::SetFont(const wxFont& font)
{ {
// We need to adjust the font size by the ratio between the scale factor we // We need to adjust the font size by the ratio between the scale factor we