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