Merge "Add ApplyCustomFragmentPrefix" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / radio-button-impl.cpp
index 4ef25d9..4b4e070 100644 (file)
  *
  */
 
-
 // CLASS HEADER
 #include "radio-button-impl.h"
 
 // EXTERNAL INCLUDES
+#include <dali/integration-api/debug.h>
 #include <dali/public-api/object/type-registry.h>
-#include <dali/public-api/images/resource-image.h>
 
-using namespace Dali;
-using namespace Dali::Toolkit::Internal;
+#if defined(DEBUG_ENABLED)
+extern Debug::Filter* gLogButtonFilter;
+#endif
 
+namespace Dali
+{
+namespace Toolkit
+{
+namespace Internal
+{
 namespace
 {
-
 BaseHandle Create()
 {
   return Toolkit::RadioButton::New();
 }
 
-TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
+TypeRegistration typeRegistration(typeid(Toolkit::RadioButton), typeid(Toolkit::Button), Create);
 
-const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-unselected.png";
-const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-selected.png";
-
-const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
-}
+} // namespace
 
 Dali::Toolkit::RadioButton RadioButton::New()
 {
   // Create the implementation, temporarily owned on stack
-  IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
+  IntrusivePtr<RadioButton> internalRadioButton = new RadioButton();
 
   // Pass ownership to CustomActor
   Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
@@ -59,119 +60,74 @@ Dali::Toolkit::RadioButton RadioButton::New()
 
 RadioButton::RadioButton()
 {
-  mUnselectedImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR );
-  mSelectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR );
-
-  mRadioIcon = Dali::ImageActor::New( mUnselectedImage );
-
-//  SetTogglableButton(true);
-  mTogglableButton = true;    // TODO: Use SetTogglableButton() after refactoring painter
+  SetTogglableButton(true);
 }
 
 RadioButton::~RadioButton()
 {
 }
 
-void RadioButton::SetLabel( Actor label )
+void RadioButton::OnInitialize()
 {
-  if( mLabel != label )
-  {
-    if( mLabel )
-    {
-      mRadioIcon.Remove( mLabel );
-    }
-
-    if( label )
-    {
-      label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
-      label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
-      label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
-      mRadioIcon.Add( label );
-    }
+  Button::OnInitialize();
 
-    mLabel = label;
+  DevelControl::SetAccessibilityConstructor(Self(), [](Dali::Actor actor) {
+    return std::unique_ptr<Dali::Accessibility::Accessible>(
+      new AccessibleImpl(actor, Dali::Accessibility::Role::RADIO_BUTTON));
+  });
+}
 
-    RelayoutRequest();
-  }
+bool RadioButton::OnToggleReleased()
+{
+  // Radio button overrides toggle release (button up) as doesn't allow un-selection to be performed on it directly.
+  return false;
 }
 
-void RadioButton::SetSelected( bool selected )
+void RadioButton::OnStateChange(State newState)
 {
-  if( IsSelected() != selected )
+  // Radio button can be part of a group, if a button in the group is selected then all others should be unselected
+  DALI_LOG_INFO(gLogButtonFilter, Debug::Verbose, "RadioButton::OnStateChange state(%d)\n", newState);
+
+  if(SELECTED_STATE == newState)
   {
-    if( selected )
+    Actor parent = Self().GetParent();
+    if(parent)
     {
-      Actor parent = Self().GetParent();
-      if( parent )
+      for(unsigned int i = 0; i < parent.GetChildCount(); ++i)
       {
-        for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
+        Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
+        if(radioButtonChild && radioButtonChild != Self())
         {
-          Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
-
-          if( rbChild )
-          {
-            rbChild.SetSelected(false);
-          }
+          radioButtonChild.SetProperty(Toolkit::Button::Property::SELECTED, false);
         }
       }
-
-      mSelected = true;
-      mRadioIcon.SetImage(mSelectedImage);
     }
-    else
-    {
-      mSelected = false;
-      mRadioIcon.SetImage(mUnselectedImage);
-    }
-
-    // Raise state changed signal
-    Toolkit::RadioButton handle( GetOwner() );
-    StateChangedSignal().Emit( handle );
+  }
 
-    RelayoutRequest();
+  // 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::CHECKED, newState == SELECTED_STATE ? 1 : 0, 0);
   }
 }
 
-void RadioButton::OnRelayout( const Vector2& /*size*/, ActorSizeContainer& container )
+Dali::Accessibility::States RadioButton::AccessibleImpl::CalculateStates()
 {
-  Vector3 newSize( mRadioIcon.GetNaturalSize() );
-
-  Actor& label = GetLabel();
+  auto state = Button::AccessibleImpl::CalculateStates();
+  auto self = Toolkit::Button::DownCast(Self());
 
-  if( label )
+  if(self.GetProperty<bool>(Toolkit::Button::Property::SELECTED))
   {
-    // Offset the label from the radio button image
-    newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
-
-    // Find the size of the control using size negotiation
-    Vector3 actorNaturalSize( label.GetNaturalSize() );
-    Control::Relayout( label, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
-
-    Vector3 actorSize( label.GetSize() );
-    newSize.width += actorSize.width;
-    newSize.height = std::max( newSize.height, actorSize.height );
+    state[Dali::Accessibility::State::CHECKED] = true;
   }
 
-  Self().SetSize( newSize );
+  state[Dali::Accessibility::State::SELECTABLE] = true;
+  return state;
 }
 
-void RadioButton::OnInitialize()
-{
-  mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
-  mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
-  Self().Add( mRadioIcon );
+} // namespace Internal
 
-  RelayoutRequest();
-}
+} // namespace Toolkit
 
-void RadioButton::OnButtonUp()
-{
-  if( ButtonDown == GetState() )
-  {
-    // Don't allow selection on an already selected radio button
-    if( !IsSelected() )
-    {
-      SetSelected(!IsSelected());
-    }
-  }
-}
+} // namespace Dali