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