]>
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"
43 #include "wx/spinbutt.h"
46 #include "wx/tglbtn.h"
49 #include "wx/valgen.h"
51 IMPLEMENT_CLASS(wxGenericValidator
, wxValidator
)
53 wxGenericValidator::wxGenericValidator(bool *val
)
59 wxGenericValidator::wxGenericValidator(int *val
)
65 wxGenericValidator::wxGenericValidator(wxString
*val
)
71 wxGenericValidator::wxGenericValidator(wxArrayInt
*val
)
77 wxGenericValidator::wxGenericValidator(const wxGenericValidator
& val
)
83 bool wxGenericValidator::Copy(const wxGenericValidator
& val
)
85 wxValidator::Copy(val
);
87 m_pBool
= val
.m_pBool
;
89 m_pString
= val
.m_pString
;
90 m_pArrayInt
= val
.m_pArrayInt
;
95 // Called to transfer data to the window
96 bool wxGenericValidator::TransferToWindow(void)
98 if ( !m_validatorWindow
)
103 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
105 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
108 pControl
->SetValue(*m_pBool
);
114 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
116 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
119 pControl
->SetValue(*m_pBool
) ;
126 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
128 wxToggleButton
* pControl
= (wxToggleButton
*) m_validatorWindow
;
131 pControl
->SetValue(*m_pBool
);
135 #if defined(__WXMAC__) || defined(__WXGTK20__)
136 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxBitmapToggleButton
)) )
138 wxBitmapToggleButton
* pControl
= (wxBitmapToggleButton
*) m_validatorWindow
;
141 pControl
->SetValue(*m_pBool
);
150 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
152 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
155 pControl
->SetValue(*m_pInt
);
161 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
163 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
166 pControl
->SetSelection(*m_pInt
) ;
172 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
174 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
177 pControl
->SetThumbPosition(*m_pInt
) ;
182 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
183 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
185 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
188 pControl
->SetValue(*m_pInt
);
194 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
196 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
199 pControl
->SetValue(*m_pInt
) ;
205 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
207 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
210 pControl
->SetValue(*m_pInt
) ;
218 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
220 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
223 pControl
->SetLabel(*m_pString
) ;
229 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
231 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
234 pControl
->SetSelection(*m_pInt
) ;
239 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
241 pControl
->SetStringSelection(* m_pString
);
243 if ((m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) == 0)
245 pControl
->SetValue(* m_pString
);
252 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
254 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
257 pControl
->SetSelection(*m_pInt
) ;
262 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
264 pControl
->SetStringSelection(* m_pString
);
271 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
273 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
276 pControl
->SetLabel(*m_pString
) ;
282 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
284 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
287 pControl
->SetValue(*m_pString
) ;
293 str
.Printf(wxT("%d"), *m_pInt
);
294 pControl
->SetValue(str
);
301 #if wxUSE_CHECKLISTBOX
302 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
303 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
305 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
308 // clear all selections
310 count
= pControl
->GetCount();
311 for ( i
= 0 ; i
< count
; i
++ )
312 pControl
->Check(i
, false);
314 // select each item in our array
315 count
= m_pArrayInt
->GetCount();
316 for ( i
= 0 ; i
< count
; i
++ )
317 pControl
->Check(m_pArrayInt
->Item(i
));
326 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
328 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
331 // clear all selections
333 count
= pControl
->GetCount();
334 for ( i
= 0 ; i
< count
; i
++ )
335 pControl
->Deselect(i
);
337 // select each item in our array
338 count
= m_pArrayInt
->GetCount();
339 for ( i
= 0 ; i
< count
; i
++ )
340 pControl
->SetSelection(m_pArrayInt
->Item(i
));
346 ; // to match the last 'else' above
348 // unrecognized control, or bad pointer
352 // Called to transfer data from the window
353 bool wxGenericValidator::TransferFromWindow(void)
355 if ( !m_validatorWindow
)
358 // BOOL CONTROLS **************************************
360 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckBox
)) )
362 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
365 *m_pBool
= pControl
->GetValue() ;
371 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioButton
)) )
373 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
376 *m_pBool
= pControl
->GetValue() ;
382 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxToggleButton
)) )
384 wxToggleButton
*pControl
= (wxToggleButton
*) m_validatorWindow
;
387 *m_pBool
= pControl
->GetValue() ;
393 // INT CONTROLS ***************************************
395 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxGauge
)) )
397 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
400 *m_pInt
= pControl
->GetValue() ;
406 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxRadioBox
)) )
408 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
411 *m_pInt
= pControl
->GetSelection() ;
417 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxScrollBar
)) )
419 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
422 *m_pInt
= pControl
->GetThumbPosition() ;
427 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
428 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinCtrl
)) )
430 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
433 *m_pInt
=pControl
->GetValue();
439 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSpinButton
)) )
441 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
444 *m_pInt
= pControl
->GetValue() ;
450 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxSlider
)) )
452 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
455 *m_pInt
= pControl
->GetValue() ;
461 // STRING CONTROLS ************************************
463 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxButton
)) )
465 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
468 *m_pString
= pControl
->GetLabel() ;
474 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)) )
476 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
479 *m_pInt
= pControl
->GetSelection() ;
484 if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
)
485 *m_pString
= pControl
->GetStringSelection();
487 *m_pString
= pControl
->GetValue();
493 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxChoice
)) )
495 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
498 *m_pInt
= pControl
->GetSelection() ;
503 *m_pString
= pControl
->GetStringSelection();
509 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxStaticText
)) )
511 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
514 *m_pString
= pControl
->GetLabel() ;
520 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
522 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
525 *m_pString
= pControl
->GetValue() ;
530 *m_pInt
= wxAtoi(pControl
->GetValue());
536 // ARRAY CONTROLS *************************************
537 #if wxUSE_CHECKLISTBOX
538 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
539 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxCheckListBox
)) )
541 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
545 m_pArrayInt
->Clear();
547 // add each selected item to our array
549 count
= pControl
->GetCount();
550 for ( i
= 0; i
< count
; i
++ )
552 if (pControl
->IsChecked(i
))
563 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxListBox
)) )
565 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
569 m_pArrayInt
->Clear();
571 // add each selected item to our array
573 count
= pControl
->GetCount();
574 for ( i
= 0; i
< count
; i
++ )
576 if (pControl
->IsSelected(i
))
585 // unrecognized control, or bad pointer
592 Called by constructors to initialize ALL data members
594 void wxGenericValidator::Initialize()