fixed bug (TapGesture improvement)
[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         for (int i = __pDateTimeBarModel->GetFirstDrawnItemIndex(); i < itemCount; i++)
259         {
260                 pDrawItem = __pDateTimeBarModel->GetItemAt(i);
261                 if (pDrawItem == null)
262                 {
263                         break;
264                 }
265
266                 itemBounds = pDrawItem->GetBounds();
267
268                 if (itemBounds.x + itemBounds.width <= 0)
269                 {
270                         continue;
271                 }
272
273                 drawText = pDrawItem->GetText();
274
275                 if (drawText.CompareTo(__selectedText) == 0)
276                 {
277                         pDrawItem->SetStatus(DATETIMEBAR_ITEM_STATUS_SELECTED);
278                 }
279
280                 itemStatus = pDrawItem->GetStatus();
281
282                 float fontSize = GetFontSize();
283
284                 TextSimple* pSimpleText = null;
285                 TextObject* pTextObject = new (std::nothrow) TextObject;
286                 SysTryReturn(NID_UI_CTRL, (pTextObject != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
287
288                 pTextObject->Construct();
289                 pSimpleText = new (std::nothrow)TextSimple((const_cast <wchar_t*>(drawText.GetPointer())), drawText.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
290                 pTextObject->AppendElement(*pSimpleText);
291
292                 pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
293                 pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
294                 pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
295
296                 SysAssertf(__pFont != null, "Font instance is null");
297
298                 if (itemStatus == DATETIMEBAR_ITEM_STATUS_SELECTED)
299                 {
300                         (_FontImpl::GetInstance(*__pFont))->SetSize(fontSize);
301                         (_FontImpl::GetInstance(*__pFont))->SetStyle(FONT_STYLE_BOLD);
302                         pTextObject->SetFont(__pFont, 0, pTextObject->GetTextLength());
303                         pTextObject->SetForegroundColor(textPressedColor, 0, pTextObject->GetTextLength());
304                 }
305                 else
306                 {
307                         (_FontImpl::GetInstance(*__pFont))->SetSize(fontSize);
308                         (_FontImpl::GetInstance(*__pFont))->SetStyle(FONT_STYLE_PLAIN);
309                         pTextObject->SetFont(__pFont, 0, pTextObject->GetTextLength());
310                         pTextObject->SetForegroundColor(textColor, 0, pTextObject->GetTextLength());
311                 }
312
313                 pTextObject->SetBounds(itemBounds);
314                 pTextObject->Draw(*_CanvasImpl::GetInstance(canvas));
315
316                 delete pTextObject;
317
318                 if ((itemBounds.x + itemBounds.width + GetLeftRightMargin()) >= clientBounds.width)
319                 {
320                         break;
321                 }
322         }
323
324         return r;
325 }
326
327 result
328 _DateTimeBarPresenter::DrawBackground(Canvas& canvas)
329 {
330         result r = E_SUCCESS;
331
332         FloatRectangle bodyAreaBounds = GetBodyBounds();
333
334         if (__pBgColorReplacementBitmap != null)
335         {
336                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pBgColorReplacementBitmap))
337                 {
338                         r = canvas.DrawNinePatchedBitmap(bodyAreaBounds, *__pBgColorReplacementBitmap);
339                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
340                 }
341                 else
342                 {
343                         r = canvas.DrawBitmap(bodyAreaBounds, *__pBgColorReplacementBitmap);
344                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
345                 }
346         }
347
348         else
349         {
350                 r = canvas.DrawRectangle(bodyAreaBounds);
351                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
352         }
353
354         return r;
355 }
356
357 result
358 _DateTimeBarPresenter::DrawArrow(Canvas& canvas)
359 {
360         result r = E_SUCCESS;
361         FloatRectangle arrowAreaBounds = GetArrowBounds();
362
363         if (__pArrowColorReplacementBitmap != null)
364         {
365                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pArrowColorReplacementBitmap))
366                 {
367                         r = canvas.DrawNinePatchedBitmap(arrowAreaBounds, *__pArrowColorReplacementBitmap);
368                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
369                 }
370                 else
371                 {
372                         r = canvas.DrawBitmap(arrowAreaBounds, *__pArrowColorReplacementBitmap);
373                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
374                 }
375         }
376
377         return r;
378 }
379
380 void
381 _DateTimeBarPresenter::SetItemSelected(int index)
382 {
383         __pDateTimeBarModel->SetSelectedItemIndex(index);
384         return;
385 }
386
387 int
388 _DateTimeBarPresenter::GetSelectedItemIndex(void) const
389 {
390         return __pDateTimeBarModel->GetSelectedItemIndex();
391 }
392
393 int
394 _DateTimeBarPresenter::GetItemIndexFromPosition(const FloatPoint& point) const
395 {
396         FloatRectangle clientBounds = __pDateTimeBar->GetBoundsF();
397
398         if (point.x > clientBounds.width
399                 || point.y > clientBounds.height)
400         {
401                 return -1;
402         }
403
404         int itemIndex = __pDateTimeBarModel->GetFirstDrawnItemIndex();
405         _DateTimeBarItem* pItem = __pDateTimeBarModel->GetItemAt(itemIndex);
406         float startX = 0.0f;
407         float itemMargin = 0.0f;
408         itemMargin = GetItemMargin();
409
410         while (pItem != null)
411         {
412                 FloatRectangle itemBounds = pItem->GetBounds();
413
414                 if (itemBounds.x > clientBounds.x)
415                 {
416                         if (itemIndex == __pDateTimeBarModel->GetFirstDrawnItemIndex())
417                         {
418                                 startX = itemBounds.x;
419                         }
420
421                         itemBounds.x -= startX;
422                 }
423
424                 itemBounds.x = itemBounds.x - (itemMargin / 2.0f);
425                 itemBounds.width = itemBounds.width + itemMargin;
426
427                 if (itemBounds.Contains(point) == true)
428                 {
429                         return itemIndex;
430                 }
431
432                 itemIndex++;
433
434                 if (itemIndex == GetItemCount())
435                 {
436                         break;
437                 }
438
439                 pItem = __pDateTimeBarModel->GetItemAt(itemIndex);
440         }
441
442         return -1;
443 }
444
445 result
446 _DateTimeBarPresenter::SetItemStatus(int index, _DateTimeBarItemStatus status)
447 {
448         _DateTimeBarItem* pItem = __pDateTimeBarModel->GetItemAt(index);
449         SysTryReturn(NID_UI_CTRL, pItem, E_SYSTEM, E_SYSTEM,
450                  "[E_SYSTEM] A system error has occurred. Failed to get _DateTimeBarItem at index (%d).", index);
451
452         if (status != DATETIMEBAR_ITEM_STATUS_NORMAL)
453         {
454                 __pDateTimeBarModel->SetSelectedItemIndex(index);
455         }
456         pItem->SetStatus(status);
457
458         return E_SUCCESS;
459 }
460
461 _DateTimeBarItemStatus
462 _DateTimeBarPresenter::GetItemStatus(int index) const
463 {
464         _DateTimeBarItem* pItem = __pDateTimeBarModel->GetItemAt(index);
465         SysTryReturn(NID_UI_CTRL, pItem, DATETIMEBAR_ITEM_STATUS_NORMAL, E_SYSTEM,
466                  "[E_SYSTEM] A system error has occurred. Failed to get _DateTimeBarItem at index (%d).", index);
467
468         return pItem->GetStatus();
469 }
470
471 int
472 _DateTimeBarPresenter::GetFirstDrawnItemIndex(void) const
473 {
474         return __pDateTimeBarModel->GetFirstDrawnItemIndex();
475 }
476
477 void
478 _DateTimeBarPresenter::SetFirstDrawnItemIndex(int index)
479 {
480         __pDateTimeBarModel->SetFirstDrawnItemIndex(index);
481         return;
482 }
483
484 int
485 _DateTimeBarPresenter::GetMinimumValue(void) const
486 {
487         return __pDateTimeBarModel->GetMinimumValue();
488 }
489
490 void
491 _DateTimeBarPresenter::SetMinimumValue(int minValue)
492 {
493         __pDateTimeBarModel->SetMinimumValue(minValue);
494         return;
495 }
496
497 int
498 _DateTimeBarPresenter::GetMaximumValue(void) const
499 {
500         return __pDateTimeBarModel->GetMaximumValue();
501 }
502
503 void
504 _DateTimeBarPresenter::SetMaximumValue(int maxValue)
505 {
506         __pDateTimeBarModel->SetMaximumValue(maxValue);
507         return;
508 }
509
510 float
511 _DateTimeBarPresenter::GetItemWidth(void) const
512 {
513         result r = E_SUCCESS;
514
515         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
516
517         float itemWidth = 0.0f;
518
519         if (boxId == DATETIME_ID_YEAR)
520         {
521                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
522         }
523         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
524         {
525                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
526         }
527         else if (boxId == DATETIME_ID_MONTH)
528         {
529                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
530
531                 FloatDimension textArea;
532
533                 float fontSize = 0;
534                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_FONT_SIZE, __pDateTimeBar->GetOrientation(), fontSize);
535
536                 SysAssertf(__pFont != null, "Font instance is null");
537
538                 (_FontImpl::GetInstance(*__pFont))->SetSize(fontSize);
539                 (_FontImpl::GetInstance(*__pFont))->SetStyle(FONT_STYLE_PLAIN);
540
541                 for (int i = DATETIME_MONTH_MIN; i <= DATETIME_MONTH_MAX; i++)
542                 {
543                         String string;
544                         _DateTimeUtils dateTimeUtils;
545                         string = dateTimeUtils.GetMonthString(i);
546
547                         r = __pFont->GetTextExtent(string, string.GetLength(), textArea);
548                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
549
550                         if (textArea.width > itemWidth)
551                         {
552                                 itemWidth = textArea.width;
553                         }
554                 }
555         }
556
557         return itemWidth;
558 }
559
560 float
561 _DateTimeBarPresenter::GetFontSize(void) const
562 {
563         float fontSize = 0.0f;
564         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
565
566         if (boxId == DATETIME_ID_YEAR)
567         {
568                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_FONT_SIZE, _ControlManager::GetInstance()->GetOrientation(), fontSize);
569         }
570         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
571         {
572                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_FONT_SIZE, _ControlManager::GetInstance()->GetOrientation(), fontSize);
573         }
574         else if (boxId == DATETIME_ID_MONTH)
575         {
576                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_FONT_SIZE, _ControlManager::GetInstance()->GetOrientation(), fontSize);
577         }
578
579         return fontSize;
580 }
581
582 float
583 _DateTimeBarPresenter::GetLeftRightMargin(void) const
584 {
585         float leftRightMargin = 0.0f;
586         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
587
588         if (boxId == DATETIME_ID_YEAR)
589         {
590                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_LEFT_RIGHT_MARGIN, _ControlManager::GetInstance()->GetOrientation(), leftRightMargin);
591         }
592         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
593         {
594                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_LEFT_RIGHT_MARGIN, _ControlManager::GetInstance()->GetOrientation(), leftRightMargin);
595         }
596         else if (boxId == DATETIME_ID_MONTH)
597         {
598                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_LEFT_RIGHT_MARGIN, _ControlManager::GetInstance()->GetOrientation(), leftRightMargin);
599         }
600
601         return leftRightMargin;
602 }
603
604 float
605 _DateTimeBarPresenter::GetItemMargin(void) const
606 {
607         float margin = 0.0f;
608         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
609
610         if (boxId == DATETIME_ID_YEAR)
611         {
612                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_ITEM_MARGIN, _ControlManager::GetInstance()->GetOrientation(), margin);
613         }
614         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
615         {
616                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_ITEM_MARGIN, _ControlManager::GetInstance()->GetOrientation(), margin);
617         }
618         else if (boxId == DATETIME_ID_MONTH)
619         {
620                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_ITEM_MARGIN, _ControlManager::GetInstance()->GetOrientation(), margin);
621         }
622
623         return margin;
624 }
625
626 result
627 _DateTimeBarPresenter::AdjustItemPosition(float distance)
628 {
629         int index = 0;
630
631         _DateTimeBarItem* pItem = null;
632
633         pItem = __pDateTimeBarModel->GetItemAt(index);
634
635         while (pItem != null)
636         {
637                 FloatRectangle bounds = pItem->GetBounds();
638                 bounds.x += distance;
639                 pItem->SetBounds(bounds);
640                 index++;
641
642                 if (index == GetItemCount())
643                 {
644                         break;
645                 }
646
647                 pItem = __pDateTimeBarModel->GetItemAt(index);
648         }
649
650         return AdjustAccessibilityElementPosition(distance);
651 }
652
653 result
654 _DateTimeBarPresenter::AdjustAccessibilityElementPosition(float distance)
655 {
656         _AccessibilityElement* pAccessibilityElement = null;
657
658         int index = 0;
659
660         pAccessibilityElement = __pDateTimeBar->GetAccessibilityElementAt(index);
661
662         while (pAccessibilityElement != null)
663         {
664                 FloatRectangle bounds = pAccessibilityElement->GetBounds();
665                 bounds.x += distance;
666                 pAccessibilityElement->SetBounds(bounds);
667                 index++;
668
669                 if (index == GetItemCount())
670                 {
671                         break;
672                 }
673
674                 pAccessibilityElement = __pDateTimeBar->GetAccessibilityElementAt(index);
675         }
676
677         return E_SUCCESS;
678 }
679
680 String
681 _DateTimeBarPresenter::GetDisplayedText(int value) const
682 {
683         String string;
684
685         if (__pDateTimeBar->GetSelectedBoxId() == DATETIME_ID_MONTH)
686         {
687                 String monthString;
688                 _DateTimeUtils dateTimeUtils;
689                 monthString = dateTimeUtils.GetMonthString(value);
690                 string.Append(monthString);
691         }
692         else if (__pDateTimeBar->GetSelectedBoxId() == DATETIME_ID_YEAR)
693         {
694                 string.Format(10, L"%04d", value);
695         }
696         else
697         {
698                 string.Format(10, L"%02d", value);
699         }
700
701         return string;
702 }
703
704 result
705 _DateTimeBarPresenter::AddItems(int actionId)
706 {
707         result r = E_SUCCESS;
708         int cachingSize = __pDateTimeBarModel->GetMaxCachingSize() / 2;
709
710         int firstDrawItemIndex = cachingSize;
711
712         int value = actionId;
713         float startPosition = 0.0f;
714         float itemWidth = GetItemWidth();
715         float itemMargin = GetItemMargin();
716
717         startPosition = (GetWindowBounds().width) / 2.0f;
718         startPosition = startPosition + (itemWidth / 2.0f) + itemMargin;
719         __pDateTimeBarModel->SetItemStartPosition(startPosition);
720
721         while (cachingSize > 0)
722         {
723                 String string;
724
725                 string = GetDisplayedText(value);
726
727                 r = __pDateTimeBarModel->AddItem(string, value, __pDateTimeBar->GetAlignment(), itemWidth, itemMargin);
728                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
729
730                 AddAccessibilityElement();
731                 cachingSize--;
732
733                 value++;
734
735                 if (__pDateTimeBarModel->GetMaximumValue() < value)
736                 {
737                         value = __pDateTimeBarModel->GetMinimumValue();
738                 }
739         }
740
741         r = InsertItems(actionId -1);
742         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
743
744         _DateTimeBarItem* pItem = null;
745         pItem = GetItemAt(firstDrawItemIndex);
746
747         if (pItem != null)
748         {
749                 __selectedText.Clear();
750                 __selectedText.Append(pItem->GetText().GetPointer());
751         }
752
753         SetItemStatus(firstDrawItemIndex, DATETIMEBAR_ITEM_STATUS_SELECTED);
754
755         return r;
756 }
757
758 result
759 _DateTimeBarPresenter::InsertItems(int actionId)
760 {
761         result r = E_SUCCESS;
762
763         int cachingSize = __pDateTimeBarModel->GetMaxCachingSize() / 2;
764
765         int value = actionId;
766         float itemWidth = GetItemWidth();
767         float itemMargin = GetItemMargin();
768
769         while (cachingSize > 0)
770         {
771                 String string;
772
773                 if (value < __pDateTimeBarModel->GetMinimumValue())
774                 {
775                         value = __pDateTimeBarModel->GetMaximumValue();
776                 }
777
778                 string = GetDisplayedText(value);
779                 r = __pDateTimeBarModel->InsertItemAt(0, string, value, __pDateTimeBar->GetAlignment(), itemWidth, itemMargin);
780                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
781                 InsertAccessibilityElementAt(0);
782                 cachingSize--;
783
784                 value--;
785         }
786
787         return r;
788 }
789
790 result
791 _DateTimeBarPresenter::InsertItem(int index, int actionId)
792 {
793         result r = E_SUCCESS;
794
795         String string;
796
797         string = GetDisplayedText(actionId);
798
799         r = __pDateTimeBarModel->InsertItemAt(index, string, actionId, __pDateTimeBar->GetAlignment(), GetItemWidth(), GetItemMargin());
800         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
801         InsertAccessibilityElementAt(index);
802         return r;
803 }
804
805 result
806 _DateTimeBarPresenter::AddItem(int actionId)
807 {
808         result r = E_SUCCESS;
809
810         String string;
811
812         string = GetDisplayedText(actionId);
813
814         r = __pDateTimeBarModel->AddItem(string, actionId, __pDateTimeBar->GetAlignment(), GetItemWidth(), GetItemMargin());
815         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
816
817         AddAccessibilityElement();
818         return r;
819 }
820
821 result
822 _DateTimeBarPresenter::DeleteItem(int index)
823 {
824         int itemCount = GetItemCount();
825
826         SysTryReturn(NID_UI_CTRL, itemCount > 0, E_INVALID_ARG, E_INVALID_ARG,
827                         "[E_INVALID_ARG] Invalid argument is used. index = %d", index);
828
829         SysTryReturn(NID_UI_CTRL, index < itemCount, E_INVALID_ARG, E_INVALID_ARG,
830                         "[E_INVALID_ARG] Invalid argument is used. index = %d", index);
831
832         SysTryReturn(NID_UI_CTRL, index >= 0, E_INVALID_ARG, E_INVALID_ARG,
833                         "[E_INVALID_ARG] Invalid argument is used. index = %d", index);
834
835         result r = E_SUCCESS;
836
837         r = __pDateTimeBarModel->RemoveItemAt(index);
838         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
839
840         __pDateTimeBar->RemoveAccessibilityElementAt(index);
841
842         return r;
843 }
844
845 result
846 _DateTimeBarPresenter::RemoveAllItems(void)
847 {
848         SysTryReturn(NID_UI_CTRL, GetItemCount() > 0, E_INVALID_OPERATION, E_INVALID_OPERATION,
849                         "[E_INVALID_OPERATION] Items count is zero.");
850
851         result r = E_SUCCESS;
852
853         __pDateTimeBarModel->RemoveAllItems();
854
855         __pDateTimeBar->RemoveAllAccessibilityElement();
856
857         return r;
858 }
859
860 int
861 _DateTimeBarPresenter::GetItemCount(void) const
862 {
863         return __pDateTimeBarModel->GetItemCount();
864 }
865
866 _DateTimeBarItem*
867 _DateTimeBarPresenter::GetItemAt(int index) const
868 {
869         return __pDateTimeBarModel->GetItemAt(index);
870 }
871
872 result
873 _DateTimeBarPresenter::CalculateWindowBounds(void)
874 {
875         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
876         FloatRectangle windowAreaBounds = FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f);
877         FloatRectangle bodyAreaBounds = FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f);
878         FloatRectangle arrowAreaBounds = FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f);
879
880         FloatRectangle parentWindowBounds = __pDateTimeBar->GetParentWindowBounds();
881
882         arrowAreaBounds = GetArrowBounds();
883
884         Point arrowPosition;
885
886         GET_SHAPE_CONFIG(DATETIMEBAR::ARROW_HEIGHT, orientation, arrowAreaBounds.height);
887         GET_SHAPE_CONFIG(DATETIMEBAR::ARROW_WIDTH, orientation, arrowAreaBounds.width);
888         GET_SHAPE_CONFIG(DATETIMEBAR::ITEM_HEIGHT, orientation, bodyAreaBounds.height);
889
890         bodyAreaBounds.width = parentWindowBounds.width;
891
892         bodyAreaBounds.x = 0.0f;
893         FloatPoint tempPoint = __pDateTimeBar->GetPositionF();
894         windowAreaBounds.x = tempPoint.x;
895
896         if (__pDateTimeBar->GetAlignment() == DATETIME_BAR_ALIGN_DOWN)
897         {
898                 bodyAreaBounds.y = arrowAreaBounds.height;
899                 arrowAreaBounds.y = 0.0f;
900                 windowAreaBounds.y = tempPoint.y - arrowAreaBounds.height;
901         }
902         else
903         {
904                 arrowAreaBounds.y = bodyAreaBounds.height;
905                 bodyAreaBounds.y = 0.0f;
906                 windowAreaBounds.y = tempPoint.y;
907         }
908
909         windowAreaBounds.width = bodyAreaBounds.width;
910         windowAreaBounds.height = bodyAreaBounds.height + arrowAreaBounds.height;
911
912         SetBodyBounds(bodyAreaBounds);
913         SetArrowBounds(arrowAreaBounds);
914         SetWindowBounds(windowAreaBounds);
915         __pDateTimeBar->SetMovable(true);
916         __pDateTimeBar->SetResizable(true);
917         __pDateTimeBar->SetBounds(windowAreaBounds);
918         __pDateTimeBar->SetMovable(false);
919         __pDateTimeBar->SetResizable(false);
920
921         return E_SUCCESS;
922 }
923
924 void
925 _DateTimeBarPresenter::SetBodyBounds(const FloatRectangle& bounds)
926 {
927         __bodyAreaBounds = bounds;
928         return;
929 }
930
931 FloatRectangle
932 _DateTimeBarPresenter::GetBodyBounds(void) const
933 {
934         return __bodyAreaBounds;
935 }
936
937 void
938 _DateTimeBarPresenter::SetArrowBounds(const FloatRectangle& bounds)
939 {
940         __arrowAreaBounds = bounds;
941         return;
942 }
943
944 FloatRectangle
945 _DateTimeBarPresenter::GetArrowBounds(void) const
946 {
947         return __arrowAreaBounds;
948 }
949
950 void
951 _DateTimeBarPresenter::SetWindowBounds(const FloatRectangle& bounds)
952 {
953         __windowAreaBounds = bounds;
954         return;
955 }
956
957 FloatRectangle
958 _DateTimeBarPresenter::GetWindowBounds(void) const
959 {
960         return __windowAreaBounds;
961 }
962
963 bool
964 _DateTimeBarPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
965 {
966         if (&source != __pDateTimeBar)
967         {
968                 return false;
969         }
970
971         __isFlickEnabled = true;
972
973         if (GetBodyBounds().Contains(touchinfo.GetCurrentPosition()) == false)
974         {
975                 __isFlickEnabled = false;
976                 return true;
977         }
978
979         if (__isFlickInProgress)
980         {
981                 ResetFlickAnimationTimer();
982         }
983
984         __touchMoveHandled = false;
985
986         __currentPoint = touchinfo.GetCurrentPosition();
987
988         int index = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
989
990         if (index == -1)
991         {
992                 return true;
993         }
994
995         __pDateTimeBar->Invalidate();
996
997         return true;
998 }
999
1000 bool
1001 _DateTimeBarPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
1002 {
1003         if (&source != __pDateTimeBar)
1004         {
1005                 return false;
1006         }
1007
1008         if (GetBodyBounds().Contains(touchinfo.GetCurrentPosition()) == false && !__isFlickInProgress && !__isTouchMoved)
1009         {
1010                 __isTouchMoved = false;
1011                 PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_TAP);
1012                 __pDateTimeBar->SetVisibleState(false);
1013                 __pDateTimeBar->Close();
1014                 ResetFlickAnimationTimer();
1015                 __pDateTimeBar->FireDateTimeChangeEvent(DATE_INTERNAL_CHANGE_CANCELED);
1016                 return true;
1017         }
1018
1019         int index = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
1020         bool isEventFire = true;
1021
1022         if (index != -1 && __isTouchMoved == false && __isFlickInProgress == false)
1023         {
1024                 PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_TAP);
1025                 SetItemStatus(index, DATETIMEBAR_ITEM_STATUS_SELECTED);
1026                 __pDateTimeBarModel->SetFirstDrawnItemIndex(index);
1027         }
1028         else
1029         {
1030                 isEventFire = false;
1031         }
1032
1033         __isTouchMoved = false;
1034         __distance = 0.0f;
1035
1036         __pDateTimeBar->Invalidate();
1037
1038         if (isEventFire == true)
1039         {
1040                 int index = GetFirstDrawnItemIndex();
1041
1042                 _DateTimeBarItem* pItem = null;
1043
1044                 if (index >= 0)
1045                 {
1046                         pItem = GetItemAt(index);
1047                 }
1048
1049                 if (pItem != null)
1050                 {
1051                         __pDateTimeBar->SetVisibleState(false);
1052                         __pDateTimeBar->Close();
1053
1054                         __pDateTimeBar->FireActionEvent(pItem->GetActionId());
1055
1056                         if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_YEAR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_DAY)
1057                         {
1058                                 __pDateTimeBar->FireDateTimeChangeEvent(DATE_INTERNAL_CHANGE_SAVED);
1059                         }
1060                         else if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_HOUR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_MINUTE)
1061                         {
1062                                 __pDateTimeBar->FireDateTimeChangeEvent(TIME_INTERNAL_CHANGE_SAVED);
1063                         }
1064                 }
1065         }
1066
1067         return true;
1068 }
1069
1070 bool
1071 _DateTimeBarPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
1072 {
1073         if (&source != __pDateTimeBar)
1074         {
1075                 return false;
1076         }
1077
1078         if (GetBodyBounds().Contains(touchinfo.GetCurrentPosition()) == false || __touchMoveHandled == true)
1079         {
1080                 return true;
1081         }
1082
1083         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
1084
1085         __isTouchMoved = true;
1086
1087         FloatDimension screen(0.0f, 0.0f);
1088         GET_DIMENSION_CONFIG(DATETIMEBAR::DEFAULT_SIZE, orientation, screen);
1089
1090         const float _BOUNDARY_X_POSITION = screen.width * 2.0f;
1091         float distance = touchinfo.GetCurrentPosition().x - __currentPoint.x;
1092
1093         _DateTimeBarItem* pItem = null;
1094
1095         FloatRectangle itemBounds;
1096
1097         bool needItem = false;
1098         int updateItemCount = 1;
1099
1100         if (distance > 0.0f)
1101         {
1102                 pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1103
1104                 if (pItem == null)
1105                 {
1106                         return true;
1107                 }
1108
1109                 itemBounds = pItem->GetBounds();
1110
1111                 __distance += distance;
1112
1113                 if (__distance > itemBounds.width / 2.0f)
1114                 {
1115                         needItem = true;
1116                         if (__distance > itemBounds.width)
1117                         {
1118                                 updateItemCount = UPDATE_ITEM_COUNT;
1119                         }
1120                 }
1121                 if (needItem == true)
1122                 {
1123                         while (updateItemCount > 0)
1124                         {
1125                                 pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1126                                 if (pItem == null)
1127                                 {
1128                                         break;
1129                                 }
1130
1131                                 int actionId = pItem->GetActionId() - 1;
1132
1133                                 if (__pDateTimeBarModel->GetMinimumValue() > actionId)
1134                                 {
1135                                         actionId = __pDateTimeBarModel->GetMaximumValue();
1136                                 }
1137
1138                                 if (pItem->GetBounds().x > _BOUNDARY_X_POSITION * -1.0f)
1139                                 {
1140                                         if (InsertItem(0, actionId) == E_SUCCESS)
1141                                         {
1142                                                 SetFirstDrawnItemIndex(0);
1143                                         }
1144                                 }
1145
1146                                 if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1147                                 {
1148                                         DeleteItem(GetItemCount() - 1);
1149                                 }
1150
1151                                 updateItemCount--;
1152                         }
1153                         __distance = 0.0f;
1154                 }
1155         }
1156         else
1157         {
1158                 pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1159                 if (pItem == null)
1160                 {
1161                         return true;
1162                 }
1163
1164                 itemBounds = pItem->GetBounds();
1165
1166                 __distance += distance;
1167
1168                 if (__distance * -1.0f > itemBounds.width / 2.0f)
1169                 {
1170                         needItem = true;
1171                         if (__distance * -1.0f > itemBounds.width)
1172                         {
1173                                 updateItemCount = UPDATE_ITEM_COUNT;
1174                         }
1175                         __distance = 0.0f;
1176                 }
1177
1178                 if (needItem == true)
1179                 {
1180                         while (updateItemCount > 0)
1181                         {
1182                                 pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1183                                 if (pItem == null)
1184                                 {
1185                                         break;
1186                                 }
1187
1188                                 int actionId = pItem->GetActionId() + 1;
1189
1190                                 if (__pDateTimeBarModel->GetMaximumValue() < actionId)
1191                                 {
1192                                         actionId = __pDateTimeBarModel->GetMinimumValue();
1193                                 }
1194
1195                                 if (pItem->GetBounds().x < _BOUNDARY_X_POSITION)
1196                                 {
1197                                         AddItem(actionId);
1198                                 }
1199
1200                                 if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1201                                 {
1202                                         DeleteItem(0);
1203                                         SetFirstDrawnItemIndex(0);
1204                                 }
1205                                 updateItemCount--;
1206                         }
1207                 }
1208         }
1209
1210         __currentPoint = touchinfo.GetCurrentPosition();
1211
1212         AdjustItemPosition(distance);
1213
1214         __pDateTimeBar->Invalidate();
1215
1216         return true;
1217 }
1218
1219 bool
1220 _DateTimeBarPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
1221 {
1222         if (&source != __pDateTimeBar)
1223         {
1224                 return false;
1225         }
1226
1227         __isTouchMoved = false;
1228         __distance = 0.0f;
1229
1230         __pDateTimeBar->Invalidate();
1231
1232         return true;
1233 }
1234
1235 result
1236 _DateTimeBarPresenter::LoadItems()
1237 {
1238         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
1239
1240         FloatDimension screen(0.0f, 0.0f);
1241         GET_DIMENSION_CONFIG(DATETIMEBAR::DEFAULT_SIZE, orientation, screen);
1242
1243         const float _BOUNDARY_X_POSITION = screen.width * 2.0f;
1244
1245         _DateTimeBarItem* pItem = null;
1246
1247         int updateItemCount = UPDATE_ITEM_COUNT;
1248
1249         if (__distance > 0.0f)
1250         {
1251                 pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1252
1253                 if (pItem == null)
1254                 {
1255                         return true;
1256                 }
1257
1258
1259                 while (updateItemCount > 0)
1260                 {
1261                         pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1262                         if (pItem == null)
1263                         {
1264                                 break;
1265                         }
1266
1267                         int actionId = pItem->GetActionId() - 1;
1268
1269                         if (__pDateTimeBarModel->GetMinimumValue() > actionId)
1270                         {
1271                                 actionId = __pDateTimeBarModel->GetMaximumValue();
1272                         }
1273
1274                         if (pItem->GetBounds().x > _BOUNDARY_X_POSITION * -1)
1275                         {
1276                                 if (InsertItem(0, actionId) == E_SUCCESS)
1277                                 {
1278                                         SetFirstDrawnItemIndex(0);
1279                                 }
1280                         }
1281
1282                         if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1283                         {
1284                                 DeleteItem(GetItemCount() - 1);
1285                         }
1286                         updateItemCount--;
1287                 }
1288
1289         }
1290         else
1291         {
1292                 pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1293                 if (pItem == null)
1294                 {
1295                         return true;
1296                 }
1297
1298
1299                 while (updateItemCount > 0)
1300                 {
1301                         pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1302                         if (pItem == null)
1303                         {
1304                                 break;
1305                         }
1306
1307                         int actionId = pItem->GetActionId() + 1;
1308
1309                         if (__pDateTimeBarModel->GetMaximumValue() < actionId)
1310                         {
1311                                 actionId = __pDateTimeBarModel->GetMinimumValue();
1312                         }
1313
1314                         if (pItem->GetBounds().x < _BOUNDARY_X_POSITION)
1315                         {
1316                                 AddItem(actionId);
1317                         }
1318
1319                         if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1320                         {
1321                                 DeleteItem(0);
1322                                 SetFirstDrawnItemIndex(0);
1323                         }
1324                         updateItemCount--;
1325                 }
1326         }
1327
1328         return true;
1329 }
1330
1331 bool
1332 _DateTimeBarPresenter::OnFlickGestureDetected(int distanceX,int distanceY,int duration)
1333 {
1334         if (!__isFlickEnabled)
1335         {
1336                 return false;
1337         }
1338
1339         __isFlickInProgress = true;
1340         __isInitialAnimation = false;
1341
1342         int velX = 0;
1343         int velY = 0;
1344
1345         __flickAnimation.CalculateInitializeVelocity(distanceX, 0, duration, &velX, &velY);
1346         __flickAnimation.InitializeFlickAmount(velX);
1347
1348         StartFlickAnimationTimer();
1349         return true;
1350 }
1351
1352 void
1353 _DateTimeBarPresenter::StartFlickAnimation(void)
1354 {
1355         int moveX = 0;
1356         int moveY = 0;
1357
1358         __flickAnimation.CalculateNextMove(&moveX, &moveY);
1359
1360         __distance = _CoordinateSystemUtils::ConvertToFloat(moveX);
1361         if (moveX != 0)
1362         {
1363                 StartFlickAnimationTimer();
1364         }
1365
1366         LoadItems();
1367         AdjustItemPosition(__distance);
1368         Draw();
1369         return;
1370 }
1371
1372 void
1373 _DateTimeBarPresenter::StartAnimationEffect(void)
1374 {
1375         __isInitialAnimation = true;
1376         __distance = -10.0f;
1377
1378         if (__initialAnimationValue >= 0.0f)
1379         {
1380                 __initialAnimationValue = __initialAnimationValue + __distance;
1381                 StartFlickAnimationTimer();
1382
1383                 LoadItems();
1384                 AdjustItemPosition(__distance);
1385                 Draw();
1386         }
1387         else
1388         {
1389                 __initialAnimationValue = 0.0f;
1390                 __isInitialAnimation = false;
1391                 ResetFlickAnimationTimer();
1392                 ValidateAndAdjustStartPosition();
1393         }
1394
1395         return;
1396 }
1397
1398 result
1399 _DateTimeBarPresenter::StartFlickAnimationTimer(void)
1400 {
1401         result r = E_SUCCESS;
1402
1403         if (__pFlickAnimationTimer == null)
1404         {
1405                 __pFlickAnimationTimer = new (std::nothrow) Timer;
1406                 SysTryReturn(NID_UI_CTRL, (__pFlickAnimationTimer != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create timer.");
1407
1408                 r = __pFlickAnimationTimer->Construct(*this);
1409                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1410         }
1411         else
1412         {
1413                 __pFlickAnimationTimer->Cancel();
1414         }
1415
1416         r = __pFlickAnimationTimer->Start(FLICK_ANIMATION_TIMER_PERIOD);
1417         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1418
1419         return r;
1420
1421 CATCH:
1422         ResetFlickAnimationTimer();
1423         return r;
1424 }
1425
1426 result
1427 _DateTimeBarPresenter::ResetFlickAnimationTimer(void)
1428 {
1429         if (__pFlickAnimationTimer)
1430         {
1431                 delete __pFlickAnimationTimer;
1432                 __pFlickAnimationTimer = null;
1433         }
1434
1435         __isFlickInProgress = false;
1436         return E_SUCCESS;
1437 }
1438
1439 void
1440 _DateTimeBarPresenter::OnTimerExpired(Timer& timer)
1441 {
1442         __isFlickInProgress = false;
1443         if (&timer == __pFlickAnimationTimer)
1444         {
1445                 if (__isInitialAnimation)
1446                 {
1447                         StartAnimationEffect();
1448                 }
1449                 else
1450                 {
1451                         StartFlickAnimation();
1452                 }
1453         }
1454         return;
1455 }
1456
1457 void
1458 _DateTimeBarPresenter::ValidateAndAdjustStartPosition(void)
1459 {
1460         int index = -1;
1461         float adjustPosition = 0.0f;
1462         float leftMargin =  GetLeftRightMargin();
1463         FloatRectangle bodyBounds = GetBodyBounds();
1464
1465         index = GetItemIndexFromPosition(FloatPoint(GetLeftRightMargin() + GetItemMargin(), bodyBounds.y + (bodyBounds.height / 2.0f)));
1466
1467         if (index == -1)
1468         {
1469                 return;
1470         }
1471
1472         _DateTimeBarItem* pDrawItem = null;
1473         pDrawItem = __pDateTimeBarModel->GetItemAt(index);
1474
1475         if (pDrawItem == null)
1476         {
1477                 return;
1478         }
1479
1480         if (pDrawItem->GetBounds().x > 0.0f && pDrawItem->GetBounds().x < leftMargin)
1481         {
1482                 adjustPosition = leftMargin - pDrawItem->GetBounds().x;
1483         }
1484         else if (pDrawItem->GetBounds().x < 0.0f)
1485         {
1486                 pDrawItem = __pDateTimeBarModel->GetItemAt(index + 1);
1487                 if (pDrawItem == null)
1488                 {
1489                         return;
1490                 }
1491                 adjustPosition = leftMargin - pDrawItem->GetBounds().x;
1492         }
1493         else if (pDrawItem->GetBounds().x > leftMargin)
1494         {
1495                 adjustPosition = leftMargin - pDrawItem->GetBounds().x;
1496         }
1497
1498         AdjustItemPosition(adjustPosition);
1499         Draw();
1500         return;
1501 }
1502
1503 void
1504 _DateTimeBarPresenter::SetInitialAnimationValue(float animationValue)
1505 {
1506         __initialAnimationValue = animationValue;
1507         return;
1508 }
1509
1510 void
1511 _DateTimeBarPresenter::SetFont(Font& pFont)
1512 {
1513         delete __pFont;
1514         __pFont = null;
1515
1516         __pFont = _FontImpl::CloneN(pFont);
1517         SysTryReturnVoidResult(NID_UI_CTRL, (__pFont != null), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1518
1519         return;
1520 }
1521
1522 bool
1523 _DateTimeBarPresenter::OnAccessibilityFocusMovedNext(const _AccessibilityContainer& control, const _AccessibilityElement& element)
1524 {
1525         return false;
1526 }
1527
1528 bool
1529 _DateTimeBarPresenter::OnAccessibilityFocusMovedPrevious(const _AccessibilityContainer& control, const _AccessibilityElement& element)
1530 {
1531         return false;
1532 }
1533
1534 void
1535 _DateTimeBarPresenter::OnTouchMoveHandled(const Tizen::Ui::_Control& control)
1536 {
1537         __touchMoveHandled = true;
1538
1539         __isTouchMoved = false;
1540         __distance = 0.0f;
1541
1542         __pDateTimeBar->Invalidate();
1543
1544         return;
1545 }
1546
1547 void
1548 _DateTimeBarPresenter::AddAccessibilityElement(void)
1549 {
1550         _DateTimeBarItem* pItem = null;
1551         pItem = __pDateTimeBarModel->GetItemAt(__pDateTimeBarModel->GetItemCount() - 1);
1552
1553         if (pItem == null)
1554         {
1555                 return;
1556         }
1557
1558         if (__pDateTimeBar->GetSelectedBoxId() == DATETIME_ID_MONTH)
1559         {
1560                 __pDateTimeBar->AddAccessibilityElement(pItem->GetBounds(), __pDateTimeBar->GetMonthValue(pItem->GetActionId()));
1561         }
1562         else
1563         {
1564                 __pDateTimeBar->AddAccessibilityElement(pItem->GetBounds(), pItem->GetText());
1565         }
1566
1567         return;
1568 }
1569
1570 void
1571 _DateTimeBarPresenter::InsertAccessibilityElementAt(int index)
1572 {
1573         _DateTimeBarItem* pItem = null;
1574         pItem = __pDateTimeBarModel->GetItemAt(index);
1575
1576         if (pItem == null)
1577         {
1578                 return;
1579         }
1580
1581         if (__pDateTimeBar->GetSelectedBoxId() == DATETIME_ID_MONTH)
1582         {
1583                 __pDateTimeBar->InsertAccessibilityElementAt(index, pItem->GetBounds(), __pDateTimeBar->GetMonthValue(pItem->GetActionId()));
1584         }
1585         else
1586         {
1587                 __pDateTimeBar->InsertAccessibilityElementAt(index, pItem->GetBounds(), pItem->GetText());
1588         }
1589
1590         return;
1591 }
1592
1593 void
1594 _DateTimeBarPresenter::MoveNext(void)
1595 {
1596         __distance = (GetItemWidth() + GetItemMargin()) * (-1.0f);
1597         LoadItems();
1598         AdjustItemPosition(__distance);
1599         Draw();
1600         return;
1601 }
1602
1603 void
1604 _DateTimeBarPresenter::MovePrevious(void)
1605 {
1606         __distance = (GetItemWidth() + GetItemMargin());
1607         LoadItems();
1608         AdjustItemPosition(__distance);
1609         Draw();
1610         return;
1611 }
1612
1613 }}} // Tizen::Ui::Controls