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