e4d49d79fdf305bcbf3c3f8fd0c1e3b8f167225a
[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
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/controls/buttons/button.h>
26 #include <dali-toolkit/public-api/controls/control-impl.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 class Button;
35
36 namespace Internal
37 {
38
39 /**
40  * Button is the base class implementation for all buttons.
41  */
42 class Button : public Control
43 {
44
45 protected:
46
47   /**
48    * Construct a new Button.
49    */
50   Button();
51
52   /**
53    * A reference counted object may only be deleted by calling Unreference()
54    */
55   virtual ~Button();
56
57 public:
58
59   /**
60    * @copydoc Dali::Toolkit::Button::SetDisabled( bool disabled )
61    */
62   void SetDisabled( bool disabled );
63
64   /**
65    * @copydoc Dali::Toolkit::Button::IsDisabled() const
66    */
67   bool IsDisabled() const;
68
69   /**
70    * @copydoc Dali::Toolkit::Button::SetAutoRepeating( bool autoRepeating )
71    */
72   void SetAutoRepeating( bool autoRepeating );
73
74   /**
75    * @copydoc Dali::Toolkit::Button::IsAutoRepeating() const
76    */
77   bool IsAutoRepeating() const;
78
79   /**
80    * @copydoc Dali::Toolkit::Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
81    */
82   void SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay );
83
84   /**
85    * @copydoc Dali::Toolkit::Button::GetInitialAutoRepeatingDelay() const
86    */
87   float GetInitialAutoRepeatingDelay() const;
88
89   /**
90    * @copydoc Dali::Toolkit::Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
91    */
92   void SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay );
93
94   /**
95    * @copydoc Dali::Toolkit::Button::GetNextAutoRepeatingDelay() const
96    */
97   float GetNextAutoRepeatingDelay() const;
98
99   /**
100    * @copydoc Dali::Toolkit::Button::SetTogglableButton( bool togglable )
101    */
102   void SetTogglableButton( bool togglable );
103
104   /**
105    * @copydoc Dali::Toolkit::Button::IsTogglableButton() const
106    */
107   bool IsTogglableButton() const;
108
109   /**
110    * @copydoc Dali::Toolkit::Button::SetSelected( bool selected )
111    */
112   void SetSelected( bool selected );
113
114   /**
115    * @copydoc Dali::Toolkit::Button::IsSelected() const
116    */
117   bool IsSelected() const;
118
119   /**
120    * @copydoc Dali::Toolkit::Button::SetAnimationTime()
121    */
122   void SetAnimationTime( float animationTime );
123
124   /**
125    * @copydoc Dali::Toolkit::Button::GetAnimationTime()
126    */
127   float GetAnimationTime() const;
128
129   /**
130    * @copydoc Dali::Toolkit::Button::SetLabel( const std::string& label )
131    */
132   void SetLabel( const std::string& label );
133
134   /**
135    * @copydoc Dali::Toolkit::Button::SetLabel( Actor label )
136    */
137   void SetLabel( Actor label );
138
139   /**
140    * @copydoc Dali::Toolkit::Button::GetLabel()
141    */
142   Actor GetLabel() const;
143
144   /**
145    * @copydoc Dali::Toolkit::PushButton::SetButtonImage( Actor image )
146    */
147   void SetButtonImage( Actor image );
148
149   /**
150    * @copydoc Dali::Toolkit::PushButton::GetButtonImage()
151    */
152   Actor GetButtonImage() const;
153
154   /**
155    * Internal use only.
156    * @return A reference to the button image.
157    */
158   Actor& GetButtonImage();
159
160   /**
161    * @copydoc Dali::Toolkit::PushButton::SetSelectedImage( Actor image )
162    */
163   void SetSelectedImage( Actor image );
164
165   /**
166    * @copydoc Dali::Toolkit::PushButton::GetSelectedImage()
167    */
168   Actor GetSelectedImage() const;
169
170   /**
171    * Internal use only.
172    * @return A reference to the selected image.
173    */
174   Actor& GetSelectedImage();
175
176   /**
177    * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage( Actor image )
178    */
179   void SetBackgroundImage( Actor image );
180
181   /**
182    * @copydoc Dali::Toolkit::PushButton::GetBackgroundImage()
183    */
184   Actor GetBackgroundImage() const;
185
186   /**
187    * Internal use only.
188    * @return A reference to the background image.
189    */
190   Actor& GetBackgroundImage();
191
192   /**
193    * @copydoc Dali::Toolkit::PushButton::SetSelectedBackgroundImage( Actor image )
194    */
195   void SetSelectedBackgroundImage( Actor image );
196
197   /**
198    * @copydoc Dali::Toolkit::PushButton::GetSelectedBackgroundImage()
199    */
200   Actor GetSelectedBackgroundImage() const;
201
202   /**
203    * Internal use only.
204    * @return A reference to the selected background image.
205    */
206   Actor& GetSelectedBackgroundImage();
207
208   /**
209    * @copydoc Dali::Toolkit::PushButton::SetDisabledImage( Actor image )
210    */
211   void SetDisabledImage( Actor image );
212
213   /**
214    * @copydoc Dali::Toolkit::PushButton::GetDisabledImage()
215    */
216   Actor GetDisabledImage() const;
217
218   /**
219    * Internal use only.
220    * @return A reference to the disabled button image.
221    */
222   Actor& GetDisabledImage();
223
224   /**
225    * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage( Actor image )
226    */
227   void SetDisabledSelectedImage( Actor image );
228
229   /**
230    * @copydoc Dali::Toolkit::CheckBoxButton::GetDisabledSelectedImage()
231    */
232   Actor GetDisabledSelectedImage() const;
233
234   /**
235    * Internal use only.
236    * @return A reference to the disabled selected image.
237    */
238   Actor& GetDisabledSelectedImage();
239
240   /**
241    * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage( Actor image )
242    */
243   void SetDisabledBackgroundImage( Actor image );
244
245   /**
246    * @copydoc Dali::Toolkit::PushButton::GetDisabledBackgroundImage()
247    */
248   Actor GetDisabledBackgroundImage() const;
249
250   /**
251    * Internal use only.
252    * @return A reference to the disabled background image.
253    */
254   Actor& GetDisabledBackgroundImage();
255
256   /**
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
262    */
263   static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
264
265 protected:
266
267   /**
268    * @return A reference to the label actor.
269    */
270   Actor& GetLabel();
271
272   /**
273    * It changes the transition state of the push button.
274    */
275   void UpdatePaintTransitionState();
276
277 private:
278
279   /**
280    * Perform the click action to click the button.
281    * @param[in] attributes The attributes to perfrom this action.
282    * @return true if this control can perform action.
283    */
284   bool DoClickAction( const Property::Map& attributes );
285
286   /**
287    * This method is called after the button initialization.
288    * Could be reimplemented in subclasses to provide specific behaviour.
289    */
290   virtual void OnButtonInitialize() { }
291
292   /**
293    * This method is called when the label is set.
294    */
295   virtual void OnLabelSet() {}
296
297   /**
298    * This method is called when the button image is set
299    */
300   virtual void OnButtonImageSet() {}
301
302   /**
303    * This method is called when the selected image is set
304    */
305   virtual void OnSelectedImageSet() {}
306
307   /**
308    * This method is called when the background image is set
309    */
310   virtual void OnBackgroundImageSet() {}
311
312   /**
313    * This method is called when the selected background image is set
314    */
315   virtual void OnSelectedBackgroundImageSet() {}
316
317   /**
318    * This method is called when the disabled button image is set
319    */
320   virtual void OnDisabledImageSet() {}
321
322   /**
323    * This method is called when the disabled selected image is set
324    */
325   virtual void OnDisabledSelectedImageSet() {}
326
327   /**
328    * This method is called when the disabled background image is set
329    */
330   virtual void OnDisabledBackgroundImageSet() {}
331
332   /**
333    * This method is called from the OnTouchEvent method when the button is down.
334    * Could be reimplemented in subclasses to provide specific behaviour.
335    */
336   virtual void OnButtonDown();
337
338   /**
339    * This method is called from the OnTouchEvent method when the button is up.
340    * Could be reimplemented in subclasses to provide specific behaviour.
341    */
342   virtual void OnButtonUp();
343
344   /**
345    * This method is called from the OnTouchEvent method when the touch point leaves the boundary of the button or
346    * more than one touch points are received.
347    * Could be reimplemented in subclasses to provide specific behaviour.
348    */
349   virtual void OnTouchPointLeave();
350
351   /**
352    * This method is called from the OnTouchEvent method when the touch point is interrupted.
353    * Could be reimplemented in subclasses to provide specific behaviour.
354    */
355   virtual void OnTouchPointInterrupted();
356
357   /**
358    * This method is called when the button is removed from the stage.
359    * Could be reimplemented in subclasses to provide specific behaviour.
360    */
361   virtual void OnButtonStageDisconnection();
362
363   /**
364    * This method is called when the \e selected property is changed.
365    * @return true if the transition animation is started.
366    */
367   virtual bool OnSelected() { return false; }
368
369   /**
370    * This method is called when the \e disabled property is changed.
371    * @return true if the transition animation is started.
372    */
373   virtual bool OnDisabled() { return false; }
374
375   /**
376    * This method is called when the button is pressed.
377    * @return true if the transition animation is started.
378    */
379   virtual bool OnPressed() { return false; }
380
381   /**
382    * This method is called when the button is released.
383    * @return true if the transition animation is started.
384    */
385   virtual bool OnReleased() { return false; }
386
387   /**
388    * This method stops all animations
389    */
390   virtual void StopAllAnimations() {}
391
392 public:
393
394   /**
395    * @copydoc Dali::Toolkit::PushButton::PressedSignal()
396    */
397   Toolkit::Button::ButtonSignalType& PressedSignal();
398
399   /**
400    * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
401    */
402   Toolkit::Button::ButtonSignalType& ReleasedSignal();
403
404   /**
405    * @copydoc Dali::Toolkit::Button::ClickedSignal()
406    */
407   Toolkit::Button::ButtonSignalType& ClickedSignal();
408
409   /**
410    * @copydoc Dali::Toolkit::Button::StateChangedSignal()
411    */
412   Toolkit::Button::ButtonSignalType& StateChangedSignal();
413
414   /**
415    * Connects a callback function with the object's signals.
416    * @param[in] object The object providing the signal.
417    * @param[in] tracker Used to disconnect the signal.
418    * @param[in] signalName The signal to connect to.
419    * @param[in] functor A newly allocated FunctorDelegate.
420    * @return True if the signal was connected.
421    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
422    */
423   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
424
425   // Properties
426
427   /**
428    * Called when a property of an object of this type is set.
429    * @param[in] object The object whose property is set.
430    * @param[in] index The property index.
431    * @param[in] value The new property value.
432    */
433   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
434
435   /**
436    * Called to retrieve a property of an object of this type.
437    * @param[in] object The object whose property is to be retrieved.
438    * @param[in] index The property index.
439    * @return The current value of the property.
440    */
441   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
442
443 protected: // From CustomActorImpl
444
445   /**
446    * @copydoc Dali::CustomActorImpl::OnTouchEvent( const TouchEvent& event )
447    */
448   virtual bool OnTouchEvent( const TouchEvent& event );
449
450 private: // From Control
451
452   /**
453    * @copydoc Toolkit::Control::OnInitialize()
454    */
455   virtual void OnInitialize();
456
457   /**
458    * @copydoc Toolkit::Control::OnAccessibilityActivated()
459    */
460   virtual bool OnAccessibilityActivated();
461
462   /**
463    * @copydoc Toolkit::Control::OnKeyboardEnter()
464    */
465   virtual bool OnKeyboardEnter();
466
467   /**
468    * Callback received when the button is disconnected from the stage.
469    * It resets the button status.
470    */
471   void OnControlStageDisconnection();
472
473 private:
474
475   /**
476    * Handler for tap events.
477    * We do not actually do anything when we receive a tap as the button handles tap event through
478    * the touch event system itself as it requires more than just tap handling (e.g. leave events).
479    * This stops any of our parents receiving a tap gesture when it occurs within our area.
480    * @param[in]  actor  The tapped actor.
481    * @param[in]  tap    The tap gesture.
482    */
483   void OnTap(Actor actor, const TapGesture& tap);
484
485   /**
486    * Sets up the autorepeating timer.
487    * @param[in] delay The delay time in seconds.
488    */
489   void SetUpTimer( float delay );
490
491   /**
492    * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
493    */
494   bool AutoRepeatingSlot();
495
496   /**
497    * Sets the button as selected or unselected.
498    * @param[in] selected \e selected property value.
499    * @param[in] emitSignal Emit a signal if this value is \e true.
500    */
501   void SetSelected( bool selected, bool emitSignal );
502
503   /**
504    * This method is called when the button is pressed.
505    */
506   void Pressed();
507
508   /**
509    * This method is called when the button is released.
510    */
511   void Released();
512
513 protected:
514
515   enum ButtonState
516   {
517     ButtonUp,                                  ///< The button is up.
518     ButtonDown,                                ///< The button is down.
519   };
520
521   /**
522    * Button paint states.
523    */
524   enum PaintState
525   {
526     UnselectedState,              ///< The button is unselected.
527     SelectedState,                ///< The button is selected.
528     DisabledUnselectedState,      ///< The button is disabled and unselected.
529     DisabledSelectedState,        ///< The button is disabled and selected.
530     UnselectedSelectedTransition, ///< The button is in transition from unselected to selected.
531     SelectedUnselectedTransition, ///< The button is in transition from selected to unselected.
532     UnselectedDisabledTransition, ///< The button is in transition from unselected to disabled.
533     DisabledUnselectedTransition, ///< The button is in transition from disabled to unselected.
534     SelectedDisabledTransition,   ///< The button is in transition from selected to disabled.
535     DisabledSelectedTransition    ///< The button is in transition from disabled to selected.
536   };
537
538   ButtonState GetState();
539   PaintState GetPaintState();
540
541   /**
542    * Inserts the actor to the button.
543    */
544   void InsertChild( unsigned int index, Actor& actor );
545
546   /**
547    * Removes the actor from the button.
548    */
549   void RemoveChild( Actor& actor );
550
551   /**
552    * Finds the index of the actor.
553    * If the actor doesn't exist, return the last index + 1.
554    */
555   unsigned int FindChildIndex( Actor& actor );
556
557 private:
558
559   // Undefined
560   Button( const Button& );
561
562   // Undefined
563   Button& operator = ( const Button& );
564
565 private:
566
567   // Signals
568   Toolkit::Button::ButtonSignalType mPressedSignal;           ///< Signal emitted when the button is pressed.
569   Toolkit::Button::ButtonSignalType mReleasedSignal;          ///< Signal emitted when the button is released.
570   Toolkit::Button::ButtonSignalType mClickedSignal;           ///< Signal emitted when the button is clicked.
571   Toolkit::Button::ButtonSignalType mStateChangedSignal;      ///< Signal emitted when the button's state is changed.
572
573   Timer mAutoRepeatingTimer;                   ///< Timer used to implement the autorepeating property.
574
575   Actor mLabel;                                ///< Stores the button label.
576
577   Actor mButtonContent;                        ///< Stores the unselected content.
578   Actor mSelectedContent;                      ///< Stores the selected content.
579   Actor mBackgroundContent;                    ///< Stores the background content.
580   Actor mSelectedBackgroundContent;            ///< Stores the selected background content.
581   Actor mDisabledContent;                      ///< Stores the disabled content.
582   Actor mDisabledSelectedContent;              ///< Stores the disabled selected content.
583   Actor mDisabledBackgroundContent;            ///< Stores the disabled background content.
584
585   TapGestureDetector mTapDetector;
586
587   bool             mDisabled;                  ///< Stores the disabled property.
588   bool             mAutoRepeating;             ///< Stores the autorepeating property.
589   bool             mTogglableButton;           ///< Stores the togglable property.
590   bool             mSelected;                  ///< Stores the selected state.
591   float            mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
592   float            mNextAutoRepeatingDelay;    ///< Stores the next autorepeating delay in seconds.
593   float            mAnimationTime;             ///< The animation time.
594
595   // Actions
596   bool             mClickActionPerforming;
597
598   ButtonState      mState;                     ///< Stores the button state.
599   PaintState       mPaintState;                ///< Stores the paint state.
600 };
601
602 } // namespace Internal
603
604 // Helpers for public-api forwarding methods
605
606 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
607 {
608   DALI_ASSERT_ALWAYS( button );
609
610   Dali::RefObject& handle = button.GetImplementation();
611
612   return static_cast<Toolkit::Internal::Button&>( handle );
613 }
614
615 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
616 {
617   DALI_ASSERT_ALWAYS( button );
618
619   const Dali::RefObject& handle = button.GetImplementation();
620
621   return static_cast<const Toolkit::Internal::Button&>( handle );
622 }
623
624 } // namespace Toolkit
625
626 } // namespace Dali
627
628 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__