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