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;
25 /// Enumeration for event periodicity, used to define if a mark should be repeated beyond event's day. It's set when a mark is added.
27 public enum CalendarMarkRepeatType
30 /// Default value. Marks will be displayed only on event day.
35 /// Marks will be displayed every day after event day.
40 /// Marks will be displayed every week after event day.
45 /// Marks will be displayed every month day that coincides to event day.
50 /// Marks will be displayed every year that coincides to event day.
55 /// Marks will be displayed every last day of month after event day.
61 /// Enumeration for the mode, which determine how user could select a day.
63 public enum CalendarSelectMode
66 /// Default value. a day is always selected.
71 /// A day is always selected.
76 /// None of the days can be selected.
81 /// User may have selected a day or not.
87 /// Enumeration used to define which fields of a tm struct will be taken into account
90 public enum CalendarSelectable
93 /// None will be taken into account
97 /// Year will be taken into account
101 /// Month will be taken into account
105 /// Day will be taken into account
111 /// The CalendarMark is a Item for marking a Calendar's type,date and repeat type.
113 public class CalendarMark
115 internal IntPtr Handle;
118 /// A string used to define the type of mark.
123 /// A time struct to represent the date of inclusion of the mark.
125 public DateTime Date;
128 /// Repeat the event following this periodicity.
130 public CalendarMarkRepeatType Repeat;
133 /// Creates and initializes a new instance of the CalendarMark class.
135 /// <param name="type">Type of mark</param>
136 /// <param name="date">Date of inclusion of the mark</param>
137 /// <param name="repeat">Repeat type</param>
138 public CalendarMark(string type, DateTime date, CalendarMarkRepeatType repeat)
140 Handle = IntPtr.Zero;
148 /// The Calendar is a widget that helps applications to flexibly display a calender with day of the week, date, year and month.
150 public class Calendar : Layout
153 DateTime _cacheSelectedDate;
154 SmartEvent _displayedMonthChanged;
155 int _cacheDisplayedMonth;
157 Interop.Elementary.Elm_Calendar_Format_Cb _calendarFormat;
158 DateFormatDelegate _dateFormatDelegate = null;
161 /// Creates and initializes a new instance of the Calendar class.
163 /// <param name="parent">
164 /// The EvasObject to which the new Calendar will be attached as a child.
166 public Calendar(EvasObject parent) : base(parent)
168 _changed = new SmartEvent(this, this.RealHandle, "changed");
169 _changed.On += (sender, e) =>
171 DateTime selectedDate = SelectedDate;
172 DateChanged?.Invoke(this, new DateChangedEventArgs(_cacheSelectedDate, selectedDate));
173 _cacheSelectedDate = selectedDate;
176 _displayedMonthChanged = new SmartEvent(this, this.RealHandle, "display,changed");
177 _displayedMonthChanged.On += (sender, e) =>
179 int currentDisplayedMonth = DisplayedTime.Month;
180 DisplayedMonthChanged?.Invoke(this, new DisplayedMonthChangedEventArgs(_cacheDisplayedMonth, currentDisplayedMonth));
181 _cacheDisplayedMonth = currentDisplayedMonth;
184 _calendarFormat = (t) => { return _dateFormatDelegate(t); };
188 /// DateChanged will be triggered when the date in the calendar is changed.
190 public event EventHandler<DateChangedEventArgs> DateChanged;
193 /// DisplayedMonthChanged will be triggered when the current month displayed in the calendar is changed.
195 public event EventHandler<DisplayedMonthChangedEventArgs> DisplayedMonthChanged;
198 /// This delegate type is used to format the string that will be used to display month and year.
200 /// <param name="time">DateTime</param>
201 /// <returns></returns>
202 public delegate string DateFormatDelegate(DateTime time);
205 /// Sets or gets the minimum for year.
207 public int MinimumYear
213 Interop.Elementary.elm_calendar_min_max_year_get(RealHandle, out minimumYear, out unused);
220 Interop.Elementary.elm_calendar_min_max_year_get(RealHandle, out unused, out maximumYear);
221 if (maximumYear < 1902)
223 maximumYear = DateTime.MaxValue.Year;
225 Interop.Elementary.elm_calendar_min_max_year_set(RealHandle, value, maximumYear);
230 /// Sets or gets the maximum for the year.
232 public int MaximumYear
238 Interop.Elementary.elm_calendar_min_max_year_get(RealHandle, out unused, out maximumYear);
245 Interop.Elementary.elm_calendar_min_max_year_get(RealHandle, out minimumYear, out unused);
246 Interop.Elementary.elm_calendar_min_max_year_set(RealHandle, minimumYear, value);
251 /// Sets or gets the first day of week, who are used on Calendar.
253 public DateTime DisplayedTime
257 var tm = new Interop.Libc.SystemTime();
258 Interop.Elementary.elm_calendar_displayed_time_get(RealHandle, out tm);
260 // If the defect is fixed, it will be removed.
261 var daysInMonth = DateTime.DaysInMonth(tm.tm_year + 1900, tm.tm_mon + 1);
262 var day = tm.tm_mday;
264 if (day > daysInMonth)
269 DateTime date = new DateTime(tm.tm_year + 1900, tm.tm_mon + 1, day, tm.tm_hour, tm.tm_min, tm.tm_sec);
276 /// Sets or gets the first day of week, who are used on Calendar.
278 public DayOfWeek FirstDayOfWeek
282 return (DayOfWeek)Interop.Elementary.elm_calendar_first_day_of_week_get(RealHandle);
286 Interop.Elementary.elm_calendar_first_day_of_week_set(RealHandle, (int)value);
291 /// Sets or gets the weekdays names to be displayed by the Calendar.
294 /// The usage should be like this;
295 /// <![CDATA[List<string> weekDayNames = new List<string>() { "S", "M", "T", "W", "T", "F", "S" };]]>
296 /// Calendar.WeekDayNames = weekDayNames;
298 public IReadOnlyList<string> WeekDayNames
302 IntPtr stringArrayPtr = Interop.Elementary.elm_calendar_weekdays_names_get(RealHandle);
303 string[] stringArray;
304 IntPtrToStringArray(stringArrayPtr, 7, out stringArray);
309 if (value != null && value.Count == 7)
311 Interop.Elementary.elm_calendar_weekdays_names_set(RealHandle, value.ToArray());
317 /// Sets or gets the selected date.
320 /// Selected date changes when the user goes to next/previous month or select a day pressing over it on calendar.
322 public DateTime SelectedDate
326 var tm = new Interop.Libc.SystemTime();
327 Interop.Elementary.elm_calendar_selected_time_get(RealHandle, ref tm);
328 if (tm.tm_year == 0 && tm.tm_mon == 0 && tm.tm_mday == 0)
336 Interop.Libc.SystemTime tm = value;
337 Interop.Elementary.elm_calendar_selected_time_set(RealHandle, ref tm);
338 _cacheSelectedDate = value;
343 /// Sets or gets the interval on time updates for an user mouse button
344 /// hold on calendar widgets' month/year selection.
346 public double Interval
350 return Interop.Elementary.elm_calendar_interval_get(RealHandle);
354 Interop.Elementary.elm_calendar_interval_set(RealHandle, value);
359 /// Gets or sets the select day mode used.
361 public CalendarSelectMode SelectMode
365 return (CalendarSelectMode)Interop.Elementary.elm_calendar_select_mode_get(RealHandle);
369 Interop.Elementary.elm_calendar_select_mode_set(RealHandle, (Interop.Elementary.Elm_Calendar_Select_Mode)value);
374 /// Gets or sets fields of a datetime will be taken into account, when SelectedDate set is invoked.
376 public CalendarSelectable Selectable
380 return (CalendarSelectable)Interop.Elementary.elm_calendar_selectable_get(RealHandle);
384 Interop.Elementary.elm_calendar_selectable_set(RealHandle, (int)value);
389 /// Gets or sets date format the string that will be used to display month and year.
391 public DateFormatDelegate DateFormat
395 return _dateFormatDelegate;
399 _dateFormatDelegate = value;
402 Interop.Elementary.elm_calendar_format_function_set(RealHandle, _calendarFormat);
406 Interop.Elementary.elm_calendar_format_function_set(RealHandle, null);
412 /// Add a new mark to the calendar.
414 /// <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 days representation.</param>
415 /// <param name="date">A time struct to represent the date of inclusion of the mark. For marks that repeats it will just be displayed after the inclusion date in the calendar.</param>
416 /// <param name="repeat">Repeat the event following this periodicity. Can be a unique mark (that don't repeat), daily, weekly, monthly or annually.</param>
417 /// <returns>Item for a calendar mark.</returns>
418 public CalendarMark AddMark(string type, DateTime date, CalendarMarkRepeatType repeat)
420 CalendarMark mark = new CalendarMark(type, date, repeat);
421 Interop.Libc.SystemTime tm = date;
422 IntPtr nativeHandle = Interop.Elementary.elm_calendar_mark_add(RealHandle, type, ref tm, (Interop.Elementary.Elm_Calendar_Mark_Repeat_Type)repeat);
423 mark.Handle = nativeHandle;
429 /// Delete mark from the calendar.
431 /// <param name="mark">Item for a calendar mark</param>
432 public void DeleteMark(CalendarMark mark)
434 Interop.Elementary.elm_calendar_mark_del(mark.Handle);
438 /// Draw calendar marks.
440 public void DrawMarks()
442 Interop.Elementary.elm_calendar_marks_draw(RealHandle);
446 /// Remove all calendar's marks.
448 public void ClearMarks()
450 Interop.Elementary.elm_calendar_marks_clear(RealHandle);
454 /// Creates a widget handle.
456 /// <param name="parent">Parent EvasObject</param>
457 /// <returns>Handle IntPtr</returns>
458 protected override IntPtr CreateHandle(EvasObject parent)
460 IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
461 Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
463 RealHandle = Interop.Elementary.elm_calendar_add(handle);
464 Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
469 static void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray)
471 managedArray = new string[size];
472 IntPtr[] IntPtrArray = new IntPtr[size];
474 Marshal.Copy(unmanagedArray, IntPtrArray, 0, size);
476 for (int iterator = 0; iterator < size; iterator++)
478 managedArray[iterator] = Marshal.PtrToStringAnsi(IntPtrArray[iterator]);