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. TabStop = TabBehavior.TabGroup;
  159. _calendar = new TableView
  160. {
  161. X = 0,
  162. Y = Pos.Bottom (_dateLabel),
  163. Height = 11,
  164. Style = new TableStyle
  165. {
  166. ShowHeaders = true,
  167. ShowHorizontalBottomline = true,
  168. ShowVerticalCellLines = true,
  169. ExpandLastColumn = true
  170. }
  171. };
  172. _dateField = new DateField (DateTime.Now)
  173. {
  174. X = Pos.Right (_dateLabel),
  175. Y = 0,
  176. Width = Dim.Width (_calendar) - Dim.Width (_dateLabel),
  177. Height = 1,
  178. Culture = Culture
  179. };
  180. _previousMonthButton = new Button
  181. {
  182. X = Pos.Center () - 2,
  183. Y = Pos.Bottom (_calendar) - 1,
  184. Width = 2,
  185. Text = GetBackButtonText (),
  186. WantContinuousButtonPressed = true,
  187. NoPadding = true,
  188. NoDecorations = true,
  189. ShadowStyle = ShadowStyle.None
  190. };
  191. _previousMonthButton.Accept += (sender, e) =>
  192. {
  193. Date = _date.AddMonths (-1);
  194. CreateCalendar ();
  195. _dateField.Date = Date;
  196. };
  197. _nextMonthButton = new Button
  198. {
  199. X = Pos.Right (_previousMonthButton) + 2,
  200. Y = Pos.Bottom (_calendar) - 1,
  201. Width = 2,
  202. Text = GetForwardButtonText (),
  203. WantContinuousButtonPressed = true,
  204. NoPadding = true,
  205. NoDecorations = true,
  206. ShadowStyle = ShadowStyle.None
  207. };
  208. _nextMonthButton.Accept += (sender, e) =>
  209. {
  210. Date = _date.AddMonths (1);
  211. CreateCalendar ();
  212. _dateField.Date = Date;
  213. };
  214. CreateCalendar ();
  215. SelectDayOnCalendar (_date.Day);
  216. _calendar.CellActivated += (sender, e) =>
  217. {
  218. object dayValue = _table.Rows [e.Row] [e.Col];
  219. if (dayValue is null)
  220. {
  221. return;
  222. }
  223. bool isDay = int.TryParse (dayValue.ToString (), out int day);
  224. if (!isDay)
  225. {
  226. return;
  227. }
  228. ChangeDayDate (day);
  229. SelectDayOnCalendar (day);
  230. Text = _date.ToString (Format);
  231. };
  232. Width = Dim.Auto (DimAutoStyle.Content);
  233. Height = Dim.Auto (DimAutoStyle.Content);
  234. _dateField.DateChanged += DateField_DateChanged;
  235. Add (_dateLabel, _dateField, _calendar, _previousMonthButton, _nextMonthButton);
  236. }
  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. }