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_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 , __lastDrawnItemIndex(0)
36 , __widthOfAllItems(0.0f)
38 __tabBarItems.Construct();
41 _TabBarModel::~_TabBarModel(void)
43 __tabBarItems.RemoveAll(true);
47 _TabBarModel::SetSelectedItemIndex(int index)
49 __selectedItemIndex = index;
53 _TabBarModel::GetSelectedItemIndex(void) const
55 return __selectedItemIndex;
59 _TabBarModel::SetFirstDrawnItemIndex(int index)
61 __firstDrawnItemIndex = index;
65 _TabBarModel::GetFirstDrawnItemIndex(void) const
67 return __firstDrawnItemIndex;
71 _TabBarModel::SetLastDrawnItemIndex(int index)
73 __lastDrawnItemIndex = index;
77 _TabBarModel::GetLastDrawnItemIndex(void) const
79 return __lastDrawnItemIndex;
83 _TabBarModel::GetItemCount(void) const
85 return __tabBarItems.GetCount();
89 _TabBarModel::GetWidthOfAllItems(void) const
91 return __widthOfAllItems;
95 _TabBarModel::AddItem(const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
97 _TabBarItem* pItem = new (std::nothrow) _TabBarItem(orientation);
98 SysTryReturn(NID_UI_CTRL, pItem != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
100 pItem->SetText(text, orientation);
101 pItem->SetActionId(actionId);
102 int itemCount = __tabBarItems.GetCount();
106 pItem->SetStatus(ITEM_STATUS_NORMAL);
110 pItem->SetStatus(ITEM_STATUS_SELECTED);
111 __selectedItemIndex = 0;
114 _TabBarItem* pPreviousItem = GetItemAt(itemCount - 1);
115 FloatRectangle itemBounds = pItem->GetBounds();
116 if (pPreviousItem != null)
118 FloatRectangle previousItemBounds = pPreviousItem->GetBounds();
119 float itemMargin = 0.0f;
120 GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
121 itemBounds.x = previousItemBounds.x + previousItemBounds.width + itemMargin;
122 __widthOfAllItems += itemMargin;
126 float arrowMargin = 0.0f;
127 GET_SHAPE_CONFIG(TABBAR::ARROW_MARGIN, orientation, arrowMargin);
128 itemBounds.x += arrowMargin;
130 pItem->SetBounds(itemBounds);
131 __widthOfAllItems += itemBounds.width;
133 return __tabBarItems.Add(*pItem);
137 _TabBarModel::InsertItemAt(int index, const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
139 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);
140 if (GetItemCount() == 0)
142 return AddItem(text, actionId, orientation);
145 _TabBarItem* pItem = new (std::nothrow) _TabBarItem(orientation);
146 SysTryReturn(NID_UI_CTRL, pItem != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
148 pItem->SetText(text, orientation);
149 pItem->SetActionId(actionId);
151 _TabBarItem* pPreviousItem = GetItemAt(index - 1);
152 FloatRectangle itemBounds = pItem->GetBounds();
153 if (pPreviousItem != null)
155 FloatRectangle previousItemBounds = pPreviousItem->GetBounds();
156 float itemMargin = 0.0f;
157 GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
158 itemBounds.x = previousItemBounds.x + previousItemBounds.width + itemMargin;
159 pItem->SetBounds(itemBounds);
162 result r = __tabBarItems.InsertAt(*pItem, index);
163 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
165 r = ReCalculateItemPosition(index + 1, orientation);
166 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
172 _TabBarModel::SetItemAt(int index, const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
174 _TabBarItem* pItem = static_cast <_TabBarItem*>(__tabBarItems.GetAt(index));
175 SysTryReturn(NID_UI_CTRL, pItem != null, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The %d index item is not exist.", index);
177 pItem->SetText(text, orientation);
178 pItem->SetActionId(actionId);
180 result r = ReCalculateItemPosition(index + 1, orientation);
181 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
187 _TabBarModel::RemoveItemAt(int index, _ControlOrientation orientation)
189 _TabBarItem* pItem = static_cast <_TabBarItem*>(__tabBarItems.GetAt(index));
190 SysTryReturn(NID_UI_CTRL, pItem != null, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The %d index item is not exist.", index);
192 float itemMargin = 0.0f;
193 GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
195 result r = __tabBarItems.RemoveAt(index, true);
196 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
198 r = ReCalculateItemPosition(index, orientation);
199 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
205 _TabBarModel::RemoveAllItems(void)
207 __widthOfAllItems = 0.0f;
208 __tabBarItems.RemoveAll(true);
212 _TabBarModel::GetItemAt(int index) const
214 const _TabBarItem* pItem = static_cast <const _TabBarItem*>(__tabBarItems.GetAt(index));
216 return const_cast<_TabBarItem*>(pItem);
217 // return static_cast <_TabBarItem*>(__tabBarItems.GetAt(index));
221 _TabBarModel::ReCalculateItemPosition(int itemIndex, _ControlOrientation orientation)
223 SysTryReturn(NID_UI_CTRL, itemIndex >= 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The itemIndex(%d) is negative.");
224 int itemCount = __tabBarItems.GetCount();
225 float itemMargin = 0.0f;
226 GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
228 _TabBarItem* pPreviousItem = null;
229 _TabBarItem* pCurrentItem = null;
230 FloatRectangle previousItemBounds;
231 FloatRectangle currentItemBounds;
232 for (int i = itemIndex; i < itemCount; i++)
234 pPreviousItem = GetItemAt(i - 1);
235 pCurrentItem = GetItemAt(i);
236 if (pCurrentItem == null)
240 if (pPreviousItem == null)
242 previousItemBounds.SetBounds(0.0f, 0.0f, 0.0f, 0.0f);
246 previousItemBounds = pPreviousItem->GetBounds();
248 currentItemBounds = pCurrentItem->GetBounds();
249 currentItemBounds.x = previousItemBounds.x + previousItemBounds.width + itemMargin;
251 pCurrentItem->SetBounds(currentItemBounds);
253 __widthOfAllItems = currentItemBounds.x + currentItemBounds.width;
258 }}} // Tizen::Ui::Controls