]>
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
)
88 bool wxGenericValidator::Copy(const wxGenericValidator
& val
)
90 wxValidator::Copy(val
);
92 m_pBool
= val
.m_pBool
;
94 m_pString
= val
.m_pString
;
95 m_pArrayInt
= val
.m_pArrayInt
;
100 wxGenericValidator::~wxGenericValidator()
104 // Called to transfer data to the window
105 bool wxGenericValidator::TransferToWindow(void)
107 if ( !m_validatorWindow
)
112 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
114 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
117 pControl
->SetValue(*m_pBool
);
123 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
125 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
128 pControl
->SetValue(*m_pBool
) ;
136 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
138 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
141 pControl
->SetValue(*m_pInt
);
147 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
149 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
152 pControl
->SetSelection(*m_pInt
) ;
158 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
160 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
163 pControl
->SetThumbPosition(*m_pInt
) ;
168 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
169 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
171 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
174 pControl
->SetValue(*m_pInt
);
179 #if wxUSE_SPINBTN && !defined(__WIN16__)
180 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
182 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
185 pControl
->SetValue(*m_pInt
) ;
191 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
193 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
196 pControl
->SetValue(*m_pInt
) ;
203 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
205 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
208 pControl
->SetLabel(*m_pString
) ;
213 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
215 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
218 pControl
->SetSelection(*m_pInt
) ;
223 if (pControl
->FindString(* m_pString
) > -1)
225 pControl
->SetStringSelection(* m_pString
);
232 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
234 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
237 pControl
->SetSelection(*m_pInt
) ;
242 if (pControl
->FindString(* m_pString
) > -1)
244 pControl
->SetStringSelection(* m_pString
);
250 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
252 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
255 pControl
->SetLabel(*m_pString
) ;
259 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
261 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
264 pControl
->SetValue(*m_pString
) ;
270 str
.Printf(wxT("%d"), *m_pInt
);
271 pControl
->SetValue(str
);
275 #if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
277 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
279 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
281 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
284 // clear all selections
286 for (i
= 0 ; i
< pControl
->Number(); ++i
)
287 pControl
->Check(i
, FALSE
);
288 // select each item in our array
290 for (u
= 0; u
< m_pArrayInt
->Count(); ++u
)
291 pControl
->Check(m_pArrayInt
->Item(u
));
299 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
301 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
304 // clear all selections
306 for (i
= 0 ; i
< pControl
->Number(); ++i
)
307 pControl
->Deselect(i
);
308 // select each item in our array
310 for (u
= 0; u
< m_pArrayInt
->Count(); ++u
)
311 pControl
->SetSelection(m_pArrayInt
->Item(u
));
316 ; // to match the last 'else' above
318 // unrecognized control, or bad pointer
322 // Called to transfer data from the window
323 bool wxGenericValidator::TransferFromWindow(void)
325 if ( !m_validatorWindow
)
330 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
332 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
335 *m_pBool
= pControl
->GetValue() ;
341 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
343 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
346 *m_pBool
= pControl
->GetValue() ;
353 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
355 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
358 *m_pInt
= pControl
->GetValue() ;
364 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
366 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
369 *m_pInt
= pControl
->GetSelection() ;
375 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
377 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
380 *m_pInt
= pControl
->GetThumbPosition() ;
385 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
386 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
388 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
391 *m_pInt
=pControl
->GetValue();
396 #if wxUSE_SPINBTN && !defined(__WIN16__)
397 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
399 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
402 *m_pInt
= pControl
->GetValue() ;
408 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
410 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
413 *m_pInt
= pControl
->GetValue() ;
419 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
421 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
424 *m_pString
= pControl
->GetLabel() ;
430 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
432 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
435 *m_pInt
= pControl
->GetSelection() ;
440 *m_pString
= pControl
->GetStringSelection();
446 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
448 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
451 *m_pInt
= pControl
->GetSelection() ;
456 *m_pString
= pControl
->GetStringSelection();
461 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
463 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
466 *m_pString
= pControl
->GetLabel() ;
470 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
472 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
475 *m_pString
= pControl
->GetValue() ;
480 *m_pInt
= wxAtoi(pControl
->GetValue());
484 #if wxUSE_CHECKLISTBOX
487 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
489 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
491 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
495 m_pArrayInt
->Clear();
496 // add each selected item to our array
498 for (i
= 0 ; i
< pControl
->Number(); ++i
)
499 if (pControl
->IsChecked(i
))
509 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
511 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
515 m_pArrayInt
->Clear();
516 // add each selected item to our array
518 for (i
= 0 ; i
< pControl
->Number(); ++i
)
519 if (pControl
->Selected(i
))
526 // unrecognized control, or bad pointer
532 Called by constructors to initialize ALL data members
534 void wxGenericValidator::Initialize()