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
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() |
60 * | %Action Name | %Control method called |
61 * |-------------------|-----------------------------------------------------|
62 * | control-activated | %OnActivated() |
64 class DALI_IMPORT_API Control : public CustomActor
69 * @brief The start and end property ranges for control.
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.
79 * @brief An enumeration of properties belonging to the Control class.
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
97 * @brief Describes how a control could be resized.
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.
109 * @brief Describes what a control should do when a contained actor/control exceeds the boundary of the control.
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.
119 * @brief Describes the direction to move the keyboard focus towards.
121 enum KeyboardFocusNavigationDirection
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
131 /// @brief Key Event signal type;
132 typedef Signal<bool ( Control, const KeyEvent& ) > KeyEventSignalType;
134 public: // Creation & Destruction
137 * @brief Create a new instance of a Control.
139 * @return A handle to a new Control.
141 static Control New();
144 * @brief Create an uninitialized Control handle.
146 * Only derived versions can be instantiated. Calling member
147 * functions with an uninitialized Dali::Object is not allowed.
152 * @brief Copy constructor.
154 * Creates another handle that points to the same real object
155 * @param[in] uiControl Handle to copy
157 Control(const Control& uiControl);
160 * @brief Dali::Control is intended as a base class
162 * This is non-virtual since derived Handle types must not contain data or virtual methods.
169 * @brief Assignment operator.
171 * Changes this handle to point to another real object
172 * @param[in] handle Object to assign this to
173 * @return reference to this
175 Control& operator=( const Control& handle );
180 * @brief Downcast an Object handle to Control.
182 * If handle points to a Control the downcast produces valid
183 * handle. If not the returned handle is left uninitialized.
185 * @param[in] handle Handle to an object
186 * @return handle to a Control or an uninitialized handle
188 static Control DownCast( BaseHandle handle );
191 * @brief Retrieve the Control implementation.
193 * @return The implementation.
195 Internal::Control& GetImplementation();
198 * @brief Retrieve the Control implementation.
200 * @return The implementation.
202 const Internal::Control& GetImplementation() const;
207 * @brief Sets the size policies for the width and height dimensions.
209 * @param[in] widthPolicy Size policy for the width dimension.
210 * @param[in] heightPolicy Size policy for the height dimension.
212 void SetSizePolicy( SizePolicy widthPolicy, SizePolicy heightPolicy );
215 * @brief Retrieves the size policies for the width and height dimensions.
217 * @param[out] widthPolicy Width's size policy.
218 * @param[out] heightPolicy Height's size policy.
220 void GetSizePolicy( SizePolicy& widthPolicy, SizePolicy& heightPolicy ) const;
223 * @brief Sets the minimum size for the control.
225 * @param[in] size The minimum size.
227 void SetMinimumSize( const Vector3& size );
230 * @brief Retrieves the minimum size.
232 * @return The minimum size.
234 const Vector3& GetMinimumSize() const;
237 * @brief Sets the maximum size.
239 * @param[in] size The maximum size.
241 void SetMaximumSize( const Vector3& size );
244 * @brief Retrieves the maximum size.
246 * @return The maximum size.
248 const Vector3& GetMaximumSize() const;
251 * @brief Works out the natural size.
253 * Natural size is the control's size with any restriction.
255 * @return The natural size.
257 Vector3 GetNaturalSize();
260 * @brief Works out the control's height for a given width.
262 * @param[in] width The control's width.
264 * @return The control's height for the given width.
266 float GetHeightForWidth( float width );
269 * @brief Works out the control's width for a given height.
271 * @param[in] height The control's height.
273 * @return The control's width for the given height.
275 float GetWidthForHeight( float height );
280 * @brief This sets the control to receive key events.
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.
287 void SetKeyInputFocus();
290 * @brief Quries whether the control has key input focus.
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
301 bool HasKeyInputFocus();
304 * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
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.
309 void ClearKeyInputFocus();
314 * @brief Retrieves the pinch gesture detector of the control.
316 * @return The pinch gesture detector.
317 * @note Will return an empty handle if the control does not handle the gesture itself.
319 PinchGestureDetector GetPinchGestureDetector() const;
322 * @brief Retrieves the pan gesture detector of the control.
324 * @return The pan gesture detector.
325 * @note Will return an empty handle if the control does not handle the gesture itself.
327 PanGestureDetector GetPanGestureDetector() const;
330 * @brief Retrieves the tap gesture detector of the control.
332 * @return The tap gesture detector.
333 * @note Will return an empty handle if the control does not handle the gesture itself.
335 TapGestureDetector GetTapGestureDetector() const;
338 * @brief Retrieves the long press gesture detector of the control.
340 * @return The long press gesture detector.
341 * @note Will return an empty handle if the control does not handle the gesture itself.
343 LongPressGestureDetector GetLongPressGestureDetector() const;
348 * @brief Sets the name of the style to be applied to the control.
350 * @param[in] styleName A string matching a style described in a stylesheet.
352 void SetStyleName( const std::string& styleName );
355 * @brief Retrieves the name of the style to be applied to the control (if any).
357 * @return A string matching a style or an empty string.
359 const std::string& GetStyleName() const;
364 * @brief Sets the background color of the control.
366 * @param[in] color The required background color of the control
368 * @note The background color fully blends with the actor color.
370 void SetBackgroundColor( const Vector4& color );
373 * @brief Retrieves the background color of the control.
375 * @return The background color of the control.
377 Vector4 GetBackgroundColor() const;
380 * @brief Sets an image as the background of the control.
382 * The color of this image is blended with the background color @see SetBackgroundColor
384 * @param[in] image The image to set as the background.
386 void SetBackground( Image image );
389 * @brief Clears the background.
391 void ClearBackground();
394 * @brief Retrieves the actor used as the background for this control.
396 * @return The actor that used as the background for this control.
398 Actor GetBackgroundActor() const;
403 * @brief This signal is emitted when key event is received.
405 * A callback of the following type may be connected:
407 * bool YourCallbackName(Control control, const KeyEvent& event);
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.
414 KeyEventSignalType& KeyEventSignal();
416 public: // Intended for control developers
419 * @brief Create an initialised Control.
421 * @param[in] implementation The implementation for this control.
422 * @return A handle to a newly allocated Dali resource.
424 * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
426 explicit Control(Internal::Control& implementation);
429 * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
430 * using an Internal CustomActor pointer.
432 * @param [in] internal A pointer to a newly allocated Dali resource
434 explicit Control(Dali::Internal::CustomActor* internal);
436 public: // Templates for Deriving Classes
439 * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
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)
447 template<typename T, typename I>
448 DALI_INTERNAL static T DownCast( BaseHandle handle )
452 CustomActor custom = Dali::CustomActor::DownCast( handle );
455 CustomActorImpl& customImpl = custom.GetImplementation();
457 I* impl = dynamic_cast<I*>(&customImpl);
461 result = T(customImpl.GetOwner());
469 * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
470 * implementation of their class.
472 * @tparam I The implementation class
473 * @param[in] internal Pointer to the Internal::CustomActor
476 DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
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.
482 DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
488 } // namespace Toolkit
492 #endif // __DALI_TOOLKIT_CONTROL_H__