Changed indicator bg color.
[platform/framework/native/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                 __pHueBarElement->SetName("ColorPickerColorSlider");
376                 __pHueBarElement->SetLabel("Hue Slider");
377                 __pHueBarElement->SetHintWithStringId("IDS_TPLATFORM_BODY_FLICK_UP_AND_DOWN_TO_ADJUST_THE_POSITION_T_TTS");
378                 __pHueBarElement->SetSupportOperatingGesture(false);
379                 __pHueBarElement->SetBounds(FloatRectangle(floatBounds.x, floatBounds.y, floatBounds.width, blockHeight));
380                 pContainer->AddElement(*__pHueBarElement);
381
382                 String hueValue;
383                 hueValue.Append(__pColorPickerPresenter->GetHue());
384                 __pHueBarElement->SetValue(hueValue);
385
386                 __pSaturationBarElement = new (std::nothrow) _AccessibilityElement(true);
387                 SysTryReturnVoidResult(NID_UI_CTRL, __pSaturationBarElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
388
389                 __pSaturationBarElement->SetName("ColorPickerSaturationSlider");
390                 __pSaturationBarElement->SetLabel("Saturation Slider");
391                 __pSaturationBarElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
392                 __pSaturationBarElement->SetHintWithStringId("IDS_TPLATFORM_BODY_FLICK_UP_AND_DOWN_TO_ADJUST_THE_POSITION_T_TTS");
393                 __pSaturationBarElement->SetSupportOperatingGesture(false);
394                 __pSaturationBarElement->SetBounds(FloatRectangle(floatBounds.x, floatBounds.y + blockHeight, floatBounds.width, blockHeight));
395                 pContainer->AddElement(*__pSaturationBarElement);
396
397                 String saturationValue;
398                 saturationValue.Append(__pColorPickerPresenter->GetSaturation());
399                 __pSaturationBarElement->SetValue(saturationValue);
400
401                 __pLuminanceBarElement = new (std::nothrow) _AccessibilityElement(true);
402                 SysTryReturnVoidResult(NID_UI_CTRL, __pLuminanceBarElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
403
404                 __pLuminanceBarElement->SetName("ColorPickerBrightnessSlider");
405                 __pLuminanceBarElement->SetLabel("Luminance Slider");
406                 __pLuminanceBarElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
407                 __pLuminanceBarElement->SetHintWithStringId("IDS_TPLATFORM_BODY_FLICK_UP_AND_DOWN_TO_ADJUST_THE_POSITION_T_TTS");
408                 __pLuminanceBarElement->SetSupportOperatingGesture(false);
409                 __pLuminanceBarElement->SetBounds(FloatRectangle(floatBounds.x, floatBounds.y + 2 * blockHeight, floatBounds.width, blockHeight));
410                 pContainer->AddElement(*__pLuminanceBarElement);
411
412                 String luminanceValue;
413                 luminanceValue.Append(__pColorPickerPresenter->GetLuminance());
414                 __pLuminanceBarElement->SetValue(luminanceValue);
415
416                 pContainer->AddListener(*this);
417         }
418         return;
419 }
420
421 void
422 _ColorPicker::OnBoundsChanged(void)
423 {
424         __pColorPickerPresenter->LoadDrawingProperties(GetBoundsF());
425         return;
426 }
427
428 void
429 _ColorPicker::OnChangeLayout(_ControlOrientation orientation)
430 {
431         __pColorPickerPresenter->OnChangeLayout(orientation);
432         return;
433 }
434
435 void
436 _ColorPicker::OnDraw(void)
437 {
438         __pColorPickerPresenter->Draw();
439         return;
440 }
441
442 result
443 _ColorPicker::AddColorChangeEventListener(const _IColorChangeEventListener& listener)
444 {
445         result r = __pColorChangeEvent->AddListener(listener);
446
447         SysTryReturnResult(NID_UI_CTRL, (r != E_OBJ_ALREADY_EXIST), E_SYSTEM,
448                         "A system error has occurred. The _IColorChangeEventListener instance already exists in the event listener list.");
449
450         return r;
451 }
452
453 result
454 _ColorPicker::RemoveColorChangeEventListener(const _IColorChangeEventListener& listener)
455 {
456         result r = __pColorChangeEvent->RemoveListener(listener);
457
458         SysTryReturnResult(NID_UI_CTRL, (r != E_OBJ_NOT_FOUND), E_SYSTEM,
459                         "A system error has occurred. The _IColorChangeEventListener instance does not exist in the event listener list.");
460
461         return r;
462 }
463
464 result
465 _ColorPicker::FireColorChangeEvent(const Color& color)
466 {
467         IEventArg* pEventArg = _ColorChangeEvent::CreateColorChangeEventArgN(color);
468         result r = GetLastResult();
469         SysTryReturn(NID_UI_CTRL, (pEventArg != null), r, r, "[%s] Propagating.", GetErrorMessage(r));
470
471         __pColorChangeEvent->Fire(*pEventArg);
472
473         return E_SUCCESS;
474 }
475
476 _VisualElement*
477 _ColorPicker::GetHueHandler(void)
478 {
479         return __pHueHandler;
480 }
481
482 _VisualElement*
483 _ColorPicker::GetSaturationHandler(void)
484 {
485         return __pSaturationHandler;
486 }
487
488 _VisualElement*
489 _ColorPicker::GetLuminanceHandler(void)
490 {
491         return __pLuminanceHandler;
492 }
493
494 _AccessibilityElement*
495 _ColorPicker::GetAccessibilityElement(int elementId)
496 {
497         _AccessibilityElement* pAccessibilityElement = null;
498
499         if (likely(!(_AccessibilityManager::IsActivated())))
500         {
501                 return null;
502         }
503
504         switch (elementId)
505         {
506         case HUE_BAR:
507                 pAccessibilityElement = __pHueBarElement;
508                 break;
509         case SAT_BAR:
510                 pAccessibilityElement = __pSaturationBarElement;
511                 break;
512         case LUM_BAR:
513                 pAccessibilityElement = __pLuminanceBarElement;
514                 break;
515         }
516
517         return pAccessibilityElement;
518 }
519
520 result
521 _ColorPicker::UpdateView(void)
522 {
523         if (GetVisualElement() == null)
524         {
525                 return E_SYSTEM;
526         }
527
528         return GetVisualElement()->SetFlushNeeded();
529 }
530
531 bool
532 _ColorPicker::OnAccessibilityValueIncreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
533 {
534         if (GetEnableState() == false)
535         {
536                 return true;
537         }
538         String elementLabel = element.GetLabel();
539         String string;
540
541         if (elementLabel.Equals(L"Hue Slider", false))
542         {
543                 SetHue(GetHue() + 1);
544                 string.Append(GetHue());
545         }
546         else if (elementLabel.Equals(L"Saturation Slider", false))
547         {
548                 SetSaturation(GetSaturation() - 1);
549                 string.Append(GetSaturation());
550         }
551         else if (elementLabel.Equals(L"Luminance Slider", false))
552         {
553                 SetLuminance(GetLuminance() + 1);
554                 string.Append(GetLuminance());
555         }
556
557         Invalidate();
558         _AccessibilityManager::GetInstance()->ReadContent(string);
559         FireColorChangeEvent(GetColor());
560
561         return true;
562 }
563
564 bool
565 _ColorPicker::OnAccessibilityValueDecreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
566 {
567         if (GetEnableState() == false)
568         {
569                 return true;
570         }
571         String elementLabel = element.GetLabel();
572         String string;
573
574         if (elementLabel.Equals(L"Hue Slider", false))
575         {
576                 SetHue(GetHue() - 1);
577                 string.Append(GetHue());
578         }
579         else if (elementLabel.Equals(L"Saturation Slider", false))
580         {
581                 SetSaturation(GetSaturation() + 1);
582                 string.Append(GetSaturation());
583         }
584         else if (elementLabel.Equals(L"Luminance Slider", false))
585         {
586                 SetLuminance(GetLuminance() - 1);
587                 string.Append(GetLuminance());
588         }
589
590         Invalidate();
591         _AccessibilityManager::GetInstance()->ReadContent(string);
592
593         FireColorChangeEvent(GetColor());
594
595         return true;
596 }
597
598 bool
599 _ColorPicker::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
600 {
601         _KeyCode keyCode = keyInfo.GetKeyCode();
602
603         if (!__isInFocusMode)
604         {
605                 return false;
606         }
607
608         switch (keyCode)
609         {
610                 case _KEY_RIGHT:
611                         __pColorPickerPresenter->StepHandler(__currentFocusedHandler, true);
612                         break;
613
614                 case _KEY_LEFT:
615                         __pColorPickerPresenter->StepHandler(__currentFocusedHandler, false);
616                         break;
617
618                 case _KEY_DOWN:
619                         if (__currentFocusedHandler < LUM_HANDLER)
620                         {
621                                 __previousFocusedHandler = __currentFocusedHandler;
622                                 __currentFocusedHandler = static_cast<_ColorPickerComponentType>(static_cast<int>(__currentFocusedHandler) + 1);
623                                 __pColorPickerPresenter->DrawFocus(__currentFocusedHandler, __previousFocusedHandler);
624                         }
625                         break;
626
627                 case _KEY_UP:
628                         if (__currentFocusedHandler > HUE_HANDLER)
629                         {
630                                 __previousFocusedHandler = __currentFocusedHandler;
631                                 __currentFocusedHandler = static_cast<_ColorPickerComponentType>(static_cast<int>(__currentFocusedHandler) - 1);
632                                 __pColorPickerPresenter->DrawFocus(__currentFocusedHandler, __previousFocusedHandler);
633                         }
634                         break;
635
636                 default:
637                         return false;
638         }
639
640         return true;
641 }
642
643 bool
644 _ColorPicker::OnFocusLost(const _Control& source)
645 {
646         result r = E_SUCCESS;
647         r = __pColorPickerPresenter->ClearFocus(__currentFocusedHandler);
648         __previousFocusedHandler = HSL_NOT;
649         __currentFocusedHandler = HUE_HANDLER;
650
651         if (r != E_SUCCESS)
652         {
653                 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
654         }
655         r =  __pColorPickerPresenter->Draw();
656
657         if (r != E_SUCCESS)
658         {
659                 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
660         }
661         _Control::OnFocusLost(source);
662
663         return true;
664 }
665
666 void
667 _ColorPicker::OnDrawFocus(void)
668 {
669         __pColorPickerPresenter->DrawFocus(__currentFocusedHandler, __previousFocusedHandler);
670         __isInFocusMode = true;
671
672         return;
673 }
674
675 void
676 _ColorPicker::OnFocusModeStateChanged(void)
677 {
678         result r = E_SUCCESS;
679
680         r = __pColorPickerPresenter->ClearFocus(__currentFocusedHandler);
681         __previousFocusedHandler = HSL_NOT;
682         __currentFocusedHandler = HUE_HANDLER;
683         __isInFocusMode = false;
684
685         if (r != E_SUCCESS)
686         {
687                 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
688         }
689         Invalidate();
690
691         return;
692 }
693
694 }}} // Tizen::Ui::Controls