DatePicker.cs 7.3 KB

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