Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_EditDatePresenter.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 /**
19  * @file                FUiCtrl_EditDatePresenter.cpp
20  * @brief               This is the implementation file for the _EditDatePresenter class.
21  */
22
23 #include <vconf.h>
24 #include <FGrp_TextTextObject.h>
25 #include <FGrp_TextTextSimple.h>
26 #include <FGrp_CanvasImpl.h>
27 #include <FGrp_FontImpl.h>
28 #include "FUi_ResourceManager.h"
29 #include "FUiCtrl_EditDatePresenter.h"
30 #include "FUiCtrl_EditDate.h"
31 #include "FUiCtrl_DateTimeModel.h"
32 #include "FUiCtrl_DateTimeUtils.h"
33 #include "FUiControl.h"
34 #include "FUiAnim_VisualElement.h"
35 #include "FUiAnimVisualElementPropertyAnimation.h"
36 #include "FGrpColor.h"
37
38 using namespace Tizen::Graphics;
39 using namespace Tizen::Base;
40 using namespace Tizen::Graphics::_Text;
41 using namespace Tizen::Ui::Animations;
42
43
44 namespace Tizen { namespace Ui { namespace Controls
45 {
46 _EditDatePresenter::_EditDatePresenter(void)
47         : __pEditDateTimeModel(null)
48         , __pEditDate(null)
49         , __dayBounds(Rectangle())
50         , __monthBounds(Rectangle())
51         , __yearBounds(Rectangle())
52         , __titleBounds(Rectangle())
53         , __datePickerEnabled(true)
54         , __selectedId(DATETIME_ID_NONE)
55         , __lastSelectedId(DATETIME_ID_NONE)
56         , __touchMoveHandled(false)
57         , __titleObject()
58         , __textObject()
59         , __pFont(null)
60         , __dividerLineWidth(0)
61         , __dividerLineHeight(0)
62         , __titleFontSize(0)
63         , __dateFontSize(0)
64         , __pContentProvider(null)
65         , __isAnimating(false)
66         , __isEditDateInitialized(false)
67 {
68 }
69
70 _EditDatePresenter::~_EditDatePresenter(void)
71 {
72         __titleObject.RemoveAll();
73         __textObject.RemoveAll();
74
75         delete __pEditDateTimeModel;
76         __pEditDateTimeModel = null;
77 }
78
79 _EditDatePresenter*
80 _EditDatePresenter::CreateInstanceN(const _EditDate& editDate, const String& title)
81 {
82         _EditDatePresenter* pEditDatePresenter = new (std::nothrow) _EditDatePresenter;
83         SysTryReturn(NID_UI_CTRL, pEditDatePresenter, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
84
85         pEditDatePresenter->__pEditDate = const_cast <_EditDate*>(&editDate);
86
87         pEditDatePresenter->__pEditDateTimeModel = new (std::nothrow) _DateTimeModel;
88         SysTryCatch(NID_UI_CTRL, pEditDatePresenter->__pEditDateTimeModel, , E_OUT_OF_MEMORY,
89                         "[E_OUT_OF_MEMORY] Memory allocation failed.");
90         pEditDatePresenter->SetTitle(title);
91
92         return pEditDatePresenter;
93
94 CATCH:
95         delete pEditDatePresenter;
96         return null;
97 }
98
99 void
100 _EditDatePresenter::SetTitle(const String& title)
101 {
102         __title = title;
103 }
104 result
105 _EditDatePresenter::Initialize(void)
106 {
107         result r = E_SUCCESS;
108         int editDateHeight = 0;
109         int editDateWidth = 0;
110         int leftMargin = 0;
111         int editDateFontSize = 0;
112         int titledateMargin = 0;
113         int dateHeight = 0;
114         Rectangle bounds(0, 0, 0, 0);
115
116         GET_SHAPE_CONFIG(EDITDATE::WIDTH, __pEditDate->GetOrientation(), editDateWidth);
117         GET_SHAPE_CONFIG(EDITDATE::HEIGHT, __pEditDate->GetOrientation(), editDateHeight);
118         GET_SHAPE_CONFIG(EDITDATE::DATE_FONT_SIZE, __pEditDate->GetOrientation(), editDateFontSize);
119         GET_SHAPE_CONFIG(EDITDATE::DATE_TEXT_LEFT_MARGIN, __pEditDate->GetOrientation(), leftMargin);
120         GET_FIXED_VALUE_CONFIG(EDITDATE::ITEM_DIVIDER_WIDTH, __pEditDate->GetOrientation(), __dividerLineWidth);
121         GET_SHAPE_CONFIG(EDITDATE::ITEM_DIVIDER_HEIGHT, __pEditDate->GetOrientation(), __dividerLineHeight);
122         GET_SHAPE_CONFIG(EDITDATE::TEXT_FONT_SIZE, __pEditDate->GetOrientation(), __titleFontSize);
123         GET_SHAPE_CONFIG(EDITDATE::DATE_FONT_SIZE, __pEditDate->GetOrientation(), __dateFontSize);
124         GET_SHAPE_CONFIG(EDITDATE::TITLE_DATE_MARGIN, __pEditDate->GetOrientation(), titledateMargin);
125         GET_SHAPE_CONFIG(EDITDATE::DATE_HEIGHT, __pEditDate->GetOrientation(), dateHeight);
126         GET_SHAPE_CONFIG(EDITDATE::TEXT_HEIGHT, __pEditDate->GetOrientation(), __titleBounds.height);
127
128         if (__pEditDate->GetBounds().height > editDateHeight)
129         {
130                 editDateHeight = __pEditDate->GetBounds().height;
131         }
132
133         __pFont = __pEditDate->GetFallbackFont();
134         r = GetLastResult();
135         SysTryReturn(NID_UI_CTRL, __pFont != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
136
137         if (__title.IsEmpty() == false)
138         {
139                 __titleBounds.x = leftMargin;
140                 __titleBounds.y = (editDateHeight - (__titleBounds.height + titledateMargin + dateHeight)) / 2;
141                 __titleBounds.width = editDateWidth;
142
143                 bounds.y = __titleBounds.y + __titleBounds.height + titledateMargin;
144
145                 __dayBounds.height = dateHeight;
146                 __monthBounds.height = dateHeight;
147                 __yearBounds.height = dateHeight;
148
149                 if (!__isEditDateInitialized)
150                 {
151                         r = InitializeTitleObject();
152                         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
153                 }
154
155                 __titleObject.SetBounds(__titleBounds);
156         }
157         else
158         {
159                 __dayBounds.height = editDateHeight;
160                 __monthBounds.height = editDateHeight;
161                 __yearBounds.height = editDateHeight;
162         }
163
164         int elementWidth = 0;
165         GET_SHAPE_CONFIG(EDITDATE::TEXT_ELEMENT_WIDTH, __pEditDate->GetOrientation(), elementWidth);
166
167         __dayBounds.width = elementWidth;
168         __monthBounds.width = elementWidth;
169         __yearBounds.width = elementWidth;
170
171         __dayBounds.x = 0;
172         __monthBounds.x = 0;
173         __yearBounds.x = 0;
174
175         __dayBounds.y = bounds.y;
176         __monthBounds.y = bounds.y;
177         __yearBounds.y = bounds.y;
178
179         if (!__isEditDateInitialized)
180         {
181                 r = InitializeTextObject();
182                 SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
183         }
184
185         __isEditDateInitialized = true;
186
187         CalculateAreaBounds();
188
189         return r;
190 }
191
192 result
193 _EditDatePresenter::InitializeTitleObject(void)
194 {
195         result r = E_SUCCESS;
196
197         Color titleNormalColor;
198
199         GET_COLOR_CONFIG(EDITDATE::TITLE_TEXT_NORMAL, titleNormalColor);
200
201         r = __titleObject.Construct();
202         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
203
204         TextSimple* pSimpleText = null;
205
206         pSimpleText = new (std::nothrow)TextSimple((const_cast <wchar_t*>(__title.GetPointer())), __title.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
207         SysTryReturn(NID_UI_CTRL, (pSimpleText != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
208
209         __titleObject.AppendElement(*pSimpleText);
210
211         __titleObject.SetForegroundColor(titleNormalColor, 0, __titleObject.GetTextLength());
212         __titleObject.SetFont(__pFont, 0, __titleObject.GetTextLength());
213         __titleObject.SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT | TEXT_OBJECT_ALIGNMENT_MIDDLE);
214         __titleObject.SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
215         __titleObject.SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
216
217         return r;
218 }
219
220 result
221 _EditDatePresenter::InitializeTextObject(void)
222 {
223         result r = E_SUCCESS;
224
225         r = __textObject.Construct();
226         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
227
228         TextSimple* pSimpleText = null;
229
230         pSimpleText = new (std::nothrow)TextSimple(null, 0, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
231         SysTryReturn(NID_UI_CTRL, (pSimpleText != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
232
233         __textObject.AppendElement(*pSimpleText);
234
235         __textObject.SetFont(__pFont, 0, __textObject.GetTextLength());
236         __textObject.SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
237         __textObject.SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
238         __textObject.SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
239
240         return r;
241 }
242
243 int
244 _EditDatePresenter::GetLocaleDateFormat(void) const
245 {
246         int localeDateFormat = -1;
247         int error = vconf_get_int(VCONFKEY_SETAPPL_DATE_FORMAT_INT, &localeDateFormat);
248         if (error == -1)
249         {
250                 localeDateFormat = SETTING_DATE_FORMAT_DD_MM_YYYY;
251         }
252
253         return localeDateFormat;
254 }
255
256 DateTime
257 _EditDatePresenter::GetDate(void) const
258 {
259         SysAssertf((__pEditDateTimeModel != null), "The _EditDateTimeModel instance is null.");
260
261         DateTime date;
262         date.SetValue(GetYear(), GetMonth(), GetDay(), DATETIME_HOUR_MIN, DATETIME_MINUTE_MIN);
263
264         return date;
265 }
266
267 int
268 _EditDatePresenter::GetDay(void) const
269 {
270         SysAssertf((__pEditDateTimeModel != null), "The _EditDateTimeModel instance is null.");
271
272         return __pEditDateTimeModel->GetDay();
273 }
274
275 int
276 _EditDatePresenter::GetMonth(void) const
277 {
278         SysAssertf((__pEditDateTimeModel != null), "The _EditDateTimeModel instance is null.");
279
280         return __pEditDateTimeModel->GetMonth();
281 }
282
283 int
284 _EditDatePresenter::GetYear(void) const
285 {
286         SysAssertf((__pEditDateTimeModel != null), "The _EditDateTimeModel instance is null.");
287
288         return __pEditDateTimeModel->GetYear();
289 }
290
291 void
292 _EditDatePresenter::SetDate(const DateTime& date)
293 {
294         SysAssertf((__pEditDateTimeModel != null), "The _EditDateTimeModel instance is null.");
295
296         __pEditDateTimeModel->SetDateTime(date);
297         return;
298 }
299
300 void
301 _EditDatePresenter::SetCurrentDate(void)
302 {
303         SysAssertf((__pEditDateTimeModel != null), "The _EditDateTimeModel instance is null.");
304
305         __pEditDateTimeModel->SetCurrentDateTime();
306         return;
307 }
308
309 result
310 _EditDatePresenter::SetYear(int year)
311 {
312         SysAssertf((__pEditDateTimeModel != null), "The _EditDateTimeModel instance is null.");
313
314         return __pEditDateTimeModel->SetYear(year);
315 }
316
317 result
318 _EditDatePresenter::SetMonth(int month)
319 {
320         SysAssertf((__pEditDateTimeModel != null), "The _EditDateTimeModel instance is null.");
321
322         return __pEditDateTimeModel->SetMonth(month);
323 }
324
325 result
326 _EditDatePresenter::SetDay(int day)
327 {
328         SysAssertf((__pEditDateTimeModel != null), "The _EditDateTimeModel instance is null.");
329
330         return __pEditDateTimeModel->SetDay(day);
331 }
332
333 void
334 _EditDatePresenter::SetDatePickerEnabled(bool enable)
335 {
336         __datePickerEnabled = enable;
337         return;
338 }
339
340 bool
341 _EditDatePresenter::IsDatePickerEnabled(void) const
342 {
343         return __datePickerEnabled;
344 }
345
346 result
347 _EditDatePresenter::SetMinYear(int minYear)
348 {
349         return __pEditDateTimeModel->SetMinYear(minYear);
350 }
351
352 result
353 _EditDatePresenter::SetMaxYear(int maxYear)
354 {
355         return __pEditDateTimeModel->SetMaxYear(maxYear);
356 }
357
358 int
359 _EditDatePresenter::GetMinYear(void) const
360 {
361         int minYear = 0;
362         int maxYear = 0;
363
364         __pEditDateTimeModel->GetYearRange(minYear, maxYear);
365
366         return minYear;
367 }
368
369 int
370 _EditDatePresenter::GetMaxYear(void) const
371 {
372         int minYear = 0;
373         int maxYear = 0;
374
375         __pEditDateTimeModel->GetYearRange(minYear, maxYear);
376
377         return maxYear;
378 }
379
380 result
381 _EditDatePresenter::Draw(void)
382 {
383         result r = E_SUCCESS;
384
385         if (__isAnimating)
386         {
387                 return E_SUCCESS;
388         }
389
390         Canvas* pCanvas = __pEditDate->GetCanvasN();
391         SysAssertf((pCanvas != null), "Failed to get canvas.");
392
393         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
394         pCanvas->Clear();
395
396         if (__title.IsEmpty() == false)
397         {
398                 DrawTitle(*pCanvas);
399         }
400
401         String monthString;
402         String yearString;
403         String dayString;
404
405         _DateTimeUtils dateTimeUtils;
406         monthString = dateTimeUtils.GetMonthString(GetMonth());
407         yearString.Format(10, L"%04d", GetYear());
408         dayString.Format(10, L"%02d", GetDay());
409
410         CalculateAreaBounds();
411
412         r = DrawText(*pCanvas, GetDateAreaBounds(DATETIME_ID_DAY), dayString);
413         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
414         r = DrawText(*pCanvas, GetDateAreaBounds(DATETIME_ID_MONTH), monthString);
415         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
416         r = DrawText(*pCanvas, GetDateAreaBounds(DATETIME_ID_YEAR), yearString);
417         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
418
419         r = DrawDividers(*pCanvas);
420         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
421
422         delete pCanvas;
423
424         return r;
425
426 CATCH:
427         delete pCanvas;
428         return r;
429 }
430
431 result
432 _EditDatePresenter::CalculateAreaBounds(void)
433 {
434         result r = E_SUCCESS;
435
436         if (GetLocaleDateFormat() == DATE_FORMAT_DDMMYYYY)
437         {
438                 __monthBounds.x = __dayBounds.x + __dayBounds.width + __dividerLineWidth;
439                 __yearBounds.x = __monthBounds.x + __monthBounds.width + __dividerLineWidth;
440         }
441         else if (GetLocaleDateFormat() == DATE_FORMAT_MMDDYYYY)
442         {
443                 __dayBounds.x = __monthBounds.x + __monthBounds.width + __dividerLineWidth;
444                 __yearBounds.x = __dayBounds.x + __dayBounds.width + __dividerLineWidth;
445         }
446         else if (GetLocaleDateFormat() == DATE_FORMAT_YYYYMMDD)
447         {
448                 __monthBounds.x = __yearBounds.x + __yearBounds.width + __dividerLineWidth;
449                 __dayBounds.x = __monthBounds.x + __monthBounds.width + __dividerLineWidth;
450         }
451         else if (GetLocaleDateFormat() == DATE_FORMAT_YYYYDDMM)
452         {
453                 __dayBounds.x = __yearBounds.x + __yearBounds.width + __dividerLineWidth;
454                 __monthBounds.x = __dayBounds.x + __dayBounds.width + __dividerLineWidth;
455         }
456
457         return r;
458 }
459
460 result
461 _EditDatePresenter::DrawTitle(Canvas& canvas)
462 {
463         if (!__pEditDate->IsEnabled())
464         {
465                 Color titleDisabledColor;
466                 GET_COLOR_CONFIG(EDITDATE::TITLE_TEXT_DISABLED, titleDisabledColor);
467                 __titleObject.SetForegroundColor(titleDisabledColor, 0, __titleObject.GetTextLength());
468         }
469
470         (_FontImpl::GetInstance(*__pFont))->SetSize(__titleFontSize);
471         __titleObject.SetFont(__pFont, 0, __titleObject.GetTextLength());
472         __titleObject.Draw(*_CanvasImpl::GetInstance(canvas));
473
474         return E_SUCCESS;
475 }
476
477 result
478 _EditDatePresenter::DrawText(Canvas& canvas, const Rectangle& bounds, const String& text)
479 {
480         Color textColor;
481
482         if (!__pEditDate->IsEnabled())
483         {
484                 GET_COLOR_CONFIG(EDITDATE::TEXT_DISABLED, textColor);
485         }
486         else
487         {
488                 GET_COLOR_CONFIG(EDITDATE::TEXT_NORMAL, textColor);
489         }
490
491         _DateTimeId boxId = DATETIME_ID_NONE;
492         boxId = GetBoxIdFromPosition(Point(bounds.x, bounds.y));
493
494         if (boxId == __selectedId && boxId != DATETIME_ID_NONE)
495         {
496                 GET_COLOR_CONFIG(EDITDATE::TEXT_PRESSED, textColor);
497         }
498
499         Rectangle drawAreaBounds(0, 0, 0, 0);
500         drawAreaBounds = bounds;
501
502         TextSimple* pSimpleText = null;
503         pSimpleText = new (std::nothrow)TextSimple((const_cast <wchar_t*>(text.GetPointer())), text.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
504         SysTryReturn(NID_UI_CTRL, (pSimpleText != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
505
506         __textObject.RemoveAll();
507
508         (_FontImpl::GetInstance(*__pFont))->SetSize(__dateFontSize);
509
510         __textObject.AppendElement(*pSimpleText);
511
512         __textObject.SetFont(__pFont, 0, __textObject.GetTextLength());
513         __textObject.SetForegroundColor(textColor, 0, __textObject.GetTextLength());
514         __textObject.SetBounds(drawAreaBounds);
515         __textObject.Draw(*_CanvasImpl::GetInstance(canvas));
516
517         return E_SUCCESS;
518 }
519
520 result
521 _EditDatePresenter::DrawDividers(Canvas& canvas)
522 {
523         result r = E_SUCCESS;
524
525         int elementWidth = 0;
526         int editDateHeight = 0;
527         int dateHeight = 0;
528
529         GET_SHAPE_CONFIG(EDITDATE::TEXT_ELEMENT_WIDTH, __pEditDate->GetOrientation(), elementWidth);
530         GET_SHAPE_CONFIG(EDITDATE::DATE_HEIGHT, __pEditDate->GetOrientation(), dateHeight);
531         GET_SHAPE_CONFIG(EDITDATE::HEIGHT, __pEditDate->GetOrientation(), editDateHeight);
532
533         if (__pEditDate->GetBounds().height > editDateHeight)
534         {
535                 editDateHeight = __pEditDate->GetBounds().height;
536         }
537
538         Color dividerHalfLeftColor;
539         Color dividerHalfRightColor;
540
541         GET_COLOR_CONFIG(EDITDATE::DIVIDER_HALF_LEFT, dividerHalfLeftColor);
542         GET_COLOR_CONFIG(EDITDATE::DIVIDER_HALF_RIGHT, dividerHalfRightColor);
543
544         Rectangle bounds(0, 0, 0, 0);
545
546         bounds.x = elementWidth;
547
548         if (__title.IsEmpty() == false)
549         {
550                 bounds.y = __dayBounds.y + ((dateHeight - __dividerLineHeight) / 2);
551         }
552         else
553         {
554                 bounds.y = __dayBounds.y + ((editDateHeight - __dividerLineHeight) / 2);
555         }
556
557         for (int divider = 0; divider < __dividerCount; divider++)
558         {
559                 canvas.SetForegroundColor(dividerHalfLeftColor);
560                 canvas.SetLineWidth(__dividerLineWidth / 2);
561
562                 r = canvas.DrawLine(Point(bounds.x, bounds.y), Point(bounds.x, bounds.y + __dividerLineHeight));
563                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
564
565                 canvas.SetForegroundColor(dividerHalfRightColor);
566                 canvas.SetLineWidth(__dividerLineWidth / 2);
567
568                 r = canvas.DrawLine(Point(bounds.x + 1, bounds.y), Point(bounds.x + 1, bounds.y + __dividerLineHeight));
569                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
570
571                 bounds.x = bounds.x + elementWidth;
572         }
573
574         return r;
575 }
576 _DateTimeId
577 _EditDatePresenter::GetBoxIdFromPosition(const Point& point) const
578 {
579         _DateTimeId displayBoxId = DATETIME_ID_NONE;
580
581         if (point.y < __dayBounds.y || point.y > __dayBounds.y + __dayBounds.height)
582         {
583                 return displayBoxId;
584         }
585
586         if (__dayBounds.Contains(point) == true)
587         {
588                 displayBoxId = DATETIME_ID_DAY;
589         }
590         else if (__monthBounds.Contains(point) == true)
591         {
592                 displayBoxId = DATETIME_ID_MONTH;
593         }
594         else if (__yearBounds.Contains(point) == true)
595         {
596                 displayBoxId = DATETIME_ID_YEAR;
597         }
598
599         return displayBoxId;
600 }
601
602 Rectangle
603 _EditDatePresenter::GetDateAreaBounds(_DateTimeId id) const
604 {
605         if (id == DATETIME_ID_DAY)
606         {
607                 return __dayBounds;
608         }
609         else if (id == DATETIME_ID_MONTH)
610         {
611                 return __monthBounds;
612         }
613         else if (id == DATETIME_ID_YEAR)
614         {
615                 return __yearBounds;
616         }
617         else
618         {
619                 return Rectangle();
620         }
621 }
622
623 Rectangle
624 _EditDatePresenter::GetTitleBounds(void) const
625 {
626         return __titleBounds;
627 }
628
629 void
630 _EditDatePresenter::SetLastSelectedId(_DateTimeId boxId)
631 {
632         __lastSelectedId = boxId;
633         return;
634 }
635
636 _DateTimeId
637 _EditDatePresenter::GetLastSelectedId(void) const
638 {
639         return __lastSelectedId;
640 }
641
642 void
643 _EditDatePresenter::OnFontChanged(Font* pFont)
644 {
645         __pFont = pFont;
646
647         if (__pEditDate->GetDateTimeBar() != null)
648         {
649                 __pEditDate->GetDateTimeBar()->SetFont(*pFont);
650         }
651
652     return;
653 }
654
655 void
656 _EditDatePresenter::OnFontInfoRequested(unsigned long& style, int& size)
657 {
658         style = FONT_STYLE_PLAIN;
659         size = __dateFontSize;
660
661         return;
662 }
663
664 void
665 _EditDatePresenter::OnChangeLayout(_ControlOrientation orientation)
666 {
667         Dimension editDateSize;
668
669         result r = E_SUCCESS;
670
671         r = GET_DIMENSION_CONFIG(EDITDATE::DEFAULT_SIZE, orientation, editDateSize);
672
673         if (r == E_SUCCESS && editDateSize != __pEditDate->GetSize())
674         {
675                 __pEditDate->SetResizable(true);
676                 __pEditDate->SetSize(editDateSize);
677                 __pEditDate->SetResizable(false);
678         }
679
680         r = Initialize();
681         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
682
683         return;
684 }
685
686 bool
687 _EditDatePresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
688 {
689         SysTryReturn(NID_UI_CTRL, IsDatePickerEnabled() == true, true, E_SYSTEM,
690                                 "[E_SYSTEM] A system error has occurred. EditDate instance is disabled.");
691
692         if (&source != __pEditDate)
693         {
694                 return false;
695         }
696
697         __touchMoveHandled = false;
698
699         Point point = touchinfo.GetCurrentPosition();
700
701         _DateTimeId boxId = DATETIME_ID_NONE;
702         boxId = GetBoxIdFromPosition(point);
703
704         if (boxId < DATETIME_ID_YEAR || boxId > DATETIME_ID_DAY)
705         {
706                 __selectedId = DATETIME_ID_NONE;
707                 return true;
708         }
709
710         int minValue = -1;
711         int maxValue = -1;
712         int displayValue = -1;
713
714         if (boxId == DATETIME_ID_DAY)
715         {
716                 if (__pEditDate->GetDateTimeBar() != null)
717                 {
718                         _DateTimeUtils dateTimeUtils;
719                         maxValue = dateTimeUtils.CalculateMaxDay(GetYear(), GetMonth());
720                         minValue = DATETIME_DAY_MIN;
721                         displayValue = GetDay();
722                 }
723         }
724         else if (boxId == DATETIME_ID_MONTH)
725         {
726                 minValue = DATETIME_MONTH_MIN;
727                 maxValue = DATETIME_MONTH_MAX;
728                 displayValue = GetMonth();
729         }
730         else if (boxId == DATETIME_ID_YEAR)
731         {
732                 minValue = GetMinYear();
733                 maxValue = GetMaxYear();
734                 displayValue = GetYear();
735         }
736
737         if (__pEditDate->GetDateTimeBar() != null)
738         {
739                 if (__pEditDate->GetDateTimeBar()->GetItemCount() > 0)
740                 {
741                         __pEditDate->GetDateTimeBar()->RemoveAllItems();
742                 }
743
744                 result r = __pEditDate->GetDateTimeBar()->SetInitialValue(minValue, maxValue, displayValue, boxId);
745                 if (r != E_SUCCESS)
746                 {
747                         __selectedId = DATETIME_ID_NONE;
748                         return true;
749                 }
750         }
751
752         __selectedId = boxId;
753
754         __pEditDate->Invalidate();
755
756         return true;
757 }
758
759 bool
760 _EditDatePresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
761 {
762         SysTryReturn(NID_UI_CTRL, IsDatePickerEnabled() == true, true, E_SYSTEM,
763                                 "[E_SYSTEM] A system error has occurred. EditDate instance is disabled.");
764
765         if (&source != __pEditDate)
766         {
767                 return false;
768         }
769
770         Point point = touchinfo.GetCurrentPosition();
771
772         _DateTimeId boxId = DATETIME_ID_NONE;
773         boxId = GetBoxIdFromPosition(point);
774
775         if (boxId != __selectedId)
776         {
777                 __selectedId = DATETIME_ID_NONE;
778         }
779
780         SetLastSelectedId(__selectedId);
781         __selectedId = DATETIME_ID_NONE;
782
783         __lastSelectedValue = "";
784         _DateTimeUtils dateTimeUtils;
785         if (GetLastSelectedId() == DATETIME_ID_YEAR)
786         {
787                   __lastSelectedValue.Format(10, L"%04d", GetYear());
788         }
789         else if (GetLastSelectedId() == DATETIME_ID_MONTH)
790         {
791                    __lastSelectedValue = dateTimeUtils.GetMonthString(GetMonth());
792         }
793         else if (GetLastSelectedId() == DATETIME_ID_DAY)
794         {
795                         __lastSelectedValue.Format(10, L"%02d", GetDay());
796         }
797
798         __pEditDate->Invalidate();
799
800         if (__pEditDate->GetDateTimeBar() != null && GetLastSelectedId() != DATETIME_ID_NONE)
801         {
802                 Rectangle bounds(0, 0, 0, 0);
803                 bounds = GetDateAreaBounds(GetLastSelectedId());
804                 Rectangle absoluteBounds(0, 0, 0, 0);
805                 absoluteBounds = __pEditDate->GetAbsoluteBounds();
806                 bounds.x += absoluteBounds.x;
807
808                 __pEditDate->GetDateTimeBar()->CalculateArrowBounds(bounds);
809                 __pEditDate->GetDateTimeBar()->SetVisibleState(true);
810                 __pEditDate->GetDateTimeBar()->Open();
811         }
812
813         return true;
814 }
815
816 bool
817 _EditDatePresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
818 {
819         if (&source != __pEditDate)
820         {
821                 return false;
822         }
823
824         __selectedId = DATETIME_ID_NONE;
825
826         __pEditDate->Invalidate();
827
828         return true;
829 }
830
831 bool
832 _EditDatePresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
833 {
834         if (&source != __pEditDate)
835         {
836                 return false;
837         }
838
839         Point touchPoint = touchinfo.GetCurrentPosition();
840
841         if (GetBoxIdFromPosition(touchPoint) != __selectedId)
842         {
843                 __selectedId = DATETIME_ID_NONE;
844         }
845
846         __pEditDate->Invalidate();
847         return false;
848 }
849
850 void
851 _EditDatePresenter::OnTouchMoveHandled(const _Control& control)
852 {
853         __touchMoveHandled = true;
854         __selectedId = DATETIME_ID_NONE;
855         __pEditDate->Invalidate();
856         return;
857 }
858
859 void
860 _EditDatePresenter::Animate(void)
861 {
862         SysAssertf((__pFont != null), "Font instance must not be null.");
863
864         (_FontImpl::GetInstance(*__pFont))->SetSize(__dateFontSize);
865
866         result r = E_SUCCESS;
867         Rectangle rect;
868         String monthString;
869         String yearString;
870         String dayString;
871         String newValue;
872
873         _DateTimeUtils dateTimeUtils;
874         monthString = dateTimeUtils.GetMonthString(GetMonth());
875         yearString.Format(10, L"%04d", GetYear());
876         dayString.Format(10, L"%02d", GetDay());
877
878         if (GetLastSelectedId() == DATETIME_ID_DAY)
879         {
880                 SysTryReturnVoidResult(NID_UI_CTRL, (__lastSelectedValue.Equals(dayString) == false), E_SUCCESS, "Day string matched.");
881
882                 newValue = dayString;
883                 rect = GetDateAreaBounds(DATETIME_ID_DAY);
884         }
885         else if (GetLastSelectedId() == DATETIME_ID_MONTH)
886         {
887                 SysTryReturnVoidResult(NID_UI_CTRL, (__lastSelectedValue.Equals(monthString) == false), E_SUCCESS, "Month string matched.");
888
889                 newValue = monthString;
890                 rect = GetDateAreaBounds(DATETIME_ID_MONTH);
891         }
892         else if (GetLastSelectedId() == DATETIME_ID_YEAR)
893         {
894                 SysTryReturnVoidResult(NID_UI_CTRL, (__lastSelectedValue.Equals(yearString) == false), E_SUCCESS, "Year string matched.");
895
896                 newValue = yearString;
897                 rect = GetDateAreaBounds(DATETIME_ID_YEAR);
898         }
899
900         Dimension newTextDim;
901         Dimension oldTextDim;
902         Point textPoint;
903         VisualElement* pNewVisualElement = null;
904         VisualElement* pOldVisualElement = null;
905         VisualElement* pEditDateElement = null;
906         VisualElementPropertyAnimation* pNewBoundsAnimation = null;
907         VisualElementPropertyAnimation* pOldBoundsAnimation = null;
908         Canvas *pCanvas = null;
909
910         __pFont->GetTextExtent(newValue, newValue.GetLength(), newTextDim);
911         __pFont->GetTextExtent(__lastSelectedValue, __lastSelectedValue.GetLength(), oldTextDim);
912
913         if (newTextDim.width > oldTextDim.width)
914         {
915                 textPoint.x = (rect.width - newTextDim.width) / 2;
916         }
917         else
918         {
919                 textPoint.x = (rect.width - oldTextDim.width) / 2;
920         }
921
922         SysTryReturnVoidResult(NID_UI_CTRL, (rect.x + textPoint.x < __pEditDate->GetBounds().width), E_SUCCESS, "Rolling animation cann't be played.");
923
924         __isAnimating = true;
925
926         __pContentProvider = new (std::nothrow) VisualElement();
927         SysTryReturnVoidResult(NID_UI_CTRL, (__pContentProvider != null), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
928
929         r = __pContentProvider->Construct();
930         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
931
932         __pContentProvider->SetShowState(true);
933         __pContentProvider->SetClipChildrenEnabled(true);
934         __pContentProvider->SetImplicitAnimationEnabled(false);
935
936         pEditDateElement = __pEditDate->GetVisualElement();
937         r = GetLastResult();
938         SysTryCatch(NID_UI_CTRL, (pEditDateElement != null), , r, "[%s] Propagating.", GetErrorMessage(r));
939
940         pNewVisualElement = new (std::nothrow) VisualElement();
941         SysTryCatch(NID_UI_CTRL, (pNewVisualElement != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
942
943         r = pNewVisualElement->Construct();
944         if (r != E_SUCCESS)
945         {
946                 pNewVisualElement->Destroy();
947         }
948         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
949
950         pOldVisualElement = new (std::nothrow) VisualElement();
951         if (pOldVisualElement == null)
952         {
953                 pNewVisualElement->Destroy();
954         }
955         SysTryCatch(NID_UI_CTRL, (pOldVisualElement != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
956
957         r = pOldVisualElement->Construct();
958         if (r != E_SUCCESS)
959         {
960                 pNewVisualElement->Destroy();
961                 pOldVisualElement->Destroy();
962         }
963         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
964
965         pNewVisualElement->SetShowState(true);
966         pOldVisualElement->SetShowState(true);
967
968         if (newTextDim.width > oldTextDim.width)
969         {
970                 textPoint.x = (rect.width - newTextDim.width) / 2;
971                 textPoint.y = (rect.height - newTextDim.height) / 2;
972                 __pContentProvider->SetBounds(FloatRectangle((rect.x + textPoint.x) * 1.0f, (rect.y + textPoint.y) * 1.0f, newTextDim.width * 1.0f, newTextDim.height * 1.0f));
973                 pNewVisualElement->SetBounds(FloatRectangle(0.0f, newTextDim.height * 1.0f, newTextDim.width * 1.0f, newTextDim.height * 1.0f));
974
975                 rect.x = static_cast<int>(__pContentProvider->GetBounds().x);
976                 rect.y = static_cast<int>(__pContentProvider->GetBounds().y);
977                 rect.width = static_cast<int>(__pContentProvider->GetBounds().width);
978                 rect.height = static_cast<int>(__pContentProvider->GetBounds().height);
979
980                 textPoint.x = (rect.width - oldTextDim.width) / 2;
981                 textPoint.y = (rect.height - oldTextDim.height) / 2;
982
983                 pOldVisualElement->SetBounds(FloatRectangle(textPoint.x * 1.0f, 0.0f, oldTextDim.width * 1.0f, oldTextDim.height * 1.0f));
984         }
985         else
986         {
987                 textPoint.x = (rect.width - oldTextDim.width) / 2;
988                 textPoint.y = (rect.height - oldTextDim.height) / 2;
989                 __pContentProvider->SetBounds(FloatRectangle((rect.x + textPoint.x) * 1.0f, (rect.y + textPoint.y) * 1.0f, oldTextDim.width * 1.0f, oldTextDim.height * 1.0f));
990                 pOldVisualElement->SetBounds(FloatRectangle(0.0f, 0.0f, oldTextDim.width * 1.0f, oldTextDim.height * 1.0f));
991
992                 rect.x = static_cast<int>(__pContentProvider->GetBounds().x);
993                 rect.y = static_cast<int>(__pContentProvider->GetBounds().y);
994                 rect.width = static_cast<int>(__pContentProvider->GetBounds().width);
995                 rect.height = static_cast<int>(__pContentProvider->GetBounds().height);
996
997                 textPoint.x = (rect.width - newTextDim.width) / 2;
998                 textPoint.y = (rect.height - newTextDim.height) / 2;
999
1000                 pNewVisualElement->SetBounds(FloatRectangle(textPoint.x * 1.0f, newTextDim.height * 1.0f, newTextDim.width * 1.0f, newTextDim.height * 1.0f));
1001         }
1002
1003
1004         pCanvas = pEditDateElement->GetCanvasN(rect);
1005         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
1006         pCanvas->Clear();
1007         delete pCanvas;
1008
1009         pEditDateElement->AttachChild(*__pContentProvider);
1010
1011         __pContentProvider->AttachChild(*pOldVisualElement);
1012         __pContentProvider->AttachChild(*pNewVisualElement);
1013
1014         pNewBoundsAnimation = new (std::nothrow) VisualElementPropertyAnimation();
1015         SysTryCatch(NID_UI_CTRL, (pNewBoundsAnimation != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1016
1017         pNewBoundsAnimation->SetDuration(300);
1018         pNewBoundsAnimation->SetPropertyName("bounds.position");
1019         pNewBoundsAnimation->SetStartValue(Variant(FloatPoint(pNewVisualElement->GetBounds().x * 1.0f, newTextDim.height * 1.0f)));
1020         pNewBoundsAnimation->SetEndValue(Variant(FloatPoint(pNewVisualElement->GetBounds().x * 1.0f, 0.0f)));
1021         pNewBoundsAnimation->SetVisualElementAnimationStatusEventListener(this);
1022
1023         pOldBoundsAnimation = new (std::nothrow) VisualElementPropertyAnimation();
1024         SysTryCatch(NID_UI_CTRL, (pOldBoundsAnimation != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1025
1026         pOldBoundsAnimation->SetDuration(300);
1027         pOldBoundsAnimation->SetPropertyName("bounds.position");
1028         pOldBoundsAnimation->SetStartValue(Variant(FloatPoint(pOldVisualElement->GetBounds().x * 1.0f, 0.0f)));
1029         pOldBoundsAnimation->SetEndValue(Variant(FloatPoint(pOldVisualElement->GetBounds().x * 1.0f, oldTextDim.height * -1.0f)));
1030         pOldBoundsAnimation->SetVisualElementAnimationStatusEventListener(this);
1031
1032         pCanvas = pOldVisualElement->GetCanvasN();
1033         r = GetLastResult();
1034         SysTryCatch(NID_UI_CTRL, (pCanvas != null), , r, "[%s] Propagating.", GetErrorMessage(r));
1035
1036         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
1037         pCanvas->Clear();
1038         pCanvas->SetFont(*__pFont);
1039         pCanvas->DrawText(Point(0,0),__lastSelectedValue);
1040
1041         delete pCanvas;
1042         pCanvas = null;
1043
1044         pCanvas = pNewVisualElement->GetCanvasN();
1045         r = GetLastResult();
1046         SysTryCatch(NID_UI_CTRL, (pCanvas != null), , r, "[%s] Propagating.", GetErrorMessage(r));
1047
1048         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
1049         pCanvas->Clear();
1050         pCanvas->SetFont(*__pFont);
1051         pCanvas->DrawText(Point(0,0),newValue);
1052
1053         delete pCanvas;
1054         pCanvas = null;
1055
1056         pOldVisualElement->SetImplicitAnimationEnabled(false);
1057         pOldVisualElement->AddAnimation(*pOldBoundsAnimation);
1058
1059         pNewVisualElement->SetImplicitAnimationEnabled(false);
1060         pNewVisualElement->AddAnimation(*pNewBoundsAnimation);
1061
1062         delete pOldBoundsAnimation;
1063         delete pNewBoundsAnimation;
1064
1065         return;
1066
1067 CATCH:
1068         __isAnimating = false;
1069         __pContentProvider->Destroy();
1070
1071         delete pNewBoundsAnimation;
1072         pNewBoundsAnimation = null;
1073
1074         delete pOldBoundsAnimation;
1075         pOldBoundsAnimation = null;
1076
1077         delete pCanvas;
1078         pCanvas = null;
1079
1080         return;
1081 }
1082
1083 void
1084 _EditDatePresenter::OnVisualElementAnimationFinished (const VisualElementAnimation &animation, const String &keyName, VisualElement &target, bool completedNormally)
1085 {
1086         result r = E_SUCCESS;
1087         __isAnimating = false;
1088
1089         VisualElement* pEditDateElement = __pEditDate->GetVisualElement();
1090         r = GetLastResult();
1091         SysTryReturnVoidResult(NID_UI_CTRL, (pEditDateElement != null), r, "[%s] Propagating.", GetErrorMessage(r));
1092
1093         pEditDateElement->DetachChild(*__pContentProvider);
1094         __pContentProvider->Destroy();
1095
1096         Draw();
1097         return;
1098 }
1099
1100 }}} // Tizen::Ui::Controls