Fix memory leak caused by the handle of scrollable object stored in ScrollBar
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scroll-bar / scroll-bar-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_SCROLL_BAR_H__
2 #define __DALI_TOOLKIT_INTERNAL_SCROLL_BAR_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/actors/image-actor.h>
24 #include <dali/public-api/animation/constraint.h>
25 #include <dali/public-api/animation/animation.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/object/property-notification.h>
28 #include <dali/devel-api/object/weak-handle.h>
29
30 // INTERNAL INCLUDES
31 #include <dali-toolkit/public-api/controls/control-impl.h>
32 #include <dali-toolkit/public-api/controls/scroll-bar/scroll-bar.h>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 class ScrollBar;
44
45 typedef IntrusivePtr<ScrollBar> ScrollBarPtr;
46
47 /**
48  * ScrollBar is a UI component that can be added to the scrollable controls
49  * indicating the current scroll position of the scrollable content.
50  */
51 class ScrollBar : public Control
52 {
53
54 public:
55
56   // Signals
57   typedef Toolkit::ScrollBar::PanFinishedSignalType PanFinishedSignalType;
58   typedef Toolkit::ScrollBar::ScrollPositionIntervalReachedSignalType ScrollPositionIntervalReachedSignalType;
59
60 public:
61
62   /**
63    * @copydoc Toolkit::ScrollBar::New()
64    */
65   static Toolkit::ScrollBar New(Toolkit::ScrollBar::Direction direction);
66
67   /**
68    * @copydoc Toolkit::ScrollBar::SetScrollPropertySource()
69    */
70   void SetScrollPropertySource( Handle handle, Property::Index propertyScrollPosition, Property::Index propertyMinScrollPosition, Property::Index propertyMaxScrollPosition, Property::Index propertyScrollContentSize );
71
72   /**
73    * @copydoc Toolkit::ScrollBar::SetScrollIndicator()
74    */
75   void SetScrollIndicator( Actor indicator );
76
77   /**
78    * @copydoc Toolkit::ScrollBar::GetScrollIndicator()
79    */
80   Actor GetScrollIndicator();
81
82   /**
83    * @copydoc Toolkit::ScrollBar::SetScrollPositionIntervals()
84    */
85   void SetScrollPositionIntervals( const Dali::Vector<float>& positions );
86
87   /**
88    * @copydoc Toolkit::ScrollBar::GetScrollPositionIntervals()
89    */
90   Dali::Vector<float> GetScrollPositionIntervals() const;
91
92   /**
93    * @copydoc Toolkit::ScrollBar::SetScrollDirection()
94    */
95   void SetScrollDirection( Toolkit::ScrollBar::Direction direction );
96
97   /**
98    * @copydoc Toolkit::ScrollBar::GetScrollDirection()
99    */
100   Toolkit::ScrollBar::Direction GetScrollDirection() const;
101
102   /**
103    * @copydoc Toolkit::ScrollBar::SetIndicatorHeightPolicy()
104    */
105   void SetIndicatorHeightPolicy( Toolkit::ScrollBar::IndicatorHeightPolicy policy );
106
107   /**
108    * @copydoc Toolkit::ScrollBar::GetIndicatorHeightPolicy()
109    */
110   Toolkit::ScrollBar::IndicatorHeightPolicy GetIndicatorHeightPolicy() const;
111
112   /**
113    * @copydoc Toolkit::ScrollBar::SetIndicatorFixedHeight()
114    */
115   void SetIndicatorFixedHeight( float height );
116
117   /**
118    * @copydoc Toolkit::ScrollBar::GetIndicatorFixedHeight()
119    */
120   float GetIndicatorFixedHeight() const;
121
122   /**
123    * @copydoc Toolkit::ScrollBar::SetIndicatorShowDuration()
124    */
125   void SetIndicatorShowDuration( float durationSeconds );
126
127   /**
128    * @copydoc Toolkit::ScrollBar::GetIndicatorShowDuration()
129    */
130   float GetIndicatorShowDuration() const;
131
132   /**
133    * @copydoc Toolkit::ScrollBar::SetIndicatorHideDuration()
134    */
135   void SetIndicatorHideDuration( float durationSeconds );
136
137   /**
138    * @copydoc Toolkit::ScrollBar::GetIndicatorHideDuration()
139    */
140   float GetIndicatorHideDuration() const;
141
142   /**
143    * @copydoc Toolkit::ScrollBar::ShowIndicator()
144    */
145   void ShowIndicator();
146
147   /**
148    * @copydoc Toolkit::ScrollBar::HideIndicator()
149    */
150  void HideIndicator();
151
152  /**
153   * @copydoc Toolkit::ScrollBar::PanFinishedSignal()
154   */
155  PanFinishedSignalType& PanFinishedSignal()
156  {
157    return mPanFinishedSignal;
158  }
159
160  /**
161   * @copydoc Toolkit::ScrollBar::ScrollPositionIntervalReachedSignal()
162   */
163  ScrollPositionIntervalReachedSignalType& ScrollPositionIntervalReachedSignal()
164  {
165    return mScrollPositionIntervalReachedSignal;
166  }
167
168  /**
169   * Connects a callback function with the object's signals.
170   * @param[in] object The object providing the signal.
171   * @param[in] tracker Used to disconnect the signal.
172   * @param[in] signalName The signal to connect to.
173   * @param[in] functor A newly allocated FunctorDelegate.
174   * @return True if the signal was connected.
175   * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
176   */
177  static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
178
179  // Properties
180
181  /**
182   * Called when a property of an object of this type is set.
183   * @param[in] object The object whose property is set.
184   * @param[in] index The property index.
185   * @param[in] value The new property value.
186   */
187  static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
188
189  /**
190   * Called to retrieve a property of an object of this type.
191   * @param[in] object The object whose property is to be retrieved.
192   * @param[in] index The property index.
193   * @return The current value of the property.
194   */
195  static Property::Value GetProperty( BaseObject* object, Property::Index index );
196
197 private: // from Control
198
199   /**
200    * @copydoc Toolkit::Control::OnInitialize
201    */
202   virtual void OnInitialize();
203
204   /**
205    * @copydoc Toolkit::Control::OnPan
206    */
207   virtual void OnPan( const PanGesture& gesture );
208
209   /**
210    * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
211    */
212   virtual void OnSizeSet( const Vector3& size );
213
214 private:
215
216   /**
217    * Create the default indicator actor.
218    */
219   void CreateDefaultIndicatorActor();
220
221   /**
222    * Apply constraints for background and indicator.
223    * These constraints are based on values from the scroll connector.
224    */
225   void ApplyConstraints();
226
227   /**
228    * Callback when the current scroll position of the scrollable content goes above or
229    * below the values specified by SetScrollPositionIntervals().
230    * @param[in] source the property notification that triggered this callback
231    */
232   void OnScrollPositionIntervalReached(PropertyNotification& source);
233
234   /**
235    * Process the pan gesture per predefined timeout until the gesture is finished.
236    * @return True if the timer should be kept running.
237    */
238   bool OnPanGestureProcessTick();
239
240   /**
241    * Handle SetProperty for scroll direction.
242    * @param[in] propertyValue The new property value.
243    */
244   void OnScrollDirectionPropertySet(Property::Value propertyValue);
245
246   /**
247    * Handle SetProperty for scroll indicator height policy.
248    * @param[in] propertyValue The new property value.
249    */
250   void OnIndicatorHeightPolicyPropertySet(Property::Value propertyValue);
251
252 private:
253
254   /**
255    * Constructor.
256    * It initializes ScrollBar members.
257    */
258   ScrollBar( Toolkit::ScrollBar::Direction direction );
259
260   /**
261    * A reference counted object may only be deleted by calling Unreference()
262    */
263   virtual ~ScrollBar();
264
265 private:
266
267   Actor mIndicator;                                                  ///< Image of scroll indicator.
268   Animation mAnimation;                                              ///< Scroll indicator Show/Hide Animation.
269
270   Toolkit::ScrollBar::Direction mDirection;                          ///< The direction of scroll bar (vertical or horizontal)
271
272   WeakHandleBase mScrollableObject;                                      ///< Object to be scrolled
273
274   Property::Index mPropertyScrollPosition;                           ///< Index of scroll position property owned by the object to be scrolled
275   Property::Index mPropertyMinScrollPosition;                        ///< Index of minimum scroll position property owned by the object to be scrolled
276   Property::Index mPropertyMaxScrollPosition;                        ///< Index of maximum scroll position property owned by the object to be scrolled
277   Property::Index mPropertyScrollContentSize;                        ///< Index of scroll content size property owned by the object to be scrolled
278
279   float mIndicatorShowDuration;                                     ///< The duration of scroll indicator show animation
280   float mIndicatorHideDuration;                                     ///< The duration of scroll indicator hide animation
281
282   float mScrollStart;                                                ///< Scroll Start position (start of drag)
283   Vector3 mGestureDisplacement;                                      ///< Gesture Displacement.
284
285   bool mIsPanning;                                                  ///< Whether the scroll bar is being panned.
286   float mCurrentScrollPosition;                                     ///< The current scroll position updated by the pan gesture
287
288   Toolkit::ScrollBar::IndicatorHeightPolicy mIndicatorHeightPolicy;  ///< The height policy of scroll indicator (variable or fixed)
289   float mIndicatorFixedHeight;                                      ///< The fixed height of scroll indicator
290
291   Timer mContractDelayTimer;                                         ///< Timer guarantee contract delay time.
292   Timer mPanProcessTimer;                                            ///< The timer to process the pan gesture after the gesture is started.
293
294   Dali::Vector<float> mScrollPositionIntervals;                     ///< List of values to receive notification for when the current scroll position goes above or below them
295   PropertyNotification mPositionNotification;                        ///< Stores the property notification used for scroll position changes
296
297   PanFinishedSignalType mPanFinishedSignal;
298   ScrollPositionIntervalReachedSignalType mScrollPositionIntervalReachedSignal;
299
300   Constraint mIndicatorPositionConstraint;
301   Constraint mIndicatorSizeConstraint;
302   Constraint mScrollPositionInCurrentAxisConstraint;
303 };
304
305 } // namespace Internal
306
307 // Helpers for public-api forwarding methods
308
309 inline Toolkit::Internal::ScrollBar& GetImpl(Toolkit::ScrollBar& scrollBar)
310 {
311   DALI_ASSERT_ALWAYS(scrollBar);
312
313   Dali::RefObject& handle = scrollBar.GetImplementation();
314
315   return static_cast<Toolkit::Internal::ScrollBar&>(handle);
316 }
317
318 inline const Toolkit::Internal::ScrollBar& GetImpl(const Toolkit::ScrollBar& scrollBar)
319 {
320   DALI_ASSERT_ALWAYS(scrollBar);
321
322   const Dali::RefObject& handle = scrollBar.GetImplementation();
323
324   return static_cast<const Toolkit::Internal::ScrollBar&>(handle);
325 }
326
327 } // namespace Toolkit
328
329 } // namespace Dali
330
331 #endif // __DALI_TOOLKIT_INTERNAL_SCROLL_BAR_H__