2283b133f6c444ebff5286a03534a9c274cafbe1
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Layouting / LinearLayout.cs
1 /* Copyright (c) 2020 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.Collections.Generic;
21 using System.Linq;
22
23 namespace Tizen.NUI
24 {
25     /// <summary>
26     /// [Draft] This class implements a linear box layout, automatically handling right to left or left to right direction change.
27     /// </summary>
28     public class LinearLayout : LayoutGroup
29     {
30         private Alignment linearAlignment = Alignment.Top;
31
32         /// <summary>
33         /// [Draft] Enumeration for the direction in which the content is laid out
34         /// </summary>
35         /// <since_tizen> 6 </since_tizen>
36         public enum Orientation
37         {
38             /// <summary>
39             /// Horizontal (row)
40             /// </summary>
41             Horizontal,
42             /// <summary>
43             /// Vertical (column)
44             /// </summary>
45             Vertical
46         }
47
48         /// <summary>
49         /// [Draft] Enumeration for the alignment of the linear layout items
50         /// </summary>
51         /// <since_tizen> 6 </since_tizen>
52         [Obsolete("Deprecated in API9, will be removed in API11. Please use HorizontalAlignment and VerticalAlignment instead!")]
53         public enum Alignment
54         {
55             /// <summary>
56             /// At the left/right edge of the container (maps to LTR/RTL direction for horizontal orientation)
57             /// </summary>
58             Begin = 0x1,
59             /// <summary>
60             /// At the right/left edge of the container (maps to LTR/RTL direction for horizontal orientation)
61             /// </summary>
62             End = 0x2,
63             /// <summary>
64             /// At the horizontal center of the container
65             /// </summary>
66             CenterHorizontal = 0x4,
67             /// <summary>
68             /// At the top edge of the container
69             /// </summary>
70             Top = 0x8,
71             /// <summary>
72             /// At the bottom edge of the container
73             /// </summary>
74             Bottom = 0x10,
75             /// <summary>
76             /// At the vertical center of the container
77             /// </summary>
78             CenterVertical = 0x20,
79             /// <summary>
80             /// At the vertical and horizontal center of the container
81             /// </summary>
82             Center = 0x40
83         }
84
85         struct HeightAndWidthState
86         {
87             public MeasuredSize.StateType widthState;
88             public MeasuredSize.StateType heightState;
89
90             public HeightAndWidthState(MeasuredSize.StateType width, MeasuredSize.StateType height)
91             {
92                 widthState = width;
93                 heightState = height;
94             }
95         }
96
97         /// <summary>
98         /// [Draft] Get/Set the orientation in the layout
99         /// </summary>
100         /// <since_tizen> 6 </since_tizen>
101         public LinearLayout.Orientation LinearOrientation
102         {
103             get
104             {
105                 return linearOrientation;
106             }
107             set
108             {
109                 linearOrientation = value;
110                 RequestLayout();
111             }
112         }
113
114         /// <summary>
115         /// [Draft] Get/Set the padding between cells in the layout
116         /// </summary>
117         /// <since_tizen> 6 </since_tizen>
118         public Size2D CellPadding
119         {
120             get
121             {
122                 if (cellPadding == null)
123                 {
124                     cellPadding = new Size2D(0, 0);
125                 }
126
127                 return cellPadding;
128             }
129             set
130             {
131                 cellPadding = value;
132                 RequestLayout();
133             }
134         }
135
136
137         /// <summary>
138         /// [Draft] Get/Set the alignment in the layout
139         /// </summary>
140         /// <since_tizen> 6 </since_tizen>
141         [Obsolete("Deprecated in API9, will be removed in API11. Please use HorizontalAlignment and VerticalAlignment properties instead!")]
142         public LinearLayout.Alignment LinearAlignment
143         {
144             get
145             {
146                 return linearAlignment;
147             }
148
149             set
150             {
151                 if (linearAlignment == value)
152                 {
153                     return;
154                 }
155
156                 linearAlignment = value;
157
158                 switch (linearAlignment)
159                 {
160                     case Alignment.Begin:
161                         HorizontalAlignment = HorizontalAlignment.Begin;
162                         break;
163                     case Alignment.End:
164                         HorizontalAlignment = HorizontalAlignment.End;
165                         break;
166                     case Alignment.CenterHorizontal:
167                         HorizontalAlignment = HorizontalAlignment.Center;
168                         break;
169                     case Alignment.Top:
170                         VerticalAlignment = VerticalAlignment.Top;
171                         break;
172                     case Alignment.Bottom:
173                         VerticalAlignment = VerticalAlignment.Bottom;
174                         break;
175                     case Alignment.CenterVertical:
176                         VerticalAlignment = VerticalAlignment.Center;
177                         break;
178                     case Alignment.Center:
179                         HorizontalAlignment = HorizontalAlignment.Center;
180                         VerticalAlignment = VerticalAlignment.Center;
181                         break;
182                     default:
183                         break;
184                 }
185             }
186          }
187
188         /// <summary>
189         /// Get/Set the horizontal alignment in the layout
190         /// </summary>
191         /// <since_tizen> 9 </since_tizen>
192         public HorizontalAlignment HorizontalAlignment { get; set; } = HorizontalAlignment.Begin;
193
194         /// <summary>
195         /// Get/Set the vertical alignment in the layout
196         /// </summary>
197         /// <since_tizen> 9 </since_tizen>
198         public VerticalAlignment VerticalAlignment { get; set; } = VerticalAlignment.Top;
199
200         private float totalLength = 0.0f;
201         private Size2D cellPadding = new Size2D(0, 0);
202         private Orientation linearOrientation = Orientation.Horizontal;
203
204         /// <summary>
205         /// [Draft] Constructor
206         /// </summary>
207         /// <since_tizen> 6 </since_tizen>
208         public LinearLayout()
209         {
210         }
211
212         /// <summary>
213         /// Measure the layout and its content to determine the measured width and the measured height.
214         /// </summary>
215         /// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
216         /// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
217         /// <since_tizen> 6 </since_tizen>
218         protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
219         {
220             if (linearOrientation == Orientation.Horizontal)
221             {
222                 MeasureHorizontal(widthMeasureSpec, heightMeasureSpec);
223             }
224             else
225             {
226                 MeasureVertical(widthMeasureSpec, heightMeasureSpec);
227             }
228         }
229
230         /// <summary>
231         /// Layout should assign a size and position to each of its children.<br />
232         /// </summary>
233         /// <param name="changed">This is a new size or position for this layout.</param>
234         /// <param name="left">Left position, relative to parent.</param>
235         /// <param name="top"> Top position, relative to parent.</param>
236         /// <param name="right">Right position, relative to parent.</param>
237         /// <param name="bottom">Bottom position, relative to parent.</param>
238         /// <since_tizen> 6 </since_tizen>
239         protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
240         {
241             if (linearOrientation == Orientation.Horizontal)
242             {
243                 LayoutHorizontal(left, top, right, bottom);
244             }
245             else
246             {
247                 LayoutVertical(left, top, right, bottom);
248             }
249         }
250
251
252         private void MeasureWeightedChild(LayoutItem childLayout, float totalWeightLength, float totalWeight, float childWeight,
253                                            MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec,
254                                            HeightAndWidthState childState, Orientation orientation)
255         {
256             bool horizontal = false;
257             if (orientation == Orientation.Horizontal)
258             {
259                 horizontal = true;
260             }
261
262             float childsShare = totalWeightLength * (childWeight / totalWeight);
263             float desiredWidth = childLayout.Owner.WidthSpecification;
264             float desiredHeight = childLayout.Owner.HeightSpecification;
265
266             MeasureSpecification childWidthMeasureSpec;
267             MeasureSpecification childHeightMeasureSpec;
268
269             if (horizontal)
270             {
271                 childWidthMeasureSpec = new MeasureSpecification(new LayoutLength(childsShare - (childLayout.Margin.Start + childLayout.Margin.End)), MeasureSpecification.ModeType.Exactly);
272
273                 childHeightMeasureSpec = GetChildMeasureSpecification(
274                                             new MeasureSpecification(
275                                                 new LayoutLength(heightMeasureSpec.Size - (childLayout.Margin.Top + childLayout.Margin.Bottom)),
276                                                 heightMeasureSpec.Mode),
277                                             new LayoutLength(Padding.Top + Padding.Bottom),
278                                             new LayoutLength(desiredHeight));
279             }
280             else // vertical
281             {
282                 childWidthMeasureSpec = GetChildMeasureSpecification(
283                                             new MeasureSpecification(
284                                                 new LayoutLength(widthMeasureSpec.Size - (childLayout.Margin.Start + childLayout.Margin.End)),
285                                                 widthMeasureSpec.Mode),
286                                             new LayoutLength(Padding.Start + Padding.End),
287                                             new LayoutLength(desiredWidth));
288
289                 childHeightMeasureSpec = new MeasureSpecification(new LayoutLength(childsShare - (childLayout.Margin.Top + childLayout.Margin.Bottom)), MeasureSpecification.ModeType.Exactly);
290             }
291
292             childLayout.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
293
294             // Child may now not fit in horizontal dimension.
295             if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
296             {
297                 childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
298             }
299
300             // Child may now not fit in vertical dimension.
301             if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
302             {
303                 childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
304             }
305         }
306
307         private void MeasureHorizontal(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
308         {
309             var widthMode = widthMeasureSpec.Mode;
310             var heightMode = heightMeasureSpec.Mode;
311             bool isWidthExactly = (widthMode == MeasureSpecification.ModeType.Exactly);
312             bool isHeightExactly = (heightMode == MeasureSpecification.ModeType.Exactly);
313             float maxHeight = 0.0f;
314             float totalWeight = 0.0f;
315             int childrenCount = IterateLayoutChildren().Count();
316
317             // Child layout, which wants to match its width to its parent's remaining width, is either following 1 or 2.
318             // 1. Child layout whose Owner.WidthSpecification is LayoutParamPolicies.MatchParent.
319             // 2. Child layout whose Owner.WidthSpecification is 0 and Owner.Weight is greater than 0.
320             // The number of child layout which wants to match its width to its parent's remaining width.
321             int childrenMatchParentCount = 0;
322
323             // Reset measure variable
324             totalLength = 0.0f;
325
326             HeightAndWidthState childState = new HeightAndWidthState(MeasuredSize.StateType.MeasuredSizeOK,
327                                                                      MeasuredSize.StateType.MeasuredSizeOK);
328
329             // 1ST PHASE:
330             //
331             // We measure all children whose width specification policy is WrapContent without weight.
332             // After 1st phase, remaining width of parent is accumulated to calculate width of children
333             // whose width specification policy is MatchParent.
334             foreach (var childLayout in LayoutChildren)
335             {
336                 if (!childLayout.SetPositionByLayout)
337                 {
338                     continue;
339                 }
340                 int childDesiredWidth = childLayout.Owner.WidthSpecification;
341                 int childDesiredHeight = childLayout.Owner.HeightSpecification;
342                 float childWeight = childLayout.Owner.Weight;
343                 Extents childMargin = childLayout.Margin;
344                 float childMarginWidth = childMargin.Start + childMargin.End;
345                 float childMarginHeight = childMargin.Top + childMargin.Bottom;
346                 bool useRemainingWidth = (childDesiredWidth == 0) && (childWeight > 0);
347
348                 totalWeight += childWeight;
349
350                 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
351                 {
352                     childrenMatchParentCount++;
353                 }
354
355                 // MatchParent child layout's margin is not added to totalLength.
356                 // Consequently, MatchParent child layout's margin is added to remaining size,
357                 // so the margin is not shared with other MatchParent child layouts.
358                 // e.g.
359                 // LinearLayout has size 100.
360                 // Child layout1 is MatchParent and its margin is 20. (This margin is not ad
361                 // Child layout2 is MatchParent and its margin is 0.
362                 // Then, child layout1's size is 30 and child layout2's size is 50.
363                 if ((childDesiredWidth == LayoutParamPolicies.WrapContent) || ((childDesiredWidth >= 0) && (!useRemainingWidth)))
364                 {
365                     MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
366
367                     float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
368
369                     if (childMeasuredWidth < 0)
370                     {
371                         totalLength = Math.Max(totalLength, totalLength + childMarginWidth);
372                     }
373                     else
374                     {
375                         totalLength = Math.Max(totalLength, totalLength + childMeasuredWidth + childMarginWidth);
376                     }
377                 }
378
379                 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
380                 {
381                     childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
382                 }
383                 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
384                 {
385                     childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
386                 }
387
388                 float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
389                 if (childMeasuredHeight < 0)
390                 {
391                     maxHeight = Math.Max(maxHeight, childMarginHeight);
392                 }
393                 else
394                 {
395                     maxHeight = Math.Max(maxHeight, childMeasuredHeight + childMarginHeight);
396                 }
397             } // 1ST PHASE foreach
398
399             totalLength = Math.Max(totalLength, totalLength + CellPadding.Width * (childrenCount - 1) + Padding.Start + Padding.End);
400             float widthSize = Math.Max(totalLength, SuggestedMinimumWidth.AsDecimal());
401             MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(widthSize), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
402             widthSize = widthSizeAndState.Size.AsDecimal();
403
404             float remainingWidth = widthSize - totalLength;
405             float totalWeightLength = 0.0f;
406
407             // Up to now, only WrapContent children's sizes are added to the totalLength.
408             // Since the totalLength is used in OnLayout as the sum of all children's sizes,
409             // the layout size is assigned to the totalLength if MatchParent child exists.
410             if (childrenMatchParentCount > 0)
411             {
412                 totalLength = widthSize;
413             }
414
415             // 2ND PHASE:
416             //
417             // We measure all children whose width specification policy is MatchParent without weight.
418             // After 2nd phase, all children's widths are calculated without considering weight.
419             // And the widths of all weighted children are accumulated to calculate weighted width.
420             foreach (var childLayout in LayoutChildren)
421             {
422                 if (!childLayout.SetPositionByLayout)
423                 {
424                     continue;
425                 }
426                 int childDesiredWidth = childLayout.Owner.WidthSpecification;
427                 int childDesiredHeight = childLayout.Owner.HeightSpecification;
428                 float childWeight = childLayout.Owner.Weight;
429                 Extents childMargin = childLayout.Margin;
430                 bool useRemainingWidth = (childDesiredWidth == 0) && (childWeight > 0);
431                 bool needToMeasure = false;
432
433                 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
434                 {
435                     if (isHeightExactly)
436                     {
437                         needToMeasure = true;
438                     }
439                     // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
440                     // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
441                     // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
442                     //
443                     // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
444                     else if (Owner.HeightSpecification == LayoutParamPolicies.WrapContent)
445                     {
446                         if (childDesiredHeight == LayoutParamPolicies.MatchParent)
447                         {
448                             Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s HeightSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s HeightSpecification is MatchParent!");
449                         }
450                         else
451                         {
452                             Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s HeightSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s HeightSpecification is 0 with positive weight!");
453                         }
454                     }
455                 }
456
457                 if (remainingWidth > 0)
458                 {
459                     if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
460                     {
461                         if (isWidthExactly)
462                         {
463                             // In MeasureChildWithMargins(), it is assumed that widthMeasureSpec includes Padding.Start and Padding.End.
464                             // Therefore, Padding.Start and Padding.End are added to widthMeasureSpec.Size before it is passed to MeasureChildWithMargins().
465                             widthMeasureSpec.SetSize(new LayoutLength((int)(remainingWidth / childrenMatchParentCount) + Padding.Start + Padding.End));
466                             needToMeasure = true;
467                         }
468                         // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
469                         // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
470                         // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
471                         //
472                         // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
473                         else if (Owner.WidthSpecification == LayoutParamPolicies.WrapContent)
474                         {
475                             if (childDesiredWidth == LayoutParamPolicies.MatchParent)
476                             {
477                                 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s WidthSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s WidthSpecification is MatchParent!");
478                             }
479                             else
480                             {
481                                 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s WidthSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s WidthSpecification is 0 with positive weight!");
482                             }
483                         }
484                     }
485                 }
486
487                 if (needToMeasure == true)
488                 {
489                     MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
490
491                     if (childWeight > 0)
492                     {
493                         float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
494
495                         if (childMeasuredWidth < 0)
496                         {
497                             totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMargin.Start + childMargin.End);
498                         }
499                         else
500                         {
501                             totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMeasuredWidth + childMargin.Start + childMargin.End);
502                         }
503                     }
504                 }
505             } // 2ND PHASE foreach
506
507             // 3RD PHASE:
508             //
509             // We measure all weighted children whose owner has weight greater than 0.
510             // After 3rd phase, all weighted children has width which is proportional to their weights
511             // in remaining width of parent.
512             if (totalWeight > 0.0f)
513             {
514                 foreach (LayoutItem childLayout in LayoutChildren)
515                 {
516                     if (!childLayout.SetPositionByLayout)
517                     {
518                         continue;
519                     }
520                     int childDesiredWidth = childLayout.Owner.WidthSpecification;
521                     float childWeight = childLayout.Owner.Weight;
522
523                     if ((childWeight > 0) && ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (childDesiredWidth == 0)))
524                     {
525                         if (isWidthExactly)
526                         {
527                             MeasureWeightedChild(childLayout, totalWeightLength, totalWeight, childWeight,
528                                                  widthMeasureSpec, heightMeasureSpec, childState,
529                                                  Orientation.Horizontal);
530                         }
531                         // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
532                         // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
533                         // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
534                         //
535                         // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
536                         else if (Owner.WidthSpecification == LayoutParamPolicies.WrapContent)
537                         {
538                             if (childDesiredWidth == LayoutParamPolicies.MatchParent)
539                             {
540                                 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s WidthSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s WidthSpecification is MatchParent!");
541                             }
542                             else
543                             {
544                                 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s WidthSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s WidthSpecification is 0 with positive weight!");
545                             }
546                         }
547                     }
548                 } // 3RD PHASE foreach
549             }
550
551             maxHeight = Math.Max(maxHeight, maxHeight + (Padding.Top + Padding.Bottom));
552             maxHeight = Math.Max(maxHeight, SuggestedMinimumHeight.AsRoundedValue());
553
554             widthSizeAndState.State = childState.widthState;
555
556             SetMeasuredDimensions(widthSizeAndState,
557                                   ResolveSizeAndState(new LayoutLength(maxHeight), heightMeasureSpec, childState.heightState));
558         } // MeasureHorizontal
559
560         private void MeasureVertical(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
561         {
562             var widthMode = widthMeasureSpec.Mode;
563             var heightMode = heightMeasureSpec.Mode;
564             bool isWidthExactly = (widthMode == MeasureSpecification.ModeType.Exactly);
565             bool isHeightExactly = (heightMode == MeasureSpecification.ModeType.Exactly);
566             float maxWidth = 0.0f;
567             float totalWeight = 0.0f;
568             int childrenCount = IterateLayoutChildren().Count();
569
570             // Child layout, which wants to match its height to its parent's remaining height, is either following 1 or 2.
571             // 1. Child layout whose Owner.HeightSpecification is LayoutParamPolicies.MatchParent.
572             // 2. Child layout whose Owner.HeightSpecification is 0 and Owner.Weight is greater than 0.
573             // The number of child layout which wants to match its height to its parent's remaining height.
574             int childrenMatchParentCount = 0;
575
576             // Reset measure variable
577             totalLength = 0.0f;
578
579             HeightAndWidthState childState = new HeightAndWidthState(MeasuredSize.StateType.MeasuredSizeOK,
580                                                                      MeasuredSize.StateType.MeasuredSizeOK);
581
582             // 1ST PHASE:
583             //
584             // We measure all children whose height specification policy is WrapContent without weight.
585             // After 1st phase, remaining height of parent is accumulated to calculate height of children
586             // whose height specification policy is MatchParent.
587             foreach (var childLayout in LayoutChildren)
588             {
589                 if (!childLayout.SetPositionByLayout)
590                 {
591                     continue;
592                 }
593                 int childDesiredWidth = childLayout.Owner.WidthSpecification;
594                 int childDesiredHeight = childLayout.Owner.HeightSpecification;
595                 float childWeight = childLayout.Owner.Weight;
596                 Extents childMargin = childLayout.Margin;
597                 float childMarginWidth = childMargin.Start + childMargin.End;
598                 float childMarginHeight = childMargin.Top + childMargin.Bottom;
599                 bool useRemainingHeight = (childDesiredHeight == 0) && (childWeight > 0);
600
601                 totalWeight += childWeight;
602
603                 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
604                 {
605                     childrenMatchParentCount++;
606                 }
607
608                 // MatchParent child layout's margin is not added to totalLength.
609                 // Consequently, MatchParent child layout's margin is added to remaining size,
610                 // so the margin is not shared with other MatchParent child layouts.
611                 // e.g.
612                 // LinearLayout has size 100.
613                 // Child layout1 is MatchParent and its margin is 20. (This margin is not ad
614                 // Child layout2 is MatchParent and its margin is 0.
615                 // Then, child layout1's size is 30 and child layout2's size is 50.
616                 if ((childDesiredHeight == LayoutParamPolicies.WrapContent) || ((childDesiredHeight > 0) && (!useRemainingHeight)))
617                 {
618                     MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
619
620                     float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
621
622                     if (childMeasuredHeight < 0)
623                     {
624                         totalLength = Math.Max(totalLength, totalLength + childMarginHeight);
625                     }
626                     else
627                     {
628                         totalLength = Math.Max(totalLength, totalLength + childMeasuredHeight + childMarginHeight);
629                     }
630                 }
631
632                 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
633                 {
634                     childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
635                 }
636                 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
637                 {
638                     childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
639                 }
640
641                 float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
642                 if (childMeasuredWidth < 0)
643                 {
644                     maxWidth = Math.Max(maxWidth, childMarginWidth);
645                 }
646                 else
647                 {
648                     maxWidth = Math.Max(maxWidth, childMeasuredWidth + childMarginWidth);
649                 }
650             } // 1ST PHASE foreach
651
652             totalLength = Math.Max(totalLength, totalLength + CellPadding.Height * (childrenCount - 1) + Padding.Top + Padding.Bottom);
653             float heightSize = Math.Max(totalLength, SuggestedMinimumHeight.AsDecimal());
654             MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(heightSize), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
655             heightSize = heightSizeAndState.Size.AsDecimal();
656
657             float remainingHeight = heightSize - totalLength;
658             float totalWeightLength = 0.0f;
659
660             // Up to now, only WrapContent children's sizes are added to the totalLength.
661             // Since the totalLength is used in OnLayout as the sum of all children's sizes,
662             // the layout size is assigned to the totalLength if MatchParent child exists.
663             if (childrenMatchParentCount > 0)
664             {
665                 totalLength = heightSize;
666             }
667
668             // 2ND PHASE:
669             //
670             // We measure all children whose height specification policy is MatchParent without weight.
671             // After 2nd phase, all children's heights are calculated without considering weight.
672             // And the heights of all weighted children are accumulated to calculate weighted height.
673             foreach (var childLayout in LayoutChildren)
674             {
675                 if (!childLayout.SetPositionByLayout)
676                 {
677                     continue;
678                 }
679                 int childDesiredWidth = childLayout.Owner.WidthSpecification;
680                 int childDesiredHeight = childLayout.Owner.HeightSpecification;
681                 float childWeight = childLayout.Owner.Weight;
682                 Extents childMargin = childLayout.Margin;
683                 bool useRemainingHeight = (childDesiredHeight == 0) && (childWeight > 0);
684                 bool needToMeasure = false;
685
686                 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
687                 {
688                     if (isWidthExactly)
689                     {
690                         needToMeasure = true;
691                     }
692                     // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
693                     // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
694                     // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
695                     //
696                     // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
697                     else if (Owner.WidthSpecification == LayoutParamPolicies.WrapContent)
698                     {
699                         if (childDesiredWidth == LayoutParamPolicies.MatchParent)
700                         {
701                             Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s WidthSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s WidthSpecification is MatchParent!");
702                         }
703                         else
704                         {
705                             Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s WidthSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s WidthSpecification is 0 with positive weight!");
706                         }
707                     }
708                 }
709
710                 if (remainingHeight > 0)
711                 {
712                     if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
713                     {
714                         if (isHeightExactly)
715                         {
716                             // In MeasureChildWithMargins(), it is assumed that heightMeasureSpec includes Padding.Top and Padding.Bottom.
717                             // Therefore, Padding.Top and Padding.Bottom are added to heightMeasureSpec.Size before it is passed to MeasureChildWithMargins().
718                             heightMeasureSpec.SetSize(new LayoutLength((int)(remainingHeight / childrenMatchParentCount) + Padding.Top + Padding.Bottom));
719                             needToMeasure = true;
720                         }
721                         // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
722                         // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
723                         // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
724                         //
725                         // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
726                         else if (Owner.HeightSpecification == LayoutParamPolicies.WrapContent)
727                         {
728                             if (childDesiredHeight == LayoutParamPolicies.MatchParent)
729                             {
730                                 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s HeightSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s HeightSpecification is MatchParent!");
731                             }
732                             else
733                             {
734                                 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s HeightSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s HeightSpecification is 0 with positive weight!");
735                             }
736                         }
737                     }
738                 }
739
740                 if (needToMeasure == true)
741                 {
742                     MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
743                 }
744
745                 if (childWeight > 0)
746                 {
747                     float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
748
749                     if (childMeasuredHeight < 0)
750                     {
751                         totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMargin.Top + childMargin.Bottom);
752                     }
753                     else
754                     {
755                         totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMeasuredHeight + childMargin.Top + childMargin.Bottom);
756                     }
757                 }
758             } // 2ND PHASE foreach
759
760             // 3RD PHASE:
761             //
762             // We measure all weighted children whose owner has weight greater than 0.
763             // After 3rd phase, all weighted children has height which is proportional to their weights
764             // in remaining height of parent.
765             if (totalWeight > 0)
766             {
767                 foreach (var childLayout in LayoutChildren)
768                 {
769                     if (!childLayout.SetPositionByLayout)
770                     {
771                         continue;
772                     }
773                     int childDesiredHeight = childLayout.Owner.HeightSpecification;
774                     float childWeight = childLayout.Owner.Weight;
775
776                     if ((childWeight > 0) && ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (childDesiredHeight == 0)))
777                     {
778                         if (isHeightExactly)
779                         {
780                             MeasureWeightedChild(childLayout, totalWeightLength, totalWeight, childWeight,
781                                                  widthMeasureSpec, heightMeasureSpec, childState,
782                                                  Orientation.Vertical);
783                         }
784                         // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
785                         // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
786                         // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
787                         //
788                         // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
789                         else if (Owner.HeightSpecification == LayoutParamPolicies.WrapContent)
790                         {
791                             if (childDesiredHeight == LayoutParamPolicies.MatchParent)
792                             {
793                                 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s HeightSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s HeightSpecification is MatchParent!");
794                             }
795                             else
796                             {
797                                 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s HeightSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s HeightSpecification is 0 with positive weight!");
798                             }
799                         }
800                     }
801                 } // 3RD PHASE foreach
802             }
803
804             maxWidth = Math.Max(maxWidth, maxWidth + (Padding.Start + Padding.End));
805             maxWidth = Math.Max(maxWidth, SuggestedMinimumWidth.AsRoundedValue());
806
807             heightSizeAndState.State = childState.heightState;
808
809             SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(maxWidth), widthMeasureSpec, childState.widthState),
810                                   heightSizeAndState);
811         } // MeasureVertical
812
813         private void LayoutHorizontal(LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
814         {
815             bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
816
817             LayoutLength childTop = new LayoutLength(Padding.Top);
818             LayoutLength childLeft = new LayoutLength(Padding.Start);
819
820             // Where bottom of child should go
821             LayoutLength height = new LayoutLength(bottom - top);
822
823             // Space available for child
824             LayoutLength childSpace = new LayoutLength(height - Padding.Top - Padding.Bottom);
825
826             var LinearChildren = IterateLayoutChildren();
827             int count = LinearChildren.Count<LayoutItem>();
828
829             switch (HorizontalAlignment)
830             {
831                 case HorizontalAlignment.End:
832                     // totalLength contains the padding already
833                     // In case of RTL map END alignment to the left edge
834                     if (isLayoutRtl)
835                     {
836                         childLeft = new LayoutLength(Padding.Start);
837                     }
838                     else
839                     {
840                         childLeft = new LayoutLength(Padding.Start + right.AsDecimal() - left.AsDecimal() - totalLength);
841                     }
842                     break;
843                 case HorizontalAlignment.Center:
844                     // totalLength contains the padding already
845                     childLeft = new LayoutLength(Padding.Start + (right.AsDecimal() - left.AsDecimal() - totalLength) / 2.0f);
846                     break;
847                 case HorizontalAlignment.Begin: // FALL THROUGH (default)
848                 default:
849                     // totalLength contains the padding already
850                     // In case of RTL map BEGIN alignment to the right edge
851                     if (isLayoutRtl)
852                     {
853                         childLeft = new LayoutLength(Padding.Start + right.AsDecimal() - left.AsDecimal() - totalLength);
854                     }
855                     else
856                     {
857                         childLeft = new LayoutLength(Padding.Start);
858                     }
859                     break;
860             }
861
862             int start = 0;
863             int dir = 1;
864
865             // In case of RTL, start drawing from the last child.
866             if (isLayoutRtl)
867             {
868                 start = count - 1;
869                 dir = -1;
870             }
871
872             for (int i = 0; i < count; i++)
873             {
874                 int childIndex = start + dir * i;
875                 // Get a reference to the childLayout at the given index
876                 LayoutItem childLayout = LinearChildren.ElementAt<LayoutItem>(i);
877
878                 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
879                 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
880                 Extents childMargin = childLayout.Margin;
881
882                 switch (VerticalAlignment)
883                 {
884                     case VerticalAlignment.Bottom:
885                         childTop = new LayoutLength(height - Padding.Bottom - childHeight - childMargin.Bottom);
886                         break;
887                     case VerticalAlignment.Center:
888                         childTop = new LayoutLength(Padding.Top + ((childSpace - childHeight).AsDecimal() / 2.0f) + childMargin.Top - childMargin.Bottom);
889                         break;
890                     case VerticalAlignment.Top: // FALLTHROUGH default
891                     default:
892                         childTop = new LayoutLength(Padding.Top + childMargin.Top);
893                         break;
894                 }
895                 childLeft += (isLayoutRtl ? childMargin.End : childMargin.Start);
896                 childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
897                 childLeft += childWidth + (isLayoutRtl ? childMargin.Start : childMargin.End) + ((i < count - 1) ? CellPadding.Width : 0);
898             }
899         } // LayoutHorizontally
900
901         private void LayoutVertical(LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
902         {
903             LayoutLength childTop = new LayoutLength(Padding.Top);
904             LayoutLength childLeft = new LayoutLength(Padding.Start);
905
906             // Where end of child should go
907             LayoutLength width = new LayoutLength(right - left);
908
909             // Space available for child
910             LayoutLength childSpace = new LayoutLength(width - Padding.Start - Padding.End);
911
912             var LinearChildren = IterateLayoutChildren();
913             int count = LinearChildren.Count<LayoutItem>();
914
915             switch (VerticalAlignment)
916             {
917                 case VerticalAlignment.Bottom:
918                     // totalLength contains the padding already
919                     childTop = new LayoutLength(Padding.Top + bottom.AsDecimal() - top.AsDecimal() - totalLength);
920                     break;
921                 case VerticalAlignment.Center:
922                     // totalLength contains the padding already
923                     childTop = new LayoutLength(Padding.Top + (bottom.AsDecimal() - top.AsDecimal() - totalLength) / 2.0f);
924                     break;
925                 case VerticalAlignment.Top:  // FALL THROUGH (default)
926                 default:
927                     // totalLength contains the padding already
928                     childTop = new LayoutLength(Padding.Top);
929                     break;
930             }
931
932             for (int i = 0; i < count; i++)
933             {
934                 LayoutItem childLayout = LinearChildren.ElementAt<LayoutItem>(i);
935
936                 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
937                 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
938                 Extents childMargin = childLayout.Margin;
939
940                 childTop += childMargin.Top;
941                 switch (HorizontalAlignment)
942                 {
943                     case HorizontalAlignment.Begin:
944                     default:
945                         {
946                             childLeft = new LayoutLength(Padding.Start + childMargin.Start);
947                             break;
948                         }
949                     case HorizontalAlignment.End:
950                         {
951                             childLeft = new LayoutLength(width - Padding.End - childWidth - childMargin.End);
952                             break;
953                         }
954                     case HorizontalAlignment.Center:
955                         {
956                             childLeft = new LayoutLength(Padding.Start + ((childSpace - childWidth).AsDecimal() / 2.0f) + childMargin.Start - childMargin.End);
957                             break;
958                         }
959                 }
960                 childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
961                 childTop += childHeight + childMargin.Bottom + ((i < count - 1) ? CellPadding.Height : 0);
962             }
963         } // LayoutVertical
964
965         private void ForceUniformHeight(MeasureSpecification widthMeasureSpec)
966         {
967             // Pretend that the linear layout has an exact size. This is the measured height of
968             // ourselves. The measured height should be the max height of the children, changed
969             // to accommodate the heightMeasureSpec from the parent
970             MeasureSpecification uniformMeasureSpec = new MeasureSpecification(MeasuredHeight.Size, MeasureSpecification.ModeType.Exactly);
971             foreach (var childLayout in LayoutChildren)
972             {
973                 if (!childLayout.SetPositionByLayout)
974                 {
975                     continue;
976                 }
977                 int desiredChildHeight = childLayout.Owner.HeightSpecification;
978                 int desiredChildWidth = childLayout.Owner.WidthSpecification;
979
980                 if (desiredChildHeight == LayoutParamPolicies.MatchParent)
981                 {
982                     // Temporarily force children to reuse their original measured width
983                     int originalWidth = desiredChildWidth;
984                     childLayout.Owner.WidthSpecification = (int)childLayout.MeasuredWidth.Size.AsRoundedValue();
985                     // Remeasure with new dimensions
986                     MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0),
987                                              uniformMeasureSpec, new LayoutLength(0));
988                     // Restore width specification
989                     childLayout.Owner.WidthSpecification = originalWidth;
990                 }
991             }
992         }
993     } //LinearLayout
994 } // namespace