[SRUK] Initial copy from Tizen 2.2 version
[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 Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18
19 #include "button-impl.h"
20
21 namespace Dali
22 {
23
24 namespace Toolkit
25 {
26
27 namespace Internal
28 {
29
30 namespace Internal
31 {
32
33 using namespace Dali;
34
35 BaseHandle Create()
36 {
37   // empty handle as we cannot create button (but type registered for clicked signal)
38   return BaseHandle();
39 }
40
41 TypeRegistration typeRegistration( typeid(Toolkit::Button), typeid(Toolkit::Control), Create );
42
43 SignalConnectorType signalConnector1( typeRegistration, Toolkit::Button::SIGNAL_CLICKED, &Button::DoConnectSignal );
44
45 }
46
47 Button::Button()
48 : ControlImpl( true ),
49   mState( ButtonUp ),
50   mDimmed( false ),
51   mPainter( NULL )
52 {
53 }
54
55 Button::~Button()
56 {
57 }
58
59 void Button::SetDimmed( bool dimmed )
60 {
61   mDimmed = dimmed;
62
63   // Notifies the painter.
64   Toolkit::Button handle( GetOwner() );
65   if( mPainter )
66   {
67     mPainter->SetDimmed( handle, mDimmed );
68   }
69 }
70
71 bool Button::IsDimmed() const
72 {
73   return mDimmed;
74 }
75
76 void Button::SetAnimationTime( float animationTime )
77 {
78   OnAnimationTimeSet( animationTime );
79 }
80
81 float Button::GetAnimationTime() const
82 {
83   return OnAnimationTimeRequested();
84 }
85
86 void Button::OnAnimationTimeSet( float animationTime )
87 {
88   // nothing to do.
89 }
90
91 float Button::OnAnimationTimeRequested() const
92 {
93   return 0.f;
94 }
95
96 Toolkit::Button::ClickedSignalV2& Button::ClickedSignal()
97 {
98   return mClickedSignalV2;
99 }
100
101 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
102 {
103   Dali::BaseHandle handle( object );
104
105   bool connected( true );
106   Toolkit::Button button = Toolkit::Button::DownCast(handle);
107
108   if( Dali::Toolkit::Button::SIGNAL_CLICKED == signalName )
109   {
110     button.ClickedSignal().Connect( tracker, functor );
111   }
112   else
113   {
114     // signalName does not match any signal
115     connected = false;
116   }
117
118   return connected;
119 }
120
121 bool Button::OnTouchEvent(const TouchEvent& event)
122 {
123   // Only events are processed when the button is not dimmed and the touch event has only
124   // one touch point.
125   if( ( !mDimmed ) && ( 1 == event.GetPointCount() ) )
126   {
127     switch( event.GetPoint(0).state )
128     {
129       case TouchPoint::Down:
130       {
131         OnButtonDown(); // Notification for derived classes.
132
133         // Sets the button state to ButtonDown.
134         mState = ButtonDown;
135         break;
136       }
137       case TouchPoint::Up:
138       {
139         OnButtonUp(); // Notification for derived classes.
140
141         // Sets the button state to ButtonUp.
142         mState = ButtonUp;
143         break;
144       }
145       case TouchPoint::Interrupted:
146       {
147         OnTouchPointInterrupted(); // Notification for derived classes.
148
149         // Sets the button state to the default (ButtonUp).
150         mState = ButtonUp;
151         break;
152       }
153       case TouchPoint::Leave:
154       {
155         OnTouchPointLeave(); // Notification for derived classes.
156
157         // Sets the button state to the default (ButtonUp).
158         mState = ButtonUp;
159         break;
160       }
161       case TouchPoint::Motion:
162       case TouchPoint::Stationary: // FALLTHROUGH
163       {
164         // Nothing to do
165         break;
166       }
167       default:
168       {
169         DALI_ASSERT_ALWAYS( !"Point status unhandled." );
170         break;
171       }
172     }
173   }
174   else if( 1 < event.GetPointCount() )
175   {
176     OnTouchPointLeave(); // Notification for derived classes.
177
178     // Sets the button state to the default (ButtonUp).
179     mState = ButtonUp;
180   }
181
182   return false;
183 }
184
185 void Button::OnInitialize()
186 {
187   // Initialize the painter and notifies subclasses.
188   Toolkit::Button handle( GetOwner() );
189   if( mPainter )
190   {
191     mPainter->Initialize( handle );
192   }
193
194   mTapDetector = TapGestureDetector::New();
195   mTapDetector.Attach(Self());
196   mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
197
198   OnButtonInitialize();
199
200   Actor self = Self();
201   mPropertyDimmed = self.RegisterProperty( Dali::Toolkit::Button::PROPERTY_DIMMED, false, Property::READ_WRITE );
202
203   self.SetKeyboardFocusable( true );
204 }
205
206 void Button::OnControlSizeSet(const Vector3& targetSize)
207 {
208   Toolkit::Button handle( GetOwner() );
209   if( mPainter )
210   {
211     mPainter->SetSize( handle, targetSize );
212   }
213 }
214
215 void Button::OnPropertySet( Property::Index index, Property::Value propertyValue )
216 {
217   if( index == mPropertyDimmed )
218   {
219     SetDimmed(propertyValue.Get<bool>());
220   }
221 }
222
223 void Button::OnTap(Actor actor, TapGesture tap)
224 {
225   // Do nothing.
226 }
227
228 void Button::OnStageDisconnection()
229 {
230   if( ButtonUp != mState )
231   {
232     OnTouchPointLeave(); // Notification for derived classes.
233     mState = ButtonUp;
234   }
235 }
236
237 } // namespace Internal
238
239 } // namespace Toolkit
240
241 } // namespace Dali