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