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.
This commit is contained in:
Vadim Zeitlin
2020-08-06 23:54:48 +02:00
parent 2256dac384
commit 52b25211c8
3 changed files with 52 additions and 13 deletions

View File

@@ -87,6 +87,21 @@ public:
// get the resolution of this monitor in pixels per inch // get the resolution of this monitor in pixels per inch
wxSize GetPPI() const; 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 // name may be empty
wxString GetName() const; wxString GetName() const;

View File

@@ -143,6 +143,33 @@ public:
*/ */
wxSize GetPPI() const; 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 Returns @true if the display is the primary display. The primary
display is the one whose index is 0. display is the one whose index is 0.

View File

@@ -101,13 +101,6 @@ bool IsInCaptureStack(wxWindowBase* win);
} // wxMouseCapture } // 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 // static data
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -805,7 +798,7 @@ static wxSize GetDPIHelper(const wxWindowBase* w)
if ( !dpi.x || !dpi.y ) if ( !dpi.x || !dpi.y )
dpi = wxScreenDC().GetPPI(); dpi = wxScreenDC().GetPPI();
if ( !dpi.x || !dpi.y ) if ( !dpi.x || !dpi.y )
dpi = wxSize(BASELINE_DPI, BASELINE_DPI); dpi = wxDisplay::GetStdPPI();
return dpi; return dpi;
} }
@@ -829,7 +822,7 @@ double wxWindowBase::GetDPIScaleFactor() const
// We use just the vertical component of the DPI because it's the one // 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 // that counts most and, in practice, it's equal to the horizontal one
// anyhow. // anyhow.
return dpi.y / (double)BASELINE_DPI; return dpi.y / (double)wxDisplay::GetStdPPIValue();
} }
// helper of GetWindowBorderSize(): as many ports don't implement support for // 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 wxSize dpi = GetDPIHelper(w);
const int baseline = wxDisplay::GetStdPPIValue();
// Take care to not scale -1 because it has a special meaning of // Take care to not scale -1 because it has a special meaning of
// "unspecified" which should be preserved. // "unspecified" which should be preserved.
return wxSize(sz.x == -1 ? -1 : wxMulDivInt32(sz.x, dpi.x, BASELINE_DPI), return wxSize(sz.x == -1 ? -1 : wxMulDivInt32(sz.x, dpi.x, baseline),
sz.y == -1 ? -1 : wxMulDivInt32(sz.y, dpi.y, BASELINE_DPI)); sz.y == -1 ? -1 : wxMulDivInt32(sz.y, dpi.y, baseline));
} }
/* static */ /* static */
@@ -2912,10 +2907,12 @@ wxWindowBase::ToDIP(const wxSize& sz, const wxWindowBase* w)
{ {
const wxSize dpi = GetDPIHelper(w); const wxSize dpi = GetDPIHelper(w);
const int baseline = wxDisplay::GetStdPPIValue();
// Take care to not scale -1 because it has a special meaning of // Take care to not scale -1 because it has a special meaning of
// "unspecified" which should be preserved. // "unspecified" which should be preserved.
return wxSize(sz.x == -1 ? -1 : wxMulDivInt32(sz.x, BASELINE_DPI, dpi.x), return wxSize(sz.x == -1 ? -1 : wxMulDivInt32(sz.x, baseline, dpi.x),
sz.y == -1 ? -1 : wxMulDivInt32(sz.y, BASELINE_DPI, dpi.y)); sz.y == -1 ? -1 : wxMulDivInt32(sz.y, baseline, dpi.y));
} }
#endif // !wxHAVE_DPI_INDEPENDENT_PIXELS #endif // !wxHAVE_DPI_INDEPENDENT_PIXELS