Move Some Devel Properties & APIs to the Public API
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / control.h
1 #ifndef __DALI_TOOLKIT_CONTROL_H__
2 #define __DALI_TOOLKIT_CONTROL_H__
3
4 /*
5  * Copyright (c) 2017 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/custom-actor.h>
23 #include <dali/public-api/common/dali-common.h>
24 #include <dali/public-api/events/long-press-gesture-detector.h>
25 #include <dali/public-api/events/pan-gesture-detector.h>
26 #include <dali/public-api/events/pinch-gesture-detector.h>
27 #include <dali/public-api/events/tap-gesture-detector.h>
28 #include <dali/public-api/events/tap-gesture-detector.h>
29 #include <dali/public-api/images/image.h>
30
31 // INTERNAL INCLUDES
32 #include <dali-toolkit/public-api/visuals/visual-properties.h>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 //Forward declarations.
41
42 namespace Internal
43 {
44 class Control;
45 }
46 /**
47  * @addtogroup dali_toolkit_controls
48  * @{
49  */
50
51 /**
52  * @brief Control is the base class for all controls.
53  *
54  * The implementation of the control must be supplied; see Internal::Control for more details.
55  * @SINCE_1_0.0
56  * @see Internal::Control
57  *
58  * Signals
59  * | %Signal Name           | Method                                              |
60  * |------------------------|-----------------------------------------------------|
61  * | keyEvent               | @ref KeyEventSignal()                               |
62  * | keyInputFocusGained    | @ref KeyInputFocusGainedSignal()                    |
63  * | keyInputFocusLost      | @ref KeyInputFocusLostSignal()                      |
64  * | resourceReady          | @ref ResourceReadySignal()                          |
65  * | tapped                 | @ref GetTapGestureDetector().DetectedSignal()       |
66  * | panned                 | @ref GetPanGestureDetector().DetectedSignal()       |
67  * | pinched                | @ref GetPinchGestureDetector().DetectedSignal()     |
68  * | longPressed            | @ref GetLongPressGestureDetector().DetectedSignal() |
69  *
70  * Actions
71  * | %Action Name           | %Control method called                             |
72  * |------------------------|----------------------------------------------------|
73  * | accessibilityActivated | %OnAccessibilityActivated()                        |
74  */
75 class DALI_IMPORT_API Control : public CustomActor
76 {
77 public:
78
79   /**
80    * @brief Enumeration for the start and end property ranges for control.
81    * @SINCE_1_0.0
82    */
83   enum PropertyRange
84   {
85     PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX,        ///< Start index is used by the property registration macro. @SINCE_1_0.0
86     CONTROL_PROPERTY_START_INDEX = PROPERTY_START_INDEX,             ///< Start index of Control properties. @SINCE_1_0.0
87     CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices. @SINCE_1_0.0
88   };
89
90   /**
91    * @brief Enumeration for the instance of properties belonging to the Control class.
92    * @SINCE_1_0.0
93    */
94   struct Property
95   {
96     /**
97      * @brief Enumeration for the instance of properties belonging to the Control class.
98      * @SINCE_1_0.0
99      */
100     enum
101     {
102       /**
103        * @brief The name of the style to be applied to the control.
104        * @details Name "styleName", type Property::STRING.
105        * @see Toolkit::Control::SetStyleName()
106        * @SINCE_1_0.0
107        */
108       STYLE_NAME = PROPERTY_START_INDEX,
109
110       /**
111        * @DEPRECATED_1_1.3
112        * @brief The background color of the control.
113        *
114        * Mutually exclusive with BACKGROUND_IMAGE & BACKGROUND.
115        * @details Name "backgroundColor", type Property::VECTOR4.
116        * @see Toolkit::Control::SetStyleName()
117        * @SINCE_1_0.0
118        */
119       BACKGROUND_COLOR,
120
121       /**
122        * @DEPRECATED_1_1.3
123        * @brief The background image of the control.
124        *
125        * Mutually exclusive with BACKGROUND_COLOR & BACKGROUND.
126        * @details Name "backgroundImage", type Property::MAP.
127        * @SINCE_1_0.0
128        */
129       BACKGROUND_IMAGE,
130
131       /**
132        * @brief Receives key events to the control.
133        * @details Name "keyInputFocus", type Property::BOOLEAN.
134        * @see Toolkit::Control::SetKeyInputFocus()
135        * @SINCE_1_0.0
136        */
137       KEY_INPUT_FOCUS,
138
139       /**
140        * @brief The background of the control.
141        *
142        * Mutually exclusive with BACKGROUND_COLOR & BACKGROUND_IMAGE.
143        * @details Name "background", type Property::MAP or std::string for URL or Property::VECTOR4 for Color.
144        * @SINCE_1_1.3
145        */
146       BACKGROUND,
147
148       /**
149        * @brief The outer space around the control.
150        * @details Name "margin", type Property::EXTENTS.
151        * @SINCE_1_2.62
152        * @note Margin property is to be supported by Layout algorithms and containers in future.
153        */
154       MARGIN,
155
156       /**
157        * @brief The inner space of the control.
158        * @details Name "padding", type Property::EXTENTS.
159        * @SINCE_1_2.62
160        */
161       PADDING
162     };
163   };
164
165   /**
166    * @brief Describes the direction to move the keyboard focus towards.
167    * @SINCE_1_0.0
168    */
169   struct KeyboardFocus
170   {
171     /**
172      * @brief Keyboard focus direction.
173      * @SINCE_1_0.0
174      */
175     enum Direction
176     {
177       LEFT,   ///< Move keyboard focus towards the left direction @SINCE_1_0.0
178       RIGHT,  ///< Move keyboard focus towards the right direction @SINCE_1_0.0
179       UP,     ///< Move keyboard focus towards the up direction @SINCE_1_0.0
180       DOWN,    ///< Move keyboard focus towards the down direction @SINCE_1_0.0
181       PAGE_UP,     ///< Move keyboard focus towards the previous page direction @SINCE_1_2.14
182       PAGE_DOWN    ///< Move keyboard focus towards the next page direction @SINCE_1_2.14
183     };
184   };
185
186   // Typedefs
187
188   /// @brief Key Event signal type. @SINCE_1_0.0
189   typedef Signal<bool ( Control, const KeyEvent& ) > KeyEventSignalType;
190
191   /// @brief Key InputFocusType signal type. @SINCE_1_0.0
192   typedef Signal<void ( Control ) > KeyInputFocusSignalType;
193
194   /// @brief ResourceReady signal type. @SINCE_1_2.60
195   typedef Signal<void ( Control ) > ResourceReadySignalType;
196
197 public: // Creation & Destruction
198
199   /**
200    * @brief Creates a new instance of a Control.
201    *
202    * @SINCE_1_0.0
203    * @return A handle to a new Control
204    */
205   static Control New();
206
207   /**
208    * @brief Creates an uninitialized Control handle.
209    *
210    * Only derived versions can be instantiated.  Calling member
211    * functions with an uninitialized Dali::Object is not allowed.
212    * @SINCE_1_0.0
213    */
214   Control();
215
216   /**
217    * @brief Copy constructor.
218    *
219    * Creates another handle that points to the same real object.
220    * @SINCE_1_0.0
221    * @param[in] uiControl Handle to copy
222    */
223   Control(const Control& uiControl);
224
225   /**
226    * @brief Dali::Control is intended as a base class.
227    *
228    * This is non-virtual since derived Handle types must not contain data or virtual methods.
229    * @SINCE_1_0.0
230    */
231   ~Control();
232
233 public: // operators
234
235   /**
236    * @brief Assignment operator.
237    *
238    * Changes this handle to point to another real object.
239    * @SINCE_1_0.0
240    * @param[in] handle Object to assign this to
241    * @return Reference to this
242    */
243   Control& operator=( const Control& handle );
244
245 public:
246
247   /**
248    * @brief Downcasts a handle to Control handle.
249    *
250    * If handle points to a Control, the downcast produces valid handle.
251    * If not, the returned handle is left uninitialized.
252    *
253    * @SINCE_1_0.0
254    * @param[in] handle Handle to an object
255    * @return A handle to a Control or an uninitialized handle
256    */
257   static Control DownCast( BaseHandle handle );
258
259   // Key Input
260
261   /**
262    * @brief This sets the control to receive key events.
263    *
264    * The key event can originate from a virtual or physical keyboard.
265    * @SINCE_1_0.0
266    * @pre The Control has been initialized.
267    * @pre The Control should be on the stage before setting keyboard focus.
268    */
269   void SetKeyInputFocus();
270
271   /**
272    * @brief Quries whether the control has key input focus.
273    *
274    * @SINCE_1_0.0
275    * @return true if this control has keyboard input focus
276    * @pre The Control has been initialized.
277    * @pre The Control should be on the stage before setting keyboard focus.
278    * @note The control can be set to have the focus and still not receive all the key events if another control has over ridden it.
279    * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
280    * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
281    * controls above it in the focus stack.
282    * To query for the control which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor().
283    */
284   bool HasKeyInputFocus();
285
286   /**
287    * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
288    *
289    * A check is performed to ensure it was previously set, if this check fails then nothing is done.
290    * @SINCE_1_0.0
291    * @pre The Actor has been initialized.
292    */
293   void ClearKeyInputFocus();
294
295   // Gesture Detection
296
297   /**
298    * @brief Retrieves the pinch gesture detector of the control.
299    *
300    * @SINCE_1_0.0
301    * @return The pinch gesture detector
302    * @note Will return an empty handle if the control does not handle the gesture itself.
303    */
304   PinchGestureDetector GetPinchGestureDetector() const;
305
306   /**
307    * @brief Retrieves the pan gesture detector of the control.
308    *
309    * @SINCE_1_0.0
310    * @return The pan gesture detector
311    * @note Will return an empty handle if the control does not handle the gesture itself.
312    */
313   PanGestureDetector GetPanGestureDetector() const;
314
315   /**
316    * @brief Retrieves the tap gesture detector of the control.
317    *
318    * @SINCE_1_0.0
319    * @return The tap gesture detector
320    * @note Will return an empty handle if the control does not handle the gesture itself.
321    */
322   TapGestureDetector GetTapGestureDetector() const;
323
324   /**
325    * @brief Retrieves the long press gesture detector of the control.
326    *
327    * @SINCE_1_0.0
328    * @return The long press gesture detector
329    * @note Will return an empty handle if the control does not handle the gesture itself.
330    */
331   LongPressGestureDetector GetLongPressGestureDetector() const;
332
333   // Styling
334
335   /**
336    * @brief Sets the name of the style to be applied to the control.
337    *
338    * @SINCE_1_0.0
339    * @param[in] styleName A string matching a style described in a stylesheet
340    */
341   void SetStyleName( const std::string& styleName );
342
343   /**
344    * @brief Retrieves the name of the style to be applied to the control (if any).
345    * @SINCE_1_0.0
346    * @return A string matching a style, or an empty string
347    */
348   const std::string& GetStyleName() const;
349
350   // Background
351
352   /**
353    * @brief Sets the background color of the control.
354    *
355    * @SINCE_1_0.0
356    * @param[in] color The required background color of the control
357    *
358    * @note If SetBackgroundImage is called later, this background color is removed.
359    *
360    * @note The background color fully blends with the actor color.
361    */
362   void SetBackgroundColor( const Vector4& color );
363
364   /**
365    * @DEPRECATED_1_1.3
366    *
367    * @brief Retrieves the background color of the control.
368    *
369    * @SINCE_1_0.0
370    * @return The background color of the control
371    */
372   Vector4 GetBackgroundColor() const DALI_DEPRECATED_API;
373
374   /**
375    * @DEPRECATED_1_2_8, use Property::BACKGROUND instead
376    *
377    * @brief Sets an image as the background of the control.
378    *
379    * @SINCE_1_0.0
380    * @param[in] image The image to set as the background
381    */
382   void SetBackgroundImage( Image image ) DALI_DEPRECATED_API;
383
384   /**
385    * @brief Clears the background.
386    * @SINCE_1_0.0
387    */
388   void ClearBackground();
389
390   // Resources
391
392   /**
393    * @brief Query if all resources required by a control are loaded and ready.
394    *
395    * Most resources are only loaded when the control is placed on stage.
396    * @SINCE_1_2.60
397    * @return true if the resources are loaded and ready, false otherwise
398    */
399   bool IsResourceReady() const;
400
401   /**
402    * @brief Get the loading state of the visual resource.
403    *
404    * @SINCE_1_3_5
405    * @param[in] index The Property index of the visual
406    * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
407    */
408   Visual::ResourceStatus GetVisualResourceStatus( const Dali::Property::Index index );
409
410   // Signals
411
412   /**
413    * @brief This signal is emitted when key event is received.
414    *
415    * A callback of the following type may be connected:
416    * @code
417    *   bool YourCallbackName(Control control, const KeyEvent& event);
418    * @endcode
419    * The return value of True, indicates that the event should be consumed.
420    * Otherwise the signal will be emitted on the next parent of the actor.
421    * @SINCE_1_0.0
422    * @return The signal to connect to
423    * @pre The Control has been initialized.
424    */
425   KeyEventSignalType& KeyEventSignal();
426
427   /**
428    * @brief This signal is emitted when the control gets Key Input Focus.
429    *
430    * A callback of the following type may be connected:
431    * @code
432    *   bool YourCallbackName( Control control );
433    * @endcode
434    * The return value of True, indicates that the event should be consumed.
435    * Otherwise the signal will be emitted on the next parent of the actor.
436    * @SINCE_1_0.0
437    * @return The signal to connect to
438    * @pre The Control has been initialized.
439    */
440   KeyInputFocusSignalType& KeyInputFocusGainedSignal();
441
442   /**
443    * @brief This signal is emitted when the control loses Key Input Focus.
444    *
445    * This could be due to it being gained by another Control or Actor or just cleared from
446    * this control as no longer required.
447    *
448    * A callback of the following type may be connected:
449    * @code
450    *   bool YourCallbackName( Control control );
451    * @endcode
452    * The return value of True, indicates that the event should be consumed.
453    * Otherwise the signal will be emitted on the next parent of the actor.
454    * @SINCE_1_0.0
455    * @return The signal to connect to
456    * @pre The Control has been initialized.
457    */
458   KeyInputFocusSignalType& KeyInputFocusLostSignal();
459
460   /**
461    * @brief This signal is emitted after all resources required by a control are loaded and ready.
462    *
463    * Most resources are only loaded when the control is placed on stage.
464    *
465    * If resources are shared between ImageViews, they are cached.
466    * In this case, the ResourceReady signal may be sent before there is an object to connect to.
467    * To protect against this, IsResourceReady() can be checked first.
468    *
469    * @code
470    *    auto newControl = Control::New();
471    *    newControl.SetResource( resourceUrl );
472    *    if ( newControl.IsResourceReady() )
473    *    {
474    *       // do something
475    *    }
476    *    else
477    *    {
478    *      newControl.ResourceReadySignal.Connect( .... )
479    *    }
480    * @endcode
481    *
482    * A callback of the following type may be connected:
483    * @code
484    *   void YourCallbackName( Control control );
485    * @endcode
486    *
487    * @SINCE_1_2.60
488    * @return The signal to connect to
489    * @note A RelayoutRequest is queued by Control before this signal is emitted
490    */
491   ResourceReadySignalType& ResourceReadySignal();
492
493 public: // Intended for control developers
494
495   /**
496    * @brief Creates an initialized Control.
497    *
498    * @SINCE_1_0.0
499    * @param[in] implementation The implementation for this control
500    * @return A handle to a newly allocated Dali resource
501    * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
502    */
503   explicit Control(Internal::Control& implementation);
504
505   /**
506    * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
507    * using an Internal CustomActor pointer.
508    *
509    * @SINCE_1_0.0
510    * @param[in] internal A pointer to a newly allocated Dali resource
511    */
512   explicit Control(Dali::Internal::CustomActor* internal);
513
514 public: // Templates for Deriving Classes
515
516   /**
517    * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
518    *
519    * @tparam     T      The handle class
520    * @tparam     I      The implementation class
521    * @SINCE_1_0.0
522    * @param[in] handle Handle to an object
523    * @return Handle to a class T or an uninitialized handle
524    * @see DownCast(BaseHandle)
525    */
526   template<typename T, typename I>
527   DALI_INTERNAL static T DownCast( BaseHandle handle )
528   {
529     T result;
530
531     CustomActor custom = Dali::CustomActor::DownCast( handle );
532     if ( custom )
533     {
534       CustomActorImpl& customImpl = custom.GetImplementation();
535
536       I* impl = dynamic_cast<I*>(&customImpl);
537
538       if (impl)
539       {
540         result = T(customImpl.GetOwner());
541       }
542     }
543
544     return result;
545   }
546
547   /**
548    * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
549    * implementation of their class.
550    *
551    * @tparam     I       The implementation class
552    * @SINCE_1_0.0
553    * @param[in] internal Pointer to the Internal::CustomActor
554    */
555   template<typename I>
556   DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
557   {
558     // Can have a NULL pointer so we only need to check if the internal implementation is our class
559     // when there is a value.
560     if (internal)
561     {
562       DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
563     }
564   }
565
566 };
567
568 /**
569  * @}
570  */
571 } // namespace Toolkit
572
573 } // namespace Dali
574
575 #endif // __DALI_TOOLKIT_CONTROL_H__