]>
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
);
230 pControl
->SetValue(* m_pString
);
237 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
239 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
242 pControl
->SetSelection(*m_pInt
) ;
247 if (pControl
->FindString(* m_pString
) > -1)
249 pControl
->SetStringSelection(* m_pString
);
255 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
257 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
260 pControl
->SetLabel(*m_pString
) ;
264 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
266 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
269 pControl
->SetValue(*m_pString
) ;
275 str
.Printf(wxT("%d"), *m_pInt
);
276 pControl
->SetValue(str
);
281 #if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
282 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
283 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
285 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
288 // clear all selections
290 count
= pControl
->GetCount();
291 for ( i
= 0 ; i
< count
; i
++ )
292 pControl
->Check(i
, FALSE
);
294 // select each item in our array
295 count
= m_pArrayInt
->GetCount();
296 for ( i
= 0 ; i
< count
; i
++ )
297 pControl
->Check(m_pArrayInt
->Item(i
));
306 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
308 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
311 // clear all selections
313 count
= pControl
->GetCount();
314 for ( i
= 0 ; i
< count
; i
++ )
315 pControl
->Deselect(i
);
317 // select each item in our array
318 count
= m_pArrayInt
->GetCount();
319 for ( i
= 0 ; i
< count
; i
++ )
320 pControl
->SetSelection(m_pArrayInt
->Item(i
));
326 ; // to match the last 'else' above
328 // unrecognized control, or bad pointer
332 // Called to transfer data from the window
333 bool wxGenericValidator::TransferFromWindow(void)
335 if ( !m_validatorWindow
)
340 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
342 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
345 *m_pBool
= pControl
->GetValue() ;
351 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
353 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
356 *m_pBool
= pControl
->GetValue() ;
363 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
365 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
368 *m_pInt
= pControl
->GetValue() ;
374 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
376 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
379 *m_pInt
= pControl
->GetSelection() ;
385 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
387 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
390 *m_pInt
= pControl
->GetThumbPosition() ;
395 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
396 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
398 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
401 *m_pInt
=pControl
->GetValue();
406 #if wxUSE_SPINBTN && !defined(__WIN16__)
407 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
409 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
412 *m_pInt
= pControl
->GetValue() ;
418 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
420 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
423 *m_pInt
= pControl
->GetValue() ;
429 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
431 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
434 *m_pString
= pControl
->GetLabel() ;
440 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
442 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
445 *m_pInt
= pControl
->GetSelection() ;
450 *m_pString
= pControl
->GetValue();
456 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
458 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
461 *m_pInt
= pControl
->GetSelection() ;
466 *m_pString
= pControl
->GetStringSelection();
471 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
473 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
476 *m_pString
= pControl
->GetLabel() ;
480 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
482 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
485 *m_pString
= pControl
->GetValue() ;
490 *m_pInt
= wxAtoi(pControl
->GetValue());
495 #if wxUSE_CHECKLISTBOX
497 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
498 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
500 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
504 m_pArrayInt
->Clear();
506 // add each selected item to our array
508 count
= pControl
->GetCount();
509 for ( i
= 0; i
< count
; i
++ )
511 if (pControl
->IsChecked(i
))
523 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
525 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
529 m_pArrayInt
->Clear();
531 // add each selected item to our array
533 count
= pControl
->GetCount();
534 for ( i
= 0; i
< count
; i
++ )
536 if (pControl
->Selected(i
))
545 // unrecognized control, or bad pointer
551 Called by constructors to initialize ALL data members
553 void wxGenericValidator::Initialize()