changing hint text for accessibility.
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_ColorPicker.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 /**
19 * @file                 FUiCtrl_ColorPicker.cpp
20 * @brief                This file contains implementation of _ColorPicker class
21 *
22 * This file contains implementation of _ColorPicker class.
23 */
24
25 #include <new>
26 #include <FBaseSysLog.h>
27 #include "FUiAnim_VisualElement.h"
28 #include "FUiCtrl_ColorPicker.h"
29 #include "FUiCtrl_ColorPickerPresenter.h"
30 #include "FUiCtrl_IColorChangeEventListener.h"
31 #include "FUi_AccessibilityContainer.h"
32 #include "FUi_AccessibilityElement.h"
33 #include "FUi_ResourceManager.h"
34 #include "FUi_ResourceSizeInfo.h"
35 #include "FUi_CoordinateSystemUtils.h"
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Graphics;
40 using namespace Tizen::Ui;
41 using namespace Tizen::Ui::Animations;
42
43 namespace Tizen { namespace Ui { namespace Controls
44 {
45
46 IMPLEMENT_PROPERTY(_ColorPicker);
47
48 _ColorPicker::_ColorPicker(void)
49         : __pColorPickerPresenter(null)
50         , __pColorChangeEvent(null)
51         , __pHueHandler(null)
52         , __pSaturationHandler(null)
53         , __pLuminanceHandler(null)
54         , __pHueBarElement(null)
55         , __pSaturationBarElement(null)
56         , __pLuminanceBarElement(null)
57         , __currentFocusedHandler(HUE_HANDLER)
58         , __previousFocusedHandler(HSL_NOT)
59         , __isInFocusMode(false)
60 {
61 }
62
63 _ColorPicker::~_ColorPicker(void)
64 {
65         delete __pColorPickerPresenter;
66         __pColorPickerPresenter = null;
67
68         delete __pColorChangeEvent;
69         __pColorChangeEvent = null;
70
71         if (__pHueHandler != null)
72         {
73                 __pHueHandler->Destroy();
74                 __pHueHandler = null;
75         }
76
77         if (__pSaturationHandler != null)
78         {
79                 __pSaturationHandler->Destroy();
80                 __pSaturationHandler = null;
81         }
82
83         if (__pLuminanceHandler != null)
84         {
85                 __pLuminanceHandler->Destroy();
86                 __pLuminanceHandler = null;
87         }
88
89         if (__pHueBarElement)
90         {
91                 __pHueBarElement->Activate(false);
92                 __pHueBarElement = null;
93         }
94         if (__pSaturationBarElement)
95         {
96                 __pSaturationBarElement->Activate(false);
97                 __pSaturationBarElement = null;
98         }
99         if (__pLuminanceBarElement)
100         {
101                 __pLuminanceBarElement->Activate(false);
102                 __pLuminanceBarElement = null;
103         }
104 }
105
106 _ColorPicker*
107 _ColorPicker::CreateColorPickerN(void)
108 {
109         result r = E_SUCCESS;
110         Dimension colorPickerSize;
111         _VisualElement* pBase = null;
112
113         r = GET_DIMENSION_CONFIG(COLORPICKER::DEFAULT_SIZE, _ControlManager::GetInstance()->GetOrientation(), colorPickerSize);
114         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get the default ColorPicker size from config file.");
115
116         _ColorPicker* pColorPicker = new (std::nothrow) _ColorPicker;
117         SysTryReturn(NID_UI_CTRL, (pColorPicker != null), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
118
119         r = GetLastResult();
120         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
121
122         pColorPicker->AcquireHandle();
123
124         pColorPicker->__pColorPickerPresenter = _ColorPickerPresenter::CreateInstanceN(*pColorPicker);
125         r = GetLastResult();
126         SysTryCatch(NID_UI_CTRL, (pColorPicker->__pColorPickerPresenter != null), , r,
127                            "[%s] Propagating.", GetErrorMessage(r));
128
129         pColorPicker->__pColorChangeEvent = _ColorChangeEvent::CreateInstanceN(*pColorPicker);
130         r = GetLastResult();
131         SysTryCatch(NID_UI_CTRL, (pColorPicker->__pColorChangeEvent != null), , r,
132                            "[%s] Propagating.", GetErrorMessage(r));
133
134         pBase = pColorPicker->GetVisualElement();
135         SysTryCatch(NID_UI_CTRL, (pBase != null), r = E_SYSTEM, E_SYSTEM,
136                            "[E_SYSTEM] A system error has occurred. Failed to get the ColorPicker visual element.");
137
138         // Set alpha merge to VisualElement
139         pBase->SetSurfaceOpaque(false);
140         pColorPicker->SetBackgroundColor(Color());
141
142         r = pColorPicker->SetSize(colorPickerSize);
143         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
144
145         pColorPicker->SetResizable(false);
146
147         if (pColorPicker->GetAccessibilityContainer() != null)
148         {
149                 pColorPicker->GetAccessibilityContainer()->Activate(true);
150                 pColorPicker->InitializeAccessibilityElement();
151         }
152
153         return pColorPicker;
154
155 CATCH:
156         delete pColorPicker;
157         return null;
158 }
159
160 Color
161 _ColorPicker::GetColor(void) const
162 {
163         Variant color = GetProperty("color");
164
165         return color.ToColor();
166 }
167
168 Variant
169 _ColorPicker::GetPropertyColor(void) const
170 {
171         return Variant(__pColorPickerPresenter->GetColor());
172 }
173
174 int
175 _ColorPicker::GetHue(void) const
176 {
177         Variant varHue = GetProperty("hue");
178
179         return varHue.ToInt();
180 }
181
182 Variant
183 _ColorPicker::GetPropertyHue(void) const
184 {
185         return Variant(__pColorPickerPresenter->GetHue());
186 }
187
188 int
189 _ColorPicker::GetSaturation(void) const
190 {
191         Variant varSaturation = GetProperty("saturation");
192
193         return varSaturation.ToInt();
194 }
195
196 Variant
197 _ColorPicker::GetPropertySaturation(void) const
198 {
199         return Variant(__pColorPickerPresenter->GetSaturation());
200 }
201
202 int
203 _ColorPicker::GetLuminance(void) const
204 {
205         Variant varLuminance = GetProperty("luminance");
206
207         return varLuminance.ToInt();
208 }
209
210 Variant
211 _ColorPicker::GetPropertyLuminance(void) const
212 {
213         return Variant(__pColorPickerPresenter->GetLuminance());
214 }
215
216 result
217 _ColorPicker::SetColor(const Color& color)
218 {
219         return SetProperty("color", Variant(color));
220 }
221
222 result
223 _ColorPicker::SetPropertyColor(const Variant& color)
224 {
225         __pColorPickerPresenter->SetColor(color.ToColor());
226
227         return GetLastResult();
228 }
229
230 result
231 _ColorPicker::SetHue(int hue)
232 {
233         return SetProperty("hue", Variant(hue));
234 }
235
236 result
237 _ColorPicker::SetPropertyHue(const Variant& hue)
238 {
239         __pColorPickerPresenter->SetHue(hue.ToInt());
240
241         if (__pHueBarElement)
242         {
243                 String hueValue;
244                 hueValue.Append(__pColorPickerPresenter->GetHue());
245                 __pHueBarElement->SetValue(hueValue);
246         }
247
248         return GetLastResult();
249 }
250
251 result
252 _ColorPicker::SetSaturation(int saturation)
253 {
254         return SetProperty("saturation", Variant(saturation));
255 }
256
257 result
258 _ColorPicker::SetPropertySaturation(const Variant& saturation)
259 {
260         __pColorPickerPresenter->SetSaturation(saturation.ToInt());
261
262         if (__pSaturationBarElement)
263         {
264                 String saturationValue;
265                 saturationValue.Append(__pColorPickerPresenter->GetSaturation());
266                 __pSaturationBarElement->SetValue(saturationValue);
267         }
268
269         return GetLastResult();
270 }
271
272 result
273 _ColorPicker::SetLuminance(int luminance)
274 {
275         return SetProperty("luminance", Variant(luminance));
276 }
277
278 result
279 _ColorPicker::SetPropertyLuminance(const Variant& luminance)
280 {
281         __pColorPickerPresenter->SetLuminance(luminance.ToInt());
282
283         if (__pLuminanceBarElement)
284         {
285                 String luminanceValue;
286                 luminanceValue.Append(__pColorPickerPresenter->GetLuminance());
287                 __pLuminanceBarElement->SetValue(luminanceValue);
288         }
289
290         return GetLastResult();
291 }
292
293 bool
294 _ColorPicker::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
295 {
296         __pColorPickerPresenter->ClearFocus(__currentFocusedHandler);
297
298         return __pColorPickerPresenter->OnTouchPressed(source, touchinfo);
299 }
300
301 bool
302 _ColorPicker::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
303 {
304         return __pColorPickerPresenter->OnTouchMoved(source, touchinfo);
305 }
306
307 bool
308 _ColorPicker::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
309 {
310         return __pColorPickerPresenter->OnTouchReleased(source, touchinfo);
311 }
312
313 bool
314 _ColorPicker::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
315 {
316         return __pColorPickerPresenter->OnTouchCanceled(source, touchinfo);
317 }
318
319 result
320 _ColorPicker::OnAttachedToMainTree(void)
321 {
322         __pColorPickerPresenter->LoadDrawingProperties(GetBoundsF());
323
324         _VisualElement* pBase = GetVisualElement();
325
326         // Create All Handler
327         _ColorPickerComponentType handlerType = HUE_HANDLER;
328
329         if (__pHueHandler == null)
330         {
331                 __pHueHandler = __pColorPickerPresenter->CreateHandlerN(*pBase, handlerType);
332         }
333
334         SysTryReturnResult(NID_UI_CTRL, (__pHueHandler != null), E_SYSTEM,
335                         "A system error has occurred. Failed to create hue handler.");
336
337         handlerType = SAT_HANDLER;
338
339         if (__pSaturationHandler == null)
340         {
341                 __pSaturationHandler = __pColorPickerPresenter->CreateHandlerN(*pBase, handlerType);
342         }
343
344         SysTryReturnResult(NID_UI_CTRL, (__pSaturationHandler != null), E_SYSTEM,
345                         "A system error has occurred. Failed to create saturation handler.");
346
347         handlerType = LUM_HANDLER;
348
349         if (__pLuminanceHandler == null)
350         {
351                 __pLuminanceHandler = __pColorPickerPresenter->CreateHandlerN(*pBase, handlerType);
352         }
353
354         SysTryReturnResult(NID_UI_CTRL, (__pLuminanceHandler != null), E_SYSTEM,
355                         "A system error has occurred. Failed to create luminance handler.");
356
357         return E_SUCCESS;
358 }
359
360 void
361 _ColorPicker::InitializeAccessibilityElement(void)
362 {
363         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
364         _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
365         FloatRectangle floatBounds = GetClientBoundsF();
366         float blockHeight = 0.0f;
367
368         GET_SHAPE_CONFIG(COLORPICKER::BLOCK_HEIGHT, orientation, blockHeight);
369
370         if (pContainer != null)
371         {
372                 __pHueBarElement = new (std::nothrow) _AccessibilityElement(true);
373                 SysTryReturnVoidResult(NID_UI_CTRL, __pHueBarElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
374
375                 String hintText(L"flick up and down to adjust");
376
377                 __pHueBarElement->SetName("ColorPickerColorSlider");
378                 __pHueBarElement->SetLabel("Hue Slider");
379                 __pHueBarElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
380                 __pHueBarElement->SetHint(hintText);
381                 __pHueBarElement->SetSupportOperatingGesture(false);
382                 __pHueBarElement->SetBounds(FloatRectangle(floatBounds.x, floatBounds.y, floatBounds.width, blockHeight));
383                 pContainer->AddElement(*__pHueBarElement);
384
385                 String hueValue;
386                 hueValue.Append(__pColorPickerPresenter->GetHue());
387                 __pHueBarElement->SetValue(hueValue);
388
389                 __pSaturationBarElement = new (std::nothrow) _AccessibilityElement(true);
390                 SysTryReturnVoidResult(NID_UI_CTRL, __pSaturationBarElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
391
392                 __pSaturationBarElement->SetName("ColorPickerSaturationSlider");
393                 __pSaturationBarElement->SetLabel("Saturation Slider");
394                 __pSaturationBarElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
395                 __pSaturationBarElement->SetHint(hintText);
396                 __pSaturationBarElement->SetSupportOperatingGesture(false);
397                 __pSaturationBarElement->SetBounds(FloatRectangle(floatBounds.x, floatBounds.y + blockHeight, floatBounds.width, blockHeight));
398                 pContainer->AddElement(*__pSaturationBarElement);
399
400                 String saturationValue;
401                 saturationValue.Append(__pColorPickerPresenter->GetSaturation());
402                 __pSaturationBarElement->SetValue(saturationValue);
403
404                 __pLuminanceBarElement = new (std::nothrow) _AccessibilityElement(true);
405                 SysTryReturnVoidResult(NID_UI_CTRL, __pLuminanceBarElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
406
407                 __pLuminanceBarElement->SetName("ColorPickerBrightnessSlider");
408                 __pLuminanceBarElement->SetLabel("Luminance Slider");
409                 __pLuminanceBarElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
410                 __pLuminanceBarElement->SetHint(hintText);
411                 __pLuminanceBarElement->SetSupportOperatingGesture(false);
412                 __pLuminanceBarElement->SetBounds(FloatRectangle(floatBounds.x, floatBounds.y + 2 * blockHeight, floatBounds.width, blockHeight));
413                 pContainer->AddElement(*__pLuminanceBarElement);
414
415                 String luminanceValue;
416                 luminanceValue.Append(__pColorPickerPresenter->GetLuminance());
417                 __pLuminanceBarElement->SetValue(luminanceValue);
418
419                 pContainer->AddListener(*this);
420         }
421         return;
422 }
423
424 void
425 _ColorPicker::OnBoundsChanged(void)
426 {
427         __pColorPickerPresenter->LoadDrawingProperties(GetBoundsF());
428         return;
429 }
430
431 void
432 _ColorPicker::OnChangeLayout(_ControlOrientation orientation)
433 {
434         __pColorPickerPresenter->OnChangeLayout(orientation);
435         return;
436 }
437
438 void
439 _ColorPicker::OnDraw(void)
440 {
441         __pColorPickerPresenter->Draw();
442         return;
443 }
444
445 result
446 _ColorPicker::AddColorChangeEventListener(const _IColorChangeEventListener& listener)
447 {
448         result r = __pColorChangeEvent->AddListener(listener);
449
450         SysTryReturnResult(NID_UI_CTRL, (r != E_OBJ_ALREADY_EXIST), E_SYSTEM,
451                         "A system error has occurred. The _IColorChangeEventListener instance already exists in the event listener list.");
452
453         return r;
454 }
455
456 result
457 _ColorPicker::RemoveColorChangeEventListener(const _IColorChangeEventListener& listener)
458 {
459         result r = __pColorChangeEvent->RemoveListener(listener);
460
461         SysTryReturnResult(NID_UI_CTRL, (r != E_OBJ_NOT_FOUND), E_SYSTEM,
462                         "A system error has occurred. The _IColorChangeEventListener instance does not exist in the event listener list.");
463
464         return r;
465 }
466
467 result
468 _ColorPicker::FireColorChangeEvent(const Color& color)
469 {
470         IEventArg* pEventArg = _ColorChangeEvent::CreateColorChangeEventArgN(color);
471         result r = GetLastResult();
472         SysTryReturn(NID_UI_CTRL, (pEventArg != null), r, r, "[%s] Propagating.", GetErrorMessage(r));
473
474         __pColorChangeEvent->Fire(*pEventArg);
475
476         return E_SUCCESS;
477 }
478
479 _VisualElement*
480 _ColorPicker::GetHueHandler(void)
481 {
482         return __pHueHandler;
483 }
484
485 _VisualElement*
486 _ColorPicker::GetSaturationHandler(void)
487 {
488         return __pSaturationHandler;
489 }
490
491 _VisualElement*
492 _ColorPicker::GetLuminanceHandler(void)
493 {
494         return __pLuminanceHandler;
495 }
496
497 _AccessibilityElement*
498 _ColorPicker::GetAccessibilityElement(int elementId)
499 {
500         _AccessibilityElement* pAccessibilityElement = null;
501
502         if (likely(!(_AccessibilityManager::IsActivated())))
503         {
504                 return null;
505         }
506
507         switch (elementId)
508         {
509         case HUE_BAR:
510                 pAccessibilityElement = __pHueBarElement;
511                 break;
512         case SAT_BAR:
513                 pAccessibilityElement = __pSaturationBarElement;
514                 break;
515         case LUM_BAR:
516                 pAccessibilityElement = __pLuminanceBarElement;
517                 break;
518         }
519
520         return pAccessibilityElement;
521 }
522
523 result
524 _ColorPicker::UpdateView(void)
525 {
526         if (GetVisualElement() == null)
527         {
528                 return E_SYSTEM;
529         }
530
531         return GetVisualElement()->SetFlushNeeded();
532 }
533
534 bool
535 _ColorPicker::OnAccessibilityValueIncreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
536 {
537         String elementLabel = element.GetLabel();
538         String string;
539
540         if (elementLabel.Equals(L"Hue Slider", false))
541         {
542                 SetHue(GetHue() + 1);
543                 string.Append(GetHue());
544         }
545         else if (elementLabel.Equals(L"Saturation Slider", false))
546         {
547                 SetSaturation(GetSaturation() - 1);
548                 string.Append(GetSaturation());
549         }
550         else if (elementLabel.Equals(L"Luminance Slider", false))
551         {
552                 SetLuminance(GetLuminance() + 1);
553                 string.Append(GetLuminance());
554         }
555
556         Invalidate();
557         _AccessibilityManager::GetInstance()->ReadContent(string);
558         FireColorChangeEvent(GetColor());
559
560         return true;
561 }
562
563 bool
564 _ColorPicker::OnAccessibilityValueDecreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
565 {
566         String elementLabel = element.GetLabel();
567         String string;
568
569         if (elementLabel.Equals(L"Hue Slider", false))
570         {
571                 SetHue(GetHue() - 1);
572                 string.Append(GetHue());
573         }
574         else if (elementLabel.Equals(L"Saturation Slider", false))
575         {
576                 SetSaturation(GetSaturation() + 1);
577                 string.Append(GetSaturation());
578         }
579         else if (elementLabel.Equals(L"Luminance Slider", false))
580         {
581                 SetLuminance(GetLuminance() - 1);
582                 string.Append(GetLuminance());
583         }
584
585         Invalidate();
586         _AccessibilityManager::GetInstance()->ReadContent(string);
587
588         FireColorChangeEvent(GetColor());
589
590         return true;
591 }
592
593 bool
594 _ColorPicker::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
595 {
596         _KeyCode keyCode = keyInfo.GetKeyCode();
597
598         if (!__isInFocusMode)
599         {
600                 return false;
601         }
602
603         switch (keyCode)
604         {
605                 case _KEY_RIGHT:
606                         __pColorPickerPresenter->StepHandler(__currentFocusedHandler, true);
607                         break;
608
609                 case _KEY_LEFT:
610                         __pColorPickerPresenter->StepHandler(__currentFocusedHandler, false);
611                         break;
612
613                 case _KEY_DOWN:
614                         if (__currentFocusedHandler < LUM_HANDLER)
615                         {
616                                 __previousFocusedHandler = __currentFocusedHandler;
617                                 __currentFocusedHandler = static_cast<_ColorPickerComponentType>(static_cast<int>(__currentFocusedHandler) + 1);
618                                 __pColorPickerPresenter->DrawFocus(__currentFocusedHandler, __previousFocusedHandler);
619                         }
620                         break;
621
622                 case _KEY_UP:
623                         if (__currentFocusedHandler > HUE_HANDLER)
624                         {
625                                 __previousFocusedHandler = __currentFocusedHandler;
626                                 __currentFocusedHandler = static_cast<_ColorPickerComponentType>(static_cast<int>(__currentFocusedHandler) - 1);
627                                 __pColorPickerPresenter->DrawFocus(__currentFocusedHandler, __previousFocusedHandler);
628                         }
629                         break;
630
631                 default:
632                         return false;
633         }
634
635         return true;
636 }
637
638 bool
639 _ColorPicker::OnFocusLost(const _Control& source)
640 {
641         result r = E_SUCCESS;
642         r = __pColorPickerPresenter->ClearFocus(__currentFocusedHandler);
643         __previousFocusedHandler = HSL_NOT;
644         __currentFocusedHandler = HUE_HANDLER;
645
646         if (r != E_SUCCESS)
647         {
648                 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
649         }
650         r =  __pColorPickerPresenter->Draw();
651
652         if (r != E_SUCCESS)
653         {
654                 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
655         }
656         _Control::OnFocusLost(source);
657
658         return true;
659 }
660
661 void
662 _ColorPicker::OnDrawFocus(void)
663 {
664         __pColorPickerPresenter->DrawFocus(__currentFocusedHandler, __previousFocusedHandler);
665         __isInFocusMode = true;
666
667         return;
668 }
669
670 void
671 _ColorPicker::OnFocusModeStateChanged(void)
672 {
673         result r = E_SUCCESS;
674
675         r = __pColorPickerPresenter->ClearFocus(__currentFocusedHandler);
676         __previousFocusedHandler = HSL_NOT;
677         __currentFocusedHandler = HUE_HANDLER;
678         __isInFocusMode = false;
679
680         if (r != E_SUCCESS)
681         {
682                 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
683         }
684         Invalidate();
685
686         return;
687 }
688
689 }}} // Tizen::Ui::Controls