From 453468f2f9d9ae99124cfdc73171a149ac078d09 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 11 Jul 2021 19:31:17 +0100 Subject: [PATCH] Add wxRescaleCoord() helper and use it No real changes, just make the code a bit shorter and more clear by using a dedicated function rather than wxMulDivInt32() directly. --- include/wx/gdicmn.h | 18 ++++++++++++++++++ interface/wx/gdicmn.h | 17 +++++++++++++++++ src/common/gdicmn.cpp | 10 ++++++++++ src/common/wincmn.cpp | 28 ++++------------------------ src/msw/button.cpp | 5 +---- 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/include/wx/gdicmn.h b/include/wx/gdicmn.h index e29a77627c..e7732db9b4 100644 --- a/include/wx/gdicmn.h +++ b/include/wx/gdicmn.h @@ -1104,5 +1104,23 @@ extern wxRect WXDLLIMPEXP_CORE wxGetClientDisplayRect(); // set global cursor extern void WXDLLIMPEXP_CORE wxSetCursor(const wxCursor& cursor); +// Scale the given value by the ratio between 2 other values, with rounding. +// Do not scale the value if it's -1, just return it unchanged in this case. +extern int WXDLLIMPEXP_CORE wxRescaleCoord(int n, int newScale, int oldScale); + +inline wxPoint +wxRescaleCoord(wxPoint pt, wxSize newScale, wxSize oldScale) +{ + return wxPoint(wxRescaleCoord(pt.x, newScale.x, oldScale.x), + wxRescaleCoord(pt.y, newScale.y, oldScale.y)); +} + +inline wxSize +wxRescaleCoord(wxSize sz, wxSize newScale, wxSize oldScale) +{ + return wxSize(wxRescaleCoord(sz.x, newScale.x, oldScale.x), + wxRescaleCoord(sz.y, newScale.y, oldScale.y)); +} + #endif // _WX_GDICMNH__ diff --git a/interface/wx/gdicmn.h b/interface/wx/gdicmn.h index 32a128eaff..b19164ce7f 100644 --- a/interface/wx/gdicmn.h +++ b/interface/wx/gdicmn.h @@ -1345,3 +1345,20 @@ void wxDisplaySizeMM(int* width, int* height); wxSize wxGetDisplaySizeMM(); //@} +/** + Scale the given value by the ratio between 2 other values, with rounding. + + Do not scale the value if it's -1, just return it unchanged in this case. + + This simply calls wxMulDivInt32() with the provided arguments, but provides + a more clear name for this operation. + + @since 3.1.6 + */ +int wxRescaleCoord(int n, int newScale, int oldScale); + +/// @overload +wxPoint wxRescaleCoord(wxPoint pt, wxSize newScale, wxSize oldScale); + +/// @overload +wxSize wxRescaleCoord(wxSize sz, wxSize newScale, wxSize oldScale); diff --git a/src/common/gdicmn.cpp b/src/common/gdicmn.cpp index 20442bbc73..9dc5784a2c 100644 --- a/src/common/gdicmn.cpp +++ b/src/common/gdicmn.cpp @@ -16,6 +16,7 @@ #include "wx/display.h" #include "wx/gdiobj.h" +#include "wx/math.h" #ifndef WX_PRECOMP #include "wx/log.h" @@ -32,6 +33,10 @@ #include "wx/math.h" #endif +#ifdef __WINDOWS__ + // Required in order to use wxMulDivInt32(). + #include "wx/msw/wrapwin.h" +#endif wxIMPLEMENT_ABSTRACT_CLASS(wxGDIObject, wxObject); @@ -921,3 +926,8 @@ wxResourceCache::~wxResourceCache () node = node->GetNext (); } } + +int wxRescaleCoord(int n, int newScale, int oldScale) +{ + return n == -1 ? -1 : wxMulDivInt32(n, newScale, oldScale); +} diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 5771b95c6a..d0180a68a7 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -2907,10 +2907,7 @@ wxWindowBase::FromDIP(const wxSize& sz, const wxWindowBase* 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), - sz.y == -1 ? -1 : wxMulDivInt32(sz.y, dpi.y, baseline)); + return wxRescaleCoord(sz, dpi, wxSize(baseline, baseline)); } /* static */ @@ -2921,10 +2918,7 @@ wxWindowBase::ToDIP(const wxSize& sz, const wxWindowBase* 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.x), - sz.y == -1 ? -1 : wxMulDivInt32(sz.y, baseline, dpi.y)); + return wxRescaleCoord(sz, wxSize(baseline, baseline), dpi); } #endif // !wxHAVE_DPI_INDEPENDENT_PIXELS @@ -2963,28 +2957,14 @@ wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt) const { const wxSize base = GetDlgUnitBase(); - // NB: wxMulDivInt32() is used, because it correctly rounds the result - - wxPoint pt2 = wxDefaultPosition; - if (pt.x != wxDefaultCoord) - pt2.x = wxMulDivInt32(pt.x, 4, base.x); - if (pt.y != wxDefaultCoord) - pt2.y = wxMulDivInt32(pt.y, 8, base.y); - - return pt2; + return wxRescaleCoord(pt, wxSize(4, 8), base); } wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt) const { const wxSize base = GetDlgUnitBase(); - wxPoint pt2 = wxDefaultPosition; - if (pt.x != wxDefaultCoord) - pt2.x = wxMulDivInt32(pt.x, base.x, 4); - if (pt.y != wxDefaultCoord) - pt2.y = wxMulDivInt32(pt.y, base.y, 8); - - return pt2; + return wxRescaleCoord(pt, base, wxSize(4, 8)); } // ---------------------------------------------------------------------------- diff --git a/src/msw/button.cpp b/src/msw/button.cpp index f49b974640..7a1a21d913 100644 --- a/src/msw/button.cpp +++ b/src/msw/button.cpp @@ -185,11 +185,8 @@ wxSize wxButtonBase::GetDefaultSize(wxWindow* win) // character width metadata stored in the font; see // http://support.microsoft.com/default.aspx/kb/145994 for detailed // discussion. - // - // NB: wxMulDivInt32() is used, because it correctly rounds the result - s_sizeBtn.SetAtNewDPI(wxSize(wxMulDivInt32(50, base.x, 4), - wxMulDivInt32(14, base.y, 8))); + s_sizeBtn.SetAtNewDPI(wxRescaleCoord(wxSize(50, 14), base, wxSize(4, 8))); } return s_sizeBtn.Get();