Fix confusion around '-' handling in numeric validators code
Actual implementation of IsCharOk() didn't correspond to the comments in it or near the function declaration which stated that it's never called with ch='-' as argument -- it was called with it and called IsMinusOk() right before a comment saying that it doesn't need to do it, which was very confusing. Fix this by making the behaviour really correspond to the comments and handling '-' at the base class level. This required introducing a new pure virtual CanBeNegative() function, but it's going to be useful for other things later too. Still keep IsMinusOk() helper, but make it private now because it doesn't need to be called from the derived class IsCharOk() any longer.
This commit is contained in:
@@ -79,11 +79,6 @@ protected:
|
||||
// bits of our style to the corresponding wxNumberFormatter::Style values.
|
||||
int GetFormatFlags() const;
|
||||
|
||||
// Return true if pressing a '-' key is acceptable for the current control
|
||||
// contents and insertion point. This is meant to be called from the
|
||||
// derived class IsCharOk() implementation.
|
||||
bool IsMinusOk(const wxString& val, int pos) const;
|
||||
|
||||
// Return the string which would result from inserting the given character
|
||||
// at the specified position.
|
||||
wxString GetValueAfterInsertingChar(wxString val, int pos, wxChar ch) const
|
||||
@@ -92,6 +87,11 @@ protected:
|
||||
return val;
|
||||
}
|
||||
|
||||
// Return true if this control allows negative numbers in it.
|
||||
//
|
||||
// If it doesn't, we don't allow entering "-" at all.
|
||||
virtual bool CanBeNegative() const = 0;
|
||||
|
||||
private:
|
||||
// Check whether the specified character can be inserted in the control at
|
||||
// the given position in the string representing the current controls
|
||||
@@ -114,6 +114,11 @@ private:
|
||||
// Determine the current insertion point and text in the associated control.
|
||||
void GetCurrentValueAndInsertionPoint(wxString& val, int& pos) const;
|
||||
|
||||
// Return true if pressing a '-' key is acceptable for the current control
|
||||
// contents and insertion point. This is used by OnChar() to handle '-' and
|
||||
// relies on CanBeNegative() implementation in the derived class.
|
||||
bool IsMinusOk(const wxString& val, int pos) const;
|
||||
|
||||
|
||||
// Combination of wxVAL_NUM_XXX values.
|
||||
int m_style;
|
||||
@@ -346,6 +351,9 @@ public:
|
||||
|
||||
virtual wxObject *Clone() const wxOVERRIDE { return new wxIntegerValidator(*this); }
|
||||
|
||||
protected:
|
||||
virtual bool CanBeNegative() const wxOVERRIDE { return DoGetMin() < 0; }
|
||||
|
||||
private:
|
||||
wxDECLARE_NO_ASSIGN_CLASS(wxIntegerValidator);
|
||||
};
|
||||
@@ -458,6 +466,9 @@ public:
|
||||
return new wxFloatingPointValidator(*this);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual bool CanBeNegative() const wxOVERRIDE { return DoGetMin() < 0; }
|
||||
|
||||
private:
|
||||
typedef typename Base::LongestValueType LongestValueType;
|
||||
|
||||
|
Reference in New Issue
Block a user