//
// DateField.cs: text entry for date
//
// Author: Barry Nolte
//
// Licensed under the MIT license
//
using System;
using System.Globalization;
using System.Linq;
using NStack;
namespace Terminal.Gui {
///
/// Simple Date editing
///
///
/// The provides date editing functionality with mouse support.
///
public class DateField : TextField {
DateTime date;
bool isShort;
int longFieldLen = 10;
int shortFieldLen = 8;
string sepChar;
string longFormat;
string shortFormat;
int FieldLen { get { return isShort ? shortFieldLen : longFieldLen; } }
string Format { get { return isShort ? shortFormat : longFormat; } }
///
/// DateChanged event, raised when the property has changed.
///
///
/// This event is raised when the property changes.
///
///
/// The passed event arguments containing the old value, new value, and format string.
///
public event Action> DateChanged;
///
/// Initializes a new instance of using layout.
///
/// The x coordinate.
/// The y coordinate.
/// Initial date contents.
/// If true, shows only two digits for the year.
public DateField (int x, int y, DateTime date, bool isShort = false) : base (x, y, isShort ? 10 : 12, "")
{
this.isShort = isShort;
Initialize (date);
}
///
/// Initializes a new instance of using layout.
///
public DateField () : this (DateTime.MinValue) { }
///
/// Initializes a new instance of using layout.
///
///
public DateField (DateTime date) : base ("")
{
this.isShort = true;
Width = FieldLen + 2;
Initialize (date);
}
void Initialize (DateTime date)
{
CultureInfo cultureInfo = CultureInfo.CurrentCulture;
sepChar = cultureInfo.DateTimeFormat.DateSeparator;
longFormat = GetLongFormat (cultureInfo.DateTimeFormat.ShortDatePattern);
shortFormat = GetShortFormat (longFormat);
CursorPosition = 1;
Date = date;
TextChanged += DateField_Changed;
}
void DateField_Changed (ustring e)
{
try {
if (!DateTime.TryParseExact (GetDate (Text).ToString (), GetInvarianteFormat (), CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result))
Text = e;
} catch (Exception) {
Text = e;
}
}
string GetInvarianteFormat ()
{
return $"MM{sepChar}dd{sepChar}yyyy";
}
string GetLongFormat (string lf)
{
ustring [] frm = ustring.Make (lf).Split (ustring.Make (sepChar));
for (int i = 0; i < frm.Length; i++) {
if (frm [i].Contains ("M") && frm [i].RuneCount < 2)
lf = lf.Replace ("M", "MM");
if (frm [i].Contains ("d") && frm [i].RuneCount < 2)
lf = lf.Replace ("d", "dd");
if (frm [i].Contains ("y") && frm [i].RuneCount < 4)
lf = lf.Replace ("yy", "yyyy");
}
return $" {lf}";
}
string GetShortFormat (string lf)
{
return lf.Replace ("yyyy", "yy");
}
///
/// Gets or sets the date of the .
///
///
///
public DateTime Date {
get {
return date;
}
set {
if (ReadOnly)
return;
var oldData = date;
date = value;
this.Text = value.ToString (Format);
var args = new DateTimeEventArgs (oldData, value, Format);
if (oldData != value) {
OnDateChanged (args);
}
}
}
///
/// Get or set the date format for the widget.
///
public bool IsShortFormat {
get => isShort;
set {
isShort = value;
if (isShort)
Width = 10;
else
Width = 12;
var ro = ReadOnly;
if (ro)
ReadOnly = false;
SetText (Text);
ReadOnly = ro;
SetNeedsDisplay ();
}
}
bool SetText (Rune key)
{
var text = TextModel.ToRunes (Text);
var newText = text.GetRange (0, CursorPosition);
newText.Add (key);
if (CursorPosition < FieldLen)
newText = newText.Concat (text.GetRange (CursorPosition + 1, text.Count - (CursorPosition + 1))).ToList ();
return SetText (ustring.Make (newText));
}
bool SetText (ustring text)
{
if (text.IsEmpty) {
return false;
}
ustring [] vals = text.Split (ustring.Make (sepChar));
ustring [] frm = ustring.Make (Format).Split (ustring.Make (sepChar));
bool isValidDate = true;
int idx = GetFormatIndex (frm, "y");
int year = Int32.Parse (vals [idx].ToString ());
int month;
int day;
idx = GetFormatIndex (frm, "M");
if (Int32.Parse (vals [idx].ToString ()) < 1) {
isValidDate = false;
month = 1;
vals [idx] = "1";
} else if (Int32.Parse (vals [idx].ToString ()) > 12) {
isValidDate = false;
month = 12;
vals [idx] = "12";
} else
month = Int32.Parse (vals [idx].ToString ());
idx = GetFormatIndex (frm, "d");
if (Int32.Parse (vals [idx].ToString ()) < 1) {
isValidDate = false;
day = 1;
vals [idx] = "1";
} else if (Int32.Parse (vals [idx].ToString ()) > 31) {
isValidDate = false;
day = DateTime.DaysInMonth (year, month);
vals [idx] = day.ToString ();
} else
day = Int32.Parse (vals [idx].ToString ());
string d = GetDate (month, day, year, frm);
if (!DateTime.TryParseExact (d, Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||
!isValidDate)
return false;
Date = result;
return true;
}
string GetDate (int month, int day, int year, ustring [] fm)
{
string date = " ";
for (int i = 0; i < fm.Length; i++) {
if (fm [i].Contains ("M")) {
date += $"{month,2:00}";
} else if (fm [i].Contains ("d")) {
date += $"{day,2:00}";
} else {
if (!isShort && year.ToString ().Length == 2) {
var y = DateTime.Now.Year.ToString ();
date += y.Substring (0, 2) + year.ToString ();
} else if (isShort && year.ToString ().Length == 4) {
date += $"{year.ToString ().Substring (2, 2)}";
} else {
date += $"{year,2:00}";
}
}
if (i < 2)
date += $"{sepChar}";
}
return date;
}
ustring GetDate (ustring text)
{
ustring [] vals = text.Split (ustring.Make (sepChar));
ustring [] frm = ustring.Make (Format).Split (ustring.Make (sepChar));
ustring [] date = { null, null, null };
for (int i = 0; i < frm.Length; i++) {
if (frm [i].Contains ("M")) {
date [0] = vals [i].TrimSpace ();
} else if (frm [i].Contains ("d")) {
date [1] = vals [i].TrimSpace ();
} else {
var year = vals [i].TrimSpace ();
if (year.RuneCount == 2) {
var y = DateTime.Now.Year.ToString ();
date [2] = y.Substring (0, 2) + year.ToString ();
} else {
date [2] = vals [i].TrimSpace ();
}
}
}
return date [0] + ustring.Make (sepChar) + date [1] + ustring.Make (sepChar) + date [2];
}
int GetFormatIndex (ustring [] fm, string t)
{
int idx = -1;
for (int i = 0; i < fm.Length; i++) {
if (fm [i].Contains (t)) {
idx = i;
break;
}
}
return idx;
}
void IncCursorPosition ()
{
if (CursorPosition == FieldLen)
return;
if (Text [++CursorPosition] == sepChar.ToCharArray () [0])
CursorPosition++;
}
void DecCursorPosition ()
{
if (CursorPosition == 1)
return;
if (Text [--CursorPosition] == sepChar.ToCharArray () [0])
CursorPosition--;
}
void AdjCursorPosition ()
{
if (Text [CursorPosition] == sepChar.ToCharArray () [0])
CursorPosition++;
}
///
public override bool ProcessKey (KeyEvent kb)
{
switch (kb.Key) {
case Key.DeleteChar:
case Key.D | Key.CtrlMask:
if (ReadOnly)
return true;
SetText ('0');
break;
case Key.Delete:
case Key.Backspace:
if (ReadOnly)
return true;
SetText ('0');
DecCursorPosition ();
break;
// Home, C-A
case Key.Home:
case Key.A | Key.CtrlMask:
CursorPosition = 1;
break;
case Key.CursorLeft:
case Key.B | Key.CtrlMask:
DecCursorPosition ();
break;
case Key.End:
case Key.E | Key.CtrlMask: // End
CursorPosition = FieldLen;
break;
case Key.CursorRight:
case Key.F | Key.CtrlMask:
IncCursorPosition ();
break;
default:
// Ignore non-numeric characters.
if (kb.Key < (Key)((int)'0') || kb.Key > (Key)((int)'9'))
return false;
if (ReadOnly)
return true;
if (SetText (TextModel.ToRunes (ustring.Make ((uint)kb.Key)).First ()))
IncCursorPosition ();
return true;
}
return true;
}
///
public override bool MouseEvent (MouseEvent ev)
{
if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
return false;
if (!HasFocus)
SetFocus ();
var point = ev.X;
if (point > FieldLen)
point = FieldLen;
if (point < 1)
point = 1;
CursorPosition = point;
AdjCursorPosition ();
return true;
}
///
/// Event firing method for the event.
///
/// Event arguments
public virtual void OnDateChanged (DateTimeEventArgs args)
{
DateChanged?.Invoke (args);
}
}
///
/// Defines the event arguments for and events.
///
public class DateTimeEventArgs : EventArgs {
///
/// The old or value.
///
public T OldValue {get;}
///
/// The new or value.
///
public T NewValue { get; }
///
/// The or format.
///
public string Format { get; }
///
/// Initializes a new instance of
///
/// The old or value.
/// The new or value.
/// The or format string.
public DateTimeEventArgs (T oldValue, T newValue, string format)
{
OldValue = oldValue;
NewValue = newValue;
Format = format;
}
}
}