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