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