1 #ifndef __DALI_TOOLKIT_CONTROL_H__
2 #define __DALI_TOOLKIT_CONTROL_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
37 //Forward declarations.
45 * @brief Control is the base class for all controls.
47 * The implementation of the control must be supplied; see Internal::Control for more details.
48 * @see Internal::Control
50 class DALI_IMPORT_API Control : public CustomActor
56 static const Property::Index PROPERTY_BACKGROUND_COLOR; ///< name "background-color", @see SetBackgroundColor, type VECTOR4
57 static const Property::Index PROPERTY_BACKGROUND; ///< name "background", @see SetBackground, type MAP
58 static const Property::Index PROPERTY_WIDTH_POLICY; ///< name "width-policy", @see SetSizePolicy, type STRING
59 static const Property::Index PROPERTY_HEIGHT_POLICY; ///< name "height-policy", @see SetSizePolicy, type STRING
60 static const Property::Index PROPERTY_MINIMUM_SIZE; ///< name "minimum-size", @see SetMinimumSize, type VECTOR3
61 static const Property::Index PROPERTY_MAXIMUM_SIZE; ///< name "maximum-size", @see SetMaximumSize, type VECTOR3
62 static const Property::Index PROPERTY_KEY_INPUT_FOCUS; ///< name "key-input-focus", @see SetKeyInputFocus, type BOOLEAN
67 static const char* const SIGNAL_KEY_EVENT; ///< name "key-event"
68 static const char* const SIGNAL_TAPPED; ///< name "tapped"
69 static const char* const SIGNAL_PANNED; ///< name "panned"
70 static const char* const SIGNAL_PINCHED; ///< name "pinched"
71 static const char* const SIGNAL_LONG_PRESSED; ///< name "long-pressed"
76 static const char* const ACTION_CONTROL_ACTIVATED; ///< name "control-activated"
80 * @brief Describes how a control could be resized.
84 Fixed, ///< Size can't grow or shrink.
85 Minimum, ///< Size can grow but shrink up to a minimum level.
86 Maximum, ///< Size can shrink but grow up to a maximum value.
87 Range, ///< Size can grow or shrink between a minimum and a maximum values.
88 Flexible, ///< Size can grow or shrink with no limits.
92 * @brief Describes what a control should do when a contained actor/control exceeds the boundary of the control.
96 Crop, ///< Control's contents will be cropped.
97 Shrink, ///< Control's contents will be shrunk.
98 Scroll ///< Control's contents will be added to a scroll.
102 * @brief Describes the direction to move the keyboard focus towards.
104 enum KeyboardFocusNavigationDirection
106 Left, ///< Move keyboard focus towards the left direction
107 Right, ///< Move keyboard focus towards the right direction
108 Up, ///< Move keyboard focus towards the up direction
109 Down ///< Move keyboard focus towards the down direction
114 /// @brief Key Event signal type;
115 typedef Signal<bool ( Control, const KeyEvent& ) > KeyEventSignalType;
117 public: // Creation & Destruction
120 * @brief Create a new instance of a Control.
122 * @return A handle to a new Control.
124 static Control New();
127 * @brief Create an uninitialized Control handle.
129 * Only derived versions can be instantiated. Calling member
130 * functions with an uninitialized Dali::Object is not allowed.
135 * @brief Copy constructor.
137 * Creates another handle that points to the same real object
138 * @param[in] uiControl Handle to copy
140 Control(const Control& uiControl);
143 * @brief Dali::Control is intended as a base class
145 * This is non-virtual since derived Handle types must not contain data or virtual methods.
152 * @brief Assignment operator.
154 * Changes this handle to point to another real object
155 * @param[in] handle Object to assign this to
156 * @return reference to this
158 Control& operator=( const Control& handle );
163 * @brief Downcast an Object handle to Control.
165 * If handle points to a Control the downcast produces valid
166 * handle. If not the returned handle is left uninitialized.
168 * @param[in] handle Handle to an object
169 * @return handle to a Control or an uninitialized handle
171 static Control DownCast( BaseHandle handle );
174 * @brief Retrieve the Control implementation.
176 * @return The implementation.
178 Internal::Control& GetImplementation();
181 * @brief Retrieve the Control implementation.
183 * @return The implementation.
185 const Internal::Control& GetImplementation() const;
190 * @brief Sets the size policies for the width and height dimensions.
192 * @param[in] widthPolicy Size policy for the width dimension.
193 * @param[in] heightPolicy Size policy for the height dimension.
195 void SetSizePolicy( SizePolicy widthPolicy, SizePolicy heightPolicy );
198 * @brief Retrieves the size policies for the width and height dimensions.
200 * @param[out] widthPolicy Width's size policy.
201 * @param[out] heightPolicy Height's size policy.
203 void GetSizePolicy( SizePolicy& widthPolicy, SizePolicy& heightPolicy ) const;
206 * @brief Sets the minimum size for the control.
208 * @param[in] size The minimum size.
210 void SetMinimumSize( const Vector3& size );
213 * @brief Retrieves the minimum size.
215 * @return The minimum size.
217 const Vector3& GetMinimumSize() const;
220 * @brief Sets the maximum size.
222 * @param[in] size The maximum size.
224 void SetMaximumSize( const Vector3& size );
227 * @brief Retrieves the maximum size.
229 * @return The maximum size.
231 const Vector3& GetMaximumSize() const;
234 * @brief Works out the natural size.
236 * Natural size is the control's size with any restriction.
238 * @return The natural size.
240 Vector3 GetNaturalSize();
243 * @brief Works out the control's height for a given width.
245 * @param[in] width The control's width.
247 * @return The control's height for the given width.
249 float GetHeightForWidth( float width );
252 * @brief Works out the control's width for a given height.
254 * @param[in] height The control's height.
256 * @return The control's width for the given height.
258 float GetWidthForHeight( float height );
263 * @brief This sets the control to receive key events.
265 * The key event can originate from a virtual or physical keyboard.
266 * @pre The Control has been initialized.
267 * @pre The Control should be on the stage before setting keyboard focus.
268 * @return True if the control has foucs, False otherwise.
270 void SetKeyInputFocus();
273 * @brief Quries whether the control has key input focus.
275 * 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.
276 * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
277 * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
278 * controls above it in the focus stack.
279 * To query for the conrol which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor()
280 * @pre The Control has been initialized.
281 * @pre The Control should be on the stage before setting keyboard focus.
282 * @return true if this control has keyboard input focus
284 bool HasKeyInputFocus();
287 * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
289 * A check is performed to ensure it was previously set, if this check fails then nothing is done.
290 * @pre The Actor has been initialized.
292 void ClearKeyInputFocus();
297 * @brief Retrieves the pinch gesture detector of the control.
299 * @return The pinch gesture detector.
300 * @note Will return an empty handle if the control does not handle the gesture itself.
302 PinchGestureDetector GetPinchGestureDetector() const;
305 * @brief Retrieves the pan gesture detector of the control.
307 * @return The pan gesture detector.
308 * @note Will return an empty handle if the control does not handle the gesture itself.
310 PanGestureDetector GetPanGestureDetector() const;
313 * @brief Retrieves the tap gesture detector of the control.
315 * @return The tap gesture detector.
316 * @note Will return an empty handle if the control does not handle the gesture itself.
318 TapGestureDetector GetTapGestureDetector() const;
321 * @brief Retrieves the long press gesture detector of the control.
323 * @return The long press gesture detector.
324 * @note Will return an empty handle if the control does not handle the gesture itself.
326 LongPressGestureDetector GetLongPressGestureDetector() const;
331 * @brief Sets the background color of the control.
333 * @param[in] color The required background color of the control
335 * @note The background color fully blends with the actor color.
337 void SetBackgroundColor( const Vector4& color );
340 * @brief Retrieves the background color of the control.
342 * @return The background color of the control.
344 Vector4 GetBackgroundColor() const;
347 * @brief Sets an image as the background of the control.
349 * The color of this image is blended with the background color @see SetBackgroundColor
351 * @param[in] image The image to set as the background.
353 void SetBackground( Image image );
356 * @brief Clears the background.
358 void ClearBackground();
361 * @brief Retrieves the actor used as the background for this control.
363 * @return The actor that used as the background for this control.
365 Actor GetBackgroundActor() const;
370 * @brief This signal is emitted when key event is received.
372 * A callback of the following type may be connected:
374 * bool YourCallbackName(Control control, const KeyEvent& event);
376 * The return value of True, indicates that the touch event should be consumed.
377 * Otherwise the signal will be emitted on the next sensitive parent of the actor.
378 * @pre The Control has been initialized.
379 * @return The signal to connect to.
381 KeyEventSignalType& KeyEventSignal();
383 public: // Intended for control developers
386 * @brief Create an initialised Control.
388 * @param[in] implementation The implementation for this control.
389 * @return A handle to a newly allocated Dali resource.
391 * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
393 explicit Control(Internal::Control& implementation);
396 * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
397 * using an Internal CustomActor pointer.
399 * @param [in] internal A pointer to a newly allocated Dali resource
401 explicit Control(Dali::Internal::CustomActor* internal);
403 public: // Templates for Deriving Classes
406 * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
408 * @tparam T The handle class
409 * @tparam I The implementation class
410 * @param[in] handle Handle to an object
411 * @return Handle to a class T or an uninitialized handle.
412 * @see DownCast(BaseHandle)
414 template<typename T, typename I>
415 DALI_INTERNAL static T DownCast( BaseHandle handle )
419 CustomActor custom = Dali::CustomActor::DownCast( handle );
422 CustomActorImpl& customImpl = custom.GetImplementation();
424 I* impl = dynamic_cast<I*>(&customImpl);
428 result = T(customImpl.GetOwner());
436 * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
437 * implementation of their class.
439 * @tparam I The implementation class
440 * @param[in] internal Pointer to the Internal::CustomActor
443 DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
445 // Can have a NULL pointer so we only need to check if the internal implementation is our class
446 // when there is a value.
449 DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
455 } // namespace Toolkit
459 #endif // __DALI_TOOLKIT_CONTROL_H__