KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_validator_numeric_ctrl.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <clocale>
23
24VALIDATOR_NUMERIC_CTRL::VALIDATOR_NUMERIC_CTRL( bool aCanBeZero, bool aIntegerOnly ) :
25 m_isIntegerOnly( aIntegerOnly ),
26 m_canBeZero( aCanBeZero ),
28{
29}
30
31
36
37
38bool VALIDATOR_NUMERIC_CTRL::Validate( wxWindow* aParent )
39{
40 wxTextCtrl* textCtrl = wxDynamicCast( GetWindow(), wxTextCtrl );
41
42 if( !textCtrl )
43 {
45 return false;
46 }
47
48 wxString value = textCtrl->GetValue().Strip( wxString::both );
49
50 if( value.IsEmpty() )
51 {
53 return false;
54 }
55
56 const struct lconv* lc = localeconv();
57 wxChar decimal_point = lc->decimal_point[0];
58
59 wxString buf( value );
60 buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
61 buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
62
63 long intVal;
64 double floatVal;
65
66 if( m_isIntegerOnly )
67 {
68 if( !buf.ToLong( &intVal ) )
69 {
71 return false;
72 }
73
74 if( !m_canBeZero && intVal <= 0 )
75 {
77 return false;
78 }
79 }
80 else
81 {
82 if( !buf.ToDouble( &floatVal ) )
83 {
85 return false;
86 }
87
88 if( !m_canBeZero && floatVal <= 0.0 )
89 {
91 return false;
92 }
93 }
94
96 return true;
97}
98
99
virtual bool Validate(wxWindow *aParent) override
VALIDATOR_NUMERIC_CTRL(bool aCanBeZero=false, bool aIntegerOnly=false)
VALIDATION_STATE GetValidationState() const
virtual wxObject * Clone() const override