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>
25 #include <dali-toolkit/public-api/controls/buttons/button.h>
26 #include <dali-toolkit/public-api/controls/control-impl.h>
40 * Button is the base class implementation for all buttons.
42 class Button : public Control
48 * Construct a new Button.
53 * A reference counted object may only be deleted by calling Unreference()
60 * @copydoc Dali::Toolkit::Button::SetDisabled( bool disabled )
62 void SetDisabled( bool disabled );
65 * @copydoc Dali::Toolkit::Button::IsDisabled() const
67 bool IsDisabled() const;
70 * @copydoc Dali::Toolkit::Button::SetAutoRepeating( bool autoRepeating )
72 void SetAutoRepeating( bool autoRepeating );
75 * @copydoc Dali::Toolkit::Button::IsAutoRepeating() const
77 bool IsAutoRepeating() const;
80 * @copydoc Dali::Toolkit::Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
82 void SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay );
85 * @copydoc Dali::Toolkit::Button::GetInitialAutoRepeatingDelay() const
87 float GetInitialAutoRepeatingDelay() const;
90 * @copydoc Dali::Toolkit::Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
92 void SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay );
95 * @copydoc Dali::Toolkit::Button::GetNextAutoRepeatingDelay() const
97 float GetNextAutoRepeatingDelay() const;
100 * @copydoc Dali::Toolkit::Button::SetTogglableButton( bool togglable )
102 void SetTogglableButton( bool togglable );
105 * @copydoc Dali::Toolkit::Button::IsTogglableButton() const
107 bool IsTogglableButton() const;
110 * @copydoc Dali::Toolkit::Button::SetSelected( bool selected )
112 void SetSelected( bool selected );
115 * @copydoc Dali::Toolkit::Button::IsSelected() const
117 bool IsSelected() const;
120 * @copydoc Dali::Toolkit::Button::SetAnimationTime()
122 void SetAnimationTime( float animationTime );
125 * @copydoc Dali::Toolkit::Button::GetAnimationTime()
127 float GetAnimationTime() const;
130 * @copydoc Dali::Toolkit::Button::SetLabel( const std::string& label )
132 void SetLabel( const std::string& label );
135 * @copydoc Dali::Toolkit::Button::SetLabel( Actor label )
137 void SetLabel( Actor label );
140 * @copydoc Dali::Toolkit::Button::GetLabel()
142 Actor GetLabel() const;
145 * @copydoc Dali::Toolkit::PushButton::SetButtonImage( Actor image )
147 void SetButtonImage( Actor image );
150 * @copydoc Dali::Toolkit::PushButton::GetButtonImage()
152 Actor GetButtonImage() const;
156 * @return A reference to the button image.
158 Actor& GetButtonImage();
161 * @copydoc Dali::Toolkit::PushButton::SetSelectedImage( Actor image )
163 void SetSelectedImage( Actor image );
166 * @copydoc Dali::Toolkit::PushButton::GetSelectedImage()
168 Actor GetSelectedImage() const;
172 * @return A reference to the selected image.
174 Actor& GetSelectedImage();
177 * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage( Actor image )
179 void SetBackgroundImage( Actor image );
182 * @copydoc Dali::Toolkit::PushButton::GetBackgroundImage()
184 Actor GetBackgroundImage() const;
188 * @return A reference to the background image.
190 Actor& GetBackgroundImage();
193 * @copydoc Dali::Toolkit::PushButton::SetSelectedBackgroundImage( Actor image )
195 void SetSelectedBackgroundImage( Actor image );
198 * @copydoc Dali::Toolkit::PushButton::GetSelectedBackgroundImage()
200 Actor GetSelectedBackgroundImage() const;
204 * @return A reference to the selected background image.
206 Actor& GetSelectedBackgroundImage();
209 * @copydoc Dali::Toolkit::PushButton::SetDisabledImage( Actor image )
211 void SetDisabledImage( Actor image );
214 * @copydoc Dali::Toolkit::PushButton::GetDisabledImage()
216 Actor GetDisabledImage() const;
220 * @return A reference to the disabled button image.
222 Actor& GetDisabledImage();
225 * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage( Actor image )
227 void SetDisabledSelectedImage( Actor image );
230 * @copydoc Dali::Toolkit::CheckBoxButton::GetDisabledSelectedImage()
232 Actor GetDisabledSelectedImage() const;
236 * @return A reference to the disabled selected image.
238 Actor& GetDisabledSelectedImage();
241 * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage( Actor image )
243 void SetDisabledBackgroundImage( Actor image );
246 * @copydoc Dali::Toolkit::PushButton::GetDisabledBackgroundImage()
248 Actor GetDisabledBackgroundImage() const;
252 * @return A reference to the disabled background image.
254 Actor& GetDisabledBackgroundImage();
257 * Performs actions as requested using the action name.
258 * @param[in] object The object on which to perform the action.
259 * @param[in] actionName The action to perform.
260 * @param[in] attributes The attributes with which to perfrom this action.
261 * @return true if action has been accepted by this control
263 static bool DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes );
268 * @return A reference to the label actor.
273 * It changes the transition state of the push button.
275 void UpdatePaintTransitionState();
280 * Perform the click action to click the button.
281 * @param[in] attributes The attributes to perfrom this action.
283 void DoClickAction( const PropertyValueContainer& attributes );
286 * This method is called after the button initialization.
287 * Could be reimplemented in subclasses to provide specific behaviour.
289 virtual void OnButtonInitialize() { }
292 * This method is called when the label is set.
294 virtual void OnLabelSet() {}
297 * This method is called when the button image is set
299 virtual void OnButtonImageSet() {}
302 * This method is called when the selected image is set
304 virtual void OnSelectedImageSet() {}
307 * This method is called when the background image is set
309 virtual void OnBackgroundImageSet() {}
312 * This method is called when the selected background image is set
314 virtual void OnSelectedBackgroundImageSet() {}
317 * This method is called when the disabled button image is set
319 virtual void OnDisabledImageSet() {}
322 * This method is called when the disabled selected image is set
324 virtual void OnDisabledSelectedImageSet() {}
327 * This method is called when the disabled background image is set
329 virtual void OnDisabledBackgroundImageSet() {}
332 * This method is called from the OnTouchEvent method when the button is down.
333 * Could be reimplemented in subclasses to provide specific behaviour.
335 virtual void OnButtonDown();
338 * This method is called from the OnTouchEvent method when the button is up.
339 * Could be reimplemented in subclasses to provide specific behaviour.
341 virtual void OnButtonUp();
344 * This method is called from the OnTouchEvent method when the touch point leaves the boundary of the button or
345 * more than one touch points are received.
346 * Could be reimplemented in subclasses to provide specific behaviour.
348 virtual void OnTouchPointLeave();
351 * This method is called from the OnTouchEvent method when the touch point is interrupted.
352 * Could be reimplemented in subclasses to provide specific behaviour.
354 virtual void OnTouchPointInterrupted();
357 * This method is called when the button is removed from the stage.
358 * Could be reimplemented in subclasses to provide specific behaviour.
360 virtual void OnButtonStageDisconnection();
363 * This method is called when the \e selected property is changed.
364 * @return true if the transition animation is started.
366 virtual bool OnSelected() { return false; }
369 * This method is called when the \e disabled property is changed.
370 * @return true if the transition animation is started.
372 virtual bool OnDisabled() { return false; }
375 * This method is called when the button is pressed.
376 * @return true if the transition animation is started.
378 virtual bool OnPressed() { return false; }
381 * This method is called when the button is released.
382 * @return true if the transition animation is started.
384 virtual bool OnReleased() { return false; }
387 * This method stops all animations
389 virtual void StopAllAnimations() {}
394 * @copydoc Dali::Toolkit::PushButton::PressedSignal()
396 Toolkit::Button::ButtonSignalType& PressedSignal();
399 * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
401 Toolkit::Button::ButtonSignalType& ReleasedSignal();
404 * @copydoc Dali::Toolkit::Button::ClickedSignal()
406 Toolkit::Button::ButtonSignalType& ClickedSignal();
409 * @copydoc Dali::Toolkit::Button::StateChangedSignal()
411 Toolkit::Button::ButtonSignalType& StateChangedSignal();
414 * Connects a callback function with the object's signals.
415 * @param[in] object The object providing the signal.
416 * @param[in] tracker Used to disconnect the signal.
417 * @param[in] signalName The signal to connect to.
418 * @param[in] functor A newly allocated FunctorDelegate.
419 * @return True if the signal was connected.
420 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
422 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
427 * Called when a property of an object of this type is set.
428 * @param[in] object The object whose property is set.
429 * @param[in] index The property index.
430 * @param[in] value The new property value.
432 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
435 * Called to retrieve a property of an object of this type.
436 * @param[in] object The object whose property is to be retrieved.
437 * @param[in] index The property index.
438 * @return The current value of the property.
440 static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
442 protected: // From CustomActorImpl
445 * @copydoc Dali::CustomActorImpl::OnTouchEvent( const TouchEvent& event )
447 virtual bool OnTouchEvent( const TouchEvent& event );
449 private: // From Control
452 * @copydoc Toolkit::Control::OnInitialize()
454 virtual void OnInitialize();
457 * @copydoc Toolkit::Control::OnActivated()
459 virtual void OnActivated();
462 * Callback received when the button is disconnected from the stage.
463 * It resets the button status.
465 void OnControlStageDisconnection();
470 * Handler for tap events.
471 * We do not actually do anything when we receive a tap as the button handles tap event through
472 * the touch event system itself as it requires more than just tap handling (e.g. leave events).
473 * This stops any of our parents receiving a tap gesture when it occurs within our area.
474 * @param[in] actor The tapped actor.
475 * @param[in] tap The tap gesture.
477 void OnTap(Actor actor, const TapGesture& tap);
480 * Sets up the autorepeating timer.
481 * @param[in] delay The delay time in seconds.
483 void SetUpTimer( float delay );
486 * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
488 bool AutoRepeatingSlot();
491 * Sets the button as selected or unselected.
492 * @param[in] selected \e selected property value.
493 * @param[in] emitSignal Emit a signal if this value is \e true.
495 void SetSelected( bool selected, bool emitSignal );
498 * This method is called when the button is pressed.
503 * This method is called when the button is released.
511 ButtonUp, ///< The button is up.
512 ButtonDown, ///< The button is down.
516 * Button paint states.
520 UnselectedState, ///< The button is unselected.
521 SelectedState, ///< The button is selected.
522 DisabledUnselectedState, ///< The button is disabled and unselected.
523 DisabledSelectedState, ///< The button is disabled and selected.
524 UnselectedSelectedTransition, ///< The button is in transition from unselected to selected.
525 SelectedUnselectedTransition, ///< The button is in transition from selected to unselected.
526 UnselectedDisabledTransition, ///< The button is in transition from unselected to disabled.
527 DisabledUnselectedTransition, ///< The button is in transition from disabled to unselected.
528 SelectedDisabledTransition, ///< The button is in transition from selected to disabled.
529 DisabledSelectedTransition ///< The button is in transition from disabled to selected.
532 ButtonState GetState();
533 PaintState GetPaintState();
536 * Inserts the actor to the button.
538 void InsertChild( unsigned int index, Actor& actor );
541 * Removes the actor from the button.
543 void RemoveChild( Actor& actor );
546 * Finds the index of the actor.
547 * If the actor doesn't exist, return the last index + 1.
549 unsigned int FindChildIndex( Actor& actor );
554 Button( const Button& );
557 Button& operator = ( const Button& );
562 Toolkit::Button::ButtonSignalType mPressedSignal; ///< Signal emitted when the button is pressed.
563 Toolkit::Button::ButtonSignalType mReleasedSignal; ///< Signal emitted when the button is released.
564 Toolkit::Button::ButtonSignalType mClickedSignal; ///< Signal emitted when the button is clicked.
565 Toolkit::Button::ButtonSignalType mStateChangedSignal; ///< Signal emitted when the button's state is changed.
567 Timer mAutoRepeatingTimer; ///< Timer used to implement the autorepeating property.
569 Actor mLabel; ///< Stores the button label.
571 Actor mButtonContent; ///< Stores the unselected content.
572 Actor mSelectedContent; ///< Stores the selected content.
573 Actor mBackgroundContent; ///< Stores the background content.
574 Actor mSelectedBackgroundContent; ///< Stores the selected background content.
575 Actor mDisabledContent; ///< Stores the disabled content.
576 Actor mDisabledSelectedContent; ///< Stores the disabled selected content.
577 Actor mDisabledBackgroundContent; ///< Stores the disabled background content.
579 TapGestureDetector mTapDetector;
581 bool mDisabled; ///< Stores the disabled property.
582 bool mAutoRepeating; ///< Stores the autorepeating property.
583 bool mTogglableButton; ///< Stores the togglable property.
584 bool mSelected; ///< Stores the selected state.
585 float mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
586 float mNextAutoRepeatingDelay; ///< Stores the next autorepeating delay in seconds.
587 float mAnimationTime; ///< The animation time.
590 bool mClickActionPerforming;
592 ButtonState mState; ///< Stores the button state.
593 PaintState mPaintState; ///< Stores the paint state.
596 } // namespace Internal
598 // Helpers for public-api forwarding methods
600 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
602 DALI_ASSERT_ALWAYS( button );
604 Dali::RefObject& handle = button.GetImplementation();
606 return static_cast<Toolkit::Internal::Button&>( handle );
609 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
611 DALI_ASSERT_ALWAYS( button );
613 const Dali::RefObject& handle = button.GetImplementation();
615 return static_cast<const Toolkit::Internal::Button&>( handle );
618 } // namespace Toolkit
622 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__