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