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.
65 Toolkit::LayoutGroup::LayoutId LayoutGroup::Add( LayoutItem& child )
67 LayoutParent* oldParent = child.GetParent();
70 LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( oldParent ) );
73 parentGroup->Remove( child );
77 Impl::ChildLayout childLayout;
78 childLayout.layoutId = mImpl->mNextLayoutId++;
79 childLayout.child = &child;
80 mImpl->mChildren.emplace_back( childLayout );
82 child.SetParent( this );
84 auto owner = child.GetOwner();
86 // If the owner does not have any LayoutItem child properties, add them
87 if( ! DevelHandle::DoesCustomPropertyExist( owner, Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) )
89 // Set default properties for LayoutGroup and LayoutItem.
90 // Deriving classes can override OnChildAdd() to add their own default properties
91 GenerateDefaultChildPropertyValues( owner );
94 // Inform deriving classes that this child has been added
95 OnChildAdd( *childLayout.child.Get() );
97 // Now listen to future changes to the child properties.
98 DevelHandle::PropertySetSignal(owner).Connect( this, &LayoutGroup::OnSetChildProperties );
102 return childLayout.layoutId;
105 void LayoutGroup::Remove( Toolkit::LayoutGroup::LayoutId childId )
107 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
109 if( iter->layoutId == childId )
111 RemoveChild( *iter->child.Get() );
112 mImpl->mChildren.erase(iter);
119 void LayoutGroup::Remove( LayoutItem& child )
121 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
123 if( iter->child.Get() == &child )
125 RemoveChild( *iter->child.Get() );
126 mImpl->mChildren.erase(iter);
133 Toolkit::LayoutGroup::LayoutId LayoutGroup::Insert( LayoutItem& target, LayoutItem& child )
135 LayoutParent* oldParent = child.GetParent();
138 LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( oldParent ) );
141 parentGroup->Remove( child );
145 // Find target position
146 std::vector< Impl::ChildLayout >::iterator position;
147 for( auto iter = mImpl->mChildren.begin(); iter != mImpl->mChildren.end(); ++iter )
149 if( iter->child.Get() == &target )
156 Impl::ChildLayout childLayout;
157 childLayout.layoutId = mImpl->mNextLayoutId++;
158 childLayout.child = &child;
159 mImpl->mChildren.insert( position, childLayout );
161 child.SetParent( this );
163 auto owner = child.GetOwner();
165 // Inform deriving classes that this child has been added
166 OnChildAdd( *childLayout.child.Get() );
168 // Now listen to future changes to the child properties.
169 DevelHandle::PropertySetSignal(owner).Connect( this, &LayoutGroup::OnSetChildProperties );
173 return childLayout.layoutId;
176 Toolkit::LayoutGroup::LayoutId LayoutGroup::Move( LayoutItem& target, LayoutItem& child )
178 // Remove child from the previous position
179 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
181 if( iter->child.Get() == &child )
183 mImpl->mChildren.erase( iter );
188 // Find target position
189 std::vector< Impl::ChildLayout >::iterator position;
190 for( auto iter = mImpl->mChildren.begin(); iter != mImpl->mChildren.end(); ++iter )
192 if( iter->child.Get() == &target )
199 Impl::ChildLayout childLayout;
200 childLayout.layoutId = mImpl->mNextLayoutId++;
201 childLayout.child = &child;
202 mImpl->mChildren.insert( position, childLayout );
206 return childLayout.layoutId;
209 Toolkit::LayoutGroup::LayoutId LayoutGroup::MoveBack( LayoutItem& child )
211 // Remove child from the previous position
212 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
214 if( iter->child.Get() == &child )
216 mImpl->mChildren.erase( iter );
221 Impl::ChildLayout childLayout;
222 childLayout.layoutId = mImpl->mNextLayoutId++;
223 childLayout.child = &child;
224 mImpl->mChildren.emplace_back( childLayout );
228 return childLayout.layoutId;
231 void LayoutGroup::RemoveAll()
233 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; )
235 RemoveChild( *iter->child.Get() );
236 iter = mImpl->mChildren.erase(iter);
240 unsigned int LayoutGroup::GetChildCount() const
242 return mImpl->mChildren.size();
245 LayoutItemPtr LayoutGroup::GetChildAt( unsigned int index ) const
247 DALI_ASSERT_ALWAYS( index < mImpl->mChildren.size() );
248 return mImpl->mChildren[ index ].child;
251 LayoutItemPtr LayoutGroup::GetChild( Toolkit::LayoutGroup::LayoutId childId ) const
253 for( auto&& childLayout : mImpl->mChildren )
255 if( childLayout.layoutId == childId )
257 return childLayout.child;
263 Toolkit::LayoutGroup::LayoutId LayoutGroup::GetChildId( LayoutItem& child ) const
265 for( auto&& childLayout : mImpl->mChildren )
267 if( childLayout.child.Get() == &child )
269 return childLayout.layoutId;
272 return Toolkit::LayoutGroup::UNKNOWN_ID;
275 void LayoutGroup::OnChildAdd( LayoutItem& child )
279 void LayoutGroup::OnChildRemove( LayoutItem& child )
283 void LayoutGroup::DoInitialize()
287 void LayoutGroup::DoRegisterChildProperties( const std::string& containerType )
291 void LayoutGroup::OnSetChildProperties( Handle& handle, Property::Index index, Property::Value value )
293 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnSetChildProperties property(" << handle.GetPropertyName(index) << ")\n" );
295 if ( ( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) &&
296 ( index <= CHILD_PROPERTY_REGISTRATION_MAX_INDEX ) )
298 ( index == Toolkit::Control::Property::MARGIN || index == Toolkit::Control::Property::PADDING ) )
300 // If any child properties are set, must perform relayout
301 for( auto&& child : mImpl->mChildren )
303 if( child.child->GetOwner() == handle )
305 child.child->RequestLayout();
312 void LayoutGroup::GenerateDefaultChildPropertyValues( Handle child )
314 child.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION,
315 Toolkit::ChildLayoutData::WRAP_CONTENT );
316 child.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,
317 Toolkit::ChildLayoutData::WRAP_CONTENT );
320 void LayoutGroup::MeasureChildren( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
322 for( auto&& child : mImpl->mChildren )
324 //if( (child.mViewFlags & Impl::VISIBILITY_MASK) != Impl::GONE ) // Use owner visibility/enabled/ready
326 MeasureChild( child.child, widthMeasureSpec, heightMeasureSpec );
331 void LayoutGroup::MeasureChild( LayoutItemPtr child,
332 MeasureSpec parentWidthMeasureSpec,
333 MeasureSpec parentHeightMeasureSpec )
335 DALI_LOG_TRACE_METHOD( gLogFilter );
337 auto childOwner = child->GetOwner();
339 auto control = Toolkit::Control::DownCast( childOwner );
341 #if defined( DEBUG_ENABLED )
344 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChild(%s) natural size(%f, %f)\n",
345 control.GetName().c_str(), control.GetNaturalSize().width, control.GetNaturalSize().height );
350 // Get last stored width and height specifications for the child
351 auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
352 auto desiredHeight = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
353 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChild desiredWidth(%d) desiredHeight(%d)\n", desiredWidth, desiredHeight );
355 auto padding = GetPadding(); // Padding of this layout's owner, not of the child being measured.
357 const MeasureSpec childWidthMeasureSpec = GetChildMeasureSpec( parentWidthMeasureSpec,
358 padding.start + padding.end,
360 const MeasureSpec childHeightMeasureSpec = GetChildMeasureSpec( parentHeightMeasureSpec,
361 padding.top + padding.bottom,
364 child->Measure( childWidthMeasureSpec, childHeightMeasureSpec );
367 void LayoutGroup::MeasureChildWithMargins( LayoutItemPtr child,
368 MeasureSpec parentWidthMeasureSpec, LayoutLength widthUsed,
369 MeasureSpec parentHeightMeasureSpec, LayoutLength heightUsed)
371 auto childOwner = child->GetOwner();
372 auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
373 auto desiredHeight = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
375 auto padding = GetPadding(); // Padding of this layout's owner, not of the child being measured.
377 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins desiredWidth(%d)\n", desiredWidth );
379 MeasureSpec childWidthMeasureSpec = GetChildMeasureSpec( parentWidthMeasureSpec,
380 LayoutLength( padding.start + padding.end ) +
381 widthUsed, desiredWidth );
383 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins desiredHeight(%d)\n", desiredHeight );
385 MeasureSpec childHeightMeasureSpec = GetChildMeasureSpec( parentHeightMeasureSpec,
386 LayoutLength( padding.top + padding.bottom )+
387 heightUsed, desiredHeight );
389 child->Measure( childWidthMeasureSpec, childHeightMeasureSpec );
393 MeasureSpec LayoutGroup::GetChildMeasureSpec(
394 MeasureSpec measureSpec,
395 LayoutLength padding,
396 LayoutLength childDimension )
398 auto specMode = measureSpec.GetMode();
399 LayoutLength specSize = measureSpec.GetSize();
401 LayoutLength size = std::max( LayoutLength(0), specSize - padding ); // reduce available size by the owners padding
403 LayoutLength resultSize = 0;
404 MeasureSpec::Mode resultMode = MeasureSpec::Mode::UNSPECIFIED;
408 // Parent has imposed an exact size on us
409 case MeasureSpec::Mode::EXACTLY:
411 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::EXACTLY\n");
412 if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
414 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension MATCH_PARENT\n");
416 // Child wants to be our size. So be it.
418 resultMode = MeasureSpec::Mode::EXACTLY;
420 else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
422 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension WRAP_CONTENT\n");
424 // Child wants to determine its own size. It can't be
427 resultMode = MeasureSpec::Mode::AT_MOST;
431 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension UNSPECIFIED\n");
432 resultSize = childDimension;
433 resultMode = MeasureSpec::Mode::EXACTLY;
439 // Parent has imposed a maximum size on us
440 case MeasureSpec::Mode::AT_MOST:
442 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::AT_MOST\n");
443 if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
445 // Child wants to be our size, but our size is not fixed.
446 // Constrain child to not be bigger than us.
448 resultMode = MeasureSpec::Mode::AT_MOST;
450 else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
452 // Child wants to determine its own size. It can't be
455 resultMode = MeasureSpec::Mode::AT_MOST;
459 // Child wants a specific size... so be it
460 resultSize = childDimension + padding;
461 resultMode = MeasureSpec::Mode::EXACTLY;
467 // Parent asked to see how big we want to be
468 case MeasureSpec::Mode::UNSPECIFIED:
470 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::UNSPECIFIED\n");
472 if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
474 // Child wants to be our size... find out how big it should be
475 resultSize = LayoutItem::Impl::sUseZeroUnspecifiedMeasureSpec ? LayoutLength(0) : size;
476 resultMode = MeasureSpec::Mode::UNSPECIFIED;
478 else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
480 // Child wants to determine its own size.... find out how big
482 resultSize = LayoutItem::Impl::sUseZeroUnspecifiedMeasureSpec ? LayoutLength(0) : size;
483 resultMode = MeasureSpec::Mode::UNSPECIFIED;
487 // Child wants a specific size... let him have it
488 resultSize = childDimension + padding;
489 resultMode = MeasureSpec::Mode::EXACTLY;
495 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec resultSize(" << resultSize << ")\n" );
497 //noinspection ResourceType
498 return MeasureSpec( resultSize, resultMode );
502 void LayoutGroup::OnInitialize()
504 auto control = Toolkit::Control::DownCast( GetOwner() );
508 // Take ownership of existing children
509 for( unsigned int childIndex = 0 ; childIndex < control.GetChildCount(); ++childIndex )
511 ChildAddedToOwnerImpl( control.GetChildAt( childIndex ) );
514 DevelActor::ChildAddedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildAddedToOwner );
515 DevelActor::ChildRemovedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildRemovedFromOwner );
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 DevelHandle::PropertySetSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::OnOwnerPropertySet );
578 void LayoutGroup::RemoveChild( LayoutItem& item )
580 item.SetParent( nullptr );
581 OnChildRemove( item );
584 void LayoutGroup::ChildAddedToOwner( Actor child )
586 ChildAddedToOwnerImpl( child );
587 RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_CHILD_ADD, child, Actor() );
590 void LayoutGroup::ChildAddedToOwnerImpl( Actor child )
592 LayoutItemPtr childLayout;
593 Toolkit::Control control = Toolkit::Control::DownCast( child );
595 #if defined(DEBUG_ENABLED)
596 auto parent = Toolkit::Control::DownCast( GetOwner() );
597 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child control(%s) owner control(%s)\n",
598 control?control.GetName().c_str():"Invalid",
599 parent?parent.GetName().c_str():"Invalid" );
602 if( control ) // Can only support adding Controls, not Actors to layout
604 Internal::Control& childControlImpl = GetImplementation( control );
605 Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
606 childLayout = childControlDataImpl.GetLayout();
610 // If the child doesn't already have a layout, then create a LayoutItem or LayoutGroup for it.
611 // If control behaviour flag set to Layout then set a LayoutGroup.
612 if( DevelControl::IsLayoutingRequired( control ) )
614 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutGroup for control:%s\n",
615 control?control.GetName().c_str():"Invalid" );
616 childLayout = LayoutGroup::New( control );
620 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutItem for control:%s\n",
621 control?control.GetName().c_str():"Invalid" );
622 childLayout = LayoutItem::New( control );
623 childLayout->SetAnimateLayout( IsLayoutAnimated() ); // forces animation inheritance.
626 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child control:" << control.GetName() <<
627 " desiredWidth: " << control.GetNaturalSize().width <<
628 " desiredHeight:" << control.GetNaturalSize().height );
630 childControlDataImpl.SetLayout( *childLayout.Get() );
632 Vector3 size = child.GetTargetSize();
633 // If the size of the control is set explicitly make sure that the control size
634 // stays the same after the layout except it is over written with match parent specs.
637 childLayout->SetMinimumWidth( size.x );
642 childLayout->SetMinimumHeight( size.y );
644 // Default layout data will be generated by Add().
648 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child(%s) already has a Layout\n", control.GetName().c_str() );
649 LayoutGroupPtr layoutGroup( dynamic_cast< LayoutGroup* >( childLayout.Get() ) );
652 // Set only in case of leaf children
653 childLayout->SetAnimateLayout( IsLayoutAnimated() );
657 Add( *childLayout.Get() );
661 void LayoutGroup::ChildRemovedFromOwner( Actor child )
663 Toolkit::Control control = Toolkit::Control::DownCast( child );
666 Internal::Control& childControlImpl = GetImplementation( control );
667 Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
668 auto childLayout = childControlDataImpl.GetLayout();
671 Remove( *childLayout.Get() );
672 RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_CHILD_REMOVE, child, Actor() );
677 void LayoutGroup::ChildOrderChanged( Actor child )
679 Toolkit::Control childControl = Toolkit::Control::DownCast( child );
682 Internal::Control& childControlImpl = GetImplementation( childControl );
683 Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
685 auto childLayout = childControlDataImpl.GetLayout();
688 Toolkit::Control control = Toolkit::Control::DownCast( GetOwner() );
689 unsigned int count = control.GetChildCount();
690 unsigned int index = static_cast< unsigned int >( childControl.GetProperty< int >( DevelActor::Property::SIBLING_ORDER ) );
692 // Find insertion position
693 while( ++index < count )
695 auto sibling = Toolkit::Control::DownCast( control.GetChildAt( index ) );
698 auto siblingLayout = DevelControl::GetLayout( sibling );
701 Internal::LayoutItem& siblingLayoutImpl = GetImplementation( siblingLayout );
702 Move( siblingLayoutImpl, *childLayout );
708 MoveBack( *childLayout );
713 void LayoutGroup::OnOwnerPropertySet( Handle& handle, Property::Index index, Property::Value value )
715 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LayoutGroup::OnOwnerPropertySet\n");
716 auto actor = Actor::DownCast( handle );
719 index == Actor::Property::LAYOUT_DIRECTION ||
720 index == Toolkit::Control::Property::PADDING ||
721 index == Toolkit::Control::Property::MARGIN
729 void LayoutGroup::OnAnimationStateChanged( bool animateLayout )
731 // Change children's animation state
732 for( auto&& child : mImpl->mChildren )
734 LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( child.child.Get() ) );
737 // Change state only in case of leaf children
738 child.child->SetAnimateLayout( animateLayout );
743 void LayoutGroup::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
745 auto childCount = GetChildCount();
747 DALI_LOG_STREAM( gLogFilter, Debug::Verbose,
748 "LayoutGroup::OnMeasure Actor Id:" << Actor::DownCast(GetOwner()).GetId() <<
749 " Owner:" << Actor::DownCast(GetOwner()).GetName() <<
750 " Child Count:" << childCount <<
751 " MeasureSpecs( width:"<<widthMeasureSpec<<", height:"<<heightMeasureSpec );
753 auto widthMode = widthMeasureSpec.GetMode();
754 auto heightMode = heightMeasureSpec.GetMode();
755 LayoutLength widthSpecSize = widthMeasureSpec.GetSize();
756 LayoutLength heightSpecSize = heightMeasureSpec.GetSize();
758 bool exactWidth ( false );
759 bool exactHeight ( false );
761 // Layouting behaviour
762 // EXACT, width and height as provided.
763 // MATCH_PARENT, width and height that of parent
764 // WRAP_CONTENT, take width of widest child and height size of longest child (within given limit)
765 // UNSPECIFIED, take width of widest child and height size of longest child.
767 LayoutLength layoutWidth( 0 );
768 LayoutLength layoutHeight( 0 );
770 // If LayoutGroup has children then measure children to get max dimensions
771 if ( childCount > 0 )
773 for( unsigned int i=0; i<childCount; ++i )
775 auto childLayout = GetChildAt( i );
778 auto childControl = Toolkit::Control::DownCast(childLayout->GetOwner());
780 // If child control has children check if a ResizePolicy is set on it. A LayoutItem could be a legacy container.
781 // A legacy container would need it's ResizePolicy to be applied as a MeasureSpec.
783 // Check below will be true for legacy containers and controls with layout required set.
784 // Other layouts will have their own OnMeasure (a checked requirement) hence not execute LayoutGroup::OnMeasure.
785 // Controls which have set layout required will not be legacy controls hence should not have a ResizePolicy set.
786 // Only need to map the resize policy the first time as the Layouting system will then set it to FIXED.
787 if( childControl.GetChildCount() > 0 && ! mImpl->mResizePolicyMapped )
789 // First pass, Static mappings that are not dependant on parent
790 SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::WIDTH );
791 SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::HEIGHT );
792 mImpl->mResizePolicyMapped = true;
795 // Second pass, if any mappings were not possible due to parent size dependancies then calculate an exact desired size for child
796 if( true == childLayout->IsResizePolicyRequired() ) // No need to test child count as this flag would only be set if control had children.
798 // Get last stored width and height specifications for the child
799 LayoutLength desiredWidth = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
800 LayoutLength desiredHeight = childControl.GetProperty<float>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
802 DALI_LOG_INFO( gLogFilter, Debug::General, "LayoutGroup::MeasureChild Initial desired size pre ResizePolicy(%f,%f)\n", desiredWidth.AsInteger(), desiredHeight.AsInteger() );
804 childLayout->SetResizePolicyRequired( false ); // clear flag incase in case of changes before next Measure
805 SizeNegotiationMapper::GetSizeofChildForParentDependentResizePolicy( childControl, widthMeasureSpec, heightMeasureSpec, desiredWidth, desiredHeight );
807 // Parent dependant ResizePolicies become exact sizes so are now set on the child before it's measured.
808 childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, desiredWidth.AsInteger() );
809 childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, desiredHeight.AsInteger() );
811 DALI_LOG_INFO( gLogFilter, Debug::General, " LayoutGroup::OnMeasure ResizePolicy Required resulting size(%f,%f)\n", desiredWidth.AsInteger(), desiredHeight.AsInteger() );
815 MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
816 LayoutLength childWidth = childLayout->GetMeasuredWidth();
817 LayoutLength childHeight = childLayout->GetMeasuredHeight();
819 Extents childMargin = childLayout->GetMargin();
820 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure child " << childControl.GetName().c_str() << " width[" << childWidth << "] height[" << childHeight << "]\n" );
822 layoutWidth = std::max( layoutWidth, childWidth + childMargin.start + childMargin.end );
823 layoutHeight = std::max( layoutHeight, childHeight + childMargin.top + childMargin.bottom );
824 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure calculated child width[" << layoutWidth << "] height[" << layoutHeight << "]\n" );
828 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Not a layout\n" );
832 Extents padding = GetPadding();
833 layoutWidth += padding.start + padding.end;
834 layoutHeight += padding.top + padding.bottom;
838 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Getting default size as a leaf\n" );
839 // LayoutGroup does not contain any children so must be a leaf
840 layoutWidth = GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec );
841 layoutHeight = GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec );
844 // Can't exceed specified width
845 if( widthMode == MeasureSpec::Mode::EXACTLY )
849 else if ( widthMode == MeasureSpec::Mode::AT_MOST )
851 layoutWidth = std::min( layoutWidth, widthSpecSize );
854 // Can't exceed specified height
855 if( heightMode == MeasureSpec::Mode::EXACTLY )
859 else if ( heightMode == MeasureSpec::Mode::AT_MOST )
861 layoutHeight = std::min( layoutHeight, heightSpecSize );
864 layoutWidth = std::max( layoutWidth, GetSuggestedMinimumWidth() );
865 layoutHeight = std::max( layoutHeight, GetSuggestedMinimumHeight() );
869 layoutWidth = widthSpecSize;
874 layoutHeight = heightSpecSize;
877 DALI_LOG_STREAM( gLogFilter, Debug::General, "LayoutGroup::OnMeasure Measured size(" << layoutWidth << "," << layoutHeight << ") for : " << Actor::DownCast(GetOwner()).GetName() << " \n" );
878 SetMeasuredDimensions( MeasuredSize( layoutWidth ), MeasuredSize( layoutHeight ) );
881 void LayoutGroup::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
883 auto count = GetChildCount();
885 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup OnLayout owner:" << ( ( Toolkit::Control::DownCast(GetOwner())) ? Toolkit::Control::DownCast(GetOwner()).GetName() : "invalid" ) << " childCount:" << count );
887 for( unsigned int childIndex = 0; childIndex < count; childIndex++)
889 LayoutItemPtr childLayout = GetChildAt( childIndex );
890 if( childLayout != nullptr )
893 auto childOwner = childLayout->GetOwner();
894 LayoutLength childWidth = childLayout->GetMeasuredWidth();
895 LayoutLength childHeight = childLayout->GetMeasuredHeight();
896 Extents childMargin = childLayout->GetMargin();
897 auto control = Toolkit::Control::DownCast( childOwner );
898 Extents padding = GetPadding();
900 auto childPosition = control.GetProperty< Vector3 >( Actor::Property::POSITION );
901 auto anchorPoint = control.GetProperty< Vector3 >( Actor::Property::ANCHOR_POINT );
903 DALI_LOG_STREAM( gLogFilter, Debug::General, "LayoutGroup::OnLayout child[" << control.GetName() <<
904 "] position(" << childPosition << ") child width[" << childWidth << "] height[" << childHeight << "]\n" );
906 // Margin and Padding only supported when child anchor point is TOP_LEFT.
907 int paddingAndMarginOffsetX = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.top + childMargin.top ) : 0;
908 int paddingAndMarginOffsetY = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.start + childMargin.start ) : 0;
909 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnLayout paddingMargin offset(%d,%d)\n", paddingAndMarginOffsetX, paddingAndMarginOffsetY );
911 LayoutLength childLeft = childPosition.x + paddingAndMarginOffsetX;
912 LayoutLength childTop = childPosition.y + paddingAndMarginOffsetY;
914 childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
920 } // namespace Internal
921 } // namespace Toolkit