Lazy regex validator

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
vasek
Experienced Solver
Experienced Solver
Posts: 77
Joined: Wed Jul 27, 2005 2:52 pm
Location: Ostrava, Czech Republic
Contact:

Lazy regex validator

Post by vasek » Wed Sep 14, 2005 9:58 am

vxRegExValidator.h

Code: Select all

#include "wx\wx.h"
#include "wx\regex.h"

class vxRegExValidator :
	public wxTextValidator
{
protected:
	wxRegEx m_regEx;
	wxString m_reString;

public:
	vxRegExValidator(wxString regExpString, wxString* valPtr = NULL);
	~vxRegExValidator(void);

	wxObject* Clone() const;

	virtual bool TransferToWindow();
	virtual bool TransferFromWindow(void);
};
vxRegExValidator.cpp

Code: Select all

#include ".\vxregexvalidator.h"

vxRegExValidator::vxRegExValidator(wxString regExpString, wxString* valPtr) : m_regEx(regExpString)
{
	m_stringValue = valPtr;
	m_reString = regExpString;
}

vxRegExValidator::~vxRegExValidator(void) {}

wxObject* vxRegExValidator::Clone() const
{
	return new vxRegExValidator(m_reString, m_stringValue);
}

bool vxRegExValidator::TransferToWindow()
{
	if (m_stringValue == NULL)
		return true;

	if (m_regEx.Matches(*m_stringValue))
		return wxTextValidator::TransferToWindow();
	
	return false;
}

bool vxRegExValidator::TransferFromWindow(void)
{
	wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
	wxString tempString = control->GetValue();

	if (m_regEx.Matches(tempString))
		return wxTextValidator::TransferFromWindow();

    return false;
}

Post Reply