diff --git a/Terminal.Gui/Views/TextValidateField.cs b/Terminal.Gui/Views/TextValidateField.cs index 942683a5a..530287303 100644 --- a/Terminal.Gui/Views/TextValidateField.cs +++ b/Terminal.Gui/Views/TextValidateField.cs @@ -350,7 +350,7 @@ namespace Terminal.Gui { /// Text field that validates input through a /// /// - public class TextValidateField : View where T : ITextValidateProvider { + public class TextValidateField : View, ITextValidateProvider where T : class { ITextValidateProvider provider; int cursorPosition = 0; @@ -358,7 +358,7 @@ namespace Terminal.Gui { /// /// Initializes a new instance of the class using positioning. /// - public TextValidateField () : this (ustring.Empty) + public TextValidateField () { } @@ -373,7 +373,7 @@ namespace Terminal.Gui { /// /// /// Initial Value - public TextValidateField (ustring mask, ustring text) : base () + public TextValidateField (ustring mask, ustring text) { provider = Activator.CreateInstance (typeof (T)) as ITextValidateProvider; @@ -408,9 +408,16 @@ namespace Terminal.Gui { /// public new ustring Text { get { + if (provider == null) { + return ustring.Empty; + } + return provider.Text; } set { + if (provider == null) { + return; + } provider.Text = value; SetNeedsDisplay (); @@ -472,6 +479,12 @@ namespace Terminal.Gui { /// public override void Redraw (Rect bounds) { + if (provider == null) { + Move (0, 0); + Driver.AddStr ("Error: ITextValidateProvider not set!"); + return; + } + var bgcolor = !IsValid ? Color.BrightRed : ColorScheme.Focus.Background; var textColor = new Attribute (ColorScheme.Focus.Foreground, bgcolor); @@ -571,6 +584,10 @@ namespace Terminal.Gui { /// public override bool ProcessKey (KeyEvent kb) { + if (provider == null) { + return true; + } + switch (kb.Key) { case Key.Home: HomeKeyHandler (); break; case Key.End: EndKeyHandler (); break; @@ -598,13 +615,88 @@ namespace Terminal.Gui { return true; } + /// + /// Set Cursor position to . + /// + /// + /// Return first valid position. + public int Cursor (int pos) + { + return provider.Cursor (pos); + } + + /// + /// First valid position before . + /// + /// + /// New cursor position if any, otherwise returns + public int CursorLeft (int pos) + { + return provider.CursorLeft (pos); + } + + /// + /// First valid position after . + /// + /// Current position. + /// New cursor position if any, otherwise returns + public int CursorRight (int pos) + { + return provider.CursorRight (pos); + } + + /// + /// Find the first valid character position. + /// + /// New cursor position. + public int CursorStart () + { + return provider.CursorStart (); + } + + /// + /// Find the last valid character position. + /// + /// New cursor position. + public int CursorEnd () + { + return provider.CursorEnd (); + } + + /// + /// Deletes the current character in . + /// + /// + /// true if the character was successfully removed, otherwise false. + public bool Delete (int pos) + { + return provider.Delete (pos); + } + + /// + /// Insert character in position . + /// + /// + /// + /// true if the character was successfully inserted, otherwise false. + public bool InsertAt (char ch, int pos) + { + return provider.InsertAt (ch, pos); + } + /// /// This property returns true if the input is valid. /// public virtual bool IsValid { get { + if (provider == null) { + return false; + } + return provider.IsValid; } } + + public bool Fixed => throw new NotImplementedException (); } } diff --git a/UnitTests/TextValidateFieldTests.cs b/UnitTests/TextValidateFieldTests.cs index 536847495..64908611f 100644 --- a/UnitTests/TextValidateFieldTests.cs +++ b/UnitTests/TextValidateFieldTests.cs @@ -392,17 +392,15 @@ namespace Terminal.Gui.Views { } [Fact] - public void Empty_Mask_Validates_Everything () + public void Empty_Mask_Does_Not_Validate () { - // Maybe it's not the right behaviour. - var field = new TextValidateField () { Width = 20 }; - field.ProcessKey (new KeyEvent (Key.D1, new KeyModifiers { })); - Assert.Equal ("1", field.Text); - Assert.True (field.IsValid); + field.ProcessKey (new KeyEvent (Key.D1, new KeyModifiers ())); + Assert.Equal ("", field.Text); + Assert.False (field.IsValid); } [Fact]