1 /* Copyright (c) 2021 Samsung Electronics Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 using Tizen.NUI.BaseComponents;
19 using System.Collections.Generic;
20 using System.Collections.ObjectModel;
21 using System.ComponentModel;
22 using System.Globalization;
23 using System.Diagnostics.CodeAnalysis;
25 namespace Tizen.NUI.Components
28 /// DateChangedEventArgs is a class to notify changed DatePicker value argument which will sent to user.
30 [EditorBrowsable(EditorBrowsableState.Never)]
31 public class DateChangedEventArgs : EventArgs
34 /// DateChangedEventArgs default constructor.
35 /// <param name="date">date value of DatePicker.</param>
37 [EditorBrowsable(EditorBrowsableState.Never)]
38 public DateChangedEventArgs(DateTime date)
44 /// DateChangedEventArgs default constructor.
45 /// <returns>The current date value of DatePicker.</returns>
47 [EditorBrowsable(EditorBrowsableState.Never)]
48 public DateTime Date { get; }
52 /// DatePicker is a class which provides a function that allows the user to select
53 /// a date through a scrolling motion by expressing the specified value as a list.
54 /// DatePicker expresses the current date using the locale information of the system.
55 /// Year range is 1970~2038 (glibc time_t struct min, max value)
57 [EditorBrowsable(EditorBrowsableState.Never)]
58 public class DatePicker : Control
60 private DateTime currentDate;
61 private Picker dayPicker;
62 private Picker monthPicker;
63 private Picker yearPicker;
66 /// Creates a new instance of DatePicker.
68 [EditorBrowsable(EditorBrowsableState.Never)]
74 /// Creates a new instance of DatePicker.
76 /// <param name="style">Creates DatePicker by special style defined in UX.</param>
77 [EditorBrowsable(EditorBrowsableState.Never)]
78 public DatePicker(string style) : base(style)
83 /// Creates a new instance of DatePicker.
85 /// <param name="datePickerStyle">Creates DatePicker by style customized by user.</param>
86 [EditorBrowsable(EditorBrowsableState.Never)]
87 public DatePicker(DatePickerStyle datePickerStyle) : base(datePickerStyle)
93 /// Dispose DatePicker and all children on it.
95 /// <param name="type">Dispose type.</param>
96 [EditorBrowsable(EditorBrowsableState.Never)]
97 protected override void Dispose(DisposeTypes type)
104 if (type == DisposeTypes.Explicit)
107 Utility.Dispose(monthPicker);
110 Utility.Dispose(dayPicker);
113 Utility.Dispose(yearPicker);
121 /// An event emitted when DatePicker value changed, user can subscribe or unsubscribe to this event handler.
123 [EditorBrowsable(EditorBrowsableState.Never)]
124 public event EventHandler<DateChangedEventArgs> DateChanged;
127 /// The Date value of DatePicker.
129 [EditorBrowsable(EditorBrowsableState.Never)]
139 dayPicker.CurrentValue = currentDate.Day;
140 monthPicker.CurrentValue = currentDate.Month;
141 yearPicker.CurrentValue = currentDate.Year;
146 /// Initialize TimePicker object.
148 [EditorBrowsable(EditorBrowsableState.Never)]
149 public override void OnInitialize()
152 SetAccessibilityConstructor(Role.DateEditor);
154 dayPicker = new Picker()
159 dayPicker.ValueChanged += OnDayValueChanged;
161 monthPicker = new Picker()
166 monthPicker.ValueChanged += OnMonthValueChanged;
168 yearPicker = new Picker()
173 yearPicker.ValueChanged += OnYearValueChanged;
175 currentDate = DateTime.Now;
176 dayPicker.CurrentValue = currentDate.Day;
177 monthPicker.CurrentValue = currentDate.Month;
178 yearPicker.CurrentValue = currentDate.Year;
184 [EditorBrowsable(EditorBrowsableState.Never)]
185 [SuppressMessage("Microsoft.Reliability",
186 "CA2000:DisposeObjectsBeforeLosingScope",
187 Justification = "The CellPadding will be dispose when the date picker disposed")]
188 public override void ApplyStyle(ViewStyle viewStyle)
190 base.ApplyStyle(viewStyle);
192 if (viewStyle is DatePickerStyle datePickerStyle && Layout is LinearLayout linearLayout)
194 linearLayout.CellPadding = new Size(datePickerStyle.CellPadding.Width, datePickerStyle.CellPadding.Height);
196 yearPicker.ApplyStyle(datePickerStyle.Pickers);
197 monthPicker.ApplyStyle(datePickerStyle.Pickers);
198 dayPicker.ApplyStyle(datePickerStyle.Pickers);
202 private void Initialize()
204 HeightSpecification = LayoutParamPolicies.MatchParent;
206 Layout = new LinearLayout() {
207 LinearOrientation = LinearLayout.Orientation.Horizontal,
212 MaxDaySet(currentDate.Month);
215 private void OnDayValueChanged(object sender, ValueChangedEventArgs e)
217 if (currentDate.Day == e.Value) return;
219 currentDate = new DateTime(currentDate.Year, currentDate.Month, e.Value);
224 private void OnMonthValueChanged(object sender, ValueChangedEventArgs e)
226 if (currentDate.Month == e.Value) return;
233 private void OnYearValueChanged(object sender, ValueChangedEventArgs e)
235 if (currentDate.Year == e.Value) return;
237 currentDate = new DateTime(e.Value, currentDate.Month, currentDate.Day);
242 private void OnDateChanged()
244 DateChangedEventArgs eventArgs = new DateChangedEventArgs(currentDate);
245 DateChanged?.Invoke(this, eventArgs);
248 private void MaxDaySet(int month)
250 int maxDaysInMonth = DateTime.DaysInMonth(currentDate.Year, month);
251 dayPicker.MaxValue = maxDaysInMonth;
252 if (currentDate.Day > maxDaysInMonth)
254 currentDate = new DateTime(currentDate.Year, month, maxDaysInMonth);
255 dayPicker.CurrentValue = maxDaysInMonth;
258 currentDate = new DateTime(currentDate.Year, month, currentDate.Day);
261 //FIXME: There is no way to know when system locale changed in NUI.
262 // Pickers order and Month text has to be follow system locale.
263 private void PickersOrderSet()
265 String locale = Environment.GetEnvironmentVariable("LC_TIME");
266 DateTimeFormatInfo DateFormat = new CultureInfo(locale, false ).DateTimeFormat;
267 String temp = DateFormat.ShortDatePattern;
268 String[] strArray = temp.Split(' ', '/');
269 foreach (String format in strArray) {
270 if (format.IndexOf("M") != -1|| format.IndexOf("m") != -1) Add(monthPicker);
271 else if (format.IndexOf("d") != -1 || format.IndexOf("D") != -1) Add(dayPicker);
272 else if (format.IndexOf("y") != -1 || format.IndexOf("Y") != -1) Add(yearPicker);
276 private void SetMonthText()
278 String locale = Environment.GetEnvironmentVariable("LC_TIME");
279 CultureInfo info = new CultureInfo(locale);
280 monthPicker.DisplayedValues = new ReadOnlyCollection<string>(info.DateTimeFormat.AbbreviatedMonthNames);