d9d4bd6ea4828e827dc81ace45587f467a9d1680
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / button-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_BUTTON_H__
2 #define __DALI_TOOLKIT_INTERNAL_BUTTON_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/adaptor-framework/timer.h>
23 #include <dali/public-api/animation/animation.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/controls/buttons/button.h>
27 #include <dali-toolkit/public-api/controls/control-impl.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 class Button;
36
37 namespace Internal
38 {
39
40 /**
41  * Button is the base class implementation for all buttons.
42  */
43 class Button : public Control
44 {
45
46 protected:
47
48   /**
49    * Construct a new Button.
50    */
51   Button();
52
53   /**
54    * A reference counted object may only be deleted by calling Unreference()
55    */
56   virtual ~Button();
57
58 public:
59
60   /**
61    * @copydoc Dali::Toolkit::Button::SetDisabled( bool disabled )
62    */
63   void SetDisabled( bool disabled );
64
65   /**
66    * @copydoc Dali::Toolkit::Button::IsDisabled() const
67    */
68   bool IsDisabled() const;
69
70   /**
71    * @copydoc Dali::Toolkit::Button::SetAutoRepeating( bool autoRepeating )
72    */
73   void SetAutoRepeating( bool autoRepeating );
74
75   /**
76    * @copydoc Dali::Toolkit::Button::IsAutoRepeating() const
77    */
78   bool IsAutoRepeating() const;
79
80   /**
81    * @copydoc Dali::Toolkit::Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
82    */
83   void SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay );
84
85   /**
86    * @copydoc Dali::Toolkit::Button::GetInitialAutoRepeatingDelay() const
87    */
88   float GetInitialAutoRepeatingDelay() const;
89
90   /**
91    * @copydoc Dali::Toolkit::Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
92    */
93   void SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay );
94
95   /**
96    * @copydoc Dali::Toolkit::Button::GetNextAutoRepeatingDelay() const
97    */
98   float GetNextAutoRepeatingDelay() const;
99
100   /**
101    * @copydoc Dali::Toolkit::Button::SetTogglableButton( bool togglable )
102    */
103   void SetTogglableButton( bool togglable );
104
105   /**
106    * @copydoc Dali::Toolkit::Button::IsTogglableButton() const
107    */
108   bool IsTogglableButton() const;
109
110   /**
111    * @copydoc Dali::Toolkit::Button::SetSelected( bool selected )
112    */
113   void SetSelected( bool selected );
114
115   /**
116    * @copydoc Dali::Toolkit::Button::IsSelected() const
117    */
118   bool IsSelected() const;
119
120   /**
121    * @copydoc Dali::Toolkit::Button::SetAnimationTime()
122    */
123   void SetAnimationTime( float animationTime );
124
125   /**
126    * @copydoc Dali::Toolkit::Button::GetAnimationTime()
127    */
128   float GetAnimationTime() const;
129
130   /**
131    * @copydoc Dali::Toolkit::Button::SetLabelText
132    */
133   void SetLabelText( const std::string& label );
134
135   /**
136    * @copydoc Dali::Toolkit::Button::GetLabelText
137    */
138   std::string GetLabelText() const;
139
140   /**
141    * @copydoc Dali::Toolkit::PushButton::SetUnselectedImage
142    */
143   void SetUnselectedImage( const std::string& filename );
144
145   /**
146    * @copydoc Dali::Toolkit::PushButton::SetSelectedImage
147    */
148   void SetSelectedImage( const std::string& filename );
149
150   /**
151    * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage
152    */
153   void SetBackgroundImage( const std::string& filename );
154
155   /**
156    * @copydoc Dali::Toolkit::PushButton::SetSelectedBackgroundImage
157    */
158   void SetSelectedBackgroundImage( const std::string& filename );
159
160   /**
161    * @copydoc Dali::Toolkit::PushButton::SetDisabledImage
162    */
163   void SetDisabledImage( const std::string& filename );
164
165   /**
166    * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage
167    */
168   void SetDisabledSelectedImage( const std::string& filename );
169
170   /**
171    * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage
172    */
173   void SetDisabledBackgroundImage( const std::string& filename );
174
175   /**
176    * @return The filename used for the button image.
177    */
178   std::string GetUnselectedImageFilename() const;
179
180   /**
181    * @return The filename used for the selected image.
182    */
183   std::string GetSelectedImageFilename() const;
184
185   /**
186    * @return The filename used for the background image.
187    */
188   std::string GetBackgroundImageFilename() const;
189
190   /**
191    * @return The filename used for the selected background image.
192    */
193   std::string GetSelectedBackgroundImageFilename() const;
194
195   /**
196    * @return The filename used for the disabled button image.
197    */
198   std::string GetDisabledImageFilename() const;
199
200   /**
201    * @return The filename used for the disabled selected image.
202    */
203   std::string GetDisabledSelectedImageFilename() const;
204
205   /**
206    * @return The filename used for the disabled background image.
207    */
208   std::string GetDisabledBackgroundImageFilename() const;
209
210   /**
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
216    */
217   static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
218
219 public: // Deprecated API
220
221   /**
222    * @copydoc Dali::Toolkit::Button::SetLabel( Actor label )
223    */
224   void SetLabel( Actor label );
225
226   /**
227    * @deprecated Sets the unselected image with an Actor.
228    * @param[in] image The Actor to use.
229    */
230   void SetButtonImage( Actor image );
231
232   /**
233    * @deprecated Sets the selected image with an Actor.
234    * @param[in] image The Actor to use.
235    */
236   void SetSelectedImage( Actor image );
237
238   /**
239    * @deprecated Sets the background image with an Actor.
240    * @param[in] image The Actor to use.
241    */
242   void SetBackgroundImage( Actor image );
243
244   /**
245    * @deprecated Sets the selected background image with an Actor.
246    * @param[in] image The Actor to use.
247    */
248   void SetSelectedBackgroundImage( Actor image );
249
250   /**
251    * @deprecated Sets the disabled image with an Actor.
252    * @param[in] image The Actor to use.
253    */
254   void SetDisabledImage( Actor image );
255
256   /**
257    * @deprecated Sets the disabled selected image with an Actor.
258    * @param[in] image The Actor to use.
259    */
260   void SetDisabledSelectedImage( Actor image );
261
262   /**
263    * @deprecated Sets the disabled background image with an Actor.
264    * @param[in] image The Actor to use.
265    */
266   void SetDisabledBackgroundImage( Actor image );
267
268   /**
269    * @copydoc Dali::Toolkit::Button::GetButtonImage()
270    */
271   Actor GetButtonImage() const;
272
273   /**
274    * @copydoc Dali::Toolkit::Button::GetSelectedImage()
275    */
276   Actor GetSelectedImage() const;
277
278 protected:
279
280   /**
281    * @return A reference to the label actor.
282    */
283   Actor& GetLabelActor();
284
285   /**
286    * @return A reference to the unselected button image.
287    */
288   Actor& GetUnselectedImage();
289
290   /**
291    * @return A reference to the selected image.
292    */
293   Actor& GetSelectedImage();
294
295   /**
296    * @return A reference to the background image.
297    */
298   Actor& GetBackgroundImage();
299
300   /**
301    * @return A reference to the selected background image.
302    */
303   Actor& GetSelectedBackgroundImage();
304
305   /**
306    * @return A reference to the disabled button image.
307    */
308   Actor& GetDisabledImage();
309
310   /**
311    * @return A reference to the disabled selected image.
312    */
313   Actor& GetDisabledSelectedImage();
314
315   /**
316    * @return A reference to the disabled background image.
317    */
318   Actor& GetDisabledBackgroundImage();
319
320 private:
321
322   /**
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.
326    */
327   bool DoClickAction( const Property::Map& attributes );
328
329   /**
330    * This method is called after the button initialization.
331    * Could be reimplemented in subclasses to provide specific behaviour.
332    */
333   virtual void OnButtonInitialize() { }
334
335   /**
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.
338    */
339   virtual void OnLabelSet( bool noPadding ) {}
340
341   /**
342    * This method is called when the unselected button image is set
343    */
344   virtual void OnUnselectedImageSet() {}
345
346   /**
347    * This method is called when the selected image is set
348    */
349   virtual void OnSelectedImageSet() {}
350
351   /**
352    * This method is called when the background image is set
353    */
354   virtual void OnBackgroundImageSet() {}
355
356   /**
357    * This method is called when the selected background image is set
358    */
359   virtual void OnSelectedBackgroundImageSet() {}
360
361   /**
362    * This method is called when the disabled button image is set
363    */
364   virtual void OnDisabledImageSet() {}
365
366   /**
367    * This method is called when the disabled selected image is set
368    */
369   virtual void OnDisabledSelectedImageSet() {}
370
371   /**
372    * This method is called when the disabled background image is set
373    */
374   virtual void OnDisabledBackgroundImageSet() {}
375
376   /**
377    * This method is called from the OnTouchEvent method when the button is down.
378    * Could be reimplemented in subclasses to provide specific behaviour.
379    */
380   virtual void OnButtonDown();
381
382   /**
383    * This method is called from the OnTouchEvent method when the button is up.
384    * Could be reimplemented in subclasses to provide specific behaviour.
385    */
386   virtual void OnButtonUp();
387
388   /**
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.
392    */
393   virtual void OnTouchPointLeave();
394
395   /**
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.
398    */
399   virtual void OnTouchPointInterrupted();
400
401   /**
402    * This method is called when the button is removed from the stage.
403    * Could be reimplemented in subclasses to provide specific behaviour.
404    */
405   virtual void OnButtonStageDisconnection();
406
407   /**
408    * This method is called when the \e selected property is changed.
409    */
410   virtual void OnSelected() {}
411
412   /**
413    * This method is called when the \e disabled property is changed.
414    */
415   virtual void OnDisabled() {}
416
417   /**
418    * This method is called when the button is pressed.
419    */
420   virtual void OnPressed() {}
421
422   /**
423    * This method is called when the button is released.
424    */
425   virtual void OnReleased() {}
426
427 public:
428
429   /**
430    * @copydoc Dali::Toolkit::PushButton::PressedSignal()
431    */
432   Toolkit::Button::ButtonSignalType& PressedSignal();
433
434   /**
435    * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
436    */
437   Toolkit::Button::ButtonSignalType& ReleasedSignal();
438
439   /**
440    * @copydoc Dali::Toolkit::Button::ClickedSignal()
441    */
442   Toolkit::Button::ButtonSignalType& ClickedSignal();
443
444   /**
445    * @copydoc Dali::Toolkit::Button::StateChangedSignal()
446    */
447   Toolkit::Button::ButtonSignalType& StateChangedSignal();
448
449   /**
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.
457    */
458   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
459
460   // Properties
461
462   /**
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.
467    */
468   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
469
470   /**
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.
475    */
476   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
477
478 protected: // From CustomActorImpl
479
480   /**
481    * @copydoc Dali::CustomActorImpl::OnTouchEvent( const TouchEvent& event )
482    */
483   virtual bool OnTouchEvent( const TouchEvent& event );
484
485 private: // From Control
486
487   /**
488    * @copydoc Toolkit::Control::OnInitialize()
489    */
490   virtual void OnInitialize();
491
492   /**
493    * @copydoc Toolkit::Control::OnAccessibilityActivated()
494    */
495   virtual bool OnAccessibilityActivated();
496
497   /**
498    * @copydoc Toolkit::Control::OnKeyboardEnter()
499    */
500   virtual bool OnKeyboardEnter();
501
502   /**
503    * Callback received when the button is disconnected from the stage.
504    * It resets the button status.
505    */
506   void OnControlStageDisconnection();
507
508 private:
509
510   /**
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.
517    */
518   void OnTap(Actor actor, const TapGesture& tap);
519
520   /**
521    * Sets up the autorepeating timer.
522    * @param[in] delay The delay time in seconds.
523    */
524   void SetUpTimer( float delay );
525
526   /**
527    * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
528    */
529   bool AutoRepeatingSlot();
530
531   /**
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.
535    */
536   void SetSelected( bool selected, bool emitSignal );
537
538   /**
539    * This method is called when the button is pressed.
540    */
541   void Pressed();
542
543   /**
544    * This method is called when the button is released.
545    */
546   void Released();
547
548   /**
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.
553    */
554   void SetupContent( Actor& actorToModify, Actor newActor );
555
556   /**
557    * Sets the color of the unselected image.
558    * If no image exists, it is created.
559    * @param[in]  color The color to use.
560    */
561   void SetUnselectedColor( const Vector4& color );
562
563   /**
564    * Gets the unselected content color.
565    * @return     The currently used unselected color.
566    */
567   const Vector4 GetUnselectedColor() const;
568
569   /**
570    * Sets the color of the selected image.
571    * If no image exists, it is created.
572    * @param[in]  color The color to use.
573    */
574   void SetSelectedColor( const Vector4& color );
575
576   /**
577    * Gets the selected content color.
578    * @return     The currently used selected color.
579    */
580   const Vector4 GetSelectedColor() const;
581
582 protected:
583
584   enum ButtonState
585   {
586     ButtonUp,                                  ///< The button is up.
587     ButtonDown,                                ///< The button is down.
588   };
589
590   /**
591    * Button paint states.
592    */
593   enum PaintState
594   {
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.
599   };
600
601   /**
602    * Enum to specify which decoration when getting and setting decorations.
603    */
604   enum DecorationState
605   {
606     UNSELECTED_DECORATION = 0,
607     SELECTED_DECORATION,
608     DECORATION_STATES
609   };
610
611   ButtonState GetState();
612   PaintState GetPaintState();
613   void SetDecoration( DecorationState state, Actor actor );
614   Actor& GetDecoration( DecorationState state );
615
616
617   /**
618    * Returns the animation to be used for transitioning creating the animation if needed.
619    * @return The initialised transition animation.
620    */
621   Dali::Animation GetTransitionAnimation();
622
623   /**
624    * Prepares the actor to be transitioned in.
625    * @param[in]  actor  The actor that will be transitioned in.
626    */
627   virtual void PrepareForTranstionIn( Actor actor ) {}
628
629   /**
630    * Prepares the actor to be transitioned in.
631    * @param[in]  actor  The actor that will be transitioned out.
632    */
633   virtual void PrepareForTranstionOut( Actor actor ) {}
634
635   /**
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.
640    */
641   virtual void OnTransitionIn( Actor actor ) {}
642
643   /**
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.
648    */
649   virtual void OnTransitionOut( Actor actor ) {}
650
651 private:
652
653   /**
654    * Starts the transition animation.
655    * Button::TransitionFinished is called when the animation finishes.
656    */
657   void StartTransitionAnimation();
658
659   /**
660    * This method stops all transition animations
661    */
662   void StopTransitionAnimation();
663
664   /**
665    * Called when the transition animation finishes.
666    */
667   void TransitionAnimationFinished( Dali::Animation& source );
668
669   /**
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.
673    */
674   void ResetImageLayers();
675
676   /**
677    * Transitions out the actor
678    */
679   void TransitionOut( Actor actor );
680
681   /**
682    * Removes the actor from the button and prepares it to be transitioned out
683    */
684   void RemoveButtonImage( Actor& actor );
685
686   /**
687    * Finds the index of the actor.
688    * If the actor doesn't exist, return the last index + 1.
689    */
690   unsigned int FindChildIndex( Actor& actor );
691
692   /**
693    * Adds an actor to the hierarchy and prepares it to be transitioned.
694    * @param[in] actor The actor to add
695    */
696   void PrepareAddButtonImage( Actor& actor );
697
698   /**
699    * Adds an actor to the hierarchy and marks it to be transitioned.
700    * @param[in] actor The actor to add
701    */
702   void TransitionButtonImage( Actor& actor );
703
704   /**
705    * Adds an actor to the hierarchy.
706    * @param[in] actor The actor to add
707    */
708   void AddButtonImage( Actor& actor );
709
710   /**
711    * (Re)Adds the label (if exists) to the hierarchy (so it is always on top).
712    */
713   void ReAddLabel();
714
715   // Undefined
716   Button( const Button& );
717
718   // Undefined
719   Button& operator = ( const Button& );
720
721 private:
722
723   // Signals
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.
728
729   Timer mAutoRepeatingTimer;                   ///< Timer used to implement the autorepeating property.
730
731   Actor mLabel;                                ///< Stores the button label.
732
733   Actor mDecoration[ DECORATION_STATES ];      ///< Stores the decorations for both selected and unselected states.
734
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.
742
743   Animation        mTransitionAnimation;       ///< Animation used in the state transitions.
744
745   TapGestureDetector mTapDetector;
746
747   Vector4          mUnselectedColor;           ///< Color to use for unselected content.
748   Vector4          mSelectedColor;             ///< Color to use for selected content.
749
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.
756
757   float            mAnimationTime;             ///< The animation time.
758
759   // Actions
760   bool             mClickActionPerforming;
761
762   ButtonState      mState;                     ///< Stores the button state.
763   PaintState       mPaintState;                ///< Stores the paint state.
764 };
765
766 } // namespace Internal
767
768 // Helpers for public-api forwarding methods
769
770 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
771 {
772   DALI_ASSERT_ALWAYS( button );
773
774   Dali::RefObject& handle = button.GetImplementation();
775
776   return static_cast<Toolkit::Internal::Button&>( handle );
777 }
778
779 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
780 {
781   DALI_ASSERT_ALWAYS( button );
782
783   const Dali::RefObject& handle = button.GetImplementation();
784
785   return static_cast<const Toolkit::Internal::Button&>( handle );
786 }
787
788 } // namespace Toolkit
789
790 } // namespace Dali
791
792 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__