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