Merge "Size negotiation patch 3: Scope size negotiation enums" into tizen
[platform/core/uifw/dali-core.git] / dali / public-api / actors / custom-actor-impl.h
1 #ifndef __DALI_CUSTOM_ACTOR_IMPL_H__
2 #define __DALI_CUSTOM_ACTOR_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 // INTERNAL INCLUDES
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/object/property.h>
24 #include <dali/public-api/object/ref-object.h>
25 #include <dali/public-api/actors/actor-enumerations.h>
26
27 namespace Dali
28 {
29
30 namespace Internal DALI_INTERNAL
31 {
32 class CustomActor;
33 }
34
35 class Actor;
36 class Animation;
37 class CustomActor;
38 class CustomActorImpl;
39 class RelayoutContainer;
40 struct KeyEvent;
41 struct TouchEvent;
42 struct HoverEvent;
43 struct MouseWheelEvent;
44 struct Vector2;
45 struct Vector3;
46
47
48 /**
49  * @brief Pointer to Dali::CustomActorImpl object.
50  */
51 typedef IntrusivePtr<CustomActorImpl> CustomActorImplPtr;
52
53 /**
54  * @brief CustomActorImpl is an abstract base class for custom control implementations.
55  *
56  * This provides a series of pure virtual methods, which are called when actor-specific events occur.
57  * An CustomActorImpl is typically owned by a single CustomActor instance; see also CustomActor::New(CustomActorImplPtr).
58  */
59 class DALI_IMPORT_API CustomActorImpl : public Dali::RefObject
60 {
61 public:
62
63   /**
64    * @brief Virtual destructor.
65    */
66   virtual ~CustomActorImpl();
67
68   /**
69    * @brief Used by derived CustomActorImpl instances, to access the public Actor interface.
70    *
71    * @return A pointer to self, or an uninitialized pointer if the CustomActorImpl is not owned.
72    */
73   CustomActor Self() const;
74
75   /**
76    * @brief Called after the actor has been connected to the stage.
77    *
78    * When an actor is connected, it will be directly or indirectly parented to the root Actor.
79    * @note The root Actor is provided automatically by Dali::Stage, and is always considered to be connected.
80    *
81    * @note When the parent of a set of actors is connected to the stage, then all of the children
82    * will received this callback.
83    *
84    * For the following actor tree, the callback order will be A, B, D, E, C, and finally F.
85    *
86    *       A (parent)
87    *      / \
88    *     B   C
89    *    / \   \
90    *   D   E   F
91    */
92   virtual void OnStageConnection() = 0;
93
94   /**
95    * @brief Called after the actor has been disconnected from the stage.
96    *
97    * If an actor is disconnected it either has no parent, or is parented to a disconnected actor.
98    *
99    * @note When the parent of a set of actors is disconnected to the stage, then all of the children
100    * will received this callback, starting with the leaf actors.
101    *
102    * For the following actor tree, the callback order will be D, E, B, F, C, and finally A.
103    *
104    *       A (parent)
105    *      / \
106    *     B   C
107    *    / \   \
108    *   D   E   F
109    */
110   virtual void OnStageDisconnection() = 0;
111
112   /**
113    * @brief Called after a child has been added to the owning actor.
114    *
115    * @param[in] child The child which has been added.
116    */
117   virtual void OnChildAdd(Actor& child) = 0;
118
119   /**
120    * @brief Called after a child has been removed from the owning actor.
121    *
122    * @param[in] child The child being removed.
123    */
124   virtual void OnChildRemove(Actor& child) = 0;
125
126   /**
127    * @brief Called when the owning actor property is set.
128    *
129    * @param[in] index The Property index that was set.
130    * @param[in] propertyValue The value to set.
131    */
132   virtual void OnPropertySet( Property::Index index, Property::Value propertyValue ) ;
133
134   /**
135    * @brief Called when the owning actor's size is set e.g. using Actor::SetSize().
136    *
137    * @param[in] targetSize The target size. Note that this target size may not match the size returned via Actor::GetSize().
138    */
139   virtual void OnSizeSet(const Vector3& targetSize) = 0;
140
141   /**
142    * @brief Called when the owning actor's size is animated e.g. using Animation::AnimateTo( Property( actor, Actor::Property::SIZE ), ... ).
143    *
144    * @param[in] animation The object which is animating the owning actor.
145    * @param[in] targetSize The target size. Note that this target size may not match the size returned via Actor::GetSize().
146    */
147   virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize) = 0;
148
149   /**
150    * @brief Called after a touch-event is received by the owning actor.
151    *
152    * @note This must be enabled during construction; see CustomActorImpl::CustomActorImpl(bool)
153    * @param[in] event The touch event.
154    * @return True if the event should be consumed.
155    */
156   virtual bool OnTouchEvent(const TouchEvent& event) = 0;
157
158   /**
159    * @brief Called after a hover-event is received by the owning actor.
160    *
161    * @note This must be enabled during construction; see CustomActorImpl::SetRequiresHoverEvents(bool)
162    * @param[in] event The hover event.
163    * @return True if the event should be consumed.
164    */
165   virtual bool OnHoverEvent(const HoverEvent& event) = 0;
166
167   /**
168    * @brief Called after a key-event is received by the actor that has had its focus set.
169    *
170    * @param[in] event the Key Event
171    * @return True if the event should be consumed.
172    */
173   virtual bool OnKeyEvent(const KeyEvent& event) = 0;
174
175   /**
176    * @brief Called after a mouse-wheel-event is received by the owning actor.
177    *
178    * @note This must be enabled during construction; see CustomActorImpl::SetRequiresMouseWheelEvents(bool)
179    * @param[in] event The mouse wheel event.
180    * @return True if the event should be consumed.
181    */
182   virtual bool OnMouseWheelEvent(const MouseWheelEvent& event) = 0;
183
184   /**
185    * @brief Called after the size negotiation has been finished for this control.
186    *
187    * The control is expected to assign this given size to itself/its children.
188    *
189    * Should be overridden by derived classes if they need to layout
190    * actors differently after certain operations like add or remove
191    * actors, resize or after changing specific properties.
192    *
193    * Note! As this function is called from inside the size negotiation algorithm, you cannot
194    * call RequestRelayout (the call would just be ignored)
195    *
196    * @param[in]      size       The allocated size.
197    * @param[in,out]  container  The control should add actors to this container that it is not able
198    *                            to allocate a size for.
199    */
200   virtual void OnRelayout( const Vector2& size, RelayoutContainer& container ) = 0;
201
202   /**
203    * @brief Notification for deriving classes
204    *
205    * @param[in] policy The policy being set
206    * @param[in] dimension The dimension the policy is being set for
207    */
208   virtual void OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension ) = 0;
209
210   /**
211    * Return the natural size of the actor
212    *
213    * @return The actor's natural size
214    */
215   virtual Vector3 GetNaturalSize() = 0;
216
217   /**
218    * @brief Calculate the size for a child
219    *
220    * @param[in] child The child actor to calculate the size for
221    * @param[in] dimension The dimension to calculate the size for. E.g. width or height.
222    * @return Return the calculated size for the given dimension
223    */
224   virtual float CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension ) = 0;
225
226   /**
227    * @brief This method is called during size negotiation when a height is required for a given width.
228    *
229    * Derived classes should override this if they wish to customize the height returned.
230    *
231    * @param width to use.
232    * @return the height based on the width.
233    */
234   virtual float GetHeightForWidth( float width ) = 0;
235
236   /**
237    * @brief This method is called during size negotiation when a width is required for a given height.
238    *
239    * Derived classes should override this if they wish to customize the width returned.
240    *
241    * @param height to use.
242    * @return the width based on the width.
243    */
244   virtual float GetWidthForHeight( float height ) = 0;
245
246   /**
247    * @brief Determine if this actor is dependent on it's children for relayout
248    *
249    * @param dimension The dimension(s) to check for
250    * @return Return if the actor is dependent on it's children
251    */
252   virtual bool RelayoutDependentOnChildren( Dimension::Type dimension = Dimension::ALL_DIMENSIONS ) = 0;
253
254   /**
255    * @brief Virtual method to notify deriving classes that relayout dependencies have been
256    * met and the size for this object is about to be calculated for the given dimension
257    *
258    * @param dimension The dimension that is about to be calculated
259    */
260   virtual void OnCalculateRelayoutSize( Dimension::Type dimension ) = 0;
261
262   /**
263    * @brief Virtual method to notify deriving classes that the size for a dimension
264    * has just been negotiated
265    *
266    * @param[in] size The new size for the given dimension
267    * @param[in] dimension The dimension that was just negotiated
268    */
269   virtual void OnLayoutNegotiated( float size, Dimension::Type dimension ) = 0;
270
271 protected: // For derived classes
272
273   /**
274    * @brief Create a CustomActorImpl.
275    * @param[in] requiresTouchEvents True if the OnTouchEvent() callback is required.
276    */
277   CustomActorImpl(bool requiresTouchEvents);
278
279   /**
280    * @brief Set whether the custom actor requires hover events.
281    * @param[in] requiresHoverEvents True if the OnHoverEvent() callback is required.
282    */
283   void SetRequiresHoverEvents(bool requiresHoverEvents);
284
285   /**
286    * @brief Set whether the custom actor requires mouse wheel events.
287    * @param[in] requiresMouseWheelEvents True if the OnMouseWheelEvent() callback is required.
288    */
289   void SetRequiresMouseWheelEvents(bool requiresMouseWheelEvents);
290
291   /**
292    * @brief Request a relayout, which means performing a size negotiation on this actor, its parent and children (and potentially whole scene)
293    *
294    * This method can also be called from a derived class every time it needs a different size.
295    * At the end of event processing, the relayout process starts and
296    * all controls which requested Relayout will have their sizes (re)negotiated.
297    *
298    * @note RelayoutRequest() can be called multiple times; the size negotiation is still
299    * only performed once, i.e. there is no need to keep track of this in the calling side.
300    */
301   void RelayoutRequest();
302
303   /**
304    * @brief Calculate the size for a child using the base actor object
305    *
306    * @param[in] child The child actor to calculate the size for
307    * @param[in] dimension The dimension to calculate the size for. E.g. width or height.
308    * @return Return the calculated size for the given dimension
309    */
310   float CalculateChildSizeBase( const Dali::Actor& child, Dimension::Type dimension );
311
312   /**
313    * @brief Determine if this actor is dependent on it's children for relayout from the base class
314    *
315    * @param dimension The dimension(s) to check for
316    * @return Return if the actor is dependent on it's children
317    */
318   bool RelayoutDependentOnChildrenBase( Dimension::Type dimension = Dimension::ALL_DIMENSIONS );
319
320 public: // Not intended for application developers
321
322   /**
323    * @brief Called when ownership of the CustomActorImpl is passed to a CustomActor.
324    * @pre The CustomActorImpl is not already owned.
325    * @param[in] owner The owning object.
326    */
327   void Initialize(Internal::CustomActor& owner);
328
329   /**
330    * @brief Get the owner.
331    *
332    * This method is needed when creating additional handle objects to existing objects.
333    * Owner is the Dali::Internal::CustomActor that owns the implementation of the custom actor
334    * inside core. Creation of a handle to Dali public API Actor requires this pointer.
335    * @return a pointer to the Actor implementation that owns this custom actor implementation
336    */
337   Internal::CustomActor* GetOwner() const;
338
339   /**
340    * @brief Called when ownership of the CustomActorImpl is passed to a CustomActor.
341    * @return True if the OnTouchEvent() callback is required.
342    */
343   bool RequiresTouchEvents() const;
344
345   /**
346    * @brief Called when ownership of the CustomActorImpl is passed to a CustomActor.
347    * @return True if the OnHoverEvent() callback is required.
348    */
349   bool RequiresHoverEvents() const;
350
351   /**
352    * @brief Called when ownership of the CustomActorImpl is passed to a CustomActor.
353    * @return True if the OnMouseWheelEvent() callback is required.
354    */
355   bool RequiresMouseWheelEvents() const;
356
357 private:
358
359   Internal::CustomActor* mOwner;  ///< Internal owner of this custom actor implementation
360   bool mRequiresTouchEvents;      ///< Whether the OnTouchEvent() callback is required
361   bool mRequiresHoverEvents;      ///< Whether the OnHoverEvent() callback is required
362   bool mRequiresMouseWheelEvents; ///< Whether the OnMouseWheelEvent() callback is required
363 };
364
365 } // namespace Dali
366
367 #endif // __DALI_CUSTOM_ACTOR_IMPL_H__