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.
This commit is contained in:
Vadim Zeitlin
2021-07-11 19:31:17 +01:00
parent 7c6f290995
commit 453468f2f9
5 changed files with 50 additions and 28 deletions

View File

@@ -1104,5 +1104,23 @@ extern wxRect WXDLLIMPEXP_CORE wxGetClientDisplayRect();
// set global cursor // set global cursor
extern void WXDLLIMPEXP_CORE wxSetCursor(const wxCursor& 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 #endif
// _WX_GDICMNH__ // _WX_GDICMNH__

View File

@@ -1345,3 +1345,20 @@ void wxDisplaySizeMM(int* width, int* height);
wxSize wxGetDisplaySizeMM(); 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);

View File

@@ -16,6 +16,7 @@
#include "wx/display.h" #include "wx/display.h"
#include "wx/gdiobj.h" #include "wx/gdiobj.h"
#include "wx/math.h"
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include "wx/log.h" #include "wx/log.h"
@@ -32,6 +33,10 @@
#include "wx/math.h" #include "wx/math.h"
#endif #endif
#ifdef __WINDOWS__
// Required in order to use wxMulDivInt32().
#include "wx/msw/wrapwin.h"
#endif
wxIMPLEMENT_ABSTRACT_CLASS(wxGDIObject, wxObject); wxIMPLEMENT_ABSTRACT_CLASS(wxGDIObject, wxObject);
@@ -921,3 +926,8 @@ wxResourceCache::~wxResourceCache ()
node = node->GetNext (); node = node->GetNext ();
} }
} }
int wxRescaleCoord(int n, int newScale, int oldScale)
{
return n == -1 ? -1 : wxMulDivInt32(n, newScale, oldScale);
}

View File

@@ -2907,10 +2907,7 @@ wxWindowBase::FromDIP(const wxSize& sz, const wxWindowBase* w)
const int baseline = wxDisplay::GetStdPPIValue(); const int baseline = wxDisplay::GetStdPPIValue();
// Take care to not scale -1 because it has a special meaning of return wxRescaleCoord(sz, dpi, wxSize(baseline, baseline));
// "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));
} }
/* static */ /* static */
@@ -2921,10 +2918,7 @@ wxWindowBase::ToDIP(const wxSize& sz, const wxWindowBase* w)
const int baseline = wxDisplay::GetStdPPIValue(); const int baseline = wxDisplay::GetStdPPIValue();
// Take care to not scale -1 because it has a special meaning of return wxRescaleCoord(sz, wxSize(baseline, baseline), dpi);
// "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));
} }
#endif // !wxHAVE_DPI_INDEPENDENT_PIXELS #endif // !wxHAVE_DPI_INDEPENDENT_PIXELS
@@ -2963,28 +2957,14 @@ wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt) const
{ {
const wxSize base = GetDlgUnitBase(); const wxSize base = GetDlgUnitBase();
// NB: wxMulDivInt32() is used, because it correctly rounds the result return wxRescaleCoord(pt, wxSize(4, 8), base);
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;
} }
wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt) const wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt) const
{ {
const wxSize base = GetDlgUnitBase(); const wxSize base = GetDlgUnitBase();
wxPoint pt2 = wxDefaultPosition; return wxRescaleCoord(pt, base, wxSize(4, 8));
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;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -185,11 +185,8 @@ wxSize wxButtonBase::GetDefaultSize(wxWindow* win)
// character width metadata stored in the font; see // character width metadata stored in the font; see
// http://support.microsoft.com/default.aspx/kb/145994 for detailed // http://support.microsoft.com/default.aspx/kb/145994 for detailed
// discussion. // discussion.
//
// NB: wxMulDivInt32() is used, because it correctly rounds the result
s_sizeBtn.SetAtNewDPI(wxSize(wxMulDivInt32(50, base.x, 4), s_sizeBtn.SetAtNewDPI(wxRescaleCoord(wxSize(50, 14), base, wxSize(4, 8)));
wxMulDivInt32(14, base.y, 8)));
} }
return s_sizeBtn.Get(); return s_sizeBtn.Get();