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