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 * @addtogroup CAPI_DALI_TOOLKIT_CONTROLS_MODULE
27 #include <dali/public-api/common/vector-wrapper.h>
30 #include <dali-toolkit/public-api/controls/control.h>
32 namespace Dali DALI_IMPORT_API
40 namespace Internal DALI_INTERNAL
42 class RelayoutControllerImpl;
43 class KeyInputFocusManager;
46 typedef std::pair< Actor, Vector2 > ActorSizePair; ///< Pair of actor and size
47 typedef std::vector< ActorSizePair > ActorSizeContainer; ///< Container of actors and their sizes
53 * @brief This is the internal base class for all controls.
55 * It will provide some common functionality required by all controls.
56 * Implements ConnectionTrackerInterface so that signals (typically connected to member functions) will
57 * be disconnected automatically when the control is destroyed.
59 class DALI_IMPORT_API Control : public CustomActorImpl, public ConnectionTrackerInterface
66 CONTROL_PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX,
67 CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
73 * @brief Create a new ControlImpl instance that does not require touch by default.
75 * If touch is required then the user can connect to this class' touch signal.
76 * @return A handle to the ConntrolImpl instance.
78 static Toolkit::Control New();
83 * @brief Virtual destructor.
90 * @brief This method should be overridden by deriving classes when they wish to be notified when they
93 virtual void OnActivated() { }
96 * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
99 * @param[in] gesture The pan gesture.
100 * @return true if the pan gesture has been consumed by this control
102 virtual bool OnAccessibilityPan(PanGesture gesture);
105 * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
108 * @param[in] touchEvent The touch event.
109 * @return true if the touch event has been consumed by this control
111 virtual bool OnAccessibilityTouch(const TouchEvent& touchEvent);
114 * @brief This method should be overridden by deriving classes when they wish to respond
115 * the accessibility up and down action (i.e. value change of slider control).
117 * @param[in] isIncrease Whether the value should be increased or decreased
118 * @return true if the value changed action has been consumed by this control
120 virtual bool OnAccessibilityValueChange(bool isIncrease);
123 * @brief Sets whether this control supports two dimensional
124 * keyboard navigation (i.e. whether it knows how to handle the
125 * keyboardn focus movement between its child actors).
127 * The control doesn't support it by default.
128 * @param[in] isSupported Whether this control supports two dimensional keyboard navigation.
130 void SetKeyboardNavigationSupport(bool isSupported);
133 * @brief Gets whether this control supports two dimensional keyboard navigation.
135 * @return true if this control supports two dimensional keyboard navigation.
137 bool IsKeyboardNavigationSupported();
140 * @brief Sets whether this control is a focus group for keyboard navigation.
142 * (i.e. the scope of keyboard focus movement
143 * can be limitied to its child actors). The control is not a focus group by default.
144 * @param[in] isFocusGroup Whether this control is set as a focus group for keyboard navigation.
146 void SetAsKeyboardFocusGroup(bool isFocusGroup);
149 * @brief Gets whether this control is a focus group for keyboard navigation.
151 * @return true if this control is set as a focus group for keyboard navigation.
153 bool IsKeyboardFocusGroup();
156 * @brief Gets the next keyboard focusable actor in this control towards the given direction.
158 * A control needs to override this function in order to support two dimensional keyboard navigation.
159 * @param[in] currentFocusedActor The current focused actor.
160 * @param[in] direction The direction to move the focus towards.
161 * @param[in] loopEnabled Whether the focus movement should be looped within the control.
162 * @return the next keyboard focusable actor in this control or an empty handle if no actor can be focused.
164 virtual Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
167 * @brief Informs this control that its chosen focusable actor will be focused.
169 * This allows the application to preform any actions if wishes
170 * before the focus is actually moved to the chosen actor.
172 * @param[in] commitedFocusableActor The commited focusable actor.
174 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor) { }
177 * @brief Performs actions as requested using the action name.
179 * @param[in] object The object on which to perform the action.
180 * @param[in] actionName The action to perform.
181 * @param[in] attributes The attributes with which to perfrom this action.
182 * @return true if action has been accepted by this control
184 static bool DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes);
187 * @brief If deriving classes wish to fine tune pinch gesture
188 * detection then they can access the gesture detector through this
189 * API and modify the detection.
191 * @return The pinch gesture detector.
192 * @pre Pinch detection should have been enabled via EnableGestureDetection().
193 * @see EnableGestureDetection
195 PinchGestureDetector GetPinchGestureDetector() const;
198 * @brief If deriving classes wish to fine tune pan gesture
199 * detection then they can access the gesture detector through this
200 * API and modify the detection.
202 * @return The pan gesture detector.
203 * @pre Pan detection should have been enabled via EnableGestureDetection().
204 * @see EnableGestureDetection
206 PanGestureDetector GetPanGestureDetector() const;
209 * @brief If deriving classes wish to fine tune tap gesture
210 * detection then they can access the gesture detector through this
211 * API and modify the detection.
213 * @return The tap gesture detector.
214 * @pre Tap detection should have been enabled via EnableGestureDetection().
215 * @see EnableGestureDetection
217 TapGestureDetector GetTapGestureDetector() const;
220 * @brief If deriving classes wish to fine tune long press gesture
221 * detection then they can access the gesture detector through this
222 * API and modify the detection.
224 * @return The long press gesture detector.
225 * @pre Long press detection should have been enabled via EnableGestureDetection().
226 * @see EnableGestureDetection
228 LongPressGestureDetector GetLongPressGestureDetector() const;
233 * @copydoc Dali::Toolkit::Control::SetBackgroundColor
235 void SetBackgroundColor( const Vector4& color );
238 * @copydoc Dali::Toolkit::Control::GetBackgroundColor
240 Vector4 GetBackgroundColor() const;
243 * @copydoc Dali::Toolkit::Control::SetBackground
245 void SetBackground( Image image );
248 * @copydoc Dali::Toolkit::Control::ClearBackground
250 void ClearBackground();
253 * @copydoc Dali::Toolkit::Control::GetBackgroundActor
255 Actor GetBackgroundActor() const;
260 * @copydoc Dali::Toolkit::Control::KeyEventSignal()
262 Toolkit::Control::KeyEventSignalV2& KeyEventSignal();
269 * @brief Second phase initialization.
276 * @brief Allows deriving classes to enable any of the gesture detectors that are available.
278 * Gesture detection can be enabled one at a time or in bitwise format as shown:
280 * EnableGestureDetection(Gesture::Type(Gesture::Pinch | Gesture::Tap | Gesture::Pan));
282 * @param[in] type The gesture type(s) to enable.
284 void EnableGestureDetection(Gesture::Type type);
287 * @brief Allows deriving classes to disable any of the gesture detectors.
289 * Like EnableGestureDetection, this can also be called using bitwise or.
290 * @param[in] type The gesture type(s) to disable.
291 * @see EnableGetureDetection
293 void DisableGestureDetection(Gesture::Type type);
295 private: // For derived classes to override
298 * @brief This method is called after the Control has been initialized.
300 * Derived classes should do any second phase initialization by
301 * overriding this method.
303 virtual void OnInitialize() { }
306 * @brief This method should be overridden by deriving classes when
307 * they wish to be notified when the style manager changes the theme.
309 * @param[in] styleManager The StyleManager object.
311 virtual void OnThemeChange( Toolkit::StyleManager styleManager );
314 * @brief This method should be overridden by deriving classes when
315 * they wish to be notified when the style changes the default font.
317 * @param[in] defaultFontChange Information denoting whether the default font has changed.
318 * @param[in] defaultFontSizeChange Information denoting whether the default font size has changed.
320 virtual void OnFontChange( bool defaultFontChange, bool defaultFontSizeChange ){ }
323 * @deprecated Use OnFontChange() instead.
324 * Before the using of StyleManager, the StyleChange only deals with font change.
326 * @brief This method should be overridden by deriving classes when
327 * they wish to be notified when the style changes.
329 * @param[in] change Information denoting what has changed.
331 virtual void OnStyleChange( StyleChange change ) { }
334 * @brief Called whenever a pinch gesture is detected on this control.
336 * This can be overridden by deriving classes when pinch detection
337 * is enabled. The default behaviour is to scale the control by the
340 * @note If overridden, then the default behaviour will not occur.
341 * @note Pinch detection should be enabled via EnableGestureDetection().
342 * @param[in] pinch The pinch gesture.
343 * @see EnableGestureDetection
345 virtual void OnPinch(PinchGesture pinch);
348 * @brief Called whenever a pan gesture is detected on this control.
350 * This should be overridden by deriving classes when pan detection
353 * @note There is no default behaviour with panning.
354 * @note Pan detection should be enabled via EnableGestureDetection().
355 * @param[in] pan The pan gesture.
356 * @see EnableGestureDetection
358 virtual void OnPan(PanGesture pan) { }
361 * @brief Called whenever a tap gesture is detected on this control.
363 * This should be overridden by deriving classes when tap detection
366 * @note There is no default behaviour with a tap.
367 * @note Tap detection should be enabled via EnableGestureDetection().
368 * @param[in] tap The tap gesture.
369 * @see EnableGestureDetection
371 virtual void OnTap(TapGesture tap) { }
374 * @brief Called whenever a long press gesture is detected on this control.
376 * This should be overridden by deriving classes when long press
377 * detection is enabled.
379 * @note There is no default behaviour associated with a long press.
380 * @note Long press detection should be enabled via EnableGestureDetection().
381 * @param[in] longPress The long press gesture.
382 * @see EnableGestureDetection
384 virtual void OnLongPress(LongPressGesture longPress) { }
387 * @brief Called whenever the control is added to the stage.
389 * Could be overridden by derived classes.
391 virtual void OnControlStageConnection() { }
394 * @brief Called whenever the control is removed from the stage.
396 * Could be overridden by derived classes.
398 virtual void OnControlStageDisconnection() { }
401 * @brief Called whenever an Actor is added to the control.
403 * Could be overridden by derived classes.
405 * @param[in] child The added actor.
407 virtual void OnControlChildAdd( Actor& child ) { }
410 * @brief Called whenever an Actor is removed from the control.
412 * Could be overridden by derived classes.
414 * @param[in] child The removed actor.
416 virtual void OnControlChildRemove( Actor& child ) { }
419 * @brief Called whenever the Control's size is set.
421 * Could be overridden by derived classes.
423 * @param[in] size The new size.
425 virtual void OnControlSizeSet( const Vector3& size ) { }
428 * @brief Called after the Dali::Stage::SignalMessageQueueFlushed()
429 * signal is emitted if this control requested to be relaid-out.
431 * Should be overridden by derived classes if they need to layout
432 * actors differently after certain operations like add or remove
433 * actors, resize or after changing especific properties.
435 * @param[in] size The allocated size.
436 * @param[in,out] container The control should add actors to this container that it is not able
437 * to allocate a size for.
439 virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
441 private: // From CustomActorImpl, derived classes can override these.
444 * @brief Sends a request to relayout this control.
446 * The control will be relaid out after the
447 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted.
449 * It calls OnControlStageConnection() to notify derived classes.
451 * @see Dali::CustomActorImpl::OnStageConnection()
453 virtual void OnStageConnection();
456 * @brief Calls OnControlStageDisconnection() to notify derived classed.
458 * @see Dali::CustomActorImpl::OnStageDisconnection()
460 virtual void OnStageDisconnection();
463 * @brief Sends a request to relayout this control.
465 * The control will be relaid out after the
466 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted. It
467 * calls OnControlChildAdd() to notify derived classes.
469 * @note This method shouldn't be overridden by derived classes.
471 * @param[in] child The added actor.
473 * @see Dali::CustomActorImpl::OnChildAdd(Actor&)
475 virtual void OnChildAdd(Actor& child);
478 * @brief Sends a request to relayout this control.
480 * The control will be relaid out after the
481 * Dali::Stage::SignalMessageQueueFlushed() signal is emitted. It
482 * calls OnControlChildRemove() to notify derived classes.
484 * @note This method shouldn't be overridden by derived classes.
486 * @param[in] child The removed actor.
488 * @see Dali::CustomActorImpl::OnChildRemove(Actor&)
490 virtual void OnChildRemove(Actor& child);
493 * @brief It stores the size set by size negotiation and relayout.
495 * It also keeps a backup of the size set through the Actor's API used in the size negotiation.
496 * It calls the OnControlSizeSet() to notify derived classes.
498 * @param[in] targetSize The new size.
500 * @see Dali::CustomActorImpl::OnSizeSet(const Vector3&)
502 virtual void OnSizeSet(const Vector3& targetSize);
505 * @copydoc Dali::CustomActorImpl::OnSizeAnimation(Animation&, const Vector3&)
507 virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize);
510 * @copydoc Dali::CustomActorImpl::OnTouchEvent(const TouchEvent&)
512 virtual bool OnTouchEvent(const TouchEvent& event);
515 * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
517 virtual bool OnKeyEvent(const KeyEvent& event);
520 * @copydoc Dali::CustomActorImpl::OnMouseWheelEvent(const MouseWheelEvent&)
522 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
525 * @copydoc Dali::CustomActorImpl::OnKeyInputFocusGained()
527 virtual void OnKeyInputFocusGained();
530 * @copydoc Dali::CustomActorImpl::OnKeyInputFocusLost()
532 virtual void OnKeyInputFocusLost();
535 * @copydoc Dali::CustomActorImpl::GetChildByAlias(const std::string& actorAlias)
537 virtual Actor GetChildByAlias(const std::string& actorAlias);
542 * @brief This method is the callback for the StyleChangeSignal from StyleManager
544 * @param[in] styleManager The StyleManager Object
545 * @param[in] change Information denoting what has changed.
547 void DoStyleChange( Toolkit::StyleManager styleManager, StyleChange change );
549 protected: // Construction
551 // Flags for the constructor
552 enum ControlBehaviour
554 CONTROL_BEHAVIOUR_NONE = 0,
555 REQUIRES_TOUCH_EVENTS = 1<<1, ///< True if the OnTouchEvent() callback is required.
556 REQUIRES_STYLE_CHANGE_SIGNALS = 1<<2, ///< True if needs to monitor style change signals such as theme/font change
557 NO_SIZE_NEGOTIATION = 1<<3 ///< True if control does not need size negotiation, i.e. it can be skipped in the algorithm
561 * @deprecated Use the constructor taking flags instead
562 * @brief Create a Control.
564 * @param[in] requiresTouchEvents True if the OnTouchEvent() callback is required.
566 Control(bool requiresTouchEvents);
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 );
666 * @copydoc ConnectionTrackerInterface::GetConnectionCount
668 virtual std::size_t GetConnectionCount() const;
673 * @brief Sends a request to be relaid-out.
675 * This method is called from OnStageConnection(), OnChildAdd(),
676 * OnChildRemove(), SetSizePolicy(), SetMinimumSize() and
679 * This method could also be called from derived classes every time
680 * a control's poperty change and it needs to be relaid-out. After
681 * the Dali::Stage::SignalMessageQueueFlushed() is emitted a
682 * relayout process starts and all controls which called this method
683 * will be relaid-out.
685 * @note RelayoutRequest() only sends a request per Control before
686 * the Dali::Stage::SignalMessageQueueFlushed() signal is
687 * emitted. That means a control will be relaid-out only once, even
688 * if more than one request is sent between two consecutive signals.
690 void RelayoutRequest();
693 * @brief Helper method for controls to Relayout their children if
694 * they do not know whether that child is a control or not.
696 * @param[in] actor The actor to relayout.
697 * @param[in] size The size to allocate to the actor.
698 * @param[in,out] container The container that holds actors that have not been allocated a size yet.
700 static void Relayout( Actor actor, Vector2 size, ActorSizeContainer& container );
702 private: // Used by the RelayoutController
705 * @brief Called by the RelayoutController to negotiate the size of a control.
707 * The size allocated by the the algorithm is passed in which the
708 * control must adhere to. A container is passed in as well which
709 * the control should populate with actors it has not / or does not
710 * need to handle in its size negotiation.
712 * @param[in] size The allocated size.
713 * @param[in,out] container The container that holds actors that are fed back into the
714 * RelayoutController algorithm.
716 void NegotiateSize( Vector2 size, ActorSizeContainer& container );
721 * @brief Called by NegotiateSize when the size to allocate to the control has been calculated.
723 * It calls the OnRelaidOut() method which can be overridden by derived classes.
725 * @param[in] size The allocated size.
726 * @param[in,out] container The control should add actors to this container that it is not able
727 * to allocate a size for.
729 void Relayout( Vector2 size, ActorSizeContainer& container );
732 * @brief Used by the KeyInputFocusManager to emit key event signals.
734 * @param[in] event The key event.
735 * @return True if the event was consumed.
737 bool EmitKeyEventSignal(const KeyEvent& event);
742 Control(const Control&);
743 Control& operator=(const Control&);
748 friend class Internal::RelayoutControllerImpl; ///< Relayout controller needs to call Relayout() which is private.
749 friend class Internal::KeyInputFocusManager; ///< KeyInputFocusManager needs to call which is private.
752 } // namespace Internal
754 typedef Internal::Control ControlImpl; ///< @deprecated, Use Internal::Control
756 } // namespace Toolkit
763 #endif // __DALI_TOOLKIT_CONTROL_IMPL_H__