1 /* Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using System.Collections.Generic;
26 /// [Draft] This class implements a linear box layout, automatically handling right to left or left to right direction change.
28 public class LinearLayout : LayoutGroup
30 private Alignment linearAlignment = Alignment.Top;
33 /// [Draft] Enumeration for the direction in which the content is laid out
35 /// <since_tizen> 6 </since_tizen>
36 public enum Orientation
49 /// [Draft] Enumeration for the alignment of the linear layout items
51 /// <since_tizen> 6 </since_tizen>
52 [Obsolete("This has been deprecated in API9 and will be removed in API11. Use HorizontalAlignment and VerticalAlignment instead.")]
56 /// At the left/right edge of the container (maps to LTR/RTL direction for horizontal orientation)
60 /// At the right/left edge of the container (maps to LTR/RTL direction for horizontal orientation)
64 /// At the horizontal center of the container
66 CenterHorizontal = 0x4,
68 /// At the top edge of the container
72 /// At the bottom edge of the container
76 /// At the vertical center of the container
78 CenterVertical = 0x20,
80 /// At the vertical and horizontal center of the container
85 struct HeightAndWidthState
87 public MeasuredSize.StateType widthState;
88 public MeasuredSize.StateType heightState;
90 public HeightAndWidthState(MeasuredSize.StateType width, MeasuredSize.StateType height)
98 /// [Draft] Get/Set the orientation in the layout
100 /// <since_tizen> 6 </since_tizen>
101 public LinearLayout.Orientation LinearOrientation
105 return linearOrientation;
109 linearOrientation = value;
115 /// [Draft] Get/Set the padding between cells in the layout
117 /// <since_tizen> 6 </since_tizen>
118 public Size2D CellPadding
122 if (cellPadding == null)
124 cellPadding = new Size2D(0, 0);
138 /// [Draft] Get/Set the alignment in the layout
140 /// <since_tizen> 6 </since_tizen>
141 [Obsolete("This has been deprecated in API9 and will be removed in API11. Use HorizontalAlignment and VerticalAlignment properties instead.")]
142 public LinearLayout.Alignment LinearAlignment
146 return linearAlignment;
151 if (linearAlignment == value)
156 linearAlignment = value;
158 switch (linearAlignment)
160 case Alignment.Begin:
161 HorizontalAlignment = HorizontalAlignment.Begin;
164 HorizontalAlignment = HorizontalAlignment.End;
166 case Alignment.CenterHorizontal:
167 HorizontalAlignment = HorizontalAlignment.Center;
170 VerticalAlignment = VerticalAlignment.Top;
172 case Alignment.Bottom:
173 VerticalAlignment = VerticalAlignment.Bottom;
175 case Alignment.CenterVertical:
176 VerticalAlignment = VerticalAlignment.Center;
178 case Alignment.Center:
179 HorizontalAlignment = HorizontalAlignment.Center;
180 VerticalAlignment = VerticalAlignment.Center;
189 /// Get/Set the horizontal alignment in the layout
191 /// <since_tizen> 9 </since_tizen>
192 public HorizontalAlignment HorizontalAlignment { get; set; } = HorizontalAlignment.Begin;
195 /// Get/Set the vertical alignment in the layout
197 /// <since_tizen> 9 </since_tizen>
198 public VerticalAlignment VerticalAlignment { get; set; } = VerticalAlignment.Top;
200 private float totalLength = 0.0f;
201 private Size2D cellPadding = new Size2D(0, 0);
202 private Orientation linearOrientation = Orientation.Horizontal;
205 /// [Draft] Constructor
207 /// <since_tizen> 6 </since_tizen>
208 public LinearLayout()
213 /// Measure the layout and its content to determine the measured width and the measured height.
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)
220 if (linearOrientation == Orientation.Horizontal)
222 MeasureHorizontal(widthMeasureSpec, heightMeasureSpec);
226 MeasureVertical(widthMeasureSpec, heightMeasureSpec);
231 /// Layout should assign a size and position to each of its children.<br />
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)
241 if (linearOrientation == Orientation.Horizontal)
243 LayoutHorizontal(left, top, right, bottom);
247 LayoutVertical(left, top, right, bottom);
252 private void MeasureWeightedChild(LayoutItem childLayout, float totalWeightLength, float totalWeight, float childWeight,
253 MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec,
254 HeightAndWidthState childState, Orientation orientation)
256 bool horizontal = false;
257 if (orientation == Orientation.Horizontal)
262 float childsShare = totalWeightLength * (childWeight / totalWeight);
263 float desiredWidth = childLayout.Owner.WidthSpecification;
264 float desiredHeight = childLayout.Owner.HeightSpecification;
266 MeasureSpecification childWidthMeasureSpec;
267 MeasureSpecification childHeightMeasureSpec;
271 childWidthMeasureSpec = new MeasureSpecification(new LayoutLength(childsShare - (childLayout.Margin.Start + childLayout.Margin.End)), MeasureSpecification.ModeType.Exactly);
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));
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));
289 childHeightMeasureSpec = new MeasureSpecification(new LayoutLength(childsShare - (childLayout.Margin.Top + childLayout.Margin.Bottom)), MeasureSpecification.ModeType.Exactly);
292 childLayout.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
294 // Child may now not fit in horizontal dimension.
295 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
297 childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
300 // Child may now not fit in vertical dimension.
301 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
303 childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
307 private void MeasureHorizontal(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
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();
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;
323 // Reset measure variable
326 HeightAndWidthState childState = new HeightAndWidthState(MeasuredSize.StateType.MeasuredSizeOK,
327 MeasuredSize.StateType.MeasuredSizeOK);
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)
336 if (!childLayout.SetPositionByLayout)
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 bool useRemainingWidth = (childDesiredWidth == 0) && (childWeight > 0);
347 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
349 totalWeight += childWeight;
350 childrenMatchParentCount++;
353 // MatchParent child layout's margin is not added to totalLength.
354 // Consequently, MatchParent child layout's margin is added to remaining size,
355 // so the margin is not shared with other MatchParent child layouts.
357 // LinearLayout has size 100.
358 // Child layout1 is MatchParent and its margin is 20. (This margin is not ad
359 // Child layout2 is MatchParent and its margin is 0.
360 // Then, child layout1's size is 30 and child layout2's size is 50.
361 if ((childDesiredWidth == LayoutParamPolicies.WrapContent) || ((childDesiredWidth >= 0) && (!useRemainingWidth)))
363 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
365 float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
367 if (childMeasuredWidth < 0)
369 totalLength = Math.Max(totalLength, totalLength + childMarginWidth);
373 totalLength = Math.Max(totalLength, totalLength + childMeasuredWidth + childMarginWidth);
377 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
379 childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
381 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
383 childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
385 } // 1ST PHASE foreach
387 totalLength = Math.Max(totalLength, totalLength + CellPadding.Width * (childrenCount - 1) + Padding.Start + Padding.End);
388 float widthSize = Math.Max(totalLength, SuggestedMinimumWidth.AsDecimal());
389 MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(widthSize), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
390 widthSize = widthSizeAndState.Size.AsDecimal();
392 float remainingWidth = widthSize - totalLength;
393 float totalWeightLength = 0.0f;
395 // Up to now, only WrapContent children's sizes are added to the totalLength.
396 // Since the totalLength is used in OnLayout as the sum of all children's sizes,
397 // the layout size is assigned to the totalLength if MatchParent child exists.
398 if (childrenMatchParentCount > 0)
400 totalLength = widthSize;
405 // We measure all children whose width specification policy is MatchParent without weight.
406 // After 2nd phase, all children's widths are calculated without considering weight.
407 // And the widths of all weighted children are accumulated to calculate weighted width.
408 foreach (var childLayout in LayoutChildren)
410 if (!childLayout.SetPositionByLayout)
414 int childDesiredWidth = childLayout.Owner.WidthSpecification;
415 int childDesiredHeight = childLayout.Owner.HeightSpecification;
416 float childWeight = childLayout.Owner.Weight;
417 Extents childMargin = childLayout.Margin;
418 bool useRemainingWidth = (childDesiredWidth == 0) && (childWeight > 0);
419 bool needToMeasure = false;
421 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
425 needToMeasure = true;
427 // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
428 // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
429 // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
431 // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
432 else if (Owner.HeightSpecification == LayoutParamPolicies.WrapContent)
434 if (childDesiredHeight == LayoutParamPolicies.MatchParent)
436 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!");
440 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!");
445 if (remainingWidth > 0)
447 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
451 // In MeasureChildWithMargins(), it is assumed that widthMeasureSpec includes Padding.Start and Padding.End.
452 // Therefore, Padding.Start and Padding.End are added to widthMeasureSpec.Size before it is passed to MeasureChildWithMargins().
453 widthMeasureSpec.SetSize(new LayoutLength((int)(remainingWidth / childrenMatchParentCount) + Padding.Start + Padding.End));
454 needToMeasure = true;
456 // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
457 // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
458 // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
460 // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
461 else if (Owner.WidthSpecification == LayoutParamPolicies.WrapContent)
463 if (childDesiredWidth == LayoutParamPolicies.MatchParent)
465 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!");
469 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!");
475 if (needToMeasure == true)
477 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
480 if ((childWeight > 0) && ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (childDesiredWidth == 0)))
482 float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
484 if (childMeasuredWidth < 0)
486 totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMargin.Start + childMargin.End);
490 totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMeasuredWidth + childMargin.Start + childMargin.End);
493 } // 2ND PHASE foreach
497 // We measure all weighted children whose owner has weight greater than 0.
498 // After 3rd phase, all weighted children has width which is proportional to their weights
499 // in remaining width of parent.
500 if (totalWeight > 0.0f)
502 foreach (LayoutItem childLayout in LayoutChildren)
504 if (!childLayout.SetPositionByLayout)
508 int childDesiredWidth = childLayout.Owner.WidthSpecification;
509 float childWeight = childLayout.Owner.Weight;
511 if ((childWeight > 0) && ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (childDesiredWidth == 0)))
515 MeasureWeightedChild(childLayout, totalWeightLength, totalWeight, childWeight,
516 widthMeasureSpec, heightMeasureSpec, childState,
517 Orientation.Horizontal);
519 // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
520 // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
521 // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
523 // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
524 else if (Owner.WidthSpecification == LayoutParamPolicies.WrapContent)
526 if (childDesiredWidth == LayoutParamPolicies.MatchParent)
528 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!");
532 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!");
540 // Decide the max height among children.
541 foreach (var childLayout in LayoutChildren)
543 if (!childLayout.SetPositionByLayout)
548 Extents childMargin = childLayout.Margin;
549 float childMarginHeight = childMargin.Top + childMargin.Bottom;
550 float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
552 if (childMeasuredHeight < 0)
554 maxHeight = Math.Max(maxHeight, childMarginHeight);
558 maxHeight = Math.Max(maxHeight, childMeasuredHeight + childMarginHeight);
562 // Decide the max height compared with paddings and its suggested height.
563 maxHeight = Math.Max(maxHeight, maxHeight + (Padding.Top + Padding.Bottom));
564 maxHeight = Math.Max(maxHeight, SuggestedMinimumHeight.AsRoundedValue());
566 widthSizeAndState.State = childState.widthState;
568 SetMeasuredDimensions(widthSizeAndState,
569 ResolveSizeAndState(new LayoutLength(maxHeight), heightMeasureSpec, childState.heightState));
570 } // MeasureHorizontal
572 private void MeasureVertical(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
574 var widthMode = widthMeasureSpec.Mode;
575 var heightMode = heightMeasureSpec.Mode;
576 bool isWidthExactly = (widthMode == MeasureSpecification.ModeType.Exactly);
577 bool isHeightExactly = (heightMode == MeasureSpecification.ModeType.Exactly);
578 float maxWidth = 0.0f;
579 float totalWeight = 0.0f;
580 int childrenCount = IterateLayoutChildren().Count();
582 // Child layout, which wants to match its height to its parent's remaining height, is either following 1 or 2.
583 // 1. Child layout whose Owner.HeightSpecification is LayoutParamPolicies.MatchParent.
584 // 2. Child layout whose Owner.HeightSpecification is 0 and Owner.Weight is greater than 0.
585 // The number of child layout which wants to match its height to its parent's remaining height.
586 int childrenMatchParentCount = 0;
588 // Reset measure variable
591 HeightAndWidthState childState = new HeightAndWidthState(MeasuredSize.StateType.MeasuredSizeOK,
592 MeasuredSize.StateType.MeasuredSizeOK);
596 // We measure all children whose height specification policy is WrapContent without weight.
597 // After 1st phase, remaining height of parent is accumulated to calculate height of children
598 // whose height specification policy is MatchParent.
599 foreach (var childLayout in LayoutChildren)
601 if (!childLayout.SetPositionByLayout)
605 int childDesiredWidth = childLayout.Owner.WidthSpecification;
606 int childDesiredHeight = childLayout.Owner.HeightSpecification;
607 float childWeight = childLayout.Owner.Weight;
608 Extents childMargin = childLayout.Margin;
609 float childMarginHeight = childMargin.Top + childMargin.Bottom;
610 bool useRemainingHeight = (childDesiredHeight == 0) && (childWeight > 0);
612 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
614 totalWeight += childWeight;
615 childrenMatchParentCount++;
618 // MatchParent child layout's margin is not added to totalLength.
619 // Consequently, MatchParent child layout's margin is added to remaining size,
620 // so the margin is not shared with other MatchParent child layouts.
622 // LinearLayout has size 100.
623 // Child layout1 is MatchParent and its margin is 20. (This margin is not ad
624 // Child layout2 is MatchParent and its margin is 0.
625 // Then, child layout1's size is 30 and child layout2's size is 50.
626 if ((childDesiredHeight == LayoutParamPolicies.WrapContent) || ((childDesiredHeight >= 0) && (!useRemainingHeight)))
628 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
630 float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
632 if (childMeasuredHeight < 0)
634 totalLength = Math.Max(totalLength, totalLength + childMarginHeight);
638 totalLength = Math.Max(totalLength, totalLength + childMeasuredHeight + childMarginHeight);
642 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
644 childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
646 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
648 childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
650 } // 1ST PHASE foreach
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();
657 float remainingHeight = heightSize - totalLength;
658 float totalWeightLength = 0.0f;
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)
665 totalLength = heightSize;
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)
675 if (!childLayout.SetPositionByLayout)
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;
686 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
690 needToMeasure = true;
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.
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)
699 if (childDesiredWidth == LayoutParamPolicies.MatchParent)
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!");
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!");
710 if (remainingHeight > 0)
712 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
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;
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.
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)
728 if (childDesiredHeight == LayoutParamPolicies.MatchParent)
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!");
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!");
740 if (needToMeasure == true)
742 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
745 if ((childWeight > 0) && ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (childDesiredHeight == 0)))
747 float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
749 if (childMeasuredHeight < 0)
751 totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMargin.Top + childMargin.Bottom);
755 totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMeasuredHeight + childMargin.Top + childMargin.Bottom);
758 } // 2ND PHASE foreach
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.
767 foreach (var childLayout in LayoutChildren)
769 if (!childLayout.SetPositionByLayout)
773 int childDesiredHeight = childLayout.Owner.HeightSpecification;
774 float childWeight = childLayout.Owner.Weight;
776 if ((childWeight > 0) && ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (childDesiredHeight == 0)))
780 MeasureWeightedChild(childLayout, totalWeightLength, totalWeight, childWeight,
781 widthMeasureSpec, heightMeasureSpec, childState,
782 Orientation.Vertical);
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.
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)
791 if (childDesiredHeight == LayoutParamPolicies.MatchParent)
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!");
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!");
802 } // 3RD PHASE foreach
804 // Decide the max width among children.
805 foreach (var childLayout in LayoutChildren)
807 if (!childLayout.SetPositionByLayout)
812 Extents childMargin = childLayout.Margin;
813 float childMarginWidth = childMargin.Start + childMargin.End;
814 float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
816 if (childMeasuredWidth < 0)
818 maxWidth = Math.Max(maxWidth, childMarginWidth);
822 maxWidth = Math.Max(maxWidth, childMeasuredWidth + childMarginWidth);
826 // Decide the max width compared with paddings and its suggested width.
827 maxWidth = Math.Max(maxWidth, maxWidth + (Padding.Start + Padding.End));
828 maxWidth = Math.Max(maxWidth, SuggestedMinimumWidth.AsRoundedValue());
830 heightSizeAndState.State = childState.heightState;
832 SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(maxWidth), widthMeasureSpec, childState.widthState),
836 private void LayoutHorizontal(LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
838 bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
840 LayoutLength childTop = new LayoutLength(Padding.Top);
841 LayoutLength childLeft = new LayoutLength(Padding.Start);
843 // Where bottom of child should go
844 LayoutLength height = new LayoutLength(bottom - top);
846 // Space available for child
847 LayoutLength childSpace = new LayoutLength(height - Padding.Top - Padding.Bottom);
849 var LinearChildren = IterateLayoutChildren();
850 int count = LinearChildren.Count<LayoutItem>();
852 switch (HorizontalAlignment)
854 case HorizontalAlignment.End:
855 // totalLength contains the padding already
856 // In case of RTL map END alignment to the left edge
859 childLeft = new LayoutLength(Padding.Start);
863 childLeft = new LayoutLength(Padding.Start + right.AsDecimal() - left.AsDecimal() - totalLength);
866 case HorizontalAlignment.Center:
867 // totalLength contains the padding already
868 childLeft = new LayoutLength(Padding.Start + (right.AsDecimal() - left.AsDecimal() - totalLength) / 2.0f);
870 case HorizontalAlignment.Begin: // FALL THROUGH (default)
872 // totalLength contains the padding already
873 // In case of RTL map BEGIN alignment to the right edge
876 childLeft = new LayoutLength(Padding.Start + right.AsDecimal() - left.AsDecimal() - totalLength);
880 childLeft = new LayoutLength(Padding.Start);
888 // In case of RTL, start drawing from the last child.
895 for (int i = 0; i < count; i++)
897 int childIndex = start + dir * i;
898 // Get a reference to the childLayout at the given index
899 LayoutItem childLayout = LinearChildren.ElementAt<LayoutItem>(i);
901 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
902 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
903 Extents childMargin = childLayout.Margin;
905 switch (VerticalAlignment)
907 case VerticalAlignment.Bottom:
908 childTop = new LayoutLength(height - Padding.Bottom - childHeight - childMargin.Bottom);
910 case VerticalAlignment.Center:
911 childTop = new LayoutLength(Padding.Top + ((childSpace - childHeight).AsDecimal() / 2.0f) + childMargin.Top - childMargin.Bottom);
913 case VerticalAlignment.Top: // FALLTHROUGH default
915 childTop = new LayoutLength(Padding.Top + childMargin.Top);
918 childLeft += (isLayoutRtl ? childMargin.End : childMargin.Start);
919 childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
920 childLeft += childWidth + (isLayoutRtl ? childMargin.Start : childMargin.End) + ((i < count - 1) ? CellPadding.Width : 0);
922 } // LayoutHorizontally
924 private void LayoutVertical(LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
926 LayoutLength childTop = new LayoutLength(Padding.Top);
927 LayoutLength childLeft = new LayoutLength(Padding.Start);
929 // Where end of child should go
930 LayoutLength width = new LayoutLength(right - left);
932 // Space available for child
933 LayoutLength childSpace = new LayoutLength(width - Padding.Start - Padding.End);
935 var LinearChildren = IterateLayoutChildren();
936 int count = LinearChildren.Count<LayoutItem>();
938 switch (VerticalAlignment)
940 case VerticalAlignment.Bottom:
941 // totalLength contains the padding already
942 childTop = new LayoutLength(Padding.Top + bottom.AsDecimal() - top.AsDecimal() - totalLength);
944 case VerticalAlignment.Center:
945 // totalLength contains the padding already
946 childTop = new LayoutLength(Padding.Top + (bottom.AsDecimal() - top.AsDecimal() - totalLength) / 2.0f);
948 case VerticalAlignment.Top: // FALL THROUGH (default)
950 // totalLength contains the padding already
951 childTop = new LayoutLength(Padding.Top);
955 for (int i = 0; i < count; i++)
957 LayoutItem childLayout = LinearChildren.ElementAt<LayoutItem>(i);
959 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
960 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
961 Extents childMargin = childLayout.Margin;
963 childTop += childMargin.Top;
964 switch (HorizontalAlignment)
966 case HorizontalAlignment.Begin:
969 childLeft = new LayoutLength(Padding.Start + childMargin.Start);
972 case HorizontalAlignment.End:
974 childLeft = new LayoutLength(width - Padding.End - childWidth - childMargin.End);
977 case HorizontalAlignment.Center:
979 childLeft = new LayoutLength(Padding.Start + ((childSpace - childWidth).AsDecimal() / 2.0f) + childMargin.Start - childMargin.End);
983 childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
984 childTop += childHeight + childMargin.Bottom + ((i < count - 1) ? CellPadding.Height : 0);
988 private void ForceUniformHeight(MeasureSpecification widthMeasureSpec)
990 // Pretend that the linear layout has an exact size. This is the measured height of
991 // ourselves. The measured height should be the max height of the children, changed
992 // to accommodate the heightMeasureSpec from the parent
993 MeasureSpecification uniformMeasureSpec = new MeasureSpecification(MeasuredHeight.Size, MeasureSpecification.ModeType.Exactly);
994 foreach (var childLayout in LayoutChildren)
996 if (!childLayout.SetPositionByLayout)
1000 int desiredChildHeight = childLayout.Owner.HeightSpecification;
1001 int desiredChildWidth = childLayout.Owner.WidthSpecification;
1003 if (desiredChildHeight == LayoutParamPolicies.MatchParent)
1005 // Temporarily force children to reuse their original measured width
1006 int originalWidth = desiredChildWidth;
1007 childLayout.Owner.WidthSpecification = (int)childLayout.MeasuredWidth.Size.AsRoundedValue();
1008 // Remeasure with new dimensions
1009 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0),
1010 uniformMeasureSpec, new LayoutLength(0));
1011 // Restore width specification
1012 childLayout.Owner.WidthSpecification = originalWidth;