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