Merge "Removed invalid handle assignments from Builder/TextInput" into tizen
[platform/core/uifw/dali-toolkit.git] / base / 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/dali.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/controls/control-impl.h>
26 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
27 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
28 #include <dali-toolkit/public-api/controls/scrollable/scroll-connector.h>
29 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
30 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 class ItemView;
42
43 typedef IntrusivePtr<ItemView> ItemViewPtr;
44
45 /**
46  * ItemView is a scrollable layout container.
47  * Multiple ItemLayouts may be provided, to determine the logical position of each item a layout.
48  * Actor-ID pairs are provided from a shared ItemFactory, to display the currently visible items.
49  */
50 class ItemView : public Scrollable
51 {
52 public:
53
54   /**
55    * Create a new ItemView.
56    * @param[in] factory The factory which provides ItemView with items.
57    * @return A public handle to the newly allocated ItemView.
58    */
59   static Dali::Toolkit::ItemView New(ItemFactory& factory);
60
61   /**
62    * @copydoc Toolkit::ItemView::GetScrollConnector
63    */
64   Dali::Toolkit::ScrollConnector GetScrollConnector() const;
65
66   /**
67    * @copydoc Toolkit::ItemView::GetLayoutCount
68    */
69   unsigned int GetLayoutCount() const;
70
71   /**
72    * @copydoc Toolkit::ItemView::AddLayout
73    */
74   void AddLayout(ItemLayout& layout);
75
76   /**
77    * @copydoc Toolkit::ItemView::RemoveLayout
78    */
79   void RemoveLayout(unsigned int layoutIndex);
80
81   /**
82    * @copydoc Toolkit::ItemView::GetLayout
83    */
84   ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
85
86   /**
87    * @copydoc Toolkit::ItemView::GetActiveLayout
88    */
89   ItemLayoutPtr GetActiveLayout() const;
90
91   /**
92    * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
93    */
94   float GetCurrentLayoutPosition(unsigned int itemId) const;
95
96   /**
97    * @copydoc Toolkit::ItemView::ActivateLayout
98    */
99   void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
100
101   /**
102    * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
103    */
104   void DeactivateCurrentLayout();
105
106   /**
107    * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
108    */
109   void SetMinimumSwipeSpeed(float speed);
110
111   /**
112    * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
113    */
114   float GetMinimumSwipeSpeed() const;
115
116   /**
117    * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
118    */
119   void SetMinimumSwipeDistance(float distance);
120
121   /**
122    * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
123    */
124   float GetMinimumSwipeDistance() const;
125
126   /**
127    * @copydoc Toolkit::ItemView::SetMouseWheelScrollDistanceStep
128    */
129   void SetMouseWheelScrollDistanceStep(float step);
130
131   /**
132    * @copydoc Toolkit::ItemView::GetMouseWheelScrollDistanceStep
133    */
134   float GetMouseWheelScrollDistanceStep() const;
135
136   /**
137    * @copydoc Toolkit::ItemView::SetAnchoring
138    */
139   void SetAnchoring(bool enabled);
140
141   /**
142    * @copydoc Toolkit::ItemView::GetAnchoring
143    */
144   bool GetAnchoring() const;
145
146   /**
147    * @copydoc Toolkit::ItemView::SetAnchoringDuration
148    */
149   void SetAnchoringDuration(float durationSeconds);
150
151   /**
152    * @copydoc Toolkit::ItemView::GetAnchoringDuration
153    */
154   float GetAnchoringDuration() const;
155
156   /**
157    * @copydoc Toolkit::ItemView::ScrollToItem
158    */
159   void ScrollToItem(unsigned int itemId, float durationSeconds);
160
161   /**
162    * @copydoc Toolkit::ItemView::SetRefreshInterval
163    */
164   void SetRefreshInterval(float intervalLayoutPositions);
165
166   /**
167    * @copydoc Toolkit::ItemView::GetRefreshInterval
168    */
169   float GetRefreshInterval() const;
170
171   /**
172    * @copydoc Toolkit::ItemView::GetItem
173    */
174   Actor GetItem(unsigned int itemId) const;
175
176   /**
177    * @copydoc Toolkit::ItemView::GetItemId
178    */
179   unsigned int GetItemId(Actor actor) const;
180
181   /**
182    * @copydoc Toolkit::ItemView::InsertItem
183    */
184   void InsertItem(Item newItem, float durationSeconds);
185
186   /**
187    * @copydoc Toolkit::ItemView::InsertItem
188    */
189   void InsertItems(const ItemContainer& newItems, float durationSeconds);
190
191   /**
192    * @copydoc Toolkit::ItemView::RemoveItem
193    */
194   void RemoveItem(ItemId itemId, float durationSeconds);
195
196   /**
197    * @copydoc Toolkit::ItemView::InsertItem
198    */
199   void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
200
201   /**
202    * @copydoc Toolkit::ItemView::InsertItem
203    */
204   void ReplaceItem(Item replacementItem, float durationSeconds);
205
206   /**
207    * @copydoc Toolkit::ItemView::InsertItem
208    */
209   void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
210
211   /**
212    * @copydoc Toolkit::Scrollable::GetDomainSize
213    */
214   Vector3 GetDomainSize() const;
215
216   /**
217    * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
218    */
219   Vector3 GetCurrentScrollPosition() const;
220
221   /**
222    * @copydoc Toolkit::Scrollable::AddOverlay()
223    */
224   void AddOverlay(Actor actor);
225
226   /**
227    * @copydoc Toolkit::Scrollable::RemoveOverlay()
228    */
229   void RemoveOverlay(Actor actor);
230
231   /**
232    * @copydoc Toolkit::Scrollable::ScrollTo(const Vector3& position, float duration)
233    */
234   void ScrollTo(const Vector3& position, float duration);
235
236   /**
237    * @copydoc Toolkit::Internal::Scrollable::SetOvershootEffectColor
238    */
239   void SetOvershootEffectColor( const Vector4& color );
240
241   /**
242    * @brief Set whether to enable automatic refresh or not. When refresh is disabled,
243    * ItemView will not automatically refresh the cache in the given interval when the
244    * layout position is changed. This is useful in some cases, for example, automatic
245    * refresh is not needed during fast scrolling, otherwise it will cache unneeded
246    * items when the layout position changes quickly.
247    *
248    * @param[in] enabled True to enable automatic refresh or false to disable it.
249    */
250   void SetRefreshEnabled(bool enabled);
251
252   /**
253    * @brief Helper to perform the refresh.
254    *
255    * @param[in] currentLayoutPosition The current layout position.
256    * @param[in] cacheExtra Whether to cache extra items during refresh.
257    */
258   void DoRefresh(float currentLayoutPosition, bool cacheExtra);
259
260   /**
261    * @copydoc Toolkit::ItemView::SetItemsParentOrigin
262    */
263   void SetItemsParentOrigin( const Vector3& parentOrigin );
264
265   /**
266    * @copydoc Toolkit::ItemView::GetItemsParentOrigin
267    */
268   Vector3 GetItemsParentOrigin() const;
269
270   /**
271    * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
272    */
273   void SetItemsAnchorPoint( const Vector3& anchorPoint );
274
275   /**
276    * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
277    */
278   Vector3 GetItemsAnchorPoint() const;
279
280   /**
281    * @copydoc Toolkit::ItemView::GetItemsRange
282    */
283   void GetItemsRange(ItemRange& range);
284
285 private:
286
287   /**
288    * Remove an Actor if found in the ItemPool.
289    * @param[in] itemId The item to remove.
290    * @return True if an actor was removed.
291    */
292   bool RemoveActor( unsigned int itemId );
293
294   /**
295    * Remove any Actors outside a given range.
296    * @param[in] @param[in] range The range of required items.
297    */
298   void RemoveActorsOutsideRange( ItemRange range );
299
300   /**
301    * Add a range of Actors, if they are not already in the ItemPool.
302    * @param[in] range The range of Item IDs to associate with the new actors.
303    * @param[in] durationSeconds The time taken to fully constrain the newly added actor.
304    */
305   void AddActorsWithinRange( ItemRange range, float durationSeconds );
306
307   /**
308    * Add a new Actor, if not already in the ItemPool.
309    * @param[in] item The ID for the new item.
310    * @param[in] durationSeconds The time taken to fully constrain the new actor.
311    */
312   void AddNewActor( ItemId item, float durationSeconds );
313
314   /**
315    * Apply the constraints etc. that are required for ItemView children.
316    * @param[in] item The item to setup.
317    * @param[in] durationSeconds The time taken to fully constrain the actor.
318    */
319   void SetupActor( Item item, float durationSeconds );
320
321   /**
322    * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
323    * @param[in] item The ID for the item to be released.
324    * @param[in] actor The actor to be removed from ItemView.
325    */
326   void ReleaseActor( ItemId item, Actor actor );
327
328 private: // From CustomActorImpl
329
330   /**
331    * From CustomActorImpl; called after a child has been added to the owning actor.
332    * @param[in] child The child which has been added.
333    */
334   virtual void OnChildAdd(Actor& child);
335
336   /**
337    * From CustomActorImpl; called after a touch-signal is received by the owning actor.
338    * @param[in] event The touch event.
339    * @return True if the event should be consumed.
340    */
341   virtual bool OnTouchEvent(const TouchEvent& event);
342
343   /**
344    * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
345    * @param[in] event The mouse wheel event.
346    * @return True if the event should be consumed.
347    */
348   virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
349
350 private: // From Control
351
352   /**
353    * @copydoc Toolkit::Control::OnInitialize()
354    */
355   virtual void OnInitialize();
356
357   /**
358    * @copydoc Toolkit::Control::OnAccessibilityPan()
359    */
360   virtual bool OnAccessibilityPan(PanGesture gesture);
361
362   /**
363    * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
364    */
365   virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
366
367   /**
368    * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
369    */
370   virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
371
372 protected:
373
374   /**
375    * Construct a new ItemView.
376    * @param[in] factory The factory which provides ItemView with items.
377    */
378   ItemView(ItemFactory& factory);
379
380   /**
381    * A reference counted object may only be deleted by calling Unreference()
382    */
383   virtual ~ItemView();
384
385 private:
386
387   // Undefined
388   ItemView(const ItemView&);
389
390   // Undefined
391   ItemView& operator=(const ItemView& rhs);
392
393   /**
394    * Helper to re-apply all the constraints after items have been inserted, removed etc.
395    * @param[in] durationSeconds The time taken to fully constrain the actors.
396    */
397   void ReapplyAllConstraints( float durationSeconds );
398
399   /**
400    * Helper to remove items outside a given range.
401    * @param[in] range The range of required items.
402    */
403   void RemoveItems(ItemRange range);
404
405   /**
406    * Helper to add a range of items, if not already in the ItemPool.
407    * @param[in] layout The layout used to position the new items.
408    * @param[in] layoutSize The current size of the layout.
409    * @param[in] range The range of required items.
410    */
411   void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
412
413   /**
414    * Helper to find the range of items to populate with.
415    * @param[in] layout The current layout.
416    * @param[in] range The range of items.
417    * @param[in] reserveExtra True if reserve items should be included.
418    */
419   ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
420
421   // Input Handling
422
423   /**
424    * Helper to handle pressed (Down) events.
425    * @param[in] x The X coordinate of the touch event.
426    * @param[in] y The Y coordinate of the touch event.
427    * @param[in] timeMs The time-stamp of the touch event.
428    */
429   void OnPressed(float x, float y, unsigned long timeMs);
430
431   /**
432    * Helper to clamp the first-item position when dragging/swiping.
433    * @param[in] targetPosition The target position of the drag etc.
434    * @param[in] targetSize The target ItemView & layout size.
435    * @param[in] layout The current layout.
436    * @return The clamped first-item position.
437    */
438   float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
439
440   /**
441    * Called upon pan gesture event.
442    *
443    * @param[in] gesture The gesture event.
444    */
445   void OnPan(PanGesture pan);
446
447   /**
448    * Helper to handle anchoring animations.
449    * @return The animation, or an uninitialized handle if not necessary.
450    */
451   Animation DoAnchoring();
452
453   /**
454    * Callback from scroll animations
455    * @param[in] animation The scroll-animation which has finished.
456    */
457   void OnScrollFinished(Animation& animation);
458
459   /**
460    * Callback from layout activation scroll animations
461    * @param[in] animation The scroll-animation which has finished.
462    */
463   void OnLayoutActivationScrollFinished(Animation& animation);
464
465   /**
466    * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
467    *
468    * @param[in] animation the animation that has finished
469    */
470   void OnOvershootOnFinished(Animation& animation);
471
472   /**
473    * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
474    * @return will return false; one-shot timer.
475    */
476   bool OnMouseWheelEventFinished();
477
478   /**
479    * Stops and removes animation if exists.
480    * @param[in,out] animation The animation handle to be removed.
481    */
482   void RemoveAnimation(Animation& animation);
483
484   /**
485    * @copydoc Toolkit::Internal::Scrollable::SetOvershootEnabled
486    */
487   virtual void SetOvershootEnabled( bool enable );
488
489   /**
490    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
491    * @return The scroll overshoot.
492    */
493   float CalculateScrollOvershoot();
494
495   /**
496    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
497    *
498    * @param[in] overshootAmount amount to animate to
499    * @param[in] animateBack whether to animate back to zero immediately after
500    * @return The scroll overshoot.
501    */
502   void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
503
504   /**
505    * Gets the scroll position in pixels according to the logical layout position.
506    * @param[in] layoutSize The current size of the layout.
507    */
508   float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
509
510   /**
511    * Calculates the minimum and maximum positions for each axis to scroll to.
512    * @param[in] layoutSize The current size of the layout.
513    */
514   void CalculateDomainSize(const Vector3& layoutSize);
515
516   /**
517    * Calculates whether we want to allow current item view to scroll.
518    * @param[in] layoutSize The current size of the layout.
519    * @return    true if itemview is scrollable
520    */
521   bool IsLayoutScrollable(const Vector3& layoutSize);
522
523   /**
524    * Callback when the current layout position of ItemView changes in both positive
525    * and negative directions by the specified amount. Refresh the ItemView to create
526    * newly visible items.
527    * @param[in] source the property notification that triggered this callback
528    */
529   void OnRefreshNotification(PropertyNotification& source);
530
531   /**
532    * This is called when scroll position has been changed by ScrollConnector::SetScrollPosition.
533    * @param[in] position The new scroll position
534    */
535   void OnScrollPositionChanged( float position );
536
537 private:
538
539   ItemFactory& mItemFactory;
540
541   typedef std::map<unsigned int, Actor> ItemPool;
542   typedef ItemPool::iterator            ItemPoolIter;
543   typedef ItemPool::const_iterator      ConstItemPoolIter;
544
545   ItemPool mItemPool;
546
547   ItemLayoutContainer mLayouts;
548   ItemLayout* mActiveLayout;
549   Vector3 mActiveLayoutTargetSize;
550
551   Animation mResizeAnimation;
552   Animation mScrollAnimation;
553   Animation mScrollOvershootAnimation;
554   bool      mAnimatingOvershootOn;          ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
555   bool      mAnimateOvershootOff;         ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
556
557   bool mAnchoringEnabled;
558   float mAnchoringDuration;
559
560   float mRefreshIntervalLayoutPositions;  ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
561   PropertyNotification mRefreshNotification; // stores the property notification used for item view refresh
562   bool mRefreshOrderHint; ///< True if scrolling towards the last item
563
564   // Input handling
565
566   float mMinimumSwipeSpeed;
567   float mMinimumSwipeDistance;
568   float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
569
570   float mScrollDistance;
571   float mScrollSpeed;
572   Vector2 mTotalPanDisplacement;
573
574   float mScrollOvershoot;
575   bool mIsFlicking;
576
577   Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
578
579   Dali::Gesture::State mGestureState;
580
581   Actor mOvershootOverlay;           ///< The overlay actor for overshoot effect
582
583   Dali::Toolkit::ScrollConnector mScrollConnector; ///< Connects ItemView with scrollable components e.g. scroll bars
584   Constrainable   mScrollPositionObject;     ///< From mScrollConnector
585
586   bool mAddingItems;
587
588   Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
589   Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
590   Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
591
592   bool mRefreshEnabled; ///< Whether to refresh the cache automatically
593
594   Vector3 mItemsParentOrigin;
595   Vector3 mItemsAnchorPoint;
596 };
597
598 } // namespace Internal
599
600 // Helpers for public-api forwarding methods
601
602 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
603 {
604   DALI_ASSERT_ALWAYS(itemView);
605
606   Dali::RefObject& handle = itemView.GetImplementation();
607
608   return static_cast<Toolkit::Internal::ItemView&>(handle);
609 }
610
611 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
612 {
613   DALI_ASSERT_ALWAYS(itemView);
614
615   const Dali::RefObject& handle = itemView.GetImplementation();
616
617   return static_cast<const Toolkit::Internal::ItemView&>(handle);
618 }
619
620 } // namespace Toolkit
621
622 } // namespace Dali
623
624 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__