From 0fc936ca411e944e79ae4bf21e99a5c1fb58be5e Mon Sep 17 00:00:00 2001 From: Simon Stone Date: Wed, 8 Dec 2021 21:37:53 +0100 Subject: [PATCH] Add wxIntegerValidatorctor ctor taking minimum and maximum value Using this ctor is more convenient than using the default ctor and then calling SetMin() and SetMax(). Document the new ctor and add tests showing that minimum and maximum values are actually respected. Closes https://github.com/wxWidgets/wxWidgets/pull/2610 --- include/wx/valnum.h | 13 +++++++++++++ interface/wx/valnum.h | 19 +++++++++++++++++++ tests/validators/valnum.cpp | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/include/wx/valnum.h b/include/wx/valnum.h index 6c94f53613..75041a1ce4 100644 --- a/include/wx/valnum.h +++ b/include/wx/valnum.h @@ -347,6 +347,19 @@ public: this->SetMax(std::numeric_limits::max()); } + // Ctor for an integer validator. + // + // Sets the range to the specified interval [min, max]. + wxIntegerValidator(ValueType *value, + ValueType min, + ValueType max, + int style = wxNUM_VAL_DEFAULT) + : Base(value, style) + { + this->SetMin(min); + this->SetMax(max); + } + virtual wxObject *Clone() const wxOVERRIDE { return new wxIntegerValidator(*this); } virtual bool IsInRange(LongestValueType value) const wxOVERRIDE diff --git a/interface/wx/valnum.h b/interface/wx/valnum.h index 6b32520203..4ebc7e7143 100644 --- a/interface/wx/valnum.h +++ b/interface/wx/valnum.h @@ -288,6 +288,25 @@ public: of wxNUM_VAL_NO_TRAILING_ZEROES which can't be used here. */ wxIntegerValidator(ValueType *value = NULL, int style = wxNUM_VAL_DEFAULT); + + /** + Validator constructor with specified range. + + @param value + A pointer to the variable associated with the validator. This variable + should have a lifetime equal to or longer than the validator lifetime + (which is usually determined by the lifetime of the window). + @param min + The minimum value accepted by the validator. + @param max + The maximum value accepted by the validator. + @param style + A combination of wxNumValidatorStyle enum values with the exception + of wxNUM_VAL_NO_TRAILING_ZEROES which can't be used here. + + @since 3.1.6 + */ + wxIntegerValidator(ValueType *value, ValueType min, ValueType max, int style = wxNUM_VAL_DEFAULT); }; /** diff --git a/tests/validators/valnum.cpp b/tests/validators/valnum.cpp index b91129359c..404b55fa36 100644 --- a/tests/validators/valnum.cpp +++ b/tests/validators/valnum.cpp @@ -114,6 +114,40 @@ TEST_CASE_METHOD(NumValidatorTestCase, "ValNum::TransferUnsigned", "[valnum]") CHECK( !valUnsigned.TransferFromWindow() ); } +TEST_CASE_METHOD(NumValidatorTestCase, "ValNum::TransferUnsignedRange", "[valnum]") +{ + unsigned value = 1; + wxIntegerValidator valUnsigned(&value, 1, 20); + valUnsigned.SetWindow(m_text); + + CHECK( valUnsigned.TransferToWindow() ); + CHECK( m_text->GetValue() == "1" ); + + value = 17; + CHECK( valUnsigned.TransferToWindow() ); + CHECK( m_text->GetValue() == "17" ); + + m_text->ChangeValue("foobar"); + CHECK( !valUnsigned.TransferFromWindow() ); + + m_text->ChangeValue("0"); + CHECK( !valUnsigned.TransferFromWindow() ); + + m_text->ChangeValue("1"); + CHECK( valUnsigned.TransferFromWindow() ); + CHECK( value == 1); + + m_text->ChangeValue("20"); + CHECK( valUnsigned.TransferFromWindow() ); + CHECK( value == 20); + + m_text->ChangeValue("21"); + CHECK( !valUnsigned.TransferFromWindow() ); + + m_text->Clear(); + CHECK( !valUnsigned.TransferFromWindow() ); +} + TEST_CASE_METHOD(NumValidatorTestCase, "ValNum::TransferULL", "[valnum]") { unsigned long long value = 0;