1 #ifndef __DALI_TOOLKIT_CONTROL_H__
2 #define __DALI_TOOLKIT_CONTROL_H__
5 * Copyright (c) 2017 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>
32 #include <dali-toolkit/public-api/visuals/visual-properties.h>
40 //Forward declarations.
47 * @addtogroup dali_toolkit_controls
52 * @brief Control is the base class for all controls.
54 * The implementation of the control must be supplied; see Internal::Control for more details.
56 * @see Internal::Control
59 * | %Signal Name | Method |
60 * |------------------------|-----------------------------------------------------|
61 * | keyEvent | @ref KeyEventSignal() |
62 * | keyInputFocusGained | @ref KeyInputFocusGainedSignal() |
63 * | keyInputFocusLost | @ref KeyInputFocusLostSignal() |
64 * | resourceReady | @ref ResourceReadySignal() |
65 * | tapped | @ref GetTapGestureDetector().DetectedSignal() |
66 * | panned | @ref GetPanGestureDetector().DetectedSignal() |
67 * | pinched | @ref GetPinchGestureDetector().DetectedSignal() |
68 * | longPressed | @ref GetLongPressGestureDetector().DetectedSignal() |
71 * | %Action Name | %Control method called |
72 * |------------------------|----------------------------------------------------|
73 * | accessibilityActivated | %OnAccessibilityActivated() |
75 class DALI_IMPORT_API Control : public CustomActor
80 * @brief Enumeration for the start and end property ranges for control.
85 PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX, ///< Start index is used by the property registration macro. @SINCE_1_0.0
86 CONTROL_PROPERTY_START_INDEX = PROPERTY_START_INDEX, ///< Start index of Control properties. @SINCE_1_0.0
87 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices. @SINCE_1_0.0
91 * @brief Enumeration for the instance of properties belonging to the Control class.
97 * @brief Enumeration for the instance of properties belonging to the Control class.
103 * @brief The name of the style to be applied to the control.
104 * @details Name "styleName", type Property::STRING.
105 * @see Toolkit::Control::SetStyleName()
108 STYLE_NAME = PROPERTY_START_INDEX,
112 * @brief The background color of the control.
114 * Mutually exclusive with BACKGROUND_IMAGE & BACKGROUND.
115 * @details Name "backgroundColor", type Property::VECTOR4.
116 * @see Toolkit::Control::SetStyleName()
123 * @brief The background image of the control.
125 * Mutually exclusive with BACKGROUND_COLOR & BACKGROUND.
126 * @details Name "backgroundImage", type Property::MAP.
132 * @brief Receives key events to the control.
133 * @details Name "keyInputFocus", type Property::BOOLEAN.
134 * @see Toolkit::Control::SetKeyInputFocus()
140 * @brief The background of the control.
142 * Mutually exclusive with BACKGROUND_COLOR & BACKGROUND_IMAGE.
143 * @details Name "background", type Property::MAP or std::string for URL or Property::VECTOR4 for Color.
149 * @brief The outer space around the control.
150 * @details Name "margin", type Property::EXTENTS.
152 * @note Margin property is to be supported by Layout algorithms and containers in future.
159 ACCESSIBILITY_ATTRIBUTES,
162 * @brief The inner space of the control.
163 * @details Name "padding", type Property::EXTENTS.
171 * @brief Describes the direction to move the keyboard focus towards.
177 * @brief Keyboard focus direction.
182 LEFT, ///< Move keyboard focus towards the left direction @SINCE_1_0.0
183 RIGHT, ///< Move keyboard focus towards the right direction @SINCE_1_0.0
184 UP, ///< Move keyboard focus towards the up direction @SINCE_1_0.0
185 DOWN, ///< Move keyboard focus towards the down direction @SINCE_1_0.0
186 PAGE_UP, ///< Move keyboard focus towards the previous page direction @SINCE_1_2.14
187 PAGE_DOWN ///< Move keyboard focus towards the next page direction @SINCE_1_2.14
193 /// @brief Key Event signal type. @SINCE_1_0.0
194 typedef Signal<bool ( Control, const KeyEvent& ) > KeyEventSignalType;
196 /// @brief Key InputFocusType signal type. @SINCE_1_0.0
197 typedef Signal<void ( Control ) > KeyInputFocusSignalType;
199 /// @brief ResourceReady signal type. @SINCE_1_2.60
200 typedef Signal<void ( Control ) > ResourceReadySignalType;
202 typedef Signal< void ( ) > AccessibilityActivateSignalType;
204 public: // Creation & Destruction
207 * @brief Creates a new instance of a Control.
210 * @return A handle to a new Control
212 static Control New();
215 * @brief Creates an uninitialized Control handle.
217 * Only derived versions can be instantiated. Calling member
218 * functions with an uninitialized Dali::Object is not allowed.
224 * @brief Copy constructor.
226 * Creates another handle that points to the same real object.
228 * @param[in] uiControl Handle to copy
230 Control(const Control& uiControl);
233 * @brief Dali::Control is intended as a base class.
235 * This is non-virtual since derived Handle types must not contain data or virtual methods.
243 * @brief 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 );
255 * @brief Downcasts a handle to Control handle.
257 * If handle points to a Control, the downcast produces valid handle.
258 * If not, the returned handle is left uninitialized.
261 * @param[in] handle Handle to an object
262 * @return A handle to a Control or an uninitialized handle
264 static Control DownCast( BaseHandle handle );
269 * @brief This sets the control to receive key events.
271 * The key event can originate from a virtual or physical keyboard.
273 * @pre The Control has been initialized.
274 * @pre The Control should be on the stage before setting keyboard focus.
276 void SetKeyInputFocus();
279 * @brief Quries whether the control has key input focus.
282 * @return true if this control has keyboard input focus
283 * @pre The Control has been initialized.
284 * @pre The Control should be on the stage before setting keyboard focus.
285 * @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.
286 * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
287 * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
288 * controls above it in the focus stack.
289 * To query for the control which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor().
291 bool HasKeyInputFocus();
294 * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
296 * A check is performed to ensure it was previously set, if this check fails then nothing is done.
298 * @pre The Actor has been initialized.
300 void ClearKeyInputFocus();
305 * @brief Retrieves the pinch gesture detector of the control.
308 * @return The pinch gesture detector
309 * @note Will return an empty handle if the control does not handle the gesture itself.
311 PinchGestureDetector GetPinchGestureDetector() const;
314 * @brief Retrieves the pan gesture detector of the control.
317 * @return The pan gesture detector
318 * @note Will return an empty handle if the control does not handle the gesture itself.
320 PanGestureDetector GetPanGestureDetector() const;
323 * @brief Retrieves the tap gesture detector of the control.
326 * @return The tap gesture detector
327 * @note Will return an empty handle if the control does not handle the gesture itself.
329 TapGestureDetector GetTapGestureDetector() const;
332 * @brief Retrieves the long press gesture detector of the control.
335 * @return The long press gesture detector
336 * @note Will return an empty handle if the control does not handle the gesture itself.
338 LongPressGestureDetector GetLongPressGestureDetector() const;
343 * @brief Sets the name of the style to be applied to the control.
346 * @param[in] styleName A string matching a style described in a stylesheet
348 void SetStyleName( const std::string& styleName );
351 * @brief Retrieves the name of the style to be applied to the control (if any).
353 * @return A string matching a style, or an empty string
355 const std::string& GetStyleName() const;
360 * @brief Sets the background color of the control.
363 * @param[in] color The required background color of the control
365 * @note If SetBackgroundImage is called later, this background color is removed.
367 * @note The background color fully blends with the actor color.
369 void SetBackgroundColor( const Vector4& color );
374 * @brief Retrieves the background color of the control.
377 * @return The background color of the control
379 Vector4 GetBackgroundColor() const DALI_DEPRECATED_API;
382 * @DEPRECATED_1_2_8, use Property::BACKGROUND instead
384 * @brief Sets an image as the background of the control.
387 * @param[in] image The image to set as the background
389 void SetBackgroundImage( Image image ) DALI_DEPRECATED_API;
392 * @brief Clears the background.
395 void ClearBackground();
400 * @brief Query if all resources required by a control are loaded and ready.
402 * Most resources are only loaded when the control is placed on stage.
404 * @return true if the resources are loaded and ready, false otherwise
406 bool IsResourceReady() const;
409 * @brief Get the loading state of the visual resource.
412 * @param[in] index The Property index of the visual
413 * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
415 Visual::ResourceStatus GetVisualResourceStatus( const Dali::Property::Index index );
420 * @brief This signal is emitted when key event is received.
422 * A callback of the following type may be connected:
424 * bool YourCallbackName(Control control, const KeyEvent& event);
426 * The return value of True, indicates that the event should be consumed.
427 * Otherwise the signal will be emitted on the next parent of the actor.
429 * @return The signal to connect to
430 * @pre The Control has been initialized.
432 KeyEventSignalType& KeyEventSignal();
435 * @brief This signal is emitted when the control gets Key Input Focus.
437 * A callback of the following type may be connected:
439 * bool YourCallbackName( Control control );
441 * The return value of True, indicates that the event should be consumed.
442 * Otherwise the signal will be emitted on the next parent of the actor.
444 * @return The signal to connect to
445 * @pre The Control has been initialized.
447 KeyInputFocusSignalType& KeyInputFocusGainedSignal();
450 * @brief This signal is emitted when the control loses Key Input Focus.
452 * This could be due to it being gained by another Control or Actor or just cleared from
453 * this control as no longer required.
455 * A callback of the following type may be connected:
457 * bool YourCallbackName( Control control );
459 * The return value of True, indicates that the event should be consumed.
460 * Otherwise the signal will be emitted on the next parent of the actor.
462 * @return The signal to connect to
463 * @pre The Control has been initialized.
465 KeyInputFocusSignalType& KeyInputFocusLostSignal();
468 * @brief This signal is emitted after all resources required by a control are loaded and ready.
470 * Most resources are only loaded when the control is placed on stage.
472 * If resources are shared between ImageViews, they are cached.
473 * In this case, the ResourceReady signal may be sent before there is an object to connect to.
474 * To protect against this, IsResourceReady() can be checked first.
477 * auto newControl = Control::New();
478 * newControl.SetResource( resourceUrl );
479 * if ( newControl.IsResourceReady() )
485 * newControl.ResourceReadySignal.Connect( .... )
489 * A callback of the following type may be connected:
491 * void YourCallbackName( Control control );
495 * @return The signal to connect to
496 * @note A RelayoutRequest is queued by Control before this signal is emitted
498 ResourceReadySignalType& ResourceReadySignal();
500 Toolkit::Control::AccessibilityActivateSignalType &AccessibilityActivateSignal();
502 public: // Intended for control developers
505 * @brief Creates an initialized Control.
508 * @param[in] implementation The implementation for this control
509 * @return A handle to a newly allocated Dali resource
510 * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
512 explicit Control(Internal::Control& implementation);
515 * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
516 * using an Internal CustomActor pointer.
519 * @param[in] internal A pointer to a newly allocated Dali resource
521 explicit Control(Dali::Internal::CustomActor* internal);
523 public: // Templates for Deriving Classes
526 * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
528 * @tparam T The handle class
529 * @tparam I The implementation class
531 * @param[in] handle Handle to an object
532 * @return Handle to a class T or an uninitialized handle
533 * @see DownCast(BaseHandle)
535 template<typename T, typename I>
536 DALI_INTERNAL static T DownCast( BaseHandle handle )
540 CustomActor custom = Dali::CustomActor::DownCast( handle );
543 CustomActorImpl& customImpl = custom.GetImplementation();
545 I* impl = dynamic_cast<I*>(&customImpl);
549 result = T(customImpl.GetOwner());
557 * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
558 * implementation of their class.
560 * @tparam I The implementation class
562 * @param[in] internal Pointer to the Internal::CustomActor
565 DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
567 // Can have a NULL pointer so we only need to check if the internal implementation is our class
568 // when there is a value.
571 DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
575 void AccessibilitySetAttribute( const std::string& key,
576 const std::string value );
577 std::string AccessibilityGetAttribute( const std::string& key );
578 void AccessibilityEraseAttribute( std::string& key );
584 } // namespace Toolkit
588 #endif // __DALI_TOOLKIT_CONTROL_H__