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