X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Fbuttons%2Fpush-button-impl.cpp;h=4331c873165112723bcef3c4566601606d6ccc81;hp=c5e078fe52f0ce1c6e04be6e9df83fa1cecbee70;hb=f3da11c2818c6d17706fbb2417f21b602b3190f5;hpb=e2eda444afbe82e9591fe198eef339227f90a616 diff --git a/dali-toolkit/internal/controls/buttons/push-button-impl.cpp b/dali-toolkit/internal/controls/buttons/push-button-impl.cpp index c5e078f..4331c87 100644 --- a/dali-toolkit/internal/controls/buttons/push-button-impl.cpp +++ b/dali-toolkit/internal/controls/buttons/push-button-impl.cpp @@ -1,32 +1,32 @@ -// -// Copyright (c) 2014 Samsung Electronics Co., Ltd. -// -// Licensed under the Flora License, Version 1.0 (the License); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://floralicense.org/license/ -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an AS IS BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ // CLASS HEADER - #include "push-button-impl.h" // EXTERNAL INCLUDES - -#include +#include +#include +#include +#include +#include // INTERNAL INCLUDES - -#include "push-button-default-painter-impl.h" - -#include +#include namespace Dali { @@ -40,35 +40,63 @@ namespace Internal namespace { -using namespace Dali; +const float ANIMATION_TIME( 0.2f ); +const Padding DEFAULT_LABEL_PADDING( 12.0f, 12.0f, 12.0f, 12.0f ); +const Padding DEFAULT_ICON_PADDING( 12.0f, 12.0f, 12.0f, 12.0f ); BaseHandle Create() { return Toolkit::PushButton::New(); } -TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create ); +// Properties -SignalConnectorType signalConnector1( typeRegistration, Toolkit::PushButton::SIGNAL_TOGGLED , &PushButton::DoConnectSignal ); -SignalConnectorType signalConnector2( typeRegistration, Toolkit::PushButton::SIGNAL_PRESSED , &PushButton::DoConnectSignal ); -SignalConnectorType signalConnector3( typeRegistration, Toolkit::PushButton::SIGNAL_RELEASED, &PushButton::DoConnectSignal ); +DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PushButton, Toolkit::Button, Create ) -TypeAction action1( typeRegistration, Toolkit::PushButton::ACTION_PUSH_BUTTON_CLICK, &PushButton::DoAction ); +DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "unselectedIcon", STRING, UNSELECTED_ICON ) +DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "selectedIcon", STRING, SELECTED_ICON ) +DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "iconAlignment", STRING, ICON_ALIGNMENT ) +DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "labelPadding", STRING, LABEL_PADDING ) +DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "iconPadding", STRING, ICON_PADDING ) -} +DALI_TYPE_REGISTRATION_END() + +/* + * Table to define Text-to-enum conversions for IconAlignment. + */ +const Dali::Scripting::StringEnum IconAlignmentTable[] = { + { "LEFT", Toolkit::Internal::PushButton::LEFT }, + { "RIGHT", Toolkit::Internal::PushButton::RIGHT }, + { "TOP", Toolkit::Internal::PushButton::TOP }, + { "BOTTOM", Toolkit::Internal::PushButton::BOTTOM }, +}; const unsigned int IconAlignmentTableCount = sizeof( IconAlignmentTable ) / sizeof( IconAlignmentTable[0] ); + +const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-up.9.png"; +const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-down.9.png"; +const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-disabled.9.png"; +const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-down-disabled.9.png"; +} // unnamed namespace namespace { - const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f ); - const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f ); - // Helper function used to cast a ButtonPainter to PushButtonDefaultPainter - PushButtonDefaultPainterPtr GetPushButtonPainter( Dali::Toolkit::Internal::ButtonPainterPtr painter ) +/** + * Get size of Actor if larger than given size + * @param[in] root the actor to get the size of + * @param[out] size the greater of the given size or the size of the Actor + */ +void SizeOfActorIfLarger( Actor root, Vector3& size ) +{ + if ( root ) { - return static_cast( painter.Get() ); + // RelayoutSize retreived for Actor to use any padding set to it. + size.width = std::max( root.GetRelayoutSize( Dimension::WIDTH ), size.width ); + size.height = std::max( root.GetRelayoutSize( Dimension::HEIGHT ), size.height ); } -} // namespace +} + +} // unnamed namespace Dali::Toolkit::PushButton PushButton::New() { @@ -85,467 +113,491 @@ Dali::Toolkit::PushButton PushButton::New() return pushButton; } -void PushButton::SetAutoRepeating( bool autoRepeating ) -{ - mAutoRepeating = autoRepeating; - - // An autorepeating button can't be a toggle button. - if( autoRepeating ) - { - mToggleButton = false; - if( mToggled ) - { - // Emit a signal is not wanted, only change the appearance. - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->Toggled( handle ); - mToggled = false; - } - } - - // Notifies the painter. - GetPushButtonPainter( mPainter )->SetAutoRepeating( mAutoRepeating ); -} - -bool PushButton::IsAutoRepeating() const -{ - return mAutoRepeating; -} - -void PushButton::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay ) -{ - DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f ); - mInitialAutoRepeatingDelay = initialAutoRepeatingDelay; -} - -float PushButton::GetInitialAutoRepeatingDelay() const -{ - return mInitialAutoRepeatingDelay; -} - -void PushButton::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay ) +PushButton::PushButton() +: Button(), + mLabelPadding( DEFAULT_LABEL_PADDING ), + mIconPadding( DEFAULT_ICON_PADDING ), + mIconAlignment( RIGHT ), + mSize() { - DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f ); - mNextAutoRepeatingDelay = nextAutoRepeatingDelay; + SetAnimationTime( ANIMATION_TIME ); } -float PushButton::GetNextAutoRepeatingDelay() const +PushButton::~PushButton() { - return mNextAutoRepeatingDelay; } -void PushButton::SetToggleButton( bool toggle ) +void PushButton::OnButtonInitialize() { - mToggleButton = toggle; - - // A toggle button can't be an autorepeating button. - if( toggle ) - { - mAutoRepeating = false; + // Push button requires the Leave event. + Actor self = Self(); + self.SetLeaveRequired( true ); - // Notifies the painter. - GetPushButtonPainter( mPainter )->SetAutoRepeating( mAutoRepeating ); - } -} + // Set resize policy to natural size so that buttons will resize to background images + self.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS ); -bool PushButton::IsToggleButton() const -{ - return mToggleButton; + SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR ); + SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR ); + SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR ); + SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR ); } -void PushButton::SetToggled( bool toggle ) +void PushButton::SetIcon( DecorationState state, const std::string iconFilename ) { - if( !mDimmed && mToggleButton && ( toggle != mToggled ) ) - { - mToggled = toggle; - - Toolkit::PushButton handle( GetOwner() ); - - // Notifies the painter the button has been toggled. - GetPushButtonPainter( mPainter )->Toggled( handle ); - - // Emit signal. - mToggledSignalV2.Emit( handle, mToggled ); - } + mIconName[ state ] = iconFilename; + SetDecoration( state, ImageActor::New( Dali::ResourceImage::New( iconFilename ) ) ); + ConfigureSizeNegotiation(); } -bool PushButton::IsToggled() const +std::string& PushButton::GetIcon( DecorationState state ) { - return mToggleButton && mToggled; + return mIconName[ state ]; } -void PushButton::SetButtonImage( Image image ) +void PushButton::SetIconAlignment( const PushButton::IconAlignment iconAlignment ) { - SetButtonImage( ImageActor::New( image ) ); + mIconAlignment = iconAlignment; + ConfigureSizeNegotiation(); } -void PushButton::SetButtonImage( Actor image ) +const PushButton::IconAlignment PushButton::GetIconAlignment() const { - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetButtonImage( handle, image ); + return mIconAlignment; } -Actor& PushButton::GetButtonImage() +void PushButton::SetLabelPadding( const Vector4& padding ) { - return mButtonImage; + mLabelPadding = Padding( padding.x, padding.y, padding.z, padding.w ); + ConfigureSizeNegotiation(); } -Actor PushButton::GetButtonImage() const +Vector4 PushButton::GetLabelPadding() { - return mButtonImage; + return Vector4( mLabelPadding.left, mLabelPadding.right, mLabelPadding.top, mLabelPadding.bottom ); } -void PushButton::SetBackgroundImage( Image image ) +void PushButton::SetIconPadding( const Vector4& padding ) { - SetBackgroundImage( ImageActor::New( image ) ); + mIconPadding = Padding( padding.x, padding.y, padding.z, padding.w ); + ConfigureSizeNegotiation(); } -void PushButton::SetBackgroundImage( Actor image ) +Vector4 PushButton::GetIconPadding() { - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetBackgroundImage( handle, image ); + return Vector4( mIconPadding.left, mIconPadding.right, mIconPadding.top, mIconPadding.bottom ); } -Actor& PushButton::GetBackgroundImage() +void PushButton::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value ) { - return mBackgroundImage; -} + Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) ); -Actor PushButton::GetBackgroundImage() const -{ - return mBackgroundImage; -} + if ( pushButton ) + { + PushButton& pushButtonImpl( GetImplementation( pushButton ) ); -void PushButton::SetPressedImage( Image image ) -{ - SetPressedImage( ImageActor::New( image ) ); + switch ( propertyIndex ) + { + case Toolkit::PushButton::Property::UNSELECTED_ICON: + { + pushButtonImpl.SetIcon( UNSELECTED_DECORATION, value.Get< std::string >() ); + break; + } + case Toolkit::PushButton::Property::SELECTED_ICON: + { + pushButtonImpl.SetIcon( SELECTED_DECORATION, value.Get< std::string >() ); + break; + } + case Toolkit::PushButton::Property::ICON_ALIGNMENT: + { + IconAlignment iconAlignment; + if( Scripting::GetEnumeration< IconAlignment >( value.Get< std::string >().c_str(), IconAlignmentTable, IconAlignmentTableCount, iconAlignment ) ) + { + pushButtonImpl.SetIconAlignment( iconAlignment ); + } + break; + } + case Toolkit::PushButton::Property::LABEL_PADDING: + { + pushButtonImpl.SetLabelPadding( value.Get< Vector4 >() ); + break; + } + case Toolkit::PushButton::Property::ICON_PADDING: + { + pushButtonImpl.SetIconPadding( value.Get< Vector4 >() ); + break; + } + } + } } -void PushButton::SetPressedImage( Actor image ) +Property::Value PushButton::GetProperty( BaseObject* object, Property::Index propertyIndex ) { - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetPressedImage( handle, image ); -} + Property::Value value; -Actor& PushButton::GetPressedImage() -{ - return mPressedImage; -} + Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) ); -Actor PushButton::GetPressedImage() const -{ - return mPressedImage; -} + if ( pushButton ) + { + PushButton& pushButtonImpl( GetImplementation( pushButton ) ); -void PushButton::SetDimmedBackgroundImage( Image image ) -{ - SetDimmedBackgroundImage( ImageActor::New( image ) ); -} + switch ( propertyIndex ) + { + case Toolkit::PushButton::Property::UNSELECTED_ICON: + { + value = pushButtonImpl.GetIcon( UNSELECTED_DECORATION ); + break; + } + case Toolkit::PushButton::Property::SELECTED_ICON: + { + value = pushButtonImpl.GetIcon( UNSELECTED_DECORATION ); + break; + } + case Toolkit::PushButton::Property::ICON_ALIGNMENT: + { + value = Scripting::GetLinearEnumerationName< IconAlignment >( pushButtonImpl.GetIconAlignment(), IconAlignmentTable, IconAlignmentTableCount ); + break; + } + case Toolkit::PushButton::Property::LABEL_PADDING: + { + value = pushButtonImpl.GetLabelPadding(); + break; + } + case Toolkit::PushButton::Property::ICON_PADDING: + { + value = pushButtonImpl.GetIconPadding(); + break; + } + } + } -void PushButton::SetDimmedBackgroundImage( Actor image ) -{ - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetDimmedBackgroundImage( handle, image ); + return value; } -Actor& PushButton::GetDimmedBackgroundImage() +void PushButton::OnLabelSet( bool noPadding ) { - return mDimmedBackgroundImage; -} + Actor& label = GetLabelActor(); -Actor PushButton::GetDimmedBackgroundImage() const -{ - return mDimmedBackgroundImage; -} + if( label ) + { + if( noPadding ) + { + mLabelPadding = Padding( 0.0f, 0.0f, 0.0f, 0.0f ); + } -void PushButton::SetDimmedImage( Image image ) -{ - SetDimmedImage( ImageActor::New( image ) ); + Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( label ); + if( textLabel ) + { + textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, false ); + } + } + ConfigureSizeNegotiation(); } -void PushButton::SetDimmedImage( Actor image ) +void PushButton::OnButtonImageSet() { - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetDimmedImage( handle, image ); + ConfigureSizeNegotiation(); } -Actor& PushButton::GetDimmedImage() +void PushButton::OnSelectedImageSet() { - return mDimmedImage; + ConfigureSizeNegotiation(); } -Actor PushButton::GetDimmedImage() const +void PushButton::OnBackgroundImageSet() { - return mDimmedImage; + ConfigureSizeNegotiation(); } -void PushButton::SetLabelText( const std::string& text ) +void PushButton::OnSelectedBackgroundImageSet() { - Toolkit::TextView textView ( Toolkit::TextView::New( text ) ); - textView.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit ); // Make sure our text always fits inside the button - SetLabelText( textView ); + ConfigureSizeNegotiation(); } -void PushButton::SetLabelText( Actor text ) +void PushButton::OnDisabledImageSet() { - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetLabelText( handle, text ); + ConfigureSizeNegotiation(); } -Actor& PushButton::GetLabel() +void PushButton::OnDisabledSelectedImageSet() { - return mLabel; + ConfigureSizeNegotiation(); } -Actor PushButton::GetLabelText() const +void PushButton::OnDisabledBackgroundImageSet() { - return mLabel; + ConfigureSizeNegotiation(); } -Actor& PushButton::GetFadeOutBackgroundImage() +void PushButton::OnSizeSet( const Vector3& targetSize ) { - return mFadeOutBackgroundImage; -} + if( targetSize != mSize ) + { + mSize = targetSize; -Actor& PushButton::GetFadeOutButtonImage() -{ - return mFadeOutButtonImage; -} + Actor& label = GetLabelActor(); -Toolkit::PushButton::ToggledSignalV2& PushButton::ToggledSignal() -{ - return mToggledSignalV2; + if( label ) + { + label.SetSize( mSize ); + } + } } -Toolkit::PushButton::PressedSignalV2& PushButton::PressedSignal() +void PushButton::PrepareForTranstionIn( Actor actor ) { - return mPressedSignalV2; + actor.SetOpacity( 0.0f ); } -Toolkit::PushButton::ReleasedSignalV2& PushButton::ReleasedSignal() +void PushButton::PrepareForTranstionOut( Actor actor ) { - return mReleasedSignalV2; + actor.SetOpacity( 1.0f ); } -bool PushButton::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor ) +void PushButton::OnTransitionIn( Actor actor ) { - Dali::BaseHandle handle( object ); - - bool connected( true ); - Toolkit::PushButton button = Toolkit::PushButton::DownCast(handle); - - if( Toolkit::PushButton::SIGNAL_TOGGLED == signalName ) - { - button.ToggledSignal().Connect( tracker, functor ); - } - else if( Toolkit::PushButton::SIGNAL_PRESSED == signalName ) - { - button.PressedSignal().Connect( tracker, functor ); - } - else if( Toolkit::PushButton::SIGNAL_RELEASED == signalName ) - { - button.ReleasedSignal().Connect( tracker, functor ); - } - else - { - // signalName does not match any signal - connected = false; - } - - return connected; + FadeImageTo( actor, 1.f ); } -void PushButton::OnButtonInitialize() +void PushButton::OnTransitionOut( Actor actor ) { - // Push button requires the Leave event. - Actor root = Self(); - root.SetLeaveRequired( true ); + FadeImageTo( actor, 0.0f ); } -void PushButton::OnButtonDown() +void PushButton::FadeImageTo( Actor actor, float opacity ) { - if( !mToggleButton ) + if( actor ) { - Toolkit::PushButton handle( GetOwner() ); - - // Notifies the painter the button has been pressed. - GetPushButtonPainter( mPainter )->Pressed( handle ); + Dali::Animation transitionAnimation = GetTransitionAnimation(); + DALI_ASSERT_DEBUG( transitionAnimation ); - if( mAutoRepeating ) + if( transitionAnimation ) { - SetUpTimer( mInitialAutoRepeatingDelay ); + transitionAnimation.AnimateTo( Property( actor, Actor::Property::COLOR_ALPHA ), opacity ); } - - //Emit signal. - mPressedSignalV2.Emit( handle ); } } -void PushButton::OnButtonUp() +Vector3 PushButton::GetNaturalSize() { - if( ButtonDown == mState ) - { - if( mToggleButton ) - { - mToggled = !mToggled; + Vector3 size; - Toolkit::PushButton handle( GetOwner() ); + // If label, test against it's size + Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabelActor() ); - // Notifies the painter the button has been toggled. - GetPushButtonPainter( mPainter )->Toggled( handle ); + Actor icon = GetDecoration( UNSELECTED_DECORATION ); + if( label || icon ) + { + Vector3 labelSize( Vector3::ZERO ); + Vector3 iconSize( Vector3::ZERO ); - //Emit signal. - mToggledSignalV2.Emit( handle, mToggled ); - } - else + if( label ) { - Toolkit::PushButton handle( GetOwner() ); - - // Notifies the painter the button has been clicked. - GetPushButtonPainter( mPainter )->Released( handle ); - GetPushButtonPainter( mPainter )->Clicked( handle ); - - if( mAutoRepeating ) - { - mAutoRepeatingTimer.Reset(); - } - - //Emit signal. - mReleasedSignalV2.Emit( handle ); - mClickedSignalV2.Emit( handle ); + Vector3 labelNaturalSize = label.GetNaturalSize(); + labelSize.width = labelNaturalSize.width + mLabelPadding.left + mLabelPadding.right; + labelSize.height = labelNaturalSize.height + mLabelPadding.top + mLabelPadding.bottom; } - } -} -void PushButton::OnTouchPointLeave() -{ - if( ButtonDown == mState ) - { - if( !mToggleButton ) + if( icon ) { - Toolkit::PushButton handle( GetOwner() ); - - // Notifies the painter the button has been released. - GetPushButtonPainter( mPainter )->Released( handle ); + Vector3 iconNaturalSize = icon.GetNaturalSize(); + iconSize.width = iconNaturalSize.width + mIconPadding.left + mIconPadding.right; + iconSize.height = iconNaturalSize.height + mIconPadding.top + mIconPadding.bottom; - if( mAutoRepeating ) + switch( mIconAlignment ) { - mAutoRepeatingTimer.Reset(); + case LEFT: + case RIGHT: + { + size.width = labelSize.width + iconSize.width; + size.height = std::max( labelSize.height, iconSize.height ); + break; + } + case TOP: + case BOTTOM: + { + size.width = std::max( labelSize.width, iconSize.width ); + size.height = labelSize.height + iconSize.height; + break; + } } - - //Emit signal. - mReleasedSignalV2.Emit( handle ); + } + else + { + // No icon, so size is the same as label size. + // (If there is no label this is zero). + size = labelSize; } } -} + else + { + // Check Image and Background image and use the largest size as the control's Natural size. + SizeOfActorIfLarger( GetUnselectedImage(), size ); + SizeOfActorIfLarger( GetBackgroundImage(), size ); + } -void PushButton::OnTouchPointInterrupted() -{ - OnTouchPointLeave(); + return size; } -void PushButton::OnAnimationTimeSet( float animationTime ) +void PushButton::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension ) { - GetPushButtonPainter( mPainter )->SetAnimationTime( animationTime ); + ConfigureSizeNegotiation(); } -float PushButton::OnAnimationTimeRequested() const +void PushButton::ConfigureSizeNegotiation() { - return GetPushButtonPainter( mPainter )->GetAnimationTime(); -} + std::vector< Actor > images; + images.reserve( 7 ); -PushButton::PushButton() -: Button(), - mAutoRepeating( false ), - mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ), - mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ), - mToggleButton( false ), - mAutoRepeatingTimer(), - mToggled( false ), - mClickActionPerforming(false) -{ - // Creates specific painter. - mPainter = PushButtonDefaultPainterPtr( new PushButtonDefaultPainter() ); -} + images.push_back( GetUnselectedImage() ); + images.push_back( GetSelectedImage() ); + images.push_back( GetSelectedBackgroundImage() ); + images.push_back( GetBackgroundImage() ); + images.push_back( GetDisabledImage() ); + images.push_back( GetDisabledSelectedImage() ); + images.push_back( GetDisabledBackgroundImage() ); -PushButton::~PushButton() -{ - if( mAutoRepeatingTimer ) + Actor label = GetLabelActor(); + + for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i ) { - mAutoRepeatingTimer.Reset(); + ConfigureSizeNegotiationDimension( static_cast< Dimension::Type >( 1 << i ), images, label ); } - mPainter = NULL; -} + // Add any vertical padding directly to the actors. + Actor icon = GetDecoration( UNSELECTED_DECORATION ); + Actor selectedIcon = GetDecoration( SELECTED_DECORATION ); + bool iconExists = icon || selectedIcon; -void PushButton::SetUpTimer( float delay ) -{ - mAutoRepeatingTimer = Dali::Timer::New( static_cast( 1000.f * delay ) ); - mAutoRepeatingTimer.TickSignal().Connect( this, &PushButton::AutoRepeatingSlot ); - mAutoRepeatingTimer.Start(); -} - -bool PushButton::AutoRepeatingSlot() -{ - bool consumed = false; - if( !mDimmed ) + if( label ) { - // Restart the autorepeat timer. - SetUpTimer( mNextAutoRepeatingDelay ); - - Toolkit::PushButton handle( GetOwner() ); - - // Notifies the painter the button has been pressed. - GetPushButtonPainter( mPainter )->Pressed( handle ); + label.SetPadding( mLabelPadding ); + } + if( icon ) + { + icon.SetPadding( mIconPadding ); + } + if( selectedIcon ) + { + selectedIcon.SetPadding( mIconPadding ); + } - //Emit signal. - consumed = mReleasedSignalV2.Emit( handle ); - consumed |= mClickedSignalV2.Emit( handle ); - consumed |= mPressedSignalV2.Emit( handle ); - } + // Calculate and apply horizontal alignments and offsets + // to text and icon (depending on existence). + Vector3 iconPosition( Vector3::ZERO ); + Vector3 labelPosition( Vector3::ZERO ); + Vector3 iconAnchoring( AnchorPoint::CENTER ); + Vector3 labelAnchoring( AnchorPoint::CENTER ); + std::string horizontalLabelAlignment = "CENTER"; + std::string verticalLabelAlignment = "CENTER"; - return consumed; -} + if( iconExists && label ) + { + // There is an icon and a label to lay out. + switch( mIconAlignment ) + { + case LEFT: + { + iconPosition.x = mIconPadding.left; + labelPosition.x = -mLabelPadding.right; + iconAnchoring = AnchorPoint::CENTER_LEFT; + labelAnchoring = AnchorPoint::CENTER_RIGHT; + horizontalLabelAlignment = "END"; + break; + } + case RIGHT: + { + iconPosition.x = -mIconPadding.right; + labelPosition.x = mLabelPadding.left; + iconAnchoring = AnchorPoint::CENTER_RIGHT; + labelAnchoring = AnchorPoint::CENTER_LEFT; + horizontalLabelAlignment = "BEGIN"; + break; + } + case TOP: + { + iconPosition.y = mIconPadding.top; + labelPosition.y = -mLabelPadding.bottom; + iconAnchoring = AnchorPoint::TOP_CENTER; + labelAnchoring = AnchorPoint::BOTTOM_CENTER; + verticalLabelAlignment = "BOTTOM"; + break; + } + case BOTTOM: + { + iconPosition.y = -mIconPadding.bottom; + labelPosition.y = mLabelPadding.top; + iconAnchoring = AnchorPoint::BOTTOM_CENTER; + labelAnchoring = AnchorPoint::TOP_CENTER; + verticalLabelAlignment = "TOP"; + break; + } + } + } -void PushButton::OnActivated() -{ - // When the button is activated, it performs the click action - std::vector attributes; - DoClickAction(attributes); -} + // Note: If there is only an icon, or only a label, the default values are now correct. + // Setup the icon(s) with the precalculated values. + if( icon ) + { + icon.SetPosition( iconPosition ); + icon.SetParentOrigin( iconAnchoring ); + icon.SetAnchorPoint( iconAnchoring ); + } + if( selectedIcon ) + { + selectedIcon.SetPosition( iconPosition ); + selectedIcon.SetParentOrigin( iconAnchoring ); + selectedIcon.SetAnchorPoint( iconAnchoring ); + } -void PushButton::DoClickAction(const PropertyValueContainer& attributes) -{ - // Prevents the button signals from doing a recursive loop by sending an action - // and re-emitting the signals. - if(!mClickActionPerforming) + // Setup the label. + if( label ) { - mClickActionPerforming = true; - OnButtonDown(); - mState = ButtonDown; - OnButtonUp(); - mClickActionPerforming = false; + label.SetPosition( labelPosition ); + label.SetParentOrigin( labelAnchoring ); + label.SetAnchorPoint( labelAnchoring ); + label.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, horizontalLabelAlignment ); + label.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, verticalLabelAlignment ); } + + RelayoutRequest(); } -bool PushButton::DoAction(BaseObject* object, const std::string& actionName, const std::vector& attributes) -{ - bool ret = false; - Dali::BaseHandle handle(object); +void PushButton::ConfigureSizeNegotiationDimension( Dimension::Type dimension, const std::vector< Actor >& images, Actor& label ) +{ + ResizePolicy::Type imageResizePolicy = ResizePolicy::FILL_TO_PARENT; + ResizePolicy::Type labelResizePolicy = ResizePolicy::FILL_TO_PARENT; - Toolkit::PushButton button = Toolkit::PushButton::DownCast(handle); + ResizePolicy::Type resizePolicy = Self().GetResizePolicy( dimension ); - DALI_ASSERT_ALWAYS(button); + if( resizePolicy == ResizePolicy::FIT_TO_CHILDREN || resizePolicy == ResizePolicy::USE_NATURAL_SIZE ) + { + if( label ) + { + labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE; + } + else + { + imageResizePolicy = ResizePolicy::USE_NATURAL_SIZE; + } + } - if(Toolkit::PushButton::ACTION_PUSH_BUTTON_CLICK == actionName) + if( label ) { - GetImplementation(button).DoClickAction(attributes); - ret = true; + label.SetResizePolicy( labelResizePolicy, dimension ); } - return ret; + for( std::vector< Actor >::const_iterator it = images.begin(), itEnd = images.end(); it != itEnd; ++it ) + { + Actor actor = *it; + if( actor ) + { + actor.SetResizePolicy( imageResizePolicy, dimension ); + } + } } + } // namespace Internal } // namespace Toolkit