* Add special wxValidator derivation: wxNumericTextValidator

* This class validates input for wxTextCtrl to be numeric only
  * Also upon initialization of the window that contains the "validated" text controls; these controls will be filled with a number given by a passed pointer
  * Upon closing of this window the current value from these text controls will be written back into that pointer

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@2211 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2007-07-26 22:11:35 +00:00
parent 962a6db92c
commit 18bf3bfa9c
2 changed files with 264 additions and 0 deletions

View File

@ -0,0 +1,192 @@
/*
This file is part of Warzone 2100.
Copyright (C) 2007 Giel van Schijndel
Copyright (C) 2007 Warzone Resurrection Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
$Revision$
$Id$
$HeadURL$
*/
#include "numtextval.hpp"
#include <wx/string.h>
#include <wx/wxchar.h>
BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
EVT_CHAR(wxNumericTextValidator::OnChar)
END_EVENT_TABLE()
wxNumericTextValidator::wxNumericTextValidator(long* val) :
_longValue(val),
_UlongValue(NULL)
{
}
wxNumericTextValidator::wxNumericTextValidator(unsigned long* val) :
_longValue(NULL),
_UlongValue(val)
{
}
wxNumericTextValidator::wxNumericTextValidator(const wxNumericTextValidator& rhs) :
_longValue(rhs._longValue),
_UlongValue(rhs._UlongValue)
{
wxValidator::Copy(rhs);
}
const wxNumericTextValidator& wxNumericTextValidator::operator=(const wxNumericTextValidator& rhs)
{
wxValidator::Copy(rhs);
_longValue = rhs._longValue;
_UlongValue = rhs._UlongValue;
}
wxObject* wxNumericTextValidator::Clone() const;
{
return new wxNumericTextValidator(*this);
}
static inline bool wxIsNumeric(int keycode)
{
return keycode == wxT('.')
|| keycode == wxT(',')
|| keycode == wxT('e')
|| keycode == wxT('E')
|| keycode == wxT('+')
|| keycode == wxT('-')
|| wxIsdigit(keycode);
}
static bool wxIsNumeric(const wxString& val)
{
for (unsigned int i = 0; i < val.Length(); ++i)
{
// Allow for "," (French) as well as "." -- in future we should
// use wxSystemSettings or other to do better localisation
if (!wxIsNumeric(val[i]))
return false;
}
return true;
}
// Called when the value in the window must be validated.
// This function can pop up an error message.
bool wxNumericTextValidator::Validate(wxWindow *parent)
{
if (!CheckValidator())
return false;
wxTextCtrl* control = dynamic_cast<wxTextCtrl*>(m_validatorWindow);
if (!control)
return false;
// If window is disabled, simply return
if ( !control->IsEnabled() )
return true;
if (!wxIsNumeric(control->GetValue()))
{
m_validatorWindow->SetFocus();
wxString buf;
buf.Printf(_("'%s' should be numeric."), val.c_str());
wxMessageBox(buf, _("Validation conflict"),
wxOK | wxICON_EXCLAMATION, parent);
return false;
}
return true;
}
// Called to transfer data to the window
bool wxNumericTextValidator::TransferToWindow()
{
if (!CheckValidator())
return false;
if (!_longValue
&& !_UlongValue)
return true;
wxTextCtrl* control = dynamic_cast<wxTextCtrl*>(m_validatorWindow);
if (!control)
return false;
if (_longValue)
control->SetValue(wxString::Format("%d", *_longValue));
else
control->SetValue(wxString::Format("%u", *_UlongValue));
return true;
}
// Called to transfer data from the window
bool wxNumericTextValidator::TransferFromWindow()
{
if (!CheckValidator())
return false;
if (!_longValue
&& !_UlongValue)
return true;
wxTextCtrl* control = dynamic_cast<wxTextCtrl*>(m_validatorWindow);
if (!control)
return false;
if (_longValue)
control->ToLong(_longValue);
else
control->ToULong(_UlongValue);
return true;
}
// Filter keystrokes
void wxNumericTextValidator::OnChar(wxKeyEvent& event)
{
if (!m_validatorWindow)
return;
int keycode = event.GetKeyCode();
// we don't filter special keys and Delete
if (keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START)
{
// Don't disable following event handlers in the chain (i.e. use the key)
event.Skip();
return;
}
if (!wxIsNumeric(keycode))
{
if (!wxValidator::IsSilent())
wxBell();
// eat message
return;
}
// Don't disable following event handlers in the chain (i.e. use the key)
event.Skip();
}

View File

@ -0,0 +1,72 @@
/*
This file is part of Warzone 2100.
Copyright (C) 2007 Giel van Schijndel
Copyright (C) 2007 Warzone Resurrection Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
$Revision$
$Id$
$HeadURL$
*/
#ifndef __INCLUDE_NUMTEXTVAL_HPP__
#define __INCLUDE_NUMTEXTVAL_HPP__
#include <wx/wxprec.h>
#include <wx/textctrl.h>
#include <wx/validate.h>
class wxNumericTextValidator : public wxValidator
{
public:
wxNumericTextValidator(long* val = NULL);
wxNumericTextValidator(unsigned long* val = NULL);
wxNumericTextValidator(const wxNumericTextValidator& rhs);
const wxNumericTextValidator& operator=(const wxNumericTextValidator& rhs);
virtual wxObject* Clone() const;
// Called when the value in the window must be validated.
// This function can pop up an error message.
virtual bool Validate(wxWindow *parent)
// Called to transfer data to the window
virtual bool TransferToWindow();
// Called to transfer data from the window
virtual bool TransferFromWindow();
// Filter keystrokes
void OnChar(wxKeyEvent& event);
protected:
inline bool CheckValidator() const
{
wxCHECK_MSG( m_validatorWindow, false,
_T("No window associated with validator") );
wxCHECK_MSG( m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)), false,
_T("wxNumericTextValidator is only for wxTextCtrl's") );
return true;
}
private:
long* _longValue;
unsigned long* _UlongValue;
};
#endif // __INCLUDE_NUMTEXTVAL_HPP__