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