TimeField.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. /// <summary>
  40. /// Initializes a new instance of <see cref="TimeField"/>
  41. /// </summary>
  42. /// <param name="time"></param>
  43. public TimeField (DateTime time) : base ("")
  44. {
  45. this.isShort = true;
  46. Width = FieldLen + 2;
  47. Initialize (time);
  48. }
  49. void Initialize (DateTime time)
  50. {
  51. CultureInfo cultureInfo = CultureInfo.CurrentCulture;
  52. sepChar = cultureInfo.DateTimeFormat.TimeSeparator;
  53. longFormat = $" HH{sepChar}mm{sepChar}ss";
  54. shortFormat = $" HH{sepChar}mm";
  55. CursorPosition = 1;
  56. Time = time;
  57. Changed += TimeField_Changed;
  58. }
  59. void TimeField_Changed (object sender, ustring e)
  60. {
  61. if (!DateTime.TryParseExact (Text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result))
  62. Text = e;
  63. }
  64. /// <summary>
  65. /// Gets or sets the time of the <see cref="TimeField"/>.
  66. /// </summary>
  67. /// <remarks>
  68. /// </remarks>
  69. public DateTime Time {
  70. get {
  71. if (!DateTime.TryParseExact (Text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) return new DateTime ();
  72. return result;
  73. }
  74. set {
  75. this.Text = value.ToString (Format);
  76. }
  77. }
  78. /// <summary>
  79. /// Get or set the data format for the widget.
  80. /// </summary>
  81. public bool IsShortFormat {
  82. get => isShort;
  83. set {
  84. isShort = value;
  85. if (isShort)
  86. Width = 7;
  87. else
  88. Width = 10;
  89. var ro = ReadOnly;
  90. if (ro)
  91. ReadOnly = false;
  92. SetText (Text);
  93. ReadOnly = ro;
  94. SetNeedsDisplay ();
  95. }
  96. }
  97. bool SetText (Rune key)
  98. {
  99. var text = TextModel.ToRunes (Text);
  100. var newText = text.GetRange (0, CursorPosition);
  101. newText.Add (key);
  102. if (CursorPosition < FieldLen)
  103. newText = newText.Concat (text.GetRange (CursorPosition + 1, text.Count - (CursorPosition + 1))).ToList ();
  104. return SetText (ustring.Make (newText));
  105. }
  106. bool SetText (ustring text)
  107. {
  108. ustring [] vals = text.Split (ustring.Make (sepChar));
  109. bool isValidTime = true;
  110. int hour = Int32.Parse (vals [0].ToString ());
  111. int minute = Int32.Parse (vals [1].ToString ());
  112. int second = isShort ? 0 : vals.Length > 2 ? Int32.Parse (vals [2].ToString ()) : 0;
  113. if (hour < 0) {
  114. isValidTime = false;
  115. hour = 0;
  116. vals [0] = "0";
  117. } else if (hour > 23) {
  118. isValidTime = false;
  119. hour = 23;
  120. vals [0] = "23";
  121. }
  122. if (minute < 0) {
  123. isValidTime = false;
  124. minute = 0;
  125. vals [1] = "0";
  126. } else if (minute > 59) {
  127. isValidTime = false;
  128. minute = 59;
  129. vals [1] = "59";
  130. }
  131. if (second < 0) {
  132. isValidTime = false;
  133. second = 0;
  134. vals [2] = "0";
  135. } else if (second > 59) {
  136. isValidTime = false;
  137. second = 59;
  138. vals [2] = "59";
  139. }
  140. string time = isShort ? $" {hour,2:00}{sepChar}{minute,2:00}" : $" {hour,2:00}{sepChar}{minute,2:00}{sepChar}{second,2:00}";
  141. Text = time;
  142. if (!DateTime.TryParseExact (text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||
  143. !isValidTime)
  144. return false;
  145. return true;
  146. }
  147. void IncCursorPosition ()
  148. {
  149. if (CursorPosition == FieldLen)
  150. return;
  151. if (Text [++CursorPosition] == sepChar.ToCharArray () [0])
  152. CursorPosition++;
  153. }
  154. void DecCursorPosition ()
  155. {
  156. if (CursorPosition == 1)
  157. return;
  158. if (Text [--CursorPosition] == sepChar.ToCharArray () [0])
  159. CursorPosition--;
  160. }
  161. void AdjCursorPosition ()
  162. {
  163. if (Text [CursorPosition] == sepChar.ToCharArray () [0])
  164. CursorPosition++;
  165. }
  166. ///<inheritdoc cref="ProcessKey(KeyEvent)"/>
  167. public override bool ProcessKey (KeyEvent kb)
  168. {
  169. switch (kb.Key) {
  170. case Key.DeleteChar:
  171. case Key.ControlD:
  172. SetText ('0');
  173. break;
  174. case Key.Delete:
  175. case Key.Backspace:
  176. SetText ('0');
  177. DecCursorPosition ();
  178. break;
  179. // Home, C-A
  180. case Key.Home:
  181. case Key.ControlA:
  182. CursorPosition = 1;
  183. break;
  184. case Key.CursorLeft:
  185. case Key.ControlB:
  186. DecCursorPosition ();
  187. break;
  188. case Key.End:
  189. case Key.ControlE: // End
  190. CursorPosition = FieldLen;
  191. break;
  192. case Key.CursorRight:
  193. case Key.ControlF:
  194. IncCursorPosition ();
  195. break;
  196. default:
  197. // Ignore non-numeric characters.
  198. if (kb.Key < (Key)((int)'0') || kb.Key > (Key)((int)'9'))
  199. return false;
  200. if (SetText (TextModel.ToRunes (ustring.Make ((uint)kb.Key)).First ()))
  201. IncCursorPosition ();
  202. return true;
  203. }
  204. return true;
  205. }
  206. ///<inheritdoc cref="MouseEvent(Gui.MouseEvent)"/>
  207. public override bool MouseEvent (MouseEvent ev)
  208. {
  209. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
  210. return false;
  211. if (!HasFocus)
  212. SuperView.SetFocus (this);
  213. var point = ev.X;
  214. if (point > FieldLen)
  215. point = FieldLen;
  216. if (point < 1)
  217. point = 1;
  218. CursorPosition = point;
  219. AdjCursorPosition ();
  220. return true;
  221. }
  222. }
  223. }