TimeField.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // TimeField.cs: text entry for time
  3. //
  4. // Author: Jörg Preiß
  5. //
  6. // Licensed under the MIT license
  7. using System;
  8. using System.Globalization;
  9. using System.Linq;
  10. using NStack;
  11. namespace Terminal.Gui {
  12. /// <summary>
  13. /// Time editing <see cref="View"/>
  14. /// </summary>
  15. /// <remarks>
  16. /// The <see cref="TimeField"/> <see cref="View"/> provides time editing functionality with mouse support.
  17. /// </remarks>
  18. public class TimeField : TextField {
  19. bool isShort;
  20. int longFieldLen = 8;
  21. int shortFieldLen = 5;
  22. string sepChar;
  23. string longFormat;
  24. string shortFormat;
  25. int FieldLen { get { return isShort ? shortFieldLen : longFieldLen; } }
  26. string Format { get { return isShort ? shortFormat : longFormat; } }
  27. /// <summary>
  28. /// Initializes a new instance of <see cref="TimeField"/> at an absolute position and fixed size.
  29. /// </summary>
  30. /// <param name="x">The x coordinate.</param>
  31. /// <param name="y">The y coordinate.</param>
  32. /// <param name="time">Initial time contents.</param>
  33. /// <param name="isShort">If true, the seconds are hidden.</param>
  34. public TimeField (int x, int y, DateTime time, bool isShort = false) : base (x, y, isShort ? 7 : 10, "")
  35. {
  36. this.isShort = isShort;
  37. Initialize (time);
  38. }
  39. public TimeField (DateTime time) : base ("")
  40. {
  41. this.isShort = true;
  42. Width = FieldLen + 2;
  43. Initialize (time);
  44. }
  45. void Initialize (DateTime time)
  46. {
  47. CultureInfo cultureInfo = CultureInfo.CurrentCulture;
  48. sepChar = cultureInfo.DateTimeFormat.TimeSeparator;
  49. longFormat = $" HH{sepChar}mm{sepChar}ss";
  50. shortFormat = $" HH{sepChar}mm";
  51. CursorPosition = 1;
  52. Time = time;
  53. Changed += TimeField_Changed;
  54. }
  55. void TimeField_Changed (object sender, ustring e)
  56. {
  57. if (!DateTime.TryParseExact (Text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result))
  58. Text = e;
  59. }
  60. /// <summary>
  61. /// Gets or sets the time of the <see cref="TimeField"/>.
  62. /// </summary>
  63. /// <remarks>
  64. /// </remarks>
  65. public DateTime Time {
  66. get {
  67. if (!DateTime.TryParseExact (Text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) return new DateTime ();
  68. return result;
  69. }
  70. set {
  71. this.Text = value.ToString (Format);
  72. }
  73. }
  74. /// <summary>
  75. /// Get or set the data format for the widget.
  76. /// </summary>
  77. public bool IsShortFormat {
  78. get => isShort;
  79. set {
  80. isShort = value;
  81. if (isShort)
  82. Width = 7;
  83. else
  84. Width = 10;
  85. var ro = ReadOnly;
  86. if (ro)
  87. ReadOnly = false;
  88. SetText (Text);
  89. ReadOnly = ro;
  90. SetNeedsDisplay ();
  91. }
  92. }
  93. bool SetText (Rune key)
  94. {
  95. var text = TextModel.ToRunes (Text);
  96. var newText = text.GetRange (0, CursorPosition);
  97. newText.Add (key);
  98. if (CursorPosition < FieldLen)
  99. newText = newText.Concat (text.GetRange (CursorPosition + 1, text.Count - (CursorPosition + 1))).ToList ();
  100. return SetText (ustring.Make (newText));
  101. }
  102. bool SetText (ustring text)
  103. {
  104. ustring [] vals = text.Split (ustring.Make (sepChar));
  105. bool isValidTime = true;
  106. int hour = Int32.Parse (vals [0].ToString ());
  107. int minute = Int32.Parse (vals [1].ToString ());
  108. int second = isShort ? 0 : vals.Length > 2 ? Int32.Parse (vals [2].ToString ()) : 0;
  109. if (hour < 0) {
  110. isValidTime = false;
  111. hour = 0;
  112. vals [0] = "0";
  113. } else if (hour > 23) {
  114. isValidTime = false;
  115. hour = 23;
  116. vals [0] = "23";
  117. }
  118. if (minute < 0) {
  119. isValidTime = false;
  120. minute = 0;
  121. vals [1] = "0";
  122. } else if (minute > 59) {
  123. isValidTime = false;
  124. minute = 59;
  125. vals [1] = "59";
  126. }
  127. if (second < 0) {
  128. isValidTime = false;
  129. second = 0;
  130. vals [2] = "0";
  131. } else if (second > 59) {
  132. isValidTime = false;
  133. second = 59;
  134. vals [2] = "59";
  135. }
  136. string time = isShort ? $" {hour,2:00}{sepChar}{minute,2:00}" : $" {hour,2:00}{sepChar}{minute,2:00}{sepChar}{second,2:00}";
  137. Text = time;
  138. if (!DateTime.TryParseExact (text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||
  139. !isValidTime)
  140. return false;
  141. return true;
  142. }
  143. void IncCursorPosition ()
  144. {
  145. if (CursorPosition == FieldLen)
  146. return;
  147. if (Text [++CursorPosition] == sepChar.ToCharArray () [0])
  148. CursorPosition++;
  149. }
  150. void DecCursorPosition ()
  151. {
  152. if (CursorPosition == 1)
  153. return;
  154. if (Text [--CursorPosition] == sepChar.ToCharArray () [0])
  155. CursorPosition--;
  156. }
  157. void AdjCursorPosition ()
  158. {
  159. if (Text [CursorPosition] == sepChar.ToCharArray () [0])
  160. CursorPosition++;
  161. }
  162. ///<inheritdoc cref="ProcessKey(KeyEvent)"/>
  163. public override bool ProcessKey (KeyEvent kb)
  164. {
  165. switch (kb.Key) {
  166. case Key.DeleteChar:
  167. case Key.ControlD:
  168. SetText ('0');
  169. break;
  170. case Key.Delete:
  171. case Key.Backspace:
  172. SetText ('0');
  173. DecCursorPosition ();
  174. break;
  175. // Home, C-A
  176. case Key.Home:
  177. case Key.ControlA:
  178. CursorPosition = 1;
  179. break;
  180. case Key.CursorLeft:
  181. case Key.ControlB:
  182. DecCursorPosition ();
  183. break;
  184. case Key.End:
  185. case Key.ControlE: // End
  186. CursorPosition = FieldLen;
  187. break;
  188. case Key.CursorRight:
  189. case Key.ControlF:
  190. IncCursorPosition ();
  191. break;
  192. default:
  193. // Ignore non-numeric characters.
  194. if (kb.Key < (Key)((int)'0') || kb.Key > (Key)((int)'9'))
  195. return false;
  196. if (SetText (TextModel.ToRunes (ustring.Make ((uint)kb.Key)).First ()))
  197. IncCursorPosition ();
  198. return true;
  199. }
  200. return true;
  201. }
  202. ///<inheritdoc cref="MouseEvent(Gui.MouseEvent)"/>
  203. public override bool MouseEvent (MouseEvent ev)
  204. {
  205. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
  206. return false;
  207. if (!HasFocus)
  208. SuperView.SetFocus (this);
  209. var point = ev.X;
  210. if (point > FieldLen)
  211. point = FieldLen;
  212. if (point < 1)
  213. point = 1;
  214. CursorPosition = point;
  215. AdjCursorPosition ();
  216. return true;
  217. }
  218. }
  219. }