1 #ifndef __DALI_TOOLKIT_INTERNAL_BUTTON_H__
2 #define __DALI_TOOLKIT_INTERNAL_BUTTON_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/adaptor-framework/timer.h>
23 #include <dali/public-api/animation/animation.h>
26 #include <dali-toolkit/public-api/controls/buttons/button.h>
27 #include <dali-toolkit/public-api/controls/control-impl.h>
41 * Button is the base class implementation for all buttons.
43 class Button : public Control
49 * Construct a new Button.
54 * A reference counted object may only be deleted by calling Unreference()
61 * @copydoc Dali::Toolkit::Button::SetDisabled
63 void SetDisabled( bool disabled );
66 * @copydoc Dali::Toolkit::Button::IsDisabled
68 bool IsDisabled() const;
71 * @copydoc Dali::Toolkit::Button::SetAutoRepeating
73 void SetAutoRepeating( bool autoRepeating );
76 * @copydoc Dali::Toolkit::Button::IsAutoRepeating
78 bool IsAutoRepeating() const;
81 * @copydoc Dali::Toolkit::Button::SetInitialAutoRepeatingDelay
83 void SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay );
86 * @copydoc Dali::Toolkit::Button::GetInitialAutoRepeatingDelay
88 float GetInitialAutoRepeatingDelay() const;
91 * @copydoc Dali::Toolkit::Button::SetNextAutoRepeatingDelay
93 void SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay );
96 * @copydoc Dali::Toolkit::Button::GetNextAutoRepeatingDelay
98 float GetNextAutoRepeatingDelay() const;
101 * @copydoc Dali::Toolkit::Button::SetTogglableButton
103 void SetTogglableButton( bool togglable );
106 * @copydoc Dali::Toolkit::Button::IsTogglableButton
108 bool IsTogglableButton() const;
111 * @copydoc Dali::Toolkit::Button::SetSelected
113 void SetSelected( bool selected );
116 * @copydoc Dali::Toolkit::Button::IsSelected
118 bool IsSelected() const;
121 * @copydoc Dali::Toolkit::Button::SetAnimationTime
123 void SetAnimationTime( float animationTime );
126 * @copydoc Dali::Toolkit::Button::GetAnimationTime
128 float GetAnimationTime() const;
131 * @copydoc Dali::Toolkit::Button::SetLabelText
133 void SetLabelText( const std::string& label );
136 * @copydoc Dali::Toolkit::Button::GetLabelText
138 std::string GetLabelText() const;
141 * @copydoc Dali::Toolkit::PushButton::SetUnselectedImage
143 void SetUnselectedImage( const std::string& filename );
146 * @copydoc Dali::Toolkit::PushButton::SetSelectedImage
148 void SetSelectedImage( const std::string& filename );
151 * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage
153 void SetBackgroundImage( const std::string& filename );
156 * @copydoc Dali::Toolkit::PushButton::SetSelectedBackgroundImage
158 void SetSelectedBackgroundImage( const std::string& filename );
161 * @copydoc Dali::Toolkit::PushButton::SetDisabledImage
163 void SetDisabledImage( const std::string& filename );
166 * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage
168 void SetDisabledSelectedImage( const std::string& filename );
171 * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage
173 void SetDisabledBackgroundImage( const std::string& filename );
176 * @return The filename used for the button image.
178 std::string GetUnselectedImageFilename() const;
181 * @return The filename used for the selected image.
183 std::string GetSelectedImageFilename() const;
186 * @return The filename used for the background image.
188 std::string GetBackgroundImageFilename() const;
191 * @return The filename used for the selected background image.
193 std::string GetSelectedBackgroundImageFilename() const;
196 * @return The filename used for the disabled button image.
198 std::string GetDisabledImageFilename() const;
201 * @return The filename used for the disabled selected image.
203 std::string GetDisabledSelectedImageFilename() const;
206 * @return The filename used for the disabled background image.
208 std::string GetDisabledBackgroundImageFilename() const;
211 * @brief Sets the specified properties on the button label.
212 * If the label does not exist yet, it is created.
213 * The derived buttons are notified if any properties are changed.
214 * @param[in] properties A Property::Map of key-value pairs of properties to set.
216 void ModifyLabel( const Property::Map& properties );
219 * Performs actions as requested using the action name.
220 * @param[in] object The object on which to perform the action.
221 * @param[in] actionName The action to perform.
222 * @param[in] attributes The attributes with which to perfrom this action.
223 * @return true if action has been accepted by this control
225 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
227 public: // Deprecated API
230 * @copydoc Dali::Toolkit::Button::SetLabel( Actor label )
232 void SetLabel( Actor label );
235 * @deprecated Sets the unselected image with an Actor.
236 * @param[in] image The Actor to use.
238 void SetButtonImage( Actor image );
241 * @deprecated Sets the selected image with an Actor.
242 * @param[in] image The Actor to use.
244 void SetSelectedImage( Actor image );
247 * @deprecated Sets the background image with an Actor.
248 * @param[in] image The Actor to use.
250 void SetBackgroundImage( Actor image );
253 * @deprecated Sets the selected background image with an Actor.
254 * @param[in] image The Actor to use.
256 void SetSelectedBackgroundImage( Actor image );
259 * @deprecated Sets the disabled image with an Actor.
260 * @param[in] image The Actor to use.
262 void SetDisabledImage( Actor image );
265 * @deprecated Sets the disabled selected image with an Actor.
266 * @param[in] image The Actor to use.
268 void SetDisabledSelectedImage( Actor image );
271 * @deprecated Sets the disabled background image with an Actor.
272 * @param[in] image The Actor to use.
274 void SetDisabledBackgroundImage( Actor image );
277 * @copydoc Dali::Toolkit::Button::GetButtonImage
279 Actor GetButtonImage() const;
282 * @copydoc Dali::Toolkit::Button::GetSelectedImage
284 Actor GetSelectedImage() const;
289 * @return A reference to the label actor.
291 Actor& GetLabelActor();
294 * @return A reference to the unselected button image.
296 Actor& GetUnselectedImage();
299 * @return A reference to the selected image.
301 Actor& GetSelectedImage();
304 * @return A reference to the background image.
306 Actor& GetBackgroundImage();
309 * @return A reference to the selected background image.
311 Actor& GetSelectedBackgroundImage();
314 * @return A reference to the disabled button image.
316 Actor& GetDisabledImage();
319 * @return A reference to the disabled selected image.
321 Actor& GetDisabledSelectedImage();
324 * @return A reference to the disabled background image.
326 Actor& GetDisabledBackgroundImage();
331 * Perform the click action to click the button.
332 * @param[in] attributes The attributes to perfrom this action.
333 * @return true if this control can perform action.
335 bool DoClickAction( const Property::Map& attributes );
338 * This method is called when the label is set.
339 * @param[in] noPadding Used to bypass padding if the label is to be treated generically.
341 virtual void OnLabelSet( bool noPadding ) {}
344 * This method is called when the unselected button image is set
346 virtual void OnUnselectedImageSet() {}
349 * This method is called when the selected image is set
351 virtual void OnSelectedImageSet() {}
354 * This method is called when the background image is set
356 virtual void OnBackgroundImageSet() {}
359 * This method is called when the selected background image is set
361 virtual void OnSelectedBackgroundImageSet() {}
364 * This method is called when the disabled button image is set
366 virtual void OnDisabledImageSet() {}
369 * This method is called when the disabled selected image is set
371 virtual void OnDisabledSelectedImageSet() {}
374 * This method is called when the disabled background image is set
376 virtual void OnDisabledBackgroundImageSet() {}
379 * This method is called from the OnTouchEvent method when the button is down.
380 * Could be reimplemented in subclasses to provide specific behaviour.
382 virtual void OnButtonDown();
385 * This method is called from the OnTouchEvent method when the button is up.
386 * Could be reimplemented in subclasses to provide specific behaviour.
388 virtual void OnButtonUp();
391 * This method is called from the OnTouchEvent method when the touch point leaves the boundary of the button or
392 * more than one touch points are received.
393 * Could be reimplemented in subclasses to provide specific behaviour.
395 virtual void OnTouchPointLeave();
398 * This method is called from the OnTouchEvent method when the touch point is interrupted.
399 * Could be reimplemented in subclasses to provide specific behaviour.
401 virtual void OnTouchPointInterrupted();
404 * This method is called when the \e selected property is changed.
406 virtual void OnSelected() {}
409 * This method is called when the \e disabled property is changed.
411 virtual void OnDisabled() {}
414 * This method is called when the button is pressed.
416 virtual void OnPressed() {}
419 * This method is called when the button is released.
421 virtual void OnReleased() {}
426 * @copydoc Dali::Toolkit::PushButton::PressedSignal()
428 Toolkit::Button::ButtonSignalType& PressedSignal();
431 * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
433 Toolkit::Button::ButtonSignalType& ReleasedSignal();
436 * @copydoc Dali::Toolkit::Button::ClickedSignal()
438 Toolkit::Button::ButtonSignalType& ClickedSignal();
441 * @copydoc Dali::Toolkit::Button::StateChangedSignal()
443 Toolkit::Button::ButtonSignalType& StateChangedSignal();
446 * Connects a callback function with the object's signals.
447 * @param[in] object The object providing the signal.
448 * @param[in] tracker Used to disconnect the signal.
449 * @param[in] signalName The signal to connect to.
450 * @param[in] functor A newly allocated FunctorDelegate.
451 * @return True if the signal was connected.
452 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
454 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
459 * Called when a property of an object of this type is set.
460 * @param[in] object The object whose property is set.
461 * @param[in] index The property index.
462 * @param[in] value The new property value.
464 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
467 * Called to retrieve a property of an object of this type.
468 * @param[in] object The object whose property is to be retrieved.
469 * @param[in] index The property index.
470 * @return The current value of the property.
472 static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
474 protected: // From Control
477 * @copydoc Dali::Control::OnTouchEvent( const TouchEvent& event )
479 virtual bool OnTouchEvent( const TouchEvent& event );
482 * @copydoc Toolkit::Control::OnInitialize()
483 * @note If overridden by deriving button classes, then an up-call to Button::OnInitialize MUST be made at the start.
485 virtual void OnInitialize();
488 * @copydoc Toolkit::Control::OnAccessibilityActivated()
490 virtual bool OnAccessibilityActivated();
493 * @copydoc Toolkit::Control::OnKeyboardEnter()
495 virtual bool OnKeyboardEnter();
498 * @copydoc Toolkit::Control::OnStageDisconnection()
499 * @note If overridden by deriving button classes, then an up-call to Button::OnStageDisconnection MUST be made at the end.
501 void OnStageDisconnection();
506 * Handler for tap events.
507 * We do not actually do anything when we receive a tap as the button handles tap event through
508 * the touch event system itself as it requires more than just tap handling (e.g. leave events).
509 * This stops any of our parents receiving a tap gesture when it occurs within our area.
510 * @param[in] actor The tapped actor.
511 * @param[in] tap The tap gesture.
513 void OnTap(Actor actor, const TapGesture& tap);
516 * Sets up the autorepeating timer.
517 * @param[in] delay The delay time in seconds.
519 void SetUpTimer( float delay );
522 * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
524 bool AutoRepeatingSlot();
527 * Sets the button as selected or unselected.
528 * @param[in] selected \e selected property value.
529 * @param[in] emitSignal Emit a signal if this value is \e true.
531 void SetSelected( bool selected, bool emitSignal );
534 * This method is called when the button is pressed.
539 * This method is called when the button is released.
544 * Used to perform common setup applied to images within button.
545 * This will replace the current image with the specifed one.
546 * @param[in] actorToModify The image to replace.
547 * @param[out] newActor The new image to use.
549 void SetupContent( Actor& actorToModify, Actor newActor );
552 * Sets the color of the unselected image.
553 * If no image exists, it is created.
554 * @param[in] color The color to use.
556 void SetUnselectedColor( const Vector4& color );
559 * Gets the unselected content color.
560 * @return The currently used unselected color.
562 const Vector4 GetUnselectedColor() const;
565 * Sets the color of the selected image.
566 * If no image exists, it is created.
567 * @param[in] color The color to use.
569 void SetSelectedColor( const Vector4& color );
572 * Gets the selected content color.
573 * @return The currently used selected color.
575 const Vector4 GetSelectedColor() const;
581 ButtonUp, ///< The button is up.
582 ButtonDown, ///< The button is down.
586 * Button paint states.
590 UnselectedState, ///< The button is unselected.
591 SelectedState, ///< The button is selected.
592 DisabledUnselectedState, ///< The button is disabled and unselected.
593 DisabledSelectedState, ///< The button is disabled and selected.
597 * Enum to specify which decoration when getting and setting decorations.
601 UNSELECTED_DECORATION = 0,
606 ButtonState GetState();
607 PaintState GetPaintState();
608 void SetDecoration( DecorationState state, Actor actor );
609 Actor& GetDecoration( DecorationState state );
613 * Returns the animation to be used for transitioning creating the animation if needed.
614 * @return The initialised transition animation.
616 Dali::Animation GetTransitionAnimation();
619 * Prepares the actor to be transitioned in.
620 * @param[in] actor The actor that will be transitioned in.
622 virtual void PrepareForTranstionIn( Actor actor ) {}
625 * Prepares the actor to be transitioned in.
626 * @param[in] actor The actor that will be transitioned out.
628 virtual void PrepareForTranstionOut( Actor actor ) {}
631 * Transitions the actor in, allowing derived classes to configure
632 * the GetTransitionAnimation() animation ready.
633 * Button is in charge of calling Dali::Animation::Play and so derived classes
634 * only need to add the animation.
636 virtual void OnTransitionIn( Actor actor ) {}
639 * Transitions the actor out, allowing derived classes to configure
640 * the GetTransitionAnimation() animation ready.
641 * Button is in charge of calling Dali::Animation::Play and so derived classes
642 * only need to add the animation.
644 virtual void OnTransitionOut( Actor actor ) {}
649 * Starts the transition animation.
650 * Button::TransitionFinished is called when the animation finishes.
652 void StartTransitionAnimation();
655 * This method stops all transition animations
657 void StopTransitionAnimation();
660 * Called when the transition animation finishes.
662 void TransitionAnimationFinished( Dali::Animation& source );
665 * Resets the Button to the base state for the current paint state.
666 * Any additionally inserted images needed for transitions that are
667 * no longer needed and the removed.
669 void ResetImageLayers();
672 * Transitions out the actor
674 void TransitionOut( Actor actor );
677 * Removes the actor from the button and prepares it to be transitioned out
679 void RemoveButtonImage( Actor& actor );
682 * Finds the index of the actor.
683 * If the actor doesn't exist, return the last index + 1.
685 unsigned int FindChildIndex( Actor& actor );
688 * Adds an actor to the hierarchy and prepares it to be transitioned.
689 * @param[in] actor The actor to add
691 void PrepareAddButtonImage( Actor& actor );
694 * Adds an actor to the hierarchy and marks it to be transitioned.
695 * @param[in] actor The actor to add
697 void TransitionButtonImage( Actor& actor );
700 * Adds an actor to the hierarchy.
701 * @param[in] actor The actor to add
703 void AddButtonImage( Actor& actor );
706 * (Re)Adds the label (if exists) to the hierarchy (so it is always on top).
711 Button( const Button& );
714 Button& operator = ( const Button& );
719 Toolkit::Button::ButtonSignalType mPressedSignal; ///< Signal emitted when the button is pressed.
720 Toolkit::Button::ButtonSignalType mReleasedSignal; ///< Signal emitted when the button is released.
721 Toolkit::Button::ButtonSignalType mClickedSignal; ///< Signal emitted when the button is clicked.
722 Toolkit::Button::ButtonSignalType mStateChangedSignal; ///< Signal emitted when the button's state is changed.
724 Timer mAutoRepeatingTimer; ///< Timer used to implement the autorepeating property.
726 Actor mLabel; ///< Stores the button label.
728 Actor mDecoration[ DECORATION_STATES ]; ///< Stores the decorations for both selected and unselected states.
730 Actor mUnselectedContent; ///< Stores the unselected content.
731 Actor mSelectedContent; ///< Stores the selected content.
732 Actor mBackgroundContent; ///< Stores the background content.
733 Actor mSelectedBackgroundContent; ///< Stores the selected background content.
734 Actor mDisabledContent; ///< Stores the disabled content.
735 Actor mDisabledSelectedContent; ///< Stores the disabled selected content.
736 Actor mDisabledBackgroundContent; ///< Stores the disabled background content.
738 Animation mTransitionAnimation; ///< Animation used in the state transitions.
740 TapGestureDetector mTapDetector;
742 Vector4 mUnselectedColor; ///< Color to use for unselected content.
743 Vector4 mSelectedColor; ///< Color to use for selected content.
745 bool mDisabled; ///< Stores the disabled property.
746 bool mAutoRepeating; ///< Stores the autorepeating property.
747 bool mTogglableButton; ///< Stores the togglable property.
748 bool mSelected; ///< Stores the selected state.
749 float mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
750 float mNextAutoRepeatingDelay; ///< Stores the next autorepeating delay in seconds.
752 float mAnimationTime; ///< The animation time.
755 bool mClickActionPerforming;
757 ButtonState mState; ///< Stores the button state.
758 PaintState mPaintState; ///< Stores the paint state.
761 } // namespace Internal
763 // Helpers for public-api forwarding methods
765 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
767 DALI_ASSERT_ALWAYS( button );
769 Dali::RefObject& handle = button.GetImplementation();
771 return static_cast<Toolkit::Internal::Button&>( handle );
774 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
776 DALI_ASSERT_ALWAYS( button );
778 const Dali::RefObject& handle = button.GetImplementation();
780 return static_cast<const Toolkit::Internal::Button&>( handle );
783 } // namespace Toolkit
787 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__