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