]>
git.saurik.com Git - wxWidgets.git/blob - src/common/valgen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/valgen.cpp
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"
22 #include "wx/dynarray.h"
25 #include "wx/choice.h"
26 #include "wx/combobox.h"
27 #include "wx/radiobox.h"
28 #include "wx/radiobut.h"
29 #include "wx/checkbox.h"
30 #include "wx/scrolbar.h"
32 #include "wx/stattext.h"
33 #include "wx/textctrl.h"
34 #include "wx/button.h"
35 #include "wx/listbox.h"
36 #include "wx/slider.h"
37 #include "wx/checklst.h"
40 #include "wx/spinctrl.h"
43 #include "wx/spinbutt.h"
46 #include "wx/tglbtn.h"
49 #include "wx/valgen.h"
51 IMPLEMENT_CLASS(wxGenericValidator
, wxValidator
)
53 wxGenericValidator::wxGenericValidator(bool *val
)
59 wxGenericValidator::wxGenericValidator(int *val
)
65 wxGenericValidator::wxGenericValidator(wxString
*val
)
71 wxGenericValidator::wxGenericValidator(wxArrayInt
*val
)
77 wxGenericValidator::wxGenericValidator(const wxGenericValidator
& val
)
83 bool wxGenericValidator::Copy(const wxGenericValidator
& val
)
85 wxValidator::Copy(val
);
87 m_pBool
= val
.m_pBool
;
89 m_pString
= val
.m_pString
;
90 m_pArrayInt
= val
.m_pArrayInt
;
95 // Called to transfer data to the window
96 bool wxGenericValidator::TransferToWindow(void)
98 if ( !m_validatorWindow
)
103 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
105 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
108 pControl
->SetValue(*m_pBool
);
114 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
116 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
119 pControl
->SetValue(*m_pBool
) ;
125 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
127 wxToggleButton
* pControl
= (wxToggleButton
*) m_validatorWindow
;
130 pControl
->SetValue(*m_pBool
);
138 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
140 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
143 pControl
->SetValue(*m_pInt
);
149 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
151 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
154 pControl
->SetSelection(*m_pInt
) ;
160 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
162 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
165 pControl
->SetThumbPosition(*m_pInt
) ;
170 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
171 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
173 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
176 pControl
->SetValue(*m_pInt
);
182 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
184 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
187 pControl
->SetValue(*m_pInt
) ;
193 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
195 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
198 pControl
->SetValue(*m_pInt
) ;
206 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
208 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
211 pControl
->SetLabel(*m_pString
) ;
217 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
219 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
222 pControl
->SetSelection(*m_pInt
) ;
227 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
229 pControl
->SetStringSelection(* m_pString
);
231 if ((m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) == 0)
233 pControl
->SetValue(* m_pString
);
240 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
242 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
245 pControl
->SetSelection(*m_pInt
) ;
250 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
252 pControl
->SetStringSelection(* m_pString
);
259 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
261 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
264 pControl
->SetLabel(*m_pString
) ;
270 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
272 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
275 pControl
->SetValue(*m_pString
) ;
281 str
.Printf(wxT("%d"), *m_pInt
);
282 pControl
->SetValue(str
);
289 #if wxUSE_CHECKLISTBOX
290 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
291 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
293 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
296 // clear all selections
298 count
= pControl
->GetCount();
299 for ( i
= 0 ; i
< count
; i
++ )
300 pControl
->Check(i
, false);
302 // select each item in our array
303 count
= m_pArrayInt
->GetCount();
304 for ( i
= 0 ; i
< count
; i
++ )
305 pControl
->Check(m_pArrayInt
->Item(i
));
314 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
316 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
319 // clear all selections
321 count
= pControl
->GetCount();
322 for ( i
= 0 ; i
< count
; i
++ )
323 pControl
->Deselect(i
);
325 // select each item in our array
326 count
= m_pArrayInt
->GetCount();
327 for ( i
= 0 ; i
< count
; i
++ )
328 pControl
->SetSelection(m_pArrayInt
->Item(i
));
334 ; // to match the last 'else' above
336 // unrecognized control, or bad pointer
340 // Called to transfer data from the window
341 bool wxGenericValidator::TransferFromWindow(void)
343 if ( !m_validatorWindow
)
346 // BOOL CONTROLS **************************************
348 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
350 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
353 *m_pBool
= pControl
->GetValue() ;
359 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
361 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
364 *m_pBool
= pControl
->GetValue() ;
370 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
372 wxToggleButton
*pControl
= (wxToggleButton
*) m_validatorWindow
;
375 *m_pBool
= pControl
->GetValue() ;
381 // INT CONTROLS ***************************************
383 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
385 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
388 *m_pInt
= pControl
->GetValue() ;
394 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
396 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
399 *m_pInt
= pControl
->GetSelection() ;
405 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
407 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
410 *m_pInt
= pControl
->GetThumbPosition() ;
415 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
416 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
418 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
421 *m_pInt
=pControl
->GetValue();
427 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
429 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
432 *m_pInt
= pControl
->GetValue() ;
438 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
440 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
443 *m_pInt
= pControl
->GetValue() ;
449 // STRING CONTROLS ************************************
451 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
453 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
456 *m_pString
= pControl
->GetLabel() ;
462 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
464 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
467 *m_pInt
= pControl
->GetSelection() ;
472 if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
)
473 *m_pString
= pControl
->GetStringSelection();
475 *m_pString
= pControl
->GetValue();
481 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
483 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
486 *m_pInt
= pControl
->GetSelection() ;
491 *m_pString
= pControl
->GetStringSelection();
497 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
499 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
502 *m_pString
= pControl
->GetLabel() ;
508 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
510 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
513 *m_pString
= pControl
->GetValue() ;
518 *m_pInt
= wxAtoi(pControl
->GetValue());
524 // ARRAY CONTROLS *************************************
525 #if wxUSE_CHECKLISTBOX
526 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
527 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
529 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
533 m_pArrayInt
->Clear();
535 // add each selected item to our array
537 count
= pControl
->GetCount();
538 for ( i
= 0; i
< count
; i
++ )
540 if (pControl
->IsChecked(i
))
551 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
553 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
557 m_pArrayInt
->Clear();
559 // add each selected item to our array
561 count
= pControl
->GetCount();
562 for ( i
= 0; i
< count
; i
++ )
564 if (pControl
->IsSelected(i
))
573 // unrecognized control, or bad pointer
580 Called by constructors to initialize ALL data members
582 void wxGenericValidator::Initialize()