]>
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"
39 #include "wx/spinctrl.h"
42 #include "wx/spinbutt.h"
44 #if wxUSE_CHECKLISTBOX
45 #include "wx/checklst.h"
48 #include "wx/tglbtn.h"
51 #include "wx/valgen.h"
53 IMPLEMENT_CLASS(wxGenericValidator
, wxValidator
)
55 wxGenericValidator::wxGenericValidator(bool *val
)
61 wxGenericValidator::wxGenericValidator(int *val
)
67 wxGenericValidator::wxGenericValidator(wxString
*val
)
73 wxGenericValidator::wxGenericValidator(wxArrayInt
*val
)
79 wxGenericValidator::wxGenericValidator(const wxGenericValidator
& val
)
85 bool wxGenericValidator::Copy(const wxGenericValidator
& val
)
87 wxValidator::Copy(val
);
89 m_pBool
= val
.m_pBool
;
91 m_pString
= val
.m_pString
;
92 m_pArrayInt
= val
.m_pArrayInt
;
97 // Called to transfer data to the window
98 bool wxGenericValidator::TransferToWindow(void)
100 if ( !m_validatorWindow
)
105 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
107 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
110 pControl
->SetValue(*m_pBool
);
116 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
118 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
121 pControl
->SetValue(*m_pBool
) ;
127 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
129 wxToggleButton
* pControl
= (wxToggleButton
*) m_validatorWindow
;
132 pControl
->SetValue(*m_pBool
);
140 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
142 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
145 pControl
->SetValue(*m_pInt
);
151 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
153 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
156 pControl
->SetSelection(*m_pInt
) ;
162 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
164 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
167 pControl
->SetThumbPosition(*m_pInt
) ;
172 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
173 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
175 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
178 pControl
->SetValue(*m_pInt
);
184 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
186 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
189 pControl
->SetValue(*m_pInt
) ;
195 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
197 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
200 pControl
->SetValue(*m_pInt
) ;
208 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
210 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
213 pControl
->SetLabel(*m_pString
) ;
219 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
221 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
224 pControl
->SetSelection(*m_pInt
) ;
229 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
231 pControl
->SetStringSelection(* m_pString
);
233 if ((m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) == 0)
235 pControl
->SetValue(* m_pString
);
242 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
244 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
247 pControl
->SetSelection(*m_pInt
) ;
252 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
254 pControl
->SetStringSelection(* m_pString
);
261 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
263 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
266 pControl
->SetLabel(*m_pString
) ;
272 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
274 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
277 pControl
->SetValue(*m_pString
) ;
283 str
.Printf(wxT("%d"), *m_pInt
);
284 pControl
->SetValue(str
);
291 #if wxUSE_CHECKLISTBOX
292 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
293 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
295 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
298 // clear all selections
300 count
= pControl
->GetCount();
301 for ( i
= 0 ; i
< count
; i
++ )
302 pControl
->Check(i
, false);
304 // select each item in our array
305 count
= m_pArrayInt
->GetCount();
306 for ( i
= 0 ; i
< count
; i
++ )
307 pControl
->Check(m_pArrayInt
->Item(i
));
316 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
318 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
321 // clear all selections
323 count
= pControl
->GetCount();
324 for ( i
= 0 ; i
< count
; i
++ )
325 pControl
->Deselect(i
);
327 // select each item in our array
328 count
= m_pArrayInt
->GetCount();
329 for ( i
= 0 ; i
< count
; i
++ )
330 pControl
->SetSelection(m_pArrayInt
->Item(i
));
336 ; // to match the last 'else' above
338 // unrecognized control, or bad pointer
342 // Called to transfer data from the window
343 bool wxGenericValidator::TransferFromWindow(void)
345 if ( !m_validatorWindow
)
348 // BOOL CONTROLS **************************************
350 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
352 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
355 *m_pBool
= pControl
->GetValue() ;
361 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
363 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
366 *m_pBool
= pControl
->GetValue() ;
372 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
374 wxToggleButton
*pControl
= (wxToggleButton
*) m_validatorWindow
;
377 *m_pBool
= pControl
->GetValue() ;
383 // INT CONTROLS ***************************************
385 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
387 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
390 *m_pInt
= pControl
->GetValue() ;
396 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
398 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
401 *m_pInt
= pControl
->GetSelection() ;
407 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
409 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
412 *m_pInt
= pControl
->GetThumbPosition() ;
417 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
418 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
420 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
423 *m_pInt
=pControl
->GetValue();
429 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
431 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
434 *m_pInt
= pControl
->GetValue() ;
440 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
442 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
445 *m_pInt
= pControl
->GetValue() ;
451 // STRING CONTROLS ************************************
453 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
455 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
458 *m_pString
= pControl
->GetLabel() ;
464 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
466 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
469 *m_pInt
= pControl
->GetSelection() ;
474 if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
)
475 *m_pString
= pControl
->GetStringSelection();
477 *m_pString
= pControl
->GetValue();
483 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
485 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
488 *m_pInt
= pControl
->GetSelection() ;
493 *m_pString
= pControl
->GetStringSelection();
499 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
501 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
504 *m_pString
= pControl
->GetLabel() ;
510 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
512 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
515 *m_pString
= pControl
->GetValue() ;
520 *m_pInt
= wxAtoi(pControl
->GetValue());
526 // ARRAY CONTROLS *************************************
527 #if wxUSE_CHECKLISTBOX
528 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
529 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
531 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
535 m_pArrayInt
->Clear();
537 // add each selected item to our array
539 count
= pControl
->GetCount();
540 for ( i
= 0; i
< count
; i
++ )
542 if (pControl
->IsChecked(i
))
553 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
555 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
559 m_pArrayInt
->Clear();
561 // add each selected item to our array
563 count
= pControl
->GetCount();
564 for ( i
= 0; i
< count
; i
++ )
566 if (pControl
->IsSelected(i
))
575 // unrecognized control, or bad pointer
582 Called by constructors to initialize ALL data members
584 void wxGenericValidator::Initialize()