2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0/
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.
19 * @file FUiCtrl_ColorPicker.cpp
20 * @brief This file contains implementation of _ColorPicker class
22 * This file contains implementation of _ColorPicker class.
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"
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;
43 namespace Tizen { namespace Ui { namespace Controls
46 IMPLEMENT_PROPERTY(_ColorPicker);
48 _ColorPicker::_ColorPicker(void)
49 : __pColorPickerPresenter(null)
50 , __pColorChangeEvent(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)
63 _ColorPicker::~_ColorPicker(void)
65 delete __pColorPickerPresenter;
66 __pColorPickerPresenter = null;
68 delete __pColorChangeEvent;
69 __pColorChangeEvent = null;
71 if (__pHueHandler != null)
73 __pHueHandler->Destroy();
77 if (__pSaturationHandler != null)
79 __pSaturationHandler->Destroy();
80 __pSaturationHandler = null;
83 if (__pLuminanceHandler != null)
85 __pLuminanceHandler->Destroy();
86 __pLuminanceHandler = null;
91 __pHueBarElement->Activate(false);
92 __pHueBarElement = null;
94 if (__pSaturationBarElement)
96 __pSaturationBarElement->Activate(false);
97 __pSaturationBarElement = null;
99 if (__pLuminanceBarElement)
101 __pLuminanceBarElement->Activate(false);
102 __pLuminanceBarElement = null;
107 _ColorPicker::CreateColorPickerN(void)
109 result r = E_SUCCESS;
110 Dimension colorPickerSize;
111 _VisualElement* pBase = null;
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.");
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.");
120 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
122 pColorPicker->AcquireHandle();
124 pColorPicker->__pColorPickerPresenter = _ColorPickerPresenter::CreateInstanceN(*pColorPicker);
126 SysTryCatch(NID_UI_CTRL, (pColorPicker->__pColorPickerPresenter != null), , r,
127 "[%s] Propagating.", GetErrorMessage(r));
129 pColorPicker->__pColorChangeEvent = _ColorChangeEvent::CreateInstanceN(*pColorPicker);
131 SysTryCatch(NID_UI_CTRL, (pColorPicker->__pColorChangeEvent != null), , r,
132 "[%s] Propagating.", GetErrorMessage(r));
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.");
138 // Set alpha merge to VisualElement
139 pBase->SetSurfaceOpaque(false);
140 pColorPicker->SetBackgroundColor(Color());
142 r = pColorPicker->SetSize(colorPickerSize);
143 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
145 pColorPicker->SetResizable(false);
147 if (pColorPicker->GetAccessibilityContainer() != null)
149 pColorPicker->GetAccessibilityContainer()->Activate(true);
150 pColorPicker->InitializeAccessibilityElement();
161 _ColorPicker::GetColor(void) const
163 Variant color = GetProperty("color");
165 return color.ToColor();
169 _ColorPicker::GetPropertyColor(void) const
171 return Variant(__pColorPickerPresenter->GetColor());
175 _ColorPicker::GetHue(void) const
177 Variant varHue = GetProperty("hue");
179 return varHue.ToInt();
183 _ColorPicker::GetPropertyHue(void) const
185 return Variant(__pColorPickerPresenter->GetHue());
189 _ColorPicker::GetSaturation(void) const
191 Variant varSaturation = GetProperty("saturation");
193 return varSaturation.ToInt();
197 _ColorPicker::GetPropertySaturation(void) const
199 return Variant(__pColorPickerPresenter->GetSaturation());
203 _ColorPicker::GetLuminance(void) const
205 Variant varLuminance = GetProperty("luminance");
207 return varLuminance.ToInt();
211 _ColorPicker::GetPropertyLuminance(void) const
213 return Variant(__pColorPickerPresenter->GetLuminance());
217 _ColorPicker::SetColor(const Color& color)
219 return SetProperty("color", Variant(color));
223 _ColorPicker::SetPropertyColor(const Variant& color)
225 __pColorPickerPresenter->SetColor(color.ToColor());
227 return GetLastResult();
231 _ColorPicker::SetHue(int hue)
233 return SetProperty("hue", Variant(hue));
237 _ColorPicker::SetPropertyHue(const Variant& hue)
239 __pColorPickerPresenter->SetHue(hue.ToInt());
241 if (__pHueBarElement)
244 hueValue.Append(__pColorPickerPresenter->GetHue());
245 __pHueBarElement->SetValue(hueValue);
248 return GetLastResult();
252 _ColorPicker::SetSaturation(int saturation)
254 return SetProperty("saturation", Variant(saturation));
258 _ColorPicker::SetPropertySaturation(const Variant& saturation)
260 __pColorPickerPresenter->SetSaturation(saturation.ToInt());
262 if (__pSaturationBarElement)
264 String saturationValue;
265 saturationValue.Append(__pColorPickerPresenter->GetSaturation());
266 __pSaturationBarElement->SetValue(saturationValue);
269 return GetLastResult();
273 _ColorPicker::SetLuminance(int luminance)
275 return SetProperty("luminance", Variant(luminance));
279 _ColorPicker::SetPropertyLuminance(const Variant& luminance)
281 __pColorPickerPresenter->SetLuminance(luminance.ToInt());
283 if (__pLuminanceBarElement)
285 String luminanceValue;
286 luminanceValue.Append(__pColorPickerPresenter->GetLuminance());
287 __pLuminanceBarElement->SetValue(luminanceValue);
290 return GetLastResult();
294 _ColorPicker::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
296 __pColorPickerPresenter->ClearFocus(__currentFocusedHandler);
298 return __pColorPickerPresenter->OnTouchPressed(source, touchinfo);
302 _ColorPicker::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
304 return __pColorPickerPresenter->OnTouchMoved(source, touchinfo);
308 _ColorPicker::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
310 return __pColorPickerPresenter->OnTouchReleased(source, touchinfo);
314 _ColorPicker::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
316 return __pColorPickerPresenter->OnTouchCanceled(source, touchinfo);
320 _ColorPicker::OnAttachedToMainTree(void)
322 __pColorPickerPresenter->LoadDrawingProperties(GetBoundsF());
324 _VisualElement* pBase = GetVisualElement();
326 // Create All Handler
327 _ColorPickerComponentType handlerType = HUE_HANDLER;
329 if (__pHueHandler == null)
331 __pHueHandler = __pColorPickerPresenter->CreateHandlerN(*pBase, handlerType);
334 SysTryReturnResult(NID_UI_CTRL, (__pHueHandler != null), E_SYSTEM,
335 "A system error has occurred. Failed to create hue handler.");
337 handlerType = SAT_HANDLER;
339 if (__pSaturationHandler == null)
341 __pSaturationHandler = __pColorPickerPresenter->CreateHandlerN(*pBase, handlerType);
344 SysTryReturnResult(NID_UI_CTRL, (__pSaturationHandler != null), E_SYSTEM,
345 "A system error has occurred. Failed to create saturation handler.");
347 handlerType = LUM_HANDLER;
349 if (__pLuminanceHandler == null)
351 __pLuminanceHandler = __pColorPickerPresenter->CreateHandlerN(*pBase, handlerType);
354 SysTryReturnResult(NID_UI_CTRL, (__pLuminanceHandler != null), E_SYSTEM,
355 "A system error has occurred. Failed to create luminance handler.");
361 _ColorPicker::InitializeAccessibilityElement(void)
363 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
364 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
365 FloatRectangle floatBounds = GetClientBoundsF();
366 float blockHeight = 0.0f;
368 GET_SHAPE_CONFIG(COLORPICKER::BLOCK_HEIGHT, orientation, blockHeight);
370 if (pContainer != null)
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.");
375 String hintText(L"Double tap and drag to adjust");
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);
386 hueValue.Append(__pColorPickerPresenter->GetHue());
387 __pHueBarElement->SetValue(hueValue);
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.");
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);
400 String saturationValue;
401 saturationValue.Append(__pColorPickerPresenter->GetSaturation());
402 __pSaturationBarElement->SetValue(saturationValue);
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.");
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);
415 String luminanceValue;
416 luminanceValue.Append(__pColorPickerPresenter->GetLuminance());
417 __pLuminanceBarElement->SetValue(luminanceValue);
419 pContainer->AddListener(*this);
425 _ColorPicker::OnBoundsChanged(void)
427 __pColorPickerPresenter->LoadDrawingProperties(GetBoundsF());
432 _ColorPicker::OnChangeLayout(_ControlOrientation orientation)
434 __pColorPickerPresenter->OnChangeLayout(orientation);
439 _ColorPicker::OnDraw(void)
441 __pColorPickerPresenter->Draw();
446 _ColorPicker::AddColorChangeEventListener(const _IColorChangeEventListener& listener)
448 result r = __pColorChangeEvent->AddListener(listener);
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.");
457 _ColorPicker::RemoveColorChangeEventListener(const _IColorChangeEventListener& listener)
459 result r = __pColorChangeEvent->RemoveListener(listener);
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.");
468 _ColorPicker::FireColorChangeEvent(const Color& color)
470 IEventArg* pEventArg = _ColorChangeEvent::CreateColorChangeEventArgN(color);
471 result r = GetLastResult();
472 SysTryReturn(NID_UI_CTRL, (pEventArg != null), r, r, "[%s] Propagating.", GetErrorMessage(r));
474 __pColorChangeEvent->Fire(*pEventArg);
480 _ColorPicker::GetHueHandler(void)
482 return __pHueHandler;
486 _ColorPicker::GetSaturationHandler(void)
488 return __pSaturationHandler;
492 _ColorPicker::GetLuminanceHandler(void)
494 return __pLuminanceHandler;
497 _AccessibilityElement*
498 _ColorPicker::GetAccessibilityElement(int elementId)
500 _AccessibilityElement* pAccessibilityElement = null;
502 if (likely(!(_AccessibilityManager::IsActivated())))
510 pAccessibilityElement = __pHueBarElement;
513 pAccessibilityElement = __pSaturationBarElement;
516 pAccessibilityElement = __pLuminanceBarElement;
520 return pAccessibilityElement;
524 _ColorPicker::UpdateView(void)
526 if (GetVisualElement() == null)
531 return GetVisualElement()->SetFlushNeeded();
535 _ColorPicker::OnAccessibilityValueIncreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
537 String elementLabel = element.GetLabel();
540 if (elementLabel.Equals(L"Hue Slider", false))
542 SetHue(GetHue() + 1);
543 string.Append(GetHue());
545 else if (elementLabel.Equals(L"Saturation Slider", false))
547 SetSaturation(GetSaturation() - 1);
548 string.Append(GetSaturation());
550 else if (elementLabel.Equals(L"Luminance Slider", false))
552 SetLuminance(GetLuminance() + 1);
553 string.Append(GetLuminance());
557 _AccessibilityManager::GetInstance()->ReadContent(string);
558 FireColorChangeEvent(GetColor());
564 _ColorPicker::OnAccessibilityValueDecreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
566 String elementLabel = element.GetLabel();
569 if (elementLabel.Equals(L"Hue Slider", false))
571 SetHue(GetHue() - 1);
572 string.Append(GetHue());
574 else if (elementLabel.Equals(L"Saturation Slider", false))
576 SetSaturation(GetSaturation() + 1);
577 string.Append(GetSaturation());
579 else if (elementLabel.Equals(L"Luminance Slider", false))
581 SetLuminance(GetLuminance() - 1);
582 string.Append(GetLuminance());
586 _AccessibilityManager::GetInstance()->ReadContent(string);
588 FireColorChangeEvent(GetColor());
594 _ColorPicker::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
596 _KeyCode keyCode = keyInfo.GetKeyCode();
598 if (!__isInFocusMode)
606 __pColorPickerPresenter->StepHandler(__currentFocusedHandler, true);
610 __pColorPickerPresenter->StepHandler(__currentFocusedHandler, false);
614 if (__currentFocusedHandler < LUM_HANDLER)
616 __previousFocusedHandler = __currentFocusedHandler;
617 __currentFocusedHandler = static_cast<_ColorPickerComponentType>(static_cast<int>(__currentFocusedHandler) + 1);
618 __pColorPickerPresenter->DrawFocus(__currentFocusedHandler, __previousFocusedHandler);
623 if (__currentFocusedHandler > HUE_HANDLER)
625 __previousFocusedHandler = __currentFocusedHandler;
626 __currentFocusedHandler = static_cast<_ColorPickerComponentType>(static_cast<int>(__currentFocusedHandler) - 1);
627 __pColorPickerPresenter->DrawFocus(__currentFocusedHandler, __previousFocusedHandler);
639 _ColorPicker::OnFocusLost(const _Control& source)
641 result r = E_SUCCESS;
642 r = __pColorPickerPresenter->ClearFocus(__currentFocusedHandler);
643 __previousFocusedHandler = HSL_NOT;
644 __currentFocusedHandler = HUE_HANDLER;
648 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
650 r = __pColorPickerPresenter->Draw();
654 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
656 _Control::OnFocusLost(source);
662 _ColorPicker::OnDrawFocus(void)
664 __pColorPickerPresenter->DrawFocus(__currentFocusedHandler, __previousFocusedHandler);
665 __isInFocusMode = true;
671 _ColorPicker::OnFocusModeStateChanged(void)
673 result r = E_SUCCESS;
675 r = __pColorPickerPresenter->ClearFocus(__currentFocusedHandler);
676 __previousFocusedHandler = HSL_NOT;
677 __currentFocusedHandler = HUE_HANDLER;
678 __isInFocusMode = false;
682 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
689 }}} // Tizen::Ui::Controls