// CLASS HEADER
-
#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>
+
using namespace Dali;
using namespace Dali::Toolkit::Internal;
-namespace Dali
-{
-namespace Toolkit
-{
-const Property::Index RadioButton::PROPERTY_ACTIVE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 11;
-const Property::Index RadioButton::PROPERTY_LABEL_ACTOR = Internal::Button::BUTTON_PROPERTY_END_INDEX + 12;
-}
-}
-
namespace
{
return Toolkit::RadioButton::New();
}
-TypeRegistration typeRegistration(typeid (Toolkit::RadioButton ), typeid (Toolkit::Button ), Create);
+TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
-PropertyRegistration property1(typeRegistration, "active", Toolkit::RadioButton::PROPERTY_ACTIVE, Property::BOOLEAN, &RadioButton::SetProperty, &RadioButton::GetProperty);
-PropertyRegistration property2(typeRegistration, "label-actor", Toolkit::RadioButton::PROPERTY_LABEL_ACTOR, Property::MAP, &RadioButton::SetProperty, &RadioButton::GetProperty);
-}
+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";
-namespace
-{
-const char* const INACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-inactive.png";
-const char* const ACTIVE_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-active.png";
-const Vector3 IMAGE_WIDTH(16.f, 0.f, 0.f);
-const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.f, 0.f, 0.f);
+const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
}
Dali::Toolkit::RadioButton RadioButton::New()
}
RadioButton::RadioButton()
- : Button(),
- mActive(false)
+ : mSelected(false)
{
- mInactiveImage = Dali::Image::New(INACTIVE_BUTTON_IMAGE_DIR);
- mActiveImage = Dali::Image::New(ACTIVE_BUTTON_IMAGE_DIR);
+ mUnselectedImage = Dali::Image::New( UNSELECTED_BUTTON_IMAGE_DIR );
+ mSelectedImage = Dali::Image::New( SELECTED_BUTTON_IMAGE_DIR );
- mImageActor = Dali::ImageActor::New(mInactiveImage);
- mLabel = Actor::New();
+ mRadioIcon = Dali::ImageActor::New( mUnselectedImage );
}
RadioButton::~RadioButton()
void RadioButton::SetLabel(const std::string& label)
{
- mLabel.Reset();
- mLabel = Actor::New();
-
- Toolkit::TextView textView = Toolkit::TextView::New(label);
- textView.SetWidthExceedPolicy(Toolkit::TextView::ShrinkToFit); // Make sure our text always fits inside the button
- textView.SetAnchorPoint(AnchorPoint::TOP_LEFT);
+ TextActor textActor = TextActor::DownCast( mLabel );
+ if( textActor )
+ {
+ textActor.SetText( label );
+ }
+ else
+ {
+ Toolkit::TextView newTextView = Toolkit::TextView::New( label );
+ SetLabel( newTextView );
+ }
- mLabel.Add(textView);
+ RelayoutRequest();
}
void RadioButton::SetLabel(Actor label)
{
if( mLabel != label )
{
- Self().Remove(mLabel);
+ 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;
- Self().Add(mLabel);
+
+ RelayoutRequest();
}
}
return mLabel;
}
-void RadioButton::SetActive(bool active)
+void RadioButton::SetSelected(bool selected)
{
- if( mActive != active )
+ if( mSelected != selected )
{
- if( active )
+ if( selected )
{
Actor parent = Self().GetParent();
-
if( parent )
{
for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
if( rbChild )
{
- rbChild.SetActive(false);
+ rbChild.SetSelected(false);
}
}
}
- mActive = true;
- mImageActor.SetImage(mActiveImage);
+
+ mSelected = true;
+ mRadioIcon.SetImage(mSelectedImage);
}
else
{
- mActive = false;
- mImageActor.SetImage(mInactiveImage);
+ mSelected = false;
+ mRadioIcon.SetImage(mUnselectedImage);
}
+
+ // Raise state changed signal
+ Toolkit::RadioButton handle( GetOwner() );
+ mStateChangedSignal.Emit( handle, mSelected );
+
+ RelayoutRequest();
}
}
-bool RadioButton::IsActive()const
+bool RadioButton::IsSelected()const
{
- return mActive;
+ return mSelected;
}
void RadioButton::ToggleState()
{
- SetActive(!mActive);
+ 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()
{
- mImageActor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
- Self().Add(mImageActor);
+ mRadioIcon.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
+ mRadioIcon.SetParentOrigin( ParentOrigin::CENTER_LEFT );
+ Self().Add( mRadioIcon );
- mLabel.SetAnchorPoint(AnchorPoint::TOP_LEFT);
- mLabel.MoveBy(IMAGE_WIDTH + DISTANCE_BETWEEN_IMAGE_AND_LABEL);
- Self().Add(mLabel);
+ RelayoutRequest();
}
void RadioButton::OnButtonUp()
{
- ToggleState();
+ // 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));
+ Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle( object ) );
if( radioButton )
{
- RadioButton & radioButtonImpl(GetImplementation(radioButton));
+ RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
- switch ( propertyIndex )
+ if ( propertyIndex == Toolkit::Button::PROPERTY_TOGGLED )
{
- case Toolkit::RadioButton::PROPERTY_ACTIVE:
- {
- radioButtonImpl.SetActive(value.Get< bool >( ));
- break;
- }
- case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
- {
- radioButtonImpl.SetLabel(Scripting::NewActor(value.Get< Property::Map >( )));
- break;
- }
+ radioButtonImpl.SetSelected( value.Get< bool >( ) );
+ }
+ else if ( propertyIndex == Toolkit::Button::PROPERTY_LABEL_ACTOR )
+ {
+ radioButtonImpl.SetLabel( Scripting::NewActor( value.Get< Property::Map >( ) ) );
}
}
}
{
Property::Value value;
- Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast(Dali::BaseHandle(object));
+ Toolkit::RadioButton radioButton = Toolkit::RadioButton::DownCast( Dali::BaseHandle(object) );
if( radioButton )
{
- RadioButton & radioButtonImpl(GetImplementation(radioButton));
+ RadioButton& radioButtonImpl( GetImplementation( radioButton ) );
- switch ( propertyIndex )
+ if ( propertyIndex == Toolkit::Button::PROPERTY_TOGGLED )
{
- case Toolkit::RadioButton::PROPERTY_ACTIVE:
- {
- value = radioButtonImpl.mActive;
- break;
- }
- case Toolkit::RadioButton::PROPERTY_LABEL_ACTOR:
- {
- Property::Map map;
- Scripting::CreatePropertyMap(radioButtonImpl.mLabel, map);
- value = map;
- break;
- }
+ value = radioButtonImpl.mSelected;
+ }
+ else if ( propertyIndex == Toolkit::Button::PROPERTY_LABEL_ACTOR )
+ {
+ Property::Map map;
+ Scripting::CreatePropertyMap( radioButtonImpl.mLabel, map );
+ value = map;
}
}
return value;
-}
\ No newline at end of file
+}