[NUI] add exception docs of layout classes (#2052)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Layouting / FlexLayout.cs
1 /* Copyright (c) 2019 Samsung Electronics Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */
16
17 using System;
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using System.Runtime.InteropServices;
21 using System.Diagnostics;
22 using Tizen.NUI.Binding;
23
24 namespace Tizen.NUI
25 {
26     /// <summary>
27     /// This class implements a flex layout.
28     /// The flex layout implementation is based on open source Facebook Yoga layout engine.
29     /// For more information about the flex layout API and how to use it please refer to https://yogalayout.com/docs/
30     /// We implement the subset of the API in the class below.
31     /// </summary>
32     public class FlexLayout : LayoutGroup, global::System.IDisposable
33     {
34         /// <summary>
35         /// FlexItemProperty
36         /// </summary>
37         [EditorBrowsable(EditorBrowsableState.Never)]
38         internal static readonly BindableProperty FlexItemProperty = BindableProperty.CreateAttached("FlexItem", typeof(HandleRef), typeof(FlexLayout), default(HandleRef));
39
40         /// <summary>
41         /// FlexAlignmentSelfProperty
42         /// </summary>
43         [EditorBrowsable(EditorBrowsableState.Never)]
44         public static readonly BindableProperty FlexAlignmentSelfProperty = BindableProperty.CreateAttached("FlexAlignmentSelf", typeof(AlignmentType), typeof(FlexLayout), AlignmentType.Auto, validateValue: ValidateEnum((int)AlignmentType.Auto, (int)AlignmentType.Stretch), propertyChanged: OnChildPropertyChanged);
45
46         /// <summary>
47         /// FlexPositionTypeProperty
48         /// </summary>
49         [EditorBrowsable(EditorBrowsableState.Never)]
50         public static readonly BindableProperty FlexPositionTypeProperty = BindableProperty.CreateAttached("FlexPositionType", typeof(PositionType), typeof(FlexLayout), PositionType.Relative, validateValue: ValidateEnum((int)PositionType.Relative, (int)PositionType.Absolute), propertyChanged: OnChildPropertyChanged);
51
52         /// <summary>
53         /// AspectRatioProperty
54         /// </summary>
55         [EditorBrowsable(EditorBrowsableState.Never)]
56         public static readonly BindableProperty FlexAspectRatioProperty = BindableProperty.CreateAttached("FlexAspectRatio", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value > 0, propertyChanged: OnChildPropertyChanged);
57
58         /// <summary>
59         /// FlexBasisProperty
60         /// </summary>
61         [EditorBrowsable(EditorBrowsableState.Never)]
62         public static readonly BindableProperty FlexBasisProperty = BindableProperty.CreateAttached("FlexBasis", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
63
64         /// <summary>
65         /// FlexShrinkProperty
66         /// </summary>
67         [EditorBrowsable(EditorBrowsableState.Never)]
68         public static readonly BindableProperty FlexShrinkProperty = BindableProperty.CreateAttached("FlexShrink", typeof(float), typeof(FlexLayout), 1.0f, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
69
70         /// <summary>
71         /// FlexGrowProperty
72         /// </summary>
73         [EditorBrowsable(EditorBrowsableState.Never)]
74         public static readonly BindableProperty FlexGrowProperty = BindableProperty.CreateAttached("FlexGrow", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
75
76         private static bool LayoutDebugFlex = false; // Debug flag
77
78         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
79         private bool swigCMemOwn;
80         private bool disposed;
81         private bool isDisposeQueued = false;
82
83         private MeasureSpecification parentMeasureSpecificationWidth;
84         private MeasureSpecification parentMeasureSpecificationHeight;
85
86         private IntPtr _rootFlex;  // Pointer to the unmanged flex layout class.
87
88         internal const float FlexUndefined = 10E20F; // Auto setting which is equivalent to WrapContent.
89
90         internal struct MeasuredSize
91         {
92             public MeasuredSize(float x, float y)
93             {
94                 width = x;
95                 height = y;
96             }
97             public float width;
98             public float height;
99         };
100
101         /// <summary>
102         /// Gets the alignment self of the child view.
103         /// </summary>
104         /// <seealso cref="SetFlexAlignmentSelf(View, AlignmentType)"/>
105         /// <param name="view">The child view.</param>
106         /// <returns> The value of alignment self.</returns>
107         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
108         /// <since_tizen> 8 </since_tizen>
109         public static AlignmentType GetFlexAlignmentSelf(View view) => GetAttachedValue<AlignmentType>(view, FlexAlignmentSelfProperty);
110
111         /// <summary>
112         /// Gets the position type of the child view.
113         /// </summary>
114         /// <seealso cref="SetFlexPositionType(View, PositionType)"/>
115         /// <param name="view">The child view.</param>
116         /// <returns> The value of position type</returns>
117         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
118         /// <since_tizen> 8 </since_tizen>
119         public static PositionType GetFlexPositionType(View view) => GetAttachedValue<PositionType>(view, FlexPositionTypeProperty);
120
121         /// <summary>
122         /// Gets the aspect ratio of the child view.
123         /// </summary>
124         /// <seealso cref="SetFlexAspectRatio(View, float)"/>
125         /// <param name="view">The child view.</param>
126         /// <returns> The value of aspect ratio</returns>
127         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
128         /// <since_tizen> 8 </since_tizen>
129         public static float GetFlexAspectRatio(View view) => GetAttachedValue<float>(view, FlexAspectRatioProperty);
130
131         /// <summary>
132         /// Gets the basis of the child view.
133         /// </summary>
134         /// <seealso cref="SetFlexBasis(View, float)"/>
135         /// <param name="view">The child view.</param>
136         /// <returns> The value of basis</returns>
137         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
138         /// <since_tizen> 8 </since_tizen>
139         public static float GetFlexBasis(View view) => GetAttachedValue<float>(view, FlexBasisProperty);
140
141         /// <summary>
142         /// Gets the shrink of the child view.
143         /// </summary>
144         /// <seealso cref="SetFlexShrink(View, float)"/>
145         /// <param name="view">The child view.</param>
146         /// <returns> The value of shrink</returns>
147         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
148         /// <since_tizen> 8 </since_tizen>
149         public static float GetFlexShrink(View view) => GetAttachedValue<float>(view, FlexShrinkProperty);
150
151         /// <summary>
152         /// Gets the grow of the child view.
153         /// </summary>
154         /// <seealso cref="SetFlexGrow(View, float)"/>
155         /// <param name="view">The child view.</param>
156         /// <returns> The value of grow</returns>
157         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
158         /// <since_tizen> 8 </since_tizen>
159         public static float GetFlexGrow(View view) => GetAttachedValue<float>(view, FlexGrowProperty);
160
161         /// <summary>
162         /// Sets the alignment self of the child view.<br/>
163         /// Alignment self has the same options and effect as <see cref="ItemsAlignment"/> but instead of affecting the children within a container,
164         /// you can apply this property to a single child to change its alignment within its parent.<br/>
165         /// Alignment self overrides any option set by the parent with <see cref="ItemsAlignment"/>.
166         /// </summary>
167         /// <param name="view">The child view.</param>
168         /// <param name="value">The value of alignment self.</param>
169         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
170         /// <exception cref="ArgumentException">The <paramref name="value"/> should be <see cref="AlignmentType"/>.</exception>
171         /// <since_tizen> 8 </since_tizen>
172         public static void SetFlexAlignmentSelf(View view, AlignmentType value) => SetAttachedValue(view, FlexAlignmentSelfProperty, value);
173
174         /// <summary>
175         /// Sets the position type of the child view.<br/>
176         /// The position type of an element defines how it is positioned within its parent.
177         /// By default a child is positioned relatively. This means a child is positioned according to the normal flow of the layout,
178         /// and then offset relative to that position based on the values of <see cref="View.Margin"/>.<br/>
179         /// When positioned absolutely an element doesn't take part in the normal layout flow.
180         /// It is instead laid out independent of its siblings. The position is determined based on the <see cref="View.Margin"/>.
181         /// </summary>
182         /// <param name="view">The child view.</param>
183         /// <param name="value">The value of position type.</param>
184         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
185         /// <exception cref="ArgumentException">The <paramref name="value"/> should be <see cref="PositionType"/>.</exception>
186         /// <since_tizen> 8 </since_tizen>
187         public static void SetFlexPositionType(View view, PositionType value) => SetAttachedValue(view, FlexPositionTypeProperty, value);
188
189         /// <summary>
190         /// Sets the aspect ratio of the child view.
191         /// Aspect ratio Defines as the ratio between the width and the height of a node
192         /// e.g. if a node has an aspect ratio of 2 then its width is twice the size of its height.<br/>
193         /// Aspect ratio accepts any floating point value > 0. this has higher priority than flex grow.
194         /// </summary>
195         /// <param name="view">The child view.</param>
196         /// <param name="value">The value of aspect ratio</param>
197         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
198         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than or equal to 0.0f.</exception>
199         /// <since_tizen> 8 </since_tizen>
200         public static void SetFlexAspectRatio(View view, float value) => SetAttachedValue(view, FlexAspectRatioProperty, value);
201
202         /// <summary>
203         /// Sets the flex basis of the child view.
204         /// Flex basis is an axis-independent way of providing the default size of an item along the main axis.<br/>
205         /// Setting the flex basis of a child is similar to setting the width of that child if its parent is a container with <see cref="FlexDirection.Row"/>
206         /// or setting the height of a child if its parent is a container with <see cref="FlexDirection.Column"/>.<br/>
207         /// The flex basis of an item is the default size of that item, the size of the item before any flex grow and flex shrink calculations are performed.
208         /// </summary>
209         /// <param name="view">The child view.</param>
210         /// <param name="value">The value of basis</param>
211         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
212         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
213         /// <since_tizen> 8 </since_tizen>
214         public static void SetFlexBasis(View view, float value) => SetAttachedValue(view, FlexBasisProperty, value);
215
216         /// <summary>
217         /// Sets the flex shrink of the child view.
218         /// Flex shrink describes how to shrink children along the main axis in the case that the total size of the children overflow the size of the container on the main axis.<br/>
219         /// Flex shrink is very similar to flex grow and can be thought of in the same way if any overflowing size is considered to be negative remaining space.
220         /// These two properties also work well together by allowing children to grow and shrink as needed.<br/>
221         /// Flex shrink accepts any floating point value >= 0, with 1 being the default value. A container will shrink its children weighted by the child's flex shrink value.
222         /// </summary>
223         /// <param name="view">The child view.</param>
224         /// <param name="value">The value of shrink</param>
225         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
226         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
227         /// <since_tizen> 8 </since_tizen>
228         public static void SetFlexShrink(View view, float value) => SetAttachedValue(view, FlexShrinkProperty, value);
229
230         /// <summary>
231         /// Sets the grow of the child view.
232         /// Flex grow describes how any space within a container should be distributed among its children along the main axis.
233         /// After laying out its children, a container will distribute any remaining space according to the flex grow values specified by its children.<br/>
234         /// Flex grow accepts any floating point value >= 0, with 0 being the default value.<br/>
235         /// A container will distribute any remaining space among its children weighted by the child's flex grow value.
236         /// </summary>
237         /// <param name="view">The child view.</param>
238         /// <param name="value">The value of flex</param>
239         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
240         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
241         /// <since_tizen> 8 </since_tizen>
242         public static void SetFlexGrow(View view, float value) => SetAttachedValue(view, FlexGrowProperty, value);
243
244         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
245         internal delegate void ChildMeasureCallback( global::System.IntPtr child, float width, int measureModeWidth, float height, int measureModeHeight, out MeasuredSize measureSize );
246
247         event ChildMeasureCallback measureChildDelegate; // Stores a delegate to the child measure callback. Used for all children of this FlexLayout.
248
249         internal FlexLayout(global::System.IntPtr cPtr, bool cMemoryOwn)
250         {
251             swigCMemOwn = cMemoryOwn;
252             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
253             _rootFlex = Interop.FlexLayout.FlexLayout_New();
254             measureChildDelegate = new ChildMeasureCallback(measureChild);
255         }
256
257         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FlexLayout obj)
258         {
259             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
260         }
261
262         /// <inheritdoc/>
263         /// <since_tizen> 6 </since_tizen>
264         public void Dispose()
265         {
266             // Throw exception if Dispose() is called in separate thread.
267             if (!Window.IsInstalled())
268             {
269                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
270             }
271
272             if (isDisposeQueued)
273             {
274                 Dispose(DisposeTypes.Implicit);
275             }
276             else
277             {
278                 Dispose(DisposeTypes.Explicit);
279                 System.GC.SuppressFinalize(this);
280             }
281         }
282
283         /// <inheritdoc/>
284         /// <since_tizen> 6 </since_tizen>
285         protected virtual void Dispose(DisposeTypes type)
286         {
287             if (disposed)
288             {
289                 return;
290             }
291
292             if (type == DisposeTypes.Explicit)
293             {
294                 // Called by User
295                 // Release your own managed resources here.
296                 // You should release all of your own disposable objects here.
297
298             }
299
300             // Release your own unmanaged resources here.
301             // You should not access any managed member here except static instance.
302             // because the execution order of Finalizes is non-deterministic.
303             if (swigCPtr.Handle != global::System.IntPtr.Zero)
304             {
305                 if (swigCMemOwn)
306                 {
307                     swigCMemOwn = false;
308                     Interop.FlexLayout.delete_FlexLayout(swigCPtr);
309                 }
310                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
311             }
312             disposed = true;
313         }
314
315         /// <summary>
316         /// Creates a FlexLayout object.
317         /// </summary>
318         /// <since_tizen> 6 </since_tizen>
319         public FlexLayout() : this(Interop.FlexLayout.FlexLayout_New(), true)
320         {
321             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
322         }
323
324         internal static FlexLayout DownCast(BaseHandle handle)
325         {
326             FlexLayout ret = new FlexLayout(Interop.FlexLayout.FlexLayout_DownCast(BaseHandle.getCPtr(handle)), true);
327             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
328             return ret;
329         }
330
331         internal FlexLayout(FlexLayout other) : this(Interop.FlexLayout.new_FlexLayout__SWIG_1(FlexLayout.getCPtr(other)), true)
332         {
333             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
334         }
335
336         internal FlexLayout.AlignmentType GetFlexAlignment()
337         {
338             FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.FlexLayout_GetFlexAlignment(swigCPtr);
339             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
340             return ret;
341         }
342
343         internal FlexLayout.AlignmentType GetFlexItemsAlignment()
344         {
345             FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.FlexLayout_GetFlexItemsAlignment(swigCPtr);
346             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
347             return ret;
348         }
349
350         /// <summary>
351         /// Gets/Sets the flex direction in the layout.
352         /// The direction of the main-axis which determines the direction that flex items are laid out.
353         /// </summary>
354         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
355         /// <since_tizen> 6 </since_tizen>
356         public FlexDirection Direction
357         {
358             get => (FlexDirection)Interop.FlexLayout.FlexLayout_GetFlexDirection(swigCPtr);
359             set
360             {
361                 if (value < FlexDirection.Column || value > FlexDirection.RowReverse)
362                     throw new InvalidEnumArgumentException(nameof(Direction));
363
364                 Interop.FlexLayout.FlexLayout_SetFlexDirection(swigCPtr, (int)value);
365                 RequestLayout();
366             }
367         }
368
369         /// <summary>
370         /// Gets/Sets the justification in the layout.
371         /// Justify content describes how to align children within the main axis of their container.<br/>
372         /// For example, you can use this property to center a child horizontally within a container with <see cref="Direction"/> set to <see cref="FlexDirection.Row"/>
373         /// or vertically within a container with <see cref="Direction"/> set to <see cref="FlexDirection.Column"/>.
374         /// </summary>
375         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
376         /// <since_tizen> 6 </since_tizen>
377         public FlexJustification Justification
378         {
379             get => (FlexJustification)Interop.FlexLayout.FlexLayout_GetFlexJustification(swigCPtr);
380             set
381             {
382                 if (value < FlexJustification.FlexStart || value > FlexJustification.SpaceAround)
383                     throw new InvalidEnumArgumentException(nameof(Justification));
384
385                 Interop.FlexLayout.FlexLayout_SetFlexJustification(swigCPtr, (int)value);
386                 RequestLayout();
387             }
388         }
389
390         /// <summary>
391         /// Gets/Sets the wrap in the layout.
392         /// The flex wrap property is set on containers and controls what happens when children overflow the size of the container along the main axis.<br/>
393         /// By default children are forced into a single line (which can shrink elements).<br/>
394         /// If wrapping is allowed items are wrapped into multiple lines along the main axis if needed. wrap reverse behaves the same, but the order of the lines is reversed.<br/>
395         /// When wrapping lines <see cref="Alignment"/> can be used to specify how the lines are placed in the container.
396         /// </summary>
397         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
398         /// <since_tizen> 6 </since_tizen>
399         public FlexWrapType WrapType
400         {
401             get => (FlexWrapType)Interop.FlexLayout.FlexLayout_GetFlexWrap(swigCPtr);
402             set
403             {
404                 if (value != FlexWrapType.NoWrap && value != FlexWrapType.Wrap)
405                     throw new InvalidEnumArgumentException(nameof(WrapType));
406
407                 Interop.FlexLayout.FlexLayout_SetFlexWrap(swigCPtr, (int)value);
408                 RequestLayout();
409
410             }
411         }
412
413         /// <summary>
414         /// Gets/Sets the alignment of the layout content.
415         /// Alignment defines the distribution of lines along the cross-axis.<br/>
416         /// This only has effect when items are wrapped to multiple lines using flex wrap.
417         /// </summary>
418         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
419         /// <since_tizen> 6 </since_tizen>
420         public AlignmentType Alignment
421         {
422             get => GetFlexAlignment();
423             set
424             {
425                 if (value < AlignmentType.Auto || value > AlignmentType.Stretch)
426                     throw new InvalidEnumArgumentException(nameof(Alignment));
427
428                 Interop.FlexLayout.FlexLayout_SetFlexAlignment(swigCPtr, (int)value);
429                 RequestLayout();
430             }
431         }
432
433         /// <summary>
434         /// Gets/Sets the alignment of the layout items.
435         /// Items alignment describes how to align children along the cross axis of their container.<br/>
436         /// Align items is very similar to <see cref="Justification"/> but instead of applying to the main axis, align items applies to the cross axis.
437         /// </summary>
438         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
439         /// <since_tizen> 6 </since_tizen>
440         public AlignmentType ItemsAlignment
441         {
442             get => GetFlexItemsAlignment();
443             set
444             {
445                 if (value < AlignmentType.Auto || value > AlignmentType.Stretch)
446                     throw new InvalidEnumArgumentException(nameof(ItemsAlignment));
447
448                 Interop.FlexLayout.FlexLayout_SetFlexItemsAlignment(swigCPtr, (int)value);
449                 RequestLayout();
450             }
451         }
452
453         /// <summary>
454         /// Enumeration for the direction of the main axis in the flex container.
455         /// This determines the direction that flex items are laid out in the flex container.
456         /// </summary>
457         /// <since_tizen> 6 </since_tizen>
458         public enum FlexDirection
459         {
460             /// <summary>
461             /// The flexible items are displayed vertically as a column
462             /// </summary>
463             Column,
464             /// <summary>
465             /// The flexible items are displayed vertically as a column, but in reverse order
466             /// </summary>
467             ColumnReverse,
468             /// <summary>
469             /// The flexible items are displayed horizontally as a row
470             /// </summary>
471             Row,
472             /// <summary>
473             /// The flexible items are displayed horizontally as a row, but in reverse order
474             /// </summary>
475             RowReverse
476         }
477
478         /// <summary>
479         /// Enumeration for the alignment of the flex items when the items do not use all available space on the main-axis.
480         /// </summary>
481         /// <since_tizen> 6 </since_tizen>
482         public enum FlexJustification
483         {
484             /// <summary>
485             /// Items are positioned at the beginning of the container.
486             /// </summary>
487             FlexStart,
488             /// <summary>
489             /// Items are positioned at the center of the container.
490             /// </summary>
491             Center,
492             /// <summary>
493             /// Items are positioned at the end of the container.
494             /// </summary>
495             FlexEnd,
496             /// <summary>
497             /// Items are positioned with equal space between the lines.
498             /// </summary>
499             SpaceBetween,
500             /// <summary>
501             /// Items are positioned with equal space before, between, and after the lines.<br/>
502             /// Compared to <see cref="FlexJustification.SpaceBetween"/> using <see cref="FlexJustification.SpaceAround"/>
503             /// will result in space being distributed to the beginning of the first child and end of the last child.
504             /// </summary>
505             SpaceAround
506         }
507
508         /// <summary>
509         /// Enumeration for the wrap type of the flex container when there is no enough room for all the items on one flex line.
510         /// </summary>
511         /// <since_tizen> 6 </since_tizen>
512         public enum FlexWrapType
513         {
514             /// <summary>
515             /// Flex items laid out in single line (shrunk to fit the flex container along the main axis)
516             /// </summary>
517             NoWrap,
518             /// <summary>
519             /// Flex items laid out in multiple lines if needed
520             /// </summary>
521             Wrap
522         }
523
524         /// <summary>
525         /// Enumeration for the alignment of the flex items or lines when the items or lines do not use all the available space on the cross-axis.
526         /// </summary>
527         /// <since_tizen> 6 </since_tizen>
528         public enum AlignmentType
529         {
530             /// <summary>
531             /// Inherits the same alignment from the parent
532             /// </summary>
533             Auto,
534             /// <summary>
535             /// At the beginning of the container
536             /// </summary>
537             FlexStart,
538             /// <summary>
539             /// At the center of the container
540             /// </summary>
541             Center,
542             /// <summary>
543             /// At the end of the container
544             /// </summary>
545             FlexEnd,
546             /// <summary>
547             /// Stretch to fit the container
548             /// </summary>
549             Stretch
550         }
551
552         /// <summary>
553         /// Enumeration for the position type of the flex item how it is positioned within its parent.
554         /// </summary>
555         /// <since_tizen> 8 </since_tizen>
556         public enum PositionType
557         {
558             /// <summary>
559             /// Flex items laid out relatively.
560             /// </summary>
561             Relative,
562             /// <summary>
563             /// Flex items laid out absolutely.
564             /// </summary>
565             Absolute
566         }
567
568         private void measureChild(global::System.IntPtr childPtr, float width, int measureModeWidth, float height, int measureModeHeight, out MeasuredSize measureSize)
569         {
570             // We need to measure child layout
571             View child = Registry.GetManagedBaseHandleFromNativePtr(childPtr) as View;
572
573             LayoutItem childLayout = child.Layout;
574
575             MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
576                                     new MeasureSpecification(
577                                         new LayoutLength(parentMeasureSpecificationWidth.Size - (child.Margin.Start + child.Margin.End)),
578                                         parentMeasureSpecificationWidth.Mode),
579                                     new LayoutLength(Padding.Start + Padding.End),
580                                     new LayoutLength(child.WidthSpecification));
581
582             MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
583                                     new MeasureSpecification(
584                                         new LayoutLength(parentMeasureSpecificationHeight.Size - (child.Margin.Top + child.Margin.Bottom)),
585                                         parentMeasureSpecificationHeight.Mode),
586                                     new LayoutLength(Padding.Top + Padding.Bottom),
587                                     new LayoutLength(child.HeightSpecification));
588
589             childLayout.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
590
591             measureSize.width = childLayout.MeasuredWidth.Size.AsRoundedValue();
592             measureSize.height = childLayout.MeasuredHeight.Size.AsRoundedValue();
593         }
594
595         void InsertChild(LayoutItem child)
596         {
597             // Store created node for child
598             IntPtr childPtr = Interop.FlexLayout.FlexLayout_AddChildWithMargin(swigCPtr, View.getCPtr(child.Owner), Extents.getCPtr(child.Owner.Margin), measureChildDelegate, LayoutChildren.Count - 1);
599             HandleRef childHandleRef = new HandleRef(child.Owner, childPtr);
600             SetAttachedValue(child.Owner, FlexItemProperty, childHandleRef);
601         }
602
603         /// <summary>
604         /// Callback when child is added to container.<br />
605         /// Derived classes can use this to set their own child properties on the child layout's owner.<br />
606         /// </summary>
607         /// <param name="child">The Layout child.</param>
608         /// <since_tizen> 6 </since_tizen>
609         protected override void OnChildAdd(LayoutItem child)
610         {
611             InsertChild(child);
612         }
613
614         /// <summary>
615         /// Callback when child is removed from container.<br />
616         /// </summary>
617         /// <param name="child">The Layout child.</param>
618         /// <since_tizen> 6 </since_tizen>
619         protected override void OnChildRemove(LayoutItem child)
620         {
621             // When child View is removed from it's parent View (that is a Layout) then remove it from the layout too.
622             // FlexLayout refers to the child as a View not LayoutItem.
623             Interop.FlexLayout.FlexLayout_RemoveChild(swigCPtr, child);
624         }
625
626         /// <summary>
627         /// Measure the layout and its content to determine the measured width and the measured height.<br />
628         /// </summary>
629         /// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
630         /// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
631         /// <since_tizen> 6 </since_tizen>
632         protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
633         {
634             bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
635             Extents padding = Owner.Padding;
636             Extents margin = Owner.Margin;
637
638             Interop.FlexLayout.FlexLayout_SetMargin(swigCPtr, Extents.getCPtr(margin));
639             Interop.FlexLayout.FlexLayout_SetPadding(swigCPtr, Extents.getCPtr(padding));
640
641             float width = FlexUndefined; // Behaves as WrapContent (Flex Auto)
642             float height = FlexUndefined; // Behaves as WrapContent (Flex Auto)
643
644             if (widthMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || widthMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost)
645             {
646                 width = widthMeasureSpec.Size.AsDecimal();
647             }
648
649             if (heightMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || heightMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost)
650             {
651                 height = heightMeasureSpec.Size.AsDecimal();
652             }
653
654             // Save measureSpec to measure children.
655             // In other Layout, we can pass it as parameter to measuring child but in FlexLayout we can't
656             // because measurChild function is called by native callback.
657             parentMeasureSpecificationWidth = widthMeasureSpec;
658             parentMeasureSpecificationHeight = heightMeasureSpec;
659
660             // Assign child properties
661             for (int i = 0; i < LayoutChildren.Count; i++)
662             {
663                 LayoutItem layoutItem = LayoutChildren[i];
664                 View Child = layoutItem?.Owner;
665                 HandleRef childHandleRef = (HandleRef)Child.GetValue(FlexItemProperty);
666                 if (childHandleRef.Handle == IntPtr.Zero || Child == null)
667                     continue;
668
669                 AlignmentType flexAlignemnt = GetFlexAlignmentSelf(Child);
670                 PositionType flexPosition = GetFlexPositionType(Child);
671                 float flexAspectRatio = GetFlexAspectRatio(Child);
672                 float flexBasis = GetFlexBasis(Child);
673                 float flexShrink = GetFlexShrink(Child);
674                 float flexGrow = GetFlexGrow(Child);
675
676                 Interop.FlexLayout.FlexLayout_SetFlexAlignmentSelf(childHandleRef, (int)flexAlignemnt);
677                 Interop.FlexLayout.FlexLayout_SetFlexPositionType(childHandleRef, (int)flexPosition);
678                 Interop.FlexLayout.FlexLayout_SetFlexAspectRatio(childHandleRef, flexAspectRatio);
679                 Interop.FlexLayout.FlexLayout_SetFlexBasis(childHandleRef, flexBasis);
680                 Interop.FlexLayout.FlexLayout_SetFlexShrink(childHandleRef, flexShrink);
681                 Interop.FlexLayout.FlexLayout_SetFlexGrow(childHandleRef, flexGrow);
682             }
683
684             Interop.FlexLayout.FlexLayout_CalculateLayout(swigCPtr, width, height, isLayoutRtl);
685
686             LayoutLength flexLayoutWidth = new LayoutLength(Interop.FlexLayout.FlexLayout_GetWidth(swigCPtr));
687             LayoutLength flexLayoutHeight = new LayoutLength(Interop.FlexLayout.FlexLayout_GetHeight(swigCPtr));
688
689             Debug.WriteLineIf(LayoutDebugFlex, "FlexLayout OnMeasure width:" + flexLayoutWidth.AsRoundedValue()
690                                                 + " height:" + flexLayoutHeight.AsRoundedValue());
691
692             SetMeasuredDimensions(GetDefaultSize(flexLayoutWidth, widthMeasureSpec),
693                                    GetDefaultSize(flexLayoutHeight, heightMeasureSpec));
694         }
695
696         /// <summary>
697         /// Assign a size and position to each of its children.<br />
698         /// </summary>
699         /// <param name="changed">This is a new size or position for this layout.</param>
700         /// <param name="left">Left position, relative to parent.</param>
701         /// <param name="top"> Top position, relative to parent.</param>
702         /// <param name="right">Right position, relative to parent.</param>
703         /// <param name="bottom">Bottom position, relative to parent.</param>
704         /// <since_tizen> 6 </since_tizen>
705         protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
706         {
707
708             bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
709             LayoutLength width = right - left;
710             LayoutLength height = bottom - top;
711
712             // Call to FlexLayout implementation to calculate layout values for later retrieval.
713             Interop.FlexLayout.FlexLayout_CalculateLayout(swigCPtr, width.AsDecimal(), height.AsDecimal(), isLayoutRtl);
714
715             int count = LayoutChildren.Count;
716             for (int childIndex = 0; childIndex < count; childIndex++)
717             {
718                 LayoutItem childLayout = LayoutChildren[childIndex];
719                 if (childLayout != null)
720                 {
721                     // Get the frame for the child, start, top, end, bottom.
722                     Vector4 frame = new Vector4(Interop.FlexLayout.FlexLayout_GetNodeFrame(swigCPtr, childIndex), true);
723                     childLayout.Layout(new LayoutLength(frame.X), new LayoutLength(frame.Y), new LayoutLength(frame.Z), new LayoutLength(frame.W));
724                 }
725             }
726         }
727
728     } // FLexlayout
729 } // namesspace Tizen.NUI