1 #ifndef DALI_TOOLKIT_CONTROL_H
2 #define DALI_TOOLKIT_CONTROL_H
5 * Copyright (c) 2022 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-toolkit/public-api/dali-toolkit-common.h>
23 #include <dali/public-api/actors/custom-actor.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>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
36 //Forward declarations.
43 * @addtogroup dali_toolkit_controls
48 * @brief Control is the base class for all controls.
50 * The implementation of the control must be supplied; see Internal::Control for more details.
52 * @see Internal::Control
55 * | %Signal Name | Method |
56 * |------------------------|-----------------------------------------------------|
57 * | keyEvent | @ref KeyEventSignal() |
58 * | keyInputFocusGained | @ref KeyInputFocusGainedSignal() |
59 * | keyInputFocusLost | @ref KeyInputFocusLostSignal() |
60 * | resourceReady | @ref ResourceReadySignal() |
61 * | tapped | @ref GetTapGestureDetector().DetectedSignal() |
62 * | panned | @ref GetPanGestureDetector().DetectedSignal() |
63 * | pinched | @ref GetPinchGestureDetector().DetectedSignal() |
64 * | longPressed | @ref GetLongPressGestureDetector().DetectedSignal() |
67 * | %Action Name | %Control method called |
68 * |------------------------|----------------------------------------------------|
69 * | accessibilityActivated | %OnAccessibilityActivated() |
71 class DALI_TOOLKIT_API Control : public CustomActor
75 * @brief Enumeration for the start and end property ranges for control.
80 PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX, ///< Start index is used by the property registration macro. @SINCE_1_0.0
81 CONTROL_PROPERTY_START_INDEX = PROPERTY_START_INDEX, ///< Start index of Control properties. @SINCE_1_0.0
82 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices. @SINCE_1_0.0
86 * @brief Enumeration for the instance of properties belonging to the Control class.
92 * @brief Enumeration for the instance of properties belonging to the Control class.
98 * @brief The name of the style to be applied to the control.
99 * @details Name "styleName", type Property::STRING.
100 * @see Toolkit::Control::SetStyleName()
103 STYLE_NAME = PROPERTY_START_INDEX,
106 * @brief Receives key events to the control.
107 * @details Name "keyInputFocus", type Property::BOOLEAN.
108 * @see Toolkit::Control::SetKeyInputFocus()
114 * @brief The background of the control.
116 * @details Name "background", type Property::MAP or std::string for URL or Property::VECTOR4 for Color.
122 * @brief The outer space around the control.
123 * @details Name "margin", type Property::EXTENTS.
125 * @note Margin property is to be supported by Layout algorithms and containers in future.
130 * @brief The inner space of the control.
131 * @details Name "padding", type Property::EXTENTS.
139 * @brief Describes the direction to move the keyboard focus towards.
145 * @brief Keyboard focus direction.
150 LEFT, ///< Move keyboard focus towards the left direction @SINCE_1_0.0
151 RIGHT, ///< Move keyboard focus towards the right direction @SINCE_1_0.0
152 UP, ///< Move keyboard focus towards the up direction @SINCE_1_0.0
153 DOWN, ///< Move keyboard focus towards the down direction @SINCE_1_0.0
154 PAGE_UP, ///< Move keyboard focus towards the previous page direction @SINCE_1_2.14
155 PAGE_DOWN, ///< Move keyboard focus towards the next page direction @SINCE_1_2.14
156 FORWARD, ///< Move keyboard focus towards the forward direction @SINCE_2_1.10
157 BACKWARD, ///< Move keyboard focus towards the backward direction @SINCE_2_1.10
158 CLOCKWISE, ///< Move keyboard focus towards the clockwise direction @SINCE_2_1.14
159 COUNTER_CLOCKWISE, ///< Move keyboard focus towards the counter clockwise direction @SINCE_2_1.14
165 /// @brief Key Event signal type. @SINCE_1_0.0
166 typedef Signal<bool(Control, const KeyEvent&)> KeyEventSignalType;
168 /// @brief Key InputFocusType signal type. @SINCE_1_0.0
169 typedef Signal<void(Control)> KeyInputFocusSignalType;
171 /// @brief ResourceReady signal type. @SINCE_1_2.60
172 typedef Signal<void(Control)> ResourceReadySignalType;
174 public: // Creation & Destruction
177 * @brief Additional control behaviour flags for the control constructor.
178 * @note TODO : Currunt code is hard-coded. We Should sync type values as
179 * CustomActorImpl::ActorFlag and Internal::Control::ControlBehaviour in future.
182 enum ControlBehaviour
184 CONTROL_BEHAVIOUR_DEFAULT = 0, ///< Default behaviour: Size negotiation is enabled & listens to Style Change signal, but doesn't receive event callbacks. @SINCE_1_2_10
185 DISABLE_SIZE_NEGOTIATION = 1 << (0 + 0), ///< True if control does not need size negotiation, i.e. it can be skipped in the algorithm @SINCE_1_0.0
186 REQUIRES_KEYBOARD_NAVIGATION_SUPPORT = 1 << (4 + 1), ///< True if needs to support keyboard navigation @SINCE_1_0.0
187 DISABLE_STYLE_CHANGE_SIGNALS = 1 << (4 + 2), ///< True if control should not monitor style change signals @SINCE_1_2_10
191 * @brief Creates a new instance of a Control.
194 * @return A handle to a new Control
196 static Control New();
199 * @brief Creates a new instance of a Control with additional behaviour.
202 * @param[in] additionalBehaviour Additional control behaviour
203 * @return A handle to a new Control
205 static Control New(ControlBehaviour additionalBehaviour);
208 * @brief Creates an uninitialized Control handle.
210 * Only derived versions can be instantiated. Calling member
211 * functions with an uninitialized Dali::Object is not allowed.
217 * @brief Copy constructor.
219 * Creates another handle that points to the same real object.
221 * @param[in] uiControl Handle to copy
223 Control(const Control& uiControl);
226 * @brief Move constructor.
229 * @param[in] rhs Handle to move
231 Control(Control&& rhs);
234 * @brief Dali::Control is intended as a base class.
236 * This is non-virtual since derived Handle types must not contain data or virtual methods.
243 * @brief Copy assignment operator.
245 * Changes this handle to point to another real object.
247 * @param[in] handle Object to assign this to
248 * @return Reference to this
250 Control& operator=(const Control& handle);
253 * @brief Move assignment operator.
256 * @param[in] rhs Object to assign this to
257 * @return Reference to this
259 Control& operator=(Control&& rhs);
263 * @brief Downcasts a handle to Control handle.
265 * If handle points to a Control, the downcast produces valid handle.
266 * If not, the returned handle is left uninitialized.
269 * @param[in] handle Handle to an object
270 * @return A handle to a Control or an uninitialized handle
272 static Control DownCast(BaseHandle handle);
277 * @brief This sets the control to receive key events.
279 * The key event can originate from a virtual or physical keyboard.
281 * @pre The Control has been initialized.
282 * @pre The Control should be on the stage before setting keyboard focus.
284 void SetKeyInputFocus();
287 * @brief Quries whether the control has key input focus.
290 * @return true if this control has keyboard input focus
291 * @pre The Control has been initialized.
292 * @pre The Control should be on the stage before setting keyboard focus.
293 * @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.
294 * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
295 * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
296 * controls above it in the focus stack.
297 * To query for the control which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor().
299 bool HasKeyInputFocus();
302 * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
304 * A check is performed to ensure it was previously set, if this check fails then nothing is done.
306 * @pre The Actor has been initialized.
308 void ClearKeyInputFocus();
313 * @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.
325 * @return The pan gesture detector
326 * @note Will return an empty handle if the control does not handle the gesture itself.
328 PanGestureDetector GetPanGestureDetector() const;
331 * @brief Retrieves the tap gesture detector of the control.
334 * @return The tap gesture detector
335 * @note Will return an empty handle if the control does not handle the gesture itself.
337 TapGestureDetector GetTapGestureDetector() const;
340 * @brief Retrieves the long press gesture detector of the control.
343 * @return The long press gesture detector
344 * @note Will return an empty handle if the control does not handle the gesture itself.
346 LongPressGestureDetector GetLongPressGestureDetector() const;
351 * @brief Sets the name of the style to be applied to the control.
354 * @param[in] styleName A string matching a style described in a stylesheet
356 void SetStyleName(const std::string& styleName);
359 * @brief Retrieves the name of the style to be applied to the control (if any).
361 * @return A string matching a style, or an empty string
363 const std::string& GetStyleName() const;
368 * @brief Sets the background color of the control.
371 * @param[in] color The required background color of the control
373 * @note If SetBackgroundImage is called later, this background color is removed.
375 * @note The background color fully blends with the actor color.
377 void SetBackgroundColor(const Vector4& color);
380 * @brief Clears the background.
383 void ClearBackground();
388 * @brief Query if all resources required by a control are loaded and ready.
390 * Most resources are only loaded when the control is placed on stage.
392 * @return true if the resources are loaded and ready, false otherwise
394 bool IsResourceReady() const;
397 * @brief Get the loading state of the visual resource.
400 * @param[in] index The Property index of the visual
401 * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
403 Visual::ResourceStatus GetVisualResourceStatus(const Dali::Property::Index index);
408 * @brief This signal is emitted when key event is received.
410 * A callback of the following type may be connected:
412 * bool YourCallbackName(Control control, const KeyEvent& event);
414 * The return value of True, indicates that the event should be consumed.
415 * Otherwise the signal will be emitted on the next parent of the actor.
417 * @return The signal to connect to
418 * @pre The Control has been initialized.
420 KeyEventSignalType& KeyEventSignal();
423 * @brief This signal is emitted when the control gets Key Input Focus.
425 * A callback of the following type may be connected:
427 * bool YourCallbackName( Control control );
429 * The return value of True, indicates that the event should be consumed.
430 * Otherwise the signal will be emitted on the next parent of the actor.
432 * @return The signal to connect to
433 * @pre The Control has been initialized.
435 KeyInputFocusSignalType& KeyInputFocusGainedSignal();
438 * @brief This signal is emitted when the control loses Key Input Focus.
440 * This could be due to it being gained by another Control or Actor or just cleared from
441 * this control as no longer required.
443 * A callback of the following type may be connected:
445 * bool YourCallbackName( Control control );
447 * The return value of True, indicates that the event should be consumed.
448 * Otherwise the signal will be emitted on the next parent of the actor.
450 * @return The signal to connect to
451 * @pre The Control has been initialized.
453 KeyInputFocusSignalType& KeyInputFocusLostSignal();
456 * @brief This signal is emitted after all resources required by a control are loaded and ready.
458 * Most resources are only loaded when the control is placed on stage.
460 * If resources are shared between ImageViews, they are cached.
461 * In this case, the ResourceReady signal may be sent before there is an object to connect to.
462 * To protect against this, IsResourceReady() can be checked first.
465 * auto newControl = Control::New();
466 * newControl.SetResource( resourceUrl );
467 * if ( newControl.IsResourceReady() )
473 * newControl.ResourceReadySignal.Connect( .... )
477 * A callback of the following type may be connected:
479 * void YourCallbackName( Control control );
483 * @return The signal to connect to
484 * @note A RelayoutRequest is queued by Control before this signal is emitted
486 ResourceReadySignalType& ResourceReadySignal();
488 public: // Intended for control developers
490 * @brief Creates an initialized Control.
493 * @param[in] implementation The implementation for this control
494 * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
496 explicit Control(Internal::Control& implementation);
499 * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
500 * using an Internal CustomActor pointer.
503 * @param[in] internal A pointer to a newly allocated Dali resource
505 explicit Control(Dali::Internal::CustomActor* internal);
507 public: // Templates for Deriving Classes
509 * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
511 * @tparam T The handle class
512 * @tparam I The implementation class
514 * @param[in] handle Handle to an object
515 * @return Handle to a class T or an uninitialized handle
516 * @see DownCast(BaseHandle)
518 template<typename T, typename I>
519 DALI_INTERNAL static T DownCast(BaseHandle handle)
523 CustomActor custom = Dali::CustomActor::DownCast(handle);
526 CustomActorImpl& customImpl = custom.GetImplementation();
528 I* impl = dynamic_cast<I*>(&customImpl);
532 result = T(customImpl.GetOwner());
540 * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
541 * implementation of their class.
543 * @tparam I The implementation class
545 * @param[in] internal Pointer to the Internal::CustomActor
548 DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
550 // Can have a NULL pointer so we only need to check if the internal implementation is our class
551 // when there is a value.
554 DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
562 } // namespace Toolkit
566 #endif // DALI_TOOLKIT_CONTROL_H