9115e3f3171a43f5c134bd6823cb96681ec0aa96
[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                 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
349                 {
350                     totalWeight += childWeight;
351                     childrenMatchParentCount++;
352                 }
353
354                 // MatchParent child layout's margin is not added to totalLength.
355                 // Consequently, MatchParent child layout's margin is added to remaining size,
356                 // so the margin is not shared with other MatchParent child layouts.
357                 // e.g.
358                 // LinearLayout has size 100.
359                 // Child layout1 is MatchParent and its margin is 20. (This margin is not ad
360                 // Child layout2 is MatchParent and its margin is 0.
361                 // Then, child layout1's size is 30 and child layout2's size is 50.
362                 if ((childDesiredWidth == LayoutParamPolicies.WrapContent) || ((childDesiredWidth >= 0) && (!useRemainingWidth)))
363                 {
364                     MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
365
366                     float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
367
368                     if (childMeasuredWidth < 0)
369                     {
370                         totalLength = Math.Max(totalLength, totalLength + childMarginWidth);
371                     }
372                     else
373                     {
374                         totalLength = Math.Max(totalLength, totalLength + childMeasuredWidth + childMarginWidth);
375                     }
376                 }
377
378                 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
379                 {
380                     childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
381                 }
382                 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
383                 {
384                     childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
385                 }
386
387                 float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
388                 if (childMeasuredHeight < 0)
389                 {
390                     maxHeight = Math.Max(maxHeight, childMarginHeight);
391                 }
392                 else
393                 {
394                     maxHeight = Math.Max(maxHeight, childMeasuredHeight + childMarginHeight);
395                 }
396             } // 1ST PHASE foreach
397
398             totalLength = Math.Max(totalLength, totalLength + CellPadding.Width * (childrenCount - 1) + Padding.Start + Padding.End);
399             float widthSize = Math.Max(totalLength, SuggestedMinimumWidth.AsDecimal());
400             MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(widthSize), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
401             widthSize = widthSizeAndState.Size.AsDecimal();
402
403             float remainingWidth = widthSize - totalLength;
404             float totalWeightLength = 0.0f;
405
406             // Up to now, only WrapContent children's sizes are added to the totalLength.
407             // Since the totalLength is used in OnLayout as the sum of all children's sizes,
408             // the layout size is assigned to the totalLength if MatchParent child exists.
409             if (childrenMatchParentCount > 0)
410             {
411                 totalLength = widthSize;
412             }
413
414             // 2ND PHASE:
415             //
416             // We measure all children whose width specification policy is MatchParent without weight.
417             // After 2nd phase, all children's widths are calculated without considering weight.
418             // And the widths of all weighted children are accumulated to calculate weighted width.
419             foreach (var childLayout in LayoutChildren)
420             {
421                 if (!childLayout.SetPositionByLayout)
422                 {
423                     continue;
424                 }
425                 int childDesiredWidth = childLayout.Owner.WidthSpecification;
426                 int childDesiredHeight = childLayout.Owner.HeightSpecification;
427                 float childWeight = childLayout.Owner.Weight;
428                 Extents childMargin = childLayout.Margin;
429                 bool useRemainingWidth = (childDesiredWidth == 0) && (childWeight > 0);
430                 bool needToMeasure = false;
431
432                 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
433                 {
434                     if (isHeightExactly)
435                     {
436                         needToMeasure = true;
437                     }
438                     // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
439                     // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
440                     // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
441                     //
442                     // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
443                     else if (Owner.HeightSpecification == LayoutParamPolicies.WrapContent)
444                     {
445                         if (childDesiredHeight == LayoutParamPolicies.MatchParent)
446                         {
447                             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!");
448                         }
449                         else
450                         {
451                             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!");
452                         }
453                     }
454                 }
455
456                 if (remainingWidth > 0)
457                 {
458                     if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
459                     {
460                         if (isWidthExactly)
461                         {
462                             // In MeasureChildWithMargins(), it is assumed that widthMeasureSpec includes Padding.Start and Padding.End.
463                             // Therefore, Padding.Start and Padding.End are added to widthMeasureSpec.Size before it is passed to MeasureChildWithMargins().
464                             widthMeasureSpec.SetSize(new LayoutLength((int)(remainingWidth / childrenMatchParentCount) + Padding.Start + Padding.End));
465                             needToMeasure = true;
466                         }
467                         // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
468                         // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
469                         // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
470                         //
471                         // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
472                         else if (Owner.WidthSpecification == LayoutParamPolicies.WrapContent)
473                         {
474                             if (childDesiredWidth == LayoutParamPolicies.MatchParent)
475                             {
476                                 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!");
477                             }
478                             else
479                             {
480                                 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!");
481                             }
482                         }
483                     }
484                 }
485
486                 if (needToMeasure == true)
487                 {
488                     MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
489                 }
490
491                 if ((childWeight > 0) && ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (childDesiredWidth == 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             } // 2ND PHASE foreach
505
506             // 3RD PHASE:
507             //
508             // We measure all weighted children whose owner has weight greater than 0.
509             // After 3rd phase, all weighted children has width which is proportional to their weights
510             // in remaining width of parent.
511             if (totalWeight > 0.0f)
512             {
513                 foreach (LayoutItem childLayout in LayoutChildren)
514                 {
515                     if (!childLayout.SetPositionByLayout)
516                     {
517                         continue;
518                     }
519                     int childDesiredWidth = childLayout.Owner.WidthSpecification;
520                     float childWeight = childLayout.Owner.Weight;
521
522                     if ((childWeight > 0) && ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (childDesiredWidth == 0)))
523                     {
524                         if (isWidthExactly)
525                         {
526                             MeasureWeightedChild(childLayout, totalWeightLength, totalWeight, childWeight,
527                                                  widthMeasureSpec, heightMeasureSpec, childState,
528                                                  Orientation.Horizontal);
529                         }
530                         // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
531                         // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
532                         // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
533                         //
534                         // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
535                         else if (Owner.WidthSpecification == LayoutParamPolicies.WrapContent)
536                         {
537                             if (childDesiredWidth == LayoutParamPolicies.MatchParent)
538                             {
539                                 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!");
540                             }
541                             else
542                             {
543                                 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!");
544                             }
545                         }
546                     }
547                 } // 3RD PHASE foreach
548             }
549
550             maxHeight = Math.Max(maxHeight, maxHeight + (Padding.Top + Padding.Bottom));
551             maxHeight = Math.Max(maxHeight, SuggestedMinimumHeight.AsRoundedValue());
552
553             widthSizeAndState.State = childState.widthState;
554
555             SetMeasuredDimensions(widthSizeAndState,
556                                   ResolveSizeAndState(new LayoutLength(maxHeight), heightMeasureSpec, childState.heightState));
557         } // MeasureHorizontal
558
559         private void MeasureVertical(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
560         {
561             var widthMode = widthMeasureSpec.Mode;
562             var heightMode = heightMeasureSpec.Mode;
563             bool isWidthExactly = (widthMode == MeasureSpecification.ModeType.Exactly);
564             bool isHeightExactly = (heightMode == MeasureSpecification.ModeType.Exactly);
565             float maxWidth = 0.0f;
566             float totalWeight = 0.0f;
567             int childrenCount = IterateLayoutChildren().Count();
568
569             // Child layout, which wants to match its height to its parent's remaining height, is either following 1 or 2.
570             // 1. Child layout whose Owner.HeightSpecification is LayoutParamPolicies.MatchParent.
571             // 2. Child layout whose Owner.HeightSpecification is 0 and Owner.Weight is greater than 0.
572             // The number of child layout which wants to match its height to its parent's remaining height.
573             int childrenMatchParentCount = 0;
574
575             // Reset measure variable
576             totalLength = 0.0f;
577
578             HeightAndWidthState childState = new HeightAndWidthState(MeasuredSize.StateType.MeasuredSizeOK,
579                                                                      MeasuredSize.StateType.MeasuredSizeOK);
580
581             // 1ST PHASE:
582             //
583             // We measure all children whose height specification policy is WrapContent without weight.
584             // After 1st phase, remaining height of parent is accumulated to calculate height of children
585             // whose height specification policy is MatchParent.
586             foreach (var childLayout in LayoutChildren)
587             {
588                 if (!childLayout.SetPositionByLayout)
589                 {
590                     continue;
591                 }
592                 int childDesiredWidth = childLayout.Owner.WidthSpecification;
593                 int childDesiredHeight = childLayout.Owner.HeightSpecification;
594                 float childWeight = childLayout.Owner.Weight;
595                 Extents childMargin = childLayout.Margin;
596                 float childMarginWidth = childMargin.Start + childMargin.End;
597                 float childMarginHeight = childMargin.Top + childMargin.Bottom;
598                 bool useRemainingHeight = (childDesiredHeight == 0) && (childWeight > 0);
599
600                 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
601                 {
602                     totalWeight += childWeight;
603                     childrenMatchParentCount++;
604                 }
605
606                 // MatchParent child layout's margin is not added to totalLength.
607                 // Consequently, MatchParent child layout's margin is added to remaining size,
608                 // so the margin is not shared with other MatchParent child layouts.
609                 // e.g.
610                 // LinearLayout has size 100.
611                 // Child layout1 is MatchParent and its margin is 20. (This margin is not ad
612                 // Child layout2 is MatchParent and its margin is 0.
613                 // Then, child layout1's size is 30 and child layout2's size is 50.
614                 if ((childDesiredHeight == LayoutParamPolicies.WrapContent) || ((childDesiredHeight > 0) && (!useRemainingHeight)))
615                 {
616                     MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
617
618                     float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
619
620                     if (childMeasuredHeight < 0)
621                     {
622                         totalLength = Math.Max(totalLength, totalLength + childMarginHeight);
623                     }
624                     else
625                     {
626                         totalLength = Math.Max(totalLength, totalLength + childMeasuredHeight + childMarginHeight);
627                     }
628                 }
629
630                 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
631                 {
632                     childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
633                 }
634                 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
635                 {
636                     childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
637                 }
638
639                 float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
640                 if (childMeasuredWidth < 0)
641                 {
642                     maxWidth = Math.Max(maxWidth, childMarginWidth);
643                 }
644                 else
645                 {
646                     maxWidth = Math.Max(maxWidth, childMeasuredWidth + childMarginWidth);
647                 }
648             } // 1ST PHASE foreach
649
650             totalLength = Math.Max(totalLength, totalLength + CellPadding.Height * (childrenCount - 1) + Padding.Top + Padding.Bottom);
651             float heightSize = Math.Max(totalLength, SuggestedMinimumHeight.AsDecimal());
652             MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(heightSize), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
653             heightSize = heightSizeAndState.Size.AsDecimal();
654
655             float remainingHeight = heightSize - totalLength;
656             float totalWeightLength = 0.0f;
657
658             // Up to now, only WrapContent children's sizes are added to the totalLength.
659             // Since the totalLength is used in OnLayout as the sum of all children's sizes,
660             // the layout size is assigned to the totalLength if MatchParent child exists.
661             if (childrenMatchParentCount > 0)
662             {
663                 totalLength = heightSize;
664             }
665
666             // 2ND PHASE:
667             //
668             // We measure all children whose height specification policy is MatchParent without weight.
669             // After 2nd phase, all children's heights are calculated without considering weight.
670             // And the heights of all weighted children are accumulated to calculate weighted height.
671             foreach (var childLayout in LayoutChildren)
672             {
673                 if (!childLayout.SetPositionByLayout)
674                 {
675                     continue;
676                 }
677                 int childDesiredWidth = childLayout.Owner.WidthSpecification;
678                 int childDesiredHeight = childLayout.Owner.HeightSpecification;
679                 float childWeight = childLayout.Owner.Weight;
680                 Extents childMargin = childLayout.Margin;
681                 bool useRemainingHeight = (childDesiredHeight == 0) && (childWeight > 0);
682                 bool needToMeasure = false;
683
684                 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
685                 {
686                     if (isWidthExactly)
687                     {
688                         needToMeasure = true;
689                     }
690                     // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
691                     // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
692                     // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
693                     //
694                     // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
695                     else if (Owner.WidthSpecification == LayoutParamPolicies.WrapContent)
696                     {
697                         if (childDesiredWidth == LayoutParamPolicies.MatchParent)
698                         {
699                             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!");
700                         }
701                         else
702                         {
703                             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!");
704                         }
705                     }
706                 }
707
708                 if (remainingHeight > 0)
709                 {
710                     if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
711                     {
712                         if (isHeightExactly)
713                         {
714                             // In MeasureChildWithMargins(), it is assumed that heightMeasureSpec includes Padding.Top and Padding.Bottom.
715                             // Therefore, Padding.Top and Padding.Bottom are added to heightMeasureSpec.Size before it is passed to MeasureChildWithMargins().
716                             heightMeasureSpec.SetSize(new LayoutLength((int)(remainingHeight / childrenMatchParentCount) + Padding.Top + Padding.Bottom));
717                             needToMeasure = true;
718                         }
719                         // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
720                         // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
721                         // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
722                         //
723                         // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
724                         else if (Owner.HeightSpecification == LayoutParamPolicies.WrapContent)
725                         {
726                             if (childDesiredHeight == LayoutParamPolicies.MatchParent)
727                             {
728                                 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!");
729                             }
730                             else
731                             {
732                                 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!");
733                             }
734                         }
735                     }
736                 }
737
738                 if (needToMeasure == true)
739                 {
740                     MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
741                 }
742
743                 if ((childWeight > 0) && ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (childDesiredHeight == 0)))
744                 {
745                     float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
746
747                     if (childMeasuredHeight < 0)
748                     {
749                         totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMargin.Top + childMargin.Bottom);
750                     }
751                     else
752                     {
753                         totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMeasuredHeight + childMargin.Top + childMargin.Bottom);
754                     }
755                 }
756             } // 2ND PHASE foreach
757
758             // 3RD PHASE:
759             //
760             // We measure all weighted children whose owner has weight greater than 0.
761             // After 3rd phase, all weighted children has height which is proportional to their weights
762             // in remaining height of parent.
763             if (totalWeight > 0)
764             {
765                 foreach (var childLayout in LayoutChildren)
766                 {
767                     if (!childLayout.SetPositionByLayout)
768                     {
769                         continue;
770                     }
771                     int childDesiredHeight = childLayout.Owner.HeightSpecification;
772                     float childWeight = childLayout.Owner.Weight;
773
774                     if ((childWeight > 0) && ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (childDesiredHeight == 0)))
775                     {
776                         if (isHeightExactly)
777                         {
778                             MeasureWeightedChild(childLayout, totalWeightLength, totalWeight, childWeight,
779                                                  widthMeasureSpec, heightMeasureSpec, childState,
780                                                  Orientation.Vertical);
781                         }
782                         // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
783                         // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
784                         // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
785                         //
786                         // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
787                         else if (Owner.HeightSpecification == LayoutParamPolicies.WrapContent)
788                         {
789                             if (childDesiredHeight == LayoutParamPolicies.MatchParent)
790                             {
791                                 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!");
792                             }
793                             else
794                             {
795                                 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!");
796                             }
797                         }
798                     }
799                 } // 3RD PHASE foreach
800             }
801
802             maxWidth = Math.Max(maxWidth, maxWidth + (Padding.Start + Padding.End));
803             maxWidth = Math.Max(maxWidth, SuggestedMinimumWidth.AsRoundedValue());
804
805             heightSizeAndState.State = childState.heightState;
806
807             SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(maxWidth), widthMeasureSpec, childState.widthState),
808                                   heightSizeAndState);
809         } // MeasureVertical
810
811         private void LayoutHorizontal(LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
812         {
813             bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
814
815             LayoutLength childTop = new LayoutLength(Padding.Top);
816             LayoutLength childLeft = new LayoutLength(Padding.Start);
817
818             // Where bottom of child should go
819             LayoutLength height = new LayoutLength(bottom - top);
820
821             // Space available for child
822             LayoutLength childSpace = new LayoutLength(height - Padding.Top - Padding.Bottom);
823
824             var LinearChildren = IterateLayoutChildren();
825             int count = LinearChildren.Count<LayoutItem>();
826
827             switch (HorizontalAlignment)
828             {
829                 case HorizontalAlignment.End:
830                     // totalLength contains the padding already
831                     // In case of RTL map END alignment to the left edge
832                     if (isLayoutRtl)
833                     {
834                         childLeft = new LayoutLength(Padding.Start);
835                     }
836                     else
837                     {
838                         childLeft = new LayoutLength(Padding.Start + right.AsDecimal() - left.AsDecimal() - totalLength);
839                     }
840                     break;
841                 case HorizontalAlignment.Center:
842                     // totalLength contains the padding already
843                     childLeft = new LayoutLength(Padding.Start + (right.AsDecimal() - left.AsDecimal() - totalLength) / 2.0f);
844                     break;
845                 case HorizontalAlignment.Begin: // FALL THROUGH (default)
846                 default:
847                     // totalLength contains the padding already
848                     // In case of RTL map BEGIN alignment to the right edge
849                     if (isLayoutRtl)
850                     {
851                         childLeft = new LayoutLength(Padding.Start + right.AsDecimal() - left.AsDecimal() - totalLength);
852                     }
853                     else
854                     {
855                         childLeft = new LayoutLength(Padding.Start);
856                     }
857                     break;
858             }
859
860             int start = 0;
861             int dir = 1;
862
863             // In case of RTL, start drawing from the last child.
864             if (isLayoutRtl)
865             {
866                 start = count - 1;
867                 dir = -1;
868             }
869
870             for (int i = 0; i < count; i++)
871             {
872                 int childIndex = start + dir * i;
873                 // Get a reference to the childLayout at the given index
874                 LayoutItem childLayout = LinearChildren.ElementAt<LayoutItem>(i);
875
876                 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
877                 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
878                 Extents childMargin = childLayout.Margin;
879
880                 switch (VerticalAlignment)
881                 {
882                     case VerticalAlignment.Bottom:
883                         childTop = new LayoutLength(height - Padding.Bottom - childHeight - childMargin.Bottom);
884                         break;
885                     case VerticalAlignment.Center:
886                         childTop = new LayoutLength(Padding.Top + ((childSpace - childHeight).AsDecimal() / 2.0f) + childMargin.Top - childMargin.Bottom);
887                         break;
888                     case VerticalAlignment.Top: // FALLTHROUGH default
889                     default:
890                         childTop = new LayoutLength(Padding.Top + childMargin.Top);
891                         break;
892                 }
893                 childLeft += (isLayoutRtl ? childMargin.End : childMargin.Start);
894                 childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
895                 childLeft += childWidth + (isLayoutRtl ? childMargin.Start : childMargin.End) + ((i < count - 1) ? CellPadding.Width : 0);
896             }
897         } // LayoutHorizontally
898
899         private void LayoutVertical(LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
900         {
901             LayoutLength childTop = new LayoutLength(Padding.Top);
902             LayoutLength childLeft = new LayoutLength(Padding.Start);
903
904             // Where end of child should go
905             LayoutLength width = new LayoutLength(right - left);
906
907             // Space available for child
908             LayoutLength childSpace = new LayoutLength(width - Padding.Start - Padding.End);
909
910             var LinearChildren = IterateLayoutChildren();
911             int count = LinearChildren.Count<LayoutItem>();
912
913             switch (VerticalAlignment)
914             {
915                 case VerticalAlignment.Bottom:
916                     // totalLength contains the padding already
917                     childTop = new LayoutLength(Padding.Top + bottom.AsDecimal() - top.AsDecimal() - totalLength);
918                     break;
919                 case VerticalAlignment.Center:
920                     // totalLength contains the padding already
921                     childTop = new LayoutLength(Padding.Top + (bottom.AsDecimal() - top.AsDecimal() - totalLength) / 2.0f);
922                     break;
923                 case VerticalAlignment.Top:  // FALL THROUGH (default)
924                 default:
925                     // totalLength contains the padding already
926                     childTop = new LayoutLength(Padding.Top);
927                     break;
928             }
929
930             for (int i = 0; i < count; i++)
931             {
932                 LayoutItem childLayout = LinearChildren.ElementAt<LayoutItem>(i);
933
934                 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
935                 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
936                 Extents childMargin = childLayout.Margin;
937
938                 childTop += childMargin.Top;
939                 switch (HorizontalAlignment)
940                 {
941                     case HorizontalAlignment.Begin:
942                     default:
943                         {
944                             childLeft = new LayoutLength(Padding.Start + childMargin.Start);
945                             break;
946                         }
947                     case HorizontalAlignment.End:
948                         {
949                             childLeft = new LayoutLength(width - Padding.End - childWidth - childMargin.End);
950                             break;
951                         }
952                     case HorizontalAlignment.Center:
953                         {
954                             childLeft = new LayoutLength(Padding.Start + ((childSpace - childWidth).AsDecimal() / 2.0f) + childMargin.Start - childMargin.End);
955                             break;
956                         }
957                 }
958                 childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
959                 childTop += childHeight + childMargin.Bottom + ((i < count - 1) ? CellPadding.Height : 0);
960             }
961         } // LayoutVertical
962
963         private void ForceUniformHeight(MeasureSpecification widthMeasureSpec)
964         {
965             // Pretend that the linear layout has an exact size. This is the measured height of
966             // ourselves. The measured height should be the max height of the children, changed
967             // to accommodate the heightMeasureSpec from the parent
968             MeasureSpecification uniformMeasureSpec = new MeasureSpecification(MeasuredHeight.Size, MeasureSpecification.ModeType.Exactly);
969             foreach (var childLayout in LayoutChildren)
970             {
971                 if (!childLayout.SetPositionByLayout)
972                 {
973                     continue;
974                 }
975                 int desiredChildHeight = childLayout.Owner.HeightSpecification;
976                 int desiredChildWidth = childLayout.Owner.WidthSpecification;
977
978                 if (desiredChildHeight == LayoutParamPolicies.MatchParent)
979                 {
980                     // Temporarily force children to reuse their original measured width
981                     int originalWidth = desiredChildWidth;
982                     childLayout.Owner.WidthSpecification = (int)childLayout.MeasuredWidth.Size.AsRoundedValue();
983                     // Remeasure with new dimensions
984                     MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0),
985                                              uniformMeasureSpec, new LayoutLength(0));
986                     // Restore width specification
987                     childLayout.Owner.WidthSpecification = originalWidth;
988                 }
989             }
990         }
991     } //LinearLayout
992 } // namespace