Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_EditTimeImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FUiCtrl_EditTimeImpl.cpp
20  * @brief               This is the implementation file for the _EditTimeImpl class.
21  */
22
23 #include <FBaseInternalTypes.h>
24 #include <FBaseSysLog.h>
25 #include <FUiAccessibilityContainer.h>
26 #include <FUiAccessibilityElement.h>
27 #include <FApp_AppInfo.h>
28 #include "FUi_CoordinateSystemUtils.h"
29 #include "FUi_UiBuilder.h"
30 #include "FUi_ResourceManager.h"
31 #include "FUi_ResourceSizeInfo.h"
32 #include "FUiCtrl_EditTimeImpl.h"
33 #include "FUiCtrl_DateTimeDefine.h"
34
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Base;
38 using namespace Tizen::App;
39
40 namespace Tizen { namespace Ui { namespace Controls
41 {
42
43 _EditTimeImpl::_EditTimeImpl(EditTime* pPublic, _EditTime* pCore)
44         : _ControlImpl(pPublic, pCore)
45         , __pPublicTimeChangeEvent(null)
46 {
47         ClearLastResult();
48 }
49
50 _EditTimeImpl::~_EditTimeImpl(void)
51 {
52         if (__pPublicTimeChangeEvent)
53         {
54                 delete __pPublicTimeChangeEvent;
55                 __pPublicTimeChangeEvent = null;
56         }
57
58         ClearLastResult();
59 }
60
61 _EditTimeImpl*
62 _EditTimeImpl::GetInstance(EditTime& editTime)
63 {
64         return static_cast<_EditTimeImpl*> (editTime._pControlImpl);
65 }
66
67 const _EditTimeImpl*
68 _EditTimeImpl::GetInstance(const EditTime& editTime)
69 {
70         return static_cast<const _EditTimeImpl*> (editTime._pControlImpl);
71 }
72
73 _EditTimeImpl*
74 _EditTimeImpl::CreateEditTimeImplN(EditTime* pControl, const Point& point, const String& title)
75 {
76         return CreateEditTimeImplFN(pControl, _CoordinateSystemUtils::ConvertToFloat(point), title);
77 }
78
79 _EditTimeImpl*
80 _EditTimeImpl::CreateEditTimeImplFN(EditTime* pControl, const FloatPoint& point, const String& title)
81 {
82         result r = E_SUCCESS;
83         FloatDimension dim;
84
85         _EditTime* pCore = _EditTime::CreateEditTimeN(title);
86         SysTryReturn(NID_UI_CTRL, pCore, null, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
87
88         _EditTimeImpl* pImpl = new (std::nothrow) _EditTimeImpl(pControl, pCore);
89
90         r = CheckConstruction(pCore, pImpl);
91         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
92
93         r = pCore->AddTimeChangeEventListener(*pImpl);
94         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
95
96         GET_SHAPE_CONFIG(EDITTIME::WIDTH, pCore->GetOrientation(), dim.width);
97         GET_SHAPE_CONFIG(EDITTIME::HEIGHT, pCore->GetOrientation(), dim.height);
98
99         r = pImpl->InitializeBoundsPropertiesF(GET_SIZE_INFO(EditTime), FloatRectangle(point.x, point.y, dim.width, dim.height), pCore->GetOrientation());
100
101         ClearLastResult();
102
103         return pImpl;
104
105 CATCH:
106         delete pImpl;
107         return null;
108 }
109
110 const char*
111 _EditTimeImpl::GetPublicClassName(void) const
112 {
113         return "Tizen::Ui::Controls::EditTime";
114 }
115
116 const EditTime&
117 _EditTimeImpl::GetPublic(void) const
118 {
119         return static_cast <const EditTime&>(_ControlImpl::GetPublic());
120 }
121
122 EditTime&
123 _EditTimeImpl::GetPublic(void)
124 {
125         return static_cast <EditTime&>(_ControlImpl::GetPublic());
126 }
127
128 const _EditTime&
129 _EditTimeImpl::GetCore(void) const
130 {
131         return static_cast <const _EditTime&>(_ControlImpl::GetCore());
132 }
133
134 _EditTime&
135 _EditTimeImpl::GetCore(void)
136 {
137         return static_cast <_EditTime&>(_ControlImpl::GetCore());
138 }
139
140 result
141 _EditTimeImpl::OnAttachedToMainTree(void)
142 {
143         return _ControlImpl::OnAttachedToMainTree();
144 }
145
146 result
147 _EditTimeImpl::AddTimeChangeEventListener(ITimeChangeEventListener& listener)
148 {
149         ClearLastResult();
150
151         if (__pPublicTimeChangeEvent == null)
152         {
153                 __pPublicTimeChangeEvent = new (std::nothrow) _PublicDateTimeChangeEvent(GetPublic());
154
155                 SysTryReturn(NID_UI_CTRL, __pPublicTimeChangeEvent,
156                                         E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
157         }
158
159         result r = __pPublicTimeChangeEvent->AddListener(listener);
160         SysTryReturnResult(NID_UI_CTRL, r != E_OBJ_ALREADY_EXIST, E_SYSTEM, "The ITimeChangeEventListener instance already exists.");
161
162         return r;
163 }
164
165 result
166 _EditTimeImpl::RemoveTimeChangeEventListener(ITimeChangeEventListener& listener)
167 {
168         ClearLastResult();
169
170         result r = E_OBJ_NOT_FOUND;
171
172         if (__pPublicTimeChangeEvent)
173         {
174                 r = __pPublicTimeChangeEvent->RemoveListener(listener);
175         }
176
177         SysTryReturnResult(NID_UI_CTRL, r != E_OBJ_NOT_FOUND, E_SYSTEM, "ITimeChangeEventListener instance is not found.");
178
179         return r;
180 }
181
182 DateTime
183 _EditTimeImpl::GetTime(void) const
184 {
185         ClearLastResult();
186
187         Variant time = GetCore().GetPropertyTime();
188
189         return time.ToDateTime();
190 }
191
192 int
193 _EditTimeImpl::GetHour(void) const
194 {
195         ClearLastResult();
196
197         Variant hour = GetCore().GetPropertyHour();
198
199         return hour.ToInt();
200 }
201
202 int
203 _EditTimeImpl::GetMinute(void) const
204 {
205         ClearLastResult();
206
207         Variant minute = GetCore().GetPropertyMinute();
208
209         return minute.ToInt();
210 }
211
212 void
213 _EditTimeImpl::SetTime(const DateTime& time)
214 {
215         ClearLastResult();
216
217         GetCore().SetPropertyTime(Variant(time));
218         return;
219 }
220
221 void
222 _EditTimeImpl::SetCurrentTime(void)
223 {
224         ClearLastResult();
225
226         GetCore().SetCurrentTime();
227         return;
228 }
229
230 result
231 _EditTimeImpl::SetHour(int hour)
232 {
233         ClearLastResult();
234         SysTryReturn(NID_UI_CTRL, (hour >= DATETIME_HOUR_MIN && hour <= DATETIME_HOUR_MAX), E_INVALID_ARG, E_INVALID_ARG,
235                         "[E_INVALID_ARG] Invalid argument is used. hour (%d).", hour);
236
237         result r = GetCore().SetPropertyHour(Variant(hour));
238         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
239
240         return r;
241 }
242
243 result
244 _EditTimeImpl::SetMinute(int minute)
245 {
246         ClearLastResult();
247         SysTryReturn(NID_UI_CTRL, (minute >= DATETIME_MINUTE_MIN && minute <= DATETIME_MINUTE_MAX), E_INVALID_ARG, E_INVALID_ARG,
248                         "[E_INVALID_ARG] Invalid argument is used. minute (%d).", minute);
249
250         result r = GetCore().SetPropertyMinute(Variant(minute));
251         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
252
253         return r;
254 }
255
256 void
257 _EditTimeImpl::SetTimePickerEnabled(bool enable)
258 {
259         ClearLastResult();
260
261         GetCore().SetPropertyTimePickerEnabled(Variant(enable));
262         return;
263 }
264
265 bool
266 _EditTimeImpl::IsTimePickerEnabled(void) const
267 {
268         ClearLastResult();
269
270         Variant enable = GetCore().GetPropertyTimePickerEnabled();
271
272         return enable.ToBool();
273 }
274
275 void
276 _EditTimeImpl::Set24HourNotationEnabled(bool enable)
277 {
278         ClearLastResult();
279
280         GetCore().Set24HourNotationEnabled(enable);
281         return;
282 }
283
284 void
285 _EditTimeImpl::OnDateTimeChanged(const _Control& source, int year, int month, int day, int hour, int minute)
286 {
287         ClearLastResult();
288
289         if (__pPublicTimeChangeEvent == null)
290         {
291                 return;
292         }
293
294         _PublicDateTimeChangeEventArg* pEventArg = new (std::nothrow) _PublicDateTimeChangeEventArg(TIME_PUBLIC_CHANGE_SAVED);
295         SysTryReturnVoidResult(NID_UI_CTRL, pEventArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
296
297         pEventArg->SetDate(year, month, day);
298         pEventArg->SetTime(hour, minute);
299         __pPublicTimeChangeEvent->Fire(*pEventArg);
300
301         return;
302 }
303
304 void
305 _EditTimeImpl::OnDateTimeChangeCanceled(const _Control& source)
306 {
307         ClearLastResult();
308
309         if (__pPublicTimeChangeEvent == null)
310         {
311                 return;
312         }
313
314         _PublicDateTimeChangeEventArg* pEventArg = new (std::nothrow) _PublicDateTimeChangeEventArg(TIME_PUBLIC_CHANGE_CANCELED);
315         SysTryReturnVoidResult(NID_UI_CTRL, pEventArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
316
317         __pPublicTimeChangeEvent->Fire(*pEventArg);
318
319         return;
320 }
321
322 bool
323 _EditTimeImpl::IsXmlBoundsExist(void)
324 {
325         FloatRectangle builderBounds;
326         _ControlOrientation controlOrientation = _CONTROL_ORIENTATION_PORTRAIT;
327         bool exist = GetBuilderBoundsF(controlOrientation, builderBounds);
328         if(!exist)
329         {
330                 return false;
331         }
332
333         controlOrientation = GetCore().GetOrientation();
334
335         if (controlOrientation == _CONTROL_ORIENTATION_LANDSCAPE)
336         {
337                 exist = GetBuilderBoundsF(controlOrientation, builderBounds);
338         }
339
340         if(!exist)
341         {
342                 return false;
343         }
344
345         return true;
346 }
347
348 void
349 _EditTimeImpl::OnChangeLayout(_ControlOrientation orientation)
350 {
351         GetCore().SetXmlBoundsExist(IsXmlBoundsExist());
352
353         return _ControlImpl::OnChangeLayout(orientation);
354 }
355
356 class _EditTimeMaker
357         : public _UiBuilderControlMaker
358 {
359 public:
360         _EditTimeMaker(_UiBuilder* uibuilder)
361                 : _UiBuilderControlMaker(uibuilder){}
362         virtual ~_EditTimeMaker(void){}
363         static _UiBuilderControlMaker* GetInstance(_UiBuilder* pUiBuilder)
364         {
365                 _EditTimeMaker* pEditTimeMaker = new (std::nothrow) _EditTimeMaker(pUiBuilder);
366                 return pEditTimeMaker;
367         }
368
369 protected:
370         virtual Control* Make(_UiBuilderControl* pControl)
371         {
372                 result r = E_SUCCESS;
373
374                 _UiBuilderControlLayout* pControlProperty = null;
375                 EditTime* pEditTime = null;
376
377                 FloatPoint point;
378                 Tizen::Base::String elementString;
379                 bool titleEnable = false;
380                 bool hourNotationEnable = false;
381                 FloatRectangle rect;
382
383                 GetProperty(pControl, &pControlProperty);
384                 if (pControlProperty == null)
385                 {
386                         return null;
387                 }
388
389                 rect = pControlProperty->GetRectF();
390                 point.x = rect.x;
391                 point.y = rect.y;
392
393                 if (pControl->GetElement(L"titleText", elementString))
394                 {
395                         titleEnable = true;
396                 }
397
398                 pEditTime = new (std::nothrow) EditTime;
399                 if (pEditTime == null)
400                 {
401                         return null;
402                 }
403
404                 if (titleEnable)
405                 {
406                         r = pEditTime->Construct(point, elementString);
407                 }
408                 else
409                 {
410                         r = pEditTime->Construct(point);
411                 }
412
413                 if (r != E_SUCCESS)
414                 {
415                         delete pEditTime;
416                         pEditTime = null;
417                         return null;
418                 }
419
420                 if (pControl->GetElement(L"b24HourNotationEnabled", elementString))
421                 {
422                         if (elementString.Equals(L"true", false))
423                         {
424                                 hourNotationEnable = true;
425                                 pEditTime->Set24HourNotationEnabled(hourNotationEnable);
426                         }
427                 }
428
429                 if (pControl->GetElement(L"accessibilityHint", elementString))
430                 {
431                         AccessibilityContainer* pContainer = pEditTime->GetAccessibilityContainer();
432                         if (pContainer)
433                         {
434                                 AccessibilityElement* pElement = pContainer->GetElement(L"EditTimeText");
435                                 if (pElement)
436                                 {
437                                         pElement->SetHint(elementString);
438                                 }
439                         }
440                 }
441
442                 return pEditTime;
443         }
444
445 }; // _EditTimeMaker
446
447 _EditTimeRegister::_EditTimeRegister(void)
448 {
449         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
450         pUiBuilderControlTableManager->RegisterControl(L"EditTime", _EditTimeMaker::GetInstance);
451 }
452
453 _EditTimeRegister::~_EditTimeRegister(void)
454 {
455         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
456         pUiBuilderControlTableManager->UnregisterControl(L"EditTime");
457 }
458 static _EditTimeRegister EditTimeRegisterToUiBuilder;
459 }}} // Tizen::Ui::Controls