NCBI C++ ToolKit
float_text_validator.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: float_text_validator.cpp 25482 2012-03-27 15:00:20Z kuznets $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Andrey Yazhuk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
34 #include <corelib/ncbistr.hpp>
35 
38 
39 #include <wx/msgdlg.h>
40 
41 
43 
44 //IMPLEMENT_DYNAMIC_CLASS(CFloatTextValidator, wxValidator)
45 
46 #define INIT_BASE_MEMBERS \
47  : wxTextValidator(wxFILTER_INCLUDE_CHAR_LIST) \
48  , m_OutputFormat( outputFormat ? outputFormat : "%.3g" ) \
49  , m_ValPtr( valPtr )
50 
52  double* valPtr,
53  double minVal,
54  double maxVal,
55  const char* outputFormat
56 )
58  , m_MinVal(minVal)
59  , m_MaxVal(maxVal)
60 {
61  Init();
62 }
63 
64 CFloatTextValidator::CFloatTextValidator( double* valPtr, const char* outputFormat )
66  , m_MinVal( -numeric_limits<double>::infinity() )
67  , m_MaxVal( numeric_limits<double>::infinity() )
68 {
69  Init();
70 }
71 
73 {
74  static const wxChar* chars[]
75  = { wxT("0"), wxT("1"), wxT("2"), wxT("3"), wxT("4"), wxT("5"), wxT("6"), wxT("7"), wxT("8"), wxT("9"), wxT("e"), wxT("E"), wxT("-"), wxT("+"), wxT(".") };
76  const int n = sizeof(chars) / sizeof(wxChar*);
77 
78  wxArrayString valid_chars(n, chars);
79  SetIncludes(valid_chars);
80 }
81 
83  : wxTextValidator()
84 {
85  Copy(val);
86 }
87 
88 
90 {
91  wxTextValidator::Copy(val);
92 
93  m_OutputFormat = val.m_OutputFormat;
94  m_ValPtr = val.m_ValPtr;
95  m_MinVal = val.m_MinVal;
96  m_MaxVal = val.m_MaxVal;
97 
98  return true;
99 }
100 
102 {
103  if ( ! CheckValidator() ) {
104  return false;
105  }
106 
107  if (m_ValPtr) {
108  char s[128];
109  sprintf(s, m_OutputFormat.c_str(), *m_ValPtr);
110 
111  wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
112  control->SetValue(ToWxString(s));
113  }
114  return true;
115 }
116 
117 
118 // Called to transfer data to the window
120 {
121  if ( ! CheckValidator() ){
122  return false;
123  }
124 
125  wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
126 
127  if( m_ValPtr && control->IsEnabled() && !control->HasFlag( wxTE_READONLY ) ){
128  string str = ToStdString(control->GetValue());
130  }
131  return true;
132 }
133 
134 
135 bool CFloatTextValidator::Validate(wxWindow *parent)
136 {
137  wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
138  if ( ! control->IsEnabled())
139  return true;
140 
141  if(wxTextValidator::Validate(parent)) {
142  double val = 0;
143  try {
144  string str = ToStdString(control->GetValue());
146  } catch(CStringException& e) {
147  string err_msg = e.GetMsg();
148  wxMessageBox(ToWxString(err_msg), wxT("Input is not valid"),
149  wxOK | wxICON_EXCLAMATION, parent);
150 
151  control->SetSelection(-1,-1);
152  control->SetFocus();
153  return false;
154  }
155 
156  if (val >= m_MinVal && val <= m_MaxVal)
157  return true;
158 
159  wxString errMsg, fmt;
162  fmt.Printf(wxT("Please enter number between %s and %s."),
163  m_OutputFormat.c_str(), m_OutputFormat.c_str());
164  errMsg.Printf (fmt, m_MinVal, m_MaxVal);
165  }
167  fmt.Printf(wxT("Please enter number greater or equal then %s."),
168  m_OutputFormat.c_str());
169  errMsg.Printf(fmt, m_MinVal);
170  }
172  fmt.Printf(wxT("Please enter number less or equal then %s."),
173  m_OutputFormat.c_str());
174  errMsg.Printf(fmt, m_MaxVal);
175  }
176 
177  wxMessageBox(errMsg, wxT("Input is not valid"), wxOK | wxICON_ERROR, parent);
178  control->SetSelection(-1,-1);
179  control->SetFocus();
180  return false;
181  }
182  return false;
183 }
184 
185 
CFloatTextValidator(double *val_ptr, double minVal=-numeric_limits< double >::infinity(), double maxVal=numeric_limits< double >::infinity(), const char *out_format="%.3g")
bool Copy(const CFloatTextValidator &val)
virtual bool TransferFromWindow()
virtual bool Validate(wxWindow *parent)
CStringException –.
Definition: ncbistr.hpp:4506
The NCBI C++ standard methods for dealing with std::string.
#define INIT_BASE_MEMBERS
static void Init(void)
Definition: cursor6.c:76
static const char * str(char *buf, int n)
Definition: stats.c:84
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static double StringToDouble(const CTempStringEx str, TStringToNumFlags flags=0)
Convert string to double.
Definition: ncbistr.cpp:1387
const int infinity
Definition: nucprot.cpp:52
yy_size_t n
#define wxT(x)
Definition: muParser.cpp:41
static const BitmapCharRec *const chars[]
Definition: ncbi_10x20.c:1829
wxString ToWxString(const string &s)
Definition: wx_utils.hpp:173
string ToStdString(const wxString &s)
Definition: wx_utils.hpp:161
Modified on Sun Jun 16 04:30:25 2024 by modify_doxy.py rev. 669887