2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0/
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.
18 * @file FUiCtrl_TabBar.cpp
19 * @brief This is the implementation file for the _TabBar class.
23 #include <FBaseErrorDefine.h>
24 #include <FBaseSysLog.h>
25 #include "FUi_AccessibilityContainer.h"
26 #include "FUi_AccessibilityElement.h"
27 #include "FUi_CoordinateSystemUtils.h"
28 #include "FUi_ResourceManager.h"
29 #include "FUi_UiTouchEvent.h"
31 #include "FUiCtrl_TabBar.h"
33 using namespace Tizen::Base;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Base::Runtime;
37 namespace Tizen { namespace Ui { namespace Controls
40 IMPLEMENT_PROPERTY(_TabBar);
43 : __pTabBarPresenter(null)
44 , __pActionEvent(null)
46 GET_COLOR_CONFIG(TABBAR::BG_NORMAL, __color);
48 GET_COLOR_CONFIG(TABBAR::ITEM_BG_NORMAL, __itemColor[ITEM_STATUS_NORMAL]);
49 GET_COLOR_CONFIG(TABBAR::ITEM_BG_SELECTED, __itemColor[ITEM_STATUS_SELECTED]);
50 GET_COLOR_CONFIG(TABBAR::ITEM_BG_PRESSED, __itemColor[ITEM_STATUS_PRESSED]);
51 GET_COLOR_CONFIG(TABBAR::ITEM_BG_DISABLED, __itemColor[ITEM_STATUS_DISABLED]);
53 GET_COLOR_CONFIG(TABBAR::ITEM_TEXT_NORMAL, __itemTextColor[ITEM_STATUS_NORMAL]);
54 GET_COLOR_CONFIG(TABBAR::ITEM_TEXT_SELECTED, __itemTextColor[ITEM_STATUS_SELECTED]);
55 GET_COLOR_CONFIG(TABBAR::ITEM_TEXT_PRESSED, __itemTextColor[ITEM_STATUS_PRESSED]);
56 GET_COLOR_CONFIG(TABBAR::ITEM_TEXT_DISABLED, __itemTextColor[ITEM_STATUS_DISABLED]);
59 _TabBar::~_TabBar(void)
61 delete __pTabBarPresenter;
62 __pTabBarPresenter = null;
66 delete __pActionEvent;
67 __pActionEvent = null;
70 RemoveAllAccessibilityElement();
74 _TabBar::CreateTabBarN(void)
76 _TabBar* pTabBar = new (std::nothrow) _TabBar();
77 SysTryReturn(NID_UI_CTRL, pTabBar != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
79 result r = pTabBar->Initialize();
80 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to Initialize.", GetErrorMessage(r));
82 pTabBar->AcquireHandle();
83 pTabBar->SetTouchPressThreshold(0.08f);
85 if (pTabBar->GetAccessibilityContainer() != null)
87 pTabBar->GetAccessibilityContainer()->Activate(true);
88 pTabBar->GetAccessibilityContainer()->AddListener(*pTabBar);
100 _TabBar::Initialize(void)
102 _TabBarPresenter* pTabBarPresenter = new (std::nothrow) _TabBarPresenter(*this);
103 SysTryReturn(NID_UI_CTRL, pTabBarPresenter != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
105 result r = pTabBarPresenter->Construct();
106 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
108 r = SetPresenter(*pTabBarPresenter);
109 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
111 r = pTabBarPresenter->InitializeFont();
112 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
117 __pTabBarPresenter = null;
119 delete pTabBarPresenter;
125 _TabBar::SetPresenter(const _TabBarPresenter& tabBarPresenter)
127 __pTabBarPresenter = const_cast<_TabBarPresenter*>(&tabBarPresenter);
133 _TabBar::AddItem(const Tizen::Base::String& text, int actionId)
135 return __pTabBarPresenter->AddItem(text, actionId);
139 _TabBar::InsertItemAt(int index, const Tizen::Base::String& text, int actionId)
141 return __pTabBarPresenter->InsertItemAt(index, text, actionId);
145 _TabBar::SetItemAt(int index, const Tizen::Base::String& text, int actionId)
147 return __pTabBarPresenter->SetItemAt(index, text, actionId);
151 _TabBar::RemoveItemAt(int index)
153 return __pTabBarPresenter->RemoveItemAt(index);
157 _TabBar::RemoveAllItems(void)
159 return __pTabBarPresenter->RemoveAllItems();
163 _TabBar::SetPropertyColor(const Variant& color)
165 result r = E_SUCCESS;
167 r = __pTabBarPresenter->SetReplacementColor(color.ToColor());
171 __color = color.ToColor();
178 _TabBar::GetPropertyColor(void) const
180 return Tizen::Ui::Variant(__color);
184 _TabBar::SetColor(Tizen::Graphics::Color color)
186 return SetProperty("color", Variant(color));
189 Tizen::Graphics::Color
190 _TabBar::GetColor(void) const
192 Variant color = GetProperty("color");
194 return color.ToColor();
198 _TabBar::SetPropertyNormalItemColor(const Variant& color)
200 result r = E_SUCCESS;
202 r = __pTabBarPresenter->SetItemReplacementColor(ITEM_STATUS_NORMAL, color.ToColor());
205 __itemColor[ITEM_STATUS_NORMAL] = color.ToColor();
212 _TabBar::GetPropertyNormalItemColor(void) const
214 return Tizen::Ui::Variant(__itemColor[ITEM_STATUS_NORMAL]);
218 _TabBar::SetPropertySelectedItemColor(const Variant& color)
220 result r = E_SUCCESS;
222 r = __pTabBarPresenter->SetItemReplacementColor(ITEM_STATUS_SELECTED, color.ToColor());
225 __itemColor[ITEM_STATUS_SELECTED] = color.ToColor();
232 _TabBar::GetPropertySelectedItemColor(void) const
234 return Tizen::Ui::Variant(__itemColor[ITEM_STATUS_SELECTED]);
238 _TabBar::SetPropertyPressedItemColor(const Variant& color)
240 result r = E_SUCCESS;
242 r = __pTabBarPresenter->SetItemReplacementColor(ITEM_STATUS_PRESSED, color.ToColor());
245 __itemColor[ITEM_STATUS_PRESSED] = color.ToColor();
252 _TabBar::GetPropertyPressedItemColor(void) const
254 return Tizen::Ui::Variant(__itemColor[ITEM_STATUS_PRESSED]);
258 _TabBar::SetPropertyDisabledItemColor(const Variant& color)
260 result r = E_SUCCESS;
262 r = __pTabBarPresenter->SetItemReplacementColor(ITEM_STATUS_DISABLED, color.ToColor());
265 __itemColor[ITEM_STATUS_DISABLED] = color.ToColor();
272 _TabBar::GetPropertyDisabledItemColor(void) const
274 return Tizen::Ui::Variant(__itemColor[ITEM_STATUS_DISABLED]);
278 _TabBar::SetItemColor(_TabBarItemStatus itemStatus, Tizen::Graphics::Color color)
280 result r = E_SUCCESS;
282 if (itemStatus == ITEM_STATUS_NORMAL)
284 r = SetProperty("normalItemColor", Variant(color));
286 else if (itemStatus == ITEM_STATUS_SELECTED)
288 r = SetProperty("selectedItemColor", Variant(color));
290 else if (itemStatus == ITEM_STATUS_PRESSED)
292 r = SetProperty("pressedItemColor", Variant(color));
296 r = SetProperty("disabledItemColor", Variant(color));
302 Tizen::Graphics::Color
303 _TabBar::GetItemColor(_TabBarItemStatus itemStatus) const
307 if (itemStatus == ITEM_STATUS_NORMAL)
309 Variant color = GetProperty("normalItemColor");
310 itemColor = color.ToColor();
312 else if (itemStatus == ITEM_STATUS_SELECTED)
314 Variant color = GetProperty("selectedItemColor");
315 itemColor = color.ToColor();
317 else if (itemStatus == ITEM_STATUS_PRESSED)
319 Variant color = GetProperty("pressedItemColor");
320 itemColor = color.ToColor();
324 Variant color = GetProperty("disabledItemColor");
325 itemColor = color.ToColor();
332 _TabBar::SetPropertyNormalItemTextColor(const Variant & color)
334 __itemTextColor[ITEM_STATUS_NORMAL] = color.ToColor();
340 _TabBar::GetPropertyNormalItemTextColor(void) const
342 return Tizen::Ui::Variant(__itemTextColor[ITEM_STATUS_NORMAL]);
346 _TabBar::SetPropertySelectedItemTextColor(const Variant & color)
348 __itemTextColor[ITEM_STATUS_SELECTED] = color.ToColor();
354 _TabBar::GetPropertySelectedItemTextColor(void) const
356 return Tizen::Ui::Variant(__itemTextColor[ITEM_STATUS_SELECTED]);
360 _TabBar::SetPropertyPressedItemTextColor(const Variant & color)
362 __itemTextColor[ITEM_STATUS_PRESSED] = color.ToColor();
368 _TabBar::GetPropertyPressedItemTextColor(void) const
370 return Tizen::Ui::Variant(__itemTextColor[ITEM_STATUS_PRESSED]);
374 _TabBar::SetPropertyDisabledItemTextColor(const Variant & color)
376 __itemTextColor[ITEM_STATUS_DISABLED] = color.ToColor();
382 _TabBar::GetPropertyDisabledItemTextColor(void) const
384 return Tizen::Ui::Variant(__itemTextColor[ITEM_STATUS_DISABLED]);
388 _TabBar::SetItemTextColor(_TabBarItemStatus status, Tizen::Graphics::Color color)
391 result r = E_SUCCESS;
393 if (status == ITEM_STATUS_NORMAL)
395 r = SetProperty("normalItemTextColor", Variant(color));
397 else if (status == ITEM_STATUS_SELECTED)
399 r = SetProperty("selectedItemTextColor", Variant(color));
403 r = SetProperty("pressedItemTextColor", Variant(color));
408 SysLogException(NID_UI_CTRL, E_INVALID_STATE, "Color is invalid.");
413 Tizen::Graphics::Color
414 _TabBar::GetItemTextColor(_TabBarItemStatus status) const
418 if (status == ITEM_STATUS_NORMAL)
420 Variant color = GetProperty("normalItemTextColor");
421 itemTextColor = color.ToColor();
423 else if (status == ITEM_STATUS_SELECTED)
425 Variant color = GetProperty("selectedItemTextColor");
426 itemTextColor = color.ToColor();
428 else if (status == ITEM_STATUS_PRESSED)
430 Variant color = GetProperty("pressedItemTextColor");
431 itemTextColor = color.ToColor();
435 Variant color = GetProperty("disabledItemTextColor");
436 itemTextColor = color.ToColor();
439 return itemTextColor;
443 _TabBar::SetItemSelected(int index)
445 return __pTabBarPresenter->SetItemSelected(index);
449 _TabBar::GetSelectedItemIndex(void) const
451 return __pTabBarPresenter->GetSelectedItemIndex();
455 _TabBar::GetItemCount(void) const
457 return __pTabBarPresenter->GetItemCount();
461 _TabBar::GetItemFromPosition(const Tizen::Graphics::FloatPoint& position) const
463 return __pTabBarPresenter->GetItemFromPosition(position);
467 _TabBar::SetTopDrawnItemIndex(int index)
469 return __pTabBarPresenter->SetTopDrawnItemIndex(index);
473 _TabBar::SetWidth(float width)
476 GET_SHAPE_CONFIG(TABBAR::HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, height);
478 result r = SetSize(FloatDimension(width, height));
479 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
480 __pTabBarPresenter->ResetItemPositionX();
486 _TabBar::AddActionEventListener(const Tizen::Ui::Controls::_IActionEventListener& listener)
490 if (__pActionEvent == null)
492 __pActionEvent = _ActionEvent::CreateInstanceN(*this);
494 if (__pActionEvent == null || IsFailed(GetLastResult()))
496 SysLog(NID_UI_CTRL, "[E_INVALID_STATE] System error occured.");
497 SetLastResult(E_INVALID_STATE);
498 delete __pActionEvent;
503 SetLastResult(E_SUCCESS);
504 __pActionEvent->AddListener(listener);
508 _TabBar::RemoveActionEventListener(const Tizen::Ui::Controls::_IActionEventListener& listener)
510 SysTryReturnVoidResult(NID_UI_CTRL, __pActionEvent != null, E_INVALID_STATE, "[E_INVALID_STATE] This instance isn't constructed.")
511 // Todo : check fail case of RemoveListener
512 __pActionEvent->RemoveListener(listener);
516 _TabBar::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
518 return __pTabBarPresenter->OnTouchPressed(source, touchinfo);
522 _TabBar::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
524 bool eventFire = __pTabBarPresenter->OnTouchReleased(source, touchinfo);
525 FloatPoint touchPosition = touchinfo.GetCurrentPosition();
527 if (eventFire == true)
529 int index = __pTabBarPresenter->GetItemIndexFromPosition(touchPosition);
530 _TabBarItem* pItem = __pTabBarPresenter->GetItemAt(index);
535 IEventArg* pEventArg = _ActionEvent::CreateActionEventArgN(pItem->GetActionId());
536 SysTryReturn(NID_UI_CTRL, pEventArg != null, E_INVALID_STATE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
538 PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_TAP, this);
539 __pActionEvent->Fire(*pEventArg);
548 _TabBar::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
550 return __pTabBarPresenter->OnTouchCanceled(source, touchinfo);
554 _TabBar::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
556 return __pTabBarPresenter->OnTouchMoved(source, touchinfo);
560 _TabBar::OnFontChanged(Font * pFont)
562 __pTabBarPresenter->OnFontChanged(pFont);
566 _TabBar::OnFontInfoRequested(unsigned long& style, float& size)
568 __pTabBarPresenter->OnFontInfoRequested(style, size);
572 _TabBar::OnDraw(void)
574 __pTabBarPresenter->Draw();
575 SetAllAccessibilityElement();
579 _TabBar::OnBoundsChanged(void)
581 __pTabBarPresenter->OnBoundsChanged();
585 _TabBar::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
591 _KeyCode keyCode = keyInfo.GetKeyCode();
592 int itemCount = __pTabBarPresenter->GetItemCount();
593 if (itemCount == 0 || __isInFocusMode == false)
598 if (keyCode == _KEY_RIGHT)
600 if (__currentPressedItemIndex < (itemCount - 1))
602 if (__pTabBarPresenter->GetItemStatus(__currentPressedItemIndex) != ITEM_STATUS_SELECTED)
604 __pTabBarPresenter->SetItemStatus(__currentPressedItemIndex, ITEM_STATUS_NORMAL);
607 __currentPressedItemIndex++;
609 if (__pTabBarPresenter->GetItemStatus(__currentPressedItemIndex) != ITEM_STATUS_SELECTED)
611 __pTabBarPresenter->SetItemStatus(__currentPressedItemIndex, ITEM_STATUS_PRESSED);
613 __pTabBarPresenter->ShiftToFocusedItem(__currentPressedItemIndex, _FOCUS_DIRECTION_MOVE_RIGHT);
617 else if (keyCode == _KEY_LEFT)
619 if (__currentPressedItemIndex >= -1)
621 if (__pTabBarPresenter->GetItemStatus(__currentPressedItemIndex) != ITEM_STATUS_SELECTED)
623 __pTabBarPresenter->SetItemStatus(__currentPressedItemIndex, ITEM_STATUS_NORMAL);
625 if (__currentPressedItemIndex == -1)
627 __currentPressedItemIndex = 0;
629 if (__currentPressedItemIndex > 0)
631 __currentPressedItemIndex--;
633 if (__pTabBarPresenter->GetItemStatus(__currentPressedItemIndex) != ITEM_STATUS_SELECTED)
635 __pTabBarPresenter->SetItemStatus(__currentPressedItemIndex, ITEM_STATUS_PRESSED);
637 __pTabBarPresenter->ShiftToFocusedItem(__currentPressedItemIndex, _FOCUS_DIRECTION_MOVE_LEFT);
641 else if (keyCode == _KEY_ENTER && __currentPressedItemIndex != -1)
643 __pTabBarPresenter->SetItemStatus(__currentPressedItemIndex, ITEM_STATUS_SELECTED);
644 __pTabBarPresenter->SetItemSelected(__currentPressedItemIndex);
646 _TabBarItem* pItem = __pTabBarPresenter->GetItemAt(__currentPressedItemIndex);
651 IEventArg* pEventArg = _ActionEvent::CreateActionEventArgN(pItem->GetActionId());
652 SysTryReturn(NID_UI_CTRL, pEventArg != null, E_INVALID_STATE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
654 PLAY_FEEDBACK(_RESOURCE_FEEDBACK_PATTERN_TAP, this);
655 __pActionEvent->Fire(*pEventArg);
662 else if (keyCode == _KEY_DOWN || keyCode == _KEY_UP || keyCode == _KEY_TAB)
664 if (__pTabBarPresenter->GetItemStatus(__currentPressedItemIndex) == ITEM_STATUS_PRESSED)
666 __pTabBarPresenter->SetItemStatus(__currentPressedItemIndex, ITEM_STATUS_NORMAL);
675 _TabBar::OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
681 _TabBar::OnDrawFocus(void)
683 if (__pTabBarPresenter->GetItemCount() > 0)
685 __currentPressedItemIndex = 0;
689 __currentPressedItemIndex = -1;
692 if (__pTabBarPresenter->GetItemStatus(__currentPressedItemIndex) != ITEM_STATUS_SELECTED)
694 __pTabBarPresenter->SetItemStatus(__currentPressedItemIndex, ITEM_STATUS_PRESSED);
697 __pTabBarPresenter->ShiftToFocusedItem(__currentPressedItemIndex, _FOCUS_DIRECTION_MOVE_LEFT);
698 __isInFocusMode = true;
704 _TabBar::OnFocusModeStateChanged(void)
706 if (__pTabBarPresenter->GetItemStatus(__currentPressedItemIndex) == ITEM_STATUS_PRESSED)
708 __pTabBarPresenter->SetItemStatus(__currentPressedItemIndex, ITEM_STATUS_NORMAL);
711 __currentPressedItemIndex = -1;
712 __isInFocusMode = false;
717 _AccessibilityElement*
718 _TabBar::GetAccessibilityElement(const int mainIndex) const
720 _AccessibilityElement* pElement = null;
721 result r = __accessibilityElements.GetAt(mainIndex, pElement);
722 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed at AccessibiltyElement.", GetErrorMessage(r));
733 _TabBar::AddAccessibilityElement(const _AccessibilityElement& element)
735 __accessibilityElements.Add(const_cast<_AccessibilityElement*>(&element));
739 _TabBar::SetAllAccessibilityElement(void)
741 __pTabBarPresenter->SetAllAccessibilityElement();
745 _TabBar::RemoveAllAccessibilityElement(void)
747 while (__accessibilityElements.GetCount() > 0)
749 _AccessibilityElement* pElement = null;
750 if (__accessibilityElements.GetAt(0, pElement) == E_SUCCESS)
752 pElement->GetParent()->RemoveElement(*pElement);
753 __accessibilityElements.RemoveAt(0);
759 _TabBar::OnAccessibilityFocusMovedNext(const _AccessibilityContainer& control, const _AccessibilityElement& element)
761 return __pTabBarPresenter->OnAccessibilityFocusMovedNext(control, element);
765 _TabBar::OnAccessibilityFocusMovedPrevious(const _AccessibilityContainer& control, const _AccessibilityElement& element)
767 return __pTabBarPresenter->OnAccessibilityFocusMovedPrevious(control, element);
771 _TabBar::OnAccessibilityReadElement(const _AccessibilityContainer& control, const _AccessibilityElement& element)
773 return __pTabBarPresenter->OnAccessibilityReadElement(control, element);
777 _TabBar::OnAccessibilityReadingElement(const _AccessibilityContainer& control, const _AccessibilityElement& element)
779 return __pTabBarPresenter->OnAccessibilityReadingElement(control, element);
783 _TabBar::OnAccessibilityFocusIn(const _AccessibilityContainer& control, const _AccessibilityElement& element)
785 return __pTabBarPresenter->OnAccessibilityFocusIn(control, element);
789 _TabBar::OnAccessibilityFocusOut(const _AccessibilityContainer& control, const _AccessibilityElement& element)
791 return __pTabBarPresenter->OnAccessibilityFocusOut(control, element);
795 _TabBar::OnAccessibilityActionPerformed(const _AccessibilityContainer& control, const _AccessibilityElement& element)
797 return __pTabBarPresenter->OnAccessibilityActionPerformed(control, element);
801 _TabBar::OnAccessibilityValueIncreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
803 return __pTabBarPresenter->OnAccessibilityValueIncreased(control, element);
807 _TabBar::OnAccessibilityValueDecreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
809 return __pTabBarPresenter->OnAccessibilityValueDecreased(control, element);
813 _TabBar::OnAccessibilityItemRefreshed(const _AccessibilityContainer& control, const _AccessibilityElement& element, _AccessibilityFocusDirection direction)
815 return __pTabBarPresenter->OnAccessibilityItemRefreshed(control, element, direction);
818 }}} // Tizen::Ui::Controls