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