DatePicker.cs 10 KB

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