Merge "Make radio buttons work with size negotiation, bug fixes." into tizen
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / buttons / push-button-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_PUSH_BUTTON_H__
2 #define __DALI_TOOLKIT_INTERNAL_PUSH_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 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-vector.h>
23
24 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
25
26 #include "button-impl.h"
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 /**
38  * PushButton implementation class.
39  *
40  * \sa Dali::Toolkit::PushButton
41  */
42 class PushButton : public Button
43 {
44 public:
45
46   /**
47    * Create a new PushButton.
48    * @return A smart-pointer to the newly allocated PushButton.
49    */
50   static Dali::Toolkit::PushButton New();
51
52   /**
53    * @copydoc Dali::Toolkit::PushButton::SetAutoRepeating( bool autoRepeating )
54    */
55   void SetAutoRepeating( bool autoRepeating );
56
57   /**
58    * @copydoc Dali::Toolkit::PushButton::IsAutoRepeating() const
59    */
60   bool IsAutoRepeating() const;
61
62   /**
63    * @copydoc Dali::Toolkit::PushButton::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
64    */
65   void SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay );
66
67   /**
68    * @copydoc Dali::Toolkit::PushButton::GetInitialAutoRepeatingDelay() const
69    */
70   float GetInitialAutoRepeatingDelay() const;
71
72   /**
73    * @copydoc Dali::Toolkit::PushButton::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
74    */
75   void SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay );
76
77   /**
78    * @copydoc Dali::Toolkit::PushButton::GetNextAutoRepeatingDelay() const
79    */
80   float GetNextAutoRepeatingDelay() const;
81
82   /**
83    * @copydoc Dali::Toolkit::PushButton::SetToggleButton( bool toggle )
84    */
85   void SetToggleButton( bool toggle );
86
87   /**
88    * @copydoc Dali::Toolkit::PushButton::IsToggleButton() const
89    */
90   bool IsToggleButton() const;
91
92   /**
93    * @copydoc Dali::Toolkit::PushButton::SetToggled( bool toggle )
94    */
95   void SetToggled( bool toggle );
96
97   /**
98    * @copydoc Dali::Toolkit::PushButton::IsToggled() const
99    */
100   bool IsToggled() const;
101
102   /**
103    * @copydoc Dali::Toolkit::PushButton::SetButtonImage( const Image image )
104    */
105   void SetButtonImage( Image image );
106
107   /**
108    * @copydoc Dali::Toolkit::PushButton::SetButtonImage( Actor image )
109    */
110   void SetButtonImage( Actor image );
111
112   /**
113    * Used by the painter only.
114    * @return A reference to the button image.
115    */
116   Actor& GetButtonImage();
117
118   /**
119    * @copydoc Dali::Toolkit::PushButton::
120    */
121   Actor GetButtonImage() const;
122
123   /**
124    * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage( const Image image )
125    */
126   void SetBackgroundImage( Image image );
127
128   /**
129    * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage( Actor image )
130    */
131   void SetBackgroundImage( Actor image );
132
133   /**
134    * Used by the painter only.
135    * @return A reference to the background image.
136    */
137   Actor& GetBackgroundImage();
138
139   /**
140    * @copydoc Dali::Toolkit::PushButton::GetBackgroundImage()
141    */
142   Actor GetBackgroundImage() const;
143
144   /**
145    * @copydoc Dali::Toolkit::PushButton::SetPressedImage( const Image image )
146    */
147   void SetPressedImage( Image image );
148
149   /**
150    * @copydoc Dali::Toolkit::PushButton::SetPressedImage( Actor image )
151    */
152   void SetPressedImage( Actor image );
153
154   /**
155    * Used by the painter only.
156    * @return A reference to the pressed image.
157    */
158   Actor& GetPressedImage();
159
160   /**
161    * @copydoc Dali::Toolkit::PushButton::GetPressedImage()
162    */
163   Actor GetPressedImage() const;
164
165   /**
166    * @copydoc Dali::Toolkit::PushButton::SetDimmedBackgroundImage( Image image )
167    */
168   void SetDimmedBackgroundImage( Image image );
169
170   /**
171    * @copydoc Dali::Toolkit::PushButton::SetDimmedBackgroundImage( Actor image )
172    */
173   void SetDimmedBackgroundImage( Actor image );
174
175   /**
176    * Used by the painter only.
177    * @return A reference to the dimmed background image.
178    */
179   Actor& GetDimmedBackgroundImage();
180
181   /**
182    * @copydoc Dali::Toolkit::PushButton::GetDimmedBackgroundImage()
183    */
184   Actor GetDimmedBackgroundImage() const;
185
186   /**
187    * @copydoc Dali::Toolkit::PushButton::SetDimmedImage( Image image )
188    */
189   void SetDimmedImage( Image image );
190
191   /**
192    * @copydoc Dali::Toolkit::PushButton::SetDimmedImage( Actor image )
193    */
194   void SetDimmedImage( Actor image );
195
196   /**
197    * Used by the painter only.
198    * @return A reference to the dimmed button image.
199    */
200   Actor& GetDimmedImage();
201
202   /**
203    * @copydoc Dali::Toolkit::PushButton::GetDimmedImage()
204    */
205   Actor GetDimmedImage() const;
206
207   /**
208    * @copydoc Dali::Toolkit::PushButton::SetLabelText( const std::string& text )
209    */
210   void SetLabelText( const std::string& text );
211
212   /**
213    * @copydoc Dali::Toolkit::PushButton::SetLabelText( Actor text )
214    */
215   void SetLabelText( Actor text );
216
217   /**
218    * Used by the painter only.
219    * @return A reference to the label actor.
220    */
221   Actor& GetLabel();
222
223   /**
224    * @copydoc Dali::Toolkit::PushButton::GetLabelText()
225    */
226   Actor GetLabelText() const;
227
228   /**
229    * Used by the painter only.
230    * @return A reference to the background image that is fading out.
231    */
232   Actor& GetFadeOutBackgroundImage();
233
234   /**
235    * Used by the painter only.
236    * @return A reference to the button image that is fading out.
237    */
238   Actor& GetFadeOutButtonImage();
239
240 public:
241
242   // Signals
243
244   /**
245    * @copydoc Dali::Toolkit::PushButton::PressedSignal()
246    */
247   Toolkit::PushButton::PressedSignalV2& PressedSignal();
248
249   /**
250    * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
251    */
252   Toolkit::PushButton::ReleasedSignalV2& ReleasedSignal();
253
254   /**
255    * Connects a callback function with the object's signals.
256    * @param[in] object The object providing the signal.
257    * @param[in] tracker Used to disconnect the signal.
258    * @param[in] signalName The signal to connect to.
259    * @param[in] functor A newly allocated FunctorDelegate.
260    * @return True if the signal was connected.
261    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
262    */
263   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
264
265   // Properties
266
267   /**
268    * @copydoc Button::SetProperty
269    */
270   static void SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value );
271
272   /**
273    * @copydoc Button::GetProperty
274    */
275   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
276
277 protected: // From Button
278
279   /**
280    * Sets the Leave signal.
281    */
282   virtual void OnButtonInitialize();
283
284   /**
285    * Emits signals and notifies the painter accordingly with the set button
286    * properties when the button is pressed.
287    */
288   virtual void OnButtonDown();
289
290   /**
291    * Emits signals and notifies the painter accordingly with the set button
292    * properties when the button is released.
293    */
294   virtual void OnButtonUp();
295
296   /**
297    * Emits signals and notifies the painter accordingly with the set button
298    * properties when the touch point leaves the boundary of the button.
299    */
300   virtual void OnTouchPointLeave();
301
302   /**
303    * Currently it doesn't need different behaviour than @see OnTouchPointLeave()
304    */
305   virtual void OnTouchPointInterrupted();
306
307   /**
308    * Sets the push button animation time.
309    * @param animationTime The animation time in seconds.
310    */
311   virtual void OnAnimationTimeSet( float animationTime );
312
313   /**
314    * Retrieves the animation time.
315    * @return The animation time in seconds.
316    */
317   virtual float OnAnimationTimeRequested() const;
318
319   /**
320    * This method is called when the button is removed from the stage.
321    */
322   virtual void OnButtonStageDisconnection();
323
324 protected: // From Control
325
326   /**
327    * Respond the activate notification.
328    */
329   virtual void OnActivated();
330
331   /**
332    * @copydoc Control::GetNaturalSize()
333    */
334   virtual Vector3 GetNaturalSize();
335
336 private:
337
338   /**
339    * Perform the click action to click the button.
340    * @param[in] attributes The attributes to perfrom this action.
341    */
342   void DoClickAction(const PropertyValueContainer& attributes);
343
344 public:
345
346   /**
347    * Performs actions as requested using the action name.
348    * @param[in] object The object on which to perform the action.
349    * @param[in] actionName The action to perform.
350    * @param[in] attributes The attributes with which to perfrom this action.
351    * @return true if action has been accepted by this control
352    */
353   static bool DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes);
354
355   /**
356    * Construct a new PushButton.
357    */
358   PushButton();
359
360   /**
361    * A reference counted object may only be deleted by calling Unreference()
362    */
363   virtual ~PushButton();
364
365 private:
366
367   // Undefined
368   PushButton( const PushButton& );
369
370   // Undefined
371   PushButton& operator=( const PushButton& );
372
373   /**
374    * Sets up the autorepeating timer.
375    * @param[in] delay The delay time in seconds.
376    */
377   void SetUpTimer( float delay );
378
379   /**
380    * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
381    */
382   bool AutoRepeatingSlot();
383
384 private:
385   bool  mAutoRepeating;             ///< Stores the autorepeating property.
386   float mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
387   float mNextAutoRepeatingDelay;    ///< Stores the next autorepeating delay in seconds.
388   bool  mToggleButton;              ///< Stores the toggle property.
389
390   // AutoRepeating
391   Timer mAutoRepeatingTimer;        ///< Timer used to implement the autorepeating property.
392
393   // Toggle
394   bool  mToggled;                   ///< Stores the toggle state.
395
396   // Signals
397   Toolkit::PushButton::PressedSignalV2 mPressedSignalV2;   ///< Signal emitted when the button is pressed.
398   Toolkit::PushButton::ReleasedSignalV2 mReleasedSignalV2; ///< Signal emitted when the button is released.
399
400   Actor mButtonImage;               ///< Stores the released image.
401   Actor mBackgroundImage;           ///< Stores the background image.
402   Actor mPressedImage;              ///< Stores the pressed image.
403   Actor mDimmedImage;               ///< Stores the dimmed image.
404   Actor mDimmedBackgroundImage;     ///< Stores the dimmed background image.
405
406   Actor mLabel;                     ///< Stores the text label.
407
408   Actor mFadeOutBackgroundImage;    ///< Stores a background image, which is in a fade out animation, to be removed when the animation finishes.
409   Actor mFadeOutButtonImage;        ///< Stores a foreground image, which is in a fade out animation, to be removed when the animation finishes.
410
411   // Actions
412   bool mClickActionPerforming;
413 };
414
415 } // namespace Internal
416
417 // Helpers for public-api forwarding methods
418
419 inline Toolkit::Internal::PushButton& GetImplementation( Toolkit::PushButton& button )
420 {
421   DALI_ASSERT_ALWAYS( button );
422
423   Dali::RefObject& handle = button.GetImplementation();
424
425   return static_cast<Toolkit::Internal::PushButton&>( handle );
426 }
427
428 inline const Toolkit::Internal::PushButton& GetImplementation( const Toolkit::PushButton& button )
429 {
430   DALI_ASSERT_ALWAYS( button );
431
432   const Dali::RefObject& handle = button.GetImplementation();
433
434   return static_cast<const Toolkit::Internal::PushButton&>( handle );
435 }
436
437 } // namespace Toolkit
438
439 } // namespace Dali
440
441 #endif // __DALI_TOOLKIT_INTERNAL_PUSH_BUTTON_H__