[dali_1.0.1] Merge branch 'tizen'
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / scrollable / scrollable-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 // INTERNAL INCLUDES
19 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
20 #include <dali-toolkit/internal/controls/scroll-component/scroll-bar-internal-impl.h>
21
22 using namespace Dali;
23
24 namespace
25 {
26
27 } // unnamed namespace
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 const Property::Index Scrollable::PROPERTY_OVERSHOOT_EFFECT_COLOR( Toolkit::Internal::Control::CONTROL_PROPERTY_END_INDEX + 1 );
36
37 namespace Internal
38 {
39
40 namespace
41 {
42 const Vector4 DEFAULT_OVERSHOOT_COLOUR(0.0f, 0.64f, 0.85f, 0.25f);
43
44 BaseHandle Create()
45 {
46   // empty handle as we cannot create Scrollable (but type registered for scroll signal)
47   return BaseHandle();
48 }
49
50 TypeRegistration mType( typeid(Toolkit::Scrollable), typeid(Toolkit::Control), Create );
51
52 SignalConnectorType s1(mType, Toolkit::Scrollable::SIGNAL_SCROLL_STARTED,   &Scrollable::DoConnectSignal);
53 SignalConnectorType s2(mType, Toolkit::Scrollable::SIGNAL_SCROLL_COMPLETED, &Scrollable::DoConnectSignal);
54 SignalConnectorType s3(mType, Toolkit::Scrollable::SIGNAL_SCROLL_UPDATED,   &Scrollable::DoConnectSignal);
55 SignalConnectorType s4(mType, Toolkit::Scrollable::SIGNAL_SCROLL_CLAMPED,   &Scrollable::DoConnectSignal);
56
57 PropertyRegistration property1( mType,
58                                 "overshoot-effect-color",
59                                 Toolkit::Scrollable::PROPERTY_OVERSHOOT_EFFECT_COLOR,
60                                 Property::VECTOR4,
61                                 &Scrollable::SetProperty,
62                                 &Scrollable::GetProperty );
63
64 }
65
66 const std::string Scrollable::SCROLLABLE_CAN_SCROLL_VERTICAL( "scrollable-can-scroll-vertical" );
67 const std::string Scrollable::SCROLLABLE_CAN_SCROLL_HORIZONTAL( "scrollable-can-scroll-horizontal" );
68
69 ///////////////////////////////////////////////////////////////////////////////////////////////////
70 // Scrollable
71 ///////////////////////////////////////////////////////////////////////////////////////////////////
72
73 // Scrollable controls are not layout containers so they dont need size negotiation..
74 // we dont want size negotiation while scrolling if we can avoid it
75 Scrollable::Scrollable()
76 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS | NO_SIZE_NEGOTIATION ) ),
77   mOvershootEffectColor(  DEFAULT_OVERSHOOT_COLOUR ),
78   mPropertyRelativePosition(Property::INVALID_INDEX),
79   mPropertyPositionMin(Property::INVALID_INDEX),
80   mPropertyPositionMax(Property::INVALID_INDEX),
81   mPropertyScrollDirection(Property::INVALID_INDEX),
82   mPropertyCanScrollVertical(Property::INVALID_INDEX),
83   mPropertyCanScrollHorizontal(Property::INVALID_INDEX),
84   mOvershootEnabled(false)
85 {
86 }
87
88 Scrollable::~Scrollable()
89 {
90   // Clear scroll components, forces their destruction before Scrollable is destroyed.
91   mComponents.clear();
92 }
93
94 void Scrollable::RegisterCommonProperties()
95 {
96   Actor self = Self();
97
98   // Register properties.
99   mPropertyRelativePosition = self.RegisterProperty(Toolkit::Scrollable::SCROLL_RELATIVE_POSITION_PROPERTY_NAME, Vector3::ZERO);
100   mPropertyPositionMin = self.RegisterProperty(Toolkit::Scrollable::SCROLL_POSITION_MIN_PROPERTY_NAME, Vector3::ZERO);
101   mPropertyPositionMax = self.RegisterProperty(Toolkit::Scrollable::SCROLL_POSITION_MAX_PROPERTY_NAME, Vector3::ZERO);
102   mPropertyScrollDirection = self.RegisterProperty(Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME, Vector3::ZERO);
103   mPropertyCanScrollVertical = self.RegisterProperty(SCROLLABLE_CAN_SCROLL_VERTICAL, true);
104   mPropertyCanScrollHorizontal = self.RegisterProperty(SCROLLABLE_CAN_SCROLL_HORIZONTAL, true);
105 }
106
107 bool Scrollable::IsScrollComponentEnabled(Toolkit::Scrollable::ScrollComponentType type) const
108 {
109   if(type == Toolkit::Scrollable::OvershootIndicator)
110   {
111     return mOvershootEnabled;
112   }
113   return (mComponents.find(type) != mComponents.end());
114 }
115
116 void Scrollable::EnableScrollComponent(Toolkit::Scrollable::ScrollComponentType type)
117 {
118   if(type == Toolkit::Scrollable::OvershootIndicator)
119   {
120     if( !mOvershootEnabled )
121     {
122       SetOvershootEnabled(true);
123       mOvershootEnabled = true;
124     }
125     return;
126   }
127   if( mComponents.find(type) == mComponents.end() )
128   {
129     // Create ScrollComponent
130     Toolkit::Scrollable scrollable = Toolkit::Scrollable::DownCast(Self());
131     Toolkit::ScrollComponent scrollComponent = NewScrollComponent(scrollable, type);
132     Toolkit::ScrollComponentImpl& component = static_cast<Toolkit::ScrollComponentImpl&>(scrollComponent.GetImplementation());
133     ScrollComponentPtr componentPtr(&component);
134
135     mComponents[type] = componentPtr;
136   }
137 }
138
139 void Scrollable::DisableScrollComponent(Toolkit::Scrollable::ScrollComponentType type)
140 {
141   if(type == Toolkit::Scrollable::OvershootIndicator)
142   {
143     if( mOvershootEnabled )
144     {
145       SetOvershootEnabled(false);
146       mOvershootEnabled = false;
147     }
148     return;
149   }
150   ComponentIter pair = mComponents.find( type );
151
152   if( mComponents.end() != pair )
153   {
154     ScrollComponentPtr component = pair->second;
155
156     // Disconnect the scroll component first.
157     component->OnDisconnect();
158
159     // Destroy ScrollComponent.
160     mComponents.erase( type );
161   }
162 }
163
164 Vector4 Scrollable::GetOvershootEffectColor() const
165 {
166   return mOvershootEffectColor;
167 };
168
169 Toolkit::Scrollable::ScrollStartedSignalV2& Scrollable::ScrollStartedSignal()
170 {
171   return mScrollStartedSignalV2;
172 }
173
174 Toolkit::Scrollable::ScrollUpdatedSignalV2& Scrollable::ScrollUpdatedSignal()
175 {
176   return mScrollUpdatedSignalV2;
177 }
178
179 Toolkit::Scrollable::ScrollCompletedSignalV2& Scrollable::ScrollCompletedSignal()
180 {
181   return mScrollCompletedSignalV2;
182 }
183
184 Toolkit::Scrollable::ScrollClampedSignalV2& Scrollable::ScrollClampedSignal()
185 {
186   return mScrollClampedSignalV2;
187 }
188
189 bool Scrollable::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
190 {
191   Dali::BaseHandle handle( object );
192
193   bool connected( true );
194   Toolkit::Scrollable scrollable = Toolkit::Scrollable::DownCast( handle );
195
196   if( Toolkit::Scrollable::SIGNAL_SCROLL_STARTED == signalName )
197   {
198     scrollable.ScrollStartedSignal().Connect( tracker, functor );
199   }
200   else if( Toolkit::Scrollable::SIGNAL_SCROLL_UPDATED == signalName )
201   {
202     scrollable.ScrollUpdatedSignal().Connect( tracker, functor );
203   }
204   else if( Toolkit::Scrollable::SIGNAL_SCROLL_COMPLETED == signalName )
205   {
206     scrollable.ScrollCompletedSignal().Connect( tracker, functor );
207   }
208   else if( Toolkit::Scrollable::SIGNAL_SCROLL_CLAMPED == signalName )
209   {
210     scrollable.ScrollClampedSignal().Connect( tracker, functor );
211   }
212   else
213   {
214     // signalName does not match any signal
215     connected = false;
216   }
217
218   return connected;
219 }
220
221 void Scrollable::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
222 {
223   Toolkit::Scrollable scrollable = Toolkit::Scrollable::DownCast( Dali::BaseHandle( object ) );
224
225   if( scrollable )
226   {
227     Scrollable& scrollableImpl( GetImpl( scrollable ) );
228     switch( index )
229     {
230       case Toolkit::Scrollable::PROPERTY_OVERSHOOT_EFFECT_COLOR:
231       {
232         scrollableImpl.SetOvershootEffectColor( value.Get<Vector4>() );
233         break;
234       }
235     }
236   }
237 }
238
239 Property::Value Scrollable::GetProperty( BaseObject* object, Property::Index index )
240 {
241   Property::Value value;
242
243   Toolkit::Scrollable scrollable = Toolkit::Scrollable::DownCast( Dali::BaseHandle( object ) );
244
245   if( scrollable )
246   {
247     Scrollable& scrollableImpl( GetImpl( scrollable ) );
248     switch( index )
249     {
250       case Toolkit::Scrollable::PROPERTY_OVERSHOOT_EFFECT_COLOR:
251       {
252         value = scrollableImpl.GetOvershootEffectColor();
253         break;
254       }
255     }
256   }
257
258   return value;
259 }
260
261 Toolkit::ScrollComponent Scrollable::NewScrollComponent(Toolkit::Scrollable& scrollable, Toolkit::Scrollable::ScrollComponentType type)
262 {
263   Toolkit::ScrollComponent instance;
264
265   switch(type)
266   {
267     case Toolkit::Scrollable::VerticalScrollBar:
268     {
269       instance = static_cast<Toolkit::ScrollComponent>(Toolkit::ScrollBarInternal::New(scrollable, true));
270       break;
271     }
272     case Toolkit::Scrollable::HorizontalScrollBar:
273     {
274       instance = static_cast<Toolkit::ScrollComponent>(Toolkit::ScrollBarInternal::New(scrollable, false));
275       break;
276     }
277     default:
278     {
279       DALI_ASSERT_ALWAYS(true && "Unrecognized component type");
280     }
281   }
282
283   return instance;
284 }
285
286 } // namespace Internal
287
288 } // namespace Toolkit
289
290 } // namespace Dali