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.h>
31 #include <dali-toolkit/public-api/controls/control.h>
41 namespace Internal DALI_INTERNAL
43 class RelayoutControllerImpl;
44 class KeyInputFocusManager;
47 typedef std::pair< Actor, Vector2 > ActorSizePair; ///< Pair of actor and size
48 typedef std::vector< ActorSizePair > ActorSizeContainer; ///< Container of actors and their sizes
54 * @brief This is the internal base class for all controls.
56 * It will provide some common functionality required by all controls.
57 * Implements ConnectionTrackerInterface so that signals (typically connected to member functions) will
58 * be disconnected automatically when the control is destroyed.
60 class DALI_IMPORT_API Control : public CustomActorImpl, public ConnectionTrackerInterface
67 CONTROL_PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX,
68 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
74 * @brief Create a new ControlImpl instance that does not require touch by default.
76 * If touch is required then the user can connect to this class' touch signal.
77 * @return A handle to the ConntrolImpl instance.
79 static Toolkit::Control New();
84 * @brief Virtual destructor.
91 * @brief This method should be overridden by deriving classes when they wish to be notified when they
94 virtual void OnActivated() { }
97 * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
100 * @param[in] gesture The pan gesture.
101 * @return true if the pan gesture has been consumed by this control
103 virtual bool OnAccessibilityPan(PanGesture gesture);
106 * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
109 * @param[in] touchEvent The touch event.
110 * @return true if the touch event has been consumed by this control
112 virtual bool OnAccessibilityTouch(const TouchEvent& touchEvent);
115 * @brief This method should be overridden by deriving classes when they wish to respond
116 * the accessibility up and down action (i.e. value change of slider control).
118 * @param[in] isIncrease Whether the value should be increased or decreased
119 * @return true if the value changed action has been consumed by this control
121 virtual bool OnAccessibilityValueChange(bool isIncrease);
124 * @brief Sets whether this control supports two dimensional
125 * keyboard navigation (i.e. whether it knows how to handle the
126 * keyboardn focus movement between its child actors).
128 * The control doesn't support it by default.
129 * @param[in] isSupported Whether this control supports two dimensional keyboard navigation.
131 void SetKeyboardNavigationSupport(bool isSupported);
134 * @brief Gets whether this control supports two dimensional keyboard navigation.
136 * @return true if this control supports two dimensional keyboard navigation.
138 bool IsKeyboardNavigationSupported();
141 * @brief Sets whether this control is a focus group for keyboard navigation.
143 * (i.e. the scope of keyboard focus movement
144 * can be limitied to its child actors). The control is not a focus group by default.
145 * @param[in] isFocusGroup Whether this control is set as a focus group for keyboard navigation.
147 void SetAsKeyboardFocusGroup(bool isFocusGroup);
150 * @brief Gets whether this control is a focus group for keyboard navigation.
152 * @return true if this control is set as a focus group for keyboard navigation.
154 bool IsKeyboardFocusGroup();
157 * @brief Gets the next keyboard focusable actor in this control towards the given direction.
159 * A control needs to override this function in order to support two dimensional keyboard navigation.
160 * @param[in] currentFocusedActor The current focused actor.
161 * @param[in] direction The direction to move the focus towards.
162 * @param[in] loopEnabled Whether the focus movement should be looped within the control.
163 * @return the next keyboard focusable actor in this control or an empty handle if no actor can be focused.
165 virtual Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
168 * @brief Informs this control that its chosen focusable actor will be focused.
170 * This allows the application to preform any actions if wishes
171 * before the focus is actually moved to the chosen actor.
173 * @param[in] commitedFocusableActor The commited focusable actor.
175 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor) { }
178 * @brief Performs actions as requested using the action name.
180 * @param[in] object The object on which to perform the action.
181 * @param[in] actionName The action to perform.
182 * @param[in] attributes The attributes with which to perfrom this action.
183 * @return true if action has been accepted by this control
185 static bool DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes);
188 * Connects a callback function with the object's signals.
189 * @param[in] object The object providing the signal.
190 * @param[in] tracker Used to disconnect the signal.
191 * @param[in] signalName The signal to connect to.
192 * @param[in] functor A newly allocated FunctorDelegate.
193 * @return True if the signal was connected.
194 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
196 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
199 * @brief If deriving classes wish to fine tune pinch gesture
200 * detection then they can access the gesture detector through this
201 * API and modify the detection.
203 * @return The pinch gesture detector.
204 * @pre Pinch detection should have been enabled via EnableGestureDetection().
205 * @see EnableGestureDetection
207 PinchGestureDetector GetPinchGestureDetector() const;
210 * @brief If deriving classes wish to fine tune pan gesture
211 * detection then they can access the gesture detector through this
212 * API and modify the detection.
214 * @return The pan gesture detector.
215 * @pre Pan detection should have been enabled via EnableGestureDetection().
216 * @see EnableGestureDetection
218 PanGestureDetector GetPanGestureDetector() const;
221 * @brief If deriving classes wish to fine tune tap gesture
222 * detection then they can access the gesture detector through this
223 * API and modify the detection.
225 * @return The tap gesture detector.
226 * @pre Tap detection should have been enabled via EnableGestureDetection().
227 * @see EnableGestureDetection
229 TapGestureDetector GetTapGestureDetector() const;
232 * @brief If deriving classes wish to fine tune long press gesture
233 * detection then they can access the gesture detector through this
234 * API and modify the detection.
236 * @return The long press gesture detector.
237 * @pre Long press detection should have been enabled via EnableGestureDetection().
238 * @see EnableGestureDetection
240 LongPressGestureDetector GetLongPressGestureDetector() const;
245 * @copydoc Dali::Toolkit::Control::SetBackgroundColor
247 void SetBackgroundColor( const Vector4& color );
250 * @copydoc Dali::Toolkit::Control::GetBackgroundColor
252 Vector4 GetBackgroundColor() const;
255 * @copydoc Dali::Toolkit::Control::SetBackground
257 void SetBackground( Image image );
260 * @copydoc Dali::Toolkit::Control::ClearBackground
262 void ClearBackground();
265 * @copydoc Dali::Toolkit::Control::GetBackgroundActor
267 Actor GetBackgroundActor() const;
272 * @copydoc Dali::Toolkit::Control::KeyEventSignal()
274 Toolkit::Control::KeyEventSignalV2& KeyEventSignal();
281 * @brief Second phase initialization.
288 * @brief Allows deriving classes to enable any of the gesture detectors that are available.
290 * Gesture detection can be enabled one at a time or in bitwise format as shown:
292 * EnableGestureDetection(Gesture::Type(Gesture::Pinch | Gesture::Tap | Gesture::Pan));
294 * @param[in] type The gesture type(s) to enable.
296 void EnableGestureDetection(Gesture::Type type);
299 * @brief Allows deriving classes to disable any of the gesture detectors.
301 * Like EnableGestureDetection, this can also be called using bitwise or.
302 * @param[in] type The gesture type(s) to disable.
303 * @see EnableGetureDetection
305 void DisableGestureDetection(Gesture::Type type);
307 private: // For derived classes to override
310 * @brief This method is called after the Control has been initialized.
312 * Derived classes should do any second phase initialization by
313 * overriding this method.
315 virtual void OnInitialize() { }
318 * @brief This method should be overridden by deriving classes when
319 * they wish to be notified when the style manager changes the theme.
321 * @param[in] styleManager The StyleManager object.
323 virtual void OnThemeChange( Toolkit::StyleManager styleManager );
326 * @brief This method should be overridden by deriving classes when
327 * they wish to be notified when the style changes the default font.
329 * @param[in] defaultFontChange Information denoting whether the default font has changed.
330 * @param[in] defaultFontSizeChange Information denoting whether the default font size has changed.
332 virtual void OnFontChange( bool defaultFontChange, bool defaultFontSizeChange ){ }
335 * @brief Called whenever a pinch gesture is detected on this control.
337 * This can be overridden by deriving classes when pinch detection
338 * is enabled. The default behaviour is to scale the control by the
341 * @note If overridden, then the default behaviour will not occur.
342 * @note Pinch detection should be enabled via EnableGestureDetection().
343 * @param[in] pinch The pinch gesture.
344 * @see EnableGestureDetection
346 virtual void OnPinch(PinchGesture pinch);
349 * @brief Called whenever a pan gesture is detected on this control.
351 * This should be overridden by deriving classes when pan detection
354 * @note There is no default behaviour with panning.
355 * @note Pan detection should be enabled via EnableGestureDetection().
356 * @param[in] pan The pan gesture.
357 * @see EnableGestureDetection
359 virtual void OnPan(PanGesture pan) { }
362 * @brief Called whenever a tap gesture is detected on this control.
364 * This should be overridden by deriving classes when tap detection
367 * @note There is no default behaviour with a tap.
368 * @note Tap detection should be enabled via EnableGestureDetection().
369 * @param[in] tap The tap gesture.
370 * @see EnableGestureDetection
372 virtual void OnTap(TapGesture tap) { }
375 * @brief Called whenever a long press gesture is detected on this control.
377 * This should be overridden by deriving classes when long press
378 * detection is enabled.
380 * @note There is no default behaviour associated with a long press.
381 * @note Long press detection should be enabled via EnableGestureDetection().
382 * @param[in] longPress The long press gesture.
383 * @see EnableGestureDetection
385 virtual void OnLongPress(LongPressGesture longPress) { }
388 * @brief Called whenever the control is added to the stage.
390 * Could be overridden by derived classes.
392 virtual void OnControlStageConnection() { }
395 * @brief Called whenever the control is removed from the stage.
397 * Could be overridden by derived classes.
399 virtual void OnControlStageDisconnection() { }
402 * @brief Called whenever an Actor is added to the control.
404 * Could be overridden by derived classes.
406 * @param[in] child The added actor.
408 virtual void OnControlChildAdd( Actor& child ) { }
411 * @brief Called whenever an Actor is removed from the control.
413 * Could be overridden by derived classes.
415 * @param[in] child The removed actor.
417 virtual void OnControlChildRemove( Actor& child ) { }
420 * @brief Called whenever the Control's size is set.
422 * Could be overridden by derived classes.
424 * @param[in] size The new size.
426 virtual void OnControlSizeSet( const Vector3& size ) { }
429 * @brief Called after the Dali::Stage::SignalMessageQueueFlushed()
430 * signal is emitted if this control requested to be relaid-out.
432 * Should be overridden by derived classes if they need to layout
433 * actors differently after certain operations like add or remove
434 * actors, resize or after changing especific properties.
436 * @param[in] size The allocated size.
437 * @param[in,out] container The control should add actors to this container that it is not able
438 * to allocate a size for.
440 virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
442 private: // From CustomActorImpl, derived classes can override these.
445 * @brief Sends a request to relayout this control.
447 * The control will be relaid out after the
448 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted.
450 * It calls OnControlStageConnection() to notify derived classes.
452 * @see Dali::CustomActorImpl::OnStageConnection()
454 virtual void OnStageConnection();
457 * @brief Calls OnControlStageDisconnection() to notify derived classed.
459 * @see Dali::CustomActorImpl::OnStageDisconnection()
461 virtual void OnStageDisconnection();
464 * @brief Sends a request to relayout this control.
466 * The control will be relaid out after the
467 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted. It
468 * calls OnControlChildAdd() to notify derived classes.
470 * @note This method shouldn't be overridden by derived classes.
472 * @param[in] child The added actor.
474 * @see Dali::CustomActorImpl::OnChildAdd(Actor&)
476 virtual void OnChildAdd(Actor& child);
479 * @brief Sends a request to relayout this control.
481 * The control will be relaid out after the
482 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted. It
483 * calls OnControlChildRemove() to notify derived classes.
485 * @note This method shouldn't be overridden by derived classes.
487 * @param[in] child The removed actor.
489 * @see Dali::CustomActorImpl::OnChildRemove(Actor&)
491 virtual void OnChildRemove(Actor& child);
494 * @brief It stores the size set by size negotiation and relayout.
496 * It also keeps a backup of the size set through the Actor's API used in the size negotiation.
497 * It calls the OnControlSizeSet() to notify derived classes.
499 * @param[in] targetSize The new size.
501 * @see Dali::CustomActorImpl::OnSizeSet(const Vector3&)
503 virtual void OnSizeSet(const Vector3& targetSize);
506 * @copydoc Dali::CustomActorImpl::OnSizeAnimation(Animation&, const Vector3&)
508 virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize);
511 * @copydoc Dali::CustomActorImpl::OnTouchEvent(const TouchEvent&)
513 virtual bool OnTouchEvent(const TouchEvent& event);
516 * @copydoc Dali::CustomActorImpl::OnHoverEvent(const HoverEvent&)
518 virtual bool OnHoverEvent(const HoverEvent& event);
521 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
523 virtual bool OnKeyEvent(const KeyEvent& event);
526 * @copydoc Dali::CustomActorImpl::OnMouseWheelEvent(const MouseWheelEvent&)
528 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
531 * @copydoc Dali::CustomActorImpl::OnKeyInputFocusGained()
533 virtual void OnKeyInputFocusGained();
536 * @copydoc Dali::CustomActorImpl::OnKeyInputFocusLost()
538 virtual void OnKeyInputFocusLost();
541 * @copydoc Dali::CustomActorImpl::GetChildByAlias(const std::string& actorAlias)
543 virtual Actor GetChildByAlias(const std::string& actorAlias);
548 * @brief This method is the callback for the StyleChangeSignal from StyleManager
550 * @param[in] styleManager The StyleManager Object
551 * @param[in] change Information denoting what has changed.
553 DALI_INTERNAL void DoStyleChange( Toolkit::StyleManager styleManager, StyleChange change );
555 protected: // Construction
557 // Flags for the constructor
558 enum ControlBehaviour
560 CONTROL_BEHAVIOUR_NONE = 0,
561 REQUIRES_TOUCH_EVENTS = 1<<1, ///< True if the OnTouchEvent() callback is required.
562 REQUIRES_STYLE_CHANGE_SIGNALS = 1<<2, ///< True if needs to monitor style change signals such as theme/font change
563 NO_SIZE_NEGOTIATION = 1<<3, ///< True if control does not need size negotiation, i.e. it can be skipped in the algorithm
564 REQUIRES_HOVER_EVENTS = 1<<4, ///< True if the OnHoverEvent() callback is required.
565 REQUIRES_MOUSE_WHEEL_EVENTS = 1<<5 ///< True if the OnMouseWheelEvent() callback is required.
569 * @brief Create a Control.
571 * @param[in] behaviourFlags Behavioural flags from ControlBehaviour enum
573 Control(ControlBehaviour behaviourFlags);
580 * @copydoc Toolkit::Control::SetSizePolicy()
582 void SetSizePolicy( Toolkit::Control::SizePolicy widthPolicy, Toolkit::Control::SizePolicy heightPolicy );
585 * @copydoc Toolkit::Control::GetSizePolicy()
587 void GetSizePolicy( Toolkit::Control::SizePolicy& widthPolicy, Toolkit::Control::SizePolicy& heightPolicy ) const;
590 * @copydoc Toolkit::Control::SetMinimumSize()
592 void SetMinimumSize( const Vector3& size );
595 * @copydoc Toolkit::Control::GetMinimumSize()
597 const Vector3& GetMinimumSize() const;
600 * @copydoc Toolkit::Control::SetMaximumSize()
602 void SetMaximumSize( const Vector3& size );
605 * @copydoc Toolkit::Control::GetMaximumSize()
607 const Vector3& GetMaximumSize() const;
610 * @copydoc Toolkit::Control::GetNaturalSize()
612 virtual Vector3 GetNaturalSize();
615 * @copydoc Toolkit::Control::GetHeightForWidth()
617 virtual float GetHeightForWidth( float width );
620 * @copydoc Toolkit::Control::GetWidthForHeight()
622 virtual float GetWidthForHeight( float height );
625 * @brief Retrieves the current Control's size.
627 * @return The control's size.
629 const Vector3& GetControlSize() const;
632 * @brief Retrieves the Control's size set by the Application / Control.
634 * @return The control's size.
636 const Vector3& GetSizeSet() const;
641 * @copydoc Toolkit::Control::SetKeyInputFocus()
643 void SetKeyInputFocus();
646 * @copydoc Toolkit::Control::HasKeyInputFocus()
648 bool HasKeyInputFocus();
651 * @copydoc Toolkit::Control::ClearKeyInputFocus()
653 void ClearKeyInputFocus();
656 * @copydoc ConnectionTrackerInterface::SignalConnected
658 virtual void SignalConnected( SlotObserver* slotObserver, CallbackBase* callback );
661 * @copydoc ConnectionTrackerInterface::SignalDisconnected
663 virtual void SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback );
668 * @brief Sends a request to be relaid-out.
670 * This method is called from OnStageConnection(), OnChildAdd(),
671 * OnChildRemove(), SetSizePolicy(), SetMinimumSize() and
674 * This method could also be called from derived classes every time
675 * a control's poperty change and it needs to be relaid-out. After
676 * the Dali::Stage::SignalMessageQueueFlushed() is emitted a
677 * relayout process starts and all controls which called this method
678 * will be relaid-out.
680 * @note RelayoutRequest() only sends a request per Control before
681 * the Dali::Stage::SignalMessageQueueFlushed() signal is
682 * emitted. That means a control will be relaid-out only once, even
683 * if more than one request is sent between two consecutive signals.
685 void RelayoutRequest();
688 * @brief Helper method for controls to Relayout their children if
689 * they do not know whether that child is a control or not.
691 * @param[in] actor The actor to relayout.
692 * @param[in] size The size to allocate to the actor.
693 * @param[in,out] container The container that holds actors that have not been allocated a size yet.
695 static void Relayout( Actor actor, Vector2 size, ActorSizeContainer& container );
697 private: // Used by the RelayoutController
700 * @brief Called by the RelayoutController to negotiate the size of a control.
702 * The size allocated by the the algorithm is passed in which the
703 * control must adhere to. A container is passed in as well which
704 * the control should populate with actors it has not / or does not
705 * need to handle in its size negotiation.
707 * @param[in] size The allocated size.
708 * @param[in,out] container The container that holds actors that are fed back into the
709 * RelayoutController algorithm.
711 DALI_INTERNAL void NegotiateSize( Vector2 size, ActorSizeContainer& container );
716 * @brief Called by NegotiateSize when the size to allocate to the control has been calculated.
718 * It calls the OnRelaidOut() method which can be overridden by derived classes.
720 * @param[in] size The allocated size.
721 * @param[in,out] container The control should add actors to this container that it is not able
722 * to allocate a size for.
724 DALI_INTERNAL void Relayout( Vector2 size, ActorSizeContainer& container );
727 * @brief Used by the KeyInputFocusManager to emit key event signals.
729 * @param[in] event The key event.
730 * @return True if the event was consumed.
732 DALI_INTERNAL bool EmitKeyEventSignal(const KeyEvent& event);
737 DALI_INTERNAL Control(const Control&);
738 DALI_INTERNAL Control& operator=(const Control&);
743 friend class Internal::RelayoutControllerImpl; ///< Relayout controller needs to call Relayout() which is private.
744 friend class Internal::KeyInputFocusManager; ///< KeyInputFocusManager needs to call which is private.
747 } // namespace Internal
749 } // namespace Toolkit
753 #endif // __DALI_TOOLKIT_CONTROL_IMPL_H__