Bumped copyright dates for 2013
[barry.git] / desktop / src / wxval.h
blob180dee59f0a5704e2c8e4f9aa1da8a1183199ad6
1 ///
2 /// \file wxval.h
3 /// Homemade validators derived from wxValidator
4 ///
6 /*
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__
25 #include <algorithm>
26 #include "wxi18n.h"
28 class DateTimeValidator : public wxValidator
30 wxDateTime *m_pDateTime;
31 mutable wxDatePickerCtrl *m_pCtrl;
33 public:
34 explicit DateTimeValidator(wxDateTime *pval)
35 : m_pDateTime(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
51 if( !m_pDateTime )
52 return false;
53 if( !m_validatorWindow )
54 return false;
56 if( !m_validatorWindow->IsKindOf(CLASSINFO(wxDatePickerCtrl)) )
57 return false;
59 m_pCtrl = dynamic_cast<wxDatePickerCtrl*>(m_validatorWindow);
60 return m_pCtrl;
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);
71 return false;
73 return true;
76 virtual bool TransferToWindow()
78 if( !CheckState() )
79 return false;
81 if( !m_pDateTime->IsValid() )
82 return false;
83 m_pCtrl->SetValue(*m_pDateTime);
84 return true;
87 virtual bool TransferFromWindow()
89 if( !CheckState() )
90 return false;
92 *m_pDateTime = m_pCtrl->GetValue();
93 return true;
97 template <class EnumT>
98 class RadioBoxValidator : public wxValidator
100 mutable std::vector<EnumT> m_codes;
101 EnumT *m_pEnum;
102 mutable wxRadioBox *m_pCtrl;
104 public:
105 explicit RadioBoxValidator(EnumT *pval)
106 : m_pEnum(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
122 // on constructor
123 const RadioBoxValidator& Add(EnumT code) const
125 m_codes.push_back(code);
126 return *this;
129 bool CheckState() const
131 if( !m_pEnum )
132 return false;
133 if( !m_validatorWindow )
134 return false;
136 if( !m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
137 return false;
139 m_pCtrl = dynamic_cast<wxRadioBox*>(m_validatorWindow);
140 return m_pCtrl;
143 bool IsSelectionValid() const
145 return std::find(m_codes.begin(), m_codes.end(), *m_pEnum)
146 != m_codes.end();
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()) ) {
155 wxMessageBox(
156 _W("Please select one of the radio buttons."),
157 _W("Validation"),
158 wxOK | wxICON_INFORMATION, parent);
159 return false;
161 return true;
164 virtual bool TransferToWindow()
166 if( !CheckState() )
167 return false;
169 typename std::vector<EnumT>::iterator i = std::find(
170 m_codes.begin(), m_codes.end(), *m_pEnum);
171 if( i == m_codes.end() )
172 return false;
173 m_pCtrl->SetSelection(i - m_codes.begin());
174 return true;
177 virtual bool TransferFromWindow()
179 if( !CheckState() )
180 return false;
182 *m_pEnum = m_codes.at(m_pCtrl->GetSelection());
183 return true;
187 template <class EnumT>
188 RadioBoxValidator<EnumT> MakeRadioBoxValidator(EnumT *pval)
190 return RadioBoxValidator<EnumT>(pval);
193 #endif