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>
37 //Forward declarations.
44 * @addtogroup dali_toolkit_controls
49 * @brief Control is the base class for all controls.
51 * The implementation of the control must be supplied; see Internal::Control for more details.
53 * @see Internal::Control
56 * | %Signal Name | Method |
57 * |------------------------|-----------------------------------------------------|
58 * | keyEvent | @ref KeyEventSignal() |
59 * | keyInputFocusGained | @ref KeyInputFocusGainedSignal() |
60 * | keyInputFocusLost | @ref KeyInputFocusLostSignal() |
61 * | resourceReady | @ref ResourceReadySignal() |
62 * | tapped | @ref GetTapGestureDetector().DetectedSignal() |
63 * | panned | @ref GetPanGestureDetector().DetectedSignal() |
64 * | pinched | @ref GetPinchGestureDetector().DetectedSignal() |
65 * | longPressed | @ref GetLongPressGestureDetector().DetectedSignal() |
68 * | %Action Name | %Control method called |
69 * |------------------------|----------------------------------------------------|
70 * | accessibilityActivated | %OnAccessibilityActivated() |
72 class DALI_IMPORT_API Control : public CustomActor
77 * @brief Enumeration for the start and end property ranges for control.
82 PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX, ///< Start index is used by the property registration macro. @SINCE_1_0.0
83 CONTROL_PROPERTY_START_INDEX = PROPERTY_START_INDEX, ///< Start index of Control properties. @SINCE_1_0.0
84 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices. @SINCE_1_0.0
88 * @brief Enumeration for the instance of properties belonging to the Control class.
94 * @brief Enumeration for the instance of properties belonging to the Control class.
100 * @brief name "styleName", type std::string.
104 STYLE_NAME = PROPERTY_START_INDEX,
107 * @brief name "backgroundColor", mutually exclusive with BACKGROUND_IMAGE & BACKGROUND, type Vector4.
114 * @brief name "backgroundImage", mutually exclusive with BACKGROUND_COLOR & BACKGROUND, type Map.
119 * @brief name "keyInputFocus", type bool.
121 * @see SetKeyInputFocus
125 * @brief name "background", mutually exclusive with BACKGROUND_COLOR & BACKGROUND_IMAGE, type Map or std::string for URL or Vector4 for Color.
133 * @brief Describes the direction to move the keyboard focus towards.
139 * @brief Keyboard focus direction.
144 LEFT, ///< Move keyboard focus towards the left direction @SINCE_1_0.0
145 RIGHT, ///< Move keyboard focus towards the right direction @SINCE_1_0.0
146 UP, ///< Move keyboard focus towards the up direction @SINCE_1_0.0
147 DOWN, ///< Move keyboard focus towards the down direction @SINCE_1_0.0
148 PAGE_UP, ///< Move keyboard focus towards the previous page direction @SINCE_1_2.14
149 PAGE_DOWN ///< Move keyboard focus towards the next page direction @SINCE_1_2.14
155 /// @brief Key Event signal type. @SINCE_1_0.0
156 typedef Signal<bool ( Control, const KeyEvent& ) > KeyEventSignalType;
158 /// @brief Key InputFocusType signal type. @SINCE_1_0.0
159 typedef Signal<void ( Control ) > KeyInputFocusSignalType;
161 /// @brief ResourceReady signal type. @SINCE_1_2.60
162 typedef Signal<void ( Control ) > ResourceReadySignalType;
164 public: // Creation & Destruction
167 * @brief Creates a new instance of a Control.
170 * @return A handle to a new Control
172 static Control New();
175 * @brief Creates an uninitialized Control handle.
177 * Only derived versions can be instantiated. Calling member
178 * functions with an uninitialized Dali::Object is not allowed.
184 * @brief Copy constructor.
186 * Creates another handle that points to the same real object.
188 * @param[in] uiControl Handle to copy
190 Control(const Control& uiControl);
193 * @brief Dali::Control is intended as a base class.
195 * This is non-virtual since derived Handle types must not contain data or virtual methods.
203 * @brief Assignment operator.
205 * Changes this handle to point to another real object.
207 * @param[in] handle Object to assign this to
208 * @return Reference to this
210 Control& operator=( const Control& handle );
215 * @brief Downcasts a handle to Control handle.
217 * If handle points to a Control, the downcast produces valid handle.
218 * If not, the returned handle is left uninitialized.
221 * @param[in] handle Handle to an object
222 * @return A handle to a Control or an uninitialized handle
224 static Control DownCast( BaseHandle handle );
229 * @brief This sets the control to receive key events.
231 * The key event can originate from a virtual or physical keyboard.
233 * @pre The Control has been initialized.
234 * @pre The Control should be on the stage before setting keyboard focus.
236 void SetKeyInputFocus();
239 * @brief Quries whether the control has key input focus.
242 * @return true if this control has keyboard input focus
243 * @pre The Control has been initialized.
244 * @pre The Control should be on the stage before setting keyboard focus.
245 * @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.
246 * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
247 * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
248 * controls above it in the focus stack.
249 * To query for the control which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor().
251 bool HasKeyInputFocus();
254 * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
256 * A check is performed to ensure it was previously set, if this check fails then nothing is done.
258 * @pre The Actor has been initialized.
260 void ClearKeyInputFocus();
265 * @brief Retrieves the pinch gesture detector of the control.
268 * @return The pinch gesture detector
269 * @note Will return an empty handle if the control does not handle the gesture itself.
271 PinchGestureDetector GetPinchGestureDetector() const;
274 * @brief Retrieves the pan gesture detector of the control.
277 * @return The pan gesture detector
278 * @note Will return an empty handle if the control does not handle the gesture itself.
280 PanGestureDetector GetPanGestureDetector() const;
283 * @brief Retrieves the tap gesture detector of the control.
286 * @return The tap gesture detector
287 * @note Will return an empty handle if the control does not handle the gesture itself.
289 TapGestureDetector GetTapGestureDetector() const;
292 * @brief Retrieves the long press gesture detector of the control.
295 * @return The long press gesture detector
296 * @note Will return an empty handle if the control does not handle the gesture itself.
298 LongPressGestureDetector GetLongPressGestureDetector() const;
303 * @brief Sets the name of the style to be applied to the control.
306 * @param[in] styleName A string matching a style described in a stylesheet
308 void SetStyleName( const std::string& styleName );
311 * @brief Retrieves the name of the style to be applied to the control (if any).
313 * @return A string matching a style, or an empty string
315 const std::string& GetStyleName() const;
320 * @brief Sets the background color of the control.
323 * @param[in] color The required background color of the control
325 * @note If SetBackgroundImage is called later, this background color is removed.
327 * @note The background color fully blends with the actor color.
329 void SetBackgroundColor( const Vector4& color );
334 * @brief Retrieves the background color of the control.
337 * @return The background color of the control
339 Vector4 GetBackgroundColor() const DALI_DEPRECATED_API;
342 * @DEPRECATED_1_2_8, use Property::BACKGROUND instead
344 * @brief Sets an image as the background of the control.
347 * @param[in] image The image to set as the background
349 void SetBackgroundImage( Image image ) DALI_DEPRECATED_API;
352 * @brief Clears the background.
355 void ClearBackground();
360 * @brief Query if all resources required by a control are loaded and ready.
362 * Most resources are only loaded when the control is placed on stage.
364 * @return true if the resources are loaded and ready, false otherwise
366 bool IsResourceReady() const;
371 * @brief This signal is emitted when key event is received.
373 * A callback of the following type may be connected:
375 * bool YourCallbackName(Control control, const KeyEvent& event);
377 * The return value of True, indicates that the event should be consumed.
378 * Otherwise the signal will be emitted on the next parent of the actor.
380 * @return The signal to connect to
381 * @pre The Control has been initialized.
383 KeyEventSignalType& KeyEventSignal();
386 * @brief This signal is emitted when the control gets Key Input Focus.
388 * A callback of the following type may be connected:
390 * bool YourCallbackName( Control control );
392 * The return value of True, indicates that the event should be consumed.
393 * Otherwise the signal will be emitted on the next parent of the actor.
395 * @return The signal to connect to
396 * @pre The Control has been initialized.
398 KeyInputFocusSignalType& KeyInputFocusGainedSignal();
401 * @brief This signal is emitted when the control loses Key Input Focus.
403 * This could be due to it being gained by another Control or Actor or just cleared from
404 * this control as no longer required.
406 * A callback of the following type may be connected:
408 * bool YourCallbackName( Control control );
410 * The return value of True, indicates that the event should be consumed.
411 * Otherwise the signal will be emitted on the next parent of the actor.
413 * @return The signal to connect to
414 * @pre The Control has been initialized.
416 KeyInputFocusSignalType& KeyInputFocusLostSignal();
419 * @brief This signal is emitted after all resources required by a control are loaded and ready.
421 * Most resources are only loaded when the control is placed on stage.
423 * A callback of the following type may be connected:
425 * void YourCallbackName( Control control );
429 * @note A RelayoutRequest is queued by Control before this signal is emitted
431 ResourceReadySignalType& ResourceReadySignal();
433 public: // Intended for control developers
436 * @brief Creates an initialized Control.
439 * @param[in] implementation The implementation for this control
440 * @return A handle to a newly allocated Dali resource
441 * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
443 explicit Control(Internal::Control& implementation);
446 * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
447 * using an Internal CustomActor pointer.
450 * @param[in] internal A pointer to a newly allocated Dali resource
452 explicit Control(Dali::Internal::CustomActor* internal);
454 public: // Templates for Deriving Classes
457 * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
459 * @tparam T The handle class
460 * @tparam I The implementation class
462 * @param[in] handle Handle to an object
463 * @return Handle to a class T or an uninitialized handle
464 * @see DownCast(BaseHandle)
466 template<typename T, typename I>
467 DALI_INTERNAL static T DownCast( BaseHandle handle )
471 CustomActor custom = Dali::CustomActor::DownCast( handle );
474 CustomActorImpl& customImpl = custom.GetImplementation();
476 I* impl = dynamic_cast<I*>(&customImpl);
480 result = T(customImpl.GetOwner());
488 * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
489 * implementation of their class.
491 * @tparam I The implementation class
493 * @param[in] internal Pointer to the Internal::CustomActor
496 DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
498 // Can have a NULL pointer so we only need to check if the internal implementation is our class
499 // when there is a value.
502 DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
511 } // namespace Toolkit
515 #endif // __DALI_TOOLKIT_CONTROL_H__