]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/validator.h
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / docs / doxygen / overviews / validator.h
CommitLineData
15b6757b 1/////////////////////////////////////////////////////////////////////////////
3863c5eb 2// Name: validator.h
15b6757b
FM
3// Purpose: topic overview
4// Author: wxWidgets team
526954c5 5// Licence: wxWindows licence
15b6757b
FM
6/////////////////////////////////////////////////////////////////////////////
7
880efa2a 8/**
36c9828f 9
3863c5eb 10@page overview_validator wxValidator Overview
36c9828f 11
831e1028 12@tableofcontents
141794f1 13
3863c5eb
BP
14The aim of the validator concept is to make dialogs very much easier to write.
15A validator is an object that can be plugged into a control (such as a
16wxTextCtrl), and mediates between C++ data and the control, transferring the
17data in either direction and validating it. It also is able to intercept events
18generated by the control, providing filtering behaviour without the need to
19derive a new control class.
20
21You can use a stock validator, such as wxTextValidator (which does text control
22data transfer, validation and filtering) and wxGenericValidator (which does
23data transfer for a range of controls); or you can write your own.
24
3863c5eb
BP
25Here is an example of wxTextValidator usage.
26
27@code
28wxTextCtrl *txt1 = new wxTextCtrl(
141794f1 29 this, -1, wxT(""), wxDefaultPosition, wxDefaultSize, 0,
3863c5eb
BP
30 wxTextValidator(wxFILTER_ALPHA, &g_data.m_string));
31@endcode
32
33In this example, the text validator object provides the following
34functionality:
35
36@li It transfers the value of g_data.m_string (a wxString variable) to the
37 wxTextCtrl when the dialog is initialised.
38@li It transfers the wxTextCtrl data back to this variable when the dialog is
39 dismissed.
40@li It filters input characters so that only alphabetic characters are allowed.
41
42The validation and filtering of input is accomplished in two ways. When a
43character is input, wxTextValidator checks the character against the allowed
141794f1
FM
44filter flag (@c wxFILTER_ALPHA in this case). If the character is inappropriate,
45it is vetoed (does not appear) and a warning beep sounds (unless
46wxValidator::SetBellOnError(false) has been called).
47The second type of validation is performed when the dialog is about to be dismissed,
48so if the default string contained invalid characters already, a dialog box is shown
3863c5eb
BP
49giving the error, and the dialog is not dismissed.
50
141794f1
FM
51Note that any wxWindow may have a validator; using the @c wxWS_EX_VALIDATE_RECURSIVELY
52style (see wxWindow extended styles) you can also implement recursive validation.
53
831e1028
BP
54@see wxValidator, wxTextValidator, wxGenericValidator, wxIntegerValidator,
55 wxFloatingPointValidator
56
57
3863c5eb
BP
58
59@section overview_validator_anatomy Anatomy of a Validator
60
61A programmer creating a new validator class should provide the following
62functionality.
63
64A validator constructor is responsible for allowing the programmer to specify
65the kind of validation required, and perhaps a pointer to a C++ variable that
66is used for storing the data for the control. If such a variable address is not
67supplied by the user, then the validator should store the data internally.
68
69The wxValidator::Validate member function should return @true if the data in
70the control (not the C++ variable) is valid. It should also show an appropriate
71message if data was not valid.
72
73The wxValidator::TransferToWindow member function should transfer the data from
74the validator or associated C++ variable to the control.
75
76The wxValidator::TransferFromWindow member function should transfer the data
77from the control to the validator or associated C++ variable.
78
79There should be a copy constructor, and a wxValidator::Clone function which
80returns a copy of the validator object. This is important because validators
81are passed by reference to window constructors, and must therefore be cloned
82internally.
83
84You can optionally define event handlers for the validator, to implement
141794f1 85filtering. These handlers will capture events before the control itself does
830b7aa7 86(see @ref overview_events_processing).
141794f1 87For an example implementation, see the @c valtext.h and @c valtext.cpp files in the
3863c5eb
BP
88wxWidgets library.
89
90
91@section overview_validator_dialogs How Validators Interact with Dialogs
92
93For validators to work correctly, validator functions must be called at the
94right times during dialog initialisation and dismissal.
95
96When a wxDialog::Show is called (for a modeless dialog) or wxDialog::ShowModal
97is called (for a modal dialog), the function wxWindow::InitDialog is
98automatically called. This in turn sends an initialisation event to the dialog.
141794f1
FM
99The default handler for the @c wxEVT_INIT_DIALOG event is defined in the wxWindow
100class to simply call the function wxWindow::TransferDataToWindow.
101This function finds all the validators in the window's children and calls the
102wxValidator::TransferToWindow function for each. Thus, data is transferred from C++
3863c5eb
BP
103variables to the dialog just as the dialog is being shown.
104
105@note If you are using a window or panel instead of a dialog, you will need to
106call wxWindow::InitDialog explicitly before showing the window.
107
108When the user clicks on a button, for example the OK button, the application
109should first call wxWindow::Validate, which returns @false if any of the child
110window validators failed to validate the window data. The button handler should
111return immediately if validation failed. Secondly, the application should call
112wxWindow::TransferDataFromWindow and return if this failed. It is then safe to
141794f1 113end the dialog by calling wxDialog::EndModal (if modal) or wxDialog::Show (if modeless).
3863c5eb 114
141794f1 115In fact, wxDialog contains a default command event handler for the @c wxID_OK
3863c5eb
BP
116button. It goes like this:
117
118@code
119void wxDialog::OnOK(wxCommandEvent& event)
120{
121 if ( Validate() && TransferDataFromWindow() )
122 {
123 if ( IsModal() )
124 EndModal(wxID_OK);
125 else
126 {
127 SetReturnCode(wxID_OK);
128 this->Show(false);
129 }
130 }
131}
132@endcode
133
134So if using validators and a normal OK button, you may not even need to write
135any code for handling dialog dismissal.
136
137If you load your dialog from a resource file, you will need to iterate through
138the controls setting validators, since validators can't be specified in a
139dialog resource.
140
141*/