1 #ifndef __DALI_TOOLKIT_CONTROL_IMPL_H__
2 #define __DALI_TOOLKIT_CONTROL_IMPL_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/adaptor-framework/style-change.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <dali/public-api/events/long-press-gesture.h>
25 #include <dali/public-api/events/pan-gesture.h>
26 #include <dali/public-api/events/pinch-gesture.h>
27 #include <dali/public-api/events/tap-gesture.h>
28 #include <dali/public-api/object/property-index-ranges.h>
29 #include <dali/public-api/object/type-info.h>
32 #include <dali-toolkit/public-api/controls/control.h>
42 namespace Internal DALI_INTERNAL
44 class RelayoutControllerImpl;
45 class KeyInputFocusManager;
48 typedef std::pair< Actor, Vector2 > ActorSizePair; ///< Pair of actor and size
49 typedef std::vector< ActorSizePair > ActorSizeContainer; ///< Container of actors and their sizes
55 * @brief This is the internal base class for all controls.
57 * It will provide some common functionality required by all controls.
58 * Implements ConnectionTrackerInterface so that signals (typically connected to member functions) will
59 * be disconnected automatically when the control is destroyed.
61 class DALI_IMPORT_API Control : public CustomActorImpl, public ConnectionTrackerInterface
65 // Creation & Destruction
68 * @brief Create a new ControlImpl instance that does not require touch by default.
70 * If touch is required then the user can connect to this class' touch signal.
71 * @return A handle to the ControlImpl instance.
73 static Toolkit::Control New();
76 * @brief Virtual destructor.
83 * @copydoc Toolkit::Control::SetSizePolicy()
85 void SetSizePolicy( Toolkit::Control::SizePolicy widthPolicy, Toolkit::Control::SizePolicy heightPolicy );
88 * @copydoc Toolkit::Control::GetSizePolicy()
90 void GetSizePolicy( Toolkit::Control::SizePolicy& widthPolicy, Toolkit::Control::SizePolicy& heightPolicy ) const;
93 * @copydoc Toolkit::Control::SetMinimumSize()
95 void SetMinimumSize( const Vector3& size );
98 * @copydoc Toolkit::Control::GetMinimumSize()
100 const Vector3& GetMinimumSize() const;
103 * @copydoc Toolkit::Control::SetMaximumSize()
105 void SetMaximumSize( const Vector3& size );
108 * @copydoc Toolkit::Control::GetMaximumSize()
110 const Vector3& GetMaximumSize() const;
113 * @copydoc Toolkit::Control::GetNaturalSize()
115 virtual Vector3 GetNaturalSize();
118 * @brief This method is called during size negotiation when a height is required for a given width.
120 * Derived classes should override this if they wish to customize the height returned.
122 * @param width to use.
123 * @return the height based on the width.
125 virtual float GetHeightForWidth( float width );
128 * @brief This method is called during size negotiation when a width is required for a given height.
130 * Derived classes should override this if they wish to customize the width returned.
132 * @param height to use.
133 * @return the width based on the width.
135 virtual float GetWidthForHeight( float height );
138 * @brief Retrieves the current Control's size.
140 * @return The control's size.
142 const Vector3& GetControlSize() const;
145 * @brief Retrieves the Control's size set by the Application / Control.
147 * @return The control's size.
149 const Vector3& GetSizeSet() const;
154 * @copydoc Toolkit::Control::SetKeyInputFocus()
156 void SetKeyInputFocus();
159 * @copydoc Toolkit::Control::HasKeyInputFocus()
161 bool HasKeyInputFocus();
164 * @copydoc Toolkit::Control::ClearKeyInputFocus()
166 void ClearKeyInputFocus();
171 * @brief If deriving classes wish to fine tune pinch gesture
172 * detection then they can access the gesture detector through this
173 * API and modify the detection.
175 * @return The pinch gesture detector.
176 * @pre Pinch detection should have been enabled via EnableGestureDetection().
177 * @see EnableGestureDetection
179 PinchGestureDetector GetPinchGestureDetector() const;
182 * @brief If deriving classes wish to fine tune pan gesture
183 * detection then they can access the gesture detector through this
184 * API and modify the detection.
186 * @return The pan gesture detector.
187 * @pre Pan detection should have been enabled via EnableGestureDetection().
188 * @see EnableGestureDetection
190 PanGestureDetector GetPanGestureDetector() const;
193 * @brief If deriving classes wish to fine tune tap gesture
194 * detection then they can access the gesture detector through this
195 * API and modify the detection.
197 * @return The tap gesture detector.
198 * @pre Tap detection should have been enabled via EnableGestureDetection().
199 * @see EnableGestureDetection
201 TapGestureDetector GetTapGestureDetector() const;
204 * @brief If deriving classes wish to fine tune long press gesture
205 * detection then they can access the gesture detector through this
206 * API and modify the detection.
208 * @return The long press gesture detector.
209 * @pre Long press detection should have been enabled via EnableGestureDetection().
210 * @see EnableGestureDetection
212 LongPressGestureDetector GetLongPressGestureDetector() const;
217 * @copydoc Dali::Toolkit::Control::SetBackgroundColor
219 void SetBackgroundColor( const Vector4& color );
222 * @copydoc Dali::Toolkit::Control::GetBackgroundColor
224 Vector4 GetBackgroundColor() const;
227 * @copydoc Dali::Toolkit::Control::SetBackgroundImage
229 void SetBackgroundImage( Image image );
232 * @copydoc Dali::Toolkit::Control::ClearBackground
234 void ClearBackground();
237 * @copydoc Dali::Toolkit::Control::GetBackgroundActor
239 Actor GetBackgroundActor() const;
241 // Keyboard Navigation
244 * @brief Sets whether this control supports two dimensional
245 * keyboard navigation (i.e. whether it knows how to handle the
246 * keyboardn focus movement between its child actors).
248 * The control doesn't support it by default.
249 * @param[in] isSupported Whether this control supports two dimensional keyboard navigation.
251 void SetKeyboardNavigationSupport(bool isSupported);
254 * @brief Gets whether this control supports two dimensional keyboard navigation.
256 * @return true if this control supports two dimensional keyboard navigation.
258 bool IsKeyboardNavigationSupported();
260 // Called by Focus Managers
263 * @brief Called by the focus manager and keyboard focus manager to Activate the Control
265 DALI_INTERNAL void Activate();
268 * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
271 * @param[in] gesture The pan gesture.
272 * @return true if the pan gesture has been consumed by this control
274 virtual bool OnAccessibilityPan(PanGesture gesture);
277 * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
280 * @param[in] touchEvent The touch event.
281 * @return true if the touch event has been consumed by this control
283 virtual bool OnAccessibilityTouch(const TouchEvent& touchEvent);
286 * @brief This method should be overridden by deriving classes when they wish to respond
287 * the accessibility up and down action (i.e. value change of slider control).
289 * @param[in] isIncrease Whether the value should be increased or decreased
290 * @return true if the value changed action has been consumed by this control
292 virtual bool OnAccessibilityValueChange(bool isIncrease);
294 // Called by the RelayoutController
297 * @brief Called by the RelayoutController to negotiate the size of a control.
299 * The size allocated by the the algorithm is passed in which the
300 * control must adhere to. A container is passed in as well which
301 * the control should populate with actors it has not / or does not
302 * need to handle in its size negotiation.
304 * @param[in] size The allocated size.
305 * @param[in,out] container The container that holds actors that are fed back into the
306 * RelayoutController algorithm.
308 DALI_INTERNAL void NegotiateSize( const Vector2& size, ActorSizeContainer& container );
313 * @brief Sets whether this control is a focus group for keyboard navigation.
315 * (i.e. the scope of keyboard focus movement
316 * can be limitied to its child actors). The control is not a focus group by default.
317 * @param[in] isFocusGroup Whether this control is set as a focus group for keyboard navigation.
319 void SetAsKeyboardFocusGroup(bool isFocusGroup);
322 * @brief Gets whether this control is a focus group for keyboard navigation.
324 * @return true if this control is set as a focus group for keyboard navigation.
326 bool IsKeyboardFocusGroup();
329 * @brief Gets the next keyboard focusable actor in this control towards the given direction.
331 * A control needs to override this function in order to support two dimensional keyboard navigation.
332 * @param[in] currentFocusedActor The current focused actor.
333 * @param[in] direction The direction to move the focus towards.
334 * @param[in] loopEnabled Whether the focus movement should be looped within the control.
335 * @return the next keyboard focusable actor in this control or an empty handle if no actor can be focused.
337 virtual Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
340 * @brief Informs this control that its chosen focusable actor will be focused.
342 * This allows the application to preform any actions if wishes
343 * before the focus is actually moved to the chosen actor.
345 * @param[in] commitedFocusableActor The commited focusable actor.
347 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
352 * @brief Performs actions as requested using the action name.
354 * @param[in] object The object on which to perform the action.
355 * @param[in] actionName The action to perform.
356 * @param[in] attributes The attributes with which to perfrom this action.
357 * @return true if action has been accepted by this control
359 static bool DoAction(BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes);
362 * Connects a callback function with the object's signals.
363 * @param[in] object The object providing the signal.
364 * @param[in] tracker Used to disconnect the signal.
365 * @param[in] signalName The signal to connect to.
366 * @param[in] functor A newly allocated FunctorDelegate.
367 * @return True if the signal was connected.
368 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
370 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
373 * @copydoc Dali::Toolkit::Control::KeyEventSignal()
375 Toolkit::Control::KeyEventSignalType& KeyEventSignal();
378 * @brief Called by the KeyInputFocusManager to emit key event signals.
380 * @param[in] event The key event.
381 * @return True if the event was consumed.
383 DALI_INTERNAL bool EmitKeyEventSignal(const KeyEvent& event);
389 // Flags for the constructor
390 enum ControlBehaviour
392 CONTROL_BEHAVIOUR_NONE = 0,
393 REQUIRES_TOUCH_EVENTS = 1<<1, ///< True if the OnTouchEvent() callback is required.
394 REQUIRES_STYLE_CHANGE_SIGNALS = 1<<2, ///< True if needs to monitor style change signals such as theme/font change
395 NO_SIZE_NEGOTIATION = 1<<3, ///< True if control does not need size negotiation, i.e. it can be skipped in the algorithm
396 REQUIRES_HOVER_EVENTS = 1<<4, ///< True if the OnHoverEvent() callback is required.
397 REQUIRES_MOUSE_WHEEL_EVENTS = 1<<5 ///< True if the OnMouseWheelEvent() callback is required.
401 * @brief Create a Control.
403 * @param[in] behaviourFlags Behavioural flags from ControlBehaviour enum
405 Control(ControlBehaviour behaviourFlags);
408 * @brief Second phase initialization.
415 * @brief Allows deriving classes to enable any of the gesture detectors that are available.
417 * Gesture detection can be enabled one at a time or in bitwise format as shown:
419 * EnableGestureDetection(Gesture::Type(Gesture::Pinch | Gesture::Tap | Gesture::Pan));
421 * @param[in] type The gesture type(s) to enable.
423 void EnableGestureDetection(Gesture::Type type);
426 * @brief Allows deriving classes to disable any of the gesture detectors.
428 * Like EnableGestureDetection, this can also be called using bitwise or.
429 * @param[in] type The gesture type(s) to disable.
430 * @see EnableGetureDetection
432 void DisableGestureDetection(Gesture::Type type);
437 * @brief Request a relayout, which means performing a size negotiation on this control, its parent and children (and potentially whole scene)
439 * This method is automatically called from OnStageConnection(), OnChildAdd(),
440 * OnChildRemove(), SetSizePolicy(), SetMinimumSize() and SetMaximumSize().
442 * This method can also be called from a derived class every time it needs a different size.
443 * At the end of event processing, the relayout process starts and
444 * all controls which requested Relayout will have their sizes (re)negotiated.
446 * @note RelayoutRequest() can be called multiple times; the size negotiation is still
447 * only performed once, i.e. there is no need to keep track of this in the calling side.
449 void RelayoutRequest();
452 * @brief Helper method for controls to Relayout their children if
453 * they do not know whether that child is a control or not.
455 * @param[in] actor The actor to relayout.
456 * @param[in] size The size to allocate to the actor.
457 * @param[in,out] container The container that holds actors that have not been allocated a size yet.
459 static void Relayout( Actor actor, const Vector2& size, ActorSizeContainer& container );
463 // For derived classes to override
466 * @brief This method is called after the Control has been initialized.
468 * Derived classes should do any second phase initialization by overriding this method.
470 virtual void OnInitialize();
473 * @brief This method is called when the control is activated.
475 * Derived classes should override this if they wish to be notified when they are activated.
477 virtual void OnActivated();
480 * @brief This method should be overridden by deriving classes when
481 * they wish to be notified when the style manager changes the theme.
483 * @param[in] styleManager The StyleManager object.
485 virtual void OnThemeChange( Toolkit::StyleManager styleManager );
488 * @brief This method should be overridden by deriving classes when
489 * they wish to be notified when the style changes the default font.
491 * @param[in] defaultFontChange Information denoting whether the default font has changed.
492 * @param[in] defaultFontSizeChange Information denoting whether the default font size has changed.
494 virtual void OnFontChange( bool defaultFontChange, bool defaultFontSizeChange );
497 * @brief Called whenever a pinch gesture is detected on this control.
499 * This can be overridden by deriving classes when pinch detection
500 * is enabled. The default behaviour is to scale the control by the
503 * @note If overridden, then the default behaviour will not occur.
504 * @note Pinch detection should be enabled via EnableGestureDetection().
505 * @param[in] pinch The pinch gesture.
506 * @see EnableGestureDetection
508 virtual void OnPinch(const PinchGesture& pinch);
511 * @brief Called whenever a pan gesture is detected on this control.
513 * This should be overridden by deriving classes when pan detection
516 * @note There is no default behaviour with panning.
517 * @note Pan detection should be enabled via EnableGestureDetection().
518 * @param[in] pan The pan gesture.
519 * @see EnableGestureDetection
521 virtual void OnPan( const PanGesture& pan );
524 * @brief Called whenever a tap gesture is detected on this control.
526 * This should be overridden by deriving classes when tap detection
529 * @note There is no default behaviour with a tap.
530 * @note Tap detection should be enabled via EnableGestureDetection().
531 * @param[in] tap The tap gesture.
532 * @see EnableGestureDetection
534 virtual void OnTap( const TapGesture& tap );
537 * @brief Called whenever a long press gesture is detected on this control.
539 * This should be overridden by deriving classes when long press
540 * detection is enabled.
542 * @note There is no default behaviour associated with a long press.
543 * @note Long press detection should be enabled via EnableGestureDetection().
544 * @param[in] longPress The long press gesture.
545 * @see EnableGestureDetection
547 virtual void OnLongPress( const LongPressGesture& longPress );
550 * @brief Called whenever the control is added to the stage.
552 * Could be overridden by derived classes.
554 virtual void OnControlStageConnection();
557 * @brief Called whenever the control is removed from the stage.
559 * Could be overridden by derived classes.
561 virtual void OnControlStageDisconnection();
564 * @brief Called whenever an Actor is added to the control.
566 * Could be overridden by derived classes.
568 * @param[in] child The added actor.
570 virtual void OnControlChildAdd( Actor& child );
573 * @brief Called whenever an Actor is removed from the control.
575 * Could be overridden by derived classes.
577 * @param[in] child The removed actor.
579 virtual void OnControlChildRemove( Actor& child );
582 * @brief Called whenever the Control's size is set.
584 * Could be overridden by derived classes.
586 * @param[in] size The new size.
588 virtual void OnControlSizeSet( const Vector3& size );
591 * @brief Called after the size negotiation has been finished for this control.
593 * The control is expected to assign this given size to itself/its children.
595 * Should be overridden by derived classes if they need to layout
596 * actors differently after certain operations like add or remove
597 * actors, resize or after changing specific properties.
599 * Note! As this function is called from inside the size negotiation algorithm, you cannot
600 * call RequestRelayout (the call would just be ignored)
602 * @param[in] size The allocated size.
603 * @param[in,out] container The control should add actors to this container that it is not able
604 * to allocate a size for.
606 virtual void OnRelayout( const Vector2& size, ActorSizeContainer& container );
609 * @brief Called when the control gains key input focus.
611 * Should be overridden by derived classes if they need to customize what happens when focus is gained.
613 virtual void OnKeyInputFocusGained();
616 * @brief Called when the control loses key input focus.
618 * Should be overridden by derived classes if they need to customize what happens when focus is lost.
620 virtual void OnKeyInputFocusLost();
622 // From CustomActorImpl, derived classes can override these.
625 * @copydoc Dali::CustomActorImpl::OnSizeAnimation(Animation&, const Vector3&)
627 virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize);
630 * @copydoc Dali::CustomActorImpl::OnTouchEvent(const TouchEvent&)
632 virtual bool OnTouchEvent(const TouchEvent& event);
635 * @copydoc Dali::CustomActorImpl::OnHoverEvent(const HoverEvent&)
637 virtual bool OnHoverEvent(const HoverEvent& event);
640 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
642 virtual bool OnKeyEvent(const KeyEvent& event);
645 * @copydoc Dali::CustomActorImpl::OnMouseWheelEvent(const MouseWheelEvent&)
647 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
649 // From CustomActorImpl, derived classes should NOT override these.
652 * @brief Sends a request to relayout this control.
654 * The control will be relaid out after the
655 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted.
657 * It calls OnControlStageConnection() to notify derived classes.
659 * @see Dali::CustomActorImpl::OnStageConnection()
661 virtual void OnStageConnection();
664 * @brief Calls OnControlStageDisconnection() to notify derived classed.
666 * @see Dali::CustomActorImpl::OnStageDisconnection()
668 virtual void OnStageDisconnection();
671 * @brief Sends a request to relayout this control.
673 * The control will be relaid out after the
674 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted. It
675 * calls OnControlChildAdd() to notify derived classes.
677 * @note This method shouldn't be overridden by derived classes.
679 * @param[in] child The added actor.
681 * @see Dali::CustomActorImpl::OnChildAdd(Actor&)
683 virtual void OnChildAdd(Actor& child);
686 * @brief Sends a request to relayout this control.
688 * The control will be relaid out after the
689 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted. It
690 * calls OnControlChildRemove() to notify derived classes.
692 * @note This method shouldn't be overridden by derived classes.
694 * @param[in] child The removed actor.
696 * @see Dali::CustomActorImpl::OnChildRemove(Actor&)
698 virtual void OnChildRemove(Actor& child);
701 * @brief It stores the size set by size negotiation and relayout.
703 * It also keeps a backup of the size set through the Actor's API used in the size negotiation.
704 * It calls the OnControlSizeSet() to notify derived classes.
706 * @param[in] targetSize The new size.
708 * @see Dali::CustomActorImpl::OnSizeSet(const Vector3&)
710 virtual void OnSizeSet(const Vector3& targetSize);
712 // From ConnectionTrackerInterface
715 * @copydoc ConnectionTrackerInterface::SignalConnected
717 virtual void SignalConnected( SlotObserver* slotObserver, CallbackBase* callback );
720 * @copydoc ConnectionTrackerInterface::SignalDisconnected
722 virtual void SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback );
727 * @brief This method is the callback for the StyleChangeSignal from StyleManager
729 * @param[in] styleManager The StyleManager Object
730 * @param[in] change Information denoting what has changed.
732 DALI_INTERNAL void DoStyleChange( Toolkit::StyleManager styleManager, StyleChange change );
737 DALI_INTERNAL Control(const Control&);
738 DALI_INTERNAL Control& operator=(const Control&);
743 friend class Internal::KeyInputFocusManager; ///< KeyInputFocusManager needs to call several methods which are private. // TODO: Remove
746 } // namespace Internal
748 } // namespace Toolkit
752 #endif // __DALI_TOOLKIT_CONTROL_IMPL_H__