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