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