]>
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"
48 #include "wx/spinctrl.h"
51 #include "wx/spinbutt.h"
53 #if wxUSE_CHECKLISTBOX
54 #include "wx/checklst.h"
57 #include "wx/valgen.h"
59 IMPLEMENT_CLASS(wxGenericValidator
, wxValidator
)
61 wxGenericValidator::wxGenericValidator(bool *val
)
67 wxGenericValidator::wxGenericValidator(int *val
)
73 wxGenericValidator::wxGenericValidator(wxString
*val
)
79 wxGenericValidator::wxGenericValidator(wxArrayInt
*val
)
85 wxGenericValidator::wxGenericValidator(const wxGenericValidator
& val
)
91 bool wxGenericValidator::Copy(const wxGenericValidator
& val
)
93 wxValidator::Copy(val
);
95 m_pBool
= val
.m_pBool
;
97 m_pString
= val
.m_pString
;
98 m_pArrayInt
= val
.m_pArrayInt
;
103 wxGenericValidator::~wxGenericValidator()
107 // Called to transfer data to the window
108 bool wxGenericValidator::TransferToWindow(void)
110 if ( !m_validatorWindow
)
115 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
117 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
120 pControl
->SetValue(*m_pBool
);
126 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
128 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
131 pControl
->SetValue(*m_pBool
) ;
139 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
141 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
144 pControl
->SetValue(*m_pInt
);
150 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
152 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
155 pControl
->SetSelection(*m_pInt
) ;
161 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
163 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
166 pControl
->SetThumbPosition(*m_pInt
) ;
171 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
172 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
174 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
177 pControl
->SetValue(*m_pInt
);
182 #if wxUSE_SPINBTN && !defined(__WIN16__)
183 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
185 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
188 pControl
->SetValue(*m_pInt
) ;
194 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
196 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
199 pControl
->SetValue(*m_pInt
) ;
206 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
208 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
211 pControl
->SetLabel(*m_pString
) ;
216 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
218 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
221 pControl
->SetSelection(*m_pInt
) ;
226 if (pControl
->FindString(* m_pString
) > -1)
228 pControl
->SetStringSelection(* m_pString
);
232 pControl
->SetValue(* m_pString
);
239 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
241 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
244 pControl
->SetSelection(*m_pInt
) ;
249 if (pControl
->FindString(* m_pString
) > -1)
251 pControl
->SetStringSelection(* m_pString
);
257 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
259 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
262 pControl
->SetLabel(*m_pString
) ;
266 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
268 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
271 pControl
->SetValue(*m_pString
) ;
277 str
.Printf(wxT("%d"), *m_pInt
);
278 pControl
->SetValue(str
);
283 #if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
284 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
285 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
287 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
290 // clear all selections
292 count
= pControl
->GetCount();
293 for ( i
= 0 ; i
< count
; i
++ )
294 pControl
->Check(i
, FALSE
);
296 // select each item in our array
297 count
= m_pArrayInt
->GetCount();
298 for ( i
= 0 ; i
< count
; i
++ )
299 pControl
->Check(m_pArrayInt
->Item(i
));
308 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
310 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
313 // clear all selections
315 count
= pControl
->GetCount();
316 for ( i
= 0 ; i
< count
; i
++ )
317 pControl
->Deselect(i
);
319 // select each item in our array
320 count
= m_pArrayInt
->GetCount();
321 for ( i
= 0 ; i
< count
; i
++ )
322 pControl
->SetSelection(m_pArrayInt
->Item(i
));
328 ; // to match the last 'else' above
330 // unrecognized control, or bad pointer
334 // Called to transfer data from the window
335 bool wxGenericValidator::TransferFromWindow(void)
337 if ( !m_validatorWindow
)
342 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
344 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
347 *m_pBool
= pControl
->GetValue() ;
353 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
355 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
358 *m_pBool
= pControl
->GetValue() ;
365 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
367 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
370 *m_pInt
= pControl
->GetValue() ;
376 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
378 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
381 *m_pInt
= pControl
->GetSelection() ;
387 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
389 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
392 *m_pInt
= pControl
->GetThumbPosition() ;
397 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
398 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
400 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
403 *m_pInt
=pControl
->GetValue();
408 #if wxUSE_SPINBTN && !defined(__WIN16__)
409 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
411 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
414 *m_pInt
= pControl
->GetValue() ;
420 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
422 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
425 *m_pInt
= pControl
->GetValue() ;
431 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
433 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
436 *m_pString
= pControl
->GetLabel() ;
442 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
444 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
447 *m_pInt
= pControl
->GetSelection() ;
452 *m_pString
= pControl
->GetValue();
458 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
460 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
463 *m_pInt
= pControl
->GetSelection() ;
468 *m_pString
= pControl
->GetStringSelection();
473 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
475 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
478 *m_pString
= pControl
->GetLabel() ;
482 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
484 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
487 *m_pString
= pControl
->GetValue() ;
492 *m_pInt
= wxAtoi(pControl
->GetValue());
497 #if wxUSE_CHECKLISTBOX
499 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
500 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
502 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
506 m_pArrayInt
->Clear();
508 // add each selected item to our array
510 count
= pControl
->GetCount();
511 for ( i
= 0; i
< count
; i
++ )
513 if (pControl
->IsChecked(i
))
525 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
527 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
531 m_pArrayInt
->Clear();
533 // add each selected item to our array
535 count
= pControl
->GetCount();
536 for ( i
= 0; i
< count
; i
++ )
538 if (pControl
->Selected(i
))
547 // unrecognized control, or bad pointer
553 Called by constructors to initialize ALL data members
555 void wxGenericValidator::Initialize()