1 #ifndef DALI_TOOLKIT_CONTROL_H
2 #define DALI_TOOLKIT_CONTROL_H
5 * Copyright (c) 2019 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-toolkit/public-api/dali-toolkit-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>
32 #include <dali-toolkit/public-api/visuals/visual-properties.h>
40 //Forward declarations.
47 * @addtogroup dali_toolkit_controls
54 * @brief Control is the base class for all controls.
56 * The implementation of the control must be supplied; see Internal::Control for more details.
58 * @see Internal::Control
61 * | %Signal Name | Method |
62 * |------------------------|-----------------------------------------------------|
63 * | keyEvent | @ref KeyEventSignal() |
64 * | keyInputFocusGained | @ref KeyInputFocusGainedSignal() |
65 * | keyInputFocusLost | @ref KeyInputFocusLostSignal() |
66 * | resourceReady | @ref ResourceReadySignal() |
67 * | tapped | @ref GetTapGestureDetector().DetectedSignal() |
68 * | panned | @ref GetPanGestureDetector().DetectedSignal() |
69 * | pinched | @ref GetPinchGestureDetector().DetectedSignal() |
70 * | longPressed | @ref GetLongPressGestureDetector().DetectedSignal() |
73 * | %Action Name | %Control method called |
74 * |------------------------|----------------------------------------------------|
75 * | accessibilityActivated | %OnAccessibilityActivated() |
77 class DALI_TOOLKIT_API Control : public CustomActor
82 * @brief Enumeration for the start and end property ranges for control.
87 PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX, ///< Start index is used by the property registration macro. @SINCE_1_0.0
88 CONTROL_PROPERTY_START_INDEX = PROPERTY_START_INDEX, ///< Start index of Control properties. @SINCE_1_0.0
89 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices. @SINCE_1_0.0
93 * @brief Enumeration for the instance of properties belonging to the Control class.
99 * @brief Enumeration for the instance of properties belonging to the Control class.
105 * @brief The name of the style to be applied to the control.
106 * @details Name "styleName", type Property::STRING.
107 * @see Toolkit::Control::SetStyleName()
110 STYLE_NAME = PROPERTY_START_INDEX,
112 RESERVED_PROPERTY_01, ///< Reserved index for a removed property.
113 RESERVED_PROPERTY_02, ///< Reserved index for a removed property.
116 * @brief Receives key events to the control.
117 * @details Name "keyInputFocus", type Property::BOOLEAN.
118 * @see Toolkit::Control::SetKeyInputFocus()
124 * @brief The background of the control.
126 * @details Name "background", type Property::MAP or std::string for URL or Property::VECTOR4 for Color.
132 * @brief The outer space around the control.
133 * @details Name "margin", type Property::EXTENTS.
135 * @note Margin property is to be supported by Layout algorithms and containers in future.
140 * @brief The inner space of the control.
141 * @details Name "padding", type Property::EXTENTS.
149 * @brief Describes the direction to move the keyboard focus towards.
155 * @brief Keyboard focus direction.
160 LEFT, ///< Move keyboard focus towards the left direction @SINCE_1_0.0
161 RIGHT, ///< Move keyboard focus towards the right direction @SINCE_1_0.0
162 UP, ///< Move keyboard focus towards the up direction @SINCE_1_0.0
163 DOWN, ///< Move keyboard focus towards the down direction @SINCE_1_0.0
164 PAGE_UP, ///< Move keyboard focus towards the previous page direction @SINCE_1_2.14
165 PAGE_DOWN ///< Move keyboard focus towards the next page direction @SINCE_1_2.14
171 /// @brief Key Event signal type. @SINCE_1_0.0
172 typedef Signal<bool ( Control, const KeyEvent& ) > KeyEventSignalType;
174 /// @brief Key InputFocusType signal type. @SINCE_1_0.0
175 typedef Signal<void ( Control ) > KeyInputFocusSignalType;
177 /// @brief ResourceReady signal type. @SINCE_1_2.60
178 typedef Signal<void ( Control ) > ResourceReadySignalType;
180 public: // Creation & Destruction
185 * @brief Creates a new instance of a Control.
188 * @return A handle to a new Control
190 static Control New();
193 * @brief Creates an uninitialized Control handle.
195 * Only derived versions can be instantiated. Calling member
196 * functions with an uninitialized Dali::Object is not allowed.
202 * @brief Copy constructor.
204 * Creates another handle that points to the same real object.
206 * @param[in] uiControl Handle to copy
208 Control(const Control& uiControl);
211 * @brief Dali::Control is intended as a base class.
213 * This is non-virtual since derived Handle types must not contain data or virtual methods.
221 * @brief Assignment operator.
223 * Changes this handle to point to another real object.
225 * @param[in] handle Object to assign this to
226 * @return Reference to this
228 Control& operator=( const Control& handle );
233 * @brief Downcasts a handle to Control handle.
235 * If handle points to a Control, the downcast produces valid handle.
236 * If not, the returned handle is left uninitialized.
239 * @param[in] handle Handle to an object
240 * @return A handle to a Control or an uninitialized handle
242 static Control DownCast( BaseHandle handle );
247 * @brief This sets the control to receive key events.
249 * 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.
254 void SetKeyInputFocus();
257 * @brief Quries whether the control has key input focus.
260 * @return true if this control has keyboard input focus
261 * @pre The Control has been initialized.
262 * @pre The Control should be on the stage before setting keyboard focus.
263 * @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.
264 * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
265 * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
266 * controls above it in the focus stack.
267 * To query for the control which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor().
269 bool HasKeyInputFocus();
272 * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
274 * A check is performed to ensure it was previously set, if this check fails then nothing is done.
276 * @pre The Actor has been initialized.
278 void ClearKeyInputFocus();
283 * @brief Retrieves the pinch gesture detector of the control.
286 * @return The pinch gesture detector
287 * @note Will return an empty handle if the control does not handle the gesture itself.
289 PinchGestureDetector GetPinchGestureDetector() const;
292 * @brief Retrieves the pan gesture detector of the control.
295 * @return The pan gesture detector
296 * @note Will return an empty handle if the control does not handle the gesture itself.
298 PanGestureDetector GetPanGestureDetector() const;
301 * @brief Retrieves the tap gesture detector of the control.
304 * @return The tap gesture detector
305 * @note Will return an empty handle if the control does not handle the gesture itself.
307 TapGestureDetector GetTapGestureDetector() const;
310 * @brief Retrieves the long press gesture detector of the control.
313 * @return The long press gesture detector
314 * @note Will return an empty handle if the control does not handle the gesture itself.
316 LongPressGestureDetector GetLongPressGestureDetector() const;
321 * @brief Sets the name of the style to be applied to the control.
324 * @param[in] styleName A string matching a style described in a stylesheet
326 void SetStyleName( const std::string& styleName );
329 * @brief Retrieves the name of the style to be applied to the control (if any).
331 * @return A string matching a style, or an empty string
333 const std::string& GetStyleName() const;
338 * @brief Sets the background color of the control.
341 * @param[in] color The required background color of the control
343 * @note If SetBackgroundImage is called later, this background color is removed.
345 * @note The background color fully blends with the actor color.
347 void SetBackgroundColor( const Vector4& color );
350 * @brief Clears the background.
353 void ClearBackground();
358 * @brief Query if all resources required by a control are loaded and ready.
360 * Most resources are only loaded when the control is placed on stage.
362 * @return true if the resources are loaded and ready, false otherwise
364 bool IsResourceReady() const;
367 * @brief Get the loading state of the visual resource.
370 * @param[in] index The Property index of the visual
371 * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
373 Visual::ResourceStatus GetVisualResourceStatus( const Dali::Property::Index index );
378 * @brief This signal is emitted when key event is received.
380 * A callback of the following type may be connected:
382 * bool YourCallbackName(Control control, const KeyEvent& event);
384 * The return value of True, indicates that the event should be consumed.
385 * Otherwise the signal will be emitted on the next parent of the actor.
387 * @return The signal to connect to
388 * @pre The Control has been initialized.
390 KeyEventSignalType& KeyEventSignal();
393 * @brief This signal is emitted when the control gets Key Input Focus.
395 * A callback of the following type may be connected:
397 * bool YourCallbackName( Control control );
399 * The return value of True, indicates that the event should be consumed.
400 * Otherwise the signal will be emitted on the next parent of the actor.
402 * @return The signal to connect to
403 * @pre The Control has been initialized.
405 KeyInputFocusSignalType& KeyInputFocusGainedSignal();
408 * @brief This signal is emitted when the control loses Key Input Focus.
410 * This could be due to it being gained by another Control or Actor or just cleared from
411 * this control as no longer required.
413 * A callback of the following type may be connected:
415 * bool YourCallbackName( Control control );
417 * The return value of True, indicates that the event should be consumed.
418 * Otherwise the signal will be emitted on the next parent of the actor.
420 * @return The signal to connect to
421 * @pre The Control has been initialized.
423 KeyInputFocusSignalType& KeyInputFocusLostSignal();
426 * @brief This signal is emitted after all resources required by a control are loaded and ready.
428 * Most resources are only loaded when the control is placed on stage.
430 * If resources are shared between ImageViews, they are cached.
431 * In this case, the ResourceReady signal may be sent before there is an object to connect to.
432 * To protect against this, IsResourceReady() can be checked first.
435 * auto newControl = Control::New();
436 * newControl.SetResource( resourceUrl );
437 * if ( newControl.IsResourceReady() )
443 * newControl.ResourceReadySignal.Connect( .... )
447 * A callback of the following type may be connected:
449 * void YourCallbackName( Control control );
453 * @return The signal to connect to
454 * @note A RelayoutRequest is queued by Control before this signal is emitted
456 ResourceReadySignalType& ResourceReadySignal();
458 public: // Intended for control developers
461 * @brief Creates an initialized Control.
464 * @param[in] implementation The implementation for this control
465 * @return A handle to a newly allocated Dali resource
466 * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
468 explicit Control(Internal::Control& implementation);
471 * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
472 * using an Internal CustomActor pointer.
475 * @param[in] internal A pointer to a newly allocated Dali resource
477 explicit Control(Dali::Internal::CustomActor* internal);
479 public: // Templates for Deriving Classes
482 * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
484 * @tparam T The handle class
485 * @tparam I The implementation class
487 * @param[in] handle Handle to an object
488 * @return Handle to a class T or an uninitialized handle
489 * @see DownCast(BaseHandle)
491 template<typename T, typename I>
492 DALI_INTERNAL static T DownCast( BaseHandle handle )
496 CustomActor custom = Dali::CustomActor::DownCast( handle );
499 CustomActorImpl& customImpl = custom.GetImplementation();
501 I* impl = dynamic_cast<I*>(&customImpl);
505 result = T(customImpl.GetOwner());
513 * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
514 * implementation of their class.
516 * @tparam I The implementation class
518 * @param[in] internal Pointer to the Internal::CustomActor
521 DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
523 // Can have a NULL pointer so we only need to check if the internal implementation is our class
524 // when there is a value.
527 DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
536 } // namespace Toolkit
540 #endif // DALI_TOOLKIT_CONTROL_H