Fixed unparenting a layout item from a layout group
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / layout-item-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_ITEM_H
2 #define DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_ITEM_H
3 /*
4  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <memory>
20
21 #include <dali/public-api/common/intrusive-ptr.h>
22 #include <dali/public-api/object/base-object.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/actors/actor-enumerations.h>
25 #include <dali-toolkit/devel-api/layouting/child-layout-data.h>
26 #include <dali-toolkit/devel-api/layouting/layout-item.h>
27 #include <dali-toolkit/devel-api/layouting/layout-child-impl.h>
28 #include <dali-toolkit/devel-api/layouting/layout-controller.h>
29 #include <dali-toolkit/devel-api/layouting/layout-size.h>
30 #include <dali-toolkit/devel-api/layouting/measure-spec.h>
31 #include <dali-toolkit/devel-api/layouting/measured-size.h>
32
33 namespace Dali
34 {
35 namespace Toolkit
36 {
37 namespace Internal
38 {
39
40 class LayoutItem;
41 using LayoutItemPtr = IntrusivePtr<LayoutItem>;
42
43
44 /**
45  * Base class for layouts.
46  */
47 class DALI_TOOLKIT_API LayoutItem : public BaseObject,
48                                     public LayoutChild
49 {
50 public:
51   /**
52    * Constructor.
53    */
54   LayoutItem();
55
56 protected:
57   /**
58    * A reference counted object may only be deleted by calling Unreference()
59    */
60   virtual ~LayoutItem();
61
62 public:
63
64 /**
65    * @brief Construct
66    *
67    * @param[in] owner The owner (container view / child view / visual ) of this layout
68    * @return a new LayoutItem object
69    */
70   static LayoutItemPtr New( Handle& owner );
71
72   /**
73    * @brief Remove the default copy constructor
74    */
75   LayoutItem(const LayoutItem& copy)=delete;
76
77   /**
78    * @brief Remove the default assignment operator
79    */
80   LayoutItem& operator=(const LayoutItem& rhs)=delete;
81
82   /**
83    * @brief Initialize the layout with it's owner and owner's type name
84    * @param[in] owner a handle to the owner container
85    * @param[in] containerType the type name of the owner container
86    */
87   void Initialize( Handle& owner, const std::string& containerType );
88
89   /**
90    * @brief Get a handle to the owner of this layout
91    *
92    * @return a handle to the owner of this layout
93    */
94   Handle GetOwner() const;
95
96   /**
97    * @brief Unparent this layout from it's parent, remove it from it's owner
98    * and remove any layout children in derived types.
99    */
100   void Unparent();
101
102   /**
103    * @brief Set whether this layout should be animated or not
104    *
105    * @param[in] animateLayout True if the layout should be animated when applied
106    */
107   void SetAnimateLayout( bool animateLayout );
108
109   /**
110    * @brief Get whether this layout should be animated or not
111    *
112    * @return True if the layout should be animated when applied
113    */
114   bool IsLayoutAnimated() const;
115
116   /**
117    * @brief  This is called to find out how big a layout should be.
118    *
119    * The parent supplies constraint information in the width and height parameters.
120    *
121    * The actual measurement work of a layout is performed in
122    * {@link #OnMeasure(MeasureSpec, MeasureSpec)}, called by this method. Therefore, only
123    * {@link #OnMeasure(MeasureSpec, MeasureSpec)} can and must be overridden by subclasses.
124    *
125    * @param[in] widthMeasureSpec Horizontal space requirements as imposed by the parent
126    * @param[in] heightMeasureSpec Vertical space requirements as imposed by the parent
127    *
128    * @see #OnMeasure(MeasureSpec, MeasureSpec)
129    */
130   void Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec );
131
132   /**
133    * @brief Assign a size and position to a layout and all of its descendants.
134    *
135    * This is the second phase of the layout mechanism.  (The first is measuring). In this phase, each parent
136    * calls layout on all of its children to position them.  This is typically done using the child
137    * measurements that were stored in the measure pass.
138    *
139    * Derived classes with children should override OnLayout. In that method, they should call Layout on each
140    * of their children.
141    *
142    * @param[in] left position, relative to parent
143    * @param[in] top position, relative to parent
144    * @param[in] right position, relative to parent
145    * @param[in] bottom position, relative to parent
146    */
147   void Layout( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
148
149   /**
150    * @brief Utility to return a default size.
151    *
152    * Uses the supplied size if the MeasureSpec imposed no constraints. Will get larger if allowed by the
153    * MeasureSpec.
154    *
155    * @param[in] size Default size for this layout
156    * @param[in] measureSpec Constraints imposed by the parent
157    * @return The size this layout should be.
158    */
159   static LayoutLength GetDefaultSize( LayoutLength size, MeasureSpec measureSpec );
160
161   /**
162    * @copydoc LayoutChild::SetParent
163    */
164   void SetParent( LayoutParent* parent ) override;
165
166   /**
167    * @copydoc LayoutChild::GetParent
168    */
169   LayoutParent* GetParent() override;
170
171   /**
172    * @brief Request that this layout is re-laid out.
173    *
174    * This will make this layout and all it's parent layouts dirty.
175    */
176   void RequestLayout();
177
178   /**
179    * @brief Predicate to determine if this layout has been requested to re-layout
180    *
181    * @return True if a layout request has occured on this layout
182    */
183   bool IsLayoutRequested() const;
184
185   /**
186    * @brief Set layout requested flag (mark the layout dirty).
187    */
188   void SetLayoutRequested();
189
190   /**
191    * @brief Get the measured width (without any measurement flags).
192    *
193    * This method should be used only during measurement and layout calculations.
194    *
195    * Use {@link Dali::Actor::GetTargetSize()} to see how wide a control is after layout
196    */
197   LayoutLength GetMeasuredWidth() const;
198
199   /**
200    * @brief Get the measured height (without any measurement flags).
201    *
202    * This method should be used only during measurement and layout calculations.
203    *
204    * Use {@link Dali::Actor::GetTargetSize()} to see how high a control is after layout
205    */
206   LayoutLength GetMeasuredHeight() const;
207
208   /**
209    * @brief Get the measured width and state.
210    *
211    * This method should be used only during measurement and layout calculations.
212    *
213    * Use {@link Dali::Actor::GetTargetSize()} to see how wide a view is after layout
214    */
215   MeasuredSize GetMeasuredWidthAndState() const;
216
217   /**
218    * @brief Get the measured height and state.
219    *
220    * This method should be used only during measurement and layout calculations.
221    *
222    * Use {@link Dali::Actor::GetTargetSize()} to see how high a view is after layout
223    */
224   MeasuredSize GetMeasuredHeightAndState() const;
225
226   /**
227    * @brief Returns the suggested minimum width that the layout should use.
228    *
229    * This returns the maximum of the layout's minimum width and the background's minimum width
230    *
231    * When being used in {@link #OnMeasure()}, the caller should still
232    * ensure the returned width is within the requirements of the parent.
233    *
234    * @return The suggested minimum width of the layout.
235    */
236   LayoutLength GetSuggestedMinimumWidth() const;
237
238   /**
239    * @brief Returns the suggested minimum height that the layout should use.
240    *
241    * This returns the maximum of the layout's minimum height and the background's minimum height
242    *
243    * When being used in {@link #OnMeasure()}, the caller should still
244    * ensure the returned height is within the requirements of the parent.
245    *
246    * @return The suggested minimum height of the layout.
247    */
248   LayoutLength GetSuggestedMinimumHeight() const;
249
250   /**
251    * @brief Sets the minimum width of the layout.
252    *
253    * It is not guaranteed the layout will be able to achieve this minimum width (for example, if its parent
254    * layout constrains it with less available width).
255    *
256    * @param[in] minWidth The minimum width the layout will try to be, in pixels
257    *
258    * @see #GetMinimumWidth()
259    */
260   void SetMinimumWidth( LayoutLength minWidth );
261
262   /**
263    * @brief Sets the minimum height of the layout.
264    *
265    * It is not guaranteed the layout will be able to achieve this minimum height (for example, if its parent
266    * layout constrains it with less available height).
267    *
268    * @param[in] minHeight The minimum height the layout will try to be, in pixels
269    *
270    * @see #GetMinimumHeight()
271    */
272   void SetMinimumHeight( LayoutLength minHeight );
273
274   /**
275    * @brief Returns the minimum width of the layout.
276    *
277    * @return the minimum width the layout will try to be, in pixels
278    *
279    * @see #SetMinimumWidth(LayoutLength)
280    */
281   LayoutLength GetMinimumWidth() const;
282
283   /**
284    * @brief Returns the minimum height of the layout.
285    *
286    * @return the minimum height the layout will try to be, in pixels
287    *
288    * @see #SetMinimumHeight(LayoutLength)
289    */
290   LayoutLength GetMinimumHeight() const;
291
292   /**
293    * Get the padding information.
294    * @return The padding information
295    */
296   Extents GetPadding() const;
297
298   /**
299    * Get the margin information.
300    * @return The margin information
301    */
302   Extents GetMargin() const;
303
304 protected:
305   /**
306    * @brief Allow directly deriving classes to remove layout children when unparented
307    */
308   virtual void OnUnparent(){};
309
310   /**
311    * @brief Ensure direct derived types register their child properties with the owner
312    *
313    * @param[in] containerType The type name of the owner container
314    */
315   virtual void OnRegisterChildProperties( const std::string& containerType );
316
317   /**
318    * @brief Measure the layout and its content to determine the measured width and the
319    * measured height.
320    *
321    * This method is invoked by {@link #Measure(MeasureSpec, MeasureSpec)} and
322    * should be overridden by subclasses to provide accurate and efficient
323    * measurement of their contents.
324    *
325    * <strong>CONTRACT:</strong> When overriding this method, you
326    * <em>must</em> call {@link #SetMeasuredDimensions(MeasuredSize,MeasuredSize)} to store the
327    * measured width and height of this layout. Failure to do so will trigger an
328    * <code>IllegalStateException</code>, thrown by
329    * {@link #Measure(MeasureSpec,MeasureSpec)}. Calling the superclass'
330    * {@link #OnMeasure(MeasureSpec,MeasureSpec)} is a valid use.
331    *
332    * The base class implementation of measure defaults to the background size,
333    * unless a larger size is allowed by the MeasureSpec. Subclasses should
334    * override {@link #OnMeasure(MeasureSpec,MeasureSpec)} to provide better measurements of
335    * their content.
336    *
337    * If this method is overridden, it is the subclass's responsibility to make
338    * sure the measured height and width are at least the layout's minimum height
339    * and width ({@link #GetSuggestedMinimumHeight()} and
340    * {@link #GetSuggestedMinimumWidth()}).
341    *
342    * @param[in] widthMeasureSpec horizontal space requirements as imposed by the parent.
343    *                             The requirements are encoded with
344    *                             {@link MeasureSpec}.
345    * @param[in] heightMeasureSpec vertical space requirements as imposed by the parent.
346    *                              The requirements are encoded with
347    *                              {@link MeasureSpec}.
348    *
349    * @see #GetMeasuredWidth()
350    * @see #GetMeasuredHeight()
351    * @see #GetSuggestedMinimumHeight()
352    * @see #GetSuggestedMinimumWidth()
353    * @see MeasureSpec#GetMode(int)
354    * @see MeasureSpec#GetSize(int)
355    */
356   virtual void OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec );
357
358   /**
359    * @brief Called from Layout() when this layout should assign a size and position to each of its children.
360    *
361    * Derived classes with children should override this method and call Layout() on each of their children.
362    *
363    * @param[in] changed This is a new size or position for this layout
364    * @param[in] left Left position, relative to parent
365    * @param[in] top Top position, relative to parent
366    * @param[in] right Right position, relative to parent
367    * @param[in] bottom Bottom position, relative to parent
368    */
369   virtual void OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
370
371
372   /**
373    * @brief This method must be called by {@link #OnMeasure(MeasureSpec,MeasureSpec)} to store the
374    * measured width and measured height.
375    *
376    * Failing to do so will trigger an exception at measurement time.
377    *
378    * @param[in] measuredWidth The measured width of this layout. This may have a state of
379    * {@link MeasuredSize::MEASURED_SIZE_TOO_SMALL}
380    *
381    * @param[in] measuredHeight The measured height of this layout. This may have a state of
382    * {@link MeasuredSize::MEASURED_SIZE_TOO_SMALL}
383    */
384   void SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight );
385
386   /**
387    * @brief Utility to reconcile a desired size and state, with constraints imposed by a MeasureSpec.
388    *
389    * @param[in] size How big the layout wants to be.
390    * @param[in] measureSpec Constraints imposed by the parent.
391    * @param[in] childMeasuredState Size information bit mask for the layout's children.
392    *
393    * @return A measured size, which may indicate that it is too small.
394    */
395   static MeasuredSize ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec,
396                                            MeasuredSize::State childMeasuredState );
397
398   /**
399    * @brief Sets the frame (the size and position) of the layout onto it's owner
400    *
401    * @todo Consider instead, collating properties into LayoutCollector in order to set/animate them all
402    * in one block.
403    * @param[in] left The horizontal position of the left edge of this frame within the parent layout
404    * @param[in] top The vertical position of the top edge of this frame within the parent layout
405    * @param[in] right The horizontal position of the right edge of this frame within the parent layout
406    * @param[in] bottom The vertical position of the bottom edge of this frame within the parent layout
407    */
408   bool SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
409
410   /**
411    * Virtual method to inform derived classes when the layout size changed
412    * @param[in] newSize The new size of the layout
413    * @param[in] oldSize The old size of the layout
414    */
415   virtual void OnSizeChanged( LayoutSize newSize, LayoutSize oldSize );
416
417
418   /**
419    * @brief Initialization method for LayoutGroup to override
420    */
421   virtual void OnInitialize();
422
423   /**
424    * @brief Called when a layer animation state is changed.
425    */
426   virtual void OnAnimationStateChanged( bool animateLayout ) {}
427
428 private:
429   /**
430    * @brief Called to change the size of the layout.
431    *
432    * @param[in] newSize The new size of the layout
433    * @param[in] oldSize The old size of the layout
434    */
435   void SizeChange( LayoutSize newSize, LayoutSize oldSize );
436
437   /**
438    * @brief Triggered when a layout animation finished.
439    *
440    * @param[in] animation  A handle to the layout animation
441    */
442   void OnLayoutAnimationFinished( Animation& animation );
443
444   /**
445    * @brief Register child properties of layout with owner type.
446    *
447    * The Actor hierarchy uses these registered properties in the type
448    * system to ensure child custom properties are properly initialized.
449    *
450    * @param[in] containerType The type of the containing view (owner)
451    */
452   void RegisterChildProperties( const std::string& containerType );
453
454 public:
455   class Impl; // Class declaration is public so we can add devel API's in the future
456
457
458 private:
459   std::unique_ptr<Impl> mImpl; ///< Implementation class holds all the data
460   SlotDelegate<LayoutItem> mSlotDelegate;
461 };
462
463 } //namespace Internal
464
465 inline Internal::LayoutItem& GetImplementation( Dali::Toolkit::LayoutItem& handle )
466 {
467   DALI_ASSERT_ALWAYS( handle && "LayoutItem handle is empty" );
468   BaseObject& object = handle.GetBaseObject();
469   return static_cast< Internal::LayoutItem& >( object );
470 }
471
472 inline const Internal::LayoutItem& GetImplementation( const Dali::Toolkit::LayoutItem& handle )
473 {
474   DALI_ASSERT_ALWAYS( handle && "LayoutItem handle is empty" );
475   const BaseObject& object = handle.GetBaseObject();
476   return static_cast< const Internal::LayoutItem& >( object );
477 }
478
479 } //namespace Toolkit
480 } //namespace Dali
481
482 #endif // DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_ITEM_H