]>
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
28 #include "wx/dynarray.h"
29 #include "wx/choice.h"
30 #include "wx/combobox.h"
31 #include "wx/radiobox.h"
32 #include "wx/radiobut.h"
33 #include "wx/checkbox.h"
34 #include "wx/scrolbar.h"
36 #include "wx/stattext.h"
37 #include "wx/textctrl.h"
38 #include "wx/button.h"
39 #include "wx/listbox.h"
40 #include "wx/slider.h"
43 #include "wx/spinctrl.h"
46 #include "wx/spinbutt.h"
48 #if wxUSE_CHECKLISTBOX
49 #include "wx/checklst.h"
52 #include "wx/tglbtn.h"
55 #include "wx/valgen.h"
57 IMPLEMENT_CLASS(wxGenericValidator
, wxValidator
)
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 // Called to transfer data to the window
102 bool wxGenericValidator::TransferToWindow(void)
104 if ( !m_validatorWindow
)
109 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
111 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
114 pControl
->SetValue(*m_pBool
);
120 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
122 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
125 pControl
->SetValue(*m_pBool
) ;
131 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
133 wxToggleButton
* pControl
= (wxToggleButton
*) m_validatorWindow
;
136 pControl
->SetValue(*m_pBool
);
144 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
146 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
149 pControl
->SetValue(*m_pInt
);
155 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
157 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
160 pControl
->SetSelection(*m_pInt
) ;
166 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
168 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
171 pControl
->SetThumbPosition(*m_pInt
) ;
176 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
177 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
179 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
182 pControl
->SetValue(*m_pInt
);
188 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
190 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
193 pControl
->SetValue(*m_pInt
) ;
199 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
201 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
204 pControl
->SetValue(*m_pInt
) ;
212 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
214 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
217 pControl
->SetLabel(*m_pString
) ;
223 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
225 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
228 pControl
->SetSelection(*m_pInt
) ;
233 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
235 pControl
->SetStringSelection(* m_pString
);
237 if ((m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) == 0)
239 pControl
->SetValue(* m_pString
);
246 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
248 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
251 pControl
->SetSelection(*m_pInt
) ;
256 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
258 pControl
->SetStringSelection(* m_pString
);
265 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
267 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
270 pControl
->SetLabel(*m_pString
) ;
276 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
278 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
281 pControl
->SetValue(*m_pString
) ;
287 str
.Printf(wxT("%d"), *m_pInt
);
288 pControl
->SetValue(str
);
295 #if wxUSE_CHECKLISTBOX
296 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
297 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
299 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
302 // clear all selections
304 count
= pControl
->GetCount();
305 for ( i
= 0 ; i
< count
; i
++ )
306 pControl
->Check(i
, false);
308 // select each item in our array
309 count
= m_pArrayInt
->GetCount();
310 for ( i
= 0 ; i
< count
; i
++ )
311 pControl
->Check(m_pArrayInt
->Item(i
));
320 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
322 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
325 // clear all selections
327 count
= pControl
->GetCount();
328 for ( i
= 0 ; i
< count
; i
++ )
329 pControl
->Deselect(i
);
331 // select each item in our array
332 count
= m_pArrayInt
->GetCount();
333 for ( i
= 0 ; i
< count
; i
++ )
334 pControl
->SetSelection(m_pArrayInt
->Item(i
));
340 ; // to match the last 'else' above
342 // unrecognized control, or bad pointer
346 // Called to transfer data from the window
347 bool wxGenericValidator::TransferFromWindow(void)
349 if ( !m_validatorWindow
)
352 // BOOL CONTROLS **************************************
354 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
356 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
359 *m_pBool
= pControl
->GetValue() ;
365 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
367 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
370 *m_pBool
= pControl
->GetValue() ;
376 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
378 wxToggleButton
*pControl
= (wxToggleButton
*) m_validatorWindow
;
381 *m_pBool
= pControl
->GetValue() ;
387 // INT CONTROLS ***************************************
389 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
391 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
394 *m_pInt
= pControl
->GetValue() ;
400 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
402 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
405 *m_pInt
= pControl
->GetSelection() ;
411 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
413 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
416 *m_pInt
= pControl
->GetThumbPosition() ;
421 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
422 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
424 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
427 *m_pInt
=pControl
->GetValue();
433 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
435 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
438 *m_pInt
= pControl
->GetValue() ;
444 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
446 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
449 *m_pInt
= pControl
->GetValue() ;
455 // STRING CONTROLS ************************************
457 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
459 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
462 *m_pString
= pControl
->GetLabel() ;
468 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
470 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
473 *m_pInt
= pControl
->GetSelection() ;
478 if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
)
479 *m_pString
= pControl
->GetStringSelection();
481 *m_pString
= pControl
->GetValue();
487 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
489 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
492 *m_pInt
= pControl
->GetSelection() ;
497 *m_pString
= pControl
->GetStringSelection();
503 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
505 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
508 *m_pString
= pControl
->GetLabel() ;
514 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
516 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
519 *m_pString
= pControl
->GetValue() ;
524 *m_pInt
= wxAtoi(pControl
->GetValue());
530 // ARRAY CONTROLS *************************************
531 #if wxUSE_CHECKLISTBOX
532 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
533 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
535 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
539 m_pArrayInt
->Clear();
541 // add each selected item to our array
543 count
= pControl
->GetCount();
544 for ( i
= 0; i
< count
; i
++ )
546 if (pControl
->IsChecked(i
))
557 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
559 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
563 m_pArrayInt
->Clear();
565 // add each selected item to our array
567 count
= pControl
->GetCount();
568 for ( i
= 0; i
< count
; i
++ )
570 if (pControl
->Selected(i
))
579 // unrecognized control, or bad pointer
586 Called by constructors to initialize ALL data members
588 void wxGenericValidator::Initialize()