Make wxRescaleCoord() safer by requiring explicitly using From/To
This should help with accidentally exchanging the order of parameters and makes the code using this function more readable. No real changes.
This commit is contained in:
		@@ -3142,10 +3142,10 @@ public:
 | 
			
		||||
 | 
			
		||||
    // Scale the value by the ratio between new and old DPIs carried by this
 | 
			
		||||
    // event.
 | 
			
		||||
    int ScaleX(int x) const;
 | 
			
		||||
    int ScaleY(int y) const;
 | 
			
		||||
    wxSize Scale(wxSize sz) const;
 | 
			
		||||
 | 
			
		||||
    wxSize Scale(wxSize sz) const { return wxSize(ScaleX(sz.x), ScaleY(sz.y)); }
 | 
			
		||||
    int ScaleX(int x) const { return Scale(wxSize(x, -1)).x; }
 | 
			
		||||
    int ScaleY(int y) const { return Scale(wxSize(-1, y)).y; }
 | 
			
		||||
 | 
			
		||||
    virtual wxEvent *Clone() const wxOVERRIDE { return new wxDPIChangedEvent(*this); }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -18,26 +18,98 @@
 | 
			
		||||
    #include "wx/msw/wrapwin.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
// wxRescaleCoord is used to scale the components of the given wxSize by the
 | 
			
		||||
// ratio between 2 scales, with rounding. It doesn't not scale the components
 | 
			
		||||
// if they're set to -1 (wxDefaultCoord), as this value is special in wxSize.
 | 
			
		||||
//
 | 
			
		||||
// The way it's used is special because we want to ensure there is no confusion
 | 
			
		||||
// between the scale being converted from and the scale being converted to, so
 | 
			
		||||
// instead of just using a single function, we use an intermediate object,
 | 
			
		||||
// which is not supposed to be used directly, but is only returned by From() in
 | 
			
		||||
// order to allow calling To() on it.
 | 
			
		||||
//
 | 
			
		||||
// Another complication is that we want this to work for both wxSize and
 | 
			
		||||
// wxPoint, so wxRescaleCoord() is a overloaded and the helper classes are
 | 
			
		||||
// templates, with their template parameter being either one or the other.
 | 
			
		||||
 | 
			
		||||
// 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.
 | 
			
		||||
inline int wxRescaleCoord(int n, int newScale, int oldScale)
 | 
			
		||||
namespace wxPrivate
 | 
			
		||||
{
 | 
			
		||||
    return n == -1 ? -1 : wxMulDivInt32(n, newScale, oldScale);
 | 
			
		||||
 | 
			
		||||
template <typename T> class wxRescaleCoordWithValue;
 | 
			
		||||
 | 
			
		||||
template <typename T>
 | 
			
		||||
class wxRescaleCoordWithFrom
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    T To(wxSize newScale) const
 | 
			
		||||
    {
 | 
			
		||||
        T value(m_value);
 | 
			
		||||
 | 
			
		||||
        if ( value.x != wxDefaultCoord )
 | 
			
		||||
            value.x = wxMulDivInt32(value.x, newScale.x, m_oldScale.x);
 | 
			
		||||
 | 
			
		||||
        if ( value.y != wxDefaultCoord )
 | 
			
		||||
            value.y = wxMulDivInt32(value.y, newScale.y, m_oldScale.y);
 | 
			
		||||
 | 
			
		||||
        return value;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    T To(int newScaleX, int newScaleY) const
 | 
			
		||||
    {
 | 
			
		||||
        return To(wxSize(newScaleX, newScaleY));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    wxRescaleCoordWithFrom(T value, wxSize oldScale)
 | 
			
		||||
        : m_value(value), m_oldScale(oldScale)
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const T m_value;
 | 
			
		||||
    const wxSize m_oldScale;
 | 
			
		||||
 | 
			
		||||
    // Only it can create objects of this class.
 | 
			
		||||
    friend wxRescaleCoordWithValue<T>;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template <typename T>
 | 
			
		||||
class wxRescaleCoordWithValue
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    explicit wxRescaleCoordWithValue(T value)
 | 
			
		||||
        : m_value(value)
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    wxRescaleCoordWithFrom<T> From(wxSize oldScale)
 | 
			
		||||
    {
 | 
			
		||||
        return wxRescaleCoordWithFrom<T>(m_value, oldScale);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    wxRescaleCoordWithFrom<T> From(int oldScaleX, int oldScaleY)
 | 
			
		||||
    {
 | 
			
		||||
        return From(wxSize(oldScaleX, oldScaleY));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    const T m_value;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace wxPrivate
 | 
			
		||||
 | 
			
		||||
inline wxPrivate::wxRescaleCoordWithValue<wxSize> wxRescaleCoord(wxSize sz)
 | 
			
		||||
{
 | 
			
		||||
    return wxPrivate::wxRescaleCoordWithValue<wxSize>(sz);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline wxPoint
 | 
			
		||||
wxRescaleCoord(wxPoint pt, wxSize newScale, wxSize oldScale)
 | 
			
		||||
inline wxPrivate::wxRescaleCoordWithValue<wxSize> wxRescaleCoord(int x, int y)
 | 
			
		||||
{
 | 
			
		||||
    return wxPoint(wxRescaleCoord(pt.x, newScale.x, oldScale.x),
 | 
			
		||||
                   wxRescaleCoord(pt.y, newScale.y, oldScale.y));
 | 
			
		||||
    return wxPrivate::wxRescaleCoordWithValue<wxSize>(wxSize(x, y));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline wxSize
 | 
			
		||||
wxRescaleCoord(wxSize sz, wxSize newScale, wxSize oldScale)
 | 
			
		||||
inline wxPrivate::wxRescaleCoordWithValue<wxPoint> wxRescaleCoord(wxPoint pt)
 | 
			
		||||
{
 | 
			
		||||
    return wxSize(wxRescaleCoord(sz.x, newScale.x, oldScale.x),
 | 
			
		||||
                  wxRescaleCoord(sz.y, newScale.y, oldScale.y));
 | 
			
		||||
    return wxPrivate::wxRescaleCoordWithValue<wxPoint>(pt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // _WX_PRIVATE_RESCALE_H_
 | 
			
		||||
 
 | 
			
		||||
@@ -941,14 +941,9 @@ wxHelpEvent::Origin wxHelpEvent::GuessOrigin(Origin origin)
 | 
			
		||||
// wxDPIChangedEvent
 | 
			
		||||
// ----------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
int wxDPIChangedEvent::ScaleX(int x) const
 | 
			
		||||
wxSize wxDPIChangedEvent::Scale(wxSize sz) const
 | 
			
		||||
{
 | 
			
		||||
    return wxRescaleCoord(x, m_newDPI.x, m_oldDPI.x);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int wxDPIChangedEvent::ScaleY(int y) const
 | 
			
		||||
{
 | 
			
		||||
    return wxRescaleCoord(y, m_newDPI.y, m_oldDPI.y);
 | 
			
		||||
    return wxRescaleCoord(sz).From(m_oldDPI).To(m_newDPI);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // wxUSE_GUI
 | 
			
		||||
 
 | 
			
		||||
@@ -2902,9 +2902,9 @@ wxWindowBase::FromDIP(const wxSize& sz, const wxWindowBase* w)
 | 
			
		||||
{
 | 
			
		||||
    const wxSize dpi = GetDPIHelper(w);
 | 
			
		||||
 | 
			
		||||
    const int baseline = wxDisplay::GetStdPPIValue();
 | 
			
		||||
    const wxSize baseline = wxDisplay::GetStdPPI();
 | 
			
		||||
 | 
			
		||||
    return wxRescaleCoord(sz, dpi, wxSize(baseline, baseline));
 | 
			
		||||
    return wxRescaleCoord(sz).From(baseline).To(dpi);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* static */
 | 
			
		||||
@@ -2913,9 +2913,9 @@ wxWindowBase::ToDIP(const wxSize& sz, const wxWindowBase* w)
 | 
			
		||||
{
 | 
			
		||||
    const wxSize dpi = GetDPIHelper(w);
 | 
			
		||||
 | 
			
		||||
    const int baseline = wxDisplay::GetStdPPIValue();
 | 
			
		||||
    const wxSize baseline = wxDisplay::GetStdPPI();
 | 
			
		||||
 | 
			
		||||
    return wxRescaleCoord(sz, wxSize(baseline, baseline), dpi);
 | 
			
		||||
    return wxRescaleCoord(sz).From(dpi).To(baseline);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // !wxHAVE_DPI_INDEPENDENT_PIXELS
 | 
			
		||||
@@ -2954,14 +2954,14 @@ wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt) const
 | 
			
		||||
{
 | 
			
		||||
    const wxSize base = GetDlgUnitBase();
 | 
			
		||||
 | 
			
		||||
    return wxRescaleCoord(pt, wxSize(4, 8), base);
 | 
			
		||||
    return wxRescaleCoord(pt).From(base).To(4, 8);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt) const
 | 
			
		||||
{
 | 
			
		||||
    const wxSize base = GetDlgUnitBase();
 | 
			
		||||
 | 
			
		||||
    return wxRescaleCoord(pt, base, wxSize(4, 8));
 | 
			
		||||
    return wxRescaleCoord(pt).From(4, 8).To(base);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ----------------------------------------------------------------------------
 | 
			
		||||
 
 | 
			
		||||
@@ -187,7 +187,7 @@ wxSize wxButtonBase::GetDefaultSize(wxWindow* win)
 | 
			
		||||
        // http://support.microsoft.com/default.aspx/kb/145994 for detailed
 | 
			
		||||
        // discussion.
 | 
			
		||||
 | 
			
		||||
        s_sizeBtn.SetAtNewDPI(wxRescaleCoord(wxSize(50, 14), base, wxSize(4, 8)));
 | 
			
		||||
        s_sizeBtn.SetAtNewDPI(wxRescaleCoord(50, 14).From(4, 8).To(base));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return s_sizeBtn.Get();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user