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/dali.h>
24 namespace Dali DALI_IMPORT_API
30 //Forward declarations.
38 * @brief Control is the base class for all controls.
40 * The implementation of the control must be supplied; see Internal::Control for more details.
41 * @see Internal::Control
43 class Control : public CustomActor
49 static const Property::Index PROPERTY_BACKGROUND_COLOR; ///< name "background-color", @see SetBackgroundColor, type VECTOR4
50 static const Property::Index PROPERTY_BACKGROUND; ///< name "background", @see SetBackground, type MAP
51 static const Property::Index PROPERTY_WIDTH_POLICY; ///< name "width-policy", @see SetSizePolicy, type STRING
52 static const Property::Index PROPERTY_HEIGHT_POLICY; ///< name "height-policy", @see SetSizePolicy, type STRING
53 static const Property::Index PROPERTY_MINIMUM_SIZE; ///< name "minimum-size", @see SetMinimumSize, type VECTOR3
54 static const Property::Index PROPERTY_MAXIMUM_SIZE; ///< name "maximum-size", @see SetMaximumSize, type VECTOR3
55 static const Property::Index PROPERTY_KEY_INPUT_FOCUS; ///< name "key-input-focus", @see SetKeyInputFocus, type BOOLEAN
60 static const char* const SIGNAL_KEY_EVENT; ///< name "key-event"
65 static const char* const ACTION_CONTROL_ACTIVATED; ///< name "control-activated"
69 * @brief Describes how a control could be resized.
73 Fixed, ///< Size can't grow or shrink.
74 Minimum, ///< Size can grow but shrink up to a minimum level.
75 Maximum, ///< Size can shrink but grow up to a maximum value.
76 Range, ///< Size can grow or shrink between a minimum and a maximum values.
77 Flexible, ///< Size can grow or shrink with no limits.
81 * @brief Describes what a control should do when a contained actor/control exceeds the boundary of the control.
85 Crop, ///< Control's contents will be cropped.
86 Shrink, ///< Control's contents will be shrunk.
87 Scroll ///< Control's contents will be added to a scroll.
91 * @brief Describes the direction to move the keyboard focus towards.
93 enum KeyboardFocusNavigationDirection
95 Left, ///< Move keyboard focus towards the left direction
96 Right, ///< Move keyboard focus towards the right direction
97 Up, ///< Move keyboard focus towards the up direction
98 Down ///< Move keyboard focus towards the down direction
103 /// @brief Key Event signal type;
104 typedef SignalV2<bool ( Control, const KeyEvent& ) > KeyEventSignalV2;
106 public: // Creation & Destruction
109 * @brief Create a new instance of a Control.
111 * @return A handle to a new Control.
113 static Control New();
116 * @brief Create an uninitialized Control handle.
118 * Only derived versions can be instantiated. Calling member
119 * functions with an uninitialized Dali::Object is not allowed.
124 * @brief Copy constructor.
126 * Creates another handle that points to the same real object
127 * @param[in] uiControl Handle to copy
129 Control(const Control& uiControl);
132 * @brief Dali::Control is intended as a base class
134 * This is non-virtual since derived Handle types must not contain data or virtual methods.
141 * @brief Assignment operator.
143 * Changes this handle to point to another real object
144 * @param[in] handle Object to assign this to
145 * @return reference to this
147 Control& operator=( const Control& handle );
152 * @brief Downcast an Object handle to Control.
154 * If handle points to a Control the downcast produces valid
155 * handle. If not the returned handle is left uninitialized.
157 * @param[in] handle Handle to an object
158 * @return handle to a Control or an uninitialized handle
160 static Control DownCast( BaseHandle handle );
163 * @brief Retrieve the Control implementation.
165 * @return The implementation.
167 Internal::Control& GetImplementation();
170 * @brief Retrieve the Control implementation.
172 * @return The implementation.
174 const Internal::Control& GetImplementation() const;
179 * @brief Sets the size policies for the width and height dimensions.
181 * @param[in] widthPolicy Size policy for the width dimension.
182 * @param[in] heightPolicy Size policy for the height dimension.
184 void SetSizePolicy( SizePolicy widthPolicy, SizePolicy heightPolicy );
187 * @brief Retrieves the size policies for the width and height dimensions.
189 * @param[out] widthPolicy Width's size policy.
190 * @param[out] heightPolicy Height's size policy.
192 void GetSizePolicy( SizePolicy& widthPolicy, SizePolicy& heightPolicy ) const;
195 * @brief Sets the minimum size for the control.
197 * @param[in] size The minimum size.
199 void SetMinimumSize( const Vector3& size );
202 * @brief Retrieves the minimum size.
204 * @return The minimum size.
206 const Vector3& GetMinimumSize() const;
209 * @brief Sets the maximum size.
211 * @param[in] size The maximum size.
213 void SetMaximumSize( const Vector3& size );
216 * @brief Retrieves the maximum size.
218 * @return The maximum size.
220 const Vector3& GetMaximumSize() const;
223 * @brief Works out the natural size.
225 * Natural size is the control's size with any restriction.
227 * @return The natural size.
229 Vector3 GetNaturalSize();
232 * @brief Works out the control's height for a given width.
234 * @param[in] width The control's width.
236 * @return The control's height for the given width.
238 float GetHeightForWidth( float width );
241 * @brief Works out the control's width for a given height.
243 * @param[in] height The control's height.
245 * @return The control's width for the given height.
247 float GetWidthForHeight( float height );
252 * @brief This sets the control to receive key events.
254 * The key event can originate from a virtual or physical keyboard.
255 * @pre The Control has been initialized.
256 * @pre The Control should be on the stage before setting keyboard focus.
257 * @return True if the control has foucs, False otherwise.
259 void SetKeyInputFocus();
262 * @brief Quries whether the control has key input focus.
264 * 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.
265 * As the key input focus mechanism works like a stack, the top most control receives all the key events, and passes on the
266 * unhandled events to the controls below in the stack. A control in the stack will regain key input focus when there are no more
267 * controls above it in the focus stack.
268 * To query for the conrol which is on top of the focus stack use Dali::Toolkit::KeyInputFocusManager::GetCurrentKeyboardFocusActor()
269 * @pre The Control has been initialized.
270 * @pre The Control should be on the stage before setting keyboard focus.
271 * @return true if this control has keyboard input focus
273 bool HasKeyInputFocus();
276 * @brief Once an actor is Set to receive key input focus this function is called to stop it receiving key events.
278 * A check is performed to ensure it was previously set, if this check fails then nothing is done.
279 * @pre The Actor has been initialized.
281 void ClearKeyInputFocus();
286 * @brief Retrieves the pinch gesture detector of the control.
288 * @return The pinch gesture detector.
289 * @pre Pinch detection should have been enabled in the control.
291 PinchGestureDetector GetPinchGestureDetector() const;
294 * @brief Retrieves the pan gesture detector of the control.
296 * @return The pan gesture detector.
297 * @pre Pan detection should have been enabled in the control.
299 PanGestureDetector GetPanGestureDetector() const;
302 * @brief Retrieves the tap gesture detector of the control.
304 * @return The tap gesture detector.
305 * @pre Tap detection should have been enabled in the control.
307 TapGestureDetector GetTapGestureDetector() const;
310 * @brief Retrieves the long press gesture detector of the control.
312 * @return The long press gesture detector.
313 * @pre Long press detection should have been enabled in the control.
315 LongPressGestureDetector GetLongPressGestureDetector() const;
320 * @brief Sets the background color of the control.
322 * @param[in] color The required background color of the control
324 void SetBackgroundColor( const Vector4& color );
327 * @brief Retrieves the background color of the control.
329 * @return The background color of the control.
331 Vector4 GetBackgroundColor() const;
334 * @brief Sets an image as the background of the control.
336 * The color of this image is blended with the background color @see SetBackgroundColor
338 * @param[in] image The image to set as the background.
340 void SetBackground( Image image );
343 * @brief Clears the background.
345 void ClearBackground();
348 * @brief Retrieves the actor used as the background for this control.
350 * @return The actor that used as the background for this control.
352 Actor GetBackgroundActor() const;
357 * @brief This signal is emitted when key event is received.
359 * A callback of the following type may be connected:
361 * bool YourCallbackName(Control control, const KeyEvent& event);
363 * The return value of True, indicates that the touch event should be consumed.
364 * Otherwise the signal will be emitted on the next sensitive parent of the actor.
365 * @pre The Control has been initialized.
366 * @return The signal to connect to.
368 KeyEventSignalV2& KeyEventSignal();
370 public: // Not intended for application developers
373 * @brief Create an initialised Control.
375 * @param[in] implementation The implementation for this control.
376 * @return A handle to a newly allocated Dali resource.
378 Control(Internal::Control& implementation);
381 * @brief This constructor is used by CustomActor within Dali core to create additional Control handles
382 * using an Internal CustomActor pointer.
384 * @param [in] internal A pointer to a newly allocated Dali resource
386 Control(Dali::Internal::CustomActor* internal);
388 public: // Templates for Deriving Classes
391 * @brief Template to allow deriving controls to DownCast handles to deriving handle classes.
393 * @tparam T The handle class
394 * @tparam I The implementation class
395 * @param[in] handle Handle to an object
396 * @return Handle to a class T or an uninitialized handle.
397 * @see DownCast(BaseHandle)
399 template<typename T, typename I>
400 static T DownCast( BaseHandle handle )
404 CustomActor custom = Dali::CustomActor::DownCast( handle );
407 CustomActorImpl& customImpl = custom.GetImplementation();
409 I* impl = dynamic_cast<I*>(&customImpl);
413 result = T(customImpl.GetOwner());
421 * @brief Template to allow deriving controls to verify whether the Internal::CustomActor* is actually an
422 * implementation of their class.
424 * @tparam I The implementation class
425 * @param[in] internal Pointer to the Internal::CustomActor
428 void VerifyCustomActorPointer(Dali::Internal::CustomActor* internal)
430 // Can have a NULL pointer so we only need to check if the internal implementation is our class
431 // when there is a value.
434 DALI_ASSERT_DEBUG(dynamic_cast<I*>(&CustomActor(internal).GetImplementation()));
440 } // namespace Toolkit
444 #endif // __DALI_TOOLKIT_CONTROL_H__