My wxHexTextValidator Topic is solved

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
shaohao
Earned a small fee
Earned a small fee
Posts: 16
Joined: Thu Nov 23, 2006 2:19 pm
Location: Shanghai, China
Contact:

My wxHexTextValidator

Post by shaohao » Sat Dec 01, 2007 4:16 pm

Here is my HexText Validator.

Have a fun:)

Code: Select all

class wxHexTextValidator : public wxValidator
{
protected:
	int m_maxLen, m_minLen;
	wxString * m_strValue;
public:
	wxHexTextValidator(wxString *val, int maxLen = -1, int minLen = 0)
		: m_strValue(val)
		, m_maxLen(maxLen), m_minLen(minLen)
	{
	}
	wxHexTextValidator(const wxHexTextValidator & val)
		: wxValidator()
	{
		Copy(val);
	}
	// Make a clone of this validator (or return NULL) - currently necessary
	// if you're passing a reference to a validator.
	// Another possibility is to always pass a pointer to a new validator
	// (so the calling code can use a copy constructor of the relevant class).
	wxObject* Clone() const // Note the 'const' here
	{
		return new wxHexTextValidator(*this);
	}
	bool Copy(const wxHexTextValidator& val)
	{
		wxValidator::Copy(val);
		m_strValue = val.m_strValue;
		m_minLen = val.m_minLen;
		m_maxLen = val.m_maxLen;
		return true;
	}

	// Called to transfer data from the window
	bool TransferFromWindow()
	{
		if( !CheckValidator()) return false;
		if ( m_strValue) *m_strValue = ((wxTextCtrl *)m_validatorWindow)->GetValue();
		return true;
	}
	// Called to transfer data to the window
	bool TransferToWindow()
	{
		if( !CheckValidator()) return false;
		if ( m_strValue) ((wxTextCtrl *)m_validatorWindow)->SetValue( (*m_strValue).Upper());
		return true;
	}
	// Called when the value in the window must be validated.
	// This function can pop up an error message.
	bool Validate(wxWindow* parent)
	{
		wxString val(((wxTextCtrl*)m_validatorWindow)->GetValue());

		// valid the length
		size_t len = val.Len();
		if ( len < m_minLen || (m_maxLen > 0 && len > m_maxLen))
			return false;

		// valid the value
		for ( size_t i = 0; i < val.Len(); ++i) {
			if ( !wxIsxdigit(val[i])) return false;
		}
		return true;
	}
	bool CheckValidator() const
	{
		wxCHECK_MSG(m_validatorWindow,false,_T("No window associated with validator"));
		wxCHECK_MSG(m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)),false,_T("wxTextValidator is only for wxTextCtrl's"));
		return true;
	}
	void OnChar(wxKeyEvent& event)
	{
		if ( m_validatorWindow ) {
			wxTextCtrl * ctrl = (wxTextCtrl *)m_validatorWindow;
			size_t len = ctrl->GetValue().Len() - ctrl->GetStringSelection().Len();
			int keyCode = event.GetKeyCode();

			// we don't filter special keys and Delete
			if ( keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) {
				event.Skip();
				return ;
			}

			// validate length & character/input
			if ( 
				(m_maxLen > 0 &&  (len + 1) > m_maxLen) // valid length
				||
				!wxIsxdigit(keyCode) // character
			   )
			{
				if ( !wxValidator::IsSilent()) wxBell();
				// eat message
				return ;
			}

			// Uppercase
			if ( wxIsxdigit(keyCode)) {
				wxChar c = wxToupper(keyCode);
				ctrl->WriteText( c);
				// eat msg! we have done it
				return;
			}
		}
	}
DECLARE_EVENT_TABLE();
};

Code: Select all

BEGIN_EVENT_TABLE(wxHexTextValidator, wxValidator)
	EVT_CHAR(wxHexTextValidator::OnChar)
END_EVENT_TABLE()

Troels
Experienced Solver
Experienced Solver
Posts: 79
Joined: Fri Jan 07, 2005 12:02 pm
Location: Denmark

Re: My wxHexTextValidator

Post by Troels » Thu Feb 21, 2008 9:26 am

Neat. If you used Connect(), you could cut the cpp part, DECLARE_EVENT_TABLE/BEGIN_EVENT_TABLE/END_EVENT_TABLE.
Regards

priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia » Thu Feb 21, 2008 12:59 pm

You can just use the wxRegexValidator, instead of writing a new class for HEX characters validation.

Post Reply