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