]>
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 // Called to transfer data to the window
104 bool wxGenericValidator::TransferToWindow(void)
106 if ( !m_validatorWindow
)
111 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
113 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
116 pControl
->SetValue(*m_pBool
);
122 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
124 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
127 pControl
->SetValue(*m_pBool
) ;
135 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
137 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
140 pControl
->SetValue(*m_pInt
);
146 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
148 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
151 pControl
->SetSelection(*m_pInt
) ;
157 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
159 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
162 pControl
->SetThumbPosition(*m_pInt
) ;
167 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
168 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
170 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
173 pControl
->SetValue(*m_pInt
);
179 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
181 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
184 pControl
->SetValue(*m_pInt
) ;
190 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
192 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
195 pControl
->SetValue(*m_pInt
) ;
203 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
205 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
208 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
) != wxNOT_FOUND
)
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
) != wxNOT_FOUND
)
249 pControl
->SetStringSelection(* m_pString
);
255 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
257 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
260 pControl
->SetLabel(*m_pString
) ;
265 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
267 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
270 pControl
->SetValue(*m_pString
) ;
276 str
.Printf(wxT("%d"), *m_pInt
);
277 pControl
->SetValue(str
);
283 #if wxUSE_CHECKLISTBOX
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(__WXMOTIF__)
398 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
400 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
403 *m_pInt
=pControl
->GetValue();
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() ;
432 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
434 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
437 *m_pString
= pControl
->GetLabel() ;
443 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
445 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
448 *m_pInt
= pControl
->GetSelection() ;
453 *m_pString
= pControl
->GetValue();
459 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
461 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
464 *m_pInt
= pControl
->GetSelection() ;
469 *m_pString
= pControl
->GetStringSelection();
474 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
476 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
479 *m_pString
= pControl
->GetLabel() ;
484 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
486 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
489 *m_pString
= pControl
->GetValue() ;
494 *m_pInt
= wxAtoi(pControl
->GetValue());
500 #if wxUSE_CHECKLISTBOX
501 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
502 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
504 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
508 m_pArrayInt
->Clear();
510 // add each selected item to our array
512 count
= pControl
->GetCount();
513 for ( i
= 0; i
< count
; i
++ )
515 if (pControl
->IsChecked(i
))
526 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
528 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
532 m_pArrayInt
->Clear();
534 // add each selected item to our array
536 count
= pControl
->GetCount();
537 for ( i
= 0; i
< count
; i
++ )
539 if (pControl
->Selected(i
))
548 // unrecognized control, or bad pointer
554 Called by constructors to initialize ALL data members
556 void wxGenericValidator::Initialize()