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