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
)
35 : m_pDateTime(pval
), m_pCtrl(0)
39 DateTimeValidator(const DateTimeValidator
&other
)
40 : m_pDateTime(other
.m_pDateTime
), m_pCtrl(0)
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
)
106 : m_pEnum(pval
), m_pCtrl(0)
110 RadioBoxValidator(const RadioBoxValidator
&other
)
111 : m_codes(other
.m_codes
)
112 , m_pEnum(other
.m_pEnum
)
117 virtual wxValidator
* Clone() const
119 return new RadioBoxValidator(*this);
122 // return const reference to self to allow .Add().Add() series
124 const RadioBoxValidator
& Add(EnumT code
) const
126 m_codes
.push_back(code
);
130 bool CheckState() const
134 if( !m_validatorWindow
)
137 if( !m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
140 m_pCtrl
= dynamic_cast<wxRadioBox
*>(m_validatorWindow
);
144 bool IsSelectionValid() const
146 return std::find(m_codes
.begin(), m_codes
.end(), *m_pEnum
)
150 virtual bool Validate(wxWindow
*parent
)
152 // Validate() checks the *control's* value, not the
153 // in-memory value, according to the wxWidgets documentation,
154 // so do TransferFromWindow() first
155 if( !(TransferFromWindow() && IsSelectionValid()) ) {
157 _W("Please select one of the radio buttons."),
159 wxOK
| wxICON_INFORMATION
, parent
);
165 virtual bool TransferToWindow()
170 typename
std::vector
<EnumT
>::iterator i
= std::find(
171 m_codes
.begin(), m_codes
.end(), *m_pEnum
);
172 if( i
== m_codes
.end() )
174 m_pCtrl
->SetSelection(i
- m_codes
.begin());
178 virtual bool TransferFromWindow()
183 *m_pEnum
= m_codes
.at(m_pCtrl
->GetSelection());
188 template <class EnumT
>
189 RadioBoxValidator
<EnumT
> MakeRadioBoxValidator(EnumT
*pval
)
191 return RadioBoxValidator
<EnumT
>(pval
);