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/devel-api/visual-factory/visual-base.h>
27 #include <dali-toolkit/public-api/controls/buttons/button.h>
28 #include <dali-toolkit/public-api/controls/control-impl.h>
30 //#if defined(DEBUG_ENABLED)
31 // Debug::Filter* gLogButtonFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_BUTTON_CONTROL");
46 * @copydoc Toolkit::Button
48 * Button is the base class implementation for all buttons.
52 * All Foreground/Icon visuals expected to be the same size.
53 * Background visuals will take the size of the control.
54 * Padding and struts take size precedence over visuals when available space is limited.
55 * Icon/Foreground visuals take size precedence over Labels when available space is limited.
57 class Button : public Control
63 * Enum describing the position the text label can be in relation to the control (and foreground/icon)
67 BEGIN, // At the start of the control before the foreground/icon
68 END, // At the end of the control after the foreground/icon
69 TOP, // At the top of the control above the foreground/icon
70 BOTTOM // At the bottom of the control below the foreground/icon
76 * @copydoc Dali::Toolkit::Button::SetDisabled
78 void SetDisabled( bool disabled );
81 * @copydoc Dali::Toolkit::Button::IsDisabled
83 bool IsDisabled() const;
86 * @copydoc Dali::Toolkit::Button::SetAutoRepeating
88 void SetAutoRepeating( bool autoRepeating );
91 * @copydoc Dali::Toolkit::Button::IsAutoRepeating
93 bool IsAutoRepeating() const;
96 * @copydoc Dali::Toolkit::Button::SetInitialAutoRepeatingDelay
98 void SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay );
101 * @copydoc Dali::Toolkit::Button::GetInitialAutoRepeatingDelay
103 float GetInitialAutoRepeatingDelay() const;
106 * @copydoc Dali::Toolkit::Button::SetNextAutoRepeatingDelay
108 void SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay );
111 * @copydoc Dali::Toolkit::Button::GetNextAutoRepeatingDelay
113 float GetNextAutoRepeatingDelay() const;
116 * @copydoc Dali::Toolkit::Button::SetTogglableButton
118 void SetTogglableButton( bool togglable );
121 * @copydoc Dali::Toolkit::Button::IsTogglableButton
123 bool IsTogglableButton() const;
126 * @copydoc Dali::Toolkit::Button::SetSelected
128 void SetSelected( bool selected );
131 * @copydoc Dali::Toolkit::Button::IsSelected
133 bool IsSelected() const;
136 * @copydoc Dali::Toolkit::Button::SetAnimationTime
138 void SetAnimationTime( float animationTime );
141 * @copydoc Dali::Toolkit::Button::GetAnimationTime
143 float GetAnimationTime() const;
146 * @copydoc Dali::Toolkit::Button::SetLabelText
148 void SetLabelText( const std::string& label );
151 * @copydoc Dali::Toolkit::Button::GetLabelText
153 std::string GetLabelText() const;
156 * @brief Sets the specified properties on the button label.
157 * If the label does not exist yet, it is created.
158 * The derived buttons are notified if any properties are changed.
159 * @param[in] properties A Property::Map of key-value pairs of properties to set.
161 void SetupLabel( const Property::Map& properties );
164 * Performs actions as requested using the action name.
165 * @param[in] object The object on which to perform the action.
166 * @param[in] actionName The action to perform.
167 * @param[in] attributes The attributes with which to perfrom this action.
168 * @return true if action has been accepted by this control
170 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
172 public: // Deprecated API
175 * @copydoc Dali::Toolkit::Button::SetLabel( Actor label )
177 void SetLabel( Actor label );
180 * @deprecated Sets the unselected image with an url.
181 * @param[in] image The Actor to use.
183 void SetUnselectedImage( const std::string& filename );
186 * @deprecated Sets the selected image with an url.
187 * @param[in] filename The url of the image to use to use.
189 void SetSelectedImage( const std::string& filename );
192 * @deprecated Sets the selected background image with an url.
193 * @param[in] filename The url of the image to use to use.
195 void SetSelectedBackgroundImage( const std::string& filename );
198 * @deprecated Sets the background image with an url.
199 * @param[in] filename The url of the image to use to use.
201 void SetBackgroundImage( const std::string& filename );
204 * @deprecated Sets the disabled unselected background image with an url.
205 * @param[in] filename The url of the image to use to use.
207 void SetDisabledBackgroundImage( const std::string& filename );
210 * @deprecated Sets the disabled unselected image with an url.
211 * @param[in] filename The url of the image to use to use.
213 void SetDisabledImage( const std::string& filename );
216 * @deprecated Sets the disabled selected image with an url.
217 * @param[in] filename The url of the image to use to use.
219 void SetDisabledSelectedImage( const std::string& filename );
222 * @deprecated Sets the unselected image with an Actor.
223 * @param[in] image The Actor to use.
225 void SetUnselectedImage( Actor image );
228 * @deprecated Sets the selected image with an Actor.
229 * @param[in] image The Actor to use.
231 void SetSelectedImage( Actor image );
234 * @deprecated Sets the selected background image with an Actor.
235 * @param[in] image The Actor to use.
237 void SetSelectedBackgroundImage( Actor image );
240 * @deprecated Sets the disabled image with an Actor.
241 * @param[in] image The Actor to use.
243 void SetDisabledImage( Actor image );
246 * @deprecated Sets the disabled selected image with an Actor.
247 * @param[in] image The Actor to use.
249 void SetDisabledSelectedImage( Actor image );
252 * @deprecated Sets the disabled background image with an Actor.
253 * @param[in] image The Actor to use.
255 void SetDisabledBackgroundImage( Actor image );
258 * @Gets url of a image visual, used by GetProperty but for deprecated Properties
259 * @param[in] index Visual index of url required
260 * @return filename for the corresponding visual
262 std::string GetUrlForImageVisual( Property::Index index );
266 * @copydoc Dali::Toolkit::Button::GetButtonImage
268 Actor GetButtonImage() const;
271 * @copydoc Dali::Toolkit::Button::GetSelectedImage
273 Actor GetSelectedImage() const;
282 UNSELECTED_STATE, ///< The button is unselected.
283 SELECTED_STATE, ///< The button is selected.
284 DISABLED_UNSELECTED_STATE, ///< The button is disabled and unselected.
285 DISABLED_SELECTED_STATE, ///< The button is disabled and selected.
286 STATE_COUNT, ///< Number of States
290 * Enum to distinguish the different style-able components of the button
294 UNSELECTED_FOREGROUND = 0,
296 DISABLED_SELECTED_FOREGROUND,
297 DISABLED_UNSELECTED_FOREGROUND,
298 UNSELECTED_BACKGROUND,
300 DISABLED_UNSELECTED_BACKGROUND,
301 DISABLED_SELECTED_BACKGROUND,
306 * Enum to list types of visual a state can have.
318 * Button press state which is not the same as the actual button's state.
319 * A button can be DEPRESSED but the until released the button state may have not changed,
320 * For example DEPRESSING a toggle button that is already in the SELECTED state will not change the button state untill released.
324 DEPRESSED, ///< The button is up.
325 UNPRESSED, ///< The button is down.
326 TOGGLE_DEPRESSED, ///< The button has been pressed down and will stay depressed when released.
330 * Construct a new Button.
335 * A reference counted object may only be deleted by calling Unreference()
339 * @return A reference to the label actor.
341 Actor& GetLabelActor();
344 * @return A reference to the unselected button image.
346 Actor GetUnselectedImage();
349 * @return A reference to the selected image.
351 Actor GetSelectedImage();
356 * Perform the click action to click the button.
357 * @param[in] attributes The attributes to perfrom this action.
358 * @return true if this control can perform action.
360 bool DoClickAction( const Property::Map& attributes );
363 * This method is called when the label is set.
364 * @param[in] noPadding Used to bypass padding if the label is to be treated generically.
366 virtual void OnLabelSet( bool noPadding ) {}
369 * This method is called when the unselected button image is set
371 virtual void OnUnselectedImageSet() {}
374 * This method is called when the selected image is set
376 virtual void OnSelectedImageSet() {}
379 * This method is called when the background image is set
381 virtual void OnBackgroundImageSet() {}
384 * This method is called when the selected background image is set
386 virtual void OnSelectedBackgroundImageSet() {}
389 * This method is called when the disabled button image is set
391 virtual void OnDisabledImageSet() {}
394 * This method is called when the disabled selected image is set
396 virtual void OnDisabledSelectedImageSet() {}
399 * This method is called when the disabled background image is set
401 virtual void OnDisabledBackgroundImageSet() {}
404 * This method is called the button is down.
405 * Could be reimplemented in subclasses to provide specific behaviour.
407 virtual void OnButtonDown();
410 * This method is called when the button is up.
411 * Could be reimplemented in subclasses to provide specific behaviour.
413 virtual void OnButtonUp();
416 * This method is called when touch leaves the boundary of the button or several touch points are received.
417 * Could be reimplemented in subclasses to provide specific behaviour.
419 virtual void OnTouchPointLeave();
422 * This method is called when the touch is interrupted.
423 * Could be reimplemented in subclasses to provide specific behaviour.
425 virtual void OnTouchPointInterrupted();
428 * This method is called when the \e selected property is changed.
430 virtual void OnStateChange( State newState ){}
433 * This method is called when the \e disabled property is changed.
435 virtual void OnDisabled() {}
438 * This method is called when the button is pressed.
440 virtual void OnPressed() {}
443 * This method is called when the button is released.
445 virtual void OnReleased() {}
450 * @copydoc Dali::Toolkit::PushButton::PressedSignal()
452 Toolkit::Button::ButtonSignalType& PressedSignal();
455 * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
457 Toolkit::Button::ButtonSignalType& ReleasedSignal();
460 * @copydoc Dali::Toolkit::Button::ClickedSignal()
462 Toolkit::Button::ButtonSignalType& ClickedSignal();
465 * @copydoc Dali::Toolkit::Button::StateChangedSignal()
467 Toolkit::Button::ButtonSignalType& StateChangedSignal();
470 * Connects a callback function with the object's signals.
471 * @param[in] object The object providing the signal.
472 * @param[in] tracker Used to disconnect the signal.
473 * @param[in] signalName The signal to connect to.
474 * @param[in] functor A newly allocated FunctorDelegate.
475 * @return True if the signal was connected.
476 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
478 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
483 * Called when a property of an object of this type is set.
484 * @param[in] object The object whose property is set.
485 * @param[in] index The property index.
486 * @param[in] value The new property value.
488 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
491 * Called to retrieve a property of an object of this type.
492 * @param[in] object The object whose property is to be retrieved.
493 * @param[in] index The property index.
494 * @return The current value of the property.
496 static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
498 protected: // From Control
501 * @copydoc Toolkit::Control::OnInitialize()
502 * @note If overridden by deriving button classes, then an up-call to Button::OnInitialize MUST be made at the start.
504 virtual void OnInitialize();
507 * @copydoc Toolkit::Control::OnAccessibilityActivated()
509 virtual bool OnAccessibilityActivated();
512 * @copydoc Toolkit::Control::OnKeyboardEnter()
514 virtual bool OnKeyboardEnter();
517 * @copydoc Toolkit::Control::OnStageDisconnection()
518 * @note If overridden by deriving button classes, then an up-call to Button::OnStageDisconnection MUST be made at the end.
520 virtual void OnStageDisconnection();
523 * @copydoc Toolkit::Control::OnStageConnnection()
525 virtual void OnStageConnection( int depth );
528 * @copydoc Toolkit::Control::GetNaturalSize
530 virtual Vector3 GetNaturalSize();
533 * @copydoc Toolkit::Control::OnSetResizePolicy
535 virtual void OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension );
538 * @copydoc Toolkit::Control::OnRelayout
540 virtual void OnRelayout( const Vector2& size, RelayoutContainer& container );
545 * @brief Handler for touch data
546 * @param[in] actor The touched actor.
547 * @param[in] touch The touch info.
548 * @return true, if consumed, false otherwise.
550 bool OnTouch( Actor actor, const TouchData& touch );
553 * Handler for tap events.
554 * We do not actually do anything when we receive a tap as the button handles tap event through
555 * the touch event system itself as it requires more than just tap handling (e.g. leave events).
556 * This stops any of our parents receiving a tap gesture when it occurs within our area.
557 * @param[in] actor The tapped actor.
558 * @param[in] tap The tap gesture.
560 void OnTap(Actor actor, const TapGesture& tap);
563 * Sets up the autorepeating timer.
564 * @param[in] delay The delay time in seconds.
566 void SetUpTimer( float delay );
569 * Button has been pressed
574 * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
576 bool AutoRepeatingSlot();
579 * Check the requested state is an allowed transition.
580 * Some states can not be transitioned to from certain states.
581 * @param[in] requestedState check if can transition to this state
582 * @return bool true if state change valid
584 bool ValidateState( State requestedState );
587 * Perform the given function on the visuals in the given state. Can be used to add and remove visuals.
588 * @param[in] functionPtr pointer to the function to perform an action on a visual
589 * @param[in] state Visuals in this state will be the target
591 void PerformFunctionOnVisualsInState( void(Button::*functionPtr)( Property::Index visualIndex), State state );
594 * Transition the visual in or out using the set animation
595 * @param[in] state State to transition in or out
597 void SetVisualsForTransition( State state );
600 * Changes the button state when an action occurs on it
601 * @param[in] requestedState the state to change to
603 void ChangeState( State requestedState );
606 * @brief Get unselected button color
607 * @return color as vector4
609 const Vector4 GetUnselectedColor() const;
612 * @brief Get selected button color
613 * @return color as vector4
615 const Vector4 GetSelectedColor() const;
618 * Sets the color of button in selected or unselected state, if image also supplied this color will be appplied to it.
619 * If no visual exists, it is created.
620 * @param[in] color The color to use.
621 * @param[in] visualIndex The Visual to apply the color
623 void SetColor( const Vector4& color, Property::Index visualIndex );
626 * This method is called when the button is released.
633 * Set Text Label Padding
634 * @param[in] padding BEGIN END BOTTOM TOP
636 void SetLabelPadding( const Padding& padding );
639 * Get Text Label padding
642 Padding GetLabelPadding();
645 * Set Foreground/icon Padding
646 * @param[in] padding BEGIN END BOTTOM TOP
648 void SetForegroundPadding( const Padding& padding);
651 * Get Foreground padding
654 Padding GetForegroundPadding();
657 * @brief Get the pressed state of the button
658 * @return ButtonPressedState the state the button is in
660 PressState GetPressedState();
663 * @brief Get the state of the button
664 * @return PaintState the state the button is in
666 State GetButtonState();
669 * @brief Setup the button components for example foregrounds and background
670 * @param[in] index the index of the visual to set
671 * @param[in] value the value to set on the component
672 * @param[in] visualDepth the depth of the visual if overlapping another
674 void CreateVisualsForComponent( Property::Index index ,const Property::Value& value, const float visualDepth );
677 * Returns the animation to be used for transition, creating the animation if needed.
678 * @return The initialised transition animation.
680 Dali::Animation GetTransitionAnimation();
683 * @brief Set distance between label and foreground/icon, if both present
684 * @param[in] length length of strut
686 void SetLabelStrutLength( unsigned int length );
689 * @brief Set the position of the label relative to foreground/icon, if both present
690 * @param[in] labelAlignment given alignment setting
692 void SetLabelAlignment( Align labelAlignment);
695 * @brief Get set distance between label and foreground/icon visual
696 * @return length of strut
698 float GetLabelStrutLength();
701 * @brief Get set alignment of label in relation to foreground/icon
702 * @return Set alignment value
704 Align GetLabelAlignment();
707 * Prepares the actor to be transitioned in.
708 * @param[in] actor The actor that will be transitioned in.
710 virtual void PrepareForTransitionIn( Actor actor ) {}
713 * Prepares the actor to be transitioned in.
714 * @param[in] actor The actor that will be transitioned out.
716 virtual void PrepareForTransitionOut( Actor actor ) {}
719 * Transitions the actor in, allowing derived classes to configure
720 * the GetTransitionAnimation() animation ready.
721 * Button is in charge of calling Dali::Animation::Play and so derived classes
722 * only need to add the animation.
724 virtual void OnTransitionIn( Actor actor );
727 * Transitions the actor out, allowing derived classes to configure
728 * the GetTransitionAnimation() animation ready.
729 * Button is in charge of calling Dali::Animation::Play and so derived classes
730 * only need to add the animation.
732 virtual void OnTransitionOut( Actor actor ) {}
735 * Removes the visual from the button (un-staged)
736 * If the derived button does not want the visual removed then use this virtual function to
737 * define the required behaviour.
738 * Can decide to only remove specified visuals via index
740 virtual void OnButtonVisualRemoval( Property::Index visualIndex );
746 * Starts the transition animation.
747 * Button::TransitionFinished is called when the animation finishes.
749 void StartTransitionAnimation();
752 * This method stops and clears animations
754 void ClearTransitionAnimation();
757 * Called when the transition animation finishes.
759 void TransitionAnimationFinished( Dali::Animation& source );
762 * Transition button visual using an animation before removal from Stage
764 void TransitionButtonVisualOut( Property::Index visualIndex );
767 * Transition button visual using an animation before adding to Stage
769 void TransitionButtonVisualIn( Property::Index visualIndex );
772 * Removes the visual from the button and prepares it to be transitioned out
773 * @param[in] visualIndex the visual to remove
775 void RemoveVisual( Property::Index visualIndex );
778 * Adds the required visual to the button.
779 * @param[in] visualIndex The Property index of the visual required
781 void SelectRequiredVisual( Property::Index visualIndex );
784 Button( const Button& );
787 Button& operator = ( const Button& );
792 Toolkit::Button::ButtonSignalType mPressedSignal; ///< Signal emitted when the button is pressed.
793 Toolkit::Button::ButtonSignalType mReleasedSignal; ///< Signal emitted when the button is released.
794 Toolkit::Button::ButtonSignalType mClickedSignal; ///< Signal emitted when the button is clicked.
795 Toolkit::Button::ButtonSignalType mStateChangedSignal; ///< Signal emitted when the button's state is changed.
797 Timer mAutoRepeatingTimer;
799 Actor mLabel; ///< Stores the button text label.
800 Padding mLabelPadding; ///< The padding around the label (if present).
801 Padding mForegroundPadding; ///< The padding around the foreground/icon visual (if present).
803 unsigned int mForeGroundToLabelStrutLength; ///< Distance between foreground/icon and label.
804 Align mTextLabelAlignment; ///< Position of text label in relation to foreground/icon when both are present.
806 Animation mTransitionAnimation; ///< Used in the state transitions.
808 TapGestureDetector mTapDetector;
810 Vector4 mUnselectedColor;
811 Vector4 mSelectedColor;
813 bool mAutoRepeating; ///< Stores the autorepeating property.
814 bool mTogglableButton; ///< Stores the togglable property as a flag.
815 float mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
816 float mNextAutoRepeatingDelay; ///< Stores the next autorepeating delay in seconds.
818 float mAnimationTime;
820 PressState mButtonPressedState; ///< In relation to the button being pressed/released
822 State mPreviousButtonState; ///< During a transition between two states, this stores the previous state so Visuals can be removed.
825 bool mClickActionPerforming; ///< Used to manage signal emissions during action
828 } // namespace Internal
830 // Helpers for public-api forwarding methods
832 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
834 DALI_ASSERT_ALWAYS( button );
836 Dali::RefObject& handle = button.GetImplementation();
838 return static_cast<Toolkit::Internal::Button&>( handle );
841 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
843 DALI_ASSERT_ALWAYS( button );
845 const Dali::RefObject& handle = button.GetImplementation();
847 return static_cast<const Toolkit::Internal::Button&>( handle );
850 } // namespace Toolkit
854 #endif // DALI_TOOLKIT_INTERNAL_BUTTON_H