2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "button-impl.h"
22 #include <dali/public-api/events/touch-event.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/actors/image-actor.h>
25 #include <dali/public-api/scripting/scripting.h>
35 const Property::Index Button::PROPERTY_DISABLED = Internal::Button::BUTTON_PROPERTY_START_INDEX;
36 const Property::Index Button::PROPERTY_AUTO_REPEATING = Internal::Button::BUTTON_PROPERTY_START_INDEX + 1;
37 const Property::Index Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_START_INDEX + 2;
38 const Property::Index Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_START_INDEX + 3;
39 const Property::Index Button::PROPERTY_TOGGLABLE = Internal::Button::BUTTON_PROPERTY_START_INDEX + 4;
40 const Property::Index Button::PROPERTY_SELECTED = Internal::Button::BUTTON_PROPERTY_START_INDEX + 5;
41 const Property::Index Button::PROPERTY_NORMAL_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 6;
42 const Property::Index Button::PROPERTY_SELECTED_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 7;
43 const Property::Index Button::PROPERTY_DISABLED_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 8;
44 const Property::Index Button::PROPERTY_LABEL_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 9;
52 const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f );
53 const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f );
57 const char* const SIGNAL_PRESSED = "pressed";
58 const char* const SIGNAL_RELEASED = "released";
59 const char* const SIGNAL_CLICKED = "clicked";
60 const char* const SIGNAL_STATE_CHANGED = "state-changed";
64 const char* const ACTION_BUTTON_CLICK = "button-click";
68 // empty handle as we cannot create button (but type registered for clicked signal)
72 TypeRegistration typeRegistration( typeid( Toolkit::Button ), typeid( Toolkit::Control ), Create );
74 SignalConnectorType signalConnector1( typeRegistration, SIGNAL_PRESSED , &Button::DoConnectSignal );
75 SignalConnectorType signalConnector2( typeRegistration, SIGNAL_RELEASED, &Button::DoConnectSignal );
76 SignalConnectorType signalConnector3( typeRegistration, SIGNAL_CLICKED, &Button::DoConnectSignal );
77 SignalConnectorType signalConnector4( typeRegistration, SIGNAL_STATE_CHANGED, &Button::DoConnectSignal );
79 TypeAction action1( typeRegistration, ACTION_BUTTON_CLICK, &Button::DoAction );
81 PropertyRegistration property1( typeRegistration, "disabled", Toolkit::Button::PROPERTY_DISABLED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
82 PropertyRegistration property2( typeRegistration, "auto-repeating", Toolkit::Button::PROPERTY_AUTO_REPEATING, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
83 PropertyRegistration property3( typeRegistration, "initial-auto-repeating-delay", Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY, Property::FLOAT, &Button::SetProperty, &Button::GetProperty );
84 PropertyRegistration property4( typeRegistration, "next-auto-repeating-delay", Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY, Property::FLOAT, &Button::SetProperty, &Button::GetProperty );
85 PropertyRegistration property5( typeRegistration, "togglable", Toolkit::Button::PROPERTY_TOGGLABLE, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
86 PropertyRegistration property6( typeRegistration, "selected", Toolkit::Button::PROPERTY_SELECTED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
87 PropertyRegistration property7( typeRegistration, "normal-state-actor", Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
88 PropertyRegistration property8( typeRegistration, "selected-state-actor", Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
89 PropertyRegistration property9( typeRegistration, "disabled-state-actor", Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
90 PropertyRegistration property10( typeRegistration, "label-actor", Toolkit::Button::PROPERTY_LABEL_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
92 } // unnamed namespace
95 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
96 mAutoRepeatingTimer(),
98 mAutoRepeating( false ),
99 mTogglableButton( false ),
101 mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ),
102 mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ),
103 mAnimationTime( 0.0f ),
104 mClickActionPerforming( false ),
111 if( mAutoRepeatingTimer )
113 mAutoRepeatingTimer.Reset();
117 void Button::SetDisabled( bool disabled )
119 if( disabled != mDisabled )
121 mDisabled = disabled;
123 OnDisabled( mDisabled );
127 bool Button::IsDisabled() const
132 void Button::SetAutoRepeating( bool autoRepeating )
134 mAutoRepeating = autoRepeating;
136 // An autorepeating button can't be a togglable button.
139 mTogglableButton = false;
143 // Emit a signal is not wanted, only change the appearance.
153 bool Button::IsAutoRepeating() const
155 return mAutoRepeating;
158 void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
160 DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
161 mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
164 float Button::GetInitialAutoRepeatingDelay() const
166 return mInitialAutoRepeatingDelay;
169 void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
171 DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
172 mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
175 float Button::GetNextAutoRepeatingDelay() const
177 return mNextAutoRepeatingDelay;
180 void Button::SetTogglableButton( bool togglable )
182 mTogglableButton = togglable;
184 // A togglable button can't be an autorepeating button.
187 mAutoRepeating = false;
191 bool Button::IsTogglableButton() const
193 return mTogglableButton;
196 void Button::SetSelected( bool selected )
198 if( !mDisabled && mTogglableButton && ( selected != mSelected ) )
200 // Notifies the derived class the button has been selected.
201 OnSelected( selected );
203 mSelected = selected;
205 Toolkit::Button handle( GetOwner() );
208 mStateChangedSignal.Emit( handle );
214 bool Button::IsSelected() const
216 return mTogglableButton && mSelected;
219 void Button::SetAnimationTime( float animationTime )
221 mAnimationTime = animationTime;
224 float Button::GetAnimationTime() const
226 return mAnimationTime;
229 void Button::SetLabel( const std::string& label )
233 void Button::SetLabel( Actor label )
235 if( mLabel != label )
237 if( mLabel && mLabel.GetParent() )
239 mLabel.GetParent().Remove( mLabel );
250 Actor Button::GetLabel() const
255 Actor& Button::GetLabel()
260 Actor Button::GetButtonImage() const
262 return mButtonContent;
265 Actor& Button::GetButtonImage()
267 return mButtonContent;
270 Actor Button::GetSelectedImage() const
272 return mSelectedContent;
275 Actor& Button::GetSelectedImage()
277 return mSelectedContent;
280 Actor Button::GetBackgroundImage() const
282 return mBackgroundContent;
285 Actor& Button::GetBackgroundImage()
287 return mBackgroundContent;
290 Actor Button::GetDisabledImage() const
292 return mDisabledContent;
295 Actor& Button::GetDisabledImage()
297 return mDisabledContent;
300 Actor Button::GetDisabledSelectedImage() const
302 return mDisabledSelectedContent;
305 Actor& Button::GetDisabledSelectedImage()
307 return mDisabledSelectedContent;
310 Actor Button::GetDisabledBackgroundImage() const
312 return mDisabledBackgroundContent;
315 Actor& Button::GetDisabledBackgroundImage()
317 return mDisabledBackgroundContent;
320 bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
324 Dali::BaseHandle handle( object );
326 Toolkit::Button button = Toolkit::Button::DownCast( handle );
328 DALI_ASSERT_ALWAYS( button );
330 if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
332 GetImplementation( button ).DoClickAction( attributes );
339 void Button::DoClickAction( const PropertyValueContainer& attributes )
341 // Prevents the button signals from doing a recursive loop by sending an action
342 // and re-emitting the signals.
343 if( !mClickActionPerforming )
345 mClickActionPerforming = true;
349 mClickActionPerforming = false;
353 void Button::OnButtonStageDisconnection()
355 if( ButtonDown == mState )
357 if( !mTogglableButton )
359 Toolkit::Button handle( GetOwner() );
361 // Notifies the derived class the button has been released.
366 mAutoRepeatingTimer.Reset();
372 void Button::OnButtonDown()
374 if( !mTogglableButton )
376 Toolkit::Button handle( GetOwner() );
378 // Notifies the derived class the button has been pressed.
383 SetUpTimer( mInitialAutoRepeatingDelay );
387 mPressedSignal.Emit( handle );
391 void Button::OnButtonUp()
393 if( ButtonDown == mState )
395 if( mTogglableButton )
397 SetSelected( !mSelected );
401 // Notifies the derived class the button has been clicked.
407 mAutoRepeatingTimer.Reset();
410 Toolkit::Button handle( GetOwner() );
413 mReleasedSignal.Emit( handle );
414 mClickedSignal.Emit( handle );
419 void Button::OnTouchPointLeave()
421 if( ButtonDown == mState )
423 if( !mTogglableButton )
425 Toolkit::Button handle( GetOwner() );
427 // Notifies the derived class the button has been released.
432 mAutoRepeatingTimer.Reset();
436 mReleasedSignal.Emit( handle );
441 void Button::OnTouchPointInterrupted()
446 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
448 return mPressedSignal;
451 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
453 return mReleasedSignal;
456 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
458 return mClickedSignal;
461 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
463 return mStateChangedSignal;
466 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
468 Dali::BaseHandle handle( object );
470 bool connected( true );
471 Toolkit::Button button = Toolkit::Button::DownCast( handle );
473 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) )
475 button.PressedSignal().Connect( tracker, functor );
477 else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) )
479 button.ReleasedSignal().Connect( tracker, functor );
481 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) )
483 button.ClickedSignal().Connect( tracker, functor );
485 else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) )
487 button.StateChangedSignal().Connect( tracker, functor );
491 // signalName does not match any signal
498 bool Button::OnTouchEvent(const TouchEvent& event)
500 // Only events are processed when the button is not disabled and the touch event has only
502 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
504 switch( event.GetPoint(0).state )
506 case TouchPoint::Down:
508 OnButtonDown(); // Notification for derived classes.
510 // Sets the button state to ButtonDown.
516 OnButtonUp(); // Notification for derived classes.
518 // Sets the button state to ButtonUp.
522 case TouchPoint::Interrupted:
524 OnTouchPointInterrupted(); // Notification for derived classes.
526 // Sets the button state to the default (ButtonUp).
530 case TouchPoint::Leave:
532 OnTouchPointLeave(); // Notification for derived classes.
534 // Sets the button state to the default (ButtonUp).
538 case TouchPoint::Motion:
539 case TouchPoint::Stationary: // FALLTHROUGH
546 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
551 else if( 1 < event.GetPointCount() )
553 OnTouchPointLeave(); // Notification for derived classes.
555 // Sets the button state to the default (ButtonUp).
562 void Button::OnInitialize()
566 mTapDetector = TapGestureDetector::New();
567 mTapDetector.Attach( self );
568 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
570 OnButtonInitialize();
572 self.SetKeyboardFocusable( true );
575 void Button::OnActivated()
577 // When the button is activated, it performs the click action
578 PropertyValueContainer attributes;
579 DoClickAction( attributes );
582 void Button::OnTap(Actor actor, const TapGesture& tap)
587 void Button::SetUpTimer( float delay )
589 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
590 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
591 mAutoRepeatingTimer.Start();
594 bool Button::AutoRepeatingSlot()
596 bool consumed = false;
599 // Restart the autorepeat timer.
600 SetUpTimer( mNextAutoRepeatingDelay );
602 Toolkit::Button handle( GetOwner() );
604 // Notifies the derived class the button has been pressed.
608 consumed = mReleasedSignal.Emit( handle );
609 consumed |= mClickedSignal.Emit( handle );
610 consumed |= mPressedSignal.Emit( handle );
616 void Button::OnControlStageDisconnection()
618 OnButtonStageDisconnection(); // Notification for derived classes.
622 Button::ButtonState Button::GetState()
627 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
629 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
635 case Toolkit::Button::PROPERTY_DISABLED:
637 GetImplementation( button ).SetDisabled( value.Get<bool>() );
641 case Toolkit::Button::PROPERTY_AUTO_REPEATING:
643 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
647 case Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY:
649 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
653 case Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY:
655 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
659 case Toolkit::Button::PROPERTY_TOGGLABLE:
661 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
665 case Toolkit::Button::PROPERTY_SELECTED:
667 GetImplementation( button ).SetSelected( value.Get< bool >() );
671 case Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR:
673 GetImplementation( button ).SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) );
677 case Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR:
679 GetImplementation( button ).SetSelectedImage( Scripting::NewActor( value.Get< Property::Map >() ) );
683 case Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR:
685 GetImplementation( button ).SetDisabledImage( Scripting::NewActor( value.Get< Property::Map >() ) );
689 case Toolkit::Button::PROPERTY_LABEL_ACTOR:
691 GetImplementation( button ).SetLabel( Scripting::NewActor( value.Get< Property::Map >() ) );
698 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
700 Property::Value value;
702 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
706 switch ( propertyIndex )
708 case Toolkit::Button::PROPERTY_DISABLED:
710 value = GetImplementation( button ).mDisabled;
714 case Toolkit::Button::PROPERTY_AUTO_REPEATING:
716 value = GetImplementation( button ).mAutoRepeating;
720 case Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY:
722 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
726 case Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY:
728 value = GetImplementation( button ).mNextAutoRepeatingDelay;
732 case Toolkit::Button::PROPERTY_TOGGLABLE:
734 value = GetImplementation( button ).mTogglableButton;
738 case Toolkit::Button::PROPERTY_SELECTED:
740 value = GetImplementation( button ).mSelected;
744 case Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR:
747 Scripting::CreatePropertyMap( GetImplementation( button ).mButtonContent, map );
752 case Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR:
755 Scripting::CreatePropertyMap( GetImplementation( button ).mSelectedContent, map );
760 case Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR:
763 Scripting::CreatePropertyMap( GetImplementation( button ).mDisabledContent, map );
768 case Toolkit::Button::PROPERTY_LABEL_ACTOR:
771 Scripting::CreatePropertyMap( GetImplementation( button ).mLabel, map );
781 } // namespace Internal
783 } // namespace Toolkit