f588a2c69d257eea562307c0289f82668f484ac9
[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   // Action Names
49   static const char* const ACTION_CONTROL_ACTIVATED; ///< name "control-activated"
50
51   // Signal Names
52   static const char* const SIGNAL_KEY_EVENT;         ///< name "key-event"
53
54   /**
55    * @brief Describes how a control could be resized.
56    */
57   enum SizePolicy
58   {
59     Fixed,    ///< Size can't grow or shrink.
60     Minimum,  ///< Size can grow but shrink up to a minimum level.
61     Maximum,  ///< Size can shrink but grow up to a maximum value.
62     Range,    ///< Size can grow or shrink between a minimum and a maximum values.
63     Flexible, ///< Size can grow or shrink with no limits.
64   };
65
66   /**
67    * @brief Describes what a control should do when a contained actor/control exceeds the boundary of the control.
68    */
69   enum ExceedPolicy
70   {
71     Crop,   ///< Control's contents will be cropped.
72     Shrink, ///< Control's contents will be shrunk.
73     Scroll  ///< Control's contents will be added to a scroll.
74   };
75
76   /**
77    * @brief Describes the direction to move the keyboard focus towards.
78    */
79   enum KeyboardFocusNavigationDirection
80   {
81     Left,   ///< Move keyboard focus towards the left direction
82     Right,  ///< Move keyboard focus towards the right direction
83     Up,     ///< Move keyboard focus towards the up direction
84     Down    ///< Move keyboard focus towards the down direction
85   };
86
87   // Typedefs
88
89   /// @brief Key Event signal type;
90   typedef SignalV2<bool ( Control, const KeyEvent& ) > KeyEventSignalV2;
91
92 public: // Creation & Destruction
93
94   /**
95    * @brief Create a new instance of a Control.
96    *
97    * @return A handle to a new Control.
98    */
99   static Control New();
100
101   /**
102    * @brief Create an uninitialized Control handle.
103    *
104    * Only derived versions can be instantiated.  Calling member
105    * functions with an uninitialized Dali::Object is not allowed.
106    */
107   Control();
108
109   /**
110    * @brief Copy constructor.
111    *
112    * Creates another handle that points to the same real object
113    * @param[in] uiControl Handle to copy
114    */
115   Control(const Control& uiControl);
116
117   /**
118    * @brief Virtual destructor.
119    *
120    * Dali::Object derived classes do not contain member data.
121    */
122   virtual ~Control();
123
124 public: // operators
125
126   /**
127    * @brief Assignment operator.
128    *
129    * Changes this handle to point to another real object
130    * @param[in] handle Object to assign this to
131    * @return reference to this
132    */
133   Control& operator=( const Control& handle );
134
135 public:
136
137   /**
138    * @brief Downcast an Object handle to Control.
139    *
140    * If handle points to a Control the downcast produces valid
141    * handle. If not the returned handle is left uninitialized.
142    *
143    * @param[in] handle Handle to an object
144    * @return handle to a Control or an uninitialized handle
145    */
146   static Control DownCast( BaseHandle handle );
147
148   /**
149    * @brief Retrieve the Control implementation.
150    *
151    * @return The implementation.
152    */
153   ControlImpl& GetImplementation();
154
155   /**
156    * @brief Retrieve the Control implementation.
157    *
158    * @return The implementation.
159    */
160   const ControlImpl& GetImplementation() const;
161
162   /**
163    * @brief Sets the size policies for the width and height dimensions.
164    *
165    * @param[in] widthPolicy Size policy for the width dimension.
166    * @param[in] heightPolicy Size policy for the height dimension.
167    */
168   void SetSizePolicy( SizePolicy widthPolicy, SizePolicy heightPolicy );
169
170   /**
171    * @brief Retrieves the size policies for the width and height dimensions.
172    *
173    * @param[out] widthPolicy Width's size policy.
174    * @param[out] heightPolicy Height's size policy.
175    */
176   void GetSizePolicy( SizePolicy& widthPolicy, SizePolicy& heightPolicy ) const;
177
178   /**
179    * @brief Sets the minimum size for the control.
180    *
181    * @param[in] size The minimum size.
182    */
183   void SetMinimumSize( const Vector3& size );
184
185   /**
186    * @brief Retrieves the minimum size.
187    *
188    * @return The minimum size.
189    */
190   const Vector3& GetMinimumSize() const;
191
192   /**
193    * @brief Sets the maximum size.
194    *
195    * @param[in] size The maximum size.
196    */
197   void SetMaximumSize( const Vector3& size );
198
199   /**
200    * @brief Retrieves the maximum size.
201    *
202    * @return The maximum size.
203    */
204   const Vector3& GetMaximumSize() const;
205
206   /**
207    * @brief Works out the natural size.
208    *
209    * Natural size is the control's size with any restriction.
210    *
211    * @return The natural size.
212    */
213   Vector3 GetNaturalSize();
214
215   /**
216    * @brief Works out the control's height for a given width.
217    *
218    * @param[in] width The control's width.
219    *
220    * @return The control's height for the given width.
221    */
222   float GetHeightForWidth( float width );
223
224   /**
225    * @brief Works out the control's width for a given height.
226    *
227    * @param[in] height The control's height.
228    *
229    * @return The control's width for the given height.
230    */
231   float GetWidthForHeight( float height );
232
233   /**
234    * @brief This sets the control to receive key events.
235    *
236    * The key event can originate from a virtual or physical keyboard.
237    * @pre The Control has been initialized.
238    * @pre The Control should be on the stage before setting keyboard focus.
239    * @return True if the control has foucs, False otherwise.
240    */
241   void SetKeyInputFocus();
242
243   /**
244    * @brief Quries whether the control has key input focus.
245    *
246    * 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.
247    * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
248    * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
249    * controls above it in the focus stack.
250    * To query for the conrol which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor()
251    * @pre The Control has been initialized.
252    * @pre The Control should be on the stage before setting keyboard focus.
253    * @return true if this control has keyboard input focus
254    */
255   bool HasKeyInputFocus();
256
257   /**
258    * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
259    *
260    * A check is performed to ensure it was previously set, if this check fails then nothing is done.
261    * @pre The Actor has been initialized.
262    */
263   void ClearKeyInputFocus();
264
265 //signals
266 public:
267
268   /**
269    * @brief This signal is emitted when key event is received.
270    *
271    * A callback of the following type may be connected:
272    * @code
273    *   bool YourCallbackName(Control control, const KeyEvent& event);
274    * @endcode
275    * The return value of True, indicates that the touch event should be consumed.
276    * Otherwise the signal will be emitted on the next sensitive parent of the actor.
277    * @pre The Control has been initialized.
278    * @return The signal to connect to.
279    */
280   KeyEventSignalV2& KeyEventSignal();
281
282 protected:
283
284   /**
285    * @copydoc ConnectionTrackerInterface::SignalConnected
286    */
287   virtual void SignalConnected( SlotObserver* slotObserver, CallbackBase* callback );
288
289   /**
290    * @copydoc ConnectionTrackerInterface::SignalDisconnected
291    */
292   virtual void SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback );
293
294   /**
295    * @copydoc ConnectionTrackerInterface::GetConnectionCount
296    */
297   virtual std::size_t GetConnectionCount() const;
298
299 public: // Not intended for application developers
300
301   /**
302    * @brief Create an initialised Control.
303    *
304    * @param[in] implementation The implementation for this control.
305    * @return A handle to a newly allocated Dali resource.
306    */
307   Control(ControlImpl& implementation);
308
309   /**
310    * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
311    * using an Internal CustomActor pointer.
312    *
313    * @param [in] internal A pointer to a newly allocated Dali resource
314    */
315   Control(Dali::Internal::CustomActor* internal);
316
317 public: // Templates for Deriving Classes
318
319   /**
320    * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
321    *
322    * @tparam     T       The handle class
323    * @tparam     I       The implementation class
324    * @param[in]  handle  Handle to an object
325    * @return Handle to a class T or an uninitialized handle.
326    * @see DownCast(BaseHandle)
327    */
328   template<typename T, typename I>
329   static T DownCast( BaseHandle handle )
330   {
331     T result;
332
333     CustomActor custom = Dali::CustomActor::DownCast( handle );
334     if ( custom )
335     {
336       CustomActorImpl& customImpl = custom.GetImplementation();
337
338       I* impl = dynamic_cast<I*>(&customImpl);
339
340       if (impl)
341       {
342         result = T(customImpl.GetOwner());
343       }
344     }
345
346     return result;
347   }
348
349   /**
350    * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
351    * implementation of their class.
352    *
353    * @tparam     I         The implementation class
354    * @param[in]  internal  Pointer to the Internal::CustomActor
355    */
356   template<typename I>
357   void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
358   {
359     // Can have a NULL pointer so we only need to check if the internal implementation is our class
360     // when there is a value.
361     if (internal)
362     {
363       DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
364     }
365   }
366
367 };
368
369 } // namespace Toolkit
370
371 } // namespace Dali
372
373 /**
374  * @}
375  */
376 #endif // __DALI_TOOLKIT_CONTROL_H__