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