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>
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
68 CONTROL_PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX,
69 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
75 * @brief Create a new ControlImpl instance that does not require touch by default.
77 * If touch is required then the user can connect to this class' touch signal.
78 * @return A handle to the ConntrolImpl instance.
80 static Toolkit::Control New();
85 * @brief Virtual destructor.
92 * @brief This method should be overridden by deriving classes when they wish to be notified when they
95 virtual void OnActivated() { }
98 * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
101 * @param[in] gesture The pan gesture.
102 * @return true if the pan gesture has been consumed by this control
104 virtual bool OnAccessibilityPan(PanGesture gesture);
107 * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
110 * @param[in] touchEvent The touch event.
111 * @return true if the touch event has been consumed by this control
113 virtual bool OnAccessibilityTouch(const TouchEvent& touchEvent);
116 * @brief This method should be overridden by deriving classes when they wish to respond
117 * the accessibility up and down action (i.e. value change of slider control).
119 * @param[in] isIncrease Whether the value should be increased or decreased
120 * @return true if the value changed action has been consumed by this control
122 virtual bool OnAccessibilityValueChange(bool isIncrease);
125 * @brief Sets whether this control supports two dimensional
126 * keyboard navigation (i.e. whether it knows how to handle the
127 * keyboardn focus movement between its child actors).
129 * The control doesn't support it by default.
130 * @param[in] isSupported Whether this control supports two dimensional keyboard navigation.
132 void SetKeyboardNavigationSupport(bool isSupported);
135 * @brief Gets whether this control supports two dimensional keyboard navigation.
137 * @return true if this control supports two dimensional keyboard navigation.
139 bool IsKeyboardNavigationSupported();
142 * @brief Sets whether this control is a focus group for keyboard navigation.
144 * (i.e. the scope of keyboard focus movement
145 * can be limitied to its child actors). The control is not a focus group by default.
146 * @param[in] isFocusGroup Whether this control is set as a focus group for keyboard navigation.
148 void SetAsKeyboardFocusGroup(bool isFocusGroup);
151 * @brief Gets whether this control is a focus group for keyboard navigation.
153 * @return true if this control is set as a focus group for keyboard navigation.
155 bool IsKeyboardFocusGroup();
158 * @brief Gets the next keyboard focusable actor in this control towards the given direction.
160 * A control needs to override this function in order to support two dimensional keyboard navigation.
161 * @param[in] currentFocusedActor The current focused actor.
162 * @param[in] direction The direction to move the focus towards.
163 * @param[in] loopEnabled Whether the focus movement should be looped within the control.
164 * @return the next keyboard focusable actor in this control or an empty handle if no actor can be focused.
166 virtual Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
169 * @brief Informs this control that its chosen focusable actor will be focused.
171 * This allows the application to preform any actions if wishes
172 * before the focus is actually moved to the chosen actor.
174 * @param[in] commitedFocusableActor The commited focusable actor.
176 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor) { }
179 * @brief Performs actions as requested using the action name.
181 * @param[in] object The object on which to perform the action.
182 * @param[in] actionName The action to perform.
183 * @param[in] attributes The attributes with which to perfrom this action.
184 * @return true if action has been accepted by this control
186 static bool DoAction(BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes);
189 * Connects a callback function with the object's signals.
190 * @param[in] object The object providing the signal.
191 * @param[in] tracker Used to disconnect the signal.
192 * @param[in] signalName The signal to connect to.
193 * @param[in] functor A newly allocated FunctorDelegate.
194 * @return True if the signal was connected.
195 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
197 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
200 * @brief If deriving classes wish to fine tune pinch gesture
201 * detection then they can access the gesture detector through this
202 * API and modify the detection.
204 * @return The pinch gesture detector.
205 * @pre Pinch detection should have been enabled via EnableGestureDetection().
206 * @see EnableGestureDetection
208 PinchGestureDetector GetPinchGestureDetector() const;
211 * @brief If deriving classes wish to fine tune pan gesture
212 * detection then they can access the gesture detector through this
213 * API and modify the detection.
215 * @return The pan gesture detector.
216 * @pre Pan detection should have been enabled via EnableGestureDetection().
217 * @see EnableGestureDetection
219 PanGestureDetector GetPanGestureDetector() const;
222 * @brief If deriving classes wish to fine tune tap gesture
223 * detection then they can access the gesture detector through this
224 * API and modify the detection.
226 * @return The tap gesture detector.
227 * @pre Tap detection should have been enabled via EnableGestureDetection().
228 * @see EnableGestureDetection
230 TapGestureDetector GetTapGestureDetector() const;
233 * @brief If deriving classes wish to fine tune long press gesture
234 * detection then they can access the gesture detector through this
235 * API and modify the detection.
237 * @return The long press gesture detector.
238 * @pre Long press detection should have been enabled via EnableGestureDetection().
239 * @see EnableGestureDetection
241 LongPressGestureDetector GetLongPressGestureDetector() const;
246 * @copydoc Dali::Toolkit::Control::SetBackgroundColor
248 void SetBackgroundColor( const Vector4& color );
251 * @copydoc Dali::Toolkit::Control::GetBackgroundColor
253 Vector4 GetBackgroundColor() const;
256 * @copydoc Dali::Toolkit::Control::SetBackground
258 void SetBackground( Image image );
261 * @copydoc Dali::Toolkit::Control::ClearBackground
263 void ClearBackground();
266 * @copydoc Dali::Toolkit::Control::GetBackgroundActor
268 Actor GetBackgroundActor() const;
273 * @copydoc Dali::Toolkit::Control::KeyEventSignal()
275 Toolkit::Control::KeyEventSignalV2& KeyEventSignal();
282 * @brief Second phase initialization.
289 * @brief Allows deriving classes to enable any of the gesture detectors that are available.
291 * Gesture detection can be enabled one at a time or in bitwise format as shown:
293 * EnableGestureDetection(Gesture::Type(Gesture::Pinch | Gesture::Tap | Gesture::Pan));
295 * @param[in] type The gesture type(s) to enable.
297 void EnableGestureDetection(Gesture::Type type);
300 * @brief Allows deriving classes to disable any of the gesture detectors.
302 * Like EnableGestureDetection, this can also be called using bitwise or.
303 * @param[in] type The gesture type(s) to disable.
304 * @see EnableGetureDetection
306 void DisableGestureDetection(Gesture::Type type);
308 private: // For derived classes to override
311 * @brief This method is called after the Control has been initialized.
313 * Derived classes should do any second phase initialization by
314 * overriding this method.
316 virtual void OnInitialize() { }
319 * @brief This method should be overridden by deriving classes when
320 * they wish to be notified when the style manager changes the theme.
322 * @param[in] styleManager The StyleManager object.
324 virtual void OnThemeChange( Toolkit::StyleManager styleManager );
327 * @brief This method should be overridden by deriving classes when
328 * they wish to be notified when the style changes the default font.
330 * @param[in] defaultFontChange Information denoting whether the default font has changed.
331 * @param[in] defaultFontSizeChange Information denoting whether the default font size has changed.
333 virtual void OnFontChange( bool defaultFontChange, bool defaultFontSizeChange ){ }
336 * @brief Called whenever a pinch gesture is detected on this control.
338 * This can be overridden by deriving classes when pinch detection
339 * is enabled. The default behaviour is to scale the control by the
342 * @note If overridden, then the default behaviour will not occur.
343 * @note Pinch detection should be enabled via EnableGestureDetection().
344 * @param[in] pinch The pinch gesture.
345 * @see EnableGestureDetection
347 virtual void OnPinch(const PinchGesture& pinch);
350 * @brief Called whenever a pan gesture is detected on this control.
352 * This should be overridden by deriving classes when pan detection
355 * @note There is no default behaviour with panning.
356 * @note Pan detection should be enabled via EnableGestureDetection().
357 * @param[in] pan The pan gesture.
358 * @see EnableGestureDetection
360 virtual void OnPan( const PanGesture& pan );
363 * @brief Called whenever a tap gesture is detected on this control.
365 * This should be overridden by deriving classes when tap detection
368 * @note There is no default behaviour with a tap.
369 * @note Tap detection should be enabled via EnableGestureDetection().
370 * @param[in] tap The tap gesture.
371 * @see EnableGestureDetection
373 virtual void OnTap( const TapGesture& tap );
376 * @brief Called whenever a long press gesture is detected on this control.
378 * This should be overridden by deriving classes when long press
379 * detection is enabled.
381 * @note There is no default behaviour associated with a long press.
382 * @note Long press detection should be enabled via EnableGestureDetection().
383 * @param[in] longPress The long press gesture.
384 * @see EnableGestureDetection
386 virtual void OnLongPress( const LongPressGesture& longPress );
389 * @brief Called whenever the control is added to the stage.
391 * Could be overridden by derived classes.
393 virtual void OnControlStageConnection() { }
396 * @brief Called whenever the control is removed from the stage.
398 * Could be overridden by derived classes.
400 virtual void OnControlStageDisconnection() { }
403 * @brief Called whenever an Actor is added to the control.
405 * Could be overridden by derived classes.
407 * @param[in] child The added actor.
409 virtual void OnControlChildAdd( Actor& child ) { }
412 * @brief Called whenever an Actor is removed from the control.
414 * Could be overridden by derived classes.
416 * @param[in] child The removed actor.
418 virtual void OnControlChildRemove( Actor& child ) { }
421 * @brief Called whenever the Control's size is set.
423 * Could be overridden by derived classes.
425 * @param[in] size The new size.
427 virtual void OnControlSizeSet( const Vector3& size ) { }
430 * @brief Called after the Dali::Stage::SignalMessageQueueFlushed()
431 * signal is emitted if this control requested to be relaid-out.
433 * Should be overridden by derived classes if they need to layout
434 * actors differently after certain operations like add or remove
435 * actors, resize or after changing especific properties.
437 * @param[in] size The allocated size.
438 * @param[in,out] container The control should add actors to this container that it is not able
439 * to allocate a size for.
441 virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
443 private: // From CustomActorImpl, derived classes can override these.
446 * @brief Sends a request to relayout this control.
448 * The control will be relaid out after the
449 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted.
451 * It calls OnControlStageConnection() to notify derived classes.
453 * @see Dali::CustomActorImpl::OnStageConnection()
455 virtual void OnStageConnection();
458 * @brief Calls OnControlStageDisconnection() to notify derived classed.
460 * @see Dali::CustomActorImpl::OnStageDisconnection()
462 virtual void OnStageDisconnection();
465 * @brief Sends a request to relayout this control.
467 * The control will be relaid out after the
468 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted. It
469 * calls OnControlChildAdd() to notify derived classes.
471 * @note This method shouldn't be overridden by derived classes.
473 * @param[in] child The added actor.
475 * @see Dali::CustomActorImpl::OnChildAdd(Actor&)
477 virtual void OnChildAdd(Actor& child);
480 * @brief Sends a request to relayout this control.
482 * The control will be relaid out after the
483 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted. It
484 * calls OnControlChildRemove() to notify derived classes.
486 * @note This method shouldn't be overridden by derived classes.
488 * @param[in] child The removed actor.
490 * @see Dali::CustomActorImpl::OnChildRemove(Actor&)
492 virtual void OnChildRemove(Actor& child);
495 * @brief It stores the size set by size negotiation and relayout.
497 * It also keeps a backup of the size set through the Actor's API used in the size negotiation.
498 * It calls the OnControlSizeSet() to notify derived classes.
500 * @param[in] targetSize The new size.
502 * @see Dali::CustomActorImpl::OnSizeSet(const Vector3&)
504 virtual void OnSizeSet(const Vector3& targetSize);
507 * @copydoc Dali::CustomActorImpl::OnSizeAnimation(Animation&, const Vector3&)
509 virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize);
512 * @copydoc Dali::CustomActorImpl::OnTouchEvent(const TouchEvent&)
514 virtual bool OnTouchEvent(const TouchEvent& event);
517 * @copydoc Dali::CustomActorImpl::OnHoverEvent(const HoverEvent&)
519 virtual bool OnHoverEvent(const HoverEvent& event);
522 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
524 virtual bool OnKeyEvent(const KeyEvent& event);
527 * @copydoc Dali::CustomActorImpl::OnMouseWheelEvent(const MouseWheelEvent&)
529 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
532 * @copydoc Dali::CustomActorImpl::OnKeyInputFocusGained()
534 virtual void OnKeyInputFocusGained();
537 * @copydoc Dali::CustomActorImpl::OnKeyInputFocusLost()
539 virtual void OnKeyInputFocusLost();
542 * @copydoc Dali::CustomActorImpl::GetChildByAlias(const std::string& actorAlias)
544 virtual Actor GetChildByAlias(const std::string& actorAlias);
549 * @brief This method is the callback for the StyleChangeSignal from StyleManager
551 * @param[in] styleManager The StyleManager Object
552 * @param[in] change Information denoting what has changed.
554 DALI_INTERNAL void DoStyleChange( Toolkit::StyleManager styleManager, StyleChange change );
556 protected: // Construction
558 // Flags for the constructor
559 enum ControlBehaviour
561 CONTROL_BEHAVIOUR_NONE = 0,
562 REQUIRES_TOUCH_EVENTS = 1<<1, ///< True if the OnTouchEvent() callback is required.
563 REQUIRES_STYLE_CHANGE_SIGNALS = 1<<2, ///< True if needs to monitor style change signals such as theme/font change
564 NO_SIZE_NEGOTIATION = 1<<3, ///< True if control does not need size negotiation, i.e. it can be skipped in the algorithm
565 REQUIRES_HOVER_EVENTS = 1<<4, ///< True if the OnHoverEvent() callback is required.
566 REQUIRES_MOUSE_WHEEL_EVENTS = 1<<5 ///< True if the OnMouseWheelEvent() callback is required.
570 * @brief Create a Control.
572 * @param[in] behaviourFlags Behavioural flags from ControlBehaviour enum
574 Control(ControlBehaviour behaviourFlags);
581 * @copydoc Toolkit::Control::SetSizePolicy()
583 void SetSizePolicy( Toolkit::Control::SizePolicy widthPolicy, Toolkit::Control::SizePolicy heightPolicy );
586 * @copydoc Toolkit::Control::GetSizePolicy()
588 void GetSizePolicy( Toolkit::Control::SizePolicy& widthPolicy, Toolkit::Control::SizePolicy& heightPolicy ) const;
591 * @copydoc Toolkit::Control::SetMinimumSize()
593 void SetMinimumSize( const Vector3& size );
596 * @copydoc Toolkit::Control::GetMinimumSize()
598 const Vector3& GetMinimumSize() const;
601 * @copydoc Toolkit::Control::SetMaximumSize()
603 void SetMaximumSize( const Vector3& size );
606 * @copydoc Toolkit::Control::GetMaximumSize()
608 const Vector3& GetMaximumSize() const;
611 * @copydoc Toolkit::Control::GetNaturalSize()
613 virtual Vector3 GetNaturalSize();
616 * @copydoc Toolkit::Control::GetHeightForWidth()
618 virtual float GetHeightForWidth( float width );
621 * @copydoc Toolkit::Control::GetWidthForHeight()
623 virtual float GetWidthForHeight( float height );
626 * @brief Retrieves the current Control's size.
628 * @return The control's size.
630 const Vector3& GetControlSize() const;
633 * @brief Retrieves the Control's size set by the Application / Control.
635 * @return The control's size.
637 const Vector3& GetSizeSet() const;
642 * @copydoc Toolkit::Control::SetKeyInputFocus()
644 void SetKeyInputFocus();
647 * @copydoc Toolkit::Control::HasKeyInputFocus()
649 bool HasKeyInputFocus();
652 * @copydoc Toolkit::Control::ClearKeyInputFocus()
654 void ClearKeyInputFocus();
657 * @copydoc ConnectionTrackerInterface::SignalConnected
659 virtual void SignalConnected( SlotObserver* slotObserver, CallbackBase* callback );
662 * @copydoc ConnectionTrackerInterface::SignalDisconnected
664 virtual void SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback );
669 * @brief Sends a request to be relaid-out.
671 * This method is called from OnStageConnection(), OnChildAdd(),
672 * OnChildRemove(), SetSizePolicy(), SetMinimumSize() and
675 * This method could also be called from derived classes every time
676 * a control's poperty change and it needs to be relaid-out. After
677 * the Dali::Stage::SignalMessageQueueFlushed() is emitted a
678 * relayout process starts and all controls which called this method
679 * will be relaid-out.
681 * @note RelayoutRequest() only sends a request per Control before
682 * the Dali::Stage::SignalMessageQueueFlushed() signal is
683 * emitted. That means a control will be relaid-out only once, even
684 * if more than one request is sent between two consecutive signals.
686 void RelayoutRequest();
689 * @brief Helper method for controls to Relayout their children if
690 * they do not know whether that child is a control or not.
692 * @param[in] actor The actor to relayout.
693 * @param[in] size The size to allocate to the actor.
694 * @param[in,out] container The container that holds actors that have not been allocated a size yet.
696 static void Relayout( Actor actor, Vector2 size, ActorSizeContainer& container );
698 private: // Used by the RelayoutController
701 * @brief Called by the RelayoutController to negotiate the size of a control.
703 * The size allocated by the the algorithm is passed in which the
704 * control must adhere to. A container is passed in as well which
705 * the control should populate with actors it has not / or does not
706 * need to handle in its size negotiation.
708 * @param[in] size The allocated size.
709 * @param[in,out] container The container that holds actors that are fed back into the
710 * RelayoutController algorithm.
712 DALI_INTERNAL void NegotiateSize( Vector2 size, ActorSizeContainer& container );
717 * @brief Called by NegotiateSize when the size to allocate to the control has been calculated.
719 * It calls the OnRelaidOut() method which can be overridden by derived classes.
721 * @param[in] size The allocated size.
722 * @param[in,out] container The control should add actors to this container that it is not able
723 * to allocate a size for.
725 DALI_INTERNAL void Relayout( Vector2 size, ActorSizeContainer& container );
728 * @brief Used by the KeyInputFocusManager to emit key event signals.
730 * @param[in] event The key event.
731 * @return True if the event was consumed.
733 DALI_INTERNAL bool EmitKeyEventSignal(const KeyEvent& event);
738 DALI_INTERNAL Control(const Control&);
739 DALI_INTERNAL Control& operator=(const Control&);
744 friend class Internal::RelayoutControllerImpl; ///< Relayout controller needs to call Relayout() which is private.
745 friend class Internal::KeyInputFocusManager; ///< KeyInputFocusManager needs to call which is private.
748 } // namespace Internal
750 } // namespace Toolkit
754 #endif // __DALI_TOOLKIT_CONTROL_IMPL_H__