b3d542f406b103ebe9dca182b77f0603793c7b9a
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / control-impl.h
1 #ifndef DALI_TOOLKIT_CONTROL_IMPL_H
2 #define DALI_TOOLKIT_CONTROL_IMPL_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/adaptor-framework/style-change.h>
23 #include <dali/public-api/animation/alpha-function.h>
24 #include <dali/public-api/animation/time-period.h>
25 #include <dali/public-api/events/long-press-gesture.h>
26 #include <dali/public-api/events/pan-gesture.h>
27 #include <dali/public-api/events/pinch-gesture.h>
28 #include <dali/public-api/events/tap-gesture.h>
29 #include <dali/public-api/object/property-index-ranges.h>
30 #include <dali/public-api/object/type-info.h>
31
32 // INTERNAL INCLUDES
33 #include <dali-toolkit/public-api/controls/control.h>
34
35 namespace Dali
36 {
37 namespace Toolkit
38 {
39 /**
40  * @addtogroup dali_toolkit_controls
41  * @{
42  */
43
44 class StyleManager;
45
46 namespace Internal
47 {
48 /**
49  * @brief This is the internal base class for all controls.
50  *
51  * It will provide some common functionality required by all controls.
52  * Implements ConnectionTrackerInterface so that signals (typically connected to member functions) will
53  * be disconnected automatically when the control is destroyed.
54  * @SINCE_1_0.0
55  */
56 class DALI_TOOLKIT_API Control : public CustomActorImpl, public ConnectionTrackerInterface
57 {
58 public:
59   class Extension; ///< Forward declare future extension interface
60
61   // Creation & Destruction
62
63   /**
64    * @brief Creates a new ControlImpl instance that does not require touch by default.
65    *
66    * If touch is required, then the user can connect to this class' touch signal.
67    * @SINCE_1_0.0
68    * @return A handle to the ControlImpl instance
69    */
70   static Toolkit::Control New();
71
72 protected:
73   /**
74    * @brief Virtual destructor.
75    * @SINCE_1_0.0
76    */
77   virtual ~Control();
78
79 public:
80   // Styling
81
82   /**
83    * @copydoc Dali::Toolkit::Control::SetStyleName
84    */
85   void SetStyleName(const std::string& styleName);
86
87   /**
88    * @copydoc Dali::Toolkit::Control::GetStyleName
89    */
90   const std::string& GetStyleName() const;
91
92   // Background
93
94   /**
95    * @copydoc Dali::Toolkit::Control::SetBackgroundColor
96    */
97   void SetBackgroundColor(const Vector4& color);
98
99   /**
100    * @brief Sets the background with a property map.
101    *
102    * @SINCE_1_0.0
103    * @param[in] map The background property map
104    */
105   void SetBackground(const Property::Map& map);
106
107   /**
108    * @copydoc Dali::Toolkit::Control::ClearBackground
109    */
110   void ClearBackground();
111
112   // Gesture Detection
113
114   /**
115    * @brief Allows deriving classes to enable any of the gesture detectors that are available.
116    *
117    * Gesture detection can be enabled one at a time or in bitwise format as shown:
118    * @code
119    * EnableGestureDetection(GestureType::Value(GestureType::PINCH | GestureType::TAP | GestureType::PAN));
120    * @endcode
121    * @SINCE_1_0.0
122    * @param[in] type The gesture type(s) to enable
123    */
124   void EnableGestureDetection(GestureType::Value type);
125
126   /**
127    * @brief Allows deriving classes to disable any of the gesture detectors.
128    *
129    * Like EnableGestureDetection, this can also be called using bitwise or.
130    * @SINCE_1_0.0
131    * @param[in] type The gesture type(s) to disable
132    * @see EnableGetureDetection
133    */
134   void DisableGestureDetection(GestureType::Value type);
135
136   /**
137    * @brief If deriving classes wish to fine tune pinch gesture
138    * detection, then they can access the gesture detector through this
139    * API and modify the detection.
140    *
141    * @SINCE_1_0.0
142    * @return The pinch gesture detector
143    * @pre Pinch detection should have been enabled via EnableGestureDetection().
144    * @see EnableGestureDetection
145    */
146   PinchGestureDetector GetPinchGestureDetector() const;
147
148   /**
149    * @brief If deriving classes wish to fine tune pan gesture
150    * detection, then they can access the gesture detector through this
151    * API and modify the detection.
152    *
153    * @SINCE_1_0.0
154    * @return The pan gesture detector
155    * @pre Pan detection should have been enabled via EnableGestureDetection().
156    * @see EnableGestureDetection
157    */
158   PanGestureDetector GetPanGestureDetector() const;
159
160   /**
161    * @brief If deriving classes wish to fine tune tap gesture
162    * detection, then they can access the gesture detector through this
163    * API and modify the detection.
164    *
165    * @SINCE_1_0.0
166    * @return The tap gesture detector
167    * @pre Tap detection should have been enabled via EnableGestureDetection().
168    * @see EnableGestureDetection
169    */
170   TapGestureDetector GetTapGestureDetector() const;
171
172   /**
173    * @brief If deriving classes wish to fine tune long press gesture
174    * detection, then they can access the gesture detector through this
175    * API and modify the detection.
176    *
177    * @SINCE_1_0.0
178    * @return The long press gesture detector
179    * @pre Long press detection should have been enabled via EnableGestureDetection().
180    * @see EnableGestureDetection
181    */
182   LongPressGestureDetector GetLongPressGestureDetector() const;
183
184   // Keyboard Navigation
185
186   /**
187    * @brief Sets whether this control supports two dimensional
188    * keyboard navigation (i.e. whether it knows how to handle the
189    * keyboard focus movement between its child actors).
190    *
191    * The control doesn't support it by default.
192    * @SINCE_1_0.0
193    * @param[in] isSupported Whether this control supports two dimensional keyboard navigation
194    */
195   void SetKeyboardNavigationSupport(bool isSupported);
196
197   /**
198    * @brief Gets whether this control supports two dimensional keyboard navigation.
199    *
200    * @SINCE_1_0.0
201    * @return true if this control supports two dimensional keyboard navigation
202    */
203   bool IsKeyboardNavigationSupported();
204
205   // Key Input
206
207   /**
208    * @copydoc Toolkit::Control::SetKeyInputFocus()
209    */
210   void SetKeyInputFocus();
211
212   /**
213    * @copydoc Toolkit::Control::HasKeyInputFocus()
214    */
215   bool HasKeyInputFocus();
216
217   /**
218    * @copydoc Toolkit::Control::ClearKeyInputFocus()
219    */
220   void ClearKeyInputFocus();
221
222   // Keyboard Focus
223
224   /**
225    * @brief Sets whether this control is a focus group for keyboard navigation.
226    *
227    * (i.e. the scope of keyboard focus movement
228    * can be limited to its child actors). The control is not a focus group by default.
229    * @SINCE_1_0.0
230    * @param[in] isFocusGroup Whether this control is set as a focus group for keyboard navigation
231    */
232   void SetAsKeyboardFocusGroup(bool isFocusGroup);
233
234   /**
235    * @brief Gets whether this control is a focus group for keyboard navigation.
236    *
237    * @SINCE_1_0.0
238    * @return true if this control is set as a focus group for keyboard navigation
239    */
240   bool IsKeyboardFocusGroup();
241
242   /// @cond internal
243   /**
244    * @brief Called by the AccessibilityManager to activate the Control.
245    * @SINCE_1_0.0
246    */
247   DALI_INTERNAL void AccessibilityActivate();
248
249   /**
250    * @brief Called by the KeyboardFocusManager.
251    * @SINCE_1_0.0
252    */
253   DALI_INTERNAL void KeyboardEnter();
254   /// @endcond
255
256   /**
257    * @brief Retrieve visual property animations.
258    * This Control is a destination.
259    *
260    * @param[in] animation generated animation
261    * @param[in] source source control of the animation.
262    * @param[in] alphaFunction AlphaFunction of the animation
263    * @param[in] timePeriod TimePeriod of the animation
264    */
265   virtual void CreateTransitions(Dali::Animation& animation, Dali::Toolkit::Control source, AlphaFunction alphaFunction, TimePeriod timePeriod);
266
267   /**
268    * @brief Set this Control is transparent or not without any affection on the child Actors.
269    */
270   void SetTransparent(bool transparent) override;
271
272   /**
273    * @brief Get this Control is transparent or not.
274    */
275   bool GetTransparent() const override;
276
277   // Signals
278
279   /**
280    * @copydoc Dali::Toolkit::Control::KeyEventSignal()
281    */
282   Toolkit::Control::KeyEventSignalType& KeyEventSignal();
283
284   /**
285    * @copydoc Dali::Toolkit::Control::KeyInputFocusGainedSignal()
286    */
287   Toolkit::Control::KeyInputFocusSignalType& KeyInputFocusGainedSignal();
288
289   /**
290    * @copydoc Dali::Toolkit::Control::KeyInputFocusLostSignal()
291    */
292   Toolkit::Control::KeyInputFocusSignalType& KeyInputFocusLostSignal();
293
294   /// @cond internal
295   /**
296    * @brief Called by the KeyInputFocusManager to emit key event signals.
297    *
298    * @SINCE_1_0.0
299    * @param[in] event The key event
300    * @return True if the event was consumed
301    */
302   DALI_INTERNAL bool EmitKeyEventSignal(const KeyEvent& event);
303   /// @endcond
304
305 private: // For utility method
306   void MakeVisualTransition(Dali::Animation& animation, Dali::Toolkit::Control source, Dali::Property::Index index, AlphaFunction alphaFunction, TimePeriod timePeriod);
307
308 protected: // For derived classes to call
309   /**
310    * @brief Emits KeyInputFocusGained signal if true else emits KeyInputFocusLost signal.
311    *
312    * Should be called last by the control after it acts on the Input Focus change.
313    *
314    * @SINCE_1_0.0
315    * @param[in] focusGained True if gained, False if lost
316    */
317   void EmitKeyInputFocusSignal(bool focusGained);
318
319 protected: // From CustomActorImpl
320   /**
321    * @copydoc CustomActorImpl::OnSceneConnection()
322    * @note If overridden, then an up-call to Control::OnSceneConnection MUST be made at the end.
323    */
324   void OnSceneConnection(int depth) override;
325
326   /**
327    * @copydoc CustomActorImpl::OnSceneDisconnection()
328    * @note If overridden, then an up-call to Control::OnSceneDisconnection MUST be made at the end.
329    */
330   void OnSceneDisconnection() override;
331
332   /**
333    * @copydoc CustomActorImpl::OnChildAdd()
334    * @note If overridden, then an up-call to Control::OnChildAdd MUST be made at the end.
335    */
336   void OnChildAdd(Actor& child) override;
337
338   /**
339    * @copydoc CustomActorImpl::OnChildRemove()
340    * @note If overridden, then an up-call to Control::OnChildRemove MUST be made at the end.
341    */
342   void OnChildRemove(Actor& child) override;
343
344   /**
345    * @copydoc CustomActorImpl::OnPropertySet()
346    * @note If overridden, then an up-call to Control::OnChildRemove MUST be made at the end.
347    */
348   void OnPropertySet(Property::Index index, const Property::Value& propertyValue) override;
349
350   /**
351    * @copydoc CustomActorImpl::OnSizeSet()
352    * @note If overridden, then an up-call to Control::OnSizeSet MUST be made at the end.
353    */
354   void OnSizeSet(const Vector3& targetSize) override;
355
356   /**
357    * @copydoc CustomActorImpl::OnSizeAnimation()
358    * @note If overridden, then an up-call to Control::OnSizeAnimation MUST be made at the end.
359    */
360   void OnSizeAnimation(Animation& animation, const Vector3& targetSize) override;
361
362   /**
363    * @copydoc CustomActorImpl::OnRelayout()
364    */
365   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
366
367   /**
368    * @copydoc CustomActorImpl::OnSetResizePolicy()
369    */
370   void OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension) override;
371
372   /**
373    * @copydoc CustomActorImpl::GetNaturalSize()
374    */
375   Vector3 GetNaturalSize() override;
376
377   /**
378    * @copydoc CustomActorImpl::CalculateChildSize()
379    */
380   float CalculateChildSize(const Dali::Actor& child, Dimension::Type dimension) override;
381
382   /**
383    * @copydoc CustomActorImpl::GetHeightForWidth()
384    */
385   float GetHeightForWidth(float width) override;
386
387   /**
388    * @copydoc CustomActorImpl::GetWidthForHeight()
389    */
390   float GetWidthForHeight(float height) override;
391
392   /**
393    * @copydoc CustomActorImpl::RelayoutDependentOnChildren()
394    */
395   bool RelayoutDependentOnChildren(Dimension::Type dimension = Dimension::ALL_DIMENSIONS) override;
396
397   /**
398    * @copydoc CustomActorImpl::OnCalculateRelayoutSize()
399    */
400   void OnCalculateRelayoutSize(Dimension::Type dimension) override;
401
402   /**
403    * @copydoc CustomActorImpl::OnLayoutNegotiated()
404    */
405   void OnLayoutNegotiated(float size, Dimension::Type dimension) override;
406
407 protected: // Helpers for deriving classes
408   // Construction
409
410   /**
411    * @brief Flags for the constructor.
412    * @SINCE_1_0.0
413    */
414   enum ControlBehaviour
415   {
416     CONTROL_BEHAVIOUR_DEFAULT            = 0, ///< Default behaviour: Size negotiation is enabled & listens to Style Change signal, but doesn't receive event callbacks. @SINCE_1_2_10
417     NOT_IN_USE_1                         = 1 << (CustomActorImpl::ACTOR_FLAG_COUNT + 0),
418     REQUIRES_KEYBOARD_NAVIGATION_SUPPORT = 1 << (CustomActorImpl::ACTOR_FLAG_COUNT + 1), ///< True if needs to support keyboard navigation @SINCE_1_0.0
419     DISABLE_STYLE_CHANGE_SIGNALS         = 1 << (CustomActorImpl::ACTOR_FLAG_COUNT + 2), ///< True if control should not monitor style change signals @SINCE_1_2_10
420
421     LAST_CONTROL_BEHAVIOUR_FLAG
422   };
423
424   static const int CONTROL_BEHAVIOUR_FLAG_COUNT = Log<LAST_CONTROL_BEHAVIOUR_FLAG - 1>::value + 1; ///< Total count of flags
425
426   /**
427    * @brief Control constructor.
428    *
429    * @SINCE_1_0.0
430    * @param[in] behaviourFlags Behavioural flags from ControlBehaviour enum
431    */
432   Control(ControlBehaviour behaviourFlags);
433
434   /**
435    * @brief Second phase initialization.
436    * @SINCE_1_0.0
437    */
438   void Initialize();
439
440 public: // API for derived classes to override
441   // Lifecycle
442
443   /**
444    * @brief This method is called after the Control has been initialized.
445    *
446    * Derived classes should do any second phase initialization by overriding this method.
447    * @SINCE_1_0.0
448    */
449   virtual void OnInitialize();
450
451   // Styling
452
453   /**
454    * @brief This method should be overridden by deriving classes requiring notifications when the style changes.
455    *
456    * @SINCE_1_0.0
457    * @param[in] styleManager The StyleManager object
458    * @param[in] change Information denoting what has changed
459    */
460   virtual void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change);
461
462   // Accessibility
463
464   /**
465    * @brief This method is called when the control is accessibility activated.
466    *
467    * Derived classes should override this to perform custom accessibility activation.
468    * @SINCE_1_0.0
469    * @return true if this control can perform accessibility activation
470    */
471   virtual bool OnAccessibilityActivated();
472
473   /**
474    * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
475    * pan gesture.
476    *
477    * @SINCE_1_0.0
478    * @param[in] gesture The pan gesture
479    * @return true if the pan gesture has been consumed by this control
480    */
481   virtual bool OnAccessibilityPan(PanGesture gesture);
482
483   /**
484    * @brief This method should be overridden by deriving classes when they wish to respond
485    * the accessibility up and down action (i.e. value change of slider control).
486    *
487    * @SINCE_1_0.0
488    * @param[in] isIncrease Whether the value should be increased or decreased
489    * @return true if the value changed action has been consumed by this control
490    */
491   virtual bool OnAccessibilityValueChange(bool isIncrease);
492
493   /**
494    * @brief This method should be overridden by deriving classes when they wish to respond
495    * the accessibility zoom action.
496    *
497    * @SINCE_1_0.0
498    * @return true if the zoom action has been consumed by this control
499    */
500   virtual bool OnAccessibilityZoom();
501
502   // Keyboard focus
503
504   /**
505    * @brief Called when the control gains key input focus.
506    *
507    * Should be overridden by derived classes if they need to customize what happens when focus is gained.
508    * @SINCE_1_0.0
509    */
510   virtual void OnKeyInputFocusGained();
511
512   /**
513    * @brief Called when the control loses key input focus.
514    *
515    * Should be overridden by derived classes if they need to customize what happens when focus is lost.
516    * @SINCE_1_0.0
517    */
518   virtual void OnKeyInputFocusLost();
519
520   /**
521    * @brief Gets the next keyboard focusable actor in this control towards the given direction.
522    *
523    * A control needs to override this function in order to support two dimensional keyboard navigation.
524    * @SINCE_1_0.0
525    * @param[in] currentFocusedActor The current focused actor
526    * @param[in] direction The direction to move the focus towards
527    * @param[in] loopEnabled Whether the focus movement should be looped within the control
528    * @return The next keyboard focusable actor in this control or an empty handle if no actor can be focused
529    */
530   virtual Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled);
531
532   /**
533    * @brief Informs this control that its chosen focusable actor will be focused.
534    *
535    * This allows the application to perform any actions if wishes
536    * before the focus is actually moved to the chosen actor.
537    *
538    * @SINCE_1_0.0
539    * @param[in] commitedFocusableActor The commited focusable actor
540    */
541   virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
542
543   /**
544    * @brief This method is called when the control has enter pressed on it.
545    *
546    * Derived classes should override this to perform custom actions.
547    * @SINCE_1_0.0
548    * @return true if this control supported this action
549    */
550   virtual bool OnKeyboardEnter();
551
552   /**
553    * @brief Called after a key-event is received by the actor that has had its focus set.
554    *
555    * @SINCE_1_0.0
556    * @param[in] event The Key Event
557    * @return True if the event should be consumed
558    */
559   virtual bool OnKeyEvent(const KeyEvent& event);
560
561   // Gestures
562
563   /**
564    * @brief Called whenever a pinch gesture is detected on this control.
565    *
566    * This can be overridden by deriving classes when pinch detection
567    * is enabled.  The default behaviour is to scale the control by the
568    * pinch scale.
569    *
570    * @SINCE_1_0.0
571    * @param[in] pinch The pinch gesture
572    * @note If overridden, then the default behavior will not occur.
573    * @note Pinch detection should be enabled via EnableGestureDetection().
574    * @see EnableGestureDetection
575    */
576   virtual void OnPinch(const PinchGesture& pinch);
577
578   /**
579    * @brief Called whenever a pan gesture is detected on this control.
580    *
581    * This should be overridden by deriving classes when pan detection
582    * is enabled.
583    *
584    * @SINCE_1_0.0
585    * @param[in] pan The pan gesture
586    * @note There is no default behavior with panning.
587    * @note Pan detection should be enabled via EnableGestureDetection().
588    * @see EnableGestureDetection
589    */
590   virtual void OnPan(const PanGesture& pan);
591
592   /**
593    * @brief Called whenever a tap gesture is detected on this control.
594    *
595    * This should be overridden by deriving classes when tap detection
596    * is enabled.
597    *
598    * @SINCE_1_0.0
599    * @param[in] tap The tap gesture
600    * @note There is no default behavior with a tap.
601    * @note Tap detection should be enabled via EnableGestureDetection().
602    * @see EnableGestureDetection
603    */
604   virtual void OnTap(const TapGesture& tap);
605
606   /**
607    * @brief Called whenever a long press gesture is detected on this control.
608    *
609    * This should be overridden by deriving classes when long press
610    * detection is enabled.
611    *
612    * @SINCE_1_0.0
613    * @param[in] longPress The long press gesture
614    * @note There is no default behaviour associated with a long press.
615    * @note Long press detection should be enabled via EnableGestureDetection().
616    * @see EnableGestureDetection
617    */
618   virtual void OnLongPress(const LongPressGesture& longPress);
619
620   // From ConnectionTrackerInterface
621
622   /**
623    * @copydoc ConnectionTrackerInterface::SignalConnected
624    */
625   void SignalConnected(SlotObserver* slotObserver, CallbackBase* callback) override;
626
627   /**
628    * @copydoc ConnectionTrackerInterface::SignalDisconnected
629    */
630   void SignalDisconnected(SlotObserver* slotObserver, CallbackBase* callback) override;
631
632   /**
633    * @brief Retrieves the extension for this control.
634    *
635    * @SINCE_1_0.0
636    * @return The extension if available, NULL otherwise
637    */
638   virtual Extension* GetControlExtension()
639   {
640     return NULL;
641   }
642
643 private:
644   /// @cond internal
645
646   // Not copyable or movable
647   DALI_INTERNAL Control(const Control&) = delete;            ///< Deleted copy constructor.
648   DALI_INTERNAL Control(Control&&)      = delete;            ///< Deleted move constructor.
649   DALI_INTERNAL Control& operator=(const Control&) = delete; ///< Deleted copy assignment operator.
650   DALI_INTERNAL Control& operator=(Control&&) = delete;      ///< Deleted move assignment operator.
651
652 public:
653   class DALI_INTERNAL Impl; // Class declaration is public so we can internally add devel API's to the Controls Impl
654
655 private:
656   Impl* mImpl;
657   /// @endcond
658 };
659
660 /**
661  * @brief Gets implementation from the handle.
662  *
663  * @SINCE_1_0.0
664  * @param handle
665  * @return Implementation
666  * @pre handle is initialized and points to a control
667  */
668 DALI_TOOLKIT_API Internal::Control& GetImplementation(Dali::Toolkit::Control& handle);
669
670 /**
671  * @brief Gets implementation from the handle.
672  *
673  * @SINCE_1_0.0
674  * @param handle
675  * @return Implementation
676  * @pre Handle is initialized and points to a control.
677  */
678 DALI_TOOLKIT_API const Internal::Control& GetImplementation(const Dali::Toolkit::Control& handle);
679
680 } // namespace Internal
681
682 /**
683  * @}
684  */
685 } // namespace Toolkit
686
687 } // namespace Dali
688
689 #endif // DALI_TOOLKIT_CONTROL_IMPL_H