modify license, permission and remove ^M char
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_DateTimeBarPresenter.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an ”AS IS” BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FUiCtrl_DateTimeBarPresenter.cpp
20  * @brief               This is the implementation file for the _DateTimeBarPresenter class.
21  */
22
23 #include <FGrp_BitmapImpl.h>
24 #include <FGrp_FontImpl.h>
25 #include <FGrp_TextTextSimple.h>
26 #include "FUi_CoordinateSystemUtils.h"
27 #include "FUi_ResourceManager.h"
28 #include "FUiCtrl_DateTimeUtils.h"
29 #include "FUiCtrl_DateTimeBar.h"
30 #include "FUiCtrl_DateTimeBarPresenter.h"
31
32 using namespace Tizen::Graphics;
33 using namespace Tizen::Ui;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Graphics::_Text;
37
38 const int UPDATE_ITEM_COUNT = 3;
39
40 namespace Tizen { namespace Ui { namespace Controls
41 {
42
43 _DateTimeBarPresenter::_DateTimeBarPresenter(_DateTimeBar* pDateTimeBar)
44         : __pDateTimeBar(pDateTimeBar)
45         , __pDateTimeBarModel(null)
46         , __currentPoint(FloatPoint(0.0f, 0.0f))
47         , __isTouchMoved(false)
48         , __touchMoveHandled(false)
49         , __isFlickEnabled(true)
50         , __isFlickInProgress(false)
51         , __distance(0.0f)
52         , __pFlickAnimationTimer(null)
53         , __flickAnimation()
54         , __selectedText(L"")
55         , __bodyAreaBounds(FloatRectangle())
56         , __arrowAreaBounds(FloatRectangle())
57         , __windowAreaBounds(FloatRectangle())
58         , __pBgColorReplacementBitmap(null)
59         , __pArrowColorReplacementBitmap(null)
60         , __isInitialAnimation(false)
61         , __initialAnimationValue(0.0f)
62         , __pFont(null)
63 {
64 }
65
66 _DateTimeBarPresenter::~_DateTimeBarPresenter(void)
67 {
68         delete __pDateTimeBarModel;
69         __pDateTimeBarModel = 0;
70
71         delete __pBgColorReplacementBitmap;
72         __pBgColorReplacementBitmap = null;
73
74         delete __pArrowColorReplacementBitmap;
75         __pArrowColorReplacementBitmap = null;
76
77         delete __pFlickAnimationTimer;
78         __pFlickAnimationTimer = null;
79
80         delete __pFont;
81         __pFont = null;
82 }
83
84 _DateTimeBarPresenter*
85 _DateTimeBarPresenter::CreateInstanceN(_DateTimeBar& dateTimeBar)
86 {
87         result r = E_SUCCESS;
88
89         _DateTimeBarPresenter* pDateTimeBarPresenter = new (std::nothrow) _DateTimeBarPresenter(&dateTimeBar);
90         SysTryReturn(NID_UI_CTRL, pDateTimeBarPresenter, null, E_OUT_OF_MEMORY,
91                         "[E_OUT_OF_MEMORY] Memory allocation failed.");
92
93         pDateTimeBarPresenter->__pDateTimeBarModel = _DateTimeBarModel::CreateInstanceN();
94         r = GetLastResult();
95         SysTryCatch(NID_UI_CTRL, pDateTimeBarPresenter->__pDateTimeBarModel, , r,
96                         "[%s] Propagating.", GetErrorMessage(r));
97
98         r = GET_SHAPE_CONFIG(DATETIMEBAR::ARROW_WIDTH, dateTimeBar.GetOrientation(), pDateTimeBarPresenter->__arrowAreaBounds.width);
99         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to get arrow width from resource.", GetErrorMessage(r));
100
101         r = GET_SHAPE_CONFIG(DATETIMEBAR::ARROW_HEIGHT, dateTimeBar.GetOrientation(), pDateTimeBarPresenter->__arrowAreaBounds.height);
102         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to get arrow height from resource.", GetErrorMessage(r));
103
104         r = pDateTimeBarPresenter->LoadResource();
105         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to load the resources.", GetErrorMessage(r));
106
107         return pDateTimeBarPresenter;
108
109 CATCH:
110         delete pDateTimeBarPresenter;
111
112         return null;
113 }
114
115 void
116 _DateTimeBarPresenter::AddFlickAnimationInfo()
117 {
118         __flickAnimation.SetSensitivity(FLICK_ANIMATION_FPS_DATE_TIME_BAR, FLICK_ANIMATION_SENSITIVITY_DATE_TIME_BAR);
119         __flickAnimation.SetDirection(FD_HORIZONTAL);
120 }
121
122 result
123 _DateTimeBarPresenter::LoadResource(void)
124 {
125         result r = E_SUCCESS;
126         Color bgBitmapColor;
127         Bitmap* pBackgroundNormalBitmap = null;
128
129         r = GET_BITMAP_CONFIG_N(DATETIMEBAR::TIMEPICKERBAR_BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pBackgroundNormalBitmap);
130         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
131
132         GET_COLOR_CONFIG(DATETIMEBAR::BG_NORMAL, bgBitmapColor);
133         __pBgColorReplacementBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pBackgroundNormalBitmap,
134                                                                            Color::GetColor(COLOR_ID_MAGENTA), bgBitmapColor);
135         SysTryCatch(NID_UI_CTRL, __pBgColorReplacementBitmap != null, r = GetLastResult(), GetLastResult(), "[%s] Propagating.",
136                     GetErrorMessage(GetLastResult()));
137
138         r = LoadArrowBitmap();
139         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to load resources.", GetErrorMessage(r));
140
141         delete pBackgroundNormalBitmap;
142         pBackgroundNormalBitmap = null;
143
144         AddFlickAnimationInfo();
145
146         return r;
147
148 CATCH:
149
150         delete pBackgroundNormalBitmap;
151         pBackgroundNormalBitmap = null;
152
153         delete __pBgColorReplacementBitmap;
154         __pBgColorReplacementBitmap = null;
155
156         return r;
157 }
158
159 result
160 _DateTimeBarPresenter::LoadArrowBitmap(void)
161 {
162         result r = E_SUCCESS;
163         Color arrowColor;
164         Bitmap* pArrowNormalBitmap = null;
165
166         GET_COLOR_CONFIG(DATETIMEBAR::BG_NORMAL, arrowColor);
167
168         if (__pDateTimeBar->GetAlignment() == DATETIME_BAR_ALIGN_DOWN)
169         {
170                 r = GET_BITMAP_CONFIG_N(DATETIMEBAR::TAIL_DOWN, BITMAP_PIXEL_FORMAT_ARGB8888, pArrowNormalBitmap);
171                 SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
172         }
173         else
174         {
175                 r = GET_BITMAP_CONFIG_N(DATETIMEBAR::TAIL_UP, BITMAP_PIXEL_FORMAT_ARGB8888, pArrowNormalBitmap);
176                 SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
177         }
178
179         if (__pArrowColorReplacementBitmap != null)
180         {
181                 delete __pArrowColorReplacementBitmap;
182                 __pArrowColorReplacementBitmap = null;
183         }
184
185         __pArrowColorReplacementBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pArrowNormalBitmap,
186                                                                                 Color::GetColor(COLOR_ID_MAGENTA), arrowColor);
187         SysTryCatch(NID_UI_CTRL, __pArrowColorReplacementBitmap != null, r = GetLastResult(), GetLastResult(), "[%s] Propagating.",
188                         GetErrorMessage(GetLastResult()));
189
190         delete pArrowNormalBitmap;
191         pArrowNormalBitmap = null;
192
193         return r;
194
195 CATCH:
196
197         delete pArrowNormalBitmap;
198         pArrowNormalBitmap = null;
199
200         delete __pArrowColorReplacementBitmap;
201         __pArrowColorReplacementBitmap = null;
202
203         return r;
204 }
205
206 result
207 _DateTimeBarPresenter::Draw(void)
208 {
209         result r = E_SUCCESS;
210
211         Canvas* pCanvas = __pDateTimeBar->GetCanvasN();
212         SysAssertf((pCanvas != null), "Failed to get canvas.");
213
214         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
215
216         r = pCanvas->Clear();
217         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
218
219         r = DrawBackground(*pCanvas);
220         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
221
222         r = DrawItem(*pCanvas);
223         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
224
225         r = DrawArrow(*pCanvas);
226         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
227
228         delete pCanvas;
229         return r;
230
231 CATCH:
232         delete pCanvas;
233         return r;
234 }
235
236 result
237 _DateTimeBarPresenter::DrawItem(Canvas& canvas)
238 {
239         result r = E_SUCCESS;
240
241         FloatRectangle clientBounds = canvas.GetBoundsF();
242
243         _DateTimeBarItem* pDrawItem = null;
244         FloatRectangle itemBounds(0.0f, 0.0f, 0.0f, 0.0f);
245
246         String drawText;
247
248         Color textColor;
249         GET_COLOR_CONFIG(DATETIMEBAR::TEXT_NORMAL, textColor);
250
251         Color textPressedColor;
252         GET_COLOR_CONFIG(DATETIMEBAR::TEXT_PRESSED, textPressedColor);
253
254         _DateTimeBarItemStatus itemStatus = DATETIMEBAR_ITEM_STATUS_NORMAL;
255
256         int itemCount = GetItemCount();
257
258         RefreshAccessibilityElement();
259
260         for (int i = __pDateTimeBarModel->GetFirstDrawnItemIndex(); i < itemCount; i++)
261         {
262                 pDrawItem = __pDateTimeBarModel->GetItemAt(i);
263                 if (pDrawItem == null)
264                 {
265                         break;
266                 }
267
268                 itemBounds = pDrawItem->GetBounds();
269
270                 if (itemBounds.x + itemBounds.width <= 0)
271                 {
272                         continue;
273                 }
274
275                 drawText = pDrawItem->GetText();
276
277                 if (drawText.CompareTo(__selectedText) == 0)
278                 {
279                         pDrawItem->SetStatus(DATETIMEBAR_ITEM_STATUS_SELECTED);
280                 }
281
282                 itemStatus = pDrawItem->GetStatus();
283
284                 float fontSize = GetFontSize();
285
286                 TextSimple* pSimpleText = null;
287                 TextObject* pTextObject = new (std::nothrow) TextObject;
288                 SysTryReturn(NID_UI_CTRL, (pTextObject != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
289
290                 pTextObject->Construct();
291                 pSimpleText = new (std::nothrow)TextSimple((const_cast <wchar_t*>(drawText.GetPointer())), drawText.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
292                 pTextObject->AppendElement(*pSimpleText);
293
294                 pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
295                 pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
296                 pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
297
298                 SysAssertf(__pFont != null, "Font instance is null");
299
300                 if (itemStatus == DATETIMEBAR_ITEM_STATUS_SELECTED)
301                 {
302                         (_FontImpl::GetInstance(*__pFont))->SetSize(fontSize);
303                         (_FontImpl::GetInstance(*__pFont))->SetStyle(FONT_STYLE_BOLD);
304                         pTextObject->SetFont(__pFont, 0, pTextObject->GetTextLength());
305                         pTextObject->SetForegroundColor(textPressedColor, 0, pTextObject->GetTextLength());
306                 }
307                 else
308                 {
309                         (_FontImpl::GetInstance(*__pFont))->SetSize(fontSize);
310                         (_FontImpl::GetInstance(*__pFont))->SetStyle(FONT_STYLE_PLAIN);
311                         pTextObject->SetFont(__pFont, 0, pTextObject->GetTextLength());
312                         pTextObject->SetForegroundColor(textColor, 0, pTextObject->GetTextLength());
313                 }
314
315                 pTextObject->SetBounds(itemBounds);
316                 pTextObject->Draw(*_CanvasImpl::GetInstance(canvas));
317
318                 delete pTextObject;
319
320                 if ((itemBounds.x + itemBounds.width + GetLeftRightMargin()) >= clientBounds.width)
321                 {
322                         break;
323                 }
324         }
325
326         return r;
327 }
328
329 result
330 _DateTimeBarPresenter::DrawBackground(Canvas& canvas)
331 {
332         result r = E_SUCCESS;
333
334         FloatRectangle bodyAreaBounds = GetBodyBounds();
335
336         if (__pBgColorReplacementBitmap != null)
337         {
338                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pBgColorReplacementBitmap))
339                 {
340                         r = canvas.DrawNinePatchedBitmap(bodyAreaBounds, *__pBgColorReplacementBitmap);
341                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
342                 }
343                 else
344                 {
345                         r = canvas.DrawBitmap(bodyAreaBounds, *__pBgColorReplacementBitmap);
346                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
347                 }
348         }
349
350         else
351         {
352                 r = canvas.DrawRectangle(bodyAreaBounds);
353                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
354         }
355
356         return r;
357 }
358
359 result
360 _DateTimeBarPresenter::DrawArrow(Canvas& canvas)
361 {
362         result r = E_SUCCESS;
363         FloatRectangle arrowAreaBounds = GetArrowBounds();
364
365         if (__pArrowColorReplacementBitmap != null)
366         {
367                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pArrowColorReplacementBitmap))
368                 {
369                         r = canvas.DrawNinePatchedBitmap(arrowAreaBounds, *__pArrowColorReplacementBitmap);
370                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
371                 }
372                 else
373                 {
374                         r = canvas.DrawBitmap(arrowAreaBounds, *__pArrowColorReplacementBitmap);
375                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
376                 }
377         }
378
379         return r;
380 }
381
382 void
383 _DateTimeBarPresenter::SetItemSelected(int index)
384 {
385         __pDateTimeBarModel->SetSelectedItemIndex(index);
386         return;
387 }
388
389 int
390 _DateTimeBarPresenter::GetSelectedItemIndex(void) const
391 {
392         return __pDateTimeBarModel->GetSelectedItemIndex();
393 }
394
395 int
396 _DateTimeBarPresenter::GetItemIndexFromPosition(const FloatPoint& point) const
397 {
398         FloatRectangle clientBounds = __pDateTimeBar->GetBoundsF();
399
400         if (point.x > clientBounds.width
401                 || point.y > clientBounds.height)
402         {
403                 return -1;
404         }
405
406         int itemIndex = __pDateTimeBarModel->GetFirstDrawnItemIndex();
407         _DateTimeBarItem* pItem = __pDateTimeBarModel->GetItemAt(itemIndex);
408         float startX = 0.0f;
409         float itemMargin = 0.0f;
410         itemMargin = GetItemMargin();
411
412         while (pItem != null)
413         {
414                 FloatRectangle itemBounds = pItem->GetBounds();
415
416                 if (itemBounds.x > clientBounds.x)
417                 {
418                         if (itemIndex == __pDateTimeBarModel->GetFirstDrawnItemIndex())
419                         {
420                                 startX = itemBounds.x;
421                         }
422
423                         itemBounds.x -= startX;
424                 }
425
426                 itemBounds.x = itemBounds.x - (itemMargin / 2.0f);
427                 itemBounds.width = itemBounds.width + itemMargin;
428
429                 if (itemBounds.Contains(point) == true)
430                 {
431                         return itemIndex;
432                 }
433
434                 itemIndex++;
435
436                 if (itemIndex == GetItemCount())
437                 {
438                         break;
439                 }
440
441                 pItem = __pDateTimeBarModel->GetItemAt(itemIndex);
442         }
443
444         return -1;
445 }
446
447 result
448 _DateTimeBarPresenter::SetItemStatus(int index, _DateTimeBarItemStatus status)
449 {
450         _DateTimeBarItem* pItem = __pDateTimeBarModel->GetItemAt(index);
451         SysTryReturn(NID_UI_CTRL, pItem, E_SYSTEM, E_SYSTEM,
452                  "[E_SYSTEM] A system error has occurred. Failed to get _DateTimeBarItem at index (%d).", index);
453
454         if (status != DATETIMEBAR_ITEM_STATUS_NORMAL)
455         {
456                 __pDateTimeBarModel->SetSelectedItemIndex(index);
457         }
458         pItem->SetStatus(status);
459
460         return E_SUCCESS;
461 }
462
463 _DateTimeBarItemStatus
464 _DateTimeBarPresenter::GetItemStatus(int index) const
465 {
466         _DateTimeBarItem* pItem = __pDateTimeBarModel->GetItemAt(index);
467         SysTryReturn(NID_UI_CTRL, pItem, DATETIMEBAR_ITEM_STATUS_NORMAL, E_SYSTEM,
468                  "[E_SYSTEM] A system error has occurred. Failed to get _DateTimeBarItem at index (%d).", index);
469
470         return pItem->GetStatus();
471 }
472
473 int
474 _DateTimeBarPresenter::GetFirstDrawnItemIndex(void) const
475 {
476         return __pDateTimeBarModel->GetFirstDrawnItemIndex();
477 }
478
479 void
480 _DateTimeBarPresenter::SetFirstDrawnItemIndex(int index)
481 {
482         __pDateTimeBarModel->SetFirstDrawnItemIndex(index);
483         return;
484 }
485
486 int
487 _DateTimeBarPresenter::GetMinimumValue(void) const
488 {
489         return __pDateTimeBarModel->GetMinimumValue();
490 }
491
492 void
493 _DateTimeBarPresenter::SetMinimumValue(int minValue)
494 {
495         __pDateTimeBarModel->SetMinimumValue(minValue);
496         return;
497 }
498
499 int
500 _DateTimeBarPresenter::GetMaximumValue(void) const
501 {
502         return __pDateTimeBarModel->GetMaximumValue();
503 }
504
505 void
506 _DateTimeBarPresenter::SetMaximumValue(int maxValue)
507 {
508         __pDateTimeBarModel->SetMaximumValue(maxValue);
509         return;
510 }
511
512 float
513 _DateTimeBarPresenter::GetItemWidth(void) const
514 {
515         result r = E_SUCCESS;
516
517         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
518
519         float itemWidth = 0.0f;
520
521         if (boxId == DATETIME_ID_YEAR)
522         {
523                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
524         }
525         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
526         {
527                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
528         }
529         else if (boxId == DATETIME_ID_MONTH)
530         {
531                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
532
533                 FloatDimension textArea;
534
535                 float fontSize = 0;
536                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_FONT_SIZE, __pDateTimeBar->GetOrientation(), fontSize);
537
538                 SysAssertf(__pFont != null, "Font instance is null");
539
540                 (_FontImpl::GetInstance(*__pFont))->SetSize(fontSize);
541                 (_FontImpl::GetInstance(*__pFont))->SetStyle(FONT_STYLE_PLAIN);
542
543                 for (int i = DATETIME_MONTH_MIN; i <= DATETIME_MONTH_MAX; i++)
544                 {
545                         String string;
546                         _DateTimeUtils dateTimeUtils;
547                         string = dateTimeUtils.GetMonthString(i);
548
549                         r = __pFont->GetTextExtent(string, string.GetLength(), textArea);
550                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
551
552                         if (textArea.width > itemWidth)
553                         {
554                                 itemWidth = textArea.width;
555                         }
556                 }
557         }
558
559         return itemWidth;
560 }
561
562 float
563 _DateTimeBarPresenter::GetFontSize(void) const
564 {
565         float fontSize = 0.0f;
566         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
567
568         if (boxId == DATETIME_ID_YEAR)
569         {
570                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_FONT_SIZE, _ControlManager::GetInstance()->GetOrientation(), fontSize);
571         }
572         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
573         {
574                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_FONT_SIZE, _ControlManager::GetInstance()->GetOrientation(), fontSize);
575         }
576         else if (boxId == DATETIME_ID_MONTH)
577         {
578                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_FONT_SIZE, _ControlManager::GetInstance()->GetOrientation(), fontSize);
579         }
580
581         return fontSize;
582 }
583
584 float
585 _DateTimeBarPresenter::GetLeftRightMargin(void) const
586 {
587         float leftRightMargin = 0.0f;
588         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
589
590         if (boxId == DATETIME_ID_YEAR)
591         {
592                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_LEFT_RIGHT_MARGIN, _ControlManager::GetInstance()->GetOrientation(), leftRightMargin);
593         }
594         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
595         {
596                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_LEFT_RIGHT_MARGIN, _ControlManager::GetInstance()->GetOrientation(), leftRightMargin);
597         }
598         else if (boxId == DATETIME_ID_MONTH)
599         {
600                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_LEFT_RIGHT_MARGIN, _ControlManager::GetInstance()->GetOrientation(), leftRightMargin);
601         }
602
603         return leftRightMargin;
604 }
605
606 float
607 _DateTimeBarPresenter::GetItemMargin(void) const
608 {
609         float margin = 0.0f;
610         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
611
612         if (boxId == DATETIME_ID_YEAR)
613         {
614                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_ITEM_MARGIN, _ControlManager::GetInstance()->GetOrientation(), margin);
615         }
616         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
617         {
618                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_ITEM_MARGIN, _ControlManager::GetInstance()->GetOrientation(), margin);
619         }
620         else if (boxId == DATETIME_ID_MONTH)
621         {
622                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_ITEM_MARGIN, _ControlManager::GetInstance()->GetOrientation(), margin);
623         }
624
625         return margin;
626 }
627
628 result
629 _DateTimeBarPresenter::AdjustItemPosition(float distance)
630 {
631         int index = 0;
632
633         _DateTimeBarItem* pItem = null;
634         pItem = __pDateTimeBarModel->GetItemAt(index);
635
636         while (pItem != null)
637         {
638                 FloatRectangle bounds = pItem->GetBounds();
639                 bounds.x += distance;
640                 pItem->SetBounds(bounds);
641                 index++;
642
643                 if (index == GetItemCount())
644                 {
645                         break;
646                 }
647
648                 pItem = __pDateTimeBarModel->GetItemAt(index);
649         }
650
651         return E_SUCCESS;
652 }
653
654 String
655 _DateTimeBarPresenter::GetDisplayedText(int value) const
656 {
657         String string;
658
659         if (__pDateTimeBar->GetSelectedBoxId() == DATETIME_ID_MONTH)
660         {
661                 String monthString;
662                 _DateTimeUtils dateTimeUtils;
663                 monthString = dateTimeUtils.GetMonthString(value);
664                 string.Append(monthString);
665         }
666         else if (__pDateTimeBar->GetSelectedBoxId() == DATETIME_ID_YEAR)
667         {
668                 string.Format(10, L"%04d", value);
669         }
670         else
671         {
672                 string.Format(10, L"%02d", value);
673         }
674
675         return string;
676 }
677
678 result
679 _DateTimeBarPresenter::AddItems(int actionId)
680 {
681         result r = E_SUCCESS;
682         int cachingSize = __pDateTimeBarModel->GetMaxCachingSize() / 2;
683
684         int firstDrawItemIndex = cachingSize;
685
686         int value = actionId;
687         float startPosition = 0.0f;
688         float itemWidth = GetItemWidth();
689         float itemMargin = GetItemMargin();
690
691         startPosition = (GetWindowBounds().width) / 2.0f;
692         startPosition = startPosition + (itemWidth / 2.0f) + itemMargin;
693         __pDateTimeBarModel->SetItemStartPosition(startPosition);
694
695         while (cachingSize > 0)
696         {
697                 String string;
698
699                 string = GetDisplayedText(value);
700
701                 r = __pDateTimeBarModel->AddItem(string, value, __pDateTimeBar->GetAlignment(), itemWidth, itemMargin);
702                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
703
704                 cachingSize--;
705
706                 value++;
707
708                 if (__pDateTimeBarModel->GetMaximumValue() < value)
709                 {
710                         value = __pDateTimeBarModel->GetMinimumValue();
711                 }
712         }
713
714         r = InsertItems(actionId -1);
715         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
716
717         _DateTimeBarItem* pItem = null;
718         pItem = GetItemAt(firstDrawItemIndex);
719
720         if (pItem != null)
721         {
722                 __selectedText.Clear();
723                 __selectedText.Append(pItem->GetText().GetPointer());
724         }
725
726         SetItemStatus(firstDrawItemIndex, DATETIMEBAR_ITEM_STATUS_SELECTED);
727
728         return r;
729 }
730
731 result
732 _DateTimeBarPresenter::InsertItems(int actionId)
733 {
734         result r = E_SUCCESS;
735
736         int cachingSize = __pDateTimeBarModel->GetMaxCachingSize() / 2;
737
738         int value = actionId;
739         float itemWidth = GetItemWidth();
740         float itemMargin = GetItemMargin();
741
742         while (cachingSize > 0)
743         {
744                 String string;
745
746                 if (value < __pDateTimeBarModel->GetMinimumValue())
747                 {
748                         value = __pDateTimeBarModel->GetMaximumValue();
749                 }
750
751                 string = GetDisplayedText(value);
752                 r = __pDateTimeBarModel->InsertItemAt(0, string, value, __pDateTimeBar->GetAlignment(), itemWidth, itemMargin);
753                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
754
755                 cachingSize--;
756
757                 value--;
758         }
759
760         return r;
761 }
762
763 result
764 _DateTimeBarPresenter::InsertItem(int index, int actionId)
765 {
766         result r = E_SUCCESS;
767
768         String string;
769
770         string = GetDisplayedText(actionId);
771
772         r = __pDateTimeBarModel->InsertItemAt(index, string, actionId, __pDateTimeBar->GetAlignment(), GetItemWidth(), GetItemMargin());
773         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
774
775         return r;
776 }
777
778 result
779 _DateTimeBarPresenter::AddItem(int actionId)
780 {
781         result r = E_SUCCESS;
782
783         String string;
784
785         string = GetDisplayedText(actionId);
786
787         r = __pDateTimeBarModel->AddItem(string, actionId, __pDateTimeBar->GetAlignment(), GetItemWidth(), GetItemMargin());
788         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
789
790         return r;
791 }
792
793 result
794 _DateTimeBarPresenter::DeleteItem(int index)
795 {
796         int itemCount = GetItemCount();
797
798         SysTryReturn(NID_UI_CTRL, itemCount > 0, E_INVALID_ARG, E_INVALID_ARG,
799                         "[E_INVALID_ARG] Invalid argument is used. index = %d", index);
800
801         SysTryReturn(NID_UI_CTRL, index < itemCount, E_INVALID_ARG, E_INVALID_ARG,
802                         "[E_INVALID_ARG] Invalid argument is used. index = %d", index);
803
804         SysTryReturn(NID_UI_CTRL, index >= 0, E_INVALID_ARG, E_INVALID_ARG,
805                         "[E_INVALID_ARG] Invalid argument is used. index = %d", index);
806
807         result r = E_SUCCESS;
808
809         r = __pDateTimeBarModel->RemoveItemAt(index);
810         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
811
812         return r;
813 }
814
815 result
816 _DateTimeBarPresenter::RemoveAllItems(void)
817 {
818         SysTryReturn(NID_UI_CTRL, GetItemCount() > 0, E_INVALID_OPERATION, E_INVALID_OPERATION,
819                                 "[E_INVALID_OPERATION] Items count is zero.");
820
821         result r = E_SUCCESS;
822
823         __pDateTimeBarModel->RemoveAllItems();
824
825         return r;
826 }
827
828 int
829 _DateTimeBarPresenter::GetItemCount(void) const
830 {
831         return __pDateTimeBarModel->GetItemCount();
832 }
833
834 _DateTimeBarItem*
835 _DateTimeBarPresenter::GetItemAt(int index) const
836 {
837         return __pDateTimeBarModel->GetItemAt(index);
838 }
839
840 result
841 _DateTimeBarPresenter::CalculateWindowBounds(void)
842 {
843         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
844         FloatRectangle windowAreaBounds = FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f);
845         FloatRectangle bodyAreaBounds = FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f);
846         FloatRectangle arrowAreaBounds = FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f);
847
848         FloatRectangle parentWindowBounds = __pDateTimeBar->GetParentWindowBounds();
849
850         arrowAreaBounds = GetArrowBounds();
851
852         Point arrowPosition;
853
854         GET_SHAPE_CONFIG(DATETIMEBAR::ARROW_HEIGHT, orientation, arrowAreaBounds.height);
855         GET_SHAPE_CONFIG(DATETIMEBAR::ARROW_WIDTH, orientation, arrowAreaBounds.width);
856         GET_SHAPE_CONFIG(DATETIMEBAR::ITEM_HEIGHT, orientation, bodyAreaBounds.height);
857
858         bodyAreaBounds.width = parentWindowBounds.width;
859
860         bodyAreaBounds.x = 0.0f;
861         FloatPoint tempPoint = __pDateTimeBar->GetPositionF();
862         windowAreaBounds.x = tempPoint.x;
863
864         if (__pDateTimeBar->GetAlignment() == DATETIME_BAR_ALIGN_DOWN)
865         {
866                 bodyAreaBounds.y = arrowAreaBounds.height;
867                 arrowAreaBounds.y = 0.0f;
868                 windowAreaBounds.y = tempPoint.y - arrowAreaBounds.height;
869         }
870         else
871         {
872                 arrowAreaBounds.y = bodyAreaBounds.height;
873                 bodyAreaBounds.y = 0.0f;
874                 windowAreaBounds.y = tempPoint.y;
875         }
876
877         windowAreaBounds.width = bodyAreaBounds.width;
878         windowAreaBounds.height = bodyAreaBounds.height + arrowAreaBounds.height;
879
880         SetBodyBounds(bodyAreaBounds);
881         SetArrowBounds(arrowAreaBounds);
882         SetWindowBounds(windowAreaBounds);
883         __pDateTimeBar->SetMovable(true);
884         __pDateTimeBar->SetResizable(true);
885         __pDateTimeBar->SetBounds(windowAreaBounds);
886         __pDateTimeBar->SetMovable(false);
887         __pDateTimeBar->SetResizable(false);
888
889         return E_SUCCESS;
890 }
891
892 void
893 _DateTimeBarPresenter::SetBodyBounds(const FloatRectangle& bounds)
894 {
895         __bodyAreaBounds = bounds;
896         return;
897 }
898
899 FloatRectangle
900 _DateTimeBarPresenter::GetBodyBounds(void) const
901 {
902         return __bodyAreaBounds;
903 }
904
905 void
906 _DateTimeBarPresenter::SetArrowBounds(const FloatRectangle& bounds)
907 {
908         __arrowAreaBounds = bounds;
909         return;
910 }
911
912 FloatRectangle
913 _DateTimeBarPresenter::GetArrowBounds(void) const
914 {
915         return __arrowAreaBounds;
916 }
917
918 void
919 _DateTimeBarPresenter::SetWindowBounds(const FloatRectangle& bounds)
920 {
921         __windowAreaBounds = bounds;
922         return;
923 }
924
925 FloatRectangle
926 _DateTimeBarPresenter::GetWindowBounds(void) const
927 {
928         return __windowAreaBounds;
929 }
930
931 bool
932 _DateTimeBarPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
933 {
934         if (&source != __pDateTimeBar)
935         {
936                 return false;
937         }
938
939         __isFlickEnabled = true;
940
941         if (GetBodyBounds().Contains(touchinfo.GetCurrentPosition()) == false)
942         {
943                 __isFlickEnabled = false;
944                 return true;
945         }
946
947         if (__isFlickInProgress)
948         {
949                 ResetFlickAnimationTimer();
950         }
951
952         __touchMoveHandled = false;
953
954         __currentPoint = touchinfo.GetCurrentPosition();
955
956         int index = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
957
958         if (index == -1)
959         {
960                 return true;
961         }
962
963         __pDateTimeBar->Invalidate();
964
965         return true;
966 }
967
968 bool
969 _DateTimeBarPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
970 {
971         if (&source != __pDateTimeBar)
972         {
973                 return false;
974         }
975
976         if (GetBodyBounds().Contains(touchinfo.GetCurrentPosition()) == false && !__isFlickInProgress && !__isTouchMoved)
977         {
978                 __isTouchMoved = false;
979                 PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_TAP);
980                 __pDateTimeBar->SetVisibleState(false);
981                 __pDateTimeBar->Close();
982                 ResetFlickAnimationTimer();
983                 __pDateTimeBar->FireDateTimeChangeEvent(DATE_INTERNAL_CHANGE_CANCELED);
984                 return true;
985         }
986
987         int index = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
988         bool isEventFire = true;
989
990         if (index != -1 && __isTouchMoved == false && __isFlickInProgress == false)
991         {
992                 PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_TAP);
993                 SetItemStatus(index, DATETIMEBAR_ITEM_STATUS_SELECTED);
994                 __pDateTimeBarModel->SetFirstDrawnItemIndex(index);
995         }
996         else
997         {
998                 isEventFire = false;
999         }
1000
1001         __isTouchMoved = false;
1002         __distance = 0.0f;
1003
1004         __pDateTimeBar->Invalidate();
1005
1006         if (isEventFire == true)
1007         {
1008                 int index = GetFirstDrawnItemIndex();
1009
1010                 _DateTimeBarItem* pItem = null;
1011
1012                 if (index >= 0)
1013                 {
1014                         pItem = GetItemAt(index);
1015                 }
1016
1017                 if (pItem != null)
1018                 {
1019                         __pDateTimeBar->SetVisibleState(false);
1020                         __pDateTimeBar->Close();
1021
1022                         __pDateTimeBar->FireActionEvent(pItem->GetActionId());
1023
1024                         if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_YEAR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_DAY)
1025                         {
1026                                 __pDateTimeBar->FireDateTimeChangeEvent(DATE_INTERNAL_CHANGE_SAVED);
1027                         }
1028                         else if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_HOUR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_MINUTE)
1029                         {
1030                                 __pDateTimeBar->FireDateTimeChangeEvent(TIME_INTERNAL_CHANGE_SAVED);
1031                         }
1032                 }
1033         }
1034
1035         return true;
1036 }
1037
1038 bool
1039 _DateTimeBarPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
1040 {
1041         if (&source != __pDateTimeBar)
1042         {
1043                 return false;
1044         }
1045
1046         if (GetBodyBounds().Contains(touchinfo.GetCurrentPosition()) == false || __touchMoveHandled == true)
1047         {
1048                 return true;
1049         }
1050
1051         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
1052
1053         __isTouchMoved = true;
1054
1055         FloatDimension screen(0.0f, 0.0f);
1056         GET_DIMENSION_CONFIG(DATETIMEBAR::DEFAULT_SIZE, orientation, screen);
1057
1058         const float _BOUNDARY_X_POSITION = screen.width * 2.0f;
1059         float distance = touchinfo.GetCurrentPosition().x - __currentPoint.x;
1060
1061         _DateTimeBarItem* pItem = null;
1062
1063         FloatRectangle itemBounds;
1064
1065         bool needItem = false;
1066         int updateItemCount = 1;
1067
1068         if (distance > 0.0f)
1069         {
1070                 pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1071
1072                 if (pItem == null)
1073                 {
1074                         return true;
1075                 }
1076
1077                 itemBounds = pItem->GetBounds();
1078
1079                 __distance += distance;
1080
1081                 if (__distance > itemBounds.width / 2.0f)
1082                 {
1083                         needItem = true;
1084                         if (__distance > itemBounds.width)
1085                         {
1086                                 updateItemCount = UPDATE_ITEM_COUNT;
1087                         }
1088                 }
1089                 if (needItem == true)
1090                 {
1091                         while (updateItemCount > 0)
1092                         {
1093                                 pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1094                                 if (pItem == null)
1095                                 {
1096                                         break;
1097                                 }
1098
1099                                 int actionId = pItem->GetActionId() - 1;
1100
1101                                 if (__pDateTimeBarModel->GetMinimumValue() > actionId)
1102                                 {
1103                                         actionId = __pDateTimeBarModel->GetMaximumValue();
1104                                 }
1105
1106                                 if (pItem->GetBounds().x > _BOUNDARY_X_POSITION * -1.0f)
1107                                 {
1108                                         if (InsertItem(0, actionId) == E_SUCCESS)
1109                                         {
1110                                                 SetFirstDrawnItemIndex(0);
1111                                         }
1112                                 }
1113
1114                                 if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1115                                 {
1116                                         DeleteItem(GetItemCount() - 1);
1117                                 }
1118
1119                                 updateItemCount--;
1120                         }
1121                         __distance = 0.0f;
1122                 }
1123         }
1124         else
1125         {
1126                 pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1127                 if (pItem == null)
1128                 {
1129                         return true;
1130                 }
1131
1132                 itemBounds = pItem->GetBounds();
1133
1134                 __distance += distance;
1135
1136                 if (__distance * -1.0f > itemBounds.width / 2.0f)
1137                 {
1138                         needItem = true;
1139                         if (__distance * -1.0f > itemBounds.width)
1140                         {
1141                                 updateItemCount = UPDATE_ITEM_COUNT;
1142                         }
1143                         __distance = 0.0f;
1144                 }
1145
1146                 if (needItem == true)
1147                 {
1148                         while (updateItemCount > 0)
1149                         {
1150                                 pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1151                                 if (pItem == null)
1152                                 {
1153                                         break;
1154                                 }
1155
1156                                 int actionId = pItem->GetActionId() + 1;
1157
1158                                 if (__pDateTimeBarModel->GetMaximumValue() < actionId)
1159                                 {
1160                                         actionId = __pDateTimeBarModel->GetMinimumValue();
1161                                 }
1162
1163                                 if (pItem->GetBounds().x < _BOUNDARY_X_POSITION)
1164                                 {
1165                                         AddItem(actionId);
1166                                 }
1167
1168                                 if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1169                                 {
1170                                         DeleteItem(0);
1171                                         SetFirstDrawnItemIndex(0);
1172                                 }
1173                                 updateItemCount--;
1174                         }
1175                 }
1176         }
1177
1178         __currentPoint = touchinfo.GetCurrentPosition();
1179
1180         AdjustItemPosition(distance);
1181
1182         __pDateTimeBar->Invalidate();
1183
1184         return true;
1185 }
1186
1187 bool
1188 _DateTimeBarPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
1189 {
1190         if (&source != __pDateTimeBar)
1191         {
1192                 return false;
1193         }
1194
1195         __isTouchMoved = false;
1196         __distance = 0.0f;
1197
1198         __pDateTimeBar->Invalidate();
1199
1200         return true;
1201 }
1202
1203 result
1204 _DateTimeBarPresenter::LoadItems()
1205 {
1206         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
1207
1208         FloatDimension screen(0.0f, 0.0f);
1209         GET_DIMENSION_CONFIG(DATETIMEBAR::DEFAULT_SIZE, orientation, screen);
1210
1211         const float _BOUNDARY_X_POSITION = screen.width * 2.0f;
1212
1213         _DateTimeBarItem* pItem = null;
1214
1215         Rectangle itemBounds;
1216
1217         int updateItemCount = UPDATE_ITEM_COUNT;
1218
1219         if (__distance > 0.0f)
1220         {
1221                 pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1222
1223                 if (pItem == null)
1224                 {
1225                         return true;
1226                 }
1227
1228
1229                 while (updateItemCount > 0)
1230                 {
1231                         pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1232                         if (pItem == null)
1233                         {
1234                                 break;
1235                         }
1236
1237                         int actionId = pItem->GetActionId() - 1;
1238
1239                         if (__pDateTimeBarModel->GetMinimumValue() > actionId)
1240                         {
1241                                 actionId = __pDateTimeBarModel->GetMaximumValue();
1242                         }
1243
1244                         if (pItem->GetBounds().x > _BOUNDARY_X_POSITION * -1)
1245                         {
1246                                 if (InsertItem(0, actionId) == E_SUCCESS)
1247                                 {
1248                                         SetFirstDrawnItemIndex(0);
1249                                 }
1250                         }
1251
1252                         if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1253                         {
1254                                 DeleteItem(GetItemCount() - 1);
1255                         }
1256                         updateItemCount--;
1257                 }
1258
1259         }
1260         else
1261         {
1262                 pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1263                 if (pItem == null)
1264                 {
1265                         return true;
1266                 }
1267
1268
1269                 while (updateItemCount > 0)
1270                 {
1271                         pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1272                         if (pItem == null)
1273                         {
1274                                 break;
1275                         }
1276
1277                         int actionId = pItem->GetActionId() + 1;
1278
1279                         if (__pDateTimeBarModel->GetMaximumValue() < actionId)
1280                         {
1281                                 actionId = __pDateTimeBarModel->GetMinimumValue();
1282                         }
1283
1284                         if (pItem->GetBounds().x < _BOUNDARY_X_POSITION)
1285                         {
1286                                 AddItem(actionId);
1287                         }
1288
1289                         if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1290                         {
1291                                 DeleteItem(0);
1292                                 SetFirstDrawnItemIndex(0);
1293                         }
1294                         updateItemCount--;
1295                 }
1296         }
1297
1298         return true;
1299 }
1300
1301 bool
1302 _DateTimeBarPresenter::OnFlickGestureDetected(int distanceX,int distanceY,int duration)
1303 {
1304         if (!__isFlickEnabled)
1305         {
1306                 return false;
1307         }
1308
1309         __isFlickInProgress = true;
1310         __isInitialAnimation = false;
1311
1312         int velX = 0;
1313         int velY = 0;
1314
1315         __flickAnimation.CalculateInitializeVelocity(distanceX, 0, duration, &velX, &velY);
1316         __flickAnimation.InitializeFlickAmount(velX);
1317
1318         StartFlickAnimationTimer();
1319         return true;
1320 }
1321
1322 void
1323 _DateTimeBarPresenter::StartFlickAnimation(void)
1324 {
1325         int moveX = 0;
1326         int moveY = 0;
1327
1328         __flickAnimation.CalculateNextMove(&moveX, &moveY);
1329
1330         __distance = _CoordinateSystemUtils::ConvertToFloat(moveX);
1331         if (moveX != 0)
1332         {
1333                 StartFlickAnimationTimer();
1334         }
1335
1336         LoadItems();
1337         AdjustItemPosition(__distance);
1338         Draw();
1339         return;
1340 }
1341
1342 void
1343 _DateTimeBarPresenter::StartAnimationEffect(void)
1344 {
1345         __isInitialAnimation = true;
1346         __distance = -10.0f;
1347
1348         if (__initialAnimationValue >= 0.0f)
1349         {
1350                 __initialAnimationValue = __initialAnimationValue + __distance;
1351                 StartFlickAnimationTimer();
1352
1353                 LoadItems();
1354                 AdjustItemPosition(__distance);
1355                 Draw();
1356         }
1357         else
1358         {
1359                 __initialAnimationValue = 0.0f;
1360                 __isInitialAnimation = false;
1361                 ResetFlickAnimationTimer();
1362                 ValidateAndAdjustStartPosition();
1363         }
1364
1365         return;
1366 }
1367
1368 result
1369 _DateTimeBarPresenter::StartFlickAnimationTimer(void)
1370 {
1371         result r = E_SUCCESS;
1372
1373         if (__pFlickAnimationTimer == null)
1374         {
1375                 __pFlickAnimationTimer = new (std::nothrow) Timer;
1376                 SysTryReturn(NID_UI_CTRL, (__pFlickAnimationTimer != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create timer.");
1377
1378                 r = __pFlickAnimationTimer->Construct(*this);
1379                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1380         }
1381         else
1382         {
1383                 __pFlickAnimationTimer->Cancel();
1384         }
1385
1386         r = __pFlickAnimationTimer->Start(FLICK_ANIMATION_TIMER_PERIOD);
1387         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1388
1389         return r;
1390
1391 CATCH:
1392         ResetFlickAnimationTimer();
1393         return r;
1394 }
1395
1396 result
1397 _DateTimeBarPresenter::ResetFlickAnimationTimer(void)
1398 {
1399         if (__pFlickAnimationTimer)
1400         {
1401                 delete __pFlickAnimationTimer;
1402                 __pFlickAnimationTimer = null;
1403         }
1404
1405         __isFlickInProgress = false;
1406         return E_SUCCESS;
1407 }
1408
1409 void
1410 _DateTimeBarPresenter::OnTimerExpired(Timer& timer)
1411 {
1412         __isFlickInProgress = false;
1413         if (&timer == __pFlickAnimationTimer)
1414         {
1415                 if (__isInitialAnimation)
1416                 {
1417                         StartAnimationEffect();
1418                 }
1419                 else
1420                 {
1421                         StartFlickAnimation();
1422                 }
1423         }
1424         return;
1425 }
1426
1427 void
1428 _DateTimeBarPresenter::ValidateAndAdjustStartPosition(void)
1429 {
1430         int index = -1;
1431         float adjustPosition = 0.0f;
1432         float leftMargin =  GetLeftRightMargin();
1433         FloatRectangle bodyBounds = GetBodyBounds();
1434
1435         index = GetItemIndexFromPosition(FloatPoint(GetLeftRightMargin() + GetItemMargin(), bodyBounds.y + (bodyBounds.height / 2.0f)));
1436
1437         if (index == -1)
1438         {
1439                 return;
1440         }
1441
1442         _DateTimeBarItem* pDrawItem = null;
1443         pDrawItem = __pDateTimeBarModel->GetItemAt(index);
1444
1445         if (pDrawItem == null)
1446         {
1447                 return;
1448         }
1449
1450         if (pDrawItem->GetBounds().x > 0.0f && pDrawItem->GetBounds().x < leftMargin)
1451         {
1452                 adjustPosition = leftMargin - pDrawItem->GetBounds().x;
1453         }
1454         else if (pDrawItem->GetBounds().x < 0.0f)
1455         {
1456                 pDrawItem = __pDateTimeBarModel->GetItemAt(index + 1);
1457                 if (pDrawItem == null)
1458                 {
1459                         return;
1460                 }
1461                 adjustPosition = leftMargin - pDrawItem->GetBounds().x;
1462         }
1463         else if (pDrawItem->GetBounds().x > leftMargin)
1464         {
1465                 adjustPosition = leftMargin - pDrawItem->GetBounds().x;
1466         }
1467
1468         AdjustItemPosition(adjustPosition);
1469         Draw();
1470         return;
1471 }
1472
1473 void
1474 _DateTimeBarPresenter::SetInitialAnimationValue(float animationValue)
1475 {
1476         __initialAnimationValue = animationValue;
1477         return;
1478 }
1479
1480 void
1481 _DateTimeBarPresenter::SetFont(Font& pFont)
1482 {
1483         delete __pFont;
1484         __pFont = null;
1485
1486         __pFont = _FontImpl::CloneN(pFont);
1487         SysTryReturnVoidResult(NID_UI_CTRL, (__pFont != null), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1488
1489         return;
1490 }
1491
1492 bool
1493 _DateTimeBarPresenter::OnAccessibilityFocusMovedNext(const _AccessibilityContainer& control, const _AccessibilityElement& element)
1494 {
1495         __distance = (GetItemWidth() + GetItemMargin()) * (-1.0f);
1496
1497         if (element.GetBounds().x + element.GetBounds().width > GetWindowBounds().width)
1498         {
1499                 AdjustItemPosition(__distance);
1500                 Draw();
1501         }
1502         return true;
1503 }
1504
1505 bool
1506 _DateTimeBarPresenter::OnAccessibilityFocusMovedPrevious(const _AccessibilityContainer& control, const _AccessibilityElement& element)
1507 {
1508         __distance = (GetItemWidth() + GetItemMargin());
1509
1510         if (element.GetBounds().x < GetWindowBounds().x)
1511         {
1512                 AdjustItemPosition(__distance);
1513                 Draw();
1514         }
1515         return true;
1516 }
1517
1518 void
1519 _DateTimeBarPresenter::OnTouchMoveHandled(const Tizen::Ui::_Control& control)
1520 {
1521         __touchMoveHandled = true;
1522
1523         __isTouchMoved = false;
1524         __distance = 0.0f;
1525
1526         __pDateTimeBar->Invalidate();
1527
1528         return;
1529 }
1530
1531 void
1532 _DateTimeBarPresenter::RefreshAccessibilityElement(void)
1533 {
1534         __pDateTimeBar->RemoveAllAccessibilityElement();
1535
1536         for (int index = 0; index < __pDateTimeBarModel->GetItemCount(); index++)
1537         {
1538                 _DateTimeBarItem* pItem = __pDateTimeBarModel->GetItemAt(index);
1539
1540                 if (pItem != null)
1541                 {
1542                         if (__pDateTimeBar->GetSelectedBoxId() == DATETIME_ID_MONTH)
1543                         {
1544                                 __pDateTimeBar->AddAccessibilityElement(pItem->GetBounds(), __pDateTimeBar->GetMonthValue(pItem->GetActionId()));
1545                         }
1546                         else
1547                         {
1548                                 __pDateTimeBar->AddAccessibilityElement(pItem->GetBounds(), pItem->GetText());
1549                         }
1550                 }
1551         }
1552
1553         return;
1554 }
1555
1556 }}} // Tizen::Ui::Controls