Remove obsolete and non functional SizeChanged signal from actor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / button-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "button-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/events/touch-event.h>
23 #include <dali/public-api/object/type-registry.h>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 const Property::Index Button::PROPERTY_DISABLED                     = Internal::Button::BUTTON_PROPERTY_START_INDEX;
32 const Property::Index Button::PROPERTY_AUTO_REPEATING               = Internal::Button::BUTTON_PROPERTY_START_INDEX + 1;
33 const Property::Index Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_START_INDEX + 2;
34 const Property::Index Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY    = Internal::Button::BUTTON_PROPERTY_START_INDEX + 3;
35 const Property::Index Button::PROPERTY_TOGGLABLE                    = Internal::Button::BUTTON_PROPERTY_START_INDEX + 4;
36 const Property::Index Button::PROPERTY_TOGGLED                      = Internal::Button::BUTTON_PROPERTY_START_INDEX + 5;
37 const Property::Index Button::PROPERTY_NORMAL_STATE_ACTOR           = Internal::Button::BUTTON_PROPERTY_START_INDEX + 6;
38 const Property::Index Button::PROPERTY_SELECTED_STATE_ACTOR         = Internal::Button::BUTTON_PROPERTY_START_INDEX + 7;
39 const Property::Index Button::PROPERTY_DISABLED_STATE_ACTOR         = Internal::Button::BUTTON_PROPERTY_START_INDEX + 8;
40 const Property::Index Button::PROPERTY_LABEL_ACTOR                  = Internal::Button::BUTTON_PROPERTY_START_INDEX + 9;
41
42 namespace Internal
43 {
44
45 namespace
46 {
47
48 BaseHandle Create()
49 {
50   // empty handle as we cannot create button (but type registered for clicked signal)
51   return BaseHandle();
52 }
53
54 TypeRegistration typeRegistration( typeid(Toolkit::Button), typeid(Toolkit::Control), Create );
55
56 SignalConnectorType signalConnector1( typeRegistration, Toolkit::Button::SIGNAL_CLICKED, &Button::DoConnectSignal );
57 SignalConnectorType signalConnector2( typeRegistration, Toolkit::Button::SIGNAL_STATE_CHANGED, &Button::DoConnectSignal );
58
59 PropertyRegistration property1( typeRegistration, "disabled",                     Toolkit::Button::PROPERTY_DISABLED,                     Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
60 PropertyRegistration property2( typeRegistration, "auto-repeating",               Toolkit::Button::PROPERTY_AUTO_REPEATING,               Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
61 PropertyRegistration property3( typeRegistration, "initial-auto-repeating-delay", Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY, Property::FLOAT,   &Button::SetProperty, &Button::GetProperty );
62 PropertyRegistration property4( typeRegistration, "next-auto-repeating-delay",    Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY,    Property::FLOAT,   &Button::SetProperty, &Button::GetProperty );
63 PropertyRegistration property5( typeRegistration, "togglable",                    Toolkit::Button::PROPERTY_TOGGLABLE,                    Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
64 PropertyRegistration property6( typeRegistration, "toggled",                      Toolkit::Button::PROPERTY_TOGGLED,                      Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
65 PropertyRegistration property7( typeRegistration, "normal-state-actor",           Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR,           Property::MAP,     &Button::SetProperty, &Button::GetProperty );
66 PropertyRegistration property8( typeRegistration, "selected-state-actor",         Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR,         Property::MAP,     &Button::SetProperty, &Button::GetProperty );
67 PropertyRegistration property9( typeRegistration, "disabled-state-actor",         Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR,         Property::MAP,     &Button::SetProperty, &Button::GetProperty );
68 PropertyRegistration property10( typeRegistration, "label-actor",                 Toolkit::Button::PROPERTY_LABEL_ACTOR,                  Property::MAP,     &Button::SetProperty, &Button::GetProperty );
69
70 } // unnamed namespace
71
72 Button::Button()
73 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
74   mPainter( NULL ),
75   mState( ButtonUp ),
76   mDisabled( false )
77 {
78 }
79
80 Button::~Button()
81 {
82 }
83
84 void Button::SetDisabled( bool disabled )
85 {
86   mDisabled = disabled;
87
88   // Notifies the painter.
89   Toolkit::Button handle( GetOwner() );
90   if( mPainter )
91   {
92     mPainter->SetDisabled( handle, mDisabled );
93   }
94 }
95
96 bool Button::IsDisabled() const
97 {
98   return mDisabled;
99 }
100
101 void Button::SetAnimationTime( float animationTime )
102 {
103   OnAnimationTimeSet( animationTime );
104 }
105
106 float Button::GetAnimationTime() const
107 {
108   return OnAnimationTimeRequested();
109 }
110
111 void Button::OnAnimationTimeSet( float animationTime )
112 {
113   // nothing to do.
114 }
115
116 void Button::OnButtonDown()
117 {
118
119 }
120
121 void Button::OnButtonUp()
122 {
123
124 }
125
126 float Button::OnAnimationTimeRequested() const
127 {
128   return 0.f;
129 }
130
131 Toolkit::Button::ClickedSignalType& Button::ClickedSignal()
132 {
133   return mClickedSignal;
134 }
135
136 Toolkit::Button::StateChangedSignalType& Button::StateChangedSignal()
137 {
138   return mStateChangedSignal;
139 }
140
141 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
142 {
143   Dali::BaseHandle handle( object );
144
145   bool connected( true );
146   Toolkit::Button button = Toolkit::Button::DownCast(handle);
147
148   if( Dali::Toolkit::Button::SIGNAL_CLICKED == signalName )
149   {
150     button.ClickedSignal().Connect( tracker, functor );
151   }
152   else if( Dali::Toolkit::Button::SIGNAL_STATE_CHANGED == signalName )
153   {
154     button.StateChangedSignal().Connect( tracker, functor );
155   }
156   else
157   {
158     // signalName does not match any signal
159     connected = false;
160   }
161
162   return connected;
163 }
164
165 bool Button::OnTouchEvent(const TouchEvent& event)
166 {
167   // Only events are processed when the button is not disabled and the touch event has only
168   // one touch point.
169   if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
170   {
171     switch( event.GetPoint(0).state )
172     {
173       case TouchPoint::Down:
174       {
175         OnButtonDown(); // Notification for derived classes.
176
177         // Sets the button state to ButtonDown.
178         mState = ButtonDown;
179         break;
180       }
181       case TouchPoint::Up:
182       {
183         OnButtonUp(); // Notification for derived classes.
184
185         // Sets the button state to ButtonUp.
186         mState = ButtonUp;
187         break;
188       }
189       case TouchPoint::Interrupted:
190       {
191         OnTouchPointInterrupted(); // Notification for derived classes.
192
193         // Sets the button state to the default (ButtonUp).
194         mState = ButtonUp;
195         break;
196       }
197       case TouchPoint::Leave:
198       {
199         OnTouchPointLeave(); // Notification for derived classes.
200
201         // Sets the button state to the default (ButtonUp).
202         mState = ButtonUp;
203         break;
204       }
205       case TouchPoint::Motion:
206       case TouchPoint::Stationary: // FALLTHROUGH
207       {
208         // Nothing to do
209         break;
210       }
211       default:
212       {
213         DALI_ASSERT_ALWAYS( !"Point status unhandled." );
214         break;
215       }
216     }
217   }
218   else if( 1 < event.GetPointCount() )
219   {
220     OnTouchPointLeave(); // Notification for derived classes.
221
222     // Sets the button state to the default (ButtonUp).
223     mState = ButtonUp;
224   }
225
226   return false;
227 }
228
229 void Button::OnInitialize()
230 {
231   // Initialize the painter and notifies subclasses.
232   Toolkit::Button handle( GetOwner() );
233   if( mPainter )
234   {
235     mPainter->Initialize( handle );
236   }
237
238   Actor self = Self();
239
240   mTapDetector = TapGestureDetector::New();
241   mTapDetector.Attach( self );
242   mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
243
244   OnButtonInitialize();
245
246   self.SetKeyboardFocusable( true );
247 }
248
249 void Button::OnControlSizeSet(const Vector3& targetSize)
250 {
251   Toolkit::Button handle( GetOwner() );
252   if( mPainter )
253   {
254     mPainter->SetSize( handle, targetSize );
255   }
256 }
257
258 void Button::OnTap(Actor actor, const TapGesture& tap)
259 {
260   // Do nothing.
261 }
262
263 void Button::OnControlStageDisconnection()
264 {
265   OnButtonStageDisconnection(); // Notification for derived classes.
266   mState = ButtonUp;
267 }
268
269 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
270 {
271   Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
272
273   if ( button && ( index == Toolkit::Button::PROPERTY_DISABLED ) )
274   {
275     GetImplementation( button ).SetDisabled( value.Get<bool>() );
276   }
277 }
278
279 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
280 {
281   Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
282
283   if ( button && ( propertyIndex == Toolkit::Button::PROPERTY_DISABLED ) )
284   {
285     return Property::Value( GetImplementation( button ).mDisabled );
286   }
287
288   return Property::Value();
289 }
290
291 } // namespace Internal
292
293 } // namespace Toolkit
294
295 } // namespace Dali