]>
git.saurik.com Git - wxWidgets.git/blob - src/common/valgen.cpp
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"
44 #include "wx/spinbutt.h"
47 #include "wx/tglbtn.h"
50 #include "wx/valgen.h"
52 IMPLEMENT_CLASS(wxGenericValidator
, wxValidator
)
54 wxGenericValidator::wxGenericValidator(bool *val
)
60 wxGenericValidator::wxGenericValidator(int *val
)
66 wxGenericValidator::wxGenericValidator(wxString
*val
)
72 wxGenericValidator::wxGenericValidator(wxArrayInt
*val
)
80 wxGenericValidator::wxGenericValidator(wxDateTime
*val
)
86 #endif // wxUSE_DATETIME
88 wxGenericValidator::wxGenericValidator(const wxGenericValidator
& val
)
94 bool wxGenericValidator::Copy(const wxGenericValidator
& val
)
96 wxValidator::Copy(val
);
98 m_pBool
= val
.m_pBool
;
100 m_pString
= val
.m_pString
;
101 m_pArrayInt
= val
.m_pArrayInt
;
103 m_pDateTime
= val
.m_pDateTime
;
104 #endif // wxUSE_DATETIME
109 // Called to transfer data to the window
110 bool wxGenericValidator::TransferToWindow(void)
112 if ( !m_validatorWindow
)
117 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
119 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
122 pControl
->SetValue(*m_pBool
);
128 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
130 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
133 pControl
->SetValue(*m_pBool
) ;
140 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
142 wxToggleButton
* pControl
= (wxToggleButton
*) m_validatorWindow
;
145 pControl
->SetValue(*m_pBool
);
149 #if (defined(__WXMAC__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__)
150 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxBitmapToggleButton
)) )
152 wxBitmapToggleButton
* pControl
= (wxBitmapToggleButton
*) m_validatorWindow
;
155 pControl
->SetValue(*m_pBool
);
164 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
166 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
169 pControl
->SetValue(*m_pInt
);
175 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
177 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
180 pControl
->SetSelection(*m_pInt
) ;
186 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
188 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
191 pControl
->SetThumbPosition(*m_pInt
) ;
196 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
197 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
199 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
202 pControl
->SetValue(*m_pInt
);
208 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
210 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
213 pControl
->SetValue(*m_pInt
) ;
219 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
221 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
224 pControl
->SetValue(*m_pInt
) ;
230 // date time controls
231 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
232 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxDatePickerCtrl
)) )
234 wxDatePickerCtrl
* pControl
= (wxDatePickerCtrl
*) m_validatorWindow
;
237 pControl
->SetValue(*m_pDateTime
) ;
245 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
247 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
250 pControl
->SetLabel(*m_pString
) ;
256 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
258 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
261 pControl
->SetSelection(*m_pInt
) ;
266 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
268 pControl
->SetStringSelection(* m_pString
);
270 if ((m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) == 0)
272 pControl
->SetValue(* m_pString
);
279 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
281 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
284 pControl
->SetSelection(*m_pInt
) ;
289 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
291 pControl
->SetStringSelection(* m_pString
);
298 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
300 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
303 pControl
->SetLabel(*m_pString
) ;
309 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
311 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
314 pControl
->SetValue(*m_pString
) ;
320 str
.Printf(wxT("%d"), *m_pInt
);
321 pControl
->SetValue(str
);
328 #if wxUSE_CHECKLISTBOX
329 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
330 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
332 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
335 // clear all selections
337 count
= pControl
->GetCount();
338 for ( i
= 0 ; i
< count
; i
++ )
339 pControl
->Check(i
, false);
341 // select each item in our array
342 count
= m_pArrayInt
->GetCount();
343 for ( i
= 0 ; i
< count
; i
++ )
344 pControl
->Check(m_pArrayInt
->Item(i
));
353 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
355 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
358 // clear all selections
360 count
= pControl
->GetCount();
361 for ( i
= 0 ; i
< count
; i
++ )
362 pControl
->Deselect(i
);
364 // select each item in our array
365 count
= m_pArrayInt
->GetCount();
366 for ( i
= 0 ; i
< count
; i
++ )
367 pControl
->SetSelection(m_pArrayInt
->Item(i
));
373 { // to match the last 'else' above
376 // unrecognized control, or bad pointer
380 // Called to transfer data from the window
381 bool wxGenericValidator::TransferFromWindow(void)
383 if ( !m_validatorWindow
)
386 // BOOL CONTROLS **************************************
388 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
390 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
393 *m_pBool
= pControl
->GetValue() ;
399 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
401 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
404 *m_pBool
= pControl
->GetValue() ;
410 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
412 wxToggleButton
*pControl
= (wxToggleButton
*) m_validatorWindow
;
415 *m_pBool
= pControl
->GetValue() ;
421 // INT CONTROLS ***************************************
423 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
425 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
428 *m_pInt
= pControl
->GetValue() ;
434 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
436 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
439 *m_pInt
= pControl
->GetSelection() ;
445 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
447 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
450 *m_pInt
= pControl
->GetThumbPosition() ;
455 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
456 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
458 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
461 *m_pInt
=pControl
->GetValue();
467 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
469 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
472 *m_pInt
= pControl
->GetValue() ;
478 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
480 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
483 *m_pInt
= pControl
->GetValue() ;
489 // DATE TIME CONTROLS ************************************
490 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
491 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxDatePickerCtrl
)) )
493 wxDatePickerCtrl
* pControl
= (wxDatePickerCtrl
*) m_validatorWindow
;
496 *m_pDateTime
= pControl
->GetValue() ;
502 // STRING CONTROLS ************************************
504 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
506 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
509 *m_pString
= pControl
->GetLabel() ;
515 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
517 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
520 *m_pInt
= pControl
->GetSelection() ;
525 if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
)
526 *m_pString
= pControl
->GetStringSelection();
528 *m_pString
= pControl
->GetValue();
534 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
536 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
539 *m_pInt
= pControl
->GetSelection() ;
544 *m_pString
= pControl
->GetStringSelection();
550 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
552 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
555 *m_pString
= pControl
->GetLabel() ;
561 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
563 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
566 *m_pString
= pControl
->GetValue() ;
571 *m_pInt
= wxAtoi(pControl
->GetValue());
577 // ARRAY CONTROLS *************************************
578 #if wxUSE_CHECKLISTBOX
579 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
580 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
582 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
586 m_pArrayInt
->Clear();
588 // add each selected item to our array
590 count
= pControl
->GetCount();
591 for ( i
= 0; i
< count
; i
++ )
593 if (pControl
->IsChecked(i
))
604 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
606 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
610 m_pArrayInt
->Clear();
612 // add each selected item to our array
614 count
= pControl
->GetCount();
615 for ( i
= 0; i
< count
; i
++ )
617 if (pControl
->IsSelected(i
))
626 // unrecognized control, or bad pointer
633 Called by constructors to initialize ALL data members
635 void wxGenericValidator::Initialize()
643 #endif // wxUSE_DATETIME
646 #endif // wxUSE_VALIDATORS