1 #ifndef DALI_TOOLKIT_CONTROL_H
2 #define DALI_TOOLKIT_CONTROL_H
5 * Copyright (c) 2020 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>
31 #include <dali-toolkit/public-api/visuals/visual-properties.h>
39 //Forward declarations.
46 * @addtogroup dali_toolkit_controls
51 * @brief Control is the base class for all controls.
53 * The implementation of the control must be supplied; see Internal::Control for more details.
55 * @see Internal::Control
58 * | %Signal Name | Method |
59 * |------------------------|-----------------------------------------------------|
60 * | keyEvent | @ref KeyEventSignal() |
61 * | keyInputFocusGained | @ref KeyInputFocusGainedSignal() |
62 * | keyInputFocusLost | @ref KeyInputFocusLostSignal() |
63 * | resourceReady | @ref ResourceReadySignal() |
64 * | tapped | @ref GetTapGestureDetector().DetectedSignal() |
65 * | panned | @ref GetPanGestureDetector().DetectedSignal() |
66 * | pinched | @ref GetPinchGestureDetector().DetectedSignal() |
67 * | longPressed | @ref GetLongPressGestureDetector().DetectedSignal() |
70 * | %Action Name | %Control method called |
71 * |------------------------|----------------------------------------------------|
72 * | accessibilityActivated | %OnAccessibilityActivated() |
74 class DALI_TOOLKIT_API Control : public CustomActor
79 * @brief Enumeration for the start and end property ranges for control.
84 PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX, ///< Start index is used by the property registration macro. @SINCE_1_0.0
85 CONTROL_PROPERTY_START_INDEX = PROPERTY_START_INDEX, ///< Start index of Control properties. @SINCE_1_0.0
86 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices. @SINCE_1_0.0
90 * @brief Enumeration for the instance of properties belonging to the Control class.
96 * @brief Enumeration for the instance of properties belonging to the Control class.
102 * @brief The name of the style to be applied to the control.
103 * @details Name "styleName", type Property::STRING.
104 * @see Toolkit::Control::SetStyleName()
107 STYLE_NAME = PROPERTY_START_INDEX,
110 * @brief Receives key events to the control.
111 * @details Name "keyInputFocus", type Property::BOOLEAN.
112 * @see Toolkit::Control::SetKeyInputFocus()
118 * @brief The background of the control.
120 * @details Name "background", type Property::MAP or std::string for URL or Property::VECTOR4 for Color.
126 * @brief The outer space around the control.
127 * @details Name "margin", type Property::EXTENTS.
129 * @note Margin property is to be supported by Layout algorithms and containers in future.
134 * @brief The inner space of the control.
135 * @details Name "padding", type Property::EXTENTS.
143 * @brief Describes the direction to move the keyboard focus towards.
149 * @brief Keyboard focus direction.
154 LEFT, ///< Move keyboard focus towards the left direction @SINCE_1_0.0
155 RIGHT, ///< Move keyboard focus towards the right direction @SINCE_1_0.0
156 UP, ///< Move keyboard focus towards the up direction @SINCE_1_0.0
157 DOWN, ///< Move keyboard focus towards the down direction @SINCE_1_0.0
158 PAGE_UP, ///< Move keyboard focus towards the previous page direction @SINCE_1_2.14
159 PAGE_DOWN ///< Move keyboard focus towards the next page direction @SINCE_1_2.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 Creates a new instance of a Control.
180 * @return A handle to a new Control
182 static Control New();
185 * @brief Creates an uninitialized Control handle.
187 * Only derived versions can be instantiated. Calling member
188 * functions with an uninitialized Dali::Object is not allowed.
194 * @brief Copy constructor.
196 * Creates another handle that points to the same real object.
198 * @param[in] uiControl Handle to copy
200 Control(const Control& uiControl);
203 * @brief Dali::Control is intended as a base class.
205 * This is non-virtual since derived Handle types must not contain data or virtual methods.
213 * @brief Assignment operator.
215 * Changes this handle to point to another real object.
217 * @param[in] handle Object to assign this to
218 * @return Reference to this
220 Control& operator=( const Control& handle );
225 * @brief Downcasts a handle to Control handle.
227 * If handle points to a Control, the downcast produces valid handle.
228 * If not, the returned handle is left uninitialized.
231 * @param[in] handle Handle to an object
232 * @return A handle to a Control or an uninitialized handle
234 static Control DownCast( BaseHandle handle );
239 * @brief This sets the control to receive key events.
241 * The key event can originate from a virtual or physical keyboard.
243 * @pre The Control has been initialized.
244 * @pre The Control should be on the stage before setting keyboard focus.
246 void SetKeyInputFocus();
249 * @brief Quries whether the control has key input focus.
252 * @return true if this control has keyboard input focus
253 * @pre The Control has been initialized.
254 * @pre The Control should be on the stage before setting keyboard focus.
255 * @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.
256 * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
257 * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
258 * controls above it in the focus stack.
259 * To query for the control which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor().
261 bool HasKeyInputFocus();
264 * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
266 * A check is performed to ensure it was previously set, if this check fails then nothing is done.
268 * @pre The Actor has been initialized.
270 void ClearKeyInputFocus();
275 * @brief Retrieves the pinch gesture detector of the control.
278 * @return The pinch gesture detector
279 * @note Will return an empty handle if the control does not handle the gesture itself.
281 PinchGestureDetector GetPinchGestureDetector() const;
284 * @brief Retrieves the pan gesture detector of the control.
287 * @return The pan gesture detector
288 * @note Will return an empty handle if the control does not handle the gesture itself.
290 PanGestureDetector GetPanGestureDetector() const;
293 * @brief Retrieves the tap gesture detector of the control.
296 * @return The tap gesture detector
297 * @note Will return an empty handle if the control does not handle the gesture itself.
299 TapGestureDetector GetTapGestureDetector() const;
302 * @brief Retrieves the long press gesture detector of the control.
305 * @return The long press gesture detector
306 * @note Will return an empty handle if the control does not handle the gesture itself.
308 LongPressGestureDetector GetLongPressGestureDetector() const;
313 * @brief Sets the name of the style to be applied to the control.
316 * @param[in] styleName A string matching a style described in a stylesheet
318 void SetStyleName( const std::string& styleName );
321 * @brief Retrieves the name of the style to be applied to the control (if any).
323 * @return A string matching a style, or an empty string
325 const std::string& GetStyleName() const;
330 * @brief Sets the background color of the control.
333 * @param[in] color The required background color of the control
335 * @note If SetBackgroundImage is called later, this background color is removed.
337 * @note The background color fully blends with the actor color.
339 void SetBackgroundColor( const Vector4& color );
342 * @brief Clears the background.
345 void ClearBackground();
350 * @brief Query if all resources required by a control are loaded and ready.
352 * Most resources are only loaded when the control is placed on stage.
354 * @return true if the resources are loaded and ready, false otherwise
356 bool IsResourceReady() const;
359 * @brief Get the loading state of the visual resource.
362 * @param[in] index The Property index of the visual
363 * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
365 Visual::ResourceStatus GetVisualResourceStatus( const Dali::Property::Index index );
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 event should be consumed.
377 * Otherwise the signal will be emitted on the next parent of the actor.
379 * @return The signal to connect to
380 * @pre The Control has been initialized.
382 KeyEventSignalType& KeyEventSignal();
385 * @brief This signal is emitted when the control gets Key Input Focus.
387 * A callback of the following type may be connected:
389 * bool YourCallbackName( Control control );
391 * The return value of True, indicates that the event should be consumed.
392 * Otherwise the signal will be emitted on the next parent of the actor.
394 * @return The signal to connect to
395 * @pre The Control has been initialized.
397 KeyInputFocusSignalType& KeyInputFocusGainedSignal();
400 * @brief This signal is emitted when the control loses Key Input Focus.
402 * This could be due to it being gained by another Control or Actor or just cleared from
403 * this control as no longer required.
405 * A callback of the following type may be connected:
407 * bool YourCallbackName( Control control );
409 * The return value of True, indicates that the event should be consumed.
410 * Otherwise the signal will be emitted on the next parent of the actor.
412 * @return The signal to connect to
413 * @pre The Control has been initialized.
415 KeyInputFocusSignalType& KeyInputFocusLostSignal();
418 * @brief This signal is emitted after all resources required by a control are loaded and ready.
420 * Most resources are only loaded when the control is placed on stage.
422 * If resources are shared between ImageViews, they are cached.
423 * In this case, the ResourceReady signal may be sent before there is an object to connect to.
424 * To protect against this, IsResourceReady() can be checked first.
427 * auto newControl = Control::New();
428 * newControl.SetResource( resourceUrl );
429 * if ( newControl.IsResourceReady() )
435 * newControl.ResourceReadySignal.Connect( .... )
439 * A callback of the following type may be connected:
441 * void YourCallbackName( Control control );
445 * @return The signal to connect to
446 * @note A RelayoutRequest is queued by Control before this signal is emitted
448 ResourceReadySignalType& ResourceReadySignal();
450 public: // Intended for control developers
453 * @brief Creates an initialized Control.
456 * @param[in] implementation The implementation for this control
457 * @return A handle to a newly allocated Dali resource
458 * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
460 explicit Control(Internal::Control& implementation);
463 * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
464 * using an Internal CustomActor pointer.
467 * @param[in] internal A pointer to a newly allocated Dali resource
469 explicit Control(Dali::Internal::CustomActor* internal);
471 public: // Templates for Deriving Classes
474 * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
476 * @tparam T The handle class
477 * @tparam I The implementation class
479 * @param[in] handle Handle to an object
480 * @return Handle to a class T or an uninitialized handle
481 * @see DownCast(BaseHandle)
483 template<typename T, typename I>
484 DALI_INTERNAL static T DownCast( BaseHandle handle )
488 CustomActor custom = Dali::CustomActor::DownCast( handle );
491 CustomActorImpl& customImpl = custom.GetImplementation();
493 I* impl = dynamic_cast<I*>(&customImpl);
497 result = T(customImpl.GetOwner());
505 * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
506 * implementation of their class.
508 * @tparam I The implementation class
510 * @param[in] internal Pointer to the Internal::CustomActor
513 DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
515 // Can have a NULL pointer so we only need to check if the internal implementation is our class
516 // when there is a value.
519 DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
528 } // namespace Toolkit
532 #endif // DALI_TOOLKIT_CONTROL_H