// 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
{
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()
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::OnInitialize()
+{
+ Button::OnInitialize();
+
+ Actor self = Self();
+
+ // Wrap size of radio button around all its children
+ self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
+
+ SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
+ SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
+ SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
+ SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
+
+ 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( bool noPadding )
+{
+ Actor& label = GetLabelActor();
+
+ 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() && GetSelectedImage() )
+ {
+ label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+ }
+ else if( GetUnselectedImage() )
+ {
+ label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+ }
+ else
+ {
+ label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+ }
}
}
-void RadioButton::SetSelected( bool selected )
+void RadioButton::OnSelected()
{
- if( IsSelected() != selected )
+ Actor& label = GetLabelActor();
+
+ 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);
+ Actor& buttonImage = GetUnselectedImage();
+ if( label && buttonImage )
+ {
+ label.SetX( buttonImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+ }
+ break;
+ }
+ default:
+ {
+ break;
}
-
- // 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 );
-
- Vector3 actorSize( label.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 );
+} // 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