]>
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
7 // Copyright: (c) 1999 Kevin Smith
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
21 #include "wx/dynarray.h"
24 #include "wx/choice.h"
25 #include "wx/combobox.h"
26 #include "wx/radiobox.h"
27 #include "wx/radiobut.h"
28 #include "wx/checkbox.h"
29 #include "wx/scrolbar.h"
31 #include "wx/stattext.h"
32 #include "wx/textctrl.h"
33 #include "wx/button.h"
34 #include "wx/listbox.h"
35 #include "wx/slider.h"
36 #include "wx/checklst.h"
39 #include "wx/spinctrl.h"
40 // #include "wx/datectrl.h" -- can't use it in this (core) file for now
43 #include "wx/spinbutt.h"
46 #include "wx/tglbtn.h"
48 #include "wx/filename.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(wxFileName
*val
)
94 wxGenericValidator::wxGenericValidator(float *val
)
100 wxGenericValidator::wxGenericValidator(double *val
)
106 wxGenericValidator::wxGenericValidator(const wxGenericValidator
& val
)
112 bool wxGenericValidator::Copy(const wxGenericValidator
& val
)
114 wxValidator::Copy(val
);
116 m_pBool
= val
.m_pBool
;
118 m_pString
= val
.m_pString
;
119 m_pArrayInt
= val
.m_pArrayInt
;
121 m_pDateTime
= val
.m_pDateTime
;
122 #endif // wxUSE_DATETIME
123 m_pFileName
= val
.m_pFileName
;
124 m_pFloat
= val
.m_pFloat
;
125 m_pDouble
= val
.m_pDouble
;
130 // Called to transfer data to the window
131 bool wxGenericValidator::TransferToWindow(void)
133 if ( !m_validatorWindow
)
138 if (wxDynamicCast(m_validatorWindow
, wxCheckBox
))
140 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
143 pControl
->SetValue(*m_pBool
);
149 if (wxDynamicCast(m_validatorWindow
, wxRadioButton
))
151 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
154 pControl
->SetValue(*m_pBool
) ;
161 if (wxDynamicCast(m_validatorWindow
, wxToggleButton
))
163 wxToggleButton
* pControl
= (wxToggleButton
*) m_validatorWindow
;
166 pControl
->SetValue(*m_pBool
);
170 #if (defined(__WXMAC__) || defined(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__)
171 if (wxDynamicCast(m_validatorWindow
, wxBitmapToggleButton
))
173 wxBitmapToggleButton
* pControl
= (wxBitmapToggleButton
*) m_validatorWindow
;
176 pControl
->SetValue(*m_pBool
);
185 if (wxDynamicCast(m_validatorWindow
, wxGauge
))
187 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
190 pControl
->SetValue(*m_pInt
);
196 if (wxDynamicCast(m_validatorWindow
, wxRadioBox
))
198 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
201 pControl
->SetSelection(*m_pInt
) ;
207 if (wxDynamicCast(m_validatorWindow
, wxScrollBar
))
209 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
212 pControl
->SetThumbPosition(*m_pInt
) ;
217 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
218 if (wxDynamicCast(m_validatorWindow
, wxSpinCtrl
))
220 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
223 pControl
->SetValue(*m_pInt
);
229 if (wxDynamicCast(m_validatorWindow
, wxSpinButton
))
231 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
234 pControl
->SetValue(*m_pInt
) ;
240 if (wxDynamicCast(m_validatorWindow
, wxSlider
))
242 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
245 pControl
->SetValue(*m_pInt
) ;
251 // date time controls
252 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
253 if (wxDynamicCast(m_validatorWindow
, wxDatePickerCtrl
))
255 wxDatePickerCtrl
* pControl
= (wxDatePickerCtrl
*) m_validatorWindow
;
258 pControl
->SetValue(*m_pDateTime
) ;
266 if (wxDynamicCast(m_validatorWindow
, wxButton
))
268 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
271 pControl
->SetLabel(*m_pString
) ;
277 if (wxDynamicCast(m_validatorWindow
, wxComboBox
))
279 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
282 pControl
->SetSelection(*m_pInt
) ;
287 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
289 pControl
->SetStringSelection(* m_pString
);
291 if ((m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
) == 0)
293 pControl
->SetValue(* m_pString
);
300 if (wxDynamicCast(m_validatorWindow
, wxChoice
))
302 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
305 pControl
->SetSelection(*m_pInt
) ;
310 if (pControl
->FindString(* m_pString
) != wxNOT_FOUND
)
312 pControl
->SetStringSelection(* m_pString
);
319 if (wxDynamicCast(m_validatorWindow
, wxStaticText
))
321 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
324 pControl
->SetLabel(*m_pString
) ;
330 if (wxDynamicCast(m_validatorWindow
, wxTextCtrl
))
332 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
335 pControl
->SetValue(*m_pString
) ;
341 str
.Printf(wxT("%d"), *m_pInt
);
342 pControl
->SetValue(str
);
345 else if (m_pFileName
)
347 pControl
->SetValue(m_pFileName
->GetFullPath());
352 pControl
->SetValue(wxString::Format(wxT("%g"), *m_pFloat
));
357 pControl
->SetValue(wxString::Format(wxT("%g"), *m_pDouble
));
364 #if wxUSE_CHECKLISTBOX
365 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
366 if (wxDynamicCast(m_validatorWindow
, wxCheckListBox
))
368 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
371 // clear all selections
373 count
= pControl
->GetCount();
374 for ( i
= 0 ; i
< count
; i
++ )
375 pControl
->Check(i
, false);
377 // select each item in our array
378 count
= m_pArrayInt
->GetCount();
379 for ( i
= 0 ; i
< count
; i
++ )
380 pControl
->Check(m_pArrayInt
->Item(i
));
389 if (wxDynamicCast(m_validatorWindow
, wxListBox
))
391 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
394 // clear all selections
396 count
= pControl
->GetCount();
397 for ( i
= 0 ; i
< count
; i
++ )
398 pControl
->Deselect(i
);
400 // select each item in our array
401 count
= m_pArrayInt
->GetCount();
402 for ( i
= 0 ; i
< count
; i
++ )
403 pControl
->SetSelection(m_pArrayInt
->Item(i
));
409 { // to match the last 'else' above
412 // unrecognized control, or bad pointer
416 // Called to transfer data from the window
417 bool wxGenericValidator::TransferFromWindow(void)
419 if ( !m_validatorWindow
)
422 // BOOL CONTROLS **************************************
424 if (wxDynamicCast(m_validatorWindow
, wxCheckBox
))
426 wxCheckBox
* pControl
= (wxCheckBox
*) m_validatorWindow
;
429 *m_pBool
= pControl
->GetValue() ;
435 if (wxDynamicCast(m_validatorWindow
, wxRadioButton
))
437 wxRadioButton
* pControl
= (wxRadioButton
*) m_validatorWindow
;
440 *m_pBool
= pControl
->GetValue() ;
446 if (wxDynamicCast(m_validatorWindow
, wxToggleButton
))
448 wxToggleButton
*pControl
= (wxToggleButton
*) m_validatorWindow
;
451 *m_pBool
= pControl
->GetValue() ;
455 #if (defined(__WXMAC__) || defined(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__)
456 if (wxDynamicCast(m_validatorWindow
, wxBitmapToggleButton
))
458 wxBitmapToggleButton
*pControl
= (wxBitmapToggleButton
*) m_validatorWindow
;
461 *m_pBool
= pControl
->GetValue() ;
468 // INT CONTROLS ***************************************
470 if (wxDynamicCast(m_validatorWindow
, wxGauge
))
472 wxGauge
* pControl
= (wxGauge
*) m_validatorWindow
;
475 *m_pInt
= pControl
->GetValue() ;
481 if (wxDynamicCast(m_validatorWindow
, wxRadioBox
))
483 wxRadioBox
* pControl
= (wxRadioBox
*) m_validatorWindow
;
486 *m_pInt
= pControl
->GetSelection() ;
492 if (wxDynamicCast(m_validatorWindow
, wxScrollBar
))
494 wxScrollBar
* pControl
= (wxScrollBar
*) m_validatorWindow
;
497 *m_pInt
= pControl
->GetThumbPosition() ;
502 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
503 if (wxDynamicCast(m_validatorWindow
, wxSpinCtrl
))
505 wxSpinCtrl
* pControl
= (wxSpinCtrl
*) m_validatorWindow
;
508 *m_pInt
=pControl
->GetValue();
514 if (wxDynamicCast(m_validatorWindow
, wxSpinButton
))
516 wxSpinButton
* pControl
= (wxSpinButton
*) m_validatorWindow
;
519 *m_pInt
= pControl
->GetValue() ;
525 if (wxDynamicCast(m_validatorWindow
, wxSlider
))
527 wxSlider
* pControl
= (wxSlider
*) m_validatorWindow
;
530 *m_pInt
= pControl
->GetValue() ;
536 // DATE TIME CONTROLS ************************************
537 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
538 if (wxDynamicCast(m_validatorWindow
, wxDatePickerCtrl
))
540 wxDatePickerCtrl
* pControl
= (wxDatePickerCtrl
*) m_validatorWindow
;
543 *m_pDateTime
= pControl
->GetValue() ;
549 // STRING CONTROLS ************************************
551 if (wxDynamicCast(m_validatorWindow
, wxButton
))
553 wxButton
* pControl
= (wxButton
*) m_validatorWindow
;
556 *m_pString
= pControl
->GetLabel() ;
562 if (wxDynamicCast(m_validatorWindow
, wxComboBox
))
564 wxComboBox
* pControl
= (wxComboBox
*) m_validatorWindow
;
567 *m_pInt
= pControl
->GetSelection() ;
572 if (m_validatorWindow
->GetWindowStyle() & wxCB_READONLY
)
573 *m_pString
= pControl
->GetStringSelection();
575 *m_pString
= pControl
->GetValue();
581 if (wxDynamicCast(m_validatorWindow
, wxChoice
))
583 wxChoice
* pControl
= (wxChoice
*) m_validatorWindow
;
586 *m_pInt
= pControl
->GetSelection() ;
591 *m_pString
= pControl
->GetStringSelection();
597 if (wxDynamicCast(m_validatorWindow
, wxStaticText
))
599 wxStaticText
* pControl
= (wxStaticText
*) m_validatorWindow
;
602 *m_pString
= pControl
->GetLabel() ;
608 if (wxDynamicCast(m_validatorWindow
, wxTextCtrl
))
610 wxTextCtrl
* pControl
= (wxTextCtrl
*) m_validatorWindow
;
613 *m_pString
= pControl
->GetValue() ;
618 *m_pInt
= wxAtoi(pControl
->GetValue());
621 else if (m_pFileName
)
623 m_pFileName
->Assign(pControl
->GetValue());
628 *m_pFloat
= (float)wxAtof(pControl
->GetValue());
633 *m_pDouble
= wxAtof(pControl
->GetValue());
639 // ARRAY CONTROLS *************************************
640 #if wxUSE_CHECKLISTBOX
641 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
642 if (wxDynamicCast(m_validatorWindow
, wxCheckListBox
))
644 wxCheckListBox
* pControl
= (wxCheckListBox
*) m_validatorWindow
;
648 m_pArrayInt
->Clear();
650 // add each selected item to our array
652 count
= pControl
->GetCount();
653 for ( i
= 0; i
< count
; i
++ )
655 if (pControl
->IsChecked(i
))
666 if (wxDynamicCast(m_validatorWindow
, wxListBox
))
668 wxListBox
* pControl
= (wxListBox
*) m_validatorWindow
;
672 m_pArrayInt
->Clear();
674 // add each selected item to our array
676 count
= pControl
->GetCount();
677 for ( i
= 0; i
< count
; i
++ )
679 if (pControl
->IsSelected(i
))
688 // unrecognized control, or bad pointer
695 Called by constructors to initialize ALL data members
697 void wxGenericValidator::Initialize()
705 #endif // wxUSE_DATETIME
711 #endif // wxUSE_VALIDATORS