debian: made the changelog more verbose as per intrigeri's feedback
[barry.git] / desktop / src / wxval.h
blob1e02a116d5d8646bf63b8008ef19f0c73d26c5d9
1 ///
2 /// \file wxval.h
3 /// Homemade validators derived from wxValidator
4 ///
6 /*
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__
25 #include <algorithm>
27 class DateTimeValidator : public wxValidator
29 wxDateTime *m_pDateTime;
30 mutable wxDatePickerCtrl *m_pCtrl;
32 public:
33 explicit DateTimeValidator(wxDateTime *pval)
34 : m_pDateTime(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
50 if( !m_pDateTime )
51 return false;
52 if( !m_validatorWindow )
53 return false;
55 if( !m_validatorWindow->IsKindOf(CLASSINFO(wxDatePickerCtrl)) )
56 return false;
58 m_pCtrl = dynamic_cast<wxDatePickerCtrl*>(m_validatorWindow);
59 return m_pCtrl;
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);
70 return false;
72 return true;
75 virtual bool TransferToWindow()
77 if( !CheckState() )
78 return false;
80 if( !m_pDateTime->IsValid() )
81 return false;
82 m_pCtrl->SetValue(*m_pDateTime);
83 return true;
86 virtual bool TransferFromWindow()
88 if( !CheckState() )
89 return false;
91 *m_pDateTime = m_pCtrl->GetValue();
92 return true;
96 template <class EnumT>
97 class RadioBoxValidator : public wxValidator
99 mutable std::vector<EnumT> m_codes;
100 EnumT *m_pEnum;
101 mutable wxRadioBox *m_pCtrl;
103 public:
104 explicit RadioBoxValidator(EnumT *pval)
105 : m_pEnum(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
121 // on constructor
122 const RadioBoxValidator& Add(EnumT code) const
124 m_codes.push_back(code);
125 return *this;
128 bool CheckState() const
130 if( !m_pEnum )
131 return false;
132 if( !m_validatorWindow )
133 return false;
135 if( !m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
136 return false;
138 m_pCtrl = dynamic_cast<wxRadioBox*>(m_validatorWindow);
139 return m_pCtrl;
142 bool IsSelectionValid() const
144 return std::find(m_codes.begin(), m_codes.end(), *m_pEnum)
145 != m_codes.end();
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()) ) {
154 wxMessageBox(
155 _T("Please select one of the radio buttons."),
156 _T("Validation"),
157 wxOK | wxICON_INFORMATION, parent);
158 return false;
160 return true;
163 virtual bool TransferToWindow()
165 if( !CheckState() )
166 return false;
168 typename std::vector<EnumT>::iterator i = std::find(
169 m_codes.begin(), m_codes.end(), *m_pEnum);
170 if( i == m_codes.end() )
171 return false;
172 m_pCtrl->SetSelection(i - m_codes.begin());
173 return true;
176 virtual bool TransferFromWindow()
178 if( !CheckState() )
179 return false;
181 *m_pEnum = m_codes.at(m_pCtrl->GetSelection());
182 return true;
186 template <class EnumT>
187 RadioBoxValidator<EnumT> MakeRadioBoxValidator(EnumT *pval)
189 return RadioBoxValidator<EnumT>(pval);
192 #endif