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 totalWeight += childWeight;
350 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
352 childrenMatchParentCount++;
355 // MatchParent child layout's margin is not added to totalLength.
356 // Consequently, MatchParent child layout's margin is added to remaining size,
357 // so the margin is not shared with other MatchParent child layouts.
359 // LinearLayout has size 100.
360 // Child layout1 is MatchParent and its margin is 20. (This margin is not ad
361 // Child layout2 is MatchParent and its margin is 0.
362 // Then, child layout1's size is 30 and child layout2's size is 50.
363 if ((childDesiredWidth == LayoutParamPolicies.WrapContent) || ((childDesiredWidth >= 0) && (!useRemainingWidth)))
365 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
367 float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
369 if (childMeasuredWidth < 0)
371 totalLength = Math.Max(totalLength, totalLength + childMarginWidth);
375 totalLength = Math.Max(totalLength, totalLength + childMeasuredWidth + childMarginWidth);
379 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
381 childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
383 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
385 childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
388 float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
389 if (childMeasuredHeight < 0)
391 maxHeight = Math.Max(maxHeight, childMarginHeight);
395 maxHeight = Math.Max(maxHeight, childMeasuredHeight + childMarginHeight);
397 } // 1ST PHASE foreach
399 totalLength = Math.Max(totalLength, totalLength + CellPadding.Width * (childrenCount - 1) + Padding.Start + Padding.End);
400 float widthSize = Math.Max(totalLength, SuggestedMinimumWidth.AsDecimal());
401 MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(widthSize), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
402 widthSize = widthSizeAndState.Size.AsDecimal();
404 float remainingWidth = widthSize - totalLength;
405 float totalWeightLength = 0.0f;
407 // Up to now, only WrapContent children's sizes are added to the totalLength.
408 // Since the totalLength is used in OnLayout as the sum of all children's sizes,
409 // the layout size is assigned to the totalLength if MatchParent child exists.
410 if (childrenMatchParentCount > 0)
412 totalLength = widthSize;
417 // We measure all children whose width specification policy is MatchParent without weight.
418 // After 2nd phase, all children's widths are calculated without considering weight.
419 // And the widths of all weighted children are accumulated to calculate weighted width.
420 foreach (var childLayout in LayoutChildren)
422 if (!childLayout.SetPositionByLayout)
426 int childDesiredWidth = childLayout.Owner.WidthSpecification;
427 int childDesiredHeight = childLayout.Owner.HeightSpecification;
428 float childWeight = childLayout.Owner.Weight;
429 Extents childMargin = childLayout.Margin;
430 bool useRemainingWidth = (childDesiredWidth == 0) && (childWeight > 0);
431 bool needToMeasure = false;
433 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
437 needToMeasure = true;
439 // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
440 // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
441 // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
443 // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
444 else if (Owner.HeightSpecification == LayoutParamPolicies.WrapContent)
446 if (childDesiredHeight == LayoutParamPolicies.MatchParent)
448 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s HeightSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s HeightSpecification is MatchParent!");
452 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s HeightSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s HeightSpecification is 0 with positive weight!");
457 if (remainingWidth > 0)
459 if ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (useRemainingWidth))
463 // In MeasureChildWithMargins(), it is assumed that widthMeasureSpec includes Padding.Start and Padding.End.
464 // Therefore, Padding.Start and Padding.End are added to widthMeasureSpec.Size before it is passed to MeasureChildWithMargins().
465 widthMeasureSpec.SetSize(new LayoutLength((int)(remainingWidth / childrenMatchParentCount) + Padding.Start + Padding.End));
466 needToMeasure = true;
468 // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
469 // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
470 // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
472 // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
473 else if (Owner.WidthSpecification == LayoutParamPolicies.WrapContent)
475 if (childDesiredWidth == LayoutParamPolicies.MatchParent)
477 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s WidthSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s WidthSpecification is MatchParent!");
481 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s WidthSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s WidthSpecification is 0 with positive weight!");
487 if (needToMeasure == true)
489 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(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);
505 } // 2ND PHASE foreach
509 // We measure all weighted children whose owner has weight greater than 0.
510 // After 3rd phase, all weighted children has width which is proportional to their weights
511 // in remaining width of parent.
512 if (totalWeight > 0.0f)
514 foreach (LayoutItem childLayout in LayoutChildren)
516 if (!childLayout.SetPositionByLayout)
520 int childDesiredWidth = childLayout.Owner.WidthSpecification;
521 float childWeight = childLayout.Owner.Weight;
523 if ((childWeight > 0) && ((childDesiredWidth == LayoutParamPolicies.MatchParent) || (childDesiredWidth == 0)))
527 MeasureWeightedChild(childLayout, totalWeightLength, totalWeight, childWeight,
528 widthMeasureSpec, heightMeasureSpec, childState,
529 Orientation.Horizontal);
531 // RelativeLayout's MatchParent children should not fill to the RelativeLayout.
532 // Because the children's sizes and positions are calculated by RelativeLayout's APIs.
533 // Therefore, not to fill the RelativeLayout, the mode is changed from Exactly to AtMost.
535 // Not to print the recursive reference error message for this case, Specification is checked if it is WrapContent.
536 else if (Owner.WidthSpecification == LayoutParamPolicies.WrapContent)
538 if (childDesiredWidth == LayoutParamPolicies.MatchParent)
540 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s WidthSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s WidthSpecification is MatchParent!");
544 Tizen.Log.Error("NUI", "There is a recursive reference! Parent layout(Owner: " + Owner + ")'s WidthSpecification is WrapContent and child layout(Owner: " + childLayout.Owner + ")'s WidthSpecification is 0 with positive weight!");
548 } // 3RD PHASE foreach
551 maxHeight = Math.Max(maxHeight, maxHeight + (Padding.Top + Padding.Bottom));
552 maxHeight = Math.Max(maxHeight, SuggestedMinimumHeight.AsRoundedValue());
554 widthSizeAndState.State = childState.widthState;
556 SetMeasuredDimensions(widthSizeAndState,
557 ResolveSizeAndState(new LayoutLength(maxHeight), heightMeasureSpec, childState.heightState));
558 } // MeasureHorizontal
560 private void MeasureVertical(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
562 var widthMode = widthMeasureSpec.Mode;
563 var heightMode = heightMeasureSpec.Mode;
564 bool isWidthExactly = (widthMode == MeasureSpecification.ModeType.Exactly);
565 bool isHeightExactly = (heightMode == MeasureSpecification.ModeType.Exactly);
566 float maxWidth = 0.0f;
567 float totalWeight = 0.0f;
568 int childrenCount = IterateLayoutChildren().Count();
570 // Child layout, which wants to match its height to its parent's remaining height, is either following 1 or 2.
571 // 1. Child layout whose Owner.HeightSpecification is LayoutParamPolicies.MatchParent.
572 // 2. Child layout whose Owner.HeightSpecification is 0 and Owner.Weight is greater than 0.
573 // The number of child layout which wants to match its height to its parent's remaining height.
574 int childrenMatchParentCount = 0;
576 // Reset measure variable
579 HeightAndWidthState childState = new HeightAndWidthState(MeasuredSize.StateType.MeasuredSizeOK,
580 MeasuredSize.StateType.MeasuredSizeOK);
584 // We measure all children whose height specification policy is WrapContent without weight.
585 // After 1st phase, remaining height of parent is accumulated to calculate height of children
586 // whose height specification policy is MatchParent.
587 foreach (var childLayout in LayoutChildren)
589 if (!childLayout.SetPositionByLayout)
593 int childDesiredWidth = childLayout.Owner.WidthSpecification;
594 int childDesiredHeight = childLayout.Owner.HeightSpecification;
595 float childWeight = childLayout.Owner.Weight;
596 Extents childMargin = childLayout.Margin;
597 float childMarginWidth = childMargin.Start + childMargin.End;
598 float childMarginHeight = childMargin.Top + childMargin.Bottom;
599 bool useRemainingHeight = (childDesiredHeight == 0) && (childWeight > 0);
601 totalWeight += childWeight;
603 if ((childDesiredHeight == LayoutParamPolicies.MatchParent) || (useRemainingHeight))
605 childrenMatchParentCount++;
608 // MatchParent child layout's margin is not added to totalLength.
609 // Consequently, MatchParent child layout's margin is added to remaining size,
610 // so the margin is not shared with other MatchParent child layouts.
612 // LinearLayout has size 100.
613 // Child layout1 is MatchParent and its margin is 20. (This margin is not ad
614 // Child layout2 is MatchParent and its margin is 0.
615 // Then, child layout1's size is 30 and child layout2's size is 50.
616 if ((childDesiredHeight == LayoutParamPolicies.WrapContent) || ((childDesiredHeight > 0) && (!useRemainingHeight)))
618 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
620 float childMeasuredHeight = childLayout.MeasuredHeight.Size.AsDecimal();
622 if (childMeasuredHeight < 0)
624 totalLength = Math.Max(totalLength, totalLength + childMarginHeight);
628 totalLength = Math.Max(totalLength, totalLength + childMeasuredHeight + childMarginHeight);
632 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
634 childState.widthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
636 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
638 childState.heightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
641 float childMeasuredWidth = childLayout.MeasuredWidth.Size.AsDecimal();
642 if (childMeasuredWidth < 0)
644 maxWidth = Math.Max(maxWidth, childMarginWidth);
648 maxWidth = Math.Max(maxWidth, childMeasuredWidth + childMarginWidth);
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));
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!");
801 } // 3RD PHASE foreach
804 maxWidth = Math.Max(maxWidth, maxWidth + (Padding.Start + Padding.End));
805 maxWidth = Math.Max(maxWidth, SuggestedMinimumWidth.AsRoundedValue());
807 heightSizeAndState.State = childState.heightState;
809 SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(maxWidth), widthMeasureSpec, childState.widthState),
813 private void LayoutHorizontal(LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
815 bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
817 LayoutLength childTop = new LayoutLength(Padding.Top);
818 LayoutLength childLeft = new LayoutLength(Padding.Start);
820 // Where bottom of child should go
821 LayoutLength height = new LayoutLength(bottom - top);
823 // Space available for child
824 LayoutLength childSpace = new LayoutLength(height - Padding.Top - Padding.Bottom);
826 var LinearChildren = IterateLayoutChildren();
827 int count = LinearChildren.Count<LayoutItem>();
829 switch (HorizontalAlignment)
831 case HorizontalAlignment.End:
832 // totalLength contains the padding already
833 // In case of RTL map END alignment to the left edge
836 childLeft = new LayoutLength(Padding.Start);
840 childLeft = new LayoutLength(Padding.Start + right.AsDecimal() - left.AsDecimal() - totalLength);
843 case HorizontalAlignment.Center:
844 // totalLength contains the padding already
845 childLeft = new LayoutLength(Padding.Start + (right.AsDecimal() - left.AsDecimal() - totalLength) / 2.0f);
847 case HorizontalAlignment.Begin: // FALL THROUGH (default)
849 // totalLength contains the padding already
850 // In case of RTL map BEGIN alignment to the right edge
853 childLeft = new LayoutLength(Padding.Start + right.AsDecimal() - left.AsDecimal() - totalLength);
857 childLeft = new LayoutLength(Padding.Start);
865 // In case of RTL, start drawing from the last child.
872 for (int i = 0; i < count; i++)
874 int childIndex = start + dir * i;
875 // Get a reference to the childLayout at the given index
876 LayoutItem childLayout = LinearChildren.ElementAt<LayoutItem>(i);
878 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
879 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
880 Extents childMargin = childLayout.Margin;
882 switch (VerticalAlignment)
884 case VerticalAlignment.Bottom:
885 childTop = new LayoutLength(height - Padding.Bottom - childHeight - childMargin.Bottom);
887 case VerticalAlignment.Center:
888 childTop = new LayoutLength(Padding.Top + ((childSpace - childHeight).AsDecimal() / 2.0f) + childMargin.Top - childMargin.Bottom);
890 case VerticalAlignment.Top: // FALLTHROUGH default
892 childTop = new LayoutLength(Padding.Top + childMargin.Top);
895 childLeft += (isLayoutRtl ? childMargin.End : childMargin.Start);
896 childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
897 childLeft += childWidth + (isLayoutRtl ? childMargin.Start : childMargin.End) + ((i < count - 1) ? CellPadding.Width : 0);
899 } // LayoutHorizontally
901 private void LayoutVertical(LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
903 LayoutLength childTop = new LayoutLength(Padding.Top);
904 LayoutLength childLeft = new LayoutLength(Padding.Start);
906 // Where end of child should go
907 LayoutLength width = new LayoutLength(right - left);
909 // Space available for child
910 LayoutLength childSpace = new LayoutLength(width - Padding.Start - Padding.End);
912 var LinearChildren = IterateLayoutChildren();
913 int count = LinearChildren.Count<LayoutItem>();
915 switch (VerticalAlignment)
917 case VerticalAlignment.Bottom:
918 // totalLength contains the padding already
919 childTop = new LayoutLength(Padding.Top + bottom.AsDecimal() - top.AsDecimal() - totalLength);
921 case VerticalAlignment.Center:
922 // totalLength contains the padding already
923 childTop = new LayoutLength(Padding.Top + (bottom.AsDecimal() - top.AsDecimal() - totalLength) / 2.0f);
925 case VerticalAlignment.Top: // FALL THROUGH (default)
927 // totalLength contains the padding already
928 childTop = new LayoutLength(Padding.Top);
932 for (int i = 0; i < count; i++)
934 LayoutItem childLayout = LinearChildren.ElementAt<LayoutItem>(i);
936 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
937 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
938 Extents childMargin = childLayout.Margin;
940 childTop += childMargin.Top;
941 switch (HorizontalAlignment)
943 case HorizontalAlignment.Begin:
946 childLeft = new LayoutLength(Padding.Start + childMargin.Start);
949 case HorizontalAlignment.End:
951 childLeft = new LayoutLength(width - Padding.End - childWidth - childMargin.End);
954 case HorizontalAlignment.Center:
956 childLeft = new LayoutLength(Padding.Start + ((childSpace - childWidth).AsDecimal() / 2.0f) + childMargin.Start - childMargin.End);
960 childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
961 childTop += childHeight + childMargin.Bottom + ((i < count - 1) ? CellPadding.Height : 0);
965 private void ForceUniformHeight(MeasureSpecification widthMeasureSpec)
967 // Pretend that the linear layout has an exact size. This is the measured height of
968 // ourselves. The measured height should be the max height of the children, changed
969 // to accommodate the heightMeasureSpec from the parent
970 MeasureSpecification uniformMeasureSpec = new MeasureSpecification(MeasuredHeight.Size, MeasureSpecification.ModeType.Exactly);
971 foreach (var childLayout in LayoutChildren)
973 if (!childLayout.SetPositionByLayout)
977 int desiredChildHeight = childLayout.Owner.HeightSpecification;
978 int desiredChildWidth = childLayout.Owner.WidthSpecification;
980 if (desiredChildHeight == LayoutParamPolicies.MatchParent)
982 // Temporarily force children to reuse their original measured width
983 int originalWidth = desiredChildWidth;
984 childLayout.Owner.WidthSpecification = (int)childLayout.MeasuredWidth.Size.AsRoundedValue();
985 // Remeasure with new dimensions
986 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0),
987 uniformMeasureSpec, new LayoutLength(0));
988 // Restore width specification
989 childLayout.Owner.WidthSpecification = originalWidth;