cbcb6ff21474ff53c52d78b5f6afd402531ade1b
[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) 2022 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     };
157   };
158
159   // Typedefs
160
161   /// @brief Key Event signal type. @SINCE_1_0.0
162   typedef Signal<bool(Control, const KeyEvent&)> KeyEventSignalType;
163
164   /// @brief Key InputFocusType signal type. @SINCE_1_0.0
165   typedef Signal<void(Control)> KeyInputFocusSignalType;
166
167   /// @brief ResourceReady signal type. @SINCE_1_2.60
168   typedef Signal<void(Control)> ResourceReadySignalType;
169
170 public: // Creation & Destruction
171
172   /**
173    * @brief Additional control behaviour flags for the control constructor.
174    * @note TODO : Currunt code is hard-coded. We Should sync type values as
175    * CustomActorImpl::ActorFlag and Internal::Control::ControlBehaviour in future.
176    * @SINCE_2_1.8
177    */
178   enum ControlBehaviour
179   {
180     CONTROL_BEHAVIOUR_DEFAULT            = 0,            ///< Default behaviour: Size negotiation is enabled & listens to Style Change signal, but doesn't receive event callbacks. @SINCE_1_2_10
181     DISABLE_SIZE_NEGOTIATION             = 1 << (0 + 0), ///< True if control does not need size negotiation, i.e. it can be skipped in the algorithm @SINCE_1_0.0
182     REQUIRES_KEYBOARD_NAVIGATION_SUPPORT = 1 << (4 + 1), ///< True if needs to support keyboard navigation @SINCE_1_0.0
183     DISABLE_STYLE_CHANGE_SIGNALS         = 1 << (4 + 2), ///< True if control should not monitor style change signals @SINCE_1_2_10
184   };
185
186   /**
187    * @brief Creates a new instance of a Control.
188    *
189    * @SINCE_1_0.0
190    * @return A handle to a new Control
191    */
192   static Control New();
193
194   /**
195    * @brief Creates a new instance of a Control with additional behaviour.
196    *
197    * @SINCE_2_1.8
198    * @param[in] additionalBehaviour Additional control behaviour
199    * @return A handle to a new Control
200    */
201   static Control New(ControlBehaviour additionalBehaviour);
202
203   /**
204    * @brief Creates an uninitialized Control handle.
205    *
206    * Only derived versions can be instantiated.  Calling member
207    * functions with an uninitialized Dali::Object is not allowed.
208    * @SINCE_1_0.0
209    */
210   Control();
211
212   /**
213    * @brief Copy constructor.
214    *
215    * Creates another handle that points to the same real object.
216    * @SINCE_1_0.0
217    * @param[in] uiControl Handle to copy
218    */
219   Control(const Control& uiControl);
220
221   /**
222    * @brief Move constructor.
223    *
224    * @SINCE_1_9.23
225    * @param[in] rhs Handle to move
226    */
227   Control(Control&& rhs);
228
229   /**
230    * @brief Dali::Control is intended as a base class.
231    *
232    * This is non-virtual since derived Handle types must not contain data or virtual methods.
233    * @SINCE_1_0.0
234    */
235   ~Control();
236
237 public: // operators
238   /**
239    * @brief Copy assignment operator.
240    *
241    * Changes this handle to point to another real object.
242    * @SINCE_1_0.0
243    * @param[in] handle Object to assign this to
244    * @return Reference to this
245    */
246   Control& operator=(const Control& handle);
247
248   /**
249    * @brief Move assignment operator.
250    *
251    * @SINCE_1_9.23
252    * @param[in] rhs Object to assign this to
253    * @return Reference to this
254    */
255   Control& operator=(Control&& rhs);
256
257 public:
258   /**
259    * @brief Downcasts a handle to Control handle.
260    *
261    * If handle points to a Control, the downcast produces valid handle.
262    * If not, the returned handle is left uninitialized.
263    *
264    * @SINCE_1_0.0
265    * @param[in] handle Handle to an object
266    * @return A handle to a Control or an uninitialized handle
267    */
268   static Control DownCast(BaseHandle handle);
269
270   // Key Input
271
272   /**
273    * @brief This sets the control to receive key events.
274    *
275    * The key event can originate from a virtual or physical keyboard.
276    * @SINCE_1_0.0
277    * @pre The Control has been initialized.
278    * @pre The Control should be on the stage before setting keyboard focus.
279    */
280   void SetKeyInputFocus();
281
282   /**
283    * @brief Quries whether the control has key input focus.
284    *
285    * @SINCE_1_0.0
286    * @return true if this control has keyboard input focus
287    * @pre The Control has been initialized.
288    * @pre The Control should be on the stage before setting keyboard focus.
289    * @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.
290    * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
291    * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
292    * controls above it in the focus stack.
293    * To query for the control which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor().
294    */
295   bool HasKeyInputFocus();
296
297   /**
298    * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
299    *
300    * A check is performed to ensure it was previously set, if this check fails then nothing is done.
301    * @SINCE_1_0.0
302    * @pre The Actor has been initialized.
303    */
304   void ClearKeyInputFocus();
305
306   // Gesture Detection
307
308   /**
309    * @brief Retrieves the pinch gesture detector of the control.
310    *
311    * @SINCE_1_0.0
312    * @return The pinch gesture detector
313    * @note Will return an empty handle if the control does not handle the gesture itself.
314    */
315   PinchGestureDetector GetPinchGestureDetector() const;
316
317   /**
318    * @brief Retrieves the pan gesture detector of the control.
319    *
320    * @SINCE_1_0.0
321    * @return The pan gesture detector
322    * @note Will return an empty handle if the control does not handle the gesture itself.
323    */
324   PanGestureDetector GetPanGestureDetector() const;
325
326   /**
327    * @brief Retrieves the tap gesture detector of the control.
328    *
329    * @SINCE_1_0.0
330    * @return The tap gesture detector
331    * @note Will return an empty handle if the control does not handle the gesture itself.
332    */
333   TapGestureDetector GetTapGestureDetector() const;
334
335   /**
336    * @brief Retrieves the long press gesture detector of the control.
337    *
338    * @SINCE_1_0.0
339    * @return The long press gesture detector
340    * @note Will return an empty handle if the control does not handle the gesture itself.
341    */
342   LongPressGestureDetector GetLongPressGestureDetector() const;
343
344   // Styling
345
346   /**
347    * @brief Sets the name of the style to be applied to the control.
348    *
349    * @SINCE_1_0.0
350    * @param[in] styleName A string matching a style described in a stylesheet
351    */
352   void SetStyleName(const std::string& styleName);
353
354   /**
355    * @brief Retrieves the name of the style to be applied to the control (if any).
356    * @SINCE_1_0.0
357    * @return A string matching a style, or an empty string
358    */
359   const std::string& GetStyleName() const;
360
361   // Background
362
363   /**
364    * @brief Sets the background color of the control.
365    *
366    * @SINCE_1_0.0
367    * @param[in] color The required background color of the control
368    *
369    * @note If SetBackgroundImage is called later, this background color is removed.
370    *
371    * @note The background color fully blends with the actor color.
372    */
373   void SetBackgroundColor(const Vector4& color);
374
375   /**
376    * @brief Clears the background.
377    * @SINCE_1_0.0
378    */
379   void ClearBackground();
380
381   // Resources
382
383   /**
384    * @brief Query if all resources required by a control are loaded and ready.
385    *
386    * Most resources are only loaded when the control is placed on stage.
387    * @SINCE_1_2.60
388    * @return true if the resources are loaded and ready, false otherwise
389    */
390   bool IsResourceReady() const;
391
392   /**
393    * @brief Get the loading state of the visual resource.
394    *
395    * @SINCE_1_3_5
396    * @param[in] index The Property index of the visual
397    * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
398    */
399   Visual::ResourceStatus GetVisualResourceStatus(const Dali::Property::Index index);
400
401   // Signals
402
403   /**
404    * @brief This signal is emitted when key event is received.
405    *
406    * A callback of the following type may be connected:
407    * @code
408    *   bool YourCallbackName(Control control, const KeyEvent& event);
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   KeyEventSignalType& KeyEventSignal();
417
418   /**
419    * @brief This signal is emitted when the control gets Key Input Focus.
420    *
421    * A callback of the following type may be connected:
422    * @code
423    *   bool YourCallbackName( Control control );
424    * @endcode
425    * The return value of True, indicates that the event should be consumed.
426    * Otherwise the signal will be emitted on the next parent of the actor.
427    * @SINCE_1_0.0
428    * @return The signal to connect to
429    * @pre The Control has been initialized.
430    */
431   KeyInputFocusSignalType& KeyInputFocusGainedSignal();
432
433   /**
434    * @brief This signal is emitted when the control loses Key Input Focus.
435    *
436    * This could be due to it being gained by another Control or Actor or just cleared from
437    * this control as no longer required.
438    *
439    * A callback of the following type may be connected:
440    * @code
441    *   bool YourCallbackName( Control control );
442    * @endcode
443    * The return value of True, indicates that the event should be consumed.
444    * Otherwise the signal will be emitted on the next parent of the actor.
445    * @SINCE_1_0.0
446    * @return The signal to connect to
447    * @pre The Control has been initialized.
448    */
449   KeyInputFocusSignalType& KeyInputFocusLostSignal();
450
451   /**
452    * @brief This signal is emitted after all resources required by a control are loaded and ready.
453    *
454    * Most resources are only loaded when the control is placed on stage.
455    *
456    * If resources are shared between ImageViews, they are cached.
457    * In this case, the ResourceReady signal may be sent before there is an object to connect to.
458    * To protect against this, IsResourceReady() can be checked first.
459    *
460    * @code
461    *    auto newControl = Control::New();
462    *    newControl.SetResource( resourceUrl );
463    *    if ( newControl.IsResourceReady() )
464    *    {
465    *       // do something
466    *    }
467    *    else
468    *    {
469    *      newControl.ResourceReadySignal.Connect( .... )
470    *    }
471    * @endcode
472    *
473    * A callback of the following type may be connected:
474    * @code
475    *   void YourCallbackName( Control control );
476    * @endcode
477    *
478    * @SINCE_1_2.60
479    * @return The signal to connect to
480    * @note A RelayoutRequest is queued by Control before this signal is emitted
481    */
482   ResourceReadySignalType& ResourceReadySignal();
483
484 public: // Intended for control developers
485   /**
486    * @brief Creates an initialized Control.
487    *
488    * @SINCE_1_0.0
489    * @param[in] implementation The implementation for this control
490    * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
491    */
492   explicit Control(Internal::Control& implementation);
493
494   /**
495    * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
496    * using an Internal CustomActor pointer.
497    *
498    * @SINCE_1_0.0
499    * @param[in] internal A pointer to a newly allocated Dali resource
500    */
501   explicit Control(Dali::Internal::CustomActor* internal);
502
503 public: // Templates for Deriving Classes
504   /**
505    * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
506    *
507    * @tparam     T      The handle class
508    * @tparam     I      The implementation class
509    * @SINCE_1_0.0
510    * @param[in] handle Handle to an object
511    * @return Handle to a class T or an uninitialized handle
512    * @see DownCast(BaseHandle)
513    */
514   template<typename T, typename I>
515   DALI_INTERNAL static T DownCast(BaseHandle handle)
516   {
517     T result;
518
519     CustomActor custom = Dali::CustomActor::DownCast(handle);
520     if(custom)
521     {
522       CustomActorImpl& customImpl = custom.GetImplementation();
523
524       I* impl = dynamic_cast<I*>(&customImpl);
525
526       if(impl)
527       {
528         result = T(customImpl.GetOwner());
529       }
530     }
531
532     return result;
533   }
534
535   /**
536    * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
537    * implementation of their class.
538    *
539    * @tparam     I       The implementation class
540    * @SINCE_1_0.0
541    * @param[in] internal Pointer to the Internal::CustomActor
542    */
543   template<typename I>
544   DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
545   {
546     // Can have a NULL pointer so we only need to check if the internal implementation is our class
547     // when there is a value.
548     if(internal)
549     {
550       DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
551     }
552   }
553 };
554
555 /**
556  * @}
557  */
558 } // namespace Toolkit
559
560 } // namespace Dali
561
562 #endif // DALI_TOOLKIT_CONTROL_H