From 15a37b91fb3673b830d321572d11284695adcf1f Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 00:50:39 +0200 Subject: [PATCH] Add FromDIP to wxDC and wxGraphicsContext Using the same implementation as wxWindow has. --- include/wx/dc.h | 12 ++++++++++++ include/wx/graphics.h | 11 +++++++++++ src/common/dcbase.cpp | 13 +++++++++++++ src/common/graphcmn.cpp | 13 +++++++++++++ 4 files changed, 49 insertions(+) diff --git a/include/wx/dc.h b/include/wx/dc.h index 8a3e796114..c1f978f2c4 100644 --- a/include/wx/dc.h +++ b/include/wx/dc.h @@ -539,6 +539,8 @@ public: virtual double GetContentScaleFactor() const { return m_contentScaleFactor; } + virtual wxSize FromDIP(const wxSize& sz) const; + #ifdef __WXMSW__ // Native Windows functions using the underlying HDC don't honour GDI+ // transformations which may be applied to it. Using this function we can @@ -827,6 +829,16 @@ public: double GetContentScaleFactor() const { return m_pimpl->GetContentScaleFactor(); } + wxSize FromDIP(const wxSize& sz) const + { return m_pimpl->FromDIP(sz); } + wxPoint FromDIP(const wxPoint& pt) const + { + const wxSize sz = FromDIP(wxSize(pt.x, pt.y)); + return wxPoint(sz.x, sz.y); + } + int FromDIP(int d) const + { return FromDIP(wxSize(d, 0)).x; } + // Right-To-Left (RTL) modes void SetLayoutDirection(wxLayoutDirection dir) diff --git a/include/wx/graphics.h b/include/wx/graphics.h index 5defa492d9..935f06ac53 100644 --- a/include/wx/graphics.h +++ b/include/wx/graphics.h @@ -739,6 +739,17 @@ public: // returns the resolution of the graphics context in device points per inch virtual void GetDPI( wxDouble* dpiX, wxDouble* dpiY) const; + wxSize FromDIP(const wxSize& sz) const; + wxPoint FromDIP(const wxPoint& pt) const + { + const wxSize sz = FromDIP(wxSize(pt.x, pt.y)); + return wxPoint(sz.x, sz.y); + } + int FromDIP(int d) const + { + return FromDIP(wxSize(d, 0)).x; + } + #if 0 // sets the current alpha on this context virtual void SetAlpha( wxDouble alpha ); diff --git a/src/common/dcbase.cpp b/src/common/dcbase.cpp index 82be5c669d..f718a1163a 100644 --- a/src/common/dcbase.cpp +++ b/src/common/dcbase.cpp @@ -36,6 +36,8 @@ #endif #include "wx/private/textmeasure.h" +#include "wx/private/rescale.h" +#include "wx/display.h" #ifdef __WXMSW__ #include "wx/msw/dcclient.h" @@ -604,6 +606,17 @@ void wxDCImpl::SetAxisOrientation( bool xLeftRight, bool yBottomUp ) ComputeScaleAndOrigin(); } +wxSize wxDCImpl::FromDIP(const wxSize& sz) const +{ +#ifdef wxHAS_DPI_INDEPENDENT_PIXELS + return sz; +#else + const wxSize dpi = GetPPI(); + const wxSize baseline = wxDisplay::GetStdPPI(); + return wxRescaleCoord(sz).From(baseline).To(dpi); +#endif // wxHAS_DPI_INDEPENDENT_PIXELS +} + bool wxDCImpl::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const { wxTextMeasure tm(GetOwner(), &m_font); diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index 1bbb3e0a65..179e0f2fc6 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -34,6 +34,7 @@ #endif #include "wx/private/graphics.h" +#include "wx/private/rescale.h" #include "wx/display.h" //----------------------------------------------------------------------------- @@ -641,6 +642,18 @@ void wxGraphicsContext::GetDPI( wxDouble* dpiX, wxDouble* dpiY) const } } +wxSize wxGraphicsContext::FromDIP(const wxSize& sz) const +{ +#ifdef wxHAS_DPI_INDEPENDENT_PIXELS + return sz; +#else + wxRealPoint dpi; + GetDPI(&dpi.x, &dpi.y); + const wxSize baseline = wxDisplay::GetStdPPI(); + return wxRescaleCoord(sz).From(baseline).To(wxSize((int)dpi.x, (int)dpi.y)); +#endif // wxHAS_DPI_INDEPENDENT_PIXELS +} + // sets the pen void wxGraphicsContext::SetPen( const wxGraphicsPen& pen ) {