]>
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"
41 #include "wx/datectrl.h"
44 #include "wx/spinbutt.h"
47 #include "wx/tglbtn.h"
50 #include "wx/valgen.h"
52 IMPLEMENT_CLASS(wxGenericValidator
, wxValidator
)
54 wxGenericValidator::wxGenericValidator(bool *val
)
60 wxGenericValidator::wxGenericValidator(int *val
)
66 wxGenericValidator::wxGenericValidator(wxString
*val
)
72 wxGenericValidator::wxGenericValidator(wxArrayInt
*val
)
80 wxGenericValidator::wxGenericValidator(wxDateTime
*val
)
86 #endif // wxUSE_DATETIME
88 wxGenericValidator::wxGenericValidator(const wxGenericValidator
& val
)
94 bool wxGenericValidator::Copy(const wxGenericValidator
& val
)
96 wxValidator::Copy(val
);
98 m_pBool
= val
.m_pBool
;
100 m_pString
= val
.m_pString
;
101 m_pArrayInt
= val
.m_pArrayInt
;
103 m_pDateTime
= val
.m_pDateTime
;
104 #endif // wxUSE_DATETIME
109 // Called to transfer data to the window
110 bool wxGenericValidator::TransferToWindow(void)
112 if ( !m_validatorWindow
)
117 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
119 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
122 pControl
->SetValue(*m_pBool
);
128 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
130 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
133 pControl
->SetValue(*m_pBool
) ;
140 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
142 wxToggleButton
* pControl
= (wxToggleButton
*) m_validatorWindow
;
145 pControl
->SetValue(*m_pBool
);
149 #if defined(__WXMAC__) || defined(__WXGTK20__)
150 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxBitmapToggleButton
)) )
152 wxBitmapToggleButton
* pControl
= (wxBitmapToggleButton
*) m_validatorWindow
;
155 pControl
->SetValue(*m_pBool
);
164 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
166 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
169 pControl
->SetValue(*m_pInt
);
175 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
177 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
180 pControl
->SetSelection(*m_pInt
) ;
186 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
188 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
191 pControl
->SetThumbPosition(*m_pInt
) ;
196 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
197 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
199 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
202 pControl
->SetValue(*m_pInt
);
208 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
210 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
213 pControl
->SetValue(*m_pInt
) ;
219 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
221 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
224 pControl
->SetValue(*m_pInt
) ;
230 // date time controls
231 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
232 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxDatePickerCtrl
)) )
234 wxDatePickerCtrl
* pControl
= (wxDatePickerCtrl
*) m_validatorWindow
;
237 pControl
->SetValue(*m_pDateTime
) ;
245 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
247 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
250 pControl
->SetLabel(*m_pString
) ;
256 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
258 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
261 pControl
->SetSelection(*m_pInt
) ;
266 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
268 pControl
->SetStringSelection(* m_pString
);
270 if ((m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) == 0)
272 pControl
->SetValue(* m_pString
);
279 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
281 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
284 pControl
->SetSelection(*m_pInt
) ;
289 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
291 pControl
->SetStringSelection(* m_pString
);
298 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
300 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
303 pControl
->SetLabel(*m_pString
) ;
309 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
311 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
314 pControl
->SetValue(*m_pString
) ;
320 str
.Printf(wxT("%d"), *m_pInt
);
321 pControl
->SetValue(str
);
328 #if wxUSE_CHECKLISTBOX
329 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
330 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
332 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
335 // clear all selections
337 count
= pControl
->GetCount();
338 for ( i
= 0 ; i
< count
; i
++ )
339 pControl
->Check(i
, false);
341 // select each item in our array
342 count
= m_pArrayInt
->GetCount();
343 for ( i
= 0 ; i
< count
; i
++ )
344 pControl
->Check(m_pArrayInt
->Item(i
));
353 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
355 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
358 // clear all selections
360 count
= pControl
->GetCount();
361 for ( i
= 0 ; i
< count
; i
++ )
362 pControl
->Deselect(i
);
364 // select each item in our array
365 count
= m_pArrayInt
->GetCount();
366 for ( i
= 0 ; i
< count
; i
++ )
367 pControl
->SetSelection(m_pArrayInt
->Item(i
));
373 ; // to match the last 'else' above
375 // unrecognized control, or bad pointer
379 // Called to transfer data from the window
380 bool wxGenericValidator::TransferFromWindow(void)
382 if ( !m_validatorWindow
)
385 // BOOL CONTROLS **************************************
387 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
389 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
392 *m_pBool
= pControl
->GetValue() ;
398 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
400 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
403 *m_pBool
= pControl
->GetValue() ;
409 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
411 wxToggleButton
*pControl
= (wxToggleButton
*) m_validatorWindow
;
414 *m_pBool
= pControl
->GetValue() ;
420 // INT CONTROLS ***************************************
422 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
424 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
427 *m_pInt
= pControl
->GetValue() ;
433 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
435 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
438 *m_pInt
= pControl
->GetSelection() ;
444 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
446 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
449 *m_pInt
= pControl
->GetThumbPosition() ;
454 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
455 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
457 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
460 *m_pInt
=pControl
->GetValue();
466 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
468 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
471 *m_pInt
= pControl
->GetValue() ;
477 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
479 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
482 *m_pInt
= pControl
->GetValue() ;
488 // DATE TIME CONTROLS ************************************
489 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
490 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxDatePickerCtrl
)) )
492 wxDatePickerCtrl
* pControl
= (wxDatePickerCtrl
*) m_validatorWindow
;
495 *m_pDateTime
= pControl
->GetValue() ;
501 // STRING CONTROLS ************************************
503 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
505 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
508 *m_pString
= pControl
->GetLabel() ;
514 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
516 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
519 *m_pInt
= pControl
->GetSelection() ;
524 if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
)
525 *m_pString
= pControl
->GetStringSelection();
527 *m_pString
= pControl
->GetValue();
533 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
535 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
538 *m_pInt
= pControl
->GetSelection() ;
543 *m_pString
= pControl
->GetStringSelection();
549 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
551 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
554 *m_pString
= pControl
->GetLabel() ;
560 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
562 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
565 *m_pString
= pControl
->GetValue() ;
570 *m_pInt
= wxAtoi(pControl
->GetValue());
576 // ARRAY CONTROLS *************************************
577 #if wxUSE_CHECKLISTBOX
578 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
579 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
581 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
585 m_pArrayInt
->Clear();
587 // add each selected item to our array
589 count
= pControl
->GetCount();
590 for ( i
= 0; i
< count
; i
++ )
592 if (pControl
->IsChecked(i
))
603 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
605 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
609 m_pArrayInt
->Clear();
611 // add each selected item to our array
613 count
= pControl
->GetCount();
614 for ( i
= 0; i
< count
; i
++ )
616 if (pControl
->IsSelected(i
))
625 // unrecognized control, or bad pointer
632 Called by constructors to initialize ALL data members
634 void wxGenericValidator::Initialize()
642 #endif // wxUSE_DATETIME
645 #endif // wxUSE_VALIDATORS