validate.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/validate.h
  3. // Purpose: wxValidator class
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 29/01/98
  7. // Copyright: (c) 1998 Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_VALIDATE_H_
  11. #define _WX_VALIDATE_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_VALIDATORS
  14. #include "wx/event.h"
  15. class WXDLLIMPEXP_FWD_CORE wxWindow;
  16. class WXDLLIMPEXP_FWD_CORE wxWindowBase;
  17. /*
  18. A validator has up to three purposes:
  19. 1) To validate the data in the window that's associated
  20. with the validator.
  21. 2) To transfer data to and from the window.
  22. 3) To filter input, using its role as a wxEvtHandler
  23. to intercept e.g. OnChar.
  24. Note that wxValidator and derived classes use reference counting.
  25. */
  26. class WXDLLIMPEXP_CORE wxValidator : public wxEvtHandler
  27. {
  28. public:
  29. wxValidator();
  30. wxValidator(const wxValidator& other)
  31. : wxEvtHandler()
  32. , m_validatorWindow(other.m_validatorWindow)
  33. {
  34. }
  35. virtual ~wxValidator();
  36. // Make a clone of this validator (or return NULL) - currently necessary
  37. // if you're passing a reference to a validator.
  38. // Another possibility is to always pass a pointer to a new validator
  39. // (so the calling code can use a copy constructor of the relevant class).
  40. virtual wxObject *Clone() const
  41. { return NULL; }
  42. bool Copy(const wxValidator& val)
  43. { m_validatorWindow = val.m_validatorWindow; return true; }
  44. // Called when the value in the window must be validated.
  45. // This function can pop up an error message.
  46. virtual bool Validate(wxWindow *WXUNUSED(parent)) { return false; }
  47. // Called to transfer data to the window
  48. virtual bool TransferToWindow() { return false; }
  49. // Called to transfer data from the window
  50. virtual bool TransferFromWindow() { return false; }
  51. // accessors
  52. wxWindow *GetWindow() const { return (wxWindow *)m_validatorWindow; }
  53. void SetWindow(wxWindowBase *win) { m_validatorWindow = win; }
  54. // validators beep by default if invalid key is pressed, this function
  55. // allows to change this
  56. static void SuppressBellOnError(bool suppress = true)
  57. { ms_isSilent = suppress; }
  58. // test if beep is currently disabled
  59. static bool IsSilent() { return ms_isSilent; }
  60. // this function is deprecated because it handled its parameter
  61. // unnaturally: it disabled the bell when it was true, not false as could
  62. // be expected; use SuppressBellOnError() instead
  63. #if WXWIN_COMPATIBILITY_2_8
  64. static wxDEPRECATED_INLINE(
  65. void SetBellOnError(bool doIt = true),
  66. ms_isSilent = doIt;
  67. )
  68. #endif
  69. protected:
  70. wxWindowBase *m_validatorWindow;
  71. private:
  72. static bool ms_isSilent;
  73. DECLARE_DYNAMIC_CLASS(wxValidator)
  74. wxDECLARE_NO_ASSIGN_CLASS(wxValidator);
  75. };
  76. extern WXDLLIMPEXP_DATA_CORE(const wxValidator) wxDefaultValidator;
  77. #define wxVALIDATOR_PARAM(val) val
  78. #else // !wxUSE_VALIDATORS
  79. // wxWidgets is compiled without support for wxValidator, but we still
  80. // want to be able to pass wxDefaultValidator to the functions which take
  81. // a wxValidator parameter to avoid using "#if wxUSE_VALIDATORS"
  82. // everywhere
  83. class WXDLLIMPEXP_FWD_CORE wxValidator;
  84. static const wxValidator* const wxDefaultValidatorPtr = NULL;
  85. #define wxDefaultValidator (*wxDefaultValidatorPtr)
  86. // this macro allows to avoid warnings about unused parameters when
  87. // wxUSE_VALIDATORS == 0
  88. #define wxVALIDATOR_PARAM(val)
  89. #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
  90. #endif // _WX_VALIDATE_H_