Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_TabPresenter.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FUiCtrl_TabPresenter.cpp
19  * @brief               This is the implementation file for the _TabPresenter class.
20  */
21
22 #include <FBaseErrorDefine.h>
23 #include <FBaseRtITimerEventListener.h>
24 #include <FBaseRtTimer.h>
25 #include <FGrpFont.h>
26 #include <FBaseSys.h>
27 #include <FBaseSysLog.h>
28 #include <FUiAnimVisualElementPropertyAnimation.h>
29 #include <FGrp_FontImpl.h>
30 #include <FGrp_TextTextObject.h>
31 #include <FGrp_BitmapImpl.h>
32 #include <FGrp_TextTextSimple.h>
33 #include "FUi_AccessibilityElement.h"
34 #include "FUi_IAccessibilityListener.h"
35 #include "FUi_AccessibilityContainer.h"
36 #include "FUiAnim_VisualElement.h"
37 #include "FUi_UiTouchEvent.h"
38 #include "FUi_ResourceManager.h"
39 #include "FUiCtrl_TabPresenter.h"
40
41 using namespace Tizen::Base;
42 using namespace Tizen::Graphics;
43 using namespace Tizen::Graphics::_Text;
44 using namespace Tizen::Base::Runtime;
45 using namespace Tizen::Ui::Animations;
46
47 namespace Tizen { namespace Ui { namespace Controls {
48
49 struct ReorderAnimationInfo{
50
51         int xPosition;
52         int index;
53         int totalDistance;
54         float move;
55         int reorderValue;
56 };
57
58 struct TimerInfo{
59
60         int index;
61         Timer* pReorderTimer;
62 };
63
64 _TabPresenter::_TabPresenter(void)
65         : __pTab(null)
66         , __pTabModel(null)
67         , __pFont(null)
68         , __pTextObject(null)
69         , __recalc(false)
70         , __editMode(false)
71         , __editItemIndex(-1)
72         , __pVerticalLineBitmap(null)
73         , __pHorizontalLineBitmap(null)
74         , __pFrontScrollingEffectBitmap(null)
75         , __pRearScrollingEffectBitmap(null)
76         , __pScrollingEffectBitmap(null)
77         , __pScrollingEffectVe(null)
78         , __pFlickAnimationTimer(null)
79         , __pScrollAnimationTimer(null)
80         , __flickAnimation()
81         , __showItemCount(0)
82         , __width(0)
83         , __height(0)
84         , __moveDistance(0)
85         , __isFlickEnabled(false)
86         , __itemMaxCount(0)
87         , __itemWidth(0)
88         , __itemHeight(0)
89         , __rearIndex(0)
90         , __isRightScrolable(false)
91         , __isLeftScrolable(false)
92         , __isDirectionRight(true)
93         , __isTimerCanceled(false)
94         , __flickMove(0.0)
95         , __flickDistance(0)
96         , __flickFlag(0)
97         , __reorderDist(0)
98         , __firstItemIndex(0)
99         , __lastItemIndex(0)
100         , __selectedLineLeftMargin(0)
101         , __selectedLineRightMargin(0)
102         , __isScrollEffectEnded(false)
103         , __isScrollEffectStarted(false)
104         , __timerCount(0)
105         , __isTouchMove(false)
106         , __scrollFlag(0)
107         , __tabMinimumSize(0)
108
109 {
110         __currentTouchPosition = Point(0, 0);
111         __dividerLine = Rectangle(0, 0, 0, 0);
112         __selectedLine = Rectangle(0, 0, 0, 0);
113
114         for (int i = 0; i < (_TIMER_COUNT); i++)
115         {
116                 __pTimerInfo[i] = null;
117         }
118
119         for (int i = 0; i < _TAB_ITEM_MAXCOUNT; i++)
120         {
121                 __pAnimInfo[i] = null;
122         }
123 }
124
125
126 _TabPresenter::~_TabPresenter(void)
127 {
128         if (__pTabModel)
129         {
130                 delete __pTabModel;
131                 __pTabModel = null;
132         }
133
134         if (__pTextObject)
135         {
136                 delete __pTextObject;
137                 __pTextObject = null;
138         }
139
140         if (__pVerticalLineBitmap)
141         {
142                 delete __pVerticalLineBitmap;
143                 __pVerticalLineBitmap = null;
144         }
145
146         if (__pHorizontalLineBitmap)
147         {
148                 delete __pHorizontalLineBitmap;
149                 __pHorizontalLineBitmap = null;
150         }
151
152         if (__pFlickAnimationTimer)
153         {
154                 delete __pFlickAnimationTimer;
155                 __pFlickAnimationTimer = null;
156         }
157
158         for (int i = 0; i < (_TIMER_COUNT); i++)
159         {
160                 if (__pTimerInfo[i])
161                 {
162                         if (__pTimerInfo[i]->pReorderTimer)
163                         {
164                                 delete __pTimerInfo[i]->pReorderTimer;
165                                 __pTimerInfo[i]->pReorderTimer = null;
166                         }
167                         delete __pTimerInfo[i];
168                         __pTimerInfo[i] = null;
169                 }
170         }
171
172         for (int i = 0; i < _TAB_ITEM_MAXCOUNT; i++)
173         {
174                 if (__pAnimInfo[i])
175                 {
176                         delete __pAnimInfo[i];
177                         __pAnimInfo[i] = null;
178                 }
179         }
180
181         if (__pScrollAnimationTimer)
182         {
183                 delete __pScrollAnimationTimer;
184                 __pScrollAnimationTimer = null;
185         }
186
187         if (__pFrontScrollingEffectBitmap)
188         {
189                 delete __pFrontScrollingEffectBitmap;
190                 __pFrontScrollingEffectBitmap = null;
191         }
192
193         if (__pRearScrollingEffectBitmap)
194         {
195                 delete __pRearScrollingEffectBitmap;
196                 __pRearScrollingEffectBitmap = null;
197         }
198
199         if (__pScrollingEffectBitmap)
200         {
201                 __pScrollingEffectBitmap = null;
202         }
203
204         if (__pScrollingEffectVe)
205         {
206                 __pScrollingEffectVe->RemoveAllAnimations();
207                 __pScrollingEffectVe->Destroy();
208                 __pScrollingEffectVe = null;
209         }
210 }
211
212 result
213 _TabPresenter::Construct(const _Tab& tab)
214 {
215         result r = E_SUCCESS;
216
217         __pTab = const_cast<_Tab*>(&tab);
218
219         __pFont = __pTab->GetFallbackFont();
220         SysTryReturn(NID_UI_CTRL, __pFont, E_SYSTEM , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to retrive Font.");
221
222         _TabModel* pModel = new (std::nothrow) _TabModel;
223         SysTryReturn(NID_UI_CTRL, pModel, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
224
225         r = SetModel(*pModel);
226         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to set _TabModel instance.");
227
228         r = pModel->Construct();
229         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to construct _TabModel instance.");
230
231         __pTextObject = new (std::nothrow) TextObject;
232         SysTryCatch(NID_UI_CTRL, __pTextObject, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
233
234         __pTextObject->Construct();
235         __pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
236         __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
237
238         r = LoadBitmap();
239         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "Failed to locate bitmap.");
240
241         GET_FIXED_VALUE_CONFIG(TAB::ITEM_MAX_COUNT, __pTab->GetOrientation(), __itemMaxCount);
242         GET_SHAPE_CONFIG(TAB::SCREEN_WIDTH, __pTab->GetOrientation(), __width);
243         GET_SHAPE_CONFIG(TAB::SCREEN_HEIGHT, __pTab->GetOrientation(), __height);
244
245         GET_SHAPE_CONFIG(TAB::DIVIDER_WIDTH, __pTab->GetOrientation(), __dividerLine.width);
246         GET_SHAPE_CONFIG(TAB::DIVIDER_HEIGHT, __pTab->GetOrientation(), __dividerLine.height);
247
248         GET_SHAPE_CONFIG(TAB::TAB_MINIMUM_SIZE, __pTab->GetOrientation(), __tabMinimumSize);
249
250         GET_SHAPE_CONFIG(TAB::SELECTED_LINE_LEFT_MARGIN, __pTab->GetOrientation(), __selectedLineLeftMargin);
251         GET_SHAPE_CONFIG(TAB::SELECTED_LINE_RIGHT_MARGIN, __pTab->GetOrientation(), __selectedLineRightMargin);
252         GET_SHAPE_CONFIG(TAB::SELECTED_LINE_HEIGHT, __pTab->GetOrientation(), __selectedLine.height);
253
254         __flickAnimation.SetSizeInformation(__width, __height, DEVICE_SIZE_HORIZONTAL, DEVICE_SIZE_VERTICAL);
255         __flickAnimation.SetSensitivity(FLICK_ANIMATION_FPS_TAB, FLICK_ANIMATION_SENSITIVITY_TAB);
256         __flickAnimation.SetDirection(FD_HORIZONTAL);
257
258         for (int i = 0; i < _TIMER_COUNT; i++)
259         {
260                 __pTimerInfo[i] = new (std::nothrow) TimerInfo();
261                 SysTryCatch(NID_UI_CTRL, __pTimerInfo[i], , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
262
263                 __pTimerInfo[i]->index = -1;
264                 __pTimerInfo[i]->pReorderTimer = null;
265         }
266
267         return r;
268
269 CATCH:
270         delete pModel;
271         pModel = null;
272
273         delete __pTextObject;
274         __pTextObject = null;
275
276         return r;
277 }
278
279 result
280 _TabPresenter::LoadBitmap(void)
281 {
282         result r = E_SUCCESS;
283         Tizen::Graphics::Bitmap* pBitmap = null;
284         r = GET_BITMAP_CONFIG_N(TAB::VERTICAL_DIVIDER_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pVerticalLineBitmap);
285         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "Failed to locate divider line bitmap.");
286
287         r = GET_BITMAP_CONFIG_N(TAB::ITEM_SELECTED, BITMAP_PIXEL_FORMAT_ARGB8888, pBitmap);
288         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "Failed to locate horizontal line bitmap.");
289
290         if (pBitmap)
291         {
292                 __pHorizontalLineBitmap = _BitmapImpl::GetColorReplacedBitmapN(*(pBitmap), Color::GetColor(COLOR_ID_MAGENTA), __pTab->GetPressedTextColor());
293                 SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "Failed to locate focus item line bitmap.");
294         }
295         r = GET_BITMAP_CONFIG_N(TAB::TAB_BOUNCE_EFFECT_LEFT, BITMAP_PIXEL_FORMAT_ARGB8888, __pFrontScrollingEffectBitmap);
296         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "Failed to locate overscrolling left bitmap.");
297
298         r = GET_BITMAP_CONFIG_N(TAB::TAB_BOUNCE_EFFECT_RIGHT, BITMAP_PIXEL_FORMAT_ARGB8888, __pRearScrollingEffectBitmap);
299         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "Failed to locate overscrolling right bitmap.");
300
301         delete pBitmap;
302         return r;
303 }
304
305 void
306 _TabPresenter::DrawResourceBitmap(Canvas& canvas, const Rectangle& bounds, Bitmap* pBitmap)
307 {
308         result r = E_SUCCESS;
309
310         if (pBitmap == null)
311         {
312                 return;
313         }
314
315         if (pBitmap->IsNinePatchedBitmap())
316         {
317                 r = canvas.DrawNinePatchedBitmap(bounds, *pBitmap);
318                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
319         }
320         else
321         {
322                 r = canvas.DrawBitmap(bounds, *pBitmap);
323                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
324         }
325
326         return;
327 }
328
329 result
330 _TabPresenter::SetModel(const _TabModel& tabModel)
331 {
332         __pTabModel = const_cast<_TabModel*>(&tabModel);
333
334         return E_SUCCESS;
335 }
336
337 void
338 _TabPresenter::SetSelectedItemIndex(int index)
339 {
340         int i = GetSelectedItemIndex();
341         SetItemStatus(i, _TABITEM_STATUS_NORMAL);
342         __pTabModel->SetSelectedItemIndex(index);
343         SetItemStatus(index, _TABITEM_STATUS_SELECTED);
344         return;
345 }
346
347 int
348 _TabPresenter::GetSelectedItemIndex(void) const
349 {
350         return __pTabModel->GetSelectedItemIndex();
351 }
352
353 void
354 _TabPresenter::SetPressedItemIndex(int index)
355 {
356         __pTabModel->SetPressedItemIndex(index);
357         return;
358 }
359
360 int
361 _TabPresenter::GetPressedItemIndex(void) const
362 {
363         return __pTabModel->GetPressedItemIndex();
364 }
365
366 void
367 _TabPresenter::SetFirstDrawnItemIndex(int index)
368 {
369         __pTabModel->SetFirstDrawnItemIndex(index);
370         return;
371 }
372
373 int
374 _TabPresenter::GetFirstDrawnItemIndex(void) const
375 {
376
377         return __pTabModel->GetFirstDrawnItemIndex();
378 }
379
380 void
381 _TabPresenter::SetEditModeEnabled(bool enable)
382 {
383
384         return __pTabModel->SetEditModeEnabled(enable);
385 }
386
387 bool
388 _TabPresenter::IsEditModeEnabled(void) const
389 {
390         return __pTabModel->IsEditModeEnabled();
391 }
392
393 int
394 _TabPresenter::GetItemCount(void) const
395 {
396
397         return __pTab->GetItemCount();
398 }
399
400 _TabItem*
401 _TabPresenter::GetItemAt(int index)
402 {
403         return __pTab->GetItemAt(index);
404 }
405
406 result
407 _TabPresenter::SetItemStatus(int index, _TabItemStatus status)
408 {
409         _TabItem *pItem = GetItemAt(index);
410         SysTryReturn(NID_UI_CTRL, pItem, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
411
412         pItem->SetUpdateState(true);
413         pItem->SetStatus(status);
414
415         return E_SUCCESS;
416 }
417
418 _TabItemStatus
419 _TabPresenter::GetItemStatus(int index) const
420 {
421
422         _TabItem *pItem = __pTab->GetItemAt(index);
423
424         if (pItem == null)
425         {
426                 return _TABITEM_STATUS_NORMAL;
427         }
428
429         return pItem->GetStatus();
430 }
431
432 int
433 _TabPresenter::GetItemIndexFromPosition(const Point& point) const
434 {
435
436         Rectangle itemBounds(0, 0, 0, 0);
437
438         int itemIndex = GetFirstDrawnItemIndex();
439
440         if ((point.x < 0) || (point.x > __pTab->GetBounds().width)
441                 || (point.y < 0 ) || (point.y > __pTab->GetBounds().height))
442         {
443                 return -1;
444         }
445
446         _TabItem* pItem = __pTab->GetItemAt(itemIndex);
447         if (pItem == null)
448         {
449                 return -1;
450         }
451
452         while (pItem != null)
453         {
454                 itemBounds = pItem->GetItemBounds();
455
456                 if ((point.x > itemBounds.x) && (point.x < itemBounds.x + itemBounds.width)
457                         && (point.y > itemBounds.y) && (point.y < itemBounds.y + itemBounds.height))
458                 {
459                         return itemIndex;
460                 }
461
462                 itemIndex++;
463                 pItem = __pTab->GetItemAt(itemIndex);
464                 if (pItem == null)
465                 {
466                         break;
467                 }
468         }
469
470         return -1;
471 }
472
473 void
474 _TabPresenter::Draw(void)
475 {
476         ClearLastResult();
477
478         SysTryReturnVoidResult(NID_UI_CTRL, __pTab, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Tab instance is null.");
479         GET_COLOR_CONFIG(TAB::ITEM_BG_NORMAL, backgroundColor);
480         if (__recalc)
481         {
482                 __recalc = false;
483                 CalcItemBounds();
484         }
485         else if (__isFlickEnabled )
486         {
487                 DrawHorizontal();
488         }
489         Canvas* pCanvas = __pTab->GetCanvasN();
490         SysTryReturnVoidResult(NID_UI_CTRL, pCanvas, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
491
492         pCanvas->SetBackgroundColor(backgroundColor);
493         pCanvas->Clear();
494
495         if (__pTab->GetStyle() == _TAB_STYLE_TEXT)
496         {
497                 DrawTextItem(pCanvas);
498         }
499         else if (__pTab->GetStyle() == _TAB_STYLE_ICON)
500         {
501                 DrawIconItem(pCanvas);
502         }
503         else    // _TAB_STYLE_ICON_TEXT
504         {
505                 DrawItem(pCanvas);
506         }
507
508         RefreshAccessibilityElement();
509
510         delete pCanvas;
511         return;
512 }
513
514 void
515 _TabPresenter::SetReCalculateItemBounds(bool reCalculationRequired)
516 {
517         __recalc = reCalculationRequired;
518         return;
519 }
520
521 void
522 _TabPresenter::DrawTextItem(Canvas* pCanvas)
523 {
524         ClearLastResult();
525         TextSimple* pSimpleText = null;
526         Rectangle bounds = __pTab->GetBounds();
527
528         _TabItem* pDrawItem = null;
529         String itemText(L"");
530         Rectangle itemDrawRect(0, 0, 0, 0);
531
532         const int itemCount = GetItemCount();
533
534         _TabItemStatus itemStatus = _TABITEM_STATUS_NORMAL;
535
536         bool multiline = false;
537         int fontChangeItemCount = 0;
538         GET_FIXED_VALUE_CONFIG(TAB::FONT_CHANGE_ITEM_COUNT, __pTab->GetOrientation(), fontChangeItemCount);
539
540         int topMargin = 0;
541         int bottomMargin = 0;
542         int leftMargin = 0;
543         int rightMargin = 0;
544         GET_SHAPE_CONFIG(TAB::TOP_MARGIN, __pTab->GetOrientation(), topMargin);
545         GET_SHAPE_CONFIG(TAB::BOTTOM_MARGIN, __pTab->GetOrientation(), bottomMargin);
546         GET_SHAPE_CONFIG(TAB::ITEM_LEFT_MARGIN, __pTab->GetOrientation(), leftMargin);
547         GET_SHAPE_CONFIG(TAB::ITEM_RIGHT_MARGIN, __pTab->GetOrientation(), rightMargin);
548
549         for (int i = 0; i < itemCount; i++)
550         {
551                 pDrawItem = GetItemAt(i);
552
553                 SysTryReturnVoidResult(NID_UI_CTRL, pDrawItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
554
555                 SetFirstDrawnItemIndex(0);
556
557                 // Item Rect
558                 itemDrawRect = pDrawItem->GetItemBounds();
559                 __dividerLine.x = itemDrawRect.x + (itemDrawRect.width - (__dividerLine.width)/2);
560                 __dividerLine.y = ((itemDrawRect.height - __dividerLine.height)/2);
561
562                 // Item Status
563                 itemStatus = pDrawItem->GetStatus();
564
565                 // Status
566                 if (itemStatus == _TABITEM_STATUS_SELECTED)
567                 {
568                         DrawSelectedItemBackground(pCanvas, itemDrawRect);
569                         __selectedLine.width = __itemWidth - __selectedLineLeftMargin -  __selectedLineRightMargin;
570                         __selectedLine.x = itemDrawRect.x + __selectedLineLeftMargin;
571                 }
572                 else if (itemStatus == _TABITEM_STATUS_PRESSED)
573                 {
574                         DrawFocusItemBackground(pCanvas, itemDrawRect);
575                 }
576                 else
577                 {
578                         DrawItemBackground(pCanvas, itemDrawRect);
579                 }
580
581                 // Draw Text
582                 itemText = pDrawItem->GetText();
583
584                 __pTextObject->RemoveAll();
585                 pSimpleText = new (std::nothrow)TextSimple(const_cast <mchar*>(itemText.GetPointer()), itemText.GetLength());
586                 SysTryReturnVoidResult(NID_UI_CTRL, pSimpleText, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
587
588                 __pTextObject->AppendElement(*pSimpleText);
589                 __pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
590                 __pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
591
592                 // Margin - Item Rect.
593                 itemDrawRect.x = itemDrawRect.x + leftMargin;
594                 itemDrawRect.y = itemDrawRect.y + topMargin;
595                 itemDrawRect.width = itemDrawRect.width - leftMargin - rightMargin;
596                 itemDrawRect.height = itemDrawRect.height - topMargin - bottomMargin;
597
598                 __pTextObject->SetBounds(itemDrawRect);
599                 __pTextObject->Compose();
600
601                 const int textRowCount = __pTextObject->GetTotalLineCount();
602
603                 // Init.
604                 multiline = false;
605
606                 if (textRowCount >= 2)
607                 {
608                         multiline = true;
609                 }
610
611                 // Font Size Change
612                 if (__pFont != null )
613                 {
614                         int fontSize = 0;
615
616                         if (__showItemCount < fontChangeItemCount)
617                         {
618                                 if (multiline == false)
619                                 {
620                                         GET_SHAPE_CONFIG(TAB::FONT_SIZE_01, __pTab->GetOrientation(), fontSize);
621                                 }
622                                 else
623                                 {
624                                         GET_SHAPE_CONFIG(TAB::MULTILINE_FONT_SIZE_01, __pTab->GetOrientation(), fontSize);
625                                 }
626                         }
627                         else if (__showItemCount == fontChangeItemCount)
628                         {
629                                 if (multiline == false)
630                                 {
631                                         GET_SHAPE_CONFIG(TAB::FONT_SIZE_02, __pTab->GetOrientation(), fontSize);
632                                 }
633                                 else
634                                 {
635                                         GET_SHAPE_CONFIG(TAB::MULTILINE_FONT_SIZE_02, __pTab->GetOrientation(), fontSize);
636                                 }
637                         }
638                         else
639                         {
640                                 if (multiline == false)
641                                 {
642                                         GET_SHAPE_CONFIG(TAB::FONT_SIZE_03, __pTab->GetOrientation(), fontSize);
643                                 }
644                                 else
645                                 {
646                                         GET_SHAPE_CONFIG(TAB::MULTILINE_FONT_SIZE_03, __pTab->GetOrientation(), fontSize);
647                                 }
648                         }
649
650                         (_FontImpl::GetInstance(*__pFont))->SetSize(fontSize);
651                 }
652
653                 __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
654
655                 if (itemStatus == _TABITEM_STATUS_SELECTED)
656                 {
657                         __pTextObject->SetForegroundColor(__pTab->GetSelectedTextColor(), 0, __pTextObject->GetTextLength());
658                         __selectedLine.y = itemDrawRect.y + itemDrawRect.height;
659                         if (__pTab->GetSelectedItemBackgroundBitmap() == null)
660                         {
661                                 DrawResourceBitmap(*pCanvas, __selectedLine, __pHorizontalLineBitmap);
662                         }
663
664                 }
665                 else if (itemStatus == _TABITEM_STATUS_PRESSED)
666                 {
667                         __pTextObject->SetForegroundColor(__pTab->GetPressedTextColor(), 0, __pTextObject->GetTextLength());
668                 }
669                 else if (itemStatus == _TABITEM_STATUS_HIGHLIGHTED)
670                 {
671                         __pTextObject->SetForegroundColor(__pTab->GetHighlightedTextColor(), 0, __pTextObject->GetTextLength());
672                 }
673                 else
674                 {
675                         __pTextObject->SetForegroundColor(__pTab->GetTextColor(), 0, __pTextObject->GetTextLength());
676                 }
677
678                 __pTextObject->SetBounds(itemDrawRect);
679                 __pTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
680                 if (i < (itemCount - 1))
681                 {
682                         DrawItemDivisionVerticalLine(pCanvas, __dividerLine);
683                 }
684
685                 DrawBadgeIcon(pDrawItem, pCanvas);
686
687         }
688         return;
689 }
690
691 void
692 _TabPresenter::DrawItemBackground(Canvas* pCanvas, Rectangle& itemRect)
693 {
694         Bitmap* pBackgroundBitmap = __pTab->GetBackgroundBitmap();
695
696         DrawResourceBitmap(*pCanvas, itemRect, pBackgroundBitmap);
697
698         return;
699 }
700
701 void
702 _TabPresenter::DrawFocusItemBackground(Canvas* pCanvas, Rectangle& itemRect)
703 {
704         Bitmap* pBackgroundBitmap = __pTab->GetPressedItemBackgroundBitmap();
705
706         DrawResourceBitmap(*pCanvas, itemRect, pBackgroundBitmap);
707
708         return;
709 }
710
711 void
712 _TabPresenter::DrawSelectedItemBackground(Canvas* pCanvas, Rectangle& itemRect)
713 {
714         Bitmap* pBackgroundBitmap = __pTab->GetSelectedItemBackgroundBitmap();
715
716         DrawResourceBitmap(*pCanvas, itemRect, pBackgroundBitmap);
717
718         return;
719 }
720
721 void
722 _TabPresenter::DrawIconItem(Canvas* pCanvas)
723 {
724         ClearLastResult();
725         Rectangle bounds = __pTab->GetBounds();
726
727         _TabItem* pDrawItem = null;
728         String itemText(L"");
729         Rectangle itemDrawRect(0, 0, 0, 0);
730         const int itemCount = GetItemCount();
731
732         _TabItemStatus itemStatus = _TABITEM_STATUS_NORMAL;
733
734         int iconWidth = 0;
735         int iconHeight = 0;
736         int bottomMargin = 0;
737
738         GET_SHAPE_CONFIG(TAB::ICON_WIDTH, __pTab->GetOrientation(), iconWidth);
739         GET_SHAPE_CONFIG(TAB::ICON_HEIGHT, __pTab->GetOrientation(), iconHeight);
740         GET_SHAPE_CONFIG(TAB::BOTTOM_MARGIN, __pTab->GetOrientation(), bottomMargin);
741
742         for (int i = 0; i < itemCount; i++)
743         {
744                 pDrawItem = GetItemAt(i);
745                 SysTryReturnVoidResult(NID_UI_CTRL, pDrawItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
746
747                 SetFirstDrawnItemIndex(0);
748
749                 // Item Rect
750                 itemDrawRect = pDrawItem->GetItemBounds();
751                 __dividerLine.x = itemDrawRect.x + (itemDrawRect.width - (__dividerLine.width)/2);
752                 __dividerLine.y = ((itemDrawRect.height - __dividerLine.height)/2);
753
754                 // Item Status
755                 itemStatus = pDrawItem->GetStatus();
756
757                 // Status
758                 if (itemStatus == _TABITEM_STATUS_SELECTED)
759                 {
760                         DrawSelectedItemBackground(pCanvas, itemDrawRect);
761                         __selectedLine.width = __itemWidth - __selectedLineLeftMargin -  __selectedLineRightMargin;
762                         __selectedLine.x = itemDrawRect.x + __selectedLineLeftMargin;
763                         __selectedLine.y = itemDrawRect.height - (bottomMargin/2);
764
765                         if (__pTab->GetSelectedItemBackgroundBitmap() == null)
766                         {
767                                 DrawResourceBitmap(*pCanvas, __selectedLine, __pHorizontalLineBitmap);
768                         }
769                 }
770                 else if (itemStatus == _TABITEM_STATUS_PRESSED)
771                 {
772                         DrawFocusItemBackground(pCanvas, itemDrawRect);
773                 }
774                 else
775                 {
776                         DrawItemBackground(pCanvas, itemDrawRect);
777                 }
778                 // Disable / Normal
779
780                 // Margin - Item Rect.
781                 itemDrawRect.x = itemDrawRect.x + ((itemDrawRect.width - iconWidth) / 2);
782                 itemDrawRect.y = itemDrawRect.y + ((itemDrawRect.height - iconHeight) / 2);
783                 itemDrawRect.width = iconWidth;
784                 itemDrawRect.height = iconHeight;
785
786                 Bitmap* pIconBitmap = pDrawItem->GetIcon();
787
788                 DrawResourceBitmap(*pCanvas, itemDrawRect, pIconBitmap);
789
790                 if (i < itemCount - 1)
791                 {
792                         DrawItemDivisionVerticalLine(pCanvas, __dividerLine);
793                 }
794                 DrawBadgeIcon(pDrawItem, pCanvas);
795         }
796         return;
797
798 }
799
800 void
801 _TabPresenter::DrawItem(Canvas* pCanvas)
802 {
803         ClearLastResult();
804
805         Rectangle bounds = __pTab->GetBounds();
806
807         TextSimple* pSimpleText = null;
808         _TabItem* pDrawItem = null;
809         Rectangle buttonBounds(0, 0, 0, 0);
810         String itemText(L"");
811         Rectangle itemDrawRect(0, 0, 0, 0);
812         Rectangle itemDrawIconRect(0, 0, 0, 0);
813         Rectangle itemDrawTextRect(0, 0, 0, 0);
814         int iconTextGap = 0;
815         const int itemCount = GetItemCount();
816
817         _TabItemStatus itemStatus = _TABITEM_STATUS_NORMAL;
818
819         bool multiline = false;
820         int fontChangeItemCount = 0;
821         GET_FIXED_VALUE_CONFIG(TAB::FONT_CHANGE_ITEM_COUNT, __pTab->GetOrientation(), fontChangeItemCount);
822         GET_FIXED_VALUE_CONFIG(TAB::ICON_TEXT_GAP, __pTab->GetOrientation(), iconTextGap);
823
824         int topMargin = 0;
825         int bottomMargin = 0;
826         int leftMargin = 0;
827         int rightMargin = 0;
828         int topMarginTextOnly = 0;
829         int textLength = 0;
830         int bottomMarginTextOnly = 0;
831         GET_SHAPE_CONFIG(TAB::TOP_MARGIN, __pTab->GetOrientation(), topMarginTextOnly);
832         GET_SHAPE_CONFIG(TAB::BOTTOM_MARGIN, __pTab->GetOrientation(), bottomMarginTextOnly);
833
834         GET_SHAPE_CONFIG(TAB::ICON_TEXT_TOP_MARGIN, __pTab->GetOrientation(), topMargin);
835         GET_SHAPE_CONFIG(TAB::ICON_TEXT_BOTTOM_MARGIN, __pTab->GetOrientation(), bottomMargin);
836         GET_SHAPE_CONFIG(TAB::ITEM_LEFT_MARGIN, __pTab->GetOrientation(), leftMargin);
837         GET_SHAPE_CONFIG(TAB::ITEM_RIGHT_MARGIN, __pTab->GetOrientation(), rightMargin);
838         int iconWidth = 0;
839         int iconHeight = 0;
840         GET_SHAPE_CONFIG(TAB::ICON_TEXT_ICON_WIDTH, __pTab->GetOrientation(), iconWidth);
841         GET_SHAPE_CONFIG(TAB::ICON_TEXT_ICON_HEIGHT, __pTab->GetOrientation(), iconHeight);
842         int iconGap = 0;
843         GET_FIXED_VALUE_CONFIG(TAB::ICON_TEXT_GAP, __pTab->GetOrientation(), iconGap);
844
845         for (int i = 0; i < itemCount; i++)
846         {
847                 pDrawItem = GetItemAt(i);
848                 SysTryReturnVoidResult(NID_UI_CTRL, pDrawItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
849
850                 SetFirstDrawnItemIndex(0);
851
852                 itemDrawRect = pDrawItem->GetItemBounds();
853                 __dividerLine.x = itemDrawRect.x + (itemDrawRect.width - (__dividerLine.width)/2);
854                 __dividerLine.y = ((itemDrawRect.height - __dividerLine.height)/2);
855
856                 itemStatus = pDrawItem->GetStatus();
857
858                 if (itemStatus == _TABITEM_STATUS_SELECTED)
859                 {
860                         DrawSelectedItemBackground(pCanvas, itemDrawRect);
861                         __selectedLine.width = __itemWidth - __selectedLineLeftMargin -  __selectedLineRightMargin;
862                         __selectedLine.x = itemDrawRect.x + __selectedLineLeftMargin;
863                 }
864                 else if (itemStatus == _TABITEM_STATUS_PRESSED)
865                 {
866                         DrawFocusItemBackground(pCanvas, itemDrawRect);
867                 }
868                 else
869                 {
870                         DrawItemBackground(pCanvas, itemDrawRect);
871                 }
872
873
874                 itemText = pDrawItem->GetText();
875
876                 __pTextObject->RemoveAll();
877                 pSimpleText = new (std::nothrow)TextSimple(const_cast <mchar*>(itemText.GetPointer()), itemText.GetLength());
878                 SysTryReturnVoidResult(NID_UI_CTRL, pSimpleText, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
879
880                 __pTextObject->AppendElement(*pSimpleText);
881
882                 __pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
883                 __pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
884                 __pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
885                 textLength = __pTextObject->GetTextLength();
886
887                 if ((pDrawItem->GetIcon()) == null)
888                 {
889                         topMargin = topMarginTextOnly;
890                         bottomMargin = bottomMarginTextOnly;
891                 }
892                 Dimension textDim(0,0);
893
894                 // Font Size Change
895                 if (__pFont != null )
896                 {
897                         int fontSize = 0;
898
899                         if (__showItemCount < fontChangeItemCount)
900                         {
901                                 if (multiline == false)
902                                 {
903                                         GET_SHAPE_CONFIG(TAB::ICON_TEXT_FONT_SIZE_01, __pTab->GetOrientation(), fontSize);
904                                 }
905                                 else
906                                 {
907                                         GET_SHAPE_CONFIG(TAB::ICON_TEXT_FONT_SIZE_02, __pTab->GetOrientation(), fontSize);
908                                 }
909                         }
910                         else if (__showItemCount == fontChangeItemCount)
911                         {
912                                 if (multiline == false)
913                                 {
914                                         GET_SHAPE_CONFIG(TAB::ICON_TEXT_FONT_SIZE_02, __pTab->GetOrientation(), fontSize);
915                                 }
916                                 else
917                                 {
918                                         GET_SHAPE_CONFIG(TAB::ICON_TEXT_FONT_SIZE_02, __pTab->GetOrientation(), fontSize);
919                                 }
920                         }
921                         else
922                         {
923                                 if (multiline == false)
924                                 {
925                                         GET_SHAPE_CONFIG(TAB::ICON_TEXT_FONT_SIZE_03, __pTab->GetOrientation(), fontSize);
926                                 }
927                                 else
928                                 {
929                                         GET_SHAPE_CONFIG(TAB::ICON_TEXT_FONT_SIZE_03, __pTab->GetOrientation(), fontSize);
930                                 }
931                         }
932
933                         (_FontImpl::GetInstance(*__pFont))->SetSize(fontSize);
934
935                         __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
936
937                         __pFont->GetTextExtent(itemText, itemText.GetLength(), textDim);
938                 }
939
940                 itemDrawTextRect = itemDrawRect;
941                 itemDrawTextRect.x = itemDrawRect.x + leftMargin;
942                 multiline = false;
943                 if (textDim.width > (__itemWidth/2))
944                 {
945                         multiline = true;
946                         textDim.width = (__itemWidth/2);
947                         textDim.height = textDim.height * 2;
948                 }
949                 itemDrawTextRect.width = textDim.width;
950                 itemDrawTextRect.x = itemDrawRect.x + leftMargin;
951                 itemDrawTextRect.x = itemDrawTextRect.x + ((itemDrawRect.width - leftMargin - rightMargin) - (iconWidth + iconGap + itemDrawTextRect.width))/2;
952                 itemDrawTextRect.height = textDim.height;
953                 itemDrawIconRect = itemDrawTextRect;
954                 itemDrawTextRect.x = itemDrawIconRect.x + iconWidth + iconGap;
955
956                 if ((pDrawItem->GetIcon()) != null)
957                 {
958                         itemDrawIconRect.height = itemDrawRect.height - topMargin - bottomMargin;
959                         itemDrawIconRect.y = itemDrawIconRect.y +topMargin+ (itemDrawIconRect.height - iconHeight)/2;
960
961                         itemDrawIconRect.width = iconWidth;
962                         itemDrawIconRect.height = iconHeight;
963                         Bitmap* pIconBitmap = pDrawItem->GetIcon();
964
965                         DrawResourceBitmap(*pCanvas, itemDrawIconRect, pIconBitmap);
966
967                 }
968                 else
969                 {
970                         itemDrawTextRect.x = itemDrawRect.x + leftMargin + (itemDrawRect.width - leftMargin - rightMargin - itemDrawTextRect.width)/2;
971                 }
972
973
974                 if (multiline == false)
975                 {
976                         itemDrawTextRect.y = itemDrawRect.y + topMargin + (itemDrawRect.height - topMargin - bottomMargin - itemDrawTextRect.height)/2;
977                 }
978                 else
979                 {
980                         itemDrawTextRect.y = itemDrawRect.y + topMargin + (itemDrawRect.height - topMargin - bottomMargin - itemDrawTextRect.height)/2;
981                 }
982                 __pTextObject->SetBounds(itemDrawTextRect);
983                 __pTextObject->Compose();
984                 if (itemStatus == _TABITEM_STATUS_SELECTED)
985                 {
986                         __pTextObject->SetForegroundColor(__pTab->GetSelectedTextColor(), 0, __pTextObject->GetTextLength());
987                         __selectedLine.y = itemDrawRect.height - bottomMargin;
988                         if (__pTab->GetSelectedItemBackgroundBitmap() == null)
989                         {
990                                 DrawResourceBitmap(*pCanvas, __selectedLine, __pHorizontalLineBitmap);
991                         }
992                 }
993                 else if (itemStatus == _TABITEM_STATUS_PRESSED)
994                 {
995                         __pTextObject->SetForegroundColor(__pTab->GetPressedTextColor(), 0, __pTextObject->GetTextLength());
996                 }
997                 else if (itemStatus == _TABITEM_STATUS_HIGHLIGHTED)
998                 {
999                         __pTextObject->SetForegroundColor(__pTab->GetHighlightedTextColor(), 0, __pTextObject->GetTextLength());
1000                 }
1001                 else
1002                 {
1003                         __pTextObject->SetForegroundColor(__pTab->GetTextColor(), 0, __pTextObject->GetTextLength());
1004                 }
1005
1006                 __pTextObject->SetBounds(itemDrawTextRect);
1007
1008                 __pTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
1009
1010                 if (i < itemCount - 1)
1011                 {
1012                         DrawItemDivisionVerticalLine(pCanvas, __dividerLine);
1013                 }
1014                 DrawBadgeIcon(pDrawItem, pCanvas);
1015
1016         }
1017         return;
1018 }
1019
1020 void
1021 _TabPresenter::DrawEditItem(const Point& point)
1022 {
1023
1024         _TabItem *pTabItem = null;
1025         Rectangle editRect(0, 0, 0, 0);
1026         pTabItem = GetItemAt(__editItemIndex);
1027         SysTryReturnVoidResult(NID_UI_CTRL, pTabItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1028
1029         editRect = pTabItem->GetItemBounds();
1030
1031         editRect.x = point.x - (__itemWidth / 2);
1032         pTabItem->SetItemBounds(editRect);
1033
1034         return;
1035
1036 }
1037
1038 void
1039 _TabPresenter::DrawItemDivisionVerticalLine(Canvas* pCanvas, Rectangle& lineRect)
1040 {
1041         DrawResourceBitmap(*pCanvas, lineRect, __pVerticalLineBitmap);
1042
1043         return;
1044 }
1045
1046 void
1047 _TabPresenter::DrawHorizontal()
1048 {
1049         ClearLastResult();
1050
1051         int itemCount = GetItemCount();
1052         int boundItemIndex = 0;
1053         if (__moveDistance == 0)
1054         {
1055                 return;
1056         }
1057         if (itemCount == 0)
1058         {
1059                 SysLog(NID_UI_CTRL, "itemCount is 0.");
1060                 return;
1061         }
1062         //Bound beyond which the item shouldnt move when flicked
1063         boundItemIndex = itemCount - __itemMaxCount;
1064
1065         _TabItem* pItem = null;
1066         pItem = GetItemAt(boundItemIndex);
1067         SysTryReturnVoidResult(NID_UI_CTRL, pItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1068
1069         Rectangle bounds = pItem->GetItemBounds();
1070         Rectangle itemBounds(0, 0, 0, 0);
1071         if ((bounds.x + __moveDistance) < 0)
1072         {
1073                 __moveDistance = -bounds.x;
1074                 ResetFlickAnimationTimer();
1075         }
1076
1077         pItem = GetItemAt(0);
1078         SysTryReturnVoidResult(NID_UI_CTRL, pItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1079
1080         bounds = pItem->GetItemBounds();
1081
1082         if ((bounds.x + __moveDistance) > 0)
1083         {
1084                 CalcItemBounds();
1085                 ResetFlickAnimationTimer();
1086                 return;
1087         }
1088
1089         for (int i = 0; i < itemCount; i++)
1090         {
1091                 pItem = GetItemAt(i);
1092                 SysTryReturnVoidResult(NID_UI_CTRL, pItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1093
1094                 itemBounds = pItem->GetItemBounds();
1095
1096                 itemBounds.x = itemBounds.x + __moveDistance;
1097                 pItem->SetItemBounds(itemBounds);
1098         }
1099         return;
1100 }
1101
1102 void
1103 _TabPresenter::DrawBadgeIcon(_TabItem* pDrawItem, Canvas* pCanvas)
1104 {
1105         Bitmap* pBadgeIcon = null;
1106         int badgeMargin = 0;
1107         GET_SHAPE_CONFIG(TAB::BADGE_ICON_MARGIN, __pTab->GetOrientation(), badgeMargin);
1108         // Badge.
1109         pBadgeIcon = pDrawItem->GetBadgeIcon();
1110
1111         if (pBadgeIcon != null)
1112         {
1113                 Rectangle badgeRect = pDrawItem->GetItemBounds();
1114
1115                 badgeRect.x = (badgeRect.x + badgeRect.width) - pBadgeIcon->GetWidth() - badgeMargin;
1116                 badgeRect.y = badgeMargin;
1117                 badgeRect.width = pBadgeIcon->GetWidth();
1118                 badgeRect.height = pBadgeIcon->GetHeight();
1119
1120                 DrawResourceBitmap(*pCanvas, badgeRect, pBadgeIcon);
1121
1122                 pBadgeIcon = null;
1123         }
1124         return;
1125 }
1126
1127 void
1128 _TabPresenter::CalcItemBounds(void)
1129 {
1130         ClearLastResult();
1131
1132         const int itemCount = GetItemCount();
1133
1134         if (itemCount == 0)
1135         {
1136                 return;
1137         }
1138
1139         CalcTabBounds(true);
1140
1141         Rectangle bounds = __pTab->GetBounds();
1142
1143         _TabItem* pItem = null;
1144         Rectangle itemBounds(0, 0, 0, 0);
1145
1146         if (__itemMaxCount > itemCount)
1147         {
1148                 __showItemCount = itemCount;
1149         }
1150         else
1151         {
1152                 __showItemCount = __itemMaxCount;
1153         }
1154
1155         itemBounds.width = bounds.width / __showItemCount;
1156         itemBounds.height = bounds.height;
1157         __itemWidth = itemBounds.width;
1158         __itemHeight = bounds.height;
1159
1160
1161         for (int i = 0; i < itemCount; i++)
1162         {
1163                 pItem = GetItemAt(i);
1164                 SysTryReturnVoidResult(NID_UI_CTRL, pItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1165
1166                 __pAnimInfo[i]->xPosition = itemBounds.x;
1167                 __pAnimInfo[i]->totalDistance = __itemWidth;
1168                 pItem->SetItemBounds(itemBounds);
1169                 itemBounds.x = itemBounds.x + itemBounds.width;
1170         }
1171         return;
1172 }
1173
1174 void
1175 _TabPresenter::CalcTabBounds(bool bReset)
1176 {
1177         Rectangle bounds = __pTab->GetBounds();
1178
1179         bool movable = __pTab->IsMovable();
1180         bool resizable = __pTab->IsResizable();
1181
1182         int lineHeight = 0;
1183         int tabLeftMargin = 0;
1184         int tabRightMargin = 0;
1185         GET_SHAPE_CONFIG(TAB::LEFT_MARGIN, __pTab->GetOrientation(), tabLeftMargin);
1186         GET_SHAPE_CONFIG(TAB::RIGHT_MARGIN, __pTab->GetOrientation(), tabRightMargin);
1187
1188         if (__pTab->GetStyle() == _TAB_STYLE_ICON_TEXT)
1189         {
1190                 GET_SHAPE_CONFIG(TAB::ICON_TEXT_HEIGHT, __pTab->GetOrientation(), lineHeight);
1191         }
1192         else
1193         {
1194                 GET_SHAPE_CONFIG(TAB::HEIGHT, __pTab->GetOrientation(), lineHeight);
1195         }
1196         __lastItemIndex = (__pTab->GetItemCount() - 1);
1197         // Only 1 line
1198         bounds.height = lineHeight;
1199
1200         for (int i = 0; i < (__pTab->GetItemCount()); i++)
1201         {
1202                 if (__pAnimInfo[i] == null)
1203                 {
1204                         __pAnimInfo[i] = new (std::nothrow) ReorderAnimationInfo();
1205                         SysTryReturnVoidResult(NID_UI_CTRL, __pAnimInfo[i], E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1206
1207                         __pAnimInfo[i]->totalDistance = 0;
1208                         __pAnimInfo[i]->move = 0;
1209                         __pAnimInfo[i]->reorderValue = 0;
1210                 }
1211         }
1212
1213         __pTab->SetMovable(true);
1214         __pTab->SetResizable(true);
1215
1216         __pTab->SetBounds(bounds);
1217
1218         __pTab->SetMovable(movable);
1219         __pTab->SetResizable(resizable);
1220
1221         return;
1222
1223 }
1224
1225 bool
1226 _TabPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
1227 {
1228         if (GetItemCount() == 0)
1229         {
1230                 return true;
1231         }
1232         __currentTouchPosition = touchinfo.GetCurrentPosition();
1233         SetFirstLoadedItemIndex();
1234         SetLastLoadedItemIndex();
1235         SetItemFitToLeft();
1236         ResetScrollAnimationTimer();
1237         SetReCalculateItemBounds(false);
1238         ResetFlickAnimationTimer();
1239
1240         Point point = touchinfo.GetCurrentPosition();
1241
1242         const int index = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
1243         if (index == -1)
1244         {
1245                 return false;
1246         }
1247         if (GetItemStatus(index) != _TABITEM_STATUS_SELECTED)
1248         {
1249                 SetItemStatus(index, _TABITEM_STATUS_PRESSED);
1250                 SetPressedItemIndex(index);
1251         }
1252
1253         __pTab->Invalidate();
1254
1255         return true;
1256 }
1257
1258 bool
1259 _TabPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
1260 {
1261         if (GetItemCount() == 0)
1262         {
1263                 return true;
1264         }
1265         Point point = touchinfo.GetCurrentPosition();
1266         __scrollFlag = 0;
1267         CancelReorderTimer();
1268         ResetScrollAnimationTimer();
1269         Rectangle itemBounds(0, 0, 0, 0);
1270         _TabItem* pItem = null;
1271         int index = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
1272         if (__pScrollingEffectVe)
1273         {
1274                 __pScrollingEffectVe->RemoveAllAnimations();
1275                 __pTab->GetVisualElement()->DetachChild(*__pScrollingEffectVe);
1276                 __pScrollingEffectVe->Destroy();
1277                 __pScrollingEffectVe = null;
1278                 __isScrollEffectEnded = false;
1279                 __isScrollEffectStarted = false;
1280         }
1281         int pressedIndex = GetPressedItemIndex();
1282         __isRightScrolable = false;
1283         if (index == -1 && !__isFlickEnabled)
1284         {
1285                 SetFirstLoadedItemIndex();
1286                 if (GetItemStatus(pressedIndex) == _TABITEM_STATUS_PRESSED)
1287                 {
1288                         SetItemStatus(pressedIndex, _TABITEM_STATUS_NORMAL);
1289                 }
1290                 CalcItemBounds();
1291                 __moveDistance = -(GetItemAt(__firstLoadedItemIndex)->GetItemBounds().x);
1292                 DrawHorizontal();
1293                 __pTab->Invalidate();
1294                 __rearIndex = 0;
1295                 __editMode =  false;
1296                 return false;
1297         }
1298         if (__editMode && __editItemIndex == __rearIndex)
1299         {
1300                 Rectangle bounds(0,0,0,0);
1301                 if(__editItemIndex > 0)
1302                 {
1303                         pItem = GetItemAt((__editItemIndex - 1));
1304                         SysTryReturn(NID_UI_CTRL, pItem, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1305
1306                         bounds = pItem->GetItemBounds();
1307                         bounds.x = bounds.x + __itemWidth;
1308                         GetItemAt(__editItemIndex)->SetItemBounds(bounds);
1309                 }
1310                 else
1311                 {
1312                         CalcItemBounds();
1313                 }
1314
1315         }
1316         // Edit Mode - Item Insert
1317         if (__editMode && (__editItemIndex != __rearIndex))
1318         {
1319                 index = __rearIndex;
1320                 int i = GetSelectedItemIndex();
1321                 SetItemStatus(i, _TABITEM_STATUS_NORMAL);
1322
1323                 if (__isDirectionRight)
1324                 {
1325                         index = index + 1;
1326                 }
1327
1328                 __pTab->ChangeItemAt(__editItemIndex, (index));
1329                 if (__editItemIndex < index)
1330                 {
1331                         SetItemStatus((index-1), _TABITEM_STATUS_SELECTED);
1332                         SetSelectedItemIndex((index-1));
1333                 }
1334                 else if (__editItemIndex > index)
1335                 {
1336                         SetItemStatus(index, _TABITEM_STATUS_SELECTED);
1337                         SetSelectedItemIndex(index);
1338                 }
1339
1340                 SetFirstLoadedItemIndex();
1341                 SetLastLoadedItemIndex();
1342
1343
1344                 CalcItemBounds();
1345                 __moveDistance = -(__firstLoadedItemIndex * __itemWidth);
1346                 DrawHorizontal();
1347         }
1348         else
1349         {
1350                 // Pressed -> Selected or Normal
1351                 if (GetItemStatus(index) == _TABITEM_STATUS_PRESSED && !__isFlickEnabled && !__isTouchMove)
1352                 {
1353                         int i = GetSelectedItemIndex();
1354                         SetItemStatus(i, _TABITEM_STATUS_NORMAL);
1355
1356                         SetItemStatus(index, _TABITEM_STATUS_SELECTED);
1357                         SetSelectedItemIndex(index);
1358                 }
1359                 else
1360                 {
1361                         int i = GetPressedItemIndex();
1362                         if (GetItemStatus(i) == _TABITEM_STATUS_PRESSED)
1363                         {
1364                                 SetItemStatus(i, _TABITEM_STATUS_NORMAL);
1365                         }
1366                 }
1367
1368         }
1369
1370         __pTab->Invalidate();
1371         __isTouchMove = false;
1372         __editMode = false;
1373         __editItemIndex = -1;
1374         __rearIndex = -1;
1375
1376         if (__firstItemIndex > 0)
1377         {
1378                 __firstItemIndex--;
1379         }
1380
1381         if (__lastItemIndex < (__pTab->GetItemCount() - 1))
1382         {
1383                 __lastItemIndex++;
1384         }
1385
1386         return true;
1387 }
1388
1389 bool
1390 _TabPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
1391 {
1392         if (GetItemCount() == 0)
1393         {
1394                 return true;
1395         }
1396         _TabItem *pTabItem = null;
1397         _TabItem *pNextItem = null;
1398         _TabItem *pPrevItem = null;
1399         Point movedPosition = touchinfo.GetCurrentPosition();
1400
1401         int index = GetNextItemIndexFromPosition(movedPosition, __editItemIndex);
1402         SetFirstLoadedItemIndex();
1403         SetLastLoadedItemIndex();
1404         //Remove the Pressed Bitmap during Touch Move
1405         int i = GetPressedItemIndex();
1406         if (!__editMode)
1407         {
1408                 if (GetItemStatus(i) == _TABITEM_STATUS_PRESSED)
1409                 {
1410                         SetItemStatus(i, _TABITEM_STATUS_NORMAL);
1411                         Draw();
1412                 }
1413         }
1414
1415         int distance;
1416         distance = movedPosition.x - __currentTouchPosition.x;
1417         __currentTouchPosition.x = movedPosition.x;
1418         if (!__editMode && distance > 0) //Move right i.e Bounce at leftEnd
1419         {
1420                 __isTouchMove = true;
1421                 if (!__isDirectionRight)
1422                 {
1423                         if (__pScrollingEffectVe)
1424                         {
1425                                 __pScrollingEffectVe->RemoveAllAnimations();
1426                                 __pTab->GetVisualElement()->DetachChild(*__pScrollingEffectVe);
1427                                 __pScrollingEffectVe->Destroy();
1428                                 __pScrollingEffectVe = null;
1429                         }
1430                         __isScrollEffectEnded = false;
1431                         __isScrollEffectStarted = false;
1432                 }
1433                 __isDirectionRight = true;
1434                 SetFirstLoadedItemIndex();
1435                 if ( GetItemAt(0)->GetItemBounds().x >= 0)
1436                 {
1437                         if (!__isScrollEffectStarted)
1438                         {
1439                                 __pScrollingEffectBitmap = __pFrontScrollingEffectBitmap;
1440                                 SetOpacityVisualElement(0);
1441                                 StartScrollingEffect();
1442                         }
1443                         __isScrollEffectStarted = true;
1444                 }
1445                 __moveDistance = distance;
1446                 DrawHorizontal();
1447                 Draw();
1448                 __moveDistance = 0;
1449
1450
1451                 return true;
1452         }
1453         if (!__editMode && distance < 0)//Move left i.e Bounce at RightEnd
1454         {
1455                 __isTouchMove = true;
1456                 if (__isDirectionRight)
1457                 {
1458                         if (__pScrollingEffectVe)
1459                         {
1460                                 __pScrollingEffectVe->RemoveAllAnimations();
1461                                 __pTab->GetVisualElement()->DetachChild(*__pScrollingEffectVe);
1462                                 __pScrollingEffectVe->Destroy();
1463                                 __pScrollingEffectVe = null;
1464                         }
1465                         __isScrollEffectEnded = false;
1466                         __isScrollEffectStarted = false;
1467                 }
1468                 __isDirectionRight = false;
1469                 SetLastLoadedItemIndex();
1470
1471                 if ( __lastLoadedItemIndex == (GetItemCount() - 1) && ((GetItemAt(__lastLoadedItemIndex)->GetItemBounds().x + __itemWidth) <= (__width)))
1472                 {
1473                         if (!__isScrollEffectStarted)
1474                         {
1475                                 int index = (GetItemCount() - 1);
1476                                 int xPosition = (GetItemAt(index)->GetItemBounds().x + (__itemWidth/2));
1477                                 __pScrollingEffectBitmap = __pRearScrollingEffectBitmap;
1478                                 SetOpacityVisualElement(xPosition);
1479                                 StartScrollingEffect();
1480
1481                         }
1482                         __isScrollEffectStarted = true;
1483                 }
1484                 __moveDistance = distance;
1485                 DrawHorizontal();
1486                 Draw();
1487                 __moveDistance = 0;
1488
1489                 return true;
1490         }
1491         if (!__editMode)
1492         {
1493                 return false;
1494         }
1495
1496         if (distance > 0)
1497         {
1498                 if (!__isDirectionRight)
1499                 {
1500                         __isTimerCanceled = true;
1501                         __isRightScrolable = false;
1502                         __isLeftScrolable = false;
1503                         CancelReorderTimer();
1504                 }
1505                 __isDirectionRight = true;
1506         }
1507         else
1508         {
1509                 if (__isDirectionRight)
1510                 {
1511                         __isTimerCanceled = true;
1512                         __isLeftScrolable = false;
1513                         __isRightScrolable = false;
1514                         CancelReorderTimer();
1515                 }
1516                 __isDirectionRight = false;
1517         }
1518         pTabItem = GetItemAt(__editItemIndex);
1519         SysTryReturn(NID_UI_CTRL, pTabItem, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1520
1521         Rectangle bounds = pTabItem->GetItemBounds();
1522         if (index != -1)
1523         {
1524                 if (__isDirectionRight)
1525                 {
1526                         pNextItem = GetItemAt(index);
1527                         SysTryReturn(NID_UI_CTRL, pNextItem, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1528
1529                         Rectangle boundsNext = pNextItem->GetItemBounds();
1530                         if ((bounds.x + (__itemWidth / 2) ) > (boundsNext.x) && ((bounds.x + __itemWidth) < (boundsNext.x + boundsNext.width)))
1531                         {
1532                                 __pTimerInfo[__timerCount]->index = index;
1533                                 StartReorderTimer(__timerCount);
1534                                 __timerCount++;
1535                                 if (__timerCount >= (__itemMaxCount - 1))
1536                                 {
1537                                         __timerCount = 0;
1538                                 }
1539                                 __isTimerCanceled = false;
1540                                 __rearIndex = index;
1541
1542                         }
1543                 }
1544                 else
1545                 {
1546                         pPrevItem = GetItemAt(index);
1547                         SysTryReturn(NID_UI_CTRL, pPrevItem, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1548
1549                         Rectangle boundsPrev = pPrevItem->GetItemBounds();
1550                         if ((bounds.x + 100) > (boundsPrev.x + 100) && ((bounds.x + 100) < (boundsPrev.x + boundsPrev.width)))
1551                         {
1552                                 __pTimerInfo[__timerCount]->index = index;
1553                                 StartReorderTimer(__timerCount);
1554                                 __timerCount++;
1555                                 if (__timerCount >= (__itemMaxCount - 1))
1556                                 {
1557                                         __timerCount = 0;
1558                                 }
1559                                 __isTimerCanceled = false;
1560                                 __rearIndex = index;
1561
1562                         }
1563                 }
1564         }
1565         else //Scroll the entire tab when item moves beyond boundary
1566         {
1567                 if ((bounds.x + __itemWidth) > (__width - (__itemWidth / 2)))
1568                 {
1569                         if (__isDirectionRight)
1570                         {
1571                                 _TabItem *pLastItem = GetItemAt(__lastItemIndex);
1572                                 SysTryReturn(NID_UI_CTRL, pLastItem, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1573
1574                                 if ((pLastItem->GetItemBounds().x + pLastItem->GetItemBounds().width) > __width)
1575                                 {
1576                                         __isRightScrolable = true;
1577                                         StartScrollAnimationTimer();
1578                                 }
1579                         }
1580                 }
1581
1582                 if (bounds.x < 0)
1583                 {
1584                         if (!__isDirectionRight)
1585                         {
1586                                 if (GetItemAt(__firstItemIndex)->GetItemBounds().x < __itemWidth)
1587                                 {
1588                                         __isLeftScrolable = true;
1589                                         StartScrollAnimationTimer();
1590                                 }
1591                         }
1592                 }
1593         }
1594
1595         DrawEditItem(movedPosition);
1596         __pTab->Invalidate();
1597
1598         return true;
1599 }
1600
1601 void
1602 _TabPresenter::ShiftItems(void)
1603 {
1604         Point pt = Point(0,0);
1605         int itemCount = GetItemCount();
1606         int fitDistance = 0;
1607         _TabItem* pTabItem = null;
1608         Rectangle bounds(0,0,0,0);
1609         pTabItem = GetItemAt(0);
1610         SysTryReturnVoidResult(NID_UI_CTRL, pTabItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1611
1612         bounds = pTabItem->GetItemBounds();
1613
1614         for (int i = 0; i < itemCount; i++)
1615         {
1616                 if (i == __editItemIndex)
1617                 {
1618                         continue;
1619                 }
1620                 if (__isRightScrolable)
1621                 {
1622
1623                         if (i == __rearIndex + 1)
1624                         {
1625                                 fitDistance = -(2 * __itemWidth);
1626
1627                         }
1628                         else
1629                         {
1630                                 fitDistance = -__itemWidth;
1631                         }
1632                 }
1633                 else
1634                 {
1635                         if (i == __rearIndex - 1)
1636                         {
1637                                 fitDistance = (2 * __itemWidth);
1638
1639                         }
1640                         else
1641                         {
1642                                 fitDistance = __itemWidth;
1643                         }
1644                 }
1645
1646                 pTabItem =  GetItemAt(i);
1647                 SysTryReturnVoidResult(NID_UI_CTRL, pTabItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1648
1649                 bounds = pTabItem->GetItemBounds();
1650                 bounds.x = bounds.x + fitDistance;
1651                 if ( i == __firstItemIndex && ( bounds.x ) >= (2 * __itemWidth))
1652                 {
1653                         return;
1654                 }
1655                 pTabItem->SetItemBounds(bounds);
1656
1657         }
1658
1659         if (__isRightScrolable)
1660         {
1661                 __rearIndex++;
1662         }
1663
1664         if (__isLeftScrolable)
1665         {
1666                 __rearIndex--;
1667         }
1668         Draw();
1669         return;
1670 }
1671
1672 bool
1673 _TabPresenter::OnLongPressGestureDetected(void)
1674 {
1675         if (!__pTabModel->IsEditModeEnabled())
1676         {
1677                 return true;
1678         }
1679         const int index = GetItemIndexFromPosition(__currentTouchPosition);
1680
1681         if (index == -1)
1682         {
1683                 return false;
1684         }
1685
1686         if (GetItemStatus(index) == _TABITEM_STATUS_PRESSED && GetSelectedItemIndex() != index)
1687         {
1688                 SetItemStatus(index, _TABITEM_STATUS_NORMAL);
1689         }
1690         __editMode = true;
1691
1692         __editItemIndex = index;
1693         if (__editItemIndex == __firstItemIndex)
1694         {
1695                 __firstItemIndex = __firstItemIndex + 1;
1696         }
1697
1698         if (__editItemIndex == __lastItemIndex)
1699         {
1700                 __lastItemIndex = __lastItemIndex - 1;
1701         }
1702
1703         __rearIndex = index;
1704         __pTab->Invalidate();
1705
1706         return true;
1707 }
1708
1709 bool
1710 _TabPresenter::OnFlickGestureDetected(_TouchFlickGestureDetector& gesture)
1711 {
1712         if (GetItemCount() == 0)
1713         {
1714                 return true;
1715         }
1716
1717         if (__editMode)
1718         {
1719                 return true;
1720         }
1721         if (gesture.GetDirection() != _FLICK_DIRECTION_RIGHT && gesture.GetDirection() != _FLICK_DIRECTION_LEFT)
1722         {
1723                 return false;
1724         }
1725         int distanceX = 0;
1726         int distanceY = 0;
1727         gesture.GetDistance(distanceX, distanceY);
1728         SetFirstLoadedItemIndex();
1729         SetLastLoadedItemIndex();
1730         int xPosition = 0;
1731         int itemsBeyondScreen = 0;
1732         int itemCount = GetItemCount();
1733         if (distanceX < 0)  //left
1734         {
1735                 itemsBeyondScreen = (itemCount - 1) - __lastLoadedItemIndex;
1736                 xPosition = GetItemAt(__lastLoadedItemIndex)->GetItemBounds().x;
1737                 if (xPosition < (3 * __itemWidth))
1738                 {
1739                         __flickDistance = xPosition - (4 * __itemWidth);
1740                 }
1741         }
1742         else //Right
1743         {
1744                 itemsBeyondScreen = __firstLoadedItemIndex;
1745                 xPosition = GetItemAt(__firstLoadedItemIndex)->GetItemBounds().x;
1746                 if (xPosition < 0)
1747                 {
1748                         __flickDistance = -xPosition;
1749                 }
1750
1751         }
1752         if (itemsBeyondScreen >= (__itemMaxCount - 1))
1753         {
1754                 __flickDistance = (3 * __itemWidth);
1755         }
1756         else
1757         {
1758                 __flickDistance = (itemsBeyondScreen * __itemWidth);
1759         }
1760         if (distanceX < 0) //Left flick
1761         {
1762                 __flickMove = 0.006;
1763                 __flickFlag = 0;
1764         }
1765         else if (distanceX > 0) //Right flick
1766         {
1767                 __flickMove = 0.006;
1768                 __flickDistance = -(__flickDistance);
1769                 __flickFlag = 0;
1770         }
1771         else
1772         {
1773                 __flickMove = 0;
1774                 __flickDistance = 0;
1775                 __flickFlag = 0;
1776                 __moveDistance =0;
1777         }
1778         __isFlickEnabled = true;
1779
1780         StartFlickAnimationTimer();
1781
1782         return true;
1783 }
1784
1785 void
1786 _TabPresenter::StartFlickAnimation(void)
1787 {
1788
1789         float distance = CalculateProgress(__flickMove);
1790         __moveDistance = -(__flickDistance * distance);
1791
1792         __flickDistance = (__flickDistance + __moveDistance);
1793         __flickMove = __flickMove + 0.006;
1794         __flickFlag++;
1795         if (__flickFlag <= 400)
1796         {
1797                 StartFlickAnimationTimer();
1798         }
1799         else
1800         {
1801                 ResetFlickAnimationTimer();
1802         }
1803
1804         Draw();
1805         return;
1806 }
1807
1808 result
1809 _TabPresenter::StartFlickAnimationTimer(void)
1810 {
1811         result r = E_SUCCESS;
1812
1813         if (__pFlickAnimationTimer == null)
1814         {
1815                 __pFlickAnimationTimer = new (std::nothrow) Timer;
1816                 SysTryReturn(NID_UI_CTRL, (__pFlickAnimationTimer != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1817
1818                 r = __pFlickAnimationTimer->Construct(*this);
1819                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1820         }
1821         else
1822         {
1823                 __pFlickAnimationTimer->Cancel();
1824         }
1825
1826         r = __pFlickAnimationTimer->Start(FLICK_ANIMATION_TIMER_PERIOD);
1827         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1828
1829         return r;
1830
1831 CATCH:
1832         ResetFlickAnimationTimer();
1833         return r;
1834 }
1835
1836 result
1837 _TabPresenter::StartScrollAnimationTimer(void)
1838 {
1839         result r = E_SUCCESS;
1840         ResetReorderTimer();
1841         if (__pScrollAnimationTimer == null)
1842         {
1843                 __pScrollAnimationTimer = new (std::nothrow) Timer;
1844                 SysTryReturn(NID_UI_CTRL, (__pScrollAnimationTimer != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1845
1846                 r = __pScrollAnimationTimer->Construct(*this);
1847                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1848         }
1849         else
1850         {
1851                 __pScrollAnimationTimer->Cancel();
1852         }
1853
1854         r = __pScrollAnimationTimer->Start(SCROLL_ANIMATION_TIMER_PERIOD);
1855         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1856
1857         return r;
1858
1859 CATCH:
1860         ResetScrollAnimationTimer();
1861         return r;
1862 }
1863
1864 result
1865 _TabPresenter::ResetScrollAnimationTimer(void)
1866 {
1867         if (__pScrollAnimationTimer)
1868         {
1869                 delete __pScrollAnimationTimer;
1870                 __pScrollAnimationTimer = null;
1871         }
1872         return E_SUCCESS;
1873 }
1874
1875 result
1876 _TabPresenter::ResetFlickAnimationTimer(void)
1877 {
1878         if (__pFlickAnimationTimer)
1879         {
1880                 delete __pFlickAnimationTimer;
1881                 __pFlickAnimationTimer = null;
1882         }
1883
1884         __isFlickEnabled = false;
1885         __moveDistance =0;
1886         return E_SUCCESS;
1887 }
1888
1889 void
1890 _TabPresenter::SetItemFit(int index)
1891 {
1892         Point pt = Point(0,0);
1893         int itemCount = GetItemCount();
1894         int fitDistance = 0;
1895         _TabItem* pTabItem = null;
1896         Rectangle bounds(0,0,0,0);
1897         pTabItem = GetItemAt(index);
1898         SysTryReturnVoidResult(NID_UI_CTRL, pTabItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1899
1900         bounds = pTabItem->GetItemBounds();
1901         fitDistance = -bounds.x;
1902         for (int i = 0; i < itemCount; i++)
1903         {
1904                 pTabItem = GetItemAt(i);
1905                 SysTryReturnVoidResult(NID_UI_CTRL, pTabItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1906
1907                 bounds = pTabItem->GetItemBounds();
1908                 bounds.x = bounds.x + fitDistance;
1909                 pTabItem->SetItemBounds(bounds);
1910         }
1911         return;
1912 }
1913
1914 void
1915 _TabPresenter::SetItemFitToLeft(void)
1916 {
1917         SetItemFit(__firstLoadedItemIndex);
1918         return;
1919 }
1920
1921 void
1922 _TabPresenter::SetItemFitToRight(void)
1923 {
1924         SetItemFit(__lastLoadedItemIndex);
1925         return;
1926 }
1927
1928 void
1929 _TabPresenter::OnTimerExpired(Timer& timer)
1930 {
1931          if (&timer == __pFlickAnimationTimer)
1932         {
1933                 StartFlickAnimation();
1934         }
1935         for (int i = 0; i < _TIMER_COUNT; i++)
1936         {
1937
1938                 if (!__isTimerCanceled)
1939                 {
1940                         if (&timer == __pTimerInfo[i]->pReorderTimer)
1941                         {
1942                                 StartReorderAnimation(__pTimerInfo[i]->index, i);
1943                         }
1944                 }
1945         }
1946         if (&timer == __pScrollAnimationTimer)
1947         {
1948                 _TabItem* pLastItem = GetItemAt(__lastItemIndex);
1949                 SysTryReturnVoidResult(NID_UI_CTRL, pLastItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1950
1951                 if ((GetItemAt(__firstItemIndex)->GetItemBounds().x >= __itemWidth))
1952                 {
1953                         __isRightScrolable = false;
1954                         __isLeftScrolable = false;
1955                 }
1956                 if ((pLastItem->GetItemBounds().x + pLastItem->GetItemBounds().width) <= __width && __isDirectionRight)
1957                 {
1958                         __isRightScrolable = false;
1959                         __isLeftScrolable = false;
1960                 }
1961                 if (__isRightScrolable || __isLeftScrolable)
1962                 {
1963                         if ((__rearIndex - 1) == __editItemIndex && !__isDirectionRight)
1964                         {
1965                                 if (__isLeftScrolable)
1966                                 {
1967                                         __rearIndex--;
1968                                 }
1969                         }
1970                         if ((__rearIndex + 1) == __editItemIndex && __isDirectionRight)
1971                         {
1972                                 if (__isRightScrolable)
1973                                 {
1974                                         __rearIndex++;
1975                                 }
1976                         }
1977                         ShiftItems();
1978                         StartScrollAnimationTimer();
1979
1980                 }
1981                 else
1982                 {
1983                         ResetScrollAnimationTimer();
1984                 }
1985         }
1986         return;
1987 }
1988
1989 float
1990 _TabPresenter::CalculateProgress(float timeProgress) const
1991 {
1992         const float segments[3][3] = {{0.0f, 0.01f, 0.45f}, {0.45f, 0.80f, 0.908f}, {0.908f, 0.9999f, 1.0f}};
1993         float loc_5 = timeProgress / 1;
1994         int loc_6 = 3;  //Length of the segments array
1995         int loc_9 = (int)floor(loc_6 * loc_5);
1996         if (loc_9 >= loc_6)
1997         {
1998                 loc_9 = loc_6 - 1;
1999         }
2000         float loc_7 = (loc_5 - loc_9 * (1.0 / loc_6)) * loc_6;
2001         float loc_8[3];
2002         for (int i = 0; i < 3; i++)
2003         {
2004                  loc_8[i] = segments[loc_9][i];
2005         }
2006         float ret = 0 + 1 * (loc_8[0] + loc_7 * (2 * (1 - loc_7) * (loc_8[1] - loc_8[0]) + loc_7 * (loc_8[2] - loc_8[0])));
2007         return ret;
2008 }
2009
2010 result
2011 _TabPresenter::StartReorderTimer(int index)
2012 {
2013
2014         result r = E_SUCCESS;
2015         if (__pTimerInfo[index]->pReorderTimer == null)
2016         {
2017                 __pTimerInfo[index]->pReorderTimer = new (std::nothrow) Timer;
2018                 SysTryReturn(NID_UI_CTRL, (__pTimerInfo[index]->pReorderTimer != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2019
2020                 r = __pTimerInfo[index]->pReorderTimer->Construct(*this);
2021                 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
2022         }
2023         else
2024         {
2025                 __pTimerInfo[index]->pReorderTimer->Cancel();
2026         }
2027
2028         r = __pTimerInfo[index]->pReorderTimer->Start(REORDER_TIMER_PERIOD);
2029         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
2030
2031         return r;
2032
2033 CATCH:
2034         ResetReorderTimer();
2035         return r;
2036 }
2037
2038 void
2039 _TabPresenter::FreeHorizontalLineBitmap()
2040 {
2041         if (__pHorizontalLineBitmap != null)
2042         {
2043                 delete __pHorizontalLineBitmap;
2044                 __pHorizontalLineBitmap = null;
2045         }
2046         return;
2047 }
2048
2049
2050 result
2051 _TabPresenter::CancelReorderTimer()
2052 {
2053
2054         for (int index = 0; index < (__pTab->GetItemCount()); index++)
2055         {
2056                 if (index < _TIMER_COUNT && __pTimerInfo[index])
2057                 {
2058                         if (__pTimerInfo[index]->pReorderTimer)
2059                         {
2060                                 __pTimerInfo[index]->pReorderTimer->Cancel();
2061                         }
2062                 }
2063
2064                 __pAnimInfo[index]->totalDistance = __itemWidth;
2065                 __pAnimInfo[index]->move = 0.5;
2066                 __pAnimInfo[index]->reorderValue =0;
2067         }
2068         return E_SUCCESS;
2069 }
2070
2071 result
2072 _TabPresenter::ResetReorderTimer(int index)
2073 {
2074         __pAnimInfo[index]->totalDistance = __itemWidth;
2075         __pAnimInfo[index]->move = 0.5;
2076         __pAnimInfo[index]->reorderValue =0;
2077
2078         for (int i = 0; i <_TIMER_COUNT; i++)
2079         {
2080                 if (__pTimerInfo[i])
2081                 {
2082                         if (__pTimerInfo[i]->pReorderTimer)
2083                         {
2084                                 __pTimerInfo[i]->pReorderTimer->Cancel();
2085                         }
2086                 }
2087         }
2088         return E_SUCCESS;
2089 }
2090
2091 result
2092 _TabPresenter::StopReorderTimer()
2093 {
2094         for (int i = 0 ; i < _TIMER_COUNT; i++)
2095         {
2096                 if (__pTimerInfo[i])
2097                 {
2098                         delete __pTimerInfo[i]->pReorderTimer;
2099                         __pTimerInfo[i]->pReorderTimer = null;
2100                 }
2101         }
2102
2103         return E_SUCCESS;
2104 }
2105
2106 void
2107 _TabPresenter::StartReorderAnimation(int index, int timerIndex)
2108 {
2109
2110         float dist = CalculateProgress(__pAnimInfo[index]->move);
2111         __reorderDist = -(__pAnimInfo[index]->totalDistance * dist);
2112
2113         __pAnimInfo[index]->move = __pAnimInfo[index]->move + 0.5;
2114         __pAnimInfo[index]->totalDistance = (__pAnimInfo[index]->totalDistance + __reorderDist);
2115
2116         if (!__isDirectionRight)
2117         {
2118                 __reorderDist = -__reorderDist;
2119         }
2120         __pAnimInfo[index]->reorderValue++;
2121
2122         if (__pAnimInfo[index]->reorderValue <= 50)
2123         {
2124                 SwapItems(index, __reorderDist);
2125                 StartReorderTimer(timerIndex);
2126
2127         }
2128         else
2129         {
2130                 ResetReorderTimer(index);
2131         }
2132
2133
2134         Draw();
2135         return;
2136
2137 }
2138
2139 void
2140 _TabPresenter::SwapItems(int indexDest, int distance)
2141 {
2142         _TabItem *pItem;
2143         Rectangle itemBounds(0,0,0,0);
2144         pItem = GetItemAt(indexDest);
2145         SysTryReturnVoidResult(NID_UI_CTRL, pItem, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2146
2147         itemBounds = pItem->GetItemBounds();
2148         itemBounds.x = itemBounds.x + distance;
2149         SetFirstLoadedItemIndex();
2150         SetLastLoadedItemIndex();
2151         int animIndex;
2152         int index;
2153         index = GetNextItemIndexFromPosition(Point(itemBounds.x,itemBounds.y), __editItemIndex);
2154         if (distance < 0) //Moving Left
2155         {
2156                 animIndex = indexDest - __lastLoadedItemIndex;
2157                 if (__pTab->GetItemCount() >= __itemMaxCount)
2158                 {
2159                         animIndex = animIndex + (__itemMaxCount - 1);
2160                 }
2161                 else
2162                 {
2163                         animIndex = animIndex + (__pTab->GetItemCount() - 1);
2164                 }
2165                 if ((__lastLoadedItemIndex > __editItemIndex) && (indexDest < __editItemIndex))
2166                 {
2167                         animIndex++;
2168                 }
2169                 if ((itemBounds.x <= __pAnimInfo[animIndex]->xPosition) &&  (itemBounds.x >= __pAnimInfo[animIndex-1]->xPosition))
2170                 {
2171                         if (index == -1 || index == indexDest) //to avoid overlaping of items on each other
2172                         {
2173                                 pItem->SetItemBounds(itemBounds);
2174                         }
2175                 }
2176         }
2177         if (distance > 0)
2178         {
2179                 animIndex = indexDest - __firstLoadedItemIndex;
2180                 if ((__firstLoadedItemIndex < __editItemIndex) && (indexDest > __editItemIndex))
2181                 {
2182                         animIndex--;
2183                 }
2184                 if ((itemBounds.x >= __pAnimInfo[animIndex]->xPosition) &&  (itemBounds.x <= __pAnimInfo[animIndex+1]->xPosition))
2185                 {
2186                         if (index == -1 || index == indexDest)
2187                         {
2188                                 pItem->SetItemBounds(itemBounds);
2189                         }
2190                 }
2191         }
2192         Draw();
2193         return;
2194
2195 }
2196
2197 int
2198 _TabPresenter::GetNextItemIndexFromPosition(const Point& point, int curIndex) const
2199 {
2200
2201         Rectangle itemBounds(0, 0, 0, 0);
2202
2203         int itemIndex = GetFirstDrawnItemIndex();
2204
2205         if ((point.x < 0) || (point.x > __pTab->GetBounds().width))
2206         {
2207                 return -1;
2208         }
2209
2210         _TabItem* pItem = __pTab->GetItemAt(itemIndex);
2211         if (pItem == null)
2212         {
2213                 return -1;
2214         }
2215
2216         while (pItem != null)
2217         {
2218                 itemBounds = pItem->GetItemBounds();
2219
2220                 if ((point.x > itemBounds.x) && (point.x < itemBounds.x + itemBounds.width))
2221                 {
2222                         if (curIndex != itemIndex)
2223                         return itemIndex;
2224                 }
2225
2226                 itemIndex++;
2227                 pItem = __pTab->GetItemAt(itemIndex);
2228                 if (pItem == null)
2229                 {
2230                         break;
2231                 }
2232         }
2233
2234         return -1;
2235 }
2236
2237 void
2238 _TabPresenter::SetFirstLoadedItemIndex()
2239 {
2240         for (int i = 0; i < __pTab->GetItemCount(); i++)
2241         {
2242                 if (__editItemIndex == i)
2243                 {
2244                         continue;
2245                 }
2246                 if ((GetItemAt(i)->GetItemBounds().x + 50)  <= __itemWidth)
2247                 {
2248                         __firstLoadedItemIndex = i;
2249                 }
2250         }
2251         return;
2252 }
2253
2254 void
2255 _TabPresenter::SetLastLoadedItemIndex()
2256 {
2257         for (int i = 0; i < __pTab->GetItemCount(); i++)
2258         {
2259                 int X = GetItemAt(i)->GetItemBounds().x;
2260                 if (__editItemIndex == i)
2261                 {
2262                         continue;
2263                 }
2264                 if ((X + 50) < __width)
2265                 {
2266                         __lastLoadedItemIndex = i;
2267                 }
2268         }
2269         return;
2270 }
2271
2272 void
2273 _TabPresenter::RefreshAccessibilityElement(void)
2274 {
2275         if (likely(!(_AccessibilityManager::IsActivated())))
2276         {
2277                 return;
2278         }
2279
2280         __pTab->RemoveAllAccessibilityElement();
2281
2282         for (int index = 0; index < GetItemCount(); index++)
2283         {
2284                 _TabItem* pItem = GetItemAt(index);
2285
2286                 if (pItem != null)
2287                 {
2288                         __pTab->AddAccessibilityElement(pItem->GetItemBounds(), pItem->GetText(), pItem->GetStatus());
2289                 }
2290         }
2291
2292         return;
2293 }
2294
2295
2296 void
2297 _TabPresenter::OnFontChanged(Font* pFont)
2298 {
2299         __pFont = pFont;
2300         return;
2301 }
2302
2303 void
2304 _TabPresenter::OnFontInfoRequested(unsigned long& style, int& size)
2305 {
2306         style = FONT_STYLE_BOLD;
2307         GET_SHAPE_CONFIG(TAB::FONT_SIZE_01, __pTab->GetOrientation(), size);
2308         return;
2309 }
2310
2311 void
2312 _TabPresenter::OnBoundsChanged()
2313 {
2314         Rectangle bounds = __pTab->GetBounds();
2315         if (bounds.width <= __tabMinimumSize)
2316         {
2317                 bounds.width = __tabMinimumSize;
2318         }
2319         __pTab->SetBounds(bounds);
2320         SetReCalculateItemBounds(true);
2321         return;
2322 }
2323
2324
2325 bool
2326 _TabPresenter::OnAccessibilityFocusMovedNext(const _AccessibilityContainer& control, const _AccessibilityElement& element)
2327 {
2328         if (element.GetBounds().x < 0)
2329         {
2330                 SetReCalculateItemBounds(true);
2331                 Draw();
2332         }
2333
2334         if ((element.GetBounds().x + element.GetBounds().width) > __pTab->GetBounds().width)
2335         {
2336                 __moveDistance = (element.GetBounds().width) * (-1);
2337                 DrawHorizontal();
2338                 Draw();
2339         }
2340
2341         return true;
2342 }
2343
2344 bool
2345 _TabPresenter::OnAccessibilityFocusMovedPrevious(const _AccessibilityContainer& control, const _AccessibilityElement& element)
2346 {
2347         if (element.GetBounds().x > (__pTab->GetBounds().x + __pTab->GetBounds().width))
2348         {
2349                 int noOfItems = GetItemCount();
2350                 __moveDistance = (noOfItems - __itemMaxCount) * element.GetBounds().width * (-1);
2351                 DrawHorizontal();
2352                 Draw();
2353         }
2354
2355         if (element.GetBounds().x < 0)
2356         {
2357                 __moveDistance = element.GetBounds().width;
2358                 DrawHorizontal();
2359                 Draw();
2360         }
2361
2362         return true;
2363 }
2364
2365 void
2366 _TabPresenter::SetOpacityVisualElement(int xPosition)
2367 {
2368         Canvas * pCanvas = null;
2369         int bounceWidth = (__itemWidth / 2);
2370         if (__pScrollingEffectVe == null)
2371         {
2372                 __pScrollingEffectVe = new (std::nothrow) _VisualElement();
2373                 SysTryReturnVoidResult(NID_UI_CTRL, __pScrollingEffectVe, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2374
2375                 __pScrollingEffectVe->Construct();
2376                 __pScrollingEffectVe->SetName(L"Opacity");
2377         }
2378         __pScrollingEffectVe->SetBounds(FloatRectangle(xPosition, 0, bounceWidth, __itemHeight));
2379         __pScrollingEffectVe->SetShowState(true);
2380         __pScrollingEffectVe->SetImplicitAnimationEnabled(true);
2381         __pTab->GetVisualElement()->AttachChild(*__pScrollingEffectVe);
2382         pCanvas = __pScrollingEffectVe->GetCanvasN();
2383         if (!pCanvas)
2384         {
2385                 __pScrollingEffectVe->Destroy();
2386                 __pScrollingEffectVe = null;
2387
2388                 SysLog(NID_UI_CTRL, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2389                 return;
2390         }
2391         Color backgroundColor(0,0,0,0);
2392         pCanvas->SetBackgroundColor(backgroundColor);
2393         pCanvas->Clear();
2394         Rectangle rect(0, 0, bounceWidth, __itemHeight);
2395
2396         DrawResourceBitmap(*pCanvas, rect, __pScrollingEffectBitmap);
2397
2398         delete pCanvas;
2399         return;
2400 }
2401
2402 void
2403 _TabPresenter::StartScrollingEffect(void)
2404 {
2405         float __startOpacity = 0.0f;
2406         float __endOpacity = 1.0f;
2407         VisualElementPropertyAnimation* pAnimation = new (std::nothrow) VisualElementPropertyAnimation();
2408         SysTryReturnVoidResult(NID_UI_CTRL, pAnimation, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2409
2410         pAnimation->SetPropertyName(L"opacity");
2411         pAnimation->SetStartValue(Variant(__startOpacity));
2412         pAnimation->SetEndValue(Variant(__endOpacity));
2413         pAnimation->SetDuration(500);
2414         pAnimation->SetVisualElementAnimationStatusEventListener(this);
2415
2416         String animationName3(L"opacity");
2417         __pScrollingEffectVe->AddAnimation(animationName3, *pAnimation);
2418
2419         delete pAnimation;
2420         __isScrollEffectStarted = true;
2421         return;
2422
2423 }
2424
2425 void
2426 _TabPresenter::OnVisualElementAnimationStarted(const VisualElementAnimation& animation, const String& keyName, VisualElement& target)
2427 {
2428         return;
2429 }
2430
2431 void
2432 _TabPresenter::OnVisualElementAnimationRepeated(const VisualElementAnimation& animation, const String& keyName, VisualElement& target, long currentRepeatCount)
2433 {
2434         return;
2435 }
2436
2437 void
2438 _TabPresenter::OnVisualElementAnimationFinished(const VisualElementAnimation& animation, const String& keyName, VisualElement& target, bool completedNormally)
2439 {
2440
2441         return;
2442 }
2443
2444 }}} // Tizen::Ui::Controls