Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_TabBarPresenter.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_TabBarPresenter.cpp
19  * @brief               This is the implementation file for the _TabBarPresenter class.
20  */
21
22
23 #include <FBaseErrorDefine.h>
24 #include <FBaseSysLog.h>
25 #include <FGrp_CanvasImpl.h>
26 #include <FGrp_BitmapImpl.h>
27 #include <FGrp_TextTextSimple.h>
28 #include "FUi_UiTouchEvent.h"
29 #include "FUi_ResourceManager.h"
30 #include "FUi_AccessibilityContainer.h"
31 #include "FUi_AccessibilityElement.h"
32 #include "FUiCtrl_TabBarPresenter.h"
33
34
35 using namespace Tizen::Ui;
36 using namespace Tizen::Base;
37 using namespace Tizen::Graphics;
38 using namespace Tizen::Graphics::_Text;
39
40 namespace Tizen { namespace Ui { namespace Controls
41 {
42
43 _TabBarPresenter::_TabBarPresenter(_TabBar& tabBar)
44         : __tabBar(tabBar)
45         , __pTabBarModel(null)
46         , __pFont(null)
47         , __pTextObject(null)
48         , __highlightedItemIndex(-1)
49         , __touchMoved(false)
50         , __touchBubblingBlocked(true)
51         , __pBgBitmapCached(null)
52         , __pLeftArrowBgBitmapCached(null)
53         , __pRightArrowBgBitmapCached(null)
54         , __pLeftArrowBitmapCached(null)
55         , __pRightArrowBitmapCached(null)
56 {
57         __startPosition = Point(0, 0);
58         for (int i = 0; i < ITEM_STATUS_MAX; i++)
59         {
60                 __pItemBgBitmapCached[i] = null;
61         }
62 }
63
64 _TabBarPresenter::~_TabBarPresenter(void)
65 {
66         delete __pTabBarModel;
67         __pTabBarModel = null;
68
69         if (__pTextObject)
70         {
71                 __pTextObject->RemoveAll();
72                 delete __pTextObject;
73                 __pTextObject = null;
74         }
75
76         delete __pBgBitmapCached;
77         __pBgBitmapCached = null;
78
79         for (int i = 0; i < ITEM_STATUS_MAX; i++)
80         {
81                 delete __pItemBgBitmapCached[i];
82                 __pItemBgBitmapCached[i] = null;
83         }
84
85         delete __pLeftArrowBgBitmapCached;
86         __pLeftArrowBgBitmapCached = null;
87
88         delete __pRightArrowBgBitmapCached;
89         __pRightArrowBgBitmapCached = null;
90
91         delete __pLeftArrowBitmapCached;
92         __pLeftArrowBitmapCached = null;
93
94         delete __pRightArrowBitmapCached;
95         __pRightArrowBitmapCached = null;
96 }
97
98 result
99 _TabBarPresenter::Construct()
100 {
101         __pTabBarModel = new (std::nothrow) _TabBarModel;
102         SysTryReturn(NID_UI_CTRL, __pTabBarModel != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
103
104         result r = E_SUCCESS;
105
106         r = LoadBitmap();
107         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
108
109         return r;
110
111 CATCH:
112         delete __pTabBarModel;
113         __pTabBarModel = null;
114
115         return r;
116 }
117
118 result
119 _TabBarPresenter::InitializeFont(void)
120 {
121         result r = E_SUCCESS;
122         __pFont = __tabBar.GetFallbackFont();
123         SysTryCatch(NID_UI_CTRL, __pFont != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
124
125         __pTextObject = new (std::nothrow) TextObject();
126         SysTryCatch(NID_UI_CTRL, __pTextObject != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
127         __pTextObject->Construct();
128         __pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
129         __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
130
131         return E_SUCCESS;
132
133 CATCH:
134         delete __pTextObject;
135         __pTextObject = null;
136
137         return r;
138 }
139
140 void
141 _TabBarPresenter::OnFontChanged(Font* pFont)
142 {
143         __pFont = pFont;
144 }
145
146 void
147 _TabBarPresenter::OnFontInfoRequested(unsigned long& style, int& size)
148 {
149         int fontSize = 0;
150         GET_SHAPE_CONFIG(TABBAR::FONT_SIZE, __tabBar.GetOrientation(), fontSize);
151
152         style = FONT_STYLE_PLAIN;
153         size = fontSize;
154 }
155
156 result
157 _TabBarPresenter::LoadBitmap(void)
158 {
159         Bitmap* pBitmap = null;
160
161         Color arrowColor;
162         GET_COLOR_CONFIG(TABBAR::ARROW_NORMAL, arrowColor);
163
164         if (__pBgBitmapCached != null)
165         {
166                 delete __pBgBitmapCached;
167                 __pBgBitmapCached = null;
168         }
169
170         if (__pLeftArrowBgBitmapCached != null)
171         {
172                 delete __pLeftArrowBgBitmapCached;
173                 __pLeftArrowBgBitmapCached = null;
174         }
175
176         if (__pLeftArrowBitmapCached != null)
177         {
178                 delete __pLeftArrowBitmapCached;
179                 __pLeftArrowBitmapCached = null;
180         }
181
182         if (__pRightArrowBgBitmapCached == null)
183         {
184                 delete __pRightArrowBgBitmapCached;
185                 __pRightArrowBgBitmapCached = null;
186         }
187
188         if (__pRightArrowBitmapCached == null)
189         {
190                 delete __pRightArrowBitmapCached;
191                 __pRightArrowBitmapCached = null;
192         }
193
194         for (int i = 0; i < ITEM_STATUS_MAX; i++)
195         {
196                 if (__pItemBgBitmapCached[i] != null)
197                 {
198                         delete __pItemBgBitmapCached[i];
199                         __pItemBgBitmapCached[i] = null;
200                 }
201         }
202
203         result r;
204         r = GET_BITMAP_CONFIG_N(TABBAR::ARROW_BG_LEFT, BITMAP_PIXEL_FORMAT_ARGB8888, __pLeftArrowBgBitmapCached);
205         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "Failed to load bitmap.");
206
207         r = GET_BITMAP_CONFIG_N(TABBAR::ARROW_BG_RIGHT, BITMAP_PIXEL_FORMAT_ARGB8888, __pRightArrowBgBitmapCached);
208         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "Failed to load bitmap.");
209
210         r = GET_BITMAP_CONFIG_N(TABBAR::BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pBitmap);
211         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "Failed to load bitmap.");
212         __pBgBitmapCached = _BitmapImpl::GetColorReplacedBitmapN(*pBitmap, Color::GetColor(COLOR_ID_MAGENTA), __tabBar.GetColor());
213         delete pBitmap;
214         pBitmap = null;
215
216         r = GET_BITMAP_CONFIG_N(TABBAR::ARROW_LEFT, BITMAP_PIXEL_FORMAT_ARGB8888, pBitmap);
217         __pLeftArrowBitmapCached = _BitmapImpl::GetColorReplacedBitmapN(*pBitmap, Color::GetColor(COLOR_ID_MAGENTA), arrowColor);
218         SysTryCatch(NID_UI_CTRL, __pLeftArrowBitmapCached != null, , E_OUT_OF_MEMORY, "Failed to load bitmap.");
219         delete pBitmap;
220         pBitmap = null;
221
222         r = GET_BITMAP_CONFIG_N(TABBAR::ARROW_RIGHT, BITMAP_PIXEL_FORMAT_ARGB8888, pBitmap);
223         __pRightArrowBitmapCached = _BitmapImpl::GetColorReplacedBitmapN(*pBitmap, Color::GetColor(COLOR_ID_MAGENTA), arrowColor);
224         SysTryCatch(NID_UI_CTRL, __pRightArrowBitmapCached != null, , E_OUT_OF_MEMORY, "Failed to load bitmap.");
225         delete pBitmap;
226         pBitmap = null;
227
228         r = GET_BITMAP_CONFIG_N(TABBAR::ITEM_SELECTED, BITMAP_PIXEL_FORMAT_ARGB8888, pBitmap);
229         __pItemBgBitmapCached[ITEM_STATUS_SELECTED] = _BitmapImpl::GetColorReplacedBitmapN(*pBitmap, Color::GetColor(COLOR_ID_MAGENTA), __tabBar.GetItemColor(ITEM_STATUS_SELECTED));
230         SysTryCatch(NID_UI_CTRL, __pItemBgBitmapCached[ITEM_STATUS_SELECTED] != null, , E_OUT_OF_MEMORY, "Failed to load bitmap.");
231         delete pBitmap;
232         pBitmap = null;
233
234         r = GET_BITMAP_CONFIG_N(TABBAR::ITEM_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, pBitmap);
235         __pItemBgBitmapCached[ITEM_STATUS_HIGHLIGHTED] = _BitmapImpl::GetColorReplacedBitmapN(*pBitmap, Color::GetColor(COLOR_ID_MAGENTA), __tabBar.GetItemColor(ITEM_STATUS_HIGHLIGHTED));
236         SysTryCatch(NID_UI_CTRL, __pItemBgBitmapCached[ITEM_STATUS_HIGHLIGHTED] != null, , E_OUT_OF_MEMORY, "Failed to load bitmap.");
237         delete pBitmap;
238         pBitmap = null;
239
240         return E_SUCCESS;
241
242 CATCH:
243         delete pBitmap;
244
245         delete __pBgBitmapCached;
246         __pBgBitmapCached = null;
247
248         delete __pLeftArrowBgBitmapCached;
249         __pLeftArrowBgBitmapCached = null;
250
251         delete __pLeftArrowBitmapCached;
252         __pLeftArrowBitmapCached = null;
253
254         delete __pRightArrowBgBitmapCached;
255         __pRightArrowBgBitmapCached = null;
256
257         delete __pRightArrowBitmapCached;
258         __pRightArrowBitmapCached = null;
259
260         for (int i = 0; i < ITEM_STATUS_MAX; i++)
261         {
262                 delete __pItemBgBitmapCached[i];
263                 __pItemBgBitmapCached[i] = null;
264         }
265
266         return E_OUT_OF_MEMORY;
267 }
268
269 result
270 _TabBarPresenter::AddItem(const Tizen::Base::String& text, int actionId)
271 {
272         return __pTabBarModel->AddItem(text, actionId, _CONTROL_ORIENTATION_PORTRAIT);
273 }
274
275 result
276 _TabBarPresenter::InsertItemAt(int index, const Tizen::Base::String& text, int actionId)
277 {
278         if (index <= __pTabBarModel->GetSelectedItemIndex())
279         {
280                 if (__pTabBarModel->GetItemCount() != 0)
281                 {
282                         __pTabBarModel->SetSelectedItemIndex(GetSelectedItemIndex() + 1);
283                 }
284         }
285
286         result r = __pTabBarModel->InsertItemAt(index, text, actionId, _CONTROL_ORIENTATION_PORTRAIT);
287         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
288
289         return E_SUCCESS;
290 }
291
292 result
293 _TabBarPresenter::SetItemAt(int index, const Tizen::Base::String& text, int actionId)
294 {
295         return __pTabBarModel->SetItemAt(index, text, actionId, _CONTROL_ORIENTATION_PORTRAIT);
296 }
297
298 _TabBarItem*
299 _TabBarPresenter::GetItemAt(int index) const
300 {
301         _TabBarItem* pItem = __pTabBarModel->GetItemAt(index);
302         return pItem;
303 }
304
305 result
306 _TabBarPresenter::RemoveItemAt(int index)
307 {
308         if (index <= GetSelectedItemIndex())
309         {
310                 if (__pTabBarModel->GetItemCount() != 0 && GetSelectedItemIndex() != 0)
311                 {
312                         __pTabBarModel->SetSelectedItemIndex(GetSelectedItemIndex() - 1);
313                 }
314         }
315
316         result r = __pTabBarModel->RemoveItemAt(index, _CONTROL_ORIENTATION_PORTRAIT);
317         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
318
319         if (GetItemCount() != 0)
320         {
321                 r = SetItemSelected(0);
322                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
323         }
324         else
325         {
326                 __pTabBarModel->SetSelectedItemIndex(0);
327         }
328
329         return E_SUCCESS;
330 }
331
332 result
333 _TabBarPresenter::RemoveAllItems(void)
334 {
335         __pTabBarModel->RemoveAllItems();
336         return E_SUCCESS;
337 }
338
339 result
340 _TabBarPresenter::SetItemSelected(int index)
341 {
342
343         SysTryReturn(NID_UI_CTRL, index >= 0 && index < GetItemCount(), E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The argument is out of range.");
344
345         int oldSelectedItemIndex = GetSelectedItemIndex();
346         if (index == oldSelectedItemIndex)
347         {
348                 return E_SUCCESS;
349         }
350         _TabBarItem* pItem = GetItemAt(oldSelectedItemIndex);
351         result r = GetLastResult();
352         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
353         pItem->SetStatus(ITEM_STATUS_NORMAL);
354
355         pItem = GetItemAt(index);
356         r = GetLastResult();
357         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
358         pItem->SetStatus(ITEM_STATUS_SELECTED);
359
360         __pTabBarModel->SetSelectedItemIndex(index);
361         return E_SUCCESS;
362 }
363
364 int
365 _TabBarPresenter::GetSelectedItemIndex(void) const
366 {
367         return __pTabBarModel->GetSelectedItemIndex();
368 }
369
370 int
371 _TabBarPresenter::GetItemCount(void) const
372 {
373         return __pTabBarModel->GetItemCount();
374 }
375
376 int
377 _TabBarPresenter::GetItemIndexFromPosition(const Tizen::Graphics::Point& point) const
378 {
379         Rectangle clientBounds = __tabBar.GetBounds();
380         int topMargin = 0;
381         int sideMargin = 0;
382         GET_SHAPE_CONFIG(TABBAR::TOP_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, topMargin);
383         GET_SHAPE_CONFIG(TABBAR::SIDE_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, sideMargin);
384
385         if (point.x < sideMargin
386                 || point.x > clientBounds.width - sideMargin
387                 || point.y < topMargin
388                 || point.y > clientBounds.height - topMargin)
389         {
390                 return -1;
391         }
392
393         int itemIndex = __pTabBarModel->GetFirstDrawnItemIndex();
394         _TabBarItem* pItem = __pTabBarModel->GetItemAt(itemIndex);
395
396         while (pItem != null)
397         {
398                 Rectangle itemBounds = pItem->GetBounds();
399
400                 if (point.x - sideMargin > itemBounds.x
401                         && point.x - sideMargin < itemBounds.x + itemBounds.width
402                         && point.y - topMargin > itemBounds.y
403                         && point.y - topMargin < itemBounds.y + itemBounds.height)
404                 {
405                         return itemIndex;
406                 }
407                 itemIndex++;
408                 pItem = __pTabBarModel->GetItemAt(itemIndex);
409         }
410         return -1;
411 }
412
413 _TabBarItem*
414 _TabBarPresenter::GetItemFromPosition(const Tizen::Graphics::Point& position) const
415 {
416         SetLastResult(E_SUCCESS);
417         int index = GetItemIndexFromPosition(position);
418         SysTryReturn(NID_UI_CTRL, index != -1, null, E_SYSTEM, "[E_SYSTEM] Item not found.");
419
420         return __pTabBarModel->GetItemAt(index);
421 }
422
423 result
424 _TabBarPresenter::SetTopDrawnItemIndex(int index)
425 {
426         SysTryReturn(NID_UI_CTRL, index >= 0 && index < GetItemCount(),
427                         E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The argument(%d) is out of range.", index);
428
429         _TabBarItem* pTopItem = GetItemAt(index);
430         result r = GetLastResult();
431         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
432
433         int distance = pTopItem->GetBounds().x;
434
435         _TabBarItem* pBottomItem = GetItemAt(GetItemCount() - 1);
436         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
437
438         Rectangle clientBounds = __tabBar.GetBounds();
439         int sideMargin = 0;
440         GET_SHAPE_CONFIG(TABBAR::SIDE_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, sideMargin);
441
442         if ((pBottomItem->GetBounds().x + pBottomItem->GetBounds().width - distance) < clientBounds.width - sideMargin * 2)
443         {
444                 distance = (pBottomItem->GetBounds().x + pBottomItem->GetBounds().width) - (clientBounds.width - sideMargin * 2);
445         }
446
447         r = AdjustItemPositionX(-distance);
448         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
449
450         return E_SUCCESS;
451 }
452
453 result
454 _TabBarPresenter::SetItemStatus(int index, _TabBarItemStatus status)
455 {
456         _TabBarItem* pItem = __pTabBarModel->GetItemAt(index);
457         SysTryReturn(NID_UI_CTRL, pItem, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Item(%d) not exists.", index);
458
459         pItem->SetStatus(status);
460
461         return E_SUCCESS;
462 }
463
464 _TabBarItemStatus
465 _TabBarPresenter::GetItemStatus(int index) const
466 {
467         _TabBarItem* pItem = __pTabBarModel->GetItemAt(index);
468         SysTryReturn(NID_UI_CTRL, pItem, ITEM_STATUS_NORMAL, E_SYSTEM, "[E_SYSTEM] Item(%d) not exists.", index);
469
470         _TabBarItemStatus status = pItem->GetStatus();
471
472         return status;
473 }
474
475 result
476 _TabBarPresenter::AdjustItemPositionX(int distance)
477 {
478         int index = 0;
479
480         while (index < GetItemCount())
481         {
482                 _TabBarItem* pItem = __pTabBarModel->GetItemAt(index);
483                 SysTryReturn(NID_UI_CTRL, pItem, E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Item(%d) not exists.", index);
484                 Rectangle bounds = pItem->GetBounds();
485                 bounds.x += distance;
486                 pItem->SetBounds(bounds);
487                 index++;
488         }
489
490         return E_SUCCESS;
491 }
492
493 result
494 _TabBarPresenter::SetReplacementColor(const Tizen::Graphics::Color& color)
495 {
496         Bitmap* pBitmap = null;
497         Bitmap* pColorReplcedBitmap = null;
498
499         result r;
500         r = GET_BITMAP_CONFIG_N(TABBAR::BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pBitmap);
501         SysTryReturn(NID_UI_CTRL, pBitmap != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Image loading fail.");
502
503         pColorReplcedBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pBitmap, Color::GetColor(COLOR_ID_MAGENTA), color);
504         delete pBitmap;
505
506         if (__pBgBitmapCached != null)
507         {
508                 delete __pBgBitmapCached;
509         }
510         __pBgBitmapCached = pColorReplcedBitmap;
511
512         return E_SUCCESS;
513 }
514
515 result
516 _TabBarPresenter::SetItemReplacementColor(_TabBarItemStatus itemStatus, const Tizen::Graphics::Color& color)
517 {
518         Bitmap* pBitmap = null;
519         Bitmap* pColorReplcedBitmap = null;
520
521         result r;
522         if (itemStatus == ITEM_STATUS_SELECTED)
523         {
524                 r = GET_BITMAP_CONFIG_N(TABBAR::ITEM_SELECTED, BITMAP_PIXEL_FORMAT_ARGB8888, pBitmap);
525                 SysTryReturn(NID_UI_CTRL, pBitmap != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Image loading fail.");
526         }
527         else if (itemStatus == ITEM_STATUS_HIGHLIGHTED)
528         {
529                 r = GET_BITMAP_CONFIG_N(TABBAR::ITEM_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, pBitmap);
530                 SysTryReturn(NID_UI_CTRL, pBitmap != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Image loading fail.");
531         }
532         else
533         {
534                 return E_SUCCESS;
535         }
536
537         pColorReplcedBitmap = _BitmapImpl::GetColorReplacedBitmapN(*pBitmap, Color::GetColor(COLOR_ID_MAGENTA), color);
538         delete pBitmap;
539
540         if (__pItemBgBitmapCached[itemStatus] != null)
541         {
542                 delete __pItemBgBitmapCached[itemStatus];
543         }
544         __pItemBgBitmapCached[itemStatus] = pColorReplcedBitmap;
545
546         return E_SUCCESS;
547 }
548
549 void
550 _TabBarPresenter::Draw(void)
551 {
552         Canvas* pCanvas = __tabBar.GetCanvasN();
553         SysTryReturnVoidResult(NID_UI_CTRL, pCanvas != null, E_SYSTEM, "[E_SYSTEM] It can not get canvas.");
554
555         int sideMargin = 0;
556         GET_SHAPE_CONFIG(TABBAR::SIDE_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, sideMargin);
557
558         Rectangle clientBounds = __tabBar.GetBounds();
559
560         pCanvas->Clear();
561         DrawBackground(pCanvas);
562         DrawArrow(pCanvas);
563         delete pCanvas;
564
565         Rectangle rect(sideMargin, 0, clientBounds.width - sideMargin * 2, clientBounds.height);
566         Canvas* pSubCanvas = __tabBar.GetCanvasN(rect);
567
568         if (pSubCanvas != null)
569         {
570                 DrawItem(pSubCanvas);
571                 delete pSubCanvas;
572         }
573 }
574
575 void
576 _TabBarPresenter::InitItemPositionX(void)
577 {
578         int itemCount = GetItemCount();
579         if (itemCount < 1)
580         {
581                 return;
582         }
583
584         int sideMargin = 0;
585         GET_SHAPE_CONFIG(TABBAR::SIDE_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, sideMargin);
586
587         _TabBarItem* pFirstItem = __pTabBarModel->GetItemAt(0);
588         _TabBarItem* pLastItem  = __pTabBarModel->GetItemAt(itemCount - 1);
589         SysTryReturnVoidResult(NID_UI_CTRL, pFirstItem != null && pLastItem != null, E_SYSTEM, "[E_SYSTEM] Item not found.");
590
591         Rectangle clientBounds    = __tabBar.GetBounds();
592         Rectangle firstItemBounds = pFirstItem->GetBounds();
593         Rectangle lastItemBounds  = pLastItem->GetBounds();
594
595         if (firstItemBounds.x != 0 && lastItemBounds.x + lastItemBounds.width - firstItemBounds.x < clientBounds.width - sideMargin * 2)
596         {
597                 AdjustItemPositionX(-firstItemBounds.x);
598         }
599
600         return;
601 }
602
603 void
604 _TabBarPresenter::OnBoundsChanged(void)
605 {
606         return InitItemPositionX();
607 }
608
609 void
610 _TabBarPresenter::DrawItem(Tizen::Graphics::Canvas* pCanvas)
611 {
612         SysTryReturnVoidResult(NID_UI_CTRL, __pTextObject != null, E_INVALID_STATE, "[E_INVALID_STATE] TextObject is not constructed.");
613
614         Rectangle clientBounds = pCanvas->GetBounds();
615
616         _TabBarItem* pDrawItem = null;
617         Rectangle itemBounds(0, 0, 0, 0);
618
619         String drawText(L"");
620         Rectangle textRect(0, 0, 0, 0);
621         TextSimple* pSimpleText = null;
622
623         _TabBarItemStatus itemStatus = ITEM_STATUS_NORMAL;
624
625         int itemCount = GetItemCount();
626         for (int i = 0; i < itemCount; i++)
627         {
628                 pDrawItem = __pTabBarModel->GetItemAt(i);
629                 if (pDrawItem == null)
630                 {
631                         break;
632                 }
633
634                 itemBounds = pDrawItem->GetBounds();
635
636                 if (itemBounds.x + itemBounds.width < 0)
637                 {
638                         continue;
639                 }
640
641                 if (itemBounds.x > clientBounds.width)
642                 {
643                         break;
644                 }
645
646                 itemStatus = pDrawItem->GetStatus();
647
648                 // draw item BG
649                 if (__pItemBgBitmapCached[itemStatus] != null)
650                 {
651                         pCanvas->DrawNinePatchedBitmap(itemBounds, *__pItemBgBitmapCached[itemStatus]);
652                 }
653
654                 // draw item text
655                 textRect = Rectangle(itemBounds.x, itemBounds.y, itemBounds.width, itemBounds.height);
656                 if (textRect.x < 0)
657                 {
658                         textRect.width += textRect.x;
659                         textRect.x = 0;
660                 }
661
662                 if (textRect.x + textRect.width > clientBounds.width)
663                 {
664                         textRect.width = clientBounds.width - textRect.x;
665                 }
666
667                 drawText = pDrawItem->GetText();
668                 __pTextObject->RemoveAll();
669                 pSimpleText = new (std::nothrow)TextSimple(const_cast <wchar_t*>(drawText.GetPointer()), drawText.GetLength());
670                 SysTryReturnVoidResult(NID_UI_CTRL, pSimpleText != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] TextSimple is not constructed.");
671
672                 __pFont = __tabBar.GetFallbackFont();
673                 SysTryReturnVoidResult(NID_UI_CTRL, __pFont != null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
674
675                 __pTextObject->AppendElement(*pSimpleText);
676
677                 __pTextObject->SetForegroundColor(__tabBar.GetItemTextColor(itemStatus), 0, __pTextObject->GetTextLength());
678                 __pTextObject->SetBounds(textRect);
679                 __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
680                 __pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
681                 __pTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
682         } // end for loop
683 }
684
685 void
686 _TabBarPresenter::DrawBackground(Tizen::Graphics::Canvas* pCanvas)
687 {
688         Rectangle clientBounds = __tabBar.GetBounds();
689
690         if (__pBgBitmapCached != null)
691         {
692                 pCanvas->DrawNinePatchedBitmap(Rectangle(0, 0, clientBounds.width, clientBounds.height), *__pBgBitmapCached);
693         }
694 }
695
696 void
697 _TabBarPresenter::DrawArrow(Tizen::Graphics::Canvas* pCanvas)
698 {
699         Rectangle clientBounds = __tabBar.GetBounds();
700
701         _TabBarItem* pFirstItem = __pTabBarModel->GetItemAt(0);
702         if (pFirstItem == null)
703         {
704                 return;
705         }
706
707         Rectangle firstItemBounds = pFirstItem->GetBounds();
708         int sideMargin = 0;
709         int arrowMargin = 0;
710         GET_SHAPE_CONFIG(TABBAR::SIDE_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, sideMargin);
711         GET_SHAPE_CONFIG(TABBAR::ARROW_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, arrowMargin);
712
713         Rectangle drawRect(0, 0, 0, 0);
714         drawRect.width = sideMargin;
715         drawRect.height = clientBounds.height;
716
717         // draw left arrow
718         if (firstItemBounds.x < 0)
719         {
720                 drawRect.width = __pLeftArrowBitmapCached->GetWidth();
721                 drawRect.height = __pLeftArrowBitmapCached->GetHeight();
722                 drawRect.x = (sideMargin - __pLeftArrowBitmapCached->GetWidth()) / 2;
723                 drawRect.y = (clientBounds.height - __pLeftArrowBitmapCached->GetHeight()) / 2;
724                 DrawBitmap(*pCanvas, drawRect, *__pLeftArrowBitmapCached);
725         }
726
727         int itemCount = __pTabBarModel->GetItemCount();
728         _TabBarItem* pLastItem = __pTabBarModel->GetItemAt(itemCount - 1);
729         if (pLastItem == null)
730         {
731                 return;
732         }
733
734         Rectangle lastItemBounds = pLastItem->GetBounds();
735
736         drawRect.SetBounds(0, 0, 0, 0);
737         drawRect.x = clientBounds.width - sideMargin;
738         drawRect.width = sideMargin;
739         drawRect.height = clientBounds.height;
740
741         // draw right arrow
742         if (lastItemBounds.x + lastItemBounds.width > clientBounds.width - sideMargin * 2)
743         {
744                 drawRect.x += (sideMargin - __pRightArrowBitmapCached->GetWidth()) / 2;
745                 drawRect.y = (clientBounds.height - __pRightArrowBitmapCached->GetHeight()) / 2;
746                 drawRect.width = __pRightArrowBitmapCached->GetWidth();
747                 drawRect.height = __pRightArrowBitmapCached->GetHeight();
748                 DrawBitmap(*pCanvas, drawRect, *__pRightArrowBitmapCached);
749         }
750 }
751
752 bool
753 _TabBarPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
754 {
755         __startPosition = touchinfo.GetCurrentPosition();
756         __touchBubblingBlocked = true;
757
758         int index = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
759         if (index == -1)
760         {
761                 return true;
762         }
763
764         __highlightedItemIndex = index;
765         if (GetItemStatus(index) == ITEM_STATUS_SELECTED)
766         {
767                 return true;
768         }
769
770         result r = SetItemStatus(index, ITEM_STATUS_HIGHLIGHTED);
771         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, true, r, "[%s] propagating.", GetErrorMessage(r));
772
773         Draw();
774
775         return true;
776 }
777
778 bool
779 _TabBarPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
780 {
781         int index = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
782         int selectedItemIndex = GetSelectedItemIndex();
783         bool eventFire = false;
784         result r = E_SUCCESS;
785
786
787         if (index != -1 && GetItemStatus(index) != ITEM_STATUS_NORMAL && __touchMoved == false)
788         {
789                 if (GetItemStatus(index) == ITEM_STATUS_HIGHLIGHTED)
790                 {
791                         r = SetItemStatus(index, ITEM_STATUS_SELECTED);
792                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] propagating.", GetErrorMessage(r));
793                         SetItemSelected(index);
794                         SetItemStatus(selectedItemIndex, ITEM_STATUS_NORMAL);
795                         eventFire = true;
796                 }
797         }
798         else
799         {
800                 for (int i = 0; i < GetItemCount(); i++)
801                 {
802                         if (GetItemStatus(i) == ITEM_STATUS_HIGHLIGHTED)
803                         {
804                                 r = SetItemStatus(i, ITEM_STATUS_NORMAL);
805                                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] propagating.", GetErrorMessage(r));
806                         }
807                 }
808         }
809         __highlightedItemIndex = -1;
810         __touchMoved = false;
811
812         Draw();
813
814         return eventFire;
815 }
816
817 bool
818 _TabBarPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
819 {
820         if (&source != &__tabBar)
821         {
822                 return false;
823         }
824
825         int sideMargin = 0;
826         GET_SHAPE_CONFIG(TABBAR::SIDE_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, sideMargin);
827         int arrowMargin = 0;
828         GET_SHAPE_CONFIG(TABBAR::ARROW_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, arrowMargin);
829
830         Rectangle clientBounds = __tabBar.GetBounds();
831         if (__pTabBarModel->GetWidthOfAllItems() + (sideMargin * 2) <= clientBounds.width + arrowMargin * 2)
832         {
833                 return true;
834         }
835
836         if (__touchMoved == false)
837         {
838                 int distanceX = abs((touchinfo.GetCurrentPosition()).x - __startPosition.x);
839                 int distanceY = abs((touchinfo.GetCurrentPosition()).y - __startPosition.y);
840
841                 if (distanceY > distanceX)
842                 {
843                         __touchBubblingBlocked = false;
844                 }
845         }
846
847         __touchMoved = true;
848
849         int distance = (touchinfo.GetCurrentPosition()).x - __startPosition.x;
850
851         __startPosition = touchinfo.GetCurrentPosition();
852         _TabBarItem* pItem = null;
853         Rectangle itemBounds;
854         int itemCount = GetItemCount();
855
856         if (distance > 0) // right move
857         {
858                 pItem = __pTabBarModel->GetItemAt(0);
859                 if (pItem == null)
860                 {
861                         return true;
862                 }
863
864                 itemBounds = pItem->GetBounds();
865
866                 if (itemBounds.x == arrowMargin)
867                 {
868                         return true;
869                 }
870
871                 if (itemBounds.x + distance >= arrowMargin)
872                 {
873                         distance = arrowMargin - itemBounds.x;
874                 }
875         }
876         else // left move
877         {
878                 pItem = __pTabBarModel->GetItemAt(itemCount - 1);
879                 if (pItem == null)
880                 {
881                         return true;
882                 }
883
884                 itemBounds = pItem->GetBounds();
885                 if (itemBounds.x + itemBounds.width + (arrowMargin * 2) == clientBounds.width - (sideMargin * 2))
886                 {
887                         return true;
888                 }
889
890                 if (itemBounds.x + itemBounds.width + distance + (arrowMargin * 2) <= clientBounds.width - (sideMargin * 2))
891                 {
892                         distance = (clientBounds.width - (sideMargin * 2)) - (itemBounds.x + itemBounds.width + (arrowMargin * 2));
893                 }
894         }
895
896         AdjustItemPositionX(distance);
897
898         Draw();
899
900         return __touchBubblingBlocked;
901 }
902
903 result
904 _TabBarPresenter::DrawBitmap(Tizen::Graphics::Canvas& canvas, const Tizen::Graphics::Rectangle& bounds, const Tizen::Graphics::Bitmap& bitmap)
905 {
906         result r = E_SUCCESS;
907         if (bitmap.IsNinePatchedBitmap())
908         {
909                 r = canvas.DrawNinePatchedBitmap(bounds, bitmap);
910                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Fail to draw ninepatched bitmap.")
911         }
912         else
913         {
914                 r = canvas.DrawBitmap(bounds, bitmap);
915                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Fail to draw bitmap.")
916         }
917
918         return r;
919 }
920
921 void
922 _TabBarPresenter::SetAllAccessibilityElement(void)
923 {
924         _AccessibilityContainer* pContainer = __tabBar.GetAccessibilityContainer();
925         int sideMargin = 0;
926         GET_SHAPE_CONFIG(TABBAR::SIDE_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, sideMargin);
927
928         if (pContainer != null)
929         {
930                 int itemCount = __pTabBarModel->GetItemCount();
931                 for (int i = 0; i < itemCount; i++)
932                 {
933                         _TabBarItem* pItem = __pTabBarModel->GetItemAt(i);
934                         _AccessibilityElement* pElement = new (std::nothrow) _AccessibilityElement(true);
935                         if (pItem != null && pElement != null)
936                         {
937                                 pElement->SetLabel(pItem->GetText());
938 //                              pElement->SetTrait(ACCESSIBILITY_TRAITS_TABBAR);
939                                 pElement->SetTrait(ACCESSIBILITY_TRAITS_TAB);   // not yet
940                                 Rectangle itemBounds = pItem->GetBounds();
941                                 itemBounds.x += sideMargin;
942                                 pElement->SetBounds(itemBounds);
943
944                                 if (i == 0)
945                                 {
946                                         pElement->SetHint(L"The first of TabBar");
947                                 }
948                                 else if(i == itemCount - 1)
949                                 {
950                                         pElement->SetHint(L"The last of TabBar");
951                                 }
952                                 pContainer->AddElement(*pElement);
953                                 __tabBar.AddAccessibilityElement(*pElement);
954                         }
955                         else
956                         {
957                                 delete pElement;
958                         }
959                 }
960         }
961
962 }
963
964 }}} // Tizen::Ui::Controls