+void LinearLayout::MeasureVertical( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
+{
+ auto widthMode = widthMeasureSpec.GetMode();
+
+ bool matchWidth = false;
+ bool allFillParent = true;
+ LayoutLength maxWidth = 0;
+ LayoutLength alternativeMaxWidth = 0;
+
+ struct
+ {
+ MeasuredSize::State widthState;
+ MeasuredSize::State heightState;
+ } childState = { MeasuredSize::State::MEASURED_SIZE_OK, MeasuredSize::State::MEASURED_SIZE_OK };
+
+ // measure children, and determine if further resolution is required
+ for( unsigned int i=0; i<GetChildCount(); ++i )
+ {
+ auto childLayout = GetChildAt( i );
+ if( childLayout )
+ {
+ auto childOwner = childLayout->GetOwner();
+ auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
+
+ MeasureChildWithMargins( childLayout, widthMeasureSpec, 0, heightMeasureSpec, 0 );
+ auto childHeight = childLayout->GetMeasuredHeight();
+ auto childMargin = childLayout->GetMargin();
+ auto length = childHeight + LayoutLength::IntType(childMargin.top + childMargin.bottom );
+
+ auto cellPadding = i<GetChildCount()-1 ? mCellPadding.height : 0;
+ auto totalLength = mTotalLength;
+ mTotalLength = std::max( totalLength, totalLength + length + cellPadding);
+
+ bool matchWidthLocally = false;
+ if( widthMode != MeasureSpec::Mode::EXACTLY && desiredWidth == Toolkit::ChildLayoutData::MATCH_PARENT )
+ {
+ // Will have to re-measure at least this child when we know exact height.
+ matchWidth = true;
+ matchWidthLocally = true;
+ }
+
+ auto marginWidth = LayoutLength( childMargin.start + childMargin.end );
+ auto childWidth = childLayout->GetMeasuredWidth() + marginWidth;
+
+ // was combineMeasuredStates()
+ if( childLayout->GetMeasuredWidthAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
+ {
+ childState.widthState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
+ }
+ if( childLayout->GetMeasuredHeightAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
+ {
+ childState.heightState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
+ }
+
+ maxWidth = std::max( maxWidth, childWidth );
+ allFillParent = ( allFillParent && desiredWidth == Toolkit::ChildLayoutData::MATCH_PARENT );
+ alternativeMaxWidth = std::max( alternativeMaxWidth, matchWidthLocally ? marginWidth : childWidth );
+ }
+ }
+ Extents padding = GetPadding();
+ mTotalLength += padding.top + padding.bottom;
+ auto heightSize = mTotalLength;
+ heightSize = std::max( heightSize, GetSuggestedMinimumHeight() );
+ MeasuredSize heightSizeAndState = ResolveSizeAndState( heightSize, heightMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK);
+ heightSize = heightSizeAndState.GetSize();
+
+ if( !allFillParent && widthMode != MeasureSpec::Mode::EXACTLY )
+ {
+ maxWidth = alternativeMaxWidth;
+ }
+ maxWidth += padding.start + padding.end;
+ maxWidth = std::max( maxWidth, GetSuggestedMinimumWidth() );
+
+ heightSizeAndState.SetState( childState.heightState );
+
+ SetMeasuredDimensions( ResolveSizeAndState( maxWidth, widthMeasureSpec, childState.widthState ),
+ heightSizeAndState );
+
+ if( matchWidth )
+ {
+ ForceUniformWidth( GetChildCount(), heightMeasureSpec );
+ }
+}
+
+void LinearLayout::ForceUniformWidth( int count, MeasureSpec heightMeasureSpec )
+{
+ // Pretend that the linear layout has an exact size.
+ auto uniformMeasureSpec = MeasureSpec( GetMeasuredWidth(), MeasureSpec::Mode::EXACTLY );
+ for (int i = 0; i < count; ++i)
+ {
+ LayoutItemPtr childLayout = GetChildAt(i);
+ if( childLayout != nullptr )
+ {
+ auto childOwner = childLayout->GetOwner();
+ auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
+ auto desiredHeight = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
+
+ if( desiredWidth == Toolkit::ChildLayoutData::MATCH_PARENT )
+ {
+ // Temporarily force children to reuse their old measured height
+ int oldHeight = desiredHeight;
+ childOwner.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, childLayout->GetMeasuredHeight().mValue );
+
+ // Remeasure with new dimensions
+ MeasureChildWithMargins( childLayout, uniformMeasureSpec, 0, heightMeasureSpec, 0 );
+
+ childOwner.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, oldHeight );
+ }
+ }
+ }
+}
+
+void LinearLayout::LayoutVertical( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
+{
+ Extents padding = GetPadding();
+
+ LayoutLength childTop( 0 );
+ LayoutLength childLeft( padding.start );
+
+ // Where bottom of child should go
+ auto width = right - left;
+
+ // Space available for child
+ auto childSpace = width - padding.start - padding.end;
+ auto count = GetChildCount();
+
+ for( unsigned int childIndex = 0; childIndex < count; childIndex++)
+ {
+ LayoutItemPtr childLayout = GetChildAt( childIndex );
+ if( childLayout != nullptr )
+ {
+ auto childWidth = childLayout->GetMeasuredWidth();
+ auto childHeight = childLayout->GetMeasuredHeight();
+ auto childMargin = childLayout->GetMargin();
+
+ childTop += childMargin.top;
+ childLeft = ( childSpace - childWidth ) / 2 + childMargin.start - childMargin.end;
+
+ childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
+ childTop += childHeight + childMargin.bottom + mCellPadding.height;
+ }
+ }
+}
+