Changes for std::vector removal from api
[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    */
283   void DoClickAction( const Property::Map& attributes );
284
285   /**
286    * This method is called after the button initialization.
287    * Could be reimplemented in subclasses to provide specific behaviour.
288    */
289   virtual void OnButtonInitialize() { }
290
291   /**
292    * This method is called when the label is set.
293    */
294   virtual void OnLabelSet() {}
295
296   /**
297    * This method is called when the button image is set
298    */
299   virtual void OnButtonImageSet() {}
300
301   /**
302    * This method is called when the selected image is set
303    */
304   virtual void OnSelectedImageSet() {}
305
306   /**
307    * This method is called when the background image is set
308    */
309   virtual void OnBackgroundImageSet() {}
310
311   /**
312    * This method is called when the selected background image is set
313    */
314   virtual void OnSelectedBackgroundImageSet() {}
315
316   /**
317    * This method is called when the disabled button image is set
318    */
319   virtual void OnDisabledImageSet() {}
320
321   /**
322    * This method is called when the disabled selected image is set
323    */
324   virtual void OnDisabledSelectedImageSet() {}
325
326   /**
327    * This method is called when the disabled background image is set
328    */
329   virtual void OnDisabledBackgroundImageSet() {}
330
331   /**
332    * This method is called from the OnTouchEvent method when the button is down.
333    * Could be reimplemented in subclasses to provide specific behaviour.
334    */
335   virtual void OnButtonDown();
336
337   /**
338    * This method is called from the OnTouchEvent method when the button is up.
339    * Could be reimplemented in subclasses to provide specific behaviour.
340    */
341   virtual void OnButtonUp();
342
343   /**
344    * This method is called from the OnTouchEvent method when the touch point leaves the boundary of the button or
345    * more than one touch points are received.
346    * Could be reimplemented in subclasses to provide specific behaviour.
347    */
348   virtual void OnTouchPointLeave();
349
350   /**
351    * This method is called from the OnTouchEvent method when the touch point is interrupted.
352    * Could be reimplemented in subclasses to provide specific behaviour.
353    */
354   virtual void OnTouchPointInterrupted();
355
356   /**
357    * This method is called when the button is removed from the stage.
358    * Could be reimplemented in subclasses to provide specific behaviour.
359    */
360   virtual void OnButtonStageDisconnection();
361
362   /**
363    * This method is called when the \e selected property is changed.
364    * @return true if the transition animation is started.
365    */
366   virtual bool OnSelected() { return false; }
367
368   /**
369    * This method is called when the \e disabled property is changed.
370    * @return true if the transition animation is started.
371    */
372   virtual bool OnDisabled() { return false; }
373
374   /**
375    * This method is called when the button is pressed.
376    * @return true if the transition animation is started.
377    */
378   virtual bool OnPressed() { return false; }
379
380   /**
381    * This method is called when the button is released.
382    * @return true if the transition animation is started.
383    */
384   virtual bool OnReleased() { return false; }
385
386   /**
387    * This method stops all animations
388    */
389   virtual void StopAllAnimations() {}
390
391 public:
392
393   /**
394    * @copydoc Dali::Toolkit::PushButton::PressedSignal()
395    */
396   Toolkit::Button::ButtonSignalType& PressedSignal();
397
398   /**
399    * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
400    */
401   Toolkit::Button::ButtonSignalType& ReleasedSignal();
402
403   /**
404    * @copydoc Dali::Toolkit::Button::ClickedSignal()
405    */
406   Toolkit::Button::ButtonSignalType& ClickedSignal();
407
408   /**
409    * @copydoc Dali::Toolkit::Button::StateChangedSignal()
410    */
411   Toolkit::Button::ButtonSignalType& StateChangedSignal();
412
413   /**
414    * Connects a callback function with the object's signals.
415    * @param[in] object The object providing the signal.
416    * @param[in] tracker Used to disconnect the signal.
417    * @param[in] signalName The signal to connect to.
418    * @param[in] functor A newly allocated FunctorDelegate.
419    * @return True if the signal was connected.
420    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
421    */
422   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
423
424   // Properties
425
426   /**
427    * Called when a property of an object of this type is set.
428    * @param[in] object The object whose property is set.
429    * @param[in] index The property index.
430    * @param[in] value The new property value.
431    */
432   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
433
434   /**
435    * Called to retrieve a property of an object of this type.
436    * @param[in] object The object whose property is to be retrieved.
437    * @param[in] index The property index.
438    * @return The current value of the property.
439    */
440   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
441
442 protected: // From CustomActorImpl
443
444   /**
445    * @copydoc Dali::CustomActorImpl::OnTouchEvent( const TouchEvent& event )
446    */
447   virtual bool OnTouchEvent( const TouchEvent& event );
448
449 private: // From Control
450
451   /**
452    * @copydoc Toolkit::Control::OnInitialize()
453    */
454   virtual void OnInitialize();
455
456   /**
457    * @copydoc Toolkit::Control::OnActivated()
458    */
459   virtual void OnActivated();
460
461   /**
462    * Callback received when the button is disconnected from the stage.
463    * It resets the button status.
464    */
465   void OnControlStageDisconnection();
466
467 private:
468
469   /**
470    * Handler for tap events.
471    * We do not actually do anything when we receive a tap as the button handles tap event through
472    * the touch event system itself as it requires more than just tap handling (e.g. leave events).
473    * This stops any of our parents receiving a tap gesture when it occurs within our area.
474    * @param[in]  actor  The tapped actor.
475    * @param[in]  tap    The tap gesture.
476    */
477   void OnTap(Actor actor, const TapGesture& tap);
478
479   /**
480    * Sets up the autorepeating timer.
481    * @param[in] delay The delay time in seconds.
482    */
483   void SetUpTimer( float delay );
484
485   /**
486    * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
487    */
488   bool AutoRepeatingSlot();
489
490   /**
491    * Sets the button as selected or unselected.
492    * @param[in] selected \e selected property value.
493    * @param[in] emitSignal Emit a signal if this value is \e true.
494    */
495   void SetSelected( bool selected, bool emitSignal );
496
497   /**
498    * This method is called when the button is pressed.
499    */
500   void Pressed();
501
502   /**
503    * This method is called when the button is released.
504    */
505   void Released();
506
507 protected:
508
509   enum ButtonState
510   {
511     ButtonUp,                                  ///< The button is up.
512     ButtonDown,                                ///< The button is down.
513   };
514
515   /**
516    * Button paint states.
517    */
518   enum PaintState
519   {
520     UnselectedState,              ///< The button is unselected.
521     SelectedState,                ///< The button is selected.
522     DisabledUnselectedState,      ///< The button is disabled and unselected.
523     DisabledSelectedState,        ///< The button is disabled and selected.
524     UnselectedSelectedTransition, ///< The button is in transition from unselected to selected.
525     SelectedUnselectedTransition, ///< The button is in transition from selected to unselected.
526     UnselectedDisabledTransition, ///< The button is in transition from unselected to disabled.
527     DisabledUnselectedTransition, ///< The button is in transition from disabled to unselected.
528     SelectedDisabledTransition,   ///< The button is in transition from selected to disabled.
529     DisabledSelectedTransition    ///< The button is in transition from disabled to selected.
530   };
531
532   ButtonState GetState();
533   PaintState GetPaintState();
534
535   /**
536    * Inserts the actor to the button.
537    */
538   void InsertChild( unsigned int index, Actor& actor );
539
540   /**
541    * Removes the actor from the button.
542    */
543   void RemoveChild( Actor& actor );
544
545   /**
546    * Finds the index of the actor.
547    * If the actor doesn't exist, return the last index + 1.
548    */
549   unsigned int FindChildIndex( Actor& actor );
550
551 private:
552
553   // Undefined
554   Button( const Button& );
555
556   // Undefined
557   Button& operator = ( const Button& );
558
559 private:
560
561   // Signals
562   Toolkit::Button::ButtonSignalType mPressedSignal;           ///< Signal emitted when the button is pressed.
563   Toolkit::Button::ButtonSignalType mReleasedSignal;          ///< Signal emitted when the button is released.
564   Toolkit::Button::ButtonSignalType mClickedSignal;           ///< Signal emitted when the button is clicked.
565   Toolkit::Button::ButtonSignalType mStateChangedSignal;      ///< Signal emitted when the button's state is changed.
566
567   Timer mAutoRepeatingTimer;                   ///< Timer used to implement the autorepeating property.
568
569   Actor mLabel;                                ///< Stores the button label.
570
571   Actor mButtonContent;                        ///< Stores the unselected content.
572   Actor mSelectedContent;                      ///< Stores the selected content.
573   Actor mBackgroundContent;                    ///< Stores the background content.
574   Actor mSelectedBackgroundContent;            ///< Stores the selected background content.
575   Actor mDisabledContent;                      ///< Stores the disabled content.
576   Actor mDisabledSelectedContent;              ///< Stores the disabled selected content.
577   Actor mDisabledBackgroundContent;            ///< Stores the disabled background content.
578
579   TapGestureDetector mTapDetector;
580
581   bool             mDisabled;                  ///< Stores the disabled property.
582   bool             mAutoRepeating;             ///< Stores the autorepeating property.
583   bool             mTogglableButton;           ///< Stores the togglable property.
584   bool             mSelected;                  ///< Stores the selected state.
585   float            mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
586   float            mNextAutoRepeatingDelay;    ///< Stores the next autorepeating delay in seconds.
587   float            mAnimationTime;             ///< The animation time.
588
589   // Actions
590   bool             mClickActionPerforming;
591
592   ButtonState      mState;                     ///< Stores the button state.
593   PaintState       mPaintState;                ///< Stores the paint state.
594 };
595
596 } // namespace Internal
597
598 // Helpers for public-api forwarding methods
599
600 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
601 {
602   DALI_ASSERT_ALWAYS( button );
603
604   Dali::RefObject& handle = button.GetImplementation();
605
606   return static_cast<Toolkit::Internal::Button&>( handle );
607 }
608
609 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
610 {
611   DALI_ASSERT_ALWAYS( button );
612
613   const Dali::RefObject& handle = button.GetImplementation();
614
615   return static_cast<const Toolkit::Internal::Button&>( handle );
616 }
617
618 } // namespace Toolkit
619
620 } // namespace Dali
621
622 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__