wxvalidator overviewclasses: wxvalidator, wxtextvalidator, wxgenericvalidator the aim of the validator concept is to make dialogs very much easier to write. a validator is an object that can be plugged into a control (such as a wxtextctrl), and mediates between c++ data and the control, transferring the data in either direction and validating it. it also is able to intercept events generated by the control, providing filtering behaviour without the need to derive a new control class. you can use a stock validator, such as wxtextvalidator (which does text control data transfer, validation and filtering) and wxgenericvalidator (which does data transfer for a range of controls); or you can write your own. example here is an example of wxtextvalidator usage.
wxtextctrl *txt1 = new wxtextctrl(this, -1, wxt(""), wxpoint(10, 10), wxsize(100, 80), 0, wxtextvalidator(wxfilter_alpha, &g_data.m_string));in this example, the text validator object provides the following functionality:
the validation and filtering of input is accomplished in two ways. when a character is input, wxtextvalidator checks the character against the allowed filter flag (wxfilter_alpha in this case). if the character is inappropriate, it is vetoed (does not appear) and a warning beep sounds. the second type of validation is performed when the dialog is about to be dismissed, so if the default string contained invalid characters already, a dialog box is shown giving the error, and the dialog is not dismissed. anatomy of a validator a programmer creating a new validator class should provide the following functionality. a validator constructor is responsible for allowing the programmer to specify the kind of validation required, and perhaps a pointer to a c++ variable that is used for storing the data for the control. if such a variable address is not supplied by the user, then the validator should store the data internally. the wxvalidator::validate member function should return true if the data in the control (not the c++ variable) is valid. it should also show an appropriate message if data was not valid. the wxvalidator::transfertowindow member function should transfer the data from the validator or associated c++ variable to the control. the wxvalidator::transferfromwindow member function should transfer the data from the control to the validator or associated c++ variable. there should be a copy constructor, and a wxvalidator::clone function which returns a copy of the validator object. this is important because validators are passed by reference to window constructors, and must therefore be cloned internally. you can optionally define event handlers for the validator, to implement filtering. these handlers will capture events before the control itself does. for an example implementation, see the valtext.h and valtext.cpp files in the wxwidgets library. how validators interact with dialogs for validators to work correctly, validator functions must be called at the right times during dialog initialisation and dismissal. when a wxdialog::show is called (for a modeless dialog) or wxdialog::showmodal is called (for a modal dialog), the function wxwindow::initdialog is automatically called. this in turn sends an initialisation event to the dialog. the default handler for the wxevt_init_dialog event is defined in the wxwindow class to simply call the function wxwindow::transferdatatowindow. this function finds all the validators in the window's children and calls the transfertowindow function for each. thus, data is transferred from c++ variables to the dialog just as the dialog is being shown.
if you are using a window or panel instead of a dialog, you will need to call wxwindow::initdialog explicitly before showing the window.
when the user clicks on a button, for example the ok button, the application should first call wxwindow::validate, which returns false if any of the child window validators failed to validate the window data. the button handler should return immediately if validation failed. secondly, the application should call wxwindow::transferdatafromwindow and return if this failed. it is then safe to end the dialog by calling endmodal (if modal) or show (if modeless). in fact, wxdialog contains a default command event handler for the wxid_ok button. it goes like this:
void wxdialog::onok(wxcommandevent& event) { if ( validate() && transferdatafromwindow() ) { if ( ismodal() ) endmodal(wxid_ok); else { setreturncode(wxid_ok); this->show(false); } } }so if using validators and a normal ok button, you may not even need to write any code for handling dialog dismissal. if you load your dialog from a resource file, you will need to iterate through the controls setting validators, since validators can't be specified in a dialog resource.
|