2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
18 * @file FUiCtrl_ColorPicker.cpp
19 * @brief This file contains implementation of _ColorPicker class
21 * This file contains implementation of _ColorPicker class.
25 #include <FBaseSysLog.h>
26 #include "FUiAnim_VisualElement.h"
27 #include "FUiCtrl_ColorPicker.h"
28 #include "FUiCtrl_ColorPickerPresenter.h"
29 #include "FUiCtrl_IColorChangeEventListener.h"
30 #include "FUi_AccessibilityContainer.h"
31 #include "FUi_AccessibilityElement.h"
32 #include "FUi_ResourceManager.h"
33 #include "FUi_ResourceSizeInfo.h"
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Graphics;
38 using namespace Tizen::Ui;
39 using namespace Tizen::Ui::Animations;
41 namespace Tizen { namespace Ui { namespace Controls
44 IMPLEMENT_PROPERTY(_ColorPicker);
46 _ColorPicker::_ColorPicker(void)
47 : __pColorPickerPresenter(null)
48 , __pColorChangeEvent(null)
50 , __pSaturationHandler(null)
51 , __pLuminanceHandler(null)
52 , __pDecreaseHueButtonElement(null)
53 , __pIncreaseHueButtonElement(null)
54 , __pDecreaseSaturationButtonElement(null)
55 , __pIncreaseSaturationButtonElement(null)
56 , __pDecreaseLuminanceButtonElement(null)
57 , __pIncreaseLuminanceButtonElement(null)
58 , __pHueBarElement(null)
59 , __pSaturationBarElement(null)
60 , __pLuminanceBarElement(null)
64 _ColorPicker::~_ColorPicker(void)
66 delete __pColorPickerPresenter;
67 __pColorPickerPresenter = null;
69 delete __pColorChangeEvent;
70 __pColorChangeEvent = null;
72 if (__pHueHandler != null)
74 __pHueHandler->Destroy();
78 if (__pSaturationHandler != null)
80 __pSaturationHandler->Destroy();
81 __pSaturationHandler = null;
84 if (__pLuminanceHandler != null)
86 __pLuminanceHandler->Destroy();
87 __pLuminanceHandler = null;
90 if (__pDecreaseHueButtonElement)
92 __pDecreaseHueButtonElement->Activate(false);
93 __pDecreaseHueButtonElement = null;
95 if (__pIncreaseHueButtonElement)
97 __pIncreaseHueButtonElement->Activate(false);
98 __pIncreaseHueButtonElement = null;
100 if (__pDecreaseSaturationButtonElement)
102 __pDecreaseSaturationButtonElement->Activate(false);
103 __pDecreaseSaturationButtonElement = null;
105 if (__pIncreaseSaturationButtonElement)
107 __pIncreaseSaturationButtonElement->Activate(false);
108 __pIncreaseSaturationButtonElement = null;
110 if (__pDecreaseLuminanceButtonElement)
112 __pDecreaseLuminanceButtonElement->Activate(false);
113 __pDecreaseLuminanceButtonElement = null;
115 if (__pIncreaseLuminanceButtonElement)
117 __pIncreaseLuminanceButtonElement->Activate(false);
118 __pIncreaseLuminanceButtonElement = null;
120 if (__pHueBarElement)
122 __pHueBarElement->Activate(false);
123 __pHueBarElement = null;
125 if (__pSaturationBarElement)
127 __pSaturationBarElement->Activate(false);
128 __pSaturationBarElement = null;
130 if (__pLuminanceBarElement)
132 __pLuminanceBarElement->Activate(false);
133 __pLuminanceBarElement = null;
138 _ColorPicker::CreateColorPickerN(void)
140 result r = E_SUCCESS;
141 Dimension colorPickerSize;
142 _VisualElement* pBase = null;
144 r = GET_DIMENSION_CONFIG(COLORPICKER::DEFAULT_SIZE, _ControlManager::GetInstance()->GetOrientation(), colorPickerSize);
145 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.");
147 _ColorPicker* pColorPicker = new (std::nothrow) _ColorPicker;
148 SysTryReturn(NID_UI_CTRL, (pColorPicker != null), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
151 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
153 pColorPicker->AcquireHandle();
155 pColorPicker->__pColorPickerPresenter = _ColorPickerPresenter::CreateInstanceN(*pColorPicker);
157 SysTryCatch(NID_UI_CTRL, (pColorPicker->__pColorPickerPresenter != null), , r,
158 "[%s] Propagating.", GetErrorMessage(r));
160 pColorPicker->__pColorChangeEvent = _ColorChangeEvent::CreateInstanceN(*pColorPicker);
162 SysTryCatch(NID_UI_CTRL, (pColorPicker->__pColorChangeEvent != null), , r,
163 "[%s] Propagating.", GetErrorMessage(r));
165 pBase = pColorPicker->GetVisualElement();
166 SysTryCatch(NID_UI_CTRL, (pBase != null), r = E_SYSTEM, E_SYSTEM,
167 "[E_SYSTEM] A system error has occurred. Failed to get the ColorPicker visual element.");
169 // Set alpha merge to VisualElement
170 pBase->SetSurfaceOpaque(false);
171 pColorPicker->SetBackgroundColor(Color());
173 r = pColorPicker->SetSize(colorPickerSize);
174 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
176 pColorPicker->SetResizable(false);
178 if (pColorPicker->GetAccessibilityContainer() != null)
180 pColorPicker->GetAccessibilityContainer()->Activate(true);
191 _ColorPicker::GetColor(void) const
193 Variant color = GetProperty("color");
195 return color.ToColor();
199 _ColorPicker::GetPropertyColor(void) const
201 return Variant(__pColorPickerPresenter->GetColor());
205 _ColorPicker::GetHue(void) const
207 Variant varHue = GetProperty("hue");
209 return varHue.ToInt();
213 _ColorPicker::GetPropertyHue(void) const
215 return Variant(__pColorPickerPresenter->GetHue());
219 _ColorPicker::GetSaturation(void) const
221 Variant varSaturation = GetProperty("saturation");
223 return varSaturation.ToInt();
227 _ColorPicker::GetPropertySaturation(void) const
229 return Variant(__pColorPickerPresenter->GetSaturation());
233 _ColorPicker::GetLuminance(void) const
235 Variant varLuminance = GetProperty("luminance");
237 return varLuminance.ToInt();
241 _ColorPicker::GetPropertyLuminance(void) const
243 return Variant(__pColorPickerPresenter->GetLuminance());
247 _ColorPicker::SetColor(const Color& color)
249 return SetProperty("color", Variant(color));
253 _ColorPicker::SetPropertyColor(const Variant& color)
255 __pColorPickerPresenter->SetColor(color.ToColor());
257 return GetLastResult();
261 _ColorPicker::SetHue(int hue)
263 return SetProperty("hue", Variant(hue));
267 _ColorPicker::SetPropertyHue(const Variant& hue)
269 __pColorPickerPresenter->SetHue(hue.ToInt());
271 if (__pHueBarElement)
274 hueValue.Append(__pColorPickerPresenter->GetHue());
275 __pHueBarElement->SetValue(hueValue);
278 return GetLastResult();
282 _ColorPicker::SetSaturation(int saturation)
284 return SetProperty("saturation", Variant(saturation));
288 _ColorPicker::SetPropertySaturation(const Variant& saturation)
290 __pColorPickerPresenter->SetSaturation(saturation.ToInt());
292 if (__pSaturationBarElement)
294 String saturationValue;
295 saturationValue.Append(__pColorPickerPresenter->GetSaturation());
296 __pSaturationBarElement->SetValue(saturationValue);
299 return GetLastResult();
303 _ColorPicker::SetLuminance(int luminance)
305 return SetProperty("luminance", Variant(luminance));
309 _ColorPicker::SetPropertyLuminance(const Variant& luminance)
311 __pColorPickerPresenter->SetLuminance(luminance.ToInt());
313 if (__pLuminanceBarElement)
315 String luminanceValue;
316 luminanceValue.Append(__pColorPickerPresenter->GetLuminance());
317 __pLuminanceBarElement->SetValue(luminanceValue);
320 return GetLastResult();
324 _ColorPicker::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
326 return __pColorPickerPresenter->OnTouchPressed(source, touchinfo);
330 _ColorPicker::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
332 return __pColorPickerPresenter->OnTouchMoved(source, touchinfo);
336 _ColorPicker::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
338 return __pColorPickerPresenter->OnTouchReleased(source, touchinfo);
342 _ColorPicker::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
344 return __pColorPickerPresenter->OnTouchCanceled(source, touchinfo);
348 _ColorPicker::OnAttachedToMainTree(void)
350 InitializeAccessibilityElement();
352 __pColorPickerPresenter->LoadDrawingProperties(GetBounds());
354 _VisualElement* pBase = GetVisualElement();
356 // Create All Handler
357 _ColorPickerComponentType handlerType = HUE_HANDLER;
359 if (__pHueHandler == null)
361 __pHueHandler = __pColorPickerPresenter->CreateHandlerN(*pBase, handlerType);
364 SysTryReturnResult(NID_UI_CTRL, (__pHueHandler != null), E_SYSTEM,
365 "A system error has occurred. Failed to create hue handler.");
367 handlerType = SAT_HANDLER;
369 if (__pSaturationHandler == null)
371 __pSaturationHandler = __pColorPickerPresenter->CreateHandlerN(*pBase, handlerType);
374 SysTryReturnResult(NID_UI_CTRL, (__pSaturationHandler != null), E_SYSTEM,
375 "A system error has occurred. Failed to create saturation handler.");
377 handlerType = LUM_HANDLER;
379 if (__pLuminanceHandler == null)
381 __pLuminanceHandler = __pColorPickerPresenter->CreateHandlerN(*pBase, handlerType);
384 SysTryReturnResult(NID_UI_CTRL, (__pLuminanceHandler != null), E_SYSTEM,
385 "A system error has occurred. Failed to create luminance handler.");
391 _ColorPicker::InitializeAccessibilityElement(void)
393 if (likely(!(_AccessibilityManager::IsActivated())))
398 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
399 if (pContainer != null)
401 __pDecreaseHueButtonElement = new (std::nothrow) _AccessibilityElement(true);
402 SysTryReturnVoidResult(NID_UI_CTRL, __pDecreaseHueButtonElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
404 __pDecreaseHueButtonElement->SetLabel(L"Decrease Hue value");
405 __pDecreaseHueButtonElement->SetTrait(ACCESSIBILITY_TRAITS_BUTTON);
407 pContainer->AddElement(*__pDecreaseHueButtonElement);
409 __pHueBarElement = new (std::nothrow) _AccessibilityElement(true);
410 SysTryReturnVoidResult(NID_UI_CTRL, __pHueBarElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
412 String hintText(L"Double tap and drag to adjust");
414 __pHueBarElement->SetLabel("Hue Slider");
415 __pHueBarElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
416 __pHueBarElement->SetHint(hintText);
417 pContainer->AddElement(*__pHueBarElement);
420 hueValue.Append(__pColorPickerPresenter->GetHue());
421 __pHueBarElement->SetValue(hueValue);
423 __pIncreaseHueButtonElement = new (std::nothrow) _AccessibilityElement(true);
424 SysTryReturnVoidResult(NID_UI_CTRL, __pIncreaseHueButtonElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
426 __pIncreaseHueButtonElement->SetLabel(L"Increase Hue value");
427 __pIncreaseHueButtonElement->SetTrait(ACCESSIBILITY_TRAITS_BUTTON);
428 pContainer->AddElement(*__pIncreaseHueButtonElement);
430 __pDecreaseSaturationButtonElement = new (std::nothrow) _AccessibilityElement(true);
431 SysTryReturnVoidResult(NID_UI_CTRL, __pDecreaseSaturationButtonElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
433 __pDecreaseSaturationButtonElement->SetLabel(L"Decrease Saturation value");
434 __pDecreaseSaturationButtonElement->SetTrait(ACCESSIBILITY_TRAITS_BUTTON);
435 pContainer->AddElement(*__pDecreaseSaturationButtonElement);
437 __pSaturationBarElement = new (std::nothrow) _AccessibilityElement(true);
438 SysTryReturnVoidResult(NID_UI_CTRL, __pSaturationBarElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
440 __pSaturationBarElement->SetLabel("Saturation Slider");
441 __pSaturationBarElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
442 __pSaturationBarElement->SetHint(hintText);
443 pContainer->AddElement(*__pSaturationBarElement);
445 String saturationValue;
446 saturationValue.Append(__pColorPickerPresenter->GetSaturation());
447 __pSaturationBarElement->SetValue(saturationValue);
449 __pIncreaseSaturationButtonElement = new (std::nothrow) _AccessibilityElement(true);
450 SysTryReturnVoidResult(NID_UI_CTRL, __pIncreaseSaturationButtonElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
452 __pIncreaseSaturationButtonElement->SetLabel(L"Increase Saturation value");
453 __pIncreaseSaturationButtonElement->SetTrait(ACCESSIBILITY_TRAITS_BUTTON);
454 pContainer->AddElement(*__pIncreaseSaturationButtonElement);
456 __pDecreaseLuminanceButtonElement = new (std::nothrow) _AccessibilityElement(true);
457 SysTryReturnVoidResult(NID_UI_CTRL, __pDecreaseLuminanceButtonElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
459 __pDecreaseLuminanceButtonElement->SetLabel(L"Decrease Luminance value");
460 __pDecreaseLuminanceButtonElement->SetTrait(ACCESSIBILITY_TRAITS_BUTTON);
461 pContainer->AddElement(*__pDecreaseLuminanceButtonElement);
463 __pLuminanceBarElement = new (std::nothrow) _AccessibilityElement(true);
464 SysTryReturnVoidResult(NID_UI_CTRL, __pLuminanceBarElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
466 __pLuminanceBarElement->SetLabel("Luminance Slider");
467 __pLuminanceBarElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
468 __pLuminanceBarElement->SetHint(hintText);
469 pContainer->AddElement(*__pLuminanceBarElement);
471 String luminanceValue;
472 luminanceValue.Append(__pColorPickerPresenter->GetLuminance());
473 __pLuminanceBarElement->SetValue(luminanceValue);
475 __pIncreaseLuminanceButtonElement = new (std::nothrow) _AccessibilityElement(true);
476 SysTryReturnVoidResult(NID_UI_CTRL, __pIncreaseLuminanceButtonElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
478 __pIncreaseLuminanceButtonElement->SetLabel(L"Increase Luminance value");
479 __pIncreaseLuminanceButtonElement->SetTrait(ACCESSIBILITY_TRAITS_BUTTON);
480 pContainer->AddElement(*__pIncreaseLuminanceButtonElement);
482 pContainer->AddListener(*this);
488 _ColorPicker::OnBoundsChanged(void)
490 __pColorPickerPresenter->LoadDrawingProperties(GetBounds());
495 _ColorPicker::OnChangeLayout(_ControlOrientation orientation)
497 __pColorPickerPresenter->OnChangeLayout(orientation);
502 _ColorPicker::OnDraw(void)
504 __pColorPickerPresenter->Draw();
509 _ColorPicker::AddColorChangeEventListener(const _IColorChangeEventListener& listener)
511 result r = __pColorChangeEvent->AddListener(listener);
513 SysTryReturnResult(NID_UI_CTRL, (r != E_OBJ_ALREADY_EXIST), E_SYSTEM,
514 "A system error has occurred. The _IColorChangeEventListener instance already exists in the event listener list.");
520 _ColorPicker::RemoveColorChangeEventListener(const _IColorChangeEventListener& listener)
522 result r = __pColorChangeEvent->RemoveListener(listener);
524 SysTryReturnResult(NID_UI_CTRL, (r != E_OBJ_NOT_FOUND), E_SYSTEM,
525 "A system error has occurred. The _IColorChangeEventListener instance does not exist in the event listener list.");
531 _ColorPicker::FireColorChangeEvent(const Color& color)
533 IEventArg* pEventArg = _ColorChangeEvent::CreateColorChangeEventArgN(color);
534 result r = GetLastResult();
535 SysTryReturn(NID_UI_CTRL, (pEventArg != null), r, r, "[%s] Propagating.", GetErrorMessage(r));
537 __pColorChangeEvent->Fire(*pEventArg);
543 _ColorPicker::GetHueHandler(void)
545 return __pHueHandler;
549 _ColorPicker::GetSaturationHandler(void)
551 return __pSaturationHandler;
555 _ColorPicker::GetLuminanceHandler(void)
557 return __pLuminanceHandler;
560 _AccessibilityElement*
561 _ColorPicker::GetAccessibilityElement(int elementId)
563 _AccessibilityElement* pAccessibilityElement = null;
565 if (likely(!(_AccessibilityManager::IsActivated())))
573 pAccessibilityElement = __pHueBarElement;
576 pAccessibilityElement = __pSaturationBarElement;
579 pAccessibilityElement = __pLuminanceBarElement;
582 pAccessibilityElement = __pDecreaseHueButtonElement;
585 pAccessibilityElement = __pIncreaseHueButtonElement;
588 pAccessibilityElement = __pDecreaseSaturationButtonElement;
591 pAccessibilityElement = __pIncreaseSaturationButtonElement;
594 pAccessibilityElement = __pDecreaseLuminanceButtonElement;
597 pAccessibilityElement = __pIncreaseLuminanceButtonElement;
601 return pAccessibilityElement;
605 _ColorPicker::UpdateView(void)
607 if (GetVisualElement() == null)
612 return GetVisualElement()->SetFlushNeeded();
616 _ColorPicker::OnAccessibilityValueIncreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
618 String elementLabel = element.GetLabel();
621 if (elementLabel.Equals(L"Hue Slider", false))
623 SetHue(GetHue() + 1);
624 string.Append(GetHue() + 1);
626 else if (elementLabel.Equals(L"Saturation Slider", false))
628 SetSaturation(GetSaturation() + 1);
629 string.Append(GetSaturation() + 1);
631 else if (elementLabel.Equals(L"Luminance Slider", false))
633 SetLuminance(GetLuminance() + 1);
634 string.Append(GetLuminance() + 1);
638 _AccessibilityManager::GetInstance()->ReadContent(string);
644 _ColorPicker::OnAccessibilityValueDecreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
646 String elementLabel = element.GetLabel();
649 if (elementLabel.Equals(L"Hue Slider", false))
651 SetHue(GetHue() - 1);
652 string.Append(GetHue() - 1);
654 else if (elementLabel.Equals(L"Saturation Slider", false))
656 SetSaturation(GetSaturation() - 1);
657 string.Append(GetSaturation() - 1);
659 else if (elementLabel.Equals(L"Luminance Slider", false))
661 SetLuminance(GetLuminance() - 1);
662 string.Append(GetLuminance() - 1);
666 _AccessibilityManager::GetInstance()->ReadContent(string);
671 }}} // Tizen::Ui::Controls