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/devel-api/layouting/layout-group-impl.h>
21 #include <dali/public-api/object/type-registry-helper.h>
22 #include <dali/devel-api/actors/actor-devel.h>
23 #include <dali/devel-api/object/handle-devel.h>
24 #include <dali/integration-api/debug.h>
27 #include <dali-toolkit/internal/layouting/layout-group-data-impl.h>
28 #include <dali-toolkit/public-api/controls/control-impl.h>
29 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
30 #include <dali-toolkit/internal/layouting/size-negotiation-mapper.h>
34 #if defined(DEBUG_ENABLED)
35 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
46 LayoutGroup::LayoutGroup()
47 : mImpl( new LayoutGroup::Impl() ),
52 LayoutGroupPtr LayoutGroup::New( Handle& owner )
54 LayoutGroupPtr layoutPtr = new LayoutGroup();
58 LayoutGroup::~LayoutGroup()
60 // An object with a unique_ptr to an opaque structure must define it's destructor in the translation unit
61 // where the opaque structure is defined. It cannot use the default method in the header file.
64 Toolkit::LayoutGroup::LayoutId LayoutGroup::Add( LayoutItem& child )
66 LayoutParent* oldParent = child.GetParent();
69 LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( oldParent ) );
72 parentGroup->Remove( child );
76 Impl::ChildLayout childLayout;
77 childLayout.layoutId = mImpl->mNextLayoutId++;
78 childLayout.child = &child;
79 mImpl->mChildren.emplace_back( childLayout );
81 child.SetParent( this );
83 auto owner = child.GetOwner();
85 // If the owner does not have any LayoutItem child properties, add them
86 if( ! DevelHandle::DoesCustomPropertyExist( owner, Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) )
88 // Set default properties for LayoutGroup and LayoutItem.
89 // Deriving classes can override OnChildAdd() to add their own default properties
90 GenerateDefaultChildPropertyValues( owner );
93 // Inform deriving classes that this child has been added
94 OnChildAdd( *childLayout.child.Get() );
96 // Now listen to future changes to the child properties.
97 DevelHandle::PropertySetSignal(owner).Connect( this, &LayoutGroup::OnSetChildProperties );
101 return childLayout.layoutId;
104 void LayoutGroup::Remove( Toolkit::LayoutGroup::LayoutId childId )
106 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
108 if( iter->layoutId == childId )
110 RemoveChild( *iter->child.Get() );
111 mImpl->mChildren.erase(iter);
118 void LayoutGroup::Remove( LayoutItem& child )
120 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
122 if( iter->child.Get() == &child )
124 RemoveChild( *iter->child.Get() );
125 mImpl->mChildren.erase(iter);
132 Toolkit::LayoutGroup::LayoutId LayoutGroup::Insert( LayoutItem& target, LayoutItem& child )
134 LayoutParent* oldParent = child.GetParent();
137 LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( oldParent ) );
140 parentGroup->Remove( child );
144 // Find target position
145 std::vector< Impl::ChildLayout >::iterator position;
146 for( auto iter = mImpl->mChildren.begin(); iter != mImpl->mChildren.end(); ++iter )
148 if( iter->child.Get() == &target )
155 Impl::ChildLayout childLayout;
156 childLayout.layoutId = mImpl->mNextLayoutId++;
157 childLayout.child = &child;
158 mImpl->mChildren.insert( position, childLayout );
160 child.SetParent( this );
162 auto owner = child.GetOwner();
164 // Inform deriving classes that this child has been added
165 OnChildAdd( *childLayout.child.Get() );
167 // Now listen to future changes to the child properties.
168 DevelHandle::PropertySetSignal(owner).Connect( this, &LayoutGroup::OnSetChildProperties );
172 return childLayout.layoutId;
175 Toolkit::LayoutGroup::LayoutId LayoutGroup::Move( LayoutItem& target, LayoutItem& child )
177 // Remove child from the previous position
178 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
180 if( iter->child.Get() == &child )
182 mImpl->mChildren.erase( iter );
187 // Find target position
188 std::vector< Impl::ChildLayout >::iterator position;
189 for( auto iter = mImpl->mChildren.begin(); iter != mImpl->mChildren.end(); ++iter )
191 if( iter->child.Get() == &target )
198 Impl::ChildLayout childLayout;
199 childLayout.layoutId = mImpl->mNextLayoutId++;
200 childLayout.child = &child;
201 mImpl->mChildren.insert( position, childLayout );
205 return childLayout.layoutId;
208 Toolkit::LayoutGroup::LayoutId LayoutGroup::MoveBack( LayoutItem& child )
210 // Remove child from the previous position
211 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
213 if( iter->child.Get() == &child )
215 mImpl->mChildren.erase( iter );
220 Impl::ChildLayout childLayout;
221 childLayout.layoutId = mImpl->mNextLayoutId++;
222 childLayout.child = &child;
223 mImpl->mChildren.emplace_back( childLayout );
227 return childLayout.layoutId;
230 void LayoutGroup::RemoveAll()
232 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; )
234 RemoveChild( *iter->child.Get() );
235 iter = mImpl->mChildren.erase(iter);
239 unsigned int LayoutGroup::GetChildCount() const
241 return mImpl->mChildren.size();
244 LayoutItemPtr LayoutGroup::GetChildAt( unsigned int index ) const
246 DALI_ASSERT_ALWAYS( index < mImpl->mChildren.size() );
247 return mImpl->mChildren[ index ].child;
250 LayoutItemPtr LayoutGroup::GetChild( Toolkit::LayoutGroup::LayoutId childId ) const
252 for( auto&& childLayout : mImpl->mChildren )
254 if( childLayout.layoutId == childId )
256 return childLayout.child;
262 Toolkit::LayoutGroup::LayoutId LayoutGroup::GetChildId( LayoutItem& child ) const
264 for( auto&& childLayout : mImpl->mChildren )
266 if( childLayout.child.Get() == &child )
268 return childLayout.layoutId;
271 return Toolkit::LayoutGroup::UNKNOWN_ID;
274 void LayoutGroup::OnChildAdd( LayoutItem& child )
278 void LayoutGroup::OnChildRemove( LayoutItem& child )
282 void LayoutGroup::DoInitialize()
286 void LayoutGroup::DoRegisterChildProperties( const std::string& containerType )
290 void LayoutGroup::OnSetChildProperties( Handle& handle, Property::Index index, Property::Value value )
292 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnSetChildProperties property(" << handle.GetPropertyName(index) << ")\n" );
294 if ( ( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) &&
295 ( index <= CHILD_PROPERTY_REGISTRATION_MAX_INDEX ) )
297 ( index == Toolkit::Control::Property::MARGIN || index == Toolkit::Control::Property::PADDING ) )
299 // If any child properties are set, must perform relayout
300 for( auto&& child : mImpl->mChildren )
302 if( child.child->GetOwner() == handle )
304 child.child->RequestLayout();
311 void LayoutGroup::GenerateDefaultChildPropertyValues( Handle child )
313 child.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION,
314 Toolkit::ChildLayoutData::WRAP_CONTENT );
315 child.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,
316 Toolkit::ChildLayoutData::WRAP_CONTENT );
319 void LayoutGroup::MeasureChildren( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
321 for( auto&& child : mImpl->mChildren )
323 //if( (child.mViewFlags & Impl::VISIBILITY_MASK) != Impl::GONE ) // Use owner visibility/enabled/ready
325 MeasureChild( child.child, widthMeasureSpec, heightMeasureSpec );
330 void LayoutGroup::MeasureChild( LayoutItemPtr child,
331 MeasureSpec parentWidthMeasureSpec,
332 MeasureSpec parentHeightMeasureSpec )
334 DALI_LOG_TRACE_METHOD( gLogFilter );
336 auto childOwner = child->GetOwner();
338 auto control = Toolkit::Control::DownCast( childOwner );
340 #if defined( DEBUG_ENABLED )
343 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChild(%s) natural size(%f, %f)\n",
344 control.GetName().c_str(), control.GetNaturalSize().width, control.GetNaturalSize().height );
349 // Get last stored width and height specifications for the child
350 auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
351 auto desiredHeight = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
352 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChild desiredWidth(%d) desiredHeight(%d)\n", desiredWidth, desiredHeight );
354 auto padding = GetPadding(); // Padding of this layout's owner, not of the child being measured.
356 const MeasureSpec childWidthMeasureSpec = GetChildMeasureSpec( parentWidthMeasureSpec,
357 padding.start + padding.end,
359 const MeasureSpec childHeightMeasureSpec = GetChildMeasureSpec( parentHeightMeasureSpec,
360 padding.top + padding.bottom,
363 child->Measure( childWidthMeasureSpec, childHeightMeasureSpec );
366 void LayoutGroup::MeasureChildWithMargins( LayoutItemPtr child,
367 MeasureSpec parentWidthMeasureSpec, LayoutLength widthUsed,
368 MeasureSpec parentHeightMeasureSpec, LayoutLength heightUsed)
370 auto childOwner = child->GetOwner();
371 auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
372 auto desiredHeight = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
374 auto padding = GetPadding(); // Padding of this layout's owner, not of the child being measured.
376 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins desiredWidth(%d)\n", desiredWidth );
378 MeasureSpec childWidthMeasureSpec = GetChildMeasureSpec( parentWidthMeasureSpec,
379 LayoutLength( padding.start + padding.end ) +
380 widthUsed, desiredWidth );
382 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins desiredHeight(%d)\n", desiredHeight );
384 MeasureSpec childHeightMeasureSpec = GetChildMeasureSpec( parentHeightMeasureSpec,
385 LayoutLength( padding.top + padding.bottom )+
386 heightUsed, desiredHeight );
388 child->Measure( childWidthMeasureSpec, childHeightMeasureSpec );
392 MeasureSpec LayoutGroup::GetChildMeasureSpec(
393 MeasureSpec measureSpec,
394 LayoutLength padding,
395 LayoutLength childDimension )
397 auto specMode = measureSpec.GetMode();
398 LayoutLength specSize = measureSpec.GetSize();
400 LayoutLength size = std::max( LayoutLength(0), specSize - padding ); // reduce available size by the owners padding
402 LayoutLength resultSize = 0;
403 MeasureSpec::Mode resultMode = MeasureSpec::Mode::UNSPECIFIED;
407 // Parent has imposed an exact size on us
408 case MeasureSpec::Mode::EXACTLY:
410 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::EXACTLY\n");
411 if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
413 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension MATCH_PARENT\n");
415 // Child wants to be our size. So be it.
417 resultMode = MeasureSpec::Mode::EXACTLY;
419 else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
421 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension WRAP_CONTENT\n");
423 // Child wants to determine its own size. It can't be
426 resultMode = MeasureSpec::Mode::AT_MOST;
430 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension UNSPECIFIED\n");
431 resultSize = childDimension;
432 resultMode = MeasureSpec::Mode::EXACTLY;
438 // Parent has imposed a maximum size on us
439 case MeasureSpec::Mode::AT_MOST:
441 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::AT_MOST\n");
442 if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
444 // Child wants to be our size, but our size is not fixed.
445 // Constrain child to not be bigger than us.
447 resultMode = MeasureSpec::Mode::AT_MOST;
449 else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
451 // Child wants to determine its own size. It can't be
454 resultMode = MeasureSpec::Mode::AT_MOST;
458 // Child wants a specific size... so be it
459 resultSize = childDimension + padding;
460 resultMode = MeasureSpec::Mode::EXACTLY;
466 // Parent asked to see how big we want to be
467 case MeasureSpec::Mode::UNSPECIFIED:
469 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::UNSPECIFIED\n");
471 if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
473 // Child wants to be our size... find out how big it should be
474 resultSize = LayoutItem::Impl::sUseZeroUnspecifiedMeasureSpec ? LayoutLength(0) : size;
475 resultMode = MeasureSpec::Mode::UNSPECIFIED;
477 else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
479 // Child wants to determine its own size.... find out how big
481 resultSize = LayoutItem::Impl::sUseZeroUnspecifiedMeasureSpec ? LayoutLength(0) : size;
482 resultMode = MeasureSpec::Mode::UNSPECIFIED;
486 // Child wants a specific size... let him have it
487 resultSize = childDimension + padding;
488 resultMode = MeasureSpec::Mode::EXACTLY;
494 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec resultSize(" << resultSize << ")\n" );
496 //noinspection ResourceType
497 return MeasureSpec( resultSize, resultMode );
501 void LayoutGroup::OnInitialize()
503 auto control = Toolkit::Control::DownCast( GetOwner() );
507 // Take ownership of existing children
508 for( unsigned int childIndex = 0 ; childIndex < control.GetChildCount(); ++childIndex )
510 ChildAddedToOwnerImpl( control.GetChildAt( childIndex ) );
513 DevelActor::ChildAddedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildAddedToOwner );
514 DevelActor::ChildRemovedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildRemovedFromOwner );
515 DevelActor::ChildOrderChangedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildOrderChanged );
516 DevelHandle::PropertySetSignal( control ).Connect( mSlotDelegate, &LayoutGroup::OnOwnerPropertySet );
518 if( control.GetParent() )
520 auto parent = Toolkit::Control::DownCast( control.GetParent() );
523 auto parentLayout = Toolkit::LayoutGroup::DownCast( DevelControl::GetLayout( parent ) );
526 Internal::LayoutGroup& parentLayoutImpl = GetImplementation( parentLayout );
528 unsigned int count = parent.GetChildCount();
529 unsigned int index = static_cast< unsigned int >( control.GetProperty< int >( DevelActor::Property::SIBLING_ORDER ) );
531 // Find insertion position
532 while( ++index < count )
534 auto sibling = Toolkit::Control::DownCast( parent.GetChildAt( index ) );
537 auto siblingLayout = DevelControl::GetLayout( sibling );
540 Internal::LayoutItem& siblingLayoutImpl = GetImplementation( siblingLayout );
541 parentLayoutImpl.Insert( siblingLayoutImpl, *this );
549 parentLayoutImpl.Add( *this );
555 RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_OWNER_SET );
559 void LayoutGroup::OnRegisterChildProperties( const std::string& containerType )
561 DoRegisterChildProperties( containerType );
564 void LayoutGroup::OnUnparent()
569 auto control = Toolkit::Control::DownCast( GetOwner() );
572 DevelActor::ChildAddedSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::ChildAddedToOwner );
573 DevelActor::ChildRemovedSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::ChildRemovedFromOwner );
574 DevelActor::ChildOrderChangedSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::ChildOrderChanged );
575 DevelHandle::PropertySetSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::OnOwnerPropertySet );
579 void LayoutGroup::RemoveChild( LayoutItem& item )
581 item.SetParent( nullptr );
582 OnChildRemove( item );
585 void LayoutGroup::ChildAddedToOwner( Actor child )
587 ChildAddedToOwnerImpl( child );
588 RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_CHILD_ADD, child, Actor() );
591 void LayoutGroup::ChildAddedToOwnerImpl( Actor child )
593 LayoutItemPtr childLayout;
594 Toolkit::Control control = Toolkit::Control::DownCast( child );
596 #if defined(DEBUG_ENABLED)
597 auto parent = Toolkit::Control::DownCast( GetOwner() );
598 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child control(%s) owner control(%s)\n",
599 control?control.GetName().c_str():"Invalid",
600 parent?parent.GetName().c_str():"Invalid" );
603 if( control ) // Can only support adding Controls, not Actors to layout
605 Internal::Control& childControlImpl = GetImplementation( control );
606 Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
607 childLayout = childControlDataImpl.GetLayout();
611 // If the child doesn't already have a layout, then create a LayoutItem or LayoutGroup for it.
612 // If control behaviour flag set to Layout then set a LayoutGroup.
613 if( DevelControl::IsLayoutingRequired( control ) )
615 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutGroup for control:%s\n",
616 control?control.GetName().c_str():"Invalid" );
617 childLayout = LayoutGroup::New( control );
621 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutItem for control:%s\n",
622 control?control.GetName().c_str():"Invalid" );
623 childLayout = LayoutItem::New( control );
624 childLayout->SetAnimateLayout( IsLayoutAnimated() ); // forces animation inheritance.
627 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child control:" << control.GetName() <<
628 " desiredWidth: " << control.GetNaturalSize().width <<
629 " desiredHeight:" << control.GetNaturalSize().height );
631 childControlDataImpl.SetLayout( *childLayout.Get() );
633 Vector3 size = child.GetTargetSize();
634 // If the size of the control is set explicitly make sure that the control size
635 // stays the same after the layout except it is over written with match parent specs.
638 childLayout->SetMinimumWidth( size.x );
643 childLayout->SetMinimumHeight( size.y );
645 // Default layout data will be generated by Add().
649 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child(%s) already has a Layout\n", control.GetName().c_str() );
650 LayoutGroupPtr layoutGroup( dynamic_cast< LayoutGroup* >( childLayout.Get() ) );
653 // Set only in case of leaf children
654 childLayout->SetAnimateLayout( IsLayoutAnimated() );
658 Add( *childLayout.Get() );
662 void LayoutGroup::ChildRemovedFromOwner( Actor child )
664 Toolkit::Control control = Toolkit::Control::DownCast( child );
667 Internal::Control& childControlImpl = GetImplementation( control );
668 Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
669 auto childLayout = childControlDataImpl.GetLayout();
672 Remove( *childLayout.Get() );
673 RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_CHILD_REMOVE, child, Actor() );
678 void LayoutGroup::ChildOrderChanged( Actor child )
680 Toolkit::Control childControl = Toolkit::Control::DownCast( child );
683 Internal::Control& childControlImpl = GetImplementation( childControl );
684 Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
686 auto childLayout = childControlDataImpl.GetLayout();
689 Toolkit::Control control = Toolkit::Control::DownCast( GetOwner() );
690 unsigned int count = control.GetChildCount();
691 unsigned int index = static_cast< unsigned int >( childControl.GetProperty< int >( DevelActor::Property::SIBLING_ORDER ) );
693 // Find insertion position
694 while( ++index < count )
696 auto sibling = Toolkit::Control::DownCast( control.GetChildAt( index ) );
699 auto siblingLayout = DevelControl::GetLayout( sibling );
702 Internal::LayoutItem& siblingLayoutImpl = GetImplementation( siblingLayout );
703 Move( siblingLayoutImpl, *childLayout );
709 MoveBack( *childLayout );
714 void LayoutGroup::OnOwnerPropertySet( Handle& handle, Property::Index index, Property::Value value )
716 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LayoutGroup::OnOwnerPropertySet\n");
717 auto actor = Actor::DownCast( handle );
720 index == Actor::Property::LAYOUT_DIRECTION ||
721 index == Toolkit::Control::Property::PADDING ||
722 index == Toolkit::Control::Property::MARGIN
730 void LayoutGroup::OnAnimationStateChanged( bool animateLayout )
732 // Change children's animation state
733 for( auto&& child : mImpl->mChildren )
735 LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( child.child.Get() ) );
738 // Change state only in case of leaf children
739 child.child->SetAnimateLayout( animateLayout );
744 void LayoutGroup::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
746 auto childCount = GetChildCount();
748 DALI_LOG_STREAM( gLogFilter, Debug::Verbose,
749 "LayoutGroup::OnMeasure Actor Id:" << Actor::DownCast(GetOwner()).GetId() <<
750 " Owner:" << Actor::DownCast(GetOwner()).GetName() <<
751 " Child Count:" << childCount <<
752 " MeasureSpecs( width:"<<widthMeasureSpec<<", height:"<<heightMeasureSpec );
754 auto widthMode = widthMeasureSpec.GetMode();
755 auto heightMode = heightMeasureSpec.GetMode();
756 LayoutLength widthSpecSize = widthMeasureSpec.GetSize();
757 LayoutLength heightSpecSize = heightMeasureSpec.GetSize();
759 bool exactWidth ( false );
760 bool exactHeight ( false );
762 // Layouting behaviour
763 // EXACT, width and height as provided.
764 // MATCH_PARENT, width and height that of parent
765 // WRAP_CONTENT, take width of widest child and height size of longest child (within given limit)
766 // UNSPECIFIED, take width of widest child and height size of longest child.
768 LayoutLength layoutWidth( 0 );
769 LayoutLength layoutHeight( 0 );
771 // If LayoutGroup has children then measure children to get max dimensions
772 if ( childCount > 0 )
774 for( unsigned int i=0; i<childCount; ++i )
776 auto childLayout = GetChildAt( i );
779 auto childControl = Toolkit::Control::DownCast(childLayout->GetOwner());
781 // If child control has children check if a ResizePolicy is set on it. A LayoutItem could be a legacy container.
782 // A legacy container would need it's ResizePolicy to be applied as a MeasureSpec.
784 // Check below will be true for legacy containers and controls with layout required set.
785 // Other layouts will have their own OnMeasure (a checked requirement) hence not execute LayoutGroup::OnMeasure.
786 // Controls which have set layout required will not be legacy controls hence should not have a ResizePolicy set.
787 // Only need to map the resize policy the first time as the Layouting system will then set it to FIXED.
788 if( childControl.GetChildCount() > 0 && ! mImpl->mResizePolicyMapped )
790 // First pass, Static mappings that are not dependant on parent
791 SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::WIDTH );
792 SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::HEIGHT );
793 mImpl->mResizePolicyMapped = true;
796 // Second pass, if any mappings were not possible due to parent size dependancies then calculate an exact desired size for child
797 if( true == childLayout->IsResizePolicyRequired() ) // No need to test child count as this flag would only be set if control had children.
799 // Get last stored width and height specifications for the child
800 LayoutLength desiredWidth = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
801 LayoutLength desiredHeight = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
803 DALI_LOG_INFO( gLogFilter, Debug::General, "LayoutGroup::MeasureChild Initial desired size pre ResizePolicy(%f,%f)\n", desiredWidth.AsInteger(), desiredHeight.AsInteger() );
805 childLayout->SetResizePolicyRequired( false ); // clear flag incase in case of changes before next Measure
806 SizeNegotiationMapper::GetSizeofChildForParentDependentResizePolicy( childControl, widthMeasureSpec, heightMeasureSpec, desiredWidth, desiredHeight );
808 // Parent dependant ResizePolicies become exact sizes so are now set on the child before it's measured.
809 childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, desiredWidth.AsInteger() );
810 childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, desiredHeight.AsInteger() );
812 DALI_LOG_INFO( gLogFilter, Debug::General, " LayoutGroup::OnMeasure ResizePolicy Required resulting size(%f,%f)\n", desiredWidth.AsInteger(), desiredHeight.AsInteger() );
816 MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
817 LayoutLength childWidth = childLayout->GetMeasuredWidth();
818 LayoutLength childHeight = childLayout->GetMeasuredHeight();
820 Extents childMargin = childLayout->GetMargin();
821 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure child " << childControl.GetName().c_str() << " width[" << childWidth << "] height[" << childHeight << "]\n" );
823 layoutWidth = std::max( layoutWidth, childWidth + childMargin.start + childMargin.end );
824 layoutHeight = std::max( layoutHeight, childHeight + childMargin.top + childMargin.bottom );
825 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure calculated child width[" << layoutWidth << "] height[" << layoutHeight << "]\n" );
829 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Not a layout\n" );
833 Extents padding = GetPadding();
834 layoutWidth += padding.start + padding.end;
835 layoutHeight += padding.top + padding.bottom;
839 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Getting default size as a leaf\n" );
840 // LayoutGroup does not contain any children so must be a leaf
841 layoutWidth = GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec );
842 layoutHeight = GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec );
845 // Can't exceed specified width
846 if( widthMode == MeasureSpec::Mode::EXACTLY )
850 else if ( widthMode == MeasureSpec::Mode::AT_MOST )
852 layoutWidth = std::min( layoutWidth, widthSpecSize );
855 // Can't exceed specified height
856 if( heightMode == MeasureSpec::Mode::EXACTLY )
860 else if ( heightMode == MeasureSpec::Mode::AT_MOST )
862 layoutHeight = std::min( layoutHeight, heightSpecSize );
865 layoutWidth = std::max( layoutWidth, GetSuggestedMinimumWidth() );
866 layoutHeight = std::max( layoutHeight, GetSuggestedMinimumHeight() );
870 layoutWidth = widthSpecSize;
875 layoutHeight = heightSpecSize;
878 DALI_LOG_STREAM( gLogFilter, Debug::General, "LayoutGroup::OnMeasure Measured size(" << layoutWidth << "," << layoutHeight << ") for : " << Actor::DownCast(GetOwner()).GetName() << " \n" );
879 SetMeasuredDimensions( MeasuredSize( layoutWidth ), MeasuredSize( layoutHeight ) );
882 void LayoutGroup::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
884 auto count = GetChildCount();
886 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup OnLayout owner:" << ( ( Toolkit::Control::DownCast(GetOwner())) ? Toolkit::Control::DownCast(GetOwner()).GetName() : "invalid" ) << " childCount:" << count );
888 for( unsigned int childIndex = 0; childIndex < count; childIndex++)
890 LayoutItemPtr childLayout = GetChildAt( childIndex );
891 if( childLayout != nullptr )
894 auto childOwner = childLayout->GetOwner();
895 LayoutLength childWidth = childLayout->GetMeasuredWidth();
896 LayoutLength childHeight = childLayout->GetMeasuredHeight();
897 Extents childMargin = childLayout->GetMargin();
898 auto control = Toolkit::Control::DownCast( childOwner );
899 Extents padding = GetPadding();
901 auto childPosition = control.GetProperty< Vector3 >( Actor::Property::POSITION );
902 auto anchorPoint = control.GetProperty< Vector3 >( Actor::Property::ANCHOR_POINT );
904 DALI_LOG_STREAM( gLogFilter, Debug::General, "LayoutGroup::OnLayout child[" << control.GetName() <<
905 "] position(" << childPosition << ") child width[" << childWidth << "] height[" << childHeight << "]\n" );
907 // Margin and Padding only supported when child anchor point is TOP_LEFT.
908 int paddingAndMarginOffsetX = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.top + childMargin.top ) : 0;
909 int paddingAndMarginOffsetY = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.start + childMargin.start ) : 0;
910 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnLayout paddingMargin offset(%d,%d)\n", paddingAndMarginOffsetX, paddingAndMarginOffsetY );
912 LayoutLength childLeft = childPosition.x + paddingAndMarginOffsetX;
913 LayoutLength childTop = childPosition.y + paddingAndMarginOffsetY;
915 childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
921 } // namespace Internal
922 } // namespace Toolkit