]>
git.saurik.com Git - wxWidgets.git/blob - src/common/valgen.cpp
e9aaf19e38386f7a6057edb67a8c58d319e26cee
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericValidator class
6 // Created: Jan 22 1999
8 // Copyright: (c) 1999 Kevin Smith
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "valgen.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
32 #include "wx/dynarray.h"
33 #include "wx/choice.h"
34 #include "wx/combobox.h"
35 #include "wx/radiobox.h"
36 #include "wx/radiobut.h"
37 #include "wx/checkbox.h"
38 #include "wx/scrolbar.h"
40 #include "wx/stattext.h"
41 #include "wx/textctrl.h"
42 #include "wx/button.h"
43 #include "wx/listbox.h"
44 #include "wx/slider.h"
48 #include "wx/spinctrl.h"
51 #include "wx/spinbutt.h"
53 #if wxUSE_CHECKLISTBOX
54 #include "wx/checklst.h"
57 #include "wx/valgen.h"
59 IMPLEMENT_CLASS(wxGenericValidator
, wxValidator
)
61 wxGenericValidator::wxGenericValidator(bool *val
)
67 wxGenericValidator::wxGenericValidator(int *val
)
73 wxGenericValidator::wxGenericValidator(wxString
*val
)
79 wxGenericValidator::wxGenericValidator(wxArrayInt
*val
)
85 wxGenericValidator::wxGenericValidator(const wxGenericValidator
& val
)
91 bool wxGenericValidator::Copy(const wxGenericValidator
& val
)
93 wxValidator::Copy(val
);
95 m_pBool
= val
.m_pBool
;
97 m_pString
= val
.m_pString
;
98 m_pArrayInt
= val
.m_pArrayInt
;
103 // Called to transfer data to the window
104 bool wxGenericValidator::TransferToWindow(void)
106 if ( !m_validatorWindow
)
111 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
113 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
116 pControl
->SetValue(*m_pBool
);
122 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
124 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
127 pControl
->SetValue(*m_pBool
) ;
135 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
137 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
140 pControl
->SetValue(*m_pInt
);
146 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
148 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
151 pControl
->SetSelection(*m_pInt
) ;
157 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
159 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
162 pControl
->SetThumbPosition(*m_pInt
) ;
167 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
168 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
170 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
173 pControl
->SetValue(*m_pInt
);
179 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
181 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
184 pControl
->SetValue(*m_pInt
) ;
190 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
192 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
195 pControl
->SetValue(*m_pInt
) ;
203 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
205 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
208 pControl
->SetLabel(*m_pString
) ;
214 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
216 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
219 pControl
->SetSelection(*m_pInt
) ;
224 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
226 pControl
->SetStringSelection(* m_pString
);
228 if ((m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) == 0)
230 pControl
->SetValue(* m_pString
);
237 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
239 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
242 pControl
->SetSelection(*m_pInt
) ;
247 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
249 pControl
->SetStringSelection(* m_pString
);
256 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
258 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
261 pControl
->SetLabel(*m_pString
) ;
267 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
269 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
272 pControl
->SetValue(*m_pString
) ;
278 str
.Printf(wxT("%d"), *m_pInt
);
279 pControl
->SetValue(str
);
285 #if wxUSE_CHECKLISTBOX
286 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
287 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
289 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
292 // clear all selections
294 count
= pControl
->GetCount();
295 for ( i
= 0 ; i
< count
; i
++ )
296 pControl
->Check(i
, false);
298 // select each item in our array
299 count
= m_pArrayInt
->GetCount();
300 for ( i
= 0 ; i
< count
; i
++ )
301 pControl
->Check(m_pArrayInt
->Item(i
));
310 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
312 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
315 // clear all selections
317 count
= pControl
->GetCount();
318 for ( i
= 0 ; i
< count
; i
++ )
319 pControl
->Deselect(i
);
321 // select each item in our array
322 count
= m_pArrayInt
->GetCount();
323 for ( i
= 0 ; i
< count
; i
++ )
324 pControl
->SetSelection(m_pArrayInt
->Item(i
));
330 ; // to match the last 'else' above
332 // unrecognized control, or bad pointer
336 // Called to transfer data from the window
337 bool wxGenericValidator::TransferFromWindow(void)
339 if ( !m_validatorWindow
)
344 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
346 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
349 *m_pBool
= pControl
->GetValue() ;
355 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
357 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
360 *m_pBool
= pControl
->GetValue() ;
367 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
369 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
372 *m_pInt
= pControl
->GetValue() ;
378 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
380 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
383 *m_pInt
= pControl
->GetSelection() ;
389 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
391 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
394 *m_pInt
= pControl
->GetThumbPosition() ;
399 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
400 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
402 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
405 *m_pInt
=pControl
->GetValue();
411 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
413 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
416 *m_pInt
= pControl
->GetValue() ;
422 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
424 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
427 *m_pInt
= pControl
->GetValue() ;
434 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
436 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
439 *m_pString
= pControl
->GetLabel() ;
445 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
447 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
450 *m_pInt
= pControl
->GetSelection() ;
455 if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
)
456 *m_pString
= pControl
->GetStringSelection();
458 *m_pString
= pControl
->GetValue();
464 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
466 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
469 *m_pInt
= pControl
->GetSelection() ;
474 *m_pString
= pControl
->GetStringSelection();
480 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
482 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
485 *m_pString
= pControl
->GetLabel() ;
491 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
493 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
496 *m_pString
= pControl
->GetValue() ;
501 *m_pInt
= wxAtoi(pControl
->GetValue());
507 #if wxUSE_CHECKLISTBOX
508 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
509 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
511 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
515 m_pArrayInt
->Clear();
517 // add each selected item to our array
519 count
= pControl
->GetCount();
520 for ( i
= 0; i
< count
; i
++ )
522 if (pControl
->IsChecked(i
))
533 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
535 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
539 m_pArrayInt
->Clear();
541 // add each selected item to our array
543 count
= pControl
->GetCount();
544 for ( i
= 0; i
< count
; i
++ )
546 if (pControl
->Selected(i
))
555 // unrecognized control, or bad pointer
561 Called by constructors to initialize ALL data members
563 void wxGenericValidator::Initialize()