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