Merge "Fix linear layout size issue" into devel/master
[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-parent-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 LayoutParent
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 owner, and remove any layout children in derived types
98    */
99   void Unparent();
100
101   /**
102    * @brief Set whether this layout should be animated or not
103    *
104    * @param[in] animateLayout True if the layout should be animated when applied
105    */
106   void SetAnimateLayout( bool animateLayout );
107
108   /**
109    * @brief Get whether this layout should be animated or not
110    *
111    * @return True if the layout should be animated when applied
112    */
113   bool IsLayoutAnimated() const;
114
115   /**
116    * @brief Register child properties of layout with owner type.
117    *
118    * The Actor hierarchy uses these registered properties in the type
119    * system to ensure child custom properties are properly initialized.
120    *
121    * @param[in] containerType The type of the containing view (owner)
122    */
123   void RegisterChildProperties( const std::string& containerType );
124
125   /**
126    * @brief  This is called to find out how big a layout should be.
127    *
128    * The parent supplies constraint information in the width and height parameters.
129    *
130    * The actual measurement work of a layout is performed in
131    * {@link #OnMeasure(MeasureSpec, MeasureSpec)}, called by this method. Therefore, only
132    * {@link #OnMeasure(MeasureSpec, MeasureSpec)} can and must be overridden by subclasses.
133    *
134    * @param[in] widthMeasureSpec Horizontal space requirements as imposed by the parent
135    * @param[in] heightMeasureSpec Vertical space requirements as imposed by the parent
136    *
137    * @see #OnMeasure(MeasureSpec, MeasureSpec)
138    */
139   void Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec );
140
141   /**
142    * @brief Assign a size and position to a layout and all of its descendants.
143    *
144    * This is the second phase of the layout mechanism.  (The first is measuring). In this phase, each parent
145    * calls layout on all of its children to position them.  This is typically done using the child
146    * measurements that were stored in the measure pass.
147    *
148    * Derived classes with children should override OnLayout. In that method, they should call Layout on each
149    * of their children.
150    *
151    * @param[in] left position, relative to parent
152    * @param[in] top position, relative to parent
153    * @param[in] right position, relative to parent
154    * @param[in] bottom position, relative to parent
155    */
156   void Layout( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
157
158   /**
159    * @brief Utility to return a default size.
160    *
161    * Uses the supplied size if the MeasureSpec imposed no constraints. Will get larger if allowed by the
162    * MeasureSpec.
163    *
164    * @param[in] size Default size for this layout
165    * @param[in] measureSpec Constraints imposed by the parent
166    * @return The size this layout should be.
167    */
168   static LayoutLength GetDefaultSize( LayoutLength size, MeasureSpec measureSpec );
169
170   /**
171    * @copydoc LayoutParent::SetParent
172    */
173   virtual void SetParent( LayoutParent* parent ) override;
174
175   /**
176    * @copydoc LayoutParent::GetParent
177    */
178   virtual LayoutParent* GetParent() override;
179
180   /**
181    * @brief Request that this layout is re-laid out.
182    *
183    * This will make this layout and all it's parent layouts dirty.
184    */
185   void RequestLayout();
186
187   /**
188    * @brief Predicate to determine if this layout has been requested to re-layout
189    *
190    * @return True if a layout request has occured on this layout
191    */
192   bool IsLayoutRequested() const;
193
194   /**
195    * @brief Set layout requested flag (mark the layout dirty).
196    */
197   void SetLayoutRequested();
198
199   /**
200    * @brief Get the measured width (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 wide a control is after layout
205    */
206   LayoutLength GetMeasuredWidth() const;
207
208   /**
209    * @brief Get the measured height (without any measurement flags).
210    *
211    * This method should be used only during measurement and layout calculations.
212    *
213    * Use {@link Dali::Actor::GetTargetSize()} to see how high a control is after layout
214    */
215   LayoutLength GetMeasuredHeight() const;
216
217   /**
218    * @brief Get the measured width 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 wide a view is after layout
223    */
224   MeasuredSize GetMeasuredWidthAndState() const;
225
226   /**
227    * @brief Get the measured height and state.
228    *
229    * This method should be used only during measurement and layout calculations.
230    *
231    * Use {@link Dali::Actor::GetTargetSize()} to see how high a view is after layout
232    */
233   MeasuredSize GetMeasuredHeightAndState() const;
234
235   /**
236    * @brief Returns the suggested minimum width that the layout should use.
237    *
238    * This returns the maximum of the layout's minimum width and the background's minimum width
239    *
240    * When being used in {@link #OnMeasure()}, the caller should still
241    * ensure the returned width is within the requirements of the parent.
242    *
243    * @return The suggested minimum width of the layout.
244    */
245   LayoutLength GetSuggestedMinimumWidth() const;
246
247   /**
248    * @brief Returns the suggested minimum height that the layout should use.
249    *
250    * This returns the maximum of the layout's minimum height and the background's minimum height
251    *
252    * When being used in {@link #OnMeasure()}, the caller should still
253    * ensure the returned height is within the requirements of the parent.
254    *
255    * @return The suggested minimum height of the layout.
256    */
257   LayoutLength GetSuggestedMinimumHeight() const;
258
259   /**
260    * @brief Sets the minimum width of the layout.
261    *
262    * It is not guaranteed the layout will be able to achieve this minimum width (for example, if its parent
263    * layout constrains it with less available width).
264    *
265    * @param[in] minWidth The minimum width the layout will try to be, in pixels
266    *
267    * @see #GetMinimumWidth()
268    */
269   void SetMinimumWidth( LayoutLength minWidth );
270
271   /**
272    * @brief Sets the minimum height of the layout.
273    *
274    * It is not guaranteed the layout will be able to achieve this minimum height (for example, if its parent
275    * layout constrains it with less available height).
276    *
277    * @param[in] minHeight The minimum height the layout will try to be, in pixels
278    *
279    * @see #GetMinimumHeight()
280    */
281   void SetMinimumHeight( LayoutLength minHeight );
282
283   /**
284    * @brief Returns the minimum width of the layout.
285    *
286    * @return the minimum width the layout will try to be, in pixels
287    *
288    * @see #SetMinimumWidth(LayoutLength)
289    */
290   LayoutLength GetMinimumWidth() const;
291
292   /**
293    * @brief Returns the minimum height of the layout.
294    *
295    * @return the minimum height the layout will try to be, in pixels
296    *
297    * @see #SetMinimumHeight(LayoutLength)
298    */
299   LayoutLength GetMinimumHeight() const;
300
301   /**
302    * Get the padding information.
303    * @return The padding information
304    */
305   Extents GetPadding() const;
306
307   /**
308    * Get the margin information.
309    * @return The margin information
310    */
311   Extents GetMargin() const;
312
313 protected:
314   /**
315    * @brief Allow directly deriving classes to remove layout children when unparented
316    */
317   virtual void OnUnparent(){};
318
319   /**
320    * @brief Ensure direct derived types register their child properties with the owner
321    *
322    * @param[in] containerType The type name of the owner container
323    */
324   virtual void OnRegisterChildProperties( const std::string& containerType );
325
326   /**
327    * @brief Measure the layout and its content to determine the measured width and the
328    * measured height.
329    *
330    * This method is invoked by {@link #Measure(MeasureSpec, MeasureSpec)} and
331    * should be overridden by subclasses to provide accurate and efficient
332    * measurement of their contents.
333    *
334    * <strong>CONTRACT:</strong> When overriding this method, you
335    * <em>must</em> call {@link #SetMeasuredDimensions(MeasuredSize,MeasuredSize)} to store the
336    * measured width and height of this layout. Failure to do so will trigger an
337    * <code>IllegalStateException</code>, thrown by
338    * {@link #Measure(MeasureSpec,MeasureSpec)}. Calling the superclass'
339    * {@link #OnMeasure(MeasureSpec,MeasureSpec)} is a valid use.
340    *
341    * The base class implementation of measure defaults to the background size,
342    * unless a larger size is allowed by the MeasureSpec. Subclasses should
343    * override {@link #OnMeasure(MeasureSpec,MeasureSpec)} to provide better measurements of
344    * their content.
345    *
346    * If this method is overridden, it is the subclass's responsibility to make
347    * sure the measured height and width are at least the layout's minimum height
348    * and width ({@link #GetSuggestedMinimumHeight()} and
349    * {@link #GetSuggestedMinimumWidth()}).
350    *
351    * @param[in] widthMeasureSpec horizontal space requirements as imposed by the parent.
352    *                             The requirements are encoded with
353    *                             {@link MeasureSpec}.
354    * @param[in] heightMeasureSpec vertical space requirements as imposed by the parent.
355    *                              The requirements are encoded with
356    *                              {@link MeasureSpec}.
357    *
358    * @see #GetMeasuredWidth()
359    * @see #GetMeasuredHeight()
360    * @see #GetSuggestedMinimumHeight()
361    * @see #GetSuggestedMinimumWidth()
362    * @see MeasureSpec#GetMode(int)
363    * @see MeasureSpec#GetSize(int)
364    */
365   virtual void OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec );
366
367   /**
368    * @brief Called from Layout() when this layout should assign a size and position to each of its children.
369    *
370    * Derived classes with children should override this method and call Layout() on each of their children.
371    *
372    * @param[in] changed This is a new size or position for this layout
373    * @param[in] left Left position, relative to parent
374    * @param[in] top Top position, relative to parent
375    * @param[in] right Right position, relative to parent
376    * @param[in] bottom Bottom position, relative to parent
377    */
378   virtual void OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
379
380
381   /**
382    * @brief This method must be called by {@link #OnMeasure(MeasureSpec,MeasureSpec)} to store the
383    * measured width and measured height.
384    *
385    * Failing to do so will trigger an exception at measurement time.
386    *
387    * @param[in] measuredWidth The measured width of this layout. This may have a state of
388    * {@link MeasuredSize::MEASURED_SIZE_TOO_SMALL}
389    *
390    * @param[in] measuredHeight The measured height of this layout. This may have a state of
391    * {@link MeasuredSize::MEASURED_SIZE_TOO_SMALL}
392    */
393   void SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight );
394
395   /**
396    * @brief Utility to reconcile a desired size and state, with constraints imposed by a MeasureSpec.
397    *
398    * @param[in] size How big the layout wants to be.
399    * @param[in] measureSpec Constraints imposed by the parent.
400    * @param[in] childMeasuredState Size information bit mask for the layout's children.
401    *
402    * @return A measured size, which may indicate that it is too small.
403    */
404   static MeasuredSize ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec,
405                                            MeasuredSize::State childMeasuredState );
406
407   /**
408    * @brief Sets the frame (the size and position) of the layout onto it's owner
409    *
410    * @todo Consider instead, collating properties into LayoutCollector in order to set/animate them all
411    * in one block.
412    * @param[in] left The horizontal position of the left edge of this frame within the parent layout
413    * @param[in] top The vertical position of the top edge of this frame within the parent layout
414    * @param[in] right The horizontal position of the right edge of this frame within the parent layout
415    * @param[in] bottom The vertical position of the bottom edge of this frame within the parent layout
416    */
417   bool SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom );
418
419   /**
420    * Virtual method to inform derived classes when the layout size changed
421    * @param[in] newSize The new size of the layout
422    * @param[in] oldSize The old size of the layout
423    */
424   virtual void OnSizeChanged( LayoutSize newSize, LayoutSize oldSize );
425
426
427   /**
428    * @brief Initialization method for LayoutGroup to override
429    */
430   virtual void OnInitialize();
431
432 private:
433   /**
434    * @brief Called to change the size of the layout.
435    *
436    * @param[in] newSize The new size of the layout
437    * @param[in] oldSize The old size of the layout
438    */
439   void SizeChange( LayoutSize newSize, LayoutSize oldSize );
440
441   /**
442    * @brief Triggered when a layout animation finished.
443    *
444    * @param[in] animation  A handle to the layout animation
445    */
446   void OnLayoutAnimationFinished( Animation& animation );
447
448 public:
449   class Impl; // Class declaration is public so we can add devel API's in the future
450
451
452 private:
453   std::unique_ptr<Impl> mImpl; ///< Implementation class holds all the data
454   SlotDelegate<LayoutItem> mSlotDelegate;
455 };
456
457 } //namespace Internal
458
459 inline Internal::LayoutItem& GetImplementation( Dali::Toolkit::LayoutItem& handle )
460 {
461   DALI_ASSERT_ALWAYS( handle && "LayoutItem handle is empty" );
462   BaseObject& object = handle.GetBaseObject();
463   return static_cast< Internal::LayoutItem& >( object );
464 }
465
466 inline const Internal::LayoutItem& GetImplementation( const Dali::Toolkit::LayoutItem& handle )
467 {
468   DALI_ASSERT_ALWAYS( handle && "LayoutItem handle is empty" );
469   const BaseObject& object = handle.GetBaseObject();
470   return static_cast< const Internal::LayoutItem& >( object );
471 }
472
473 } //namespace Toolkit
474 } //namespace Dali
475
476 #endif // DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_ITEM_H