New size negotiation
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / radio-button-impl.cpp
index 2bcb901..05dba27 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
 {
@@ -40,7 +45,7 @@ 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 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 +64,112 @@ 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::SetImage( Actor image )
 {
-  TextActor textActor = TextActor::DownCast( mLabel );
-  if( textActor )
-  {
-    textActor.SetText( label );
-  }
-  else
-  {
-    Toolkit::TextView newTextView = Toolkit::TextView::New( label );
-    SetLabel( newTextView );
-  }
+  mLayoutContainer.RemoveChildAt( Toolkit::TableView::CellPosition( 0, 0 ) );
+  mLayoutContainer.AddChild( image, Toolkit::TableView::CellPosition( 0, 0 ) );
 
   RelayoutRequest();
 }
 
-void RadioButton::SetLabel(Actor label)
-{
-  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 );
-    }
-
-    mLabel = label;
-
-    RelayoutRequest();
-  }
-}
-
-Actor RadioButton::GetLabel() const
-{
-  return mLabel;
-}
-
-void RadioButton::SetSelected(bool selected)
-{
-  if( mSelected != selected )
-  {
-    if( selected )
-    {
-      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 )
-          {
-            rbChild.SetSelected(false);
-          }
-        }
-      }
-
-      mSelected = true;
-      mRadioIcon.SetImage(mSelectedImage);
-    }
-    else
-    {
-      mSelected = false;
-      mRadioIcon.SetImage(mUnselectedImage);
-    }
-
-    // Raise state changed signal
-    Toolkit::RadioButton handle( GetOwner() );
-    mStateChangedSignal.Emit( handle, mSelected );
-
-    RelayoutRequest();
-  }
-}
-
-bool RadioButton::IsSelected()const
+void RadioButton::SetButtonImage( Actor image )
 {
-  return mSelected;
+  Actor& buttonImage = GetButtonImage();
+  buttonImage = image;
 }
 
-void RadioButton::ToggleState()
+void RadioButton::SetSelectedImage( Actor image )
 {
-  SetSelected(!mSelected);
+  Actor& selectedImage = GetSelectedImage();
+  selectedImage = image;
 }
 
-void RadioButton::OnRelayout( const Vector2& /*size*/, ActorSizeContainer& container )
+void RadioButton::OnButtonInitialize()
 {
-  Vector3 newSize( mRadioIcon.GetNaturalSize() );
+  Actor self = Self();
 
-  if( mLabel )
-  {
-    // Offset the label from the radio button image
-    newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
+  // Wrap size of radio button around all its children
+  self.SetResizePolicy( FIT_TO_CHILDREN, ALL_DIMENSIONS );
 
-    // Find the size of the control using size negotiation
-    Vector3 actorNaturalSize( mLabel.GetNaturalSize() );
-    Control::Relayout( mLabel, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
+  // Create the layout container empty at first
+  mLayoutContainer = Toolkit::TableView::New( 0, 0 );
+  mLayoutContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+  mLayoutContainer.SetParentOrigin( ParentOrigin::TOP_LEFT );
+  mLayoutContainer.SetResizePolicy( FIT_TO_CHILDREN, ALL_DIMENSIONS );
+  self.Add( mLayoutContainer );
 
-    Vector3 actorSize( mLabel.GetSize() );
-    newSize.width += actorSize.width;
-    newSize.height = std::max( newSize.height, actorSize.height );
-  }
+  Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR );
+  Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR );
 
-  Self().SetSize( newSize );
-}
+  SetButtonImage( ImageActor::New( buttonImage ) );
+  SetSelectedImage( ImageActor::New( selectedImage ) );
 
-void RadioButton::OnInitialize()
-{
-  mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
-  mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
-  Self().Add( mRadioIcon );
+  SetImage( GetButtonImage() );
 
   RelayoutRequest();
 }
 
 void RadioButton::OnButtonUp()
 {
-  // Don't allow selection on an already selected radio button
-  if( !mSelected )
+  if( ButtonDown == GetState() )
   {
-    ToggleState();
+    // Don't allow selection on an already selected radio button
+    if( !IsSelected() )
+    {
+      SetSelected( !IsSelected() );
+    }
   }
 }
 
-void RadioButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value)
+void RadioButton::OnLabelSet()
 {
-  Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle( object ) );
+  Actor& label = GetLabel();
 
-  if( radioButton )
+  if( label )
   {
-    RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
+    // Add padding to the left of the label to create distance from the image
+    label.SetPadding( Padding( DISTANCE_BETWEEN_IMAGE_AND_LABEL, 0.0f, 0.0f, 0.0f ) );
 
-    if ( propertyIndex == Toolkit::Button::PROPERTY_TOGGLED )
-    {
-      radioButtonImpl.SetSelected( value.Get< bool >( ) );
-    }
-    else if ( propertyIndex == Toolkit::Button::PROPERTY_LABEL_ACTOR )
-    {
-      radioButtonImpl.SetLabel( Scripting::NewActor( value.Get< Property::Map >( ) ) );
-    }
+    mLayoutContainer.RemoveChildAt( Toolkit::TableView::CellPosition( 0, 1 ) );
+    mLayoutContainer.AddChild( label, Toolkit::TableView::CellPosition( 0, 1 ) );
   }
 }
 
-Property::Value RadioButton::GetProperty(BaseObject* object, Property::Index propertyIndex)
+void RadioButton::OnSelected( bool selected )
 {
-  Property::Value value;
-
-  Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle(object) );
-
-  if( radioButton )
+  if( selected )
   {
-    RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
-
-    if ( propertyIndex == Toolkit::Button::PROPERTY_TOGGLED )
-    {
-      value = radioButtonImpl.mSelected;
-    }
-    else if ( propertyIndex == Toolkit::Button::PROPERTY_LABEL_ACTOR )
+    Actor parent = Self().GetParent();
+    if( parent )
     {
-      Property::Map map;
-      Scripting::CreatePropertyMap( radioButtonImpl.mLabel, map );
-      value = map;
+      for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
+      {
+        Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast( parent.GetChildAt( i ) );
+        if( radioButtonChild )
+        {
+          radioButtonChild.SetSelected( false );
+        }
+      }
     }
-  }
 
-  return value;
+    SetImage( GetSelectedImage() );
+  }
+  else
+  {
+    SetImage( GetButtonImage() );
+  }
 }
+
+} // namespace Internal
+
+} // namespace Toolkit
+
+} // namespace Dali