Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_RadioGroup.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_RadioGroup.cpp
19  * @brief               This is the implementation file for the _RadioGroup class.
20  */
21
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include "FUiCtrl_RadioGroup.h"
25 #include "FUiCtrl_RadioGroupPresenter.h"
26
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Graphics;
30
31 namespace Tizen { namespace Ui { namespace Controls
32 {
33
34 _RadioGroup::_RadioGroup(void)
35         : __pRadioGroupPresenter(null)
36         , __checkList(null)
37 {
38         result r = E_SUCCESS;
39
40         _RadioGroupPresenter* pPresenter = new (std::nothrow) _RadioGroupPresenter();
41         SysTryReturnVoidResult(NID_UI_CTRL, pPresenter, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
42
43         r = SetPresenter(*pPresenter);
44         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
45
46         r = pPresenter->Construct(*this);
47         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
48
49         r = pPresenter->Install();
50         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
51
52         SetMovable(false);
53         SetResizable(false);
54         SetFocusable(false);
55
56         ClearLastResult();
57
58         return;
59
60 CATCH:
61         delete pPresenter;
62 }
63
64 _RadioGroup*
65 _RadioGroup::CreateRadioGroupN(void)
66 {
67         _RadioGroup* pRadioGroup = new (std::nothrow) _RadioGroup();
68         SysTryReturn(NID_UI_CTRL, pRadioGroup, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
69         SysTryCatch(NID_UI_CTRL, GetLastResult() == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
70
71         pRadioGroup->AcquireHandle();
72
73         pRadioGroup->CreateCheckList();
74
75         return pRadioGroup;
76
77 CATCH:
78         delete pRadioGroup;
79         return null;
80 }
81
82 _RadioGroup::~_RadioGroup(void)
83 {
84         if (__pRadioGroupPresenter)
85         {
86                 delete __pRadioGroupPresenter;
87                 __pRadioGroupPresenter = null;
88         }
89
90         if (__checkList)
91         {
92                 delete __checkList;
93                 __checkList = null;
94         }
95
96         ClearLastResult();
97 }
98
99 result
100 _RadioGroup::SetPresenter(const _RadioGroupPresenter& radioGroupPresenter)
101 {
102         __pRadioGroupPresenter = const_cast <_RadioGroupPresenter*>(&radioGroupPresenter);
103
104         return E_SUCCESS;
105 }
106
107 result
108 _RadioGroup::CreateCheckList(void)
109 {
110         result r = E_SUCCESS;
111
112         __checkList = new (std::nothrow) ArrayList();
113         SysTryReturn(NID_UI_CTRL, __checkList, E_INVALID_STATE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
114
115         r = __checkList->Construct();
116         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[E_SYSTEM] System error occurred.");
117
118         return E_SUCCESS;
119
120 CATCH:
121         delete __checkList;
122         return E_SYSTEM;
123 }
124
125 result
126 _RadioGroup::Add(_CheckButton& checkButton)
127 {
128         result r = E_SUCCESS;
129
130         if ((checkButton.__checkButtonStyle != CHECK_BUTTON_STYLE_RADIO) &&
131                         (checkButton.__checkButtonStyle != CHECK_BUTTON_STYLE_RADIO_WITH_DIVIDER)
132                         && (checkButton.__checkButtonStyle != CHECK_BUTTON_STYLE_RADIO_WITH_DETAILED_BUTTON))
133         {
134                 SysTryReturn(NID_UI_CTRL, 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The CheckButton style is invalid.\n");
135         }
136
137         r = checkButton.AddRadioGroup(*this);
138         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_INVALID_ARG, E_INVALID_ARG,
139                                 "[E_INVALID_ARG] The CheckButton is already belonged to a RadioGroup.\n");
140
141         r = __checkList->Add(checkButton);
142         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_INVALID_ARG, E_INVALID_ARG,
143                                 "[E_INVALID_ARG] The CheckButton is already belonged to a RadioGroup.\n");
144
145         return E_SUCCESS;
146 }
147
148 result
149 _RadioGroup::Remove(_CheckButton& checkButton)
150 {
151         result r = E_SUCCESS;
152
153         r = checkButton.RemoveRadioGroup();
154         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND,
155                                 "[E_OBJ_NOT_FOUND] Unable to find the CheckButton.\n");
156
157         r = __checkList->Remove(checkButton);
158         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND,
159                                 "[E_OBJ_NOT_FOUND] Unable to find the CheckButton.\n");
160
161         return E_SUCCESS;
162 }
163
164 bool
165 _RadioGroup::OnNotifiedN(const _Control& source, IList* pArgs)
166 {
167         SysTryReturn(NID_UI_CTRL, pArgs != null, false, E_SYSTEM, "[E_SYSTEM] The argument is invalid.");
168
169         String checkButtonReleasedEvent(L"CheckButtonReleasedEvent");
170         String* pType = dynamic_cast <String*>(pArgs->GetAt(0));
171
172         const _CheckButton* pSelectedCheckButton = dynamic_cast <const _CheckButton*>(pArgs->GetAt(1));
173         SysTryReturn(NID_UI_CTRL, pSelectedCheckButton, false, E_INVALID_STATE, "[E_INVALID_STATE] This instance is not constructed.");
174
175         if (pType)
176         {
177                 if (*pType == checkButtonReleasedEvent)
178                 {
179                         SetSelectedItem(*pSelectedCheckButton);
180                 }
181                 delete pArgs;
182         }
183
184         return true;
185 }
186
187 result
188 _RadioGroup::SetSelectedItem(const _CheckButton& checkButton)
189 {
190         _CheckButton* pCurrentRB = null;
191         _CheckButton* pTarget = const_cast <_CheckButton*>(&checkButton);
192
193         int count = __checkList->GetCount();
194
195         for (int i = 0; i < count; i++)
196         {
197                 pCurrentRB = dynamic_cast<_CheckButton*>(__checkList->GetAt(i));
198
199                 if (pCurrentRB)
200                 {
201                         if (pTarget == pCurrentRB)
202                         {
203                                 pCurrentRB->SetSelected(true);
204                         }
205                         else
206                         {
207                                 pCurrentRB->SetSelected(false);
208                         }
209                         pCurrentRB->Draw();
210                 }
211         }
212
213         return E_SUCCESS;
214 }
215
216 int
217 _RadioGroup::GetItemCount(void) const
218 {
219         return __checkList->GetCount();
220 }
221
222 const _CheckButton*
223 _RadioGroup::GetSelectedItem(void) const
224 {
225         const _CheckButton* pCurrentRB = null;
226         int count = __checkList->GetCount();
227
228         for (int i = 0; i < count; i++)
229         {
230                 pCurrentRB = dynamic_cast<_CheckButton*>(__checkList->GetAt(i));
231
232                 if (pCurrentRB)
233                 {
234                         if (pCurrentRB->IsSelected() == true)
235                         {
236                                 break;
237                         }
238                         else
239                         {
240                                 pCurrentRB = null;
241                         }
242                 }
243         }
244
245         return pCurrentRB;
246 }
247
248 }}} // Tizen::Ui::Controls