]>
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
);
276 #if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
277 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
278 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
280 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
283 // clear all selections
285 count
= pControl
->GetCount();
286 for ( i
= 0 ; i
< count
; i
++ )
287 pControl
->Check(i
, FALSE
);
289 // select each item in our array
290 count
= m_pArrayInt
->GetCount();
291 for ( i
= 0 ; i
< count
; i
++ )
292 pControl
->Check(m_pArrayInt
->Item(i
));
301 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
303 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
306 // clear all selections
308 count
= pControl
->GetCount();
309 for ( i
= 0 ; i
< count
; i
++ )
310 pControl
->Deselect(i
);
312 // select each item in our array
313 count
= m_pArrayInt
->GetCount();
314 for ( i
= 0 ; i
< count
; i
++ )
315 pControl
->SetSelection(m_pArrayInt
->Item(i
));
321 ; // to match the last 'else' above
323 // unrecognized control, or bad pointer
327 // Called to transfer data from the window
328 bool wxGenericValidator::TransferFromWindow(void)
330 if ( !m_validatorWindow
)
335 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
337 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
340 *m_pBool
= pControl
->GetValue() ;
346 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
348 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
351 *m_pBool
= pControl
->GetValue() ;
358 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
360 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
363 *m_pInt
= pControl
->GetValue() ;
369 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
371 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
374 *m_pInt
= pControl
->GetSelection() ;
380 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
382 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
385 *m_pInt
= pControl
->GetThumbPosition() ;
390 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
391 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
393 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
396 *m_pInt
=pControl
->GetValue();
401 #if wxUSE_SPINBTN && !defined(__WIN16__)
402 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
404 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
407 *m_pInt
= pControl
->GetValue() ;
413 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
415 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
418 *m_pInt
= pControl
->GetValue() ;
424 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
426 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
429 *m_pString
= pControl
->GetLabel() ;
435 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
437 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
440 *m_pInt
= pControl
->GetSelection() ;
445 *m_pString
= pControl
->GetValue();
451 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
453 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
456 *m_pInt
= pControl
->GetSelection() ;
461 *m_pString
= pControl
->GetStringSelection();
466 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
468 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
471 *m_pString
= pControl
->GetLabel() ;
475 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
477 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
480 *m_pString
= pControl
->GetValue() ;
485 *m_pInt
= wxAtoi(pControl
->GetValue());
490 #if wxUSE_CHECKLISTBOX
492 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
493 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
495 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
499 m_pArrayInt
->Clear();
501 // add each selected item to our array
503 count
= pControl
->GetCount();
504 for ( i
= 0; i
< count
; i
++ )
506 if (pControl
->IsChecked(i
))
518 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
520 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
524 m_pArrayInt
->Clear();
526 // add each selected item to our array
528 count
= pControl
->GetCount();
529 for ( i
= 0; i
< count
; i
++ )
531 if (pControl
->Selected(i
))
540 // unrecognized control, or bad pointer
546 Called by constructors to initialize ALL data members
548 void wxGenericValidator::Initialize()