//
// TextValidateField.cs: single-line text editor with validation through providers.
//
// Authors:
// José Miguel Perricone (jmperricone@hotmail.com)
//
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text.RegularExpressions;
using Terminal.Gui.TextValidateProviders;
using System;
namespace Terminal.Gui {
namespace TextValidateProviders {
///
/// TextValidateField Providers Interface.
/// All TextValidateField are created with a ITextValidateProvider.
///
public interface ITextValidateProvider {
///
/// Set that this provider uses a fixed width.
/// e.g. Masked ones are fixed.
///
bool Fixed { get; }
///
/// Set Cursor position to .
///
///
/// Return first valid position.
int Cursor (int pos);
///
/// First valid position before .
///
///
/// New cursor position if any, otherwise returns
int CursorLeft (int pos);
///
/// First valid position after .
///
/// Current position.
/// New cursor position if any, otherwise returns
int CursorRight (int pos);
///
/// Find the first valid character position.
///
/// New cursor position.
int CursorStart ();
///
/// Find the last valid character position.
///
/// New cursor position.
int CursorEnd ();
///
/// Deletes the current character in .
///
///
/// true if the character was successfully removed, otherwise false.
bool Delete (int pos);
///
/// Insert character in position .
///
///
///
/// true if the character was successfully inserted, otherwise false.
bool InsertAt (char ch, int pos);
///
/// True if the input is valid, otherwise false.
///
bool IsValid { get; }
///
/// Set the input text and get the current value.
///
string Text { get; set; }
///
/// Gets the formatted string for display.
///
string DisplayText { get; }
///
/// Method that invoke the event if it's defined.
///
/// The previous text before replaced.
/// Returns the
void OnTextChanged (TextChangedEventArgs oldValue);
///
/// Changed event, raised when the text has changed.
///
/// This event is raised when the changes.
/// The passed is a containing the old value.
///
///
event EventHandler TextChanged;
}
//////////////////////////////////////////////////////////////////////////////
// PROVIDERS
//////////////////////////////////////////////////////////////////////////////
#region NetMaskedTextProvider
///
/// .Net MaskedTextProvider Provider for TextValidateField.
///
/// Wrapper around MaskedTextProvider
/// Masking elements
///
public class NetMaskedTextProvider : ITextValidateProvider {
MaskedTextProvider _provider;
///
public event EventHandler TextChanged;
///
/// Empty Constructor
///
public NetMaskedTextProvider (string mask)
{
Mask = mask;
}
///
/// Mask property
///
public string Mask {
get {
return _provider?.Mask;
}
set {
var current = _provider != null ? _provider.ToString (false, false) : string.Empty;
_provider = new MaskedTextProvider (value == string.Empty ? "&&&&&&" : value);
if (!string.IsNullOrEmpty (current)) {
_provider.Set (current);
}
}
}
///
public string Text {
get {
return _provider.ToString ();
}
set {
_provider.Set (value);
}
}
///
public bool IsValid => _provider.MaskCompleted;
///
public bool Fixed => true;
///
public string DisplayText => _provider.ToDisplayString ();
///
public int Cursor (int pos)
{
if (pos < 0) {
return CursorStart ();
} else if (pos > _provider.Length) {
return CursorEnd ();
} else {
var p = _provider.FindEditPositionFrom (pos, false);
if (p == -1) p = _provider.FindEditPositionFrom (pos, true);
return p;
}
}
///
public int CursorStart ()
{
return
_provider.IsEditPosition (0)
? 0
: _provider.FindEditPositionFrom (0, true);
}
///
public int CursorEnd ()
{
return
_provider.IsEditPosition (_provider.Length - 1)
? _provider.Length - 1
: _provider.FindEditPositionFrom (_provider.Length, false);
}
///
public int CursorLeft (int pos)
{
var c = _provider.FindEditPositionFrom (pos - 1, false);
return c == -1 ? pos : c;
}
///
public int CursorRight (int pos)
{
var c = _provider.FindEditPositionFrom (pos + 1, true);
return c == -1 ? pos : c;
}
///
public bool Delete (int pos)
{
var oldValue = Text;
var result = _provider.Replace (' ', pos);// .RemoveAt (pos);
if (result) {
OnTextChanged (new TextChangedEventArgs (oldValue));
}
return result;
}
///
public bool InsertAt (char ch, int pos)
{
var oldValue = Text;
var result = _provider.Replace (ch, pos);
if (result) {
OnTextChanged (new TextChangedEventArgs (oldValue));
}
return result;
}
///
public void OnTextChanged (TextChangedEventArgs oldValue) => TextChanged?.Invoke (this, oldValue);
}
#endregion
#region TextRegexProvider
///
/// Regex Provider for TextValidateField.
///
public class TextRegexProvider : ITextValidateProvider {
Regex _regex;
List _text;
List _pattern;
///
public event EventHandler TextChanged;
///
/// Empty Constructor.
///
public TextRegexProvider (string pattern)
{
Pattern = pattern;
}
///
/// Regex pattern property.
///
public string Pattern {
get {
return StringExtensions.ToString (_pattern);
}
set {
_pattern = value.ToRuneList ();
CompileMask ();
SetupText ();
}
}
///
public string Text {
get {
return StringExtensions.ToString (_text);
}
set {
_text = value != string.Empty ? value.ToRuneList () : null;
SetupText ();
}
}
///
public string DisplayText => Text;
///
public bool IsValid {
get {
return Validate (_text);
}
}
///
public bool Fixed => false;
///
/// When true, validates with the regex pattern on each input, preventing the input if it's not valid.
///
public bool ValidateOnInput { get; set; } = true;
bool Validate (List text)
{
var match = _regex.Match (StringExtensions.ToString (text));
return match.Success;
}
///
public int Cursor (int pos)
{
if (pos < 0) {
return CursorStart ();
} else if (pos >= _text.Count) {
return CursorEnd ();
} else {
return pos;
}
}
///
public int CursorStart ()
{
return 0;
}
///
public int CursorEnd ()
{
return _text.Count;
}
///
public int CursorLeft (int pos)
{
if (pos > 0) {
return pos - 1;
}
return pos;
}
///
public int CursorRight (int pos)
{
if (pos < _text.Count) {
return pos + 1;
}
return pos;
}
///
public bool Delete (int pos)
{
if (_text.Count > 0 && pos < _text.Count) {
var oldValue = Text;
_text.RemoveAt (pos);
OnTextChanged (new TextChangedEventArgs (oldValue));
}
return true;
}
///
public bool InsertAt (char ch, int pos)
{
var aux = _text.ToList ();
aux.Insert (pos, (Rune)ch);
if (Validate (aux) || ValidateOnInput == false) {
var oldValue = Text;
_text.Insert (pos, (Rune)ch);
OnTextChanged (new TextChangedEventArgs (oldValue));
return true;
}
return false;
}
void SetupText ()
{
if (_text != null && IsValid) {
return;
}
_text = new List ();
}
///
/// Compiles the regex pattern for validation./>
///
private void CompileMask ()
{
_regex = new Regex (StringExtensions.ToString (_pattern), RegexOptions.Compiled);
}
///
public void OnTextChanged (TextChangedEventArgs oldValue) => TextChanged?.Invoke (this, oldValue);
}
#endregion
}
///
/// Text field that validates input through a
///
public class TextValidateField : View {
ITextValidateProvider _provider;
int _cursorPosition;
int _defaultLength = 10;
///
/// Initializes a new instance of the class using positioning.
///
public TextValidateField () : this (null) { }
///
/// Initializes a new instance of the class using positioning.
///
public TextValidateField (ITextValidateProvider provider)
{
if (provider != null) {
Provider = provider;
}
SetInitialProperties ();
}
void SetInitialProperties ()
{
Height = 1;
CanFocus = true;
// Things this view knows how to do
AddCommand (Command.LeftHome, () => { HomeKeyHandler (); return true; });
AddCommand (Command.RightEnd, () => { EndKeyHandler (); return true; });
AddCommand (Command.DeleteCharRight, () => { DeleteKeyHandler (); return true; });
AddCommand (Command.DeleteCharLeft, () => { BackspaceKeyHandler (); return true; });
AddCommand (Command.Left, () => { CursorLeft (); return true; });
AddCommand (Command.Right, () => { CursorRight (); return true; });
// Default keybindings for this view
KeyBindings.Add (KeyCode.Home, Command.LeftHome);
KeyBindings.Add (KeyCode.End, Command.RightEnd);
KeyBindings.Add (KeyCode.Delete, Command.DeleteCharRight);
KeyBindings.Add (KeyCode.Delete, Command.DeleteCharRight);
KeyBindings.Add (KeyCode.Backspace, Command.DeleteCharLeft);
KeyBindings.Add (KeyCode.CursorLeft, Command.Left);
KeyBindings.Add (KeyCode.CursorRight, Command.Right);
}
///
/// Provider
///
public ITextValidateProvider Provider {
get => _provider;
set {
_provider = value;
if (_provider.Fixed) {
this.Width = _provider.DisplayText == string.Empty ? _defaultLength : _provider.DisplayText.Length;
}
// HomeKeyHandler already call SetNeedsDisplay
HomeKeyHandler ();
}
}
///
public override bool MouseEvent (MouseEvent mouseEvent)
{
if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)) {
var c = _provider.Cursor (mouseEvent.X - GetMargins (Bounds.Width).left);
if (_provider.Fixed == false && TextAlignment == TextAlignment.Right && Text.Length > 0) {
c++;
}
_cursorPosition = c;
SetFocus ();
SetNeedsDisplay ();
return true;
}
return false;
}
///
/// Text
///
public new string Text {
get {
if (_provider == null) {
return string.Empty;
}
return _provider.Text;
}
set {
if (_provider == null) {
return;
}
_provider.Text = value;
SetNeedsDisplay ();
}
}
///
public override void PositionCursor ()
{
var (left, _) = GetMargins (Bounds.Width);
// Fixed = true, is for inputs that have fixed width, like masked ones.
// Fixed = false, is for normal input.
// When it's right-aligned and it's a normal input, the cursor behaves differently.
int curPos;
if (_provider?.Fixed == false && TextAlignment == TextAlignment.Right) {
curPos = _cursorPosition + left - 1;
Move (curPos, 0);
} else {
curPos = _cursorPosition + left;
Move (curPos, 0);
}
if (curPos < 0 || curPos >= Bounds.Width) {
Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
} else {
Application.Driver.SetCursorVisibility (CursorVisibility.Default);
}
}
///
/// Margins for text alignment.
///
/// Total width
/// Left and right margins
(int left, int right) GetMargins (int width)
{
var count = Text.Length;
var total = width - count;
switch (TextAlignment) {
case TextAlignment.Left:
return (0, total);
case TextAlignment.Centered:
return (total / 2, (total / 2) + (total % 2));
case TextAlignment.Right:
return (total, 0);
default:
return (0, total);
}
}
///
public override void OnDrawContent (Rect contentArea)
{
if (_provider == null) {
Move (0, 0);
Driver.AddStr ("Error: ITextValidateProvider not set!");
return;
}
var bgcolor = !IsValid ? new Color (Color.BrightRed) : ColorScheme.Focus.Background;
var textColor = new Attribute (ColorScheme.Focus.Foreground, bgcolor);
var (margin_left, margin_right) = GetMargins (Bounds.Width);
Move (0, 0);
// Left Margin
Driver.SetAttribute (textColor);
for (int i = 0; i < margin_left; i++) {
Driver.AddRune ((Rune)' ');
}
// Content
Driver.SetAttribute (textColor);
// Content
for (int i = 0; i < _provider.DisplayText.Length; i++) {
Driver.AddRune ((Rune)_provider.DisplayText [i]);
}
// Right Margin
Driver.SetAttribute (textColor);
for (int i = 0; i < margin_right; i++) {
Driver.AddRune ((Rune)' ');
}
}
///
/// Try to move the cursor to the left.
///
/// True if moved.
bool CursorLeft ()
{
var current = _cursorPosition;
_cursorPosition = _provider.CursorLeft (_cursorPosition);
SetNeedsDisplay ();
return current != _cursorPosition;
}
///
/// Try to move the cursor to the right.
///
/// True if moved.
bool CursorRight ()
{
var current = _cursorPosition;
_cursorPosition = _provider.CursorRight (_cursorPosition);
SetNeedsDisplay ();
return current != _cursorPosition;
}
///
/// Delete char at cursor position - 1, moving the cursor.
///
///
bool BackspaceKeyHandler ()
{
if (_provider.Fixed == false && TextAlignment == TextAlignment.Right && _cursorPosition <= 1) {
return false;
}
_cursorPosition = _provider.CursorLeft (_cursorPosition);
_provider.Delete (_cursorPosition);
SetNeedsDisplay ();
return true;
}
///
/// Deletes char at current position.
///
///
bool DeleteKeyHandler ()
{
if (_provider.Fixed == false && TextAlignment == TextAlignment.Right) {
_cursorPosition = _provider.CursorLeft (_cursorPosition);
}
_provider.Delete (_cursorPosition);
SetNeedsDisplay ();
return true;
}
///
/// Moves the cursor to first char.
///
///
bool HomeKeyHandler ()
{
_cursorPosition = _provider.CursorStart ();
SetNeedsDisplay ();
return true;
}
///
/// Moves the cursor to the last char.
///
///
bool EndKeyHandler ()
{
_cursorPosition = _provider.CursorEnd ();
SetNeedsDisplay ();
return true;
}
///
public override bool OnProcessKeyDown (Key a)
{
if (_provider == null) {
return false;
}
if (a.AsRune == default) {
return false;
}
var key = a.AsRune;
var inserted = _provider.InsertAt ((char)key.Value, _cursorPosition);
if (inserted) {
CursorRight ();
}
return false;
}
///
/// This property returns true if the input is valid.
///
public virtual bool IsValid {
get {
if (_provider == null) {
return false;
}
return _provider.IsValid;
}
}
///
public override bool OnEnter (View view)
{
Application.Driver.SetCursorVisibility (CursorVisibility.Default);
return base.OnEnter (view);
}
///
public override bool OnLeave (View view)
{
Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
return base.OnLeave (view);
}
}
}