|
@@ -19,15 +19,14 @@ namespace Terminal.Gui {
|
|
/// </remarks>
|
|
/// </remarks>
|
|
public class DateField : TextField {
|
|
public class DateField : TextField {
|
|
bool isShort;
|
|
bool isShort;
|
|
-
|
|
|
|
int longFieldLen = 10;
|
|
int longFieldLen = 10;
|
|
int shortFieldLen = 8;
|
|
int shortFieldLen = 8;
|
|
- int FieldLen { get { return isShort ? shortFieldLen : longFieldLen; } }
|
|
|
|
string sepChar;
|
|
string sepChar;
|
|
string longFormat;
|
|
string longFormat;
|
|
string shortFormat;
|
|
string shortFormat;
|
|
- string Format { get { return isShort ? shortFormat : longFormat; } }
|
|
|
|
|
|
|
|
|
|
+ int FieldLen { get { return isShort ? shortFieldLen : longFieldLen; } }
|
|
|
|
+ string Format { get { return isShort ? shortFormat : longFormat; } }
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Initializes a new instance of <see cref="DateField"/> at an absolute position and fixed size.
|
|
/// Initializes a new instance of <see cref="DateField"/> at an absolute position and fixed size.
|
|
@@ -37,20 +36,32 @@ namespace Terminal.Gui {
|
|
/// <param name="date">Initial date contents.</param>
|
|
/// <param name="date">Initial date contents.</param>
|
|
/// <param name="isShort">If true, shows only two digits for the year.</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, "")
|
|
public DateField (int x, int y, DateTime date, bool isShort = false) : base(x, y, isShort ? 10 : 12, "")
|
|
|
|
+ {
|
|
|
|
+ this.isShort = isShort;
|
|
|
|
+ Initialize (date);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public DateField (DateTime date) : base ("")
|
|
|
|
+ {
|
|
|
|
+ this.isShort = true;
|
|
|
|
+ Width = FieldLen + 2;
|
|
|
|
+ Initialize (date);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ void Initialize (DateTime date)
|
|
{
|
|
{
|
|
CultureInfo cultureInfo = CultureInfo.CurrentCulture;
|
|
CultureInfo cultureInfo = CultureInfo.CurrentCulture;
|
|
sepChar = cultureInfo.DateTimeFormat.DateSeparator;
|
|
sepChar = cultureInfo.DateTimeFormat.DateSeparator;
|
|
longFormat = GetLongFormat (cultureInfo.DateTimeFormat.ShortDatePattern);
|
|
longFormat = GetLongFormat (cultureInfo.DateTimeFormat.ShortDatePattern);
|
|
- shortFormat = GetShortFormat(longFormat);
|
|
|
|
- this.isShort = isShort;
|
|
|
|
|
|
+ shortFormat = GetShortFormat (longFormat);
|
|
CursorPosition = 1;
|
|
CursorPosition = 1;
|
|
Date = date;
|
|
Date = date;
|
|
Changed += DateField_Changed;
|
|
Changed += DateField_Changed;
|
|
}
|
|
}
|
|
|
|
|
|
- void DateField_Changed(object sender, ustring e)
|
|
|
|
|
|
+ void DateField_Changed (object sender, ustring e)
|
|
{
|
|
{
|
|
- if (!DateTime.TryParseExact(Text.ToString(), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result))
|
|
|
|
|
|
+ if (!DateTime.TryParseExact (Text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result))
|
|
Text = e;
|
|
Text = e;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -70,7 +81,7 @@ namespace Terminal.Gui {
|
|
|
|
|
|
string GetShortFormat (string lf)
|
|
string GetShortFormat (string lf)
|
|
{
|
|
{
|
|
- return lf.Replace("yyyy", "yy");
|
|
|
|
|
|
+ return lf.Replace ("yyyy", "yy");
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -80,85 +91,111 @@ namespace Terminal.Gui {
|
|
/// </remarks>
|
|
/// </remarks>
|
|
public DateTime Date {
|
|
public DateTime Date {
|
|
get {
|
|
get {
|
|
- if (!DateTime.TryParseExact(Text.ToString(), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) return new DateTime();
|
|
|
|
|
|
+ if (!DateTime.TryParseExact (Text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) return new DateTime ();
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
set {
|
|
set {
|
|
- this.Text = value.ToString(Format);
|
|
|
|
|
|
+ this.Text = value.ToString (Format);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- bool SetText(Rune key)
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Get or set the data format for the widget.
|
|
|
|
+ /// </summary>
|
|
|
|
+ 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);
|
|
|
|
|
|
+ var text = TextModel.ToRunes (Text);
|
|
|
|
+ var newText = text.GetRange (0, CursorPosition);
|
|
|
|
+ newText.Add (key);
|
|
if (CursorPosition < FieldLen)
|
|
if (CursorPosition < FieldLen)
|
|
- newText = newText.Concat(text.GetRange(CursorPosition + 1, text.Count - (CursorPosition + 1))).ToList();
|
|
|
|
- return SetText(ustring.Make(newText));
|
|
|
|
|
|
+ newText = newText.Concat (text.GetRange (CursorPosition + 1, text.Count - (CursorPosition + 1))).ToList ();
|
|
|
|
+ return SetText (ustring.Make (newText));
|
|
}
|
|
}
|
|
|
|
|
|
- bool SetText(ustring text)
|
|
|
|
|
|
+ bool SetText (ustring text)
|
|
{
|
|
{
|
|
- ustring[] vals = text.Split(ustring.Make(sepChar));
|
|
|
|
- ustring[] frm = ustring.Make(Format).Split(ustring.Make(sepChar));
|
|
|
|
|
|
+ ustring [] vals = text.Split (ustring.Make (sepChar));
|
|
|
|
+ ustring [] frm = ustring.Make (Format).Split (ustring.Make (sepChar));
|
|
bool isValidDate = true;
|
|
bool isValidDate = true;
|
|
- int idx = GetFormatIndex(frm, "y");
|
|
|
|
- int year = Int32.Parse(vals[idx].ToString());
|
|
|
|
|
|
+ int idx = GetFormatIndex (frm, "y");
|
|
|
|
+ int year = Int32.Parse (vals [idx].ToString ());
|
|
int month;
|
|
int month;
|
|
int day;
|
|
int day;
|
|
- idx = GetFormatIndex(frm, "M");
|
|
|
|
- if (Int32.Parse(vals[idx].ToString()) < 1) {
|
|
|
|
|
|
+ idx = GetFormatIndex (frm, "M");
|
|
|
|
+ if (Int32.Parse (vals [idx].ToString ()) < 1) {
|
|
isValidDate = false;
|
|
isValidDate = false;
|
|
month = 1;
|
|
month = 1;
|
|
- vals[idx] = "1";
|
|
|
|
- } else if (Int32.Parse(vals[idx].ToString()) > 12) {
|
|
|
|
|
|
+ vals [idx] = "1";
|
|
|
|
+ } else if (Int32.Parse (vals [idx].ToString ()) > 12) {
|
|
isValidDate = false;
|
|
isValidDate = false;
|
|
month = 12;
|
|
month = 12;
|
|
- vals[idx] = "12";
|
|
|
|
|
|
+ vals [idx] = "12";
|
|
} else
|
|
} else
|
|
- month = Int32.Parse(vals[idx].ToString());
|
|
|
|
- idx = GetFormatIndex(frm, "d");
|
|
|
|
- if (Int32.Parse(vals[idx].ToString()) < 1) {
|
|
|
|
|
|
+ month = Int32.Parse (vals [idx].ToString ());
|
|
|
|
+ idx = GetFormatIndex (frm, "d");
|
|
|
|
+ if (Int32.Parse (vals [idx].ToString ()) < 1) {
|
|
isValidDate = false;
|
|
isValidDate = false;
|
|
day = 1;
|
|
day = 1;
|
|
- vals[idx] = "1";
|
|
|
|
- } else if (Int32.Parse(vals[idx].ToString()) > 31) {
|
|
|
|
|
|
+ vals [idx] = "1";
|
|
|
|
+ } else if (Int32.Parse (vals [idx].ToString ()) > 31) {
|
|
isValidDate = false;
|
|
isValidDate = false;
|
|
- day = DateTime.DaysInMonth(year, month);
|
|
|
|
- vals[idx] = day.ToString();
|
|
|
|
|
|
+ day = DateTime.DaysInMonth (year, month);
|
|
|
|
+ vals [idx] = day.ToString ();
|
|
} else
|
|
} else
|
|
- day = Int32.Parse(vals[idx].ToString());
|
|
|
|
- string date = GetData(month, day, year, frm);
|
|
|
|
|
|
+ day = Int32.Parse (vals [idx].ToString ());
|
|
|
|
+ string date = GetDate (month, day, year, frm);
|
|
Text = date;
|
|
Text = date;
|
|
|
|
|
|
- if (!DateTime.TryParseExact(date, Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||
|
|
|
|
|
|
+ if (!DateTime.TryParseExact (date, Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||
|
|
!isValidDate)
|
|
!isValidDate)
|
|
return false;
|
|
return false;
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- string GetData(int month, int day, int year, ustring[] fm)
|
|
|
|
|
|
+ string GetDate (int month, int day, int year, ustring [] fm)
|
|
{
|
|
{
|
|
- string data = " ";
|
|
|
|
|
|
+ string date = " ";
|
|
for (int i = 0; i < fm.Length; i++) {
|
|
for (int i = 0; i < fm.Length; i++) {
|
|
- if (fm[i].Contains("M"))
|
|
|
|
- data += $"{month,2:00}";
|
|
|
|
- else if (fm[i].Contains("d"))
|
|
|
|
- data += $"{day,2:00}";
|
|
|
|
- else
|
|
|
|
- data += isShort ? $"{year,2:00}" : $"{year,4:0000}";
|
|
|
|
|
|
+ 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 {
|
|
|
|
+ date += $"{year,2:00}";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
if (i < 2)
|
|
if (i < 2)
|
|
- data += $"{sepChar}";
|
|
|
|
|
|
+ date += $"{sepChar}";
|
|
}
|
|
}
|
|
- return data;
|
|
|
|
|
|
+ return date;
|
|
}
|
|
}
|
|
|
|
|
|
- int GetFormatIndex(ustring[] fm, string t)
|
|
|
|
|
|
+ int GetFormatIndex (ustring [] fm, string t)
|
|
{
|
|
{
|
|
int idx = -1;
|
|
int idx = -1;
|
|
for (int i = 0; i < fm.Length; i++) {
|
|
for (int i = 0; i < fm.Length; i++) {
|
|
- if (fm[i].Contains(t)) {
|
|
|
|
|
|
+ if (fm [i].Contains (t)) {
|
|
idx = i;
|
|
idx = i;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -166,25 +203,25 @@ namespace Terminal.Gui {
|
|
return idx;
|
|
return idx;
|
|
}
|
|
}
|
|
|
|
|
|
- void IncCursorPosition()
|
|
|
|
|
|
+ void IncCursorPosition ()
|
|
{
|
|
{
|
|
if (CursorPosition == FieldLen)
|
|
if (CursorPosition == FieldLen)
|
|
return;
|
|
return;
|
|
- if (Text[++CursorPosition] == sepChar.ToCharArray()[0])
|
|
|
|
|
|
+ if (Text [++CursorPosition] == sepChar.ToCharArray () [0])
|
|
CursorPosition++;
|
|
CursorPosition++;
|
|
}
|
|
}
|
|
|
|
|
|
- void DecCursorPosition()
|
|
|
|
|
|
+ void DecCursorPosition ()
|
|
{
|
|
{
|
|
if (CursorPosition == 1)
|
|
if (CursorPosition == 1)
|
|
return;
|
|
return;
|
|
- if (Text[--CursorPosition] == sepChar.ToCharArray()[0])
|
|
|
|
|
|
+ if (Text [--CursorPosition] == sepChar.ToCharArray () [0])
|
|
CursorPosition--;
|
|
CursorPosition--;
|
|
}
|
|
}
|
|
|
|
|
|
- void AdjCursorPosition()
|
|
|
|
|
|
+ void AdjCursorPosition ()
|
|
{
|
|
{
|
|
- if (Text[CursorPosition] == sepChar.ToCharArray()[0])
|
|
|
|
|
|
+ if (Text [CursorPosition] == sepChar.ToCharArray () [0])
|
|
CursorPosition++;
|
|
CursorPosition++;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -194,13 +231,13 @@ namespace Terminal.Gui {
|
|
switch (kb.Key) {
|
|
switch (kb.Key) {
|
|
case Key.DeleteChar:
|
|
case Key.DeleteChar:
|
|
case Key.ControlD:
|
|
case Key.ControlD:
|
|
- SetText('0');
|
|
|
|
|
|
+ SetText ('0');
|
|
break;
|
|
break;
|
|
|
|
|
|
case Key.Delete:
|
|
case Key.Delete:
|
|
case Key.Backspace:
|
|
case Key.Backspace:
|
|
- SetText('0');
|
|
|
|
- DecCursorPosition();
|
|
|
|
|
|
+ SetText ('0');
|
|
|
|
+ DecCursorPosition ();
|
|
break;
|
|
break;
|
|
|
|
|
|
// Home, C-A
|
|
// Home, C-A
|
|
@@ -211,7 +248,7 @@ namespace Terminal.Gui {
|
|
|
|
|
|
case Key.CursorLeft:
|
|
case Key.CursorLeft:
|
|
case Key.ControlB:
|
|
case Key.ControlB:
|
|
- DecCursorPosition();
|
|
|
|
|
|
+ DecCursorPosition ();
|
|
break;
|
|
break;
|
|
|
|
|
|
case Key.End:
|
|
case Key.End:
|
|
@@ -221,15 +258,15 @@ namespace Terminal.Gui {
|
|
|
|
|
|
case Key.CursorRight:
|
|
case Key.CursorRight:
|
|
case Key.ControlF:
|
|
case Key.ControlF:
|
|
- IncCursorPosition();
|
|
|
|
|
|
+ IncCursorPosition ();
|
|
break;
|
|
break;
|
|
|
|
|
|
default:
|
|
default:
|
|
// Ignore non-numeric characters.
|
|
// Ignore non-numeric characters.
|
|
if (kb.Key < (Key)((int)'0') || kb.Key > (Key)((int)'9'))
|
|
if (kb.Key < (Key)((int)'0') || kb.Key > (Key)((int)'9'))
|
|
return false;
|
|
return false;
|
|
- if (SetText(TextModel.ToRunes(ustring.Make((uint)kb.Key)).First()))
|
|
|
|
- IncCursorPosition();
|
|
|
|
|
|
+ if (SetText (TextModel.ToRunes (ustring.Make ((uint)kb.Key)).First ()))
|
|
|
|
+ IncCursorPosition ();
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
@@ -238,10 +275,10 @@ namespace Terminal.Gui {
|
|
///<inheritdoc cref="MouseEvent(Gui.MouseEvent)"/>
|
|
///<inheritdoc cref="MouseEvent(Gui.MouseEvent)"/>
|
|
public override bool MouseEvent(MouseEvent ev)
|
|
public override bool MouseEvent(MouseEvent ev)
|
|
{
|
|
{
|
|
- if (!ev.Flags.HasFlag(MouseFlags.Button1Clicked))
|
|
|
|
|
|
+ if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
|
|
return false;
|
|
return false;
|
|
if (!HasFocus)
|
|
if (!HasFocus)
|
|
- SuperView.SetFocus(this);
|
|
|
|
|
|
+ SuperView.SetFocus (this);
|
|
|
|
|
|
var point = ev.X;
|
|
var point = ev.X;
|
|
if (point > FieldLen)
|
|
if (point > FieldLen)
|
|
@@ -249,7 +286,7 @@ namespace Terminal.Gui {
|
|
if (point < 1)
|
|
if (point < 1)
|
|
point = 1;
|
|
point = 1;
|
|
CursorPosition = point;
|
|
CursorPosition = point;
|
|
- AdjCursorPosition();
|
|
|
|
|
|
+ AdjCursorPosition ();
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|