3 /// Homemade validators derived from wxValidator
7 Copyright (C) 2012-2013, 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__
28 class DateTimeValidator
: public wxValidator
30 wxDateTime
*m_pDateTime
;
31 mutable wxDatePickerCtrl
*m_pCtrl
;
34 explicit DateTimeValidator(wxDateTime
*pval
)
39 DateTimeValidator(const DateTimeValidator
&other
)
40 : m_pDateTime(other
.m_pDateTime
)
44 virtual wxValidator
* Clone() const
46 return new DateTimeValidator(*this);
49 bool CheckState() const
53 if( !m_validatorWindow
)
56 if( !m_validatorWindow
->IsKindOf(CLASSINFO(wxDatePickerCtrl
)) )
59 m_pCtrl
= dynamic_cast<wxDatePickerCtrl
*>(m_validatorWindow
);
63 virtual bool Validate(wxWindow
*parent
)
65 // Validate() checks the *control's* value, not the
66 // in-memory value, according to the wxWidgets documentation,
67 // so do TransferFromWindow() first
68 if( !(TransferFromWindow() && m_pDateTime
->IsValid()) ) {
69 wxMessageBox(_W("Invalid date!"), _W("Validation"),
70 wxOK
| wxICON_INFORMATION
, parent
);
76 virtual bool TransferToWindow()
81 if( !m_pDateTime
->IsValid() )
83 m_pCtrl
->SetValue(*m_pDateTime
);
87 virtual bool TransferFromWindow()
92 *m_pDateTime
= m_pCtrl
->GetValue();
97 template <class EnumT
>
98 class RadioBoxValidator
: public wxValidator
100 mutable std::vector
<EnumT
> m_codes
;
102 mutable wxRadioBox
*m_pCtrl
;
105 explicit RadioBoxValidator(EnumT
*pval
)
110 RadioBoxValidator(const RadioBoxValidator
&other
)
111 : m_codes(other
.m_codes
)
112 , m_pEnum(other
.m_pEnum
)
116 virtual wxValidator
* Clone() const
118 return new RadioBoxValidator(*this);
121 // return const reference to self to allow .Add().Add() series
123 const RadioBoxValidator
& Add(EnumT code
) const
125 m_codes
.push_back(code
);
129 bool CheckState() const
133 if( !m_validatorWindow
)
136 if( !m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
139 m_pCtrl
= dynamic_cast<wxRadioBox
*>(m_validatorWindow
);
143 bool IsSelectionValid() const
145 return std::find(m_codes
.begin(), m_codes
.end(), *m_pEnum
)
149 virtual bool Validate(wxWindow
*parent
)
151 // Validate() checks the *control's* value, not the
152 // in-memory value, according to the wxWidgets documentation,
153 // so do TransferFromWindow() first
154 if( !(TransferFromWindow() && IsSelectionValid()) ) {
156 _W("Please select one of the radio buttons."),
158 wxOK
| wxICON_INFORMATION
, parent
);
164 virtual bool TransferToWindow()
169 typename
std::vector
<EnumT
>::iterator i
= std::find(
170 m_codes
.begin(), m_codes
.end(), *m_pEnum
);
171 if( i
== m_codes
.end() )
173 m_pCtrl
->SetSelection(i
- m_codes
.begin());
177 virtual bool TransferFromWindow()
182 *m_pEnum
= m_codes
.at(m_pCtrl
->GetSelection());
187 template <class EnumT
>
188 RadioBoxValidator
<EnumT
> MakeRadioBoxValidator(EnumT
*pval
)
190 return RadioBoxValidator
<EnumT
>(pval
);