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