validator.h 5.9 KB

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