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