Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_OptionMenuItem.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_OptionMenuItem.cpp
19  * @brief               This is the implementation file for the _OptionMenuItem class.
20  */
21
22 #include <new>
23 #include <FBaseSysLog.h>
24 #include <FBaseErrorDefine.h>
25
26 #include "FUiCtrl_OptionMenuItem.h"
27
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30
31 const int MAX_SUB_ITEM_COUNT = 32;
32
33 namespace Tizen { namespace Ui { namespace Controls
34 {
35
36 _OptionMenuItem::_OptionMenuItem(void)
37         : __actionId(-1)
38         , __pSubItems(null)
39 {
40 }
41
42 _OptionMenuItem::~_OptionMenuItem(void)
43 {
44         if (__pSubItems != null)
45         {
46                 __pSubItems->RemoveAll(true);
47                 delete __pSubItems;
48                 __pSubItems = null;
49         }
50 }
51
52 _OptionMenuItem*
53 _OptionMenuItem::CreateInstanceN(void)
54 {
55         _OptionMenuItem* pItem = new (std::nothrow) _OptionMenuItem;
56         SysTryReturn(NID_UI_CTRL, (pItem != null), null, E_OUT_OF_MEMORY,
57                                 "[E_OUT_OF_MEMORY] Memory allocation failed.");
58
59         return pItem;
60 }
61
62 void
63 _OptionMenuItem::SetText(const String& text)
64 {
65         __text = text;
66 }
67
68 const String&
69 _OptionMenuItem::GetText(void) const
70 {
71         return __text;
72 }
73
74 void
75 _OptionMenuItem::SetActionId(int actionId)
76 {
77         __actionId = actionId;
78 }
79
80 int
81 _OptionMenuItem::GetActionId(void) const
82 {
83         return __actionId;
84 }
85
86 result
87 _OptionMenuItem::InsertSubItemAt(_OptionMenuItem& subItem, int index)
88 {
89         if (__pSubItems == null)
90         {
91                 __pSubItems = new (std::nothrow) ArrayList;
92                 SysTryReturn(NID_UI_CTRL, (__pSubItems != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
93                                    "[E_OUT_OF_MEMORY] Memory allocation failed.");
94
95                 result r = __pSubItems->Construct(MAX_SUB_ITEM_COUNT);
96                 if (r != E_SUCCESS)
97                 {
98                         delete __pSubItems;
99                         __pSubItems = null;
100                         SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
101                         return r;
102                 }
103         }
104
105         SysTryReturn(NID_UI_CTRL, (index >= 0 && index < MAX_SUB_ITEM_COUNT), null, E_OUT_OF_RANGE,
106                                 "[E_OUT_OF_RANGE] The specified index (%d) is out of range.", index);
107         SysTryReturn(NID_UI_CTRL, (index <= __pSubItems->GetCount()), null, E_SYSTEM,
108                                 "[E_SYSTEM]  A system error has occurred. The specified index (%d) is greater than the available items count.", index);
109
110         return __pSubItems->InsertAt(subItem, index);
111 }
112
113 result
114 _OptionMenuItem::RemoveSubItemAt(int index)
115 {
116         SysTryReturn(NID_UI_CTRL, (index >= 0 && index < MAX_SUB_ITEM_COUNT), null, E_OUT_OF_RANGE,
117                                 "[E_OUT_OF_RANGE] The specified index (%d) is out of range.", index);
118         SysTryReturn(NID_UI_CTRL, (__pSubItems != null), E_SYSTEM, E_SYSTEM,
119                                 "[E_SYSTEM] A system error has occurred. The sub items list is null.");
120         SysTryReturn(NID_UI_CTRL, (index < __pSubItems->GetCount()), E_SYSTEM, E_SYSTEM,
121                                 "[E_SYSTEM] A system error has occurred. The specified index (%d) is greater than the available items count.", index);
122
123         return __pSubItems->RemoveAt(index, true);
124 }
125
126 result
127 _OptionMenuItem::RemoveAllSubItem(void)
128 {
129         SysTryReturn(NID_UI_CTRL, (__pSubItems != null), E_SYSTEM, E_SYSTEM,
130                                 "[E_SYSTEM] A system error has occurred. The sub items list is null.");
131
132         __pSubItems->RemoveAll(true);
133         return E_SUCCESS;
134 }
135
136 _OptionMenuItem*
137 _OptionMenuItem::GetSubItem(int index)
138 {
139         SysTryReturn(NID_UI_CTRL, (index >= 0 && index < MAX_SUB_ITEM_COUNT), null, E_OUT_OF_RANGE,
140                                 "[E_OUT_OF_RANGE] The specified index (%d) is out of range.", index);
141         SysTryReturn(NID_UI_CTRL, (__pSubItems != null), null, E_SYSTEM,
142                                 "[E_SYSTEM] A system error has occurred. The sub items list is null.", index);
143         SysTryReturn(NID_UI_CTRL, (index < __pSubItems->GetCount()), null, E_SYSTEM,
144                                 "[E_SYSTEM] A system error has occurred. The specified index (%d) is greater than the available items count.", index);
145
146         return static_cast<_OptionMenuItem*>(__pSubItems->GetAt(index));
147 }
148
149 const _OptionMenuItem*
150 _OptionMenuItem::GetSubItem(int index) const
151 {
152         SysTryReturn(NID_UI_CTRL, (index >= 0 && index < MAX_SUB_ITEM_COUNT), null, E_OUT_OF_RANGE,
153                                 "[E_OUT_OF_RANGE] The specified index (%d) is out of range.", index);
154         SysTryReturn(NID_UI_CTRL, (__pSubItems != null), null, E_SYSTEM,
155                                 "[E_SYSTEM] A system error has occurred. The sub items list is null.", index);
156         SysTryReturn(NID_UI_CTRL, (index < __pSubItems->GetCount()), null, E_SYSTEM,
157                                 "[E_SYSTEM] A system error has occurred. The specified index (%d) is greater than the available items count.", index);
158
159         return static_cast<const _OptionMenuItem*>(__pSubItems->GetAt(index));
160 }
161
162 int
163 _OptionMenuItem::GetSubItemCount(void) const
164 {
165         if (__pSubItems == null)
166         {
167                 return 0;
168         }
169
170         return __pSubItems->GetCount();
171 }
172
173 int
174 _OptionMenuItem::GetSubItemIndexFromActionId(int actionId) const
175 {
176         if (__pSubItems == null)
177         {
178                 return -1;
179         }
180
181         int index = 0;
182         int subItemCount = __pSubItems->GetCount();
183         const _OptionMenuItem* pItem = null;
184         for (index = 0; index < subItemCount; index++)
185         {
186                 pItem = static_cast<const _OptionMenuItem*>(__pSubItems->GetAt(index));
187                 if (pItem != null)
188                 {
189                         if (pItem->__actionId == actionId)
190                         {
191                                 return index;
192                         }
193                 }
194         }
195
196         return -1;
197 }
198
199 bool
200 _OptionMenuItem::HasSubItem(void) const
201 {
202         return (__pSubItems != null && __pSubItems->GetCount() > 0);
203 }
204
205 }}} // Tizen::Ui::Controls