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_ButtonImpl.cpp
19 * @brief This is the implementation file for the _ButtonImpl class.
22 #include <FAppApplication.h>
23 #include <FBaseInternalTypes.h>
24 #include <FBaseSysLog.h>
25 #include <FApp_AppInfo.h>
26 #include "FUi_ResourceSizeInfo.h"
27 #include "FUi_ResourceManager.h"
28 #include "FUi_UiBuilder.h"
29 #include "FUiCtrl_ButtonImpl.h"
30 #include "FUiCtrl_Button.h"
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::Graphics;
36 namespace Tizen { namespace Ui { namespace Controls
40 _ButtonImpl::ButtonSizeInfo::GetDefaultMinimumSize(_ControlOrientation orientation) const
43 Dimension dimension(0, 0);
45 r = GET_DIMENSION_CONFIG(BUTTON::MIN_SIZE, orientation, dimension);
46 SysTryReturn(NID_UI, r == E_SUCCESS, dimension, r, "[%s] A system error occurred.", GetErrorMessage(r));
54 _ButtonImpl::GetInstance(Button& button)
56 return static_cast<_ButtonImpl*> (button._pControlImpl);
60 _ButtonImpl::GetInstance(const Button& button)
62 return static_cast<const _ButtonImpl*> (button._pControlImpl);
65 _ButtonImpl::_ButtonImpl(Button* pPublic, _Button* pCore)
66 : _ControlImpl(pPublic, pCore)
67 , __pPublicActionEvent(null)
72 _ButtonImpl::~_ButtonImpl(void)
74 _ButtonImpl::GetCore().RemoveActionEventListener(*this);
76 if (__pPublicActionEvent)
78 delete __pPublicActionEvent;
79 __pPublicActionEvent = null;
86 _ButtonImpl::CreateButtonImplN(Button* pControl, const Rectangle& bounds)
89 r = GET_SIZE_INFO(Button).CheckInitialSizeValid(Dimension(bounds.width, bounds.height), _CONTROL_ORIENTATION_PORTRAIT);
90 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, E_INVALID_ARG, "[E_INVALID_ARG] The given size is not valid.");
92 _Button* pCore = _Button::CreateButtonN();
93 SysTryReturn(NID_UI_CTRL, pCore, null, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
95 _ButtonImpl* pImpl = new (std::nothrow) _ButtonImpl(pControl, pCore);
96 r = _ControlImpl::CheckConstruction(pCore, pImpl);
97 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
99 r = pImpl->InitializeBoundsProperties(GET_SIZE_INFO(Button), bounds, pCore->GetOrientation());
100 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
102 r = pCore->AddActionEventListener(*pImpl);
103 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
115 _ButtonImpl::GetColorOnError(void)
117 return Color(0xFFFFFFFF);
121 _ButtonImpl::GetPublicClassName(void) const
123 return "Tizen::Ui::Controls::Button";
127 _ButtonImpl::GetPublic(void) const
129 return static_cast <const Button&>(_ControlImpl::GetPublic());
133 _ButtonImpl::GetPublic(void)
135 return static_cast <Button&>(_ControlImpl::GetPublic());
139 _ButtonImpl::GetCore(void) const
141 return static_cast <const _Button&>(_ControlImpl::GetCore());
145 _ButtonImpl::GetCore(void)
147 return static_cast <_Button&>(_ControlImpl::GetCore());
151 _ButtonImpl::SetText(const String& text)
153 int textLength = text.GetLength();
154 int buttonTextMaxLength = 0;
155 GET_FIXED_VALUE_CONFIG(BUTTON::TEXT_MAX_LENGTH, _CONTROL_ORIENTATION_PORTRAIT, buttonTextMaxLength);
156 String tempText = String(text);
158 if (textLength >= buttonTextMaxLength)
160 tempText.Remove(buttonTextMaxLength - 1, textLength - buttonTextMaxLength + 1);
163 Variant var(tempText);
164 result r = GetCore().SetPropertyText(var);
165 SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
171 _ButtonImpl::GetText(void) const
175 return GetCore().GetPropertyText().ToString();
179 _ButtonImpl::SetColor(ButtonStatus status, const Color& color)
181 _ButtonStatus coreStatus = ConvertStatus(status);
182 result r = GetCore().SetColor(coreStatus, color);
183 SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
189 _ButtonImpl::GetColor(ButtonStatus status) const
192 _ButtonStatus coreStatus = ConvertStatus(status);
194 return GetCore().GetColor(coreStatus);
198 _ButtonImpl::AddActionEventListener(IActionEventListener& listener)
200 if (__pPublicActionEvent == null)
202 __pPublicActionEvent = _PublicActionEvent::CreateInstanceN(GetPublic());
203 SysTryReturn(NID_UI_CTRL, __pPublicActionEvent, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
206 result r = __pPublicActionEvent->AddListener(listener);
207 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
213 _ButtonImpl::RemoveActionEventListener(IActionEventListener& listener)
215 result r = E_OBJ_NOT_FOUND;
217 if (__pPublicActionEvent)
219 r = __pPublicActionEvent->RemoveListener(listener);
222 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
228 _ButtonImpl::SetActionId(int actionId)
230 result r = GetCore().SetActionId(actionId);
232 SetLastResultReturn(r);
236 _ButtonImpl::GetActionId(void) const
240 return GetCore().GetActionId();
244 _ButtonImpl::SetTextHorizontalAlignment(HorizontalAlignment alignment)
246 result r = GetCore().SetTextHorizontalAlignment(alignment);
247 SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
253 _ButtonImpl::GetTextHorizontalAlignment(void) const
257 return GetCore().GetTextHorizontalAlignment();
261 _ButtonImpl::SetTextVerticalAlignment(VerticalAlignment alignment)
263 result r = GetCore().SetTextVerticalAlignment(alignment);
264 SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
270 _ButtonImpl::GetTextVerticalAlignment(void) const
274 return GetCore().GetTextVerticalAlignment();
278 _ButtonImpl::SetTextColor(const Color& color)
280 result r = GetCore().SetTextColor(_BUTTON_STATUS_NORMAL, color);
281 SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
287 _ButtonImpl::GetTextColor(void) const
291 return GetCore().GetTextColor(_BUTTON_STATUS_NORMAL);
295 _ButtonImpl::SetDisabledTextColor(const Color& color)
297 result r = GetCore().SetTextColor(_BUTTON_STATUS_DISABLED, color);
298 SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
304 _ButtonImpl::GetDisabledTextColor(void) const
308 return GetCore().GetTextColor(_BUTTON_STATUS_DISABLED);
312 _ButtonImpl::SetPressedTextColor(const Color& color)
314 result r = GetCore().SetTextColor(_BUTTON_STATUS_PRESSED, color);
315 SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
321 _ButtonImpl::GetPressedTextColor(void) const
325 return GetCore().GetTextColor(_BUTTON_STATUS_PRESSED);
329 _ButtonImpl::SetHighlightedTextColor(const Color& color)
331 result r = GetCore().SetTextColor(_BUTTON_STATUS_HIGHLIGHTED, color);
332 SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
338 _ButtonImpl::GetHighlightedTextColor(void) const
342 return GetCore().GetTextColor(_BUTTON_STATUS_HIGHLIGHTED);
346 _ButtonImpl::SetNormalBitmap(const Point& position, const Bitmap& bitmap)
348 result r = GetCore().SetBitmap(_BUTTON_STATUS_NORMAL, position, bitmap);
349 SetLastResultReturn(r);
353 _ButtonImpl::SetDisabledBitmap(const Point& position, const Bitmap& bitmap)
355 result r = GetCore().SetBitmap(_BUTTON_STATUS_DISABLED, position, bitmap);
356 SetLastResultReturn(r);
360 _ButtonImpl::SetPressedBitmap(const Point& position, const Bitmap& bitmap)
362 result r = GetCore().SetBitmap(_BUTTON_STATUS_PRESSED, position, bitmap);
363 SetLastResultReturn(r);
367 _ButtonImpl::SetNormalBackgroundBitmap(const Bitmap& bitmap)
369 result r = GetCore().SetBackgroundBitmap(_BUTTON_STATUS_NORMAL, bitmap);
370 SetLastResultReturn(r);
374 _ButtonImpl::SetPressedBackgroundBitmap(const Bitmap& bitmap)
376 result r = GetCore().SetBackgroundBitmap(_BUTTON_STATUS_PRESSED, bitmap);
377 SetLastResultReturn(r);
381 _ButtonImpl::SetHighlightedBackgroundBitmap(const Bitmap& bitmap)
383 result r = GetCore().SetBackgroundBitmap(_BUTTON_STATUS_HIGHLIGHTED, bitmap);
384 SetLastResultReturn(r);
388 _ButtonImpl::SetTextSize(int size)
390 result r = GetCore().SetTextSize(size);
391 SetLastResultReturn(r);
395 _ButtonImpl::GetTextSize(void) const
399 return GetCore().GetTextSize();
403 _ButtonImpl::OnActionPerformed(const _Control& source, int actionId)
405 if (__pPublicActionEvent != null)
407 __pPublicActionEvent->Fire(*_PublicActionEvent::CreateActionEventArgN(actionId));
414 _ButtonImpl::OnAttachedToMainTree(void)
418 _ControlImpl::OnAttachedToMainTree();
424 _ButtonImpl::OnTouchPressed(const _ControlImpl& source, const _TouchInfo& touchinfo)
426 if ((_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()) && (!source.IsEnabled() || !source.GetEnableState()))
435 _ButtonImpl::OnTouchReleased(const _ControlImpl& source, const _TouchInfo& touchinfo)
437 if ((_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()) && (!source.IsEnabled() || !source.GetEnableState()))
446 _ButtonImpl::OnTouchMoved(const _ControlImpl& source, const _TouchInfo& touchinfo)
448 if ((_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()) && (!source.IsEnabled() || !source.GetEnableState()))
457 _ButtonImpl::OnTouchCanceled(const _ControlImpl& source, const _TouchInfo& touchinfo)
459 if ((_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()) && (!source.IsEnabled() || !source.GetEnableState()))
467 Tizen::Graphics::Dimension
468 _ButtonImpl::GetContentSize(void) const
470 return GetCore().GetContentSizeInternal();
474 _ButtonImpl::ConvertStatus(ButtonStatus status) const
476 _ButtonStatus coreStatus = _BUTTON_STATUS_HIGHLIGHTED;
479 case BUTTON_STATUS_NORMAL:
480 coreStatus = _BUTTON_STATUS_NORMAL;
482 case BUTTON_STATUS_DISABLED:
483 coreStatus = _BUTTON_STATUS_DISABLED;
485 case BUTTON_STATUS_PRESSED:
486 coreStatus = _BUTTON_STATUS_PRESSED;
489 coreStatus = _BUTTON_STATUS_HIGHLIGHTED;
496 : public _UiBuilderControlMaker
499 _ButtonMaker(_UiBuilder* uibuilder)
500 : _UiBuilderControlMaker(uibuilder){};
502 virtual ~_ButtonMaker(){};
503 static _UiBuilderControlMaker* GetInstance(_UiBuilder* uibuilder)
505 _ButtonMaker* pButtonMaker = new (std::nothrow) _ButtonMaker(uibuilder);
506 return static_cast<_UiBuilderControlMaker*>(pButtonMaker);
509 virtual Control* Make(_UiBuilderControl* pControl)
513 AppResource* pAppResource = null;
514 UiApp* pUiApp = UiApp::GetInstance();
517 pAppResource = UiApp::GetInstance()->GetAppResource();
520 _UiBuilderControlLayout* pControlProperty = null;
521 Button* pButton = null;
522 HorizontalAlignment horizontalAlignment;
523 VerticalAlignment verticalAlignment;
528 Rectangle buttonRect(0, 0, 0, 0);
529 Tizen::Base::String elementString;
531 GetProperty(pControl, &pControlProperty);
533 if (pControlProperty == null)
538 pButton = new (std::nothrow) Button();
544 buttonRect = pControlProperty->GetRect();
546 if (pControl->GetElement(L"text", elementString))
548 r = pButton->Construct(buttonRect, elementString);
552 r = pButton->Construct(buttonRect);
561 if (pControl->GetElement(L"horizontalAlign", elementString) || pControl->GetElement(L"hAlign", elementString))
563 if (ConvertHAlignToHorizontalAlignment(elementString, horizontalAlignment))
565 pButton->SetTextHorizontalAlignment(horizontalAlignment);
569 if (pControl->GetElement(L"verticalAlign", elementString) || pControl->GetElement(L"vAlign", elementString))
571 if (ConvertVAlignToVerticalAlignment(elementString, verticalAlignment))
573 pButton->SetTextVerticalAlignment(verticalAlignment);
577 if (pControl->GetElement(L"normalTextColor", elementString))
579 ConvertStringToColor(elementString, color);
580 pButton->SetTextColor(color);
583 if (pControl->GetElement(L"pressedTextColor", elementString))
585 ConvertStringToColor(elementString, color);
586 pButton->SetPressedTextColor(color);
589 if (pControl->GetElement(L"disableTextColor", elementString))
591 ConvertStringToColor(elementString, color);
592 pButton->SetDisabledTextColor(color);
595 if (pControl->GetElement(L"normalBitmapPath", elementString) || pControl->GetElement(L"NormalBitmapPath", elementString))
597 Bitmap* pNormalBitmap = null;
600 pNormalBitmap = pAppResource->GetBitmapN(elementString);
601 if (pControl->GetElement(L"NormalBitmapX", elementString))
603 Base::Integer::Parse(elementString, position.x);
604 if ((position.x < 0) || (position.x > ((Control*) (GetContainer()))->GetBounds().width - ((Control*) (GetContainer()))->GetBounds().x))
610 if (pControl->GetElement(L"NormalBitmapY", elementString))
612 Base::Integer::Parse(elementString, position.y);
613 if (position.y < 0 || (position.y > ((Control*) (GetContainer()))->GetBounds().width - ((Control*) (GetContainer()))->GetBounds().x))
619 if (pNormalBitmap != null)
621 pButton->SetNormalBitmap(position, *pNormalBitmap);
622 delete pNormalBitmap;
626 if (pControl->GetElement(L"PressedBitmapPath", elementString))
628 Bitmap* pPressedBitmap = null;
631 pPressedBitmap = pAppResource->GetBitmapN(elementString);
632 if (pControl->GetElement(L"PressedBitmapX", elementString))
634 Base::Integer::Parse(elementString, position.x);
635 if ((position.x < 0) || (position.x > ((Control*) (GetContainer()))->GetBounds().width - ((Control*) (GetContainer()))->GetBounds().x))
641 if (pControl->GetElement(L"PressedBitmapY", elementString))
643 Base::Integer::Parse(elementString, position.y);
644 if (position.y < 0 || (position.y > ((Control*) (GetContainer()))->GetBounds().width - ((Control*) (GetContainer()))->GetBounds().x))
650 if (pPressedBitmap != null)
652 pButton->SetPressedBitmap(position, *pPressedBitmap);
653 delete pPressedBitmap;
657 if (pControl->GetElement(L"DisabledBitmapPath", elementString))
659 Bitmap* pDisabledBitmap = null;
661 pDisabledBitmap = pAppResource->GetBitmapN(elementString);
662 if (pControl->GetElement(L"DisabledBitmapX", elementString))
664 Base::Integer::Parse(elementString, position.x);
665 if ((position.x < 0) || (position.x > ((Control*) (GetContainer()))->GetBounds().width - ((Control*) (GetContainer()))->GetBounds().x))
671 if (pControl->GetElement(L"DisabledBitmapY", elementString))
673 Base::Integer::Parse(elementString, position.y);
674 if (position.y < 0 || (position.y > ((Control*) (GetContainer()))->GetBounds().width - ((Control*) (GetContainer()))->GetBounds().x))
680 if (pDisabledBitmap != null)
682 pButton->SetDisabledBitmap(position, *pDisabledBitmap);
683 delete pDisabledBitmap;
687 if (pControl->GetElement(L"NormalBGBitmapPath", elementString))
689 Bitmap* pNormalBGBitmap = null;
690 pNormalBGBitmap = pAppResource->GetBitmapN(elementString);
691 if (pNormalBGBitmap != null)
693 pButton->SetNormalBackgroundBitmap(*pNormalBGBitmap);
694 delete pNormalBGBitmap;
698 if (pControl->GetElement(L"PressedBGBitmapPath", elementString))
700 Bitmap* pPressedBGBitmap = null;
701 pPressedBGBitmap = pAppResource->GetBitmapN(elementString);
702 if (pPressedBGBitmap != null)
704 pButton->SetPressedBackgroundBitmap(*pPressedBGBitmap);
705 delete pPressedBGBitmap;
709 if (pControl->GetElement(L"HighlightedBGBitmapPath", elementString))
711 Bitmap* pHighlightedBGBitmap = null;
712 pHighlightedBGBitmap = pAppResource->GetBitmapN(elementString);
713 if (pHighlightedBGBitmap != null)
715 pButton->SetHighlightedBackgroundBitmap(*pHighlightedBGBitmap);
716 delete pHighlightedBGBitmap;
720 if (pControl->GetElement(L"highlightedTextColor", elementString))
722 ConvertStringToColor(elementString, color);
723 pButton->SetHighlightedTextColor(color);
726 if (pControl->GetElement(L"normalColorOpacity", elementString))
728 Base::Integer::Parse(elementString, opacity);
731 if (pControl->GetElement(L"normalColor", elementString))
733 ConvertStringToColor32(elementString, opacity, color);
734 pButton->SetColor(BUTTON_STATUS_NORMAL, color);
737 if (pControl->GetElement(L"pressedColorOpacity", elementString))
739 Base::Integer::Parse(elementString, opacity);
742 if (pControl->GetElement(L"pressedColor", elementString))
744 ConvertStringToColor32(elementString, opacity, color);
745 pButton->SetColor(BUTTON_STATUS_PRESSED, color);
748 if (pControl->GetElement(L"disabledColorOpacity", elementString))
750 Base::Integer::Parse(elementString, opacity);
753 if (pControl->GetElement(L"disabledColor", elementString))
755 ConvertStringToColor32(elementString, opacity, color);
756 pButton->SetColor(BUTTON_STATUS_DISABLED, color);
759 if (pControl->GetElement(L"highlightedColorOpacity", elementString))
761 Base::Integer::Parse(elementString, opacity);
764 if (pControl->GetElement(L"highlightedColor", elementString))
766 ConvertStringToColor32(elementString, opacity, color);
767 pButton->SetColor(BUTTON_STATUS_HIGHLIGHTED, color);
770 if (pControl->GetElement(L"textSize", elementString))
772 _ICoordinateSystemTransformer* pTransform = GetTransformer();
773 Base::Integer::Parse(elementString, size);
776 size = pTransform->Transform(size);
779 pButton->SetTextSize(size);
787 _ButtonRegister::_ButtonRegister()
789 _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
790 pUiBuilderControlTableManager->RegisterControl(L"Button", _ButtonMaker::GetInstance);
792 _ButtonRegister::~_ButtonRegister()
794 _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
795 pUiBuilderControlTableManager->UnregisterControl(L"Button");
797 static _ButtonRegister ButtonRegisterToUiBuilder;
798 }}} // Tizen::Ui::Controls