]>
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"
44 #include "wx/spinctrl.h"
47 #include "wx/spinbutt.h"
49 #if wxUSE_CHECKLISTBOX
50 #include "wx/checklst.h"
53 #include "wx/valgen.h"
55 IMPLEMENT_CLASS(wxGenericValidator
, wxValidator
)
57 wxGenericValidator::wxGenericValidator(bool *val
)
63 wxGenericValidator::wxGenericValidator(int *val
)
69 wxGenericValidator::wxGenericValidator(wxString
*val
)
75 wxGenericValidator::wxGenericValidator(wxArrayInt
*val
)
81 wxGenericValidator::wxGenericValidator(const wxGenericValidator
& val
)
87 bool wxGenericValidator::Copy(const wxGenericValidator
& val
)
89 wxValidator::Copy(val
);
91 m_pBool
= val
.m_pBool
;
93 m_pString
= val
.m_pString
;
94 m_pArrayInt
= val
.m_pArrayInt
;
99 // Called to transfer data to the window
100 bool wxGenericValidator::TransferToWindow(void)
102 if ( !m_validatorWindow
)
107 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
109 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
112 pControl
->SetValue(*m_pBool
);
118 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
120 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
123 pControl
->SetValue(*m_pBool
) ;
131 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
133 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
136 pControl
->SetValue(*m_pInt
);
142 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
144 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
147 pControl
->SetSelection(*m_pInt
) ;
153 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
155 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
158 pControl
->SetThumbPosition(*m_pInt
) ;
163 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
164 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
166 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
169 pControl
->SetValue(*m_pInt
);
175 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
177 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
180 pControl
->SetValue(*m_pInt
) ;
186 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
188 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
191 pControl
->SetValue(*m_pInt
) ;
199 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
201 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
204 pControl
->SetLabel(*m_pString
) ;
210 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
212 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
215 pControl
->SetSelection(*m_pInt
) ;
220 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
222 pControl
->SetStringSelection(* m_pString
);
224 if ((m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) == 0)
226 pControl
->SetValue(* m_pString
);
233 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
235 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
238 pControl
->SetSelection(*m_pInt
) ;
243 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
245 pControl
->SetStringSelection(* m_pString
);
252 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
254 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
257 pControl
->SetLabel(*m_pString
) ;
263 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
265 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
268 pControl
->SetValue(*m_pString
) ;
274 str
.Printf(wxT("%d"), *m_pInt
);
275 pControl
->SetValue(str
);
281 #if wxUSE_CHECKLISTBOX
282 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
283 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
285 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
288 // clear all selections
290 count
= pControl
->GetCount();
291 for ( i
= 0 ; i
< count
; i
++ )
292 pControl
->Check(i
, false);
294 // select each item in our array
295 count
= m_pArrayInt
->GetCount();
296 for ( i
= 0 ; i
< count
; i
++ )
297 pControl
->Check(m_pArrayInt
->Item(i
));
306 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
308 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
311 // clear all selections
313 count
= pControl
->GetCount();
314 for ( i
= 0 ; i
< count
; i
++ )
315 pControl
->Deselect(i
);
317 // select each item in our array
318 count
= m_pArrayInt
->GetCount();
319 for ( i
= 0 ; i
< count
; i
++ )
320 pControl
->SetSelection(m_pArrayInt
->Item(i
));
326 ; // to match the last 'else' above
328 // unrecognized control, or bad pointer
332 // Called to transfer data from the window
333 bool wxGenericValidator::TransferFromWindow(void)
335 if ( !m_validatorWindow
)
338 // BOOL CONTROLS **************************************
340 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
342 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
345 *m_pBool
= pControl
->GetValue() ;
351 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
353 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
356 *m_pBool
= pControl
->GetValue() ;
362 // INT CONTROLS ***************************************
364 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
366 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
369 *m_pInt
= pControl
->GetValue() ;
375 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
377 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
380 *m_pInt
= pControl
->GetSelection() ;
386 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
388 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
391 *m_pInt
= pControl
->GetThumbPosition() ;
396 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
397 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
399 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
402 *m_pInt
=pControl
->GetValue();
408 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
410 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
413 *m_pInt
= pControl
->GetValue() ;
419 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
421 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
424 *m_pInt
= pControl
->GetValue() ;
430 // STRING CONTROLS ************************************
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 if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
)
454 *m_pString
= pControl
->GetStringSelection();
456 *m_pString
= pControl
->GetValue();
462 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
464 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
467 *m_pInt
= pControl
->GetSelection() ;
472 *m_pString
= pControl
->GetStringSelection();
478 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
480 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
483 *m_pString
= pControl
->GetLabel() ;
489 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
491 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
494 *m_pString
= pControl
->GetValue() ;
499 *m_pInt
= wxAtoi(pControl
->GetValue());
505 // ARRAY CONTROLS *************************************
506 #if wxUSE_CHECKLISTBOX
507 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
508 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
510 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
514 m_pArrayInt
->Clear();
516 // add each selected item to our array
518 count
= pControl
->GetCount();
519 for ( i
= 0; i
< count
; i
++ )
521 if (pControl
->IsChecked(i
))
532 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
534 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
538 m_pArrayInt
->Clear();
540 // add each selected item to our array
542 count
= pControl
->GetCount();
543 for ( i
= 0; i
< count
; i
++ )
545 if (pControl
->Selected(i
))
554 // unrecognized control, or bad pointer
561 Called by constructors to initialize ALL data members
563 void wxGenericValidator::Initialize()