1 #ifndef __DALI_TOOLKIT_CONTROL_H__
2 #define __DALI_TOOLKIT_CONTROL_H__
5 * Copyright (c) 2015 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.
52 * @see Internal::Control
55 * | %Signal Name | Method |
56 * |------------------------|-----------------------------------------------------|
57 * | keyEvent | @ref KeyEventSignal() |
58 * | keyInputFocusGained | @ref KeyInputFocusGainedSignal() |
59 * | keyInputFocusLost | @ref KeyInputFocusLostSignal() |
60 * | tapped | @ref GetTapGestureDetector().DetectedSignal() |
61 * | panned | @ref GetPanGestureDetector().DetectedSignal() |
62 * | pinched | @ref GetPinchGestureDetector().DetectedSignal() |
63 * | longPressed | @ref GetLongPressGestureDetector().DetectedSignal() |
66 * | %Action Name | %Control method called |
67 * |------------------------|----------------------------------------------------|
68 * | accessibilityActivated | %OnAccessibilityActivated() |
70 class DALI_IMPORT_API Control : public CustomActor
75 * @brief The start and end property ranges for control.
79 PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX, ///< Start index is used by the property registration macro.
80 CONTROL_PROPERTY_START_INDEX = PROPERTY_START_INDEX, ///< Start index of Control properties.
81 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices.
85 * @brief An enumeration of properties belonging to the Control class.
91 STYLE_NAME = PROPERTY_START_INDEX, ///< name "styleName", @see SetStyleName, type std::string
92 BACKGROUND_COLOR, ///< name "background-color", @deprecated DALi 1.1.3 mutually exclusive with BACKGROUND_IMAGE & BACKGROUND, type Vector4
93 BACKGROUND_IMAGE, ///< name "background-image", @deprecated DALi 1.1.3 mutually exclusive with BACKGROUND_COLOR & BACKGROUND, type Map
94 KEY_INPUT_FOCUS, ///< name "keyInputFocus", @see SetKeyInputFocus, type bool
95 BACKGROUND, ///< name "background", @since DALi 1.1.3 mutually exclusive with BACKGROUND_COLOR & BACKGROUND_IMAGE, type Map
100 * @brief Describes the direction to move the keyboard focus towards.
106 LEFT, ///< Move keyboard focus towards the left direction
107 RIGHT, ///< Move keyboard focus towards the right direction
108 UP, ///< Move keyboard focus towards the up direction
109 DOWN ///< Move keyboard focus towards the down direction
115 /// @brief Key Event signal type;
116 typedef Signal<bool ( Control, const KeyEvent& ) > KeyEventSignalType;
118 /// @brief Key InputFocusType signal type;
119 typedef Signal<void ( Control ) > KeyInputFocusSignalType;
121 public: // Creation & Destruction
124 * @brief Create a new instance of a Control.
126 * @return A handle to a new Control.
128 static Control New();
131 * @brief Create an uninitialized Control handle.
133 * Only derived versions can be instantiated. Calling member
134 * functions with an uninitialized Dali::Object is not allowed.
139 * @brief Copy constructor.
141 * Creates another handle that points to the same real object
142 * @param[in] uiControl Handle to copy
144 Control(const Control& uiControl);
147 * @brief Dali::Control is intended as a base class
149 * This is non-virtual since derived Handle types must not contain data or virtual methods.
156 * @brief Assignment operator.
158 * Changes this handle to point to another real object
159 * @param[in] handle Object to assign this to
160 * @return reference to this
162 Control& operator=( const Control& handle );
167 * @brief Downcast an Object handle to Control.
169 * If handle points to a Control the downcast produces valid
170 * handle. If not the returned handle is left uninitialized.
172 * @param[in] handle Handle to an object
173 * @return handle to a Control or an uninitialized handle
175 static Control DownCast( BaseHandle handle );
180 * @brief This sets the control to receive key events.
182 * The key event can originate from a virtual or physical keyboard.
183 * @pre The Control has been initialized.
184 * @pre The Control should be on the stage before setting keyboard focus.
185 * @return True if the control has foucs, False otherwise.
187 void SetKeyInputFocus();
190 * @brief Quries whether the control has key input focus.
192 * 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.
193 * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
194 * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
195 * controls above it in the focus stack.
196 * To query for the conrol which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor()
197 * @pre The Control has been initialized.
198 * @pre The Control should be on the stage before setting keyboard focus.
199 * @return true if this control has keyboard input focus
201 bool HasKeyInputFocus();
204 * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
206 * A check is performed to ensure it was previously set, if this check fails then nothing is done.
207 * @pre The Actor has been initialized.
209 void ClearKeyInputFocus();
214 * @brief Retrieves the pinch gesture detector of the control.
216 * @return The pinch gesture detector.
217 * @note Will return an empty handle if the control does not handle the gesture itself.
219 PinchGestureDetector GetPinchGestureDetector() const;
222 * @brief Retrieves the pan gesture detector of the control.
224 * @return The pan gesture detector.
225 * @note Will return an empty handle if the control does not handle the gesture itself.
227 PanGestureDetector GetPanGestureDetector() const;
230 * @brief Retrieves the tap gesture detector of the control.
232 * @return The tap gesture detector.
233 * @note Will return an empty handle if the control does not handle the gesture itself.
235 TapGestureDetector GetTapGestureDetector() const;
238 * @brief Retrieves the long press gesture detector of the control.
240 * @return The long press gesture detector.
241 * @note Will return an empty handle if the control does not handle the gesture itself.
243 LongPressGestureDetector GetLongPressGestureDetector() const;
248 * @brief Sets the name of the style to be applied to the control.
250 * @param[in] styleName A string matching a style described in a stylesheet.
252 void SetStyleName( const std::string& styleName );
255 * @brief Retrieves the name of the style to be applied to the control (if any).
257 * @return A string matching a style or an empty string.
259 const std::string& GetStyleName() const;
264 * @brief Sets the background color of the control.
266 * @note if SetBackgroundImage is called later, this background color is removed.
268 * @param[in] color The required background color of the control
270 * @note The background color fully blends with the actor color.
272 void SetBackgroundColor( const Vector4& color );
275 * @brief Retrieves the background color of the control.
277 * @deprecated DALi 1.1.3 API removed.
279 * @return The background color of the control.
281 Vector4 GetBackgroundColor() const;
284 * @brief Sets an image as the background of the control.
286 * @param[in] image The image to set as the background.
288 void SetBackgroundImage( Image image );
291 * @brief Clears the background.
293 void ClearBackground();
298 * @brief This signal is emitted when key event is received.
300 * A callback of the following type may be connected:
302 * bool YourCallbackName(Control control, const KeyEvent& event);
304 * The return value of True, indicates that the event should be consumed.
305 * Otherwise the signal will be emitted on the next parent of the actor.
306 * @pre The Control has been initialized.
307 * @return The signal to connect to.
309 KeyEventSignalType& KeyEventSignal();
312 * @brief This signal is emitted when the control gets Key Input Focus
314 * A callback of the following type may be connected:
316 * bool YourCallbackName( Control control );
318 * The return value of True, indicates that the event should be consumed.
319 * Otherwise the signal will be emitted on the next parent of the actor.
320 * @pre The Control has been initialized.
321 * @return The signal to connect to.
323 KeyInputFocusSignalType& KeyInputFocusGainedSignal();
326 * @brief This signal is emitted when the control loses Key Input Focus
327 * which could be due to it being gained by another Control or Actor or just cleared from
328 * this control as no longer required.
330 * A callback of the following type may be connected:
332 * bool YourCallbackName( Control control );
334 * The return value of True, indicates that the event should be consumed.
335 * Otherwise the signal will be emitted on the next parent of the actor.
336 * @pre The Control has been initialized.
337 * @return The signal to connect to.
339 KeyInputFocusSignalType& KeyInputFocusLostSignal();
341 public: // Intended for control developers
344 * @brief Create an initialised Control.
346 * @param[in] implementation The implementation for this control.
347 * @return A handle to a newly allocated Dali resource.
349 * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
351 explicit Control(Internal::Control& implementation);
354 * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
355 * using an Internal CustomActor pointer.
357 * @param [in] internal A pointer to a newly allocated Dali resource
359 explicit Control(Dali::Internal::CustomActor* internal);
361 public: // Templates for Deriving Classes
364 * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
366 * @tparam T The handle class
367 * @tparam I The implementation class
368 * @param[in] handle Handle to an object
369 * @return Handle to a class T or an uninitialized handle.
370 * @see DownCast(BaseHandle)
372 template<typename T, typename I>
373 DALI_INTERNAL static T DownCast( BaseHandle handle )
377 CustomActor custom = Dali::CustomActor::DownCast( handle );
380 CustomActorImpl& customImpl = custom.GetImplementation();
382 I* impl = dynamic_cast<I*>(&customImpl);
386 result = T(customImpl.GetOwner());
394 * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
395 * implementation of their class.
397 * @tparam I The implementation class
398 * @param[in] internal Pointer to the Internal::CustomActor
401 DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
403 // Can have a NULL pointer so we only need to check if the internal implementation is our class
404 // when there is a value.
407 DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
416 } // namespace Toolkit
420 #endif // __DALI_TOOLKIT_CONTROL_H__