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>
33 #if defined(DEBUG_ENABLED)
34 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
45 LayoutGroup::LayoutGroup()
46 : mImpl( new LayoutGroup::Impl() ),
51 LayoutGroup::~LayoutGroup()
53 // An object with a unique_ptr to an opaque structure must define it's destructor in the translation unit
54 // where the opaque structure is defined. It cannot use the default method in the header file.
57 Toolkit::LayoutGroup::LayoutId LayoutGroup::Add( LayoutItem& child )
59 LayoutParent* oldParent = child.GetParent();
62 LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( oldParent ) );
65 parentGroup->Remove( child );
69 Impl::ChildLayout childLayout;
70 childLayout.layoutId = mImpl->mNextLayoutId++;
71 childLayout.child = &child;
72 mImpl->mChildren.emplace_back( childLayout );
74 child.SetParent( this );
76 auto owner = child.GetOwner();
78 // If the owner does not have any LayoutItem child properties, add them
79 if( ! DevelHandle::DoesCustomPropertyExist( owner, Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) )
81 // Set default properties for LayoutGroup and LayoutItem.
82 // Deriving classes can override OnChildAdd() to add their own default properties
83 GenerateDefaultChildPropertyValues( owner );
86 // Inform deriving classes that this child has been added
87 OnChildAdd( *childLayout.child.Get() );
89 // Now listen to future changes to the child properties.
90 DevelHandle::PropertySetSignal(owner).Connect( this, &LayoutGroup::OnSetChildProperties );
94 return childLayout.layoutId;
97 void LayoutGroup::Remove( Toolkit::LayoutGroup::LayoutId childId )
99 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
101 if( iter->layoutId == childId )
103 OnChildRemove( *iter->child.Get() );
104 mImpl->mChildren.erase(iter);
111 void LayoutGroup::Remove( LayoutItem& child )
113 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ++iter )
115 if( iter->child.Get() == &child )
117 OnChildRemove( *iter->child.Get() );
118 mImpl->mChildren.erase(iter);
125 void LayoutGroup::RemoveAll()
127 for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; )
129 OnChildRemove( *iter->child.Get() );
130 iter = mImpl->mChildren.erase(iter);
134 unsigned int LayoutGroup::GetChildCount() const
136 return mImpl->mChildren.size();
139 LayoutItemPtr LayoutGroup::GetChildAt( unsigned int index ) const
141 DALI_ASSERT_ALWAYS( index < mImpl->mChildren.size() );
142 return mImpl->mChildren[ index ].child;
145 LayoutItemPtr LayoutGroup::GetChild( Toolkit::LayoutGroup::LayoutId childId ) const
147 for( auto&& childLayout : mImpl->mChildren )
149 if( childLayout.layoutId == childId )
151 return childLayout.child;
157 Toolkit::LayoutGroup::LayoutId LayoutGroup::GetChildId( LayoutItem& child ) const
159 for( auto&& childLayout : mImpl->mChildren )
161 if( childLayout.child.Get() == &child )
163 return childLayout.layoutId;
166 return Toolkit::LayoutGroup::UNKNOWN_ID;
169 void LayoutGroup::OnChildAdd( LayoutItem& child )
173 void LayoutGroup::OnChildRemove( LayoutItem& child )
177 void LayoutGroup::DoInitialize()
181 void LayoutGroup::DoRegisterChildProperties( const std::string& containerType )
185 void LayoutGroup::OnSetChildProperties( Handle& handle, Property::Index index, Property::Value value )
187 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnSetChildProperties\n");
189 if ( ( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) &&
190 ( index <= CHILD_PROPERTY_REGISTRATION_MAX_INDEX ) )
192 ( index == Toolkit::Control::Property::MARGIN || index == Toolkit::Control::Property::PADDING ) )
194 // If any child properties are set, must perform relayout
196 for( auto&& child : mImpl->mChildren )
198 if( child.child->GetOwner() == handle )
200 child.child->SetLayoutRequested();
207 void LayoutGroup::GenerateDefaultChildPropertyValues( Handle child )
209 child.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION,
210 Toolkit::ChildLayoutData::WRAP_CONTENT );
211 child.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,
212 Toolkit::ChildLayoutData::WRAP_CONTENT );
215 void LayoutGroup::MeasureChildren( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
217 for( auto&& child : mImpl->mChildren )
219 //if( (child.mViewFlags & Impl::VISIBILITY_MASK) != Impl::GONE ) // Use owner visibility/enabled/ready
221 MeasureChild( child.child, widthMeasureSpec, heightMeasureSpec );
226 void LayoutGroup::MeasureChild( LayoutItemPtr child,
227 MeasureSpec parentWidthMeasureSpec,
228 MeasureSpec parentHeightMeasureSpec )
230 DALI_LOG_TRACE_METHOD( gLogFilter );
232 auto childOwner = child->GetOwner();
234 auto control = Toolkit::Control::DownCast( childOwner );
236 #if defined( DEBUG_ENABLED )
239 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChild natural size(%f, %f)\n", control.GetNaturalSize().width, control.GetNaturalSize().height );
243 // Get last stored width and height specifications for the child
244 auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
245 auto desiredHeight = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
246 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChild desiredWidth(%d) desiredHeight(%d)\n", desiredWidth, desiredHeight );
248 auto padding = GetPadding(); // Padding of this layout's owner, not of the child being measured.
250 const MeasureSpec childWidthMeasureSpec = GetChildMeasureSpec( parentWidthMeasureSpec,
251 padding.start + padding.end,
253 const MeasureSpec childHeightMeasureSpec = GetChildMeasureSpec( parentHeightMeasureSpec,
254 padding.top + padding.bottom,
257 child->Measure( childWidthMeasureSpec, childHeightMeasureSpec );
260 void LayoutGroup::MeasureChildWithMargins( LayoutItemPtr child,
261 MeasureSpec parentWidthMeasureSpec, LayoutLength widthUsed,
262 MeasureSpec parentHeightMeasureSpec, LayoutLength heightUsed)
264 auto childOwner = child->GetOwner();
265 auto desiredWidth = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION );
266 auto desiredHeight = childOwner.GetProperty<int>( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION );
268 auto padding = GetPadding(); // Padding of this layout's owner, not of the child being measured.
270 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins desiredWidth(%d)\n", desiredWidth );
272 MeasureSpec childWidthMeasureSpec = GetChildMeasureSpec( parentWidthMeasureSpec,
273 padding.start + padding.end +
274 widthUsed, desiredWidth );
276 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins desiredHeight(%d)\n", desiredHeight );
278 MeasureSpec childHeightMeasureSpec = GetChildMeasureSpec( parentHeightMeasureSpec,
279 padding.top + padding.bottom +
280 heightUsed, desiredHeight );
282 child->Measure( childWidthMeasureSpec, childHeightMeasureSpec );
286 MeasureSpec LayoutGroup::GetChildMeasureSpec(
287 MeasureSpec measureSpec,
288 LayoutLength padding,
289 LayoutLength childDimension )
291 auto specMode = measureSpec.GetMode();
292 LayoutLength specSize = measureSpec.GetSize();
294 auto size = std::max( LayoutLength(0), specSize - padding ); // reduce available size by the owners padding
296 MeasureSpec::IntType resultSize = 0;
297 MeasureSpec::Mode resultMode = MeasureSpec::Mode::UNSPECIFIED;
301 // Parent has imposed an exact size on us
302 case MeasureSpec::Mode::EXACTLY:
304 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::EXACTLY\n");
305 if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
307 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension MATCH_PARENT\n");
309 // Child wants to be our size. So be it.
311 resultMode = MeasureSpec::Mode::EXACTLY;
313 else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
315 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension WRAP_CONTENT\n");
317 // Child wants to determine its own size. It can't be
320 resultMode = MeasureSpec::Mode::AT_MOST;
324 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec childDimension UNSPECIFIED\n");
325 resultSize = childDimension;
326 resultMode = MeasureSpec::Mode::EXACTLY;
332 // Parent has imposed a maximum size on us
333 case MeasureSpec::Mode::AT_MOST:
335 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::AT_MOST\n");
336 if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
338 // Child wants to be our size, but our size is not fixed.
339 // Constrain child to not be bigger than us.
341 resultMode = MeasureSpec::Mode::AT_MOST;
343 else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
345 // Child wants to determine its own size. It can't be
348 resultMode = MeasureSpec::Mode::AT_MOST;
352 // Child wants a specific size... so be it
353 resultSize = childDimension + padding;
354 resultMode = MeasureSpec::Mode::EXACTLY;
360 // Parent asked to see how big we want to be
361 case MeasureSpec::Mode::UNSPECIFIED:
363 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec MeasureSpec::Mode::UNSPECIFIED\n");
365 if (childDimension == Toolkit::ChildLayoutData::MATCH_PARENT)
367 // Child wants to be our size... find out how big it should be
368 resultSize = LayoutItem::Impl::sUseZeroUnspecifiedMeasureSpec ? LayoutLength(0) : size;
369 resultMode = MeasureSpec::Mode::UNSPECIFIED;
371 else if (childDimension == Toolkit::ChildLayoutData::WRAP_CONTENT)
373 // Child wants to determine its own size.... find out how big
375 resultSize = LayoutItem::Impl::sUseZeroUnspecifiedMeasureSpec ? LayoutLength(0) : size;
376 resultMode = MeasureSpec::Mode::UNSPECIFIED;
380 // Child wants a specific size... let him have it
381 resultSize = childDimension + padding;
382 resultMode = MeasureSpec::Mode::EXACTLY;
388 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec resultSize(%u)\n", resultSize );
391 //noinspection ResourceType
392 return MeasureSpec( resultSize, resultMode );
396 void LayoutGroup::OnInitialize()
398 auto control = Toolkit::Control::DownCast( GetOwner() );
402 // Take ownership of existing children
403 for( unsigned int childIndex = 0 ; childIndex < control.GetChildCount(); ++childIndex )
405 ChildAddedToOwner( control.GetChildAt( childIndex ) );
408 DevelActor::ChildAddedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildAddedToOwner );
409 DevelActor::ChildRemovedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildRemovedFromOwner );
410 DevelHandle::PropertySetSignal( control ).Connect( mSlotDelegate, &LayoutGroup::OnOwnerPropertySet );
412 if( control.GetParent() )
414 auto parent = Toolkit::Control::DownCast( control.GetParent() );
417 auto parentLayout = Toolkit::LayoutGroup::DownCast( DevelControl::GetLayout( parent ) );
420 Internal::LayoutGroup& parentLayoutImpl = GetImplementation( parentLayout );
421 parentLayoutImpl.Add( *this );
428 void LayoutGroup::OnRegisterChildProperties( const std::string& containerType )
430 DoRegisterChildProperties( containerType );
433 void LayoutGroup::OnUnparent()
438 // Remove myself from parent
439 LayoutParent* parent = GetParent();
442 LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( parent ) );
445 parentGroup->Remove( *this );
450 void LayoutGroup::ChildAddedToOwner( Actor child )
452 LayoutItemPtr childLayout;
453 Toolkit::Control control = Toolkit::Control::DownCast( child );
454 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner(%s)\n", control.GetName().c_str() );
456 if( control ) // Can only support adding Controls, not Actors to layout
458 Internal::Control& childControlImpl = GetImplementation( control );
459 Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
460 childLayout = childControlDataImpl.GetLayout();
464 // If the child doesn't already have a layout, then create a LayoutItem for it.
465 childLayout = LayoutItem::New( control );
466 childLayout->SetAnimateLayout( IsLayoutAnimated() ); // @todo this essentially forces animation inheritance. Bad?
467 #if defined(DEBUG_ENABLED)
468 auto desiredSize = control.GetNaturalSize();
469 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner desiredSize(%f,%f) (naturalSize)\n", desiredSize.width, desiredSize.height );
471 childControlDataImpl.SetLayout( *childLayout.Get() );
473 // Default layout data will be generated by Add().
476 Add( *childLayout.Get() );
480 void LayoutGroup::ChildRemovedFromOwner( Actor child )
482 Toolkit::Control control = Toolkit::Control::DownCast( child );
485 Internal::Control& childControlImpl = GetImplementation( control );
486 Internal::Control::Impl& childControlDataImpl = Internal::Control::Impl::Get( childControlImpl );
487 auto childLayout = childControlDataImpl.GetLayout();
490 Remove( *childLayout.Get() );
495 void LayoutGroup::OnOwnerPropertySet( Handle& handle, Property::Index index, Property::Value value )
497 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LayoutGroup::OnOwnerPropertySet\n");
498 auto actor = Actor::DownCast( handle );
501 index == Actor::Property::LAYOUT_DIRECTION ||
502 index == Toolkit::Control::Property::PADDING ||
503 index == Toolkit::Control::Property::MARGIN
511 } // namespace Internal
512 } // namespace Toolkit