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/common/vector-wrapper.h>
25 #include <dali-toolkit/public-api/controls/control.h>
27 namespace Dali DALI_IMPORT_API
35 namespace Internal DALI_INTERNAL
37 class RelayoutControllerImpl;
38 class KeyInputFocusManager;
41 typedef std::pair< Actor, Vector2 > ActorSizePair; ///< Pair of actor and size
42 typedef std::vector< ActorSizePair > ActorSizeContainer; ///< Container of actors and their sizes
48 * @brief This is the internal base class for all controls.
50 * It will provide some common functionality required by all controls.
51 * Implements ConnectionTrackerInterface so that signals (typically connected to member functions) will
52 * be disconnected automatically when the control is destroyed.
54 class DALI_IMPORT_API Control : public CustomActorImpl, public ConnectionTrackerInterface
61 CONTROL_PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX,
62 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
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 ConntrolImpl instance.
73 static Toolkit::Control New();
78 * @brief Virtual destructor.
85 * @brief This method should be overridden by deriving classes when they wish to be notified when they
88 virtual void OnActivated() { }
91 * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
94 * @param[in] gesture The pan gesture.
95 * @return true if the pan gesture has been consumed by this control
97 virtual bool OnAccessibilityPan(PanGesture gesture);
100 * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
103 * @param[in] touchEvent The touch event.
104 * @return true if the touch event has been consumed by this control
106 virtual bool OnAccessibilityTouch(const TouchEvent& touchEvent);
109 * @brief This method should be overridden by deriving classes when they wish to respond
110 * the accessibility up and down action (i.e. value change of slider control).
112 * @param[in] isIncrease Whether the value should be increased or decreased
113 * @return true if the value changed action has been consumed by this control
115 virtual bool OnAccessibilityValueChange(bool isIncrease);
118 * @brief Sets whether this control supports two dimensional
119 * keyboard navigation (i.e. whether it knows how to handle the
120 * keyboardn focus movement between its child actors).
122 * The control doesn't support it by default.
123 * @param[in] isSupported Whether this control supports two dimensional keyboard navigation.
125 void SetKeyboardNavigationSupport(bool isSupported);
128 * @brief Gets whether this control supports two dimensional keyboard navigation.
130 * @return true if this control supports two dimensional keyboard navigation.
132 bool IsKeyboardNavigationSupported();
135 * @brief Sets whether this control is a focus group for keyboard navigation.
137 * (i.e. the scope of keyboard focus movement
138 * can be limitied to its child actors). The control is not a focus group by default.
139 * @param[in] isFocusGroup Whether this control is set as a focus group for keyboard navigation.
141 void SetAsKeyboardFocusGroup(bool isFocusGroup);
144 * @brief Gets whether this control is a focus group for keyboard navigation.
146 * @return true if this control is set as a focus group for keyboard navigation.
148 bool IsKeyboardFocusGroup();
151 * @brief Gets the next keyboard focusable actor in this control towards the given direction.
153 * A control needs to override this function in order to support two dimensional keyboard navigation.
154 * @param[in] currentFocusedActor The current focused actor.
155 * @param[in] direction The direction to move the focus towards.
156 * @param[in] loopEnabled Whether the focus movement should be looped within the control.
157 * @return the next keyboard focusable actor in this control or an empty handle if no actor can be focused.
159 virtual Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
162 * @brief Informs this control that its chosen focusable actor will be focused.
164 * This allows the application to preform any actions if wishes
165 * before the focus is actually moved to the chosen actor.
167 * @param[in] commitedFocusableActor The commited focusable actor.
169 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor) { }
172 * @brief Performs actions as requested using the action name.
174 * @param[in] object The object on which to perform the action.
175 * @param[in] actionName The action to perform.
176 * @param[in] attributes The attributes with which to perfrom this action.
177 * @return true if action has been accepted by this control
179 static bool DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes);
182 * Connects a callback function with the object's signals.
183 * @param[in] object The object providing the signal.
184 * @param[in] tracker Used to disconnect the signal.
185 * @param[in] signalName The signal to connect to.
186 * @param[in] functor A newly allocated FunctorDelegate.
187 * @return True if the signal was connected.
188 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
190 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
193 * @brief If deriving classes wish to fine tune pinch gesture
194 * detection then they can access the gesture detector through this
195 * API and modify the detection.
197 * @return The pinch gesture detector.
198 * @pre Pinch detection should have been enabled via EnableGestureDetection().
199 * @see EnableGestureDetection
201 PinchGestureDetector GetPinchGestureDetector() const;
204 * @brief If deriving classes wish to fine tune pan gesture
205 * detection then they can access the gesture detector through this
206 * API and modify the detection.
208 * @return The pan gesture detector.
209 * @pre Pan detection should have been enabled via EnableGestureDetection().
210 * @see EnableGestureDetection
212 PanGestureDetector GetPanGestureDetector() const;
215 * @brief If deriving classes wish to fine tune tap gesture
216 * detection then they can access the gesture detector through this
217 * API and modify the detection.
219 * @return The tap gesture detector.
220 * @pre Tap detection should have been enabled via EnableGestureDetection().
221 * @see EnableGestureDetection
223 TapGestureDetector GetTapGestureDetector() const;
226 * @brief If deriving classes wish to fine tune long press gesture
227 * detection then they can access the gesture detector through this
228 * API and modify the detection.
230 * @return The long press gesture detector.
231 * @pre Long press detection should have been enabled via EnableGestureDetection().
232 * @see EnableGestureDetection
234 LongPressGestureDetector GetLongPressGestureDetector() const;
239 * @copydoc Dali::Toolkit::Control::SetBackgroundColor
241 void SetBackgroundColor( const Vector4& color );
244 * @copydoc Dali::Toolkit::Control::GetBackgroundColor
246 Vector4 GetBackgroundColor() const;
249 * @copydoc Dali::Toolkit::Control::SetBackground
251 void SetBackground( Image image );
254 * @copydoc Dali::Toolkit::Control::ClearBackground
256 void ClearBackground();
259 * @copydoc Dali::Toolkit::Control::GetBackgroundActor
261 Actor GetBackgroundActor() const;
266 * @copydoc Dali::Toolkit::Control::KeyEventSignal()
268 Toolkit::Control::KeyEventSignalV2& KeyEventSignal();
275 * @brief Second phase initialization.
282 * @brief Allows deriving classes to enable any of the gesture detectors that are available.
284 * Gesture detection can be enabled one at a time or in bitwise format as shown:
286 * EnableGestureDetection(Gesture::Type(Gesture::Pinch | Gesture::Tap | Gesture::Pan));
288 * @param[in] type The gesture type(s) to enable.
290 void EnableGestureDetection(Gesture::Type type);
293 * @brief Allows deriving classes to disable any of the gesture detectors.
295 * Like EnableGestureDetection, this can also be called using bitwise or.
296 * @param[in] type The gesture type(s) to disable.
297 * @see EnableGetureDetection
299 void DisableGestureDetection(Gesture::Type type);
301 private: // For derived classes to override
304 * @brief This method is called after the Control has been initialized.
306 * Derived classes should do any second phase initialization by
307 * overriding this method.
309 virtual void OnInitialize() { }
312 * @brief This method should be overridden by deriving classes when
313 * they wish to be notified when the style manager changes the theme.
315 * @param[in] styleManager The StyleManager object.
317 virtual void OnThemeChange( Toolkit::StyleManager styleManager );
320 * @brief This method should be overridden by deriving classes when
321 * they wish to be notified when the style changes the default font.
323 * @param[in] defaultFontChange Information denoting whether the default font has changed.
324 * @param[in] defaultFontSizeChange Information denoting whether the default font size has changed.
326 virtual void OnFontChange( bool defaultFontChange, bool defaultFontSizeChange ){ }
329 * @brief Called whenever a pinch gesture is detected on this control.
331 * This can be overridden by deriving classes when pinch detection
332 * is enabled. The default behaviour is to scale the control by the
335 * @note If overridden, then the default behaviour will not occur.
336 * @note Pinch detection should be enabled via EnableGestureDetection().
337 * @param[in] pinch The pinch gesture.
338 * @see EnableGestureDetection
340 virtual void OnPinch(PinchGesture pinch);
343 * @brief Called whenever a pan gesture is detected on this control.
345 * This should be overridden by deriving classes when pan detection
348 * @note There is no default behaviour with panning.
349 * @note Pan detection should be enabled via EnableGestureDetection().
350 * @param[in] pan The pan gesture.
351 * @see EnableGestureDetection
353 virtual void OnPan(PanGesture pan) { }
356 * @brief Called whenever a tap gesture is detected on this control.
358 * This should be overridden by deriving classes when tap detection
361 * @note There is no default behaviour with a tap.
362 * @note Tap detection should be enabled via EnableGestureDetection().
363 * @param[in] tap The tap gesture.
364 * @see EnableGestureDetection
366 virtual void OnTap(TapGesture tap) { }
369 * @brief Called whenever a long press gesture is detected on this control.
371 * This should be overridden by deriving classes when long press
372 * detection is enabled.
374 * @note There is no default behaviour associated with a long press.
375 * @note Long press detection should be enabled via EnableGestureDetection().
376 * @param[in] longPress The long press gesture.
377 * @see EnableGestureDetection
379 virtual void OnLongPress(LongPressGesture longPress) { }
382 * @brief Called whenever the control is added to the stage.
384 * Could be overridden by derived classes.
386 virtual void OnControlStageConnection() { }
389 * @brief Called whenever the control is removed from the stage.
391 * Could be overridden by derived classes.
393 virtual void OnControlStageDisconnection() { }
396 * @brief Called whenever an Actor is added to the control.
398 * Could be overridden by derived classes.
400 * @param[in] child The added actor.
402 virtual void OnControlChildAdd( Actor& child ) { }
405 * @brief Called whenever an Actor is removed from the control.
407 * Could be overridden by derived classes.
409 * @param[in] child The removed actor.
411 virtual void OnControlChildRemove( Actor& child ) { }
414 * @brief Called whenever the Control's size is set.
416 * Could be overridden by derived classes.
418 * @param[in] size The new size.
420 virtual void OnControlSizeSet( const Vector3& size ) { }
423 * @brief Called after the Dali::Stage::SignalMessageQueueFlushed()
424 * signal is emitted if this control requested to be relaid-out.
426 * Should be overridden by derived classes if they need to layout
427 * actors differently after certain operations like add or remove
428 * actors, resize or after changing especific properties.
430 * @param[in] size The allocated size.
431 * @param[in,out] container The control should add actors to this container that it is not able
432 * to allocate a size for.
434 virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
436 private: // From CustomActorImpl, derived classes can override these.
439 * @brief Sends a request to relayout this control.
441 * The control will be relaid out after the
442 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted.
444 * It calls OnControlStageConnection() to notify derived classes.
446 * @see Dali::CustomActorImpl::OnStageConnection()
448 virtual void OnStageConnection();
451 * @brief Calls OnControlStageDisconnection() to notify derived classed.
453 * @see Dali::CustomActorImpl::OnStageDisconnection()
455 virtual void OnStageDisconnection();
458 * @brief Sends a request to relayout this control.
460 * The control will be relaid out after the
461 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted. It
462 * calls OnControlChildAdd() to notify derived classes.
464 * @note This method shouldn't be overridden by derived classes.
466 * @param[in] child The added actor.
468 * @see Dali::CustomActorImpl::OnChildAdd(Actor&)
470 virtual void OnChildAdd(Actor& child);
473 * @brief Sends a request to relayout this control.
475 * The control will be relaid out after the
476 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted. It
477 * calls OnControlChildRemove() to notify derived classes.
479 * @note This method shouldn't be overridden by derived classes.
481 * @param[in] child The removed actor.
483 * @see Dali::CustomActorImpl::OnChildRemove(Actor&)
485 virtual void OnChildRemove(Actor& child);
488 * @brief It stores the size set by size negotiation and relayout.
490 * It also keeps a backup of the size set through the Actor's API used in the size negotiation.
491 * It calls the OnControlSizeSet() to notify derived classes.
493 * @param[in] targetSize The new size.
495 * @see Dali::CustomActorImpl::OnSizeSet(const Vector3&)
497 virtual void OnSizeSet(const Vector3& targetSize);
500 * @copydoc Dali::CustomActorImpl::OnSizeAnimation(Animation&, const Vector3&)
502 virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize);
505 * @copydoc Dali::CustomActorImpl::OnTouchEvent(const TouchEvent&)
507 virtual bool OnTouchEvent(const TouchEvent& event);
510 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
512 virtual bool OnKeyEvent(const KeyEvent& event);
515 * @copydoc Dali::CustomActorImpl::OnMouseWheelEvent(const MouseWheelEvent&)
517 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
520 * @copydoc Dali::CustomActorImpl::OnKeyInputFocusGained()
522 virtual void OnKeyInputFocusGained();
525 * @copydoc Dali::CustomActorImpl::OnKeyInputFocusLost()
527 virtual void OnKeyInputFocusLost();
530 * @copydoc Dali::CustomActorImpl::GetChildByAlias(const std::string& actorAlias)
532 virtual Actor GetChildByAlias(const std::string& actorAlias);
537 * @brief This method is the callback for the StyleChangeSignal from StyleManager
539 * @param[in] styleManager The StyleManager Object
540 * @param[in] change Information denoting what has changed.
542 void DoStyleChange( Toolkit::StyleManager styleManager, StyleChange change );
544 protected: // Construction
546 // Flags for the constructor
547 enum ControlBehaviour
549 CONTROL_BEHAVIOUR_NONE = 0,
550 REQUIRES_TOUCH_EVENTS = 1<<1, ///< True if the OnTouchEvent() callback is required.
551 REQUIRES_STYLE_CHANGE_SIGNALS = 1<<2, ///< True if needs to monitor style change signals such as theme/font change
552 NO_SIZE_NEGOTIATION = 1<<3 ///< True if control does not need size negotiation, i.e. it can be skipped in the algorithm
556 * @brief Create a Control.
558 * @param[in] behaviourFlags Behavioural flags from ControlBehaviour enum
560 Control(ControlBehaviour behaviourFlags);
567 * @copydoc Toolkit::Control::SetSizePolicy()
569 void SetSizePolicy( Toolkit::Control::SizePolicy widthPolicy, Toolkit::Control::SizePolicy heightPolicy );
572 * @copydoc Toolkit::Control::GetSizePolicy()
574 void GetSizePolicy( Toolkit::Control::SizePolicy& widthPolicy, Toolkit::Control::SizePolicy& heightPolicy ) const;
577 * @copydoc Toolkit::Control::SetMinimumSize()
579 void SetMinimumSize( const Vector3& size );
582 * @copydoc Toolkit::Control::GetMinimumSize()
584 const Vector3& GetMinimumSize() const;
587 * @copydoc Toolkit::Control::SetMaximumSize()
589 void SetMaximumSize( const Vector3& size );
592 * @copydoc Toolkit::Control::GetMaximumSize()
594 const Vector3& GetMaximumSize() const;
597 * @copydoc Toolkit::Control::GetNaturalSize()
599 virtual Vector3 GetNaturalSize();
602 * @copydoc Toolkit::Control::GetHeightForWidth()
604 virtual float GetHeightForWidth( float width );
607 * @copydoc Toolkit::Control::GetWidthForHeight()
609 virtual float GetWidthForHeight( float height );
612 * @brief Retrieves the current Control's size.
614 * @return The control's size.
616 const Vector3& GetControlSize() const;
619 * @brief Retrieves the Control's size set by the Application / Control.
621 * @return The control's size.
623 const Vector3& GetSizeSet() const;
628 * @copydoc Toolkit::Control::SetKeyInputFocus()
630 void SetKeyInputFocus();
633 * @copydoc Toolkit::Control::HasKeyInputFocus()
635 bool HasKeyInputFocus();
638 * @copydoc Toolkit::Control::ClearKeyInputFocus()
640 void ClearKeyInputFocus();
643 * @copydoc ConnectionTrackerInterface::SignalConnected
645 virtual void SignalConnected( SlotObserver* slotObserver, CallbackBase* callback );
648 * @copydoc ConnectionTrackerInterface::SignalDisconnected
650 virtual void SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback );
655 * @brief Sends a request to be relaid-out.
657 * This method is called from OnStageConnection(), OnChildAdd(),
658 * OnChildRemove(), SetSizePolicy(), SetMinimumSize() and
661 * This method could also be called from derived classes every time
662 * a control's poperty change and it needs to be relaid-out. After
663 * the Dali::Stage::SignalMessageQueueFlushed() is emitted a
664 * relayout process starts and all controls which called this method
665 * will be relaid-out.
667 * @note RelayoutRequest() only sends a request per Control before
668 * the Dali::Stage::SignalMessageQueueFlushed() signal is
669 * emitted. That means a control will be relaid-out only once, even
670 * if more than one request is sent between two consecutive signals.
672 void RelayoutRequest();
675 * @brief Helper method for controls to Relayout their children if
676 * they do not know whether that child is a control or not.
678 * @param[in] actor The actor to relayout.
679 * @param[in] size The size to allocate to the actor.
680 * @param[in,out] container The container that holds actors that have not been allocated a size yet.
682 static void Relayout( Actor actor, Vector2 size, ActorSizeContainer& container );
684 private: // Used by the RelayoutController
687 * @brief Called by the RelayoutController to negotiate the size of a control.
689 * The size allocated by the the algorithm is passed in which the
690 * control must adhere to. A container is passed in as well which
691 * the control should populate with actors it has not / or does not
692 * need to handle in its size negotiation.
694 * @param[in] size The allocated size.
695 * @param[in,out] container The container that holds actors that are fed back into the
696 * RelayoutController algorithm.
698 void NegotiateSize( Vector2 size, ActorSizeContainer& container );
703 * @brief Called by NegotiateSize when the size to allocate to the control has been calculated.
705 * It calls the OnRelaidOut() method which can be overridden by derived classes.
707 * @param[in] size The allocated size.
708 * @param[in,out] container The control should add actors to this container that it is not able
709 * to allocate a size for.
711 void Relayout( Vector2 size, ActorSizeContainer& container );
714 * @brief Used by the KeyInputFocusManager to emit key event signals.
716 * @param[in] event The key event.
717 * @return True if the event was consumed.
719 bool EmitKeyEventSignal(const KeyEvent& event);
724 Control(const Control&);
725 Control& operator=(const Control&);
730 friend class Internal::RelayoutControllerImpl; ///< Relayout controller needs to call Relayout() which is private.
731 friend class Internal::KeyInputFocusManager; ///< KeyInputFocusManager needs to call which is private.
734 } // namespace Internal
736 } // namespace Toolkit
740 #endif // __DALI_TOOLKIT_CONTROL_IMPL_H__