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