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.
45 * @brief Control is the base class for all controls.
47 * The implementation of the control must be supplied; see Internal::Control for more details.
48 * @see Internal::Control
51 * | %Signal Name | Method |
52 * |------------------------|-----------------------------------------------------|
53 * | key-event | @ref KeyEventSignal() |
54 * | key-input-focus-gained | @ref KeyInputFocusGainedSignal() |
55 * | key-input-focus-lost | @ref KeyInputFocusLostSignal() |
56 * | tapped | @ref GetTapGestureDetector().DetectedSignal() |
57 * | panned | @ref GetPanGestureDetector().DetectedSignal() |
58 * | pinched | @ref GetPinchGestureDetector().DetectedSignal() |
59 * | long-pressed | @ref GetLongPressGestureDetector().DetectedSignal() |
62 * | %Action Name | %Control method called |
63 * |-------------------|-----------------------------------------------------|
64 * | control-activated | %OnActivated() |
66 class DALI_IMPORT_API Control : public CustomActor
71 * @brief The start and end property ranges for control.
75 PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX, ///< Start index is used by the property registration macro.
76 CONTROL_PROPERTY_START_INDEX = PROPERTY_START_INDEX, ///< Start index of Control properties.
77 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices.
81 * @brief An enumeration of properties belonging to the Control class.
87 STYLE_NAME = PROPERTY_START_INDEX, ///< name "style-name", @see SetStyleName, type std::string
88 BACKGROUND_COLOR, ///< name "background-color", @see SetBackgroundColor, type Vector4
89 BACKGROUND_IMAGE, ///< name "background-image", @see SetBackgroundImage, type Map
90 KEY_INPUT_FOCUS, ///< name "key-input-focus", @see SetKeyInputFocus, type bool
95 * @brief Describes the direction to move the keyboard focus towards.
97 enum KeyboardFocusNavigationDirection
99 Left, ///< Move keyboard focus towards the left direction
100 Right, ///< Move keyboard focus towards the right direction
101 Up, ///< Move keyboard focus towards the up direction
102 Down ///< Move keyboard focus towards the down direction
107 /// @brief Key Event signal type;
108 typedef Signal<bool ( Control, const KeyEvent& ) > KeyEventSignalType;
110 /// @brief Key InputFocusType signal type;
111 typedef Signal<void ( Control ) > KeyInputFocusSignalType;
113 public: // Creation & Destruction
116 * @brief Create a new instance of a Control.
118 * @return A handle to a new Control.
120 static Control New();
123 * @brief Create an uninitialized Control handle.
125 * Only derived versions can be instantiated. Calling member
126 * functions with an uninitialized Dali::Object is not allowed.
131 * @brief Copy constructor.
133 * Creates another handle that points to the same real object
134 * @param[in] uiControl Handle to copy
136 Control(const Control& uiControl);
139 * @brief Dali::Control is intended as a base class
141 * This is non-virtual since derived Handle types must not contain data or virtual methods.
148 * @brief Assignment operator.
150 * Changes this handle to point to another real object
151 * @param[in] handle Object to assign this to
152 * @return reference to this
154 Control& operator=( const Control& handle );
159 * @brief Downcast an Object handle to Control.
161 * If handle points to a Control the downcast produces valid
162 * handle. If not the returned handle is left uninitialized.
164 * @param[in] handle Handle to an object
165 * @return handle to a Control or an uninitialized handle
167 static Control DownCast( BaseHandle handle );
172 * @brief This sets the control to receive key events.
174 * The key event can originate from a virtual or physical keyboard.
175 * @pre The Control has been initialized.
176 * @pre The Control should be on the stage before setting keyboard focus.
177 * @return True if the control has foucs, False otherwise.
179 void SetKeyInputFocus();
182 * @brief Quries whether the control has key input focus.
184 * 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.
185 * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
186 * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
187 * controls above it in the focus stack.
188 * To query for the conrol which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor()
189 * @pre The Control has been initialized.
190 * @pre The Control should be on the stage before setting keyboard focus.
191 * @return true if this control has keyboard input focus
193 bool HasKeyInputFocus();
196 * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
198 * A check is performed to ensure it was previously set, if this check fails then nothing is done.
199 * @pre The Actor has been initialized.
201 void ClearKeyInputFocus();
206 * @brief Retrieves the pinch gesture detector of the control.
208 * @return The pinch gesture detector.
209 * @note Will return an empty handle if the control does not handle the gesture itself.
211 PinchGestureDetector GetPinchGestureDetector() const;
214 * @brief Retrieves the pan gesture detector of the control.
216 * @return The pan gesture detector.
217 * @note Will return an empty handle if the control does not handle the gesture itself.
219 PanGestureDetector GetPanGestureDetector() const;
222 * @brief Retrieves the tap gesture detector of the control.
224 * @return The tap gesture detector.
225 * @note Will return an empty handle if the control does not handle the gesture itself.
227 TapGestureDetector GetTapGestureDetector() const;
230 * @brief Retrieves the long press gesture detector of the control.
232 * @return The long press gesture detector.
233 * @note Will return an empty handle if the control does not handle the gesture itself.
235 LongPressGestureDetector GetLongPressGestureDetector() const;
240 * @brief Sets the name of the style to be applied to the control.
242 * @param[in] styleName A string matching a style described in a stylesheet.
244 void SetStyleName( const std::string& styleName );
247 * @brief Retrieves the name of the style to be applied to the control (if any).
249 * @return A string matching a style or an empty string.
251 const std::string& GetStyleName() const;
256 * @brief Sets the background color of the control.
258 * @param[in] color The required background color of the control
260 * @note The background color fully blends with the actor color.
262 void SetBackgroundColor( const Vector4& color );
265 * @brief Retrieves the background color of the control.
267 * @return The background color of the control.
269 Vector4 GetBackgroundColor() const;
272 * @brief Sets an image as the background of the control.
274 * The color of this image is blended with the background color @see SetBackgroundColor
276 * @param[in] image The image to set as the background.
278 void SetBackgroundImage( Image image );
281 * @brief Clears the background.
283 void ClearBackground();
288 * @brief This signal is emitted when key event is received.
290 * A callback of the following type may be connected:
292 * bool YourCallbackName(Control control, const KeyEvent& event);
294 * The return value of True, indicates that the event should be consumed.
295 * Otherwise the signal will be emitted on the next parent of the actor.
296 * @pre The Control has been initialized.
297 * @return The signal to connect to.
299 KeyEventSignalType& KeyEventSignal();
302 * @brief This signal is emitted when the control gets Key Input Focus
304 * A callback of the following type may be connected:
306 * bool YourCallbackName( Control control );
308 * The return value of True, indicates that the event should be consumed.
309 * Otherwise the signal will be emitted on the next parent of the actor.
310 * @pre The Control has been initialized.
311 * @return The signal to connect to.
313 KeyInputFocusSignalType& KeyInputFocusGainedSignal();
316 * @brief This signal is emitted when the control loses Key Input Focus
317 * which could be due to it being gained by another Control or Actor or just cleared from
318 * this control as no longer required.
320 * A callback of the following type may be connected:
322 * bool YourCallbackName( Control control );
324 * The return value of True, indicates that the event should be consumed.
325 * Otherwise the signal will be emitted on the next parent of the actor.
326 * @pre The Control has been initialized.
327 * @return The signal to connect to.
329 KeyInputFocusSignalType& KeyInputFocusLostSignal();
331 public: // Intended for control developers
334 * @brief Create an initialised Control.
336 * @param[in] implementation The implementation for this control.
337 * @return A handle to a newly allocated Dali resource.
339 * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
341 explicit Control(Internal::Control& implementation);
344 * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
345 * using an Internal CustomActor pointer.
347 * @param [in] internal A pointer to a newly allocated Dali resource
349 explicit Control(Dali::Internal::CustomActor* internal);
351 public: // Templates for Deriving Classes
354 * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
356 * @tparam T The handle class
357 * @tparam I The implementation class
358 * @param[in] handle Handle to an object
359 * @return Handle to a class T or an uninitialized handle.
360 * @see DownCast(BaseHandle)
362 template<typename T, typename I>
363 DALI_INTERNAL static T DownCast( BaseHandle handle )
367 CustomActor custom = Dali::CustomActor::DownCast( handle );
370 CustomActorImpl& customImpl = custom.GetImplementation();
372 I* impl = dynamic_cast<I*>(&customImpl);
376 result = T(customImpl.GetOwner());
384 * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
385 * implementation of their class.
387 * @tparam I The implementation class
388 * @param[in] internal Pointer to the Internal::CustomActor
391 DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
393 // Can have a NULL pointer so we only need to check if the internal implementation is our class
394 // when there is a value.
397 DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
403 } // namespace Toolkit
407 #endif // __DALI_TOOLKIT_CONTROL_H__