2 * common__validators.cpp
4 * Created on: 27 avr. 2009
13 #include "common__validators.h"
15 int StrToInt( const wxString
& str
, bool& ok
);
16 int StrToInt( const wxString
& str
);
17 wxString
IntToStr(const int _v
);
18 wxString
FloatToStr( const float f
);
19 float StrToFloat(const wxString
& str
, bool& ok
);
21 bool IsEditableKeyCode( int kc
)
37 && ( kc
< WXK_F1
|| kc
< WXK_F24
);
41 //*********************************************************************
42 //*********************************************************************
43 //*********************************************************************
44 //*********************************************************************
45 //*********************************************************************
46 //*********************************************************************
48 BEGIN_EVENT_TABLE(wxValidator_Restrict
, wxValidator
)
49 EVT_CHAR(wxValidator_Restrict::OnChar
)
53 //*********************************************************************
55 wxValidator_Restrict::wxValidator_Restrict( const wxString
& _only_those
, wxString
* _valPtr
)
57 only_those
= _only_those
;
62 //*********************
64 wxValidator_Restrict::wxValidator_Restrict(const wxValidator_Restrict
&from
)
68 only_those
= from
.only_those
;
73 //*********************
75 wxObject
* wxValidator_Restrict::Clone() const
77 wxValidator_Restrict
*clone
= new wxValidator_Restrict(only_those
, valPtr
);
84 bool wxValidator_Restrict::Validate(wxWindow
* parent
)
86 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
87 wxString value
= ctrl
->GetValue();
89 // Is the control enabled?
90 if (!ctrl
->IsEnabled())
93 for( size_t i
= 0; i
< value
.Len(); i
++ )
94 if( ! CharAccepted( value
[i
] ) )
102 bool wxValidator_Restrict::CharAccepted( int kc
)
105 for( size_t i
=0; i
< only_those
.Len(); i
++)
107 if( kc
== only_those
[i
] )
118 void wxValidator_Restrict::OnChar(wxKeyEvent
& event
)
120 int chr
= event
.GetKeyCode();
122 if( ! IsEditableKeyCode( chr
) )
129 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
130 wxString value
= ctrl
->GetValue();
133 if( ! CharAccepted( chr
) )
146 // Transfer to window
147 bool wxValidator_Restrict::TransferToWindow()
154 // Receive from window
155 bool wxValidator_Restrict::TransferFromWindow() {
156 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
157 wxString value
= ctrl
->GetValue();
165 //*********************************************************************
166 //*********************************************************************
167 //*********************************************************************
168 //*********************************************************************
169 //*********************************************************************
170 //*********************************************************************
171 //*********************************************************************
172 //*********************************************************************
173 //*********************************************************************
174 //*********************************************************************
175 //*********************************************************************
176 //*********************************************************************
178 BEGIN_EVENT_TABLE(wxValidator_Restrict_Range
, wxValidator
)
179 EVT_CHAR(wxValidator_Restrict_Range::OnChar
)
185 wxValidator_Restrict_Range::wxValidator_Restrict_Range(wxChar _from_here
, wxChar _to_here
, wxString
* _valPtr
)
187 from_here
= _from_here
;
193 //*********************
195 wxValidator_Restrict_Range::wxValidator_Restrict_Range(const wxValidator_Restrict_Range
&from
)
198 valPtr
= from
.valPtr
;
199 from_here
= from
.from_here
;
200 to_here
= from
.to_here
;
205 //*********************
207 wxObject
* wxValidator_Restrict_Range::Clone() const
209 wxValidator_Restrict_Range
*clone
= new wxValidator_Restrict_Range(from_here
,to_here
, valPtr
);
216 bool wxValidator_Restrict_Range::Validate(wxWindow
* parent
)
218 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
219 wxString value
= ctrl
->GetValue();
221 // Is the control enabled?
222 if (!ctrl
->IsEnabled())
225 for( size_t i
= 0; i
< value
.Len(); i
++ )
226 if( ! CharAccepted( value
[i
] ) )
234 bool wxValidator_Restrict_Range::CharAccepted( int kc
)
236 return ( kc
>= from_here
&& kc
<= to_here
);
241 void wxValidator_Restrict_Range::OnChar(wxKeyEvent
& event
)
243 int chr
= event
.GetKeyCode();
245 if( ! IsEditableKeyCode( chr
) )
252 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
253 wxString value
= ctrl
->GetValue();
256 if( ! CharAccepted( chr
) )
269 // Transfer to window
270 bool wxValidator_Restrict_Range::TransferToWindow()
277 // Receive from window
278 bool wxValidator_Restrict_Range::TransferFromWindow() {
279 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
280 wxString value
= ctrl
->GetValue();
288 //****************************************************
289 //****************************************************
290 //****************************************************
292 BEGIN_EVENT_TABLE(wxTextValidator_NoWhiteSpace
, wxValidator
)
293 EVT_CHAR(wxTextValidator_NoWhiteSpace::OnChar
)
296 wxTextValidator_NoWhiteSpace::wxTextValidator_NoWhiteSpace( wxString
* _valPtr
)
301 //*********************
303 wxTextValidator_NoWhiteSpace::wxTextValidator_NoWhiteSpace(const wxTextValidator_NoWhiteSpace
&from
)
306 valPtr
= from
.valPtr
;
311 //*********************
313 wxObject
* wxTextValidator_NoWhiteSpace::Clone() const
315 wxTextValidator_NoWhiteSpace
*clone
= new wxTextValidator_NoWhiteSpace(valPtr
);
322 //*********************
324 void wxTextValidator_NoWhiteSpace::OnChar( wxKeyEvent
& evt
)
326 int kc
= evt
.GetKeyCode();
328 if( kc
== WXK_SPACE
)
334 //*************************
336 bool wxTextValidator_NoWhiteSpace::Validate(wxWindow
* parent
)
338 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
339 wxString value
= ctrl
->GetValue();
341 // Is the control enabled?
342 if (!ctrl
->IsEnabled())
351 // Transfer to window
352 bool wxTextValidator_NoWhiteSpace::TransferToWindow()
359 // Receive from window
360 bool wxTextValidator_NoWhiteSpace::TransferFromWindow() {
361 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
362 wxString value
= ctrl
->GetValue();
369 //****************************************************
370 //****************************************************
371 //****************************************************
373 BEGIN_EVENT_TABLE(wxValidatorIntegerRelative
, wxValidator
)
374 EVT_CHAR(wxValidatorIntegerRelative::OnChar
)
377 wxValidatorIntegerRelative::wxValidatorIntegerRelative( wxString
* _valPtr
)
382 //*********************
384 wxValidatorIntegerRelative::wxValidatorIntegerRelative(const wxValidatorIntegerRelative
&from
)
387 valPtr
= from
.valPtr
;
392 //*********************
394 wxObject
* wxValidatorIntegerRelative::Clone() const
396 wxValidatorIntegerRelative
*clone
= new wxValidatorIntegerRelative(valPtr
);
403 //*********************
405 void wxValidatorIntegerRelative::OnChar( wxKeyEvent
& event
)
407 int kc
= event
.GetKeyCode();
409 if( ! IsEditableKeyCode( kc
) )
415 if( kc
!= '0' && kc
!= '1' && kc
!= '2' && kc
!= '3' && kc
!= '4' && kc
!= '5'
416 && kc
!= '6' && kc
!= '7' && kc
!= '8' && kc
!= '9' && kc
!= '-' )
422 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
423 wxString value
= ctrl
->GetValue();
424 int ind
= ctrl
->GetInsertionPoint();
426 if( kc
== '-' && ind
> 0 )
435 //*************************
437 bool wxValidatorIntegerRelative::Validate(wxWindow
* parent
)
439 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
440 wxString value
= ctrl
->GetValue();
442 // Is the control enabled?
443 if (!ctrl
->IsEnabled())
446 if( value
== wxString() )
450 StrToInt( value
, ok
);
457 // Transfer to window
458 bool wxValidatorIntegerRelative::TransferToWindow()
465 // Receive from window
466 bool wxValidatorIntegerRelative::TransferFromWindow() {
467 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
468 wxString value
= ctrl
->GetValue();
475 //*************************
476 //*************************
477 //*************************
478 //*************************
479 //*************************
481 BEGIN_EVENT_TABLE(wxValidatorIntegerNegative
, wxValidator
)
482 EVT_CHAR(wxValidatorIntegerNegative::OnChar
)
486 wxValidatorIntegerNegative::wxValidatorIntegerNegative( wxString
* _valPtr
)
491 //*********************
493 wxValidatorIntegerNegative::wxValidatorIntegerNegative(const wxValidatorIntegerNegative
&from
)
494 : wxValidatorIntegerRelative()
496 valPtr
= from
.valPtr
;
501 //*********************
503 wxObject
* wxValidatorIntegerNegative::Clone() const
505 wxValidatorIntegerNegative
*clone
= new wxValidatorIntegerNegative(valPtr
);
509 //*************************
511 bool wxValidatorIntegerNegative::Validate(wxWindow
* parent
)
513 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
514 wxString value
= ctrl
->GetValue();
516 // Is the control enabled?
517 if (!ctrl
->IsEnabled())
520 if( value
== wxString() )
523 bool res
= (StrToInt( value
) <= 0);
529 //*************************
531 void wxValidatorIntegerNegative::OnChar( wxKeyEvent
& event
)
533 int kc
= event
.GetKeyCode();
535 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
536 wxString value
= ctrl
->GetValue();
537 int ind
= ctrl
->GetInsertionPoint();
541 if( kc
== WXK_BACK
&& ind
== 1 &&
542 value
.Len() > 1 && value
[0] == '-' )
544 if( kc
== WXK_DELETE
&& ind
== 0 && value
.Len() > 1 && value
[0] == '-' )
553 if( ! IsEditableKeyCode( kc
) )
559 if( ( kc
< '0' || kc
> '9') && kc
!= '-' )
566 if( kc
== '-' && ind
== 0 && ( value
.Len() == 0 || value
[0] != '-') )
569 else if( kc
!= '-' &&
570 ( (ind
> 0 && value
[0] == '-' )
572 ( kc
== '0' && value
.Len() == 0 )
587 //****************************************************
588 //****************************************************
589 //****************************************************
591 BEGIN_EVENT_TABLE(wxValidatorFloat
, wxValidator
)
592 EVT_CHAR(wxValidatorFloat::OnChar
)
595 wxValidatorFloat::wxValidatorFloat( wxString
* _valPtr
)
600 //*********************
602 wxValidatorFloat::wxValidatorFloat(const wxValidatorFloat
&from
)
605 valPtr
= from
.valPtr
;
610 //*********************
612 wxObject
* wxValidatorFloat::Clone() const
614 wxValidatorFloat
*clone
= new wxValidatorFloat(valPtr
);
621 //*********************
623 void wxValidatorFloat::OnChar( wxKeyEvent
& event
)
625 int kc
= event
.GetKeyCode();
627 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
628 wxString value
= ctrl
->GetValue();
629 int ind
= ctrl
->GetInsertionPoint();
631 if( ! IsEditableKeyCode( kc
) )
637 if( ( kc
< '0' || kc
> '9') && kc
!= '-' && kc
!= '.' )
648 else if( value
.Find( '-' ) != wxNOT_FOUND
)
654 if( value
.Find( '.' ) != wxNOT_FOUND
)
657 else if( ind
== 0 && value
[0] == '-' )
669 wxMessageBox( FloatToStr( StrToFloat( value, b_ok )));
674 //*************************
676 bool wxValidatorFloat::Validate(wxWindow
* parent
)
678 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
679 wxString value
= ctrl
->GetValue();
681 // Is the control enabled?
682 if (!ctrl
->IsEnabled())
685 if( value
== wxString() )
689 StrToInt( value
, ok
);
696 // Transfer to window
697 bool wxValidatorFloat::TransferToWindow()
704 // Receive from window
705 bool wxValidatorFloat::TransferFromWindow() {
706 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
707 wxString value
= ctrl
->GetValue();
713 //****************************************************
714 //****************************************************
715 //****************************************************
717 BEGIN_EVENT_TABLE(wxValidatorFloatPositive
, wxValidator
)
718 EVT_CHAR(wxValidatorFloatPositive::OnChar
)
721 wxValidatorFloatPositive::wxValidatorFloatPositive( wxString
* _valPtr
)
722 :wxValidatorFloat( _valPtr
)
726 //*********************
728 wxValidatorFloatPositive::wxValidatorFloatPositive(const wxValidatorFloatPositive
&from
)
730 valPtr
= from
.valPtr
;
735 //*********************
737 wxObject
* wxValidatorFloatPositive::Clone() const
739 wxValidatorFloatPositive
*clone
= new wxValidatorFloatPositive(valPtr
);
746 //*********************
748 void wxValidatorFloatPositive::OnChar( wxKeyEvent
& event
)
750 int kc
= event
.GetKeyCode();
758 wxValidatorFloat::OnChar( event
);
761 //*************************
763 bool wxValidatorFloatPositive::Validate(wxWindow
* parent
)
765 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
766 wxString value
= ctrl
->GetValue();
768 // Is the control enabled?
769 if (!ctrl
->IsEnabled())
772 if( value
== wxString() )
776 StrToInt( value
, ok
);
783 // Transfer to window
784 bool wxValidatorFloatPositive::TransferToWindow()
791 // Receive from window
792 bool wxValidatorFloatPositive::TransferFromWindow() {
793 wxTextCtrl
*ctrl
= (wxTextCtrl
*) GetWindow();
794 wxString value
= ctrl
->GetValue();