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