1 #ifndef __DALI_TOOLKIT_CONTROL_H__
2 #define __DALI_TOOLKIT_CONTROL_H__
5 * Copyright (c) 2014 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 * | tapped | @ref GetTapGestureDetector().DetectedSignal() |
55 * | panned | @ref GetPanGestureDetector().DetectedSignal() |
56 * | pinched | @ref GetPinchGestureDetector().DetectedSignal() |
57 * | long-pressed | @ref GetLongPressGestureDetector().DetectedSignal() |
60 * | %Action Name | %Control method called |
61 * |-------------------|-----------------------------------------------------|
62 * | control-activated | %OnActivated() |
64 class DALI_IMPORT_API Control : public CustomActor
69 * @brief The start and end property ranges for control.
73 PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX, ///< Start index is used by the property registration macro.
74 CONTROL_PROPERTY_START_INDEX = PROPERTY_START_INDEX, ///< Start index of Control properties.
75 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices.
79 * @brief An enumeration of properties belonging to the Control class.
85 STYLE_NAME = PROPERTY_START_INDEX, ///< name "style-name", @see SetStyleName, type std::string
86 BACKGROUND_COLOR, ///< name "background-color", @see SetBackgroundColor, type Vector4
87 BACKGROUND_IMAGE, ///< name "background-image", @see SetBackgroundImage, type Map
88 KEY_INPUT_FOCUS, ///< name "key-input-focus", @see SetKeyInputFocus, type bool
93 * @brief Describes the direction to move the keyboard focus towards.
95 enum KeyboardFocusNavigationDirection
97 Left, ///< Move keyboard focus towards the left direction
98 Right, ///< Move keyboard focus towards the right direction
99 Up, ///< Move keyboard focus towards the up direction
100 Down ///< Move keyboard focus towards the down direction
105 /// @brief Key Event signal type;
106 typedef Signal<bool ( Control, const KeyEvent& ) > KeyEventSignalType;
108 public: // Creation & Destruction
111 * @brief Create a new instance of a Control.
113 * @return A handle to a new Control.
115 static Control New();
118 * @brief Create an uninitialized Control handle.
120 * Only derived versions can be instantiated. Calling member
121 * functions with an uninitialized Dali::Object is not allowed.
126 * @brief Copy constructor.
128 * Creates another handle that points to the same real object
129 * @param[in] uiControl Handle to copy
131 Control(const Control& uiControl);
134 * @brief Dali::Control is intended as a base class
136 * This is non-virtual since derived Handle types must not contain data or virtual methods.
143 * @brief Assignment operator.
145 * Changes this handle to point to another real object
146 * @param[in] handle Object to assign this to
147 * @return reference to this
149 Control& operator=( const Control& handle );
154 * @brief Downcast an Object handle to Control.
156 * If handle points to a Control the downcast produces valid
157 * handle. If not the returned handle is left uninitialized.
159 * @param[in] handle Handle to an object
160 * @return handle to a Control or an uninitialized handle
162 static Control DownCast( BaseHandle handle );
165 * @brief Retrieve the Control implementation.
167 * @return The implementation.
169 Internal::Control& GetImplementation();
172 * @brief Retrieve the Control implementation.
174 * @return The implementation.
176 const Internal::Control& GetImplementation() const;
181 * @brief This sets the control to receive key events.
183 * The key event can originate from a virtual or physical keyboard.
184 * @pre The Control has been initialized.
185 * @pre The Control should be on the stage before setting keyboard focus.
186 * @return True if the control has foucs, False otherwise.
188 void SetKeyInputFocus();
191 * @brief Quries whether the control has key input focus.
193 * 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.
194 * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
195 * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
196 * controls above it in the focus stack.
197 * To query for the conrol which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor()
198 * @pre The Control has been initialized.
199 * @pre The Control should be on the stage before setting keyboard focus.
200 * @return true if this control has keyboard input focus
202 bool HasKeyInputFocus();
205 * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
207 * A check is performed to ensure it was previously set, if this check fails then nothing is done.
208 * @pre The Actor has been initialized.
210 void ClearKeyInputFocus();
215 * @brief Retrieves the pinch gesture detector of the control.
217 * @return The pinch gesture detector.
218 * @note Will return an empty handle if the control does not handle the gesture itself.
220 PinchGestureDetector GetPinchGestureDetector() const;
223 * @brief Retrieves the pan gesture detector of the control.
225 * @return The pan gesture detector.
226 * @note Will return an empty handle if the control does not handle the gesture itself.
228 PanGestureDetector GetPanGestureDetector() const;
231 * @brief Retrieves the tap gesture detector of the control.
233 * @return The tap gesture detector.
234 * @note Will return an empty handle if the control does not handle the gesture itself.
236 TapGestureDetector GetTapGestureDetector() const;
239 * @brief Retrieves the long press gesture detector of the control.
241 * @return The long press gesture detector.
242 * @note Will return an empty handle if the control does not handle the gesture itself.
244 LongPressGestureDetector GetLongPressGestureDetector() const;
249 * @brief Sets the name of the style to be applied to the control.
251 * @param[in] styleName A string matching a style described in a stylesheet.
253 void SetStyleName( const std::string& styleName );
256 * @brief Retrieves the name of the style to be applied to the control (if any).
258 * @return A string matching a style or an empty string.
260 const std::string& GetStyleName() const;
265 * @brief Sets the background color of the control.
267 * @param[in] color The required background color of the control
269 * @note The background color fully blends with the actor color.
271 void SetBackgroundColor( const Vector4& color );
274 * @brief Retrieves the background color of the control.
276 * @return The background color of the control.
278 Vector4 GetBackgroundColor() const;
281 * @brief Sets an image as the background of the control.
283 * The color of this image is blended with the background color @see SetBackgroundColor
285 * @param[in] image The image to set as the background.
287 void SetBackgroundImage( Image image );
290 * @brief Clears the background.
292 void ClearBackground();
295 * @brief Retrieves the actor used as the background for this control.
297 * @return The actor that used as the background for this control.
299 Actor GetBackgroundActor() const;
304 * @brief This signal is emitted when key event is received.
306 * A callback of the following type may be connected:
308 * bool YourCallbackName(Control control, const KeyEvent& event);
310 * The return value of True, indicates that the touch event should be consumed.
311 * Otherwise the signal will be emitted on the next sensitive parent of the actor.
312 * @pre The Control has been initialized.
313 * @return The signal to connect to.
315 KeyEventSignalType& KeyEventSignal();
317 public: // Intended for control developers
320 * @brief Create an initialised Control.
322 * @param[in] implementation The implementation for this control.
323 * @return A handle to a newly allocated Dali resource.
325 * @note Should NOT be called to create a handle from the implementation. As stated, this allocates a NEW Dali resource.
327 explicit Control(Internal::Control& implementation);
330 * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
331 * using an Internal CustomActor pointer.
333 * @param [in] internal A pointer to a newly allocated Dali resource
335 explicit Control(Dali::Internal::CustomActor* internal);
337 public: // Templates for Deriving Classes
340 * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
342 * @tparam T The handle class
343 * @tparam I The implementation class
344 * @param[in] handle Handle to an object
345 * @return Handle to a class T or an uninitialized handle.
346 * @see DownCast(BaseHandle)
348 template<typename T, typename I>
349 DALI_INTERNAL static T DownCast( BaseHandle handle )
353 CustomActor custom = Dali::CustomActor::DownCast( handle );
356 CustomActorImpl& customImpl = custom.GetImplementation();
358 I* impl = dynamic_cast<I*>(&customImpl);
362 result = T(customImpl.GetOwner());
370 * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
371 * implementation of their class.
373 * @tparam I The implementation class
374 * @param[in] internal Pointer to the Internal::CustomActor
377 DALI_INTERNAL void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
379 // Can have a NULL pointer so we only need to check if the internal implementation is our class
380 // when there is a value.
383 DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
389 } // namespace Toolkit
393 #endif // __DALI_TOOLKIT_CONTROL_H__