[dali_1.1.43] Merge branch 'devel/master'
[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 public:
47
48   /**
49    * @copydoc Dali::Toolkit::Button::SetDisabled
50    */
51   void SetDisabled( bool disabled );
52
53   /**
54    * @copydoc Dali::Toolkit::Button::IsDisabled
55    */
56   bool IsDisabled() const;
57
58   /**
59    * @copydoc Dali::Toolkit::Button::SetAutoRepeating
60    */
61   void SetAutoRepeating( bool autoRepeating );
62
63   /**
64    * @copydoc Dali::Toolkit::Button::IsAutoRepeating
65    */
66   bool IsAutoRepeating() const;
67
68   /**
69    * @copydoc Dali::Toolkit::Button::SetInitialAutoRepeatingDelay
70    */
71   void SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay );
72
73   /**
74    * @copydoc Dali::Toolkit::Button::GetInitialAutoRepeatingDelay
75    */
76   float GetInitialAutoRepeatingDelay() const;
77
78   /**
79    * @copydoc Dali::Toolkit::Button::SetNextAutoRepeatingDelay
80    */
81   void SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay );
82
83   /**
84    * @copydoc Dali::Toolkit::Button::GetNextAutoRepeatingDelay
85    */
86   float GetNextAutoRepeatingDelay() const;
87
88   /**
89    * @copydoc Dali::Toolkit::Button::SetTogglableButton
90    */
91   void SetTogglableButton( bool togglable );
92
93   /**
94    * @copydoc Dali::Toolkit::Button::IsTogglableButton
95    */
96   bool IsTogglableButton() const;
97
98   /**
99    * @copydoc Dali::Toolkit::Button::SetSelected
100    */
101   void SetSelected( bool selected );
102
103   /**
104    * @copydoc Dali::Toolkit::Button::IsSelected
105    */
106   bool IsSelected() const;
107
108   /**
109    * @copydoc Dali::Toolkit::Button::SetAnimationTime
110    */
111   void SetAnimationTime( float animationTime );
112
113   /**
114    * @copydoc Dali::Toolkit::Button::GetAnimationTime
115    */
116   float GetAnimationTime() const;
117
118   /**
119    * @copydoc Dali::Toolkit::Button::SetLabelText
120    */
121   void SetLabelText( const std::string& label );
122
123   /**
124    * @copydoc Dali::Toolkit::Button::GetLabelText
125    */
126   std::string GetLabelText() const;
127
128   /**
129    * @copydoc Dali::Toolkit::PushButton::SetUnselectedImage
130    */
131   void SetUnselectedImage( const std::string& filename );
132
133   /**
134    * @copydoc Dali::Toolkit::PushButton::SetSelectedImage
135    */
136   void SetSelectedImage( const std::string& filename );
137
138   /**
139    * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage
140    */
141   void SetBackgroundImage( const std::string& filename );
142
143   /**
144    * @copydoc Dali::Toolkit::PushButton::SetSelectedBackgroundImage
145    */
146   void SetSelectedBackgroundImage( const std::string& filename );
147
148   /**
149    * @copydoc Dali::Toolkit::PushButton::SetDisabledImage
150    */
151   void SetDisabledImage( const std::string& filename );
152
153   /**
154    * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage
155    */
156   void SetDisabledSelectedImage( const std::string& filename );
157
158   /**
159    * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage
160    */
161   void SetDisabledBackgroundImage( const std::string& filename );
162
163   /**
164    * @return The filename used for the button image.
165    */
166   std::string GetUnselectedImageFilename() const;
167
168   /**
169    * @return The filename used for the selected image.
170    */
171   std::string GetSelectedImageFilename() const;
172
173   /**
174    * @return The filename used for the background image.
175    */
176   std::string GetBackgroundImageFilename() const;
177
178   /**
179    * @return The filename used for the selected background image.
180    */
181   std::string GetSelectedBackgroundImageFilename() const;
182
183   /**
184    * @return The filename used for the disabled button image.
185    */
186   std::string GetDisabledImageFilename() const;
187
188   /**
189    * @return The filename used for the disabled selected image.
190    */
191   std::string GetDisabledSelectedImageFilename() const;
192
193   /**
194    * @return The filename used for the disabled background image.
195    */
196   std::string GetDisabledBackgroundImageFilename() const;
197
198   /**
199    * @brief Sets the specified properties on the button label.
200    * If the label does not exist yet, it is created.
201    * The derived buttons are notified if any properties are changed.
202    * @param[in] properties A Property::Map of key-value pairs of properties to set.
203    */
204   void ModifyLabel( const Property::Map& properties );
205
206   /**
207    * Performs actions as requested using the action name.
208    * @param[in] object The object on which to perform the action.
209    * @param[in] actionName The action to perform.
210    * @param[in] attributes The attributes with which to perfrom this action.
211    * @return true if action has been accepted by this control
212    */
213   static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
214
215 public: // Deprecated API
216
217   /**
218    * @copydoc Dali::Toolkit::Button::SetLabel( Actor label )
219    */
220   void SetLabel( Actor label );
221
222   /**
223    * @deprecated Sets the unselected image with an Actor.
224    * @param[in] image The Actor to use.
225    */
226   void SetButtonImage( Actor image );
227
228   /**
229    * @deprecated Sets the selected image with an Actor.
230    * @param[in] image The Actor to use.
231    */
232   void SetSelectedImage( Actor image );
233
234   /**
235    * @deprecated Sets the background image with an Actor.
236    * @param[in] image The Actor to use.
237    */
238   void SetBackgroundImage( Actor image );
239
240   /**
241    * @deprecated Sets the selected background image with an Actor.
242    * @param[in] image The Actor to use.
243    */
244   void SetSelectedBackgroundImage( Actor image );
245
246   /**
247    * @deprecated Sets the disabled image with an Actor.
248    * @param[in] image The Actor to use.
249    */
250   void SetDisabledImage( Actor image );
251
252   /**
253    * @deprecated Sets the disabled selected image with an Actor.
254    * @param[in] image The Actor to use.
255    */
256   void SetDisabledSelectedImage( Actor image );
257
258   /**
259    * @deprecated Sets the disabled background image with an Actor.
260    * @param[in] image The Actor to use.
261    */
262   void SetDisabledBackgroundImage( Actor image );
263
264   /**
265    * @copydoc Dali::Toolkit::Button::GetButtonImage
266    */
267   Actor GetButtonImage() const;
268
269   /**
270    * @copydoc Dali::Toolkit::Button::GetSelectedImage
271    */
272   Actor GetSelectedImage() const;
273
274 protected:
275
276   enum ButtonState
277   {
278     ButtonUp,                                  ///< The button is up.
279     ButtonDown,                                ///< The button is down.
280   };
281
282   /**
283    * Button paint states.
284    */
285   enum PaintState
286   {
287     UnselectedState,              ///< The button is unselected.
288     SelectedState,                ///< The button is selected.
289     DisabledUnselectedState,      ///< The button is disabled and unselected.
290     DisabledSelectedState,        ///< The button is disabled and selected.
291   };
292
293   /**
294    * Enum to specify which decoration when getting and setting decorations.
295    */
296   enum DecorationState
297   {
298     UNSELECTED_DECORATION = 0,
299     SELECTED_DECORATION,
300     DECORATION_STATES
301   };
302
303   /**
304    * Construct a new Button.
305    */
306   Button();
307
308   /**
309    * A reference counted object may only be deleted by calling Unreference()
310    */
311   virtual ~Button();
312   /**
313    * @return A reference to the label actor.
314    */
315   Actor& GetLabelActor();
316
317   /**
318    * @return A reference to the unselected button image.
319    */
320   Actor& GetUnselectedImage();
321
322   /**
323    * @return A reference to the selected image.
324    */
325   Actor& GetSelectedImage();
326
327   /**
328    * @return A reference to the background image.
329    */
330   Actor& GetBackgroundImage();
331
332   /**
333    * @return A reference to the selected background image.
334    */
335   Actor& GetSelectedBackgroundImage();
336
337   /**
338    * @return A reference to the disabled button image.
339    */
340   Actor& GetDisabledImage();
341
342   /**
343    * @return A reference to the disabled selected image.
344    */
345   Actor& GetDisabledSelectedImage();
346
347   /**
348    * @return A reference to the disabled background image.
349    */
350   Actor& GetDisabledBackgroundImage();
351
352 private:
353
354   /**
355    * Perform the click action to click the button.
356    * @param[in] attributes The attributes to perfrom this action.
357    * @return true if this control can perform action.
358    */
359   bool DoClickAction( const Property::Map& attributes );
360
361   /**
362    * This method is called when the label is set.
363    * @param[in] noPadding Used to bypass padding if the label is to be treated generically.
364    */
365   virtual void OnLabelSet( bool noPadding ) {}
366
367   /**
368    * This method is called when the unselected button image is set
369    */
370   virtual void OnUnselectedImageSet() {}
371
372   /**
373    * This method is called when the selected image is set
374    */
375   virtual void OnSelectedImageSet() {}
376
377   /**
378    * This method is called when the background image is set
379    */
380   virtual void OnBackgroundImageSet() {}
381
382   /**
383    * This method is called when the selected background image is set
384    */
385   virtual void OnSelectedBackgroundImageSet() {}
386
387   /**
388    * This method is called when the disabled button image is set
389    */
390   virtual void OnDisabledImageSet() {}
391
392   /**
393    * This method is called when the disabled selected image is set
394    */
395   virtual void OnDisabledSelectedImageSet() {}
396
397   /**
398    * This method is called when the disabled background image is set
399    */
400   virtual void OnDisabledBackgroundImageSet() {}
401
402   /**
403    * This method is called the button is down.
404    * Could be reimplemented in subclasses to provide specific behaviour.
405    */
406   virtual void OnButtonDown();
407
408   /**
409    * This method is called when the button is up.
410    * Could be reimplemented in subclasses to provide specific behaviour.
411    */
412   virtual void OnButtonUp();
413
414   /**
415    * This method is called when touch leaves the boundary of the button or several touch points are received.
416    * Could be reimplemented in subclasses to provide specific behaviour.
417    */
418   virtual void OnTouchPointLeave();
419
420   /**
421    * This method is called when the touch is interrupted.
422    * Could be reimplemented in subclasses to provide specific behaviour.
423    */
424   virtual void OnTouchPointInterrupted();
425
426   /**
427    * This method is called when the \e selected property is changed.
428    */
429   virtual void OnSelected() {}
430
431   /**
432    * This method is called when the \e disabled property is changed.
433    */
434   virtual void OnDisabled() {}
435
436   /**
437    * This method is called when the button is pressed.
438    */
439   virtual void OnPressed() {}
440
441   /**
442    * This method is called when the button is released.
443    */
444   virtual void OnReleased() {}
445
446 public:
447
448   /**
449    * @copydoc Dali::Toolkit::PushButton::PressedSignal()
450    */
451   Toolkit::Button::ButtonSignalType& PressedSignal();
452
453   /**
454    * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
455    */
456   Toolkit::Button::ButtonSignalType& ReleasedSignal();
457
458   /**
459    * @copydoc Dali::Toolkit::Button::ClickedSignal()
460    */
461   Toolkit::Button::ButtonSignalType& ClickedSignal();
462
463   /**
464    * @copydoc Dali::Toolkit::Button::StateChangedSignal()
465    */
466   Toolkit::Button::ButtonSignalType& StateChangedSignal();
467
468   /**
469    * Connects a callback function with the object's signals.
470    * @param[in] object The object providing the signal.
471    * @param[in] tracker Used to disconnect the signal.
472    * @param[in] signalName The signal to connect to.
473    * @param[in] functor A newly allocated FunctorDelegate.
474    * @return True if the signal was connected.
475    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
476    */
477   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
478
479   // Properties
480
481   /**
482    * Called when a property of an object of this type is set.
483    * @param[in] object The object whose property is set.
484    * @param[in] index The property index.
485    * @param[in] value The new property value.
486    */
487   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
488
489   /**
490    * Called to retrieve a property of an object of this type.
491    * @param[in] object The object whose property is to be retrieved.
492    * @param[in] index The property index.
493    * @return The current value of the property.
494    */
495   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
496
497 protected: // From Control
498
499   /**
500    * @copydoc Toolkit::Control::OnInitialize()
501    * @note If overridden by deriving button classes, then an up-call to Button::OnInitialize MUST be made at the start.
502    */
503   virtual void OnInitialize();
504
505   /**
506    * @copydoc Toolkit::Control::OnAccessibilityActivated()
507    */
508   virtual bool OnAccessibilityActivated();
509
510   /**
511    * @copydoc Toolkit::Control::OnKeyboardEnter()
512    */
513   virtual bool OnKeyboardEnter();
514
515   /**
516    * @copydoc Toolkit::Control::OnStageDisconnection()
517    * @note If overridden by deriving button classes, then an up-call to Button::OnStageDisconnection MUST be made at the end.
518    */
519   void OnStageDisconnection();
520
521
522 private:
523
524   /**
525    * @brief Handler for touch data
526    * @param[in]  actor  The touched actor.
527    * @param[in]  touch  The touch info.
528    * @return true, if consumed, false otherwise.
529    */
530   bool OnTouch( Actor actor, const TouchData& touch );
531
532   /**
533    * Handler for tap events.
534    * We do not actually do anything when we receive a tap as the button handles tap event through
535    * the touch event system itself as it requires more than just tap handling (e.g. leave events).
536    * This stops any of our parents receiving a tap gesture when it occurs within our area.
537    * @param[in]  actor  The tapped actor.
538    * @param[in]  tap    The tap gesture.
539    */
540   void OnTap(Actor actor, const TapGesture& tap);
541
542   /**
543    * Sets up the autorepeating timer.
544    * @param[in] delay The delay time in seconds.
545    */
546   void SetUpTimer( float delay );
547
548   /**
549    * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
550    */
551   bool AutoRepeatingSlot();
552
553   /**
554    * Sets the button as selected or unselected.
555    * @param[in] selected \e selected property value.
556    * @param[in] emitSignal Emit a signal if this value is \e true.
557    */
558   void SetSelected( bool selected, bool emitSignal );
559
560   /**
561    * This method is called when the button is pressed.
562    */
563   void Pressed();
564
565   /**
566    * This method is called when the button is released.
567    */
568   void Released();
569
570   /**
571    * Used to perform common setup applied to images within button.
572    * This will replace the current image with the specifed one.
573    * @param[in]  actorToModify The image to replace.
574    * @param[out] newActor The new image to use.
575    */
576   void SetupContent( Actor& actorToModify, Actor newActor );
577
578   /**
579    * Gets the unselected content color.
580    * @return     The currently used unselected color.
581    */
582   const Vector4 GetUnselectedColor() const;
583
584   /**
585    * Sets the color of button in selected or unselected state, if image also supplied this color will be appplied to it.
586    * If no visual exists, it is created.
587    * @param[in]  color The color to use.
588    * @param[in]  selectedState The state to apply the color to, SelectedState or DisabledUnselectedState.
589    */
590   void SetColor( const Vector4& color, PaintState selectedState );
591
592   /**
593    * Gets the selected content color.
594    * @return     The currently used selected color.
595    */
596   const Vector4 GetSelectedColor() const;
597
598 protected:
599
600   ButtonState GetState();
601   PaintState GetPaintState();
602   void SetDecoration( DecorationState state, Actor actor );
603   Actor& GetDecoration( DecorationState state );
604
605
606   /**
607    * Returns the animation to be used for transitioning creating the animation if needed.
608    * @return The initialised transition animation.
609    */
610   Dali::Animation GetTransitionAnimation();
611
612   /**
613    * Prepares the actor to be transitioned in.
614    * @param[in]  actor  The actor that will be transitioned in.
615    */
616   virtual void PrepareForTranstionIn( Actor actor ) {}
617
618   /**
619    * Prepares the actor to be transitioned in.
620    * @param[in]  actor  The actor that will be transitioned out.
621    */
622   virtual void PrepareForTranstionOut( Actor actor ) {}
623
624   /**
625    * Transitions the actor in, allowing derived classes to configure
626    * the GetTransitionAnimation() animation ready.
627    * Button is in charge of calling Dali::Animation::Play and so derived classes
628    * only need to add the animation.
629    */
630   virtual void OnTransitionIn( Actor actor ) {}
631
632   /**
633    * Transitions the actor out, allowing derived classes to configure
634    * the GetTransitionAnimation() animation ready.
635    * Button is in charge of calling Dali::Animation::Play and so derived classes
636    * only need to add the animation.
637    */
638   virtual void OnTransitionOut( Actor actor ) {}
639
640 private:
641
642   /**
643    * Starts the transition animation.
644    * Button::TransitionFinished is called when the animation finishes.
645    */
646   void StartTransitionAnimation();
647
648   /**
649    * This method stops all transition animations
650    */
651   void StopTransitionAnimation();
652
653   /**
654    * Called when the transition animation finishes.
655    */
656   void TransitionAnimationFinished( Dali::Animation& source );
657
658   /**
659    * Resets the Button to the base state for the current paint state.
660    * Any additionally inserted images needed for transitions that are
661    * no longer needed and the removed.
662    */
663   void ResetImageLayers();
664
665   /**
666    * Transitions out the actor
667    */
668   void TransitionOut( Actor actor );
669
670   /**
671    * Removes the actor from the button and prepares it to be transitioned out
672    */
673   void RemoveButtonImage( Actor& actor );
674
675   /**
676    * Finds the index of the actor.
677    * If the actor doesn't exist, return the last index + 1.
678    */
679   unsigned int FindChildIndex( Actor& actor );
680
681   /**
682    * Adds an actor to the hierarchy and prepares it to be transitioned.
683    * @param[in] actor The actor to add
684    */
685   void PrepareAddButtonImage( Actor& actor );
686
687   /**
688    * Adds an actor to the hierarchy and marks it to be transitioned.
689    * @param[in] actor The actor to add
690    */
691   void TransitionButtonImage( Actor& actor );
692
693   /**
694    * Adds an actor to the hierarchy.
695    * @param[in] actor The actor to add
696    */
697   void AddButtonImage( Actor& actor );
698
699   /**
700    * (Re)Adds the label (if exists) to the hierarchy (so it is always on top).
701    */
702   void ReAddLabel();
703
704   // Undefined
705   Button( const Button& );
706
707   // Undefined
708   Button& operator = ( const Button& );
709
710 private:
711
712   // Signals
713   Toolkit::Button::ButtonSignalType mPressedSignal;           ///< Signal emitted when the button is pressed.
714   Toolkit::Button::ButtonSignalType mReleasedSignal;          ///< Signal emitted when the button is released.
715   Toolkit::Button::ButtonSignalType mClickedSignal;           ///< Signal emitted when the button is clicked.
716   Toolkit::Button::ButtonSignalType mStateChangedSignal;      ///< Signal emitted when the button's state is changed.
717
718   Timer mAutoRepeatingTimer;                   ///< Timer used to implement the autorepeating property.
719
720   Actor mLabel;                                ///< Stores the button label.
721
722   Actor mDecoration[ DECORATION_STATES ];      ///< Stores the decorations for both selected and unselected states.
723
724   Actor mUnselectedContent;                    ///< Stores the unselected content.
725   Actor mSelectedContent;                      ///< Stores the selected content.
726   Actor mBackgroundContent;                    ///< Stores the background content.
727   Actor mSelectedBackgroundContent;            ///< Stores the selected background content.
728   Actor mDisabledContent;                      ///< Stores the disabled content.
729   Actor mDisabledSelectedContent;              ///< Stores the disabled selected content.
730   Actor mDisabledBackgroundContent;            ///< Stores the disabled background content.
731
732   Animation        mTransitionAnimation;       ///< Animation used in the state transitions.
733
734   TapGestureDetector mTapDetector;
735
736   Vector4          mUnselectedColor;           ///< Color to use for unselected content.
737   Vector4          mSelectedColor;             ///< Color to use for selected content.
738
739   bool             mDisabled;                  ///< Stores the disabled property.
740   bool             mAutoRepeating;             ///< Stores the autorepeating property.
741   bool             mTogglableButton;           ///< Stores the togglable property.
742   bool             mSelected;                  ///< Stores the selected state.
743   float            mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
744   float            mNextAutoRepeatingDelay;    ///< Stores the next autorepeating delay in seconds.
745
746   float            mAnimationTime;             ///< The animation time.
747
748   // Actions
749   bool             mClickActionPerforming;
750
751   ButtonState      mState;                     ///< Stores the button state.
752   PaintState       mPaintState;                ///< Stores the paint state.
753 };
754
755 } // namespace Internal
756
757 // Helpers for public-api forwarding methods
758
759 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
760 {
761   DALI_ASSERT_ALWAYS( button );
762
763   Dali::RefObject& handle = button.GetImplementation();
764
765   return static_cast<Toolkit::Internal::Button&>( handle );
766 }
767
768 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
769 {
770   DALI_ASSERT_ALWAYS( button );
771
772   const Dali::RefObject& handle = button.GetImplementation();
773
774   return static_cast<const Toolkit::Internal::Button&>( handle );
775 }
776
777 } // namespace Toolkit
778
779 } // namespace Dali
780
781 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__