Refactored Button and derived classes, moving state change and transition logic to...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / radio-button-impl.cpp
index 2bcb901..1e51ce3 100644 (file)
 #include "radio-button-impl.h"
 
 // EXTERNAL INCLUDES
-#include <dali/public-api/actors/text-actor.h>
 #include <dali/public-api/object/type-registry.h>
-#include <dali/public-api/scripting/scripting.h>
+#include <dali/public-api/images/resource-image.h>
 
-using namespace Dali;
-using namespace Dali::Toolkit::Internal;
+namespace Dali
+{
+
+namespace Toolkit
+{
+
+namespace Internal
+{
 
 namespace
 {
@@ -39,8 +44,10 @@ TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolk
 
 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 char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-unselected-disabled.png";
+const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-selected-disabled.png";
 
-const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
+const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
 }
 
 Dali::Toolkit::RadioButton RadioButton::New()
@@ -59,188 +66,123 @@ Dali::Toolkit::RadioButton RadioButton::New()
 }
 
 RadioButton::RadioButton()
-  : mSelected(false)
 {
-  mUnselectedImage = Dali::Image::New( UNSELECTED_BUTTON_IMAGE_DIR );
-  mSelectedImage = Dali::Image::New( SELECTED_BUTTON_IMAGE_DIR );
-
-  mRadioIcon = Dali::ImageActor::New( mUnselectedImage );
+  SetTogglableButton(true);
 }
 
 RadioButton::~RadioButton()
 {
 }
 
-void RadioButton::SetLabel(const std::string& label)
+void RadioButton::OnButtonInitialize()
 {
-  TextActor textActor = TextActor::DownCast( mLabel );
-  if( textActor )
-  {
-    textActor.SetText( label );
-  }
-  else
-  {
-    Toolkit::TextView newTextView = Toolkit::TextView::New( label );
-    SetLabel( newTextView );
-  }
+  Actor self = Self();
+
+  // Wrap size of radio button around all its children
+  self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
+
+  Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
+  Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
+  Image disabledImage = Dali::ResourceImage::New( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
+  Image disabledSelectedImage = Dali::ResourceImage::New( DISABLED_SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
+
+  SetButtonImage( ImageActor::New( buttonImage ) );
+  SetSelectedImage( ImageActor::New( selectedImage ) );
+  SetDisabledImage( ImageActor::New( disabledImage ) );
+  SetDisabledSelectedImage( ImageActor::New( disabledSelectedImage ) );
 
   RelayoutRequest();
 }
 
-void RadioButton::SetLabel(Actor label)
+void RadioButton::OnButtonUp()
 {
-  if( mLabel != label )
+  if( ButtonDown == GetState() )
   {
-    if( mLabel )
+    // Don't allow selection on an already selected radio button
+    if( !IsSelected() )
     {
-      mRadioIcon.Remove( mLabel );
+      SetSelected( !IsSelected() );
     }
+  }
+}
 
-    if( label )
+void RadioButton::OnLabelSet()
+{
+  Actor& label = GetLabel();
+
+  if( label )
+  {
+    label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
+    label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
+
+    // Radio button width is FIT_TO_CHILDREN, so the label must have a sensible policy to fill out the space
+    if( label.GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::FILL_TO_PARENT )
     {
-      label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
-      label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
-      label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
-      mRadioIcon.Add( label );
+      label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
     }
 
-    mLabel = label;
-
-    RelayoutRequest();
+    if( IsSelected() && GetSelectedImage() )
+    {
+      label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+    else if( GetButtonImage() )
+    {
+      label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+    else
+    {
+      label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
   }
 }
 
-Actor RadioButton::GetLabel() const
+void RadioButton::OnSelected()
 {
-  return mLabel;
-}
+  Actor& label = GetLabel();
 
-void RadioButton::SetSelected(bool selected)
-{
-  if( mSelected != selected )
+  PaintState paintState = GetPaintState();
+  switch( paintState )
   {
-    if( selected )
+    case UnselectedState:
     {
       Actor parent = Self().GetParent();
       if( parent )
       {
         for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
         {
-          Dali::Toolkit::RadioButton rbChild = Dali::Toolkit::RadioButton::DownCast(parent.GetChildAt(i));
-
-          if( rbChild )
+          Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast( parent.GetChildAt( i ) );
+          if( radioButtonChild && radioButtonChild != Self() )
           {
-            rbChild.SetSelected(false);
+            radioButtonChild.SetSelected( false );
           }
         }
       }
 
-      mSelected = true;
-      mRadioIcon.SetImage(mSelectedImage);
+      Actor& selectedImage = GetSelectedImage();
+      if( label && selectedImage )
+      {
+        label.SetX( selectedImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+      }
+      break;
     }
-    else
+    case SelectedState:
     {
-      mSelected = false;
-      mRadioIcon.SetImage(mUnselectedImage);
-    }
-
-    // Raise state changed signal
-    Toolkit::RadioButton handle( GetOwner() );
-    mStateChangedSignal.Emit( handle, mSelected );
-
-    RelayoutRequest();
-  }
-}
-
-bool RadioButton::IsSelected()const
-{
-  return mSelected;
-}
-
-void RadioButton::ToggleState()
-{
-  SetSelected(!mSelected);
-}
-
-void RadioButton::OnRelayout( const Vector2& /*size*/, ActorSizeContainer& container )
-{
-  Vector3 newSize( mRadioIcon.GetNaturalSize() );
-
-  if( mLabel )
-  {
-    // 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( mLabel.GetNaturalSize() );
-    Control::Relayout( mLabel, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
-
-    Vector3 actorSize( mLabel.GetSize() );
-    newSize.width += actorSize.width;
-    newSize.height = std::max( newSize.height, actorSize.height );
-  }
-
-  Self().SetSize( newSize );
-}
-
-void RadioButton::OnInitialize()
-{
-  mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
-  mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
-  Self().Add( mRadioIcon );
-
-  RelayoutRequest();
-}
-
-void RadioButton::OnButtonUp()
-{
-  // Don't allow selection on an already selected radio button
-  if( !mSelected )
-  {
-    ToggleState();
-  }
-}
-
-void RadioButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
-{
-  Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle( object ) );
-
-  if( radioButton )
-  {
-    RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
-
-    if ( propertyIndex == Toolkit::Button::PROPERTY_TOGGLED )
-    {
-      radioButtonImpl.SetSelected( value.Get< bool >( ) );
+      Actor& buttonImage = GetButtonImage();
+      if( label && buttonImage )
+      {
+        label.SetX( buttonImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+      }
+      break;
     }
-    else if ( propertyIndex == Toolkit::Button::PROPERTY_LABEL_ACTOR )
+    default:
     {
-      radioButtonImpl.SetLabel( Scripting::NewActor( value.Get< Property::Map >( ) ) );
+      break;
     }
   }
 }
 
-Property::Value RadioButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
-{
-  Property::Value value;
-
-  Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle(object) );
+} // namespace Internal
 
-  if( radioButton )
-  {
-    RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
-
-    if ( propertyIndex == Toolkit::Button::PROPERTY_TOGGLED )
-    {
-      value = radioButtonImpl.mSelected;
-    }
-    else if ( propertyIndex == Toolkit::Button::PROPERTY_LABEL_ACTOR )
-    {
-      Property::Map map;
-      Scripting::CreatePropertyMap( radioButtonImpl.mLabel, map );
-      value = map;
-    }
-  }
+} // namespace Toolkit
 
-  return value;
-}
+} // namespace Dali