]>
git.saurik.com Git - wxWidgets.git/blob - src/common/valgen.cpp
315d53a48ca95d8065a6b5cba53df83af22200d5
   1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/common/valgen.cpp 
   3 // Purpose:     wxGenericValidator class 
   6 // Created:     Jan 22 1999 
   8 // Copyright:   (c) 1999 Kevin Smith 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // For compilers that support precompilation, includes "wx.h". 
  13 #include "wx/wxprec.h" 
  22     #include "wx/dynarray.h" 
  25     #include "wx/choice.h" 
  26     #include "wx/combobox.h" 
  27     #include "wx/radiobox.h" 
  28     #include "wx/radiobut.h" 
  29     #include "wx/checkbox.h" 
  30     #include "wx/scrolbar.h" 
  32     #include "wx/stattext.h" 
  33     #include "wx/textctrl.h" 
  34     #include "wx/button.h" 
  35     #include "wx/listbox.h" 
  36     #include "wx/slider.h" 
  37     #include "wx/checklst.h" 
  40 #include "wx/spinctrl.h" 
  41 // #include "wx/datectrl.h" -- can't use it in this (core) file for now 
  44     #include "wx/spinbutt.h" 
  47     #include "wx/tglbtn.h" 
  49 #include "wx/filename.h" 
  51 #include "wx/valgen.h" 
  53 IMPLEMENT_CLASS(wxGenericValidator
, wxValidator
) 
  55 wxGenericValidator::wxGenericValidator(bool *val
) 
  61 wxGenericValidator::wxGenericValidator(int *val
) 
  67 wxGenericValidator::wxGenericValidator(wxString 
*val
) 
  73 wxGenericValidator::wxGenericValidator(wxArrayInt 
*val
) 
  81 wxGenericValidator::wxGenericValidator(wxDateTime 
*val
) 
  87 #endif // wxUSE_DATETIME 
  89 wxGenericValidator::wxGenericValidator(wxFileName 
*val
) 
  95 wxGenericValidator::wxGenericValidator(float *val
) 
 101 wxGenericValidator::wxGenericValidator(double *val
) 
 107 wxGenericValidator::wxGenericValidator(const wxGenericValidator
& val
) 
 113 bool wxGenericValidator::Copy(const wxGenericValidator
& val
) 
 115     wxValidator::Copy(val
); 
 117     m_pBool 
= val
.m_pBool
; 
 119     m_pString 
= val
.m_pString
; 
 120     m_pArrayInt 
= val
.m_pArrayInt
; 
 122     m_pDateTime 
= val
.m_pDateTime
; 
 123 #endif // wxUSE_DATETIME 
 124     m_pFileName 
= val
.m_pFileName
; 
 125     m_pFloat 
= val
.m_pFloat
; 
 126     m_pDouble 
= val
.m_pDouble
; 
 131 // Called to transfer data to the window 
 132 bool wxGenericValidator::TransferToWindow(void) 
 134     if ( !m_validatorWindow 
) 
 139     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) ) 
 141         wxCheckBox
* pControl 
= (wxCheckBox
*) m_validatorWindow
; 
 144             pControl
->SetValue(*m_pBool
); 
 150     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) ) 
 152         wxRadioButton
* pControl 
= (wxRadioButton
*) m_validatorWindow
; 
 155             pControl
->SetValue(*m_pBool
) ; 
 162     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) ) 
 164         wxToggleButton 
* pControl 
= (wxToggleButton 
*) m_validatorWindow
; 
 167             pControl
->SetValue(*m_pBool
); 
 171 #if (defined(__WXMAC__) || defined(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__) 
 172     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxBitmapToggleButton
)) ) 
 174         wxBitmapToggleButton 
* pControl 
= (wxBitmapToggleButton 
*) m_validatorWindow
; 
 177             pControl
->SetValue(*m_pBool
); 
 186     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) ) 
 188         wxGauge
* pControl 
= (wxGauge
*) m_validatorWindow
; 
 191             pControl
->SetValue(*m_pInt
); 
 197     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) ) 
 199         wxRadioBox
* pControl 
= (wxRadioBox
*) m_validatorWindow
; 
 202             pControl
->SetSelection(*m_pInt
) ; 
 208     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) ) 
 210         wxScrollBar
* pControl 
= (wxScrollBar
*) m_validatorWindow
; 
 213             pControl
->SetThumbPosition(*m_pInt
) ; 
 218 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__) 
 219     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) ) 
 221         wxSpinCtrl
* pControl 
= (wxSpinCtrl
*) m_validatorWindow
; 
 224             pControl
->SetValue(*m_pInt
); 
 230     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) ) 
 232         wxSpinButton
* pControl 
= (wxSpinButton
*) m_validatorWindow
; 
 235             pControl
->SetValue(*m_pInt
) ; 
 241     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) ) 
 243         wxSlider
* pControl 
= (wxSlider
*) m_validatorWindow
; 
 246             pControl
->SetValue(*m_pInt
) ; 
 252     // date time controls 
 253 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking 
 254     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxDatePickerCtrl
)) ) 
 256         wxDatePickerCtrl
* pControl 
= (wxDatePickerCtrl
*) m_validatorWindow
; 
 259             pControl
->SetValue(*m_pDateTime
) ; 
 267     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) ) 
 269         wxButton
* pControl 
= (wxButton
*) m_validatorWindow
; 
 272             pControl
->SetLabel(*m_pString
) ; 
 278     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) ) 
 280         wxComboBox
* pControl 
= (wxComboBox
*) m_validatorWindow
; 
 283             pControl
->SetSelection(*m_pInt
) ; 
 288             if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
) 
 290                 pControl
->SetStringSelection(* m_pString
); 
 292             if ((m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) == 0) 
 294                 pControl
->SetValue(* m_pString
); 
 301     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) ) 
 303         wxChoice
* pControl 
= (wxChoice
*) m_validatorWindow
; 
 306             pControl
->SetSelection(*m_pInt
) ; 
 311             if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
) 
 313                 pControl
->SetStringSelection(* m_pString
); 
 320     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) ) 
 322         wxStaticText
* pControl 
= (wxStaticText
*) m_validatorWindow
; 
 325             pControl
->SetLabel(*m_pString
) ; 
 331     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) ) 
 333         wxTextCtrl
* pControl 
= (wxTextCtrl
*) m_validatorWindow
; 
 336             pControl
->SetValue(*m_pString
) ; 
 342             str
.Printf(wxT("%d"), *m_pInt
); 
 343             pControl
->SetValue(str
); 
 346         else if (m_pFileName
) 
 348             pControl
->SetValue(m_pFileName
->GetFullPath()); 
 353             pControl
->SetValue(wxString::Format(wxT("%g"), *m_pFloat
)); 
 358             pControl
->SetValue(wxString::Format(wxT("%g"), *m_pDouble
)); 
 365 #if wxUSE_CHECKLISTBOX 
 366     // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first: 
 367     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) ) 
 369         wxCheckListBox
* pControl 
= (wxCheckListBox
*) m_validatorWindow
; 
 372             // clear all selections 
 374                    count 
= pControl
->GetCount(); 
 375             for ( i 
= 0 ; i 
< count
; i
++ ) 
 376                 pControl
->Check(i
, false); 
 378             // select each item in our array 
 379             count 
= m_pArrayInt
->GetCount(); 
 380             for ( i 
= 0 ; i 
< count
; i
++ ) 
 381                 pControl
->Check(m_pArrayInt
->Item(i
)); 
 390     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) ) 
 392         wxListBox
* pControl 
= (wxListBox
*) m_validatorWindow
; 
 395             // clear all selections 
 397                    count 
= pControl
->GetCount(); 
 398             for ( i 
= 0 ; i 
< count
; i
++ ) 
 399                 pControl
->Deselect(i
); 
 401             // select each item in our array 
 402             count 
= m_pArrayInt
->GetCount(); 
 403             for ( i 
= 0 ; i 
< count
; i
++ ) 
 404                 pControl
->SetSelection(m_pArrayInt
->Item(i
)); 
 410     {   // to match the last 'else' above 
 413   // unrecognized control, or bad pointer 
 417 // Called to transfer data from the window 
 418 bool wxGenericValidator::TransferFromWindow(void) 
 420     if ( !m_validatorWindow 
) 
 423     // BOOL CONTROLS ************************************** 
 425     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) ) 
 427         wxCheckBox
* pControl 
= (wxCheckBox
*) m_validatorWindow
; 
 430             *m_pBool 
= pControl
->GetValue() ; 
 436     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) ) 
 438         wxRadioButton
* pControl 
= (wxRadioButton
*) m_validatorWindow
; 
 441             *m_pBool 
= pControl
->GetValue() ; 
 447     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) ) 
 449         wxToggleButton 
*pControl 
= (wxToggleButton 
*) m_validatorWindow
; 
 452             *m_pBool 
= pControl
->GetValue() ; 
 456 #if (defined(__WXMAC__) || defined(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__) 
 457     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxBitmapToggleButton
)) ) 
 459         wxBitmapToggleButton 
*pControl 
= (wxBitmapToggleButton 
*) m_validatorWindow
; 
 462             *m_pBool 
= pControl
->GetValue() ; 
 469     // INT CONTROLS *************************************** 
 471     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) ) 
 473         wxGauge
* pControl 
= (wxGauge
*) m_validatorWindow
; 
 476             *m_pInt 
= pControl
->GetValue() ; 
 482     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) ) 
 484         wxRadioBox
* pControl 
= (wxRadioBox
*) m_validatorWindow
; 
 487             *m_pInt 
= pControl
->GetSelection() ; 
 493     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) ) 
 495         wxScrollBar
* pControl 
= (wxScrollBar
*) m_validatorWindow
; 
 498             *m_pInt 
= pControl
->GetThumbPosition() ; 
 503 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__) 
 504     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) ) 
 506         wxSpinCtrl
* pControl 
= (wxSpinCtrl
*) m_validatorWindow
; 
 509             *m_pInt
=pControl
->GetValue(); 
 515     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) ) 
 517         wxSpinButton
* pControl 
= (wxSpinButton
*) m_validatorWindow
; 
 520             *m_pInt 
= pControl
->GetValue() ; 
 526     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) ) 
 528         wxSlider
* pControl 
= (wxSlider
*) m_validatorWindow
; 
 531             *m_pInt 
= pControl
->GetValue() ; 
 537     // DATE TIME CONTROLS ************************************ 
 538 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking 
 539     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxDatePickerCtrl
)) ) 
 541         wxDatePickerCtrl
* pControl 
= (wxDatePickerCtrl
*) m_validatorWindow
; 
 544             *m_pDateTime 
= pControl
->GetValue() ; 
 550     // STRING CONTROLS ************************************ 
 552     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) ) 
 554         wxButton
* pControl 
= (wxButton
*) m_validatorWindow
; 
 557             *m_pString 
= pControl
->GetLabel() ; 
 563     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) ) 
 565         wxComboBox
* pControl 
= (wxComboBox
*) m_validatorWindow
; 
 568             *m_pInt 
= pControl
->GetSelection() ; 
 573             if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) 
 574                 *m_pString 
= pControl
->GetStringSelection(); 
 576                 *m_pString 
= pControl
->GetValue(); 
 582     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) ) 
 584         wxChoice
* pControl 
= (wxChoice
*) m_validatorWindow
; 
 587             *m_pInt 
= pControl
->GetSelection() ; 
 592             *m_pString 
= pControl
->GetStringSelection(); 
 598     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) ) 
 600         wxStaticText
* pControl 
= (wxStaticText
*) m_validatorWindow
; 
 603             *m_pString 
= pControl
->GetLabel() ; 
 609     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) ) 
 611         wxTextCtrl
* pControl 
= (wxTextCtrl
*) m_validatorWindow
; 
 614             *m_pString 
= pControl
->GetValue() ; 
 619             *m_pInt 
= wxAtoi(pControl
->GetValue()); 
 622         else if (m_pFileName
) 
 624             m_pFileName
->Assign(pControl
->GetValue()); 
 629             *m_pFloat 
= (float)wxAtof(pControl
->GetValue()); 
 634             *m_pDouble 
= wxAtof(pControl
->GetValue()); 
 640     // ARRAY CONTROLS ************************************* 
 641 #if wxUSE_CHECKLISTBOX 
 642     // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first: 
 643     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) ) 
 645         wxCheckListBox
* pControl 
= (wxCheckListBox
*) m_validatorWindow
; 
 649             m_pArrayInt
->Clear(); 
 651             // add each selected item to our array 
 653                    count 
= pControl
->GetCount(); 
 654             for ( i 
= 0; i 
< count
; i
++ ) 
 656                 if (pControl
->IsChecked(i
)) 
 667     if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) ) 
 669         wxListBox
* pControl 
= (wxListBox
*) m_validatorWindow
; 
 673             m_pArrayInt
->Clear(); 
 675             // add each selected item to our array 
 677                    count 
= pControl
->GetCount(); 
 678             for ( i 
= 0; i 
< count
; i
++ ) 
 680                 if (pControl
->IsSelected(i
)) 
 689     // unrecognized control, or bad pointer 
 696   Called by constructors to initialize ALL data members 
 698 void wxGenericValidator::Initialize() 
 706 #endif // wxUSE_DATETIME 
 712 #endif // wxUSE_VALIDATORS