Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / public-api / controls / scrollable / scroll-view / scroll-view.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 #include <dali-toolkit/public-api/controls/scrollable/scrollable.h>
18 #include <dali-toolkit/public-api/controls/scrollable/scroll-view/scroll-view.h>
19 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.h>
20
21 using namespace Dali;
22
23 namespace Dali
24 {
25
26 namespace Toolkit
27 {
28
29 ///////////////////////////////////////////////////////////////////////////////////////////////////
30 // RulerDomain
31 ///////////////////////////////////////////////////////////////////////////////////////////////////
32
33 RulerDomain::RulerDomain(float min, float max, bool enabled)
34 : min(min),
35   max(max),
36   enabled(enabled)
37 {
38 }
39
40 float RulerDomain::Clamp(float x, float length, float scale) const
41 {
42   ClampState clamped;
43
44   return Clamp(x, length, scale, clamped);
45 }
46
47 float RulerDomain::Clamp(float x, float length, float scale, ClampState &clamped) const
48 {
49   if(!enabled)
50   {
51     clamped = NotClamped;
52     return x;
53   }
54
55   const float minExtent = min * scale;
56   const float maxExtent = max * scale - length;
57   if(x < minExtent)
58   {
59     clamped = ClampedToMin;
60     return minExtent;
61   }
62   else if(x > maxExtent)
63   {
64     clamped = ClampedToMax;
65     return maxExtent;
66   }
67
68   clamped = NotClamped;
69   return x;
70 }
71
72 float RulerDomain::GetSize() const
73 {
74   return max-min;
75 }
76
77 ///////////////////////////////////////////////////////////////////////////////////////////////////
78 // Ruler
79 ///////////////////////////////////////////////////////////////////////////////////////////////////
80
81 Ruler::Ruler()
82 : mType(Free),
83   mEnabled(true),
84   mDomain(RulerDomain(0.0f,1.0f,false))
85 {
86 }
87
88 Ruler::~Ruler()
89 {
90 }
91
92 Ruler::RulerType Ruler::GetType() const
93 {
94   return mType;
95 }
96
97 bool Ruler::IsEnabled() const
98 {
99   return mEnabled;
100 }
101
102 void Ruler::Enable()
103 {
104   mEnabled = true;
105 }
106
107 void Ruler::Disable()
108 {
109   mEnabled = false;
110 }
111
112 void Ruler::SetDomain(RulerDomain domain)
113 {
114   mDomain = domain;
115 }
116
117 const RulerDomain &Ruler::GetDomain() const
118 {
119   return mDomain;
120 }
121
122 void Ruler::DisableDomain()
123 {
124   mDomain = RulerDomain(0.0f,1.0f,false);
125 }
126
127 float Ruler::Clamp(float x, float length, float scale) const
128 {
129   return mDomain.Clamp(x, length, scale);
130 }
131
132 float Ruler::Clamp(float x, float length, float scale, ClampState &clamped) const
133 {
134   return mDomain.Clamp(x, length, scale, clamped);
135 }
136
137 float Ruler::SnapAndClamp(float x, float bias, float length, float scale) const
138 {
139   return Clamp(Snap(x, bias), length, scale);
140 }
141
142 float Ruler::SnapAndClamp(float x, float bias, float length, float scale, ClampState &clamped) const
143 {
144   return Clamp(Snap(x, bias), length, scale, clamped);
145 }
146
147 ///////////////////////////////////////////////////////////////////////////////////////////////////
148 // DefaultRuler
149 ///////////////////////////////////////////////////////////////////////////////////////////////////
150
151 DefaultRuler::DefaultRuler()
152 {
153   mType = Free;
154 }
155
156 float DefaultRuler::Snap(float x, float bias) const
157 {
158   return x;
159 }
160
161 float DefaultRuler::GetPositionFromPage(unsigned int page, unsigned int &volume, bool wrap) const
162 {
163   volume = 0;
164   return 0.0f;
165 }
166
167 unsigned int DefaultRuler::GetPageFromPosition(float position, bool wrap) const
168 {
169   return 0;
170 }
171
172 unsigned int DefaultRuler::GetTotalPages() const
173 {
174   return 1;
175 }
176
177 ///////////////////////////////////////////////////////////////////////////////////////////////////
178 // FixedRuler
179 ///////////////////////////////////////////////////////////////////////////////////////////////////
180
181 FixedRuler::FixedRuler(float spacing)
182 : mSpacing(spacing)
183 {
184   mType = Fixed;
185 }
186
187 float FixedRuler::Snap(float x, float bias) const
188 {
189   return floor(x / mSpacing + bias) * mSpacing;
190 }
191
192 float FixedRuler::GetPositionFromPage(unsigned int page, unsigned int &volume, bool wrap) const
193 {
194   float position = mDomain.min;
195
196   volume = 0;
197
198   // spacing must be present.
199   if(mEnabled && fabsf(mSpacing) > Math::MACHINE_EPSILON_1)
200   {
201     unsigned int column = page;
202
203     // In carry mode, a volume (carry) is produced when page exceeds limit within domain
204     if(wrap)
205     {
206       unsigned int pagesPerVolume = mDomain.GetSize() / mSpacing;
207       if(pagesPerVolume>0)
208       {
209         column += pagesPerVolume;
210         column %= pagesPerVolume;
211         volume = page/pagesPerVolume;
212       }
213     }
214
215     position = mDomain.min + column * mSpacing;
216   }
217   else  // Domain (or Spacing) is not present, carry page to volume.
218   {
219     if(wrap)
220     {
221       volume = page;
222     }
223   }
224
225   return position;
226 }
227
228 unsigned int FixedRuler::GetPageFromPosition(float position, bool wrap) const
229 {
230   unsigned int page = 0;
231
232   // spacing must be present.
233   if(mEnabled && fabsf(mSpacing) > Math::MACHINE_EPSILON_1)
234   {
235     page = floor((position - mDomain.min) / mSpacing + 0.5f);
236
237     if(wrap)
238     {
239       unsigned int pagesPerVolume = mDomain.GetSize() / mSpacing;
240       page %= pagesPerVolume;
241     }
242   }
243
244   return page;
245 }
246
247 unsigned int FixedRuler::GetTotalPages() const
248 {
249   unsigned int pagesPerVolume = 1;
250
251   // spacing must be present.
252   if(mEnabled && fabsf(mSpacing) > Math::MACHINE_EPSILON_1)
253   {
254     pagesPerVolume = mDomain.GetSize() / mSpacing;
255   }
256
257   return pagesPerVolume;
258 }
259
260 ///////////////////////////////////////////////////////////////////////////////////////////////////
261 // ScrollView
262 ///////////////////////////////////////////////////////////////////////////////////////////////////
263
264 const std::string ScrollView::SCROLL_PAGE_CURRENT( "scroll-page-current" );
265 const std::string ScrollView::SCROLL_TIME_PROPERTY_NAME( "scroll-time" );
266 const std::string ScrollView::SCROLL_POSITION_PROPERTY_NAME( "scroll-position" );
267 const std::string ScrollView::SCROLL_PRE_POSITION_PROPERTY_NAME( "scroll-pre-position" );
268 const std::string ScrollView::SCROLL_OVERSHOOT_X_PROPERTY_NAME( "scroll-overshoot-x" );
269 const std::string ScrollView::SCROLL_OVERSHOOT_Y_PROPERTY_NAME( "scroll-overshoot-y" );
270 const std::string ScrollView::SCROLL_FINAL_PROPERTY_NAME( "scroll-final" );
271 const std::string ScrollView::SCROLL_X_PROPERTY_NAME( "scroll-x" );
272 const std::string ScrollView::SCROLL_Y_PROPERTY_NAME( "scroll-y" );
273 const std::string ScrollView::SCROLL_SCALE_PROPERTY_NAME( "scroll-scale" );
274 const std::string ScrollView::SCROLL_WRAP_PROPERTY_NAME( "scroll-wrap" );
275 const std::string ScrollView::SCROLL_PANNING_PROPERTY_NAME( "scroll-panning" );
276 const std::string ScrollView::SCROLL_SCROLLING_PROPERTY_NAME( "scroll-scrolling" );
277 const std::string ScrollView::SCROLL_POSITION_DELTA_PROPERTY_NAME( "scroll-position-delta" );
278 const std::string ScrollView::SCROLL_START_PAGE_POSITION_PROPERTY_NAME( "scroll-start-page-position" );
279
280 const float ScrollView::DEFAULT_SLOW_SNAP_ANIMATION_DURATION(0.5f);
281 const float ScrollView::DEFAULT_FAST_SNAP_ANIMATION_DURATION(0.25f);
282 const float ScrollView::DEFAULT_SNAP_OVERSHOOT_DURATION(1.0f);
283 const float ScrollView::DEFAULT_MAX_OVERSHOOT(100.0f);  // 100 pixels
284
285 const float ScrollView::DEFAULT_AXIS_AUTO_LOCK_GRADIENT(0.36f);
286 const float ScrollView::DEFAULT_FRICTION_COEFFICIENT(1.0f);
287 const float ScrollView::DEFAULT_FLICK_SPEED_COEFFICIENT(1.0f);
288 const float ScrollView::DEFAULT_MAX_FLICK_SPEED(3.0f);
289
290 const char* const ScrollView::SIGNAL_SNAP_STARTED = "snap-started";
291
292 ScrollView::ScrollView()
293 {
294 }
295
296 ScrollView::ScrollView(Internal::ScrollView& implementation)
297 : Scrollable(implementation)
298 {
299 }
300
301 ScrollView::ScrollView( Dali::Internal::CustomActor* internal )
302 : Scrollable( internal )
303 {
304   VerifyCustomActorPointer<Internal::ScrollView>(internal);
305 }
306
307 ScrollView::ScrollView( const ScrollView& handle )
308 : Scrollable( handle )
309 {
310 }
311
312 ScrollView& ScrollView::operator=( const ScrollView& handle )
313 {
314   if( &handle != this )
315   {
316     Control::operator=( handle );
317   }
318   return *this;
319 }
320
321 ScrollView ScrollView::New()
322 {
323   return Internal::ScrollView::New();
324 }
325
326 ScrollView::~ScrollView()
327 {
328 }
329
330 ScrollView ScrollView::DownCast( BaseHandle handle )
331 {
332   return Control::DownCast<ScrollView, Internal::ScrollView>(handle);
333 }
334
335 AlphaFunction ScrollView::GetScrollSnapAlphaFunction() const
336 {
337   return GetImpl(*this).GetScrollSnapAlphaFunction();
338 }
339
340 void ScrollView::SetScrollSnapAlphaFunction(AlphaFunction alpha)
341 {
342   GetImpl(*this).SetScrollSnapAlphaFunction(alpha);
343 }
344
345 AlphaFunction ScrollView::GetScrollFlickAlphaFunction() const
346 {
347   return GetImpl(*this).GetScrollFlickAlphaFunction();
348 }
349
350 void ScrollView::SetScrollFlickAlphaFunction(AlphaFunction alpha)
351 {
352   GetImpl(*this).SetScrollFlickAlphaFunction(alpha);
353 }
354
355 float ScrollView::GetScrollSnapDuration() const
356 {
357   return GetImpl(*this).GetScrollSnapDuration();
358 }
359
360 void ScrollView::SetScrollSnapDuration(float time)
361 {
362   GetImpl(*this).SetScrollSnapDuration(time);
363 }
364
365 float ScrollView::GetScrollFlickDuration() const
366 {
367   return GetImpl(*this).GetScrollFlickDuration();
368 }
369
370 void ScrollView::SetScrollFlickDuration(float time)
371 {
372   GetImpl(*this).SetScrollFlickDuration(time);
373 }
374
375 void ScrollView::SetRulerX(RulerPtr ruler)
376 {
377   GetImpl(*this).SetRulerX(ruler);
378 }
379
380 void ScrollView::SetRulerY(RulerPtr ruler)
381 {
382   GetImpl(*this).SetRulerY(ruler);
383 }
384
385 void ScrollView::SetRulerScaleX(RulerPtr ruler)
386 {
387   GetImpl(*this).SetRulerScaleX(ruler);
388 }
389
390 void ScrollView::SetRulerScaleY(RulerPtr ruler)
391 {
392   GetImpl(*this).SetRulerScaleY(ruler);
393 }
394
395 void ScrollView::SetScrollSensitive(bool sensitive)
396 {
397   GetImpl(*this).SetScrollSensitive(sensitive);
398 }
399
400 void ScrollView::SetMaxOvershoot(float overshootX, float overshootY)
401 {
402   GetImpl(*this).SetMaxOvershoot(overshootX, overshootY);
403 }
404
405 void ScrollView::SetSnapOvershootAlphaFunction(AlphaFunction alpha)
406 {
407   GetImpl(*this).SetSnapOvershootAlphaFunction(alpha);
408 }
409
410 void ScrollView::SetSnapOvershootDuration(float duration)
411 {
412   GetImpl(*this).SetSnapOvershootDuration(duration);
413 }
414
415 void ScrollView::SetTouchesRequiredForPanning(unsigned int minTouches, unsigned int maxTouches, bool endOutside)
416 {
417   GetImpl(*this).SetTouchesRequiredForPanning(minTouches, maxTouches, endOutside);
418 }
419
420 void ScrollView::SetActorAutoSnap(bool enable)
421 {
422   GetImpl(*this).SetActorAutoSnap(enable);
423 }
424
425 void ScrollView::SetWrapMode(bool enable)
426 {
427   GetImpl(*this).SetWrapMode(enable);
428 }
429
430 int ScrollView::GetRefreshInterval() const
431 {
432   return GetImpl(*this).GetRefreshInterval();
433 }
434
435 void ScrollView::SetRefreshInterval(int milliseconds)
436 {
437   GetImpl(*this).SetRefreshInterval(milliseconds);
438 }
439
440 bool ScrollView::GetAxisAutoLock() const
441 {
442   return GetImpl(*this).GetAxisAutoLock();
443 }
444
445 void ScrollView::SetAxisAutoLock(bool enable)
446 {
447   GetImpl(*this).SetAxisAutoLock(enable);
448 }
449
450 float ScrollView::GetAxisAutoLockGradient() const
451 {
452   return GetImpl(*this).GetAxisAutoLockGradient();
453 }
454
455 void ScrollView::SetAxisAutoLockGradient(float gradient)
456 {
457   GetImpl(*this).SetAxisAutoLockGradient(gradient);
458 }
459
460 float ScrollView::GetFrictionCoefficient() const
461 {
462   return GetImpl(*this).GetFrictionCoefficient();
463 }
464
465 void ScrollView::SetFrictionCoefficient(float friction)
466 {
467   GetImpl(*this).SetFrictionCoefficient(friction);
468 }
469
470 float ScrollView::GetFlickSpeedCoefficient() const
471 {
472   return GetImpl(*this).GetFlickSpeedCoefficient();
473 }
474
475 void ScrollView::SetFlickSpeedCoefficient(float speed)
476 {
477   GetImpl(*this).SetFlickSpeedCoefficient(speed);
478 }
479
480 float ScrollView::GetMaxFlickSpeed() const
481 {
482   return GetImpl(*this).GetMaxFlickSpeed();
483 }
484
485 void ScrollView::SetMaxFlickSpeed(float speed)
486 {
487   GetImpl(*this).SetMaxFlickSpeed(speed);
488 }
489
490 Vector2 ScrollView::GetMouseWheelScrollDistanceStep() const
491 {
492   return GetImpl(*this).GetMouseWheelScrollDistanceStep();
493 }
494
495 void ScrollView::SetMouseWheelScrollDistanceStep(Vector2 step)
496 {
497   GetImpl(*this).SetMouseWheelScrollDistanceStep(step);
498 }
499
500 Vector3 ScrollView::GetCurrentScrollPosition() const
501 {
502   return GetImpl(*this).GetCurrentScrollPosition();
503 }
504
505 Vector3 ScrollView::GetCurrentScrollScale() const
506 {
507   return GetImpl(*this).GetCurrentScrollScale();
508 }
509
510 unsigned int ScrollView::GetCurrentPage() const
511 {
512   return GetImpl(*this).GetCurrentPage();
513 }
514
515 void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation)
516 {
517   GetImpl(*this).TransformTo(position, scale, rotation);
518 }
519
520 void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation, float duration)
521 {
522   GetImpl(*this).TransformTo(position, scale, rotation, duration);
523 }
524
525 void ScrollView::ScrollTo(const Vector3 &position)
526 {
527   GetImpl(*this).ScrollTo(position);
528 }
529
530 void ScrollView::ScrollTo(const Vector3 &position, float duration)
531 {
532   GetImpl(*this).ScrollTo(position, duration);
533 }
534
535 void ScrollView::ScrollTo(const Vector3 &position, float duration,
536                           DirectionBias horizontalBias, DirectionBias verticalBias)
537 {
538   GetImpl(*this).ScrollTo(position, duration, horizontalBias, verticalBias);
539 }
540
541 void ScrollView::ScrollTo(unsigned int page)
542 {
543   GetImpl(*this).ScrollTo(page);
544 }
545
546 void ScrollView::ScrollTo(unsigned int page, float duration)
547 {
548   GetImpl(*this).ScrollTo(page, duration);
549 }
550
551 void ScrollView::ScrollTo(unsigned int page, float duration, DirectionBias bias)
552 {
553   GetImpl(*this).ScrollTo(page, duration, bias);
554 }
555
556 void ScrollView::ScrollTo(Actor &actor)
557 {
558   GetImpl(*this).ScrollTo(actor);
559 }
560
561 void ScrollView::ScrollTo(Actor &actor, float duration)
562 {
563   GetImpl(*this).ScrollTo(actor, duration);
564 }
565
566 bool ScrollView::ScrollToSnapPoint()
567 {
568   return GetImpl(*this).ScrollToSnapPoint();
569 }
570
571 void ScrollView::ScaleTo(const Vector3 &scale)
572 {
573   GetImpl(*this).ScaleTo(scale);
574 }
575
576 void ScrollView::ScaleTo(const Vector3 &scale, float duration)
577 {
578   GetImpl(*this).ScaleTo(scale, duration);
579 }
580
581 void ScrollView::ApplyConstraintToChildren(Constraint constraint)
582 {
583   GetImpl(*this).ApplyConstraintToChildren(constraint);
584 }
585
586 void ScrollView::RemoveConstraintsFromChildren()
587 {
588   GetImpl(*this).RemoveConstraintsFromChildren();
589 }
590
591 void ScrollView::ApplyEffect(ScrollViewEffect effect)
592 {
593   GetImpl(*this).ApplyEffect(effect);
594 }
595
596 ScrollViewEffect ScrollView::ApplyEffect(ScrollView::PageEffect effect)
597 {
598   return GetImpl(*this).ApplyEffect(effect);
599 }
600
601 void ScrollView::RemoveEffect(ScrollViewEffect effect)
602 {
603   GetImpl(*this).RemoveEffect(effect);
604 }
605
606 void ScrollView::RemoveAllEffects()
607 {
608   GetImpl(*this).RemoveAllEffects();
609 }
610
611 void ScrollView::BindActor(Actor child)
612 {
613   GetImpl(*this).BindActor(child);
614 }
615
616 void ScrollView::UnbindActor(Actor child)
617 {
618   GetImpl(*this).UnbindActor(child);
619 }
620
621 ScrollView::SnapStartedSignalV2& ScrollView::SnapStartedSignal()
622 {
623   return GetImpl(*this).SnapStartedSignal();
624 }
625
626 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
627 {
628   GetImpl(*this).SetScrollingDirection( direction, threshold );
629 }
630
631 void ScrollView::RemoveScrollingDirection( Radian direction )
632 {
633   GetImpl(*this).RemoveScrollingDirection( direction );
634 }
635
636 } // namespace Toolkit
637
638 } // namespace Dali