Merge "Patch to synchronize with bug fixes done in Tizen 2.4 MCD branch." into devel...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scrollable / item-view / item-view-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__
2 #define __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/adaptor-framework/timer.h>
23 #include <dali/public-api/animation/animation.h>
24 #include <dali/public-api/object/property-notification.h>
25 #include <dali/devel-api/common/map-wrapper.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/controls/control-impl.h>
29 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
30 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
31 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
32 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 class ItemView;
44
45 typedef IntrusivePtr<ItemView> ItemViewPtr;
46
47 /**
48  * ItemView is a scrollable layout container.
49  * Multiple ItemLayouts may be provided, to determine the logical position of each item a layout.
50  * Actor-ID pairs are provided from a shared ItemFactory, to display the currently visible items.
51  */
52 class ItemView : public Scrollable
53 {
54 public:
55
56   // Signals
57   typedef Toolkit::ItemView::LayoutActivatedSignalType LayoutActivatedSignalType;
58
59 public:
60
61   /**
62    * Create a new ItemView.
63    * @param[in] factory The factory which provides ItemView with items.
64    * @return A public handle to the newly allocated ItemView.
65    */
66   static Dali::Toolkit::ItemView New(ItemFactory& factory);
67
68   /**
69    * @copydoc Toolkit::ItemView::GetLayoutCount
70    */
71   unsigned int GetLayoutCount() const;
72
73   /**
74    * @copydoc Toolkit::ItemView::AddLayout
75    */
76   void AddLayout(ItemLayout& layout);
77
78   /**
79    * @copydoc Toolkit::ItemView::RemoveLayout
80    */
81   void RemoveLayout(unsigned int layoutIndex);
82
83   /**
84    * @copydoc Toolkit::ItemView::GetLayout
85    */
86   ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
87
88   /**
89    * @copydoc Toolkit::ItemView::GetActiveLayout
90    */
91   ItemLayoutPtr GetActiveLayout() const;
92
93   /**
94    * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
95    */
96   float GetCurrentLayoutPosition(unsigned int itemId) const;
97
98   /**
99    * @copydoc Toolkit::ItemView::ActivateLayout
100    */
101   void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
102
103   /**
104    * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
105    */
106   void DeactivateCurrentLayout();
107
108   /**
109    * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
110    */
111   void SetMinimumSwipeSpeed(float speed);
112
113   /**
114    * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
115    */
116   float GetMinimumSwipeSpeed() const;
117
118   /**
119    * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
120    */
121   void SetMinimumSwipeDistance(float distance);
122
123   /**
124    * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
125    */
126   float GetMinimumSwipeDistance() const;
127
128   /**
129    * @copydoc Toolkit::ItemView::SetWheelScrollDistanceStep
130    */
131   void SetWheelScrollDistanceStep(float step);
132
133   /**
134    * @copydoc Toolkit::ItemView::GetWheelScrollDistanceStep
135    */
136   float GetWheelScrollDistanceStep() const;
137
138   /**
139    * @copydoc Toolkit::ItemView::SetAnchoring
140    */
141   void SetAnchoring(bool enabled);
142
143   /**
144    * @copydoc Toolkit::ItemView::GetAnchoring
145    */
146   bool GetAnchoring() const;
147
148   /**
149    * @copydoc Toolkit::ItemView::SetAnchoringDuration
150    */
151   void SetAnchoringDuration(float durationSeconds);
152
153   /**
154    * @copydoc Toolkit::ItemView::GetAnchoringDuration
155    */
156   float GetAnchoringDuration() const;
157
158   /**
159    * @copydoc Toolkit::ItemView::ScrollToItem
160    */
161   void ScrollToItem(unsigned int itemId, float durationSeconds);
162
163   /**
164    * @copydoc Toolkit::ItemView::SetRefreshInterval
165    */
166   void SetRefreshInterval(float intervalLayoutPositions);
167
168   /**
169    * @copydoc Toolkit::ItemView::GetRefreshInterval
170    */
171   float GetRefreshInterval() const;
172
173   /**
174    * @copydoc Toolkit::ItemView::Refresh
175    */
176   void Refresh();
177
178   /**
179    * @copydoc Toolkit::ItemView::GetItem
180    */
181   Actor GetItem(unsigned int itemId) const;
182
183   /**
184    * @copydoc Toolkit::ItemView::GetItemId
185    */
186   unsigned int GetItemId(Actor actor) const;
187
188   /**
189    * @copydoc Toolkit::ItemView::InsertItem
190    */
191   void InsertItem(Item newItem, float durationSeconds);
192
193   /**
194    * @copydoc Toolkit::ItemView::InsertItem
195    */
196   void InsertItems(const ItemContainer& newItems, float durationSeconds);
197
198   /**
199    * @copydoc Toolkit::ItemView::RemoveItem
200    */
201   void RemoveItem(ItemId itemId, float durationSeconds);
202
203   /**
204    * @copydoc Toolkit::ItemView::InsertItem
205    */
206   void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
207
208   /**
209    * @copydoc Toolkit::ItemView::InsertItem
210    */
211   void ReplaceItem(Item replacementItem, float durationSeconds);
212
213   /**
214    * @copydoc Toolkit::ItemView::InsertItem
215    */
216   void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
217
218   /**
219    * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
220    */
221   Vector2 GetCurrentScrollPosition() const;
222
223   /**
224    * @copydoc Toolkit::Scrollable::AddOverlay()
225    */
226   void AddOverlay(Actor actor);
227
228   /**
229    * @copydoc Toolkit::Scrollable::RemoveOverlay()
230    */
231   void RemoveOverlay(Actor actor);
232
233   /**
234    * @copydoc Toolkit::Scrollable::ScrollTo(const Vector2& position, float duration)
235    */
236   void ScrollTo(const Vector2& position, float duration);
237
238   /**
239    * @copydoc Toolkit::Internal::Scrollable::SetOvershootEffectColor
240    */
241   void SetOvershootEffectColor( const Vector4& color );
242
243   /**
244    * @brief Set whether to enable automatic refresh or not. When refresh is disabled,
245    * ItemView will not automatically refresh the cache in the given interval when the
246    * layout position is changed. This is useful in some cases, for example, automatic
247    * refresh is not needed during fast scrolling, otherwise it will cache unneeded
248    * items when the layout position changes quickly.
249    *
250    * @param[in] enabled True to enable automatic refresh or false to disable it.
251    */
252   void SetRefreshEnabled(bool enabled);
253
254   /**
255    * @brief Helper to perform the refresh.
256    *
257    * @param[in] currentLayoutPosition The current layout position.
258    * @param[in] cacheExtra Whether to cache extra items during refresh.
259    */
260   void DoRefresh(float currentLayoutPosition, bool cacheExtra);
261
262   /**
263    * @copydoc Toolkit::ItemView::SetItemsParentOrigin
264    */
265   void SetItemsParentOrigin( const Vector3& parentOrigin );
266
267   /**
268    * @copydoc Toolkit::ItemView::GetItemsParentOrigin
269    */
270   Vector3 GetItemsParentOrigin() const;
271
272   /**
273    * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
274    */
275   void SetItemsAnchorPoint( const Vector3& anchorPoint );
276
277   /**
278    * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
279    */
280   Vector3 GetItemsAnchorPoint() const;
281
282   /**
283    * @copydoc Toolkit::ItemView::GetItemsRange
284    */
285   void GetItemsRange(ItemRange& range);
286
287   /**
288    * @copydoc Toolkit::ItemView::LayoutActivatedSignal()
289    */
290   LayoutActivatedSignalType& LayoutActivatedSignal()
291   {
292     return mLayoutActivatedSignal;
293   }
294
295   /**
296    * Connects a callback function with the object's signals.
297    * @param[in] object The object providing the signal.
298    * @param[in] tracker Used to disconnect the signal.
299    * @param[in] signalName The signal to connect to.
300    * @param[in] functor A newly allocated FunctorDelegate.
301    * @return True if the signal was connected.
302    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
303    */
304   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
305
306   //properties
307
308   /**
309    * Called when a property of an object of this type is set.
310    * @param[in] object The object whose property is set.
311    * @param[in] index The property index.
312    * @param[in] value The new property value.
313    */
314   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
315
316   /**
317    * Called to retrieve a property of an object of this type.
318    * @param[in] object The object whose property is to be retrieved.
319    * @param[in] index The property index.
320    * @return The current value of the property.
321    */
322   static Property::Value GetProperty( BaseObject* object, Property::Index index );
323
324   /**
325    * Performs actions as requested using the action name.
326    * @param[in] object The object on which to perform the action.
327    * @param[in] actionName The action to perform.
328    * @param[in] attributes The attributes with which to perfrom this action.
329    * @return true if action has been accepted by this control
330    */
331   static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
332
333   /**
334    * Helper for DoAction( ACTION_STOP_SCROLLING ).
335    */
336   void DoStopScrolling();
337
338   /**
339    * Helper for DoAction( ACTION_ENABLE/DISABLE_REFRESH_NOTIFICATIONS ).
340    * @param[in] enabled Whether to disable refresh notifications or not.
341    */
342   void SetRefreshNotificationEnabled( bool enabled );
343
344 private:
345
346   /**
347    * Remove an Actor if found in the ItemPool.
348    * @param[in] itemId The item to remove.
349    * @return True if the remaining actors were reordered.
350    */
351   bool RemoveActor( unsigned int itemId );
352
353   /**
354    * Remove any Actors outside a given range.
355    * @param[in] @param[in] range The range of required items.
356    */
357   void RemoveActorsOutsideRange( ItemRange range );
358
359   /**
360    * Add a range of Actors, if they are not already in the ItemPool.
361    * @param[in] range The range of Item IDs to associate with the new actors.
362    * @param[in] layoutSize The layout-size.
363    */
364   void AddActorsWithinRange( ItemRange range, const Vector3& layoutSize );
365
366   /**
367    * Add a new Actor, if not already in the ItemPool.
368    * @param[in] item The ID for the new item.
369    * @param[in] layoutSize The layout-size.
370    */
371   void AddNewActor( ItemId item, const Vector3& layoutSize );
372
373   /**
374    * Apply the constraints etc. that are required for ItemView children.
375    * @param[in] item The item to setup.
376    * @param[in] layoutSize The layout-size.
377    */
378   void SetupActor( Item item, const Vector3& layoutSize );
379
380   /**
381    * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
382    * @param[in] item The ID for the item to be released.
383    * @param[in] actor The actor to be removed from ItemView.
384    */
385   void ReleaseActor( ItemId item, Actor actor );
386
387 private: // From CustomActorImpl
388
389   /**
390    * From CustomActorImpl; called after a child has been added to the owning actor.
391    * @param[in] child The child which has been added.
392    */
393   virtual void OnChildAdd(Actor& child);
394
395   /**
396    * From CustomActorImpl; called after a wheel-event is received by the owning actor.
397    * @param[in] event The wheel event.
398    * @return True if the event should be consumed.
399    */
400   virtual bool OnWheelEvent(const WheelEvent& event);
401
402 private: // From Control
403
404   /**
405    * @copydoc Toolkit::Control::OnInitialize()
406    */
407   virtual void OnInitialize();
408
409   /**
410    * @copydoc Toolkit::Control::OnAccessibilityPan()
411    */
412   virtual bool OnAccessibilityPan(PanGesture gesture);
413
414   /**
415    * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
416    */
417   virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled);
418
419   /**
420    * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
421    */
422   virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
423
424 protected:
425
426   /**
427    * Construct a new ItemView.
428    * @param[in] factory The factory which provides ItemView with items.
429    */
430   ItemView(ItemFactory& factory);
431
432   /**
433    * A reference counted object may only be deleted by calling Unreference()
434    */
435   virtual ~ItemView();
436
437 private:
438
439   // Undefined
440   ItemView(const ItemView&);
441
442   // Undefined
443   ItemView& operator=(const ItemView& rhs);
444
445   /**
446    * Helper to re-apply all the constraints after items have been inserted, removed etc.
447    * @param[in] durationSeconds The time taken to fully constrain the actors.
448    */
449   void ReapplyAllConstraints();
450
451   /**
452    * Helper to relayout after item(s) are removed.
453    */
454   void OnItemsRemoved();
455
456   /**
457    * Helper to remove items outside a given range.
458    * @param[in] range The range of required items.
459    */
460   void RemoveItems(ItemRange range);
461
462   /**
463    * Helper to add a range of items, if not already in the ItemPool.
464    * @param[in] layout The layout used to position the new items.
465    * @param[in] layoutSize The current size of the layout.
466    * @param[in] range The range of required items.
467    */
468   void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
469
470   /**
471    * Helper to find the range of items to populate with.
472    * @param[in] layout The current layout.
473    * @param[in] range The range of items.
474    * @param[in] reserveExtra True if reserve items should be included.
475    */
476   ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
477
478   // Input Handling
479
480   /**
481    * Helper to clamp the first-item position when dragging/swiping.
482    * @param[in] targetPosition The target position of the drag etc.
483    * @param[in] targetSize The target ItemView & layout size.
484    * @param[in] layout The current layout.
485    * @param[in] updateOvershoot False stops the current overshoot value from being clamped also.
486    * @return The clamped first-item position.
487    */
488   float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot = true);
489
490   /**
491    * Called after a touch-signal is received by the owning actor.
492    * @param[in] actor The touched actor.
493    * @param[in] touch The touch information.
494    * @return True if the event should be consumed.
495    */
496   bool OnTouch( Actor actor, const TouchData& touch );
497
498   /**
499    * Called upon pan gesture event.
500    *
501    * @param[in] gesture The gesture event.
502    */
503   void OnPan( const PanGesture& pan );
504
505   /**
506    * Helper to handle anchoring animations.
507    * @return The animation, or an uninitialized handle if not necessary.
508    */
509   Animation DoAnchoring();
510
511   /**
512    * Callback from scroll animations
513    * @param[in] animation The scroll-animation which has finished.
514    */
515   void OnScrollFinished(Animation& animation);
516
517   /**
518    * Callback from layout activation scroll animations
519    * @param[in] animation The scroll-animation which has finished.
520    */
521   void OnLayoutActivationScrollFinished(Animation& animation);
522
523   /**
524    * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
525    *
526    * @param[in] animation the animation that has finished
527    */
528   void OnOvershootOnFinished(Animation& animation);
529
530   /**
531    * This is called after a timeout when no new wheel event is received for a certain period of time.
532    * @return will return false; one-shot timer.
533    */
534   bool OnWheelEventFinished();
535
536   /**
537    * Stops and removes animation if exists.
538    * @param[in,out] animation The animation handle to be removed.
539    */
540   void RemoveAnimation(Animation& animation);
541
542   /**
543    * @copydoc Toolkit::Internal::Scrollable::EnableScrollOvershoot
544    */
545   virtual void EnableScrollOvershoot( bool enable );
546
547   /**
548    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
549    * @return The scroll overshoot.
550    */
551   float CalculateScrollOvershoot();
552
553   /**
554    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
555    *
556    * @param[in] overshootAmount amount to animate to
557    * @param[in] animateBack whether to animate back to zero immediately after
558    * @return The scroll overshoot.
559    */
560   void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
561
562   /**
563    * Gets the scroll position in pixels according to the logical layout position.
564    * @param[in] layoutSize The current size of the layout.
565    */
566   float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
567
568   /**
569    * Calculates the minimum and maximum positions for each axis to scroll to.
570    * @param[in] layoutSize The current size of the layout.
571    */
572   void CalculateDomainSize(const Vector3& layoutSize);
573
574   /**
575    * Calculates whether we want to allow current item view to scroll.
576    * @param[in] layoutSize The current size of the layout.
577    * @return    true if itemview is scrollable
578    */
579   bool IsLayoutScrollable(const Vector3& layoutSize);
580
581   /**
582    * Callback when the current layout position of ItemView changes in both positive
583    * and negative directions by the specified amount. Refresh the ItemView to create
584    * newly visible items.
585    * @param[in] source the property notification that triggered this callback
586    */
587   void OnRefreshNotification(PropertyNotification& source);
588
589 private:
590
591   typedef std::map<unsigned int, Actor> ItemPool;
592   typedef ItemPool::iterator            ItemPoolIter;
593   typedef ItemPool::const_iterator      ConstItemPoolIter;
594
595   ItemPool mItemPool;
596   ItemFactory& mItemFactory;
597   std::vector< ItemLayoutPtr > mLayouts;            ///< Container of Dali::Toolkit::ItemLayout objects
598   Actor mOvershootOverlay;                          ///< The overlay actor for overshoot effect
599   Animation mResizeAnimation;
600   Animation mScrollAnimation;
601   Animation mScrollOvershootAnimation;
602   Timer mWheelEventFinishedTimer;                   ///< The timer to determine whether there is no wheel event received for a certain period of time.
603   PropertyNotification mRefreshNotification;        ///< Stores the property notification used for item view refresh
604   LayoutActivatedSignalType mLayoutActivatedSignal;
605   Vector3 mActiveLayoutTargetSize;
606   Vector3 mItemsParentOrigin;
607   Vector3 mItemsAnchorPoint;
608   Vector2 mTotalPanDisplacement;
609   ItemLayout* mActiveLayout;
610
611   float mAnchoringDuration;
612   float mRefreshIntervalLayoutPositions;            ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
613   float mMinimumSwipeSpeed;
614   float mMinimumSwipeDistance;
615   float mWheelScrollDistanceStep;                   ///< The step of scroll distance in actor coordinates for each wheel event received.
616   float mScrollDistance;
617   float mScrollSpeed;
618   float mScrollOvershoot;
619
620   Dali::Gesture::State mGestureState    : 3;
621   bool mAnimatingOvershootOn            : 1;        ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
622   bool mAnimateOvershootOff             : 1;        ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
623   bool mAnchoringEnabled                : 1;
624   bool mRefreshOrderHint                : 1;        ///< True if scrolling towards the last item
625   bool mIsFlicking                      : 1;
626   bool mAddingItems                     : 1;
627   bool mRefreshEnabled                  : 1;        ///< Whether to refresh the cache automatically
628   bool mRefreshNotificationEnabled      : 1;        ///< Whether to disable refresh notifications or not.
629   bool mInAnimation                     : 1;        ///< Keeps track of whether an animation is controlling the overshoot property.
630 };
631
632 } // namespace Internal
633
634 // Helpers for public-api forwarding methods
635
636 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
637 {
638   DALI_ASSERT_ALWAYS(itemView);
639
640   Dali::RefObject& handle = itemView.GetImplementation();
641
642   return static_cast<Toolkit::Internal::ItemView&>(handle);
643 }
644
645 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
646 {
647   DALI_ASSERT_ALWAYS(itemView);
648
649   const Dali::RefObject& handle = itemView.GetImplementation();
650
651   return static_cast<const Toolkit::Internal::ItemView&>(handle);
652 }
653
654 } // namespace Toolkit
655
656 } // namespace Dali
657
658 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__