]>
git.saurik.com Git - wxWidgets.git/blob - src/common/valgen.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericValidator class
6 // Created: Jan 22 1999
8 // Copyright: (c) 1999 Kevin Smith
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "valgen.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
32 #include "wx/dynarray.h"
33 #include "wx/choice.h"
34 #include "wx/combobox.h"
35 #include "wx/radiobox.h"
36 #include "wx/radiobut.h"
37 #include "wx/checkbox.h"
38 #include "wx/scrolbar.h"
40 #include "wx/stattext.h"
41 #include "wx/textctrl.h"
42 #include "wx/button.h"
43 #include "wx/listbox.h"
44 #include "wx/slider.h"
47 #if wxUSE_SPINCTRL && !defined(__WIN16__)
48 #include "wx/spinctrl.h"
50 #if wxUSE_SPINBTN && !defined(__WIN16__)
51 #include "wx/spinbutt.h"
53 #if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
54 #include "wx/checklst.h"
57 #include "wx/valgen.h"
59 wxGenericValidator::wxGenericValidator(bool *val
)
65 wxGenericValidator::wxGenericValidator(int *val
)
71 wxGenericValidator::wxGenericValidator(wxString
*val
)
77 wxGenericValidator::wxGenericValidator(wxArrayInt
*val
)
83 wxGenericValidator::wxGenericValidator(const wxGenericValidator
& val
)
89 bool wxGenericValidator::Copy(const wxGenericValidator
& val
)
91 wxValidator::Copy(val
);
93 m_pBool
= val
.m_pBool
;
95 m_pString
= val
.m_pString
;
96 m_pArrayInt
= val
.m_pArrayInt
;
101 wxGenericValidator::~wxGenericValidator()
105 // Called to transfer data to the window
106 bool wxGenericValidator::TransferToWindow(void)
108 if ( !m_validatorWindow
)
113 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
115 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
118 pControl
->SetValue(*m_pBool
);
124 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
126 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
129 pControl
->SetValue(*m_pBool
) ;
137 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
139 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
142 pControl
->SetValue(*m_pInt
);
148 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
150 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
153 pControl
->SetSelection(*m_pInt
) ;
159 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
161 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
164 pControl
->SetThumbPosition(*m_pInt
) ;
169 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
170 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
172 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
175 pControl
->SetValue(*m_pInt
);
180 #if wxUSE_SPINBTN && !defined(__WIN16__)
181 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
183 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
186 pControl
->SetValue(*m_pInt
) ;
192 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
194 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
197 pControl
->SetValue(*m_pInt
) ;
204 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
206 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
209 pControl
->SetLabel(*m_pString
) ;
214 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
216 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
219 pControl
->SetSelection(*m_pInt
) ;
224 if (pControl
->FindString(* m_pString
) > -1)
226 pControl
->SetStringSelection(* m_pString
);
233 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
235 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
238 pControl
->SetSelection(*m_pInt
) ;
243 if (pControl
->FindString(* m_pString
) > -1)
245 pControl
->SetStringSelection(* m_pString
);
251 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
253 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
256 pControl
->SetLabel(*m_pString
) ;
260 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
262 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
265 pControl
->SetValue(*m_pString
) ;
271 str
.Printf(wxT("%d"), *m_pInt
);
272 pControl
->SetValue(str
);
277 #if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
278 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
279 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
281 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
284 // clear all selections
286 count
= pControl
->GetCount();
287 for ( i
= 0 ; i
< count
; i
++ )
288 pControl
->Check(i
, FALSE
);
290 // select each item in our array
291 count
= m_pArrayInt
->GetCount();
292 for ( i
= 0 ; i
< count
; i
++ )
293 pControl
->Check(m_pArrayInt
->Item(i
));
302 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
304 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
307 // clear all selections
309 count
= pControl
->GetCount();
310 for ( i
= 0 ; i
< count
; i
++ )
311 pControl
->Deselect(i
);
313 // select each item in our array
314 count
= m_pArrayInt
->GetCount();
315 for ( i
= 0 ; i
< count
; i
++ )
316 pControl
->SetSelection(m_pArrayInt
->Item(i
));
322 ; // to match the last 'else' above
324 // unrecognized control, or bad pointer
328 // Called to transfer data from the window
329 bool wxGenericValidator::TransferFromWindow(void)
331 if ( !m_validatorWindow
)
336 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
338 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
341 *m_pBool
= pControl
->GetValue() ;
347 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
349 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
352 *m_pBool
= pControl
->GetValue() ;
359 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
361 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
364 *m_pInt
= pControl
->GetValue() ;
370 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
372 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
375 *m_pInt
= pControl
->GetSelection() ;
381 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
383 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
386 *m_pInt
= pControl
->GetThumbPosition() ;
391 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
392 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
394 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
397 *m_pInt
=pControl
->GetValue();
402 #if wxUSE_SPINBTN && !defined(__WIN16__)
403 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
405 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
408 *m_pInt
= pControl
->GetValue() ;
414 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
416 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
419 *m_pInt
= pControl
->GetValue() ;
425 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
427 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
430 *m_pString
= pControl
->GetLabel() ;
436 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
438 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
441 *m_pInt
= pControl
->GetSelection() ;
446 *m_pString
= pControl
->GetValue();
452 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
454 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
457 *m_pInt
= pControl
->GetSelection() ;
462 *m_pString
= pControl
->GetStringSelection();
467 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
469 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
472 *m_pString
= pControl
->GetLabel() ;
476 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
478 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
481 *m_pString
= pControl
->GetValue() ;
486 *m_pInt
= wxAtoi(pControl
->GetValue());
491 #if wxUSE_CHECKLISTBOX
493 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
494 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
496 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
500 m_pArrayInt
->Clear();
502 // add each selected item to our array
504 count
= pControl
->GetCount();
505 for ( i
= 0; i
< count
; i
++ )
507 if (pControl
->IsChecked(i
))
519 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
521 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
525 m_pArrayInt
->Clear();
527 // add each selected item to our array
529 count
= pControl
->GetCount();
530 for ( i
= 0; i
< count
; i
++ )
532 if (pControl
->Selected(i
))
541 // unrecognized control, or bad pointer
547 Called by constructors to initialize ALL data members
549 void wxGenericValidator::Initialize()