+Button::Button()
+: Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
+ mAutoRepeatingTimer(),
+ mDisabled( false ),
+ mAutoRepeating( false ),
+ mTogglableButton( false ),
+ mSelected( false ),
+ mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ),
+ mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ),
+ mAnimationTime( 0.0f ),
+ mClickActionPerforming( false ),
+ mState( ButtonUp ),
+ mPaintState( UnselectedState )
+{
+}
+
+Button::~Button()
+{
+ if( mAutoRepeatingTimer )
+ {
+ mAutoRepeatingTimer.Reset();
+ }
+}
+
+void Button::SetDisabled( bool disabled )
+{
+ if( disabled != mDisabled )
+ {
+ unsigned int backgroundIndex;
+ unsigned int buttonIndex;
+
+ bool animationStarted = false;
+
+ mDisabled = disabled;
+
+ switch( mPaintState )
+ {
+ case UnselectedState:
+ {
+ buttonIndex = FindChildIndex( mLabel );
+ InsertChild( buttonIndex, mDisabledContent );
+
+ if( mBackgroundContent )
+ {
+ backgroundIndex = 1;
+ }
+ else
+ {
+ backgroundIndex = 0;
+ }
+
+ InsertChild( backgroundIndex, mDisabledBackgroundContent );
+
+ animationStarted = OnDisabled();
+
+ if( animationStarted )
+ {
+ mPaintState = UnselectedDisabledTransition;
+ }
+ else
+ {
+ mPaintState = DisabledUnselectedState;
+ }
+ break;
+ }
+ case SelectedState:
+ {
+ buttonIndex = FindChildIndex( mLabel );
+ InsertChild( buttonIndex, mDisabledSelectedContent );
+
+ if( mBackgroundContent )
+ {
+ backgroundIndex = 1;
+ }
+ else
+ {
+ backgroundIndex = 0;
+ }
+
+ InsertChild( backgroundIndex, mDisabledBackgroundContent );
+
+ animationStarted = OnDisabled();
+
+ if( animationStarted )
+ {
+ mPaintState = SelectedDisabledTransition;
+ }
+ else
+ {
+ mPaintState = DisabledSelectedState;
+ }
+ break;
+ }
+ case DisabledUnselectedState:
+ {
+ buttonIndex = FindChildIndex( mLabel );
+ InsertChild( buttonIndex, mButtonContent );
+
+ if( mDisabledBackgroundContent )
+ {
+ backgroundIndex = 1;
+ }
+ else
+ {
+ backgroundIndex = 0;
+ }
+
+ InsertChild( backgroundIndex, mBackgroundContent );
+
+ animationStarted = OnDisabled();
+
+ if( animationStarted )
+ {
+ mPaintState = DisabledUnselectedTransition;
+ }
+ else
+ {
+ mPaintState = UnselectedState;
+ }
+ break;
+ }
+ case DisabledSelectedState:
+ {
+ buttonIndex = FindChildIndex( mLabel );
+ InsertChild( buttonIndex, mSelectedContent );
+
+ if( mDisabledBackgroundContent )
+ {
+ backgroundIndex = 1;
+ }
+ else
+ {
+ backgroundIndex = 0;
+ }
+
+ InsertChild( backgroundIndex, mSelectedBackgroundContent );
+ InsertChild( backgroundIndex, mBackgroundContent );
+
+ animationStarted = OnDisabled();
+
+ if( animationStarted )
+ {
+ mPaintState = DisabledSelectedTransition;
+ }
+ else
+ {
+ mPaintState = SelectedState;
+ }
+ break;
+ }
+ case UnselectedSelectedTransition:
+ {
+ buttonIndex = FindChildIndex( mLabel );
+ InsertChild( buttonIndex, mDisabledSelectedContent );
+
+ if( mBackgroundContent )
+ {
+ backgroundIndex = 1;
+ }
+ else
+ {
+ backgroundIndex = 0;
+ }
+
+ InsertChild( backgroundIndex, mDisabledBackgroundContent );
+
+ animationStarted = OnDisabled();
+
+ if( animationStarted )
+ {
+ mPaintState = SelectedDisabledTransition;
+ }
+ else
+ {
+ mPaintState = DisabledSelectedState;
+ }
+ break;
+ }
+ case SelectedUnselectedTransition:
+ {
+ buttonIndex = FindChildIndex( mLabel );
+ InsertChild( buttonIndex, mDisabledContent );
+
+ if( mBackgroundContent )
+ {
+ backgroundIndex = 1;
+ }
+ else
+ {
+ backgroundIndex = 0;
+ }
+
+ InsertChild( backgroundIndex, mDisabledBackgroundContent );
+
+ animationStarted = OnDisabled();
+
+ if( animationStarted )
+ {
+ mPaintState = UnselectedDisabledTransition;
+ }
+ else
+ {
+ mPaintState = DisabledUnselectedState;
+ }
+ break;
+ }
+ case UnselectedDisabledTransition:
+ {
+ animationStarted = OnDisabled();
+
+ if( animationStarted )
+ {
+ mPaintState = DisabledUnselectedTransition;
+ }
+ else
+ {
+ mPaintState = UnselectedState;
+ }
+ break;
+ }
+ case DisabledUnselectedTransition:
+ {
+ animationStarted = OnDisabled();
+
+ if( animationStarted )
+ {
+ mPaintState = UnselectedDisabledTransition;
+ }
+ else
+ {
+ mPaintState = DisabledUnselectedState;
+ }
+ break;
+ }
+ case SelectedDisabledTransition:
+ {
+ animationStarted = OnDisabled();
+
+ if( animationStarted )
+ {
+ mPaintState = DisabledSelectedTransition;
+ }
+ else
+ {
+ mPaintState = SelectedState;
+ }
+ break;
+ }
+ case DisabledSelectedTransition:
+ {
+ animationStarted = OnDisabled();
+
+ if( animationStarted )
+ {
+ mPaintState = SelectedDisabledTransition;
+ }
+ else
+ {
+ mPaintState = DisabledSelectedState;
+ }
+ break;
+ }
+ }
+ }
+}
+
+bool Button::IsDisabled() const
+{
+ return mDisabled;
+}
+
+void Button::SetAutoRepeating( bool autoRepeating )
+{
+ mAutoRepeating = autoRepeating;
+
+ // An autorepeating button can't be a togglable button.
+ if( autoRepeating )
+ {
+ mTogglableButton = false;
+
+ if( mSelected )
+ {
+ // Emit a signal is not wanted, only change the appearance.
+ SetSelected( false, false );
+ }
+ }
+}
+
+bool Button::IsAutoRepeating() const
+{
+ return mAutoRepeating;
+}
+
+void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
+{
+ DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
+ mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
+}
+
+float Button::GetInitialAutoRepeatingDelay() const
+{
+ return mInitialAutoRepeatingDelay;
+}
+
+void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
+{
+ DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
+ mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
+}
+
+float Button::GetNextAutoRepeatingDelay() const
+{
+ return mNextAutoRepeatingDelay;
+}
+
+void Button::SetTogglableButton( bool togglable )
+{
+ mTogglableButton = togglable;
+
+ // A togglable button can't be an autorepeating button.
+ if( togglable )
+ {
+ mAutoRepeating = false;
+ }
+}
+
+bool Button::IsTogglableButton() const
+{
+ return mTogglableButton;
+}
+
+void Button::SetSelected( bool selected )
+{
+ if( !mDisabled && mTogglableButton && ( selected != mSelected ) )
+ {
+ SetSelected( selected, true );
+ }
+}
+
+void Button::SetSelected( bool selected, bool emitSignal )
+{
+ unsigned int buttonIndex, backgroundIndex;
+ bool animationStarted = false;
+
+ mSelected = selected;
+
+ switch( mPaintState )
+ {
+ case UnselectedState:
+ {
+ buttonIndex = FindChildIndex( mLabel );
+ InsertChild( buttonIndex, mSelectedContent );
+
+ if( mBackgroundContent )
+ {
+ backgroundIndex = 1;
+ }
+ else
+ {
+ backgroundIndex = 0;
+ }
+
+ InsertChild( backgroundIndex, mSelectedBackgroundContent );
+
+ // Notifies the derived class the button has been selected.
+ animationStarted = OnSelected();
+
+ if( animationStarted )
+ {
+ mPaintState = UnselectedSelectedTransition;
+ }
+ else
+ {
+ mPaintState = SelectedState;
+ }
+ break;
+ }
+ case SelectedState:
+ {
+ buttonIndex = FindChildIndex( mLabel );
+ InsertChild( buttonIndex, mButtonContent );
+
+ // Notifies the derived class the button has been selected.
+ animationStarted = OnSelected();
+
+ if( animationStarted )
+ {
+ mPaintState = SelectedUnselectedTransition;
+ }
+ else
+ {
+ mPaintState = UnselectedState;
+ }
+ break;
+ }
+ case UnselectedSelectedTransition:
+ {
+ // Notifies the derived class the button has been selected.
+ animationStarted = OnSelected();
+
+ if( animationStarted )
+ {
+ mPaintState = SelectedUnselectedTransition;
+ }
+ else
+ {
+ mPaintState = UnselectedState;
+ }
+ break;
+ }
+ case SelectedUnselectedTransition:
+ {
+ // Notifies the derived class the button has been selected.
+ animationStarted = OnSelected();
+
+ if( animationStarted )
+ {
+ mPaintState = UnselectedSelectedTransition;
+ }
+ else
+ {
+ mPaintState = SelectedState;
+ }
+ break;
+ }
+ case DisabledUnselectedTransition:
+ {
+ buttonIndex = FindChildIndex( mLabel );
+ InsertChild( buttonIndex, mSelectedContent );
+
+ if( mDisabledBackgroundContent )
+ {
+ if( mBackgroundContent )
+ {
+ backgroundIndex = 2;
+ }
+ else
+ {
+ backgroundIndex = 1;
+ }
+ }
+ else if( mBackgroundContent )
+ {
+ backgroundIndex = 1;
+ }
+ else
+ {
+ backgroundIndex = 0;
+ }
+
+ InsertChild( backgroundIndex, mSelectedBackgroundContent );
+
+ // Notifies the derived class the button has been selected.
+ animationStarted = OnSelected();
+
+ if( animationStarted )
+ {
+ mPaintState = UnselectedSelectedTransition;
+ }
+ else
+ {
+ mPaintState = SelectedState;
+ }
+ break;
+ }
+ case DisabledSelectedTransition:
+ {
+ buttonIndex = FindChildIndex( mLabel );
+ InsertChild( buttonIndex, mButtonContent );
+
+ // Notifies the derived class the button has been selected.
+ animationStarted = OnSelected();
+
+ if( animationStarted )
+ {
+ mPaintState = SelectedUnselectedTransition;
+ }
+ else
+ {
+ mPaintState = UnselectedState;
+ }
+ break;
+ }
+ default:
+ {
+ break;
+ }
+ }
+
+ if( emitSignal )
+ {
+ Toolkit::Button handle( GetOwner() );
+
+ // Emit signal.
+ mStateChangedSignal.Emit( handle );
+ }
+
+ RelayoutRequest();
+}
+
+bool Button::IsSelected() const
+{
+ return mTogglableButton && mSelected;
+}
+
+void Button::SetAnimationTime( float animationTime )
+{
+ mAnimationTime = animationTime;
+}
+
+float Button::GetAnimationTime() const
+{
+ return mAnimationTime;
+}
+
+void Button::SetLabel( const std::string& label )
+{
+ Toolkit::TextLabel textLabel = Toolkit::TextLabel::New( label );
+ textLabel.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
+ textLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
+ textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
+ textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
+
+ SetLabel( textLabel );
+}
+
+void Button::SetLabel( Actor label )
+{
+ if( mLabel != label )
+ {
+ if( mLabel && mLabel.GetParent() )
+ {
+ mLabel.GetParent().Remove( mLabel );
+ }
+
+ mLabel = label;
+ mLabel.SetPosition( 0.f, 0.f );
+
+ // label should be the top of the button
+ Self().Add( mLabel );
+
+ OnLabelSet();
+
+ RelayoutRequest();
+ }
+}
+
+Actor Button::GetLabel() const
+{
+ return mLabel;
+}
+
+Actor& Button::GetLabel()
+{
+ return mLabel;
+}
+
+void Button::SetButtonImage( Actor image )
+{
+ StopAllAnimations();
+
+ if( mButtonContent && mButtonContent.GetParent() )
+ {
+ Self().Remove( mButtonContent );
+ }
+
+ mButtonContent = image;
+
+ mButtonContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+ mButtonContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+ mButtonContent.SetPosition( 0.f, 0.f );
+
+ if( mPaintState == UnselectedState )
+ {
+ unsigned int index = FindChildIndex( mLabel );
+
+ Self().Insert( index, mButtonContent );
+ }
+
+ OnButtonImageSet();
+}
+
+Actor Button::GetButtonImage() const
+{
+ return mButtonContent;
+}
+
+Actor& Button::GetButtonImage()
+{
+ return mButtonContent;
+}
+
+void Button::SetSelectedImage( Actor image )
+{
+ StopAllAnimations();
+
+ if( mSelectedContent && mSelectedContent.GetParent() )
+ {
+ Self().Remove( mSelectedContent );
+ }
+
+ mSelectedContent = image;
+
+ mSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+ mSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+ mSelectedContent.SetPosition( 0.f, 0.f );
+
+ if( mPaintState == SelectedState )
+ {
+ unsigned int index = FindChildIndex( mLabel );
+
+ Self().Insert( index, mSelectedContent );
+ }
+
+ OnSelectedImageSet();
+}
+
+Actor Button::GetSelectedImage() const
+{
+ return mSelectedContent;
+}
+
+Actor& Button::GetSelectedImage()
+{
+ return mSelectedContent;
+}
+
+void Button::SetBackgroundImage( Actor image )
+{
+ StopAllAnimations();
+
+ if( mBackgroundContent && mBackgroundContent.GetParent() )
+ {
+ Self().Remove( mBackgroundContent );
+ }
+
+ mBackgroundContent = image;
+
+ mBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+ mBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+ mBackgroundContent.SetPosition( 0.f, 0.f );
+
+ if( mPaintState == UnselectedState || mPaintState == SelectedState )
+ {
+ Self().Insert( 0, mBackgroundContent );
+ }
+
+ OnBackgroundImageSet();
+}
+
+Actor Button::GetBackgroundImage() const
+{
+ return mBackgroundContent;
+}
+
+Actor& Button::GetBackgroundImage()
+{
+ return mBackgroundContent;
+}
+
+void Button::SetSelectedBackgroundImage( Actor image )
+{
+ StopAllAnimations();
+
+ if( mSelectedBackgroundContent && mSelectedBackgroundContent.GetParent() )
+ {
+ Self().Remove( mSelectedBackgroundContent );
+ }
+
+ mSelectedBackgroundContent = image;
+
+ mSelectedBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+ mSelectedBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+ mSelectedBackgroundContent.SetPosition( 0.f, 0.f );
+
+ if( mPaintState == SelectedState )
+ {
+ if( mBackgroundContent )
+ {
+ Self().Insert( 1, mSelectedBackgroundContent );
+ }
+ else
+ {
+ Self().Insert( 0, mSelectedBackgroundContent );
+ }
+ }
+
+ OnSelectedBackgroundImageSet();
+}
+
+Actor Button::GetSelectedBackgroundImage() const
+{
+ return mSelectedBackgroundContent;
+}
+
+Actor& Button::GetSelectedBackgroundImage()
+{
+ return mSelectedBackgroundContent;
+}
+
+void Button::SetDisabledImage( Actor image )
+{
+ StopAllAnimations();
+
+ if( mDisabledContent && mDisabledContent.GetParent() )
+ {
+ Self().Remove( mDisabledContent );
+ }
+
+ mDisabledContent = image;
+
+ mDisabledContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+ mDisabledContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+ mDisabledContent.SetPosition( 0.f, 0.f );
+
+ if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
+ {
+ unsigned int index = FindChildIndex( mLabel );
+
+ Self().Insert( index, mDisabledContent );
+ }
+
+ OnDisabledImageSet();
+}
+
+Actor Button::GetDisabledImage() const
+{
+ return mDisabledContent;
+}
+
+Actor& Button::GetDisabledImage()
+{
+ return mDisabledContent;
+}
+
+void Button::SetDisabledSelectedImage( Actor image )
+{
+ StopAllAnimations();
+
+ if( mDisabledSelectedContent && mDisabledSelectedContent.GetParent() )
+ {
+ Self().Remove( mDisabledSelectedContent );
+ }
+
+ mDisabledSelectedContent = image;
+
+ mDisabledSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+ mDisabledSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+ mDisabledSelectedContent.SetPosition( 0.f, 0.f );
+
+ if( mPaintState == DisabledSelectedState )
+ {
+ unsigned int index = FindChildIndex( mLabel );
+
+ Self().Insert( index, mDisabledSelectedContent );
+ }
+
+ OnDisabledSelectedImageSet();
+}
+
+Actor Button::GetDisabledSelectedImage() const
+{
+ return mDisabledSelectedContent;
+}
+
+Actor& Button::GetDisabledSelectedImage()
+{
+ return mDisabledSelectedContent;
+}
+
+void Button::SetDisabledBackgroundImage( Actor image )
+{
+ StopAllAnimations();
+
+ if( mDisabledBackgroundContent && mDisabledBackgroundContent.GetParent() )
+ {
+ Self().Remove( mDisabledBackgroundContent );
+ }
+
+ mDisabledBackgroundContent = image;
+
+ mDisabledBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+ mDisabledBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+ mDisabledBackgroundContent.SetPosition( 0.f, 0.f );
+
+ if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
+ {
+ Self().Insert( 0, mDisabledBackgroundContent );
+ }
+
+ OnDisabledBackgroundImageSet();
+}
+
+Actor Button::GetDisabledBackgroundImage() const
+{
+ return mDisabledBackgroundContent;
+}
+
+Actor& Button::GetDisabledBackgroundImage()
+{
+ return mDisabledBackgroundContent;
+}
+
+bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
+{
+ bool ret = false;
+
+ Dali::BaseHandle handle( object );
+
+ Toolkit::Button button = Toolkit::Button::DownCast( handle );
+
+ DALI_ASSERT_ALWAYS( button );
+
+ if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
+ {
+ GetImplementation( button ).DoClickAction( attributes );
+ ret = true;
+ }
+
+ return ret;
+}
+
+void Button::DoClickAction( const PropertyValueContainer& attributes )
+{
+ // Prevents the button signals from doing a recursive loop by sending an action
+ // and re-emitting the signals.
+ if( !mClickActionPerforming )
+ {
+ mClickActionPerforming = true;
+ OnButtonDown();
+ mState = ButtonDown;
+ OnButtonUp();
+ mClickActionPerforming = false;
+ }
+}
+
+void Button::UpdatePaintTransitionState()