Text decorator - It consumes tap, double tap and long press events on handles.
[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::GetDomainSize
220    */
221   Vector2 GetDomainSize() const;
222
223   /**
224    * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
225    */
226   Vector2 GetCurrentScrollPosition() const;
227
228   /**
229    * @copydoc Toolkit::Scrollable::AddOverlay()
230    */
231   void AddOverlay(Actor actor);
232
233   /**
234    * @copydoc Toolkit::Scrollable::RemoveOverlay()
235    */
236   void RemoveOverlay(Actor actor);
237
238   /**
239    * @copydoc Toolkit::Scrollable::ScrollTo(const Vector2& position, float duration)
240    */
241   void ScrollTo(const Vector2& position, float duration);
242
243   /**
244    * @copydoc Toolkit::Internal::Scrollable::SetOvershootEffectColor
245    */
246   void SetOvershootEffectColor( const Vector4& color );
247
248   /**
249    * @brief Set whether to enable automatic refresh or not. When refresh is disabled,
250    * ItemView will not automatically refresh the cache in the given interval when the
251    * layout position is changed. This is useful in some cases, for example, automatic
252    * refresh is not needed during fast scrolling, otherwise it will cache unneeded
253    * items when the layout position changes quickly.
254    *
255    * @param[in] enabled True to enable automatic refresh or false to disable it.
256    */
257   void SetRefreshEnabled(bool enabled);
258
259   /**
260    * @brief Helper to perform the refresh.
261    *
262    * @param[in] currentLayoutPosition The current layout position.
263    * @param[in] cacheExtra Whether to cache extra items during refresh.
264    */
265   void DoRefresh(float currentLayoutPosition, bool cacheExtra);
266
267   /**
268    * @copydoc Toolkit::ItemView::SetItemsParentOrigin
269    */
270   void SetItemsParentOrigin( const Vector3& parentOrigin );
271
272   /**
273    * @copydoc Toolkit::ItemView::GetItemsParentOrigin
274    */
275   Vector3 GetItemsParentOrigin() const;
276
277   /**
278    * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
279    */
280   void SetItemsAnchorPoint( const Vector3& anchorPoint );
281
282   /**
283    * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
284    */
285   Vector3 GetItemsAnchorPoint() const;
286
287   /**
288    * @copydoc Toolkit::ItemView::GetItemsRange
289    */
290   void GetItemsRange(ItemRange& range);
291
292   /**
293    * @copydoc Toolkit::ItemView::LayoutActivatedSignal()
294    */
295   LayoutActivatedSignalType& LayoutActivatedSignal()
296   {
297     return mLayoutActivatedSignal;
298   }
299
300   /**
301    * Connects a callback function with the object's signals.
302    * @param[in] object The object providing the signal.
303    * @param[in] tracker Used to disconnect the signal.
304    * @param[in] signalName The signal to connect to.
305    * @param[in] functor A newly allocated FunctorDelegate.
306    * @return True if the signal was connected.
307    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
308    */
309   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
310
311   //properties
312
313   /**
314    * Called when a property of an object of this type is set.
315    * @param[in] object The object whose property is set.
316    * @param[in] index The property index.
317    * @param[in] value The new property value.
318    */
319   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
320
321   /**
322    * Called to retrieve a property of an object of this type.
323    * @param[in] object The object whose property is to be retrieved.
324    * @param[in] index The property index.
325    * @return The current value of the property.
326    */
327   static Property::Value GetProperty( BaseObject* object, Property::Index index );
328
329   /**
330    * Performs actions as requested using the action name.
331    * @param[in] object The object on which to perform the action.
332    * @param[in] actionName The action to perform.
333    * @param[in] attributes The attributes with which to perfrom this action.
334    * @return true if action has been accepted by this control
335    */
336   static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
337
338   /**
339    * Helper for DoAction( ACTION_STOP_SCROLLING ).
340    */
341   void DoStopScrolling();
342
343 private:
344
345   /**
346    * Remove an Actor if found in the ItemPool.
347    * @param[in] itemId The item to remove.
348    * @return True if the remaining actors were reordered.
349    */
350   bool RemoveActor( unsigned int itemId );
351
352   /**
353    * Remove any Actors outside a given range.
354    * @param[in] @param[in] range The range of required items.
355    */
356   void RemoveActorsOutsideRange( ItemRange range );
357
358   /**
359    * Add a range of Actors, if they are not already in the ItemPool.
360    * @param[in] range The range of Item IDs to associate with the new actors.
361    * @param[in] layoutSize The layout-size.
362    */
363   void AddActorsWithinRange( ItemRange range, const Vector3& layoutSize );
364
365   /**
366    * Add a new Actor, if not already in the ItemPool.
367    * @param[in] item The ID for the new item.
368    * @param[in] layoutSize The layout-size.
369    */
370   void AddNewActor( ItemId item, const Vector3& layoutSize );
371
372   /**
373    * Apply the constraints etc. that are required for ItemView children.
374    * @param[in] item The item to setup.
375    * @param[in] layoutSize The layout-size.
376    */
377   void SetupActor( Item item, const Vector3& layoutSize );
378
379   /**
380    * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
381    * @param[in] item The ID for the item to be released.
382    * @param[in] actor The actor to be removed from ItemView.
383    */
384   void ReleaseActor( ItemId item, Actor actor );
385
386 private: // From CustomActorImpl
387
388   /**
389    * From CustomActorImpl; called after a child has been added to the owning actor.
390    * @param[in] child The child which has been added.
391    */
392   virtual void OnChildAdd(Actor& child);
393
394   /**
395    * From CustomActorImpl; called after a wheel-event is received by the owning actor.
396    * @param[in] event The wheel event.
397    * @return True if the event should be consumed.
398    */
399   virtual bool OnWheelEvent(const WheelEvent& event);
400
401 private: // From Control
402
403   /**
404    * @copydoc Toolkit::Control::OnInitialize()
405    */
406   virtual void OnInitialize();
407
408   /**
409    * @copydoc Toolkit::Control::OnAccessibilityPan()
410    */
411   virtual bool OnAccessibilityPan(PanGesture gesture);
412
413   /**
414    * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
415    */
416   virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled);
417
418   /**
419    * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
420    */
421   virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
422
423 protected:
424
425   /**
426    * Construct a new ItemView.
427    * @param[in] factory The factory which provides ItemView with items.
428    */
429   ItemView(ItemFactory& factory);
430
431   /**
432    * A reference counted object may only be deleted by calling Unreference()
433    */
434   virtual ~ItemView();
435
436 private:
437
438   // Undefined
439   ItemView(const ItemView&);
440
441   // Undefined
442   ItemView& operator=(const ItemView& rhs);
443
444   /**
445    * Helper to re-apply all the constraints after items have been inserted, removed etc.
446    * @param[in] durationSeconds The time taken to fully constrain the actors.
447    */
448   void ReapplyAllConstraints();
449
450   /**
451    * Helper to relayout after item(s) are removed.
452    */
453   void OnItemsRemoved();
454
455   /**
456    * Helper to remove items outside a given range.
457    * @param[in] range The range of required items.
458    */
459   void RemoveItems(ItemRange range);
460
461   /**
462    * Helper to add a range of items, if not already in the ItemPool.
463    * @param[in] layout The layout used to position the new items.
464    * @param[in] layoutSize The current size of the layout.
465    * @param[in] range The range of required items.
466    */
467   void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
468
469   /**
470    * Helper to find the range of items to populate with.
471    * @param[in] layout The current layout.
472    * @param[in] range The range of items.
473    * @param[in] reserveExtra True if reserve items should be included.
474    */
475   ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
476
477   // Input Handling
478
479   /**
480    * Helper to clamp the first-item position when dragging/swiping.
481    * @param[in] targetPosition The target position of the drag etc.
482    * @param[in] targetSize The target ItemView & layout size.
483    * @param[in] layout The current layout.
484    * @param[in] updateOvershoot False stops the current overshoot value from being clamped also.
485    * @return The clamped first-item position.
486    */
487   float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot = true);
488
489   /**
490    * Called after a touch-signal is received by the owning actor.
491    * @param[in] actor The touched actor.
492    * @param[in] touch The touch information.
493    * @return True if the event should be consumed.
494    */
495   bool OnTouch( Actor actor, const TouchData& touch );
496
497   /**
498    * Called upon pan gesture event.
499    *
500    * @param[in] gesture The gesture event.
501    */
502   void OnPan( const PanGesture& pan );
503
504   /**
505    * Helper to handle anchoring animations.
506    * @return The animation, or an uninitialized handle if not necessary.
507    */
508   Animation DoAnchoring();
509
510   /**
511    * Callback from scroll animations
512    * @param[in] animation The scroll-animation which has finished.
513    */
514   void OnScrollFinished(Animation& animation);
515
516   /**
517    * Callback from layout activation scroll animations
518    * @param[in] animation The scroll-animation which has finished.
519    */
520   void OnLayoutActivationScrollFinished(Animation& animation);
521
522   /**
523    * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
524    *
525    * @param[in] animation the animation that has finished
526    */
527   void OnOvershootOnFinished(Animation& animation);
528
529   /**
530    * This is called after a timeout when no new wheel event is received for a certain period of time.
531    * @return will return false; one-shot timer.
532    */
533   bool OnWheelEventFinished();
534
535   /**
536    * Stops and removes animation if exists.
537    * @param[in,out] animation The animation handle to be removed.
538    */
539   void RemoveAnimation(Animation& animation);
540
541   /**
542    * @copydoc Toolkit::Internal::Scrollable::EnableScrollOvershoot
543    */
544   virtual void EnableScrollOvershoot( bool enable );
545
546   /**
547    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
548    * @return The scroll overshoot.
549    */
550   float CalculateScrollOvershoot();
551
552   /**
553    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
554    *
555    * @param[in] overshootAmount amount to animate to
556    * @param[in] animateBack whether to animate back to zero immediately after
557    * @return The scroll overshoot.
558    */
559   void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
560
561   /**
562    * Gets the scroll position in pixels according to the logical layout position.
563    * @param[in] layoutSize The current size of the layout.
564    */
565   float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
566
567   /**
568    * Calculates the minimum and maximum positions for each axis to scroll to.
569    * @param[in] layoutSize The current size of the layout.
570    */
571   void CalculateDomainSize(const Vector3& layoutSize);
572
573   /**
574    * Calculates whether we want to allow current item view to scroll.
575    * @param[in] layoutSize The current size of the layout.
576    * @return    true if itemview is scrollable
577    */
578   bool IsLayoutScrollable(const Vector3& layoutSize);
579
580   /**
581    * Callback when the current layout position of ItemView changes in both positive
582    * and negative directions by the specified amount. Refresh the ItemView to create
583    * newly visible items.
584    * @param[in] source the property notification that triggered this callback
585    */
586   void OnRefreshNotification(PropertyNotification& source);
587
588 private:
589
590   typedef std::map<unsigned int, Actor> ItemPool;
591   typedef ItemPool::iterator            ItemPoolIter;
592   typedef ItemPool::const_iterator      ConstItemPoolIter;
593
594   ItemPool mItemPool;
595   ItemFactory& mItemFactory;
596   std::vector< ItemLayoutPtr > mLayouts;            ///< Container of Dali::Toolkit::ItemLayout objects
597   Actor mOvershootOverlay;                          ///< The overlay actor for overshoot effect
598   Animation mResizeAnimation;
599   Animation mScrollAnimation;
600   Animation mScrollOvershootAnimation;
601   Timer mWheelEventFinishedTimer;                   ///< The timer to determine whether there is no wheel event received for a certain period of time.
602   PropertyNotification mRefreshNotification;        ///< Stores the property notification used for item view refresh
603   LayoutActivatedSignalType mLayoutActivatedSignal;
604   Vector3 mActiveLayoutTargetSize;
605   Vector3 mItemsParentOrigin;
606   Vector3 mItemsAnchorPoint;
607   Vector2 mTotalPanDisplacement;
608   ItemLayout* mActiveLayout;
609
610   float mAnchoringDuration;
611   float mRefreshIntervalLayoutPositions;            ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
612   float mMinimumSwipeSpeed;
613   float mMinimumSwipeDistance;
614   float mWheelScrollDistanceStep;                   ///< The step of scroll distance in actor coordinates for each wheel event received.
615   float mScrollDistance;
616   float mScrollSpeed;
617   float mScrollOvershoot;
618
619   Dali::Gesture::State mGestureState    : 3;
620   bool mAnimatingOvershootOn            : 1;        ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
621   bool mAnimateOvershootOff             : 1;        ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
622   bool mAnchoringEnabled                : 1;
623   bool mRefreshOrderHint                : 1;        ///< True if scrolling towards the last item
624   bool mIsFlicking                      : 1;
625   bool mAddingItems                     : 1;
626   bool mRefreshEnabled                  : 1;        ///< Whether to refresh the cache automatically
627   bool mInAnimation                     : 1;        ///< Keeps track of whether an animation is controlling the overshoot property.
628 };
629
630 } // namespace Internal
631
632 // Helpers for public-api forwarding methods
633
634 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
635 {
636   DALI_ASSERT_ALWAYS(itemView);
637
638   Dali::RefObject& handle = itemView.GetImplementation();
639
640   return static_cast<Toolkit::Internal::ItemView&>(handle);
641 }
642
643 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
644 {
645   DALI_ASSERT_ALWAYS(itemView);
646
647   const Dali::RefObject& handle = itemView.GetImplementation();
648
649   return static_cast<const Toolkit::Internal::ItemView&>(handle);
650 }
651
652 } // namespace Toolkit
653
654 } // namespace Dali
655
656 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__