123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- //
- // DateField.cs: text entry for date
- //
- // Author: Barry Nolte
- //
- // Licensed under the MIT license
- //
- using System;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- namespace Terminal.Gui;
- /// <summary>
- /// Simple Date editing <see cref="View"/>
- /// </summary>
- /// <remarks>
- /// The <see cref="DateField"/> <see cref="View"/> provides date editing functionality with mouse support.
- /// </remarks>
- public class DateField : TextField {
- DateTime _date;
- bool _isShort;
- int _longFieldLen = 10;
- int _shortFieldLen = 8;
- string _sepChar;
- string _longFormat;
- string _shortFormat;
- int _fieldLen => _isShort ? _shortFieldLen : _longFieldLen;
- string _format => _isShort ? _shortFormat : _longFormat;
- /// <summary>
- /// DateChanged event, raised when the <see cref="Date"/> property has changed.
- /// </summary>
- /// <remarks>
- /// This event is raised when the <see cref="Date"/> property changes.
- /// </remarks>
- /// <remarks>
- /// The passed event arguments containing the old value, new value, and format string.
- /// </remarks>
- public event EventHandler<DateTimeEventArgs<DateTime>> DateChanged;
- /// <summary>
- /// Initializes a new instance of <see cref="DateField"/> using <see cref="LayoutStyle.Absolute"/> layout.
- /// </summary>
- /// <param name="x">The x coordinate.</param>
- /// <param name="y">The y coordinate.</param>
- /// <param name="date">Initial date contents.</param>
- /// <param name="isShort">If true, shows only two digits for the year.</param>
- public DateField (int x, int y, DateTime date, bool isShort = false) : base (x, y, isShort ? 10 : 12, "") => SetInitialProperties (date, isShort);
- /// <summary>
- /// Initializes a new instance of <see cref="DateField"/> using <see cref="LayoutStyle.Computed"/> layout.
- /// </summary>
- public DateField () : this (DateTime.MinValue) { }
- /// <summary>
- /// Initializes a new instance of <see cref="DateField"/> using <see cref="LayoutStyle.Computed"/> layout.
- /// </summary>
- /// <param name="date"></param>
- public DateField (DateTime date) : base ("")
- {
- Width = _fieldLen + 2;
- SetInitialProperties (date);
- }
- void SetInitialProperties (DateTime date, bool isShort = false)
- {
- var cultureInfo = CultureInfo.CurrentCulture;
- _sepChar = cultureInfo.DateTimeFormat.DateSeparator;
- _longFormat = GetLongFormat (cultureInfo.DateTimeFormat.ShortDatePattern);
- _shortFormat = GetShortFormat (_longFormat);
- this._isShort = isShort;
- Date = date;
- CursorPosition = 1;
- TextChanged += DateField_Changed;
- // Things this view knows how to do
- AddCommand (Command.DeleteCharRight, () => {
- DeleteCharRight ();
- return true;
- });
- AddCommand (Command.DeleteCharLeft, () => {
- DeleteCharLeft (false);
- return true;
- });
- AddCommand (Command.LeftHome, () => MoveHome ());
- AddCommand (Command.Left, () => MoveLeft ());
- AddCommand (Command.RightEnd, () => MoveEnd ());
- AddCommand (Command.Right, () => MoveRight ());
- // Default keybindings for this view
- KeyBindings.Add (KeyCode.Delete, Command.DeleteCharRight);
- KeyBindings.Add (Key.D.WithCtrl, Command.DeleteCharRight);
- KeyBindings.Add (Key.Backspace, Command.DeleteCharLeft);
- KeyBindings.Add (Key.D.WithAlt, Command.DeleteCharLeft);
- KeyBindings.Add (Key.Home, Command.LeftHome);
- KeyBindings.Add (Key.A.WithCtrl, Command.LeftHome);
- KeyBindings.Add (Key.CursorLeft, Command.Left);
- KeyBindings.Add (Key.B.WithCtrl, Command.Left);
- KeyBindings.Add (Key.End, Command.RightEnd);
- KeyBindings.Add (Key.E.WithCtrl, Command.RightEnd);
- KeyBindings.Add (Key.CursorRight, Command.Right);
- KeyBindings.Add (Key.F.WithCtrl, Command.Right);
- }
- /// <inheritdoc />
- public override bool OnProcessKeyDown (Key a)
- {
- // Ignore non-numeric characters.
- if (a >= Key.D0 && a <= Key.D9) {
- if (!ReadOnly) {
- if (SetText ((Rune)a)) {
- IncCursorPosition ();
- }
- }
- return true;
- }
- return false;
- }
- void DateField_Changed (object sender, TextChangedEventArgs e)
- {
- try {
- var date = GetInvarianteDate (Text, _isShort);
- if ($" {date}" != Text) {
- Text = $" {date}";
- }
- if (_isShort) {
- date = GetInvarianteDate (Text, false);
- }
- if (!DateTime.TryParseExact (date, GetInvarianteFormat (), CultureInfo.CurrentCulture, DateTimeStyles.None, out var result)) {
- Text = e.OldValue;
- }
- } catch (Exception) {
- Text = e.OldValue;
- }
- }
- string GetInvarianteFormat () => $"MM{_sepChar}dd{_sepChar}yyyy";
- string GetLongFormat (string lf)
- {
- string [] frm = lf.Split (_sepChar);
- for (int i = 0; i < frm.Length; i++) {
- if (frm [i].Contains ("M") && frm [i].GetRuneCount () < 2) {
- lf = lf.Replace ("M", "MM");
- }
- if (frm [i].Contains ("d") && frm [i].GetRuneCount () < 2) {
- lf = lf.Replace ("d", "dd");
- }
- if (frm [i].Contains ("y") && frm [i].GetRuneCount () < 4) {
- lf = lf.Replace ("yy", "yyyy");
- }
- }
- return $" {lf}";
- }
- string GetShortFormat (string lf) => lf.Replace ("yyyy", "yy");
- /// <summary>
- /// Gets or sets the date of the <see cref="DateField"/>.
- /// </summary>
- /// <remarks>
- /// </remarks>
- public DateTime Date {
- get => _date;
- set {
- if (ReadOnly) {
- return;
- }
- var oldData = _date;
- _date = value;
- Text = value.ToString (_format);
- var args = new DateTimeEventArgs<DateTime> (oldData, value, _format);
- if (oldData != value) {
- OnDateChanged (args);
- }
- }
- }
- /// <summary>
- /// Get or set the date format for the widget.
- /// </summary>
- public bool IsShortFormat {
- get => _isShort;
- set {
- _isShort = value;
- if (_isShort) {
- Width = 10;
- } else {
- Width = 12;
- }
- bool ro = ReadOnly;
- if (ro) {
- ReadOnly = false;
- }
- SetText (Text);
- ReadOnly = ro;
- SetNeedsDisplay ();
- }
- }
- /// <inheritdoc/>
- public override int CursorPosition {
- get => base.CursorPosition;
- set => base.CursorPosition = Math.Max (Math.Min (value, _fieldLen), 1);
- }
- bool SetText (Rune key)
- {
- if (CursorPosition > _fieldLen) {
- CursorPosition = _fieldLen;
- return false;
- } else if (CursorPosition < 1) {
- CursorPosition = 1;
- return false;
- }
- var text = Text.EnumerateRunes ().ToList ();
- 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 (StringExtensions.ToString (newText));
- }
- bool SetText (string text)
- {
- if (string.IsNullOrEmpty (text)) {
- return false;
- }
- text = NormalizeFormat (text);
- string [] vals = text.Split (_sepChar);
- string [] frm = _format.Split (_sepChar);
- int year;
- int month;
- int day;
- int idx = GetFormatIndex (frm, "y");
- if (Int32.Parse (vals [idx]) < 1) {
- year = 1;
- vals [idx] = "1";
- } else {
- year = Int32.Parse (vals [idx]);
- }
- idx = GetFormatIndex (frm, "M");
- if (Int32.Parse (vals [idx]) < 1) {
- month = 1;
- vals [idx] = "1";
- } else if (Int32.Parse (vals [idx]) > 12) {
- month = 12;
- vals [idx] = "12";
- } else {
- month = Int32.Parse (vals [idx]);
- }
- idx = GetFormatIndex (frm, "d");
- if (Int32.Parse (vals [idx]) < 1) {
- day = 1;
- vals [idx] = "1";
- } else if (Int32.Parse (vals [idx]) > 31) {
- day = DateTime.DaysInMonth (year, month);
- vals [idx] = day.ToString ();
- } else {
- day = Int32.Parse (vals [idx]);
- }
- string d = GetDate (month, day, year, frm);
- if (!DateTime.TryParseExact (d, _format, CultureInfo.CurrentCulture, DateTimeStyles.None, out var result)) {
- return false;
- }
- Date = result;
- return true;
- }
- string NormalizeFormat (string text, string fmt = null, string sepChar = null)
- {
- if (string.IsNullOrEmpty (fmt)) {
- fmt = _format;
- }
- if (string.IsNullOrEmpty (sepChar)) {
- sepChar = _sepChar;
- }
- if (fmt.Length != text.Length) {
- return text;
- }
- var fmtText = text.ToCharArray ();
- for (int i = 0; i < text.Length; i++) {
- var c = fmt [i];
- if (c.ToString () == sepChar && text [i].ToString () != sepChar) {
- fmtText [i] = c;
- }
- }
- return new string (fmtText);
- }
- string GetDate (int month, int day, int year, string [] 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 == 4) {
- date += $"{year.ToString ().Substring (2, 2)}";
- } else if (_isShort) {
- date += $"{year,2:00}";
- } else {
- date += $"{year,4:0000}";
- }
- }
- if (i < 2) {
- date += $"{_sepChar}";
- }
- }
- return date;
- }
- string GetInvarianteDate (string text, bool isShort)
- {
- string [] vals = text.Split (_sepChar);
- string [] frm = (isShort ? $"MM{_sepChar}dd{_sepChar}yy" : GetInvarianteFormat ()).Split (_sepChar);
- string [] date = { null, null, null };
- for (int i = 0; i < frm.Length; i++) {
- if (frm [i].Contains ("M")) {
- date [0] = vals [i].Trim ();
- } else if (frm [i].Contains ("d")) {
- date [1] = vals [i].Trim ();
- } else {
- string yearString;
- if (isShort && vals [i].Length > 2) {
- yearString = vals [i].Substring (0, 2);
- } else if (!isShort && vals [i].Length > 4) {
- yearString = vals [i].Substring (0, 4);
- } else {
- yearString = vals [i].Trim ();
- }
- var year = int.Parse (yearString);
- if (isShort && year.ToString ().Length == 4) {
- date [2] = year.ToString ().Substring (2, 2);
- } else if (isShort) {
- date [2] = year.ToString ();
- } else {
- date [2] = $"{year,4:0000}";
- }
- }
- }
- return $"{date [0]}{_sepChar}{date [1]}{_sepChar}{date [2]}";
- }
- int GetFormatIndex (string [] 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) {
- CursorPosition = _fieldLen;
- return;
- }
- if (Text [++CursorPosition] == _sepChar.ToCharArray () [0]) {
- CursorPosition++;
- }
- }
- void DecCursorPosition ()
- {
- if (CursorPosition <= 1) {
- CursorPosition = 1;
- return;
- }
- if (Text [--CursorPosition] == _sepChar.ToCharArray () [0]) {
- CursorPosition--;
- }
- }
- void AdjCursorPosition ()
- {
- if (Text [CursorPosition] == _sepChar.ToCharArray () [0]) {
- CursorPosition++;
- }
- }
- bool MoveRight ()
- {
- ClearAllSelection ();
- IncCursorPosition ();
- return true;
- }
- new bool MoveEnd ()
- {
- ClearAllSelection ();
- CursorPosition = _fieldLen;
- return true;
- }
- bool MoveLeft ()
- {
- ClearAllSelection ();
- DecCursorPosition ();
- return true;
- }
- bool MoveHome ()
- {
- // Home, C-A
- ClearAllSelection ();
- CursorPosition = 1;
- return true;
- }
- /// <inheritdoc/>
- public override void DeleteCharLeft (bool useOldCursorPos = true)
- {
- if (ReadOnly) {
- return;
- }
- ClearAllSelection ();
- SetText ((Rune)'0');
- DecCursorPosition ();
- return;
- }
- /// <inheritdoc/>
- public override void DeleteCharRight ()
- {
- if (ReadOnly) {
- return;
- }
- ClearAllSelection ();
- SetText ((Rune)'0');
- return;
- }
- /// <inheritdoc/>
- public override bool MouseEvent (MouseEvent ev)
- {
- if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked)) {
- return false;
- }
- if (!HasFocus) {
- SetFocus ();
- }
- int point = ev.X;
- if (point > _fieldLen) {
- point = _fieldLen;
- }
- if (point < 1) {
- point = 1;
- }
- CursorPosition = point;
- AdjCursorPosition ();
- return true;
- }
- /// <summary>
- /// Event firing method for the <see cref="DateChanged"/> event.
- /// </summary>
- /// <param name="args">Event arguments</param>
- public virtual void OnDateChanged (DateTimeEventArgs<DateTime> args) => DateChanged?.Invoke (this, args);
- }
|