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