2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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_TabBarModel.cpp
19 * @brief This is the implementation file for the _TabBarModel class.
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include "FUiCtrl_TabBarModel.h"
27 using namespace Tizen::Graphics;
29 namespace Tizen { namespace Ui { namespace Controls
32 _TabBarModel::_TabBarModel(void)
33 : __selectedItemIndex(0)
34 , __firstDrawnItemIndex(0)
35 , __widthOfAllItems(0)
37 __tabBarItems.Construct();
40 _TabBarModel::~_TabBarModel(void)
42 __tabBarItems.RemoveAll(true);
46 _TabBarModel::SetSelectedItemIndex(int index)
48 __selectedItemIndex = index;
52 _TabBarModel::GetSelectedItemIndex(void) const
54 return __selectedItemIndex;
58 _TabBarModel::SetFirstDrawnItemIndex(int index)
60 __firstDrawnItemIndex = index;
64 _TabBarModel::GetFirstDrawnItemIndex(void) const
66 return __firstDrawnItemIndex;
70 _TabBarModel::GetItemCount(void) const
72 return __tabBarItems.GetCount();
76 _TabBarModel::GetWidthOfAllItems(void) const
78 return __widthOfAllItems;
82 _TabBarModel::AddItem(const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
84 _TabBarItem* pItem = new (std::nothrow) _TabBarItem(orientation);
85 SysTryReturn(NID_UI_CTRL, pItem != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
87 pItem->SetText(text, orientation);
88 pItem->SetActionId(actionId);
89 int itemCount = __tabBarItems.GetCount();
93 pItem->SetStatus(ITEM_STATUS_NORMAL);
97 pItem->SetStatus(ITEM_STATUS_SELECTED);
100 _TabBarItem* pPreviousItem = GetItemAt(itemCount - 1);
101 Rectangle itemBounds = pItem->GetBounds();
102 if (pPreviousItem != null)
104 Rectangle previousItemBounds = pPreviousItem->GetBounds();
106 GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
107 itemBounds.x = previousItemBounds.x + previousItemBounds.width + itemMargin;
108 __widthOfAllItems += itemMargin;
113 GET_SHAPE_CONFIG(TABBAR::ARROW_MARGIN, orientation, arrowMargin);
114 itemBounds.x += arrowMargin;
116 pItem->SetBounds(itemBounds);
117 __widthOfAllItems += itemBounds.width;
119 return __tabBarItems.Add(*pItem);
123 _TabBarModel::InsertItemAt(int index, const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
125 SysTryReturn(NID_UI_CTRL, index >= 0 && index <= GetItemCount(), E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The argument(%d) is out of range.", index);
126 if (GetItemCount() == 0)
128 return AddItem(text, actionId, orientation);
131 _TabBarItem* pItem = new (std::nothrow) _TabBarItem(orientation);
132 SysTryReturn(NID_UI_CTRL, pItem != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
134 pItem->SetText(text, orientation);
135 pItem->SetActionId(actionId);
137 _TabBarItem* pPreviousItem = GetItemAt(index - 1);
138 Rectangle itemBounds = pItem->GetBounds();
139 if (pPreviousItem != null)
141 Rectangle previousItemBounds = pPreviousItem->GetBounds();
143 GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
144 itemBounds.x = previousItemBounds.x + previousItemBounds.width + itemMargin;
145 pItem->SetBounds(itemBounds);
148 result r = __tabBarItems.InsertAt(*pItem, index);
149 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
151 r = ReCalculateItemPosition(index + 1, orientation);
152 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
158 _TabBarModel::SetItemAt(int index, const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
160 _TabBarItem* pItem = static_cast <_TabBarItem*>(__tabBarItems.GetAt(index));
161 SysTryReturn(NID_UI_CTRL, pItem != null, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The %d index item is not exist.", index);
163 pItem->SetText(text, orientation);
164 pItem->SetActionId(actionId);
166 result r = ReCalculateItemPosition(index + 1, orientation);
167 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
173 _TabBarModel::RemoveItemAt(int index, _ControlOrientation orientation)
175 _TabBarItem* pItem = static_cast <_TabBarItem*>(__tabBarItems.GetAt(index));
176 SysTryReturn(NID_UI_CTRL, pItem != null, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The %d index item is not exist.", index);
179 GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
181 result r = __tabBarItems.RemoveAt(index, true);
182 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
184 r = ReCalculateItemPosition(index, orientation);
185 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
191 _TabBarModel::RemoveAllItems(void)
193 __widthOfAllItems = 0;
194 __tabBarItems.RemoveAll(true);
198 _TabBarModel::GetItemAt(int index) const
200 const _TabBarItem* pItem = static_cast <const _TabBarItem*>(__tabBarItems.GetAt(index));
202 return const_cast<_TabBarItem*>(pItem);
203 // return static_cast <_TabBarItem*>(__tabBarItems.GetAt(index));
207 _TabBarModel::ReCalculateItemPosition(int itemIndex, _ControlOrientation orientation)
209 SysTryReturn(NID_UI_CTRL, itemIndex >= 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The itemIndex(%d) is negative.");
210 int itemCount = __tabBarItems.GetCount();
212 GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
214 _TabBarItem* pPreviousItem = null;
215 _TabBarItem* pCurrentItem = null;
216 Rectangle previousItemBounds;
217 Rectangle currentItemBounds;
218 for (int i = itemIndex; i < itemCount; i++)
220 pPreviousItem = GetItemAt(i - 1);
221 pCurrentItem = GetItemAt(i);
222 if (pCurrentItem == null)
226 if (pPreviousItem == null)
228 previousItemBounds.SetBounds(0, 0, 0, 0);
232 previousItemBounds = pPreviousItem->GetBounds();
234 currentItemBounds = pCurrentItem->GetBounds();
235 currentItemBounds.x = previousItemBounds.x + previousItemBounds.width + itemMargin;
237 pCurrentItem->SetBounds(currentItemBounds);
239 __widthOfAllItems = currentItemBounds.x + currentItemBounds.width;
244 }}} // Tizen::Ui::Controls