7f43956153f5924e95cdfc674b506ae2ed84de43
[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 true;
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
731         __pEditDate->Invalidate();
732
733         return true;
734 }
735
736 bool
737 _EditDatePresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
738 {
739         SysTryReturn(NID_UI_CTRL, IsDatePickerEnabled() == true, true, E_SYSTEM,
740                         "[E_SYSTEM] A system error has occurred. EditDate instance is disabled.");
741
742         if (&source != __pEditDate)
743         {
744                 return false;
745         }
746
747         FloatPoint point = touchinfo.GetCurrentPosition();
748
749         _DateTimeId boxId = DATETIME_ID_NONE;
750         boxId = GetBoxIdFromPosition(point);
751
752         if (boxId != __selectedId)
753         {
754                 __selectedId = DATETIME_ID_NONE;
755         }
756
757         SetLastSelectedId(__selectedId);
758         __selectedId = DATETIME_ID_NONE;
759
760         __lastSelectedValue = "";
761         _DateTimeUtils dateTimeUtils;
762         if (GetLastSelectedId() == DATETIME_ID_YEAR)
763         {
764                 __lastSelectedValue.Format(10, L"%04d", GetYear());
765                 PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_TAP);
766         }
767         else if (GetLastSelectedId() == DATETIME_ID_MONTH)
768         {
769                 __lastSelectedValue = dateTimeUtils.GetMonthString(GetMonth());
770                 PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_TAP);
771         }
772         else if (GetLastSelectedId() == DATETIME_ID_DAY)
773         {
774                 __lastSelectedValue.Format(10, L"%02d", GetDay());
775                 PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_TAP);
776         }
777
778         __pEditDate->Invalidate();
779
780         if (__pEditDate->GetDateTimeBar() != null && GetLastSelectedId() != DATETIME_ID_NONE)
781         {
782                 FloatRectangle bounds(0.0f, 0.0f, 0.0f, 0.0f);
783                 bounds = GetDateAreaBounds(GetLastSelectedId());
784                 FloatRectangle absoluteBounds(0.0f, 0.0f, 0.0f, 0.0f);
785                 absoluteBounds = __pEditDate->GetAbsoluteBoundsF();
786                 bounds.x += absoluteBounds.x;
787
788                 __pEditDate->GetDateTimeBar()->CalculateArrowBounds(bounds);
789                 __pEditDate->GetDateTimeBar()->SetVisibleState(true);
790                 __pEditDate->GetDateTimeBar()->Close();
791                 __pEditDate->GetDateTimeBar()->Open();
792         }
793
794         return true;
795 }
796
797 bool
798 _EditDatePresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
799 {
800         if (&source != __pEditDate)
801         {
802                 return false;
803         }
804
805         __selectedId = DATETIME_ID_NONE;
806
807         __pEditDate->Invalidate();
808
809         return true;
810 }
811
812 bool
813 _EditDatePresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
814 {
815         if (&source != __pEditDate)
816         {
817                 return false;
818         }
819
820         FloatPoint touchPoint = touchinfo.GetCurrentPosition();
821
822         if (GetBoxIdFromPosition(touchPoint) != __selectedId)
823         {
824                 __selectedId = DATETIME_ID_NONE;
825         }
826
827         __pEditDate->Invalidate();
828         return false;
829 }
830
831 void
832 _EditDatePresenter::OnTouchMoveHandled(const _Control& control)
833 {
834         __touchMoveHandled = true;
835         __selectedId = DATETIME_ID_NONE;
836         __pEditDate->Invalidate();
837         return;
838 }
839
840 void
841 _EditDatePresenter::Animate(void)
842 {
843         SysAssertf((__pFont != null), "Font instance must not be null.");
844
845         (_FontImpl::GetInstance(*__pFont))->SetSize(__dateFontSize);
846
847         result r = E_SUCCESS;
848         FloatRectangle rect;
849         String monthString;
850         String yearString;
851         String dayString;
852         String newValue;
853
854         _DateTimeUtils dateTimeUtils;
855         monthString = dateTimeUtils.GetMonthString(GetMonth());
856         yearString.Format(10, L"%04d", GetYear());
857         dayString.Format(10, L"%02d", GetDay());
858
859         if (GetLastSelectedId() == DATETIME_ID_DAY)
860         {
861                 SysTryReturnVoidResult(NID_UI_CTRL, (__lastSelectedValue.Equals(dayString) == false), E_SUCCESS, "Day string matched.");
862
863                 newValue = dayString;
864                 rect = GetDateAreaBounds(DATETIME_ID_DAY);
865         }
866         else if (GetLastSelectedId() == DATETIME_ID_MONTH)
867         {
868                 SysTryReturnVoidResult(NID_UI_CTRL, (__lastSelectedValue.Equals(monthString) == false), E_SUCCESS, "Month string matched.");
869
870                 newValue = monthString;
871                 rect = GetDateAreaBounds(DATETIME_ID_MONTH);
872         }
873         else if (GetLastSelectedId() == DATETIME_ID_YEAR)
874         {
875                 SysTryReturnVoidResult(NID_UI_CTRL, (__lastSelectedValue.Equals(yearString) == false), E_SUCCESS, "Year string matched.");
876
877                 newValue = yearString;
878                 rect = GetDateAreaBounds(DATETIME_ID_YEAR);
879         }
880
881         FloatDimension newTextDim;
882         FloatDimension oldTextDim;
883         FloatPoint textPoint;
884         VisualElement* pNewVisualElement = null;
885         VisualElement* pOldVisualElement = null;
886         VisualElement* pEditDateElement = null;
887         VisualElementPropertyAnimation* pNewBoundsAnimation = null;
888         VisualElementPropertyAnimation* pOldBoundsAnimation = null;
889         Canvas *pCanvas = null;
890
891         __pFont->GetTextExtent(newValue, newValue.GetLength(), newTextDim);
892         __pFont->GetTextExtent(__lastSelectedValue, __lastSelectedValue.GetLength(), oldTextDim);
893
894         if (newTextDim.width > oldTextDim.width)
895         {
896                 textPoint.x = (rect.width - newTextDim.width) / 2.0f;
897         }
898         else
899         {
900                 textPoint.x = (rect.width - oldTextDim.width) / 2.0f;
901         }
902
903         SysTryReturnVoidResult(NID_UI_CTRL, (rect.x + textPoint.x < __pEditDate->GetBoundsF().width), E_SUCCESS, "Rolling animation cann't be played.");
904
905         __isAnimating = true;
906
907         __pContentProvider = new (std::nothrow) VisualElement();
908         SysTryReturnVoidResult(NID_UI_CTRL, (__pContentProvider != null), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
909
910         r = __pContentProvider->Construct();
911         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
912
913         __pContentProvider->SetShowState(true);
914         __pContentProvider->SetClipChildrenEnabled(true);
915         __pContentProvider->SetImplicitAnimationEnabled(false);
916
917         pEditDateElement = __pEditDate->GetVisualElement();
918         r = GetLastResult();
919         SysTryCatch(NID_UI_CTRL, (pEditDateElement != null), , r, "[%s] Propagating.", GetErrorMessage(r));
920
921         pNewVisualElement = new (std::nothrow) VisualElement();
922         SysTryCatch(NID_UI_CTRL, (pNewVisualElement != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
923
924         r = pNewVisualElement->Construct();
925         if (r != E_SUCCESS)
926         {
927                 pNewVisualElement->Destroy();
928         }
929         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
930
931         pOldVisualElement = new (std::nothrow) VisualElement();
932         if (pOldVisualElement == null)
933         {
934                 pNewVisualElement->Destroy();
935         }
936         SysTryCatch(NID_UI_CTRL, (pOldVisualElement != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
937
938         r = pOldVisualElement->Construct();
939         if (r != E_SUCCESS)
940         {
941                 pNewVisualElement->Destroy();
942                 pOldVisualElement->Destroy();
943         }
944         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
945
946         pNewVisualElement->SetShowState(true);
947         pOldVisualElement->SetShowState(true);
948
949         if (newTextDim.width > oldTextDim.width)
950         {
951                 textPoint.x = (rect.width - newTextDim.width) / 2.0f;
952                 textPoint.y = (rect.height - newTextDim.height) / 2.0f;
953                 __pContentProvider->SetBounds(FloatRectangle((rect.x + textPoint.x) * 1.0f, (rect.y + textPoint.y) * 1.0f, newTextDim.width * 1.0f, newTextDim.height * 1.0f));
954                 pNewVisualElement->SetBounds(FloatRectangle(0.0f, newTextDim.height * 1.0f, newTextDim.width * 1.0f, newTextDim.height * 1.0f));
955
956                 rect.x = static_cast<int>(__pContentProvider->GetBounds().x);
957                 rect.y = static_cast<int>(__pContentProvider->GetBounds().y);
958                 rect.width = static_cast<int>(__pContentProvider->GetBounds().width);
959                 rect.height = static_cast<int>(__pContentProvider->GetBounds().height);
960
961                 textPoint.x = (rect.width - oldTextDim.width) / 2.0f;
962                 textPoint.y = (rect.height - oldTextDim.height) / 2.0f;
963
964                 pOldVisualElement->SetBounds(FloatRectangle(textPoint.x * 1.0f, 0.0f, oldTextDim.width * 1.0f, oldTextDim.height * 1.0f));
965         }
966         else
967         {
968                 textPoint.x = (rect.width - oldTextDim.width) / 2.0f;
969                 textPoint.y = (rect.height - oldTextDim.height) / 2.0f;
970                 __pContentProvider->SetBounds(FloatRectangle((rect.x + textPoint.x) * 1.0f, (rect.y + textPoint.y) * 1.0f, oldTextDim.width * 1.0f, oldTextDim.height * 1.0f));
971                 pOldVisualElement->SetBounds(FloatRectangle(0.0f, 0.0f, oldTextDim.width * 1.0f, oldTextDim.height * 1.0f));
972
973                 rect.x = static_cast<int>(__pContentProvider->GetBounds().x);
974                 rect.y = static_cast<int>(__pContentProvider->GetBounds().y);
975                 rect.width = static_cast<int>(__pContentProvider->GetBounds().width);
976                 rect.height = static_cast<int>(__pContentProvider->GetBounds().height);
977
978                 textPoint.x = (rect.width - newTextDim.width) / 2.0f;
979                 textPoint.y = (rect.height - newTextDim.height) / 2.0f;
980
981                 pNewVisualElement->SetBounds(FloatRectangle(textPoint.x * 1.0f, newTextDim.height * 1.0f, newTextDim.width * 1.0f, newTextDim.height * 1.0f));
982         }
983
984
985         pCanvas = pEditDateElement->GetCanvasN(rect);
986         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
987         pCanvas->Clear();
988         delete pCanvas;
989
990         pEditDateElement->AttachChild(*__pContentProvider);
991
992         __pContentProvider->AttachChild(*pOldVisualElement);
993         __pContentProvider->AttachChild(*pNewVisualElement);
994
995         pNewBoundsAnimation = new (std::nothrow) VisualElementPropertyAnimation();
996         SysTryCatch(NID_UI_CTRL, (pNewBoundsAnimation != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
997
998         pNewBoundsAnimation->SetDuration(300);
999         pNewBoundsAnimation->SetPropertyName("bounds.position");
1000         pNewBoundsAnimation->SetStartValue(Variant(FloatPoint(pNewVisualElement->GetBounds().x * 1.0f, newTextDim.height * 1.0f)));
1001         pNewBoundsAnimation->SetEndValue(Variant(FloatPoint(pNewVisualElement->GetBounds().x * 1.0f, 0.0f)));
1002         pNewBoundsAnimation->SetVisualElementAnimationStatusEventListener(this);
1003
1004         pOldBoundsAnimation = new (std::nothrow) VisualElementPropertyAnimation();
1005         SysTryCatch(NID_UI_CTRL, (pOldBoundsAnimation != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1006
1007         pOldBoundsAnimation->SetDuration(300);
1008         pOldBoundsAnimation->SetPropertyName("bounds.position");
1009         pOldBoundsAnimation->SetStartValue(Variant(FloatPoint(pOldVisualElement->GetBounds().x * 1.0f, 0.0f)));
1010         pOldBoundsAnimation->SetEndValue(Variant(FloatPoint(pOldVisualElement->GetBounds().x * 1.0f, oldTextDim.height * -1.0f)));
1011         pOldBoundsAnimation->SetVisualElementAnimationStatusEventListener(this);
1012
1013         pCanvas = pOldVisualElement->GetCanvasN();
1014         r = GetLastResult();
1015         SysTryCatch(NID_UI_CTRL, (pCanvas != null), , r, "[%s] Propagating.", GetErrorMessage(r));
1016
1017         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
1018         pCanvas->Clear();
1019         pCanvas->SetFont(*__pFont);
1020         pCanvas->DrawText(FloatPoint(0.0f, 0.0f), __lastSelectedValue);
1021
1022         delete pCanvas;
1023         pCanvas = null;
1024
1025         pCanvas = pNewVisualElement->GetCanvasN();
1026         r = GetLastResult();
1027         SysTryCatch(NID_UI_CTRL, (pCanvas != null), , r, "[%s] Propagating.", GetErrorMessage(r));
1028
1029         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
1030         pCanvas->Clear();
1031         pCanvas->SetFont(*__pFont);
1032         pCanvas->DrawText(FloatPoint(0.0f, 0.0f), newValue);
1033
1034         delete pCanvas;
1035         pCanvas = null;
1036
1037         pOldVisualElement->SetImplicitAnimationEnabled(false);
1038         pOldVisualElement->AddAnimation(*pOldBoundsAnimation);
1039
1040         pNewVisualElement->SetImplicitAnimationEnabled(false);
1041         pNewVisualElement->AddAnimation(*pNewBoundsAnimation);
1042
1043         delete pOldBoundsAnimation;
1044         delete pNewBoundsAnimation;
1045
1046         return;
1047
1048         CATCH:
1049         __isAnimating = false;
1050         __pContentProvider->Destroy();
1051
1052         delete pNewBoundsAnimation;
1053         pNewBoundsAnimation = null;
1054
1055         delete pOldBoundsAnimation;
1056         pOldBoundsAnimation = null;
1057
1058         delete pCanvas;
1059         pCanvas = null;
1060
1061         return;
1062 }
1063
1064 void
1065 _EditDatePresenter::OnVisualElementAnimationFinished (const VisualElementAnimation &animation, const String &keyName, VisualElement &target, bool completedNormally)
1066 {
1067         result r = E_SUCCESS;
1068         __isAnimating = false;
1069
1070         VisualElement* pEditDateElement = __pEditDate->GetVisualElement();
1071         r = GetLastResult();
1072         SysTryReturnVoidResult(NID_UI_CTRL, (pEditDateElement != null), r, "[%s] Propagating.", GetErrorMessage(r));
1073
1074         pEditDateElement->DetachChild(*__pContentProvider);
1075         __pContentProvider->Destroy();
1076
1077         Draw();
1078         return;
1079 }
1080
1081 }}} // Tizen::Ui::Controls