]>
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" -- can't use it in this (core) file for now
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(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__)
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
376 // unrecognized control, or bad pointer
380 // Called to transfer data from the window
381 bool wxGenericValidator::TransferFromWindow(void)
383 if ( !m_validatorWindow
)
386 // BOOL CONTROLS **************************************
388 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
390 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
393 *m_pBool
= pControl
->GetValue() ;
399 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
401 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
404 *m_pBool
= pControl
->GetValue() ;
410 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
412 wxToggleButton
*pControl
= (wxToggleButton
*) m_validatorWindow
;
415 *m_pBool
= pControl
->GetValue() ;
419 #if (defined(__WXMAC__) || defined(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__)
420 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxBitmapToggleButton
)) )
422 wxBitmapToggleButton
*pControl
= (wxBitmapToggleButton
*) m_validatorWindow
;
425 *m_pBool
= pControl
->GetValue() ;
432 // INT CONTROLS ***************************************
434 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
436 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
439 *m_pInt
= pControl
->GetValue() ;
445 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
447 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
450 *m_pInt
= pControl
->GetSelection() ;
456 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
458 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
461 *m_pInt
= pControl
->GetThumbPosition() ;
466 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
467 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
469 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
472 *m_pInt
=pControl
->GetValue();
478 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
480 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
483 *m_pInt
= pControl
->GetValue() ;
489 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
491 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
494 *m_pInt
= pControl
->GetValue() ;
500 // DATE TIME CONTROLS ************************************
501 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
502 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxDatePickerCtrl
)) )
504 wxDatePickerCtrl
* pControl
= (wxDatePickerCtrl
*) m_validatorWindow
;
507 *m_pDateTime
= pControl
->GetValue() ;
513 // STRING CONTROLS ************************************
515 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
517 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
520 *m_pString
= pControl
->GetLabel() ;
526 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
528 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
531 *m_pInt
= pControl
->GetSelection() ;
536 if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
)
537 *m_pString
= pControl
->GetStringSelection();
539 *m_pString
= pControl
->GetValue();
545 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
547 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
550 *m_pInt
= pControl
->GetSelection() ;
555 *m_pString
= pControl
->GetStringSelection();
561 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
563 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
566 *m_pString
= pControl
->GetLabel() ;
572 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
574 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
577 *m_pString
= pControl
->GetValue() ;
582 *m_pInt
= wxAtoi(pControl
->GetValue());
588 // ARRAY CONTROLS *************************************
589 #if wxUSE_CHECKLISTBOX
590 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
591 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
593 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
597 m_pArrayInt
->Clear();
599 // add each selected item to our array
601 count
= pControl
->GetCount();
602 for ( i
= 0; i
< count
; i
++ )
604 if (pControl
->IsChecked(i
))
615 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
617 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
621 m_pArrayInt
->Clear();
623 // add each selected item to our array
625 count
= pControl
->GetCount();
626 for ( i
= 0; i
< count
; i
++ )
628 if (pControl
->IsSelected(i
))
637 // unrecognized control, or bad pointer
644 Called by constructors to initialize ALL data members
646 void wxGenericValidator::Initialize()
654 #endif // wxUSE_DATETIME
657 #endif // wxUSE_VALIDATORS