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/bin-layout-impl.h>
21 #include <dali/integration-api/debug.h>
22 #include <dali/public-api/common/extents.h>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali-toolkit/devel-api/layouting/layout-item.h>
25 #include <dali-toolkit/public-api/controls/control-impl.h>
26 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
27 #include <dali-toolkit/internal/layouting/size-negotiation-mapper.h>
31 #if defined(DEBUG_ENABLED)
32 static Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
43 BinLayoutPtr BinLayout::New()
45 BinLayoutPtr layout( new BinLayout() );
49 BinLayout::BinLayout()
54 BinLayout::~BinLayout()
58 void BinLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
60 #if defined(DEBUG_ENABLED)
61 auto actor = Actor::DownCast(GetOwner());
63 std::ostringstream oss;
64 oss << "BinLayout::OnMeasure ";
67 oss << "Actor Id:" << actor.GetId() << " Name:" << actor.GetName() << " ";
69 oss << "widthMeasureSpec:" << widthMeasureSpec << " heightMeasureSpec:" << heightMeasureSpec << std::endl;
70 DALI_LOG_INFO( gLogFilter, Debug::Concise, oss.str().c_str() );
73 auto childCount = GetChildCount();
75 DALI_LOG_STREAM( gLogFilter, Debug::Verbose,
76 "BinLayout::OnMeasure Actor Id:" << Actor::DownCast(GetOwner()).GetId() <<
77 " Owner:" << Actor::DownCast(GetOwner()).GetName() <<
78 " Child Count:" << childCount <<
79 " MeasureSpecs( width:"<<widthMeasureSpec<<", height:"<<heightMeasureSpec );
81 auto widthMode = widthMeasureSpec.GetMode();
82 auto heightMode = heightMeasureSpec.GetMode();
83 LayoutLength widthSpecSize = widthMeasureSpec.GetSize();
84 LayoutLength heightSpecSize = heightMeasureSpec.GetSize();
86 bool exactWidth ( false );
87 bool exactHeight ( false );
89 // Layouting behaviour
90 // EXACT, width and height as provided.
91 // MATCH_PARENT, width and height that of parent
92 // WRAP_CONTENT, take width of widest child and height size of longest child (within given limit)
93 // UNSPECIFIED, take width of widest child and height size of longest child.
95 LayoutLength layoutWidth( 0 );
96 LayoutLength layoutHeight( 0 );
98 // If BinLayout has children then measure children to get max dimensions
101 for( unsigned int i=0; i<childCount; ++i )
103 auto childLayout = GetChildAt( i );
106 auto childControl = Toolkit::Control::DownCast(childLayout->GetOwner());
108 // If child control has children check if a ResizePolicy is set on it. A LayoutItem could be a legacy container.
109 // A legacy container would need it's ResizePolicy to be applied as a MeasureSpec.
111 // Check below will be true for legacy containers and for controls with the layout flag required set.
112 // Other layouts will have their own OnMeasure (a requirement) hence not execute BinLayout::OnMeasure.
113 // Controls which have set the layout required flag will not be legacy controls hence should not have a ResizePolicy set.
114 if( childControl.GetChildCount() > 0 )
116 // First pass, Static mappings that are not dependant on parent
117 SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::WIDTH );
118 SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::HEIGHT );
121 // Second pass, if any mappings were not possible due to parent size dependencies then calculate an exact desired size for child
122 if( true == childLayout->IsResizePolicyRequired() ) // No need to test child count as this flag would only be set if control had children.
124 // Get last stored width and height specifications for the child
125 LayoutLength desiredWidth = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
126 LayoutLength desiredHeight = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
128 DALI_LOG_INFO( gLogFilter, Debug::General, "BinLayout::MeasureChild Initial desired size pre ResizePolicy(%f,%f)\n", desiredWidth.AsInteger(), desiredHeight.AsInteger() );
130 childLayout->SetResizePolicyRequired( false ); // clear flag incase in case of changes before next Measure
131 SizeNegotiationMapper::GetSizeofChildForParentDependentResizePolicy( childControl, widthMeasureSpec, heightMeasureSpec, desiredWidth, desiredHeight );
133 // Parent dependant ResizePolicies become exact sizes so are now set on the child before it's measured.
134 childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, desiredWidth.AsInteger() );
135 childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, desiredHeight.AsInteger() );
137 DALI_LOG_INFO( gLogFilter, Debug::General, " BinLayout::OnMeasure ResizePolicy Required resulting size(%f,%f)\n", desiredWidth.AsInteger(), desiredHeight.AsInteger() );
141 MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
142 LayoutLength childWidth = childLayout->GetMeasuredWidth();
143 LayoutLength childHeight = childLayout->GetMeasuredHeight();
145 Extents childMargin = childLayout->GetMargin();
147 // BinLayout width is that of it's widest child and it's height of the tallest child.
148 // MeasureSpec::Mode::UNSPECIFIED will use these sizes
149 layoutWidth = std::max( layoutWidth, childWidth + childMargin.start + childMargin.end );
150 layoutHeight = std::max( layoutHeight, childHeight + childMargin.top + childMargin.bottom );
151 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "BinLayout::OnMeasure calculated current BinLayout width[" << layoutWidth << "] height[" << layoutHeight << "]\n" );
155 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "BinLayout::OnMeasure Not a layout\n" );
159 Extents padding = GetPadding();
160 layoutWidth += padding.start + padding.end;
161 layoutHeight += padding.top + padding.bottom;
165 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "BinLayout::OnMeasure Getting default size as a leaf\n" );
166 // BinLayout does not contain any children so must be a leaf
167 layoutWidth = GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec );
168 layoutHeight = GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec );
171 // Can't exceed specified width
172 if( widthMode == MeasureSpec::Mode::EXACTLY )
176 else if ( widthMode == MeasureSpec::Mode::AT_MOST )
178 layoutWidth = std::min( layoutWidth, widthSpecSize );
181 // Can't exceed specified height
182 if( heightMode == MeasureSpec::Mode::EXACTLY )
186 else if ( heightMode == MeasureSpec::Mode::AT_MOST )
188 layoutHeight = std::min( layoutHeight, heightSpecSize );
191 layoutWidth = std::max( layoutWidth, GetSuggestedMinimumWidth() );
192 layoutHeight = std::max( layoutHeight, GetSuggestedMinimumHeight() );
196 layoutWidth = widthSpecSize;
201 layoutHeight = heightSpecSize;
204 DALI_LOG_STREAM( gLogFilter, Debug::General, "BinLayout::OnMeasure Measured size(" << layoutWidth << "," << layoutHeight << ") for : " << Actor::DownCast(GetOwner()).GetName() << " \n" );
205 SetMeasuredDimensions( MeasuredSize( layoutWidth ), MeasuredSize( layoutHeight ) );
209 void BinLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
211 auto count = GetChildCount();
213 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "BinLayout OnLayout owner:" << ( ( Toolkit::Control::DownCast(GetOwner())) ? Toolkit::Control::DownCast(GetOwner()).GetName() : "invalid" ) << " childCount:" << count );
215 for( unsigned int childIndex = 0; childIndex < count; childIndex++)
217 LayoutItemPtr childLayout = GetChildAt( childIndex );
218 if( childLayout != nullptr )
221 auto childOwner = childLayout->GetOwner();
222 LayoutLength childWidth = childLayout->GetMeasuredWidth();
223 LayoutLength childHeight = childLayout->GetMeasuredHeight();
224 Extents childMargin = childLayout->GetMargin();
225 auto control = Toolkit::Control::DownCast( childOwner );
226 Extents padding = GetPadding();
228 auto childPosition = control.GetProperty< Vector3 >( Actor::Property::POSITION );
229 auto anchorPoint = control.GetProperty< Vector3 >( Actor::Property::ANCHOR_POINT );
231 DALI_LOG_STREAM( gLogFilter, Debug::General, "BinLayout::OnLayout child[" << control.GetName() <<
232 "] position(" << childPosition << ") child width[" << childWidth << "] height[" << childHeight << "]\n" );
234 // Margin and Padding only supported when child anchor point is TOP_LEFT.
235 int paddingAndMarginOffsetX = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.top + childMargin.top ) : 0;
236 int paddingAndMarginOffsetY = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.start + childMargin.start ) : 0;
237 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "BinLayout::OnLayout paddingMargin offset(%d,%d)\n", paddingAndMarginOffsetX, paddingAndMarginOffsetY );
239 LayoutLength childLeft = childPosition.x + paddingAndMarginOffsetX;
240 LayoutLength childTop = childPosition.y + paddingAndMarginOffsetY;
242 childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
247 } // namespace Internal
248 } // namespace Toolkit