(ScrollView)Found issue with change made in previous patch for overshoot enabling
[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 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 // INTERNAL INCLUDES
18 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
19 #include <dali-toolkit/internal/controls/scroll-component/scroll-bar-internal-impl.h>
20
21 using namespace Dali;
22
23 namespace
24 {
25
26 } // unnamed namespace
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 TypeRegistration mType( typeid(Toolkit::Scrollable), typeid(Toolkit::Control), Create );
47
48 SignalConnectorType s1(mType, Toolkit::Scrollable::SIGNAL_SCROLL_STARTED,   &Scrollable::DoConnectSignal);
49 SignalConnectorType s2(mType, Toolkit::Scrollable::SIGNAL_SCROLL_COMPLETED, &Scrollable::DoConnectSignal);
50 SignalConnectorType s3(mType, Toolkit::Scrollable::SIGNAL_SCROLL_UPDATED,   &Scrollable::DoConnectSignal);
51 SignalConnectorType s4(mType, Toolkit::Scrollable::SIGNAL_SCROLL_CLAMPED,   &Scrollable::DoConnectSignal);
52
53 }
54
55 const std::string Scrollable::SCROLLABLE_CAN_SCROLL_VERTICAL( "scrollable-can-scroll-vertical" );
56 const std::string Scrollable::SCROLLABLE_CAN_SCROLL_HORIZONTAL( "scrollable-can-scroll-horizontal" );
57
58 ///////////////////////////////////////////////////////////////////////////////////////////////////
59 // Scrollable
60 ///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 Scrollable::Scrollable()
63 : ControlImpl(true/*requires touch*/),
64   mPropertyRelativePosition(Property::INVALID_INDEX),
65   mPropertyPositionMin(Property::INVALID_INDEX),
66   mPropertyPositionMax(Property::INVALID_INDEX),
67   mPropertyScrollDirection(Property::INVALID_INDEX),
68   mOvershootEnabled(false)
69 {
70 }
71
72 Scrollable::~Scrollable()
73 {
74   // Clear scroll components, forces their destruction before Scrollable is destroyed.
75   mComponents.clear();
76 }
77
78 void Scrollable::RegisterCommonProperties()
79 {
80   Actor self = Self();
81
82   // Register properties.
83   mPropertyRelativePosition = self.RegisterProperty(Toolkit::Scrollable::SCROLL_RELATIVE_POSITION_PROPERTY_NAME, Vector3::ZERO);
84   mPropertyPositionMin = self.RegisterProperty(Toolkit::Scrollable::SCROLL_POSITION_MIN_PROPERTY_NAME, Vector3::ZERO);
85   mPropertyPositionMax = self.RegisterProperty(Toolkit::Scrollable::SCROLL_POSITION_MAX_PROPERTY_NAME, Vector3::ZERO);
86   mPropertyScrollDirection = self.RegisterProperty(Toolkit::Scrollable::SCROLL_DIRECTION_PROPERTY_NAME, Vector3::ZERO);
87   mPropertyCanScrollVertical = self.RegisterProperty(SCROLLABLE_CAN_SCROLL_VERTICAL, true);
88   mPropertyCanScrollHorizontal = self.RegisterProperty(SCROLLABLE_CAN_SCROLL_HORIZONTAL, true);
89 }
90
91 bool Scrollable::IsScrollComponentEnabled(Toolkit::Scrollable::ScrollComponentType type) const
92 {
93   if(type == Toolkit::Scrollable::OvershootIndicator)
94   {
95     return mOvershootEnabled;
96   }
97   return (mComponents.find(type) != mComponents.end());
98 }
99
100 void Scrollable::EnableScrollComponent(Toolkit::Scrollable::ScrollComponentType type)
101 {
102   if(type == Toolkit::Scrollable::OvershootIndicator)
103   {
104     if( !mOvershootEnabled )
105     {
106       SetOvershootEnabled(true);
107       mOvershootEnabled = true;
108     }
109     return;
110   }
111   if( mComponents.find(type) == mComponents.end() )
112   {
113     // Create ScrollComponent
114     Toolkit::Scrollable scrollable = Toolkit::Scrollable::DownCast(Self());
115     Toolkit::ScrollComponent scrollComponent = NewScrollComponent(scrollable, type);
116     Toolkit::ScrollComponentImpl& component = static_cast<Toolkit::ScrollComponentImpl&>(scrollComponent.GetImplementation());
117     ScrollComponentPtr componentPtr(&component);
118
119     mComponents[type] = componentPtr;
120   }
121 }
122
123 void Scrollable::DisableScrollComponent(Toolkit::Scrollable::ScrollComponentType type)
124 {
125   if(type == Toolkit::Scrollable::OvershootIndicator)
126   {
127     if( mOvershootEnabled )
128     {
129       SetOvershootEnabled(false);
130       mOvershootEnabled = false;
131     }
132     return;
133   }
134   ComponentIter pair = mComponents.find( type );
135
136   if( mComponents.end() != pair )
137   {
138     ScrollComponentPtr component = pair->second;
139
140     // Disconnect the scroll component first.
141     component->OnDisconnect();
142
143     // Destroy ScrollComponent.
144     mComponents.erase( type );
145   }
146 }
147
148 Toolkit::Scrollable::ScrollStartedSignalV2& Scrollable::ScrollStartedSignal()
149 {
150   return mScrollStartedSignalV2;
151 }
152
153 Toolkit::Scrollable::ScrollUpdatedSignalV2& Scrollable::ScrollUpdatedSignal()
154 {
155   return mScrollUpdatedSignalV2;
156 }
157
158 Toolkit::Scrollable::ScrollCompletedSignalV2& Scrollable::ScrollCompletedSignal()
159 {
160   return mScrollCompletedSignalV2;
161 }
162
163 Toolkit::Scrollable::ScrollClampedSignalV2& Scrollable::ScrollClampedSignal()
164 {
165   return mScrollClampedSignalV2;
166 }
167
168 bool Scrollable::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
169 {
170   Dali::BaseHandle handle( object );
171
172   bool connected( true );
173   Toolkit::Scrollable scrollable = Toolkit::Scrollable::DownCast( handle );
174
175   if( Toolkit::Scrollable::SIGNAL_SCROLL_STARTED == signalName )
176   {
177     scrollable.ScrollStartedSignal().Connect( tracker, functor );
178   }
179   else if( Toolkit::Scrollable::SIGNAL_SCROLL_UPDATED == signalName )
180   {
181     scrollable.ScrollUpdatedSignal().Connect( tracker, functor );
182   }
183   else if( Toolkit::Scrollable::SIGNAL_SCROLL_COMPLETED == signalName )
184   {
185     scrollable.ScrollCompletedSignal().Connect( tracker, functor );
186   }
187   else if( Toolkit::Scrollable::SIGNAL_SCROLL_CLAMPED == signalName )
188   {
189     scrollable.ScrollClampedSignal().Connect( tracker, functor );
190   }
191   else
192   {
193     // signalName does not match any signal
194     connected = false;
195   }
196
197   return connected;
198 }
199
200 Toolkit::ScrollComponent Scrollable::NewScrollComponent(Toolkit::Scrollable& scrollable, Toolkit::Scrollable::ScrollComponentType type)
201 {
202   Toolkit::ScrollComponent instance;
203
204   switch(type)
205   {
206     case Toolkit::Scrollable::VerticalScrollBar:
207     {
208       instance = static_cast<Toolkit::ScrollComponent>(Toolkit::ScrollBarInternal::New(scrollable, true));
209       break;
210     }
211     case Toolkit::Scrollable::HorizontalScrollBar:
212     {
213       instance = static_cast<Toolkit::ScrollComponent>(Toolkit::ScrollBarInternal::New(scrollable, false));
214       break;
215     }
216     default:
217     {
218       DALI_ASSERT_ALWAYS(true && "Unrecognized component type");
219     }
220   }
221
222   return instance;
223 }
224
225 } // namespace Internal
226
227 } // namespace Toolkit
228
229 } // namespace Dali