2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
20 using System.Runtime.InteropServices;
26 /// Enumeration for event periodicity, used to define if a mark should be repeated beyond the event's day. It's set when a mark is added.
28 /// <since_tizen> preview </since_tizen>
29 public enum CalendarMarkRepeatType
32 /// Default value. Marks will be displayed only on the event day.
37 /// Marks will be displayed every day after the event day.
42 /// Marks will be displayed every week after the event day.
47 /// Marks will be displayed every month that coincides to the event day.
52 /// Marks will be displayed every year that coincides to the event day.
57 /// Marks will be displayed every last day of month after the event day.
63 /// Enumeration for the mode, which determines how a user could select a day.
65 /// <since_tizen> preview </since_tizen>
66 public enum CalendarSelectMode
69 /// Default value. A day is always selected.
74 /// A day is always selected.
79 /// None of the days can be selected.
84 /// User may have selected a day.
90 /// Enumeration for defining which fields of a tm struct will be taken into account.
92 /// <since_tizen> preview </since_tizen>
94 public enum CalendarSelectable
97 /// None will be taken into account.
101 /// Year will be taken into account.
105 /// Month will be taken into account.
109 /// Day will be taken into account.
115 /// The CalendarMark is an item for marking a Calendar's type, date, and repeat type.
117 /// <since_tizen> preview </since_tizen>
118 public class CalendarMark
120 internal IntPtr Handle;
123 /// A string used to define the type of mark.
125 /// <since_tizen> preview </since_tizen>
129 /// A time struct to represent the date of inclusion of the mark.
131 /// <since_tizen> preview </since_tizen>
132 public DateTime Date;
135 /// Repeats the event following this periodicity.
137 /// <since_tizen> preview </since_tizen>
138 public CalendarMarkRepeatType Repeat;
141 /// Creates and initializes a new instance of the CalendarMark class.
143 /// <param name="type">Type of mark.</param>
144 /// <param name="date">Date of inclusion of the mark.</param>
145 /// <param name="repeat">Repeat type.</param>
146 /// <since_tizen> preview </since_tizen>
147 public CalendarMark(string type, DateTime date, CalendarMarkRepeatType repeat)
149 Handle = IntPtr.Zero;
157 /// The Calendar is a widget that helps applications to flexibly display a calender with day of the week, date, year, and month.
159 /// <since_tizen> preview </since_tizen>
160 public class Calendar : Layout
163 DateTime _cacheSelectedDate;
164 SmartEvent _displayedMonthChanged;
165 int _cacheDisplayedMonth;
167 Interop.Elementary.Elm_Calendar_Format_Cb _calendarFormat;
168 DateFormatDelegate _dateFormatDelegate = null;
171 /// Creates and initializes a new instance of the Calendar class.
173 /// <param name="parent">
174 /// The EvasObject to which the new calendar will be attached as a child.
176 /// <since_tizen> preview </since_tizen>
177 public Calendar(EvasObject parent) : base(parent)
179 _changed = new SmartEvent(this, this.RealHandle, "changed");
180 _changed.On += (sender, e) =>
182 DateTime selectedDate = SelectedDate;
183 DateChanged?.Invoke(this, new DateChangedEventArgs(_cacheSelectedDate, selectedDate));
184 _cacheSelectedDate = selectedDate;
187 _displayedMonthChanged = new SmartEvent(this, this.RealHandle, "display,changed");
188 _displayedMonthChanged.On += (sender, e) =>
190 int currentDisplayedMonth = DisplayedTime.Month;
191 DisplayedMonthChanged?.Invoke(this, new DisplayedMonthChangedEventArgs(_cacheDisplayedMonth, currentDisplayedMonth));
192 _cacheDisplayedMonth = currentDisplayedMonth;
195 _calendarFormat = (ref Interop.Libc.SystemTime t) => { return _dateFormatDelegate(t); };
199 /// DateChanged will be triggered when the date in the calendar is changed.
201 /// <since_tizen> preview </since_tizen>
202 public event EventHandler<DateChangedEventArgs> DateChanged;
205 /// DisplayedMonthChanged will be triggered when the current month displayed in the calendar is changed.
207 /// <since_tizen> preview </since_tizen>
208 public event EventHandler<DisplayedMonthChangedEventArgs> DisplayedMonthChanged;
211 /// This delegate type is used to format the string that will be used to display month and year.
213 /// <param name="time">DateTime</param>
214 /// <returns></returns>
215 /// <since_tizen> preview </since_tizen>
216 public delegate string DateFormatDelegate(DateTime time);
219 /// Sets or gets the minimum for year.
221 /// <since_tizen> preview </since_tizen>
222 public int MinimumYear
228 Interop.Elementary.elm_calendar_min_max_year_get(RealHandle, out minimumYear, out unused);
235 Interop.Elementary.elm_calendar_min_max_year_get(RealHandle, out unused, out maximumYear);
236 if (maximumYear < 1902)
238 maximumYear = DateTime.MaxValue.Year;
240 Interop.Elementary.elm_calendar_min_max_year_set(RealHandle, value, maximumYear);
245 /// Sets or gets the maximum for the year.
247 /// <since_tizen> preview </since_tizen>
248 public int MaximumYear
254 Interop.Elementary.elm_calendar_min_max_year_get(RealHandle, out unused, out maximumYear);
261 Interop.Elementary.elm_calendar_min_max_year_get(RealHandle, out minimumYear, out unused);
262 Interop.Elementary.elm_calendar_min_max_year_set(RealHandle, minimumYear, value);
267 /// Sets or gets the first day of the week, which is used on the calendar.
269 /// <since_tizen> preview </since_tizen>
270 public DateTime DisplayedTime
274 Interop.Elementary.elm_calendar_displayed_time_get(RealHandle, out Interop.Libc.SystemTime tm);
276 // If the defect is fixed, it will be removed.
277 var daysInMonth = DateTime.DaysInMonth(tm.tm_year + 1900, tm.tm_mon + 1);
278 var day = tm.tm_mday;
280 if (day > daysInMonth)
285 DateTime date = new DateTime(tm.tm_year + 1900, tm.tm_mon + 1, day, tm.tm_hour, tm.tm_min, tm.tm_sec);
292 /// Sets or gets the first day of the week, which is used on the calendar.
294 /// <since_tizen> preview </since_tizen>
295 public DayOfWeek FirstDayOfWeek
299 return (DayOfWeek)Interop.Elementary.elm_calendar_first_day_of_week_get(RealHandle);
303 Interop.Elementary.elm_calendar_first_day_of_week_set(RealHandle, (int)value);
308 /// Sets or gets the weekdays name to be displayed by the calendar.
311 /// The usage should be like this:
312 /// <![CDATA[List<string> weekDayNames = new List<string>() { "S", "M", "T", "W", "T", "F", "S" };]]>
313 /// Calendar.WeekDayNames = weekDayNames;
315 /// <since_tizen> preview </since_tizen>
316 public IReadOnlyList<string> WeekDayNames
320 IntPtr stringArrayPtr = Interop.Elementary.elm_calendar_weekdays_names_get(RealHandle);
321 string[] stringArray;
322 IntPtrToStringArray(stringArrayPtr, 7, out stringArray);
327 if (value != null && value.Count == 7)
329 Interop.Elementary.elm_calendar_weekdays_names_set(RealHandle, value.ToArray());
335 /// Sets or gets the selected date.
338 /// The selected date changes when the user goes to the next/previous month or selects a day pressing over it on the calendar.
340 /// <since_tizen> preview </since_tizen>
341 public DateTime SelectedDate
345 var tm = new Interop.Libc.SystemTime();
346 Interop.Elementary.elm_calendar_selected_time_get(RealHandle, ref tm);
347 if (tm.tm_year == 0 && tm.tm_mon == 0 && tm.tm_mday == 0)
355 Interop.Libc.SystemTime tm = value;
356 Interop.Elementary.elm_calendar_selected_time_set(RealHandle, ref tm);
357 _cacheSelectedDate = value;
362 /// Sets or gets the interval on time updates for a user mouse button
363 /// hold, on the calendar widgets' month/year selection.
365 /// <since_tizen> preview </since_tizen>
366 public double Interval
370 return Interop.Elementary.elm_calendar_interval_get(RealHandle);
374 Interop.Elementary.elm_calendar_interval_set(RealHandle, value);
379 /// Gets or sets the select day mode used.
381 /// <since_tizen> preview </since_tizen>
382 public CalendarSelectMode SelectMode
386 return (CalendarSelectMode)Interop.Elementary.elm_calendar_select_mode_get(RealHandle);
390 Interop.Elementary.elm_calendar_select_mode_set(RealHandle, (Interop.Elementary.Elm_Calendar_Select_Mode)value);
395 /// Gets or sets the fields of a datetime that will be taken into account, when SelectedDate set is invoked.
397 /// <since_tizen> preview </since_tizen>
398 public CalendarSelectable Selectable
402 return (CalendarSelectable)Interop.Elementary.elm_calendar_selectable_get(RealHandle);
406 Interop.Elementary.elm_calendar_selectable_set(RealHandle, (int)value);
411 /// Gets or sets the date format of the string that will be used to display month and year.
413 /// <since_tizen> preview </since_tizen>
414 public DateFormatDelegate DateFormat
418 return _dateFormatDelegate;
422 _dateFormatDelegate = value;
425 Interop.Elementary.elm_calendar_format_function_set(RealHandle, _calendarFormat);
429 Interop.Elementary.elm_calendar_format_function_set(RealHandle, null);
435 /// Adds a new mark to the calendar.
437 /// <param name="type">A string used to define the type of mark. It will be emitted to the theme that should display a related modification on these day's representation.</param>
438 /// <param name="date">A time struct to represent the date of inclusion of the mark. For marks that repeat, it will just be displayed after the inclusion date in the calendar.</param>
439 /// <param name="repeat">Repeat the event following this periodicity. Can be a unique mark (that doesn't repeat), daily, weekly, monthly, or annually.</param>
440 /// <returns>Item for a calendar mark.</returns>
441 /// <since_tizen> preview </since_tizen>
442 public CalendarMark AddMark(string type, DateTime date, CalendarMarkRepeatType repeat)
444 CalendarMark mark = new CalendarMark(type, date, repeat);
445 Interop.Libc.SystemTime tm = date;
446 IntPtr nativeHandle = Interop.Elementary.elm_calendar_mark_add(RealHandle, type, ref tm, (Interop.Elementary.Elm_Calendar_Mark_Repeat_Type)repeat);
447 mark.Handle = nativeHandle;
453 /// Deletes a mark from the calendar.
455 /// <param name="mark">Item for a calendar mark.</param>
456 /// <since_tizen> preview </since_tizen>
457 public void DeleteMark(CalendarMark mark)
459 Interop.Elementary.elm_calendar_mark_del(mark.Handle);
463 /// Draws the calendar marks.
465 /// <since_tizen> preview </since_tizen>
466 public void DrawMarks()
468 Interop.Elementary.elm_calendar_marks_draw(RealHandle);
472 /// Removes all the calendar's marks.
474 /// <since_tizen> preview </since_tizen>
475 public void ClearMarks()
477 Interop.Elementary.elm_calendar_marks_clear(RealHandle);
481 /// Creates a widget handle.
483 /// <param name="parent">Parent EvasObject.</param>
484 /// <returns>Handle IntPtr.</returns>
485 /// <since_tizen> preview </since_tizen>
486 protected override IntPtr CreateHandle(EvasObject parent)
488 IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
489 Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
491 RealHandle = Interop.Elementary.elm_calendar_add(handle);
492 Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
497 static void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray)
499 managedArray = new string[size];
500 IntPtr[] IntPtrArray = new IntPtr[size];
502 Marshal.Copy(unmanagedArray, IntPtrArray, 0, size);
504 for (int iterator = 0; iterator < size; iterator++)
506 managedArray[iterator] = Marshal.PtrToStringAnsi(IntPtrArray[iterator]);