3 /// Homemade validators derived from wxValidator
7 Copyright (C) 2012, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRYDESKTOP_WXVAL_H__
23 #define __BARRYDESKTOP_WXVAL_H__
27 class DateTimeValidator
: public wxValidator
29 wxDateTime
*m_pDateTime
;
30 mutable wxDatePickerCtrl
*m_pCtrl
;
33 explicit DateTimeValidator(wxDateTime
*pval
)
38 DateTimeValidator(const DateTimeValidator
&other
)
39 : m_pDateTime(other
.m_pDateTime
)
43 virtual wxValidator
* Clone() const
45 return new DateTimeValidator(*this);
48 bool CheckState() const
52 if( !m_validatorWindow
)
55 if( !m_validatorWindow
->IsKindOf(CLASSINFO(wxDatePickerCtrl
)) )
58 m_pCtrl
= dynamic_cast<wxDatePickerCtrl
*>(m_validatorWindow
);
62 virtual bool Validate(wxWindow
*parent
)
64 // Validate() checks the *control's* value, not the
65 // in-memory value, according to the wxWidgets documentation,
66 // so do TransferFromWindow() first
67 if( !(TransferFromWindow() && m_pDateTime
->IsValid()) ) {
68 wxMessageBox(_T("Invalid date!"), _T("Validation"),
69 wxOK
| wxICON_INFORMATION
, parent
);
75 virtual bool TransferToWindow()
80 if( !m_pDateTime
->IsValid() )
82 m_pCtrl
->SetValue(*m_pDateTime
);
86 virtual bool TransferFromWindow()
91 *m_pDateTime
= m_pCtrl
->GetValue();
96 template <class EnumT
>
97 class RadioBoxValidator
: public wxValidator
99 mutable std::vector
<EnumT
> m_codes
;
101 mutable wxRadioBox
*m_pCtrl
;
104 explicit RadioBoxValidator(EnumT
*pval
)
109 RadioBoxValidator(const RadioBoxValidator
&other
)
110 : m_codes(other
.m_codes
)
111 , m_pEnum(other
.m_pEnum
)
115 virtual wxValidator
* Clone() const
117 return new RadioBoxValidator(*this);
120 // return const reference to self to allow .Add().Add() series
122 const RadioBoxValidator
& Add(EnumT code
) const
124 m_codes
.push_back(code
);
128 bool CheckState() const
132 if( !m_validatorWindow
)
135 if( !m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
138 m_pCtrl
= dynamic_cast<wxRadioBox
*>(m_validatorWindow
);
142 bool IsSelectionValid() const
144 return std::find(m_codes
.begin(), m_codes
.end(), *m_pEnum
)
148 virtual bool Validate(wxWindow
*parent
)
150 // Validate() checks the *control's* value, not the
151 // in-memory value, according to the wxWidgets documentation,
152 // so do TransferFromWindow() first
153 if( !(TransferFromWindow() && IsSelectionValid()) ) {
155 _T("Please select one of the radio buttons."),
157 wxOK
| wxICON_INFORMATION
, parent
);
163 virtual bool TransferToWindow()
168 typename
std::vector
<EnumT
>::iterator i
= std::find(
169 m_codes
.begin(), m_codes
.end(), *m_pEnum
);
170 if( i
== m_codes
.end() )
172 m_pCtrl
->SetSelection(i
- m_codes
.begin());
176 virtual bool TransferFromWindow()
181 *m_pEnum
= m_codes
.at(m_pCtrl
->GetSelection());
186 template <class EnumT
>
187 RadioBoxValidator
<EnumT
> MakeRadioBoxValidator(EnumT
*pval
)
189 return RadioBoxValidator
<EnumT
>(pval
);