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