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