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