Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_TabBarModel.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
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_TabBarModel.cpp
19  * @brief               This is the implementation file for the _TabBarModel class.
20  */
21
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include "FUiCtrl_TabBarModel.h"
25
26
27 using namespace Tizen::Graphics;
28
29 namespace Tizen { namespace Ui { namespace Controls
30 {
31
32 _TabBarModel::_TabBarModel(void)
33         : __selectedItemIndex(0)
34         , __firstDrawnItemIndex(0)
35         , __lastDrawnItemIndex(0)
36         , __widthOfAllItems(0.0f)
37 {
38         __tabBarItems.Construct();
39 }
40
41 _TabBarModel::~_TabBarModel(void)
42 {
43         __tabBarItems.RemoveAll(true);
44 }
45
46 void
47 _TabBarModel::SetSelectedItemIndex(int index)
48 {
49         __selectedItemIndex = index;
50 }
51
52 int
53 _TabBarModel::GetSelectedItemIndex(void) const
54 {
55         return __selectedItemIndex;
56 }
57
58 void
59 _TabBarModel::SetFirstDrawnItemIndex(int index)
60 {
61         __firstDrawnItemIndex = index;
62 }
63
64 int
65 _TabBarModel::GetFirstDrawnItemIndex(void) const
66 {
67         return __firstDrawnItemIndex;
68 }
69
70 void
71 _TabBarModel::SetLastDrawnItemIndex(int index)
72 {
73         __lastDrawnItemIndex = index;
74 }
75
76 int
77 _TabBarModel::GetLastDrawnItemIndex(void) const
78 {
79         return __lastDrawnItemIndex;
80 }
81
82 int
83 _TabBarModel::GetItemCount(void) const
84 {
85         return __tabBarItems.GetCount();
86 }
87
88 float
89 _TabBarModel::GetWidthOfAllItems(void) const
90 {
91         return __widthOfAllItems;
92 }
93
94 result
95 _TabBarModel::AddItem(const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
96 {
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.");
99
100         pItem->SetText(text, orientation);
101         pItem->SetActionId(actionId);
102         int itemCount = __tabBarItems.GetCount();
103
104         if (itemCount > 0)
105         {
106                 pItem->SetStatus(ITEM_STATUS_NORMAL);
107         }
108         else
109         {
110                 pItem->SetStatus(ITEM_STATUS_SELECTED);
111                 __selectedItemIndex = 0;
112         }
113
114         _TabBarItem* pPreviousItem = GetItemAt(itemCount - 1);
115         FloatRectangle itemBounds = pItem->GetBounds();
116         if (pPreviousItem != null)
117         {
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;
123         }
124         else
125         {
126                 float arrowMargin = 0.0f;
127                 GET_SHAPE_CONFIG(TABBAR::ARROW_MARGIN, orientation, arrowMargin);
128                 itemBounds.x += arrowMargin;
129         }
130         pItem->SetBounds(itemBounds);
131         __widthOfAllItems += itemBounds.width;
132
133         return __tabBarItems.Add(*pItem);
134 }
135
136 result
137 _TabBarModel::InsertItemAt(int index, const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
138 {
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)
141         {
142                 return AddItem(text, actionId, orientation);
143         }
144
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.");
147
148         pItem->SetText(text, orientation);
149         pItem->SetActionId(actionId);
150
151         _TabBarItem* pPreviousItem = GetItemAt(index - 1);
152         FloatRectangle itemBounds = pItem->GetBounds();
153         if (pPreviousItem != null)
154         {
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);
160         }
161
162         result r = __tabBarItems.InsertAt(*pItem, index);
163         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
164
165         r = ReCalculateItemPosition(index + 1, orientation);
166         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
167
168         return E_SUCCESS;
169 }
170
171 result
172 _TabBarModel::SetItemAt(int index, const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
173 {
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);
176
177         pItem->SetText(text, orientation);
178         pItem->SetActionId(actionId);
179
180         result r = ReCalculateItemPosition(index + 1, orientation);
181         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
182
183         return E_SUCCESS;
184 }
185
186 result
187 _TabBarModel::RemoveItemAt(int index, _ControlOrientation orientation)
188 {
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);
191
192         float itemMargin = 0.0f;
193         GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
194
195         result r = __tabBarItems.RemoveAt(index, true);
196         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
197
198         r = ReCalculateItemPosition(index, orientation);
199         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
200
201         return E_SUCCESS;
202 }
203
204 void
205 _TabBarModel::RemoveAllItems(void)
206 {
207         __widthOfAllItems = 0.0f;
208         __tabBarItems.RemoveAll(true);
209 }
210
211 _TabBarItem*
212 _TabBarModel::GetItemAt(int index) const
213 {
214         const _TabBarItem* pItem = static_cast <const _TabBarItem*>(__tabBarItems.GetAt(index));
215
216         return const_cast<_TabBarItem*>(pItem);
217 //      return static_cast <_TabBarItem*>(__tabBarItems.GetAt(index));
218 }
219
220 result
221 _TabBarModel::ReCalculateItemPosition(int itemIndex, _ControlOrientation orientation)
222 {
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);
227
228         _TabBarItem* pPreviousItem = null;
229         _TabBarItem* pCurrentItem = null;
230         FloatRectangle previousItemBounds;
231         FloatRectangle currentItemBounds;
232         for (int i = itemIndex; i < itemCount; i++)
233         {
234                 pPreviousItem = GetItemAt(i - 1);
235                 pCurrentItem = GetItemAt(i);
236                 if (pCurrentItem == null)
237                 {
238                         break;
239                 }
240                 if (pPreviousItem == null)
241                 {
242                         previousItemBounds.SetBounds(0.0f, 0.0f, 0.0f, 0.0f);
243                 }
244                 else
245                 {
246                         previousItemBounds = pPreviousItem->GetBounds();
247                 }
248                 currentItemBounds = pCurrentItem->GetBounds();
249                 currentItemBounds.x = previousItemBounds.x + previousItemBounds.width + itemMargin;
250
251                 pCurrentItem->SetBounds(currentItemBounds);
252         }
253         __widthOfAllItems = currentItemBounds.x + currentItemBounds.width;
254
255         return E_SUCCESS;
256 }
257
258 }}} // Tizen::Ui::Controls