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("Deprecated in API9, will be removed in API11. Please 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("Deprecated in API9, will be removed in API11. Please 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 float childMarginHeight = childMargin.Top + childMargin.Bottom;
346 bool useRemainingWidth = (childDesiredWidth == 0) && (childWeight > 0);
348 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
350 totalWeight += childWeight;
351 childrenMatchParentCount++;
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.
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)))
364 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
366 float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
368 if (childMeasuredWidth < 0)
370 totalLength = Math.Max(totalLength, totalLength + childMarginWidth);
374 totalLength = Math.Max(totalLength, totalLength + childMeasuredWidth + childMarginWidth);
378 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
380 childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
382 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
384 childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
387 float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
388 if (childMeasuredHeight < 0)
390 maxHeight = Math.Max(maxHeight, childMarginHeight);
394 maxHeight = Math.Max(maxHeight, childMeasuredHeight + childMarginHeight);
396 } // 1ST PHASE foreach
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();
403 float remainingWidth = widthSize - totalLength;
404 float totalWeightLength = 0.0f;
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)
411 totalLength = widthSize;
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)
421 if (!childLayout.SetPositionByLayout)
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;
432 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
436 needToMeasure = true;
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.
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)
445 if (childDesiredHeight == LayoutParamPolicies.MatchParent)
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!");
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!");
456 if (remainingWidth > 0)
458 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
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;
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.
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)
474 if (childDesiredWidth == LayoutParamPolicies.MatchParent)
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!");
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!");
486 if (needToMeasure == true)
488 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
491 if ((childWeight > 0) && ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (childDesiredWidth == 0)))
493 float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
495 if (childMeasuredWidth < 0)
497 totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMargin.Start + childMargin.End);
501 totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMeasuredWidth + childMargin.Start + childMargin.End);
504 } // 2ND PHASE foreach
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)
513 foreach (LayoutItem childLayout in LayoutChildren)
515 if (!childLayout.SetPositionByLayout)
519 int childDesiredWidth = childLayout.Owner.WidthSpecification;
520 float childWeight = childLayout.Owner.Weight;
522 if ((childWeight > 0) && ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (childDesiredWidth == 0)))
526 MeasureWeightedChild(childLayout, totalWeightLength, totalWeight, childWeight,
527 widthMeasureSpec, heightMeasureSpec, childState,
528 Orientation.Horizontal);
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.
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)
537 if (childDesiredWidth == LayoutParamPolicies.MatchParent)
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!");
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!");
547 } // 3RD PHASE foreach
550 maxHeight = Math.Max(maxHeight, maxHeight + (Padding.Top + Padding.Bottom));
551 maxHeight = Math.Max(maxHeight, SuggestedMinimumHeight.AsRoundedValue());
553 widthSizeAndState.State = childState.widthState;
555 SetMeasuredDimensions(widthSizeAndState,
556 ResolveSizeAndState(new LayoutLength(maxHeight), heightMeasureSpec, childState.heightState));
557 } // MeasureHorizontal
559 private void MeasureVertical(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
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();
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;
575 // Reset measure variable
578 HeightAndWidthState childState = new HeightAndWidthState(MeasuredSize.StateType.MeasuredSizeOK,
579 MeasuredSize.StateType.MeasuredSizeOK);
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)
588 if (!childLayout.SetPositionByLayout)
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);
600 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
602 totalWeight += childWeight;
603 childrenMatchParentCount++;
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.
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)))
616 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
618 float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
620 if (childMeasuredHeight < 0)
622 totalLength = Math.Max(totalLength, totalLength + childMarginHeight);
626 totalLength = Math.Max(totalLength, totalLength + childMeasuredHeight + childMarginHeight);
630 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
632 childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
634 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
636 childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
639 float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
640 if (childMeasuredWidth < 0)
642 maxWidth = Math.Max(maxWidth, childMarginWidth);
646 maxWidth = Math.Max(maxWidth, childMeasuredWidth + childMarginWidth);
648 } // 1ST PHASE foreach
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();
655 float remainingHeight = heightSize - totalLength;
656 float totalWeightLength = 0.0f;
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)
663 totalLength = heightSize;
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)
673 if (!childLayout.SetPositionByLayout)
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;
684 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
688 needToMeasure = true;
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.
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)
697 if (childDesiredWidth == LayoutParamPolicies.MatchParent)
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!");
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!");
708 if (remainingHeight > 0)
710 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
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;
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.
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)
726 if (childDesiredHeight == LayoutParamPolicies.MatchParent)
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!");
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!");
738 if (needToMeasure == true)
740 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
743 if ((childWeight > 0) && ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (childDesiredHeight == 0)))
745 float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
747 if (childMeasuredHeight < 0)
749 totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMargin.Top + childMargin.Bottom);
753 totalWeightLength = Math.Max(totalWeightLength, totalWeightLength + childMeasuredHeight + childMargin.Top + childMargin.Bottom);
756 } // 2ND PHASE foreach
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.
765 foreach (var childLayout in LayoutChildren)
767 if (!childLayout.SetPositionByLayout)
771 int childDesiredHeight = childLayout.Owner.HeightSpecification;
772 float childWeight = childLayout.Owner.Weight;
774 if ((childWeight > 0) && ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (childDesiredHeight == 0)))
778 MeasureWeightedChild(childLayout, totalWeightLength, totalWeight, childWeight,
779 widthMeasureSpec, heightMeasureSpec, childState,
780 Orientation.Vertical);
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.
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)
789 if (childDesiredHeight == LayoutParamPolicies.MatchParent)
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!");
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!");
799 } // 3RD PHASE foreach
802 maxWidth = Math.Max(maxWidth, maxWidth + (Padding.Start + Padding.End));
803 maxWidth = Math.Max(maxWidth, SuggestedMinimumWidth.AsRoundedValue());
805 heightSizeAndState.State = childState.heightState;
807 SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(maxWidth), widthMeasureSpec, childState.widthState),
811 private void LayoutHorizontal(LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
813 bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
815 LayoutLength childTop = new LayoutLength(Padding.Top);
816 LayoutLength childLeft = new LayoutLength(Padding.Start);
818 // Where bottom of child should go
819 LayoutLength height = new LayoutLength(bottom - top);
821 // Space available for child
822 LayoutLength childSpace = new LayoutLength(height - Padding.Top - Padding.Bottom);
824 var LinearChildren = IterateLayoutChildren();
825 int count = LinearChildren.Count<LayoutItem>();
827 switch (HorizontalAlignment)
829 case HorizontalAlignment.End:
830 // totalLength contains the padding already
831 // In case of RTL map END alignment to the left edge
834 childLeft = new LayoutLength(Padding.Start);
838 childLeft = new LayoutLength(Padding.Start + right.AsDecimal() - left.AsDecimal() - totalLength);
841 case HorizontalAlignment.Center:
842 // totalLength contains the padding already
843 childLeft = new LayoutLength(Padding.Start + (right.AsDecimal() - left.AsDecimal() - totalLength) / 2.0f);
845 case HorizontalAlignment.Begin: // FALL THROUGH (default)
847 // totalLength contains the padding already
848 // In case of RTL map BEGIN alignment to the right edge
851 childLeft = new LayoutLength(Padding.Start + right.AsDecimal() - left.AsDecimal() - totalLength);
855 childLeft = new LayoutLength(Padding.Start);
863 // In case of RTL, start drawing from the last child.
870 for (int i = 0; i < count; i++)
872 int childIndex = start + dir * i;
873 // Get a reference to the childLayout at the given index
874 LayoutItem childLayout = LinearChildren.ElementAt<LayoutItem>(i);
876 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
877 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
878 Extents childMargin = childLayout.Margin;
880 switch (VerticalAlignment)
882 case VerticalAlignment.Bottom:
883 childTop = new LayoutLength(height - Padding.Bottom - childHeight - childMargin.Bottom);
885 case VerticalAlignment.Center:
886 childTop = new LayoutLength(Padding.Top + ((childSpace - childHeight).AsDecimal() / 2.0f) + childMargin.Top - childMargin.Bottom);
888 case VerticalAlignment.Top: // FALLTHROUGH default
890 childTop = new LayoutLength(Padding.Top + childMargin.Top);
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);
897 } // LayoutHorizontally
899 private void LayoutVertical(LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
901 LayoutLength childTop = new LayoutLength(Padding.Top);
902 LayoutLength childLeft = new LayoutLength(Padding.Start);
904 // Where end of child should go
905 LayoutLength width = new LayoutLength(right - left);
907 // Space available for child
908 LayoutLength childSpace = new LayoutLength(width - Padding.Start - Padding.End);
910 var LinearChildren = IterateLayoutChildren();
911 int count = LinearChildren.Count<LayoutItem>();
913 switch (VerticalAlignment)
915 case VerticalAlignment.Bottom:
916 // totalLength contains the padding already
917 childTop = new LayoutLength(Padding.Top + bottom.AsDecimal() - top.AsDecimal() - totalLength);
919 case VerticalAlignment.Center:
920 // totalLength contains the padding already
921 childTop = new LayoutLength(Padding.Top + (bottom.AsDecimal() - top.AsDecimal() - totalLength) / 2.0f);
923 case VerticalAlignment.Top: // FALL THROUGH (default)
925 // totalLength contains the padding already
926 childTop = new LayoutLength(Padding.Top);
930 for (int i = 0; i < count; i++)
932 LayoutItem childLayout = LinearChildren.ElementAt<LayoutItem>(i);
934 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
935 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
936 Extents childMargin = childLayout.Margin;
938 childTop += childMargin.Top;
939 switch (HorizontalAlignment)
941 case HorizontalAlignment.Begin:
944 childLeft = new LayoutLength(Padding.Start + childMargin.Start);
947 case HorizontalAlignment.End:
949 childLeft = new LayoutLength(width - Padding.End - childWidth - childMargin.End);
952 case HorizontalAlignment.Center:
954 childLeft = new LayoutLength(Padding.Start + ((childSpace - childWidth).AsDecimal() / 2.0f) + childMargin.Start - childMargin.End);
958 childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
959 childTop += childHeight + childMargin.Bottom + ((i < count - 1) ? CellPadding.Height : 0);
963 private void ForceUniformHeight(MeasureSpecification widthMeasureSpec)
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)
971 if (!childLayout.SetPositionByLayout)
975 int desiredChildHeight = childLayout.Owner.HeightSpecification;
976 int desiredChildWidth = childLayout.Owner.WidthSpecification;
978 if (desiredChildHeight == LayoutParamPolicies.MatchParent)
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;