[dali_1.0.33] Merge branch 'tizen'
[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::GetButtonImage()
146    */
147   Actor GetButtonImage() const;
148
149   /**
150    * Internal use only.
151    * @return A reference to the button image.
152    */
153   Actor& GetButtonImage();
154
155   /**
156    * @copydoc Dali::Toolkit::PushButton::GetSelectedImage()
157    */
158   Actor GetSelectedImage() const;
159
160   /**
161    * Internal use only.
162    * @return A reference to the selected image.
163    */
164   Actor& GetSelectedImage();
165
166   /**
167    * @copydoc Dali::Toolkit::PushButton::GetBackgroundImage()
168    */
169   Actor GetBackgroundImage() const;
170
171   /**
172    * Internal use only.
173    * @return A reference to the background image.
174    */
175   Actor& GetBackgroundImage();
176
177   /**
178    * @copydoc Dali::Toolkit::PushButton::GetDisabledImage()
179    */
180   Actor GetDisabledImage() const;
181
182   /**
183    * Internal use only.
184    * @return A reference to the disabled button image.
185    */
186   Actor& GetDisabledImage();
187
188   /**
189    * @copydoc Dali::Toolkit::CheckBoxButton::GetDisabledSelectedImage()
190    */
191   Actor GetDisabledSelectedImage() const;
192
193   /**
194    * Internal use only.
195    * @return A reference to the disabled selected image.
196    */
197   Actor& GetDisabledSelectedImage();
198
199   /**
200    * @copydoc Dali::Toolkit::PushButton::GetDisabledBackgroundImage()
201    */
202   Actor GetDisabledBackgroundImage() const;
203
204   /**
205    * Internal use only.
206    * @return A reference to the disabled background image.
207    */
208   Actor& GetDisabledBackgroundImage();
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 PropertyValueContainer& attributes );
218
219   /**
220    * @copydoc Dali::Toolkit::PushButton::SetButtonImage( Actor image )
221    */
222   virtual void SetButtonImage( Actor image ) {}
223
224   /**
225    * @copydoc Dali::Toolkit::PushButton::SetSelectedImage( Actor image )
226    */
227   virtual void SetSelectedImage( Actor image ) {}
228
229   /**
230    * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage( Actor image )
231    */
232   virtual void SetBackgroundImage( Actor image ) {}
233
234   /**
235    * @copydoc Dali::Toolkit::PushButton::SetDisabledImage( Actor image )
236    */
237   virtual void SetDisabledImage( Actor image ) {}
238
239   /**
240    * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage( Actor image )
241    */
242   virtual void SetDisabledSelectedImage( Actor image ) {}
243
244   /**
245    * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage( Actor image )
246    */
247   virtual void SetDisabledBackgroundImage( Actor image ) {}
248
249 protected:
250
251   /**
252    * Internal use only.
253    * @return A reference to the label actor.
254    */
255   Actor& GetLabel();
256
257 private:
258
259   /**
260    * Perform the click action to click the button.
261    * @param[in] attributes The attributes to perfrom this action.
262    */
263   void DoClickAction( const PropertyValueContainer& attributes );
264
265   /**
266    * This method is called after the button initialization.
267    * Could be reimplemented in subclasses to provide specific behaviour.
268    */
269   virtual void OnButtonInitialize() { }
270
271   /**
272    * This method is called from the OnTouchEvent method when the button is down.
273    * Could be reimplemented in subclasses to provide specific behaviour.
274    */
275   virtual void OnButtonDown();
276
277   /**
278    * This method is called from the OnTouchEvent method when the button is up.
279    * Could be reimplemented in subclasses to provide specific behaviour.
280    */
281   virtual void OnButtonUp();
282
283   /**
284    * This method is called from the OnTouchEvent method when the touch point leaves the boundary of the button or
285    * more than one touch points are received.
286    * Could be reimplemented in subclasses to provide specific behaviour.
287    */
288   virtual void OnTouchPointLeave();
289
290   /**
291    * This method is called from the OnTouchEvent method when the touch point is interrupted.
292    * Could be reimplemented in subclasses to provide specific behaviour.
293    */
294   virtual void OnTouchPointInterrupted();
295
296   /**
297    * This method is called when the button is removed from the stage.
298    * Could be reimplemented in subclasses to provide specific behaviour.
299    */
300   virtual void OnButtonStageDisconnection();
301
302   /**
303    * This method is called when the label is set.
304    */
305   virtual void OnLabelSet() {}
306
307   /**
308    * This method is called when the \e selected property is changed.
309    */
310   virtual void OnSelected( bool selected ) {}
311
312   /**
313    * This method is called when the \e disabled property is changed.
314    */
315   virtual void OnDisabled( bool disabled ) {}
316
317   /**
318    * This method is called when the button is pressed.
319    */
320   virtual void OnPressed() {}
321
322   /**
323    * This method is called when the button is released.
324    */
325   virtual void OnReleased() {}
326
327   /**
328    * This method is called when the button is clicked
329    */
330   virtual void OnClicked() {}
331
332 public:
333
334   /**
335    * @copydoc Dali::Toolkit::PushButton::PressedSignal()
336    */
337   Toolkit::Button::ButtonSignalType& PressedSignal();
338
339   /**
340    * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
341    */
342   Toolkit::Button::ButtonSignalType& ReleasedSignal();
343
344   /**
345    * @copydoc Dali::Toolkit::Button::ClickedSignal()
346    */
347   Toolkit::Button::ButtonSignalType& ClickedSignal();
348
349   /**
350    * @copydoc Dali::Toolkit::Button::StateChangedSignal()
351    */
352   Toolkit::Button::ButtonSignalType& StateChangedSignal();
353
354   /**
355    * Connects a callback function with the object's signals.
356    * @param[in] object The object providing the signal.
357    * @param[in] tracker Used to disconnect the signal.
358    * @param[in] signalName The signal to connect to.
359    * @param[in] functor A newly allocated FunctorDelegate.
360    * @return True if the signal was connected.
361    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
362    */
363   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
364
365   // Properties
366
367   /**
368    * Called when a property of an object of this type is set.
369    * @param[in] object The object whose property is set.
370    * @param[in] index The property index.
371    * @param[in] value The new property value.
372    */
373   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
374
375   /**
376    * Called to retrieve a property of an object of this type.
377    * @param[in] object The object whose property is to be retrieved.
378    * @param[in] index The property index.
379    * @return The current value of the property.
380    */
381   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
382
383 protected: // From CustomActorImpl
384
385   /**
386    * @copydoc Dali::CustomActorImpl::OnTouchEvent( const TouchEvent& event )
387    */
388   virtual bool OnTouchEvent( const TouchEvent& event );
389
390 private: // From Control
391
392   /**
393    * @copydoc Toolkit::Control::OnInitialize()
394    */
395   virtual void OnInitialize();
396
397   /**
398    * @copydoc Toolkit::Control::OnActivated()
399    */
400   virtual void OnActivated();
401
402   /**
403    * Callback received when the button is disconnected from the stage.
404    * It resets the button status.
405    */
406   void OnControlStageDisconnection();
407
408 private:
409
410   /**
411    * Handler for tap events.
412    * We do not actually do anything when we receive a tap as the button handles tap event through
413    * the touch event system itself as it requires more than just tap handling (e.g. leave events).
414    * This stops any of our parents receiving a tap gesture when it occurs within our area.
415    * @param[in]  actor  The tapped actor.
416    * @param[in]  tap    The tap gesture.
417    */
418   void OnTap(Actor actor, const TapGesture& tap);
419
420   /**
421    * Sets up the autorepeating timer.
422    * @param[in] delay The delay time in seconds.
423    */
424   void SetUpTimer( float delay );
425
426   /**
427    * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
428    */
429   bool AutoRepeatingSlot();
430
431 protected:
432
433   enum ButtonState
434   {
435     ButtonUp,                                  ///< The button is up.
436     ButtonDown,                                ///< The button is down.
437   };
438
439   /**
440    * Button paint states.
441    */
442   enum PaintState
443   {
444     UnselectedState,              ///< The button is unselected.
445     SelectedState,                ///< The button is selected.
446     DisabledUnselectedState,      ///< The button is disabled and unselected.
447     DisabledSelectedState,        ///< The button is disabled and selected.
448     UnselectedSelectedTransition, ///< The button is in transition from unselected to selected.
449     SelectedUnselectedTransition, ///< The push button is in transition from selected to unselected.
450     UnselectedDisabledTransition, ///< The button is in transition from unselected to disabled.
451     DisabledUnselectedTransition, ///< The button is in transition from disabled to unselected.
452     SelectedDisabledTransition,   ///< The button is in transition from selected to disabled.
453     DisabledSelectedTransition    ///< The button is in transition from disabled to selected.
454   };
455
456   ButtonState GetState();
457
458 private:
459
460   // Undefined
461   Button( const Button& );
462
463   // Undefined
464   Button& operator = ( const Button& );
465
466 private:
467
468   // Signals
469   Toolkit::Button::ButtonSignalType mPressedSignal;           ///< Signal emitted when the button is pressed.
470   Toolkit::Button::ButtonSignalType mReleasedSignal;          ///< Signal emitted when the button is released.
471   Toolkit::Button::ButtonSignalType mClickedSignal;           ///< Signal emitted when the button is clicked.
472   Toolkit::Button::ButtonSignalType mStateChangedSignal;      ///< Signal emitted when the button's state is changed.
473
474   Timer mAutoRepeatingTimer;                   ///< Timer used to implement the autorepeating property.
475
476   Actor mLabel;                                ///< Stores the button label.
477
478   Actor mButtonContent;                        ///< Stores the unselected content.
479   Actor mSelectedContent;                      ///< Stores the selected content.
480   Actor mBackgroundContent;                    ///< Stores the background content.
481   Actor mDisabledContent;                      ///< Stores the disabled content.
482   Actor mDisabledSelectedContent;              ///< Stores the disabled selected content.
483   Actor mDisabledBackgroundContent;            ///< Stores the disabled background content.
484
485   TapGestureDetector mTapDetector;
486
487   bool             mDisabled;                  ///< Stores the disabled property.
488   bool             mAutoRepeating;             ///< Stores the autorepeating property.
489   bool             mTogglableButton;           ///< Stores the togglable property.
490   bool             mSelected;                  ///< Stores the selected state.
491   float            mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
492   float            mNextAutoRepeatingDelay;    ///< Stores the next autorepeating delay in seconds.
493   float            mAnimationTime;             ///< The animation time.
494
495   // Actions
496   bool             mClickActionPerforming;
497
498   ButtonState      mState;                     ///< Stores the button state.
499 };
500
501 } // namespace Internal
502
503 // Helpers for public-api forwarding methods
504
505 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
506 {
507   DALI_ASSERT_ALWAYS( button );
508
509   Dali::RefObject& handle = button.GetImplementation();
510
511   return static_cast<Toolkit::Internal::Button&>( handle );
512 }
513
514 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
515 {
516   DALI_ASSERT_ALWAYS( button );
517
518   const Dali::RefObject& handle = button.GetImplementation();
519
520   return static_cast<const Toolkit::Internal::Button&>( handle );
521 }
522
523 } // namespace Toolkit
524
525 } // namespace Dali
526
527 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__