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
This commit is contained in:
Simon Stone
2021-12-08 21:37:53 +01:00
committed by Vadim Zeitlin
parent a2389fc512
commit 0fc936ca41
3 changed files with 66 additions and 0 deletions

View File

@@ -347,6 +347,19 @@ public:
this->SetMax(std::numeric_limits<ValueType>::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

View File

@@ -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);
};
/**

View File

@@ -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<unsigned> 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;