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=03752f125d38bd2df4db21d15142be3549020cca;hb=a073ebfd862b49692c8e6d7dff2b128e62a4f6df;hpb=e2e00e5c207b546067d57a0a747f4803a18c27bd diff --git a/dali-toolkit/internal/controls/buttons/push-button-impl.cpp b/dali-toolkit/internal/controls/buttons/push-button-impl.cpp index 03752f1..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 { @@ -34,57 +34,66 @@ namespace Dali namespace Toolkit { -const Property::Index PushButton::PROPERTY_AUTO_REPEATING = Internal::Button::BUTTON_PROPERTY_END_INDEX + 1; -const Property::Index PushButton::PROPERTY_INITIAL_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_END_INDEX + 2; -const Property::Index PushButton::PROPERTY_NEXT_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_END_INDEX + 3; -const Property::Index PushButton::PROPERTY_TOGGLABLE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 4; -const Property::Index PushButton::PROPERTY_TOGGLE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 5; -const Property::Index PushButton::PROPERTY_NORMAL_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_END_INDEX + 6; -const Property::Index PushButton::PROPERTY_PRESSED_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_END_INDEX + 7; -const Property::Index PushButton::PROPERTY_DIMMED_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_END_INDEX + 8; -const Property::Index PushButton::PROPERTY_LABEL_ACTOR = Internal::Button::BUTTON_PROPERTY_END_INDEX + 9; - namespace Internal { namespace { +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 + +DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PushButton, Toolkit::Button, Create ) -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_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 ) -TypeAction action1( typeRegistration, Toolkit::PushButton::ACTION_PUSH_BUTTON_CLICK, &PushButton::DoAction ); +DALI_TYPE_REGISTRATION_END() -PropertyRegistration property1( typeRegistration, "auto-repeating", Toolkit::PushButton::PROPERTY_AUTO_REPEATING, Property::BOOLEAN, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property2( typeRegistration, "initial-auto-repeating-delay", Toolkit::PushButton::PROPERTY_INITIAL_AUTO_REPEATING_DELAY, Property::FLOAT, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property3( typeRegistration, "next-auto-repeating-delay", Toolkit::PushButton::PROPERTY_NEXT_AUTO_REPEATING_DELAY, Property::FLOAT, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property4( typeRegistration, "togglable", Toolkit::PushButton::PROPERTY_TOGGLABLE, Property::BOOLEAN, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property5( typeRegistration, "toggle", Toolkit::PushButton::PROPERTY_TOGGLE, Property::BOOLEAN, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property6( typeRegistration, "normal-state-actor", Toolkit::PushButton::PROPERTY_NORMAL_STATE_ACTOR, Property::MAP, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property7( typeRegistration, "pressed-state-actor", Toolkit::PushButton::PROPERTY_PRESSED_STATE_ACTOR, Property::MAP, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property8( typeRegistration, "dimmed-state-actor", Toolkit::PushButton::PROPERTY_DIMMED_STATE_ACTOR, Property::MAP, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property9( typeRegistration, "label-actor", Toolkit::PushButton::PROPERTY_LABEL_ACTOR, Property::MAP, &PushButton::SetProperty, &PushButton::GetProperty ); +/* + * 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 ) { - return static_cast( painter.Get() ); + if ( root ) + { + // 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 ); + } } } // unnamed namespace @@ -104,273 +113,78 @@ 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 ) -{ - DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f ); - mNextAutoRepeatingDelay = nextAutoRepeatingDelay; -} - -float PushButton::GetNextAutoRepeatingDelay() const -{ - return mNextAutoRepeatingDelay; -} - -void PushButton::SetToggleButton( bool toggle ) -{ - mToggleButton = toggle; - - // A toggle button can't be an autorepeating button. - if( toggle ) - { - mAutoRepeating = false; - - // Notifies the painter. - GetPushButtonPainter( mPainter )->SetAutoRepeating( mAutoRepeating ); - } -} - -bool PushButton::IsToggleButton() const -{ - return mToggleButton; -} - -void PushButton::SetToggled( bool toggle ) -{ - 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 ); - } -} - -bool PushButton::IsToggled() const -{ - return mToggleButton && mToggled; -} - -void PushButton::SetButtonImage( Image image ) -{ - SetButtonImage( ImageActor::New( image ) ); -} - -void PushButton::SetButtonImage( Actor image ) -{ - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetButtonImage( handle, image ); -} - -Actor& PushButton::GetButtonImage() -{ - return mButtonImage; -} - -Actor PushButton::GetButtonImage() const -{ - return mButtonImage; -} - -void PushButton::SetBackgroundImage( Image image ) -{ - SetBackgroundImage( ImageActor::New( image ) ); -} - -void PushButton::SetBackgroundImage( Actor image ) -{ - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetBackgroundImage( handle, image ); -} - -Actor& PushButton::GetBackgroundImage() -{ - return mBackgroundImage; -} - -Actor PushButton::GetBackgroundImage() const -{ - return mBackgroundImage; -} - -void PushButton::SetPressedImage( Image image ) -{ - SetPressedImage( ImageActor::New( image ) ); -} - -void PushButton::SetPressedImage( Actor image ) -{ - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetPressedImage( handle, image ); -} - -Actor& PushButton::GetPressedImage() -{ - return mPressedImage; -} - -Actor PushButton::GetPressedImage() const -{ - return mPressedImage; -} - -void PushButton::SetDimmedBackgroundImage( Image image ) -{ - SetDimmedBackgroundImage( ImageActor::New( image ) ); -} - -void PushButton::SetDimmedBackgroundImage( Actor image ) -{ - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetDimmedBackgroundImage( handle, image ); -} - -Actor& PushButton::GetDimmedBackgroundImage() -{ - return mDimmedBackgroundImage; -} - -Actor PushButton::GetDimmedBackgroundImage() const -{ - return mDimmedBackgroundImage; -} - -void PushButton::SetDimmedImage( Image image ) -{ - SetDimmedImage( ImageActor::New( image ) ); -} - -void PushButton::SetDimmedImage( Actor image ) +PushButton::PushButton() +: Button(), + mLabelPadding( DEFAULT_LABEL_PADDING ), + mIconPadding( DEFAULT_ICON_PADDING ), + mIconAlignment( RIGHT ), + mSize() { - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetDimmedImage( handle, image ); + SetAnimationTime( ANIMATION_TIME ); } -Actor& PushButton::GetDimmedImage() +PushButton::~PushButton() { - return mDimmedImage; } -Actor PushButton::GetDimmedImage() const +void PushButton::OnButtonInitialize() { - return mDimmedImage; -} + // Push button requires the Leave event. + Actor self = Self(); + self.SetLeaveRequired( true ); -void PushButton::SetLabelText( const std::string& text ) -{ - Toolkit::TextView textView ( Toolkit::TextView::New( text ) ); - textView.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit ); // Make sure our text always fits inside the button - SetLabelText( textView ); -} + // Set resize policy to natural size so that buttons will resize to background images + self.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS ); -void PushButton::SetLabelText( Actor text ) -{ - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetLabelText( handle, text ); + SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR ); + SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR ); + SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR ); + SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR ); } -Actor& PushButton::GetLabel() +void PushButton::SetIcon( DecorationState state, const std::string iconFilename ) { - return mLabel; + mIconName[ state ] = iconFilename; + SetDecoration( state, ImageActor::New( Dali::ResourceImage::New( iconFilename ) ) ); + ConfigureSizeNegotiation(); } -Actor PushButton::GetLabelText() const +std::string& PushButton::GetIcon( DecorationState state ) { - return mLabel; + return mIconName[ state ]; } -Actor& PushButton::GetFadeOutBackgroundImage() +void PushButton::SetIconAlignment( const PushButton::IconAlignment iconAlignment ) { - return mFadeOutBackgroundImage; + mIconAlignment = iconAlignment; + ConfigureSizeNegotiation(); } -Actor& PushButton::GetFadeOutButtonImage() +const PushButton::IconAlignment PushButton::GetIconAlignment() const { - return mFadeOutButtonImage; + return mIconAlignment; } -Toolkit::PushButton::ToggledSignalV2& PushButton::ToggledSignal() +void PushButton::SetLabelPadding( const Vector4& padding ) { - return mToggledSignalV2; + mLabelPadding = Padding( padding.x, padding.y, padding.z, padding.w ); + ConfigureSizeNegotiation(); } -Toolkit::PushButton::PressedSignalV2& PushButton::PressedSignal() +Vector4 PushButton::GetLabelPadding() { - return mPressedSignalV2; + return Vector4( mLabelPadding.left, mLabelPadding.right, mLabelPadding.top, mLabelPadding.bottom ); } -Toolkit::PushButton::ReleasedSignalV2& PushButton::ReleasedSignal() +void PushButton::SetIconPadding( const Vector4& padding ) { - return mReleasedSignalV2; + mIconPadding = Padding( padding.x, padding.y, padding.z, padding.w ); + ConfigureSizeNegotiation(); } -bool PushButton::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor ) +Vector4 PushButton::GetIconPadding() { - 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; + return Vector4( mIconPadding.left, mIconPadding.right, mIconPadding.top, mIconPadding.bottom ); } void PushButton::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value ) @@ -383,57 +197,33 @@ void PushButton::SetProperty( BaseObject* object, Property::Index propertyIndex, switch ( propertyIndex ) { - case Toolkit::PushButton::PROPERTY_AUTO_REPEATING: + case Toolkit::PushButton::Property::UNSELECTED_ICON: { - pushButtonImpl.SetAutoRepeating( value.Get< bool >() ); + pushButtonImpl.SetIcon( UNSELECTED_DECORATION, value.Get< std::string >() ); break; } - - case Toolkit::PushButton::PROPERTY_INITIAL_AUTO_REPEATING_DELAY: + case Toolkit::PushButton::Property::SELECTED_ICON: { - pushButtonImpl.SetInitialAutoRepeatingDelay( value.Get< float >() ); + pushButtonImpl.SetIcon( SELECTED_DECORATION, value.Get< std::string >() ); break; } - - case Toolkit::PushButton::PROPERTY_NEXT_AUTO_REPEATING_DELAY: + case Toolkit::PushButton::Property::ICON_ALIGNMENT: { - pushButtonImpl.SetNextAutoRepeatingDelay( value.Get< float >() ); + IconAlignment iconAlignment; + if( Scripting::GetEnumeration< IconAlignment >( value.Get< std::string >().c_str(), IconAlignmentTable, IconAlignmentTableCount, iconAlignment ) ) + { + pushButtonImpl.SetIconAlignment( iconAlignment ); + } break; } - - case Toolkit::PushButton::PROPERTY_TOGGLABLE: + case Toolkit::PushButton::Property::LABEL_PADDING: { - pushButtonImpl.SetToggleButton( value.Get< bool >() ); + pushButtonImpl.SetLabelPadding( value.Get< Vector4 >() ); break; } - - case Toolkit::PushButton::PROPERTY_TOGGLE: - { - pushButtonImpl.SetToggled( value.Get< bool >() ); - break; - } - - case Toolkit::PushButton::PROPERTY_NORMAL_STATE_ACTOR: + case Toolkit::PushButton::Property::ICON_PADDING: { - pushButtonImpl.SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) ); - break; - } - - case Toolkit::PushButton::PROPERTY_PRESSED_STATE_ACTOR: - { - pushButtonImpl.SetPressedImage( Scripting::NewActor( value.Get< Property::Map >() ) ); - break; - } - - case Toolkit::PushButton::PROPERTY_DIMMED_STATE_ACTOR: - { - pushButtonImpl.SetDimmedImage( Scripting::NewActor( value.Get< Property::Map >() ) ); - break; - } - - case Toolkit::PushButton::PROPERTY_LABEL_ACTOR: - { - pushButtonImpl.SetLabelText( Scripting::NewActor( value.Get< Property::Map >() ) ); + pushButtonImpl.SetIconPadding( value.Get< Vector4 >() ); break; } } @@ -452,65 +242,29 @@ Property::Value PushButton::GetProperty( BaseObject* object, Property::Index pro switch ( propertyIndex ) { - case Toolkit::PushButton::PROPERTY_AUTO_REPEATING: + case Toolkit::PushButton::Property::UNSELECTED_ICON: { - value = pushButtonImpl.mAutoRepeating; + value = pushButtonImpl.GetIcon( UNSELECTED_DECORATION ); break; } - - case Toolkit::PushButton::PROPERTY_INITIAL_AUTO_REPEATING_DELAY: + case Toolkit::PushButton::Property::SELECTED_ICON: { - value = pushButtonImpl.mInitialAutoRepeatingDelay; + value = pushButtonImpl.GetIcon( UNSELECTED_DECORATION ); break; } - - case Toolkit::PushButton::PROPERTY_NEXT_AUTO_REPEATING_DELAY: + case Toolkit::PushButton::Property::ICON_ALIGNMENT: { - value = pushButtonImpl.mNextAutoRepeatingDelay; + value = Scripting::GetLinearEnumerationName< IconAlignment >( pushButtonImpl.GetIconAlignment(), IconAlignmentTable, IconAlignmentTableCount ); break; } - - case Toolkit::PushButton::PROPERTY_TOGGLABLE: + case Toolkit::PushButton::Property::LABEL_PADDING: { - value = pushButtonImpl.mToggleButton; + value = pushButtonImpl.GetLabelPadding(); break; } - - case Toolkit::PushButton::PROPERTY_TOGGLE: + case Toolkit::PushButton::Property::ICON_PADDING: { - value = pushButtonImpl.mToggled; - break; - } - - case Toolkit::PushButton::PROPERTY_NORMAL_STATE_ACTOR: - { - Property::Map map; - Scripting::CreatePropertyMap( pushButtonImpl.mButtonImage, map ); - value = map; - break; - } - - case Toolkit::PushButton::PROPERTY_PRESSED_STATE_ACTOR: - { - Property::Map map; - Scripting::CreatePropertyMap( pushButtonImpl.mPressedImage, map ); - value = map; - break; - } - - case Toolkit::PushButton::PROPERTY_DIMMED_STATE_ACTOR: - { - Property::Map map; - Scripting::CreatePropertyMap( pushButtonImpl.mDimmedImage, map ); - value = map; - break; - } - - case Toolkit::PushButton::PROPERTY_LABEL_ACTOR: - { - Property::Map map; - Scripting::CreatePropertyMap( pushButtonImpl.mLabel, map ); - value = map; + value = pushButtonImpl.GetIconPadding(); break; } } @@ -519,198 +273,331 @@ Property::Value PushButton::GetProperty( BaseObject* object, Property::Index pro return value; } -void PushButton::OnButtonInitialize() +void PushButton::OnLabelSet( bool noPadding ) { - // Push button requires the Leave event. - Actor root = Self(); - root.SetLeaveRequired( true ); -} + Actor& label = GetLabelActor(); -void PushButton::OnButtonDown() -{ - if( !mToggleButton ) + if( label ) { - Toolkit::PushButton handle( GetOwner() ); - - // Notifies the painter the button has been pressed. - GetPushButtonPainter( mPainter )->Pressed( handle ); - - if( mAutoRepeating ) + if( noPadding ) { - SetUpTimer( mInitialAutoRepeatingDelay ); + mLabelPadding = Padding( 0.0f, 0.0f, 0.0f, 0.0f ); } - //Emit signal. - mPressedSignalV2.Emit( handle ); + Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( label ); + if( textLabel ) + { + textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, false ); + } } + ConfigureSizeNegotiation(); } -void PushButton::OnButtonUp() +void PushButton::OnButtonImageSet() { - if( ButtonDown == mState ) - { - if( mToggleButton ) - { - mToggled = !mToggled; + ConfigureSizeNegotiation(); +} - Toolkit::PushButton handle( GetOwner() ); +void PushButton::OnSelectedImageSet() +{ + ConfigureSizeNegotiation(); +} - // Notifies the painter the button has been toggled. - GetPushButtonPainter( mPainter )->Toggled( handle ); +void PushButton::OnBackgroundImageSet() +{ + ConfigureSizeNegotiation(); +} - //Emit signal. - mToggledSignalV2.Emit( handle, mToggled ); - } - else - { - Toolkit::PushButton handle( GetOwner() ); +void PushButton::OnSelectedBackgroundImageSet() +{ + ConfigureSizeNegotiation(); +} - // Notifies the painter the button has been clicked. - GetPushButtonPainter( mPainter )->Released( handle ); - GetPushButtonPainter( mPainter )->Clicked( handle ); +void PushButton::OnDisabledImageSet() +{ + ConfigureSizeNegotiation(); +} - if( mAutoRepeating ) - { - mAutoRepeatingTimer.Reset(); - } +void PushButton::OnDisabledSelectedImageSet() +{ + ConfigureSizeNegotiation(); +} - //Emit signal. - mReleasedSignalV2.Emit( handle ); - mClickedSignalV2.Emit( handle ); - } - } +void PushButton::OnDisabledBackgroundImageSet() +{ + ConfigureSizeNegotiation(); } -void PushButton::OnTouchPointLeave() +void PushButton::OnSizeSet( const Vector3& targetSize ) { - if( ButtonDown == mState ) + if( targetSize != mSize ) { - if( !mToggleButton ) - { - Toolkit::PushButton handle( GetOwner() ); + mSize = targetSize; - // Notifies the painter the button has been released. - GetPushButtonPainter( mPainter )->Released( handle ); + Actor& label = GetLabelActor(); - if( mAutoRepeating ) - { - mAutoRepeatingTimer.Reset(); - } - - //Emit signal. - mReleasedSignalV2.Emit( handle ); + if( label ) + { + label.SetSize( mSize ); } } } -void PushButton::OnTouchPointInterrupted() +void PushButton::PrepareForTranstionIn( Actor actor ) { - OnTouchPointLeave(); + actor.SetOpacity( 0.0f ); } -void PushButton::OnAnimationTimeSet( float animationTime ) +void PushButton::PrepareForTranstionOut( Actor actor ) { - GetPushButtonPainter( mPainter )->SetAnimationTime( animationTime ); + actor.SetOpacity( 1.0f ); } -float PushButton::OnAnimationTimeRequested() const +void PushButton::OnTransitionIn( Actor actor ) { - return GetPushButtonPainter( mPainter )->GetAnimationTime(); + FadeImageTo( actor, 1.f ); } -PushButton::PushButton() -: Button(), - mAutoRepeating( false ), - mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ), - mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ), - mToggleButton( false ), - mAutoRepeatingTimer(), - mToggled( false ), - mClickActionPerforming(false) +void PushButton::OnTransitionOut( Actor actor ) { - // Creates specific painter. - mPainter = PushButtonDefaultPainterPtr( new PushButtonDefaultPainter() ); + FadeImageTo( actor, 0.0f ); } -PushButton::~PushButton() +void PushButton::FadeImageTo( Actor actor, float opacity ) { - if( mAutoRepeatingTimer ) + if( actor ) { - mAutoRepeatingTimer.Reset(); - } + Dali::Animation transitionAnimation = GetTransitionAnimation(); + DALI_ASSERT_DEBUG( transitionAnimation ); - mPainter = NULL; + if( transitionAnimation ) + { + transitionAnimation.AnimateTo( Property( actor, Actor::Property::COLOR_ALPHA ), opacity ); + } + } } -void PushButton::SetUpTimer( float delay ) +Vector3 PushButton::GetNaturalSize() { - mAutoRepeatingTimer = Dali::Timer::New( static_cast( 1000.f * delay ) ); - mAutoRepeatingTimer.TickSignal().Connect( this, &PushButton::AutoRepeatingSlot ); - mAutoRepeatingTimer.Start(); -} + Vector3 size; -bool PushButton::AutoRepeatingSlot() -{ - bool consumed = false; - if( !mDimmed ) + // If label, test against it's size + Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabelActor() ); + + Actor icon = GetDecoration( UNSELECTED_DECORATION ); + if( label || icon ) { - // Restart the autorepeat timer. - SetUpTimer( mNextAutoRepeatingDelay ); + Vector3 labelSize( Vector3::ZERO ); + Vector3 iconSize( Vector3::ZERO ); - Toolkit::PushButton handle( GetOwner() ); + if( label ) + { + Vector3 labelNaturalSize = label.GetNaturalSize(); + labelSize.width = labelNaturalSize.width + mLabelPadding.left + mLabelPadding.right; + labelSize.height = labelNaturalSize.height + mLabelPadding.top + mLabelPadding.bottom; + } - // Notifies the painter the button has been pressed. - GetPushButtonPainter( mPainter )->Pressed( handle ); + if( icon ) + { + Vector3 iconNaturalSize = icon.GetNaturalSize(); + iconSize.width = iconNaturalSize.width + mIconPadding.left + mIconPadding.right; + iconSize.height = iconNaturalSize.height + mIconPadding.top + mIconPadding.bottom; - //Emit signal. - consumed = mReleasedSignalV2.Emit( handle ); - consumed |= mClickedSignalV2.Emit( handle ); - consumed |= mPressedSignalV2.Emit( handle ); - } + switch( mIconAlignment ) + { + 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; + } + } + } + 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 ); + } - return consumed; + return size; } -void PushButton::OnActivated() +void PushButton::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension ) { - // When the button is activated, it performs the click action - std::vector attributes; - DoClickAction(attributes); + ConfigureSizeNegotiation(); } -void PushButton::DoClickAction(const PropertyValueContainer& attributes) +void PushButton::ConfigureSizeNegotiation() { - // Prevents the button signals from doing a recursive loop by sending an action - // and re-emitting the signals. - if(!mClickActionPerforming) + std::vector< Actor > images; + images.reserve( 7 ); + + 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() ); + + Actor label = GetLabelActor(); + + for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i ) { - mClickActionPerforming = true; - OnButtonDown(); - mState = ButtonDown; - OnButtonUp(); - mClickActionPerforming = false; + ConfigureSizeNegotiationDimension( static_cast< Dimension::Type >( 1 << i ), images, label ); } + + // Add any vertical padding directly to the actors. + Actor icon = GetDecoration( UNSELECTED_DECORATION ); + Actor selectedIcon = GetDecoration( SELECTED_DECORATION ); + bool iconExists = icon || selectedIcon; + + if( label ) + { + label.SetPadding( mLabelPadding ); + } + if( icon ) + { + icon.SetPadding( mIconPadding ); + } + if( selectedIcon ) + { + selectedIcon.SetPadding( mIconPadding ); + } + + // 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"; + + 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; + } + } + } + + // 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 ); + } + + // Setup the label. + if( label ) + { + 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