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