Merge "Added TextLabel and TextField tests" into tizen
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / radio-button-impl.cpp
index 4ef25d9..81b6a5d 100644 (file)
 #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
 {
@@ -39,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,119 +65,122 @@ 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::OnButtonInitialize()
+{
+  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 );
+  Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR );
+
+  SetButtonImage( ImageActor::New( buttonImage ) );
+  SetSelectedImage( ImageActor::New( selectedImage ) );
+
+  RelayoutRequest();
+}
+
+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() );
     }
+  }
+}
+
+void RadioButton::OnLabelSet()
+{
+  Actor& label = GetLabel();
+
+  if( label )
+  {
+    label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
+    label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
 
-    if( label )
+    // 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() )
+    {
+      label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+    else
+    {
+      label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
   }
 }
 
-void RadioButton::SetSelected( bool selected )
+bool RadioButton::OnSelected()
 {
-  if( IsSelected() != selected )
+  Actor& buttonImage = GetButtonImage();
+  Actor& selectedImage = GetSelectedImage();
+  Actor& label = GetLabel();
+
+  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);
+      RemoveChild( buttonImage );
+
+      if( label )
+      {
+        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() );
-    StateChangedSignal().Emit( handle );
-
-    RelayoutRequest();
-  }
-}
-
-void RadioButton::OnRelayout( const Vector2& /*size*/, ActorSizeContainer& container )
-{
-  Vector3 newSize( mRadioIcon.GetNaturalSize() );
-
-  Actor& label = GetLabel();
-
-  if( label )
-  {
-    // 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 );
+      RemoveChild( selectedImage );
 
-    Vector3 actorSize( label.GetSize() );
-    newSize.width += actorSize.width;
-    newSize.height = std::max( newSize.height, actorSize.height );
+      if( label )
+      {
+        label.SetX( buttonImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+      }
+      break;
+    }
+    default:
+    {
+      break;
+    }
   }
 
-  Self().SetSize( newSize );
+  // there is no animation
+  return false;
 }
 
-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