P:/GCC/docs/wxMask/MaskTextCtrl.cpp

Go to the documentation of this file.
00001 #ifndef __MaskTextCtrl_CPP
00002 #define __MaskTextCtrl_CPP
00003 
00004 #include <wxMask/MaskTextCtrl.h>
00005 
00006 MaskTextCtrl::MaskTextCtrl(wxWindow* parent,
00007                  wxWindowID id,
00008                  const wxString &value ,
00009                  const wxPoint &pos ,
00010                  const wxSize &size ,
00011                  long style ,
00012                  const wxValidator &validator ,
00013                  const wxString &name )
00014 {
00015     wxTextCtrl::Create(parent,id,value,pos, size,style,validator,name);
00016     Connect(wxEVT_CHAR, wxKeyEventHandler(MaskTextCtrl::MaskOnChar ));
00017         Connect(wxEVT_KEY_DOWN, wxKeyEventHandler( MaskTextCtrl::MaskEvtEnter ));
00018 
00019 #ifdef __WXMSW__
00020   wxAcceleratorEntry MaskEntry[1];
00021   MaskEntry[0].Set(wxACCEL_CTRL,  WXK_RETURN , WXK_RETURN );
00022   wxAcceleratorTable Mask_WXK(1, MaskEntry);
00023   SetAcceleratorTable(Mask_WXK);
00024 #endif
00025 }
00026 
00027 
00028 MaskTextCtrl::~MaskTextCtrl()
00029 {
00030         // Disconnect Events
00031     Disconnect(wxEVT_CHAR, wxKeyEventHandler(MaskTextCtrl::MaskOnChar ));
00032 }
00033 
00034 
00035 bool MaskTextCtrl::MaskIsUDigit(const int Mskkey)
00036 {
00037     wxChar val = wxT(Mskkey);
00038 
00039   wxRegEx re(wxT("[0-9]"), wxRE_DEFAULT );
00040 if (re.Matches(val)) return true ;
00041     return false;
00042 }
00043 bool MaskTextCtrl::MaskIsDigit(const int Mskkey)
00044 {
00045     wxChar val = wxT(Mskkey);
00046 
00047   wxRegEx re(wxT("[0-9]|\\-|\\+"),wxRE_DEFAULT  );
00048 if (re.Matches(val)) return true ;
00049     return false;
00050 }
00051 
00052 bool MaskTextCtrl::MaskIsUDecimal(const int Mskkey)
00053 {
00054     wxChar val =wxT(Mskkey);
00055 
00056   wxRegEx re(wxT("[0-9]|\\."),wxRE_DEFAULT  );
00057 if (re.Matches(val)) return true ;
00058     return false;
00059 }
00060 bool MaskTextCtrl::MaskIsDecimal(const int Mskkey)
00061 {
00062     wxChar val = wxT(Mskkey);
00063 
00064   wxRegEx re(wxT("[0-9]|\\-|\\.|\\+"),wxRE_DEFAULT  );
00065 if (re.Matches(val)) return true ;
00066     return false;
00067 }
00068 bool MaskTextCtrl::MaskIsAlphaNumeric(const int Mskkey)
00069 {
00070     wxChar val = wxT(Mskkey);
00071 
00072 
00073   wxRegEx re(wxT("[A-Za-z0-9]|[:space :]|\\-|\\.|\\+"),wxRE_DEFAULT );
00074 if (re.Matches(val)) return true ;
00075     return false;
00076 }
00077 bool MaskTextCtrl::MaskIsAlpha(const int Mskkey)
00078 {
00079      const wxChar val = wxT(Mskkey);
00080 
00081   wxRegEx re(wxT("[A-Za-z]|[:space :]|\\-|\\.|\\+"),wxRE_DEFAULT  );
00082 
00083 if (re.Matches(val) ) return true;
00084 return false ;
00085 }
00086 
00087 bool MaskTextCtrl::MaskIsNumeric(const int Mskkey)
00088 {
00089     wxChar val = wxT(Mskkey);
00090 
00091   wxRegEx re(wxT("[0-9]|[:space :]"), wxRE_DEFAULT );
00092 if (re.Matches(val)) return true ;
00093     return false;
00094 }
00095 
00096 void MaskTextCtrl::MaskEditWord(unsigned int MskFILTER, wxString MskEdtWord)
00097 {
00098     MaskFILTER = MskFILTER ;
00099     MaskEdtWord = MskEdtWord ;
00100     this->SetMaxLength( MaskEdtWord.Len());
00101     MaskNextField = true;
00102 }
00103 
00104 
00105 wxString MaskTextCtrl::MaskGetValue()
00106 {   wxString MskValue ;
00107     MaskValue = this->GetValue();
00108 
00109 
00110 // delete  '_'  and  mask ex: ( / or - etc...
00111    if (MaskEdtWord.Len() > 0) {
00112     if(MaskValue.Len() >0)
00113     for (size_t i =0 ; i <=MaskValue.Len() ; i++  ) {
00114 
00115            switch(MaskFILTER) {
00116     case Mask_UDECIMAL:
00117     case Mask_DECIMAL:
00118    if ( (MaskValue[i] != '_' && MaskEdtWord[i] =='#') || (MaskEdtWord[i] =='.')) MskValue.Append(MaskValue[i]); break;
00119     default :
00120     if ( MaskValue[i] != '_' && MaskEdtWord[i] =='#' ) MskValue.Append(MaskValue[i]); break;
00121            }
00122 
00123 
00124     }
00125    } else MskValue=MaskValue;
00126 // formatage value DECIMAL
00127 
00128        MskValue = MaskCodeEdit(MskValue);
00129 
00130 return MskValue;
00131 }
00132 
00133 
00134 wxString MaskTextCtrl::MaskGetText()
00135 {   wxString MskValue ;
00136     MaskValue = this->GetValue();
00137 
00138 
00139 // delete  '_'  and  mask ex: ( / or - etc...
00140     for (size_t i =0 ; i <=MaskValue.Len() ; i++  ) {
00141 
00142 
00143    if ( (MaskValue[i] == '_')) MskValue.Append(' ');
00144    else MskValue.Append(MaskValue[i]);
00145     }
00146 return MskValue;
00147 }
00148 
00149 
00150 
00151 void MaskTextCtrl::MaskClear()
00152 {  this->Clear();
00153 MaskSetValue(MaskGetValue());
00154 }
00155 
00156 
00157 wxString MaskTextCtrl::MaskCodeEdit(wxString MskValue)
00158 {
00159        unsigned int Decimal = 0;
00160         wxString  edt , buf;
00161        double Dval ;
00162 
00163 
00164           switch(MaskFILTER) {
00165     case Mask_UDECIMAL:
00166         MskValue.ToDouble(&Dval);
00167 
00168     for (size_t i = MaskEdtWord.Len() ; i >0 && MaskEdtWord[i] !='.' ; i-- ) {
00169         if (MaskEdtWord[i] =='#' ) Decimal ++;
00170     }
00171 
00172         edt  =_T("%#." + wxString::Format("%d",Decimal)+"f");
00173 
00174         buf=  wxString::Format(edt,Dval);
00175             break;
00176 
00177     case Mask_DECIMAL:
00178     MskValue.ToDouble(&Dval);
00179 
00180     for (size_t i = MaskEdtWord.Len() ; i >0 && MaskEdtWord[i] !='.' ; i-- ) {
00181         if (MaskEdtWord[i] =='#' ) Decimal ++;
00182     }
00183 
00184         edt  =_T("%+#." + wxString::Format("%d",Decimal)+"f");
00185 
00186         buf=  wxString::Format(edt,Dval);
00187             break;
00188 
00189     default:
00190     return MskValue;
00191           }
00192 return buf;
00193 }
00194 void MaskTextCtrl::MaskSetValue(wxString MskValue)
00195 {
00196                  MaskValue  =MskValue;
00197 
00198                 wxString EditWord ;
00199 
00200             if (MaskEdtWord.Len() > 0)
00201             for (size_t i =0 ; i<=MaskEdtWord.Len() ; i++) {
00202                 if(MaskEdtWord[i] == '#')  EditWord.Append('_');
00203                  else EditWord.Append(wxString::wxString::Format("%c",MaskEdtWord[i])) ;
00204             }
00205 
00206           switch(MaskFILTER) {
00207  // cadrage Left
00208     case Mask_ALPHANUMERIC:
00209     case Mask_ALPHA:
00210     case Mask_NUMERIC:
00211 
00212             if (MaskEdtWord.Len() > 0)
00213             for (size_t i =0,u=0 ; i< MaskEdtWord.Len() ; i++) {
00214                 if(MaskEdtWord[i] == '#' ) {
00215                     if( MaskValue.Len() >0 &&  u<MaskValue.Len()) {
00216                          EditWord[i]=MaskValue[u];
00217                     }
00218                     u++;
00219                 }
00220             } else  EditWord= MaskValue;
00221             break;
00222 // cadrage right
00223     case Mask_UDIGIT:
00224     case Mask_DIGIT:
00225     case Mask_UDECIMAL:
00226     case Mask_DECIMAL:
00227 
00228             MaskValue = MaskCodeEdit(MaskValue);
00229             if (MaskEdtWord.Len() > 0) {
00230             for (int i =MaskEdtWord.Len() , u = MaskValue.Len()  ; u >0 ; i--) {
00231                 if( (MaskEdtWord[i] == '#' && MaskValue.Len() >  0 ) ||
00232                     (MaskEdtWord[i] == '.' && MaskValue.Len() >  0 ) ) {
00233                   if ( u >= 0) {
00234                   if (MaskValue[u-1] == '-' || MaskValue[u-1] == '+' )
00235                                 EditWord[0] =MaskValue[u-1];
00236                   else EditWord[i] =MaskValue[u-1];
00237                   }
00238                 u-- ;
00239                 }
00240             }
00241             } else  EditWord.Append(MaskValue);
00242 
00243 
00244              break;
00245 
00246 
00247     default: EditWord.Append(MaskValue) ; break ;
00248     } // case FILTER
00249     this->SetValue(EditWord);
00250 }
00251 
00252 // define Last trt value enter
00253 void MaskTextCtrl::MaskLast(bool MskLast ) { MaskNextField = MskLast ;};
00254 
00255 
00256 void MaskTextCtrl::MaskEvtEnter(wxKeyEvent &event)
00257 {
00258 //wxMessageBox(wxString::Format("%i", event.GetKeyCode()));
00259 
00260 // recadrage passe à la zone suivante
00261 if (((wxKeyEvent&)event).GetKeyCode()== WXK_RETURN  )  {
00262       MaskSetValue(MaskGetValue());
00263       event.Skip();
00264       if (MaskNextField == true) Navigate(true );
00265       return ;
00266       }
00267       event.Skip();
00268 }
00269 
00270 
00271 void MaskTextCtrl::MaskOnChar(wxKeyEvent &event)
00272 {
00273     bool MaskOK = false;
00274     size_t pos;
00275 
00276     wxChar val = wxT(event.GetKeyCode());
00277 
00278   switch( ((wxKeyEvent&)event).GetKeyCode()) {
00279 
00280     case WXK_LEFT:
00281     case WXK_NUMPAD_LEFT:
00282                     if ( GetInsertionPoint()>0) this->SetInsertionPoint(GetInsertionPoint()-1);
00283                     break;
00284 
00285     case WXK_RIGHT:
00286     case WXK_NUMPAD_RIGHT:
00287                     if ( GetInsertionPoint()< this->GetLastPosition()) this->SetInsertionPoint(GetInsertionPoint()+1);
00288                     break;
00289 
00290  // suppression de caractere ('_')
00291     case WXK_BACK:
00292                      pos = this->GetInsertionPoint();
00293                      if(MaskEdtWord.length() ==0) {
00294                      this->Remove(this->GetInsertionPoint()-1, this->GetInsertionPoint());
00295                      this->SetInsertionPoint(pos-1);
00296                      }
00297                      else    if(MaskEdtWord[pos-1] =='#') {
00298                     this->Replace(this->GetInsertionPoint()-1, this->GetInsertionPoint(),wxT("_"));
00299                     this->SetInsertionPoint(pos-1);
00300                      }
00301                     this->SetInsertionPoint(pos-1);
00302                     break;
00303   // suppression de caractere ('_')
00304     case WXK_TAB:
00305                      pos = this->GetInsertionPoint();
00306                      if(MaskEdtWord.length() ==0) {
00307                      this->Remove(this->GetInsertionPoint(), this->GetInsertionPoint()+1);
00308                      this->SetInsertionPoint(pos);
00309                      }
00310                      else  if(MaskEdtWord[pos] =='#') {
00311                     this->Replace(this->GetInsertionPoint(), this->GetInsertionPoint()+1,wxT("_"));
00312                     this->SetInsertionPoint(pos+1);
00313                      } else  this->SetInsertionPoint(pos+1);
00314                     break;
00315 
00316   // suppression de caractere clear field
00317     case WXK_DELETE:
00318     case WXK_NUMPAD_DELETE:
00319                     this->Remove(0,this->GetLastPosition());
00320                     MaskSetValue(this->GetValue());
00321 
00322                     break;
00323     case WXK_END:
00324     case WXK_NUMPAD_END:
00325                     MaskSetValue(MaskGetValue());
00326                     this->SetInsertionPoint(this->GetLastPosition());
00327                     break;
00328     case WXK_HOME:
00329     case WXK_NUMPAD_HOME:
00330                     this->SetInsertionPoint(0);
00331                     break;
00332 
00333     case WXK_ESCAPE:   //this->EmulateKeyPress(WXK_RETURN); No test!!!
00334                      break;
00335     case WXK_RETURN:   //this->EmulateKeyPress(WXK_RETURN); No test!!!
00336                      break;
00337 
00338 // pas de traitement des Keycode > 300
00339     default :
00340      if (((wxKeyEvent&)event).GetKeyCode()>= WXK_START )  break;
00341 
00342         switch(MaskFILTER) {
00343 
00344     case Mask_UDIGIT:
00345                 MaskOK = MaskIsUDigit(event.GetKeyCode());
00346                 break;
00347     case Mask_DIGIT:
00348                 MaskOK = MaskIsDigit(event.GetKeyCode());
00349                 break;
00350     case Mask_UDECIMAL:
00351                 MaskOK = MaskIsUDecimal(event.GetKeyCode());
00352                 break;
00353     case Mask_DECIMAL:
00354                 MaskOK = MaskIsDecimal(event.GetKeyCode());
00355                 break;
00356     case Mask_NUMERIC:
00357                 MaskOK = MaskIsNumeric(event.GetKeyCode());
00358                 break;
00359     case Mask_ALPHANUMERIC:
00360                 MaskOK = MaskIsAlphaNumeric(event.GetKeyCode());
00361                 break;
00362     case Mask_ALPHA:
00363                 MaskOK = MaskIsAlpha(event.GetKeyCode());
00364                 break;
00365   // tout cracatère autorisés
00366     case Mask_NONE:
00367                 MaskOK = true;
00368 
00369 
00370       default:
00371         event.Skip(); break;
00372    } 
00375       if (MaskOK== true ) MaskWriteChar(val);
00376   }  
00378      event.Skip(false);
00379 
00380 }
00381 
00382 // particularité traitement char --> txtctrl
00383 void MaskTextCtrl::MaskWriteChar(wxChar val)
00384 { int pos = this->GetInsertionPoint();
00385            if(MaskEdtWord.length() ==0) WriteText(val);
00386     else {
00387         switch(MaskFILTER) {
00388     case Mask_DIGIT:
00389     case Mask_DECIMAL:
00390                 if ( val =='-' || val =='+') {  this->Replace(0,1,val); break;}
00391                 if    (MaskEdtWord[pos] !='#') this->SetInsertionPoint(pos+1);
00392                 else  this->Replace(this->GetInsertionPoint(), this->GetInsertionPoint()+1,val);
00393                 break;
00394     default:
00395             if    (MaskEdtWord[pos] !='#') this->SetInsertionPoint(pos+1);
00396             else  this->Replace(this->GetInsertionPoint(), this->GetInsertionPoint()+1,val);
00397             break;
00398         }
00399 
00400         }
00401 }
00402 
00403  char * MaskTextCtrl::ToChar()
00404 
00405 {
00406 
00407   wxString inchar = MaskGetValue();
00408 
00409   char * outchar ;
00410 
00411 
00412 
00413   inchar.Trim(); inchar.Trim(true);
00414 
00415    outchar = (char*) malloc( inchar.Length() );
00416 
00417    strcpy(outchar,inchar.c_str());
00418 
00419 
00420 
00421   return outchar;
00422 
00423 }
00424 #endif

Generated on Mon Jul 6 18:29:20 2009 for MaskTextCtrl by  doxygen 1.5.1-p1