diff --git a/include/wx/window.h b/include/wx/window.h index b1f52a6325..6dd141ac41 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -533,9 +533,8 @@ public: // returns the magnification of the content of this window // eg 2.0 for a window on a retina screen - virtual double GetContentScaleFactor() const - { return 1.0; } - + virtual double GetContentScaleFactor() const; + // return the size of the left/right and top/bottom borders in x and y // components of the result respectively virtual wxSize GetWindowBorderSize() const; diff --git a/interface/wx/window.h b/interface/wx/window.h index fbe8647a63..cc25b31beb 100644 --- a/interface/wx/window.h +++ b/interface/wx/window.h @@ -1126,10 +1126,18 @@ public: Returns the magnification of the backing store of this window, eg 2.0 for a window on a retina screen. + This method can be used to adjust hard coded pixel values to the values + appropriate for the current screen resolution. E.g. instead of using + 32px icons, which would look tiny on the high resolution (also known as + HiDPI or retina) displays, you should use + @code + wxRound(32*GetContentScaleFactor()) + @endcode instead. + @since 2.9.5 */ - virtual double GetContentScaleFactor() const; - + double GetContentScaleFactor() const; + /** Returns the size of the left/right and top/bottom borders of this window in x and y components of the result respectively. diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index dbaa328f10..58025cbc17 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -791,6 +791,21 @@ wxSize wxWindowBase::DoGetBestSize() const return best; } +double wxWindowBase::GetContentScaleFactor() const +{ + // Currently we don't support per-monitor DPI, so it's useless to construct + // a DC associated with this window, just use the global value. + // + // We also 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. + // + // Finally, we consider 96 DPI to be the standard value, this is correct + // at least for MSW, but could conceivably need adjustment for the other + // platforms. + return wxScreenDC().GetPPI().y / 96.; +} + // helper of GetWindowBorderSize(): as many ports don't implement support for // wxSYS_BORDER/EDGE_X/Y metrics in their wxSystemSettings, use hard coded // fallbacks in this case