]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tvalidat.tex
Addition at simple wxObject*, support into wxVariant.
[wxWidgets.git] / docs / latex / wx / tvalidat.tex
CommitLineData
ccaaf5b0 1\section{wxValidator overview}\label{validatoroverview}
a660d684 2
cba2db0c
JS
3Classes: \helpref{wxValidator}{wxvalidator}, \helpref{wxTextValidator}{wxtextvalidator},
4\helpref{wxGenericValidator}{wxgenericvalidator}
a660d684
KB
5
6The aim of the validator concept is to make dialogs very much easier to write.
7A validator is an object that can be plugged into a control (such as a wxTextCtrl), and
8mediates between C++ data and the control, transferring the data in either direction
9and validating it. It also is able to intercept events generated
10by the control, providing filtering behaviour without the need to derive a new control class.
11
cba2db0c
JS
12You can use a stock validator, such as \helpref{wxTextValidator}{wxtextvalidator} (which does text
13control data transfer, validation and filtering) and
14\helpref{wxGenericValidator}{wxgenericvalidator} (which does data transfer for a range of controls);
15or you can write your own.
a660d684
KB
16
17\wxheading{Example}
18
19Here is an example of wxTextValidator usage.
20
21\begin{verbatim}
22 wxTextCtrl *txt1 = new wxTextCtrl(this, VALIDATE_TEXT, "",
23 wxPoint(10, 10), wxSize(100, 80), 0,
24 wxTextValidator(wxFILTER_ALPHA, &g_data.m_string));
25\end{verbatim}
26
27In this example, the text validator object provides the following functionality:
28
29\begin{enumerate}\itemsep=0pt
30\item It transfers the value of g\_data.m\_string (a wxString variable) to the wxTextCtrl when
31the dialog is initialised.
32\item It transfers the wxTextCtrl data back to this variable when the dialog is dismissed.
33\item It filters input characters so that only alphabetic characters are allowed.
34\end{enumerate}
35
36The validation and filtering of input is accomplished in two ways. When a character is input,
37wxTextValidator checks the character against the allowed filter flag (wxFILTER\_ALPHA in this case). If
38the character is inappropriate, it is vetoed (does not appear) and a warning beep sounds.
39The second type of validation is performed when the dialog is about to be dismissed, so if
40the default string contained invalid characters already, a dialog box is shown giving the
41error, and the dialog is not dismissed.
42
43\wxheading{Anatomy of a validator}
44
45A programmer creating a new validator class should provide the following functionality.
46
47A validator constructor is responsible for allowing the programmer to specify the kind
48of validation required, and perhaps a pointer to a C++ variable that is used for storing the
49data for the control. If such a variable address is not supplied by the user, then
50the validator should store the data internally.
51
52The \helpref{wxValidator::Validate}{wxvalidatorvalidate} member function should return
cc81d32f 53true if the data in the control (not the C++ variable) is valid. It should also show
a660d684
KB
54an appropriate message if data was not valid.
55
56The \helpref{wxValidator::TransferToWindow}{wxvalidatortransfertowindow} member function should
57transfer the data from the validator or associated C++ variable to the control.
58
59The \helpref{wxValidator::TransferFromWindow}{wxvalidatortransferfromwindow} member function should
60transfer the data from the control to the validator or associated C++ variable.
61
62There should be a copy constructor, and a \helpref{wxValidator::Clone}{wxvalidatorclone} function
63which returns a copy of the validator object. This is important because validators
64are passed by reference to window constructors, and must therefore be cloned internally.
65
66You can optionally define event handlers for the validator, to implement filtering. These handlers
67will capture events before the control itself does.
68
69For an example implementation, see the valtext.h and valtext.cpp files in the wxWindows library.
70
71\wxheading{How validators interact with dialogs}
72
73For validators to work correctly, validator functions must be called at the right times during
74dialog initialisation and dismissal.
75
76When a \helpref{wxDialog::Show}{wxdialogshow} is called (for a modeless dialog)
77or \helpref{wxDialog::ShowModal}{wxdialogshowmodal} is called (for a modal dialog),
78the function \helpref{wxWindow::InitDialog}{wxwindowinitdialog} is automatically called.
79This in turn sends an initialisation event to the dialog. The default handler for
80the wxEVT\_INIT\_DIALOG event is defined in the wxWindow class to simply call
81the function \helpref{wxWindow::TransferDataToWindow}{wxwindowtransferdatatowindow}. This
82function finds all the validators in the window's children and calls the TransferToWindow
83function for each. Thus, data is transferred from C++ variables to the dialog
84just as the dialog is being shown.
85
86\normalbox{If you are using a window or panel instead of a dialog, you will need to
87call \helpref{wxWindow::InitDialog}{wxwindowinitdialog} explicitly before showing the
88window.}
89
90When the user clicks on a button, for example the OK button, the application should
cc81d32f 91first call \helpref{wxWindow::Validate}{wxwindowvalidate}, which returns false if
a660d684
KB
92any of the child window validators failed to validate the window data. The button handler
93should return immediately if validation failed. Secondly, the application should
94call \helpref{wxWindow::TransferDataFromWindow}{wxwindowtransferdatafromwindow} and
95return if this failed. It is then safe to end the dialog by calling EndModal (if modal)
96or Show (if modeless).
97
98In fact, wxDialog contains a default command event handler for the wxID\_OK button. It goes like
99this:
100
101\begin{verbatim}
102void wxDialog::OnOK(wxCommandEvent& event)
103{
104 if ( Validate() && TransferDataFromWindow() )
105 {
106 if ( IsModal() )
107 EndModal(wxID_OK);
108 else
109 {
110 SetReturnCode(wxID_OK);
cc81d32f 111 this->Show(false);
a660d684
KB
112 }
113 }
114}
115\end{verbatim}
116
117So if using validators and a normal OK button, you may not even need to write any
118code for handling dialog dismissal.
119
f6bcfd97 120If you load your dialog from a resource file, you will need to iterate through the controls
cba2db0c
JS
121setting validators, since validators can't be specified in a dialog resource.
122