2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali-toolkit/internal/layouting/vbox-layout-impl.h>
21 #include <dali/integration-api/debug.h>
22 #include <dali/public-api/common/extents.h>
23 #include <dali/devel-api/actors/actor-devel.h>
24 #include <dali/devel-api/object/handle-devel.h>
25 #include <dali-toolkit/devel-api/layouting/layout-item.h>
26 #include <dali-toolkit/public-api/controls/control-impl.h>
27 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
31 #if defined(DEBUG_ENABLED)
32 static Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
43 VboxLayoutPtr VboxLayout::New()
45 VboxLayoutPtr layout( new VboxLayout() );
49 VboxLayout::VboxLayout()
56 VboxLayout::~VboxLayout()
60 void VboxLayout::DoInitialize()
64 void VboxLayout::DoRegisterChildProperties( const std::string& containerType )
66 auto typeInfo = Dali::TypeRegistry::Get().GetTypeInfo( containerType );
69 Property::IndexContainer indices;
70 typeInfo.GetChildPropertyIndices( indices );
72 if( std::find( indices.Begin(), indices.End(), Toolkit::VboxLayout::ChildProperty::WEIGHT ) ==
75 ChildPropertyRegistration( typeInfo.GetName(), "weight", Toolkit::VboxLayout::ChildProperty::WEIGHT, Property::FLOAT );
80 void VboxLayout::OnChildAdd( LayoutItem& child )
82 auto owner = child.GetOwner();
83 owner.SetProperty( Toolkit::VboxLayout::ChildProperty::WEIGHT, 1.0f );
86 void VboxLayout::SetCellPadding( LayoutSize size )
91 LayoutSize VboxLayout::GetCellPadding()
97 void VboxLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
99 #if defined(DEBUG_ENABLED)
100 auto actor = Actor::DownCast(GetOwner());
102 std::ostringstream oss;
103 oss << "VboxLayout::OnMeasure ";
106 oss << "Actor Id:" << actor.GetId() << " Name:" << actor.GetName() << " ";
108 oss << "widthMeasureSpec:" << widthMeasureSpec << " heightMeasureSpec:" << heightMeasureSpec << std::endl;
109 DALI_LOG_INFO( gLogFilter, Debug::Concise, oss.str().c_str() );
112 auto widthMode = widthMeasureSpec.GetMode();
114 bool matchWidth = false;
115 bool allFillParent = true;
116 LayoutLength maxWidth = 0;
117 LayoutLength alternativeMaxWidth = 0;
121 MeasuredSize::State widthState;
122 MeasuredSize::State heightState;
123 } childState = { MeasuredSize::State::MEASURED_SIZE_OK, MeasuredSize::State::MEASURED_SIZE_OK };
125 // measure children, and determine if further resolution is required
126 for( unsigned int i=0; i<GetChildCount(); ++i )
128 auto childLayout = GetChildAt( i );
131 auto childOwner = childLayout->GetOwner();
132 auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
134 MeasureChildWithMargins( childLayout, widthMeasureSpec, 0, heightMeasureSpec, 0 );
135 auto childHeight = childLayout->GetMeasuredHeight();
136 auto childMargin = childLayout->GetMargin();
138 auto length = childHeight + LayoutLength::IntType(childMargin.top + childMargin.bottom );
140 auto cellPadding = i<GetChildCount()-1 ? mCellPadding.height : 0;
141 auto totalLength = mTotalLength;
142 mTotalLength = std::max( totalLength, totalLength + length + cellPadding);
144 bool matchWidthLocally = false;
145 if( widthMode != MeasureSpec::Mode::EXACTLY && desiredWidth == Toolkit::ChildLayoutData::MATCH_PARENT )
147 // Will have to re-measure at least this child when we know exact height.
149 matchWidthLocally = true;
152 auto marginWidth = LayoutLength( childMargin.start + childMargin.end );
153 auto childWidth = childLayout->GetMeasuredWidth() + marginWidth;
155 // was combineMeasuredStates()
156 if( childLayout->GetMeasuredWidthAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
158 childState.widthState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
160 if( childLayout->GetMeasuredHeightAndState().GetState() == MeasuredSize::State::MEASURED_SIZE_TOO_SMALL )
162 childState.heightState = MeasuredSize::State::MEASURED_SIZE_TOO_SMALL;
165 maxWidth = std::max( maxWidth, childWidth );
166 allFillParent = ( allFillParent && desiredWidth == Toolkit::ChildLayoutData::MATCH_PARENT );
167 alternativeMaxWidth = std::max( alternativeMaxWidth, matchWidthLocally ? marginWidth : childWidth );
170 Extents padding = GetPadding();
171 mTotalLength += padding.top + padding.bottom;
172 auto heightSize = mTotalLength;
173 heightSize = std::max( heightSize, GetSuggestedMinimumHeight() );
174 MeasuredSize heightSizeAndState = ResolveSizeAndState( heightSize, heightMeasureSpec, MeasuredSize::State::MEASURED_SIZE_OK);
175 heightSize = heightSizeAndState.GetSize();
177 if( !allFillParent && widthMode != MeasureSpec::Mode::EXACTLY )
179 maxWidth = alternativeMaxWidth;
181 maxWidth += padding.start + padding.end;
182 maxWidth = std::max( maxWidth, GetSuggestedMinimumWidth() );
184 heightSizeAndState.SetState( childState.heightState );
186 SetMeasuredDimensions( ResolveSizeAndState( maxWidth, widthMeasureSpec, childState.widthState ),
187 heightSizeAndState );
191 ForceUniformWidth( GetChildCount(), heightMeasureSpec );
195 void VboxLayout::ForceUniformWidth( int count, MeasureSpec heightMeasureSpec )
197 // Pretend that the linear layout has an exact size. This is the measured height of
198 // ourselves. The measured height should be the max height of the children, changed
199 // to accommodate the heightMeasureSpec from the parent
200 auto uniformMeasureSpec = MeasureSpec( GetMeasuredWidth(), MeasureSpec::Mode::EXACTLY );
201 for (int i = 0; i < count; ++i)
203 LayoutItemPtr childLayout = GetChildAt(i);
204 if( childLayout != nullptr )
206 auto childOwner = childLayout->GetOwner();
207 auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
208 auto desiredHeight = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
210 if( desiredWidth == Toolkit::ChildLayoutData::MATCH_PARENT )
212 // Temporarily force children to reuse their old measured height
213 int oldHeight = desiredHeight;
214 childOwner.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, childLayout->GetMeasuredHeight().mValue );
216 // Remeasure with new dimensions
217 MeasureChildWithMargins( childLayout, uniformMeasureSpec, 0, heightMeasureSpec, 0 );
219 childOwner.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, oldHeight );
225 void VboxLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
227 Extents padding = GetPadding();
229 LayoutLength childTop( 0 );
230 LayoutLength childLeft( padding.start );
232 // Where bottom of child should go
233 auto width = right - left;
235 // Space available for child
236 auto childSpace = width - padding.start - padding.end;
237 auto count = GetChildCount();
239 for( unsigned int childIndex = 0; childIndex < count; childIndex++)
241 LayoutItemPtr childLayout = GetChildAt( childIndex );
242 if( childLayout != nullptr )
244 auto childWidth = childLayout->GetMeasuredWidth();
245 auto childHeight = childLayout->GetMeasuredHeight();
247 auto childOwner = childLayout->GetOwner();
248 auto childMargin = childLayout->GetMargin();
250 childTop += childMargin.top;
251 childLeft = ( childSpace - childWidth ) / 2 + childMargin.start - childMargin.end;
253 childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
254 childTop += childHeight + childMargin.bottom + mCellPadding.height;
259 } // namespace Internal
260 } // namespace Toolkit