DatePicker.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #nullable enable
  2. //
  3. // DatePicker.cs: DatePicker control
  4. //
  5. // Author: Maciej Winnik
  6. //
  7. using System.Data;
  8. using System.Globalization;
  9. namespace Terminal.Gui.Views;
  10. /// <summary>Lets the user pick a date from a visual calendar.</summary>
  11. public class DatePicker : View
  12. {
  13. private TableView? _calendar;
  14. private DateTime _date;
  15. private DateField? _dateField;
  16. private Label? _dateLabel;
  17. private Button? _nextMonthButton;
  18. private Button? _previousMonthButton;
  19. private DataTable? _table;
  20. /// <summary>Initializes a new instance of <see cref="DatePicker"/>.</summary>
  21. public DatePicker () { SetInitialProperties (DateTime.Now); }
  22. /// <summary>Initializes a new instance of <see cref="DatePicker"/> with the specified date.</summary>
  23. public DatePicker (DateTime date) { SetInitialProperties (date); }
  24. /// <summary>CultureInfo for date. The default is CultureInfo.CurrentCulture.</summary>
  25. public CultureInfo? Culture
  26. {
  27. get => CultureInfo.CurrentCulture;
  28. set
  29. {
  30. if (value is { })
  31. {
  32. CultureInfo.CurrentCulture = value;
  33. Text = Date.ToString (Format);
  34. }
  35. }
  36. }
  37. /// <summary>Get or set the date.</summary>
  38. public DateTime Date
  39. {
  40. get => _date;
  41. set
  42. {
  43. _date = value;
  44. Text = _date.ToString (Format);
  45. }
  46. }
  47. private string Format => StandardizeDateFormat (Culture?.DateTimeFormat.ShortDatePattern);
  48. /// <inheritdoc/>
  49. protected override void Dispose (bool disposing)
  50. {
  51. _dateLabel?.Dispose ();
  52. _calendar?.Dispose ();
  53. _dateField?.Dispose ();
  54. _table?.Dispose ();
  55. _previousMonthButton?.Dispose ();
  56. _nextMonthButton?.Dispose ();
  57. base.Dispose (disposing);
  58. }
  59. private void ChangeDayDate (int day)
  60. {
  61. _date = new (_date.Year, _date.Month, day);
  62. _dateField!.Date = _date;
  63. CreateCalendar ();
  64. }
  65. private void CreateCalendar () { _calendar!.Table = new DataTableSource (_table = CreateDataTable (_date.Month, _date.Year)); }
  66. private DataTable CreateDataTable (int month, int year)
  67. {
  68. _table = new ();
  69. GenerateCalendarLabels ();
  70. int amountOfDaysInMonth = DateTime.DaysInMonth (year, month);
  71. DateTime dateValue = new DateTime (year, month, 1);
  72. DayOfWeek dayOfWeek = dateValue.DayOfWeek;
  73. _table.Rows.Add (new object [6]);
  74. for (var i = 1; i <= amountOfDaysInMonth; i++)
  75. {
  76. _table.Rows [^1] [(int)dayOfWeek] = i;
  77. if (dayOfWeek == DayOfWeek.Saturday && i != amountOfDaysInMonth)
  78. {
  79. _table.Rows.Add (new object [7]);
  80. }
  81. dayOfWeek = dayOfWeek == DayOfWeek.Saturday ? DayOfWeek.Sunday : dayOfWeek + 1;
  82. }
  83. int missingRows = 6 - _table.Rows.Count;
  84. for (var i = 0; i < missingRows; i++)
  85. {
  86. _table.Rows.Add (new object [7]);
  87. }
  88. return _table;
  89. }
  90. private void DateField_DateChanged (object? sender, EventArgs<DateTime> e)
  91. {
  92. Date = e.Value;
  93. if (e.Value.Date.Day != _date.Day)
  94. {
  95. SelectDayOnCalendar (e.Value.Day);
  96. }
  97. if (_date.Month == DateTime.MinValue.Month && _date.Year == DateTime.MinValue.Year)
  98. {
  99. _previousMonthButton!.Enabled = false;
  100. }
  101. else
  102. {
  103. _previousMonthButton!.Enabled = true;
  104. }
  105. if (_date.Month == DateTime.MaxValue.Month && _date.Year == DateTime.MaxValue.Year)
  106. {
  107. _nextMonthButton!.Enabled = false;
  108. }
  109. else
  110. {
  111. _nextMonthButton!.Enabled = true;
  112. }
  113. CreateCalendar ();
  114. SelectDayOnCalendar (_date.Day);
  115. }
  116. private void GenerateCalendarLabels ()
  117. {
  118. _calendar!.Style.ColumnStyles.Clear ();
  119. for (var i = 0; i < 7; i++)
  120. {
  121. string abbreviatedDayName =
  122. CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedDayName ((DayOfWeek)i);
  123. _calendar.Style.ColumnStyles.Add (
  124. i,
  125. new()
  126. {
  127. MaxWidth = abbreviatedDayName.Length,
  128. MinWidth = abbreviatedDayName.Length,
  129. MinAcceptableWidth = abbreviatedDayName.Length
  130. }
  131. );
  132. _table!.Columns.Add (abbreviatedDayName);
  133. }
  134. // TODO: Get rid of the +7 which is hackish
  135. _calendar.Width = _calendar.Style.ColumnStyles.Sum (c => c.Value.MinWidth) + 7;
  136. }
  137. private static string GetBackButtonText () { return Glyphs.LeftArrow + Glyphs.LeftArrow.ToString (); }
  138. private static string GetForwardButtonText () { return Glyphs.RightArrow + Glyphs.RightArrow.ToString (); }
  139. private void SelectDayOnCalendar (int day)
  140. {
  141. for (var i = 0; i < _table!.Rows.Count; i++)
  142. {
  143. for (var j = 0; j < _table.Columns.Count; j++)
  144. {
  145. if (_table.Rows [i] [j].ToString () == day.ToString ())
  146. {
  147. _calendar!.SetSelection (j, i, false);
  148. return;
  149. }
  150. }
  151. }
  152. }
  153. private void SetInitialProperties (DateTime date)
  154. {
  155. _date = date;
  156. BorderStyle = LineStyle.Single;
  157. Date = date;
  158. _dateLabel = new() { X = 0, Y = 0, Text = "Date: " };
  159. CanFocus = true;
  160. _calendar = new()
  161. {
  162. Id = "_calendar",
  163. X = 0,
  164. Y = Pos.Bottom (_dateLabel),
  165. Height = 11,
  166. Style = new()
  167. {
  168. ShowHeaders = true,
  169. ShowHorizontalBottomline = true,
  170. ShowVerticalCellLines = true,
  171. ExpandLastColumn = true
  172. },
  173. MultiSelect = false
  174. };
  175. _dateField = new (DateTime.Now)
  176. {
  177. Id = "_dateField",
  178. X = Pos.Right (_dateLabel),
  179. Y = 0,
  180. Width = Dim.Width (_calendar) - Dim.Width (_dateLabel),
  181. Height = 1,
  182. Culture = Culture
  183. };
  184. _previousMonthButton = new()
  185. {
  186. Id = "_previousMonthButton",
  187. X = Pos.Center () - 2,
  188. Y = Pos.Bottom (_calendar) - 1,
  189. Width = 2,
  190. Text = GetBackButtonText (),
  191. WantContinuousButtonPressed = true,
  192. NoPadding = true,
  193. NoDecorations = true,
  194. ShadowStyle = ShadowStyle.None
  195. };
  196. _previousMonthButton.Accepting += (_, _) => AdjustMonth (-1);
  197. _nextMonthButton = new()
  198. {
  199. Id = "_nextMonthButton",
  200. X = Pos.Right (_previousMonthButton) + 2,
  201. Y = Pos.Bottom (_calendar) - 1,
  202. Width = 2,
  203. Text = GetForwardButtonText (),
  204. WantContinuousButtonPressed = true,
  205. NoPadding = true,
  206. NoDecorations = true,
  207. ShadowStyle = ShadowStyle.None
  208. };
  209. _nextMonthButton.Accepting += (_, _) => AdjustMonth (1);
  210. CreateCalendar ();
  211. SelectDayOnCalendar (_date.Day);
  212. _calendar.CellActivated += (_, e) =>
  213. {
  214. object dayValue = _table!.Rows [e.Row] [e.Col];
  215. bool isDay = int.TryParse (dayValue.ToString (), out int day);
  216. if (!isDay)
  217. {
  218. return;
  219. }
  220. ChangeDayDate (day);
  221. SelectDayOnCalendar (day);
  222. Text = _date.ToString (Format);
  223. };
  224. Width = Dim.Auto (DimAutoStyle.Content);
  225. Height = Dim.Auto (DimAutoStyle.Content);
  226. _dateField.DateChanged += DateField_DateChanged;
  227. Add (_dateLabel, _dateField, _calendar, _previousMonthButton, _nextMonthButton);
  228. }
  229. private void AdjustMonth (int offset)
  230. {
  231. Date = _date.AddMonths (offset);
  232. CreateCalendar ();
  233. _dateField!.Date = Date;
  234. }
  235. /// <inheritdoc/>
  236. protected override bool OnDrawingText () { return true; }
  237. private static string StandardizeDateFormat (string? format)
  238. {
  239. return format switch
  240. {
  241. "MM/dd/yyyy" => "MM/dd/yyyy",
  242. "yyyy-MM-dd" => "yyyy-MM-dd",
  243. "yyyy/MM/dd" => "yyyy/MM/dd",
  244. "dd/MM/yyyy" => "dd/MM/yyyy",
  245. "d?/M?/yyyy" => "dd/MM/yyyy",
  246. "dd.MM.yyyy" => "dd.MM.yyyy",
  247. "dd-MM-yyyy" => "dd-MM-yyyy",
  248. "dd/MM yyyy" => "dd/MM/yyyy",
  249. "d. M. yyyy" => "dd.MM.yyyy",
  250. "yyyy.MM.dd" => "yyyy.MM.dd",
  251. "g yyyy/M/d" => "yyyy/MM/dd",
  252. "d/M/yyyy" => "dd/MM/yyyy",
  253. "d?/M?/yyyy g" => "dd/MM/yyyy",
  254. "d-M-yyyy" => "dd-MM-yyyy",
  255. "d.MM.yyyy" => "dd.MM.yyyy",
  256. "d.MM.yyyy '?'." => "dd.MM.yyyy",
  257. "M/d/yyyy" => "MM/dd/yyyy",
  258. "d. M. yyyy." => "dd.MM.yyyy",
  259. "d.M.yyyy." => "dd.MM.yyyy",
  260. "g yyyy-MM-dd" => "yyyy-MM-dd",
  261. "d.M.yyyy" => "dd.MM.yyyy",
  262. "d/MM/yyyy" => "dd/MM/yyyy",
  263. "yyyy/M/d" => "yyyy/MM/dd",
  264. "dd. MM. yyyy." => "dd.MM.yyyy",
  265. "yyyy. MM. dd." => "yyyy.MM.dd",
  266. "yyyy. M. d." => "yyyy.MM.dd",
  267. "d. MM. yyyy" => "dd.MM.yyyy",
  268. _ => "dd/MM/yyyy"
  269. };
  270. }
  271. }