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