0889d3794b821b5fb1d3624bb11960842e1284f7
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_DateTimePicker.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_DateTimePicker.cpp
20  * @brief               This is the implementation file for the _DateTimePicker class.
21  */
22
23 #include <FGrpDimension.h>
24 #include <FBaseColIEnumeratorT.h>
25 #include <FBaseSysLog.h>
26 #include <FGrp_BitmapImpl.h>
27
28 #include "FUiCtrl_DateTimePicker.h"
29 #include "FUiAnim_VisualElement.h"
30 #include "FUiCtrl_DateTimeDefine.h"
31 #include "FUiCtrl_DateTimePresenter.h"
32 #include "FUiCtrl_DateTimeDisplayBox.h"
33 #include "FUiCtrl_Form.h"
34 #include "FUiCtrl_Frame.h"
35 #include "FUiCtrl_Button.h"
36 #include "FUiCtrl_Toolbar.h"
37 #include "FUiCtrl_DateTimeChangeEvent.h"
38 #include "FUiCtrl_IDateTimeChangeEventListener.h"
39 #include "FUiCtrl_DateTimeUtils.h"
40 #include "FUi_AccessibilityContainer.h"
41 #include "FUi_AccessibilityElement.h"
42 #include "FUi_AccessibilityManager.h"
43 #include "FUi_UiTouchEvent.h"
44 #include "FUi_ResourceManager.h"
45 #include "FUi_CoordinateSystemUtils.h"
46
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Collection;
49 using namespace Tizen::Graphics;
50 using namespace Tizen::Ui;
51 using namespace Tizen::Ui::Animations;
52
53 namespace Tizen { namespace Ui { namespace Controls
54 {
55
56 IMPLEMENT_PROPERTY(_DateTimePicker);
57
58 _DateTimePicker::_DateTimePicker(_DateTimePresenter* pPresenter, const String& title)
59         : __pPresenter(pPresenter)
60         , __pFooter(null)
61         , __title(title)
62         , __pDateTimeChangeEvent(null)
63         , __pFont(null)
64         , __pDisplayVisualElement(null)
65 {
66 }
67
68 _DateTimePicker::~_DateTimePicker(void)
69 {
70         if (__pDateTimeChangeEvent != null)
71         {
72                 delete __pDateTimeChangeEvent;
73                 __pDateTimeChangeEvent = null;
74         }
75
76         if (__pFooter != null)
77         {
78                 DetachSystemChild(*__pFooter);
79                 delete __pFooter;
80                 __pFooter = null;
81         }
82
83         if (__pDisplayVisualElement != null)
84         {
85                 __pDisplayVisualElement->Destroy();
86                 __pDisplayVisualElement = null;
87         }
88
89         delete __pPresenter;
90         __pPresenter = null;
91 }
92
93 _DateTimePicker*
94 _DateTimePicker::CreateDateTimePickerN(int style, const String& title)
95 {
96         result r = E_SUCCESS;
97         FloatDimension pickerSize(0.0f, 0.0f);
98         FloatDimension screenSize(0.0f, 0.0f);
99         _ControlOrientation orientation = _CONTROL_ORIENTATION_PORTRAIT;
100         _DateTimePresenter* pPresenter = null;
101         _DateTimePicker* pView = null;
102
103         SysTryReturn(NID_UI_CTRL, ((style & DATETIME_OUTPUT_STYLE_DATETIME) != DATETIME_OUTPUT_STYLE_INVALID), null, E_INVALID_ARG,
104                                 "[E_INVALID_ARG] Invalid argument(s) is used. The style provided is not present in the _DateTimeOutputStyle list.");
105
106         pPresenter = new (std::nothrow) _DateTimePresenter(style, title);
107         SysTryReturn(NID_UI_CTRL, (pPresenter != null), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
108
109         pView = new (std::nothrow) _DateTimePicker(pPresenter, title);
110         SysTryCatch(NID_UI_CTRL, (pView != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
111
112         r = pView->InitializeFont();
113         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to set the Font.");
114
115         r = pView->CreateRootVisualElement();
116         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
117
118         orientation = pView->GetOrientation();
119         screenSize = _ControlManager::GetInstance()->GetScreenSizeF();
120         pickerSize = screenSize;
121         if (orientation == _CONTROL_ORIENTATION_LANDSCAPE)
122         {
123                 pickerSize.width = screenSize.height;
124                 pickerSize.height = screenSize.width;
125         }
126
127         r = pView->SetSize(pickerSize);
128         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , E_SYSTEM,
129                                 "[E_SYSTEM] A system error has occurred. Failed to set the size for this control");
130
131         r = pView->CreateFooter();
132         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , E_SYSTEM,
133                                         "[E_SYSTEM] A system error has occurred. Failed to create the footer for this control");
134
135         r = pView->CreateDisplayVisualElement();
136         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , E_SYSTEM,
137                                         "[E_SYSTEM] A system error has occurred. Failed to create the display visual element for this control");
138
139         r = pPresenter->Construct(*pView);
140         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , E_SYSTEM,
141                                         "[E_SYSTEM] A system error has occurred. Failed to construct the presenter instance for this control");
142
143         pView->AcquireHandle();
144
145         pView->__pDateTimeChangeEvent = new (std::nothrow) _DateTimeChangeEvent(*pView);
146         SysTryCatch(NID_UI_CTRL, (pView->__pDateTimeChangeEvent != null), , E_OUT_OF_MEMORY,
147                            "[E_OUT_OF_MEMORY] Memory allocation failed.");
148
149         if (pView->GetAccessibilityContainer() != null)
150         {
151                 pView->GetAccessibilityContainer()->Activate(true);
152         }
153
154         return pView;
155
156 CATCH:
157         delete pPresenter;
158         delete pView;
159
160         return null;
161 }
162
163 result
164 _DateTimePicker::CreateFooter(void)
165 {
166         result r = E_SUCCESS;
167         _Button* pSaveButton = null;
168         _Button* pCancelButton = null;
169         FloatRectangle bounds;
170         String text;
171         _ControlOrientation orientation = GetOrientation();
172
173         _Toolbar* pFooter = _Toolbar::CreateToolbarN(false);
174         r = GetLastResult();
175         SysTryReturnResult(NID_UI_CTRL, (pFooter != null), r, "[%s] Propagating.", GetErrorMessage(r));
176
177         r = AttachSystemChild(*pFooter);
178         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
179
180         GET_SHAPE_CONFIG(DATETIMEPICKER::FOOTER_HEIGHT, orientation, bounds.height);
181         GET_SHAPE_CONFIG(DATETIMEPICKER::INPUTPAD_HEIGHT, orientation, bounds.y);
182
183         bounds.x = 0.0f;
184         bounds.y = GetBoundsF().height - bounds.y - bounds.height;
185         bounds.width = GetBoundsF().width;
186
187         pFooter->SetMovable(true);
188         pFooter->SetResizable(true);
189
190         r = pFooter->SetBounds(bounds);
191         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
192
193         pFooter->SetMovable(false);
194         pFooter->SetResizable(false);
195
196         pFooter->AddActionEventListener(*this);
197
198         r = pFooter->SetStyle(TOOLBAR_TEXT);
199         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
200
201         // Create 'Save' button
202         pSaveButton = _Button::CreateButtonN();
203         r = GetLastResult();
204         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
205
206         r = pSaveButton->SetActionId(DATETIME_EVENT_ID_SAVE);
207         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
208
209         GET_STRING_CONFIG(IDS_COM_SK_SAVE, text);
210
211         r = pSaveButton->SetText(text);
212         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
213
214         // Create 'Cancel' button
215         pCancelButton = _Button::CreateButtonN();
216         r = GetLastResult();
217         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
218
219         r = pCancelButton->SetActionId(DATETIME_EVENT_ID_CANCEL);
220         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
221
222         GET_STRING_CONFIG(IDS_COM_POP_CANCEL, text);
223
224         r = pCancelButton->SetText(text);
225         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
226
227         r = pFooter->AddItem(pSaveButton);
228         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
229
230         r = pFooter->AddItem(pCancelButton);
231         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.");
232
233         if (__pFooter != null)
234         {
235                 DetachSystemChild(*__pFooter);
236
237                 delete __pFooter;
238                 __pFooter = null;
239         }
240
241         __pFooter = pFooter;
242
243         return E_SUCCESS;
244 CATCH:
245         DetachSystemChild(*pFooter);
246
247         delete pFooter;
248         delete pSaveButton;
249         delete pCancelButton;
250
251         return r;
252 }
253
254 result
255 _DateTimePicker::DestroyFooter(void)
256 {
257         if (__pFooter != null)
258         {
259                 DetachSystemChild(*__pFooter);
260
261                 delete __pFooter;
262                 __pFooter = null;
263         }
264
265         return E_SUCCESS;
266 }
267
268 result
269 _DateTimePicker::CreateDisplayVisualElement(void)
270 {
271         result r = E_SUCCESS;
272
273         __pDisplayVisualElement = new (std::nothrow) _VisualElement();
274         SysTryReturnResult(NID_UI_CTRL, __pDisplayVisualElement, E_OUT_OF_MEMORY, "Memory allocation failed.");
275
276         r = __pDisplayVisualElement->Construct();
277         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
278
279         __pDisplayVisualElement->SetShowState(true);
280         __pDisplayVisualElement->SetImplicitAnimationEnabled(false);
281
282         r = GetVisualElement()->AttachChild(*__pDisplayVisualElement);
283         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
284
285         return r;
286
287 CATCH:
288         __pDisplayVisualElement->Destroy();
289         __pDisplayVisualElement = null;
290
291         return r;
292 }
293
294 void
295 _DateTimePicker::SetDisplayVisualElementBounds(FloatRectangle bounds)
296 {
297         if (__pDisplayVisualElement != null)
298         {
299                 __pDisplayVisualElement->SetBounds(FloatRectangle(bounds.x, bounds.y, bounds.width, bounds.height));
300         }
301
302         return;
303 }
304
305 _VisualElement*
306 _DateTimePicker::GetDisplayVisualElement(void)
307 {
308         return __pDisplayVisualElement;
309 }
310
311 result
312 _DateTimePicker::AddDateTimeChangeEventListener(const Controls::_IDateTimeChangeEventListener& listener)
313 {
314         SysTryReturnResult(NID_UI_CTRL, (__pDateTimeChangeEvent != null), E_SYSTEM,
315                                            "A system error has occurred. The _DateTimeChangeEvent instance is null.");
316
317         result r = __pDateTimeChangeEvent->AddListener(listener);
318         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
319
320         return r;
321 }
322
323 result
324 _DateTimePicker::RemoveDateTimeChangeEventListener(const Controls::_IDateTimeChangeEventListener& listener)
325 {
326         SysTryReturnResult(NID_UI_CTRL, (__pDateTimeChangeEvent != null), E_SYSTEM,
327                                            "A system error has occurred. The _DateTimeChangeEvent instance is null.");
328
329         result r = __pDateTimeChangeEvent->RemoveListener(listener);
330         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
331
332         return r;
333 }
334
335 result
336 _DateTimePicker::FireDateTimeChangeEvent(_DateTimeChangeStatus status, DateTime& dateTime)
337 {
338         SysTryReturnResult(NID_UI_CTRL, (__pDateTimeChangeEvent != null), E_SYSTEM,
339                                            "A system error has occurred. The _DateTimeChangeEvent instance is null");
340
341         SysTryReturnResult(NID_UI_CTRL, (status >= DATE_INTERNAL_CHANGE_SAVED && status <= TIME_INTERNAL_CHANGE_CANCELED), E_INVALID_ARG,
342                                            "Invalid argument(s) is used. The status provided is not present in the _DateTimeChangeStatus list.");
343
344         _DateTimeChangeEventArg* pDateTimeEventArg = new (std::nothrow) _DateTimeChangeEventArg(status);
345         SysTryReturnResult(NID_UI_CTRL, (pDateTimeEventArg != null), E_OUT_OF_MEMORY, "Memory allocation failed.");
346
347         pDateTimeEventArg->SetDateTime(dateTime);
348         pDateTimeEventArg->SetDate(dateTime.GetYear(), dateTime.GetMonth(), dateTime.GetDay());
349         pDateTimeEventArg->SetTime(dateTime.GetHour(), dateTime.GetMinute());
350         __pDateTimeChangeEvent->Fire(*pDateTimeEventArg);
351
352         return E_SUCCESS;
353 }
354
355 result
356 _DateTimePicker::SetPropertyYear(const Variant& year)
357 {
358         int yearValue = year.ToInt();
359
360         SysTryReturnResult(NID_UI_CTRL, (yearValue >= DATETIME_YEAR_MIN && yearValue <= DATETIME_YEAR_MAX), E_INVALID_ARG,
361                                            "Invalid argument(s) is used. The year (%d) must be greater than or equal to (%d) and less than or equal to (%d).", yearValue, DATETIME_YEAR_MIN, DATETIME_YEAR_MAX);
362
363         int day;
364         int maxValue = -1;
365
366         _DateTimeUtils dateTimeUtils;
367
368         maxValue = dateTimeUtils.CalculateMaxDay(yearValue, GetMonth());
369         day = __pPresenter->GetDay();
370
371         if (day > maxValue)
372         {
373                 __pPresenter->SetDay(maxValue);
374         }
375
376         return __pPresenter->SetYear(yearValue);
377 }
378
379 result
380 _DateTimePicker::SetYear(int year)
381 {
382         return SetProperty("year", Variant(year));
383 }
384
385 Variant
386 _DateTimePicker::GetPropertyYear(void) const
387 {
388         int year = -1;
389
390         year = __pPresenter->GetYear();
391
392         return Variant(year);
393 }
394
395 int
396 _DateTimePicker::GetYear(void) const
397 {
398         Variant year = GetProperty("year");
399
400         return year.ToInt();
401 }
402
403 result
404 _DateTimePicker::SetPropertyMonth(const Variant& month)
405 {
406         int monthValue = month.ToInt();
407
408         SysTryReturnResult(NID_UI_CTRL, (monthValue >= DATETIME_MONTH_MIN && monthValue <= DATETIME_MONTH_MAX), E_INVALID_ARG,
409                                            "Invalid argument(s) is used. The month (%d) must be greater than or equal to (%d) and less than or equal to (%d).", monthValue, DATETIME_MONTH_MIN, DATETIME_MONTH_MAX);
410
411         int day;
412         int maxValue = -1;
413
414         _DateTimeUtils dateTimeUtils;
415
416         maxValue = dateTimeUtils.CalculateMaxDay(GetYear(), monthValue);
417         day = __pPresenter->GetDay();
418
419         if (day > maxValue)
420         {
421                 __pPresenter->SetDay(maxValue);
422         }
423
424         return __pPresenter->SetMonth(monthValue);
425 }
426
427 result
428 _DateTimePicker::SetMonth(int month)
429 {
430         return SetProperty("month", Variant(month));
431 }
432
433 Variant
434 _DateTimePicker::GetPropertyMonth(void) const
435 {
436         int month = -1;
437
438         month = __pPresenter->GetMonth();
439
440         return Variant(month);
441 }
442
443 int
444 _DateTimePicker::GetMonth(void) const
445 {
446         Variant month = GetProperty("month");
447
448         return month.ToInt();
449 }
450
451 result
452 _DateTimePicker::SetPropertyDay(const Variant& day)
453 {
454         int dayValue = day.ToInt();
455
456         SysTryReturnResult(NID_UI_CTRL, (dayValue >= DATETIME_DAY_MIN && dayValue <= DATETIME_DAY_MAX), E_INVALID_ARG,
457                                            "Invalid argument(s) is used. The day (%d) must be greater than or equal to (%d) and less than or equal to (%d).", dayValue, DATETIME_DAY_MIN, DATETIME_DAY_MAX);
458
459         int maxValue = -1;
460         _DateTimeUtils dateTimeUtils;
461         maxValue = dateTimeUtils.CalculateMaxDay(GetYear(), GetMonth());
462
463         SysTryReturn(NID_UI_CTRL, (dayValue <= maxValue), E_INVALID_ARG, E_INVALID_ARG,
464                         "[E_INVALID_ARG] Invalid argument is used. day (%d), month (%d)", dayValue, GetMonth());
465
466         return __pPresenter->SetDay(dayValue);
467 }
468
469 result
470 _DateTimePicker::SetDay(int day)
471 {
472         return SetProperty("day", Variant(day));
473 }
474
475 Variant
476 _DateTimePicker::GetPropertyDay(void) const
477 {
478         int day = -1;
479
480         day = __pPresenter->GetDay();
481
482         return Variant(day);
483 }
484
485 int
486 _DateTimePicker::GetDay(void) const
487 {
488         Variant day = GetProperty("day");
489
490         return day.ToInt();
491 }
492
493 result
494 _DateTimePicker::SetPropertyHour(const Variant& hour)
495 {
496         int hourValue = hour.ToInt();
497
498         SysTryReturnResult(NID_UI_CTRL, (hourValue >= DATETIME_HOUR_MIN && hourValue <= DATETIME_HOUR_MAX), E_INVALID_ARG,
499                                            "Invalid argument(s) is used. The hour (%d) must be greater than or equal to (%d) and less than or equal to (%d).", hourValue, DATETIME_HOUR_MIN, DATETIME_HOUR_MAX);
500
501         return __pPresenter->SetHour(hourValue);
502
503 }
504
505 result
506 _DateTimePicker::SetHour(int hour)
507 {
508         return SetProperty("hour", Variant(hour));
509 }
510
511 Variant
512 _DateTimePicker::GetPropertyHour(void) const
513 {
514         int hour = -1;
515
516         hour = __pPresenter->GetHour();
517
518         return Variant(hour);
519 }
520
521 int
522 _DateTimePicker::GetHour(void) const
523 {
524         Variant hour = GetProperty("hour");
525
526         return hour.ToInt();
527 }
528
529 result
530 _DateTimePicker::SetPropertyMinute(const Variant& minute)
531 {
532         int minuteValue = minute.ToInt();
533
534         SysTryReturnResult(NID_UI_CTRL, (minuteValue >= DATETIME_MINUTE_MIN && minuteValue <= DATETIME_MINUTE_MAX), E_INVALID_ARG,
535                                            "Invalid argument(s) is used. The minute (%d) must be greater than or equal to (%d) and less than or equal to (%d).", minuteValue, DATETIME_MINUTE_MIN, DATETIME_MINUTE_MAX);
536
537         return __pPresenter->SetMinute(minuteValue);
538
539 }
540
541 result
542 _DateTimePicker::SetMinute(int minute)
543 {
544         return SetProperty("minute", Variant(minute));
545 }
546
547 Variant
548 _DateTimePicker::GetPropertyMinute(void) const
549 {
550         int minute = -1;
551
552         minute = __pPresenter->GetMinute();
553
554         return Variant(minute);
555 }
556
557 int
558 _DateTimePicker::GetMinute(void) const
559 {
560         Variant minute = GetProperty("minute");
561
562         return minute.ToInt();
563 }
564
565 result
566 _DateTimePicker::SetProperty24HourNotation(const Variant& enable)
567 {
568         bool enableValue = enable.ToBool();
569
570         __pPresenter->Set24HourNotationEnabled(enableValue);
571
572         return E_SUCCESS;
573 }
574
575 void
576 _DateTimePicker::Set24HourNotationEnabled(bool enable)
577 {
578         SetProperty("24hourNotation", Variant(enable));
579
580         return;
581 }
582
583 Variant
584 _DateTimePicker::GetProperty24HourNotation(void) const
585 {
586         bool is24HourNotation = false;
587
588         is24HourNotation = __pPresenter->Is24HourNotationEnabled();
589
590         return Variant(is24HourNotation);
591 }
592
593 bool
594 _DateTimePicker::Is24HourNotationEnabled(void) const
595 {
596         Variant enable = GetProperty("24hourNotation");
597
598         return enable.ToBool();
599 }
600
601 result
602 _DateTimePicker::SetPropertyMinYearRange(const Variant& minYear)
603 {
604         int minYearValue = minYear.ToInt();
605         int currentMinYear = DATETIME_YEAR_MIN;
606         int currentMaxYear = DATETIME_YEAR_MAX;
607
608         SysTryReturnResult(NID_UI_CTRL, (minYearValue >= DATETIME_YEAR_MIN && minYearValue <= DATETIME_YEAR_MAX), E_INVALID_ARG,
609                                            "Invalid argument(s) is used. The min year (%d) must be greater than or equal to (%d) and less than or equal to (%d).", minYearValue, DATETIME_YEAR_MIN, DATETIME_YEAR_MAX);
610
611         __pPresenter->GetYearRange(currentMinYear, currentMaxYear);
612
613         SysTryReturnResult(NID_UI_CTRL, (currentMaxYear >= minYearValue), E_INVALID_ARG,
614                                            "Invalid argument(s) is used. The year (%d) must be less than or equal to (%d).", minYearValue, currentMaxYear);
615
616         return __pPresenter->SetYearRange(minYearValue, currentMaxYear);
617 }
618
619 Variant
620 _DateTimePicker::GetPropertyMinYearRange(void) const
621 {
622         int currentMinYear = DATETIME_YEAR_MIN;
623         int currentMaxYear = DATETIME_YEAR_MAX;
624
625         __pPresenter->GetYearRange(currentMinYear, currentMaxYear);
626
627         return Variant(currentMinYear);
628 }
629
630 result
631 _DateTimePicker::SetPropertyMaxYearRange(const Variant& maxYear)
632 {
633         int maxYearValue = maxYear.ToInt();
634         int currentMinYear = DATETIME_YEAR_MIN;
635         int currentMaxYear = DATETIME_YEAR_MAX;
636
637         SysTryReturnResult(NID_UI_CTRL, (maxYearValue >= DATETIME_YEAR_MIN && maxYearValue <= DATETIME_YEAR_MAX), E_INVALID_ARG,
638                                            "Invalid argument(s) is used. The year (%d) must be greater than or equal to (%d) and less than or equal to (%d).", maxYearValue, DATETIME_YEAR_MIN, DATETIME_YEAR_MAX);
639
640         __pPresenter->GetYearRange(currentMinYear, currentMaxYear);
641
642         SysTryReturnResult(NID_UI_CTRL, (maxYearValue >= currentMinYear), E_INVALID_ARG,
643                                            "Invalid argument(s) is used. The year (%d) must be greater than or equal to (%d).", maxYearValue, currentMinYear);
644
645         return __pPresenter->SetYearRange(currentMinYear, maxYearValue);
646 }
647
648 Variant
649 _DateTimePicker::GetPropertyMaxYearRange(void) const
650 {
651         int currentMinYear = DATETIME_YEAR_MIN;
652         int currentMaxYear = DATETIME_YEAR_MAX;
653
654         __pPresenter->GetYearRange(currentMinYear, currentMaxYear);
655
656         return  Variant(currentMaxYear);
657 }
658
659 result
660 _DateTimePicker::SetYearRange(int minYear, int maxYear)
661 {
662         result r = E_SUCCESS;
663
664         SysTryReturnResult(NID_UI_CTRL, (minYear >= DATETIME_YEAR_MIN && minYear <= DATETIME_YEAR_MAX), E_INVALID_ARG,
665                                            "Invalid argument(s) is used. The minYear (%d) must be greater than or equal to (%d) and less than or equal to (%d).", minYear, DATETIME_YEAR_MIN, DATETIME_YEAR_MAX);
666         SysTryReturnResult(NID_UI_CTRL, (maxYear >= DATETIME_YEAR_MIN && maxYear <= DATETIME_YEAR_MAX), E_INVALID_ARG,
667                                            "Invalid argument(s) is used. The maxYear (%d) must be greater than or equal to (%d) and less than or equal to (%d).", maxYear, DATETIME_YEAR_MIN, DATETIME_YEAR_MAX);
668         SysTryReturnResult(NID_UI_CTRL, (maxYear >= minYear), E_INVALID_ARG,
669                                            "Invalid argument(s) is used. The maxYear (%d) must be greater than or equal to minYear (%d).", maxYear, minYear);
670
671         r = SetProperty("minYearRange", Variant(minYear));
672         if (r != E_SUCCESS )
673         {
674                 return r;
675         }
676
677         r = SetProperty("maxYearRange", Variant(maxYear));
678
679         return r;
680 }
681
682 result
683 _DateTimePicker::GetYearRange(int& minYear, int& maxYear) const
684 {
685         Variant currentMinYear = GetProperty("minYearRange");
686         Variant currentMaxYear = GetProperty("maxYearRange");
687
688         minYear = currentMinYear.ToInt();
689         maxYear = currentMaxYear.ToInt();
690
691         return E_SUCCESS;
692 }
693
694 result
695 _DateTimePicker::SetPropertyDateTime(const Variant& varDateTime)
696 {
697         DateTime dateTime = varDateTime.ToDateTime();
698
699         SysTryReturnResult(NID_UI_CTRL, (dateTime.GetYear() >= DATETIME_YEAR_MIN && dateTime.GetYear() <= DATETIME_YEAR_MAX), E_INVALID_ARG,
700                         "[E_INVALID_ARG] Invalid argument is used. year (%d).", dateTime.GetYear());
701
702         int minYear = -1;
703         int maxYear = -1;
704
705         GetYearRange(minYear, maxYear);
706
707         DateTime date;
708
709         if (dateTime.GetYear() < minYear)
710         {
711                 date.SetValue(minYear, dateTime.GetMonth(), dateTime.GetDay(), dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
712         }
713         else if (dateTime.GetYear() > maxYear)
714         {
715                 date.SetValue(maxYear, dateTime.GetMonth(), dateTime.GetDay(), dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
716         }
717         else
718         {
719                 date.SetValue(dateTime);
720         }
721
722         __pPresenter->SetDateTime(date);
723
724         return E_SUCCESS;
725 }
726
727 void
728 _DateTimePicker::SetDateTime(const DateTime& dateTime)
729 {
730         SetProperty("dateTime", Variant(dateTime));
731
732         return;
733 }
734
735 Variant
736 _DateTimePicker::GetPropertyDateTime(void) const
737 {
738         DateTime dateTime = DateTime();
739
740         dateTime = __pPresenter->GetDateTime();
741
742         return Variant(dateTime);
743 }
744
745 DateTime
746 _DateTimePicker::GetDateTime(void) const
747 {
748         Variant dateTime = GetProperty("dateTime");
749
750         return dateTime.ToDateTime();
751 }
752
753 void
754 _DateTimePicker::SetCurrentDateTime(void)
755 {
756         __pPresenter->SetCurrentDateTime();
757
758         return;
759 }
760
761 void
762 _DateTimePicker::SetFocusBoxId(int boxId)
763 {
764         __pPresenter->SetFocusBoxId(boxId);
765
766         return;
767 }
768
769 void
770 _DateTimePicker::SetAccessibilityElementText(int index, const String& text, bool isAmPm)
771 {
772         _AccessibilityElement* pElement = null;
773         String hintAmPmText(L"Double tap to change to ");
774
775         if (__accessibilityElements.GetAt(index, pElement) == E_SUCCESS)
776         {
777                 result r = GetLastResult();
778                 SysTryReturnVoidResult(NID_UI_CTRL, (pElement != null), r, "[%s] Propagating.", GetErrorMessage(r));
779
780                 pElement->SetLabel(text);
781
782                 if (isAmPm)
783                 {
784                         String amText;
785                         String pmText;
786
787                         _DateTimeUtils dateTimeUtils;
788                         dateTimeUtils.GetAmPm(amText, AM_TYPE);
789                         dateTimeUtils.GetAmPm(pmText, PM_TYPE);
790
791                         if (text == amText)
792                         {
793                                 hintAmPmText.Append(pmText);
794                         }
795                         else
796                         {
797                                 hintAmPmText.Append(amText);
798                         }
799
800                         pElement->SetHint(hintAmPmText);
801                 }
802         }
803
804         SetLastResult(E_SUCCESS);
805
806         return;
807 }
808
809 bool
810 _DateTimePicker::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
811 {
812         return __pPresenter->OnTouchPressed(source, touchinfo);
813 }
814
815 bool
816 _DateTimePicker::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
817 {
818         return __pPresenter->OnTouchMoved(source, touchinfo);
819 }
820
821 bool
822 _DateTimePicker::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
823 {
824         return __pPresenter->OnTouchReleased(source, touchinfo);
825 }
826
827 bool
828 _DateTimePicker::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
829 {
830         return __pPresenter->OnTouchCanceled(source, touchinfo);
831 }
832
833 result
834 _DateTimePicker::OnAttachedToMainTree(void)
835 {
836         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
837         if (pContainer != null)
838         {
839                 float dateTimeDisplayBoxHeight = 0.0f;
840                 float amPmDisplayBoxHeight = 0.0f;
841                 float topMargin = 0.0f;
842                 float leftMargin = 0.0f;
843                 const _DateTimeDisplayBox* pBox = null;
844                 const _DateTimeDisplayBox* pAmPmBox = null;
845
846                 GET_SHAPE_CONFIG(DATETIMEPICKER::LEFT_MARGIN, GetOrientation(), leftMargin);
847
848                 pContainer->AddListener(*this);
849
850                 // Display Box
851                 for (int index = 0; index < DATETIME_ID_MAX; index++)
852                 {
853                         pBox = __pPresenter->GetDisplayBox(index);
854                         if (pBox != null)
855                         {
856                                 _AccessibilityElement* pElement = new (std::nothrow) _AccessibilityElement(true);
857                                 SysTryReturnResult(NID_UI_CTRL, (pElement != null), E_OUT_OF_MEMORY, "Memory allocation failed.");
858
859                                 FloatRectangle displayBoxBounds = pBox->GetDisplayBoxBounds();
860                                 int displayBoxId = pBox->GetDisplayBoxId();
861
862                                 String hintText(L"Double tap to edit");
863
864                                 switch (displayBoxId)
865                                 {
866                                 case DATETIME_ID_DAY:
867                                         pElement->SetTrait(ACCESSIBILITY_TRAITS_DAY);
868                                         pElement->SetHint(hintText);
869                                         break;
870                                 case DATETIME_ID_MONTH:
871                                         pElement->SetTrait(ACCESSIBILITY_TRAITS_MONTH);
872                                         pElement->SetHint(hintText);
873                                         break;
874                                 case DATETIME_ID_YEAR:
875                                         pElement->SetTrait(ACCESSIBILITY_TRAITS_YEAR);
876                                         pElement->SetHint(hintText);
877                                         break;
878                                 case DATETIME_ID_HOUR:
879                                         pElement->SetTrait(ACCESSIBILITY_TRAITS_HOUR);
880                                         pElement->SetHint(hintText);
881                                         break;
882                                 case DATETIME_ID_MINUTE:
883                                         pElement->SetTrait(ACCESSIBILITY_TRAITS_MINUTE);
884                                         pElement->SetHint(hintText);
885                                         break;
886                                 }
887
888                                 GET_SHAPE_CONFIG(DATETIMEPICKER::DATETIME_DISPLAY_BOX_HEIGHT, GetOrientation(), dateTimeDisplayBoxHeight);
889                                 GET_SHAPE_CONFIG(DATETIMEPICKER::AMPM_DISPLAY_BOX_HEIGHT, GetOrientation(), amPmDisplayBoxHeight);
890                                 topMargin = (amPmDisplayBoxHeight - dateTimeDisplayBoxHeight) / 2.0f;
891
892                                 displayBoxBounds.x += leftMargin;
893                                 displayBoxBounds.y -= topMargin;
894                                 displayBoxBounds.height += 2 * topMargin;
895
896                                 if (__pDisplayVisualElement)
897                                 {
898                                         displayBoxBounds.y += __pDisplayVisualElement->GetBounds().y;
899                                 }
900
901                                 pElement->SetLabel(pBox->GetText());
902                                 pElement->SetBounds(displayBoxBounds);
903                                 pContainer->AddElement(*pElement);
904                         }
905                 }
906
907                 // AmPm Button
908                 String hintAmPmText(L"Double tap to change");
909
910                 pAmPmBox = __pPresenter->GetAmPmBox();
911                 if (pAmPmBox != null)
912                 {
913                         _AccessibilityElement* pElement = new (std::nothrow) _AccessibilityElement(true);
914                         SysTryReturnResult(NID_UI_CTRL, (pElement != null), E_OUT_OF_MEMORY, "Memory allocation failed.");
915
916                         FloatRectangle amPmBoxBounds = pAmPmBox->GetDisplayBoxBounds();
917
918                         amPmBoxBounds.x += leftMargin;
919
920                         if (__pDisplayVisualElement)
921                         {
922                                 amPmBoxBounds.y += __pDisplayVisualElement->GetBounds().y;
923                         }
924
925                         pElement->SetLabel(pAmPmBox->GetText());
926                         pElement->SetBounds(amPmBoxBounds);
927                         pElement->SetTrait(ACCESSIBILITY_TRAITS_BUTTON);
928                         pElement->SetHint(hintAmPmText);
929                         pContainer->AddElement(*pElement);
930                 }
931
932                 pContainer->GetElements(__accessibilityElements);
933                 _AccessibilityManager::GetInstance()->RequestAutoReading(_ACCESSIBILITY_AUTO_READING_MODE_FIRST_ITEM);
934
935         }
936
937         return E_SUCCESS;
938 }
939
940 result
941 _DateTimePicker::OnDetachingFromMainTree(void)
942 {
943         RemoveAllAccessibilityElement();
944
945         return _Window::OnDetachingFromMainTree();
946 }
947
948 void
949 _DateTimePicker::OnChangeLayout(_ControlOrientation orientation)
950 {
951         result r = E_SUCCESS;
952         FloatDimension pickerSize(0.0f, 0.0f);
953         FloatDimension screenSize(0.0f, 0.0f);
954
955         screenSize = _ControlManager::GetInstance()->GetScreenSizeF();
956         pickerSize = screenSize;
957         if (orientation == _CONTROL_ORIENTATION_LANDSCAPE)
958         {
959                 pickerSize.width = screenSize.height;
960                 pickerSize.height = screenSize.width;
961         }
962
963         SetMovable(true);
964         SetResizable(true);
965
966         SetSize(pickerSize);
967
968         SetMovable(false);
969         SetResizable(false);
970
971         r = DestroyFooter();
972         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), E_SYSTEM,
973                                                   "[E_SYSTEM] A system error has occurred. Failed to destroy the footer for this control.");
974
975         r = CreateFooter();
976         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), E_SYSTEM,
977                                                   "[E_SYSTEM] A system error has occurred. Failed to create the footer for this control.");
978
979         __pPresenter->OnChangeLayout(orientation);
980
981         const _DateTimeDisplayBox* pBox = null;
982         IEnumeratorT<_AccessibilityElement*>* pEnumerator = __accessibilityElements.GetEnumeratorN();
983         SysTryReturnVoidResult(NID_UI_CTRL, (pEnumerator != null), GetLastResult(),
984                         "[%s] Propagating.", GetErrorMessage(GetLastResult()));
985
986         _AccessibilityElement* pElement = null;
987         int index = 0;
988         while (pEnumerator->MoveNext() == E_SUCCESS)
989         {
990                 if (pEnumerator->GetCurrent(pElement) == E_SUCCESS)
991                 {
992                         pBox = __pPresenter->GetDisplayBox(index);
993                         if (pBox != null)
994                         {
995                                 FloatRectangle displayBoxBounds = pBox->GetDisplayBoxBounds();
996
997                                 if (__pDisplayVisualElement)
998                                 {
999                                         displayBoxBounds.y += __pDisplayVisualElement->GetBounds().y;
1000                                 }
1001
1002                                 pElement->SetBounds(displayBoxBounds);
1003                         }
1004                 }
1005                 index++;
1006         }
1007         delete pEnumerator;
1008
1009         return;
1010 }
1011
1012 void
1013 _DateTimePicker::OnDraw(void)
1014 {
1015         __pPresenter->Draw();
1016
1017         return;
1018 }
1019
1020 void
1021 _DateTimePicker::OnActionPerformed(const _Control& source, int actionId)
1022 {
1023         __pPresenter->OnActionPerformed(source, actionId);
1024
1025         return;
1026 }
1027
1028 bool
1029 _DateTimePicker::OnAccessibilityActionPerformed(const _AccessibilityContainer& control, const _AccessibilityElement& element)
1030 {
1031         String amString = L"";
1032         String pmString = L"";
1033         String label = L"";
1034
1035         label = element.GetLabel();
1036
1037         _DateTimeUtils dateTimeUtils;
1038         dateTimeUtils.GetAmPm(amString, AM_TYPE);
1039         dateTimeUtils.GetAmPm(pmString, PM_TYPE);
1040
1041         if (label == amString || label == pmString)
1042         {
1043                 label.Append(L" Selected");
1044                 _AccessibilityManager::GetInstance()->ReadContent(label);
1045         }
1046
1047         return true;
1048 }
1049
1050 void
1051 _DateTimePicker::OnFontChanged(Font* pFont)
1052 {
1053         result r = E_SUCCESS;
1054
1055         if (pFont != null)
1056         {
1057                 if (__pPresenter != null)
1058                 {
1059                         r = __pPresenter->SetFont(pFont);
1060                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.");
1061                 }
1062
1063                 if (__pFooter != null)
1064                 {
1065                         for (int i=0; i < __pFooter->GetItemCount(); i++)
1066                         {
1067                                 _Button* pButton = __pFooter->GetItem(i);
1068
1069                                 if (pButton != null)
1070                                 {
1071                                         r = pButton->SetFont(pFont->GetFaceName());
1072                                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
1073                                 }
1074                         }
1075                 }
1076
1077                 __pFont = pFont;
1078         }
1079
1080         return;
1081 }
1082
1083 void
1084 _DateTimePicker::OnFontInfoRequested(unsigned long& style, float& size)
1085 {
1086         style = FONT_STYLE_PLAIN;
1087
1088         GET_SHAPE_CONFIG(DATETIMEPICKER::FONT_SIZE, GetOrientation(), size);
1089
1090         return;
1091 }
1092
1093 Font*
1094 _DateTimePicker::GetDateTimeFont(void)
1095 {
1096         return __pFont;
1097 }
1098
1099 result
1100 _DateTimePicker::InitializeFont(void)
1101 {
1102         result r = E_SUCCESS;
1103
1104         __pFont = GetFallbackFont();
1105         r = GetLastResult();
1106         SysTryReturnResult(NID_UI_CTRL, (__pFont != null), r, "[%s] Propagating.", GetErrorMessage(r));
1107
1108         return r;
1109 }
1110
1111 void
1112 _DateTimePicker::RemoveAllAccessibilityElement(void)
1113 {
1114         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
1115         SysTryReturnVoidResult(NID_UI_CTRL, (pContainer != null), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1116
1117         _AccessibilityElement* pAccessibilityElement = null;
1118
1119         while (__accessibilityElements.GetCount() > 0)
1120         {
1121                 if ((__accessibilityElements.GetAt(0, pAccessibilityElement)) == E_SUCCESS)
1122                 {
1123                         __accessibilityElements.RemoveAt(0);
1124                         pContainer->RemoveElement(*pAccessibilityElement);
1125                 }
1126                 else
1127                 {
1128                         __accessibilityElements.RemoveAt(0);
1129                 }
1130         }
1131 }
1132
1133 }}} // Tizen::Ui::Controls