Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / DateTimeSelector.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18
19 namespace ElmSharp
20 {
21     /// <summary>
22     /// Enumeration of datetime field types for DateTimeSelector.
23     /// </summary>
24     public enum DateTimeFieldType
25     {
26         Year,
27         Month,
28         Date,
29         Hour,
30         Minute,
31         AmPm
32     }
33
34     /// <summary>
35     /// It inherits <see cref="Layout"/>
36     /// DateTimeSelector is a widget to display and input date & time values.
37     /// This widget displays date and time as per the system's locale settings
38     /// (Date includes Day, Month & Year along with the defined separators and Time includes Hour, Minute & AM/PM fields. Separator for AM/PM field is ignored.
39     /// </summary>
40     public class DateTimeSelector : Layout
41     {
42         SmartEvent _changed;
43         DateTime _cacheDateTime;
44
45         /// <summary>
46         /// Creates and initializes a new instance of the DateTimeSelector class.
47         /// </summary>
48         /// <param name="parent">The parent is a given container which will be attached by DateTimeSelector
49         ///as a child.It's <see cref="EvasObject"/> type.</param>
50         public DateTimeSelector(EvasObject parent) : base(parent)
51         {
52             _changed = new SmartEvent(this, this.RealHandle, "changed");
53             _changed.On += (s, e) =>
54             {
55                 DateTime newDateTime = DateTime;
56                 DateTimeChanged?.Invoke(this, new DateChangedEventArgs(_cacheDateTime, newDateTime));
57                 DateTime = newDateTime;
58             };
59         }
60
61         /// <summary>
62         /// ItemSelected is raised when Datetime field value changed.
63         /// </summary>
64         public event EventHandler<DateChangedEventArgs> DateTimeChanged;
65
66         /// <summary>
67         /// Gets or sets the datetime format.
68         /// </summary>
69         /// <remarks>
70         /// format is a combination of allowed LIBC date format specifiers like: "%b %d, %Y %I : %M %p".
71         /// </remarks>
72         public string Format
73         {
74             get
75             {
76                 return Interop.Elementary.elm_datetime_format_get(RealHandle);
77             }
78             set
79             {
80                 Interop.Elementary.elm_datetime_format_set(RealHandle, value);
81             }
82         }
83
84         /// <summary>
85         /// Gets or sets the upper boundary of DateTime field.
86         /// </summary>
87         public DateTime MaximumDateTime
88         {
89             get
90             {
91                 var tm = new Interop.Libc.SystemTime();
92                 Interop.Elementary.elm_datetime_value_max_get(RealHandle, ref tm);
93                 return tm;
94             }
95             set
96             {
97                 Interop.Libc.SystemTime tm = value;
98                 Interop.Elementary.elm_datetime_value_max_set(RealHandle, ref tm);
99             }
100         }
101
102         /// <summary>
103         /// Gets or sets the lower boundary of DateTime field.
104         /// </summary>
105         public DateTime MinimumDateTime
106         {
107             get
108             {
109                 var tm = new Interop.Libc.SystemTime();
110                 Interop.Elementary.elm_datetime_value_min_get(RealHandle, ref tm);
111                 return tm;
112             }
113             set
114             {
115                 Interop.Libc.SystemTime tm = value;
116                 Interop.Elementary.elm_datetime_value_min_set(RealHandle, ref tm);
117             }
118         }
119
120         /// <summary>
121         /// Gets or sets the current value of DateTime field.
122         /// </summary>
123         public DateTime DateTime
124         {
125             get
126             {
127                 var tm = new Interop.Libc.SystemTime();
128                 Interop.Elementary.elm_datetime_value_get(RealHandle, ref tm);
129                 return tm;
130             }
131             set
132             {
133                 Interop.Libc.SystemTime tm = value;
134                 Interop.Elementary.elm_datetime_value_set(RealHandle, ref tm);
135                 _cacheDateTime = value;
136             }
137         }
138
139         /// <summary>
140         /// Gets whether a field can be visible.
141         /// </summary>
142         /// <param name="type">Enumeration <see cref="DateTimeFieldType"/></param>
143         /// <returns>
144         /// The field is visible or not.
145         /// Type is bool.If visible, return true.
146         /// </returns>
147         public bool IsFieldVisible(DateTimeFieldType type)
148         {
149             return Interop.Elementary.elm_datetime_field_visible_get(RealHandle, (int)type);
150         }
151
152         /// <summary>
153         /// Sets the field limits of a field.
154         /// </summary>
155         /// <param name="type">Enumeration <see cref="DateTimeFieldType"/></param>
156         /// <param name="minimum">minimum limit</param>
157         /// <param name="maximum">maximum limit</param>
158         public void SetFieldLimit(DateTimeFieldType type, int minimum, int maximum)
159         {
160             Interop.Elementary.elm_datetime_field_limit_set(RealHandle, (int)type, minimum, maximum);
161         }
162
163         /// <summary>
164         /// Gets whether a field can be visible.
165         /// </summary>
166         /// <param name="type">Enumeration <see cref="DateTimeFieldType"/></param>
167         /// <param name="visible">When set as true, the field type visible.</param>
168         public void SetFieldVisible(DateTimeFieldType type, bool visible)
169         {
170             Interop.Elementary.elm_datetime_field_visible_set(RealHandle, (int)type, visible);
171         }
172
173         protected override IntPtr CreateHandle(EvasObject parent)
174         {
175             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
176             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
177
178             RealHandle = Interop.Elementary.elm_datetime_add(handle);
179             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
180
181             return handle;
182         }
183     }
184 }