tzwrapper.cc: fixed use of iterator after erase
[barry.git] / desktop / src / wxval.h
blob86eef2f91060fc4419039e840b44e1fc890e1b12
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), 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
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), m_pCtrl(0)
110 RadioBoxValidator(const RadioBoxValidator &other)
111 : m_codes(other.m_codes)
112 , m_pEnum(other.m_pEnum)
113 , m_pCtrl(0)
117 virtual wxValidator* Clone() const
119 return new RadioBoxValidator(*this);
122 // return const reference to self to allow .Add().Add() series
123 // on constructor
124 const RadioBoxValidator& Add(EnumT code) const
126 m_codes.push_back(code);
127 return *this;
130 bool CheckState() const
132 if( !m_pEnum )
133 return false;
134 if( !m_validatorWindow )
135 return false;
137 if( !m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
138 return false;
140 m_pCtrl = dynamic_cast<wxRadioBox*>(m_validatorWindow);
141 return m_pCtrl;
144 bool IsSelectionValid() const
146 return std::find(m_codes.begin(), m_codes.end(), *m_pEnum)
147 != m_codes.end();
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()) ) {
156 wxMessageBox(
157 _W("Please select one of the radio buttons."),
158 _W("Validation"),
159 wxOK | wxICON_INFORMATION, parent);
160 return false;
162 return true;
165 virtual bool TransferToWindow()
167 if( !CheckState() )
168 return false;
170 typename std::vector<EnumT>::iterator i = std::find(
171 m_codes.begin(), m_codes.end(), *m_pEnum);
172 if( i == m_codes.end() )
173 return false;
174 m_pCtrl->SetSelection(i - m_codes.begin());
175 return true;
178 virtual bool TransferFromWindow()
180 if( !CheckState() )
181 return false;
183 *m_pEnum = m_codes.at(m_pCtrl->GetSelection());
184 return true;
188 template <class EnumT>
189 RadioBoxValidator<EnumT> MakeRadioBoxValidator(EnumT *pval)
191 return RadioBoxValidator<EnumT>(pval);
194 #endif