fix compile against newer giflib
[rofl0r-obeditor.git] / src / common__validators.cpp
blob986fe1ccbca53c7613203599ed610d632bf739fe
1 /*
2 * common__validators.cpp
4 * Created on: 27 avr. 2009
5 * Author: pat
6 */
8 #include <wx/wxprec.h>
9 #ifndef WX_PRECOMP
10 #include <wx/wx.h>
11 #endif
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 )
23 return kc != WXK_BACK
24 && kc != WXK_TAB
25 && kc != WXK_RETURN
26 && kc != WXK_ESCAPE
27 && kc != WXK_DELETE
28 && kc != WXK_END
29 && kc != WXK_HOME
30 && kc != WXK_LEFT
31 && kc != WXK_UP
32 && kc != WXK_RIGHT
33 && kc != WXK_DOWN
34 && kc != WXK_INSERT
35 && kc != WXK_PAGEUP
36 && kc != WXK_PAGEDOWN
37 && ( kc < WXK_F1 || kc < WXK_F24 );
41 //*********************************************************************
42 //*********************************************************************
43 //*********************************************************************
44 //*********************************************************************
45 //*********************************************************************
46 //*********************************************************************
47 // Event table
48 BEGIN_EVENT_TABLE(wxValidator_Restrict, wxValidator)
49 EVT_CHAR(wxValidator_Restrict::OnChar)
50 END_EVENT_TABLE()
53 //*********************************************************************
54 // Constructor
55 wxValidator_Restrict::wxValidator_Restrict( const wxString& _only_those, wxString* _valPtr)
57 only_those = _only_those;
58 valPtr = _valPtr;
62 //*********************
63 // Copy constructor
64 wxValidator_Restrict::wxValidator_Restrict(const wxValidator_Restrict &from)
65 : wxValidator()
67 valPtr = from.valPtr;
68 only_those = from.only_those;
73 //*********************
74 // Clone
75 wxObject* wxValidator_Restrict::Clone() const
77 wxValidator_Restrict *clone = new wxValidator_Restrict(only_those, valPtr );
78 return clone;
82 ////
83 // Validate
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())
91 return true;
93 for( size_t i = 0; i < value.Len(); i ++ )
94 if( ! CharAccepted( value[i] ) )
95 return false;
97 return true;
101 //********//////
102 bool wxValidator_Restrict::CharAccepted( int kc )
104 bool b_ok = false;
105 for( size_t i =0; i< only_those.Len(); i ++)
107 if( kc == only_those[i] )
109 b_ok = true;
110 break;
113 return b_ok;
116 //********//////
117 // Filter keypresses
118 void wxValidator_Restrict::OnChar(wxKeyEvent& event)
120 int chr = event.GetKeyCode();
122 if( ! IsEditableKeyCode( chr ) )
124 event.Skip();
125 return;
129 wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
130 wxString value = ctrl->GetValue();
132 // Not OK
133 if( ! CharAccepted( chr ) )
135 wxBell();
136 return;
139 // OK
140 event.Skip();
141 return;
145 //********///////
146 // Transfer to window
147 bool wxValidator_Restrict::TransferToWindow()
149 return true;
153 //*************
154 // Receive from window
155 bool wxValidator_Restrict::TransferFromWindow() {
156 wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
157 wxString value = ctrl->GetValue();
159 return true;
165 //*********************************************************************
166 //*********************************************************************
167 //*********************************************************************
168 //*********************************************************************
169 //*********************************************************************
170 //*********************************************************************
171 //*********************************************************************
172 //*********************************************************************
173 //*********************************************************************
174 //*********************************************************************
175 //*********************************************************************
176 //*********************************************************************
177 // Event table
178 BEGIN_EVENT_TABLE(wxValidator_Restrict_Range, wxValidator)
179 EVT_CHAR(wxValidator_Restrict_Range::OnChar)
180 END_EVENT_TABLE()
183 //********
184 // Constructor
185 wxValidator_Restrict_Range::wxValidator_Restrict_Range(wxChar _from_here, wxChar _to_here, wxString* _valPtr)
187 from_here = _from_here;
188 to_here = _to_here;
189 valPtr = _valPtr;
193 //*********************
194 // Copy constructor
195 wxValidator_Restrict_Range::wxValidator_Restrict_Range(const wxValidator_Restrict_Range &from)
196 : wxValidator()
198 valPtr = from.valPtr;
199 from_here = from.from_here;
200 to_here = from.to_here;
205 //*********************
206 // Clone
207 wxObject* wxValidator_Restrict_Range::Clone() const
209 wxValidator_Restrict_Range *clone = new wxValidator_Restrict_Range(from_here,to_here, valPtr );
210 return clone;
214 ////
215 // Validate
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())
223 return true;
225 for( size_t i = 0; i < value.Len(); i ++ )
226 if( ! CharAccepted( value[i] ) )
227 return false;
229 return true;
233 //********//////
234 bool wxValidator_Restrict_Range::CharAccepted( int kc )
236 return ( kc >= from_here && kc <= to_here );
239 //********//////
240 // Filter keypresses
241 void wxValidator_Restrict_Range::OnChar(wxKeyEvent& event)
243 int chr = event.GetKeyCode();
245 if( ! IsEditableKeyCode( chr ) )
247 event.Skip();
248 return;
252 wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
253 wxString value = ctrl->GetValue();
255 // Not OK
256 if( ! CharAccepted( chr ) )
258 wxBell();
259 return;
262 // OK
263 event.Skip();
264 return;
268 //********///////
269 // Transfer to window
270 bool wxValidator_Restrict_Range::TransferToWindow()
272 return true;
276 //*************
277 // Receive from window
278 bool wxValidator_Restrict_Range::TransferFromWindow() {
279 wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
280 wxString value = ctrl->GetValue();
282 return true;
288 //****************************************************
289 //****************************************************
290 //****************************************************
292 BEGIN_EVENT_TABLE(wxTextValidator_NoWhiteSpace, wxValidator)
293 EVT_CHAR(wxTextValidator_NoWhiteSpace::OnChar)
294 END_EVENT_TABLE()
296 wxTextValidator_NoWhiteSpace::wxTextValidator_NoWhiteSpace( wxString* _valPtr )
298 valPtr = _valPtr;
301 //*********************
302 // Copy constructor
303 wxTextValidator_NoWhiteSpace::wxTextValidator_NoWhiteSpace(const wxTextValidator_NoWhiteSpace &from)
304 : wxValidator()
306 valPtr = from.valPtr;
311 //*********************
312 // Clone
313 wxObject* wxTextValidator_NoWhiteSpace::Clone() const
315 wxTextValidator_NoWhiteSpace *clone = new wxTextValidator_NoWhiteSpace(valPtr );
316 return clone;
322 //*********************
324 void wxTextValidator_NoWhiteSpace::OnChar( wxKeyEvent& evt )
326 int kc = evt.GetKeyCode();
328 if( kc == WXK_SPACE )
329 wxBell();
330 else
331 evt.Skip();
334 //*************************
335 // Validate
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())
343 return true;
345 return true;
350 //*****************
351 // Transfer to window
352 bool wxTextValidator_NoWhiteSpace::TransferToWindow()
354 return true;
358 //*************
359 // Receive from window
360 bool wxTextValidator_NoWhiteSpace::TransferFromWindow() {
361 wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
362 wxString value = ctrl->GetValue();
364 return true;
369 //****************************************************
370 //****************************************************
371 //****************************************************
373 BEGIN_EVENT_TABLE(wxValidatorIntegerRelative, wxValidator)
374 EVT_CHAR(wxValidatorIntegerRelative::OnChar)
375 END_EVENT_TABLE()
377 wxValidatorIntegerRelative::wxValidatorIntegerRelative( wxString* _valPtr )
379 valPtr = _valPtr;
382 //*********************
383 // Copy constructor
384 wxValidatorIntegerRelative::wxValidatorIntegerRelative(const wxValidatorIntegerRelative &from)
385 : wxValidator()
387 valPtr = from.valPtr;
392 //*********************
393 // Clone
394 wxObject* wxValidatorIntegerRelative::Clone() const
396 wxValidatorIntegerRelative *clone = new wxValidatorIntegerRelative(valPtr );
397 return clone;
403 //*********************
405 void wxValidatorIntegerRelative::OnChar( wxKeyEvent& event )
407 int kc = event.GetKeyCode();
409 if( ! IsEditableKeyCode( kc ) )
411 event.Skip();
412 return;
415 if( kc != '0' && kc != '1' && kc != '2' && kc != '3' && kc != '4' && kc != '5'
416 && kc != '6' && kc != '7' && kc != '8' && kc != '9' && kc != '-' )
418 wxBell();
419 return;
422 wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
423 wxString value = ctrl->GetValue();
424 int ind = ctrl->GetInsertionPoint();
426 if( kc == '-' && ind > 0 )
428 wxBell();
429 return;
432 event.Skip();
435 //*************************
436 // Validate
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())
444 return true;
446 if( value == wxString() )
447 return true;
449 bool ok;
450 StrToInt( value, ok );
451 return ok;
456 //*****************
457 // Transfer to window
458 bool wxValidatorIntegerRelative::TransferToWindow()
460 return true;
464 //*************
465 // Receive from window
466 bool wxValidatorIntegerRelative::TransferFromWindow() {
467 wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
468 wxString value = ctrl->GetValue();
470 return true;
475 //*************************
476 //*************************
477 //*************************
478 //*************************
479 //*************************
481 BEGIN_EVENT_TABLE(wxValidatorIntegerNegative, wxValidator)
482 EVT_CHAR(wxValidatorIntegerNegative::OnChar)
483 END_EVENT_TABLE()
486 wxValidatorIntegerNegative::wxValidatorIntegerNegative( wxString* _valPtr )
488 valPtr = _valPtr;
491 //*********************
492 // Copy constructor
493 wxValidatorIntegerNegative::wxValidatorIntegerNegative(const wxValidatorIntegerNegative &from)
494 : wxValidatorIntegerRelative()
496 valPtr = from.valPtr;
501 //*********************
502 // Clone
503 wxObject* wxValidatorIntegerNegative::Clone() const
505 wxValidatorIntegerNegative *clone = new wxValidatorIntegerNegative(valPtr );
506 return clone;
509 //*************************
510 // Validate
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())
518 return true;
520 if( value == wxString() )
521 return true;
523 bool res = (StrToInt( value ) <= 0);
525 return res;
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();
539 bool res = true;
541 if( kc == WXK_BACK && ind == 1 &&
542 value.Len() > 1 && value[0] == '-' )
543 res = false;
544 if( kc == WXK_DELETE && ind == 0 && value.Len() > 1 && value[0] == '-' )
545 res = false;
547 if( ! res )
549 wxBell();
550 return;
553 if( ! IsEditableKeyCode( kc ) )
555 event.Skip();
556 return;
559 if( ( kc < '0' || kc > '9') && kc != '-' )
561 wxBell();
562 return;
565 res = false;
566 if( kc == '-' && ind == 0 && ( value.Len() == 0 || value[0] != '-') )
567 res = true;
569 else if( kc != '-' &&
570 ( (ind > 0 && value[0] == '-' )
572 ( kc == '0' && value.Len() == 0 )
575 res = true;
577 if( ! res )
579 wxBell();
580 return;
583 event.Skip();
587 //****************************************************
588 //****************************************************
589 //****************************************************
591 BEGIN_EVENT_TABLE(wxValidatorFloat, wxValidator)
592 EVT_CHAR(wxValidatorFloat::OnChar)
593 END_EVENT_TABLE()
595 wxValidatorFloat::wxValidatorFloat( wxString* _valPtr )
597 valPtr = _valPtr;
600 //*********************
601 // Copy constructor
602 wxValidatorFloat::wxValidatorFloat(const wxValidatorFloat &from)
603 : wxValidator()
605 valPtr = from.valPtr;
610 //*********************
611 // Clone
612 wxObject* wxValidatorFloat::Clone() const
614 wxValidatorFloat *clone = new wxValidatorFloat(valPtr );
615 return clone;
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 ) )
633 event.Skip();
634 return;
637 if( ( kc < '0' || kc > '9') && kc != '-' && kc != '.' )
639 wxBell();
640 return;
643 bool res = true;
644 if( kc == '-')
646 if( ind != 0 )
647 res = false;
648 else if( value.Find( '-' ) != wxNOT_FOUND )
649 res = false;
652 else if( kc == '.' )
654 if( value.Find( '.' ) != wxNOT_FOUND )
655 res = false;
657 else if( ind == 0 && value[0] == '-' )
658 res = false;
661 if( ! res )
663 wxBell();
664 return;
668 bool b_ok;
669 wxMessageBox( FloatToStr( StrToFloat( value, b_ok )));
671 event.Skip();
674 //*************************
675 // Validate
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())
683 return true;
685 if( value == wxString() )
686 return true;
688 bool ok;
689 StrToInt( value, ok );
690 return ok;
695 //*****************
696 // Transfer to window
697 bool wxValidatorFloat::TransferToWindow()
699 return true;
703 //*************
704 // Receive from window
705 bool wxValidatorFloat::TransferFromWindow() {
706 wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
707 wxString value = ctrl->GetValue();
709 return true;
713 //****************************************************
714 //****************************************************
715 //****************************************************
717 BEGIN_EVENT_TABLE(wxValidatorFloatPositive, wxValidator)
718 EVT_CHAR(wxValidatorFloatPositive::OnChar)
719 END_EVENT_TABLE()
721 wxValidatorFloatPositive::wxValidatorFloatPositive( wxString* _valPtr )
722 :wxValidatorFloat( _valPtr )
726 //*********************
727 // Copy constructor
728 wxValidatorFloatPositive::wxValidatorFloatPositive(const wxValidatorFloatPositive &from)
730 valPtr = from.valPtr;
735 //*********************
736 // Clone
737 wxObject* wxValidatorFloatPositive::Clone() const
739 wxValidatorFloatPositive *clone = new wxValidatorFloatPositive(valPtr );
740 return clone;
746 //*********************
748 void wxValidatorFloatPositive::OnChar( wxKeyEvent& event )
750 int kc = event.GetKeyCode();
752 if( kc == '-' )
754 wxBell();
755 return;
758 wxValidatorFloat::OnChar( event );
761 //*************************
762 // Validate
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())
770 return true;
772 if( value == wxString() )
773 return true;
775 bool ok;
776 StrToInt( value, ok );
777 return ok;
782 //*****************
783 // Transfer to window
784 bool wxValidatorFloatPositive::TransferToWindow()
786 return true;
790 //*************
791 // Receive from window
792 bool wxValidatorFloatPositive::TransferFromWindow() {
793 wxTextCtrl *ctrl = (wxTextCtrl*) GetWindow();
794 wxString value = ctrl->GetValue();
796 return true;