ccbcc7c07fdf634e9611ae18cb82e31415f11c60
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 wxGenericValidator::wxGenericValidator(wxFileName
*val
)
93 wxGenericValidator::wxGenericValidator(float *val
)
99 wxGenericValidator::wxGenericValidator(double *val
)
105 #endif // wxUSE_DATETIME
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
128 // Called to transfer data to the window
129 bool wxGenericValidator::TransferToWindow(void)
131 if ( !m_validatorWindow
)
136 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
138 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
141 pControl
->SetValue(*m_pBool
);
147 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
149 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
152 pControl
->SetValue(*m_pBool
) ;
159 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
161 wxToggleButton
* pControl
= (wxToggleButton
*) m_validatorWindow
;
164 pControl
->SetValue(*m_pBool
);
168 #if (defined(__WXMAC__) || defined(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__)
169 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxBitmapToggleButton
)) )
171 wxBitmapToggleButton
* pControl
= (wxBitmapToggleButton
*) m_validatorWindow
;
174 pControl
->SetValue(*m_pBool
);
183 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
185 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
188 pControl
->SetValue(*m_pInt
);
194 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
196 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
199 pControl
->SetSelection(*m_pInt
) ;
205 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
207 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
210 pControl
->SetThumbPosition(*m_pInt
) ;
215 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
216 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
218 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
221 pControl
->SetValue(*m_pInt
);
227 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
229 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
232 pControl
->SetValue(*m_pInt
) ;
238 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
240 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
243 pControl
->SetValue(*m_pInt
) ;
249 // date time controls
250 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
251 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxDatePickerCtrl
)) )
253 wxDatePickerCtrl
* pControl
= (wxDatePickerCtrl
*) m_validatorWindow
;
256 pControl
->SetValue(*m_pDateTime
) ;
264 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
266 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
269 pControl
->SetLabel(*m_pString
) ;
275 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
277 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
280 pControl
->SetSelection(*m_pInt
) ;
285 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
287 pControl
->SetStringSelection(* m_pString
);
289 if ((m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) == 0)
291 pControl
->SetValue(* m_pString
);
298 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
300 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
303 pControl
->SetSelection(*m_pInt
) ;
308 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
310 pControl
->SetStringSelection(* m_pString
);
317 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
319 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
322 pControl
->SetLabel(*m_pString
) ;
328 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
330 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
333 pControl
->SetValue(*m_pString
) ;
339 str
.Printf(wxT("%d"), *m_pInt
);
340 pControl
->SetValue(str
);
343 else if (m_pFileName
)
345 pControl
->SetValue(m_pFileName
->GetFullPath());
350 pControl
->SetValue(wxString::Format(wxT("%g"), *m_pFloat
));
355 pControl
->SetValue(wxString::Format(wxT("%g"), *m_pDouble
));
362 #if wxUSE_CHECKLISTBOX
363 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
364 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
366 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
369 // clear all selections
371 count
= pControl
->GetCount();
372 for ( i
= 0 ; i
< count
; i
++ )
373 pControl
->Check(i
, false);
375 // select each item in our array
376 count
= m_pArrayInt
->GetCount();
377 for ( i
= 0 ; i
< count
; i
++ )
378 pControl
->Check(m_pArrayInt
->Item(i
));
387 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
389 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
392 // clear all selections
394 count
= pControl
->GetCount();
395 for ( i
= 0 ; i
< count
; i
++ )
396 pControl
->Deselect(i
);
398 // select each item in our array
399 count
= m_pArrayInt
->GetCount();
400 for ( i
= 0 ; i
< count
; i
++ )
401 pControl
->SetSelection(m_pArrayInt
->Item(i
));
407 { // to match the last 'else' above
410 // unrecognized control, or bad pointer
414 // Called to transfer data from the window
415 bool wxGenericValidator::TransferFromWindow(void)
417 if ( !m_validatorWindow
)
420 // BOOL CONTROLS **************************************
422 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
424 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
427 *m_pBool
= pControl
->GetValue() ;
433 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
435 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
438 *m_pBool
= pControl
->GetValue() ;
444 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
446 wxToggleButton
*pControl
= (wxToggleButton
*) m_validatorWindow
;
449 *m_pBool
= pControl
->GetValue() ;
453 #if (defined(__WXMAC__) || defined(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__)
454 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxBitmapToggleButton
)) )
456 wxBitmapToggleButton
*pControl
= (wxBitmapToggleButton
*) m_validatorWindow
;
459 *m_pBool
= pControl
->GetValue() ;
466 // INT CONTROLS ***************************************
468 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
470 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
473 *m_pInt
= pControl
->GetValue() ;
479 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
481 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
484 *m_pInt
= pControl
->GetSelection() ;
490 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
492 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
495 *m_pInt
= pControl
->GetThumbPosition() ;
500 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
501 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
503 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
506 *m_pInt
=pControl
->GetValue();
512 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
514 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
517 *m_pInt
= pControl
->GetValue() ;
523 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
525 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
528 *m_pInt
= pControl
->GetValue() ;
534 // DATE TIME CONTROLS ************************************
535 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
536 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxDatePickerCtrl
)) )
538 wxDatePickerCtrl
* pControl
= (wxDatePickerCtrl
*) m_validatorWindow
;
541 *m_pDateTime
= pControl
->GetValue() ;
547 // STRING CONTROLS ************************************
549 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
551 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
554 *m_pString
= pControl
->GetLabel() ;
560 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
562 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
565 *m_pInt
= pControl
->GetSelection() ;
570 if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
)
571 *m_pString
= pControl
->GetStringSelection();
573 *m_pString
= pControl
->GetValue();
579 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
581 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
584 *m_pInt
= pControl
->GetSelection() ;
589 *m_pString
= pControl
->GetStringSelection();
595 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
597 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
600 *m_pString
= pControl
->GetLabel() ;
606 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
608 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
611 *m_pString
= pControl
->GetValue() ;
616 *m_pInt
= wxAtoi(pControl
->GetValue());
619 else if (m_pFileName
)
621 m_pFileName
->Assign(pControl
->GetValue());
626 *m_pFloat
= (float)wxAtof(pControl
->GetValue());
631 *m_pDouble
= wxAtof(pControl
->GetValue());
637 // ARRAY CONTROLS *************************************
638 #if wxUSE_CHECKLISTBOX
639 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
640 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
642 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
646 m_pArrayInt
->Clear();
648 // add each selected item to our array
650 count
= pControl
->GetCount();
651 for ( i
= 0; i
< count
; i
++ )
653 if (pControl
->IsChecked(i
))
664 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
666 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
670 m_pArrayInt
->Clear();
672 // add each selected item to our array
674 count
= pControl
->GetCount();
675 for ( i
= 0; i
< count
; i
++ )
677 if (pControl
->IsSelected(i
))
686 // unrecognized control, or bad pointer
693 Called by constructors to initialize ALL data members
695 void wxGenericValidator::Initialize()
703 #endif // wxUSE_DATETIME
709 #endif // wxUSE_VALIDATORS