ac5fe30fc3097cf07fa9c0a763676c6d441e1d38
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_EditDate.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_EditDate.cpp
20  * @brief               This is the implementation file for the _EditDate class.
21  */
22
23 #include <vconf.h>
24 #include <FSysSettingInfo.h>
25 #include "FUi_AccessibilityContainer.h"
26 #include "FUi_AccessibilityElement.h"
27 #include "FUi_ResourceManager.h"
28 #include "FUiAnim_VisualElement.h"
29 #include "FUiCtrl_EditDate.h"
30 #include "FUiCtrl_EditDatePresenter.h"
31 #include "FUiCtrl_Form.h"
32 #include "FUiCtrl_Frame.h"
33 #include "FUiCtrl_DateTimeUtils.h"
34 #include "FUi_CoordinateSystemUtils.h"
35
36 using namespace Tizen::Graphics;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Ui;
40 using namespace Tizen::System;
41
42 namespace Tizen { namespace Ui { namespace Controls
43 {
44
45 IMPLEMENT_PROPERTY(_EditDate);
46
47 _EditDate::_EditDate(void)
48         : __pEditDatePresenter(null)
49         , __pDateChangeEvent(null)
50         , __pDateTimeBar(null)
51         , __absoluteBounds(FloatRectangle())
52         , __previousSize(0.0f, 0.0f)
53         , __title()
54         , __pAccessibilityEditDateElement(null)
55         , __pAccessibilityYearElement(null)
56         , __pAccessibilityMonthElement(null)
57         , __pAccessibilityDayElement(null)
58         , __isCalledBoundsChanged(false)
59         , __isXmlBoundsExist(false)
60 {
61 }
62
63 _EditDate::~_EditDate(void)
64 {
65         SettingInfo::RemoveSettingEventListener(*this);
66
67         delete __pDateTimeBar;
68         __pDateTimeBar = null;
69
70         delete __pEditDatePresenter;
71         __pEditDatePresenter = null;
72
73         if (__pDateChangeEvent != null)
74         {
75                 delete __pDateChangeEvent;
76                 __pDateChangeEvent = null;
77         }
78
79         if (__pAccessibilityEditDateElement)
80         {
81                 __pAccessibilityEditDateElement->Activate(false);
82                 __pAccessibilityEditDateElement = null;
83         }
84         if (__pAccessibilityYearElement)
85         {
86                 __pAccessibilityYearElement->Activate(false);
87                 __pAccessibilityYearElement = null;
88         }
89         if (__pAccessibilityMonthElement)
90         {
91                 __pAccessibilityMonthElement->Activate(false);
92                 __pAccessibilityMonthElement = null;
93         }
94         if (__pAccessibilityDayElement)
95         {
96                 __pAccessibilityDayElement->Activate(false);
97                 __pAccessibilityDayElement = null;
98         }
99 }
100
101 _EditDate*
102 _EditDate::CreateEditDateN(const String& title)
103 {
104         result r = E_SUCCESS;
105
106         _AccessibilityContainer* pContainer = null;
107
108         _EditDate* pEditDate = new (std::nothrow) _EditDate;
109         SysTryReturn(NID_UI_CTRL, pEditDate, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
110
111         pEditDate->GetVisualElement()->SetSurfaceOpaque(false);
112
113         pEditDate->__pEditDatePresenter = _EditDatePresenter::CreateInstanceN(*pEditDate, title);
114
115         r = pEditDate->CreateDateTimeBar();
116         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
117
118         pEditDate->__pEditDatePresenter->Initialize();
119         r = GetLastResult();
120         SysTryCatch(NID_UI_CTRL, pEditDate->__pEditDatePresenter, , r, "[%s] Propagating.", GetErrorMessage(r));
121
122         pEditDate->__pEditDatePresenter->SetCurrentDate();
123
124         if (title.IsEmpty() != true)
125         {
126                 pEditDate->__title = title;
127         }
128
129         pContainer = pEditDate->GetAccessibilityContainer();
130
131         if (pContainer)
132         {
133                 pContainer->Activate(true);
134                 pEditDate->CreateAccessibilityElement();
135         }
136
137         pEditDate->AcquireHandle();
138         pEditDate->SetTouchPressThreshold(_TOUCH_PRESS_THRESHOLD_INSENSITIVE);
139
140         return pEditDate;
141
142 CATCH:
143         delete pEditDate;
144         return null;
145 }
146
147 result
148 _EditDate::CreateDateTimeBar(void)
149 {
150         result r = E_SUCCESS;
151
152         __pDateTimeBar = _DateTimeBar::CreateDateTimeBarN(*this);
153         r = GetLastResult();
154         SysTryReturn(NID_UI_CTRL, (__pDateTimeBar != null), r, r, "[%s] Propagating.", GetErrorMessage(r));
155
156         r = __pDateTimeBar->AddActionEventListener(*this);
157         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
158
159         r = __pDateTimeBar->AddDateTimeChangeEventListener(*this);
160         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
161
162         r = SettingInfo::AddSettingEventListener(*this);
163         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
164
165         GET_SHAPE_CONFIG(EDITDATE::WIDTH, GetOrientation(), __previousSize.width);
166         GET_SHAPE_CONFIG(EDITDATE::HEIGHT, GetOrientation(), __previousSize.height);
167
168         return r;
169
170 CATCH:
171         delete __pDateTimeBar;
172         return r;
173 }
174
175 result
176 _EditDate::AddDateChangeEventListener(const _IDateTimeChangeEventListener& listener)
177 {
178         if (__pDateChangeEvent == null)
179         {
180                 __pDateChangeEvent = new (std::nothrow) _DateTimeChangeEvent(*this);
181                 SysTryReturn(NID_UI_CTRL, (__pDateChangeEvent != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
182                                 "[E_OUT_OF_MEMORY] Memory allocation failed.");
183         }
184
185         result r = __pDateChangeEvent->AddListener(listener);
186         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
187
188         return E_SUCCESS;
189 }
190
191 result
192 _EditDate::RemoveDateChangeEventListener(const _IDateTimeChangeEventListener& listener)
193 {
194         result r = E_OBJ_NOT_FOUND;
195
196         if (__pDateChangeEvent)
197         {
198                 r = __pDateChangeEvent->RemoveListener(listener);
199         }
200
201         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
202
203         return E_SUCCESS;
204 }
205
206 void
207 _EditDate::SetDate(const DateTime& date)
208 {
209         SetProperty("date", Variant(date));
210         return;
211 }
212
213 result
214 _EditDate::SetPropertyDate(const Variant& date)
215 {
216         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), E_SYSTEM, E_SYSTEM,
217                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
218
219         __pEditDatePresenter->SetDate(date.ToDateTime());
220
221         UpdateAccessibilityElement();
222         return E_SUCCESS;
223 }
224
225 DateTime
226 _EditDate::GetDate(void) const
227 {
228         Variant date = GetProperty("date");
229
230         return date.ToDateTime();
231 }
232
233 Variant
234 _EditDate::GetPropertyDate(void) const
235 {
236         DateTime date;
237
238         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), Variant(), E_SYSTEM,
239                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
240
241         date = __pEditDatePresenter->GetDate();
242
243         return Variant(date);
244 }
245
246 result
247 _EditDate::SetDay(int day)
248 {
249         result r = E_SUCCESS;
250
251         SetProperty("day", Variant(day));
252
253         r = GetLastResult();
254
255         return r;
256 }
257
258 result
259 _EditDate::SetPropertyDay(const Variant& day)
260 {
261         result r = E_SUCCESS;
262
263         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), E_SYSTEM, E_SYSTEM,
264                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
265
266         r = __pEditDatePresenter->SetDay(day.ToInt());
267
268         if (r == E_SUCCESS)
269         {
270                 UpdateAccessibilityElement();
271         }
272
273         return r;
274 }
275
276 int
277 _EditDate::GetDay(void) const
278 {
279         Variant day = GetProperty("day");
280
281         return day.ToInt();
282 }
283
284 Variant
285 _EditDate::GetPropertyDay(void) const
286 {
287         int day = -1;
288
289         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), Variant(), E_SYSTEM,
290                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
291
292         day = __pEditDatePresenter->GetDay();
293
294         return Variant(day);
295 }
296
297 result
298 _EditDate::SetMonth(int month)
299 {
300         result r = E_SUCCESS;
301
302         SetProperty("month", Variant(month));
303
304         r = GetLastResult();
305
306         return r;
307 }
308
309 result
310 _EditDate::SetPropertyMonth(const Variant& month)
311 {
312         result r = E_SUCCESS;
313
314         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), E_SYSTEM, E_SYSTEM,
315                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
316
317         int day;
318         int maxValue = -1;
319
320         _DateTimeUtils dateTimeUtils;
321
322         maxValue = dateTimeUtils.CalculateMaxDay(GetYear(), month.ToInt());
323         day = __pEditDatePresenter->GetDay();
324
325         if (day > maxValue)
326         {
327                 __pEditDatePresenter->SetDay(maxValue);
328         }
329
330         r = __pEditDatePresenter->SetMonth(month.ToInt());
331
332         if (r == E_SUCCESS)
333         {
334                 UpdateAccessibilityElement();
335         }
336
337         return r;
338 }
339
340 int
341 _EditDate::GetMonth(void) const
342 {
343         Variant month = GetProperty("month");
344
345         return month.ToInt();
346 }
347
348 Variant
349 _EditDate::GetPropertyMonth(void) const
350 {
351         int month = -1;
352
353         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), Variant(), E_SYSTEM,
354                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
355
356         month = __pEditDatePresenter->GetMonth();
357
358         return Variant(month);
359 }
360
361 result
362 _EditDate::SetYear(int year)
363 {
364         result r = E_SUCCESS;
365
366         SetProperty("year", Variant(year));
367
368         r = GetLastResult();
369
370         return r;
371 }
372
373 result
374 _EditDate::SetPropertyYear(const Variant& year)
375 {
376         result r = E_SUCCESS;
377
378         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), E_SYSTEM, E_SYSTEM,
379                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
380
381         int day;
382         int maxValue = -1;
383
384         _DateTimeUtils dateTimeUtils;
385
386         maxValue = dateTimeUtils.CalculateMaxDay(year.ToInt(), GetMonth());
387         day = __pEditDatePresenter->GetDay();
388
389         if (day > maxValue)
390         {
391                 __pEditDatePresenter->SetDay(maxValue);
392         }
393
394         r = __pEditDatePresenter->SetYear(year.ToInt());
395
396         if (r == E_SUCCESS)
397         {
398                 UpdateAccessibilityElement();
399         }
400
401         return r;
402 }
403
404 int
405 _EditDate::GetYear(void) const
406 {
407         Variant year = GetProperty("year");
408
409         return year.ToInt();
410 }
411
412 Variant
413 _EditDate::GetPropertyYear(void) const
414 {
415         int year = -1;
416
417         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), Variant(), E_SYSTEM,
418                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
419
420         year = __pEditDatePresenter->GetYear();
421
422         return Variant(year);
423 }
424
425 void
426 _EditDate::SetCurrentDate(void)
427 {
428         SysTryReturnVoidResult(NID_UI_CTRL, (__pEditDatePresenter != null), E_SYSTEM,
429                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
430
431         __pEditDatePresenter->SetCurrentDate();
432
433         UpdateAccessibilityElement();
434
435         return;
436 }
437
438 void
439 _EditDate::SetDatePickerEnabled(bool enable)
440 {
441         SetProperty("datePickerEnabled", Variant(enable));
442         return;
443 }
444
445 result
446 _EditDate::SetPropertyDatePickerEnabled(const Variant& enable)
447 {
448         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), E_SYSTEM, E_SYSTEM,
449                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
450
451         __pEditDatePresenter->SetDatePickerEnabled(enable.ToBool());
452
453         return E_SUCCESS;
454 }
455
456 bool
457 _EditDate::IsDatePickerEnabled(void) const
458 {
459         Variant enable = GetProperty("datePickerEnabled");
460
461         return enable.ToBool();
462 }
463
464 Variant
465 _EditDate::GetPropertyDatePickerEnabled(void) const
466 {
467         bool enable = false;
468
469         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), Variant(), E_SYSTEM,
470                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
471
472         enable = __pEditDatePresenter->IsDatePickerEnabled();
473
474         return Variant(enable);
475 }
476
477 result
478 _EditDate::SetYearRange(int minYear, int maxYear)
479 {
480         result r = E_SUCCESS;
481
482         SysTryReturn(NID_UI_CTRL, (maxYear >= DATETIME_YEAR_MIN && maxYear <= DATETIME_YEAR_MAX), E_INVALID_ARG, E_INVALID_ARG,
483                         "[E_INVALID_ARG] maxYear(%d) must be greater than or equal to %d and less than or equal to %d.",
484                         maxYear, DATETIME_YEAR_MIN, DATETIME_YEAR_MAX);
485
486         SysTryReturn(NID_UI_CTRL, (minYear >= DATETIME_YEAR_MIN && minYear <= DATETIME_YEAR_MAX), E_INVALID_ARG, E_INVALID_ARG,
487                         "[E_INVALID_ARG] minYear(%d) must be greater than or equal to %d and less than or equal to %d.",
488                         minYear, DATETIME_YEAR_MIN, DATETIME_YEAR_MAX);
489
490         SysTryReturn(NID_UI_CTRL, (maxYear >= minYear), E_INVALID_ARG, E_INVALID_ARG,
491                         "[E_INVALID_ARG] maxYear(%d) must be greater than or equal to minYear(%d).", maxYear, minYear);
492
493         Variant oldMinYear = GetPropertyMinYearRange();
494
495         r = SetPropertyMinYearRange(Variant(minYear));
496         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
497
498         r = SetPropertyMaxYearRange(Variant(maxYear));
499
500         if (IsFailed(r))
501         {
502                 SetPropertyMinYearRange(oldMinYear);
503         }
504
505         return r;
506 }
507
508 result
509 _EditDate::SetMinYearRange(int minYear)
510 {
511         result r = E_SUCCESS;
512
513         r = SetProperty("minYearRange", Variant(minYear));
514
515         return r;
516 }
517
518 result
519 _EditDate::SetMaxYearRange(int maxYear)
520 {
521         result r = E_SUCCESS;
522
523         r = SetProperty("maxYearRange", Variant(maxYear));
524
525         return r;
526 }
527
528 result
529 _EditDate::SetPropertyMinYearRange(const Variant& minYear)
530 {
531         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), E_SYSTEM, E_SYSTEM,
532                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
533
534         return __pEditDatePresenter->SetMinYear(minYear.ToInt());
535 }
536
537 result
538 _EditDate::SetPropertyMaxYearRange(const Variant& maxYear)
539 {
540         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), E_SYSTEM, E_SYSTEM,
541                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
542
543         return __pEditDatePresenter->SetMaxYear(maxYear.ToInt());
544 }
545
546 result
547 _EditDate::GetYearRange(int& minYear, int& maxYear) const
548 {
549         result r = E_SUCCESS;
550
551         minYear = GetMinYearRange();
552         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
553         maxYear = GetMaxYearRange();
554         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
555
556         return r;
557 }
558
559 int
560 _EditDate::GetMinYearRange(void) const
561 {
562         Variant minYear = GetProperty("minYearRange");
563
564         return minYear.ToInt();
565 }
566
567 int
568 _EditDate::GetMaxYearRange(void) const
569 {
570         Variant maxYear = GetProperty("maxYearRange");
571
572         return maxYear.ToInt();
573 }
574
575 Variant
576 _EditDate::GetPropertyMinYearRange(void) const
577 {
578         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), Variant(), E_SYSTEM,
579                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
580
581         int minYear = 0;
582
583         minYear = __pEditDatePresenter->GetMinYear();
584
585         return Variant(minYear);
586 }
587
588 Variant
589 _EditDate::GetPropertyMaxYearRange(void) const
590 {
591         SysTryReturn(NID_UI_CTRL, (__pEditDatePresenter != null), Variant(), E_SYSTEM,
592                         "[E_SYSTEM] A system error has occurred. The _EditDatePresenter instance is null.");
593
594         int maxYear = 0;
595
596         maxYear = __pEditDatePresenter->GetMaxYear();
597
598         return Variant(maxYear);
599 }
600
601 result
602 _EditDate::CalculateDateTimeBarPosition(void)
603 {
604         result r = E_SUCCESS;
605
606         float dateTimeBarHeight = 0.0f;
607         float arrowHeight = 0.0f;
608         float dateHeight = 0.0f;
609         float dateBarMargin = 0.0f;
610         float textHeight = 0.0f;
611         float titleDateMargin = 0.0f;
612         float bottomPosition = 0.0f;
613         float dateY = 0.0f;
614
615         FloatRectangle absoluteBounds;
616         FloatRectangle frameBounds;
617         FloatRectangle parentWindowBounds;
618         FloatRectangle titleBounds;
619
620         _Frame* pFrame = dynamic_cast<_Frame*>(_ControlManager::GetInstance()->GetCurrentFrame());
621         SysTryReturn(NID_UI_CTRL, pFrame != null, E_SYSTEM, E_SYSTEM,
622                         "[E_SYSTEM] A system error has occurred. Failed to get frame instance.");
623
624         absoluteBounds = GetAbsoluteBoundsF();
625         frameBounds = pFrame->GetAbsoluteBoundsF();
626         parentWindowBounds = GetParentWindowBounds();
627         titleBounds = __pEditDatePresenter->GetTitleBounds();
628
629         GET_SHAPE_CONFIG(DATETIMEBAR::ITEM_HEIGHT, GetOrientation(), dateTimeBarHeight);
630         GET_SHAPE_CONFIG(DATETIMEBAR::ARROW_HEIGHT, GetOrientation(), arrowHeight);
631         GET_SHAPE_CONFIG(EDITDATE::DATE_HEIGHT, GetOrientation(), dateHeight);
632         GET_SHAPE_CONFIG(EDITDATE::DATE_BAR_MARGIN, GetOrientation(), dateBarMargin);
633         GET_SHAPE_CONFIG(EDITDATE::TITLE_HEIGHT, GetOrientation(), textHeight);
634         GET_SHAPE_CONFIG(EDITDATE::TITLE_DATE_MARGIN, GetOrientation(), titleDateMargin);
635
636         if (!__title.IsEmpty()) //with title
637         {
638                 dateY = titleBounds.y + textHeight + titleDateMargin ;
639         }
640         else //without title
641         {
642                 dateY = (absoluteBounds.height - dateHeight) / 2.0f ;
643         }
644
645         bottomPosition = absoluteBounds.y + dateY + dateHeight + dateBarMargin + arrowHeight + dateTimeBarHeight;
646
647         GetDateTimeBar()->SetParentWindowBounds(parentWindowBounds);
648
649         if (bottomPosition > frameBounds.y + parentWindowBounds.y + parentWindowBounds.height)
650         {
651                 r = GetDateTimeBar()->SetPositionAndAlignment(FloatPoint(parentWindowBounds.x, absoluteBounds.y + dateY - dateBarMargin - arrowHeight), DATETIME_BAR_ALIGN_UP);
652                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
653         }
654         else
655         {
656                 r = GetDateTimeBar()->SetPositionAndAlignment(FloatPoint(parentWindowBounds.x, absoluteBounds.y + dateY + dateHeight + dateBarMargin + arrowHeight), DATETIME_BAR_ALIGN_DOWN);
657                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
658         }
659
660         __absoluteBounds = absoluteBounds;
661
662         return r;
663 }
664
665 void
666 _EditDate::OnDraw(void)
667 {
668         if (GetDateTimeBar() != null)
669         {
670                 FloatRectangle absoluteBounds = GetAbsoluteBoundsF();
671
672                 if (absoluteBounds.y != __absoluteBounds.y || absoluteBounds.height != __absoluteBounds.height)
673                 {
674                         CalculateDateTimeBarPosition();
675                 }
676         }
677
678         __pEditDatePresenter->Draw();
679         return;
680 }
681
682 void
683 _EditDate::CreateAccessibilityElement(void)
684 {
685         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
686
687         FloatRectangle dayBounds = __pEditDatePresenter->GetDateAreaBounds(DATETIME_ID_DAY);
688         FloatRectangle monthBounds = __pEditDatePresenter->GetDateAreaBounds(DATETIME_ID_MONTH);
689         FloatRectangle yearBounds = __pEditDatePresenter->GetDateAreaBounds(DATETIME_ID_YEAR);
690
691         if (__pAccessibilityEditDateElement == null)
692         {
693                 __pAccessibilityEditDateElement = new _AccessibilityElement(true);
694                 __pAccessibilityEditDateElement->SetBounds(GetClientBoundsF());
695                 __pAccessibilityEditDateElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
696                 __pAccessibilityEditDateElement->SetName("EditDateText");
697                 pContainer->AddElement(*__pAccessibilityEditDateElement);
698         }
699
700         if (__pAccessibilityYearElement == null && __pAccessibilityMonthElement == null && __pAccessibilityDayElement == null)
701         {
702                 String hintText(L"Double tap to edit");
703
704                 __pAccessibilityYearElement = new _AccessibilityElement(true);
705                 __pAccessibilityYearElement->SetBounds(yearBounds);
706                 __pAccessibilityYearElement->SetTrait(ACCESSIBILITY_TRAITS_YEAR);
707                 __pAccessibilityYearElement->SetHint(hintText);
708
709                 __pAccessibilityMonthElement = new _AccessibilityElement(true);
710                 __pAccessibilityMonthElement->SetBounds(monthBounds);
711                 __pAccessibilityMonthElement->SetTrait(ACCESSIBILITY_TRAITS_MONTH);
712                 __pAccessibilityMonthElement->SetHint(hintText);
713
714                 __pAccessibilityDayElement = new _AccessibilityElement(true);
715                 __pAccessibilityDayElement->SetBounds(dayBounds);
716                 __pAccessibilityDayElement->SetTrait(ACCESSIBILITY_TRAITS_DAY);
717                 __pAccessibilityDayElement->SetHint(hintText);
718
719                 switch (__pEditDatePresenter->GetLocaleDateFormat())
720                 {
721                 case SETTING_DATE_FORMAT_DD_MM_YYYY:
722                         pContainer->AddElement(*__pAccessibilityDayElement);
723                         pContainer->AddElement(*__pAccessibilityMonthElement);
724                         pContainer->AddElement(*__pAccessibilityYearElement);
725                         break;
726                 case SETTING_DATE_FORMAT_MM_DD_YYYY:
727                         pContainer->AddElement(*__pAccessibilityMonthElement);
728                         pContainer->AddElement(*__pAccessibilityDayElement);
729                         pContainer->AddElement(*__pAccessibilityYearElement);
730                         break;
731                 case SETTING_DATE_FORMAT_YYYY_MM_DD:
732                         pContainer->AddElement(*__pAccessibilityYearElement);
733                         pContainer->AddElement(*__pAccessibilityMonthElement);
734                         pContainer->AddElement(*__pAccessibilityDayElement);
735                         break;
736                 case SETTING_DATE_FORMAT_YYYY_DD_MM:
737                         pContainer->AddElement(*__pAccessibilityYearElement);
738                         pContainer->AddElement(*__pAccessibilityDayElement);
739                         pContainer->AddElement(*__pAccessibilityMonthElement);
740                         break;
741                 default:
742                         break;
743                 }
744                 UpdateAccessibilityElement();
745         }
746 }
747
748 void
749 _EditDate::OnBoundsChanged(void)
750 {
751         FloatDimension newSize = GetSizeF();
752
753         if (newSize.width != __previousSize.width || newSize.height != __previousSize.height)
754         {
755                 __isCalledBoundsChanged = true;
756         }
757
758         __pEditDatePresenter->Initialize();
759
760         if (__pAccessibilityEditDateElement)
761         {
762                 __pAccessibilityEditDateElement->SetBounds(GetClientBoundsF());
763         }
764         if (__pAccessibilityYearElement)
765         {
766                 __pAccessibilityYearElement->SetBounds(__pEditDatePresenter->GetDateAreaBounds(DATETIME_ID_YEAR));
767         }
768         if (__pAccessibilityMonthElement)
769         {
770                 __pAccessibilityMonthElement->SetBounds(__pEditDatePresenter->GetDateAreaBounds(DATETIME_ID_MONTH));
771         }
772         if (__pAccessibilityDayElement)
773         {
774                 __pAccessibilityDayElement->SetBounds(__pEditDatePresenter->GetDateAreaBounds(DATETIME_ID_DAY));
775         }
776
777         return;
778 }
779
780 void
781 _EditDate::OnChangeLayout(_ControlOrientation orientation)
782 {
783         if (!__isCalledBoundsChanged && !__isXmlBoundsExist)
784         {
785                 FloatRectangle bounds = GetBoundsF();
786
787                 GET_SHAPE_CONFIG(EDITDATE::WIDTH, GetOrientation(), bounds.width);
788                 GET_SHAPE_CONFIG(EDITDATE::HEIGHT, GetOrientation(), bounds.height);
789
790                 SetBounds(bounds, false);
791         }
792
793         __pEditDatePresenter->Initialize();
794         __pEditDatePresenter->SetLastSelectedId(DATETIME_ID_NONE);
795
796         if (GetDateTimeBar() != null)
797         {
798                 GetDateTimeBar()->SetVisibleState(false);
799                 GetDateTimeBar()->Close();
800         }
801
802         return;
803 }
804
805 bool
806 _EditDate::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
807 {
808         SetFocused(true);
809
810         FloatRectangle absoluteBounds = GetAbsoluteBoundsF();
811
812         if (absoluteBounds.y != __absoluteBounds.y || absoluteBounds.height != __absoluteBounds.height)
813         {
814                 CalculateDateTimeBarPosition();
815         }
816
817         return __pEditDatePresenter->OnTouchPressed(source, touchinfo);
818 }
819
820 bool
821 _EditDate::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
822 {
823         return __pEditDatePresenter->OnTouchReleased(source, touchinfo);
824 }
825
826 bool
827 _EditDate::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
828 {
829         return __pEditDatePresenter->OnTouchCanceled(source, touchinfo);
830 }
831
832 bool
833 _EditDate::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
834 {
835         return __pEditDatePresenter->OnTouchMoved(source, touchinfo);
836 }
837
838 void
839 _EditDate::OnTouchMoveHandled(const _Control& control)
840 {
841         __pEditDatePresenter->OnTouchMoveHandled(control);
842         return;
843 }
844
845 result
846 _EditDate::FireDateChangeEvent(_DateTimeChangeStatus status)
847 {
848         SysTryReturn(NID_UI_CTRL, (__pDateChangeEvent != null), E_INVALID_STATE, E_INVALID_STATE,
849                         "[E_INVALID_STATE] The _DateChangeEvent instance is null.");
850
851         _DateTimeChangeEventArg* pDateTimeEventArg = new (std::nothrow) _DateTimeChangeEventArg(status);
852         SysTryReturn(NID_UI_CTRL, pDateTimeEventArg, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
853
854         pDateTimeEventArg->SetDate(GetYear(), GetMonth(), GetDay());
855
856         __pDateChangeEvent->Fire(*pDateTimeEventArg);
857
858         return E_SUCCESS;
859 }
860
861 void
862 _EditDate::OnDateTimeChanged(const _Control& source, int year, int month, int day, int hour, int minute)
863 {
864         __pEditDatePresenter->Animate();
865         __pEditDatePresenter->SetLastSelectedId(DATETIME_ID_NONE);
866         Invalidate();
867         FireDateChangeEvent(DATE_INTERNAL_CHANGE_SAVED);
868         return;
869 }
870
871 void
872 _EditDate::OnDateTimeChangeCanceled(const _Control& source)
873 {
874         __pEditDatePresenter->SetLastSelectedId(DATETIME_ID_NONE);
875         Invalidate();
876         FireDateChangeEvent(DATE_INTERNAL_CHANGE_CANCELED);
877         return;
878 }
879
880 void
881 _EditDate::OnActionPerformed(const Ui::_Control& source, int actionId)
882 {
883         _DateTimeId boxId = __pEditDatePresenter->GetLastSelectedId();
884
885         if (boxId == DATETIME_ID_YEAR)
886         {
887                 SetYear(actionId);
888                 AdjustDay(actionId, GetMonth());
889         }
890         else if (boxId == DATETIME_ID_MONTH)
891         {
892                 SetMonth(actionId);
893                 AdjustDay(GetYear(), actionId);
894         }
895         else if (boxId == DATETIME_ID_DAY)
896         {
897                 SetDay(actionId);
898         }
899
900         Invalidate();
901
902         return;
903 }
904
905 void
906 _EditDate::AdjustDay(int year, int month)
907 {
908         _DateTimeUtils dateTimeUtils;
909         int maxValue = dateTimeUtils.CalculateMaxDay(year, month);
910
911         if (GetDay() > maxValue)
912         {
913                 SetDay(maxValue);
914         }
915
916         return;
917 }
918
919 _DateTimeBar*
920 _EditDate::GetDateTimeBar(void) const
921 {
922         return __pDateTimeBar;
923 }
924
925 void
926 _EditDate::OnFontChanged(Font* pFont)
927 {
928     __pEditDatePresenter->OnFontChanged(pFont);
929
930     return;
931 }
932
933 void
934 _EditDate::OnFontInfoRequested(unsigned long& style, float& size)
935 {
936     __pEditDatePresenter->OnFontInfoRequested(style, size);
937
938     return;
939 }
940
941 void
942 _EditDate::OnSettingChanged(String& key)
943 {
944         if (key.Equals(L"http://tizen.org/setting/locale.date.format", false))
945         {
946                 __pEditDatePresenter->Initialize();
947                 __pEditDatePresenter->SetLastSelectedId(DATETIME_ID_NONE);
948
949                 if (GetDateTimeBar() != null)
950                 {
951                         GetDateTimeBar()->SetVisibleState(false);
952                         GetDateTimeBar()->Close();
953                 }
954
955                 Invalidate();
956         }
957
958         return;
959 }
960
961 FloatRectangle
962 _EditDate::GetParentWindowBounds(void) const
963 {
964         _Form* pForm = null;
965         _Window* pwindow = null;
966         _Control* pControlCore = GetParent();
967
968         FloatDimension dateTimeBarSize(0.0f, 0.0f);
969         GET_DIMENSION_CONFIG(DATETIMEBAR::DEFAULT_SIZE, GetOrientation(), dateTimeBarSize);
970         FloatRectangle parentWindowBounds(0.0f, 0.0f, dateTimeBarSize.width, dateTimeBarSize.height);
971
972         while (true)
973         {
974                 if (pControlCore == null)
975                 {
976                         SysLog(NID_UI_CTRL,"[E_SYSTEM] Parent window not found.");
977
978                         return parentWindowBounds;
979                 }
980
981                 // If the parent is a Frame, then return the Form's bounds.
982                 pForm = dynamic_cast<_Form*>(pControlCore);
983                 if (pForm != null)
984                 {
985                         parentWindowBounds = pForm->GetBoundsF();
986                         break;
987                 }
988
989                 pwindow = dynamic_cast<_Window*>(pControlCore);
990
991                 if (pwindow != null)
992                 {
993                         parentWindowBounds = pwindow->GetBoundsF();
994                         break;
995                 }
996
997                 pControlCore = pControlCore->GetParent();
998         }
999
1000         return parentWindowBounds;
1001 }
1002
1003 void
1004 _EditDate::UpdateAccessibilityElement(void)
1005 {
1006         String string;
1007         String yearString;
1008         String dayString;
1009         String space(L" ");
1010
1011         if (__pAccessibilityEditDateElement == null)
1012         {
1013                 return;
1014         }
1015
1016         if (__title.IsEmpty() == false)
1017         {
1018                 string.Append(__title);
1019                 string.Append(L", ");
1020         }
1021
1022         yearString.Append(GetYear());
1023         dayString.Append(GetDay());
1024         String monthString = GetDateTimeBar()->GetMonthValue(GetMonth()).GetPointer();
1025
1026         switch (__pEditDatePresenter->GetLocaleDateFormat())
1027         {
1028         case SETTING_DATE_FORMAT_DD_MM_YYYY:
1029                 string.Append(dayString.GetPointer());
1030                 string.Append(space.GetPointer());
1031                 string.Append(monthString.GetPointer());
1032                 string.Append(space.GetPointer());
1033                 string.Append(yearString.GetPointer());
1034                 break;
1035         case SETTING_DATE_FORMAT_MM_DD_YYYY:
1036                 string.Append(monthString.GetPointer());
1037                 string.Append(space.GetPointer());
1038                 string.Append(dayString.GetPointer());
1039                 string.Append(space.GetPointer());
1040                 string.Append(yearString.GetPointer());
1041                 break;
1042         case SETTING_DATE_FORMAT_YYYY_MM_DD:
1043                 string.Append(yearString.GetPointer());
1044                 string.Append(space.GetPointer());
1045                 string.Append(monthString.GetPointer());
1046                 string.Append(space.GetPointer());
1047                 string.Append(dayString.GetPointer());
1048                 break;
1049         case SETTING_DATE_FORMAT_YYYY_DD_MM:
1050                 string.Append(yearString.GetPointer());
1051                 string.Append(space.GetPointer());
1052                 string.Append(dayString.GetPointer());
1053                 string.Append(space.GetPointer());
1054                 string.Append(monthString.GetPointer());
1055                 break;
1056         default:
1057                 break;
1058         }
1059
1060         __pAccessibilityEditDateElement->SetLabel(string);
1061         __pAccessibilityDayElement->SetLabel(dayString);
1062         __pAccessibilityMonthElement->SetLabel(monthString);
1063         __pAccessibilityYearElement->SetLabel(yearString);
1064         return;
1065 }
1066
1067 void
1068 _EditDate::SetXmlBoundsExist(bool isXmlBoundsExist)
1069 {
1070         __isXmlBoundsExist = isXmlBoundsExist;
1071 }
1072
1073 }}}  // Tizen::Ui::Controls