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