Merge "Multi-line layout." into 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 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    */
338   virtual void OnLabelSet() {}
339
340   /**
341    * This method is called when the unselected button image is set
342    */
343   virtual void OnUnselectedImageSet() {}
344
345   /**
346    * This method is called when the selected image is set
347    */
348   virtual void OnSelectedImageSet() {}
349
350   /**
351    * This method is called when the background image is set
352    */
353   virtual void OnBackgroundImageSet() {}
354
355   /**
356    * This method is called when the selected background image is set
357    */
358   virtual void OnSelectedBackgroundImageSet() {}
359
360   /**
361    * This method is called when the disabled button image is set
362    */
363   virtual void OnDisabledImageSet() {}
364
365   /**
366    * This method is called when the disabled selected image is set
367    */
368   virtual void OnDisabledSelectedImageSet() {}
369
370   /**
371    * This method is called when the disabled background image is set
372    */
373   virtual void OnDisabledBackgroundImageSet() {}
374
375   /**
376    * This method is called from the OnTouchEvent method when the button is down.
377    * Could be reimplemented in subclasses to provide specific behaviour.
378    */
379   virtual void OnButtonDown();
380
381   /**
382    * This method is called from the OnTouchEvent method when the button is up.
383    * Could be reimplemented in subclasses to provide specific behaviour.
384    */
385   virtual void OnButtonUp();
386
387   /**
388    * This method is called from the OnTouchEvent method when the touch point leaves the boundary of the button or
389    * more than one touch points are received.
390    * Could be reimplemented in subclasses to provide specific behaviour.
391    */
392   virtual void OnTouchPointLeave();
393
394   /**
395    * This method is called from the OnTouchEvent method when the touch point is interrupted.
396    * Could be reimplemented in subclasses to provide specific behaviour.
397    */
398   virtual void OnTouchPointInterrupted();
399
400   /**
401    * This method is called when the button is removed from the stage.
402    * Could be reimplemented in subclasses to provide specific behaviour.
403    */
404   virtual void OnButtonStageDisconnection();
405
406   /**
407    * This method is called when the \e selected property is changed.
408    */
409   virtual void OnSelected() {}
410
411   /**
412    * This method is called when the \e disabled property is changed.
413    */
414   virtual void OnDisabled() {}
415
416   /**
417    * This method is called when the button is pressed.
418    */
419   virtual void OnPressed() {}
420
421   /**
422    * This method is called when the button is released.
423    */
424   virtual void OnReleased() {}
425
426 public:
427
428   /**
429    * @copydoc Dali::Toolkit::PushButton::PressedSignal()
430    */
431   Toolkit::Button::ButtonSignalType& PressedSignal();
432
433   /**
434    * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
435    */
436   Toolkit::Button::ButtonSignalType& ReleasedSignal();
437
438   /**
439    * @copydoc Dali::Toolkit::Button::ClickedSignal()
440    */
441   Toolkit::Button::ButtonSignalType& ClickedSignal();
442
443   /**
444    * @copydoc Dali::Toolkit::Button::StateChangedSignal()
445    */
446   Toolkit::Button::ButtonSignalType& StateChangedSignal();
447
448   /**
449    * Connects a callback function with the object's signals.
450    * @param[in] object The object providing the signal.
451    * @param[in] tracker Used to disconnect the signal.
452    * @param[in] signalName The signal to connect to.
453    * @param[in] functor A newly allocated FunctorDelegate.
454    * @return True if the signal was connected.
455    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
456    */
457   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
458
459   // Properties
460
461   /**
462    * Called when a property of an object of this type is set.
463    * @param[in] object The object whose property is set.
464    * @param[in] index The property index.
465    * @param[in] value The new property value.
466    */
467   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
468
469   /**
470    * Called to retrieve a property of an object of this type.
471    * @param[in] object The object whose property is to be retrieved.
472    * @param[in] index The property index.
473    * @return The current value of the property.
474    */
475   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
476
477 protected: // From CustomActorImpl
478
479   /**
480    * @copydoc Dali::CustomActorImpl::OnTouchEvent( const TouchEvent& event )
481    */
482   virtual bool OnTouchEvent( const TouchEvent& event );
483
484 private: // From Control
485
486   /**
487    * @copydoc Toolkit::Control::OnInitialize()
488    */
489   virtual void OnInitialize();
490
491   /**
492    * @copydoc Toolkit::Control::OnAccessibilityActivated()
493    */
494   virtual bool OnAccessibilityActivated();
495
496   /**
497    * @copydoc Toolkit::Control::OnKeyboardEnter()
498    */
499   virtual bool OnKeyboardEnter();
500
501   /**
502    * Callback received when the button is disconnected from the stage.
503    * It resets the button status.
504    */
505   void OnControlStageDisconnection();
506
507 private:
508
509   /**
510    * Handler for tap events.
511    * We do not actually do anything when we receive a tap as the button handles tap event through
512    * the touch event system itself as it requires more than just tap handling (e.g. leave events).
513    * This stops any of our parents receiving a tap gesture when it occurs within our area.
514    * @param[in]  actor  The tapped actor.
515    * @param[in]  tap    The tap gesture.
516    */
517   void OnTap(Actor actor, const TapGesture& tap);
518
519   /**
520    * Sets up the autorepeating timer.
521    * @param[in] delay The delay time in seconds.
522    */
523   void SetUpTimer( float delay );
524
525   /**
526    * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
527    */
528   bool AutoRepeatingSlot();
529
530   /**
531    * Sets the button as selected or unselected.
532    * @param[in] selected \e selected property value.
533    * @param[in] emitSignal Emit a signal if this value is \e true.
534    */
535   void SetSelected( bool selected, bool emitSignal );
536
537   /**
538    * This method is called when the button is pressed.
539    */
540   void Pressed();
541
542   /**
543    * This method is called when the button is released.
544    */
545   void Released();
546
547   /**
548    * Used to perform common setup applied to images within button.
549    * This will replace the current image with the specifed one.
550    * @param[in]  actorToModify The image to replace.
551    * @param[out] newActor The new image to use.
552    */
553   void SetupContent( Actor& actorToModify, Actor newActor );
554
555   /**
556    * Sets the color of the unselected image.
557    * If no image exists, it is created.
558    * @param[in]  color The color to use.
559    */
560   void SetUnselectedColor( const Vector4& color );
561
562   /**
563    * Gets the unselected content color.
564    * @return     The currently used unselected color.
565    */
566   const Vector4 GetUnselectedColor() const;
567
568   /**
569    * Sets the color of the selected image.
570    * If no image exists, it is created.
571    * @param[in]  color The color to use.
572    */
573   void SetSelectedColor( const Vector4& color );
574
575   /**
576    * Gets the selected content color.
577    * @return     The currently used selected color.
578    */
579   const Vector4 GetSelectedColor() const;
580
581 protected:
582
583   enum ButtonState
584   {
585     ButtonUp,                                  ///< The button is up.
586     ButtonDown,                                ///< The button is down.
587   };
588
589   /**
590    * Button paint states.
591    */
592   enum PaintState
593   {
594     UnselectedState,              ///< The button is unselected.
595     SelectedState,                ///< The button is selected.
596     DisabledUnselectedState,      ///< The button is disabled and unselected.
597     DisabledSelectedState,        ///< The button is disabled and selected.
598   };
599
600   /**
601    * Enum to specify which decoration when getting and setting decorations.
602    */
603   enum DecorationState
604   {
605     UNSELECTED_DECORATION = 0,
606     SELECTED_DECORATION,
607     DECORATION_STATES
608   };
609
610   ButtonState GetState();
611   PaintState GetPaintState();
612   void SetDecoration( DecorationState state, Actor actor );
613   Actor& GetDecoration( DecorationState state );
614
615
616   /**
617    * Returns the animation to be used for transitioning creating the animation if needed.
618    * @return The initialised transition animation.
619    */
620   Dali::Animation GetTransitionAnimation();
621
622   /**
623    * Prepares the actor to be transitioned in.
624    * @param[in]  actor  The actor that will be transitioned in.
625    */
626   virtual void PrepareForTranstionIn( Actor actor ) {}
627
628   /**
629    * Prepares the actor to be transitioned in.
630    * @param[in]  actor  The actor that will be transitioned out.
631    */
632   virtual void PrepareForTranstionOut( Actor actor ) {}
633
634   /**
635    * Transitions the actor in, allowing derived classes to configure
636    * the GetTransitionAnimation() animation ready.
637    * Button is in charge of calling Dali::Animation::Play and so derived classes
638    * only need to add the animation.
639    */
640   virtual void OnTransitionIn( Actor actor ) {}
641
642   /**
643    * Transitions the actor out, allowing derived classes to configure
644    * the GetTransitionAnimation() animation ready.
645    * Button is in charge of calling Dali::Animation::Play and so derived classes
646    * only need to add the animation.
647    */
648   virtual void OnTransitionOut( Actor actor ) {}
649
650 private:
651
652   /**
653    * Starts the transition animation.
654    * Button::TransitionFinished is called when the animation finishes.
655    */
656   void StartTransitionAnimation();
657
658   /**
659    * This method stops all transition animations
660    */
661   void StopTransitionAnimation();
662
663   /**
664    * Called when the transition animation finishes.
665    */
666   void TransitionAnimationFinished( Dali::Animation& source );
667
668   /**
669    * Resets the Button to the base state for the current paint state.
670    * Any additionally inserted images needed for transitions that are
671    * no longer needed and the removed.
672    */
673   void ResetImageLayers();
674
675   /**
676    * Transitions out the actor
677    */
678   void TransitionOut( Actor actor );
679
680   /**
681    * Removes the actor from the button and prepares it to be transitioned out
682    */
683   void RemoveButtonImage( Actor& actor );
684
685   /**
686    * Finds the index of the actor.
687    * If the actor doesn't exist, return the last index + 1.
688    */
689   unsigned int FindChildIndex( Actor& actor );
690
691   /**
692    * Adds an actor to the hierarchy and prepares it to be transitioned.
693    * @param[in] actor The actor to add
694    */
695   void PrepareAddButtonImage( Actor& actor );
696
697   /**
698    * Adds an actor to the hierarchy and marks it to be transitioned.
699    * @param[in] actor The actor to add
700    */
701   void TransitionButtonImage( Actor& actor );
702
703   /**
704    * Adds an actor to the hierarchy.
705    * @param[in] actor The actor to add
706    */
707   void AddButtonImage( Actor& actor );
708
709   /**
710    * (Re)Adds the label (if exists) to the hierarchy (so it is always on top).
711    */
712   void ReAddLabel();
713
714   // Undefined
715   Button( const Button& );
716
717   // Undefined
718   Button& operator = ( const Button& );
719
720 private:
721
722   // Signals
723   Toolkit::Button::ButtonSignalType mPressedSignal;           ///< Signal emitted when the button is pressed.
724   Toolkit::Button::ButtonSignalType mReleasedSignal;          ///< Signal emitted when the button is released.
725   Toolkit::Button::ButtonSignalType mClickedSignal;           ///< Signal emitted when the button is clicked.
726   Toolkit::Button::ButtonSignalType mStateChangedSignal;      ///< Signal emitted when the button's state is changed.
727
728   Timer mAutoRepeatingTimer;                   ///< Timer used to implement the autorepeating property.
729
730   Actor mLabel;                                ///< Stores the button label.
731
732   Actor mDecoration[ DECORATION_STATES ];      ///< Stores the decorations for both selected and unselected states.
733
734   Actor mUnselectedContent;                    ///< Stores the unselected content.
735   Actor mSelectedContent;                      ///< Stores the selected content.
736   Actor mBackgroundContent;                    ///< Stores the background content.
737   Actor mSelectedBackgroundContent;            ///< Stores the selected background content.
738   Actor mDisabledContent;                      ///< Stores the disabled content.
739   Actor mDisabledSelectedContent;              ///< Stores the disabled selected content.
740   Actor mDisabledBackgroundContent;            ///< Stores the disabled background content.
741
742   Animation        mTransitionAnimation;       ///< Animation used in the state transitions.
743
744   TapGestureDetector mTapDetector;
745
746   Vector4          mUnselectedColor;           ///< Color to use for unselected content.
747   Vector4          mSelectedColor;             ///< Color to use for selected content.
748
749   bool             mDisabled;                  ///< Stores the disabled property.
750   bool             mAutoRepeating;             ///< Stores the autorepeating property.
751   bool             mTogglableButton;           ///< Stores the togglable property.
752   bool             mSelected;                  ///< Stores the selected state.
753   float            mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
754   float            mNextAutoRepeatingDelay;    ///< Stores the next autorepeating delay in seconds.
755
756   float            mAnimationTime;             ///< The animation time.
757
758   // Actions
759   bool             mClickActionPerforming;
760
761   ButtonState      mState;                     ///< Stores the button state.
762   PaintState       mPaintState;                ///< Stores the paint state.
763 };
764
765 } // namespace Internal
766
767 // Helpers for public-api forwarding methods
768
769 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
770 {
771   DALI_ASSERT_ALWAYS( button );
772
773   Dali::RefObject& handle = button.GetImplementation();
774
775   return static_cast<Toolkit::Internal::Button&>( handle );
776 }
777
778 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
779 {
780   DALI_ASSERT_ALWAYS( button );
781
782   const Dali::RefObject& handle = button.GetImplementation();
783
784   return static_cast<const Toolkit::Internal::Button&>( handle );
785 }
786
787 } // namespace Toolkit
788
789 } // namespace Dali
790
791 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__