Added a copy constructor to the numeric validator, to hopefully fix the issues that xat had with it on Linux. Also, made it enforce stricter standards (that is, not allowing you to type numbers before the sign)

Originally committed to SVN as r1252.
This commit is contained in:
Rodrigo Braz Monteiro 2007-06-18 18:20:45 +00:00
parent b1b89523d0
commit 5d24b07b50
2 changed files with 21 additions and 6 deletions

View File

@ -61,6 +61,16 @@ NumValidator::NumValidator(wxString* _valPtr,bool isfloat,bool issigned) {
}
////////////////////
// Copy constructor
NumValidator::NumValidator(const NumValidator &from) {
valPtr = from.valPtr;
isFloat = from.isFloat;
isSigned = from.isSigned;
SetWindow(from.GetWindow());
}
///////////////
// Event table
BEGIN_EVENT_TABLE(NumValidator, wxValidator)
@ -91,7 +101,7 @@ bool NumValidator::Validate(wxWindow* parent) {
// Check each character
bool gotDecimal = false;
for (size_t i=0;i<value.Length();i++) {
if (!CheckCharacter(value[i],i==0,gotDecimal)) return false;
if (!CheckCharacter(value[i],i==0,true,gotDecimal)) return false;
}
// All clear
@ -101,13 +111,16 @@ bool NumValidator::Validate(wxWindow* parent) {
///////////////////////////////////////
// Check if a given character is valid
bool NumValidator::CheckCharacter(int chr,bool isFirst,bool &gotDecimal) {
bool NumValidator::CheckCharacter(int chr,bool isFirst,bool canSign,bool &gotDecimal) {
// Check sign
if (chr == _T('-') || chr == _T('+')) {
if (!isFirst || !isSigned) return false;
if (!isFirst || !canSign || !isSigned) return false;
else return true;
}
// Don't allow anything before a sign
if (isFirst && !canSign) return false;
// Check decimal point
if (chr == _T('.') || chr == _T(',')) {
if (!isFloat || gotDecimal) return false;
@ -151,10 +164,11 @@ void NumValidator::OnChar(wxKeyEvent& event) {
if (curchr == _T('+') || curchr == _T('-')) signs++;
}
bool gotDecimal = decimals > 0;
bool canSign = from == 0 && signs == 0;
bool isFirst = from == 0;
bool canSign = signs == 0;
// Check character
if (!CheckCharacter(chr,canSign,gotDecimal)) {
if (!CheckCharacter(chr,isFirst,canSign,gotDecimal)) {
if (!wxValidator::IsSilent()) wxBell();
return;
}

View File

@ -58,12 +58,13 @@ private:
bool TransferToWindow();
bool TransferFromWindow();
bool CheckCharacter(int chr,bool isFirst,bool &gotDecimal);
bool CheckCharacter(int chr,bool isFirst,bool canSign,bool &gotDecimal);
void OnChar(wxKeyEvent& event);
public:
NumValidator(wxString* valPtr = NULL,bool isfloat=false,bool issigned=false);
NumValidator(const NumValidator& from);
DECLARE_EVENT_TABLE();
};