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