Fix the Slider and Scrollbar issue with nine patch image
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / slider / slider-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_SLIDER_H__
2 #define __DALI_TOOLKIT_INTERNAL_SLIDER_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/actors/image-actor.h>
23 #include <dali/public-api/adaptor-framework/timer.h>
24 #include <dali/public-api/object/property-array.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/public-api/controls/control-impl.h>
28 #include <dali-toolkit/devel-api/controls/slider/slider.h>
29 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
30 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 class Button;
39
40 namespace Internal
41 {
42
43 class Slider;
44
45 typedef Dali::IntrusivePtr< Slider > SliderPtr;
46
47 /**
48  * @copydoc Toolkit::Slider
49  */
50 class Slider : public Control
51 {
52 public:
53
54   typedef Property::Array MarkList;
55
56   /**
57    * Create a new Slider.
58    *
59    * @return A public handle to the newly allocated Slider.
60    */
61   static Dali::Toolkit::Slider New();
62
63 public:
64
65   // Properties
66
67   /**
68    * Set marks from a list
69    *
70    * @param[in] marks The list of marks to set
71    */
72   void SetMarks( const MarkList& marks );
73
74   /**
75    * Get the list of marks
76    *
77    * @return The marks list
78    */
79   const MarkList& GetMarks() const;
80
81   /**
82    * Set if should snap to marks or not
83    *
84    * @param[in] snap Flag to snap to marks or not
85    */
86   void SetSnapToMarks( bool snap );
87
88   /**
89    * Return if snap to marks is set or not
90    *
91    * @return If snap to marks is set
92    */
93   bool GetSnapToMarks() const;
94
95   /**
96    * Set the value of the slider
97    *
98    * @param[in] value The value to set. Will be clamped to [lowerBound .. upperBound]
99    */
100   void SetValue( float value );
101
102   /**
103    * Get the value of the slider
104    *
105    * @return The current value of the slider
106    */
107   float GetValue() const;
108
109   /**
110    * Set hit region
111    *
112    * @param[in] region The hit region
113    */
114   void SetHitRegion( const Vector2& region );
115
116   /**
117    * Get hit region
118    *
119    * @return The hit region
120    */
121   const Vector2& GetHitRegion() const;
122
123   /**
124    * Set backing region
125    *
126    * @param[in] region The backing region
127    */
128   void SetBackingRegion( const Vector2& region );
129
130   /**
131    * Get backing region
132    *
133    * @return The backing region
134    */
135   const Vector2& GetBackingRegion() const;
136
137   /**
138    * @brief Set the disable color.
139    *
140    * @param[in] color The disable color.
141    */
142   void SetDisableColor( const Vector4& color );
143
144   /**
145    * @brief Get disable color
146    *
147    * @return The disable color
148    */
149   Vector4 GetDisableColor() const;
150
151   /**
152    * Get popup text color
153    *
154    * @return The popup text color
155    */
156   Vector4 GetPopupTextColor() const;
157
158   /**
159    * Set the value precision to be used for numbers in the slider
160    *
161    * @param[in] precision The number of decimal places to use for printing numbers
162    */
163   void SetValuePrecision( int precision );
164
165   /**
166    * Get value precision
167    *
168    * @return The value precision
169    */
170   int GetValuePrecision() const;
171
172   /**
173    * Show the popup
174    *
175    * @param[in] showPopup The show popup flag
176    */
177   void SetShowPopup( bool showPopup );
178
179   /**
180    * Get show value in popup
181    *
182    * @return The show value flag
183    */
184   bool GetShowPopup() const;
185
186   /**
187    * Set show value on handle
188    *
189    * @param[in] showValue The show value flag
190    */
191   void SetShowValue( bool showValue );
192
193   /**
194    * Get show value on handle
195    *
196    * @return The show value flag
197    */
198   bool GetShowValue() const;
199
200   /**
201    * Set enabled
202    *
203    * param[in] enabled Set the enabled flag
204    */
205   void SetEnabled( bool enabled );
206
207   /**
208    * Return if enabled or not
209    *
210    * @return If enabled
211    */
212   bool IsEnabled() const;
213
214   /**
215    * @brief Set the mark tolerance
216    *
217    * The tolerance is the percentage of the slider width for which snapping to
218    * marks occurs
219    *
220    * @param[in] tolerance The percentage of width for which to snap
221    */
222   void SetMarkTolerance( float tolerance );
223
224   /**
225    * Return the mark tolerance
226    *
227    * @return The tolerance set for snapping to marks
228    */
229   float GetMarkTolerance() const;
230
231 public:
232   //Signals
233
234   /**
235    * @copydoc Toolkit::Slider::ValueChangedSignal()
236    */
237   Toolkit::Slider::ValueChangedSignalType& ValueChangedSignal();
238
239   /**
240    * copydoc Toolkit::Slider::SlidingFinishedSignal()
241    */
242   Toolkit::Slider::ValueChangedSignalType& SlidingFinishedSignal();
243
244   /**
245    * @copydoc Toolkit::Slider::MarkSignal()
246    */
247   Toolkit::Slider::MarkSignalType& MarkSignal();
248
249   /**
250    * Connects a callback function with the object's signals.
251    * @param[in] object The object providing the signal.
252    * @param[in] tracker Used to disconnect the signal.
253    * @param[in] signalName The signal to connect to.
254    * @param[in] functor A newly allocated FunctorDelegate.
255    * @return True if the signal was connected.
256    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
257    */
258   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName,
259                                FunctorDelegate* functor );
260
261   // Properties
262
263   /**
264    * Called when a property of an object of this type is set.
265    * @param[in] object The object whose property is set.
266    * @param[in] index The property index.
267    * @param[in] value The new property value.
268    */
269   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
270
271   /**
272    * Called to retrieve a property of an object of this type.
273    * @param[in] object The object whose property is to be retrieved.
274    * @param[in] index The property index.
275    * @return The current value of the property.
276    */
277   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
278
279 protected:
280
281   /**
282    * Construct a new Slider.
283    */
284   Slider();
285
286   /**
287    * A reference counted object may only be deleted by calling Unreference()
288    */
289   virtual ~Slider();
290
291   /**
292    * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
293    */
294   virtual void OnSizeSet( const Vector3& size );
295
296 private:
297
298   /**
299    * Domain is a from/to pair
300    */
301   struct Domain
302   {
303     Vector2 from;
304     Vector2 to;
305
306     Domain()
307     {
308     }
309     Domain( Vector2 fromVal, Vector2 toVal )
310         : from( fromVal ), to( toVal )
311     {
312     }
313   };
314
315   /**
316    * Slider states
317    */
318   enum SliderState
319   {
320     NORMAL,
321     DISABLED,
322     PRESSED,
323     FOCUSED
324   };
325
326 private:
327
328   /**
329    * @copydoc Toolkit::Control::OnInitialize()
330    */
331   virtual void OnInitialize();
332
333   /**
334    * Hit region touch event
335    *
336    * @param[in] actor The actor the event is raised for
337    * @param[in] event The touch event info
338    * @return If the event is handled or not
339    */
340   bool OnTouchEvent( Actor actor, const TouchEvent& event );
341
342   /**
343    * Pan gesture event
344    *
345    * @param[in] actor The actor the event is raised for
346    * @param[in] gesture The pan event info
347    */
348   void OnPan( Actor actor, const PanGesture& gesture );
349
350   /**
351    * Map a position onto a domain and return the result as a percentage
352    *
353    * @param[in] point The point to map onto the domain
354    * @return The result as a percentage [0..1]
355    */
356   float MapPercentage( const Vector2& point );
357
358   /**
359    * Map a value in the range to a percentage
360    *
361    * @param[in] point The value in range [lowerBound..upperBound]
362    * @return The result as a percentage [0..1]
363    */
364   float MapValuePercentage( float value );
365
366   /**
367    * Convert a point in local hit space into domain space
368    *
369    * @param[in] x The x position to convert
370    * @return The x position in domain space
371    */
372   float HitSpaceToDomain( float x );
373
374   /**
375    * Map a percentage onto the slider's bounds
376    *
377    * @param[in] percent The current value of slider in percent
378    * @param[in] lowerBound The lower bound to map onto
379    * @param[in] upperBound The upper bound to map onto
380    * @return The value of percent mapped from lowerBound to upperBound
381    */
382   float MapBounds( float percent, float lowerBound, float upperBound );
383
384   /**
385    * Get the range of the valid values the slider handle can move between
386    *
387    * @param[in] currentSize The current size of the slider
388    * @return The range as a domain pair
389    */
390   Domain CalcDomain( const Vector2& currentSize );
391
392   /**
393    * Create the hit region
394    *
395    * @return The hit region actor
396    */
397   Actor CreateHitRegion();
398
399   /**
400    * Create the backing for the slider
401    *
402    * @return The backing actor
403    */
404   Toolkit::ImageView CreateBacking();
405
406   /**
407    * Create the progress backing for the slider
408    *
409    * @return The backing actor
410    */
411   Toolkit::ImageView CreateProgress();
412
413   /**
414    * Create the handle for the slider
415    *
416    * @return The created image handle
417    */
418   Toolkit::ImageView CreateHandle();
419
420   /**
421    * Create the popup arrow
422    *
423    * @return The created image handle
424    */
425   Toolkit::ImageView CreatePopupArrow();
426
427   /**
428    * Create the popup
429    *
430    * @return The created image handle
431    */
432   Toolkit::ImageView CreatePopup();
433
434   /**
435    * Create the textview for the popup
436    *
437    * @return The textview created for the popup
438    */
439   Toolkit::TextLabel CreatePopupText();
440
441   /**
442    * Create the value display for the slider
443    *
444    * @return The created root actor of the display
445    */
446   Actor CreateValueDisplay();
447
448   /**
449    * Set the skin based on the current state
450    */
451   void UpdateSkin();
452
453   /**
454    * Create all the children
455    */
456   void CreateChildren();
457
458   /**
459    * Create value popup
460    */
461   void AddPopup();
462
463   /**
464    * Remove value popup
465    */
466   void RemovePopup();
467
468   /**
469    * Display the popup for a set duration with the given value
470    *
471    * @param[in] value The value to display in the popup
472    */
473   void DisplayPopup( float value );
474
475   /**
476    * If there are marks present, filter the incoming percent based on snapping to any nearby marks
477    *
478    * @param[in] value The incoming value on the slider to filter
479    * @return The filtered percentage snapped to any nearby marks
480    */
481   float MarkFilter( float value );
482
483   /**
484    * If there are marks present, snap the incoming percent to the nearest mark
485    *
486    * @param[in] value The incoming value on the slider to snap
487    * @return The filtered percentage snapped to the nearest mark
488    */
489   float SnapToMark( float value );
490
491   /**
492    * Search for if a mark has been reached
493    *
494    * @param[in] value The value to search against
495    * @param[out] outIndex The index of the mark if found
496    * @return If a mark has been found to match percent
497    */
498   bool MarkReached( float value, int& outIndex );
499
500   /**
501    * Handler for when the value view needs to be hidden
502    *
503    * @return If handled or not
504    */
505   bool HideValueView();
506
507   /**
508    * Set value choosing whether to fire signals or not
509    *
510    * @paramp[in] value The value to set
511    * @param[in] raiseSignals Configure signals to be raised or not.
512    */
513   void DisplayValue( float value, bool raiseSignals );
514
515   /**
516    * Create the image for the backing
517    *
518    * @param[in] imageName The name of the image to load and set
519    */
520   void SetBackingImageName( const std::string& imageName );
521
522   /**
523    * @brief Return the backing image file name.
524    *
525    * @return The backing image file name.
526    */
527   std::string GetBackingImageName();
528
529   /**
530    * Create the image for the progress bar
531    *
532    * @param[in] imageName The name of the image to load and set
533    */
534   void SetProgressImageName( const std::string& imageName );
535
536   /**
537    * @brief Return the progress image name.
538    *
539    * @return The progress image name if it exists.
540    */
541   std::string GetProgressImageName();
542
543   /**
544    * @brief Create the image for the popup
545    *
546    * @param[in] imageName The name of the image to load and set
547    */
548   void CreatePopupImage( const std::string& imageName );
549
550   /**
551    * @brief Set the popup name
552    *
553    * @param[in] imageName The name of the image to set
554    */
555   void SetPopupImageName( const std::string& imageName );
556
557   /**
558    * @brief Return the popup image name.
559    *
560    * @return The name of the popup image if it exists.
561    */
562   std::string GetPopupImageName();
563
564   /**
565    * @brief Set the popup arrow image name
566    *
567    * @param[in] imageName The name of the image to set
568    */
569   void SetPopupArrowImageName( const std::string& imageName );
570
571   /**
572    * @brief Return the popup arrow image name.
573    *
574    * @return The name of the popup image if it exists.
575    */
576   std::string GetPopupArrowImageName();
577
578   /**
579    * Create the image for the popup arrow
580    *
581    * @param[in] imageName The name of the image to load and set
582    */
583   void CreatePopupArrowImage( const std::string& imageName );
584
585   /**
586    * Set the size of the progress bar region
587    *
588    * @param[in] region The size of the region to set
589    */
590   void ResizeProgressRegion( const Vector2& region );
591
592   /**
593    * Create the image for the handle
594    *
595    * @param[in] imageName The name of the image to load and set
596    */
597   void SetHandleImageName( const std::string& imageName );
598
599   /**
600    * @brief Return the handle image name.
601    *
602    * @return The name of the image handle if it exists.
603    */
604   std::string GetHandleImageName();
605
606   /**
607    * Set the size of the handle region
608    *
609    * @param[in] region The size of the region to set
610    */
611   void ResizeHandleRegion( const Vector2& region );
612
613   /**
614    * Create and display the value on the handle
615    */
616   void CreateHandleValueDisplay();
617
618   /**
619    * Remove and destroy the handle value display
620    */
621   void DestroyHandleValueDisplay();
622
623   /**
624    * Update the color of the popup text
625    *
626    * @param[in] color The new color
627    */
628   void SetPopupTextColor( const Vector4& color );
629
630   /**
631    * Set handle region
632    *
633    * @param[in] region The handle region
634    */
635   void SetHandleRegion( const Vector2& region );
636
637   /**
638    * Get handle region
639    *
640    * @return The handle region
641    */
642   const Vector2& GetHandleRegion() const;
643
644   /**
645    * Set the lower bound of the slider's value
646    *
647    * @param[in] bound The value to set for the lower bound
648    */
649   void SetLowerBound( float bound );
650
651   /**
652    * Get the lower bound of the slider's value
653    *
654    * @return The lower bound value
655    */
656   float GetLowerBound() const;
657
658   /**
659    * Set the upper bound of the slider's value
660    *
661    * @param[in] bound The value to set for the upper bound
662    */
663   void SetUpperBound( float bound );
664
665   /**
666    * Get the upper bound of the slider's value
667    *
668    * @return The upper bound value
669    */
670   float GetUpperBound() const;
671
672 private:
673
674   // Undefined
675   Slider( const Slider& );
676
677   // Undefined
678   Slider& operator=( const Slider& rhs );
679
680 private:
681
682   Domain mDomain;                           ///< Current domain of the handle
683
684   Actor mHitArea;                           ///< The input handler
685   Actor mValueDisplay;                      ///< Display of the value
686   Toolkit::ImageView mBacking;              ///< Backing image
687   Toolkit::ImageView mHandle;               ///< Slider handle
688   Toolkit::ImageView mProgress;             ///< Progress backing
689   Toolkit::ImageView mPopup;                ///< Popup backing
690   Toolkit::ImageView mPopupArrow;           ///< Popup arrow backing
691
692   Toolkit::TextLabel mValueTextLabel;       //< The text value in popup
693   Toolkit::TextLabel mHandleValueTextLabel; ///< The text value on handle
694   Vector2 mHandleLastTouchPoint;            ///< The last touch point for the handle
695   Timer mValueTimer;                        ///< Timer used to hide value view
696
697   Toolkit::Slider::ValueChangedSignalType mValueChangedSignal;    ///< Signal emitted when the value is changed
698   Toolkit::Slider::ValueChangedSignalType mSlidingFinishedSignal;    ///< Signal emitted when a sliding is finished
699   Toolkit::Slider::MarkSignalType mMarkSignal;                    ///< Signal emitted when a mark is reached
700
701   SliderState mState;                 ///< The state of the slider
702
703   PanGestureDetector mPanDetector;    ///< Hit region pan detector
704
705   MarkList mMarks;                    ///< List of discreet marks
706
707   std::string mPopupImageName;      ///< Image name for popup image
708   std::string mPopupArrowImageName; ///< Image name for popup arrow
709   std::string mBackingImageName;    ///< Image name for backing image
710   std::string mHandleImageName;     ///< Image name for handle image
711   std::string mProgressImageName;   ///< Image name for progress image
712
713   Vector4 mDisableColor;    ///< The color to tint the slider when disabled
714   Vector4 mPopupTextColor;  ///< The color of the popup text
715
716   Vector2 mHitRegion;     ///< Size of hit region
717   Vector2 mBackingRegion; ///< Size of backing region
718   Vector2 mHandleRegionSize;  ///< Size of the handle region
719
720   float mLowerBound;        ///< Lower bound on value
721   float mUpperBound;        ///< Upper bound on value
722   float mValue;             ///< Current value of slider
723
724   float mMarkTolerance;     ///< Tolerance in percentage of slider width for which to snap to marks
725
726   int mValuePrecision;      ///< The precision to use for outputting the value
727
728   bool mShowPopup   : 1,      ///< Show the popup or not
729        mShowValue   : 1,      ///< Whether to display the value number or not on the handle
730        mSnapToMarks : 1;      ///< Turn on or off snapping to marks
731 };
732
733 } // namespace Internal
734
735 // Helpers for public-api forwarding methods
736
737 inline Toolkit::Internal::Slider& GetImpl( Toolkit::Slider& pub )
738 {
739   DALI_ASSERT_ALWAYS( pub );
740
741   Dali::RefObject& handle = pub.GetImplementation();
742
743   return static_cast< Toolkit::Internal::Slider& >( handle );
744 }
745
746 inline const Toolkit::Internal::Slider& GetImpl( const Toolkit::Slider& pub )
747 {
748   DALI_ASSERT_ALWAYS( pub );
749
750   const Dali::RefObject& handle = pub.GetImplementation();
751
752   return static_cast< const Toolkit::Internal::Slider& >( handle );
753 }
754
755 } // namespace Toolkit
756
757 } // namespace Dali
758
759 #endif // __DALI_TOOLKIT_INTERNAL_SLIDER_H__