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