Merge branch 'tizen' into devel/new_mesh
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / radio-button-impl.cpp
index 08d10bf..05dba27 100644 (file)
 
 // EXTERNAL INCLUDES
 #include <dali/public-api/object/type-registry.h>
+#include <dali/public-api/images/resource-image.h>
 
-using namespace Dali;
-using namespace Dali::Toolkit::Internal;
+namespace Dali
+{
+
+namespace Toolkit
+{
+
+namespace Internal
+{
 
 namespace
 {
@@ -38,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()
@@ -58,119 +65,111 @@ Dali::Toolkit::RadioButton RadioButton::New()
 
 RadioButton::RadioButton()
 {
-  mUnselectedImage = Dali::Image::New( UNSELECTED_BUTTON_IMAGE_DIR );
-  mSelectedImage = Dali::Image::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::SetImage( Actor image )
 {
-  if( mLabel != label )
-  {
-    if( mLabel )
-    {
-      mRadioIcon.Remove( mLabel );
-    }
+  mLayoutContainer.RemoveChildAt( Toolkit::TableView::CellPosition( 0, 0 ) );
+  mLayoutContainer.AddChild( image, Toolkit::TableView::CellPosition( 0, 0 ) );
 
-    if( label )
-    {
-      label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
-      label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
-      label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
-      mRadioIcon.Add( label );
-    }
+  RelayoutRequest();
+}
 
-    mLabel = label;
+void RadioButton::SetButtonImage( Actor image )
+{
+  Actor& buttonImage = GetButtonImage();
+  buttonImage = image;
+}
 
-    RelayoutRequest();
-  }
+void RadioButton::SetSelectedImage( Actor image )
+{
+  Actor& selectedImage = GetSelectedImage();
+  selectedImage = image;
 }
 
-void RadioButton::SetSelected( bool selected )
+void RadioButton::OnButtonInitialize()
 {
-  if( IsSelected() != 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));
+  Actor self = Self();
 
-          if( rbChild )
-          {
-            rbChild.SetSelected(false);
-          }
-        }
-      }
+  // Wrap size of radio button around all its children
+  self.SetResizePolicy( FIT_TO_CHILDREN, ALL_DIMENSIONS );
 
-      mSelected = true;
-      mRadioIcon.SetImage(mSelectedImage);
-    }
-    else
-    {
-      mSelected = false;
-      mRadioIcon.SetImage(mUnselectedImage);
-    }
+  // 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 );
 
-    // Raise state changed signal
-    Toolkit::RadioButton handle( GetOwner() );
-    StateChangedSignal().Emit( handle );
+  Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR );
+  Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR );
 
-    RelayoutRequest();
-  }
+  SetButtonImage( ImageActor::New( buttonImage ) );
+  SetSelectedImage( ImageActor::New( selectedImage ) );
+
+  SetImage( GetButtonImage() );
+
+  RelayoutRequest();
 }
 
-void RadioButton::OnRelayout( const Vector2& /*size*/, ActorSizeContainer& container )
+void RadioButton::OnButtonUp()
 {
-  Vector3 newSize( mRadioIcon.GetNaturalSize() );
+  if( ButtonDown == GetState() )
+  {
+    // Don't allow selection on an already selected radio button
+    if( !IsSelected() )
+    {
+      SetSelected( !IsSelected() );
+    }
+  }
+}
 
+void RadioButton::OnLabelSet()
+{
   Actor& label = GetLabel();
 
   if( label )
   {
-    // Offset the label from the radio button image
-    newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
+    // 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 ) );
 
-    // 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 );
+    mLayoutContainer.RemoveChildAt( Toolkit::TableView::CellPosition( 0, 1 ) );
+    mLayoutContainer.AddChild( label, Toolkit::TableView::CellPosition( 0, 1 ) );
   }
-
-  Self().SetSize( newSize );
 }
 
-void RadioButton::OnInitialize()
+void RadioButton::OnSelected( bool selected )
 {
-  mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
-  mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
-  Self().Add( mRadioIcon );
-
-  RelayoutRequest();
-}
-
-void RadioButton::OnButtonUp()
-{
-  if( ButtonDown == GetState() )
+  if( selected )
   {
-    // Don't allow selection on an already selected radio button
-    if( !IsSelected() )
+    Actor parent = Self().GetParent();
+    if( parent )
     {
-      SetSelected(!IsSelected());
+      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 );
+        }
+      }
     }
+
+    SetImage( GetSelectedImage() );
+  }
+  else
+  {
+    SetImage( GetButtonImage() );
   }
 }
+
+} // namespace Internal
+
+} // namespace Toolkit
+
+} // namespace Dali