Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_InputPad.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 /**
19  * @file                FUiCtrl_InputPad.cpp
20  * @brief               This is the implementation file for the _InputPad class.
21  */
22
23 #include <FBaseColIEnumeratorT.h>
24
25 #include "FUiCtrl_InputPad.h"
26 #include "FUiCtrl_InputPadPresenter.h"
27 #include "FUi_AccessibilityContainer.h"
28 #include "FUi_AccessibilityElement.h"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32 using namespace Tizen::Graphics;
33 using namespace Tizen::Ui;
34
35 const int INPUTPAD_BUTTON_MAX = 12;
36
37 namespace Tizen { namespace Ui { namespace Controls
38 {
39
40 _InputPad::_InputPad(void)
41         : __pInputPadPresenter(null)
42 {
43         ClearLastResult();
44 }
45
46 _InputPad::~_InputPad(void)
47 {
48         delete __pInputPadPresenter;
49         __pInputPadPresenter = null;
50
51         RemoveAllAccessibilityElement();
52
53         ClearLastResult();
54 }
55
56 result
57 _InputPad::Construct(const Rectangle& bounds)
58 {
59         result r = E_SUCCESS;
60
61         if(likely((_AccessibilityManager::IsActivated())))
62         {
63                 InitializeAccessebilityElementMonthNames();
64         }
65
66         SetBounds(bounds);
67
68         __pInputPadPresenter = new (std::nothrow) _InputPadPresenter(this);
69         SysTryReturnResult(NID_UI_CTRL, __pInputPadPresenter, E_OUT_OF_MEMORY, "Memory allocation failed.");
70
71         r = __pInputPadPresenter->Construct(bounds);
72         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM,
73                                 "[E_SYSTEM] A system error has occurred. Failed to construct the instance of presenter for Inputpad");
74
75         AcquireHandle();
76
77         if(likely((_AccessibilityManager::IsActivated())))
78         {
79                 if (GetAccessibilityContainer() != null)
80                 {
81                         GetAccessibilityContainer()->Activate(true);
82                 }
83         }
84
85         ClearLastResult();
86
87         return r;
88
89 CATCH:
90         delete __pInputPadPresenter;
91         __pInputPadPresenter = null;
92
93         return r;
94 }
95
96 result
97 _InputPad::SetInputPadEventListener(const _IInputPadEventListener& listener)
98 {
99         ClearLastResult();
100
101         SysAssertf((__pInputPadPresenter != null), "The instance of InputpadPresenter is not yet constructed.");
102
103         __pInputPadPresenter->SetInputPadEventListener(listener);
104
105         return E_SUCCESS;
106 }
107
108 void
109 _InputPad::SetInputPadStyle(const _InputPadStyle style)
110 {
111         SysTryReturnVoidResult(NID_UI_CTRL, (style >= INPUTPAD_STYLE_NORMAL), E_INVALID_ARG,
112                                                    "[E_INVALID_ARG] Invalid argument(s) is used. The style provided is not present in the _InputPadStyle list.");
113
114         SysTryReturnVoidResult(NID_UI_CTRL, (style < INPUTPAD_STYLE_MAX), E_INVALID_ARG,
115                                                    "[E_INVALID_ARG] Invalid argument(s) is used. The style provided is not a valid _InputPadStyle value.");
116
117         if (style == __pInputPadPresenter->GetInputPadStyle())
118         {
119                 if (HasAccessibilityElement() == true)
120                 {
121                         return;
122                 }
123         }
124         else
125         {
126                 RemoveAllAccessibilityElement();
127         }
128
129         __pInputPadPresenter->SetInputPadStyle(style);
130         SetAllAccessibilityElement();
131
132         return;
133 }
134
135 String&
136 _InputPad::GetAccessebilityElementMonthName(int number)
137 {
138         SysTryReturn(NID_UI_CTRL, (number >= 0), __pMonthNames[0], E_SYSTEM,
139                                                  "[E_SYSTEM] A system error has occurred. Failed to get the month name.");
140
141         return __pMonthNames[number];
142 }
143
144 void
145 _InputPad::SetFont(Font* pFont)
146 {
147         __pInputPadPresenter->SetFont(pFont);
148
149         return;
150 }
151
152 void
153 _InputPad::OnBoundsChanged(void)
154 {
155         if (__pInputPadPresenter != null)
156         {
157                 __pInputPadPresenter->LoadProperties(GetOrientation());
158                 SetBoundsAllAccessibilityElement();
159         }
160
161         return;
162 }
163
164 void
165 _InputPad::OnDraw(void)
166 {
167         __pInputPadPresenter->Draw();
168
169         return;
170 }
171
172 bool
173 _InputPad::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
174 {
175         return __pInputPadPresenter->OnTouchPressed(source, touchinfo);
176 }
177
178 bool
179 _InputPad::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
180 {
181         return __pInputPadPresenter->OnTouchReleased(source, touchinfo);
182 }
183
184 bool
185 _InputPad::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
186 {
187         return __pInputPadPresenter->OnTouchCanceled(source, touchinfo);
188 }
189
190 void
191 _InputPad::OnChangeLayout(_ControlOrientation orientation)
192 {
193         if (__pInputPadPresenter != null)
194         {
195                 __pInputPadPresenter->EnableLayoutChangedFlag();
196                 __pInputPadPresenter->LoadProperties(orientation);
197                 __pInputPadPresenter->SetInputPadStyle(__pInputPadPresenter->GetInputPadStyle());
198                 SetBoundsAllAccessibilityElement();
199         }
200
201         return;
202 }
203
204 bool
205 _InputPad::HasAccessibilityElement(void)
206 {
207         return __accessibilityElements.GetCount() > 0;
208 }
209
210 void
211 _InputPad::SetAllAccessibilityElement(void)
212 {
213         if(likely(!(_AccessibilityManager::IsActivated())))
214         {
215                 return;
216         }
217
218         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
219         if (pContainer != null)
220         {
221                 pContainer->RemoveAllElement();
222
223                 for (int index = 0; index < INPUTPAD_BUTTON_MAX; index++)
224                 {
225                         int returnValue = __pInputPadPresenter->GetReturnValue(index);
226                         if (returnValue >= 0)
227                         {
228                                 _AccessibilityElement* pElement = new _AccessibilityElement(true);
229                                 if (pElement != null)
230                                 {
231                                         if (__pInputPadPresenter->GetInputPadStyle() == INPUTPAD_STYLE_ALPHA)
232                                         {
233                                                 pElement->SetLabel(__pMonthNames[index]);
234                                         }
235                                         else
236                                         {
237                                                 pElement->SetLabel(Integer::ToString(returnValue));
238                                         }
239                                         pElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
240 //                                      pElement->SetTrait(ACCESSIBILITY_TRAITS_INPUTPAD);
241                                         pElement->SetBounds(__pInputPadPresenter->GetButtonBounds(index));
242                                         pContainer->AddElement(*pElement);
243                                         __accessibilityElements.Add(pElement);
244                                 }
245                         }
246                 }
247         }
248
249         return;
250 }
251
252 void
253 _InputPad::SetBoundsAllAccessibilityElement(void)
254 {
255         if(likely(!(_AccessibilityManager::IsActivated())))
256         {
257                 return;
258         }
259
260         _AccessibilityElement* pElement = null;
261         IEnumeratorT<_AccessibilityElement*>* pEnumerator = __accessibilityElements.GetEnumeratorN();
262         int index = 0;
263         while (pEnumerator->MoveNext() == E_SUCCESS)
264         {
265                 if (pEnumerator->GetCurrent(pElement) == E_SUCCESS)
266                 {
267                         while (__pInputPadPresenter->GetReturnValue(index) < 0)
268                         {
269                                 index++;
270                         }
271                         pElement->SetBounds(__pInputPadPresenter->GetButtonBounds(index));
272                 }
273                 index++;
274         }
275         delete pEnumerator;
276
277         return;
278 }
279
280 void
281 _InputPad::RemoveAllAccessibilityElement(void)
282 {
283         if(likely(!(_AccessibilityManager::IsActivated())))
284         {
285                 return;
286         }
287
288         _AccessibilityElement* pElement = null;
289         while (HasAccessibilityElement() == true)
290         {
291                 if ((__accessibilityElements.GetAt(0, pElement)) == E_SUCCESS)
292                 {
293                         __accessibilityElements.RemoveAt(0);
294                         pElement->GetParent()->RemoveElement(*pElement);
295                 }
296                 else
297                 {
298                         __accessibilityElements.RemoveAt(0);
299                 }
300         }
301
302         return;
303 }
304
305 void
306 _InputPad::InitializeAccessebilityElementMonthNames(void)
307 {
308         __pMonthNames[0] = L"January";
309         __pMonthNames[1] = L"February";
310         __pMonthNames[2] = L"March";
311         __pMonthNames[3] = L"April";
312         __pMonthNames[4] = L"May";
313         __pMonthNames[5] = L"June";
314         __pMonthNames[6] = L"July";
315         __pMonthNames[7] = L"August";
316         __pMonthNames[8] = L"September";
317         __pMonthNames[9] = L"October";
318         __pMonthNames[10] = L"November";
319         __pMonthNames[11] = L"December";
320
321         return;
322 }
323
324 }}} // Tizen::Ui::Controls