valNum.cpp
gehe zur Dokumentation dieser Datei
00001 
00023 
00024 // Name:        valtext.cpp
00025 // Purpose:     wxTextValidator
00026 // Author:      Julian Smart
00027 // Modified by:
00028 // Created:     04/01/98
00029 // RCS-ID:      $Id: valNum.cpp,v 1.8 2011-02-20 22:35:59 keinstein Exp $
00030 // Copyright:   (c) Julian Smart and Markus Holzem
00031 // Licence:     wxWindows license
00033 
00034 #ifdef __GNUG__
00035 #pragma implementation "valNum.h"
00036 #endif
00037 
00038 // For compilers that support precompilation, includes "wx.h".
00039 #include "Defs.h"
00040 #include "wx/wxprec.h"
00041 
00042 #ifdef __BORLANDC__
00043 #pragma hdrstop
00044 #endif
00045 
00046 #if wxUSE_VALIDATORS
00047 
00048 #ifndef WX_PRECOMP
00049 #include <stdio.h>
00050 #include "wx/textctrl.h"
00051 #include "wx/utils.h"
00052 #include "wx/msgdlg.h"
00053 #include "wx/intl.h"
00054 #endif
00055 
00056 #include "valNum.h"
00057 /*
00058 #include <ctype.h>
00059 #include <string.h>
00060 #include <stdlib.h>
00061 
00062 #ifdef __SALFORDC__
00063     #include <clib.h>
00064 #endif
00065 */
00066 IMPLEMENT_DYNAMIC_CLASS(wxNumValidator, wxTextValidator)
00067 
00068 /*BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
00069     EVT_CHAR(wxTextValidator::OnChar)
00070 END_EVENT_TABLE()
00071 */
00072 //static bool wxIsNumeric(const wxString& val);
00073 
00074 wxNumValidator::wxNumValidator(long *val, int style, int min, int max, wxCheckBox* enabler)
00075                 : wxTextValidator(wxFILTER_NUMERIC, &bufferString)
00076 {
00077         DEBUGLOG(other,_T("val: %p, style: %d, min: %d, max: %d, enabler: %p"),
00078                  val,style,min,max,enabler);
00079         Style = style ;
00080         Min = min;
00081         Max = max;
00082         Enabler = enabler;
00083         m_intValue = val ;
00084         bufferString = _T("");
00085         /*
00086             m_refData = new wxVTextRefData;
00087 
00088             M_VTEXTDATA->m_validatorStyle = style ;
00089             M_VTEXTDATA->m_stringValue = val ;
00090         */
00091 }
00092 
00093 wxNumValidator::wxNumValidator(const wxNumValidator& val)
00094                 : wxTextValidator(wxFILTER_NUMERIC, &bufferString)
00095 {
00096         DEBUGLOG(other,_T(""));
00097         Copy(val);
00098 }
00099 
00100 bool wxNumValidator::Copy(const wxNumValidator& val)
00101 
00102 {
00103         DEBUGLOG(other,_T("%p"),m_stringValue);
00104         wxTextValidator::Copy(val);
00105         DEBUGLOG(other,_T("%p"),m_stringValue);
00106         m_stringValue = &bufferString;
00107 
00108         Style = val.Style ;
00109         m_intValue = val.m_intValue ;
00110         Min = val.Min ;
00111         Max = val.Max ;
00112         Enabler = val.Enabler ;
00113 
00114         return TRUE;
00115 }
00116 
00117 wxNumValidator::~wxNumValidator()
00118 
00119 {
00120         DEBUGLOG(other,_T("value: %s"),bufferString.c_str());
00121 }
00122 
00123 // Called when the value in the window must be validated.
00124 // This function can pop up an error message.
00125 bool wxNumValidator::Validate(wxWindow *parent)
00126 {
00127         DEBUGLOG(other,_T(""));
00128 
00129         if ( !wxTextValidator::Validate(parent) )
00130                 return FALSE;
00131 
00132         wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
00133 
00134         wxString val(control->GetValue());
00135 
00136         bool ok = TRUE;
00137 
00138         wxString errormsg;
00139 
00140         long i;
00141 
00142         if ( Enabler && !Enabler->GetValue() ) {
00143                 // nix testen
00144         } else if ( !val.ToLong(&i) ) {
00145                 if ( !Enabler || Enabler->GetValue() ) {
00146                         ok = FALSE;
00147                         errormsg = _("'%s' is not a number.");
00148                 }
00149         } else if ( (Style & NV_MIN) && i < Min ) {
00150                 ok = FALSE;
00151 
00152                 errormsg = _("'%s' is too small.");
00153         } else if ( (Style & NV_MAX) && i > Max ) {
00154                 ok = FALSE;
00155 
00156                 errormsg = _("'%s' is too big.");
00157         }
00158 
00159         if ( !ok ) {
00160                 wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") );
00161 
00162                 m_validatorWindow->SetFocus();
00163 
00164                 wxString buf;
00165                 buf.Printf(errormsg, val.c_str());
00166 
00167                 wxMessageBox(buf, _("Validation conflict"),
00168                              wxOK | wxICON_EXCLAMATION, parent);
00169         }
00170 
00171         return ok;
00172 }
00173 
00174 // Called to transfer data to the window
00175 bool wxNumValidator::TransferToWindow(void)
00176 {
00177         DEBUGLOG(other,_T(""));
00178 
00179         if ( !CheckValidator() )
00180                 return FALSE;
00181 
00182         if (!m_intValue)
00183                 return TRUE;
00184 
00185         bufferString = wxString::Format(_T("%ld"), *m_intValue);
00186 
00187         m_stringValue = &bufferString;
00188 
00189         return wxTextValidator::TransferToWindow();
00190 }
00191 
00192 // Called to transfer data to the window
00193 bool wxNumValidator::TransferFromWindow(void)
00194 {
00195         DEBUGLOG(other,_T(""));
00196 
00197         if ( !CheckValidator() )
00198                 return FALSE;
00199 
00200         if (!m_intValue)
00201                 return TRUE;
00202 
00203         if ( !wxTextValidator::TransferFromWindow() )
00204                 return FALSE;
00205 
00206         bool res = m_stringValue->ToLong(m_intValue);
00207 
00208         DEBUGLOG(other,_T("before enabler"));
00209 
00210         if ( !Enabler || Enabler->GetValue() )
00211                 return res;
00212 
00213         DEBUGLOG(other,_T("after enabler"));
00214 
00215         if ( !res )
00216                 (*m_intValue) = 0;
00217 
00218         if ( (Style & NV_MIN) && (*m_intValue) < Min )
00219                 (*m_intValue) = Min;
00220 
00221         if ( (Style & NV_MAX) && (*m_intValue) > Max )
00222                 (*m_intValue) = Max;
00223 
00224         return true;
00225 }
00226 
00227 
00228 #endif
00229 // wxUSE_VALIDATORS
00230 

Erzeugt am Sun Aug 21 2011 10:51:56 für Mutabor von doxygen 1.7.4