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( bool disabled )
63 void SetDisabled( bool disabled );
66 * @copydoc Dali::Toolkit::Button::IsDisabled() const
68 bool IsDisabled() const;
71 * @copydoc Dali::Toolkit::Button::SetAutoRepeating( bool autoRepeating )
73 void SetAutoRepeating( bool autoRepeating );
76 * @copydoc Dali::Toolkit::Button::IsAutoRepeating() const
78 bool IsAutoRepeating() const;
81 * @copydoc Dali::Toolkit::Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
83 void SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay );
86 * @copydoc Dali::Toolkit::Button::GetInitialAutoRepeatingDelay() const
88 float GetInitialAutoRepeatingDelay() const;
91 * @copydoc Dali::Toolkit::Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
93 void SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay );
96 * @copydoc Dali::Toolkit::Button::GetNextAutoRepeatingDelay() const
98 float GetNextAutoRepeatingDelay() const;
101 * @copydoc Dali::Toolkit::Button::SetTogglableButton( bool togglable )
103 void SetTogglableButton( bool togglable );
106 * @copydoc Dali::Toolkit::Button::IsTogglableButton() const
108 bool IsTogglableButton() const;
111 * @copydoc Dali::Toolkit::Button::SetSelected( bool selected )
113 void SetSelected( bool selected );
116 * @copydoc Dali::Toolkit::Button::IsSelected() const
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 * Performs actions as requested using the action name.
212 * @param[in] object The object on which to perform the action.
213 * @param[in] actionName The action to perform.
214 * @param[in] attributes The attributes with which to perfrom this action.
215 * @return true if action has been accepted by this control
217 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
219 public: // Deprecated API
222 * @copydoc Dali::Toolkit::Button::SetLabel( Actor label )
224 void SetLabel( Actor label );
227 * @deprecated Sets the unselected image with an Actor.
228 * @param[in] image The Actor to use.
230 void SetButtonImage( Actor image );
233 * @deprecated Sets the selected image with an Actor.
234 * @param[in] image The Actor to use.
236 void SetSelectedImage( Actor image );
239 * @deprecated Sets the background image with an Actor.
240 * @param[in] image The Actor to use.
242 void SetBackgroundImage( Actor image );
245 * @deprecated Sets the selected background image with an Actor.
246 * @param[in] image The Actor to use.
248 void SetSelectedBackgroundImage( Actor image );
251 * @deprecated Sets the disabled image with an Actor.
252 * @param[in] image The Actor to use.
254 void SetDisabledImage( Actor image );
257 * @deprecated Sets the disabled selected image with an Actor.
258 * @param[in] image The Actor to use.
260 void SetDisabledSelectedImage( Actor image );
263 * @deprecated Sets the disabled background image with an Actor.
264 * @param[in] image The Actor to use.
266 void SetDisabledBackgroundImage( Actor image );
269 * @copydoc Dali::Toolkit::Button::GetButtonImage()
271 Actor GetButtonImage() const;
274 * @copydoc Dali::Toolkit::Button::GetSelectedImage()
276 Actor GetSelectedImage() const;
281 * @return A reference to the label actor.
283 Actor& GetLabelActor();
286 * @return A reference to the unselected button image.
288 Actor& GetUnselectedImage();
291 * @return A reference to the selected image.
293 Actor& GetSelectedImage();
296 * @return A reference to the background image.
298 Actor& GetBackgroundImage();
301 * @return A reference to the selected background image.
303 Actor& GetSelectedBackgroundImage();
306 * @return A reference to the disabled button image.
308 Actor& GetDisabledImage();
311 * @return A reference to the disabled selected image.
313 Actor& GetDisabledSelectedImage();
316 * @return A reference to the disabled background image.
318 Actor& GetDisabledBackgroundImage();
323 * Perform the click action to click the button.
324 * @param[in] attributes The attributes to perfrom this action.
325 * @return true if this control can perform action.
327 bool DoClickAction( const Property::Map& attributes );
330 * This method is called after the button initialization.
331 * Could be reimplemented in subclasses to provide specific behaviour.
333 virtual void OnButtonInitialize() { }
336 * This method is called when the label is set.
337 * @param[in] noPadding Used to bypass padding if the label is to be treated generically.
339 virtual void OnLabelSet( bool noPadding ) {}
342 * This method is called when the unselected button image is set
344 virtual void OnUnselectedImageSet() {}
347 * This method is called when the selected image is set
349 virtual void OnSelectedImageSet() {}
352 * This method is called when the background image is set
354 virtual void OnBackgroundImageSet() {}
357 * This method is called when the selected background image is set
359 virtual void OnSelectedBackgroundImageSet() {}
362 * This method is called when the disabled button image is set
364 virtual void OnDisabledImageSet() {}
367 * This method is called when the disabled selected image is set
369 virtual void OnDisabledSelectedImageSet() {}
372 * This method is called when the disabled background image is set
374 virtual void OnDisabledBackgroundImageSet() {}
377 * This method is called from the OnTouchEvent method when the button is down.
378 * Could be reimplemented in subclasses to provide specific behaviour.
380 virtual void OnButtonDown();
383 * This method is called from the OnTouchEvent method when the button is up.
384 * Could be reimplemented in subclasses to provide specific behaviour.
386 virtual void OnButtonUp();
389 * This method is called from the OnTouchEvent method when the touch point leaves the boundary of the button or
390 * more than one touch points are received.
391 * Could be reimplemented in subclasses to provide specific behaviour.
393 virtual void OnTouchPointLeave();
396 * This method is called from the OnTouchEvent method when the touch point is interrupted.
397 * Could be reimplemented in subclasses to provide specific behaviour.
399 virtual void OnTouchPointInterrupted();
402 * This method is called when the button is removed from the stage.
403 * Could be reimplemented in subclasses to provide specific behaviour.
405 virtual void OnButtonStageDisconnection();
408 * This method is called when the \e selected property is changed.
410 virtual void OnSelected() {}
413 * This method is called when the \e disabled property is changed.
415 virtual void OnDisabled() {}
418 * This method is called when the button is pressed.
420 virtual void OnPressed() {}
423 * This method is called when the button is released.
425 virtual void OnReleased() {}
430 * @copydoc Dali::Toolkit::PushButton::PressedSignal()
432 Toolkit::Button::ButtonSignalType& PressedSignal();
435 * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
437 Toolkit::Button::ButtonSignalType& ReleasedSignal();
440 * @copydoc Dali::Toolkit::Button::ClickedSignal()
442 Toolkit::Button::ButtonSignalType& ClickedSignal();
445 * @copydoc Dali::Toolkit::Button::StateChangedSignal()
447 Toolkit::Button::ButtonSignalType& StateChangedSignal();
450 * Connects a callback function with the object's signals.
451 * @param[in] object The object providing the signal.
452 * @param[in] tracker Used to disconnect the signal.
453 * @param[in] signalName The signal to connect to.
454 * @param[in] functor A newly allocated FunctorDelegate.
455 * @return True if the signal was connected.
456 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
458 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
463 * Called when a property of an object of this type is set.
464 * @param[in] object The object whose property is set.
465 * @param[in] index The property index.
466 * @param[in] value The new property value.
468 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
471 * Called to retrieve a property of an object of this type.
472 * @param[in] object The object whose property is to be retrieved.
473 * @param[in] index The property index.
474 * @return The current value of the property.
476 static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
478 protected: // From CustomActorImpl
481 * @copydoc Dali::CustomActorImpl::OnTouchEvent( const TouchEvent& event )
483 virtual bool OnTouchEvent( const TouchEvent& event );
485 private: // From Control
488 * @copydoc Toolkit::Control::OnInitialize()
490 virtual void OnInitialize();
493 * @copydoc Toolkit::Control::OnAccessibilityActivated()
495 virtual bool OnAccessibilityActivated();
498 * @copydoc Toolkit::Control::OnKeyboardEnter()
500 virtual bool OnKeyboardEnter();
503 * Callback received when the button is disconnected from the stage.
504 * It resets the button status.
506 void OnControlStageDisconnection();
511 * Handler for tap events.
512 * We do not actually do anything when we receive a tap as the button handles tap event through
513 * the touch event system itself as it requires more than just tap handling (e.g. leave events).
514 * This stops any of our parents receiving a tap gesture when it occurs within our area.
515 * @param[in] actor The tapped actor.
516 * @param[in] tap The tap gesture.
518 void OnTap(Actor actor, const TapGesture& tap);
521 * Sets up the autorepeating timer.
522 * @param[in] delay The delay time in seconds.
524 void SetUpTimer( float delay );
527 * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
529 bool AutoRepeatingSlot();
532 * Sets the button as selected or unselected.
533 * @param[in] selected \e selected property value.
534 * @param[in] emitSignal Emit a signal if this value is \e true.
536 void SetSelected( bool selected, bool emitSignal );
539 * This method is called when the button is pressed.
544 * This method is called when the button is released.
549 * Used to perform common setup applied to images within button.
550 * This will replace the current image with the specifed one.
551 * @param[in] actorToModify The image to replace.
552 * @param[out] newActor The new image to use.
554 void SetupContent( Actor& actorToModify, Actor newActor );
557 * Sets the color of the unselected image.
558 * If no image exists, it is created.
559 * @param[in] color The color to use.
561 void SetUnselectedColor( const Vector4& color );
564 * Gets the unselected content color.
565 * @return The currently used unselected color.
567 const Vector4 GetUnselectedColor() const;
570 * Sets the color of the selected image.
571 * If no image exists, it is created.
572 * @param[in] color The color to use.
574 void SetSelectedColor( const Vector4& color );
577 * Gets the selected content color.
578 * @return The currently used selected color.
580 const Vector4 GetSelectedColor() const;
586 ButtonUp, ///< The button is up.
587 ButtonDown, ///< The button is down.
591 * Button paint states.
595 UnselectedState, ///< The button is unselected.
596 SelectedState, ///< The button is selected.
597 DisabledUnselectedState, ///< The button is disabled and unselected.
598 DisabledSelectedState, ///< The button is disabled and selected.
602 * Enum to specify which decoration when getting and setting decorations.
606 UNSELECTED_DECORATION = 0,
611 ButtonState GetState();
612 PaintState GetPaintState();
613 void SetDecoration( DecorationState state, Actor actor );
614 Actor& GetDecoration( DecorationState state );
618 * Returns the animation to be used for transitioning creating the animation if needed.
619 * @return The initialised transition animation.
621 Dali::Animation GetTransitionAnimation();
624 * Prepares the actor to be transitioned in.
625 * @param[in] actor The actor that will be transitioned in.
627 virtual void PrepareForTranstionIn( Actor actor ) {}
630 * Prepares the actor to be transitioned in.
631 * @param[in] actor The actor that will be transitioned out.
633 virtual void PrepareForTranstionOut( Actor actor ) {}
636 * Transitions the actor in, allowing derived classes to configure
637 * the GetTransitionAnimation() animation ready.
638 * Button is in charge of calling Dali::Animation::Play and so derived classes
639 * only need to add the animation.
641 virtual void OnTransitionIn( Actor actor ) {}
644 * Transitions the actor out, allowing derived classes to configure
645 * the GetTransitionAnimation() animation ready.
646 * Button is in charge of calling Dali::Animation::Play and so derived classes
647 * only need to add the animation.
649 virtual void OnTransitionOut( Actor actor ) {}
654 * Starts the transition animation.
655 * Button::TransitionFinished is called when the animation finishes.
657 void StartTransitionAnimation();
660 * This method stops all transition animations
662 void StopTransitionAnimation();
665 * Called when the transition animation finishes.
667 void TransitionAnimationFinished( Dali::Animation& source );
670 * Resets the Button to the base state for the current paint state.
671 * Any additionally inserted images needed for transitions that are
672 * no longer needed and the removed.
674 void ResetImageLayers();
677 * Transitions out the actor
679 void TransitionOut( Actor actor );
682 * Removes the actor from the button and prepares it to be transitioned out
684 void RemoveButtonImage( Actor& actor );
687 * Finds the index of the actor.
688 * If the actor doesn't exist, return the last index + 1.
690 unsigned int FindChildIndex( Actor& actor );
693 * Adds an actor to the hierarchy and prepares it to be transitioned.
694 * @param[in] actor The actor to add
696 void PrepareAddButtonImage( Actor& actor );
699 * Adds an actor to the hierarchy and marks it to be transitioned.
700 * @param[in] actor The actor to add
702 void TransitionButtonImage( Actor& actor );
705 * Adds an actor to the hierarchy.
706 * @param[in] actor The actor to add
708 void AddButtonImage( Actor& actor );
711 * (Re)Adds the label (if exists) to the hierarchy (so it is always on top).
716 Button( const Button& );
719 Button& operator = ( const Button& );
724 Toolkit::Button::ButtonSignalType mPressedSignal; ///< Signal emitted when the button is pressed.
725 Toolkit::Button::ButtonSignalType mReleasedSignal; ///< Signal emitted when the button is released.
726 Toolkit::Button::ButtonSignalType mClickedSignal; ///< Signal emitted when the button is clicked.
727 Toolkit::Button::ButtonSignalType mStateChangedSignal; ///< Signal emitted when the button's state is changed.
729 Timer mAutoRepeatingTimer; ///< Timer used to implement the autorepeating property.
731 Actor mLabel; ///< Stores the button label.
733 Actor mDecoration[ DECORATION_STATES ]; ///< Stores the decorations for both selected and unselected states.
735 Actor mUnselectedContent; ///< Stores the unselected content.
736 Actor mSelectedContent; ///< Stores the selected content.
737 Actor mBackgroundContent; ///< Stores the background content.
738 Actor mSelectedBackgroundContent; ///< Stores the selected background content.
739 Actor mDisabledContent; ///< Stores the disabled content.
740 Actor mDisabledSelectedContent; ///< Stores the disabled selected content.
741 Actor mDisabledBackgroundContent; ///< Stores the disabled background content.
743 Animation mTransitionAnimation; ///< Animation used in the state transitions.
745 TapGestureDetector mTapDetector;
747 Vector4 mUnselectedColor; ///< Color to use for unselected content.
748 Vector4 mSelectedColor; ///< Color to use for selected content.
750 bool mDisabled; ///< Stores the disabled property.
751 bool mAutoRepeating; ///< Stores the autorepeating property.
752 bool mTogglableButton; ///< Stores the togglable property.
753 bool mSelected; ///< Stores the selected state.
754 float mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
755 float mNextAutoRepeatingDelay; ///< Stores the next autorepeating delay in seconds.
757 float mAnimationTime; ///< The animation time.
760 bool mClickActionPerforming;
762 ButtonState mState; ///< Stores the button state.
763 PaintState mPaintState; ///< Stores the paint state.
766 } // namespace Internal
768 // Helpers for public-api forwarding methods
770 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
772 DALI_ASSERT_ALWAYS( button );
774 Dali::RefObject& handle = button.GetImplementation();
776 return static_cast<Toolkit::Internal::Button&>( handle );
779 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
781 DALI_ASSERT_ALWAYS( button );
783 const Dali::RefObject& handle = button.GetImplementation();
785 return static_cast<const Toolkit::Internal::Button&>( handle );
788 } // namespace Toolkit
792 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__