8cfade295c4c16f92c84c0669da66a95ef5e9be5
[framework/osp/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 "FUiAnim_VisualElement.h"
29 #include "FUiCtrl_DateTimeUtils.h"
30 #include "FUiCtrl_DateTimeBar.h"
31 #include "FUiCtrl_DateTimeBarPresenter.h"
32
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Ui;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Graphics::_Text;
38 using namespace Tizen::Ui::Animations;
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         , __focusedText(L"")
56         , __bodyAreaBounds(FloatRectangle())
57         , __arrowAreaBounds(FloatRectangle())
58         , __windowAreaBounds(FloatRectangle())
59         , __pBgColorReplacementBitmap(null)
60         , __pFocusBitmap(null)
61         , __pArrowColorReplacementBitmap(null)
62         , __isInitialAnimation(false)
63         , __initialAnimationValue(0.0f)
64         , __pFont(null)
65         , __isFocused(false)
66         , __focusedIndex(-1)
67         , __focusBounds(FloatRectangle())
68         , __isFlickCanceled(false)
69 {
70 }
71
72 _DateTimeBarPresenter::~_DateTimeBarPresenter(void)
73 {
74         delete __pDateTimeBarModel;
75         __pDateTimeBarModel = 0;
76
77         delete __pBgColorReplacementBitmap;
78         __pBgColorReplacementBitmap = null;
79
80         delete __pFocusBitmap;
81         __pFocusBitmap = null;
82
83         delete __pArrowColorReplacementBitmap;
84         __pArrowColorReplacementBitmap = null;
85
86         delete __pFlickAnimationTimer;
87         __pFlickAnimationTimer = null;
88
89         delete __pFont;
90         __pFont = null;
91 }
92
93 _DateTimeBarPresenter*
94 _DateTimeBarPresenter::CreateInstanceN(_DateTimeBar& dateTimeBar)
95 {
96         result r = E_SUCCESS;
97
98         _DateTimeBarPresenter* pDateTimeBarPresenter = new (std::nothrow) _DateTimeBarPresenter(&dateTimeBar);
99         SysTryReturn(NID_UI_CTRL, pDateTimeBarPresenter, null, E_OUT_OF_MEMORY,
100                         "[E_OUT_OF_MEMORY] Memory allocation failed.");
101
102         pDateTimeBarPresenter->__pDateTimeBarModel = _DateTimeBarModel::CreateInstanceN();
103         r = GetLastResult();
104         SysTryCatch(NID_UI_CTRL, pDateTimeBarPresenter->__pDateTimeBarModel, , r,
105                         "[%s] Propagating.", GetErrorMessage(r));
106
107         r = GET_SHAPE_CONFIG(DATETIMEBAR::ARROW_WIDTH, dateTimeBar.GetOrientation(), pDateTimeBarPresenter->__arrowAreaBounds.width);
108         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to get arrow width from resource.", GetErrorMessage(r));
109
110         r = GET_SHAPE_CONFIG(DATETIMEBAR::ARROW_HEIGHT, dateTimeBar.GetOrientation(), pDateTimeBarPresenter->__arrowAreaBounds.height);
111         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to get arrow height from resource.", GetErrorMessage(r));
112
113         r = pDateTimeBarPresenter->LoadResource();
114         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to load the resources.", GetErrorMessage(r));
115
116         return pDateTimeBarPresenter;
117
118 CATCH:
119         delete pDateTimeBarPresenter;
120
121         return null;
122 }
123
124 void
125 _DateTimeBarPresenter::AddFlickAnimationInfo()
126 {
127         __flickAnimation.SetSensitivity(FLICK_ANIMATION_FPS_DATE_TIME_BAR, FLICK_ANIMATION_SENSITIVITY_DATE_TIME_BAR);
128         __flickAnimation.SetDirection(FD_HORIZONTAL);
129 }
130
131 result
132 _DateTimeBarPresenter::LoadResource(void)
133 {
134         result r = E_SUCCESS;
135         Color bgBitmapColor;
136         Bitmap* pBackgroundNormalBitmap = null;
137
138         r = GET_BITMAP_CONFIG_N(DATETIMEBAR::TIMEPICKERBAR_BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pBackgroundNormalBitmap);
139         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
140
141         GET_COLOR_CONFIG(DATETIMEBAR::BG_NORMAL, bgBitmapColor);
142         __pBgColorReplacementBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pBackgroundNormalBitmap,
143                                                                            Color::GetColor(COLOR_ID_MAGENTA), bgBitmapColor);
144         SysTryCatch(NID_UI_CTRL, __pBgColorReplacementBitmap != null, r = GetLastResult(), GetLastResult(), "[%s] Propagating.",
145                     GetErrorMessage(GetLastResult()));
146
147         r = GET_BITMAP_CONFIG_N(FOCUSUI::FOCUS, BITMAP_PIXEL_FORMAT_ARGB8888, __pFocusBitmap);
148         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
149
150         r = LoadArrowBitmap();
151         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to load resources.", GetErrorMessage(r));
152
153         delete pBackgroundNormalBitmap;
154         pBackgroundNormalBitmap = null;
155
156         AddFlickAnimationInfo();
157
158         return r;
159
160 CATCH:
161
162         delete pBackgroundNormalBitmap;
163         pBackgroundNormalBitmap = null;
164
165         delete __pBgColorReplacementBitmap;
166         __pBgColorReplacementBitmap = null;
167
168         delete __pFocusBitmap;
169         __pFocusBitmap = null;
170
171         return r;
172 }
173
174 result
175 _DateTimeBarPresenter::LoadArrowBitmap(void)
176 {
177         result r = E_SUCCESS;
178         Color arrowColor;
179         Bitmap* pArrowNormalBitmap = null;
180
181         GET_COLOR_CONFIG(DATETIMEBAR::BG_NORMAL, arrowColor);
182
183         if (__pDateTimeBar->GetAlignment() == DATETIME_BAR_ALIGN_DOWN)
184         {
185                 r = GET_BITMAP_CONFIG_N(DATETIMEBAR::TAIL_DOWN, BITMAP_PIXEL_FORMAT_ARGB8888, pArrowNormalBitmap);
186                 SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
187         }
188         else
189         {
190                 r = GET_BITMAP_CONFIG_N(DATETIMEBAR::TAIL_UP, BITMAP_PIXEL_FORMAT_ARGB8888, pArrowNormalBitmap);
191                 SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
192         }
193
194         if (__pArrowColorReplacementBitmap != null)
195         {
196                 delete __pArrowColorReplacementBitmap;
197                 __pArrowColorReplacementBitmap = null;
198         }
199
200         __pArrowColorReplacementBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pArrowNormalBitmap,
201                                                                                 Color::GetColor(COLOR_ID_MAGENTA), arrowColor);
202         SysTryCatch(NID_UI_CTRL, __pArrowColorReplacementBitmap != null, r = GetLastResult(), GetLastResult(), "[%s] Propagating.",
203                         GetErrorMessage(GetLastResult()));
204
205         delete pArrowNormalBitmap;
206         pArrowNormalBitmap = null;
207
208         return r;
209
210 CATCH:
211
212         delete pArrowNormalBitmap;
213         pArrowNormalBitmap = null;
214
215         delete __pArrowColorReplacementBitmap;
216         __pArrowColorReplacementBitmap = null;
217
218         return r;
219 }
220
221 result
222 _DateTimeBarPresenter::Draw(void)
223 {
224         result r = E_SUCCESS;
225
226         Canvas* pCanvas = __pDateTimeBar->GetCanvasN();
227         SysAssertf((pCanvas != null), "Failed to get canvas.");
228
229         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
230
231         r = pCanvas->Clear();
232         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
233
234         r = DrawBackground(*pCanvas);
235         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
236
237         if (__isFocused)
238         {
239                 ChangeFocusStatus();
240         }
241
242         r = DrawItem(*pCanvas);
243         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
244
245         r = DrawArrow(*pCanvas);
246         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
247
248
249 CATCH:
250         delete pCanvas;
251         return r;
252 }
253
254 void
255 _DateTimeBarPresenter::ChangeFocusStatus(void)
256 {
257         _DateTimeBarItem* pItem = null;
258
259         if (__focusedIndex == -1)
260         {
261                 __focusedIndex = GetSelectedItemIndex() - 1;
262         }
263         pItem = GetItemAt(__focusedIndex);
264
265         if (pItem != null)
266         {
267                 __focusedText.Clear();
268                 __focusedText.Append(pItem->GetText().GetPointer());
269         }
270         SetItemStatus(__focusedIndex, DATETIMEBAR_ITEM_STATUS_HIGHLIGHTED);
271 }
272
273 void
274 _DateTimeBarPresenter::SetItemFocused(bool isFocused)
275 {
276         __isFocused = isFocused;
277         if (!__isFocused)
278         {
279                 __focusedIndex = -1;
280         }
281         return;
282 }
283
284 void
285 _DateTimeBarPresenter::SetFocusedItemBounds()
286 {
287         _DateTimeBarItem* pItem = null;
288
289         if (__focusedIndex > 0)
290         {
291                 pItem = GetItemAt(__focusedIndex);
292                 SysTryReturnVoidResult(NID_UI,  pItem != NULL, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
293                 __focusBounds = pItem->GetBounds();
294         }
295 }
296
297 void
298 _DateTimeBarPresenter::FocusModeChanged(void)
299 {
300         __isFocused = false;
301         SetItemStatus(__focusedIndex, DATETIMEBAR_ITEM_STATUS_NORMAL);
302         return;
303 }
304
305 result
306 _DateTimeBarPresenter::DrawItem(Canvas& canvas)
307 {
308         result r = E_SUCCESS;
309
310         FloatRectangle clientBounds = canvas.GetBoundsF();
311
312         _DateTimeBarItem* pDrawItem = null;
313         FloatRectangle itemBounds(0.0f, 0.0f, 0.0f, 0.0f);
314
315         String drawText;
316
317         Color textColor;
318         GET_COLOR_CONFIG(DATETIMEBAR::TEXT_NORMAL, textColor);
319
320         Color textPressedColor;
321         GET_COLOR_CONFIG(DATETIMEBAR::TEXT_PRESSED, textPressedColor);
322
323         _DateTimeBarItemStatus itemStatus = DATETIMEBAR_ITEM_STATUS_NORMAL;
324
325         int itemCount = GetItemCount();
326
327         for (int i = __pDateTimeBarModel->GetFirstDrawnItemIndex(); i < itemCount; i++)
328         {
329                 pDrawItem = __pDateTimeBarModel->GetItemAt(i);
330                 if (pDrawItem == null)
331                 {
332                         break;
333                 }
334
335                 itemBounds = pDrawItem->GetBounds();
336
337                 if (itemBounds.x + itemBounds.width <= 0)
338                 {
339                         continue;
340                 }
341
342                 drawText = pDrawItem->GetText();
343                 if (__isFocused)
344                 {
345                         if (drawText.CompareTo(__focusedText) == 0)
346                         {
347                                 pDrawItem->SetStatus(DATETIMEBAR_ITEM_STATUS_HIGHLIGHTED);
348                         }
349                         else
350                         {
351                                 pDrawItem->SetStatus(DATETIMEBAR_ITEM_STATUS_NORMAL);
352                         }
353                 }
354                 else
355                 {
356                         if (drawText.CompareTo(__selectedText) == 0)
357                         {
358                                 pDrawItem->SetStatus(DATETIMEBAR_ITEM_STATUS_SELECTED);
359                         }
360                 }
361
362                 itemStatus = pDrawItem->GetStatus();
363
364                 float fontSize = GetFontSize();
365
366                 TextSimple* pSimpleText = null;
367                 TextObject* pTextObject = new (std::nothrow) TextObject;
368                 SysTryReturn(NID_UI_CTRL, (pTextObject != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
369
370                 pTextObject->Construct();
371                 pSimpleText = new (std::nothrow)TextSimple((const_cast <wchar_t*>(drawText.GetPointer())), drawText.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
372                 pTextObject->AppendElement(*pSimpleText);
373
374                 pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
375                 pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
376                 pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
377
378                 SysAssertf(__pFont != null, "Font instance is null");
379
380                 if (itemStatus == DATETIMEBAR_ITEM_STATUS_SELECTED || itemStatus == DATETIMEBAR_ITEM_STATUS_HIGHLIGHTED)
381                 {
382                         (_FontImpl::GetInstance(*__pFont))->SetSize(fontSize);
383                         (_FontImpl::GetInstance(*__pFont))->SetStyle(FONT_STYLE_BOLD);
384                         pTextObject->SetFont(__pFont, 0, pTextObject->GetTextLength());
385                         pTextObject->SetForegroundColor(textPressedColor, 0, pTextObject->GetTextLength());
386                 }
387                 else
388                 {
389                         (_FontImpl::GetInstance(*__pFont))->SetSize(fontSize);
390                         (_FontImpl::GetInstance(*__pFont))->SetStyle(FONT_STYLE_PLAIN);
391                         pTextObject->SetFont(__pFont, 0, pTextObject->GetTextLength());
392                         pTextObject->SetForegroundColor(textColor, 0, pTextObject->GetTextLength());
393                 }
394
395                 pTextObject->SetBounds(itemBounds);
396                 pTextObject->Draw(*_CanvasImpl::GetInstance(canvas));
397
398                 delete pTextObject;
399
400                 if ((itemBounds.x + itemBounds.width + GetLeftRightMargin()) >= clientBounds.width)
401                 {
402                         break;
403                 }
404         }
405
406         return r;
407 }
408
409 result
410 _DateTimeBarPresenter::DrawBackground(Canvas& canvas)
411 {
412         result r = E_SUCCESS;
413
414         FloatRectangle bodyAreaBounds = GetBodyBounds();
415
416         if (__pBgColorReplacementBitmap != null)
417         {
418                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pBgColorReplacementBitmap))
419                 {
420                         r = canvas.DrawNinePatchedBitmap(bodyAreaBounds, *__pBgColorReplacementBitmap);
421                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
422                 }
423                 else
424                 {
425                         r = canvas.DrawBitmap(bodyAreaBounds, *__pBgColorReplacementBitmap);
426                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
427                 }
428         }
429
430         else
431         {
432                 r = canvas.DrawRectangle(bodyAreaBounds);
433                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
434         }
435
436         return r;
437 }
438
439 result
440 _DateTimeBarPresenter::DrawArrow(Canvas& canvas)
441 {
442         result r = E_SUCCESS;
443         FloatRectangle arrowAreaBounds = GetArrowBounds();
444
445         if (__pArrowColorReplacementBitmap != null)
446         {
447                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pArrowColorReplacementBitmap))
448                 {
449                         r = canvas.DrawNinePatchedBitmap(arrowAreaBounds, *__pArrowColorReplacementBitmap);
450                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
451                 }
452                 else
453                 {
454                         r = canvas.DrawBitmap(arrowAreaBounds, *__pArrowColorReplacementBitmap);
455                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
456                 }
457         }
458
459         return r;
460 }
461
462 void
463 _DateTimeBarPresenter::SetItemSelected(int index)
464 {
465         __pDateTimeBarModel->SetSelectedItemIndex(index);
466         return;
467 }
468
469 int
470 _DateTimeBarPresenter::GetSelectedItemIndex(void) const
471 {
472         return __pDateTimeBarModel->GetSelectedItemIndex();
473 }
474
475 int
476 _DateTimeBarPresenter::GetItemIndexFromPosition(const FloatPoint& point) const
477 {
478         FloatRectangle clientBounds = __pDateTimeBar->GetBoundsF();
479
480         if (point.x > clientBounds.width
481                 || point.y > clientBounds.height)
482         {
483                 return -1;
484         }
485
486         int itemIndex = __pDateTimeBarModel->GetFirstDrawnItemIndex();
487         _DateTimeBarItem* pItem = __pDateTimeBarModel->GetItemAt(itemIndex);
488         float startX = 0.0f;
489         float itemMargin = 0.0f;
490         itemMargin = GetItemMargin();
491
492         while (pItem != null)
493         {
494                 FloatRectangle itemBounds = pItem->GetBounds();
495
496                 if (itemBounds.x > clientBounds.x)
497                 {
498                         if (itemIndex == __pDateTimeBarModel->GetFirstDrawnItemIndex())
499                         {
500                                 startX = itemBounds.x;
501                         }
502
503                         itemBounds.x -= startX;
504                 }
505
506                 itemBounds.x = itemBounds.x - (itemMargin / 2.0f);
507                 itemBounds.width = itemBounds.width + itemMargin;
508
509                 if (itemBounds.Contains(point) == true)
510                 {
511                         return itemIndex;
512                 }
513
514                 itemIndex++;
515
516                 if (itemIndex == GetItemCount())
517                 {
518                         break;
519                 }
520
521                 pItem = __pDateTimeBarModel->GetItemAt(itemIndex);
522         }
523
524         return -1;
525 }
526
527 result
528 _DateTimeBarPresenter::SetItemStatus(int index, _DateTimeBarItemStatus status)
529 {
530         _DateTimeBarItem* pItem = __pDateTimeBarModel->GetItemAt(index);
531         SysTryReturn(NID_UI_CTRL, pItem, E_SYSTEM, E_SYSTEM,
532                  "[E_SYSTEM] A system error has occurred. Failed to get _DateTimeBarItem at index (%d).", index);
533
534         if (status == DATETIMEBAR_ITEM_STATUS_SELECTED)
535         {
536                 __pDateTimeBarModel->SetSelectedItemIndex(index);
537         }
538         pItem->SetStatus(status);
539
540         return E_SUCCESS;
541 }
542
543 _DateTimeBarItemStatus
544 _DateTimeBarPresenter::GetItemStatus(int index) const
545 {
546         _DateTimeBarItem* pItem = __pDateTimeBarModel->GetItemAt(index);
547         SysTryReturn(NID_UI_CTRL, pItem, DATETIMEBAR_ITEM_STATUS_NORMAL, E_SYSTEM,
548                  "[E_SYSTEM] A system error has occurred. Failed to get _DateTimeBarItem at index (%d).", index);
549
550         return pItem->GetStatus();
551 }
552
553 int
554 _DateTimeBarPresenter::GetFirstDrawnItemIndex(void) const
555 {
556         return __pDateTimeBarModel->GetFirstDrawnItemIndex();
557 }
558
559 void
560 _DateTimeBarPresenter::SetFirstDrawnItemIndex(int index)
561 {
562         __pDateTimeBarModel->SetFirstDrawnItemIndex(index);
563         return;
564 }
565
566 int
567 _DateTimeBarPresenter::GetMinimumValue(void) const
568 {
569         return __pDateTimeBarModel->GetMinimumValue();
570 }
571
572 void
573 _DateTimeBarPresenter::SetMinimumValue(int minValue)
574 {
575         __pDateTimeBarModel->SetMinimumValue(minValue);
576         return;
577 }
578
579 int
580 _DateTimeBarPresenter::GetMaximumValue(void) const
581 {
582         return __pDateTimeBarModel->GetMaximumValue();
583 }
584
585 void
586 _DateTimeBarPresenter::SetMaximumValue(int maxValue)
587 {
588         __pDateTimeBarModel->SetMaximumValue(maxValue);
589         return;
590 }
591
592 float
593 _DateTimeBarPresenter::GetItemWidth(void) const
594 {
595         result r = E_SUCCESS;
596
597         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
598
599         float itemWidth = 0.0f;
600
601         if (boxId == DATETIME_ID_YEAR)
602         {
603                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
604         }
605         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
606         {
607                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
608         }
609         else if (boxId == DATETIME_ID_MONTH)
610         {
611                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
612
613                 FloatDimension textArea;
614
615                 float fontSize = 0;
616                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_FONT_SIZE, __pDateTimeBar->GetOrientation(), fontSize);
617
618                 SysAssertf(__pFont != null, "Font instance is null");
619
620                 (_FontImpl::GetInstance(*__pFont))->SetSize(fontSize);
621                 (_FontImpl::GetInstance(*__pFont))->SetStyle(FONT_STYLE_PLAIN);
622
623                 for (int i = DATETIME_MONTH_MIN; i <= DATETIME_MONTH_MAX; i++)
624                 {
625                         String string;
626                         _DateTimeUtils dateTimeUtils;
627                         string = dateTimeUtils.GetMonthString(i);
628
629                         r = __pFont->GetTextExtent(string, string.GetLength(), textArea);
630                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
631
632                         if (textArea.width > itemWidth)
633                         {
634                                 itemWidth = textArea.width;
635                         }
636                 }
637         }
638
639         return itemWidth;
640 }
641
642 float
643 _DateTimeBarPresenter::GetFontSize(void) const
644 {
645         float fontSize = 0.0f;
646         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
647
648         if (boxId == DATETIME_ID_YEAR)
649         {
650                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_FONT_SIZE, _ControlManager::GetInstance()->GetOrientation(), fontSize);
651         }
652         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
653         {
654                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_FONT_SIZE, _ControlManager::GetInstance()->GetOrientation(), fontSize);
655         }
656         else if (boxId == DATETIME_ID_MONTH)
657         {
658                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_FONT_SIZE, _ControlManager::GetInstance()->GetOrientation(), fontSize);
659         }
660
661         return fontSize;
662 }
663
664 float
665 _DateTimeBarPresenter::GetLeftRightMargin(void) const
666 {
667         float leftRightMargin = 0.0f;
668         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
669
670         if (boxId == DATETIME_ID_YEAR)
671         {
672                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_LEFT_RIGHT_MARGIN, _ControlManager::GetInstance()->GetOrientation(), leftRightMargin);
673         }
674         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
675         {
676                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_LEFT_RIGHT_MARGIN, _ControlManager::GetInstance()->GetOrientation(), leftRightMargin);
677         }
678         else if (boxId == DATETIME_ID_MONTH)
679         {
680                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_LEFT_RIGHT_MARGIN, _ControlManager::GetInstance()->GetOrientation(), leftRightMargin);
681         }
682
683         return leftRightMargin;
684 }
685
686 float
687 _DateTimeBarPresenter::GetItemMargin(void) const
688 {
689         float margin = 0.0f;
690         _DateTimeId boxId = __pDateTimeBar->GetSelectedBoxId();
691
692         if (boxId == DATETIME_ID_YEAR)
693         {
694                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_ITEM_MARGIN, _ControlManager::GetInstance()->GetOrientation(), margin);
695         }
696         else if (boxId == DATETIME_ID_DAY || boxId == DATETIME_ID_HOUR || boxId == DATETIME_ID_MINUTE)
697         {
698                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_ITEM_MARGIN, _ControlManager::GetInstance()->GetOrientation(), margin);
699         }
700         else if (boxId == DATETIME_ID_MONTH)
701         {
702                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_ITEM_MARGIN, _ControlManager::GetInstance()->GetOrientation(), margin);
703         }
704
705         return margin;
706 }
707
708 result
709 _DateTimeBarPresenter::AdjustItemPosition(float distance)
710 {
711         int index = 0;
712
713         _DateTimeBarItem* pItem = null;
714
715         pItem = __pDateTimeBarModel->GetItemAt(index);
716
717         while (pItem != null)
718         {
719                 FloatRectangle bounds = pItem->GetBounds();
720                 bounds.x += distance;
721                 pItem->SetBounds(bounds);
722                 index++;
723
724                 if (index == GetItemCount())
725                 {
726                         break;
727                 }
728
729                 pItem = __pDateTimeBarModel->GetItemAt(index);
730         }
731
732         return AdjustAccessibilityElementPosition(distance);
733 }
734
735 result
736 _DateTimeBarPresenter::AdjustAccessibilityElementPosition(float distance)
737 {
738         _AccessibilityElement* pAccessibilityElement = null;
739
740         int index = 0;
741
742         pAccessibilityElement = __pDateTimeBar->GetAccessibilityElementAt(index);
743
744         while (pAccessibilityElement != null)
745         {
746                 FloatRectangle bounds = pAccessibilityElement->GetBounds();
747                 bounds.x += distance;
748                 pAccessibilityElement->SetBounds(bounds);
749                 index++;
750
751                 if (index == GetItemCount())
752                 {
753                         break;
754                 }
755
756                 pAccessibilityElement = __pDateTimeBar->GetAccessibilityElementAt(index);
757         }
758
759         return E_SUCCESS;
760 }
761
762 String
763 _DateTimeBarPresenter::GetDisplayedText(int value) const
764 {
765         String string;
766
767         if (__pDateTimeBar->GetSelectedBoxId() == DATETIME_ID_MONTH)
768         {
769                 String monthString;
770                 _DateTimeUtils dateTimeUtils;
771                 monthString = dateTimeUtils.GetMonthString(value);
772                 string.Append(monthString);
773         }
774         else if (__pDateTimeBar->GetSelectedBoxId() == DATETIME_ID_YEAR)
775         {
776                 string.Format(10, L"%04d", value);
777         }
778         else
779         {
780                 string.Format(10, L"%02d", value);
781         }
782
783         return string;
784 }
785
786 result
787 _DateTimeBarPresenter::AddItems(int actionId)
788 {
789         result r = E_SUCCESS;
790         int cachingSize = __pDateTimeBarModel->GetMaxCachingSize() / 2;
791
792         int firstDrawItemIndex = cachingSize;
793
794         int value = actionId;
795         float startPosition = 0.0f;
796         float itemWidth = GetItemWidth();
797         float itemMargin = GetItemMargin();
798
799         startPosition = (GetWindowBounds().width) / 2.0f;
800         startPosition = startPosition + (itemWidth / 2.0f) + itemMargin;
801         __pDateTimeBarModel->SetItemStartPosition(startPosition);
802
803         while (cachingSize > 0)
804         {
805                 String string;
806
807                 string = GetDisplayedText(value);
808
809                 r = __pDateTimeBarModel->AddItem(string, value, __pDateTimeBar->GetAlignment(), itemWidth, itemMargin);
810                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
811
812                 AddAccessibilityElement();
813                 cachingSize--;
814
815                 value++;
816
817                 if (__pDateTimeBarModel->GetMaximumValue() < value)
818                 {
819                         value = __pDateTimeBarModel->GetMinimumValue();
820                 }
821         }
822
823         r = InsertItems(actionId -1);
824         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
825
826         _DateTimeBarItem* pItem = null;
827         pItem = GetItemAt(firstDrawItemIndex);
828
829         if (pItem != null)
830         {
831                 __selectedText.Clear();
832                 __selectedText.Append(pItem->GetText().GetPointer());
833                 __focusedIndex = (firstDrawItemIndex - 1);
834                 __focusedValue = pItem->GetActionId();
835         }
836
837         SetItemStatus(firstDrawItemIndex, DATETIMEBAR_ITEM_STATUS_SELECTED);
838
839         return r;
840 }
841
842 result
843 _DateTimeBarPresenter::InsertItems(int actionId)
844 {
845         result r = E_SUCCESS;
846
847         int cachingSize = __pDateTimeBarModel->GetMaxCachingSize() / 2;
848
849         int value = actionId;
850         float itemWidth = GetItemWidth();
851         float itemMargin = GetItemMargin();
852
853         while (cachingSize > 0)
854         {
855                 String string;
856
857                 if (value < __pDateTimeBarModel->GetMinimumValue())
858                 {
859                         value = __pDateTimeBarModel->GetMaximumValue();
860                 }
861
862                 string = GetDisplayedText(value);
863                 r = __pDateTimeBarModel->InsertItemAt(0, string, value, __pDateTimeBar->GetAlignment(), itemWidth, itemMargin);
864                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
865                 InsertAccessibilityElementAt(0);
866                 cachingSize--;
867
868                 value--;
869         }
870
871         return r;
872 }
873
874 result
875 _DateTimeBarPresenter::InsertItem(int index, int actionId)
876 {
877         result r = E_SUCCESS;
878
879         String string;
880
881         string = GetDisplayedText(actionId);
882
883         r = __pDateTimeBarModel->InsertItemAt(index, string, actionId, __pDateTimeBar->GetAlignment(), GetItemWidth(), GetItemMargin());
884         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
885         InsertAccessibilityElementAt(index);
886         return r;
887 }
888
889 result
890 _DateTimeBarPresenter::AddItem(int actionId)
891 {
892         result r = E_SUCCESS;
893
894         String string;
895
896         string = GetDisplayedText(actionId);
897
898         r = __pDateTimeBarModel->AddItem(string, actionId, __pDateTimeBar->GetAlignment(), GetItemWidth(), GetItemMargin());
899         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
900
901         AddAccessibilityElement();
902         return r;
903 }
904
905 result
906 _DateTimeBarPresenter::DeleteItem(int index)
907 {
908         int itemCount = GetItemCount();
909
910         SysTryReturn(NID_UI_CTRL, itemCount > 0, E_INVALID_ARG, E_INVALID_ARG,
911                         "[E_INVALID_ARG] Invalid argument is used. index = %d", index);
912
913         SysTryReturn(NID_UI_CTRL, index < itemCount, E_INVALID_ARG, E_INVALID_ARG,
914                         "[E_INVALID_ARG] Invalid argument is used. index = %d", index);
915
916         SysTryReturn(NID_UI_CTRL, index >= 0, E_INVALID_ARG, E_INVALID_ARG,
917                         "[E_INVALID_ARG] Invalid argument is used. index = %d", index);
918
919         result r = E_SUCCESS;
920
921         r = __pDateTimeBarModel->RemoveItemAt(index);
922         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
923
924         __pDateTimeBar->RemoveAccessibilityElementAt(index);
925
926         return r;
927 }
928
929 result
930 _DateTimeBarPresenter::RemoveAllItems(void)
931 {
932         SysTryReturn(NID_UI_CTRL, GetItemCount() > 0, E_INVALID_OPERATION, E_INVALID_OPERATION,
933                         "[E_INVALID_OPERATION] Items count is zero.");
934
935         result r = E_SUCCESS;
936
937         __pDateTimeBarModel->RemoveAllItems();
938
939         __pDateTimeBar->RemoveAllAccessibilityElement();
940
941         return r;
942 }
943
944 int
945 _DateTimeBarPresenter::GetItemCount(void) const
946 {
947         return __pDateTimeBarModel->GetItemCount();
948 }
949
950 _DateTimeBarItem*
951 _DateTimeBarPresenter::GetItemAt(int index) const
952 {
953         return __pDateTimeBarModel->GetItemAt(index);
954 }
955
956 result
957 _DateTimeBarPresenter::CalculateWindowBounds(void)
958 {
959         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
960         FloatRectangle windowAreaBounds = FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f);
961         FloatRectangle bodyAreaBounds = FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f);
962         FloatRectangle arrowAreaBounds = FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f);
963
964         FloatRectangle parentWindowBounds = __pDateTimeBar->GetParentWindowBounds();
965
966         arrowAreaBounds = GetArrowBounds();
967
968         Point arrowPosition;
969
970         GET_SHAPE_CONFIG(DATETIMEBAR::ARROW_HEIGHT, orientation, arrowAreaBounds.height);
971         GET_SHAPE_CONFIG(DATETIMEBAR::ARROW_WIDTH, orientation, arrowAreaBounds.width);
972         GET_SHAPE_CONFIG(DATETIMEBAR::ITEM_HEIGHT, orientation, bodyAreaBounds.height);
973
974         bodyAreaBounds.width = parentWindowBounds.width;
975
976         bodyAreaBounds.x = 0.0f;
977         FloatPoint tempPoint = __pDateTimeBar->GetPositionF();
978         windowAreaBounds.x = tempPoint.x;
979
980         if (__pDateTimeBar->GetAlignment() == DATETIME_BAR_ALIGN_DOWN)
981         {
982                 bodyAreaBounds.y = arrowAreaBounds.height;
983                 arrowAreaBounds.y = 0.0f;
984                 windowAreaBounds.y = tempPoint.y - arrowAreaBounds.height;
985         }
986         else
987         {
988                 arrowAreaBounds.y = bodyAreaBounds.height;
989                 bodyAreaBounds.y = 0.0f;
990                 windowAreaBounds.y = tempPoint.y;
991         }
992
993         windowAreaBounds.width = bodyAreaBounds.width;
994         windowAreaBounds.height = bodyAreaBounds.height + arrowAreaBounds.height;
995
996         SetBodyBounds(bodyAreaBounds);
997         SetArrowBounds(arrowAreaBounds);
998         SetWindowBounds(windowAreaBounds);
999         __pDateTimeBar->SetMovable(true);
1000         __pDateTimeBar->SetResizable(true);
1001         __pDateTimeBar->SetBounds(windowAreaBounds);
1002         __pDateTimeBar->SetMovable(false);
1003         __pDateTimeBar->SetResizable(false);
1004
1005         return E_SUCCESS;
1006 }
1007
1008 void
1009 _DateTimeBarPresenter::SetBodyBounds(const FloatRectangle& bounds)
1010 {
1011         __bodyAreaBounds = bounds;
1012         return;
1013 }
1014
1015 FloatRectangle
1016 _DateTimeBarPresenter::GetBodyBounds(void) const
1017 {
1018         return __bodyAreaBounds;
1019 }
1020
1021 void
1022 _DateTimeBarPresenter::SetArrowBounds(const FloatRectangle& bounds)
1023 {
1024         __arrowAreaBounds = bounds;
1025         return;
1026 }
1027
1028 FloatRectangle
1029 _DateTimeBarPresenter::GetArrowBounds(void) const
1030 {
1031         return __arrowAreaBounds;
1032 }
1033
1034 void
1035 _DateTimeBarPresenter::SetWindowBounds(const FloatRectangle& bounds)
1036 {
1037         __windowAreaBounds = bounds;
1038         return;
1039 }
1040
1041 FloatRectangle
1042 _DateTimeBarPresenter::GetWindowBounds(void) const
1043 {
1044         return __windowAreaBounds;
1045 }
1046
1047 bool
1048 _DateTimeBarPresenter::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
1049 {
1050         _KeyCode keyCode = keyInfo.GetKeyCode();
1051
1052         if (!__isFocused && keyCode == _KEY_ENTER)
1053         {
1054                 return true;
1055         }
1056
1057         if (!__isFocused)
1058         {
1059                 ResetFlickAnimationTimer();
1060                 RemoveAllItems();
1061                 __pDateTimeBar->SetInitialValue(GetMinimumValue(), GetMaximumValue(), __focusedValue, __pDateTimeBar->GetSelectedBoxId());
1062                 __distance = (GetItemWidth() * (-1.0f));
1063                 LoadItems();
1064                 AdjustItemPosition(__distance);
1065                 ValidateAndAdjustStartPosition();
1066                 SetFocusedItemBounds();
1067                 __isFocused = true;
1068         }
1069
1070         switch (keyCode)
1071         {
1072                 case KEY_UP:
1073                         //fall through
1074                 case KEY_DOWN:
1075                         //fall through:
1076                 case KEY_TAB:
1077                 {
1078                         __pDateTimeBar->CloseDateTimeBar();
1079                         ResetFlickAnimationTimer();
1080
1081                         if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_YEAR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_DAY)
1082                         {
1083                                 __pDateTimeBar->FireDateTimeChangeEvent(DATE_INTERNAL_CHANGE_CANCELED);
1084                         }
1085                         else if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_HOUR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_MINUTE)
1086                         {
1087                                 __pDateTimeBar->FireDateTimeChangeEvent(TIME_INTERNAL_CHANGE_CANCELED);
1088                         }
1089
1090                         break;
1091                 }
1092                 case KEY_RIGHT:
1093                 {
1094                         if ((__focusBounds.x + __focusBounds.width) > (GetWindowBounds().width - __focusBounds.width))
1095                         {
1096                                 MoveNext();
1097                         }
1098                         else
1099                         {
1100                                 __focusBounds.x = __focusBounds.x + __focusBounds.width + GetItemMargin();
1101                         }
1102                         __focusedIndex = GetItemIndexFromPosition(FloatPoint(__focusBounds.x, __focusBounds.y));
1103                         __pDateTimeBar->Invalidate();
1104
1105                         break;
1106                 }
1107
1108                 case KEY_LEFT:
1109                 {
1110                         if ((__focusBounds.x - __focusBounds.width) < 0)
1111                         {
1112                                 MovePrevious();
1113                         }
1114                         else
1115                         {
1116                                 __focusBounds.x = __focusBounds.x - __focusBounds.width - GetItemMargin();
1117                         }
1118                         __focusedIndex = GetItemIndexFromPosition(FloatPoint(__focusBounds.x, __focusBounds.y));
1119                         __pDateTimeBar->Invalidate();
1120                         break;
1121                 }
1122
1123                 case KEY_ENTER:
1124                 {
1125                         _DateTimeBarItem* pItem = null;
1126
1127                         SetItemSelected(__focusedIndex);
1128                         __pDateTimeBarModel->SetFirstDrawnItemIndex((__focusedIndex));
1129
1130                         if (__focusedIndex >= 0)
1131                         {
1132                                 pItem = GetItemAt(__focusedIndex);
1133                         }
1134
1135                         if (pItem != null)
1136                         {
1137                                 __pDateTimeBar->SetVisibleState(false);
1138                                 __pDateTimeBar->CloseDateTimeBar();
1139
1140                                 __pDateTimeBar->FireActionEvent(pItem->GetActionId());
1141
1142                                 if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_YEAR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_DAY)
1143                                 {
1144                                         __pDateTimeBar->FireDateTimeChangeEvent(DATE_INTERNAL_CHANGE_SAVED);
1145                                 }
1146                                 else if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_HOUR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_MINUTE)
1147                                 {
1148                                         __pDateTimeBar->FireDateTimeChangeEvent(TIME_INTERNAL_CHANGE_SAVED);
1149                                 }
1150                         }
1151                         __isFocused = false;
1152                         break;
1153                 }
1154
1155                 default:
1156                 {
1157                         return false;
1158                 }
1159         }
1160
1161         return true;
1162 }
1163
1164 bool
1165 _DateTimeBarPresenter::OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
1166 {
1167         _KeyCode keyCode = keyInfo.GetKeyCode();
1168
1169         if (keyCode == _KEY_ESC || keyCode == _KEY_BACK)
1170         {
1171                 __pDateTimeBar->CloseDateTimeBar();
1172                 ResetFlickAnimationTimer();
1173
1174                 if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_YEAR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_DAY)
1175                 {
1176                         __pDateTimeBar->FireDateTimeChangeEvent(DATE_INTERNAL_CHANGE_CANCELED);
1177                 }
1178                 else if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_HOUR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_MINUTE)
1179                 {
1180                         __pDateTimeBar->FireDateTimeChangeEvent(TIME_INTERNAL_CHANGE_CANCELED);
1181                 }
1182
1183                 return true;
1184         }
1185
1186         return false;
1187 }
1188
1189 bool
1190 _DateTimeBarPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
1191 {
1192         if (__isFocused)
1193         {
1194                 FocusModeChanged();
1195         }
1196
1197         if (&source != __pDateTimeBar)
1198         {
1199                 return false;
1200         }
1201
1202         __isFlickEnabled = true;
1203
1204         if (GetBodyBounds().Contains(touchinfo.GetCurrentPosition()) == false)
1205         {
1206                 __isFlickEnabled = false;
1207                 return true;
1208         }
1209
1210         if (__isFlickInProgress)
1211         {
1212                 ResetFlickAnimationTimer();
1213                 __isFlickCanceled = true;
1214         }
1215
1216         __touchMoveHandled = false;
1217
1218         __currentPoint = touchinfo.GetCurrentPosition();
1219
1220         int index = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
1221
1222         if (index == -1)
1223         {
1224                 return true;
1225         }
1226
1227         __pDateTimeBar->Invalidate();
1228
1229         return true;
1230 }
1231
1232 bool
1233 _DateTimeBarPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
1234 {
1235         if (&source != __pDateTimeBar)
1236         {
1237                 return false;
1238         }
1239
1240         if (GetBodyBounds().Contains(touchinfo.GetCurrentPosition()) == false && !__isFlickInProgress && !__isTouchMoved)
1241         {
1242                 __isTouchMoved = false;
1243                 PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_TAP, __pDateTimeBar);
1244                 __pDateTimeBar->CloseDateTimeBar();
1245                 ResetFlickAnimationTimer();
1246
1247                 if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_YEAR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_DAY)
1248                 {
1249                         __pDateTimeBar->FireDateTimeChangeEvent(DATE_INTERNAL_CHANGE_CANCELED);
1250                 }
1251                 else if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_HOUR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_MINUTE)
1252                 {
1253                         __pDateTimeBar->FireDateTimeChangeEvent(TIME_INTERNAL_CHANGE_CANCELED);
1254                 }
1255
1256                 return true;
1257         }
1258
1259         int index = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
1260         bool isEventFire = true;
1261
1262         if (index != -1 && !__isTouchMoved && !__isFlickInProgress && !__isFlickCanceled)
1263         {
1264                 PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_TAP, __pDateTimeBar);
1265                 SetItemStatus(index, DATETIMEBAR_ITEM_STATUS_SELECTED);
1266                 __pDateTimeBarModel->SetFirstDrawnItemIndex(index);
1267         }
1268         else
1269         {
1270                 isEventFire = false;
1271                 __isFlickCanceled = false;
1272         }
1273
1274         __isTouchMoved = false;
1275         __distance = 0.0f;
1276
1277         __pDateTimeBar->Invalidate();
1278
1279         if (isEventFire == true)
1280         {
1281                 int index = GetFirstDrawnItemIndex();
1282
1283                 _DateTimeBarItem* pItem = null;
1284
1285                 if (index >= 0)
1286                 {
1287                         pItem = GetItemAt(index);
1288                 }
1289
1290                 if (pItem != null)
1291                 {
1292                         __pDateTimeBar->CloseDateTimeBar();
1293                         __pDateTimeBar->FireActionEvent(pItem->GetActionId());
1294
1295                         if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_YEAR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_DAY)
1296                         {
1297                                 __pDateTimeBar->FireDateTimeChangeEvent(DATE_INTERNAL_CHANGE_SAVED);
1298                         }
1299                         else if (__pDateTimeBar->GetSelectedBoxId() >= DATETIME_ID_HOUR && __pDateTimeBar->GetSelectedBoxId() <= DATETIME_ID_MINUTE)
1300                         {
1301                                 __pDateTimeBar->FireDateTimeChangeEvent(TIME_INTERNAL_CHANGE_SAVED);
1302                         }
1303                 }
1304         }
1305
1306         return true;
1307 }
1308
1309 bool
1310 _DateTimeBarPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
1311 {
1312         if (&source != __pDateTimeBar)
1313         {
1314                 return false;
1315         }
1316
1317         if (GetBodyBounds().Contains(touchinfo.GetCurrentPosition()) == false || __touchMoveHandled == true)
1318         {
1319                 return true;
1320         }
1321
1322         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
1323
1324         __isTouchMoved = true;
1325
1326         FloatDimension screen(0.0f, 0.0f);
1327         GET_DIMENSION_CONFIG(DATETIMEBAR::DEFAULT_SIZE, orientation, screen);
1328
1329         const float _BOUNDARY_X_POSITION = screen.width * 2.0f;
1330         float distance = touchinfo.GetCurrentPosition().x - __currentPoint.x;
1331
1332         _DateTimeBarItem* pItem = null;
1333
1334         FloatRectangle itemBounds;
1335
1336         bool needItem = false;
1337         int updateItemCount = 1;
1338
1339         if (distance > 0.0f)
1340         {
1341                 pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1342
1343                 if (pItem == null)
1344                 {
1345                         return true;
1346                 }
1347
1348                 itemBounds = pItem->GetBounds();
1349
1350                 __distance += distance;
1351
1352                 if (__distance > itemBounds.width / 2.0f)
1353                 {
1354                         needItem = true;
1355                         if (__distance > itemBounds.width)
1356                         {
1357                                 updateItemCount = UPDATE_ITEM_COUNT;
1358                         }
1359                 }
1360                 if (needItem == true)
1361                 {
1362                         while (updateItemCount > 0)
1363                         {
1364                                 pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1365                                 if (pItem == null)
1366                                 {
1367                                         break;
1368                                 }
1369
1370                                 int actionId = pItem->GetActionId() - 1;
1371
1372                                 if (__pDateTimeBarModel->GetMinimumValue() > actionId)
1373                                 {
1374                                         actionId = __pDateTimeBarModel->GetMaximumValue();
1375                                 }
1376
1377                                 if (pItem->GetBounds().x > _BOUNDARY_X_POSITION * -1.0f)
1378                                 {
1379                                         if (InsertItem(0, actionId) == E_SUCCESS)
1380                                         {
1381                                                 SetFirstDrawnItemIndex(0);
1382                                         }
1383                                 }
1384
1385                                 if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1386                                 {
1387                                         DeleteItem(GetItemCount() - 1);
1388                                 }
1389
1390                                 updateItemCount--;
1391                         }
1392                         __distance = 0.0f;
1393                 }
1394         }
1395         else
1396         {
1397                 pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1398                 if (pItem == null)
1399                 {
1400                         return true;
1401                 }
1402
1403                 itemBounds = pItem->GetBounds();
1404
1405                 __distance += distance;
1406
1407                 if (__distance * -1.0f > itemBounds.width / 2.0f)
1408                 {
1409                         needItem = true;
1410                         if (__distance * -1.0f > itemBounds.width)
1411                         {
1412                                 updateItemCount = UPDATE_ITEM_COUNT;
1413                         }
1414                         __distance = 0.0f;
1415                 }
1416
1417                 if (needItem == true)
1418                 {
1419                         while (updateItemCount > 0)
1420                         {
1421                                 pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1422                                 if (pItem == null)
1423                                 {
1424                                         break;
1425                                 }
1426
1427                                 int actionId = pItem->GetActionId() + 1;
1428
1429                                 if (__pDateTimeBarModel->GetMaximumValue() < actionId)
1430                                 {
1431                                         actionId = __pDateTimeBarModel->GetMinimumValue();
1432                                 }
1433
1434                                 if (pItem->GetBounds().x < _BOUNDARY_X_POSITION)
1435                                 {
1436                                         AddItem(actionId);
1437                                 }
1438
1439                                 if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1440                                 {
1441                                         DeleteItem(0);
1442                                         SetFirstDrawnItemIndex(0);
1443                                 }
1444                                 updateItemCount--;
1445                         }
1446                 }
1447         }
1448
1449         __currentPoint = touchinfo.GetCurrentPosition();
1450
1451         AdjustItemPosition(distance);
1452
1453         __pDateTimeBar->Invalidate();
1454
1455         return true;
1456 }
1457
1458 bool
1459 _DateTimeBarPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
1460 {
1461         if (&source != __pDateTimeBar)
1462         {
1463                 return false;
1464         }
1465
1466         __isTouchMoved = false;
1467         __distance = 0.0f;
1468
1469         __pDateTimeBar->Invalidate();
1470
1471         return true;
1472 }
1473
1474 result
1475 _DateTimeBarPresenter::LoadItems()
1476 {
1477         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
1478
1479         FloatDimension screen(0.0f, 0.0f);
1480         GET_DIMENSION_CONFIG(DATETIMEBAR::DEFAULT_SIZE, orientation, screen);
1481
1482         const float _BOUNDARY_X_POSITION = screen.width * 2.0f;
1483
1484         _DateTimeBarItem* pItem = null;
1485
1486         int updateItemCount = UPDATE_ITEM_COUNT;
1487
1488         if (__distance > 0.0f)
1489         {
1490                 pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1491
1492                 if (pItem == null)
1493                 {
1494                         return true;
1495                 }
1496
1497
1498                 while (updateItemCount > 0)
1499                 {
1500                         pItem = __pDateTimeBarModel->GetItemAt(GetFirstDrawnItemIndex());
1501                         if (pItem == null)
1502                         {
1503                                 break;
1504                         }
1505
1506                         int actionId = pItem->GetActionId() - 1;
1507
1508                         if (__pDateTimeBarModel->GetMinimumValue() > actionId)
1509                         {
1510                                 actionId = __pDateTimeBarModel->GetMaximumValue();
1511                         }
1512
1513                         if (pItem->GetBounds().x > _BOUNDARY_X_POSITION * -1)
1514                         {
1515                                 if (InsertItem(0, actionId) == E_SUCCESS)
1516                                 {
1517                                         SetFirstDrawnItemIndex(0);
1518                                 }
1519                         }
1520
1521                         if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1522                         {
1523                                 DeleteItem(GetItemCount() - 1);
1524                         }
1525                         updateItemCount--;
1526                 }
1527
1528         }
1529         else
1530         {
1531                 pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1532                 if (pItem == null)
1533                 {
1534                         return true;
1535                 }
1536
1537
1538                 while (updateItemCount > 0)
1539                 {
1540                         pItem = __pDateTimeBarModel->GetItemAt(GetItemCount() - 1);
1541                         if (pItem == null)
1542                         {
1543                                 break;
1544                         }
1545
1546                         int actionId = pItem->GetActionId() + 1;
1547
1548                         if (__pDateTimeBarModel->GetMaximumValue() < actionId)
1549                         {
1550                                 actionId = __pDateTimeBarModel->GetMinimumValue();
1551                         }
1552
1553                         if (pItem->GetBounds().x < _BOUNDARY_X_POSITION)
1554                         {
1555                                 AddItem(actionId);
1556                         }
1557
1558                         if (GetItemCount() >= __pDateTimeBarModel->GetMaxCachingSize())
1559                         {
1560                                 DeleteItem(0);
1561                                 SetFirstDrawnItemIndex(0);
1562                         }
1563                         updateItemCount--;
1564                 }
1565         }
1566
1567         return true;
1568 }
1569
1570 bool
1571 _DateTimeBarPresenter::OnFlickGestureDetected(int distanceX,int distanceY,int duration)
1572 {
1573         if (!__isFlickEnabled)
1574         {
1575                 return false;
1576         }
1577
1578         __isFlickInProgress = true;
1579         __isInitialAnimation = false;
1580
1581         int velX = 0;
1582         int velY = 0;
1583
1584         __flickAnimation.CalculateInitializeVelocity(distanceX, 0, duration, &velX, &velY);
1585         __flickAnimation.InitializeFlickAmount(velX * FLICK_ANIMATION_VELOCITY_FACTOR_DATE_TIME_BAR);
1586
1587         StartFlickAnimationTimer();
1588         return true;
1589 }
1590
1591 void
1592 _DateTimeBarPresenter::StartFlickAnimation(void)
1593 {
1594         int moveX = 0;
1595         int moveY = 0;
1596
1597         __flickAnimation.CalculateNextMove(&moveX, &moveY);
1598
1599         __distance = _CoordinateSystemUtils::ConvertToFloat(moveX);
1600         if (moveX != 0)
1601         {
1602                 StartFlickAnimationTimer();
1603         }
1604
1605         LoadItems();
1606         AdjustItemPosition(__distance);
1607         Draw();
1608         return;
1609 }
1610
1611 void
1612 _DateTimeBarPresenter::StartAnimationEffect(void)
1613 {
1614         __isInitialAnimation = true;
1615         __distance = -10.0f;
1616
1617         if (__initialAnimationValue >= 0.0f)
1618         {
1619                 __initialAnimationValue = __initialAnimationValue + __distance;
1620                 StartFlickAnimationTimer();
1621
1622                 LoadItems();
1623                 AdjustItemPosition(__distance);
1624                 Draw();
1625         }
1626         else
1627         {
1628                 __initialAnimationValue = 0.0f;
1629                 __isInitialAnimation = false;
1630                 ResetFlickAnimationTimer();
1631                 ValidateAndAdjustStartPosition();
1632         }
1633
1634         return;
1635 }
1636
1637 result
1638 _DateTimeBarPresenter::StartFlickAnimationTimer(void)
1639 {
1640         result r = E_SUCCESS;
1641         __isFlickInProgress = true;
1642
1643         if (__pFlickAnimationTimer == null)
1644         {
1645                 __pFlickAnimationTimer = new (std::nothrow) Timer;
1646                 SysTryReturn(NID_UI_CTRL, (__pFlickAnimationTimer != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create timer.");
1647
1648                 r = __pFlickAnimationTimer->Construct(*this);
1649                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1650         }
1651         else
1652         {
1653                 __pFlickAnimationTimer->Cancel();
1654         }
1655
1656         r = __pFlickAnimationTimer->Start(FLICK_ANIMATION_TIMER_PERIOD);
1657         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1658
1659         return r;
1660
1661 CATCH:
1662         ResetFlickAnimationTimer();
1663         return r;
1664 }
1665
1666 result
1667 _DateTimeBarPresenter::ResetFlickAnimationTimer(void)
1668 {
1669         if (__pFlickAnimationTimer)
1670         {
1671                 delete __pFlickAnimationTimer;
1672                 __pFlickAnimationTimer = null;
1673         }
1674
1675         __isFlickInProgress = false;
1676         return E_SUCCESS;
1677 }
1678
1679 void
1680 _DateTimeBarPresenter::OnTimerExpired(Timer& timer)
1681 {
1682         __isFlickInProgress = false;
1683         if (&timer == __pFlickAnimationTimer)
1684         {
1685                 if (__isInitialAnimation)
1686                 {
1687                         StartAnimationEffect();
1688                 }
1689                 else
1690                 {
1691                         StartFlickAnimation();
1692                 }
1693         }
1694         return;
1695 }
1696
1697 void
1698 _DateTimeBarPresenter::ValidateAndAdjustStartPosition(void)
1699 {
1700         int index = -1;
1701         float adjustPosition = 0.0f;
1702         float leftMargin =  GetLeftRightMargin();
1703         FloatRectangle bodyBounds = GetBodyBounds();
1704
1705         index = GetItemIndexFromPosition(FloatPoint(GetLeftRightMargin() + GetItemMargin(), bodyBounds.y + (bodyBounds.height / 2.0f)));
1706
1707         if (index == -1)
1708         {
1709                 return;
1710         }
1711
1712         _DateTimeBarItem* pDrawItem = null;
1713         pDrawItem = __pDateTimeBarModel->GetItemAt(index);
1714
1715         if (pDrawItem == null)
1716         {
1717                 return;
1718         }
1719
1720         if (pDrawItem->GetBounds().x > 0.0f && pDrawItem->GetBounds().x < leftMargin)
1721         {
1722                 adjustPosition = leftMargin - pDrawItem->GetBounds().x;
1723         }
1724         else if (pDrawItem->GetBounds().x < 0.0f)
1725         {
1726                 pDrawItem = __pDateTimeBarModel->GetItemAt(index + 1);
1727                 if (pDrawItem == null)
1728                 {
1729                         return;
1730                 }
1731                 adjustPosition = leftMargin - pDrawItem->GetBounds().x;
1732         }
1733         else if (pDrawItem->GetBounds().x > leftMargin)
1734         {
1735                 adjustPosition = leftMargin - pDrawItem->GetBounds().x;
1736         }
1737
1738         AdjustItemPosition(adjustPosition);
1739         Draw();
1740         return;
1741 }
1742
1743 void
1744 _DateTimeBarPresenter::SetInitialAnimationValue(float animationValue)
1745 {
1746         __initialAnimationValue = animationValue;
1747         return;
1748 }
1749
1750 void
1751 _DateTimeBarPresenter::SetFont(Font& pFont)
1752 {
1753         delete __pFont;
1754         __pFont = null;
1755
1756         __pFont = _FontImpl::CloneN(pFont);
1757         SysTryReturnVoidResult(NID_UI_CTRL, (__pFont != null), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1758
1759         return;
1760 }
1761
1762 bool
1763 _DateTimeBarPresenter::OnAccessibilityFocusMovedNext(const _AccessibilityContainer& control, const _AccessibilityElement& element)
1764 {
1765         return false;
1766 }
1767
1768 bool
1769 _DateTimeBarPresenter::OnAccessibilityFocusMovedPrevious(const _AccessibilityContainer& control, const _AccessibilityElement& element)
1770 {
1771         return false;
1772 }
1773
1774 void
1775 _DateTimeBarPresenter::OnTouchMoveHandled(const Tizen::Ui::_Control& control)
1776 {
1777         __touchMoveHandled = true;
1778
1779         __isTouchMoved = false;
1780         __distance = 0.0f;
1781
1782         __pDateTimeBar->Invalidate();
1783
1784         return;
1785 }
1786
1787 void
1788 _DateTimeBarPresenter::AddAccessibilityElement(void)
1789 {
1790         _DateTimeBarItem* pItem = null;
1791         pItem = __pDateTimeBarModel->GetItemAt(__pDateTimeBarModel->GetItemCount() - 1);
1792
1793         if (pItem == null)
1794         {
1795                 return;
1796         }
1797
1798         if (__pDateTimeBar->GetSelectedBoxId() == DATETIME_ID_MONTH)
1799         {
1800                 __pDateTimeBar->AddAccessibilityElement(pItem->GetBounds(), __pDateTimeBar->GetMonthValue(pItem->GetActionId()));
1801         }
1802         else
1803         {
1804                 __pDateTimeBar->AddAccessibilityElement(pItem->GetBounds(), pItem->GetText());
1805         }
1806
1807         return;
1808 }
1809
1810 void
1811 _DateTimeBarPresenter::InsertAccessibilityElementAt(int index)
1812 {
1813         _DateTimeBarItem* pItem = null;
1814         pItem = __pDateTimeBarModel->GetItemAt(index);
1815
1816         if (pItem == null)
1817         {
1818                 return;
1819         }
1820
1821         if (__pDateTimeBar->GetSelectedBoxId() == DATETIME_ID_MONTH)
1822         {
1823                 __pDateTimeBar->InsertAccessibilityElementAt(index, pItem->GetBounds(), __pDateTimeBar->GetMonthValue(pItem->GetActionId()));
1824         }
1825         else
1826         {
1827                 __pDateTimeBar->InsertAccessibilityElementAt(index, pItem->GetBounds(), pItem->GetText());
1828         }
1829
1830         return;
1831 }
1832
1833 void
1834 _DateTimeBarPresenter::MoveNext(void)
1835 {
1836         __distance = (GetItemWidth() + GetItemMargin()) * (-1.0f);
1837         LoadItems();
1838         AdjustItemPosition(__distance);
1839         Draw();
1840         return;
1841 }
1842
1843 void
1844 _DateTimeBarPresenter::MovePrevious(void)
1845 {
1846         __distance = (GetItemWidth() + GetItemMargin());
1847         LoadItems();
1848         AdjustItemPosition(__distance);
1849         Draw();
1850         return;
1851 }
1852
1853 }}} // Tizen::Ui::Controls