]>
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 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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(__WXMOTIF__)
172 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
174 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
177 pControl
->SetValue(*m_pInt
);
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
) ;
207 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
209 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
212 pControl
->SetLabel(*m_pString
) ;
218 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
220 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
223 pControl
->SetSelection(*m_pInt
) ;
228 if (pControl
->FindString(* m_pString
) > -1)
230 pControl
->SetStringSelection(* m_pString
);
234 pControl
->SetValue(* m_pString
);
241 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
243 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
246 pControl
->SetSelection(*m_pInt
) ;
251 if (pControl
->FindString(* m_pString
) > -1)
253 pControl
->SetStringSelection(* m_pString
);
259 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
261 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
264 pControl
->SetLabel(*m_pString
) ;
269 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
271 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
274 pControl
->SetValue(*m_pString
) ;
280 str
.Printf(wxT("%d"), *m_pInt
);
281 pControl
->SetValue(str
);
287 #if wxUSE_CHECKLISTBOX
288 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
289 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
291 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
294 // clear all selections
296 count
= pControl
->GetCount();
297 for ( i
= 0 ; i
< count
; i
++ )
298 pControl
->Check(i
, FALSE
);
300 // select each item in our array
301 count
= m_pArrayInt
->GetCount();
302 for ( i
= 0 ; i
< count
; i
++ )
303 pControl
->Check(m_pArrayInt
->Item(i
));
312 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
314 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
317 // clear all selections
319 count
= pControl
->GetCount();
320 for ( i
= 0 ; i
< count
; i
++ )
321 pControl
->Deselect(i
);
323 // select each item in our array
324 count
= m_pArrayInt
->GetCount();
325 for ( i
= 0 ; i
< count
; i
++ )
326 pControl
->SetSelection(m_pArrayInt
->Item(i
));
332 ; // to match the last 'else' above
334 // unrecognized control, or bad pointer
338 // Called to transfer data from the window
339 bool wxGenericValidator::TransferFromWindow(void)
341 if ( !m_validatorWindow
)
346 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
348 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
351 *m_pBool
= pControl
->GetValue() ;
357 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
359 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
362 *m_pBool
= pControl
->GetValue() ;
369 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
371 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
374 *m_pInt
= pControl
->GetValue() ;
380 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
382 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
385 *m_pInt
= pControl
->GetSelection() ;
391 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
393 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
396 *m_pInt
= pControl
->GetThumbPosition() ;
401 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
402 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
404 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
407 *m_pInt
=pControl
->GetValue();
413 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
415 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
418 *m_pInt
= pControl
->GetValue() ;
424 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
426 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
429 *m_pInt
= pControl
->GetValue() ;
436 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
438 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
441 *m_pString
= pControl
->GetLabel() ;
447 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
449 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
452 *m_pInt
= pControl
->GetSelection() ;
457 *m_pString
= pControl
->GetValue();
463 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
465 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
468 *m_pInt
= pControl
->GetSelection() ;
473 *m_pString
= pControl
->GetStringSelection();
478 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
480 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
483 *m_pString
= pControl
->GetLabel() ;
488 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
490 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
493 *m_pString
= pControl
->GetValue() ;
498 *m_pInt
= wxAtoi(pControl
->GetValue());
504 #if wxUSE_CHECKLISTBOX
505 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
506 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
508 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
512 m_pArrayInt
->Clear();
514 // add each selected item to our array
516 count
= pControl
->GetCount();
517 for ( i
= 0; i
< count
; i
++ )
519 if (pControl
->IsChecked(i
))
530 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
532 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
536 m_pArrayInt
->Clear();
538 // add each selected item to our array
540 count
= pControl
->GetCount();
541 for ( i
= 0; i
< count
; i
++ )
543 if (pControl
->Selected(i
))
552 // unrecognized control, or bad pointer
558 Called by constructors to initialize ALL data members
560 void wxGenericValidator::Initialize()