Merge remote-tracking branch 'origin/tizen' into new_text
[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
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/public-api/controls/control-impl.h>
28 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
29 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
30 #include <dali-toolkit/public-api/controls/scrollable/scroll-connector.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   /**
57    * Create a new ItemView.
58    * @param[in] factory The factory which provides ItemView with items.
59    * @return A public handle to the newly allocated ItemView.
60    */
61   static Dali::Toolkit::ItemView New(ItemFactory& factory);
62
63   /**
64    * @copydoc Toolkit::ItemView::GetScrollConnector
65    */
66   Dali::Toolkit::ScrollConnector GetScrollConnector() const;
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::SetMouseWheelScrollDistanceStep
130    */
131   void SetMouseWheelScrollDistanceStep(float step);
132
133   /**
134    * @copydoc Toolkit::ItemView::GetMouseWheelScrollDistanceStep
135    */
136   float GetMouseWheelScrollDistanceStep() 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::GetItem
175    */
176   Actor GetItem(unsigned int itemId) const;
177
178   /**
179    * @copydoc Toolkit::ItemView::GetItemId
180    */
181   unsigned int GetItemId(Actor actor) const;
182
183   /**
184    * @copydoc Toolkit::ItemView::InsertItem
185    */
186   void InsertItem(Item newItem, float durationSeconds);
187
188   /**
189    * @copydoc Toolkit::ItemView::InsertItem
190    */
191   void InsertItems(const ItemContainer& newItems, float durationSeconds);
192
193   /**
194    * @copydoc Toolkit::ItemView::RemoveItem
195    */
196   void RemoveItem(ItemId itemId, float durationSeconds);
197
198   /**
199    * @copydoc Toolkit::ItemView::InsertItem
200    */
201   void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
202
203   /**
204    * @copydoc Toolkit::ItemView::InsertItem
205    */
206   void ReplaceItem(Item replacementItem, float durationSeconds);
207
208   /**
209    * @copydoc Toolkit::ItemView::InsertItem
210    */
211   void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
212
213   /**
214    * @copydoc Toolkit::Scrollable::GetDomainSize
215    */
216   Vector3 GetDomainSize() const;
217
218   /**
219    * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
220    */
221   Vector3 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 Vector3& position, float duration)
235    */
236   void ScrollTo(const Vector3& 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 private:
288
289   /**
290    * Remove an Actor if found in the ItemPool.
291    * @param[in] itemId The item to remove.
292    * @return True if the remaining actors were reordered.
293    */
294   bool RemoveActor( unsigned int itemId );
295
296   /**
297    * Remove any Actors outside a given range.
298    * @param[in] @param[in] range The range of required items.
299    */
300   void RemoveActorsOutsideRange( ItemRange range );
301
302   /**
303    * Add a range of Actors, if they are not already in the ItemPool.
304    * @param[in] range The range of Item IDs to associate with the new actors.
305    * @param[in] durationSeconds The time taken to fully constrain the newly added actor.
306    */
307   void AddActorsWithinRange( ItemRange range, float durationSeconds );
308
309   /**
310    * Add a new Actor, if not already in the ItemPool.
311    * @param[in] item The ID for the new item.
312    * @param[in] durationSeconds The time taken to fully constrain the new actor.
313    */
314   void AddNewActor( ItemId item, float durationSeconds );
315
316   /**
317    * Apply the constraints etc. that are required for ItemView children.
318    * @param[in] item The item to setup.
319    * @param[in] durationSeconds The time taken to fully constrain the actor.
320    */
321   void SetupActor( Item item, float durationSeconds );
322
323   /**
324    * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
325    * @param[in] item The ID for the item to be released.
326    * @param[in] actor The actor to be removed from ItemView.
327    */
328   void ReleaseActor( ItemId item, Actor actor );
329
330 private: // From CustomActorImpl
331
332   /**
333    * From CustomActorImpl; called after a child has been added to the owning actor.
334    * @param[in] child The child which has been added.
335    */
336   virtual void OnChildAdd(Actor& child);
337
338   /**
339    * From CustomActorImpl; called after a touch-signal is received by the owning actor.
340    * @param[in] event The touch event.
341    * @return True if the event should be consumed.
342    */
343   virtual bool OnTouchEvent(const TouchEvent& event);
344
345   /**
346    * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
347    * @param[in] event The mouse wheel event.
348    * @return True if the event should be consumed.
349    */
350   virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
351
352 private: // From Control
353
354   /**
355    * @copydoc Toolkit::Control::OnInitialize()
356    */
357   virtual void OnInitialize();
358
359   /**
360    * @copydoc Toolkit::Control::OnAccessibilityPan()
361    */
362   virtual bool OnAccessibilityPan(PanGesture gesture);
363
364   /**
365    * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
366    */
367   virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
368
369   /**
370    * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
371    */
372   virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
373
374 protected:
375
376   /**
377    * Construct a new ItemView.
378    * @param[in] factory The factory which provides ItemView with items.
379    */
380   ItemView(ItemFactory& factory);
381
382   /**
383    * A reference counted object may only be deleted by calling Unreference()
384    */
385   virtual ~ItemView();
386
387 private:
388
389   // Undefined
390   ItemView(const ItemView&);
391
392   // Undefined
393   ItemView& operator=(const ItemView& rhs);
394
395   /**
396    * Helper to re-apply all the constraints after items have been inserted, removed etc.
397    * @param[in] durationSeconds The time taken to fully constrain the actors.
398    */
399   void ReapplyAllConstraints( float durationSeconds );
400
401   /**
402    * Helper to relayout after item(s) are removed.
403    */
404   void OnItemsRemoved();
405
406   /**
407    * Helper to remove items outside a given range.
408    * @param[in] range The range of required items.
409    */
410   void RemoveItems(ItemRange range);
411
412   /**
413    * Helper to add a range of items, if not already in the ItemPool.
414    * @param[in] layout The layout used to position the new items.
415    * @param[in] layoutSize The current size of the layout.
416    * @param[in] range The range of required items.
417    */
418   void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
419
420   /**
421    * Helper to find the range of items to populate with.
422    * @param[in] layout The current layout.
423    * @param[in] range The range of items.
424    * @param[in] reserveExtra True if reserve items should be included.
425    */
426   ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
427
428   // Input Handling
429
430   /**
431    * Helper to handle pressed (Down) events.
432    * @param[in] x The X coordinate of the touch event.
433    * @param[in] y The Y coordinate of the touch event.
434    * @param[in] timeMs The time-stamp of the touch event.
435    */
436   void OnPressed(float x, float y, unsigned long timeMs);
437
438   /**
439    * Helper to clamp the first-item position when dragging/swiping.
440    * @param[in] targetPosition The target position of the drag etc.
441    * @param[in] targetSize The target ItemView & layout size.
442    * @param[in] layout The current layout.
443    * @return The clamped first-item position.
444    */
445   float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
446
447   /**
448    * Called upon pan gesture event.
449    *
450    * @param[in] gesture The gesture event.
451    */
452   void OnPan( const PanGesture& pan );
453
454   /**
455    * Helper to handle anchoring animations.
456    * @return The animation, or an uninitialized handle if not necessary.
457    */
458   Animation DoAnchoring();
459
460   /**
461    * Callback from scroll animations
462    * @param[in] animation The scroll-animation which has finished.
463    */
464   void OnScrollFinished(Animation& animation);
465
466   /**
467    * Callback from layout activation scroll animations
468    * @param[in] animation The scroll-animation which has finished.
469    */
470   void OnLayoutActivationScrollFinished(Animation& animation);
471
472   /**
473    * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
474    *
475    * @param[in] animation the animation that has finished
476    */
477   void OnOvershootOnFinished(Animation& animation);
478
479   /**
480    * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
481    * @return will return false; one-shot timer.
482    */
483   bool OnMouseWheelEventFinished();
484
485   /**
486    * Stops and removes animation if exists.
487    * @param[in,out] animation The animation handle to be removed.
488    */
489   void RemoveAnimation(Animation& animation);
490
491   /**
492    * @copydoc Toolkit::Internal::Scrollable::SetOvershootEnabled
493    */
494   virtual void SetOvershootEnabled( bool enable );
495
496   /**
497    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
498    * @return The scroll overshoot.
499    */
500   float CalculateScrollOvershoot();
501
502   /**
503    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
504    *
505    * @param[in] overshootAmount amount to animate to
506    * @param[in] animateBack whether to animate back to zero immediately after
507    * @return The scroll overshoot.
508    */
509   void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
510
511   /**
512    * Gets the scroll position in pixels according to the logical layout position.
513    * @param[in] layoutSize The current size of the layout.
514    */
515   float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
516
517   /**
518    * Calculates the minimum and maximum positions for each axis to scroll to.
519    * @param[in] layoutSize The current size of the layout.
520    */
521   void CalculateDomainSize(const Vector3& layoutSize);
522
523   /**
524    * Calculates whether we want to allow current item view to scroll.
525    * @param[in] layoutSize The current size of the layout.
526    * @return    true if itemview is scrollable
527    */
528   bool IsLayoutScrollable(const Vector3& layoutSize);
529
530   /**
531    * Callback when the current layout position of ItemView changes in both positive
532    * and negative directions by the specified amount. Refresh the ItemView to create
533    * newly visible items.
534    * @param[in] source the property notification that triggered this callback
535    */
536   void OnRefreshNotification(PropertyNotification& source);
537
538   /**
539    * This is called when scroll position has been changed by ScrollConnector::SetScrollPosition.
540    * @param[in] position The new scroll position
541    */
542   void OnScrollPositionChanged( float position );
543
544 private:
545
546   ItemFactory& mItemFactory;
547
548   typedef std::map<unsigned int, Actor> ItemPool;
549   typedef ItemPool::iterator            ItemPoolIter;
550   typedef ItemPool::const_iterator      ConstItemPoolIter;
551
552   ItemPool mItemPool;
553
554   ItemLayoutContainer mLayouts;
555   ItemLayout* mActiveLayout;
556   Vector3 mActiveLayoutTargetSize;
557
558   Animation mResizeAnimation;
559   Animation mScrollAnimation;
560   Animation mScrollOvershootAnimation;
561   bool      mAnimatingOvershootOn;          ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
562   bool      mAnimateOvershootOff;         ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
563
564   bool mAnchoringEnabled;
565   float mAnchoringDuration;
566
567   float mRefreshIntervalLayoutPositions;  ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
568   PropertyNotification mRefreshNotification; // stores the property notification used for item view refresh
569   bool mRefreshOrderHint; ///< True if scrolling towards the last item
570
571   // Input handling
572
573   float mMinimumSwipeSpeed;
574   float mMinimumSwipeDistance;
575   float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
576
577   float mScrollDistance;
578   float mScrollSpeed;
579   Vector2 mTotalPanDisplacement;
580
581   float mScrollOvershoot;
582   bool mIsFlicking;
583
584   Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
585
586   Dali::Gesture::State mGestureState;
587
588   Actor mOvershootOverlay;           ///< The overlay actor for overshoot effect
589
590   Dali::Toolkit::ScrollConnector mScrollConnector; ///< Connects ItemView with scrollable components e.g. scroll bars
591   Constrainable   mScrollPositionObject;     ///< From mScrollConnector
592
593   bool mAddingItems;
594
595   Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
596   Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
597   Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
598
599   bool mRefreshEnabled; ///< Whether to refresh the cache automatically
600
601   Vector3 mItemsParentOrigin;
602   Vector3 mItemsAnchorPoint;
603 };
604
605 } // namespace Internal
606
607 // Helpers for public-api forwarding methods
608
609 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
610 {
611   DALI_ASSERT_ALWAYS(itemView);
612
613   Dali::RefObject& handle = itemView.GetImplementation();
614
615   return static_cast<Toolkit::Internal::ItemView&>(handle);
616 }
617
618 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
619 {
620   DALI_ASSERT_ALWAYS(itemView);
621
622   const Dali::RefObject& handle = itemView.GetImplementation();
623
624   return static_cast<const Toolkit::Internal::ItemView&>(handle);
625 }
626
627 } // namespace Toolkit
628
629 } // namespace Dali
630
631 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__