Property refactor in dali-toolkit: Toolkit changes
[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,                              ///< name "background",       @see SetBackground,      type MAP
87       WIDTH_POLICY,                            ///< name "width-policy",     @see SetSizePolicy,      type STRING
88       HEIGHT_POLICY,                           ///< name "height-policy",    @see SetSizePolicy,      type STRING
89       MINIMUM_SIZE,                            ///< name "minimum-size",     @see SetMinimumSize,     type VECTOR3
90       MAXIMUM_SIZE,                            ///< name "maximum-size",     @see SetMaximumSize,     type VECTOR3
91       KEY_INPUT_FOCUS,                         ///< name "key-input-focus",  @see SetKeyInputFocus,   type BOOLEAN
92     };
93   };
94
95   /**
96    * @brief Describes how a control could be resized.
97    */
98   enum SizePolicy
99   {
100     Fixed,    ///< Size can't grow or shrink.
101     Minimum,  ///< Size can grow but shrink up to a minimum level.
102     Maximum,  ///< Size can shrink but grow up to a maximum value.
103     Range,    ///< Size can grow or shrink between a minimum and a maximum values.
104     Flexible, ///< Size can grow or shrink with no limits.
105   };
106
107   /**
108    * @brief Describes what a control should do when a contained actor/control exceeds the boundary of the control.
109    */
110   enum ExceedPolicy
111   {
112     Crop,   ///< Control's contents will be cropped.
113     Shrink, ///< Control's contents will be shrunk.
114     Scroll  ///< Control's contents will be added to a scroll.
115   };
116
117   /**
118    * @brief Describes the direction to move the keyboard focus towards.
119    */
120   enum KeyboardFocusNavigationDirection
121   {
122     Left,   ///< Move keyboard focus towards the left direction
123     Right,  ///< Move keyboard focus towards the right direction
124     Up,     ///< Move keyboard focus towards the up direction
125     Down    ///< Move keyboard focus towards the down direction
126   };
127
128   // Typedefs
129
130   /// @brief Key Event signal type;
131   typedef Signal<bool ( Control, const KeyEvent& ) > KeyEventSignalType;
132
133 public: // Creation & Destruction
134
135   /**
136    * @brief Create a new instance of a Control.
137    *
138    * @return A handle to a new Control.
139    */
140   static Control New();
141
142   /**
143    * @brief Create an uninitialized Control handle.
144    *
145    * Only derived versions can be instantiated.  Calling member
146    * functions with an uninitialized Dali::Object is not allowed.
147    */
148   Control();
149
150   /**
151    * @brief Copy constructor.
152    *
153    * Creates another handle that points to the same real object
154    * @param[in] uiControl Handle to copy
155    */
156   Control(const Control& uiControl);
157
158   /**
159    * @brief Dali::Control is intended as a base class
160    *
161    * This is non-virtual since derived Handle types must not contain data or virtual methods.
162    */
163   ~Control();
164
165 public: // operators
166
167   /**
168    * @brief Assignment operator.
169    *
170    * Changes this handle to point to another real object
171    * @param[in] handle Object to assign this to
172    * @return reference to this
173    */
174   Control& operator=( const Control& handle );
175
176 public:
177
178   /**
179    * @brief Downcast an Object handle to Control.
180    *
181    * If handle points to a Control the downcast produces valid
182    * handle. If not the returned handle is left uninitialized.
183    *
184    * @param[in] handle Handle to an object
185    * @return handle to a Control or an uninitialized handle
186    */
187   static Control DownCast( BaseHandle handle );
188
189   /**
190    * @brief Retrieve the Control implementation.
191    *
192    * @return The implementation.
193    */
194   Internal::Control& GetImplementation();
195
196   /**
197    * @brief Retrieve the Control implementation.
198    *
199    * @return The implementation.
200    */
201   const Internal::Control& GetImplementation() const;
202
203   // Size Negotiation
204
205   /**
206    * @brief Sets the size policies for the width and height dimensions.
207    *
208    * @param[in] widthPolicy Size policy for the width dimension.
209    * @param[in] heightPolicy Size policy for the height dimension.
210    */
211   void SetSizePolicy( SizePolicy widthPolicy, SizePolicy heightPolicy );
212
213   /**
214    * @brief Retrieves the size policies for the width and height dimensions.
215    *
216    * @param[out] widthPolicy Width's size policy.
217    * @param[out] heightPolicy Height's size policy.
218    */
219   void GetSizePolicy( SizePolicy& widthPolicy, SizePolicy& heightPolicy ) const;
220
221   /**
222    * @brief Sets the minimum size for the control.
223    *
224    * @param[in] size The minimum size.
225    */
226   void SetMinimumSize( const Vector3& size );
227
228   /**
229    * @brief Retrieves the minimum size.
230    *
231    * @return The minimum size.
232    */
233   const Vector3& GetMinimumSize() const;
234
235   /**
236    * @brief Sets the maximum size.
237    *
238    * @param[in] size The maximum size.
239    */
240   void SetMaximumSize( const Vector3& size );
241
242   /**
243    * @brief Retrieves the maximum size.
244    *
245    * @return The maximum size.
246    */
247   const Vector3& GetMaximumSize() const;
248
249   /**
250    * @brief Works out the natural size.
251    *
252    * Natural size is the control's size with any restriction.
253    *
254    * @return The natural size.
255    */
256   Vector3 GetNaturalSize();
257
258   /**
259    * @brief Works out the control's height for a given width.
260    *
261    * @param[in] width The control's width.
262    *
263    * @return The control's height for the given width.
264    */
265   float GetHeightForWidth( float width );
266
267   /**
268    * @brief Works out the control's width for a given height.
269    *
270    * @param[in] height The control's height.
271    *
272    * @return The control's width for the given height.
273    */
274   float GetWidthForHeight( float height );
275
276   // Key Input
277
278   /**
279    * @brief This sets the control to receive key events.
280    *
281    * The key event can originate from a virtual or physical keyboard.
282    * @pre The Control has been initialized.
283    * @pre The Control should be on the stage before setting keyboard focus.
284    * @return True if the control has foucs, False otherwise.
285    */
286   void SetKeyInputFocus();
287
288   /**
289    * @brief Quries whether the control has key input focus.
290    *
291    * 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.
292    * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
293    * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
294    * controls above it in the focus stack.
295    * To query for the conrol which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor()
296    * @pre The Control has been initialized.
297    * @pre The Control should be on the stage before setting keyboard focus.
298    * @return true if this control has keyboard input focus
299    */
300   bool HasKeyInputFocus();
301
302   /**
303    * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
304    *
305    * A check is performed to ensure it was previously set, if this check fails then nothing is done.
306    * @pre The Actor has been initialized.
307    */
308   void ClearKeyInputFocus();
309
310   // Gesture Detection
311
312   /**
313    * @brief Retrieves the pinch gesture detector of the control.
314    *
315    * @return The pinch gesture detector.
316    * @note Will return an empty handle if the control does not handle the gesture itself.
317    */
318   PinchGestureDetector GetPinchGestureDetector() const;
319
320   /**
321    * @brief Retrieves the pan gesture detector of the control.
322    *
323    * @return The pan gesture detector.
324    * @note Will return an empty handle if the control does not handle the gesture itself.
325    */
326   PanGestureDetector GetPanGestureDetector() const;
327
328   /**
329    * @brief Retrieves the tap gesture detector of the control.
330    *
331    * @return The tap gesture detector.
332    * @note Will return an empty handle if the control does not handle the gesture itself.
333    */
334   TapGestureDetector GetTapGestureDetector() const;
335
336   /**
337    * @brief Retrieves the long press gesture detector of the control.
338    *
339    * @return The long press gesture detector.
340    * @note Will return an empty handle if the control does not handle the gesture itself.
341    */
342   LongPressGestureDetector GetLongPressGestureDetector() const;
343
344   // Background
345
346   /**
347    * @brief Sets the background color of the control.
348    *
349    * @param[in] color The required background color of the control
350    *
351    * @note The background color fully blends with the actor color.
352    */
353   void SetBackgroundColor( const Vector4& color );
354
355   /**
356    * @brief Retrieves the background color of the control.
357    *
358    * @return The background color of the control.
359    */
360   Vector4 GetBackgroundColor() const;
361
362   /**
363    * @brief Sets an image as the background of the control.
364    *
365    * The color of this image is blended with the background color @see SetBackgroundColor
366    *
367    * @param[in] image The image to set as the background.
368    */
369   void SetBackground( Image image );
370
371   /**
372    * @brief Clears the background.
373    */
374   void ClearBackground();
375
376   /**
377    * @brief Retrieves the actor used as the background for this control.
378    *
379    * @return The actor that used as the background for this control.
380    */
381   Actor GetBackgroundActor() const;
382
383   // Signals
384
385   /**
386    * @brief This signal is emitted when key event is received.
387    *
388    * A callback of the following type may be connected:
389    * @code
390    *   bool YourCallbackName(Control control, const KeyEvent& event);
391    * @endcode
392    * The return value of True, indicates that the touch event should be consumed.
393    * Otherwise the signal will be emitted on the next sensitive parent of the actor.
394    * @pre The Control has been initialized.
395    * @return The signal to connect to.
396    */
397   KeyEventSignalType& KeyEventSignal();
398
399 public: // Intended for control developers
400
401   /**
402    * @brief Create an initialised Control.
403    *
404    * @param[in] implementation The implementation for this control.
405    * @return A handle to a newly allocated Dali resource.
406    *
407    * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
408    */
409   explicit Control(Internal::Control& implementation);
410
411   /**
412    * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
413    * using an Internal CustomActor pointer.
414    *
415    * @param [in] internal A pointer to a newly allocated Dali resource
416    */
417   explicit Control(Dali::Internal::CustomActor* internal);
418
419 public: // Templates for Deriving Classes
420
421   /**
422    * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
423    *
424    * @tparam     T       The handle class
425    * @tparam     I       The implementation class
426    * @param[in]  handle  Handle to an object
427    * @return Handle to a class T or an uninitialized handle.
428    * @see DownCast(BaseHandle)
429    */
430   template<typename T, typename I>
431   DALI_INTERNAL static T DownCast( BaseHandle handle )
432   {
433     T result;
434
435     CustomActor custom = Dali::CustomActor::DownCast( handle );
436     if ( custom )
437     {
438       CustomActorImpl& customImpl = custom.GetImplementation();
439
440       I* impl = dynamic_cast<I*>(&customImpl);
441
442       if (impl)
443       {
444         result = T(customImpl.GetOwner());
445       }
446     }
447
448     return result;
449   }
450
451   /**
452    * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
453    * implementation of their class.
454    *
455    * @tparam     I         The implementation class
456    * @param[in]  internal  Pointer to the Internal::CustomActor
457    */
458   template<typename I>
459   DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
460   {
461     // Can have a NULL pointer so we only need to check if the internal implementation is our class
462     // when there is a value.
463     if (internal)
464     {
465       DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
466     }
467   }
468
469 };
470
471 } // namespace Toolkit
472
473 } // namespace Dali
474
475 #endif // __DALI_TOOLKIT_CONTROL_H__