b2b687fd1b023efc148038f3fffe7acc08611993
[platform/core/uifw/dali-toolkit.git] / capi / 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) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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  * @addtogroup CAPI_DALI_TOOLKIT_CONTROLS_MODULE
22  * @{
23  */
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/controls/control.h>
27
28 namespace Dali DALI_IMPORT_API
29 {
30
31 namespace Toolkit
32 {
33
34 class StyleManager;
35
36 namespace Internal DALI_INTERNAL
37 {
38 class RelayoutControllerImpl;
39 class KeyInputFocusManager;
40 }
41
42 typedef std::pair< Actor, Vector2 > ActorSizePair;       ///< Pair of actor and size
43 typedef std::vector< ActorSizePair > ActorSizeContainer; ///< Container of actors and their sizes
44
45 namespace Internal
46 {
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  */
55 class DALI_IMPORT_API Control : public CustomActorImpl, public ConnectionTrackerInterface
56 {
57 public:
58
59   // Properties
60   enum
61   {
62     CONTROL_PROPERTY_START_INDEX = PROPERTY_REGISTRATION_START_INDEX,
63     CONTROL_PROPERTY_END_INDEX = CONTROL_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
64   };
65
66   // Creation
67
68   /**
69    * @brief Create a new ControlImpl instance that does not require touch by default.
70    *
71    * If touch is required then the user can connect to this class' touch signal.
72    * @return A handle to the ConntrolImpl instance.
73    */
74   static Toolkit::Control New();
75
76   // Destruction
77
78   /**
79    * @brief Virtual destructor.
80    */
81   virtual ~Control();
82
83   // Actions
84
85   /**
86    * @brief This method should be overridden by deriving classes when they wish to be notified when they
87    * are activated.
88    */
89   virtual void OnActivated() { }
90
91   /**
92    * @brief This method should be overridden by deriving classes when they wish to respond the accessibility
93    * pan gesture.
94    *
95    * @param[in] gesture The pan gesture.
96    * @return true if the pan gesture has been consumed by this control
97    */
98   virtual bool OnAccessibilityPan(PanGesture gesture);
99
100   /**
101    * @brief This method should be overridden by deriving classes when they wish to respond
102    * the accessibility up and down action (i.e. value change of slider control).
103    *
104    * @param[in] isIncrease Whether the value should be increased or decreased
105    * @return true if the value changed action has been consumed by this control
106    */
107   virtual bool OnAccessibilityValueChange(bool isIncrease);
108
109   /**
110    * @brief Sets whether this control supports two dimensional
111    * keyboard navigation (i.e. whether it knows how to handle the
112    * keyboardn focus movement between its child actors).
113    *
114    * The control doesn't support it by default.
115    * @param[in] isSupported Whether this control supports two dimensional keyboard navigation.
116    */
117   void SetKeyboardNavigationSupport(bool isSupported);
118
119   /**
120    * @brief Gets whether this control supports two dimensional keyboard navigation.
121    *
122    * @return true if this control supports two dimensional keyboard navigation.
123    */
124   bool IsKeyboardNavigationSupported();
125
126   /**
127    * @brief Sets whether this control is a focus group for keyboard navigation.
128    *
129    * (i.e. the scope of keyboard focus movement
130    * can be limitied to its child actors). The control is not a focus group by default.
131    * @param[in] isFocusGroup Whether this control is set as a focus group for keyboard navigation.
132    */
133   void SetAsKeyboardFocusGroup(bool isFocusGroup);
134
135   /**
136    * @brief Gets whether this control is a focus group for keyboard navigation.
137    *
138    * @return true if this control is set as a focus group for keyboard navigation.
139    */
140   bool IsKeyboardFocusGroup();
141
142   /**
143    * @brief Gets the next keyboard focusable actor in this control towards the given direction.
144    *
145    * A control needs to override this function in order to support two dimensional keyboard navigation.
146    * @param[in] currentFocusedActor The current focused actor.
147    * @param[in] direction The direction to move the focus towards.
148    * @param[in] loopEnabled Whether the focus movement should be looped within the control.
149    * @return the next keyboard focusable actor in this control or an empty handle if no actor can be focused.
150    */
151   virtual Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
152
153   /**
154    * @brief Informs this control that its chosen focusable actor will be focused.
155    *
156    * This allows the application to preform any actions if wishes
157    * before the focus is actually moved to the chosen actor.
158    *
159    * @param[in] commitedFocusableActor The commited focusable actor.
160    */
161   virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor) { }
162
163   /**
164    * @brief Performs actions as requested using the action name.
165    *
166    * @param[in] object The object on which to perform the action.
167    * @param[in] actionName The action to perform.
168    * @param[in] attributes The attributes with which to perfrom this action.
169    * @return true if action has been accepted by this control
170    */
171   static bool DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes);
172
173   /**
174    * @brief If deriving classes wish to fine tune pinch gesture
175    * detection then they can access the gesture detector through this
176    * API and modify the detection.
177    *
178    * @return The pinch gesture detector.
179    * @pre Pinch detection should have been enabled via EnableGestureDetection().
180    * @see EnableGestureDetection
181    */
182   PinchGestureDetector GetPinchGestureDetector() const;
183
184   /**
185    * @brief If deriving classes wish to fine tune pan gesture
186    * detection then they can access the gesture detector through this
187    * API and modify the detection.
188    *
189    * @return The pan gesture detector.
190    * @pre Pan detection should have been enabled via EnableGestureDetection().
191    * @see EnableGestureDetection
192    */
193   PanGestureDetector GetPanGestureDetector() const;
194
195   /**
196    * @brief If deriving classes wish to fine tune tap gesture
197    * detection then they can access the gesture detector through this
198    * API and modify the detection.
199    *
200    * @return The tap gesture detector.
201    * @pre Tap detection should have been enabled via EnableGestureDetection().
202    * @see EnableGestureDetection
203    */
204   TapGestureDetector GetTapGestureDetector() const;
205
206   /**
207    * @brief If deriving classes wish to fine tune long press gesture
208    * detection then they can access the gesture detector through this
209    * API and modify the detection.
210    *
211    * @return The long press gesture detector.
212    * @pre Long press detection should have been enabled via EnableGestureDetection().
213    * @see EnableGestureDetection
214    */
215   LongPressGestureDetector GetLongPressGestureDetector() const;
216
217   // Background
218
219   /**
220    * @copydoc Dali::Toolkit::Control::SetBackgroundColor
221    */
222   void SetBackgroundColor( const Vector4& color );
223
224   /**
225    * @copydoc Dali::Toolkit::Control::GetBackgroundColor
226    */
227   Vector4 GetBackgroundColor() const;
228
229   /**
230    * @copydoc Dali::Toolkit::Control::SetBackground
231    */
232   void SetBackground( Image image );
233
234   /**
235    * @copydoc Dali::Toolkit::Control::ClearBackground
236    */
237   void ClearBackground();
238
239   /**
240    * @copydoc Dali::Toolkit::Control::GetBackgroundActor
241    */
242   Actor GetBackgroundActor() const;
243
244 public:
245
246   /**
247    * @copydoc Dali::Toolkit::Control::KeyEventSignal()
248    */
249   Toolkit::Control::KeyEventSignalV2& KeyEventSignal();
250
251 protected:
252
253   // Construction
254
255   /**
256    * @brief Second phase initialization.
257    */
258   void Initialize();
259
260   // Gesture Detection
261
262   /**
263    * @brief Allows deriving classes to enable any of the gesture detectors that are available.
264    *
265    * Gesture detection can be enabled one at a time or in bitwise format as shown:
266    * @code
267    * EnableGestureDetection(Gesture::Type(Gesture::Pinch | Gesture::Tap | Gesture::Pan));
268    * @endcode
269    * @param[in]  type  The gesture type(s) to enable.
270    */
271   void EnableGestureDetection(Gesture::Type type);
272
273   /**
274    * @brief Allows deriving classes to disable any of the gesture detectors.
275    *
276    * Like EnableGestureDetection, this can also be called using bitwise or.
277    * @param[in]  type  The gesture type(s) to disable.
278    * @see EnableGetureDetection
279    */
280   void DisableGestureDetection(Gesture::Type type);
281
282 private: // For derived classes to override
283
284   /**
285    * @brief This method is called after the Control has been initialized.
286    *
287    * Derived classes should do any second phase initialization by
288    * overriding this method.
289    */
290   virtual void OnInitialize() { }
291
292   /**
293    * @brief Callback for when the theme changes.
294    */
295   virtual void OnThemeChange( Toolkit::StyleManager styleManager );
296
297   /**
298    * @brief Called whenever a pinch gesture is detected on this control.
299    *
300    * This can be overridden by deriving classes when pinch detection
301    * is enabled.  The default behaviour is to scale the control by the
302    * pinch scale.
303    *
304    * @note If overridden, then the default behaviour will not occur.
305    * @note Pinch detection should be enabled via EnableGestureDetection().
306    * @param[in]  pinch  The pinch gesture.
307    * @see EnableGestureDetection
308    */
309   virtual void OnPinch(PinchGesture pinch);
310
311   /**
312    * @brief Called whenever a pan gesture is detected on this control.
313    *
314    * This should be overridden by deriving classes when pan detection
315    * is enabled.
316    *
317    * @note There is no default behaviour with panning.
318    * @note Pan detection should be enabled via EnableGestureDetection().
319    * @param[in]  pan  The pan gesture.
320    * @see EnableGestureDetection
321    */
322   virtual void OnPan(PanGesture pan) { }
323
324   /**
325    * @brief Called whenever a tap gesture is detected on this control.
326    *
327    * This should be overridden by deriving classes when tap detection
328    * is enabled.
329    *
330    * @note There is no default behaviour with a tap.
331    * @note Tap detection should be enabled via EnableGestureDetection().
332    * @param[in]  tap  The tap gesture.
333    * @see EnableGestureDetection
334    */
335   virtual void OnTap(TapGesture tap) { }
336
337   /**
338    * @brief Called whenever a long press gesture is detected on this control.
339    *
340    * This should be overridden by deriving classes when long press
341    * detection is enabled.
342    *
343    * @note There is no default behaviour associated with a long press.
344    * @note Long press detection should be enabled via EnableGestureDetection().
345    * @param[in]  longPress  The long press gesture.
346    * @see EnableGestureDetection
347    */
348   virtual void OnLongPress(LongPressGesture longPress) { }
349
350   /**
351    * @brief Called whenever the control is added to the stage.
352    *
353    * Could be overridden by derived classes.
354    */
355   virtual void OnControlStageConnection() { }
356
357   /**
358    * @brief Called whenever the control is removed from the stage.
359    *
360    * Could be overridden by derived classes.
361    */
362   virtual void OnControlStageDisconnection() { }
363
364   /**
365    * @brief Called whenever an Actor is added to the control.
366    *
367    * Could be overridden by derived classes.
368    *
369    * @param[in] child The added actor.
370    */
371   virtual void OnControlChildAdd( Actor& child ) { }
372
373   /**
374    * @brief Called whenever an Actor is removed from the control.
375    *
376    * Could be overridden by derived classes.
377    *
378    * @param[in] child The removed actor.
379    */
380   virtual void OnControlChildRemove( Actor& child ) { }
381
382   /**
383    * @brief Called whenever the Control's size is set.
384    *
385    * Could be overridden by derived classes.
386    *
387    * @param[in] size The new size.
388    */
389   virtual void OnControlSizeSet( const Vector3& size ) { }
390
391   /**
392    * @brief Called after the Dali::Stage::SignalMessageQueueFlushed()
393    * signal is emitted if this control requested to be relaid-out.
394    *
395    * Should be overridden by derived classes if they need to layout
396    * actors differently after certain operations like add or remove
397    * actors, resize or after changing especific properties.
398    *
399    * @param[in]      size       The allocated size.
400    * @param[in,out]  container  The control should add actors to this container that it is not able
401    *                            to allocate a size for.
402    */
403   virtual void OnRelaidOut( Vector2 size, ActorSizeContainer& container );
404
405 private: // From CustomActorImpl, derived classes can override these.
406
407   /**
408    * @brief Sends a request to relayout this control.
409    *
410    * The control will be relaid out after the
411    * Dali::Stage::SignalMessageQueueFlushed() signal is emitted.
412    *
413    * It calls OnControlStageConnection() to notify derived classes.
414    *
415    * @see Dali::CustomActorImpl::OnStageConnection()
416    */
417   virtual void OnStageConnection();
418
419   /**
420    * @brief Calls OnControlStageDisconnection() to notify derived classed.
421    *
422    * @see Dali::CustomActorImpl::OnStageDisconnection()
423    */
424   virtual void OnStageDisconnection();
425
426   /**
427    * @brief Sends a request to relayout this control.
428    *
429    * The control will be relaid out after the
430    * Dali::Stage::SignalMessageQueueFlushed() signal is emitted.  It
431    * calls OnControlChildAdd() to notify derived classes.
432    *
433    * @note This method shouldn't be overridden by derived classes.
434    *
435    * @param[in] child The added actor.
436    *
437    * @see Dali::CustomActorImpl::OnChildAdd(Actor&)
438    */
439   virtual void OnChildAdd(Actor& child);
440
441   /**
442    * @brief Sends a request to relayout this control.
443    *
444    * The control will be relaid out after the
445    * Dali::Stage::SignalMessageQueueFlushed() signal is emitted.  It
446    * calls OnControlChildRemove() to notify derived classes.
447    *
448    * @note This method shouldn't be overridden by derived classes.
449    *
450    * @param[in] child The removed actor.
451    *
452    * @see Dali::CustomActorImpl::OnChildRemove(Actor&)
453    */
454   virtual void OnChildRemove(Actor& child);
455
456   /**
457    * @brief It stores the size set by size negotiation and relayout.
458    *
459    * It also keeps a backup of the size set through the Actor's API used in the size negotiation.
460    * It calls the OnControlSizeSet() to notify derived classes.
461    *
462    * @param[in] targetSize The new size.
463    *
464    * @see Dali::CustomActorImpl::OnSizeSet(const Vector3&)
465    */
466   virtual void OnSizeSet(const Vector3& targetSize);
467
468   /**
469    * @copydoc Dali::CustomActorImpl::OnSizeAnimation(Animation&, const Vector3&)
470    */
471   virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize);
472
473   /**
474    * @copydoc Dali::CustomActorImpl::OnTouchEvent(const TouchEvent&)
475    */
476   virtual bool OnTouchEvent(const TouchEvent& event);
477
478   /**
479    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
480    */
481   virtual bool OnKeyEvent(const KeyEvent& event);
482
483   /**
484    * @copydoc Dali::CustomActorImpl::OnMouseWheelEvent(const MouseWheelEvent&)
485    */
486   virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
487
488   /**
489    * @copydoc Dali::CustomActorImpl::OnKeyInputFocusGained()
490    */
491   virtual void OnKeyInputFocusGained();
492
493   /**
494    * @copydoc Dali::CustomActorImpl::OnKeyInputFocusLost()
495    */
496   virtual void OnKeyInputFocusLost();
497
498   /**
499    * @copydoc Dali::CustomActorImpl::GetChildByAlias(const std::string& actorAlias)
500    */
501   virtual Actor GetChildByAlias(const std::string& actorAlias);
502
503 private:
504
505   /**
506    * @brief Perform the activated action.
507    *
508    * @param[in] attributes The attributes to perfrom this action.
509    */
510   void DoActivatedAction(const PropertyValueContainer& attributes);
511
512 protected: // Construction
513
514   // Flags for the constructor
515   enum ControlBehaviour
516   {
517     CONTROL_BEHAVIOUR_NONE        = 0x0,
518     REQUIRES_TOUCH_EVENTS         = 0x1,     ///< True if the OnTouchEvent() callback is required.
519     REQUIRES_THEME_CHANGE_SIGNALS = 0x2      ///< True if this control should listen to theme change signals
520   };
521
522   /**
523    * @deprecated Use the constructor taking flags instead
524    * @brief Create a Control.
525    *
526    * @param[in] requiresTouchEvents True if the OnTouchEvent() callback is required.
527    */
528   Control(bool requiresTouchEvents);
529
530   /**
531    * @brief Create a Control.
532    *
533    * @param[in] behaviourFlags Behavioural flags from ControlBehaviour enum
534    */
535   Control(ControlBehaviour behaviourFlags);
536
537 public:
538
539   // Size negotiation
540
541   /**
542    * @copydoc Toolkit::Control::SetSizePolicy()
543    */
544   void SetSizePolicy( Toolkit::Control::SizePolicy widthPolicy, Toolkit::Control::SizePolicy heightPolicy );
545
546   /**
547    * @copydoc Toolkit::Control::GetSizePolicy()
548    */
549   void GetSizePolicy( Toolkit::Control::SizePolicy& widthPolicy, Toolkit::Control::SizePolicy& heightPolicy ) const;
550
551   /**
552    * @copydoc Toolkit::Control::SetMinimumSize()
553    */
554   void SetMinimumSize( const Vector3& size );
555
556   /**
557    * @copydoc Toolkit::Control::GetMinimumSize()
558    */
559   const Vector3& GetMinimumSize() const;
560
561   /**
562    * @copydoc Toolkit::Control::SetMaximumSize()
563    */
564   void SetMaximumSize( const Vector3& size );
565
566   /**
567    * @copydoc Toolkit::Control::GetMaximumSize()
568    */
569   const Vector3& GetMaximumSize() const;
570
571   /**
572    * @copydoc Toolkit::Control::GetNaturalSize()
573    */
574   virtual Vector3 GetNaturalSize();
575
576   /**
577    * @copydoc Toolkit::Control::GetHeightForWidth()
578    */
579   virtual float GetHeightForWidth( float width );
580
581   /**
582    * @copydoc Toolkit::Control::GetWidthForHeight()
583    */
584   virtual float GetWidthForHeight( float height );
585
586   /**
587    * @brief Retrieves the current Control's size.
588    *
589    * @return The control's size.
590    */
591   const Vector3& GetControlSize() const;
592
593   /**
594    * @brief Retrieves the Control's size set by the Application / Control.
595    *
596    * @return The control's size.
597    */
598   const Vector3& GetSizeSet() const;
599
600   //KeyInput
601
602   /**
603    * @copydoc Toolkit::Control::SetKeyInputFocus()
604    */
605   void SetKeyInputFocus();
606
607   /**
608    * @copydoc Toolkit::Control::HasKeyInputFocus()
609    */
610   bool HasKeyInputFocus();
611
612   /**
613    * @copydoc Toolkit::Control::ClearKeyInputFocus()
614    */
615   void ClearKeyInputFocus();
616
617   /**
618    * @copydoc ConnectionTrackerInterface::SignalConnected
619    */
620   virtual void SignalConnected( SlotObserver* slotObserver, CallbackBase* callback );
621
622   /**
623    * @copydoc ConnectionTrackerInterface::SignalDisconnected
624    */
625   virtual void SignalDisconnected( SlotObserver* slotObserver, CallbackBase* callback );
626
627   /**
628    * @copydoc ConnectionTrackerInterface::GetConnectionCount
629    */
630   virtual std::size_t GetConnectionCount() const;
631
632 protected:
633
634   /**
635    * @brief Sends a request to be relaid-out.
636    *
637    * This method is called from OnStageConnection(), OnChildAdd(),
638    * OnChildRemove(), SetSizePolicy(), SetMinimumSize() and
639    * SetMaximumSize().
640    *
641    * This method could also be called from derived classes every time
642    * a control's poperty change and it needs to be relaid-out.  After
643    * the Dali::Stage::SignalMessageQueueFlushed() is emitted a
644    * relayout process starts and all controls which called this method
645    * will be relaid-out.
646    *
647    * @note RelayoutRequest() only sends a request per Control before
648    * the Dali::Stage::SignalMessageQueueFlushed() signal is
649    * emitted. That means a control will be relaid-out only once, even
650    * if more than one request is sent between two consecutive signals.
651    */
652   void RelayoutRequest();
653
654   /**
655    * @brief Helper method for controls to Relayout their children if
656    * they do not know whether that child is a control or not.
657    *
658    * @param[in]      actor      The actor to relayout.
659    * @param[in]      size       The size to allocate to the actor.
660    * @param[in,out]  container  The container that holds actors that have not been allocated a size yet.
661    */
662   static void Relayout( Actor actor, Vector2 size, ActorSizeContainer& container );
663
664 private: // Used by the RelayoutController
665
666   /**
667    * @brief Called by the RelayoutController to negotiate the size of a control.
668    *
669    * The size allocated by the the algorithm is passed in which the
670    * control must adhere to.  A container is passed in as well which
671    * the control should populate with actors it has not / or does not
672    * need to handle in its size negotiation.
673    *
674    * @param[in]      size       The allocated size.
675    * @param[in,out]  container  The container that holds actors that are fed back into the
676    *                            RelayoutController algorithm.
677    */
678   void NegotiateSize( Vector2 size, ActorSizeContainer& container );
679
680 private:
681
682   /**
683    * @brief Called by NegotiateSize when the size to allocate to the control has been calculated.
684    *
685    * It calls the OnRelaidOut() method which can be overridden by derived classes.
686    *
687    * @param[in]      size       The allocated size.
688    * @param[in,out]  container  The control should add actors to this container that it is not able
689    *                            to allocate a size for.
690    */
691   void Relayout( Vector2 size, ActorSizeContainer& container );
692
693   /**
694    * @brief Used by the KeyInputFocusManager to emit key event signals.
695    *
696    * @param[in] event The key event.
697    * @return True if the event was consumed.
698    */
699   bool EmitKeyEventSignal(const KeyEvent& event);
700
701
702
703 private:
704
705   // Undefined
706   Control(const Control&);
707   Control& operator=(const Control&);
708
709   class Impl;
710   Impl *mImpl;
711
712   friend class Internal::RelayoutControllerImpl;   ///< Relayout controller needs to call Relayout() which is private.
713   friend class Internal::KeyInputFocusManager;     ///< KeyInputFocusManager needs to call which is private.
714 };
715
716 } // namespace Internal
717
718 typedef Internal::Control ControlImpl; ///< @deprecated, Use Internal::Control
719
720 } // namespace Toolkit
721
722 } // namespace Dali
723
724 /**
725  * @}
726  */
727 #endif // __DALI_TOOLKIT_CONTROL_IMPL_H__