[AT-SPI] Use WeakHandle<Actor> in Accessible objects
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / push-button-impl.cpp
index 628fe67..2dc4385 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "push-button-impl.h"
 
 // EXTERNAL INCLUDES
-#include <algorithm>
-#include <dali/public-api/actors/image-actor.h>
+#include <dali/devel-api/scripting/scripting.h>
+#include <dali/integration-api/debug.h>
+#include <dali/public-api/object/type-registry-helper.h>
 #include <dali/public-api/object/type-registry.h>
 
 // INTERNAL INCLUDES
-#include "push-button-default-painter-impl.h"
+#include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
+#include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
+#include <dali-toolkit/public-api/controls/image-view/image-view.h>
+#include <dali-toolkit/public-api/controls/text-controls/text-label.h>
 
-#include <dali-toolkit/internal/controls/relayout-helper.h>
+#if defined(DEBUG_ENABLED)
+extern Debug::Filter* gLogButtonFilter;
+#endif
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
-
 namespace
 {
-
 BaseHandle Create()
 {
   return Toolkit::PushButton::New();
 }
 
-TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create );
-
-} // unnamed namespace
-
-namespace
-{
+// Properties
 
-const float TEXT_PADDING = 12.0f;
+DALI_TYPE_REGISTRATION_BEGIN(Toolkit::PushButton, Toolkit::Button, Create)
 
-/**
- * Find the first image actor in the actor hierarchy
- */
-ImageActor FindImageActor( Actor root )
-{
-  ImageActor imageActor = ImageActor::DownCast( root );
-  if( !imageActor && root )
-  {
-    for( unsigned int i = 0, numChildren = root.GetChildCount(); i < numChildren; ++i )
-    {
-      ImageActor childImageActor = FindImageActor( root.GetChildAt( i ) );
-      if( childImageActor )
-      {
-        return childImageActor;
-      }
-    }
-  }
+DALI_PROPERTY_REGISTRATION(Toolkit, PushButton, "labelPadding", STRING, LABEL_PADDING)
+DALI_PROPERTY_REGISTRATION(Toolkit, PushButton, "iconPadding", STRING, ICON_PADDING)
 
-  return imageActor;
-}
+DALI_TYPE_REGISTRATION_END()
 
+} // unnamed namespace
 
+namespace
+{
 } // unnamed namespace
 
 Dali::Toolkit::PushButton PushButton::New()
 {
   // Create the implementation, temporarily owned on stack
-  IntrusivePtr< PushButton > internalPushButton = new PushButton();
+  IntrusivePtr<PushButton> internalPushButton = new PushButton();
 
   // Pass ownership to CustomActor
-  Dali::Toolkit::PushButton pushButton( *internalPushButton );
+  Dali::Toolkit::PushButton pushButton(*internalPushButton);
 
   // Second-phase init of the implementation
   // This can only be done after the CustomActor connection has been made...
@@ -93,70 +77,146 @@ Dali::Toolkit::PushButton PushButton::New()
   return pushButton;
 }
 
-void PushButton::OnButtonInitialize()
-{
-  // Push button requires the Leave event.
-  Actor root = Self();
-  root.SetLeaveRequired( true );
-}
-
 PushButton::PushButton()
-: Button()
+: Button(),
+  mIconAlignment(RIGHT)
 {
-  // Creates specific painter.GetBu
-  ButtonPainterPtr painter = PushButtonDefaultPainterPtr( new PushButtonDefaultPainter() );
-  SetPainter( painter );
 }
 
 PushButton::~PushButton()
 {
-  SetPainter( NULL );
 }
 
-Vector3 PushButton::GetNaturalSize()
+void PushButton::OnInitialize()
 {
-  Vector3 size = Control::GetNaturalSize();
+  Button::OnInitialize();
 
-  const bool widthIsZero = EqualsZero( size.width );
-  const bool heightIsZero = EqualsZero( size.height );
+  // Push button requires the Leave event.
+  Actor self = Self();
+  self.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
 
-  if( widthIsZero || heightIsZero )
+  DevelControl::SetAccessibilityConstructor(self, [](Dali::Actor actor) {
+    return std::unique_ptr<Dali::Accessibility::Accessible>(
+      new AccessibleImpl(actor, Dali::Accessibility::Role::PUSH_BUTTON));
+  });
+}
+
+void PushButton::SetIconAlignment(const PushButton::IconAlignment iconAlignment)
+{
+  mIconAlignment = iconAlignment;
+  Button::Align labelAlignment;
+  switch(iconAlignment)
   {
-    // If background and background not scale9 try get size from that
-    ImageActor imageActor = FindImageActor( GetButtonImage() );
-    if( imageActor && imageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
+    case RIGHT:
+    {
+      labelAlignment = Button::BEGIN;
+      break;
+    }
+    case TOP:
+    {
+      labelAlignment = Button::BOTTOM;
+      break;
+    }
+    case BOTTOM:
     {
-      Vector3 imageSize = RelayoutHelper::GetNaturalSize( imageActor );
+      labelAlignment = Button::TOP;
+      break;
+    }
+    case LEFT:
+    default:
+      labelAlignment = Button::END;
+      break;
+  }
+
+  Button::SetLabelAlignment(labelAlignment);
+}
 
-      if( widthIsZero )
+const PushButton::IconAlignment PushButton::GetIconAlignment() const
+{
+  return mIconAlignment;
+}
+
+void PushButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
+{
+  Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast(Dali::BaseHandle(object));
+
+  if(pushButton)
+  {
+    PushButton& pushButtonImpl(GetImplementation(pushButton));
+
+    // Properties remain here for Tizen 3.0 legacy requirements. Are now in Button base class
+
+    switch(propertyIndex)
+    {
+      case Toolkit::PushButton::Property::LABEL_PADDING:
       {
-        size.width = imageSize.width;
+        Vector4 padding(value.Get<Vector4>());
+        pushButtonImpl.Button::SetLabelPadding(Padding(padding.x, padding.y, padding.z, padding.w));
+        break;
       }
-
-      if( heightIsZero )
+      case Toolkit::PushButton::Property::ICON_PADDING:
       {
-        size.height = imageSize.height;
+        Vector4 padding(value.Get<Vector4>());
+        pushButtonImpl.Button::SetForegroundPadding(Padding(padding.x, padding.y, padding.z, padding.w));
+        break;
       }
     }
+  }
+}
 
-    ImageActor backgroundImageActor = FindImageActor( GetBackgroundImage() );
-    if( backgroundImageActor && backgroundImageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
-    {
-      Vector3 imageSize = RelayoutHelper::GetNaturalSize( backgroundImageActor );
+Property::Value PushButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
+{
+  Property::Value value;
 
-      if( widthIsZero )
+  Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast(Dali::BaseHandle(object));
+
+  if(pushButton)
+  {
+    PushButton& pushButtonImpl(GetImplementation(pushButton));
+
+    switch(propertyIndex)
+    {
+      case Toolkit::PushButton::Property::LABEL_PADDING:
       {
-        size.width = std::max( size.width, imageSize.width );
+        Padding padding = pushButtonImpl.Button::GetLabelPadding();
+        value           = Vector4(padding.x, padding.y, padding.top, padding.bottom);
+        break;
       }
-
-      if( heightIsZero )
+      case Toolkit::PushButton::Property::ICON_PADDING:
       {
-        size.height = std::max( size.height, imageSize.height );
+        Padding padding = pushButtonImpl.Button::GetForegroundPadding();
+        value           = Vector4(padding.x, padding.y, padding.top, padding.bottom);
+        break;
       }
     }
   }
 
-  return size;
+  return value;
+}
+
+Dali::Accessibility::States PushButton::AccessibleImpl::CalculateStates()
+{
+  auto tmp                                 = Button::AccessibleImpl::CalculateStates();
+  auto slf                                 = Toolkit::Button::DownCast(Self());
+  tmp[Dali::Accessibility::State::PRESSED] = slf.GetProperty<bool>(Toolkit::Button::Property::SELECTED);
+  return tmp;
+}
+
+void PushButton::OnStateChange(State newState)
+{
+  // TODO: replace it with OnPropertySet hook once Button::Property::SELECTED will be consistently used
+  if(Dali::Accessibility::IsUp() && (Dali::Accessibility::Accessible::GetCurrentlyHighlightedActor() == Self())
+     && (newState == SELECTED_STATE || newState == UNSELECTED_STATE))
+  {
+    Dali::Accessibility::Accessible::Get(Self())->EmitStateChanged(
+      Dali::Accessibility::State::PRESSED, newState == SELECTED_STATE ? 1 : 0, 0);
+
+    if(Self().GetProperty<bool>(Toolkit::Button::Property::TOGGLABLE))
+    {
+      Dali::Accessibility::Accessible::Get(Self())->EmitStateChanged(
+        Dali::Accessibility::State::CHECKED, newState == SELECTED_STATE ? 1 : 0, 0);
+    }
+  }
 }
 
 } // namespace Internal