fix klocwork issue
[platform/framework/native/uifw.git] / src / ui / FUi_AccessibilityElement.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 #include <FBaseString.h>
19 #include <FUiAccessibilityTypes.h>
20 #include <FUiIAccessibilityListener.h>
21 #include <FBaseSysLog.h>
22 #include "FUi_AccessibilityContainer.h"
23 #include "FUi_AccessibilityElementImpl.h"
24 #include "FUi_AccessibilityElement.h"
25 #include "FUi_AccessibilityManager.h"
26 #include "FUi_Control.h"
27 #include "FUi_ResourceManager.h"
28
29 using namespace Tizen::Base;
30 using namespace Tizen::Graphics;
31
32 namespace Tizen { namespace Ui {
33
34 _AccessibilityElement::_AccessibilityElement(bool systemElement)
35         : __name(L"")
36         , __bounds(0,0,0,0)
37         , __absBounds(0,0,0,0)
38         , __label(L"")
39         , __hint(L"")
40         , __status(L"")
41         , __traitString(L"")
42         , __trait(ACCESSIBILITY_TRAITS_NONE)
43         , __value(L"")
44         , __pParent(null)
45         , __pLabelId(null)
46         , __pTraitId(null)
47         , __pHintId(null)
48         , __pStatusId(null)
49         , __pValueId(null)
50         , __pUserData(null)
51         , __systemElement(systemElement)
52         , __activated(true)
53         , __supportOperatingGesture(true)
54         , __setHintByUser(false)
55         , __disabledHint(false)
56         , __updateContents(false)
57         , __publicLabelUpdated(false)
58 {
59 }
60 _AccessibilityElement::~_AccessibilityElement(void)
61 {
62         if (__pUserData)
63         {
64                 static_cast<_AccessibilityElementImpl*>(__pUserData)->SetCore(null);
65                 __pUserData = null;
66         }
67         delete [] __pLabelId;
68         __pLabelId = null;
69         delete [] __pTraitId;
70         __pTraitId = null;
71         delete [] __pHintId;
72         __pHintId = null;
73         delete [] __pStatusId;
74         __pStatusId = null;
75         delete [] __pValueId;
76         __pValueId = null;
77         if (__pParent && this == __pParent->GetCurrentFocusedElement())
78         {
79                 _AccessibilityManager::GetInstance()->RequestAutoReading(_ACCESSIBILITY_AUTO_READING_MODE_FIRST_ITEM);
80         }
81 }
82 result
83 _AccessibilityElement::Construct(const String& name, const FloatRectangle& bounds)
84 {
85         __name = name;
86         __bounds.SetBounds(bounds.x,bounds.y,bounds.width,bounds.height);
87         return E_SUCCESS;
88 }
89 result
90 _AccessibilityElement::Construct(const String& name, const Rectangle& bounds)
91 {
92         __name = name;
93         __bounds.SetBounds(bounds.x,bounds.y,bounds.width,bounds.height);
94         return E_SUCCESS;
95 }
96 void
97 _AccessibilityElement::SetName(const String& name)
98 {
99         __name = name;
100 }
101 void
102 _AccessibilityElement::SetLabel(const String& label)
103 {
104         __label = label;
105         __updateContents = true;
106         if(__pLabelId)
107         {
108                 delete [] __pLabelId;
109                 __pLabelId = null;
110         }
111 }
112 void
113 _AccessibilityElement::SetHint(const String& hint, bool setHintByUser)
114 {
115         if(__setHintByUser && !setHintByUser)
116         {
117                 return;
118         }
119         __hint = hint;
120         __setHintByUser = setHintByUser;
121         __updateContents = true;
122         if(__pHintId)
123         {
124                 delete [] __pHintId;
125                 __pHintId = null;
126         }
127 }
128 void
129 _AccessibilityElement::SetStatus(const String& status)
130 {
131         __status = status;
132         __updateContents = true;
133         if(__pStatusId)
134         {
135                 delete [] __pStatusId;
136                 __pStatusId = null;
137         }
138 }
139 void
140 _AccessibilityElement::SetTrait(const Tizen::Base::String& trait)
141 {
142         __traitString = trait;
143         __updateContents = true;
144         if(__pTraitId)
145         {
146                 delete [] __pTraitId;
147                 __pTraitId = null;
148         }
149 }
150 void
151 _AccessibilityElement::SetParent(const _AccessibilityContainer& parent)
152 {
153         __pParent = &const_cast<_AccessibilityContainer&>(parent);
154 }
155 void
156 _AccessibilityElement::SetValue(const String& value)
157 {
158         __value = value;
159         __updateContents = true;
160         if(__pValueId)
161         {
162                 delete [] __pValueId;
163                 __pValueId = null;
164         }
165 }
166 _AccessibilityContainer*
167 _AccessibilityElement::GetParent(void) const
168 {
169         return __pParent;
170 }
171 String
172 _AccessibilityElement::GetName(void) const
173 {
174         return __name;
175 }
176 FloatRectangle
177 _AccessibilityElement::GetBounds(void) const
178 {
179         return __bounds;
180 }
181 String
182 _AccessibilityElement::GetLabel(void) const
183 {
184         String out;
185         if (__pLabelId)
186         {
187                 Tizen::Ui::_ResourceManager::GetInstance()->GetString(__pLabelId, out);
188                 return out;
189         }
190
191         return __label;
192 }
193 String
194 _AccessibilityElement::GetHint(void) const
195 {
196         String out;
197         if (__pHintId)
198         {
199                 Tizen::Ui::_ResourceManager::GetInstance()->GetString(__pHintId, out);
200                 return out;
201         }
202         return __hint;
203 }
204 String
205 _AccessibilityElement::GetStatus(void) const
206 {
207         String out;
208         if (__pStatusId)
209         {
210                 Tizen::Ui::_ResourceManager::GetInstance()->GetString(__pStatusId, out);
211                 return out;
212         }
213         return __status;
214 }
215 String
216 _AccessibilityElement::GetTraitString(void) const
217 {
218         String out;
219         if (__pTraitId)
220         {
221                 Tizen::Ui::_ResourceManager::GetInstance()->GetString(__pTraitId, out);
222                 return out;
223         }
224         return __traitString;
225 }
226 String
227 _AccessibilityElement::GetValue(void) const
228 {
229         String out;
230         if (__pValueId)
231         {
232                 Tizen::Ui::_ResourceManager::GetInstance()->GetString(__pValueId, out);
233                 return out;
234         }
235         return __value;
236 }
237 void
238 _AccessibilityElement::SetHintDisabled(bool enable)
239 {
240         __disabledHint = enable;
241 }
242 void
243 _AccessibilityElement::Activate(bool enable)
244 {
245         __activated = enable;
246         _AccessibilityManager::GetInstance()->NeedRefreshItem();
247 }
248 bool
249 _AccessibilityElement::IsActivated(void) const
250 {
251         return __activated;
252 }
253 String
254 _AccessibilityElement::GetReadingContents(void) const
255 {
256         String out = L"";
257         if (__pLabelId != null)
258         {
259                 String string;
260                 Tizen::Ui::_ResourceManager::GetInstance()->GetString(__pLabelId, string);
261                 out = string;
262         }
263         else if(__label.GetLength() > 0)
264         {
265                 out += __label;
266         }
267
268         if (__pTraitId != null)
269         {
270                 String string;
271                 Tizen::Ui::_ResourceManager::GetInstance()->GetString(__pTraitId, string);
272                 out += L", ";
273                 out += string;
274         }
275         else if(__traitString.GetLength() > 0)
276         {
277                 out += L", ";
278                 out += __traitString;
279         }
280
281         if (__pValueId != null)
282         {
283                 String string;
284                 Tizen::Ui::_ResourceManager::GetInstance()->GetString(__pValueId, string);
285                 out += L", ";
286                 out += string;
287         }
288         else if(__value.GetLength() > 0)
289         {
290                 out += L", ";
291                 out += __value;
292         }
293
294         if (__pStatusId != null)
295         {
296                 String string;
297                 Tizen::Ui::_ResourceManager::GetInstance()->GetString(__pStatusId, string);
298                 out += L", ";
299                 out += string;
300         }
301         else if(__status.GetLength() > 0)
302         {
303                 out += L", ";
304                 out += __status;
305         }
306         if(__pParent->GetEnableState() == false)
307         {
308                 out += L", ";
309                 out += L"disable"; //ScrLocalization
310         }
311         if (!__disabledHint && __pParent->GetEnableState())
312         {
313                 if (__pHintId != null)
314                 {
315                         String string;
316                         Tizen::Ui::_ResourceManager::GetInstance()->GetString(__pHintId, string);
317                         out += L"\n ";
318                         out += string;
319                 }
320                 else if(__hint.GetLength() > 0)
321                 {
322                         out += L"\n ";
323                         out += __hint;
324                 }
325         }
326         const_cast<_AccessibilityElement*>(this)->__updateContents = false;
327         return out;
328 }
329 void
330 _AccessibilityElement::SetBounds(const FloatRectangle& bounds)
331 {
332         __bounds.SetBounds(bounds.x,bounds.y,bounds.width,bounds.height);
333         if(__pParent && this == __pParent->GetCurrentFocusedElement())
334         {
335                 _AccessibilityManager::GetInstance()->RequestToDrawFocusUi();
336         }
337         _AccessibilityManager::GetInstance()->NeedRefreshItem();
338 }
339
340 void
341 _AccessibilityElement::SetBounds(const Tizen::Graphics::Rectangle& bounds)
342 {
343         __bounds.SetBounds(bounds.x,bounds.y,bounds.width,bounds.height);
344         if(__pParent && this == __pParent->GetCurrentFocusedElement())
345         {
346                 _AccessibilityManager::GetInstance()->RequestToDrawFocusUi();
347         }
348         _AccessibilityManager::GetInstance()->NeedRefreshItem();
349 }
350
351 FloatRectangle
352 _AccessibilityElement::GetAbsoluteBounds(void) const
353 {
354         FloatRectangle controlAbsBounds = GetParent()->GetOwner().GetAbsoluteBoundsF(true);
355         FloatRectangle rect(controlAbsBounds.x+__bounds.x, controlAbsBounds.y+__bounds.y, __bounds.width, __bounds.height);
356         const_cast<FloatRectangle&>(__absBounds).SetBounds(controlAbsBounds.x+__bounds.x, controlAbsBounds.y+__bounds.y, __bounds.width, __bounds.height);
357         return rect;
358 }
359 void
360 _AccessibilityElement::SetSupportOperatingGesture(bool set)
361 {
362         __supportOperatingGesture = set;
363 }
364 bool
365 _AccessibilityElement::GetSupportOperatingGesture(void)
366 {
367         return __supportOperatingGesture;
368 }
369 void*
370 _AccessibilityElement::GetUserData(void) const
371 {
372         return __pUserData;
373 }
374
375 void
376 _AccessibilityElement::SetUserData(void* pUserData)
377 {
378         __pUserData = pUserData;
379 }
380 bool
381 _AccessibilityElement::IsSystemElement(void)
382 {
383         return __systemElement;
384 }
385 bool
386 _AccessibilityElement::IsUpdated(void)
387 {
388         return __updateContents;
389 }
390
391 void
392 _AccessibilityElement::SetLabelWithStringId(const char* id)
393 {
394         if(__pLabelId)
395         {
396                 delete [] __pLabelId;
397         __pLabelId = null;
398         }
399         int length = strlen(id);
400         if(length == 0)
401         {
402                 return;
403         }
404         __pLabelId = new char[length+1];
405         strcpy(__pLabelId, id);
406         __pLabelId[length] = '\0';
407 }
408 void
409 _AccessibilityElement::SetTraitWithStringId(const char* id)
410 {
411         if(__pTraitId)
412         {
413                 delete [] __pTraitId;
414         __pTraitId = null;
415         }
416         int length = strlen(id);
417         if(length == 0)
418         {
419                 return;
420         }
421         __pTraitId = new char[length+1];
422         strcpy(__pTraitId, id);
423         __pTraitId[length] = '\0';
424 }
425 void
426 _AccessibilityElement::SetHintWithStringId(const char* id)
427 {
428         if(__pHintId)
429         {
430                 delete [] __pHintId;
431         __pHintId = null;
432         }
433
434         int length = strlen(id);
435         if(length == 0)
436         {
437                 return;
438         }
439         __pHintId = new char[length+1];
440         strcpy(__pHintId, id);
441         __pHintId[length] = '\0';
442 }
443 void
444 _AccessibilityElement::SetStatusWithStringId(const char* id)
445 {
446         if(__pStatusId)
447         {
448                 delete [] __pStatusId;
449         __pStatusId = null;
450         }
451         int length = strlen(id);
452         if(length == 0)
453         {
454                 return;
455         }
456         __pStatusId = new char[length+1];
457         strcpy(__pStatusId, id);
458         __pStatusId[length] = '\0';
459 }
460 void
461 _AccessibilityElement::SetValueWithStringId(const char* id)
462 {
463         if(__pValueId)
464         {
465                 delete [] __pValueId;
466         __pValueId = null;
467         }
468         int length = strlen(id);
469         if(length == 0)
470         {
471                 return;
472         }
473         __pValueId = new char[length+1];
474         strcpy(__pValueId, id);
475         __pValueId[length] = '\0';
476 }
477
478 void
479 _AccessibilityElement::SetPublicLabelUpdate(bool isPublicLabelUpdated)
480 {
481         __publicLabelUpdated = isPublicLabelUpdated;
482 }
483
484 bool
485 _AccessibilityElement::IsPublicLabelUpdated(void)
486 {
487         return __publicLabelUpdated;
488 }
489
490 }}