From 52b25211c8c388d9b9435f1572337be41ac92e2f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 6 Aug 2020 23:54:48 +0200 Subject: [PATCH] Add wxDisplay::GetStdPPIValue() and GetStdPPI() Provide public way to access the default platform DPI, which was previously a constant private to src/common/wincmn.cpp. --- include/wx/display.h | 15 +++++++++++++++ interface/wx/display.h | 27 +++++++++++++++++++++++++++ src/common/wincmn.cpp | 23 ++++++++++------------- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/include/wx/display.h b/include/wx/display.h index b9fec42aba..d6f501cd7e 100644 --- a/include/wx/display.h +++ b/include/wx/display.h @@ -87,6 +87,21 @@ public: // get the resolution of this monitor in pixels per inch wxSize GetPPI() const; + // get the default resolution for displays on this platform + static int GetStdPPIValue() + { +#ifdef __WXOSX__ + return 72; +#else + return 96; +#endif + } + + static wxSize GetStdPPI() + { + return wxSize(GetStdPPIValue(), GetStdPPIValue()); + } + // name may be empty wxString GetName() const; diff --git a/interface/wx/display.h b/interface/wx/display.h index 2085ff15db..b71cbb0d72 100644 --- a/interface/wx/display.h +++ b/interface/wx/display.h @@ -143,6 +143,33 @@ public: */ wxSize GetPPI() const; + /** + Returns default display resolution for the current platform in pixels + per inch. + + This function mostly used internally, use GetPPI() to get the actual + display resolution. + + Currently the standard PPI is the same in both horizontal and vertical + directions on all platforms and its value is 96 everywhere except under + Apple devices (those running macOS, iOS, watchOS etc), where it is 72. + + @see GetStdPPI() + + @since 3.1.5 + */ + static int GetStdPPIValue(); + + /** + Returns default display resolution for the current platform as wxSize. + + This function is equivalent to constructing wxSize object with both + components set to GetStdPPIValue(). + + @since 3.1.5 + */ + static wxSize GetStdPPI(); + /** Returns @true if the display is the primary display. The primary display is the one whose index is 0. diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 9b2e522920..563406a801 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -101,13 +101,6 @@ bool IsInCaptureStack(wxWindowBase* win); } // wxMouseCapture -// Most platforms use 96 DPI by default, but Mac traditionally uses 72. -#ifdef __WXOSX__ -static const int BASELINE_DPI = 72; -#else -static const int BASELINE_DPI = 96; -#endif - // ---------------------------------------------------------------------------- // static data // ---------------------------------------------------------------------------- @@ -805,7 +798,7 @@ static wxSize GetDPIHelper(const wxWindowBase* w) if ( !dpi.x || !dpi.y ) dpi = wxScreenDC().GetPPI(); if ( !dpi.x || !dpi.y ) - dpi = wxSize(BASELINE_DPI, BASELINE_DPI); + dpi = wxDisplay::GetStdPPI(); return dpi; } @@ -829,7 +822,7 @@ double wxWindowBase::GetDPIScaleFactor() const // We use just the vertical component of the DPI because it's the one // that counts most and, in practice, it's equal to the horizontal one // anyhow. - return dpi.y / (double)BASELINE_DPI; + return dpi.y / (double)wxDisplay::GetStdPPIValue(); } // helper of GetWindowBorderSize(): as many ports don't implement support for @@ -2900,10 +2893,12 @@ wxWindowBase::FromDIP(const wxSize& sz, const wxWindowBase* w) { const wxSize dpi = GetDPIHelper(w); + const int baseline = wxDisplay::GetStdPPIValue(); + // Take care to not scale -1 because it has a special meaning of // "unspecified" which should be preserved. - return wxSize(sz.x == -1 ? -1 : wxMulDivInt32(sz.x, dpi.x, BASELINE_DPI), - sz.y == -1 ? -1 : wxMulDivInt32(sz.y, dpi.y, BASELINE_DPI)); + return wxSize(sz.x == -1 ? -1 : wxMulDivInt32(sz.x, dpi.x, baseline), + sz.y == -1 ? -1 : wxMulDivInt32(sz.y, dpi.y, baseline)); } /* static */ @@ -2912,10 +2907,12 @@ wxWindowBase::ToDIP(const wxSize& sz, const wxWindowBase* w) { const wxSize dpi = GetDPIHelper(w); + const int baseline = wxDisplay::GetStdPPIValue(); + // Take care to not scale -1 because it has a special meaning of // "unspecified" which should be preserved. - return wxSize(sz.x == -1 ? -1 : wxMulDivInt32(sz.x, BASELINE_DPI, dpi.x), - sz.y == -1 ? -1 : wxMulDivInt32(sz.y, BASELINE_DPI, dpi.y)); + return wxSize(sz.x == -1 ? -1 : wxMulDivInt32(sz.x, baseline, dpi.x), + sz.y == -1 ? -1 : wxMulDivInt32(sz.y, baseline, dpi.y)); } #endif // !wxHAVE_DPI_INDEPENDENT_PIXELS