Tizen 2.1 base
[framework/osp/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 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_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         , __widthOfAllItems(0)
36 {
37         __tabBarItems.Construct();
38 }
39
40 _TabBarModel::~_TabBarModel(void)
41 {
42         __tabBarItems.RemoveAll(true);
43 }
44
45 void
46 _TabBarModel::SetSelectedItemIndex(int index)
47 {
48         __selectedItemIndex = index;
49 }
50
51 int
52 _TabBarModel::GetSelectedItemIndex(void) const
53 {
54         return __selectedItemIndex;
55 }
56
57 void
58 _TabBarModel::SetFirstDrawnItemIndex(int index)
59 {
60         __firstDrawnItemIndex = index;
61 }
62
63 int
64 _TabBarModel::GetFirstDrawnItemIndex(void) const
65 {
66         return __firstDrawnItemIndex;
67 }
68
69 int
70 _TabBarModel::GetItemCount(void) const
71 {
72         return __tabBarItems.GetCount();
73 }
74
75 int
76 _TabBarModel::GetWidthOfAllItems(void) const
77 {
78         return __widthOfAllItems;
79 }
80
81 result
82 _TabBarModel::AddItem(const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
83 {
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.");
86
87         pItem->SetText(text, orientation);
88         pItem->SetActionId(actionId);
89         int itemCount = __tabBarItems.GetCount();
90
91         if (itemCount > 0)
92         {
93                 pItem->SetStatus(ITEM_STATUS_NORMAL);
94         }
95         else
96         {
97                 pItem->SetStatus(ITEM_STATUS_SELECTED);
98         }
99
100         _TabBarItem* pPreviousItem = GetItemAt(itemCount - 1);
101         Rectangle itemBounds = pItem->GetBounds();
102         if (pPreviousItem != null)
103         {
104                 Rectangle previousItemBounds = pPreviousItem->GetBounds();
105                 int itemMargin = 0;
106                 GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
107                 itemBounds.x = previousItemBounds.x + previousItemBounds.width + itemMargin;
108                 __widthOfAllItems += itemMargin;
109         }
110         else
111         {
112                 int arrowMargin = 0;
113                 GET_SHAPE_CONFIG(TABBAR::ARROW_MARGIN, orientation, arrowMargin);
114                 itemBounds.x += arrowMargin;
115         }
116         pItem->SetBounds(itemBounds);
117         __widthOfAllItems += itemBounds.width;
118
119         return __tabBarItems.Add(*pItem);
120 }
121
122 result
123 _TabBarModel::InsertItemAt(int index, const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
124 {
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)
127         {
128                 return AddItem(text, actionId, orientation);
129         }
130
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.");
133
134         pItem->SetText(text, orientation);
135         pItem->SetActionId(actionId);
136
137         _TabBarItem* pPreviousItem = GetItemAt(index - 1);
138         Rectangle itemBounds = pItem->GetBounds();
139         if (pPreviousItem != null)
140         {
141                 Rectangle previousItemBounds = pPreviousItem->GetBounds();
142                 int itemMargin = 0;
143                 GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
144                 itemBounds.x = previousItemBounds.x + previousItemBounds.width + itemMargin;
145                 pItem->SetBounds(itemBounds);
146         }
147
148         result r = __tabBarItems.InsertAt(*pItem, index);
149         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
150
151         r = ReCalculateItemPosition(index + 1, orientation);
152         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
153
154         return E_SUCCESS;
155 }
156
157 result
158 _TabBarModel::SetItemAt(int index, const Tizen::Base::String& text, int actionId, _ControlOrientation orientation)
159 {
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);
162
163         pItem->SetText(text, orientation);
164         pItem->SetActionId(actionId);
165
166         result r = ReCalculateItemPosition(index + 1, orientation);
167         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
168
169         return E_SUCCESS;
170 }
171
172 result
173 _TabBarModel::RemoveItemAt(int index, _ControlOrientation orientation)
174 {
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);
177
178         int itemMargin = 0;
179         GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
180
181         result r = __tabBarItems.RemoveAt(index, true);
182         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
183
184         r = ReCalculateItemPosition(index, orientation);
185         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
186
187         return E_SUCCESS;
188 }
189
190 void
191 _TabBarModel::RemoveAllItems(void)
192 {
193         __widthOfAllItems = 0;
194         __tabBarItems.RemoveAll(true);
195 }
196
197 _TabBarItem*
198 _TabBarModel::GetItemAt(int index) const
199 {
200         const _TabBarItem* pItem = static_cast <const _TabBarItem*>(__tabBarItems.GetAt(index));
201
202         return const_cast<_TabBarItem*>(pItem);
203 //      return static_cast <_TabBarItem*>(__tabBarItems.GetAt(index));
204 }
205
206 result
207 _TabBarModel::ReCalculateItemPosition(int itemIndex, _ControlOrientation orientation)
208 {
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();
211         int itemMargin = 0;
212         GET_SHAPE_CONFIG(TABBAR::ITEM_MARGIN, orientation, itemMargin);
213
214         _TabBarItem* pPreviousItem = null;
215         _TabBarItem* pCurrentItem = null;
216         Rectangle previousItemBounds;
217         Rectangle currentItemBounds;
218         for (int i = itemIndex; i < itemCount; i++)
219         {
220                 pPreviousItem = GetItemAt(i - 1);
221                 pCurrentItem = GetItemAt(i);
222                 if (pCurrentItem == null)
223                 {
224                         break;
225                 }
226                 if (pPreviousItem == null)
227                 {
228                         previousItemBounds.SetBounds(0, 0, 0, 0);
229                 }
230                 else
231                 {
232                         previousItemBounds = pPreviousItem->GetBounds();
233                 }
234                 currentItemBounds = pCurrentItem->GetBounds();
235                 currentItemBounds.x = previousItemBounds.x + previousItemBounds.width + itemMargin;
236
237                 pCurrentItem->SetBounds(currentItemBounds);
238         }
239         __widthOfAllItems = currentItemBounds.x + currentItemBounds.width;
240
241         return E_SUCCESS;
242 }
243
244 }}} // Tizen::Ui::Controls