Fixed various SVACE errors
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scrollable / item-view / item-view-impl.cpp
1 /*
2  * Copyright (c) 2016 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 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/scrollable/item-view/item-view-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23 #include <algorithm>
24 #include <dali/public-api/actors/layer.h>
25
26 #include <dali/public-api/animation/constraint.h>
27 #include <dali/public-api/animation/constraints.h>
28 #include <dali/devel-api/common/set-wrapper.h>
29 #include <dali/public-api/common/stage.h>
30 #include <dali/public-api/events/wheel-event.h>
31 #include <dali/public-api/events/touch-data.h>
32 #include <dali/public-api/object/type-registry.h>
33 #include <dali/public-api/object/type-registry-helper.h>
34 #include <dali/devel-api/object/property-helper-devel.h>
35
36 // INTERNAL INCLUDES
37 #include <dali-toolkit/public-api/controls/scroll-bar/scroll-bar.h>
38 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-factory.h>
39 #include <dali-toolkit/public-api/controls/scrollable/item-view/default-item-layout.h>
40 #include <dali-toolkit/devel-api/controls/scrollable/item-view/default-item-layout-property.h>
41 #include <dali-toolkit/devel-api/controls/scrollable/item-view/item-view-devel.h>
42 #include <dali-toolkit/internal/controls/scrollable/item-view/grid-layout.h>
43 #include <dali-toolkit/internal/controls/scrollable/item-view/depth-layout.h>
44 #include <dali-toolkit/internal/controls/scrollable/item-view/spiral-layout.h>
45 #include <dali-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
46
47 using std::string;
48 using std::set;
49 using namespace Dali;
50
51 namespace // Unnamed namespace
52 {
53
54 const float DEFAULT_MINIMUM_SWIPE_SPEED = 1.0f;
55 const float DEFAULT_MINIMUM_SWIPE_DISTANCE = 3.0f;
56 const float DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION = 0.1f;
57
58 const float DEFAULT_MINIMUM_SWIPE_DURATION = 0.45f;
59 const float DEFAULT_MAXIMUM_SWIPE_DURATION = 2.6f;
60
61 const float DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS = 20.0f; // 1 updates per 20 items
62 const int WHEEL_EVENT_FINISHED_TIME_OUT = 500;  // 0.5 second
63
64 const float DEFAULT_ANCHORING_DURATION = 1.0f;  // 1 second
65
66 const float MILLISECONDS_PER_SECONDS = 1000.0f;
67
68 const float OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD = 180.0f;
69 const Vector4 OVERSHOOT_OVERLAY_NINE_PATCH_BORDER(0.0f, 0.0f, 1.0f, 12.0f);
70 const float DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION = 0.2f;
71
72 /**
73  * Local helper to convert pan distance (in actor coordinates) to the layout-specific scrolling direction
74  */
75 float CalculateScrollDistance(Vector2 panDistance, Toolkit::ItemLayout& layout)
76 {
77   Radian scrollDirection(layout.GetScrollDirection());
78
79   float cosTheta = cosf(scrollDirection);
80   float sinTheta = sinf(scrollDirection);
81
82   return panDistance.x * sinTheta + panDistance.y * cosTheta;
83 }
84
85 // Overshoot overlay constraints
86 struct OvershootOverlaySizeConstraint
87 {
88   OvershootOverlaySizeConstraint( float height )
89   : mOvershootHeight( height )
90   {
91   }
92
93   void operator()( Vector3& current, const PropertyInputContainer& inputs )
94   {
95     const Vector2& parentScrollDirection = inputs[0]->GetVector2();
96     const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
97     const Vector3& parentSize = inputs[2]->GetVector3();
98
99     if(Toolkit::IsVertical(layoutOrientation))
100     {
101       current.width = fabsf(parentScrollDirection.y) > Math::MACHINE_EPSILON_1 ? parentSize.x : parentSize.y;
102     }
103     else
104     {
105       current.width = fabsf(parentScrollDirection.x) > Math::MACHINE_EPSILON_1 ? parentSize.y : parentSize.x;
106     }
107
108     current.height = ( current.width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD ) ? mOvershootHeight : mOvershootHeight*0.5f;
109   }
110
111   float mOvershootHeight;
112 };
113
114 void OvershootOverlayRotationConstraint( Quaternion& current, const PropertyInputContainer& inputs )
115 {
116   const Vector2& parentScrollDirection = inputs[0]->GetVector2();
117   const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
118   const float parentOvershoot = inputs[2]->GetFloat();
119
120   float multiplier = 0;
121   if(Toolkit::IsVertical(layoutOrientation))
122   {
123     if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
124     {
125       if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
126           || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
127       {
128         multiplier = 0.5f;
129       }
130       else
131       {
132         multiplier = 1.5f;
133       }
134     }
135     else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
136           || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
137     {
138       multiplier = 0.0f;
139     }
140     else
141     {
142       multiplier = 1.0f;
143     }
144   }
145   else
146   {
147     if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
148     {
149       if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot > Math::MACHINE_EPSILON_0)
150           ||(layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot < Math::MACHINE_EPSILON_0) )
151       {
152         multiplier = 1.0f;
153       }
154       else
155       {
156         multiplier = 0.0f;
157       }
158     }
159     else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
160           || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
161     {
162       multiplier = 1.5f;
163     }
164     else
165     {
166       multiplier = 0.5f;
167     }
168   }
169
170   current = Quaternion( Radian( multiplier * Math::PI ), Vector3::ZAXIS );
171 }
172
173 void OvershootOverlayPositionConstraint( Vector3& current, const PropertyInputContainer& inputs )
174 {
175   const Vector3& parentSize = inputs[0]->GetVector3();
176   const Vector2& parentScrollDirection = inputs[1]->GetVector2();
177   const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[2]->GetInteger());
178   const float parentOvershoot = inputs[3]->GetFloat();
179
180   Vector3 relativeOffset;
181
182   if(Toolkit::IsVertical(layoutOrientation))
183   {
184     if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
185     {
186       if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
187           || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
188       {
189         relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
190       }
191       else
192       {
193         relativeOffset =Vector3(0.0f, 1.0f, 0.0f);
194       }
195     }
196     else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
197           || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
198     {
199       relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
200     }
201     else
202     {
203       relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
204     }
205   }
206   else
207   {
208     if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
209     {
210       if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot < Math::MACHINE_EPSILON_0)
211           || (layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot > Math::MACHINE_EPSILON_0) )
212       {
213         relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
214       }
215       else
216       {
217         relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
218       }
219     }
220     else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
221           || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
222     {
223       relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
224     }
225     else
226     {
227       relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
228     }
229   }
230
231   current = relativeOffset * parentSize;
232 }
233
234 void OvershootOverlayVisibilityConstraint( bool& current, const PropertyInputContainer& inputs )
235 {
236   current = inputs[0]->GetBoolean();
237 }
238
239 } // unnamed namespace
240
241 namespace Dali
242 {
243
244 namespace Toolkit
245 {
246
247 namespace Internal
248 {
249
250 namespace // unnamed namespace
251 {
252
253 //Type registration
254
255 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ItemView, Toolkit::Scrollable, NULL)
256
257 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "minimumSwipeSpeed",          FLOAT,     MINIMUM_SWIPE_SPEED          )
258 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "minimumSwipeDistance",       FLOAT,     MINIMUM_SWIPE_DISTANCE       )
259 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "wheelScrollDistanceStep",    FLOAT,     WHEEL_SCROLL_DISTANCE_STEP   )
260 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "snapToItemEnabled",          BOOLEAN,   SNAP_TO_ITEM_ENABLED         )
261 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "refreshInterval",            FLOAT,     REFRESH_INTERVAL             )
262 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, ItemView, "layout",               ARRAY,     LAYOUT                       )
263
264
265 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "layoutPosition",      FLOAT,    LAYOUT_POSITION)
266 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollSpeed",         FLOAT,    SCROLL_SPEED)
267 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "overshoot",           FLOAT,    OVERSHOOT)
268 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollDirection",     VECTOR2,  SCROLL_DIRECTION)
269 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "layoutOrientation",   INTEGER,  LAYOUT_ORIENTATION)
270 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollContentSize",   FLOAT,    SCROLL_CONTENT_SIZE)
271
272 DALI_SIGNAL_REGISTRATION(              Toolkit, ItemView, "layoutActivated",     LAYOUT_ACTIVATED_SIGNAL )
273
274 DALI_ACTION_REGISTRATION(              Toolkit, ItemView, "stopScrolling",       ACTION_STOP_SCROLLING   )
275
276 DALI_ACTION_REGISTRATION(              Toolkit, ItemView, "enableRefresh",       ACTION_ENABLE_REFRESH   )
277 DALI_ACTION_REGISTRATION(              Toolkit, ItemView, "disableRefresh",      ACTION_DISABLE_REFRESH  )
278
279 DALI_TYPE_REGISTRATION_END()
280
281 bool FindById( const ItemContainer& items, ItemId id )
282 {
283   for( ConstItemIter iter = items.begin(); items.end() != iter; ++iter )
284   {
285     if( iter->first == id )
286     {
287       return true;
288     }
289   }
290
291   return false;
292 }
293
294 } // unnamed namespace
295
296 Dali::Toolkit::ItemView ItemView::New(ItemFactory& factory)
297 {
298   // Create the implementation
299   ItemViewPtr itemView(new ItemView(factory));
300
301   // Pass ownership to CustomActor via derived handle
302   Dali::Toolkit::ItemView handle(*itemView);
303
304   // Second-phase init of the implementation
305   // This can only be done after the CustomActor connection has been made...
306   itemView->Initialize();
307
308   return handle;
309 }
310
311 ItemView::ItemView(ItemFactory& factory)
312 : Scrollable( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | DISABLE_STYLE_CHANGE_SIGNALS | REQUIRES_WHEEL_EVENTS | REQUIRES_KEYBOARD_NAVIGATION_SUPPORT ) ),
313   mItemFactory(factory),
314   mItemsParentOrigin(ParentOrigin::CENTER),
315   mItemsAnchorPoint(AnchorPoint::CENTER),
316   mTotalPanDisplacement(Vector2::ZERO),
317   mActiveLayout(NULL),
318   mAnchoringDuration(DEFAULT_ANCHORING_DURATION),
319   mRefreshIntervalLayoutPositions(0.0f),
320   mMinimumSwipeSpeed(DEFAULT_MINIMUM_SWIPE_SPEED),
321   mMinimumSwipeDistance(DEFAULT_MINIMUM_SWIPE_DISTANCE),
322   mWheelScrollDistanceStep(0.0f),
323   mScrollDistance(0.0f),
324   mScrollSpeed(0.0f),
325   mScrollOvershoot(0.0f),
326   mGestureState(Gesture::Clear),
327   mAnimatingOvershootOn(false),
328   mAnimateOvershootOff(false),
329   mAnchoringEnabled(false),
330   mRefreshOrderHint(true/*Refresh item 0 first*/),
331   mIsFlicking(false),
332   mAddingItems(false),
333   mRefreshEnabled(true),
334   mRefreshNotificationEnabled(true),
335   mInAnimation(false)
336 {
337 }
338
339 void ItemView::OnInitialize()
340 {
341   Actor self = Self();
342
343   Vector2 stageSize = Stage::GetCurrent().GetSize();
344   mWheelScrollDistanceStep = stageSize.y * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
345
346   self.TouchSignal().Connect( this, &ItemView::OnTouch );
347   EnableGestureDetection(Gesture::Type(Gesture::Pan));
348
349   mWheelEventFinishedTimer = Timer::New( WHEEL_EVENT_FINISHED_TIME_OUT );
350   mWheelEventFinishedTimer.TickSignal().Connect( this, &ItemView::OnWheelEventFinished );
351
352   SetRefreshInterval(DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS);
353 }
354
355 ItemView::~ItemView()
356 {
357 }
358
359 unsigned int ItemView::GetLayoutCount() const
360 {
361   return mLayouts.size();
362 }
363
364 void ItemView::AddLayout(ItemLayout& layout)
365 {
366   mLayouts.push_back(ItemLayoutPtr(&layout));
367 }
368
369 void ItemView::RemoveLayout(unsigned int layoutIndex)
370 {
371   DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
372
373   if (mActiveLayout == mLayouts[layoutIndex].Get())
374   {
375     mActiveLayout = NULL;
376   }
377
378   mLayouts.erase(mLayouts.begin() + layoutIndex);
379 }
380
381 ItemLayoutPtr ItemView::GetLayout(unsigned int layoutIndex) const
382 {
383   return mLayouts[layoutIndex];
384 }
385
386 ItemLayoutPtr ItemView::GetActiveLayout() const
387 {
388   return ItemLayoutPtr(mActiveLayout);
389 }
390
391 float ItemView::GetCurrentLayoutPosition(unsigned int itemId) const
392 {
393   return Self().GetProperty<float>( Toolkit::ItemView::Property::LAYOUT_POSITION ) + static_cast<float>( itemId );
394 }
395
396 void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds)
397 {
398   DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
399
400   mRefreshEnabled = false;
401
402   Actor self = Self();
403
404   // The ItemView size should match the active layout size
405   self.SetSize(targetSize);
406   mActiveLayoutTargetSize = targetSize;
407
408   // Switch to the new layout
409   mActiveLayout = mLayouts[layoutIndex].Get();
410
411   // Move the items to the new layout positions...
412
413   for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
414   {
415     unsigned int itemId = iter->first;
416     Actor actor = iter->second;
417
418     // Remove constraints from previous layout
419     actor.RemoveConstraints();
420
421     mActiveLayout->ApplyConstraints(actor, itemId, targetSize, Self() );
422
423     Vector3 size;
424     mActiveLayout->GetItemSize( itemId, targetSize, size );
425     actor.SetSize( size.GetVectorXY() );
426   }
427
428   // Refresh the new layout
429   ItemRange range = GetItemRange(*mActiveLayout, targetSize, GetCurrentLayoutPosition(0), false/* don't reserve extra*/);
430   AddActorsWithinRange( range, targetSize );
431
432   // Scroll to an appropriate layout position
433
434   bool scrollAnimationNeeded(false);
435   float firstItemScrollPosition(0.0f);
436
437   float current = GetCurrentLayoutPosition(0);
438   float minimum = ClampFirstItemPosition(current, targetSize, *mActiveLayout);
439
440   if (current < minimum)
441   {
442     scrollAnimationNeeded = true;
443     firstItemScrollPosition = minimum;
444   }
445   else if (mAnchoringEnabled)
446   {
447     scrollAnimationNeeded = true;
448     firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(current);
449   }
450
451   if (scrollAnimationNeeded)
452   {
453     RemoveAnimation(mScrollAnimation);
454     mScrollAnimation = Animation::New(durationSeconds);
455     mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
456     mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnLayoutActivationScrollFinished);
457     mScrollAnimation.Play();
458   }
459   else
460   {
461     // Emit the layout activated signal
462     mLayoutActivatedSignal.Emit();
463   }
464
465   AnimateScrollOvershoot(0.0f);
466   mScrollOvershoot = 0.0f;
467
468   Radian scrollDirection(mActiveLayout->GetScrollDirection());
469   self.SetProperty(Toolkit::ItemView::Property::SCROLL_DIRECTION, Vector2(sinf(scrollDirection), cosf(scrollDirection)));
470   self.SetProperty(Toolkit::ItemView::Property::LAYOUT_ORIENTATION, static_cast<int>(mActiveLayout->GetOrientation()));
471   self.SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
472
473   CalculateDomainSize(targetSize);
474 }
475
476 void ItemView::DeactivateCurrentLayout()
477 {
478   if (mActiveLayout)
479   {
480     for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
481     {
482       Actor actor = iter->second;
483       actor.RemoveConstraints();
484     }
485
486     mActiveLayout = NULL;
487   }
488 }
489
490 void ItemView::OnRefreshNotification(PropertyNotification& source)
491 {
492   if( mRefreshNotificationEnabled )
493   {
494     // Cancel scroll animation to prevent any fighting of setting the scroll position property by scroll bar during fast scroll.
495     if(!mRefreshEnabled && mScrollAnimation)
496     {
497       RemoveAnimation(mScrollAnimation);
498     }
499
500     // Only cache extra items when it is not a fast scroll
501     DoRefresh(GetCurrentLayoutPosition(0), mRefreshEnabled || mScrollAnimation);
502   }
503 }
504
505 void ItemView::Refresh()
506 {
507   for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
508   {
509     ReleaseActor( iter->first, iter->second );
510   }
511   mItemPool.clear();
512
513   DoRefresh(GetCurrentLayoutPosition(0), true);
514 }
515
516 void ItemView::DoRefresh(float currentLayoutPosition, bool cacheExtra)
517 {
518   if (mActiveLayout)
519   {
520     ItemRange range = GetItemRange(*mActiveLayout, mActiveLayoutTargetSize, currentLayoutPosition, cacheExtra/*reserve extra*/);
521     RemoveActorsOutsideRange( range );
522     AddActorsWithinRange( range, Self().GetCurrentSize() );
523
524     mScrollUpdatedSignal.Emit( Vector2(0.0f, currentLayoutPosition) );
525   }
526 }
527
528 void ItemView::SetMinimumSwipeSpeed(float speed)
529 {
530   mMinimumSwipeSpeed = speed;
531 }
532
533 float ItemView::GetMinimumSwipeSpeed() const
534 {
535   return mMinimumSwipeSpeed;
536 }
537
538 void ItemView::SetMinimumSwipeDistance(float distance)
539 {
540   mMinimumSwipeDistance = distance;
541 }
542
543 float ItemView::GetMinimumSwipeDistance() const
544 {
545   return mMinimumSwipeDistance;
546 }
547
548 void ItemView::SetWheelScrollDistanceStep(float step)
549 {
550   mWheelScrollDistanceStep = step;
551 }
552
553 float ItemView::GetWheelScrollDistanceStep() const
554 {
555   return mWheelScrollDistanceStep;
556 }
557
558 void ItemView::SetAnchoring(bool enabled)
559 {
560   mAnchoringEnabled = enabled;
561 }
562
563 bool ItemView::GetAnchoring() const
564 {
565   return mAnchoringEnabled;
566 }
567
568 void ItemView::SetAnchoringDuration(float durationSeconds)
569 {
570   mAnchoringDuration = durationSeconds;
571 }
572
573 float ItemView::GetAnchoringDuration() const
574 {
575   return mAnchoringDuration;
576 }
577
578 void ItemView::SetRefreshInterval(float intervalLayoutPositions)
579 {
580   if( !Equals(mRefreshIntervalLayoutPositions, intervalLayoutPositions) )
581   {
582     mRefreshIntervalLayoutPositions = intervalLayoutPositions;
583
584     Actor self = Self();
585     if(mRefreshNotification)
586     {
587       self.RemovePropertyNotification(mRefreshNotification);
588     }
589     mRefreshNotification = self.AddPropertyNotification( Toolkit::ItemView::Property::LAYOUT_POSITION, StepCondition(mRefreshIntervalLayoutPositions, 0.0f) );
590     mRefreshNotification.NotifySignal().Connect( this, &ItemView::OnRefreshNotification );
591   }
592 }
593
594 float ItemView::GetRefreshInterval() const
595 {
596   return mRefreshIntervalLayoutPositions;
597 }
598
599 void ItemView::SetRefreshEnabled(bool enabled)
600 {
601   mRefreshEnabled = enabled;
602 }
603
604 Actor ItemView::GetItem(unsigned int itemId) const
605 {
606   Actor actor;
607
608   ConstItemPoolIter iter = mItemPool.find( itemId );
609   if( iter != mItemPool.end() )
610   {
611     actor = iter->second;
612   }
613
614   return actor;
615 }
616
617 unsigned int ItemView::GetItemId( Actor actor ) const
618 {
619   unsigned int itemId( 0 );
620
621   for ( ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
622   {
623     if( iter->second == actor )
624     {
625       itemId = iter->first;
626       break;
627     }
628   }
629
630   return itemId;
631 }
632
633 void ItemView::InsertItem( Item newItem, float durationSeconds )
634 {
635   mAddingItems = true;
636   Vector3 layoutSize = Self().GetCurrentSize();
637
638   Actor displacedActor;
639   ItemPoolIter afterDisplacedIter = mItemPool.end();
640
641   ItemPoolIter foundIter = mItemPool.find( newItem.first );
642   if( mItemPool.end() != foundIter )
643   {
644     SetupActor( newItem, layoutSize );
645     Self().Add( newItem.second );
646
647     displacedActor = foundIter->second;
648     foundIter->second = newItem.second;
649
650     afterDisplacedIter = ++foundIter;
651   }
652   else
653   {
654     // Inserting before the existing item range?
655     ItemPoolIter iter = mItemPool.begin();
656     if( iter != mItemPool.end() &&
657         iter->first > newItem.first )
658     {
659       displacedActor = iter->second;
660       mItemPool.erase( iter++ ); // iter is still valid after the erase
661
662       afterDisplacedIter = iter;
663     }
664   }
665
666   if( displacedActor )
667   {
668     // Move the existing actors to make room
669     for( ItemPoolIter iter = afterDisplacedIter; mItemPool.end() != iter; ++iter )
670     {
671       Actor temp = iter->second;
672       iter->second = displacedActor;
673       displacedActor = temp;
674
675       iter->second.RemoveConstraints();
676       mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
677     }
678
679     // Create last item
680     ItemPool::reverse_iterator lastIter = mItemPool.rbegin();
681     if ( lastIter != mItemPool.rend() )
682     {
683       ItemId lastId = lastIter->first;
684       Item lastItem( lastId + 1, displacedActor );
685       mItemPool.insert( lastItem );
686
687       lastItem.second.RemoveConstraints();
688       mActiveLayout->ApplyConstraints( lastItem.second, lastItem.first, layoutSize, Self() );
689     }
690   }
691
692   CalculateDomainSize( layoutSize );
693
694   mAddingItems = false;
695 }
696
697 void ItemView::InsertItems( const ItemContainer& newItems, float durationSeconds )
698 {
699   mAddingItems = true;
700   Vector3 layoutSize = Self().GetCurrentSize();
701
702   // Insert from lowest id to highest
703   std::set<Item> sortedItems;
704   for( ConstItemIter iter = newItems.begin(); newItems.end() != iter; ++iter )
705   {
706     sortedItems.insert( *iter );
707   }
708
709   for( std::set<Item>::iterator iter = sortedItems.begin(); sortedItems.end() != iter; ++iter )
710   {
711     Self().Add( iter->second );
712
713     ItemPoolIter foundIter = mItemPool.find( iter->first );
714     if( mItemPool.end() != foundIter )
715     {
716       Actor moveMe = foundIter->second;
717       foundIter->second = iter->second;
718
719       // Move the existing actors to make room
720       for( ItemPoolIter iter = ++foundIter; mItemPool.end() != iter; ++iter )
721       {
722         Actor temp = iter->second;
723         iter->second = moveMe;
724         moveMe = temp;
725       }
726
727       // Create last item
728       ItemId lastId = mItemPool.rbegin()->first;
729       Item lastItem( lastId + 1, moveMe );
730       mItemPool.insert( lastItem );
731     }
732     else
733     {
734       mItemPool.insert( *iter );
735     }
736   }
737
738   // Relayout everything
739   for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
740   {
741     // If newly inserted
742     if( FindById( newItems, iter->first ) )
743     {
744       SetupActor( *iter, layoutSize );
745     }
746     else
747     {
748       iter->second.RemoveConstraints();
749       mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
750     }
751   }
752
753   CalculateDomainSize( layoutSize );
754
755   mAddingItems = false;
756 }
757
758 void ItemView::RemoveItem( unsigned int itemId, float durationSeconds )
759 {
760   bool actorsReordered = RemoveActor( itemId );
761   if( actorsReordered )
762   {
763     ReapplyAllConstraints();
764
765     OnItemsRemoved();
766   }
767 }
768
769 void ItemView::RemoveItems( const ItemIdContainer& itemIds, float durationSeconds )
770 {
771   bool actorsReordered( false );
772
773   // Remove from highest id to lowest
774   set<ItemId> sortedItems;
775   for( ConstItemIdIter iter = itemIds.begin(); itemIds.end() != iter; ++iter )
776   {
777     sortedItems.insert( *iter );
778   }
779
780   for( set<ItemId>::reverse_iterator iter = sortedItems.rbegin(); sortedItems.rend() != iter; ++iter )
781   {
782     if( RemoveActor( *iter ) )
783     {
784       actorsReordered = true;
785     }
786   }
787
788   if( actorsReordered )
789   {
790     ReapplyAllConstraints();
791
792     OnItemsRemoved();
793   }
794 }
795
796 bool ItemView::RemoveActor(unsigned int itemId)
797 {
798   bool reordered( false );
799
800   ItemPoolIter removeIter = mItemPool.find( itemId );
801   if( removeIter != mItemPool.end() )
802   {
803     ReleaseActor(itemId, removeIter->second);
804   }
805   else
806   {
807     // Removing before the existing item range?
808     ItemPoolIter iter = mItemPool.begin();
809     if( iter != mItemPool.end() &&
810         iter->first > itemId )
811     {
812       // In order to decrement the first visible item ID
813       mItemPool.insert( Item(iter->first - 1, Actor()) );
814
815       removeIter = mItemPool.begin();
816     }
817   }
818
819   if( removeIter != mItemPool.end() )
820   {
821     reordered = true;
822
823     // Adjust the remaining item IDs, for example if item 2 is removed:
824     //   Initial actors:     After insert:
825     //     ID 1 - ActorA       ID 1 - ActorA
826     //     ID 2 - ActorB       ID 2 - ActorC (previously ID 3)
827     //     ID 3 - ActorC       ID 3 - ActorB (previously ID 4)
828     //     ID 4 - ActorD
829     for (ItemPoolIter iter = removeIter; iter != mItemPool.end(); ++iter)
830     {
831       if( iter->first < mItemPool.rbegin()->first )
832       {
833         iter->second = mItemPool[ iter->first + 1 ];
834       }
835       else
836       {
837         mItemPool.erase( iter );
838         break;
839       }
840     }
841   }
842
843   return reordered;
844 }
845
846 void ItemView::ReplaceItem( Item replacementItem, float durationSeconds )
847 {
848   mAddingItems = true;
849   Vector3 layoutSize = Self().GetCurrentSize();
850
851   SetupActor( replacementItem, layoutSize );
852   Self().Add( replacementItem.second );
853
854   const ItemPoolIter iter = mItemPool.find( replacementItem.first );
855   if( mItemPool.end() != iter )
856   {
857     ReleaseActor(iter->first, iter->second);
858     iter->second = replacementItem.second;
859   }
860   else
861   {
862     mItemPool.insert( replacementItem );
863   }
864
865   CalculateDomainSize( layoutSize );
866
867   mAddingItems = false;
868 }
869
870 void ItemView::ReplaceItems( const ItemContainer& replacementItems, float durationSeconds )
871 {
872   for( ConstItemIter iter = replacementItems.begin(); replacementItems.end() != iter; ++iter )
873   {
874     ReplaceItem( *iter, durationSeconds );
875   }
876 }
877
878 void ItemView::RemoveActorsOutsideRange( ItemRange range )
879 {
880   // Remove unwanted actors from the ItemView & ItemPool
881   for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); )
882   {
883     unsigned int current = iter->first;
884
885     if( ! range.Within( current ) )
886     {
887       ReleaseActor(iter->first, iter->second);
888
889       mItemPool.erase( iter++ ); // erase invalidates the return value of post-increment; iter remains valid
890     }
891     else
892     {
893       ++iter;
894     }
895   }
896 }
897
898 void ItemView::AddActorsWithinRange( ItemRange range, const Vector3& layoutSize )
899 {
900   range.end = std::min(mItemFactory.GetNumberOfItems(), range.end);
901
902   // The order of addition depends on the scroll direction.
903   if (mRefreshOrderHint)
904   {
905     for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
906     {
907       AddNewActor( itemId, layoutSize );
908     }
909   }
910   else
911   {
912     for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
913     {
914       AddNewActor( itemId-1, layoutSize );
915     }
916   }
917
918   // Total number of items may change dynamically.
919   // Always recalculate the domain size to reflect that.
920   CalculateDomainSize(Self().GetCurrentSize());
921 }
922
923 void ItemView::AddNewActor( unsigned int itemId, const Vector3& layoutSize )
924 {
925   mAddingItems = true;
926
927   if( mItemPool.end() == mItemPool.find( itemId ) )
928   {
929     Actor actor = mItemFactory.NewItem( itemId );
930
931     if( actor )
932     {
933       Item newItem( itemId, actor );
934
935       mItemPool.insert( newItem );
936
937       SetupActor( newItem, layoutSize );
938       Self().Add( actor );
939     }
940   }
941
942   mAddingItems = false;
943 }
944
945 void ItemView::SetupActor( Item item, const Vector3& layoutSize )
946 {
947   item.second.SetParentOrigin( mItemsParentOrigin );
948   item.second.SetAnchorPoint( mItemsAnchorPoint );
949
950   if( mActiveLayout )
951   {
952     Vector3 size;
953     mActiveLayout->GetItemSize( item.first, mActiveLayoutTargetSize, size );
954     item.second.SetSize( size.GetVectorXY() );
955
956     mActiveLayout->ApplyConstraints( item.second, item.first, layoutSize, Self() );
957   }
958 }
959
960 void ItemView::ReleaseActor( ItemId item, Actor actor )
961 {
962   Self().Remove( actor );
963   mItemFactory.ItemReleased(item, actor);
964 }
965
966 ItemRange ItemView::GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra)
967 {
968   unsigned int itemCount = mItemFactory.GetNumberOfItems();
969
970   ItemRange available(0u, itemCount);
971
972   ItemRange range = layout.GetItemsWithinArea( layoutPosition, layoutSize );
973
974   if (reserveExtra)
975   {
976     // Add the reserve items for scrolling
977     unsigned int extra = layout.GetReserveItemCount(layoutSize);
978     range.begin = (range.begin >= extra) ? (range.begin - extra) : 0u;
979     range.end += extra;
980   }
981
982   return range.Intersection(available);
983 }
984
985 void ItemView::OnChildAdd(Actor& child)
986 {
987   if(!mAddingItems)
988   {
989     // We don't want to do this downcast check for any item added by ItemView itself.
990     Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
991     if(scrollBar)
992     {
993       scrollBar.SetScrollPropertySource(Self(),
994                                         Toolkit::ItemView::Property::LAYOUT_POSITION,
995                                         Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
996                                         Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y,
997                                         Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE);
998     }
999   }
1000 }
1001
1002 bool ItemView::OnWheelEvent(const WheelEvent& event)
1003 {
1004   // Respond the wheel event to scroll
1005   if (mActiveLayout)
1006   {
1007     Actor self = Self();
1008     const Vector3 layoutSize = Self().GetCurrentSize();
1009     float layoutPositionDelta = GetCurrentLayoutPosition(0) - (event.z * mWheelScrollDistanceStep * mActiveLayout->GetScrollSpeedFactor());
1010     float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1011
1012     self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1013
1014     mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1015     mRefreshEnabled = true;
1016   }
1017
1018   if (mWheelEventFinishedTimer.IsRunning())
1019   {
1020     mWheelEventFinishedTimer.Stop();
1021   }
1022
1023   mWheelEventFinishedTimer.Start();
1024
1025   return true;
1026 }
1027
1028 bool ItemView::OnWheelEventFinished()
1029 {
1030   if (mActiveLayout)
1031   {
1032     RemoveAnimation(mScrollAnimation);
1033
1034     // No more wheel events coming. Do the anchoring if enabled.
1035     mScrollAnimation = DoAnchoring();
1036     if (mScrollAnimation)
1037     {
1038       mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1039       mScrollAnimation.Play();
1040     }
1041     else
1042     {
1043       mScrollOvershoot = 0.0f;
1044       AnimateScrollOvershoot(0.0f);
1045
1046       mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1047     }
1048   }
1049
1050   return false;
1051 }
1052
1053 void ItemView::ReapplyAllConstraints()
1054 {
1055   Vector3 layoutSize = Self().GetCurrentSize();
1056
1057   for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1058   {
1059     unsigned int id = iter->first;
1060     Actor actor = iter->second;
1061
1062     actor.RemoveConstraints();
1063     mActiveLayout->ApplyConstraints(actor, id, layoutSize, Self());
1064   }
1065 }
1066
1067 void ItemView::OnItemsRemoved()
1068 {
1069   CalculateDomainSize(Self().GetCurrentSize());
1070
1071   // Adjust scroll-position after an item is removed
1072   if( mActiveLayout )
1073   {
1074     float firstItemScrollPosition = ClampFirstItemPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize(), *mActiveLayout);
1075     Self().SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1076   }
1077 }
1078
1079 float ItemView::ClampFirstItemPosition( float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot )
1080 {
1081   Actor self = Self();
1082   float minLayoutPosition = layout.GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize);
1083   float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, targetPosition));
1084   self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1085
1086   if( updateOvershoot )
1087   {
1088     mScrollOvershoot = targetPosition - clamppedPosition;
1089   }
1090
1091   return clamppedPosition;
1092 }
1093
1094 bool ItemView::OnTouch( Actor actor, const TouchData& touch )
1095 {
1096   // Ignore events with multiple-touch points
1097   if (touch.GetPointCount() != 1)
1098   {
1099     return false;
1100   }
1101
1102   if ( touch.GetState( 0 ) == PointState::DOWN )
1103   {
1104     // Cancel ongoing scrolling etc.
1105     mGestureState = Gesture::Clear;
1106
1107     mScrollDistance = 0.0f;
1108     mScrollSpeed = 0.0f;
1109     Self().SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
1110
1111     mScrollOvershoot = 0.0f;
1112     AnimateScrollOvershoot(0.0f);
1113
1114     if(mScrollAnimation)
1115     {
1116       mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1117     }
1118
1119     RemoveAnimation(mScrollAnimation);
1120   }
1121
1122   return true; // consume since we're potentially scrolling
1123 }
1124
1125 void ItemView::OnPan( const PanGesture& gesture )
1126 {
1127   Actor self = Self();
1128   const Vector3 layoutSize = Self().GetCurrentSize();
1129
1130   RemoveAnimation(mScrollAnimation);
1131
1132   // Short-circuit if there is no active layout
1133   if (!mActiveLayout)
1134   {
1135     mGestureState = Gesture::Clear;
1136     return;
1137   }
1138
1139   mGestureState = gesture.state;
1140
1141   switch (mGestureState)
1142   {
1143     case Gesture::Finished:
1144     {
1145       // Swipe Detection
1146       if (fabsf(mScrollDistance) > mMinimumSwipeDistance &&
1147           mScrollSpeed > mMinimumSwipeSpeed)
1148       {
1149         float direction = (mScrollDistance < 0.0f) ? -1.0f : 1.0f;
1150
1151         mRefreshOrderHint = true;
1152
1153         float currentLayoutPosition = GetCurrentLayoutPosition(0);
1154         float firstItemScrollPosition = ClampFirstItemPosition(currentLayoutPosition + mScrollSpeed * direction,
1155                                                                layoutSize,
1156                                                                *mActiveLayout);
1157
1158         if (mAnchoringEnabled)
1159         {
1160           firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(firstItemScrollPosition);
1161         }
1162
1163         RemoveAnimation(mScrollAnimation);
1164
1165         float flickAnimationDuration = Clamp( mActiveLayout->GetItemFlickAnimationDuration() * std::max(1.0f, fabsf(firstItemScrollPosition - GetCurrentLayoutPosition(0)))
1166                                        , DEFAULT_MINIMUM_SWIPE_DURATION, DEFAULT_MAXIMUM_SWIPE_DURATION);
1167
1168         mScrollAnimation = Animation::New(flickAnimationDuration);
1169         mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION ), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1170         mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1171
1172         mIsFlicking = true;
1173         // Check whether it has already scrolled to the end
1174         if(fabs(currentLayoutPosition - firstItemScrollPosition) > Math::MACHINE_EPSILON_0)
1175         {
1176           AnimateScrollOvershoot(0.0f);
1177         }
1178       }
1179
1180       // Anchoring may be triggered when there was no swipe
1181       if (!mScrollAnimation)
1182       {
1183         mScrollAnimation = DoAnchoring();
1184       }
1185
1186       // Reset the overshoot if no scroll animation.
1187       if (!mScrollAnimation)
1188       {
1189         mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1190
1191         AnimateScrollOvershoot(0.0f, false);
1192       }
1193     }
1194     break;
1195
1196     case Gesture::Started: // Fall through
1197     {
1198       mTotalPanDisplacement = Vector2::ZERO;
1199       mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1200       mRefreshEnabled = true;
1201     }
1202
1203     case Gesture::Continuing:
1204     {
1205       mScrollDistance = CalculateScrollDistance(gesture.displacement, *mActiveLayout);
1206       mScrollSpeed = Clamp((gesture.GetSpeed() * gesture.GetSpeed() * mActiveLayout->GetFlickSpeedFactor() * MILLISECONDS_PER_SECONDS), 0.0f, mActiveLayout->GetMaximumSwipeSpeed());
1207
1208       // Refresh order depends on the direction of the scroll; negative is towards the last item.
1209       mRefreshOrderHint = mScrollDistance < 0.0f;
1210
1211       float layoutPositionDelta = GetCurrentLayoutPosition(0) + (mScrollDistance * mActiveLayout->GetScrollSpeedFactor());
1212
1213       float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1214
1215       float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1216
1217       self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1218
1219       if( ( firstItemScrollPosition >= 0.0f &&
1220             currentOvershoot < 1.0f ) ||
1221           ( firstItemScrollPosition <= mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize) &&
1222             currentOvershoot > -1.0f ) )
1223       {
1224         mTotalPanDisplacement += gesture.displacement;
1225       }
1226
1227       mScrollOvershoot = CalculateScrollOvershoot();
1228
1229       // If the view is moved in a direction against the overshoot indicator, then the indicator should be animated off.
1230       // First make sure we are not in an animation, otherwise a previously started
1231       // off-animation will be overwritten as the user continues scrolling.
1232       if( !mInAnimation )
1233       {
1234         // Check if the movement is against the current overshoot amount (if we are currently displaying the indicator).
1235         if( ( ( mScrollOvershoot > Math::MACHINE_EPSILON_0 ) && ( mScrollDistance < -Math::MACHINE_EPSILON_0 ) ) ||
1236           ( ( mScrollOvershoot < Math::MACHINE_EPSILON_0 ) && ( mScrollDistance > Math::MACHINE_EPSILON_0 ) ) )
1237         {
1238           // The user has moved against the indicator direction.
1239           // First, we reset the total displacement. This means the overshoot amount will become zero the next frame,
1240           // and if the user starts dragging in the overshoot direction again, the indicator will appear once more.
1241           mTotalPanDisplacement = Vector2::ZERO;
1242           // Animate the overshoot indicator off.
1243           AnimateScrollOvershoot( 0.0f, false );
1244         }
1245         else
1246         {
1247           // Only set the property directly if we are not animating the overshoot away,
1248           // as otherwise this will overwrite the animation generated value.
1249           self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, mScrollOvershoot );
1250         }
1251       }
1252     }
1253     break;
1254
1255     case Gesture::Cancelled:
1256     {
1257       mScrollAnimation = DoAnchoring();
1258     }
1259     break;
1260
1261     default:
1262       break;
1263   }
1264
1265   if (mScrollAnimation)
1266   {
1267     mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1268     mScrollAnimation.Play();
1269   }
1270 }
1271
1272 bool ItemView::OnAccessibilityPan(PanGesture gesture)
1273 {
1274   OnPan(gesture);
1275   return true;
1276 }
1277
1278 Actor ItemView::GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1279 {
1280   Actor nextFocusActor;
1281   if(mActiveLayout)
1282   {
1283     int nextItemID = 0;
1284     if(!actor || actor == this->Self())
1285     {
1286       nextFocusActor = GetItem(nextItemID);
1287     }
1288     else if(actor && actor.GetParent() == this->Self())
1289     {
1290       int itemID = GetItemId(actor);
1291       nextItemID = mActiveLayout->GetNextFocusItemID(itemID, mItemFactory.GetNumberOfItems(), direction, loopEnabled);
1292       nextFocusActor = GetItem(nextItemID);
1293       if(nextFocusActor == actor)
1294       {
1295         // need to pass NULL actor back to focus manager
1296         nextFocusActor.Reset();
1297         return nextFocusActor;
1298       }
1299     }
1300     float layoutPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1301     Vector3 layoutSize = Self().GetCurrentSize();
1302     if(!nextFocusActor)
1303     {
1304       // likely the current item is not buffered, so not in our item pool, probably best to get first viewable item
1305       ItemRange viewableItems = mActiveLayout->GetItemsWithinArea(layoutPosition, layoutSize);
1306       nextItemID = viewableItems.begin;
1307       nextFocusActor = GetItem(nextItemID);
1308     }
1309   }
1310   return nextFocusActor;
1311 }
1312
1313 void ItemView::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1314 {
1315   // only in this function if our chosen focus actor was actually used
1316   if(commitedFocusableActor)
1317   {
1318     int nextItemID = GetItemId(commitedFocusableActor);
1319     float layoutPosition = GetCurrentLayoutPosition(0);
1320     Vector3 layoutSize = Self().GetCurrentSize();
1321
1322     float scrollTo = mActiveLayout->GetClosestOnScreenLayoutPosition(nextItemID, layoutPosition, layoutSize);
1323     ScrollTo(Vector2(0.0f, scrollTo), DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION);
1324   }
1325 }
1326
1327 Animation ItemView::DoAnchoring()
1328 {
1329   Animation anchoringAnimation;
1330   Actor self = Self();
1331
1332   if (mActiveLayout && mAnchoringEnabled)
1333   {
1334     float anchorPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1335
1336     anchoringAnimation = Animation::New(mAnchoringDuration);
1337     anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), anchorPosition, AlphaFunction::EASE_OUT );
1338     anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1339     if(!mIsFlicking)
1340     {
1341       AnimateScrollOvershoot(0.0f);
1342     }
1343   }
1344
1345   return anchoringAnimation;
1346 }
1347
1348 void ItemView::OnScrollFinished(Animation& source)
1349 {
1350   Actor self = Self();
1351
1352   RemoveAnimation(mScrollAnimation); // mScrollAnimation is used to query whether we're scrolling
1353
1354   mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1355
1356   if(mIsFlicking && fabsf(mScrollOvershoot) > Math::MACHINE_EPSILON_1)
1357   {
1358     AnimateScrollOvershoot( mScrollOvershoot > 0.0f ? 1.0f : -1.0f, true);
1359   }
1360   else
1361   {
1362     // Reset the overshoot
1363     AnimateScrollOvershoot( 0.0f );
1364   }
1365   mIsFlicking = false;
1366
1367   mScrollOvershoot = 0.0f;
1368 }
1369
1370 void ItemView::OnLayoutActivationScrollFinished(Animation& source)
1371 {
1372   RemoveAnimation(mScrollAnimation);
1373   mRefreshEnabled = true;
1374   DoRefresh(GetCurrentLayoutPosition(0), true);
1375
1376   // Emit the layout activated signal
1377   mLayoutActivatedSignal.Emit();
1378 }
1379
1380 void ItemView::OnOvershootOnFinished(Animation& animation)
1381 {
1382   mAnimatingOvershootOn = false;
1383   mScrollOvershootAnimation.FinishedSignal().Disconnect(this, &ItemView::OnOvershootOnFinished);
1384   RemoveAnimation(mScrollOvershootAnimation);
1385   if(mAnimateOvershootOff)
1386   {
1387     AnimateScrollOvershoot(0.0f);
1388   }
1389   mInAnimation = false;
1390 }
1391
1392 void ItemView::ScrollToItem(unsigned int itemId, float durationSeconds)
1393 {
1394   Actor self = Self();
1395   const Vector3 layoutSize = Self().GetCurrentSize();
1396   float firstItemScrollPosition = ClampFirstItemPosition(mActiveLayout->GetItemScrollToPosition(itemId), layoutSize, *mActiveLayout);
1397
1398   if(durationSeconds > 0.0f)
1399   {
1400     RemoveAnimation(mScrollAnimation);
1401     mScrollAnimation = Animation::New(durationSeconds);
1402     mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, mScrollToAlphaFunction );
1403     mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1404     mScrollAnimation.Play();
1405   }
1406   else
1407   {
1408     self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1409     AnimateScrollOvershoot(0.0f);
1410   }
1411
1412   mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1413   mRefreshEnabled = true;
1414 }
1415
1416 void ItemView::RemoveAnimation(Animation& animation)
1417 {
1418   if(animation)
1419   {
1420     // Cease animating, and reset handle.
1421     animation.Clear();
1422     animation.Reset();
1423   }
1424 }
1425
1426 void ItemView::CalculateDomainSize(const Vector3& layoutSize)
1427 {
1428   Actor self = Self();
1429
1430   Vector3 firstItemPosition(Vector3::ZERO);
1431   Vector3 lastItemPosition(Vector3::ZERO);
1432
1433   if(mActiveLayout)
1434   {
1435     firstItemPosition = mActiveLayout->GetItemPosition( 0,0,layoutSize );
1436
1437     float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize);
1438     lastItemPosition = mActiveLayout->GetItemPosition( fabs(minLayoutPosition),fabs(minLayoutPosition),layoutSize );
1439
1440     float domainSize;
1441
1442     if(IsHorizontal(mActiveLayout->GetOrientation()))
1443     {
1444       domainSize = fabs(firstItemPosition.x - lastItemPosition.x);
1445     }
1446     else
1447     {
1448       domainSize = fabs(firstItemPosition.y - lastItemPosition.y);
1449     }
1450
1451     self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector2::ZERO);
1452     self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1453
1454     self.SetProperty(Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE, domainSize);
1455
1456     bool isLayoutScrollable = IsLayoutScrollable(layoutSize);
1457     self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, isLayoutScrollable);
1458     self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, false);
1459   }
1460 }
1461
1462 bool ItemView::IsLayoutScrollable(const Vector3& layoutSize)
1463 {
1464   Actor self = Self();
1465
1466   float currentLayoutPosition = ClampFirstItemPosition( GetCurrentLayoutPosition(0), layoutSize, *mActiveLayout, false );
1467   float forwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition + 1.0, layoutSize, *mActiveLayout, false );
1468   float backwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition - 1.0, layoutSize, *mActiveLayout, false );
1469
1470   return (fabs(forwardClampedPosition - backwardClampedPosition) > Math::MACHINE_EPSILON_0);
1471 }
1472
1473 float ItemView::GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const
1474 {
1475   Vector3 firstItemPosition( mActiveLayout->GetItemPosition(0, layoutPosition, layoutSize ) );
1476   return IsHorizontal(mActiveLayout->GetOrientation()) ? firstItemPosition.x: firstItemPosition.y;
1477 }
1478
1479 Vector2 ItemView::GetCurrentScrollPosition() const
1480 {
1481   return Vector2(0.0f, GetScrollPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize()));
1482 }
1483
1484 void ItemView::AddOverlay(Actor actor)
1485 {
1486   actor.SetDrawMode( DrawMode::OVERLAY_2D );
1487   Self().Add(actor);
1488 }
1489
1490 void ItemView::RemoveOverlay(Actor actor)
1491 {
1492   Self().Remove(actor);
1493 }
1494
1495 void ItemView::ScrollTo(const Vector2& position, float duration)
1496 {
1497   Actor self = Self();
1498   const Vector3 layoutSize = Self().GetCurrentSize();
1499
1500   float firstItemScrollPosition = ClampFirstItemPosition(position.y, layoutSize, *mActiveLayout);
1501
1502   if(duration > 0.0f)
1503   {
1504     RemoveAnimation(mScrollAnimation);
1505     mScrollAnimation = Animation::New(duration);
1506     mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, mScrollToAlphaFunction );
1507     mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1508     mScrollAnimation.Play();
1509   }
1510   else
1511   {
1512     self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1513     AnimateScrollOvershoot(0.0f);
1514   }
1515
1516   mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1517   mRefreshEnabled = true;
1518 }
1519
1520 void ItemView::SetOvershootEffectColor( const Vector4& color )
1521 {
1522   mOvershootEffectColor = color;
1523   if( mOvershootOverlay )
1524   {
1525     mOvershootOverlay.SetColor( color );
1526   }
1527 }
1528
1529 void ItemView::EnableScrollOvershoot( bool enable )
1530 {
1531   Actor self = Self();
1532   if( enable )
1533   {
1534     if( !mOvershootOverlay )
1535     {
1536       Property::Index effectOvershootPropertyIndex = Property::INVALID_INDEX;
1537       mOvershootOverlay = CreateBouncingEffectActor( effectOvershootPropertyIndex );
1538       mOvershootOverlay.SetColor(mOvershootEffectColor);
1539       mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
1540       mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1541       mOvershootOverlay.SetDrawMode( DrawMode::OVERLAY_2D );
1542       self.Add(mOvershootOverlay);
1543
1544       Constraint constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::SIZE, OvershootOverlaySizeConstraint(mOvershootSize.height) );
1545       constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1546       constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1547       constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1548       constraint.Apply();
1549
1550       mOvershootOverlay.SetSize(mOvershootSize.width, mOvershootSize.height);
1551
1552       constraint = Constraint::New<Quaternion>( mOvershootOverlay, Actor::Property::ORIENTATION, OvershootOverlayRotationConstraint );
1553       constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1554       constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1555       constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1556       constraint.Apply();
1557
1558       constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::POSITION, OvershootOverlayPositionConstraint );
1559       constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1560       constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1561       constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1562       constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1563       constraint.Apply();
1564
1565       constraint = Constraint::New<bool>( mOvershootOverlay, Actor::Property::VISIBLE, OvershootOverlayVisibilityConstraint );
1566       constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
1567       constraint.Apply();
1568
1569       constraint = Constraint::New<float>( mOvershootOverlay, effectOvershootPropertyIndex, EqualToConstraint() );
1570       constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1571       constraint.Apply();
1572     }
1573   }
1574   else
1575   {
1576     if( mOvershootOverlay )
1577     {
1578       self.Remove(mOvershootOverlay);
1579       mOvershootOverlay.Reset();
1580     }
1581   }
1582 }
1583
1584 float ItemView::CalculateScrollOvershoot()
1585 {
1586   float overshoot = 0.0f;
1587
1588   if(mActiveLayout)
1589   {
1590     // The overshoot must be calculated from the accumulated pan gesture displacement
1591     // since the pan gesture starts.
1592     Actor self = Self();
1593     float scrollDistance = CalculateScrollDistance(mTotalPanDisplacement, *mActiveLayout) * mActiveLayout->GetScrollSpeedFactor();
1594     float positionDelta = GetCurrentLayoutPosition(0) + scrollDistance;
1595     float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), Self().GetCurrentSize());
1596     self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1597     float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, positionDelta));
1598     overshoot = positionDelta - clamppedPosition;
1599   }
1600
1601   return overshoot > 0.0f ? std::min(overshoot, 1.0f) : std::max(overshoot, -1.0f);
1602 }
1603
1604 void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
1605 {
1606   bool animatingOn = fabsf(overshootAmount) > Math::MACHINE_EPSILON_1;
1607
1608   // make sure we animate back if needed
1609   mAnimateOvershootOff = animateBack || (!animatingOn && mAnimatingOvershootOn);
1610
1611   if( mAnimatingOvershootOn )
1612   {
1613     // animating on, do not allow animate off
1614     return;
1615   }
1616
1617   Actor self = Self();
1618
1619   if(mOvershootAnimationSpeed > Math::MACHINE_EPSILON_0)
1620   {
1621     float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1622     float duration = 0.0f;
1623
1624     if (mOvershootOverlay)
1625     {
1626       duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot)) / mOvershootAnimationSpeed;
1627     }
1628
1629     // Mark the animation as in progress to prevent manual property sets overwriting it.
1630     mInAnimation = true;
1631     mAnimatingOvershootOn = animatingOn;
1632     RemoveAnimation(mScrollOvershootAnimation);
1633     mScrollOvershootAnimation = Animation::New(duration);
1634     mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
1635     mScrollOvershootAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
1636     mScrollOvershootAnimation.Play();
1637   }
1638   else
1639   {
1640     self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, overshootAmount );
1641   }
1642 }
1643
1644 void ItemView::SetItemsParentOrigin( const Vector3& parentOrigin )
1645 {
1646   if( parentOrigin != mItemsParentOrigin )
1647   {
1648     mItemsParentOrigin = parentOrigin;
1649     for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1650     {
1651       iter->second.SetParentOrigin(parentOrigin);
1652     }
1653   }
1654 }
1655
1656 Vector3 ItemView::GetItemsParentOrigin() const
1657 {
1658   return mItemsParentOrigin;
1659 }
1660
1661 void ItemView::SetItemsAnchorPoint( const Vector3& anchorPoint )
1662 {
1663   if( anchorPoint != mItemsAnchorPoint )
1664   {
1665     mItemsAnchorPoint = anchorPoint;
1666     for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1667     {
1668       iter->second.SetAnchorPoint(anchorPoint);
1669     }
1670   }
1671 }
1672
1673 Vector3 ItemView::GetItemsAnchorPoint() const
1674 {
1675   return mItemsAnchorPoint;
1676 }
1677
1678 void ItemView::GetItemsRange(ItemRange& range)
1679 {
1680   if( !mItemPool.empty() )
1681   {
1682     range.begin = mItemPool.begin()->first;
1683     range.end = mItemPool.rbegin()->first + 1;
1684   }
1685   else
1686   {
1687     range.begin = 0;
1688     range.end = 0;
1689   }
1690 }
1691
1692 bool ItemView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1693 {
1694   Dali::BaseHandle handle( object );
1695
1696   bool connected( true );
1697   Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1698
1699   if( 0 == strcmp( signalName.c_str(), LAYOUT_ACTIVATED_SIGNAL ) )
1700   {
1701     itemView.LayoutActivatedSignal().Connect( tracker, functor );
1702   }
1703   else
1704   {
1705     // signalName does not match any signal
1706     connected = false;
1707   }
1708
1709   return connected;
1710 }
1711
1712 void ItemView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1713 {
1714   Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1715
1716   if( itemView )
1717   {
1718     ItemView& itemViewImpl( GetImpl( itemView ) );
1719     switch( index )
1720     {
1721       case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1722       {
1723         itemViewImpl.SetMinimumSwipeSpeed( value.Get<float>() );
1724         break;
1725       }
1726       case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1727       {
1728         itemViewImpl.SetMinimumSwipeDistance( value.Get<float>() );
1729         break;
1730       }
1731       case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1732       {
1733         itemViewImpl.SetWheelScrollDistanceStep( value.Get<float>() );
1734         break;
1735       }
1736       case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1737       {
1738         itemViewImpl.SetAnchoring( value.Get<bool>() );
1739         break;
1740       }
1741       case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1742       {
1743         itemViewImpl.SetRefreshInterval( value.Get<float>() );
1744         break;
1745       }
1746       case Toolkit::DevelItemView::Property::LAYOUT:
1747       {
1748         // Get a Property::Array from the property if possible.
1749         Property::Array layoutArray;
1750         if( value.Get( layoutArray ) )
1751         {
1752           itemViewImpl.SetLayoutArray( layoutArray );
1753         }
1754         break;
1755       }
1756     }
1757   }
1758 }
1759
1760 Property::Array ItemView::GetLayoutArray()
1761 {
1762   return mlayoutArray;
1763 }
1764
1765 void ItemView::SetLayoutArray( const Property::Array& layouts )
1766 {
1767   mlayoutArray = layouts;
1768   const int layoutCount = GetLayoutCount();
1769   if( layoutCount > 0 )
1770   {
1771     for(int index = layoutCount - 1; index >= 0; --index)
1772     {
1773       RemoveLayout(index);
1774       if(index == 0) break;
1775     }
1776   }
1777
1778   for( unsigned int arrayIdx = 0, arrayCount = layouts.Count(); arrayIdx < arrayCount; ++arrayIdx )
1779   {
1780     const Property::Value& element = layouts.GetElementAt( arrayIdx );
1781
1782     Property::Map* layout = element.GetMap();
1783     if( layout != NULL )
1784     {
1785       for( unsigned int mapIdx = 0, mapCount = (*layout).Count(); mapIdx < mapCount; ++mapIdx )
1786       {
1787         KeyValuePair propertyPair( (*layout).GetKeyValue( mapIdx ) );
1788
1789         if(propertyPair.first == DefaultItemLayoutProperty::TYPE)
1790         {
1791           int layoutType = propertyPair.second.Get<int>();
1792           if(layoutType <= DefaultItemLayout::SPIRAL && layoutType >= DefaultItemLayout::DEPTH)
1793           {
1794             //DEPTH, GRID, LIST, SPIRAL
1795             switch(DefaultItemLayout::Type(layoutType))
1796             {
1797               case DefaultItemLayout::DEPTH:
1798               {
1799                 Internal::DepthLayoutPtr depthLayout = Internal::DepthLayout::New();
1800                 (*depthLayout).SetLayoutProperties(*layout);
1801                 AddLayout(*depthLayout);
1802                 break;
1803               }
1804               case DefaultItemLayout::GRID:
1805               {
1806                 Internal::GridLayoutPtr gridLayout = Internal::GridLayout::New();
1807                 (*gridLayout).SetLayoutProperties(*layout);
1808                 AddLayout(*gridLayout);
1809                 break;
1810               }
1811               case DefaultItemLayout::LIST:
1812               {
1813                 Internal::GridLayoutPtr listLayout = Internal::GridLayout::New();
1814                 listLayout->SetNumberOfColumns( 1 );
1815                 (*listLayout).SetLayoutProperties(*layout);
1816                 AddLayout(*listLayout);
1817                 break;
1818               }
1819               case DefaultItemLayout::SPIRAL:
1820               {
1821                 Internal::SpiralLayoutPtr spiralLayout = Internal::SpiralLayout::New();
1822                 (*spiralLayout).SetLayoutProperties(*layout);
1823                 AddLayout(*spiralLayout);
1824                 break;
1825               }
1826             }
1827           }
1828         }
1829       }
1830     }
1831   }
1832 }
1833
1834 Property::Value ItemView::GetProperty( BaseObject* object, Property::Index index )
1835 {
1836   Property::Value value;
1837
1838   Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1839
1840   if( itemView )
1841   {
1842     ItemView& itemViewImpl( GetImpl( itemView ) );
1843     switch( index )
1844     {
1845       case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1846       {
1847         value = itemViewImpl.GetMinimumSwipeSpeed();
1848         break;
1849       }
1850       case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1851       {
1852         value = itemViewImpl.GetMinimumSwipeDistance();
1853         break;
1854       }
1855       case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1856       {
1857         value = itemViewImpl.GetWheelScrollDistanceStep();
1858         break;
1859       }
1860       case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1861       {
1862         value = itemViewImpl.GetAnchoring();
1863         break;
1864       }
1865       case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1866       {
1867         value = itemViewImpl.GetRefreshInterval();
1868         break;
1869       }
1870       case Toolkit::DevelItemView::Property::LAYOUT:
1871       {
1872         Property::Array layouts= itemViewImpl.GetLayoutArray();
1873         value = layouts;
1874         break;
1875       }
1876
1877     }
1878   }
1879
1880   return value;
1881 }
1882
1883 bool ItemView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
1884 {
1885   Dali::BaseHandle handle( object );
1886
1887   Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1888
1889   DALI_ASSERT_ALWAYS( itemView );
1890
1891   if( 0 == strcmp( actionName.c_str(), ACTION_STOP_SCROLLING ) )
1892   {
1893     GetImpl( itemView ).DoStopScrolling();
1894   }
1895   else if ( 0 == strcmp( actionName.c_str(), ACTION_ENABLE_REFRESH ) )
1896   {
1897     GetImpl( itemView ).SetRefreshNotificationEnabled( true );
1898   }
1899   else if ( 0 == strcmp( actionName.c_str(), ACTION_DISABLE_REFRESH ) )
1900   {
1901     GetImpl( itemView ).SetRefreshNotificationEnabled( false );
1902   }
1903
1904   return true;
1905 }
1906
1907 void ItemView::DoStopScrolling()
1908 {
1909   if( mScrollAnimation )
1910   {
1911     mScrollAnimation.Stop();
1912     mScrollAnimation.Reset();
1913   }
1914 }
1915
1916 void ItemView::SetRefreshNotificationEnabled( bool enabled )
1917 {
1918   mRefreshNotificationEnabled = enabled;
1919 }
1920
1921 } // namespace Internal
1922
1923 } // namespace Toolkit
1924
1925 } // namespace Dali